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 9e079fb58771bcdf43b6e9329d7617ccf06f7b99 Author: Alexandre Gramfort <[email protected]> Date: Sun Jun 17 17:37:44 2012 +0300 ENH : add tmin = tmax to compute_covariance --- doc/source/whats_new.rst | 2 + .../plot_estimate_covariance_matrix_baseline.py | 51 ++++++++++++++++++++++ ...x.py => plot_estimate_covariance_matrix_raw.py} | 0 mne/cov.py | 16 ++++++- mne/tests/test_cov.py | 30 ++++++++----- 5 files changed, 85 insertions(+), 14 deletions(-) diff --git a/doc/source/whats_new.rst b/doc/source/whats_new.rst index 4372f4b..0beaf5f 100644 --- a/doc/source/whats_new.rst +++ b/doc/source/whats_new.rst @@ -9,6 +9,8 @@ Current Changelog ~~~~~~~~~ + - Add tmin + tmax parameters in mne.compute_covariance to estimate noise covariance in epochs baseline without creating new epochs by `Alex Gramfort`_. + - Add method to regularize a noise covariance by `Alex Gramfort`_. - Read and write measurement info in forward and inverse operators for interactive visualization in mne_analyze by `Alex Gramfort`_. diff --git a/examples/plot_estimate_covariance_matrix_baseline.py b/examples/plot_estimate_covariance_matrix_baseline.py new file mode 100644 index 0000000..43cb625 --- /dev/null +++ b/examples/plot_estimate_covariance_matrix_baseline.py @@ -0,0 +1,51 @@ +""" +======================================== +Estimate covariance matrix from baseline +======================================== + +""" +# Author: Alexandre Gramfort <[email protected]> +# +# License: BSD (3-clause) + +print __doc__ + +import mne +from mne import fiff +from mne.datasets import sample + +data_path = sample.data_path('.') +fname = data_path + '/MEG/sample/sample_audvis_raw.fif' +event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif' +event_id, tmin, tmax = 1, -0.2, 0.5 + +raw = fiff.Raw(fname) + +############################################################################### +# Set parameters +raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' + +# Setup for reading the raw data +raw = fiff.Raw(raw_fname) +events = mne.read_events(event_fname) + +# Set up pick list: EEG + STI 014 - bad channels (modify to your needs) +include = [] # or stim channels ['STI 014'] +exclude = raw.info['bads'] + ['EEG 053'] # bads + 1 more + +# pick EEG channels +picks = fiff.pick_types(raw.info, meg=True, eeg=True, stim=False, eog=True, + include=include, exclude=exclude) +# Read epochs +reject = dict(grad=4000e-13, mag=4e-12, eeg=80e-6, eog=150e-6) +epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, + baseline=(None, 0), reject=reject) + +# Compute the covariance on baseline +cov = mne.compute_covariance(epochs, tmin=None, tmax=0) +print cov + +############################################################################### +# Show covariance +mne.viz.plot_cov(cov, raw.info, exclude=raw.info['bads'], colorbar=True, + proj=True) # try setting proj to False to see the effect diff --git a/examples/plot_estimate_covariance_matrix.py b/examples/plot_estimate_covariance_matrix_raw.py similarity index 100% rename from examples/plot_estimate_covariance_matrix.py rename to examples/plot_estimate_covariance_matrix_raw.py diff --git a/mne/cov.py b/mne/cov.py index 79ae554..d740240 100644 --- a/mne/cov.py +++ b/mne/cov.py @@ -230,7 +230,7 @@ def compute_raw_data_covariance(raw, tmin=None, tmax=None, tstep=0.2, return cov -def compute_covariance(epochs, keep_sample_mean=True): +def compute_covariance(epochs, keep_sample_mean=True, tmin=None, tmax=None): """Estimate noise covariance matrix from epochs The noise covariance is typically estimated on pre-stim periods @@ -257,6 +257,10 @@ def compute_covariance(epochs, keep_sample_mean=True): each event type and subtracted during the covariance computation. This is useful if the evoked response from a previous stimulus extends into the baseline period of the next. + tmin : float | None + Start time for baseline. If None start at first sample. + tmax : float | None + End time for baseline. If None end at last sample. Returns ------- @@ -295,8 +299,16 @@ def compute_covariance(epochs, keep_sample_mean=True): picks_meeg = pick_types(epochs[0].info, meg=True, eeg=True, eog=False) ch_names = [epochs[0].ch_names[k] for k in picks_meeg] for i, epochs_t in enumerate(epochs): + + tstart, tend = None, None + if tmin is not None: + tstart = np.where(epochs_t.times >= tmin)[0][0] + if tmax is not None: + tend = np.where(epochs_t.times <= tmax)[0][-1] + 1 + tslice = slice(tstart, tend, None) + for e in epochs_t: - e = e[picks_meeg] + e = e[picks_meeg][:, tslice] if not keep_sample_mean: data_mean[i] += e data += np.dot(e, e.T) diff --git a/mne/tests/test_cov.py b/mne/tests/test_cov.py index 95c1ac7..9341330 100644 --- a/mne/tests/test_cov.py +++ b/mne/tests/test_cov.py @@ -6,7 +6,7 @@ import numpy as np from scipy import linalg from ..cov import regularize -from .. import Covariance, Epochs, merge_events, \ +from .. import read_cov, Epochs, merge_events, \ find_events, compute_raw_data_covariance, \ compute_covariance from ..fiff import Raw, pick_channels_cov @@ -24,9 +24,9 @@ erm_cov_fname = op.join('mne', 'fiff', 'tests', 'data', def test_io_cov(): """Test IO for noise covariance matrices """ - cov = Covariance(cov_fname) + cov = read_cov(cov_fname) cov.save('cov.fif') - cov2 = Covariance('cov.fif') + cov2 = read_cov('cov.fif') assert_array_almost_equal(cov.data, cov2.data) cov['bads'] = ['EEG 039'] @@ -41,7 +41,7 @@ def test_cov_estimation_on_raw_segment(): """ raw = Raw(raw_fname) cov = compute_raw_data_covariance(raw) - cov_mne = Covariance(erm_cov_fname) + cov_mne = read_cov(erm_cov_fname) assert_true(cov_mne.ch_names == cov.ch_names) print (linalg.norm(cov.data - cov_mne.data, ord='fro') / linalg.norm(cov.data, ord='fro')) @@ -50,7 +50,7 @@ def test_cov_estimation_on_raw_segment(): # test IO when computation done in Python cov.save('test-cov.fif') # test saving - cov_read = Covariance('test-cov.fif') + cov_read = read_cov('test-cov.fif') assert_true(cov_read.ch_names == cov.ch_names) assert_true(cov_read.nfree == cov.nfree) assert_true((linalg.norm(cov.data - cov_read.data, ord='fro') @@ -68,15 +68,21 @@ def test_cov_estimation_with_triggers(): # cov with merged events and keep_sample_mean=True events_merged = merge_events(events, event_ids, 1234) epochs = Epochs(raw, events_merged, 1234, tmin=-0.2, tmax=0, - baseline=(-0.2, -0.1), proj=True, - reject=reject) + baseline=(-0.2, -0.1), proj=True, + reject=reject, preload=True) cov = compute_covariance(epochs, keep_sample_mean=True) - cov_mne = Covariance(cov_km_fname) + cov_mne = read_cov(cov_km_fname) assert_true(cov_mne.ch_names == cov.ch_names) assert_true((linalg.norm(cov.data - cov_mne.data, ord='fro') / linalg.norm(cov.data, ord='fro')) < 0.005) + # Test with tmin and tmax (different but not too much) + cov_tmin_tmax = compute_covariance(epochs, tmin=-0.19, tmax=-0.01) + assert_true(np.all(cov.data != cov_tmin_tmax.data)) + assert_true((linalg.norm(cov.data - cov_tmin_tmax.data, ord='fro') + / linalg.norm(cov_tmin_tmax.data, ord='fro')) < 0.05) + # cov using a list of epochs and keep_sample_mean=True epochs = [Epochs(raw, events, ev_id, tmin=-0.2, tmax=0, baseline=(-0.2, -0.1), proj=True, reject=reject) @@ -88,14 +94,14 @@ def test_cov_estimation_with_triggers(): # cov with keep_sample_mean=False using a list of epochs cov = compute_covariance(epochs, keep_sample_mean=False) - cov_mne = Covariance(cov_fname) + cov_mne = read_cov(cov_fname) assert_true(cov_mne.ch_names == cov.ch_names) assert_true((linalg.norm(cov.data - cov_mne.data, ord='fro') / linalg.norm(cov.data, ord='fro')) < 0.005) # test IO when computation done in Python cov.save('test-cov.fif') # test saving - cov_read = Covariance('test-cov.fif') + cov_read = read_cov('test-cov.fif') assert_true(cov_read.ch_names == cov.ch_names) assert_true(cov_read.nfree == cov.nfree) assert_true((linalg.norm(cov.data - cov_read.data, ord='fro') @@ -105,7 +111,7 @@ def test_cov_estimation_with_triggers(): def test_arithmetic_cov(): """Test arithmetic with noise covariance matrices """ - cov = Covariance(cov_fname) + cov = read_cov(cov_fname) cov_sum = cov + cov assert_array_almost_equal(2 * cov.nfree, cov_sum.nfree) assert_array_almost_equal(2 * cov.data, cov_sum.data) @@ -120,7 +126,7 @@ def test_arithmetic_cov(): def test_regularize_cov(): """Test cov regularization """ - noise_cov = Covariance(cov_fname) + noise_cov = read_cov(cov_fname) raw = Raw(raw_fname) # Regularize noise cov reg_noise_cov = regularize(noise_cov, raw.info, -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git _______________________________________________ debian-med-commit mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/debian-med-commit
