[med-svn] [python-mne] 308/376: ENH : better support for CTF data + tests

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:23:12 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 17d1dfe84bf255fdc19619441575b95a1963a2e3
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Sun Jul 3 09:06:55 2011 -0400

    ENH : better support for CTF data + tests
---
 mne/fiff/pick.py                     |  11 ++++-
 mne/fiff/raw.py                      |  33 ++++++++++++---
 mne/fiff/tests/data/test_ctf_raw.fif | Bin 0 -> 9017895 bytes
 mne/fiff/tests/test_raw.py           |  78 ++++++++++++++++++-----------------
 4 files changed, 77 insertions(+), 45 deletions(-)

diff --git a/mne/fiff/pick.py b/mne/fiff/pick.py
index 306ab5c..5042c8d 100644
--- a/mne/fiff/pick.py
+++ b/mne/fiff/pick.py
@@ -77,7 +77,7 @@ def pick_channels(ch_names, include, exclude=[]):
 
 
 def pick_types(info, meg=True, eeg=False, stim=False, eog=False, ecg=False,
-               emg=False, include=[], exclude=[]):
+               emg=False, misc=False, include=[], exclude=[]):
     """Pick channels by type and names
 
     Parameters
@@ -87,7 +87,8 @@ def pick_types(info, meg=True, eeg=False, stim=False, eog=False, ecg=False,
     meg : bool or string
         If True include all MEG channels. If False include None
         If string it can be 'mag' or 'grad' to select only gradiometers
-        or magnetometers.
+        or magnetometers. It can also be 'ref_meg' to get CTF
+        reference channels.
     eeg : bool
         If True include EEG channels
     eog : bool
@@ -98,6 +99,8 @@ def pick_types(info, meg=True, eeg=False, stim=False, eog=False, ecg=False,
         If True include EMG channels
     stim : bool
         If True include stimulus channels
+    misc : bool
+        If True include miscellaneous analog channels
     include : list of string
         List of additional channels to include. If empty do not include any.
 
@@ -123,6 +126,8 @@ def pick_types(info, meg=True, eeg=False, stim=False, eog=False, ecg=False,
             elif (meg == 'mag'
                     and info['chs'][k]['unit'] == FIFF.FIFF_UNIT_T):
                 pick[k] = True
+            elif meg == 'ref_meg':
+                pick[k] = True
         elif kind == FIFF.FIFFV_EEG_CH and eeg:
             pick[k] = True
         elif kind == FIFF.FIFFV_STIM_CH and stim:
@@ -133,6 +138,8 @@ def pick_types(info, meg=True, eeg=False, stim=False, eog=False, ecg=False,
             pick[k] = True
         elif kind == FIFF.FIFFV_EMG_CH and emg:
             pick[k] = True
+        elif kind == FIFF.FIFFV_MISC_CH and misc:
+            pick[k] = True
 
     myinclude = [info['ch_names'][k] for k in range(nchan) if pick[k]]
     myinclude += include
diff --git a/mne/fiff/raw.py b/mne/fiff/raw.py
index 55e8234..e022cc2 100644
--- a/mne/fiff/raw.py
+++ b/mne/fiff/raw.py
@@ -4,6 +4,7 @@
 # License: BSD (3-clause)
 
 from math import floor, ceil
+import copy
 import numpy as np
 
 from .constants import FIFF
@@ -176,6 +177,10 @@ class Raw(dict):
                 start = 0
             if step is not None:
                 raise ValueError('step needs to be 1 : %d given' % step)
+
+            if len(sel) == 0:
+                raise Exception("Empty channel list")
+
             return read_raw_segment(self, start=start, stop=stop, sel=sel)
         else:
             return super(Raw, self).__getitem__(item)
@@ -224,7 +229,10 @@ class Raw(dict):
             if last >= stop:
                 last = stop + 1
 
-            data, times = self[picks, first:last]
+            if picks is None:
+                data, times = self[:, first:last]
+            else:
+                data, times = self[picks, first:last]
 
             print 'Writing ... ',
             write_raw_buffer(outfid, data, cals)
@@ -488,10 +496,12 @@ def start_writing_raw(name, info, sel=None):
     #   We will always write floats
     #
     if sel is None:
-        sel = np.arange(info['nchan'])
+        chs = info['chs']
+        nchan = len(chs)
+    else:
+        chs = [info['chs'][k] for k in sel]
+        nchan = len(sel)
     data_type = 4
-    chs = [info['chs'][k] for k in sel]
-    nchan = len(chs)
     #
     #  Create the file and save the essentials
     #
@@ -558,7 +568,20 @@ def start_writing_raw(name, info, sel=None):
     #
     #    CTF compensation info
     #
-    write_ctf_comp(fid, info['comps'])
+    comps = info['comps']
+    if sel is not None:
+        ch_names = [c['ch_name'] for c in chs]  # name of good channels
+        comps = copy.deepcopy(comps)
+        for c in comps:
+            row_idx = [k for k, n in enumerate(c['data']['row_names'])
+                                                            if n in ch_names]
+            row_names = [c['data']['row_names'][i] for i in row_idx]
+            rowcals = c['rowcals'][row_idx]
+            c['rowcals'] = rowcals
+            c['data']['nrow'] = len(row_names)
+            c['data']['row_names'] = row_names
+            c['data']['data'] = c['data']['data'][row_idx]
+    write_ctf_comp(fid, comps)
     #
     #    Bad channels
     #
diff --git a/mne/fiff/tests/data/test_ctf_raw.fif b/mne/fiff/tests/data/test_ctf_raw.fif
new file mode 100644
index 0000000..37a6f32
Binary files /dev/null and b/mne/fiff/tests/data/test_ctf_raw.fif differ
diff --git a/mne/fiff/tests/test_raw.py b/mne/fiff/tests/test_raw.py
index 3ecbf15..64f203c 100644
--- a/mne/fiff/tests/test_raw.py
+++ b/mne/fiff/tests/test_raw.py
@@ -2,46 +2,48 @@ import os.path as op
 
 from numpy.testing import assert_array_almost_equal
 
-from .. import Raw, pick_types
+from .. import Raw, pick_types, pick_channels
 
-fname = op.join(op.dirname(__file__), 'data', 'test_raw.fif')
+fif_fname = op.join(op.dirname(__file__), 'data', 'test_raw.fif')
+ctf_fname = op.join(op.dirname(__file__), 'data', 'test_ctf_raw.fif')
 
 
 def test_io_raw():
-    """Test IO for raw data
+    """Test IO for raw data (Neuromag + CTF)
     """
