[med-svn] [python-mne] 22/376: more tests + writing stc files

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:21:58 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 efed27f8307779b0e49eeb9d8f39b8577516f867
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Thu Dec 30 16:17:51 2010 -0500

    more tests + writing stc files
---
 examples/read_inverse.py        |  26 +++++
 examples/read_stc.py            |  20 ++--
 fiff/__init__.py                |   3 +-
 fiff/bem_surfaces.py            |   8 +-
 fiff/forward.py                 |  95 +---------------
 fiff/inverse.py                 | 235 ++++++++++++++++++++++++++++++++++++++++
 fiff/matrix.py                  |  70 ++++++++++++
 fiff/source_space.py            |  20 ++++
 fiff/stc.py                     |  38 +++++++
 fiff/tests/test_bem_surfaces.py |  17 +++
 fiff/tests/test_forward.py      |  17 +++
 fiff/tests/test_inverse.py      |  16 +++
 fiff/tests/test_raw.py          |  25 +++++
 fiff/tests/test_stc.py          |  24 ++++
 14 files changed, 509 insertions(+), 105 deletions(-)

diff --git a/examples/read_inverse.py b/examples/read_inverse.py
new file mode 100644
index 0000000..940a786
--- /dev/null
+++ b/examples/read_inverse.py
@@ -0,0 +1,26 @@
+"""Reading an inverse operator
+"""
+print __doc__
+
+import fiff
+
+fname = 'MNE-sample-data/MEG/sample/sample_audvis-ave-7-meg-inv.fif'
+
+inv = fiff.read_inverse_operator(fname)
+
+print "Method: %s" % inv['methods']
+print "fMRI prior: %s" % inv['fmri_prior']
+print "Number of sources: %s" % inv['nsource']
+print "Number of channels: %s" % inv['nchan']
+
+# ###############################################################################
+# # Show result
+# 
+# # 3D source space
+# lh_points = inv['src'][0]['rr']
+# lh_faces = inv['src'][0]['use_tris']
+# rh_points = inv['src'][1]['rr']
+# rh_faces = inv['src'][1]['use_tris']
+# from enthought.mayavi import mlab
+# mlab.triangular_mesh(lh_points[:,0], lh_points[:,1], lh_points[:,2], lh_faces)
+# mlab.triangular_mesh(rh_points[:,0], rh_points[:,1], rh_points[:,2], rh_faces)
diff --git a/examples/read_stc.py b/examples/read_stc.py
index f1a8573..96bd266 100644
--- a/examples/read_stc.py
+++ b/examples/read_stc.py
@@ -4,15 +4,19 @@ print __doc__
 
 import fiff
 
-# fname = 'MNE-sample-data/MEG/sample/sample_audvis_raw.fif'
-fname = 'hk_ret12_offl-7-meg-snr-3-spm-rh.stc'
+fname = 'MNE-sample-data/MEG/sample/sample_audvis-ave-7-meg-lh.stc'
 
 stc = fiff.read_stc(fname)
+fiff.write_stc("tmp.stc", stc)
+stc2 = fiff.read_stc("tmp.stc")
 
-n_vertices, n_samples = stc['data'].shape
-print "tmin : %s (s)" % stc['tmin']
-print "tstep : %s" % stc['tstep']
-print "tmax : %s (s)" % (stc['tmin'] + stc['tstep'] * n_samples)
-print "stc data size: %s (nb of vertices) x %s (nb of samples)" % (
-                                                    n_vertices, n_samples)
+from scipy import linalg
+print linalg.norm(stc['data'] - stc2['data'])
 
+# n_vertices, n_samples = stc['data'].shape
+# print "tmin : %s (s)" % stc['tmin']
+# print "tstep : %s" % stc['tstep']
+# print "tmax : %s (s)" % (stc['tmin'] + stc['tstep'] * n_samples)
+# print "stc data size: %s (nb of vertices) x %s (nb of samples)" % (
+#                                                     n_vertices, n_samples)
+# 
diff --git a/fiff/__init__.py b/fiff/__init__.py
index dce372e..0273e75 100644
--- a/fiff/__init__.py
+++ b/fiff/__init__.py
@@ -7,6 +7,7 @@ from .cov import read_cov, write_cov, write_cov_file
 from .raw import setup_read_raw, read_raw_segment, read_raw_segment_times
 from .event import read_events, write_events
 from .forward import read_forward_solution
