Reading AFNI BRIK & HEAD files in Python.


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
import afni

In [3]:
fname = "TT_N27+tlrc" # make sure you have downloaded (and g-unzipped it)
hdr,dat = afni.read(fname)

In [4]:
dat.shape


Out[4]:
(161, 191, 151, 1)

In [5]:
f, axarr = plt.subplots(1, 3, figsize=(12,5.5))
axarr[0].imshow(dat[50,:,:,0].T,origin='lower',cmap='gray')
axarr[0].set_title("x=50")
axarr[1].imshow(dat[:,50,:,0].T,origin='lower',cmap='gray')
axarr[1].set_title("y=50")
axarr[2].imshow(dat[:,:,50,0].T,origin='lower',cmap='gray')
axarr[2].set_title("z=50")


Out[5]:
<matplotlib.text.Text at 0x7ff8a7fa2128>

Show one slice from an EPI image.


In [6]:
#header["BRICK_TYPES"]
fname = "pb05.s04.pre.r01.blur+tlrc"
hdr,dat = afni.read(fname)

In [7]:
f, axarr = plt.subplots(1, 3, figsize=(12,5.5))
tr = 10
axarr[0].imshow(dat[50,:,:,tr].T,origin='lower')
axarr[0].set_title("x=50")
axarr[1].imshow(dat[:,50,:,tr].T,origin='lower')
axarr[1].set_title("y=50")
axarr[2].imshow(dat[:,:,50,tr].T,origin='lower')
axarr[2].set_title("z=50")


Out[7]:
<matplotlib.text.Text at 0x7ff8a7e837b8>

Plot a time series of one voxel.


In [8]:
ts = dat[50,50,50,:]
plot(ts)
xlabel("EPI Volume")
ylabel("Signal")


Out[8]:
<matplotlib.text.Text at 0x7ff8a7e1c048>

In [9]:
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets

In [10]:
def plotslices(x,y,z,t):
    
    f, axarr = plt.subplots(1, 3, figsize=(15,6.5))

    axarr[0].imshow(dat[x,:,:,t].T,origin='lower')
    axarr[0].set_title("x=%i"%x)
    axarr[1].imshow(dat[:,y,:,t].T,origin='lower')
    axarr[1].set_title("y=%i"%y)
    axarr[2].imshow(dat[:,:,z,t].T,origin='lower')
    axarr[2].set_title("z=%i"%z)


interact_manual(plotslices, 
                x=widgets.IntSlider(min=0,max=dat.shape[0]-1,step=1,value=50),
                y=widgets.IntSlider(min=0,max=dat.shape[1]-1,step=1,value=50),
                z=widgets.IntSlider(min=0,max=dat.shape[2]-1,step=1,value=50),
                t=widgets.IntSlider(min=0,max=dat.shape[3]-1,step=1,value=0));


Widget Javascript not detected.  It may not be installed properly. Did you enable the widgetsnbextension? If not, then run "jupyter nbextension enable --py --sys-prefix widgetsnbextension"