[med-svn] [python-mne] 34/52: ENH : allowing export of volume stc to full MRI resolution

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:23:47 UTC 2015


This is an automated email from the git hooks/post-receive script.

yoh pushed a commit to annotated tag v0.2
in repository python-mne.

commit 109a9c5c5f87f139515ef5bad342c48d580acad3
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Thu Oct 13 21:13:48 2011 -0400

    ENH : allowing export of volume stc to full MRI resolution
---
 .../inverse/plot_compute_mne_inverse_volume.py     | 10 +++++---
 mne/fiff/constants.py                              |  2 ++
 mne/source_estimate.py                             | 28 ++++++++++++++++++---
 mne/source_space.py                                | 29 ++++++++++++++++------
 4 files changed, 55 insertions(+), 14 deletions(-)

diff --git a/examples/inverse/plot_compute_mne_inverse_volume.py b/examples/inverse/plot_compute_mne_inverse_volume.py
index a87f658..884a43b 100644
--- a/examples/inverse/plot_compute_mne_inverse_volume.py
+++ b/examples/inverse/plot_compute_mne_inverse_volume.py
@@ -20,7 +20,6 @@ import mne
 from mne.datasets import sample
 from mne.fiff import Evoked
 from mne.minimum_norm import apply_inverse, read_inverse_operator
-from mne.source_space import read_source_spaces
 
 data_path = sample.data_path('..')
 fname_inv = data_path + '/MEG/sample/sample_audvis-meg-vol-7-meg-inv.fif'
@@ -41,13 +40,16 @@ stc = apply_inverse(evoked, inverse_operator, lambda2, dSPM)
 stc.crop(0.0, 0.2)
 
 # Save result in a 4D nifti file
-img = mne.save_stc_as_volume('mne_dSPM_inverse.nii', stc, src)
+img = mne.save_stc_as_volume('mne_dSPM_inverse.nii.gz', stc, src,
+          mri_resolution=False)  # set to True for full MRI resolution
 data = img.get_data()
 
-# plot result
+# plot result (one slice)
 coronal_slice = data[:, 10, :, 60]
+pl.close('all')
 pl.imshow(np.ma.masked_less(coronal_slice, 8), cmap=pl.cm.Reds,
-         interpolation='nearest')
+          interpolation='nearest')
+pl.colorbar()
 pl.contour(coronal_slice != 0, 1, colors=['black'])
 pl.xticks([])
 pl.yticks([])
diff --git a/mne/fiff/constants.py b/mne/fiff/constants.py
index 518b932..c46258d 100644
--- a/mne/fiff/constants.py
+++ b/mne/fiff/constants.py
@@ -187,6 +187,8 @@ FIFF.FIFF_MRI_WIDTH             = 2010
 FIFF.FIFF_MRI_WIDTH_M           = 2011
 FIFF.FIFF_MRI_HEIGHT            = 2012
 FIFF.FIFF_MRI_HEIGHT_M          = 2013
+FIFF.FIFF_MRI_DEPTH             = 2014
+FIFF.FIFF_MRI_DEPTH_M           = 2015
 #
 FIFF.FIFFV_MRI_PIXEL_BYTE       = 1
 FIFF.FIFFV_MRI_PIXEL_WORD       = 2
diff --git a/mne/source_estimate.py b/mne/source_estimate.py
index 11adfa3..f5076b6 100644
--- a/mne/source_estimate.py
+++ b/mne/source_estimate.py
@@ -567,7 +567,7 @@ def _get_ico_tris(grade):
     return ico['tris']
 
 
