[med-svn] [python-mne] 302/376: ENH : first support for CTF data

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


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 ef97d5164ba98f43c931764ec8d4fd041dde30b1
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Mon Jun 13 17:11:57 2011 -0400

    ENH : first support for CTF data
---
 mne/fiff/ctf.py       | 228 +++++++++++++++++++++++++-------------------------
 mne/fiff/meas_info.py |   3 +-
 mne/fiff/pick.py      |   5 +-
 mne/fiff/write.py     |  27 +++---
 mne/viz.py            |   2 +-
 5 files changed, 131 insertions(+), 134 deletions(-)

diff --git a/mne/fiff/ctf.py b/mne/fiff/ctf.py
index 6b36816..b7a6683 100644
--- a/mne/fiff/ctf.py
+++ b/mne/fiff/ctf.py
@@ -15,12 +15,22 @@ def hex2dec(s):
 
 
 def _read_named_matrix(fid, node, matkind):
-    """
-    %
-    % [mat] = fiff__read_named_matrix(fid,node)
-    %
-    % Read named matrix from the given node
-    %
+    """read_named_matrix(fid,node)
+
+    Read named matrix from the given node
+
+    Parameters
+    ----------
+    fid : file
+        The file descriptor
+    node : dict
+        Node
+    matkind : mat kind
+        XXX
+    Returns
+    -------
+    mat : dict
+        The matrix with row and col names.
     """
 
     #   Descend one level if necessary
@@ -110,91 +120,87 @@ def read_ctf_comp(fid, node, chs):
     comps = dir_tree_find(node, FIFF.FIFFB_MNE_CTF_COMP_DATA)
 
     for node in comps:
-        # XXX
-        raise NotImplementedError("CTF data processing is not supported yet")
-
-        #
-        # #   Read the data we need
-        # mat = _read_named_matrix(fid, node, FIFF.FIFF_MNE_CTF_COMP_DATA)
-        # for p in range(node['nent']):
-        #     kind = node['dir'][p].kind
-        #     pos = node['dir'][p].pos
-        #     if kind == FIFF.FIFF_MNE_CTF_COMP_KIND:
-        #         tag = read_tag(fid, pos)
-        #         break
-        # else:
-        #     raise ValueError, 'Compensation type not found'
-        #
-        # #   Get the compensation kind and map it to a simple number
-        # one = dict(ctfkind=tag.data, kind=-1)
-        # del tag
-        #
-        # if one.ctfkind == int('47314252', 16): # hex2dec('47314252'):
-        #     one.kind = 1
-        # elif one.ctfkind == int('47324252', 16): # hex2dec('47324252'):
-        #     one.kind = 2
-        # elif one.ctfkind == int('47334252', 16): # hex2dec('47334252'):
-        #     one.kind = 3
-        # else:
-        #     one.kind = one.ctfkind
-        #
-        # for p in range(node['nent']):
-        #     kind = node['dir'][p].kind
-        #     pos = node['dir'][p].pos
-        #     if kind == FIFF.FIFF_MNE_CTF_COMP_CALIBRATED:
-        #         tag = read_tag(fid, pos)
-        #         calibrated = tag.data
-        #         break
-        # else:
-        #     calibrated = False
-        #
-        # one['save_calibrated'] = calibrated
-        # one['rowcals'] = np.ones(1, mat.shape[0])
-        # one['colcals'] = np.ones(1, mat.shape[1])
-        # if not calibrated:
-        #     #
-        #     #   Calibrate...
-        #     #
-        #     #   Do the columns first
-        #     #
-        #     ch_names = []
-        #     for p in range(len(chs)):
-        #         ch_names.append(chs[p]['ch_name'])
-        #
-        #     col_cals = np.zeros(mat.data.shape[1])
-        #     for col in range(mat.data.shape[1]):
-        #         p = ch_names.count(mat.col_names[col])
-        #         if p == 0:
-        #             raise ValueError, 'Channel %s is not available in data' \
-        #                                         % mat.col_names[col]
-        #         elif p > 1:
-        #             raise ValueError, 'Ambiguous channel %s' % \
-        #                                                 mat.col_names[col]
-        #
-        #         col_cals[col] = 1.0 / (chs[p]['range'] * chs[p]['cal'])
-        #
-        #     #    Then the rows
-        #     row_cals = np.zeros(mat.data.shape[0])
-        #     for row in range(mat.data.shape[0]):
-        #         p = ch_names.count(mat.row_names[row])
-        #         if p == 0:
-        #             raise ValueError, 'Channel %s is not available in data' \
-        #                                        % mat.row_names[row]
-        #         elif p > 1:
-        #             raise ValueError, 'Ambiguous channel %s' % \
-        #                                         mat.row_names[row]
-        #
-        #         row_cals[row] = chs[p]['range'] * chs[p]['cal']
-        #
-        #     mat.data = np.dot(np.diag(row_cals), np.dot(mat.data,
-        #                                                 np.diag(col_cals)))
-        #     one.rowcals = row_cals
-        #     one.colcals = col_cals
-        #
-        # one.data = mat
-        # compdata.append(one)
-        # del row_cals
-        # del col_cals
+        #   Read the data we need
+        mat = _read_named_matrix(fid, node, FIFF.FIFF_MNE_CTF_COMP_DATA)
+        for p in range(node['nent']):
+            kind = node['directory'][p].kind
+            pos = node['directory'][p].pos
+            if kind == FIFF.FIFF_MNE_CTF_COMP_KIND:
+                tag = read_tag(fid, pos)
+                break
+        else:
+            raise Exception('Compensation type not found')
+
+        #   Get the compensation kind and map it to a simple number
+        one = dict(ctfkind=tag.data, kind=-1)
+        del tag
+
+        if one['ctfkind'] == int('47314252', 16):  # hex2dec('47314252'):
+            one['kind'] = 1
+        elif one['ctfkind'] == int('47324252', 16):  # hex2dec('47324252'):
+            one['kind'] = 2
+        elif one['ctfkind'] == int('47334252', 16):  # hex2dec('47334252'):
+            one['kind'] = 3
+        else:
+            one['kind'] = one['ctfkind']
+
+        for p in range(node['nent']):
+            kind = node['directory'][p].kind
+            pos = node['directory'][p].pos
+            if kind == FIFF.FIFF_MNE_CTF_COMP_CALIBRATED:
+                tag = read_tag(fid, pos)
+                calibrated = tag.data
+                break
+        else:
+            calibrated = False
+
+        one['save_calibrated'] = calibrated
+        one['rowcals'] = np.ones(mat['data'].shape[0], dtype=np.float)
+        one['colcals'] = np.ones(mat['data'].shape[1], dtype=np.float)
+        if not calibrated:
+            #
+            #   Calibrate...
+            #
+            #   Do the columns first
+            #
+            ch_names = []
+            for p in range(len(chs)):
+                ch_names.append(chs[p]['ch_name'])
+
+            col_cals = np.zeros(mat['data'].shape[1], dtype=np.float)
+            for col in range(mat['data'].shape[1]):
+                p = ch_names.count(mat['col_names'][col])
+                if p == 0:
+                    raise Exception('Channel %s is not available in data'
+                                                % mat['col_names'][col])
+                elif p > 1:
+                    raise Exception('Ambiguous channel %s' %
+                                                        mat['col_names'][col])
+
+                col_cals[col] = 1.0 / (chs[p]['range'] * chs[p]['cal'])
+
+            #    Then the rows
+            row_cals = np.zeros(mat['data'].shape[0])
+            for row in range(mat['data'].shape[0]):
+                p = ch_names.count(mat['row_names'][row])
+                if p == 0:
+                    raise Exception('Channel %s is not available in data'
+                                               % mat['row_names'][row])
+                elif p > 1:
+                    raise Exception('Ambiguous channel %s' %
+                                                mat['row_names'][row])
+
+                row_cals[row] = chs[p]['range'] * chs[p]['cal']
+
+            mat['data'] = np.dot(np.diag(row_cals), np.dot(mat['data'],
+                                                        np.diag(col_cals)))
+            one['rowcals'] = row_cals
+            one['colcals'] = col_cals
+
+        one['data'] = mat
+        compdata.append(one)
+        del row_cals
+        del col_cals
 
     if len(compdata) > 0:
         print '\tRead %d compensation matrices' % len(compdata)
@@ -205,7 +211,6 @@ def read_ctf_comp(fid, node, chs):
 ###############################################################################
 # Writing
 
-from scipy import linalg
 from .write import start_block, end_block, write_int, write_named_matrix
 
 
@@ -223,26 +228,19 @@ def write_ctf_comp(fid, comps):
     if len(comps) <= 0:
         return
 
-    # XXX
-    raise NotImplementedError("CTF data processing is not supported yet")
-
-    # #  This is very simple in fact
-    # start_block(fid, FIFF.FIFFB_MNE_CTF_COMP)
-    # for comp in comps:
-    #     start_block(fid, FIFF.FIFFB_MNE_CTF_COMP_DATA)
-    #     #    Write the compensation kind
-    #     write_int(fid, FIFF.FIFF_MNE_CTF_COMP_KIND, comp['ctfkind'])
-    #     write_int(fid, FIFF.FIFF_MNE_CTF_COMP_CALIBRATED,
-    #                   comp['save_calibrated'])
-    #
-    #     #    Write an uncalibrated or calibrated matrix
-    #     import pdb; pdb.set_trace()
-    #
-    #     comp['data']['data'] = linalg.inv(
-    #                         np.dot(np.diag(comp['rowcals'].ravel())),
-    #                            np.dot(comp.data.data,
-    #                               linalg.inv(np.diag(comp.colcals.ravel()))))
-    #     write_named_matrix(fid, FIFF.FIFF_MNE_CTF_COMP_DATA, comp['data'])
-    #     end_block(fid, FIFF.FIFFB_MNE_CTF_COMP_DATA)
-    #
-    # end_block(fid, FIFF.FIFFB_MNE_CTF_COMP)
+    #  This is very simple in fact
+    start_block(fid, FIFF.FIFFB_MNE_CTF_COMP)
+    for comp in comps:
+        start_block(fid, FIFF.FIFFB_MNE_CTF_COMP_DATA)
+        #    Write the compensation kind
+        write_int(fid, FIFF.FIFF_MNE_CTF_COMP_KIND, comp['ctfkind'])
+        write_int(fid, FIFF.FIFF_MNE_CTF_COMP_CALIBRATED,
+                      comp['save_calibrated'])
+
+        #    Write an uncalibrated or calibrated matrix
+        comp['data']['data'] = (comp['rowcals'][:, None] * comp['data']['data']
+                                                    * comp['colcals'][None, :])
+        write_named_matrix(fid, FIFF.FIFF_MNE_CTF_COMP_DATA, comp['data'])
+        end_block(fid, FIFF.FIFFB_MNE_CTF_COMP_DATA)
+
+    end_block(fid, FIFF.FIFFB_MNE_CTF_COMP)
diff --git a/mne/fiff/meas_info.py b/mne/fiff/meas_info.py
index a9afd67..61efcf3 100644
--- a/mne/fiff/meas_info.py
+++ b/mne/fiff/meas_info.py
@@ -4,6 +4,7 @@
 # License: BSD (3-clause)
 
 import numpy as np
+from scipy import linalg
 
 from .open import fiff_open
 from .tree import dir_tree_find, copy_tree
@@ -216,7 +217,7 @@ def read_meas_info(fid, tree):
     if dev_head_t is not None and ctf_head_t is not None:
         info['dev_ctf_t'] = info['dev_head_t']
         info['dev_ctf_t']['to'] = info['ctf_head_t']['from']
-        info['dev_ctf_t']['trans'] = np.dot(np.inv(ctf_head_t['trans']),
+        info['dev_ctf_t']['trans'] = np.dot(linalg.inv(ctf_head_t['trans']),
                                         info['dev_ctf_t']['trans'])
     else:
         info['dev_ctf_t'] = []
diff --git a/mne/fiff/pick.py b/mne/fiff/pick.py
index d615655..306ab5c 100644
--- a/mne/fiff/pick.py
+++ b/mne/fiff/pick.py
@@ -26,11 +26,13 @@ def channel_type(info, idx):
     """
 
     kind = info['chs'][idx]['kind']
