Hardware:MRI Monitor

From Center for Cognitive Neuroscience
Revision as of 03:12, 16 January 2014 by Ccn admin (talk | contribs) (3 revisions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Our lab does concurrent EEG/fMRI experiments in the Staglin Center MR machine from time to time. MR machines involve the use of a superconductor cooled with liquid Helium. When these experiments take place, we turn off the pump that supplies the liquid Helium because the mechanical pump generate noise in the EEG data we are trying to collect. This slowly causes the temperature of the magnet to rise. If the pump is left off for too long and the magnet’s temperature rises too much, bad consequences ensue. So we would like to be able to make sure that we turn that pump back on as soon as we finish any experiments.

Enter the Raspberry Pi Linux box. A new low-cost embedded computing device, the Pi can offer us a full Debian-derived computer (Raspbian) that can sit in the machine room and measure the Helium pump power. It can then notify a web server when the power is turned off unexpectedly. We will use Python for the main program logic.


Raspberry Pi and Raspbian

The Raspberry Pi is a credit-card sized computer, complete with USB input, HDMI output, and ethernet web connection.

Raspbian is a free operating system based on Debian optimized for the Raspberry Pi hardware. Installing Raspbian on your Raspberry Pi is as simple as burning a downloaded disk image to an SD card, and inserting the card into the Raspberry Pi. When you turn the Pi on for the first time, it will guide you through a (brief) setup of the operating system. Make sure your monitor is connected to the Pi's HDMI port before booting up, so that it starts in graphical mode.

Input and Output

In order to use the Raspbian box for its intended purpose, we need to be able to read high or low values from the Raspberry Pi's pins. There are two well-established methods of doing so:

  1. RPi.GPIO
  2. WiringPi

RPi.GPIO

If you aren't concerned about needing superuser privileges to run your programs, then RPi.GPIO is for you. Run the following commands from the terminal in order to install it.

sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio

You can now read and write to the pins in Python by saying:

import RPi.GPIO as GPIO
GPIO.setup(12,GPIO.in)
GPIO.setup(13,GPIO.out)
input_value = GPIO.input(12)
GPIO.output(13,1)

For more information on how to read and write pins using RPi.GPIO, see the official Python page.

WiringPi

For security purposes, we preferred that the user account running the monitoring software not have superuser privileges. For this reason, we chose to use WiringPi rather than RPi.GPIO. WiringPi can be installed by following the instructions here. You can either install git and use it to retrieve the files, or download them yourself and build them directly. You can test your installation by running GPIO readall from the command line to check the status of all of the pins.

In order to use WiringPi with Python, run the following code:

sudo apt-get install python-dev
git clone https://github.com/WiringPi/WiringPi-Python.git
cd WiringPi-Python
git submodule update --init
sudo python setup.py install

You can now access the Raspberry Pi's pins in Python:

import wiringpi
io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_SYS)
io.pinMode(18,io.INPUT)
input = io.digitalRead(18)

However, running the command io.pinMode() requires superuser privileges when run within Python. However, you can set the pin modes outside of Python by running gpio export 18 in from the command line. This action does not require superuser privileges. So, using a combination of Bash scripting and Python scripting, you can create programs that make use of the Pi's input and output pins without the need for superusers.

For more information on how to set up WiringPi for use with Python, see Installing WiringPi and WiringPi with Python.

File System Navigation

The following series of tutorials provide a very basic introduction to file system navigation on unix like systems without any assumptions of prior knowledge on the topics.

  • Listing files & directories, making directories, changing directories, the . and .. directories, pathnames, the "home" directories
  • Copying files, moving files, removing files and directories, displaying the contents of a file, searching the contents of a file

List of Utilities Covered

  • ls
  • mkdir
  • cd
  • pwd
  • cp
  • mv
  • rm
  • cat
  • less
  • head & tail
  • grep


File & Shell Management

This is where your career on a UNIX type system can be made or crippled. Sure you know how to move around, list files, find out where you are, and display the contents of files. But now you have to do something with those files. And let's face it, there are a whole lot of files.

It is highly recommended that the reader look over Tutorial Four on how to use wildcards for matching before preceding with this section.

The good news is, using the above utilties we just learned about we can accomplish almost anything we want to do using a very handy utility called find.

As the name might imply, find, well, finds things. What it finds is up to you. find has many, many options. All laid out in its man page. However, for most purposes only a few are needed. We'll cover those here.

A basic find command looks like

$ find /path/to/directory -name 'filename.txt'

This command looks at all files in /path/to/directory and in all directories therein for a file named 'filename.txt'.

Common Options

-type
Specifies the type of file we're looking for. e.g. text file, directory, link, etc.
-name
Specifies the name of the file. Case Sensitive
-iname
Specifies the name of the file. Case Insensitive
-or
Joins the precedeing and following terms by the boolean OR
-and
Joins the preceding and following terms by the boolean AND
-not
negates the next term. e.g. -not -empty means "is not empty"
-exec
excutes a shell command for each file found. The only 'trick' is to replace the actual file name with {} and end the command with a \;. This should become clear when reviewing the examples below.
-empty
The file or directory is empty.