-def save_stc_as_volume(fname, stc, src, dest='mri'):
+def save_stc_as_volume(fname, stc, src, dest='mri', mri_resolution=False):
     """Save a volume source estimate in a nifti file
 
     Parameters
@@ -582,6 +582,10 @@ def save_stc_as_volume(fname, stc, src, dest='mri'):
         If 'mri' the volume is defined in the coordinate system of
         the original T1 image. If 'surf' the coordinate system
         of the FreeSurfer surface is used (Surface RAS).
+    mri_resolution: bool
+        It True the image is saved in MRI resolution.
+        WARNING: if you have many time points the file produced can be
+        huge.
 
     Returns
     -------
@@ -592,17 +596,35 @@ def save_stc_as_volume(fname, stc, src, dest='mri'):
         raise Exception('Only volume source estimates can be saved as '
                         'volumes')
 
-    shape = src[0]['shape']
     n_times = stc.data.shape[1]
+    shape = src[0]['shape']
     shape3d = (shape[2], shape[1], shape[0])
     shape = (n_times, shape[2], shape[1], shape[0])
     vol = np.zeros(shape)
     mask3d = src[0]['inuse'].reshape(shape3d).astype(np.bool)
 
+    if mri_resolution:
+        mri_shape3d = (src[0]['mri_height'], src[0]['mri_depth'],
+                       src[0]['mri_width'])
+        mri_shape = (n_times, src[0]['mri_height'], src[0]['mri_depth'],
+                     src[0]['mri_width'])
+        mri_vol = np.zeros(mri_shape)
+        interpolator = src[0]['interpolator']
+
     for k, v in enumerate(vol):
         v[mask3d] = stc.data[:, k]
+        if mri_resolution:
+            mri_vol[k] = (interpolator * v.ravel()).reshape(mri_shape3d)
+
+    if mri_resolution:
+        vol = mri_vol
+
     vol = vol.T
-    affine = src[0]['vox_mri_t']['trans'].copy()
+
+    if mri_resolution:
+        affine = src[0]['vox_mri_t']['trans'].copy()
+    else:
+        affine = src[0]['src_mri_t']['trans'].copy()
     if dest == 'mri':
         affine = np.dot(src[0]['mri_ras_t']['trans'], affine)
     affine[:3] *= 1e3
diff --git a/mne/source_space.py b/mne/source_space.py
index 9f78ee2..addfc5d 100644
--- a/mne/source_space.py
+++ b/mne/source_space.py
@@ -138,15 +138,12 @@ def _read_one_source_space(fid, this):
         if tag is not None:
             res['shape'] = tuple(tag.data)
 
-        tag = find_tag(fid, this, FIFF.FIFF_MNE_SOURCE_SPACE_INTERPOLATOR)
-        if tag is not None:
-            res['interpolator'] = tag.data
-
         tag = find_tag(fid, this, FIFF.FIFF_COORD_TRANS)
         if tag is not None:
-            res['mri_head_t'] = tag.data
+            res['src_mri_t'] = tag.data
 
-        for d in this['directory']:
+        mri = dir_tree_find(this, FIFF.FIFFB_MNE_PARENT_MRI_FILE)[0]
+        for d in mri['directory']:
             if d.kind == FIFF.FIFF_COORD_TRANS:
                 tag = read_tag(fid, d.pos)
                 trans = tag.data
@@ -155,10 +152,28 @@ def _read_one_source_space(fid, this):
                 if trans['to'] == FIFF.FIFFV_MNE_COORD_RAS:
                     res['mri_ras_t'] = tag.data
 
-        tag = find_tag(fid, this, FIFF.FIFF_MNE_SOURCE_SPACE_MRI_FILE)
+        tag = find_tag(fid, mri, FIFF.FIFF_MNE_SOURCE_SPACE_INTERPOLATOR)
+        if tag is not None:
+            res['interpolator'] = tag.data
+        else:
+            print "Interpolation matrix for MRI not found."
+
+        tag = find_tag(fid, mri, FIFF.FIFF_MNE_SOURCE_SPACE_MRI_FILE)
         if tag is not None:
             res['mri_file'] = tag.data
 
+        tag = find_tag(fid, mri, FIFF.FIFF_MRI_WIDTH)
+        if tag is not None:
+            res['mri_width'] = int(tag.data)
+
+        tag = find_tag(fid, mri, FIFF.FIFF_MRI_HEIGHT)
+        if tag is not None:
+            res['mri_height'] = int(tag.data)
+
+        tag = find_tag(fid, mri, FIFF.FIFF_MRI_DEPTH)
+        if tag is not None:
+            res['mri_depth'] = int(tag.data)
+
 
     tag = find_tag(fid, this, FIFF.FIFF_MNE_SOURCE_SPACE_NPOINTS)
     if tag is None:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git



More information about the debian-med-commit mailing list