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 efb94008cbfa67bab91b8401fb34bad56046e0eb Author: Alexandre Gramfort <[email protected]> Date: Thu Oct 13 10:58:20 2011 -0400 ENH : adding support for nifti output + nice example --- .../inverse/plot_compute_mne_inverse_volume.py | 54 ++++++++++++++++++++++ mne/__init__.py | 3 +- mne/source_estimate.py | 53 +++++++++++++++++++++ mne/source_space.py | 11 ++++- 4 files changed, 119 insertions(+), 2 deletions(-) diff --git a/examples/inverse/plot_compute_mne_inverse_volume.py b/examples/inverse/plot_compute_mne_inverse_volume.py new file mode 100644 index 0000000..a87f658 --- /dev/null +++ b/examples/inverse/plot_compute_mne_inverse_volume.py @@ -0,0 +1,54 @@ +""" +======================================================================= +Compute MNE-dSPM inverse solution on evoked data in volume source space +======================================================================= + +Compute dSPM inverse solution on MNE evoked dataset in a volume source +space and stores the solution in a nifti file for visualisation. + +""" + +# Author: Alexandre Gramfort <[email protected]> +# +# License: BSD (3-clause) + +print __doc__ + +import numpy as np +import pylab as pl +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' +fname_evoked = data_path + '/MEG/sample/sample_audvis-ave.fif' + +setno = 0 +snr = 3.0 +lambda2 = 1.0 / snr ** 2 +dSPM = True + +# Load data +evoked = Evoked(fname_evoked, setno=setno, baseline=(None, 0)) +inverse_operator = read_inverse_operator(fname_inv) +src = inverse_operator['src'] + +# Compute inverse solution +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) +data = img.get_data() + +# plot result +coronal_slice = data[:, 10, :, 60] +pl.imshow(np.ma.masked_less(coronal_slice, 8), cmap=pl.cm.Reds, + interpolation='nearest') +pl.contour(coronal_slice != 0, 1, colors=['black']) +pl.xticks([]) +pl.yticks([]) +pl.show() diff --git a/mne/__init__.py b/mne/__init__.py index 92379c9..e2e11d5 100644 --- a/mne/__init__.py +++ b/mne/__init__.py @@ -6,7 +6,8 @@ from .event import read_events, write_events, find_events, merge_events from .forward import read_forward_solution from .source_estimate import read_stc, write_stc, SourceEstimate, morph_data, \ spatio_temporal_src_connectivity, \ - spatio_temporal_tris_connectivity + spatio_temporal_tris_connectivity, \ + save_stc_as_volume from .surface import read_bem_surfaces, read_surface from .source_space import read_source_spaces from .epochs import Epochs diff --git a/mne/source_estimate.py b/mne/source_estimate.py index 6d23912..11adfa3 100644 --- a/mne/source_estimate.py +++ b/mne/source_estimate.py @@ -565,3 +565,56 @@ def _get_ico_tris(grade): ico = s break return ico['tris'] + + +def save_stc_as_volume(fname, stc, src, dest='mri'): + """Save a volume source estimate in a nifti file + + Parameters + ---------- + fname: string + The name of the generated nifti file. + stc: instance of SourceEstimate + The source estimate + src: list + The list of source spaces (should actually be of length 1) + dest: 'mri' | 'surf' + 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). + + Returns + ------- + img : instance Nifti1Image + The image object. + """ + if stc.is_surface(): + raise Exception('Only volume source estimates can be saved as ' + 'volumes') + + shape = src[0]['shape'] + n_times = stc.data.shape[1] + 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) + + for k, v in enumerate(vol): + v[mask3d] = stc.data[:, k] + vol = vol.T + affine = src[0]['vox_mri_t']['trans'].copy() + if dest == 'mri': + affine = np.dot(src[0]['mri_ras_t']['trans'], affine) + affine[:3] *= 1e3 + + try: + import nibabel as nib # lazy import to avoid dependency + except ImportError: + raise ImportError("nibabel is required to save volume images.") + + header = nib.nifti1.Nifti1Header() + header.set_xyzt_units('mm', 'msec') + header['pixdim'][4] = 1e3 * stc.tstep + img = nib.Nifti1Image(vol, affine, header=header) + nib.save(img, fname) + return img diff --git a/mne/source_space.py b/mne/source_space.py index d9bca0e..9f78ee2 100644 --- a/mne/source_space.py +++ b/mne/source_space.py @@ -7,7 +7,7 @@ import numpy as np from .fiff.constants import FIFF from .fiff.tree import dir_tree_find -from .fiff.tag import find_tag +from .fiff.tag import find_tag, read_tag from .fiff.open import fiff_open @@ -146,6 +146,15 @@ def _read_one_source_space(fid, this): if tag is not None: res['mri_head_t'] = tag.data + for d in this['directory']: + if d.kind == FIFF.FIFF_COORD_TRANS: + tag = read_tag(fid, d.pos) + trans = tag.data + if trans['from'] == FIFF.FIFFV_MNE_COORD_MRI_VOXEL: + res['vox_mri_t'] = tag.data + 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) if tag is not None: res['mri_file'] = tag.data -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git _______________________________________________ debian-med-commit mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/debian-med-commit
