In this notebook we explore the nature of NIfTI images.
We are currently working in the IPython notebook. The notebook contains "cells". The cells can contain text or code. This is the second cell in the notebook, and it is a text cell. You can see what kind of cell this is by navigating to the cell and then looking in the toolbar at the top of the page. The text cells are of type "Markdown".
Press Shift-Return to execute the contents and move down one cell.
# We always put these at the top of our files for compatibility with Python 3
# We always use "print('my string')" instead of "print 'my string'"
from __future__ import print_function
# We always want division to be floating point division, so
# 1 / 2 is 0.5 rather than 0
from __future__ import division
First we will have a look at Python strings. Here is a variable called my_string with value "PNA is hard but fair"
my_string = "PNA is hard but fair"
We can see what type of thing this variable contains (points to) using the type function:
type(my_string)
We can see how many characters the string has with the len function:
len(my_string)
# The first character of the string
print(my_string[0])
# The last character of the string (don't forget indexing starts at 0)
print(my_string[19])
Now we introduce string slicing. This is where you take some sequential characters from the string, using the colon (:) between the square brackets. The value before the colon is the index to the first character you want, and the value after the colon is the index to the character after the last character you want. It sounds strange, but you will get used to it...
# The first two characters of the string
print(my_string[0:2]) # from index 0 up to, but not including, 2
# The first 5 characters of the string
print(my_string[0:5]) # from index 0 up to, but not including, 5
We will go into more details on strings and slicing next week.
Now we will try loading an example image and seeing if we can understand the image data.
# This is a Python module (more next week)
import os # module for interacting with the operating system
If you want to explore modules or objects, type their name in a new cell, followed by a period, and press tab to see what functions or classes are available. Press 'b' to open up a new code cell below this one. Press Return to start editing the contents, and then type os. (os then a period) followed by a tab to see what functions are in the os module. Continue typing so you have os.getcwd, and then type ? followed by Shift-Return. This shows you the help for the os.getcwd function. You can close the help window by clicking on the thin horizontal grey bar.
# Your cell above this one
The image we are going to explore is in the notebook working directory
cwd = os.getcwd() # Get the current working directory (CWD)
print(cwd)
print(os.listdir(cwd)) # List files and directories in the current working directory
Let's read the image into memory.
fobj = open('highres001.nii', 'rb') # Open a file in Read Binary mode
print(fobj)
contents = fobj.read() # read all the characters into a variable in memory
How do I find out what type of object is attached to this variable called contents?
# here you will print out the type of `contents`
type(contents)
How big is this file in terms of bytes? Can you find out from the contents variable? (Hint: you want to know the length of contents).
# n_bytes = ?
n_bytes = len(contents)
If 1 mebibyte (MiB) (http://en.wikipedia.org/wiki/Megabyte) is size 1024 * 1024, what is the file size in MiB? (Hint - the right answer is between 0 and 100).
# n_mib = ?
n_mib = n_bytes / (1024 * 1024)
This is a NIfTI1 format file. That means that the first 352 bytes contains the "header" that describes the parameters of the image and the data following.
We want to print out the contents of the first 352 bytes of contents to have a look at it.
To do this, we are going to need string slicing to get the first 352 bytes:
# Here you print out the first 352 characters of `contents`
print(contents[:352])