-    raw = Raw(fname)
-
-    nchan = raw.info['nchan']
-    ch_names = raw.info['ch_names']
-    meg_channels_idx = [k for k in range(nchan) if ch_names[k][:3] == 'MEG']
-    meg_channels_idx = meg_channels_idx[:5]
-
-    start, stop = raw.time_to_index(0, 5)
-    data, times = raw[meg_channels_idx, start:(stop+1)]
-
-    # Set up pick list: MEG + STI 014 - bad channels
-    want_meg = True
-    want_eeg = False
-    want_stim = False
-    include = ['STI 014']
-
-    picks = pick_types(raw.info, meg=want_meg, eeg=want_eeg,
-                            stim=want_stim, include=include,
-                            exclude=raw.info['bads'])
-    picks = picks[:5] # take 5 first
-
-    print "Number of picked channels : %d" % len(picks)
-
-    # Writing
-    raw.save('raw.fif', picks, tmin=0, tmax=5)
-
-    raw2 = Raw('raw.fif')
-
-    data2, times2 = raw2[:,:]
-
-    assert_array_almost_equal(data, data2)
-    assert_array_almost_equal(times, times2)
-    assert_array_almost_equal(raw.info['dev_head_t']['trans'],
-                              raw2.info['dev_head_t']['trans'])
-    assert_array_almost_equal(raw.info['sfreq'], raw2.info['sfreq'])
+    for fname in [fif_fname, ctf_fname]:
+        raw = Raw(fname)
+
+        nchan = raw.info['nchan']
+        ch_names = raw.info['ch_names']
+        meg_channels_idx = [k for k in range(nchan)
+                                            if ch_names[k][0] == 'M']
+        n_channels = 100
+        meg_channels_idx = meg_channels_idx[:n_channels]
+        start, stop = raw.time_to_index(0, 5)
+        data, times = raw[meg_channels_idx, start:(stop + 1)]
+        meg_ch_names = [ch_names[k] for k in meg_channels_idx]
+
+        # Set up pick list: MEG + STI 014 - bad channels
+        include = ['STI 014']
+        include += meg_ch_names
+        picks = pick_types(raw.info, meg=True, eeg=False,
+                                stim=True, misc=True, include=include,
+                                exclude=raw.info['bads'])
+        print "Number of picked channels : %d" % len(picks)
+
+        # Writing
+        raw.save('raw.fif', picks, tmin=0, tmax=5)
+
+        raw2 = Raw('raw.fif')
+
+        sel = pick_channels(raw2.ch_names, meg_ch_names)
+        data2, times2 = raw2[sel, :]
+
+        assert_array_almost_equal(data, data2)
+        assert_array_almost_equal(times, times2)
+        assert_array_almost_equal(raw.info['dev_head_t']['trans'],
+                                  raw2.info['dev_head_t']['trans'])
+        assert_array_almost_equal(raw.info['sfreq'], raw2.info['sfreq'])
+
+        fname = op.join(op.dirname(__file__), 'data', 'test_raw.fif')

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