This viewer is done is matplotlib http://matplotlib.sourceforge.net/. While a bit on the slow side, it gets the jobs done for coregistration and viewing mri.
Requires the 'Nifti' module http://niftilib.sourceforge.net/pynifti/.
Note: REQUIRES MRI TO BE ORIENT 0. This means dimension1=Right to Left, dim2= Posterior to Anterior, dim3= Inferior to Superior
from mri import img, viewmri
from numpy import flipud
mri=img.read('/home/danc/python/data/standardmri/ch2b.img')
viewmri.display(mri)
This is an interactive pylab based display of the mri. Click anywhere in any of the 3 series, and the 2 other views will redisplay to match that position.

Creation of source space from MRI
In practice what we want to do is create a source space as reduced as possible to save on computation. To do this, we want to use a complete mri to produce the transformation and skull stripped mri to create a source space.
lets load the non-stripped mri (and we can use any mri, because later we use points from the patients headshape file to do a linear transform of this mri into subject space)
from mri import img, viewmri, transform
from pdf2py import channel
nim = img.read('/home/danc/data/standardmri/ch2b.img')
Coregistration can be accomplished by using the mriviewer, and clicking on each of the three fiducials (nas, lpa, rpa). You will see the index points of each click in the command window. Write these down for the next step.
Now we load the stripped brain and return the decimated untransformed source space.
Now, if we wanted to decimate the mri, we would use the method in the module img, called decimate.
This decimates the mri and returns a less than original resolution mri file. Why? Because we will use this result to construct a source space and if the original mri is highres, say 1mm cubed, than beamforming or other multigrid source space calculations might take a really long time.
Say we want to decimate by a factor of 10 from the original 1mm X 1mm X 1mm mri.
Use the img.decimate function to return, mrixyz (the positions of the non-zero voxels in the mri) and mrid (which is that actual decimated image).
nimstripped = img.read('/home/danc/data/standardmri/ch2b_brain.img')
dec = img.decimate(nimstripped, 10)
Now how many gridpoints of non-zero values were there in the mri?
(3, 255). So 255 evenly spaced gridpoints exist in the stripped mri that have non-zero values.
Now were ready to transform this subspace into the patients space.
First, input the fiducial points. Second, create the transform. Third, apply the transform to the mri source space (mrixyz). And lastly, linearly scale the newly created patient frame source space (megxyz) by the headshape points (this minimizes big/small head effects).
rpa=[5,99,27]
lpa=[174,102,22]
nas=[91, 209, 31]
[t,r] = transform.meg2mri(lpa,rpa,nas)
megxyz = transform.mri2meg(t,r,dec.mrixyz)
datapdf = ('/home/danc/vault/decrypted/programming/python/data/0611SEF/e,rfhp1.0Hz,n,x,baha001-1SEF,f50lp')
scaledmegxyz = transform.scalesourcespace(datapdf, megxyz)
Lastly if we want to calculate the leadfield for each of these gridpoints
from meg import leadfield
ch = channel.index(datapdf, 'meg')
lf = leadfield.calc(datapdf, ch, scaledmegxyz)
Lets double check the leadfield size.
returns
(255, 244, 3). Which translates into 255 gridpoints, 244 channels, and 3 directional vectors.
We can now use this leadfield to do some source solutions with real data. pymeg-matrix-projection