-from .stc import read_stc
+from .stc import read_stc, write_stc
 from .bem_surfaces import read_bem_surfaces
+from .inverse import read_inverse_operator
 
diff --git a/fiff/bem_surfaces.py b/fiff/bem_surfaces.py
index 2e0fe4c..f5ee64d 100644
--- a/fiff/bem_surfaces.py
+++ b/fiff/bem_surfaces.py
@@ -75,7 +75,7 @@ def read_bem_surfaces(fname, add_geom=False):
         this = read_bem_surface(fid, bsurf, coord_frame)
         print '[done]'
         if add_geom:
-            complete_surface_info()
+            complete_surface_info(this)
         surf.append(this)
 
     print '\t%d BEM surfaces read' % len(surf)
@@ -171,18 +171,18 @@ def complete_surface_info(this):
     #
     print '\tCompleting triangulation info...'
     print 'triangle normals...'
-    this.tri_area = np.zeros(this['ntri'])
+    this['tri_area'] = np.zeros(this['ntri'])
     r1 = this['rr'][this['tris'][:,0],:]
     r2 = this['rr'][this['tris'][:,1],:]
     r3 = this['rr'][this['tris'][:,2],:]
-    this.tri_cent = (r1+r2+r3) /3.0
+    this['tri_cent'] = (r1+r2+r3) /3.0
     this['tri_nn'] = np.cross((r2-r1), (r3-r1))
     #
     #   Triangle normals and areas
     #
     for p in range(this['ntri']):
         size = linalg.norm(this['tri_nn'][p,:])
-        this.tri_area[p] = size / 2.0
+        this['tri_area'][p] = size / 2.0
     if size > 0.0:
         this['tri_nn'][p,:] = this['tri_nn'][p,:] / size
     #
diff --git a/fiff/forward.py b/fiff/forward.py
index dd6bbdf..489e717 100644
--- a/fiff/forward.py
+++ b/fiff/forward.py
@@ -6,19 +6,9 @@ from .constants import FIFF
 from .open import fiff_open
 from .tree import dir_tree_find
 from .channels import read_bad_channels
-from .tag import find_tag, has_tag
-from .source_space import read_source_spaces
-
-
-def transpose_named_matrix(mat):
-    """Transpose mat inplace (no copy)
-    """
-    mat['nrow'] = mat['ncol']
-    mat['ncol'] = mat['nrow']
-    mat['row_names'] = mat['col_names']
-    mat['col_names'] = mat['row_names']
-    mat['data'] = mat['data'].T
-    return mat
+from .tag import find_tag
+from .source_space import read_source_spaces, find_source_space_hemi
+from .matrix import read_named_matrix, transpose_named_matrix
 
 
 def block_diag(A, n):
@@ -138,85 +128,6 @@ def invert_transform(trans):
     return itrans
 
 
