[med-svn] [python-mne] 47/376: fix tests + examples
Yaroslav Halchenko
debian at onerussian.com
Fri Nov 27 17:22:04 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 81f317daeb3719d879fd894737a0661d64130a30
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date: Mon Jan 24 15:01:10 2011 -0500
fix tests + examples
---
examples/plot_compute_mne_inverse.py | 10 ++++++---
examples/plot_read_noise_covariance_matrix.py | 14 ++++++-------
examples/{read_stc.py => plot_read_stc.py} | 13 +++++++++++-
examples/read_forward.py | 4 +++-
mne/cov.py | 29 +++++++++++++--------------
mne/tests/test_stc.py | 6 ++++--
6 files changed, 47 insertions(+), 29 deletions(-)
diff --git a/examples/plot_compute_mne_inverse.py b/examples/plot_compute_mne_inverse.py
index 8cf7f7f..6591c76 100644
--- a/examples/plot_compute_mne_inverse.py
+++ b/examples/plot_compute_mne_inverse.py
@@ -15,6 +15,8 @@ and stores the solution in stc files for visualisation.
print __doc__
import os
+import numpy as np
+import pylab as pl
import mne
fname_inv = os.environ['MNE_SAMPLE_DATASET_PATH']
@@ -35,14 +37,16 @@ rh_vertices = res['inv']['src'][1]['vertno']
lh_data = res['sol'][:len(lh_vertices)]
rh_data = res['sol'][len(rh_vertices):]
-# Save result in stc file
+# Save result in stc files
mne.write_stc('mne_dSPM_inverse-lh.stc', tmin=res['tmin'], tstep=res['tstep'],
vertices=lh_vertices, data=lh_data)
mne.write_stc('mne_dSPM_inverse-rh.stc', tmin=res['tmin'], tstep=res['tstep'],
vertices=rh_vertices, data=rh_data)
-import pylab as pl
-pl.plot(res['sol'][::100,:].T)
+###############################################################################
+# View activation time-series
+times = res['tmin'] + res['tstep'] * np.arange(lh_data.shape[1])
+pl.plot(times, res['sol'][::100,:].T)
pl.xlabel('time (ms)')
pl.ylabel('Source amplitude')
pl.show()
diff --git a/examples/plot_read_noise_covariance_matrix.py b/examples/plot_read_noise_covariance_matrix.py
index b93382b..7a3eee4 100644
--- a/examples/plot_read_noise_covariance_matrix.py
+++ b/examples/plot_read_noise_covariance_matrix.py
@@ -25,10 +25,10 @@ fid.close()
# Writing
mne.write_cov_file('cov.fif', cov)
-print "covariance matrix size: %s x %s" % cov['data'].shape
-
-###############################################################################
-# Show covariance
-import pylab as pl
-pl.matshow(cov['data'])
-pl.show()
+# print "covariance matrix size: %s x %s" % cov['data'].shape
+#
+# ###############################################################################
+# # Show covariance
+# import pylab as pl
+# pl.matshow(cov['data'])
+# pl.show()
diff --git a/examples/read_stc.py b/examples/plot_read_stc.py
similarity index 64%
rename from examples/read_stc.py
rename to examples/plot_read_stc.py
index fe6d107..f35f06e 100644
--- a/examples/read_stc.py
+++ b/examples/plot_read_stc.py
@@ -12,9 +12,12 @@ reconstructions
print __doc__
+import os
+import numpy as np
import mne
-fname = 'MNE-sample-data/MEG/sample/sample_audvis-meg-lh.stc'
+fname = os.environ['MNE_SAMPLE_DATASET_PATH']
+fname += '/MEG/sample/sample_audvis-meg-lh.stc'
stc = mne.read_stc(fname)
@@ -24,3 +27,11 @@ 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)
+
+# View source activations
+times = stc['tmin'] + stc['tstep'] * np.arange(n_samples)
+import pylab as pl
+pl.plot(times, stc['data'][::100,:].T)
+pl.xlabel('time (ms)')
+pl.ylabel('Source amplitude')
+pl.show()
diff --git a/examples/read_forward.py b/examples/read_forward.py
index 11aa940..583f6d3 100644
--- a/examples/read_forward.py
+++ b/examples/read_forward.py
@@ -9,9 +9,11 @@ Reading a forward operator a.k.a. lead field matrix
print __doc__
+import os
import mne
-fname = 'MNE-sample-data/MEG/sample/sample_audvis-meg-oct-6-fwd.fif'
+fname = os.environ['MNE_SAMPLE_DATASET_PATH']
+fname += '/MEG/sample/sample_audvis-meg-oct-6-fwd.fif'
data = mne.read_forward_solution(fname)
leadfield = data['sol']['data']
diff --git a/mne/cov.py b/mne/cov.py
index 16882ef..096387c 100644
--- a/mne/cov.py
+++ b/mne/cov.py
@@ -65,7 +65,8 @@ def read_cov(fid, node, cov_kind):
else:
names = tag.data.split(':')
if len(names) != dim:
- raise ValueError, 'Number of names does not match covariance matrix dimension'
+ raise ValueError, ('Number of names does not match '
+ 'covariance matrix dimension')
tag = find_tag(fid, this, FIFF.FIFF_MNE_COV)
if tag is None:
@@ -76,7 +77,8 @@ def read_cov(fid, node, cov_kind):
# Diagonal is stored
data = tag.data
diagmat = True
- print '\t%d x %d diagonal covariance (kind = %d) found.' % (dim, dim, cov_kind)
+ print '\t%d x %d diagonal covariance (kind = %d) found.' \
+ % (dim, dim, cov_kind)
else:
from scipy import sparse
@@ -87,12 +89,14 @@ def read_cov(fid, node, cov_kind):
data[np.tril(np.ones((dim, dim))) > 0] = vals
data = data + data.T
data.flat[::dim+1] /= 2.0
- diagmat = False;
- print '\t%d x %d full covariance (kind = %d) found.' % (dim, dim, cov_kind)
+ diagmat = False
+ print '\t%d x %d full covariance (kind = %d) found.' \
+ % (dim, dim, cov_kind)
else:
diagmat = False
data = tag.data
- print '\t%d x %d sparse covariance (kind = %d) found.' % (dim, dim, cov_kind)
+ print '\t%d x %d sparse covariance (kind = %d) found.' \
+ % (dim, dim, cov_kind)
# Read the possibly precomputed decomposition
tag1 = find_tag(fid, this, FIFF.FIFF_MNE_COV_EIGENVALUES)
@@ -140,7 +144,7 @@ def write_cov(fid, cov):
write_int(fid, FIFF.FIFF_MNE_COV_KIND, cov['kind'])
write_int(fid, FIFF.FIFF_MNE_COV_DIM, cov['dim'])
if cov['nfree'] > 0:
- write_int(fid, FIFF.FIFF_MNE_COV_NFREE, cov['nfree']);
+ write_int(fid, FIFF.FIFF_MNE_COV_NFREE, cov['nfree'])
# Channel names
if cov['names'] is not None:
@@ -148,17 +152,12 @@ def write_cov(fid, cov):
# Data
if cov['diag']:
- write_double(fid, FIFF.FIFF_MNE_COV_DIAG, cov['data']);
+ write_double(fid, FIFF.FIFF_MNE_COV_DIAG, cov['data'])
else:
+ # Store only lower part of covariance matrix
dim = cov['dim']
- vals = np.empty(dim*(dim + 1)/2)
- # XXX : should be improved later
- q = 0
- for j in range(dim):
- for k in range(j):
- vals[q] = cov['data'][j,k]
- q = q + 1
-
+ mask = np.tril(np.ones((dim, dim), dtype=np.bool)) > 0
+ vals = cov['data'][mask].ravel()
write_double(fid, FIFF.FIFF_MNE_COV, vals)
# Eigenvalues and vectors if present
diff --git a/mne/tests/test_stc.py b/mne/tests/test_stc.py
index 8dc2faa..634d4d6 100644
--- a/mne/tests/test_stc.py
+++ b/mne/tests/test_stc.py
@@ -1,7 +1,7 @@
import os
import os.path as op
-from numpy.testing import assert_array_almost_equal, assert_equal
+from numpy.testing import assert_array_almost_equal
import mne
@@ -9,12 +9,14 @@ MNE_SAMPLE_DATASET_PATH = os.getenv('MNE_SAMPLE_DATASET_PATH')
fname = op.join(MNE_SAMPLE_DATASET_PATH, 'MEG', 'sample',
'sample_audvis-meg-lh.stc')
+
def test_io_stc():
"""Test IO for STC files
"""
stc = mne.read_stc(fname)
- mne.write_stc("tmp.stc", stc)
+ mne.write_stc("tmp.stc", stc['tmin'], stc['tstep'],
+ stc['vertices'], stc['data'])
stc2 = mne.read_stc("tmp.stc")
assert_array_almost_equal(stc['data'], stc2['data'])
--
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