This is an automated email from the git hooks/post-receive script. yoh pushed a commit to annotated tag v0.1 in repository python-mne.
commit 9caac935b0d25c8a918b68210d600dc11c6a164e Author: Alexandre Gramfort <[email protected]> Date: Tue Aug 16 18:15:59 2011 -0400 ENH: adding transform_coordinates --- mne/__init__.py | 1 + mne/transforms.py | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/mne/__init__.py b/mne/__init__.py index 5a01bf5..92379c9 100644 --- a/mne/__init__.py +++ b/mne/__init__.py @@ -12,6 +12,7 @@ from .source_space import read_source_spaces from .epochs import Epochs from .label import label_time_courses, read_label from .misc import parse_config, read_reject_parameters +from .transforms import transform_coordinates import fiff import artifacts import stats diff --git a/mne/transforms.py b/mne/transforms.py index 2b07e49..8908e5a 100644 --- a/mne/transforms.py +++ b/mne/transforms.py @@ -2,7 +2,9 @@ import copy import numpy as np from scipy import linalg -# from .fiff import FIFF +from .fiff import FIFF +from .fiff.open import fiff_open +from .fiff.tag import read_tag def invert_transform(trans): @@ -53,6 +55,111 @@ def transform_source_space_to(src, dest, trans): return res +def transform_coordinates(filename, pos, orig, dest): + """Transform coordinates between various MRI-related coordinate frames + + Parameters + ---------- + filename: string + Name of a fif file containing the coordinate transformations + This file can be conveniently created with mne_collect_transforms + pos: array of shape N x 3 + array of locations to transform (in meters) + orig: 'meg' | 'mri' + Coordinate frame of the above locations. + 'meg' is MEG head coordinates + 'mri' surface RAS coordinates + dest: 'meg' | 'mri' | 'fs_tal' | 'mni_tal' + Coordinate frame of the result. + 'mni_tal' is MNI Talairach + 'fs_tal' is FreeSurfer Talairach + + Return + ------ + trans_pos: array of shape N x 3 + The transformed locations + + Example + ------- + >>> transform_coordinates('all-trans.fif', np.eye(3), 'meg', 'fs_tal') + >>> transform_coordinates('all-trans.fif', np.eye(3), 'mri', 'mni_tal') + """ + # Read the fif file containing all necessary transformations + fid, tree, directory = fiff_open(filename) + + coord_names = dict(mri=FIFF.FIFFV_COORD_MRI, + meg=FIFF.FIFFV_COORD_HEAD, + mni_tal=FIFF.FIFFV_MNE_COORD_MNI_TAL, + fs_tal=FIFF.FIFFV_MNE_COORD_FS_TAL) + + orig = coord_names[orig] + dest = coord_names[dest] + + T0 = T1 = T2 = T3plus = T3minus = None + for d in directory: + if d.kind == FIFF.FIFF_COORD_TRANS: + tag = read_tag(fid, d.pos) + trans = tag.data + if (trans['from'] == FIFF.FIFFV_COORD_MRI and + trans['to'] == FIFF.FIFFV_COORD_HEAD): + T0 = invert_transform(trans) + elif (trans['from'] == FIFF.FIFFV_COORD_MRI and + trans['to'] == FIFF.FIFFV_MNE_COORD_RAS): + T1 = trans + elif (trans['from'] == FIFF.FIFFV_MNE_COORD_RAS and + trans['to'] == FIFF.FIFFV_MNE_COORD_MNI_TAL): + T2 = trans + elif trans['from'] == FIFF.FIFFV_MNE_COORD_MNI_TAL: + if trans['to'] == FIFF.FIFFV_MNE_COORD_FS_TAL_GTZ: + T3plus = trans + elif trans['to'] == FIFF.FIFFV_MNE_COORD_FS_TAL_LTZ: + T3minus = trans + fid.close() + # + # Check we have everything we need + # + if ((orig == FIFF.FIFFV_COORD_HEAD and T0 is None) or (T1 is None) + or (T2 is None) or (dest == FIFF.FIFFV_MNE_COORD_FS_TAL and + ((T3minus is None) or (T3minus is None)))): + raise ValueError('All required coordinate transforms not found') + + # + # Go ahead and transform the data + # + if pos.shape[1] != 3: + raise ValueError('Coordinates must be given in a N x 3 array') + + if dest == orig: + trans_pos = pos.copy() + else: + n_points = pos.shape[0] + pos = np.c_[pos, np.ones(n_points)].T + if orig == FIFF.FIFFV_COORD_HEAD: + pos = np.dot(T0['trans'], pos) + elif orig != FIFF.FIFFV_COORD_MRI: + raise ValueError('Input data must be in MEG head or surface RAS ' + 'coordinates') + + if dest == FIFF.FIFFV_COORD_HEAD: + pos = np.dot(linalg.inv(T0['trans']), pos) + elif dest != FIFF.FIFFV_COORD_MRI: + pos = np.dot(np.dot(T2['trans'], T1['trans']), pos) + if dest != FIFF.FIFFV_MNE_COORD_MNI_TAL: + if dest == FIFF.FIFFV_MNE_COORD_FS_TAL: + for k in xrange(n_points): + if pos[2, k] > 0: + pos[:, k] = np.dot(T3plus['trans'], pos[:, k]) + else: + pos[:, k] = np.dot(T3minus['trans'], pos[:, k]) + else: + raise ValueError('Illegal choice for the output ' + 'coordinates') + + trans_pos = pos[:3, :].T + + return trans_pos + + # def transform_meg_chs(chs, trans): # """ # % -- 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