-def read_named_matrix(fid, node, matkind):
-    """
-    %
-    % [mat] = fiff_read_named_matrix(fid,node)
-    %
-    % Read named matrix from the given node
-    %
-    """
-
-    #   Descend one level if necessary
-    if node.block != FIFF.FIFFB_MNE_NAMED_MATRIX:
-        for k in range(node.nchild):
-            if node.children[k].block == FIFF.FIFFB_MNE_NAMED_MATRIX:
-                if has_tag(node.children[k], matkind):
-                    node = node.children[k]
-                    break
-        else:
-            raise ValueError, 'Desired named matrix (kind = %d) not available' % matkind
-    else:
-       if not has_tag(node, matkind):
-          raise ValueError, 'Desired named matrix (kind = %d) not available' % matkind
-
-    #   Read everything we need
-    tag = find_tag(fid, node, matkind)
-    if tag is None:
-       raise ValueError, 'Matrix data missing'
-    else:
-       data = tag.data;
-
-    nrow, ncol = data.shape
-    tag = find_tag(fid, node, FIFF.FIFF_MNE_NROW)
-    if tag is not None:
-       if tag.data != nrow:
-          raise ValueError, 'Number of rows in matrix data and FIFF_MNE_NROW tag do not match'
-
-    tag = find_tag(fid, node, FIFF.FIFF_MNE_NCOL)
-    if tag is not None:
-       if tag.data != ncol:
-          raise ValueError, 'Number of columns in matrix data and FIFF_MNE_NCOL tag do not match'
-
-    tag = find_tag(fid, node, FIFF.FIFF_MNE_ROW_NAMES)
-    if tag is not None:
-        row_names = tag.data.split(':')
-    else:
-        row_names = []
-
-    tag = find_tag(fid, node, FIFF.FIFF_MNE_COL_NAMES)
-    if tag is not None:
-        col_names = tag.data.split(':')
-    else:
-        col_names = []
-
-    mat = dict(nrow=nrow, ncol=ncol, row_names=row_names, col_names=col_names,
-               data=data)
-    return mat
-
-
-def find_source_space_hemi(src):
-    """
-    %
-    % function mne_find_source_space_hemi(src)
-    %
-    % Return the hemisphere id for a source space
-    %
-    % src      - The source space to investigate
-    % hemi     - Deduced hemisphere id
-    %
-    """
-
-    xave = src['rr'][:,0].sum();
-
-    if xave < 0:
-        hemi = int(FIFF.FIFFV_MNE_SURF_LEFT_HEMI)
-    else:
-        hemi = int(FIFF.FIFFV_MNE_SURF_RIGHT_HEMI)
-
-    return hemi
-
-
 def read_one(fid, node):
     """
     %
diff --git a/fiff/inverse.py b/fiff/inverse.py
new file mode 100644
index 0000000..5d516d1
--- /dev/null
+++ b/fiff/inverse.py
@@ -0,0 +1,235 @@
+from .constants import FIFF
+from .open import fiff_open
+from .tag import find_tag
+from .matrix import read_named_matrix, transpose_named_matrix
+from .cov import read_cov
+from .proj import read_proj
+from .tree import dir_tree_find
+from .source_space import read_source_spaces, find_source_space_hemi
+from .forward import invert_transform, transform_source_space_to
+
+
+def read_inverse_operator(fname):
+    """
+    %
+    % [inv] = mne_read_inverse_operator(fname)
+    %
+    % Reads the inverse operator decomposition from a fif file
+    %
+    % fname        - The name of the file
+    %
+    """
+    #
+    #   Open the file, create directory
+    #
+    print 'Reading inverse operator decomposition from %s...' % fname
+    fid, tree, _ = fiff_open(fname)
+    #
+    #   Find all inverse operators
+    #
+    invs = dir_tree_find(tree, FIFF.FIFFB_MNE_INVERSE_SOLUTION)
+    if invs is None:
+        fid.close()
+        raise ValueError, 'No inverse solutions in %s' % fname
+
+    invs = invs[0]
+    #
+    #   Parent MRI data
+    #
+    parent_mri = dir_tree_find(tree, FIFF.FIFFB_MNE_PARENT_MRI_FILE)
+    if len(parent_mri) == 0:
+        fid.close()
+        raise ValueError, 'No parent MRI information in %s' % fname
+    parent_mri = parent_mri[0]
+
+    print '\tReading inverse operator info...'
+    #
+    #   Methods and source orientations
+    #
+    tag = find_tag(fid, invs, FIFF.FIFF_MNE_INCLUDED_METHODS)
+    if tag is None:
+        fid.close()
+        raise ValueError, 'Modalities not found'
+
+    inv = dict()
+    inv['methods'] = tag.data;
+
+    tag = find_tag(fid, invs, FIFF.FIFF_MNE_SOURCE_ORIENTATION)
+    if tag is None:
+        fid.close()
+        raise ValueError, 'Source orientation constraints not found'
+
+    inv['source_ori'] = tag.data;
+
+    tag = find_tag(fid, invs, FIFF.FIFF_MNE_SOURCE_SPACE_NPOINTS)
+    if tag is None:
+        fid.close()
+        raise ValueError, 'Number of sources not found'
+
+    inv['nsource'] = tag.data
+    inv['nchan'] = 0
+    #
+    #   Coordinate frame
+    #
+    tag = find_tag(fid, invs, FIFF.FIFF_MNE_COORD_FRAME)
+    if tag is None:
+        fid.close()
+        raise ValueError, 'Coordinate frame tag not found'
+
+    inv['coord_frame'] = tag.data
+    #
+    #   The actual source orientation vectors
+    #
+    tag = find_tag(fid, invs, FIFF.FIFF_MNE_INVERSE_SOURCE_ORIENTATIONS)
+    if tag is None:
+        fid.close()
+        raise ValueError, 'Source orientation information not found'
+
+    inv['source_nn'] = tag.data
+    print '[done]\n'
+    #
+    #   The SVD decomposition...
+    #
+    print '\tReading inverse operator decomposition...'
+    tag = find_tag(fid, invs, FIFF.FIFF_MNE_INVERSE_SING)
+    if tag is None:
+        fid.close()
+        raise ValueError, 'Singular values not found'
+
+    inv['sing']  = tag.data
+    inv['nchan'] = len(inv['sing'])
+    #
+    #   The eigenleads and eigenfields
+    #
+    inv['eigen_leads_weighted'] = False
+    try:
+       inv['eigen_leads'] = read_named_matrix(fid, invs, FIFF.FIFF_MNE_INVERSE_LEADS)
+    except:
+       inv['eigen_leads_weighted'] = True
+       try:
+          inv.eigen_leads = read_named_matrix(fid,invs,FIFF.FIFF_MNE_INVERSE_LEADS_WEIGHTED);
+       except Exception as inst:
+          raise ValueError, '%s' % inst
+    #
+    #   Having the eigenleads as columns is better for the inverse calculations
+    #
+    inv['eigen_leads'] = transpose_named_matrix(inv['eigen_leads'])
+    try:
+        inv['eigen_fields'] = read_named_matrix(fid, invs, FIFF.FIFF_MNE_INVERSE_FIELDS)
+    except Exception as inst:
+        raise ValueError, '%s' % inst
+
+    print '[done]'
+    #
+    #   Read the covariance matrices
+    #
+    try:
+        inv['noise_cov'] = read_cov(fid, invs, FIFF.FIFFV_MNE_NOISE_COV)
+        print '\tNoise covariance matrix read.'
+    except Exception as inst:
+        fid.close()
+        raise ValueError, '%s' % inst
+
+    try:
+        inv['source_cov'] = read_cov(fid, invs, FIFF.FIFFV_MNE_SOURCE_COV)
+        print '\tSource covariance matrix read.'
+    except Exception as inst:
+        fid.close()
+        raise ValueError, '%s' % inst
+    #
+    #   Read the various priors
+    #
+    try:
+        inv.orient_prior = read_cov(fid, invs, FIFF.FIFFV_MNE_ORIENT_PRIOR_COV)
+        print '\tOrientation priors read.'
+    except Exception as inst:
+        inv['orient_prior'] = []
+
+    try:
+        inv['depth_prior'] = read_cov(fid, invs,
+                                          FIFF.FIFFV_MNE_DEPTH_PRIOR_COV)
+        print '\tDepth priors read.\n'
+    except:
+        inv['depth_prior'] = [];
+
+    try:
+        inv['fmri_prior'] = read_cov(fid, invs, FIFF.FIFFV_MNE_FMRI_PRIOR_COV)
+        print '\tfMRI priors read.\n'
+    except:
+        inv['fmri_prior'] = [];
+
+    #
+    #   Read the source spaces
+    #
+    try:
+        inv['src'] = read_source_spaces(fid, False, tree)
+    except Exception as inst:
+        fid.close()
+        raise ValueError, 'Could not read the source spaces (%s)' % inst
+
+    for s in inv['src']:
+       s['id'] = find_source_space_hemi(s)
+
+    #
+    #   Get the MRI <-> head coordinate transformation
+    #
+    tag = find_tag(fid, parent_mri, FIFF.FIFF_COORD_TRANS)
+    if tag is None:
+        fid.close()
+        raise ValueError, 'MRI/head coordinate transformation not found'
+    else:
+        mri_head_t = tag.data;
+        if mri_head_t['from_'] != FIFF.FIFFV_COORD_MRI or \
+                        mri_head_t['to'] != FIFF.FIFFV_COORD_HEAD:
+            mri_head_t = invert_transform(mri_head_t)
+            if mri_head_t['from_'] != FIFF.FIFFV_COORD_MRI or \
+                        mri_head_t['to'] != FIFF.FIFFV_COORD_HEAD:
+                fid.close()
+                raise ValueError, 'MRI/head coordinate transformation not found'
+
+    inv['mri_head_t'] = mri_head_t
+    #
+    #   Transform the source spaces to the correct coordinate frame
+    #   if necessary
+    #
+    if inv['coord_frame'] != FIFF.FIFFV_COORD_MRI and \
+            inv['coord_frame'] != FIFF.FIFFV_COORD_HEAD:
+        fid.close()
+        raise ValueError, 'Only inverse solutions computed in MRI or ' \
+                          'head coordinates are acceptable'
+
+    #
+    #  Number of averages is initially one
+    #
+    inv['nave'] = 1;
+    #
+    #  We also need the SSP operator
+    #
+    inv['projs'] = read_proj(fid, tree)
+    #
+    #  Some empty fields to be filled in later
+    #
+    inv['proj']      = []      #   This is the projector to apply to the data
+    inv['whitener']  = []      #   This whitens the data
+    inv['reginv']    = []      #   This the diagonal matrix implementing
+                             #   regularization and the inverse
+    inv['noisenorm'] = []      #   These are the noise-normalization factors
+    #
+    nuse = 0
+    for k in range(len(inv['src'])):
+       try:
+          inv['src'][k] = transform_source_space_to(inv['src'][k],
+                                                inv['coord_frame'], mri_head_t)
+       except Exception as inst:
+          fid.close()
+          raise ValueError, 'Could not transform source space (%s)', inst
+
+       nuse += inv['src'][k]['nuse']
+
+    print '\tSource spaces transformed to the inverse solution coordinate frame'
+    #
+    #   Done!
+    #
+    fid.close()
+
+    return inv
\ No newline at end of file
diff --git a/fiff/matrix.py b/fiff/matrix.py
new file mode 100644
index 0000000..ce51a4c
--- /dev/null
+++ b/fiff/matrix.py
@@ -0,0 +1,70 @@
+from .constants import FIFF
+from .tag import find_tag, has_tag
+
+
+def transpose_named_matrix(mat):
+    """Transpose mat inplace (no copy)
+    """
+    mat['nrow'] = mat['ncol']
+    mat['ncol'] = mat['nrow']
+    mat['row_names'] = mat['col_names']
+    mat['col_names'] = mat['row_names']
+    mat['data'] = mat['data'].T
+    return mat
+
+
+def read_named_matrix(fid, node, matkind):
+    """
+    %
+    % [mat] = fiff_read_named_matrix(fid,node)
+    %
+    % Read named matrix from the given node
+    %
+    """
+
+    #   Descend one level if necessary
+    if node.block != FIFF.FIFFB_MNE_NAMED_MATRIX:
+        for k in range(node.nchild):
+            if node.children[k].block == FIFF.FIFFB_MNE_NAMED_MATRIX:
+                if has_tag(node.children[k], matkind):
+                    node = node.children[k]
+                    break
+        else:
+            raise ValueError, 'Desired named matrix (kind = %d) not available' % matkind
+    else:
+       if not has_tag(node, matkind):
+          raise ValueError, 'Desired named matrix (kind = %d) not available' % matkind
+
+    #   Read everything we need
+    tag = find_tag(fid, node, matkind)
+    if tag is None:
+       raise ValueError, 'Matrix data missing'
+    else:
+       data = tag.data;
+
+    nrow, ncol = data.shape
+    tag = find_tag(fid, node, FIFF.FIFF_MNE_NROW)
+    if tag is not None:
+       if tag.data != nrow:
+          raise ValueError, 'Number of rows in matrix data and FIFF_MNE_NROW tag do not match'
+
+    tag = find_tag(fid, node, FIFF.FIFF_MNE_NCOL)
+    if tag is not None:
+       if tag.data != ncol:
+          raise ValueError, 'Number of columns in matrix data and FIFF_MNE_NCOL tag do not match'
+
+    tag = find_tag(fid, node, FIFF.FIFF_MNE_ROW_NAMES)
+    if tag is not None:
+        row_names = tag.data.split(':')
+    else:
+        row_names = []
+
+    tag = find_tag(fid, node, FIFF.FIFF_MNE_COL_NAMES)
+    if tag is not None:
+        col_names = tag.data.split(':')
+    else:
+        col_names = []
+
+    mat = dict(nrow=nrow, ncol=ncol, row_names=row_names, col_names=col_names,
+               data=data)
+    return mat
diff --git a/fiff/source_space.py b/fiff/source_space.py
index 1b5576c..0dd866b 100644
--- a/fiff/source_space.py
+++ b/fiff/source_space.py
@@ -252,3 +252,23 @@ def complete_source_space_info(this):
     print '[done]'
 
 
+def find_source_space_hemi(src):
+    """
+    %
+    % function mne_find_source_space_hemi(src)
+    %
+    % Return the hemisphere id for a source space
+    %
+    % src      - The source space to investigate
+    % hemi     - Deduced hemisphere id
+    %
+    """
+
+    xave = src['rr'][:,0].sum();
+
+    if xave < 0:
+        hemi = int(FIFF.FIFFV_MNE_SURF_LEFT_HEMI)
+    else:
+        hemi = int(FIFF.FIFFV_MNE_SURF_RIGHT_HEMI)
+
+    return hemi
diff --git a/fiff/stc.py b/fiff/stc.py
index 9c10f24..c21cc15 100644
--- a/fiff/stc.py
+++ b/fiff/stc.py
@@ -51,3 +51,41 @@ def read_stc(filename):
     # close the file
     fid.close()
     return stc
+
+
+def write_stc(filename, stc):
+    """
+    %
+    % mne_write_stc_file(filename,stc)
+    % 
+    % writes an stc file
+    %
+    %     filename      output file
+    %     stc           a stucture containing the stc data with fields:
+    %
+    %     tmin          The time of the first frame in seconds
+    %     tstep         Time between frames in seconds
+    %     vertices      Vertex indices (0 based)
+    %     data          The data matrix nvert * ntime
+    %
+    """
+    fid = open(filename, 'wb')
+
+    # write start time in ms
+    fid.write(np.array(1000*stc['tmin'], dtype='>f4').tostring())
+    # write sampling rate in ms
+    fid.write(np.array(1000*stc['tstep'], dtype='>f4').tostring())
+    # write number of vertices
+    fid.write(np.array(stc['vertices'].shape[0], dtype='>I4').tostring())
+    # write the vertex indices
+    fid.write(np.array(stc['vertices'], dtype='>I4').tostring())
+
+    # write the number of timepts
+    fid.write(np.array(stc['data'].shape[1], dtype='>I4').tostring())
+    #
+    # write the data
+    #
+    fid.write(np.array(stc['data'].T, dtype='>f4').tostring())
+
+    # close the file
+    fid.close()
diff --git a/fiff/tests/test_bem_surfaces.py b/fiff/tests/test_bem_surfaces.py
new file mode 100644
index 0000000..3f4bdfb
--- /dev/null
+++ b/fiff/tests/test_bem_surfaces.py
@@ -0,0 +1,17 @@
+import os
+import os.path as op
+
+import numpy as np
+from numpy.testing import assert_array_almost_equal
+
+import fiff
+
+MNE_SAMPLE_DATASET_PATH = os.getenv('MNE_SAMPLE_DATASET_PATH')
+fname = op.join(MNE_SAMPLE_DATASET_PATH, 'subjects', 'sample', 'bem',
+                                            'sample-5120-bem-sol.fif')
+
+def test_io_bem_surfaces():
+    """Testing reading of bem surfaces
+    """
+    surf = fiff.read_bem_surfaces(fname, add_geom=True)
+    print "Number of surfaces : %d" % len(surf)
diff --git a/fiff/tests/test_forward.py b/fiff/tests/test_forward.py
new file mode 100644
index 0000000..82bd638
--- /dev/null
+++ b/fiff/tests/test_forward.py
@@ -0,0 +1,17 @@
+import os
+import os.path as op
+
+import numpy as np
+from numpy.testing import assert_array_almost_equal, assert_equal
+
+import fiff
+
+MNE_SAMPLE_DATASET_PATH = os.getenv('MNE_SAMPLE_DATASET_PATH')
+fname = op.join(MNE_SAMPLE_DATASET_PATH, 'MEG', 'sample',
+                                            'sample_audvis-ave-7-fwd.fif')
+
+def test_io_forward():
+    """Test IO for forward solutions
+    """
+    fwd = fiff.read_forward_solution(fname)
+    leadfield = fwd['sol']['data']
diff --git a/fiff/tests/test_inverse.py b/fiff/tests/test_inverse.py
new file mode 100644
index 0000000..748d43e
--- /dev/null
+++ b/fiff/tests/test_inverse.py
@@ -0,0 +1,16 @@
+import os
+import os.path as op
+
+import numpy as np
+from numpy.testing import assert_array_almost_equal, assert_equal
+
+import fiff
+
+MNE_SAMPLE_DATASET_PATH = os.getenv('MNE_SAMPLE_DATASET_PATH')
+fname = op.join(MNE_SAMPLE_DATASET_PATH, 'MEG', 'sample',
+                                            'sample_audvis-ave-7-meg-inv.fif')
+
+def test_io_forward():
+    """Test IO for inverse operator
+    """
+    fwd = fiff.read_inverse_operator(fname)
diff --git a/fiff/tests/test_raw.py b/fiff/tests/test_raw.py
new file mode 100644
index 0000000..df98397
--- /dev/null
+++ b/fiff/tests/test_raw.py
@@ -0,0 +1,25 @@
+import os
+import os.path as op
+
+import numpy as np
+from numpy.testing import assert_array_almost_equal, assert_equal
+
+import fiff
+
+MNE_SAMPLE_DATASET_PATH = os.getenv('MNE_SAMPLE_DATASET_PATH')
+fname = op.join(MNE_SAMPLE_DATASET_PATH, 'MEG', 'sample',
+                                            'sample_audvis_raw.fif')
+
+def test_io_raw():
+    """Test IO for raw data
+    """
+    raw = fiff.setup_read_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]
+
+    data, times = fiff.read_raw_segment_times(raw, from_=100, to=115,
+                                              sel=meg_channels_idx)
+
diff --git a/fiff/tests/test_stc.py b/fiff/tests/test_stc.py
new file mode 100644
index 0000000..9ece422
--- /dev/null
+++ b/fiff/tests/test_stc.py
@@ -0,0 +1,24 @@
+import os
+import os.path as op
+
+import numpy as np
+from numpy.testing import assert_array_almost_equal, assert_equal
+
+import fiff
+
+MNE_SAMPLE_DATASET_PATH = os.getenv('MNE_SAMPLE_DATASET_PATH')
+fname = op.join(MNE_SAMPLE_DATASET_PATH, 'MEG', 'sample',
+                                            'sample_audvis-ave-7-meg-lh.stc')
+
+def test_io_stc():
+    """Test IO for STC files
+    """
+    stc = fiff.read_stc(fname)
+
+    fiff.write_stc("tmp.stc", stc)
+    stc2 = fiff.read_stc("tmp.stc")
+
+    assert_array_almost_equal(stc['data'], stc2['data'])
+    assert_array_almost_equal(stc['tmin'], stc2['tmin'])
+    assert_array_almost_equal(stc['vertices'], stc2['vertices'])
+    assert_array_almost_equal(stc['tstep'], stc2['tstep'])

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