-    if (kind == FIFF.FIFFV_MEG_CH or kind == FIFF.FIFFV_REF_MEG_CH):
+    if kind == FIFF.FIFFV_MEG_CH:
         if info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_T_M:
             return 'grad'
         elif info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_T:
             return 'mag'
+    elif kind == FIFF.FIFFV_REF_MEG_CH:
+        return 'ref_meg'
     elif kind == FIFF.FIFFV_EEG_CH:
         return 'eeg'
     elif kind == FIFF.FIFFV_STIM_CH:
@@ -41,6 +43,7 @@ def channel_type(info, idx):
         return 'emg'
     elif kind == FIFF.FIFFV_ECG_CH:
         return 'ecg'
+    raise Exception('Unknown channel type')
 
 
 def pick_channels(ch_names, include, exclude=[]):
diff --git a/mne/fiff/write.py b/mne/fiff/write.py
index e50679b..d255944 100644
--- a/mne/fiff/write.py
+++ b/mne/fiff/write.py
@@ -290,20 +290,15 @@ def write_dig_point(fid, dig):
 
 def write_named_matrix(fid, kind, mat):
     """Writes a named single-precision floating-point matrix"""
+    start_block(fid, FIFF.FIFFB_MNE_NAMED_MATRIX)
+    write_int(fid, FIFF.FIFF_MNE_NROW, mat['nrow'])
+    write_int(fid, FIFF.FIFF_MNE_NCOL, mat['ncol'])
 