We can combine the above options to preform complex searches on the file system and, even better, execute commands on those search terms. For a list of all options and their arguments, please see Find Man Page.

Examples

  1. Find all directories named 'tsplot' in the current directory
     $ find . -name tsplot -type d 
  2. Find all empty directories in the directory /u/home9/foo/data
     $ find /u/home9/foo/data -empty 
  3. Find all empty files or directories named 'tsplot' in the current directory
     $ find . -name tsplot -or -type d -empty 

Using commands we've already learned to perform actions on the above.

  1. Find all directories named 'tsplot' in the current directory and delete them using rm
     $ find . -name tsplot -type d -exec rm -rf {} \; 
  2. Find all empty directories in the directory /u/home9/foo/data and delete them
     $ find . -type d -empty -exec rmdir {} \; # using rmdir for empty directories is safe as it will refuse to delete a directory with a file in it 
  3. Find all empty files or directories named 'tsplot' in the current directory and delete them
     $ find . -name tsplot -or -type d -empty -exec rm -rf {} \; 

But we're not restricted to rm, of course, we can use any of the utilities we've learned about as the -exec argument.

  1. Find all files named design.fsf and look for a subject named 'foo'
     $ find . -name design.fsf -type f -exec grep "foo" {} \; 

With a little reading of the man page, we find a new option called -user, which surprisingly finds all files that belong to the specified user

  1. Find all files owned by user 'foo' and change their permissions
     $ find . -type f -user foo -exec chmod -R ug+rwX {} \; 


Environment Variables

UNIX uses environment variables to pass information to various tools during a session. These variables are named in all capitals by convention. You can see all of the environment variables and their values by using the command

$ env

To simply see the value of one variable, you can echo its value

$ echo $VARIABLENAME

There are a few environment variables that you should be familiar with...

USER

This has the username of the current user, which should usually be you. See what the value is

$ echo $USER

HOME

This is the path to the home directory of the current user. The tilde symbol (~) is also recognized as shorthand for this home directory.

$ echo $HOME

PATH

This is a list of directories, separated by colons, in which the operating system should check for commands that you type. For instance, when you were using the ls, grep, or find commands in previous tutorials, the operating system started looking through the directories in your PATH variable to find the first command that matched that name and tried executing it. See the directories that the operating system will check for you by typing

$ echo $PATH

Making additions to this variable can be important. Let's say you have a personal set of scripts you have created and you store them in a directory ~/scripts. Every time you want to use one of those scripts, you have to type out the full path to it

$ ~/scripts/my-first-script.sh

or

$ $HOME/scripts/my-first-script.sh

This can get tiring. If you added your scripts directory to your path, you wouldn't have to type that extra bit every time. The best way to do this would be to edit your Bash Profile with a text editor. e.g.

$ vim ~/.bash_profile

and add the line

export PATH=$PATH:~/scripts

to the end of the file and saved it. This will "export" the variable named PATH to the environment and set its value equal to whatever was already in PATH plus the directory ~/scripts. The next time you login, you can do

$ echo $PATH

and see that at the end of the list of directories to search, your directory ~/scripts has been added. Now you can be anywhere in the filesystem and call the command

$ my-first-script.sh

to run that same script from before.

Collisions on the PATH

If you get in the habit of naming your scripts the same thing (e.g. my-script.sh) and placing them in different directories, you may run into a collision on your PATH. This is a case where you think you are running one script, but the operating system is actually running another. Let's look at an example.

Continuing from the previous example where we have the script ~/scripts/my-first-script.sh, let's say that we make another directory called analyze and we are working with some data there and make a processing script coincidentally called my-first-script.sh. So we have the files

~/scripts/my-first-script.sh
~/analyze/my-first-script.sh
~/analyze/data-file-1
~/analyze/data-file-2
...

And we have amended our .bash_profile so that ~/scripts is at the end of our PATH environment variable.

If we change to the analyze directory

$ cd ~/analyze

and wish to run the processing script my-first-script.sh on the data, you may think we can execute

$ my-first-script.sh

and call it a day. But this will actually run the file ~/scripts/my-first-script.sh because it is the first file the operating system found in the directories of PATH that matched that name. If you wanted to verify this, you can execute

$ which my-first-script.sh

This command will search your PATH variable for the first instance of my-first-script.sh and return the full path to it, something like this

~/scripts/my-first-script.sh

To run the script we had intended, we would need to execute

$ ./my-first-script.sh

The period and slash specify that the operating system should look in the current directory for this script.

If something seems weird or script isn't working, a good starting point is to check that you are running the script you think you are. Use which to find out


Learning to Fish or The Man Pages

However, possibly even more important is being able to find information on one's own. In the spirit of this, the instructional is concluded with a detailed coverage of the built-in unix manual pages. Virtually every program installed on a UNIX like system provied a manual page for reference. Even the most proficient of UNIX users could never retain the dizzying array of programs and their options in their working memory and with the built-in manual pages, they'll never need to.

The man pages (for manual) are the be all end all reference on UNIX systems. When reading a man page becomes natural, you know there is nothing you can't get done quickly and efficiently.

A Beginners Guide to man Pages is an excellent introduction into how to move around a man page easily and understand what it's telling you.


External Links