[med-svn] [python-mne] 19/353: ENH: better reading of w, writing of w, unit test

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:24:25 UTC 2015


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

yoh pushed a commit to tag 0.4
in repository python-mne.

commit bd2f6ff662e1d843d212f3b040fcd6342ac610aa
Author: Martin Luessi <mluessi at nmr.mgh.harvard.edu>
Date:   Wed Dec 7 18:10:39 2011 -0500

    ENH: better reading of w, writing of w, unit test
    
    command used to generate sample data:
    mne_sensitivity_map --fwd sample_audvis-meg-oct-6-fwd.fif --map 1 --w sample_audvis-meg-oct-6-fwd-sensmap
---
 mne/__init__.py                   |  3 +-
 mne/source_estimate.py            | 96 ++++++++++++++++++++++++++++++++++-----
 mne/tests/test_source_estimate.py | 19 +++++++-
 3 files changed, 104 insertions(+), 14 deletions(-)

diff --git a/mne/__init__.py b/mne/__init__.py
index 0136e9a..9679ff5 100644
--- a/mne/__init__.py
+++ b/mne/__init__.py
@@ -4,7 +4,8 @@ from .cov import read_cov, write_cov, write_cov_file, Covariance, \
                  compute_raw_data_covariance, compute_covariance
 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, \
+from .source_estimate import read_stc, write_stc, read_w, write_w, \
+                             SourceEstimate, morph_data, \
                              spatio_temporal_src_connectivity, \
                              spatio_temporal_tris_connectivity, \
                              save_stc_as_volume
diff --git a/mne/source_estimate.py b/mne/source_estimate.py
index 089df7e..156f49b 100644
--- a/mne/source_estimate.py
+++ b/mne/source_estimate.py
@@ -129,8 +129,9 @@ def read_w(filename):
     data: dict
         The w structure. It has the following keys:
            vertices       vertex indices (0 based)
-           data           The data matrix (nvert)
+           data           The data matrix (nvert long)
     """
+
     fid = open(filename, 'rb')
 
     # skip first 2 bytes
@@ -142,10 +143,10 @@ def read_w(filename):
     vertices = np.zeros((vertices_n), dtype=np.int32)
     data = np.zeros((vertices_n), dtype=np.float32)
 
-    # read the data
+    # read the vertices and data
     for i in range(vertices_n):
         vertices[i] = _read_3(fid)
-        data[i] = np.fromfile(fid, dtype=">f4", count=1)
+        data[i] = np.fromfile(fid, dtype='>f4', count=1)
 
     w = dict()
     w['vertices'] = vertices
@@ -156,6 +157,56 @@ def read_w(filename):
     return w
 
 
+def _write_3(fid, val):
+    """ Write 3 byte integer to file
+    """
+
+    f_bytes = np.zeros((3), dtype=np.uint8)
+
+    f_bytes[0] = (val >> 16) & 255
+    f_bytes[1] = (val >> 8) & 255
+    f_bytes[2] = val & 255
+
+    fid.write(f_bytes.tostring())
+
+
+def write_w(filename, vertices, data):
+    """Read a w file
+
+    w files contain activations or source reconstructions for a single time
+    point
+
+    Parameters
+    ----------
+    filename: string
+        The name of the w file
+    vertices: array of integers
+        Vertex indices (0 based)
+    data: 1D array
+        The data array (nvert)
+    """
+
+    assert(len(vertices) == len(data))
+
+    fid = open(filename, 'wb')
+
+    # write 2 zero bytes
+    fid.write(np.zeros((2), dtype=np.uint8).tostring())
+
+    # write number of vertices/sources (3 byte integer)
+    vertices_n = len(vertices)
+    _write_3(fid, vertices_n)
+
+    # write the vertices and data
+    for i in range(vertices_n):
+        _write_3(fid, vertices[i])
+        #XXX: without float() endianness is wrong, not sure why
+        fid.write(np.array(float(data[i]), dtype='>f4').tostring())
+
+    # close the file
+    fid.close()
+
+
 class SourceEstimate(object):
     """SourceEstimate container
 
@@ -181,9 +232,12 @@ class SourceEstimate(object):
                 self.times = self.tmin + (self.tstep *
                                           np.arange(self.data.shape[1]))
                 self.vertno = [vl['vertices']]
-            elif fname.endswith('-lh.stc') or fname.endswith('-rh.stc'):
+            elif (fname.endswith('.stc') or os.path.exists(fname + '-lh.stc')
+                  or os.path.exists(fname + '-rh.stc')):
                 # stc file with surface source spaces