-    raise NotImplementedError("CTF data processing is not supported yet")
-
-    # start_block(fid, FIFF.FIFFB_MNE_NAMED_MATRIX)
-    # write_int(fid, FIFF.FIFF_MNE_NROW, mat['nrow'])
-    # write_int(fid, FIFF.FIFF_MNE_NCOL, mat['ncol'])
-    #
-    # if len(mat['row_names']) > 0:
-    #     write_name_list(fid, FIFF.FIFF_MNE_ROW_NAMES, mat['row_names'])
-    #
-    # if len(mat['col_names']) > 0:
-    #     write_name_list(fid, FIFF.FIFF_MNE_COL_NAMES, mat['col_names'])
-    #
-    # write_float_matrix(fid,kind, mat.data)
-    # end_block(fid, FIFF.FIFFB_MNE_NAMED_MATRIX)
-    #
-    # return;
+    if len(mat['row_names']) > 0:
+        write_name_list(fid, FIFF.FIFF_MNE_ROW_NAMES, mat['row_names'])
+
+    if len(mat['col_names']) > 0:
+        write_name_list(fid, FIFF.FIFF_MNE_COL_NAMES, mat['col_names'])
+
+    write_float_matrix(fid, kind, mat['data'])
+    end_block(fid, FIFF.FIFFB_MNE_NAMED_MATRIX)
diff --git a/mne/viz.py b/mne/viz.py
index acb80b6..8670b2a 100644
--- a/mne/viz.py
+++ b/mne/viz.py
@@ -65,7 +65,7 @@ def plot_evoked(evoked, picks=None, unit=True, show=True):
         if unit is False:
             scaling = 1.0
             ch_unit = 'NA'  # no unit
-        idx = [picks[i] for i in range(len(picks)) if types[i] is t]
+        idx = [picks[i] for i in range(len(picks)) if types[i] == t]
         if len(idx) > 0:
             pl.subplot(n_channel_types, 1, counter)
             pl.plot(times, scaling * evoked.data[idx, :].T)

-- 
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