[med-svn] [python-mne] 191/376: API: refactoring inverse pb to pass whitener to solver and not covariance
Yaroslav Halchenko
debian at onerussian.com
Fri Nov 27 17:22:35 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 eb67b7adc3f21436a86b904c18167ad7585e1400
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date: Tue Apr 12 11:48:22 2011 -0400
API: refactoring inverse pb to pass whitener to solver and not covariance
---
examples/plot_minimum_norm_estimate.py | 7 ++++--
mne/cov.py | 39 ++++++++++++++++++++++++++--------
mne/inverse.py | 17 ++++-----------
mne/tests/test_inverse.py | 22 ++++++++++++++++++-
4 files changed, 60 insertions(+), 25 deletions(-)
diff --git a/examples/plot_minimum_norm_estimate.py b/examples/plot_minimum_norm_estimate.py
index b6c88de..490bb20 100644
--- a/examples/plot_minimum_norm_estimate.py
+++ b/examples/plot_minimum_norm_estimate.py
@@ -35,9 +35,12 @@ evoked = Evoked(fname_evoked, setno=setno, baseline=(None, 0))
forward = mne.read_forward_solution(fname_fwd)
noise_cov = mne.Covariance(fname_cov)
+# Compute whitener from noise covariance matrix
+whitener = noise_cov.get_whitener(evoked.info, mag_reg=0.1,
+ grad_reg=0.1, eeg_reg=0.1, pca=True)
# Compute inverse solution
-stc, K, W = mne.minimum_norm(evoked, forward, noise_cov, orientation='loose',
- method='dspm', snr=3, loose=0.2, pca=True)
+stc, K, W = mne.minimum_norm(evoked, forward, whitener, orientation='loose',
+ method='dspm', snr=3, loose=0.2)
# Save result in stc files
lh_vertices = stc['inv']['src'][0]['vertno']
diff --git a/mne/cov.py b/mne/cov.py
index 24d8a8b..b7c345c 100755
--- a/mne/cov.py
+++ b/mne/cov.py
@@ -79,7 +79,8 @@ class Covariance(object):
"""save covariance matrix in a FIF file"""
write_cov_file(fname, self._cov)
- def whitener(self, info, mag_reg=0.1, grad_reg=0.1, eeg_reg=0.1, pca=True):
+ def get_whitener(self, info, mag_reg=0.1, grad_reg=0.1, eeg_reg=0.1,
+ pca=True):
"""Compute whitener based on a list of channels
Parameters
@@ -104,13 +105,16 @@ class Covariance(object):
Returns
-------
- W : array
- Whitening matrix
- ch_names : list of strings
- List of channel names on which to apply the whitener.
- It corresponds to the columns of W.
+ W : instance of Whitener
"""
+ if not 0 <= grad_reg <= 1:
+ raise ValueError('grad_reg should be a scalar between 0 and 1')
+ if not 0 <= mag_reg <= 1:
+ raise ValueError('mag_reg should be a scalar between 0 and 1')
+ if not 0 <= eeg_reg <= 1:
+ raise ValueError('eeg_reg should be a scalar between 0 and 1')
+
if pca and self.kind == 'diagonal':
print "Setting pca to False with a diagonal covariance matrix."
pca = False
@@ -195,7 +199,8 @@ class Covariance(object):
W = np.r_[np.c_[W_meg, np.zeros((W_meg.shape[0], W_eeg.shape[1]))],
np.c_[np.zeros((W_eeg.shape[0], W_meg.shape[1])), W_eeg]]
- return W, names_meg + names_eeg
+ whitener = Whitener(W, names_meg + names_eeg)
+ return whitener
def __repr__(self):
s = "kind : %s" % self.kind
@@ -203,6 +208,22 @@ class Covariance(object):
s += ", data : %s" % self.data
return "Covariance (%s)" % s
+
+class Whitener(object):
+ """Whitener
+
+ Attributes
+ ----------
+ W : array
+ Whiten matrix
+ ch_names : list of strings
+ Channel names (columns of W)
+ """
+
+ def __init__(self, W, ch_names):
+ self.W = W
+ self.ch_names = ch_names
+
###############################################################################
# IO
@@ -341,8 +362,8 @@ def _estimate_noise_covariance_from_epochs(epochs, bmin, bmax, reject, flat,
continue
e = e[picks_no_eog]
- mu = e[:,bmask].mean(axis=1)
- e -= mu[:,None]
+ mu = e[:, bmask].mean(axis=1)
+ e -= mu[:, None]
if not keep_sample_mean:
e -= np.mean(e, axis=0)
data += np.dot(e, e.T)
diff --git a/mne/inverse.py b/mne/inverse.py
index 31ad3a6..e563b61 100755
--- a/mne/inverse.py
+++ b/mne/inverse.py
@@ -534,11 +534,10 @@ def _xyz2lf(Lf_xyz, normals):
return Lf_cortex
-def minimum_norm(evoked, forward, cov, picks=None, method='dspm',
+def minimum_norm(evoked, forward, whitener, method='dspm',
orientation='fixed', snr=3, loose=0.2, depth=True,
- weight_exp=0.5, weight_limit=10, mag_reg=0.1,
- grad_reg=0.1, eeg_reg=0.1, fmri=None, fmri_thresh=None,
- fmri_off=0.1, pca=True):
+ weight_exp=0.5, weight_limit=10, fmri=None, fmri_thresh=None,
+ fmri_off=0.1):
"""Minimum norm estimate (MNE)
Compute MNE, dSPM and sLORETA on evoked data starting from
@@ -552,8 +551,6 @@ def minimum_norm(evoked, forward, cov, picks=None, method='dspm',
Forward operator
cov : Covariance
Noise covariance matrix
- picks : array-like
- List of indices of channels to include
method : 'wmne' | 'dspm' | 'sloreta'
The method to use
orientation : 'fixed' | 'free' | 'loose'
@@ -597,12 +594,6 @@ def minimum_norm(evoked, forward, cov, picks=None, method='dspm',
' 0, or empty for no loose orientations.')
if not 0 <= weight_exp <= 1:
raise ValueError('weight_exp should be a scalar between 0 and 1')
- if not 0 <= grad_reg <= 1:
- raise ValueError('grad_reg should be a scalar between 0 and 1')
- if not 0 <= mag_reg <= 1:
- raise ValueError('mag_reg should be a scalar between 0 and 1')
- if not 0 <= eeg_reg <= 1:
- raise ValueError('eeg_reg should be a scalar between 0 and 1')
# Set regularization parameter based on SNR
lambda2 = 1.0 / snr**2
@@ -612,7 +603,7 @@ def minimum_norm(evoked, forward, cov, picks=None, method='dspm',
normals.append(s['nn'][s['inuse'] != 0])
normals = np.concatenate(normals)
- W, ch_names = cov.whitener(evoked.info, mag_reg, grad_reg, eeg_reg, pca)
+ W, ch_names = whitener.W, whitener.ch_names
gain = forward['sol']['data']
fwd_ch_names = [forward['chs'][k]['ch_name'] for k in range(gain.shape[0])]
diff --git a/mne/tests/test_inverse.py b/mne/tests/test_inverse.py
index 8589fde..6321089 100755
--- a/mne/tests/test_inverse.py
+++ b/mne/tests/test_inverse.py
@@ -12,9 +12,13 @@ fname_inv = op.join(data_path, 'MEG', 'sample',
'sample_audvis-meg-oct-6-meg-inv.fif')
fname_data = op.join(data_path, 'MEG', 'sample',
'sample_audvis-ave.fif')
+fname_cov = op.join(data_path, 'MEG', 'sample',
+ 'sample_audvis-cov.fif')
+fname_fwd = op.join(data_path, 'MEG', 'sample',
+ 'sample_audvis-meg-eeg-oct-6-fwd.fif')
-def test_compute_mne_inverse():
+def test_apply_mne_inverse_operator():
"""Test MNE inverse computation
"""
@@ -30,3 +34,19 @@ def test_compute_mne_inverse():
assert np.all(res['sol'] > 0)
assert np.all(res['sol'] < 35)
+
+
+def test_compute_minimum_norm():
+ """Test MNE inverse computation
+ """
+
+ setno = 0
+ noise_cov = mne.Covariance(fname_cov)
+ forward = mne.read_forward_solution(fname_fwd)
+ evoked = mne.fiff.Evoked(fname_data, setno=setno, baseline=(None, 0))
+ whitener = noise_cov.get_whitener(evoked.info, mag_reg=0.1,
+ grad_reg=0.1, eeg_reg=0.1, pca=True)
+ stc, K, W = mne.minimum_norm(evoked, forward, whitener, orientation='loose',
+ method='dspm', snr=3, loose=0.2)
+
+ # XXX : test something
--
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