-                fname = fname[:-7]
+
+                if fname.endswith('-lh.stc') or fname.endswith('-rh.stc'):
+                    fname = fname[:-7]
                 lh = read_stc(fname + '-lh.stc')
                 rh = read_stc(fname + '-rh.stc')
                 self.data = np.r_[lh['data'], rh['data']]
@@ -194,9 +248,12 @@ class SourceEstimate(object):
                 self.times = self.tmin + (self.tstep *
                                           np.arange(self.data.shape[1]))
                 self.vertno = [lh['vertices'], rh['vertices']]
-            elif fname.endswith('-lh.w') or fname.endswith('-rh.w'):
+            elif (fname.endswith('.w') or os.path.exists(fname + '-lh.w')
+                  or os.path.exists(fname + '-rh.w')):
                 # w file with surface source spaces
-                fname = fname[:-5]
+
+                if fname.endswith('-lh.w') or fname.endswith('-rh.w'):
+                    fname = fname[:-5]
                 lh = read_w(fname + '-lh.w')
                 rh = read_w(fname + '-rh.w')
                 self.data = np.atleast_2d(np.r_[lh['data'], rh['data']]).T
@@ -213,18 +270,33 @@ class SourceEstimate(object):
         """create self.times"""
         self.times = self.tmin + self.tstep * np.arange(self.data.shape[1])
 
-    def save(self, fname):
+    def save(self, fname, ftype='stc'):
         """save to source estimates to file"""
         if self.is_surface():
             lh_data = self.data[:len(self.lh_vertno)]
             rh_data = self.data[-len(self.rh_vertno):]
 
-            print 'Writing STC to disk...',
-            write_stc(fname + '-lh.stc', tmin=self.tmin, tstep=self.tstep,
-                           vertices=self.lh_vertno, data=lh_data)
-            write_stc(fname + '-rh.stc', tmin=self.tmin, tstep=self.tstep,
+            if ftype == 'stc':
+                print 'Writing STC to disk...',
+                write_stc(fname + '-lh.stc', tmin=self.tmin, tstep=self.tstep,
+                          vertices=self.lh_vertno, data=lh_data)
+                write_stc(fname + '-rh.stc', tmin=self.tmin, tstep=self.tstep,
                            vertices=self.rh_vertno, data=rh_data)
+            elif ftype == 'w':
+                if self.data.shape[1] != 1:
+                    raise ValueError('w files can only contain a single time '
+                                     'point')
+                print 'Writing STC to disk (w format)...',
+                write_w(fname + '-lh.w', vertices=self.lh_vertno,
+                        data=lh_data[:, 0])
+                write_w(fname + '-rh.w', vertices=self.rh_vertno,
+                        data=rh_data[:, 0])
+            else:
+                raise ValueError('invalid file type')
         else:
+            if ftype != 'stc':
+                raise ValueError('ftype has to be \"stc\" volume source '
+                                 'spaces')
             print 'Writing STC to disk...',
             if not fname.endswith('-vl.stc'):
                 fname += '-vl.stc'
diff --git a/mne/tests/test_source_estimate.py b/mne/tests/test_source_estimate.py
index d10c433..761e491 100644
--- a/mne/tests/test_source_estimate.py
+++ b/mne/tests/test_source_estimate.py
@@ -6,7 +6,7 @@ from numpy.testing import assert_array_almost_equal, assert_array_equal
 
 from ..datasets import sample
 from .. import stats
-from .. import read_stc, write_stc, SourceEstimate, morph_data
+from .. import read_stc, write_stc, read_w, write_w, SourceEstimate, morph_data
 from ..source_estimate import spatio_temporal_tris_connectivity, \
                                 spatio_temporal_src_connectivity
 
@@ -31,6 +31,23 @@ def test_io_stc():
     assert_array_almost_equal(stc['tstep'], stc2['tstep'])
 
 
+def test_io_w():
+    """Test IO for w files
+    """
+    w_fname = op.join(data_path, 'MEG', 'sample',
+                      'sample_audvis-meg-oct-6-fwd-sensmap')
+
+    src = SourceEstimate(w_fname)
+
+    src.save('tmp', ftype='w')
+
+    src2 = SourceEstimate('tmp-lh.w')
+
+    assert_array_almost_equal(src.data, src2.data)
+    assert_array_almost_equal(src.lh_vertno, src2.lh_vertno)
+    assert_array_almost_equal(src.rh_vertno, src2.rh_vertno)
+
+
 def test_stc_arithmetic():
     """Test arithmetic for STC files
     """

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