def test_cov_estimation_on_raw(): """Test estimation from raw (typically empty room)""" tempdir = _TempDir() raw = Raw(raw_fname, preload=False) cov_mne = read_cov(erm_cov_fname) cov = compute_raw_covariance(raw, tstep=None) assert_equal(cov.ch_names, cov_mne.ch_names) assert_equal(cov.nfree, cov_mne.nfree) assert_snr(cov.data, cov_mne.data, 1e4) cov = compute_raw_covariance(raw) # tstep=0.2 (default) assert_equal(cov.nfree, cov_mne.nfree - 119) # cutoff some samples assert_snr(cov.data, cov_mne.data, 1e2) # test IO when computation done in Python cov.save(op.join(tempdir, 'test-cov.fif')) # test saving cov_read = read_cov(op.join(tempdir, 'test-cov.fif')) assert_true(cov_read.ch_names == cov.ch_names) assert_true(cov_read.nfree == cov.nfree) assert_array_almost_equal(cov.data, cov_read.data) # test with a subset of channels picks = pick_channels(raw.ch_names, include=raw.ch_names[:5]) cov = compute_raw_covariance(raw, picks=picks, tstep=None) assert_true(cov_mne.ch_names[:5] == cov.ch_names) assert_snr(cov.data, cov_mne.data[picks][:, picks], 1e4) cov = compute_raw_covariance(raw, picks=picks) assert_snr(cov.data, cov_mne.data[picks][:, picks], 90) # cutoff samps # make sure we get a warning with too short a segment raw_2 = raw.crop(0, 1) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') cov = compute_raw_covariance(raw_2) assert_true(any('Too few samples' in str(ww.message) for ww in w))
def test_cov_estimation_on_raw_segment(): """Test estimation from raw on continuous recordings (typically empty room) """ tempdir = _TempDir() raw = Raw(raw_fname, preload=False) cov = compute_raw_covariance(raw) cov_mne = read_cov(erm_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') < 1e-4) # test IO when computation done in Python cov.save(op.join(tempdir, 'test-cov.fif')) # test saving cov_read = read_cov(op.join(tempdir, 'test-cov.fif')) assert_true(cov_read.ch_names == cov.ch_names) assert_true(cov_read.nfree == cov.nfree) assert_array_almost_equal(cov.data, cov_read.data) # test with a subset of channels picks = pick_channels(raw.ch_names, include=raw.ch_names[:5]) cov = compute_raw_covariance(raw, picks=picks) assert_true(cov_mne.ch_names[:5] == cov.ch_names) assert_true(linalg.norm(cov.data - cov_mne.data[picks][:, picks], ord='fro') / linalg.norm(cov.data, ord='fro') < 1e-4) # make sure we get a warning with too short a segment raw_2 = raw.crop(0, 1) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') cov = compute_raw_covariance(raw_2) assert_true(len(w) == 1)
def test_compute_whitener(proj, pca): """Test properties of compute_whitener.""" raw = read_raw_fif(raw_fname).crop(0, 3).load_data() raw.pick_types(eeg=True, exclude=()) if proj: raw.apply_proj() else: raw.del_proj() with pytest.warns(RuntimeWarning, match='Too few samples'): cov = compute_raw_covariance(raw) W, _, C = compute_whitener(cov, raw.info, pca=pca, return_colorer=True, verbose='error') n_channels = len(raw.ch_names) n_reduced = len(raw.ch_names) rank = n_channels - len(raw.info['projs']) n_reduced = rank if pca is True else n_channels assert W.shape == C.shape[::-1] == (n_reduced, n_channels) # round-trip mults round_trip = np.dot(W, C) if pca is True: assert_allclose(round_trip, np.eye(n_reduced), atol=1e-7) elif pca == 'white': # Our first few rows/cols are zeroed out in the white space assert_allclose(round_trip[-rank:, -rank:], np.eye(rank), atol=1e-7) else: assert pca is False assert_allclose(round_trip, np.eye(n_channels), atol=0.05)
def compute_noise_cov(subject, hcp_path, noise_cov_fname=''): if noise_cov_fname == '': noise_cov_fname = meg.NOISE_COV.format(cond='empty_room') if op.isfile(noise_cov_fname): noise_cov = mne.read_cov(noise_cov_fname) return noise_cov utils.make_dir(utils.get_parent_fol(noise_cov_fname)) raw_noise = hcp.read_raw(subject=subject, hcp_path=hcp_path, data_type='noise_empty_room') raw_noise.load_data() # apply ref channel correction and drop ref channels preproc.apply_ref_correction(raw_noise) raw_noise.filter(0.50, None, method='iir', iir_params=dict(order=4, ftype='butter'), n_jobs=1) raw_noise.filter(None, 60, method='iir', iir_params=dict(order=4, ftype='butter'), n_jobs=1) ############################################################################## # Note that using the empty room noise covariance will inflate the SNR of the # evkoked and renders comparisons to `baseline` rather uninformative. noise_cov = mne.compute_raw_covariance(raw_noise, method='empirical') noise_cov.save(noise_cov_fname) return noise_cov
def test_lcmv_raw(): """Test LCMV with raw data """ raw, _, _, _, noise_cov, label, forward, _, _, _ =\ _get_data(all_forward=False, epochs=False, data_cov=False) tmin, tmax = 0, 20 start, stop = raw.time_as_index([tmin, tmax]) # use only the left-temporal MEG channels for LCMV left_temporal_channels = mne.read_selection('Left-temporal') picks = mne.pick_types(raw.info, meg=True, exclude='bads', selection=left_temporal_channels) data_cov = mne.compute_raw_covariance(raw, tmin=tmin, tmax=tmax) stc = lcmv_raw(raw, forward, noise_cov, data_cov, reg=0.01, label=label, start=start, stop=stop, picks=picks) assert_array_almost_equal(np.array([tmin, tmax]), np.array([stc.times[0], stc.times[-1]]), decimal=2) # make sure we get an stc with vertices only in the lh vertno = [forward['src'][0]['vertno'], forward['src'][1]['vertno']] assert_true(len(stc.vertices[0]) == len(np.intersect1d(vertno[0], label.vertices))) assert_true(len(stc.vertices[1]) == 0)
def compute_cov_from_empty_room(subject, session): deriv_path = config.get_subject_deriv_path(subject=subject, session=session, kind=config.get_kind()) bids_basename = BIDSPath(subject=subject, session=session, task=config.get_task(), acquisition=config.acq, run=None, recording=config.rec, space=config.space, prefix=deriv_path, check=False) raw_er_fname = bids_basename.copy().update(kind=config.get_kind(), task='noise', processing='filt', extension='.fif') cov_fname = bids_basename.copy().update(kind='cov', extension='.fif') extra_params = dict() if not config.use_maxwell_filter and config.allow_maxshield: extra_params['allow_maxshield'] = config.allow_maxshield msg = (f'Computing regularized covariance based on empty-room recording. ' f'Input: {raw_er_fname}, Output: {cov_fname}') logger.info( gen_log_message(message=msg, step=11, subject=subject, session=session)) raw_er = mne.io.read_raw_fif(raw_er_fname, preload=True, **extra_params) cov = mne.compute_raw_covariance(raw_er, method='shrunk', rank='info') cov.save(cov_fname)
def test_lcmv_raw(): """Test LCMV with raw data.""" raw, _, _, _, noise_cov, label, forward, _, _, _ =\ _get_data(all_forward=False, epochs=False, data_cov=False) tmin, tmax = 0, 20 start, stop = raw.time_as_index([tmin, tmax]) # use only the left-temporal MEG channels for LCMV data_cov = mne.compute_raw_covariance(raw, tmin=tmin, tmax=tmax) filters = make_lcmv(raw.info, forward, data_cov, reg=0.01, noise_cov=noise_cov, label=label) stc = apply_lcmv_raw(raw, filters, start=start, stop=stop, max_ori_out='signed') assert_array_almost_equal(np.array([tmin, tmax]), np.array([stc.times[0], stc.times[-1]]), decimal=2) # make sure we get an stc with vertices only in the lh vertno = [forward['src'][0]['vertno'], forward['src'][1]['vertno']] assert len(stc.vertices[0]) == len( np.intersect1d(vertno[0], label.vertices)) assert len(stc.vertices[1]) == 0
def compute_cov_from_empty_room(cfg, subject, session): bids_path = BIDSPath(subject=subject, session=session, task=cfg.task, acquisition=cfg.acq, run=None, recording=cfg.rec, space=cfg.space, extension='.fif', datatype=cfg.datatype, root=cfg.deriv_root, check=False) raw_er_fname = bids_path.copy().update(processing='filt', task='noise', suffix='raw') cov_fname = bids_path.copy().update(suffix='cov') msg = (f'Computing regularized covariance based on empty-room recording. ' f'Input: {raw_er_fname}, Output: {cov_fname}') logger.info( **gen_log_kwargs(message=msg, subject=subject, session=session)) raw_er = mne.io.read_raw_fif(raw_er_fname, preload=True) cov = mne.compute_raw_covariance(raw_er, method='shrunk', rank='info') cov.save(cov_fname)
def test_XdawnTransformer(): """Test _XdawnTransformer.""" # Get data raw, events, picks = _get_data() epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, baseline=None, verbose=False, add_eeg_ref=False) X = epochs._data y = epochs.events[:, -1] # Fit xdt = _XdawnTransformer() xdt.fit(X, y) assert_raises(ValueError, xdt.fit, X, y[1:]) assert_raises(ValueError, xdt.fit, 'foo') # Provide covariance object signal_cov = compute_raw_covariance(raw, picks=picks) xdt = _XdawnTransformer(signal_cov=signal_cov) xdt.fit(X, y) # Provide ndarray signal_cov = np.eye(len(picks)) xdt = _XdawnTransformer(signal_cov=signal_cov) xdt.fit(X, y) # Provide ndarray of bad shape signal_cov = np.eye(len(picks) - 1) xdt = _XdawnTransformer(signal_cov=signal_cov) assert_raises(ValueError, xdt.fit, X, y) # Provide another type signal_cov = 42 xdt = _XdawnTransformer(signal_cov=signal_cov) assert_raises(ValueError, xdt.fit, X, y) # Fit with y as None xdt = _XdawnTransformer() xdt.fit(X) # Compare xdawn and _XdawnTransformer xd = Xdawn(correct_overlap=False) xd.fit(epochs) xdt = _XdawnTransformer() xdt.fit(X, y) assert_array_almost_equal(xd.filters_['cond2'][:, :2], xdt.filters_.reshape(2, 2, 8)[0].T) # Transform testing xdt.transform(X[1:, ...]) # different number of epochs xdt.transform(X[:, :, 1:]) # different number of time assert_raises(ValueError, xdt.transform, X[:, 1:, :]) Xt = xdt.transform(X) assert_raises(ValueError, xdt.transform, 42) # Inverse transform testing Xinv = xdt.inverse_transform(Xt) assert_equal(Xinv.shape, X.shape) xdt.inverse_transform(Xt[1:, ...]) xdt.inverse_transform(Xt[:, :, 1:]) # should raise an error if not correct number of components assert_raises(ValueError, xdt.inverse_transform, Xt[:, 1:, :]) assert_raises(ValueError, xdt.inverse_transform, 42)
def test_XdawnTransformer(): """Test _XdawnTransformer.""" # Get data raw, events, picks = _get_data() raw.del_proj() epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, baseline=None, verbose=False) X = epochs._data y = epochs.events[:, -1] # Fit xdt = _XdawnTransformer() xdt.fit(X, y) pytest.raises(ValueError, xdt.fit, X, y[1:]) pytest.raises(ValueError, xdt.fit, 'foo') # Provide covariance object signal_cov = compute_raw_covariance(raw, picks=picks) xdt = _XdawnTransformer(signal_cov=signal_cov) xdt.fit(X, y) # Provide ndarray signal_cov = np.eye(len(picks)) xdt = _XdawnTransformer(signal_cov=signal_cov) xdt.fit(X, y) # Provide ndarray of bad shape signal_cov = np.eye(len(picks) - 1) xdt = _XdawnTransformer(signal_cov=signal_cov) pytest.raises(ValueError, xdt.fit, X, y) # Provide another type signal_cov = 42 xdt = _XdawnTransformer(signal_cov=signal_cov) pytest.raises(ValueError, xdt.fit, X, y) # Fit with y as None xdt = _XdawnTransformer() xdt.fit(X) # Compare xdawn and _XdawnTransformer xd = Xdawn(correct_overlap=False) xd.fit(epochs) xdt = _XdawnTransformer() xdt.fit(X, y) assert_array_almost_equal(xd.filters_['cond2'][:, :2], xdt.filters_.reshape(2, 2, 8)[0].T) # Transform testing xdt.transform(X[1:, ...]) # different number of epochs xdt.transform(X[:, :, 1:]) # different number of time pytest.raises(ValueError, xdt.transform, X[:, 1:, :]) Xt = xdt.transform(X) pytest.raises(ValueError, xdt.transform, 42) # Inverse transform testing Xinv = xdt.inverse_transform(Xt) assert Xinv.shape == X.shape xdt.inverse_transform(Xt[1:, ...]) xdt.inverse_transform(Xt[:, :, 1:]) # should raise an error if not correct number of components pytest.raises(ValueError, xdt.inverse_transform, Xt[:, 1:, :]) pytest.raises(ValueError, xdt.inverse_transform, 42)
def test_sss_proj(): """Test `meg` proj option.""" raw = read_raw_fif(raw_fname) raw.crop(0, 1.0).load_data().pick_types(exclude=()) raw.pick_channels(raw.ch_names[:51]).del_proj() with pytest.raises(ValueError, match='can only be used with Maxfiltered'): compute_proj_raw(raw, meg='combined') raw_sss = maxwell_filter(raw, int_order=5, ext_order=2) sss_rank = 21 # really low due to channel picking assert len(raw_sss.info['projs']) == 0 for meg, n_proj, want_rank in (('separate', 6, sss_rank), ('combined', 3, sss_rank - 3)): proj = compute_proj_raw(raw_sss, n_grad=3, n_mag=3, meg=meg, verbose='error') this_raw = raw_sss.copy().add_proj(proj).apply_proj() assert len(this_raw.info['projs']) == n_proj sss_proj_rank = _compute_rank_int(this_raw) cov = compute_raw_covariance(this_raw, verbose='error') W, ch_names, rank = compute_whitener(cov, this_raw.info, return_rank=True) assert ch_names == this_raw.ch_names assert want_rank == sss_proj_rank == rank # proper reduction if meg == 'combined': assert this_raw.info['projs'][0]['data']['col_names'] == ch_names else: mag_names = ch_names[2::3] assert this_raw.info['projs'][3]['data']['col_names'] == mag_names
def compute_noise_cov(cov_fname, raw): import os.path as op from mne import compute_raw_covariance, pick_types, write_cov from nipype.utils.filemanip import split_filename as split_f from neuropype_ephy.preproc import create_reject_dict print '***** COMPUTE RAW COV *****' + cov_fname if not op.isfile(cov_fname): data_path, basename, ext = split_f(raw.info['filename']) fname = op.join(data_path, '%s-cov.fif' % basename) reject = create_reject_dict(raw.info) # reject = dict(mag=4e-12, grad=4000e-13, eog=250e-6) picks = pick_types(raw.info, meg=True, ref_meg=False, exclude='bads') noise_cov = compute_raw_covariance(raw, picks=picks, reject=reject) write_cov(fname, noise_cov) else: print '*** NOISE cov file %s exists!!!' % cov_fname return cov_fname
def test_sss_proj(): """Test `meg` proj option.""" raw = read_raw_fif(raw_fname) raw.crop(0, 1.0).load_data().pick_types(meg=True, exclude=()) raw.pick_channels(raw.ch_names[:51]).del_proj() raw_sss = maxwell_filter(raw, int_order=5, ext_order=2) sss_rank = 21 # really low due to channel picking assert len(raw_sss.info['projs']) == 0 for meg, n_proj, want_rank in (('separate', 6, sss_rank), ('combined', 3, sss_rank - 3)): proj = compute_proj_raw(raw_sss, n_grad=3, n_mag=3, meg=meg, verbose='error') this_raw = raw_sss.copy().add_proj(proj).apply_proj() assert len(this_raw.info['projs']) == n_proj sss_proj_rank = _compute_rank_int(this_raw) cov = compute_raw_covariance(this_raw, verbose='error') W, ch_names, rank = compute_whitener(cov, this_raw.info, return_rank=True) assert ch_names == this_raw.ch_names assert want_rank == sss_proj_rank == rank # proper reduction if meg == 'combined': assert this_raw.info['projs'][0]['data']['col_names'] == ch_names else: mag_names = ch_names[2::3] assert this_raw.info['projs'][3]['data']['col_names'] == mag_names
def get_epochs_and_cov(X, y, window=100): """return epochs from array.""" raw_train = toMNE(X, y) picks = range(len(getChannelNames())) events = list() events_id = dict() for j, eid in enumerate(getEventNames()): tmp = find_events(raw_train, stim_channel=eid, verbose=False) tmp[:, -1] = j + 1 events.append(tmp) events_id[eid] = j + 1 # concatenate and sort events events = np.concatenate(events, axis=0) order_ev = np.argsort(events[:, 0]) events = events[order_ev] # epochs = Epochs(raw_train, events, events_id, # tmin=-(window / 500.0) + 1 / 500.0 + 0.150, # tmax=0.150, proj=False, picks=picks, baseline=None, # preload=True, add_eeg_ref=False, verbose=False) epochs = Epochs(raw_train, events, events_id, tmin=-(window / 128.0) + 1 / 128.0 + 0.150, tmax=0.150, proj=False, picks=picks, baseline=None, preload=True, add_eeg_ref=False, verbose=False) cov_signal = compute_raw_covariance(raw_train, verbose=False) # pdb.set_trace() return epochs, cov_signal
def test_maxwell_filter_additional(): """Test processing of Maxwell filtered data""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) data_path = op.join(testing.data_path(download=False)) file_name = 'test_move_anon' raw_fname = op.join(data_path, 'SSS', file_name + '_raw.fif') with warnings.catch_warnings(record=True): # maxshield # Use 2.0 seconds of data to get stable cov. estimate raw = Raw(raw_fname, preload=False, proj=False, allow_maxshield=True).crop(0., 2., False) # Get MEG channels, compute Maxwell filtered data raw.load_data() raw.pick_types(meg=True, eeg=False) int_order, ext_order = 8, 3 raw_sss = maxwell.maxwell_filter(raw, int_order=int_order, ext_order=ext_order) # Test io on processed data tempdir = _TempDir() test_outname = op.join(tempdir, 'test_raw_sss.fif') raw_sss.save(test_outname) raw_sss_loaded = Raw(test_outname, preload=True, proj=False, allow_maxshield=True) # Some numerical imprecision since save uses 'single' fmt assert_allclose(raw_sss_loaded._data[:, :], raw_sss._data[:, :], rtol=1e-6, atol=1e-20) # Test rank of covariance matrices for raw and SSS processed data cov_raw = compute_raw_covariance(raw) cov_sss = compute_raw_covariance(raw_sss) scalings = None cov_raw_rank = _estimate_rank_meeg_cov(cov_raw['data'], raw.info, scalings) cov_sss_rank = _estimate_rank_meeg_cov(cov_sss['data'], raw_sss.info, scalings) assert_equal(cov_raw_rank, raw.info['nchan']) assert_equal(cov_sss_rank, maxwell.get_num_moments(int_order, 0))
def test_add_noise(): """Test noise addition.""" if check_version('numpy', '1.17'): rng = np.random.default_rng(0) else: rng = np.random.RandomState(0) raw = read_raw_fif(raw_fname) raw.del_proj() picks = pick_types(raw.info, meg=True, eeg=True, exclude=()) cov = compute_raw_covariance(raw, picks=picks) with pytest.raises(RuntimeError, match='to be loaded'): add_noise(raw, cov) raw.crop(0, 1).load_data() with pytest.raises(TypeError, match='Raw, Epochs, or Evoked'): add_noise(0., cov) with pytest.raises(TypeError, match='Covariance'): add_noise(raw, 0.) # test a no-op (data preserved) orig_data = raw[:][0] zero_cov = cov.copy() zero_cov['data'].fill(0) add_noise(raw, zero_cov) new_data = raw[:][0] assert_allclose(orig_data, new_data, atol=1e-30) # set to zero to make comparisons easier raw._data[:] = 0. epochs = EpochsArray(np.zeros((1, len(raw.ch_names), 100)), raw.info.copy()) epochs.info['bads'] = [] evoked = epochs.average(picks=np.arange(len(raw.ch_names))) for inst in (raw, epochs, evoked): with catch_logging() as log: add_noise(inst, cov, random_state=rng, verbose=True) log = log.getvalue() want = ('to {0}/{1} channels ({0}' .format(len(cov['names']), len(raw.ch_names))) assert want in log if inst is evoked: inst = EpochsArray(inst.data[np.newaxis], inst.info) if inst is raw: cov_new = compute_raw_covariance(inst, picks=picks) else: cov_new = compute_covariance(inst) assert cov['names'] == cov_new['names'] r = np.corrcoef(cov['data'].ravel(), cov_new['data'].ravel())[0, 1] assert r > 0.99
def test_xdawn_fit(): """Test Xdawn fit.""" # Get data raw, events, picks = _get_data() raw.del_proj() epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, baseline=None, verbose=False) # =========== Basic Fit test ================= # Test base xdawn xd = Xdawn(n_components=2, correct_overlap='auto') xd.fit(epochs) # With these parameters, the overlap correction must be False assert not xd.correct_overlap_ # No overlap correction should give averaged evoked evoked = epochs['cond2'].average() assert_array_equal(evoked.data, xd.evokeds_['cond2'].data) assert_allclose(np.linalg.norm(xd.filters_['cond2'], axis=1), 1) # ========== with signal cov provided ==================== # Provide covariance object signal_cov = compute_raw_covariance(raw, picks=picks) xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov) xd.fit(epochs) # Provide ndarray signal_cov = np.eye(len(picks)) xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov) xd.fit(epochs) # Provide ndarray of bad shape signal_cov = np.eye(len(picks) - 1) xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov) pytest.raises(ValueError, xd.fit, epochs) # Provide another type signal_cov = 42 xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov) pytest.raises(ValueError, xd.fit, epochs) # Fit with baseline correction and overlap correction should throw an # error # XXX This is a buggy test, the epochs here don't overlap epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, baseline=(None, 0), verbose=False) xd = Xdawn(n_components=2, correct_overlap=True) pytest.raises(ValueError, xd.fit, epochs)
def test_maxwell_filter_additional(): """Test processing of Maxwell filtered data""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) data_path = op.join(testing.data_path(download=False)) file_name = 'test_move_anon' raw_fname = op.join(data_path, 'SSS', file_name + '_raw.fif') with warnings.catch_warnings(record=True): # maxshield # Use 2.0 seconds of data to get stable cov. estimate raw = Raw(raw_fname, allow_maxshield=True).crop(0., 2., False) # Get MEG channels, compute Maxwell filtered data raw.load_data() raw.pick_types(meg=True, eeg=False) int_order = 8 raw_sss = maxwell_filter(raw, origin=mf_head_origin, regularize=None, bad_condition='ignore') # Test io on processed data tempdir = _TempDir() test_outname = op.join(tempdir, 'test_raw_sss.fif') raw_sss.save(test_outname) raw_sss_loaded = Raw(test_outname, preload=True) # Some numerical imprecision since save uses 'single' fmt assert_allclose(raw_sss_loaded[:][0], raw_sss[:][0], rtol=1e-6, atol=1e-20) # Test rank of covariance matrices for raw and SSS processed data cov_raw = compute_raw_covariance(raw) cov_sss = compute_raw_covariance(raw_sss) scalings = None cov_raw_rank = _estimate_rank_meeg_cov(cov_raw['data'], raw.info, scalings) cov_sss_rank = _estimate_rank_meeg_cov(cov_sss['data'], raw_sss.info, scalings) assert_equal(cov_raw_rank, raw.info['nchan']) assert_equal(cov_sss_rank, _get_n_moments(int_order))
def test_add_noise(): """Test noise addition.""" rng = np.random.RandomState(0) data_path = testing.data_path() raw = read_raw_fif(data_path + '/MEG/sample/sample_audvis_trunc_raw.fif') raw.del_proj() picks = pick_types(raw.info, eeg=True, exclude=()) cov = compute_raw_covariance(raw, picks=picks) with pytest.raises(RuntimeError, match='to be loaded'): add_noise(raw, cov) raw.crop(0, 1).load_data() with pytest.raises(TypeError, match='Raw, Epochs, or Evoked'): add_noise(0., cov) with pytest.raises(TypeError, match='Covariance'): add_noise(raw, 0.) # test a no-op (data preserved) orig_data = raw[:][0] zero_cov = cov.copy() zero_cov['data'].fill(0) add_noise(raw, zero_cov) new_data = raw[:][0] assert_allclose(orig_data, new_data, atol=1e-30) # set to zero to make comparisons easier raw._data[:] = 0. epochs = EpochsArray(np.zeros((1, len(raw.ch_names), 100)), raw.info.copy()) epochs.info['bads'] = [] evoked = epochs.average(picks=np.arange(len(raw.ch_names))) for inst in (raw, epochs, evoked): with catch_logging() as log: add_noise(inst, cov, random_state=rng, verbose=True) log = log.getvalue() want = ('to {0}/{1} channels ({0}' .format(len(cov['names']), len(raw.ch_names))) assert want in log if inst is evoked: inst = EpochsArray(inst.data[np.newaxis], inst.info) if inst is raw: cov_new = compute_raw_covariance(inst, picks=picks, verbose='error') # samples else: cov_new = compute_covariance(inst, verbose='error') # avg ref assert cov['names'] == cov_new['names'] r = np.corrcoef(cov['data'].ravel(), cov_new['data'].ravel())[0, 1] assert r > 0.99
def compute_cov(cov_object, reject=None, tmax=0, method='shrunk'): if isinstance(cov_object, mne.epochs._BaseEpochs): cov = mne.compute_covariance(cov_object, tmax=tmax, method=method) elif isinstance(cov_object, mne.io.base._BaseRaw): if reject is None: reject, picks = compute_reject_picks(cov_object) cov = mne.compute_raw_covariance(cov_object, reject=reject, picks=picks) return cov
def test_maxwell_filter_additional(): """Test processing of Maxwell filtered data.""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) data_path = op.join(testing.data_path(download=False)) file_name = 'test_move_anon' raw_fname = op.join(data_path, 'SSS', file_name + '_raw.fif') # Use 2.0 seconds of data to get stable cov. estimate raw = read_crop(raw_fname, (0., 2.)) # Get MEG channels, compute Maxwell filtered data raw.load_data() raw.pick_types(meg=True, eeg=False) int_order = 8 raw_sss = maxwell_filter(raw, origin=mf_head_origin, regularize=None, bad_condition='ignore') # Test io on processed data tempdir = _TempDir() test_outname = op.join(tempdir, 'test_raw_sss.fif') raw_sss.save(test_outname) raw_sss_loaded = read_crop(test_outname).load_data() # Some numerical imprecision since save uses 'single' fmt assert_allclose(raw_sss_loaded[:][0], raw_sss[:][0], rtol=1e-6, atol=1e-20) # Test rank of covariance matrices for raw and SSS processed data cov_raw = compute_raw_covariance(raw) cov_sss = compute_raw_covariance(raw_sss) scalings = None cov_raw_rank = _estimate_rank_meeg_cov(cov_raw['data'], raw.info, scalings) cov_sss_rank = _estimate_rank_meeg_cov(cov_sss['data'], raw_sss.info, scalings) assert_equal(cov_raw_rank, raw.info['nchan']) assert_equal(cov_sss_rank, _get_n_moments(int_order))
def test_maxwell_filter_additional(tmpdir): """Test processing of Maxwell filtered data.""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) data_path = op.join(testing.data_path(download=False)) file_name = 'test_move_anon' raw_fname = op.join(data_path, 'SSS', file_name + '_raw.fif') # Use 2.0 seconds of data to get stable cov. estimate raw = read_crop(raw_fname, (0., 2.)) # Get MEG channels, compute Maxwell filtered data raw.load_data() raw.pick_types(meg=True, eeg=False) int_order = 8 raw_sss = maxwell_filter(raw, origin=mf_head_origin, regularize=None, bad_condition='ignore') # Test io on processed data tempdir = str(tmpdir) test_outname = op.join(tempdir, 'test_raw_sss.fif') raw_sss.save(test_outname) raw_sss_loaded = read_crop(test_outname).load_data() # Some numerical imprecision since save uses 'single' fmt assert_allclose(raw_sss_loaded[:][0], raw_sss[:][0], rtol=1e-6, atol=1e-20) # Test rank of covariance matrices for raw and SSS processed data cov_raw = compute_raw_covariance(raw) cov_sss = compute_raw_covariance(raw_sss) scalings = None cov_raw_rank = _compute_rank_int( cov_raw, scalings=scalings, info=raw.info, proj=False) cov_sss_rank = _compute_rank_int( cov_sss, scalings=scalings, info=raw_sss.info, proj=False) assert cov_raw_rank == raw.info['nchan'] assert cov_sss_rank == _get_n_moments(int_order)
def test_cov_estimation_on_raw_reg(): """Test estimation from raw with regularization.""" raw = read_raw_fif(raw_fname, preload=True) raw.info['sfreq'] /= 10. raw = RawArray(raw._data[:, ::10].copy(), raw.info) # decimate for speed cov_mne = read_cov(erm_cov_fname) with pytest.warns(RuntimeWarning, match='Too few samples'): # "diagonal_fixed" is much faster. Use long epochs for speed. cov = compute_raw_covariance(raw, tstep=5., method='diagonal_fixed') assert_snr(cov.data, cov_mne.data, 5)
def test_cov_estimation_on_raw(): """Test estimation from raw (typically empty room)""" tempdir = _TempDir() raw = Raw(raw_fname, preload=True) cov_mne = read_cov(erm_cov_fname) # The pure-string uses the more efficient numpy-based method, the # the list gets triaged to compute_covariance (should be equivalent # but use more memory) for method in ('empirical', ['empirical']): cov = compute_raw_covariance(raw, tstep=None, method=method) assert_equal(cov.ch_names, cov_mne.ch_names) assert_equal(cov.nfree, cov_mne.nfree) assert_snr(cov.data, cov_mne.data, 1e4) cov = compute_raw_covariance(raw, method=method) # tstep=0.2 (default) assert_equal(cov.nfree, cov_mne.nfree - 119) # cutoff some samples assert_snr(cov.data, cov_mne.data, 1e2) # test IO when computation done in Python cov.save(op.join(tempdir, 'test-cov.fif')) # test saving cov_read = read_cov(op.join(tempdir, 'test-cov.fif')) assert_true(cov_read.ch_names == cov.ch_names) assert_true(cov_read.nfree == cov.nfree) assert_array_almost_equal(cov.data, cov_read.data) # test with a subset of channels picks = pick_channels(raw.ch_names, include=raw.ch_names[:5]) raw_pick = raw.pick_channels([raw.ch_names[pick] for pick in picks], copy=True) raw_pick.info.normalize_proj() cov = compute_raw_covariance(raw_pick, picks=picks, tstep=None, method=method) assert_true(cov_mne.ch_names[:5] == cov.ch_names) assert_snr(cov.data, cov_mne.data[picks][:, picks], 1e4) cov = compute_raw_covariance(raw_pick, picks=picks, method=method) assert_snr(cov.data, cov_mne.data[picks][:, picks], 90) # cutoff samps # make sure we get a warning with too short a segment raw_2 = Raw(raw_fname).crop(0, 1) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') cov = compute_raw_covariance(raw_2, method=method) assert_true(any('Too few samples' in str(ww.message) for ww in w))
def test_cov_estimation_on_raw_reg(): """Test estimation from raw with regularization.""" raw = read_raw_fif(raw_fname, preload=True) raw.info['sfreq'] /= 10. raw = RawArray(raw._data[:, ::10].copy(), raw.info) # decimate for speed cov_mne = read_cov(erm_cov_fname) with pytest.warns(RuntimeWarning, match='Too few samples'): # XXX don't use "shrunk" here, for some reason it makes Travis 2.7 # hang... "diagonal_fixed" is much faster. Use long epochs for speed. cov = compute_raw_covariance(raw, tstep=5., method='diagonal_fixed') assert_snr(cov.data, cov_mne.data, 5)
def test_maxwell_filter_additional(): """Test processing of Maxwell filtered data""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) data_path = op.join(testing.data_path(download=False)) file_name = "test_move_anon" raw_fname = op.join(data_path, "SSS", file_name + "_raw.fif") with warnings.catch_warnings(record=True): # maxshield # Use 2.0 seconds of data to get stable cov. estimate raw = Raw(raw_fname, allow_maxshield=True).crop(0.0, 2.0, False) # Get MEG channels, compute Maxwell filtered data raw.load_data() raw.pick_types(meg=True, eeg=False) int_order = 8 raw_sss = maxwell_filter(raw, origin=mf_head_origin, regularize=None, bad_condition="ignore") # Test io on processed data tempdir = _TempDir() test_outname = op.join(tempdir, "test_raw_sss.fif") raw_sss.save(test_outname) raw_sss_loaded = Raw(test_outname, preload=True) # Some numerical imprecision since save uses 'single' fmt assert_allclose(raw_sss_loaded[:][0], raw_sss[:][0], rtol=1e-6, atol=1e-20) # Test rank of covariance matrices for raw and SSS processed data cov_raw = compute_raw_covariance(raw) cov_sss = compute_raw_covariance(raw_sss) scalings = None cov_raw_rank = _estimate_rank_meeg_cov(cov_raw["data"], raw.info, scalings) cov_sss_rank = _estimate_rank_meeg_cov(cov_sss["data"], raw_sss.info, scalings) assert_equal(cov_raw_rank, raw.info["nchan"]) assert_equal(cov_sss_rank, _get_n_moments(int_order))
def test_cov_estimation_on_raw_reg(): """Test estimation from raw with regularization.""" raw = read_raw_fif(raw_fname, preload=True, add_eeg_ref=False) raw.info["sfreq"] /= 10.0 raw = RawArray(raw._data[:, ::10].copy(), raw.info) # decimate for speed cov_mne = read_cov(erm_cov_fname) with warnings.catch_warnings(record=True): # too few samples warnings.simplefilter("always") # XXX don't use "shrunk" here, for some reason it makes Travis 2.7 # hang... "diagonal_fixed" is much faster. Use long epochs for speed. cov = compute_raw_covariance(raw, tstep=5.0, method="diagonal_fixed") assert_snr(cov.data, cov_mne.data, 5)
def test_make_fixed_length_events(): """Test making events of a fixed length.""" raw = read_raw_fif(raw_fname) events = make_fixed_length_events(raw, id=1) assert events.shape[1] == 3 events_zero = make_fixed_length_events(raw, 1, first_samp=False) assert_equal(events_zero[0, 0], 0) assert_array_equal(events_zero[:, 0], events[:, 0] - raw.first_samp) # With limits tmin, tmax = raw.times[[0, -1]] duration = tmax - tmin events = make_fixed_length_events(raw, 1, tmin, tmax, duration) assert_equal(events.shape[0], 1) # With bad limits (no resulting events) pytest.raises(ValueError, make_fixed_length_events, raw, 1, tmin, tmax - 1e-3, duration) # not raw, bad id or duration pytest.raises(TypeError, make_fixed_length_events, raw, 2.3) pytest.raises(TypeError, make_fixed_length_events, 'not raw', 2) pytest.raises(TypeError, make_fixed_length_events, raw, 23, tmin, tmax, 'abc') # Let's try some ugly sample rate/sample count combos data = np.random.RandomState(0).randn(1, 27768) # This breaks unless np.round() is used in make_fixed_length_events info = create_info(1, 155.4499969482422) raw = RawArray(data, info) events = make_fixed_length_events(raw, 1, duration=raw.times[-1]) assert events[0, 0] == 0 assert len(events) == 1 # Without use_rounding=True this breaks raw = RawArray(data[:, :21216], info) events = make_fixed_length_events(raw, 1, duration=raw.times[-1]) assert events[0, 0] == 0 assert len(events) == 1 # Make sure it gets used properly by compute_raw_covariance cov = compute_raw_covariance(raw, tstep=None) expected = np.cov(data[:, :21216]) np.testing.assert_allclose(cov['data'], expected, atol=1e-12) # overlaps events = make_fixed_length_events(raw, 1, duration=1) assert len(events) == 136 events_ol = make_fixed_length_events(raw, 1, duration=1, overlap=0.5) assert len(events_ol) == 271 events_ol_2 = make_fixed_length_events(raw, 1, duration=1, overlap=0.9) assert len(events_ol_2) == 1355 assert_array_equal(events_ol_2[:, 0], np.unique(events_ol_2[:, 0])) with pytest.raises(ValueError, match='overlap must be'): make_fixed_length_events(raw, 1, duration=1, overlap=1.1)
def test_cov_estimation_on_raw_reg(): """Test estimation from raw with regularization.""" raw = read_raw_fif(raw_fname, preload=True) raw.info['sfreq'] /= 10. raw = RawArray(raw._data[:, ::10].copy(), raw.info) # decimate for speed cov_mne = read_cov(erm_cov_fname) with warnings.catch_warnings(record=True): # too few samples warnings.simplefilter('always') # XXX don't use "shrunk" here, for some reason it makes Travis 2.7 # hang... "diagonal_fixed" is much faster. Use long epochs for speed. cov = compute_raw_covariance(raw, tstep=5., method='diagonal_fixed') assert_snr(cov.data, cov_mne.data, 5)
def test_cov_estimation_on_raw(method, tmpdir): """Test estimation from raw (typically empty room).""" tempdir = str(tmpdir) raw = read_raw_fif(raw_fname, preload=True) cov_mne = read_cov(erm_cov_fname) # The pure-string uses the more efficient numpy-based method, the # the list gets triaged to compute_covariance (should be equivalent # but use more memory) with pytest.warns(None): # can warn about EEG ref cov = compute_raw_covariance(raw, tstep=None, method=method, rank='full') assert_equal(cov.ch_names, cov_mne.ch_names) assert_equal(cov.nfree, cov_mne.nfree) assert_snr(cov.data, cov_mne.data, 1e4) # tstep=0.2 (default) with pytest.warns(None): # can warn about EEG ref cov = compute_raw_covariance(raw, method=method, rank='full') assert_equal(cov.nfree, cov_mne.nfree - 119) # cutoff some samples assert_snr(cov.data, cov_mne.data, 1e2) # test IO when computation done in Python cov.save(op.join(tempdir, 'test-cov.fif')) # test saving cov_read = read_cov(op.join(tempdir, 'test-cov.fif')) assert cov_read.ch_names == cov.ch_names assert cov_read.nfree == cov.nfree assert_array_almost_equal(cov.data, cov_read.data) # test with a subset of channels raw_pick = raw.copy().pick_channels(raw.ch_names[:5]) raw_pick.info.normalize_proj() cov = compute_raw_covariance(raw_pick, tstep=None, method=method, rank='full') assert cov_mne.ch_names[:5] == cov.ch_names assert_snr(cov.data, cov_mne.data[:5, :5], 1e4) cov = compute_raw_covariance(raw_pick, method=method, rank='full') assert_snr(cov.data, cov_mne.data[:5, :5], 90) # cutoff samps # make sure we get a warning with too short a segment raw_2 = read_raw_fif(raw_fname).crop(0, 1) with pytest.warns(RuntimeWarning, match='Too few samples'): cov = compute_raw_covariance(raw_2, method=method) # no epochs found due to rejection pytest.raises(ValueError, compute_raw_covariance, raw, tstep=None, method='empirical', reject=dict(eog=200e-6)) # but this should work cov = compute_raw_covariance(raw.copy().crop(0, 10.), tstep=None, method=method, reject=dict(eog=1000e-6), verbose='error')
def test_bad_proj(): """Test dealing with bad projection application.""" raw = read_raw_fif(raw_fname, preload=True) events = read_events(event_fname) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') picks = picks[2:9:3] _check_warnings(raw, events, picks) # still bad raw.pick_channels([raw.ch_names[ii] for ii in picks]) _check_warnings(raw, events) # "fixed" raw.info.normalize_proj() # avoid projection warnings _check_warnings(raw, events, count=0) # eeg avg ref is okay raw = read_raw_fif(raw_fname, preload=True).pick_types(meg=False, eeg=True) raw.set_eeg_reference(projection=True) _check_warnings(raw, events, count=0) raw.info['bads'] = raw.ch_names[:10] _check_warnings(raw, events, count=0) raw = read_raw_fif(raw_fname) pytest.raises(ValueError, raw.del_proj, 'foo') n_proj = len(raw.info['projs']) raw.del_proj(0) assert_equal(len(raw.info['projs']), n_proj - 1) raw.del_proj() assert_equal(len(raw.info['projs']), 0) # Ensure we deal with newer-style Neuromag projs properly, were getting: # # Projection vector "PCA-v2" has magnitude 1.00 (should be unity), # applying projector with 101/306 of the original channels available # may be dangerous. raw = read_raw_fif(raw_fname).crop(0, 1) raw.info['bads'] = ['MEG 0111'] meg_picks = mne.pick_types(raw.info, meg=True, exclude=()) ch_names = [raw.ch_names[pick] for pick in meg_picks] for p in raw.info['projs'][:-1]: data = np.zeros((1, len(ch_names))) idx = [ch_names.index(ch_name) for ch_name in p['data']['col_names']] data[:, idx] = p['data']['data'] p['data'].update(ncol=len(meg_picks), col_names=ch_names, data=data) with warnings.catch_warnings(record=True) as w: mne.cov.regularize(mne.compute_raw_covariance(raw, verbose='error'), raw.info) assert_equal(len(w), 0)
def _compute_cov(subject): fname = subject + '_EC.set' raw = mne.io.read_raw_eeglab(op.join(out_path, fname)) raw.annotations.duration[raw.annotations.description == 'boundary'] = 0.0 raw.annotations.description[raw.annotations.description == 'boundary'] = "edge" raw = _interpolate_missing(raw, ref_info) for fmin, fmax, band in freqs: raw_f = raw.copy().filter(fmin, fmax) cov_f = mne.compute_raw_covariance(raw_f, method="oas") cov_f.save(f"./derivatives/{subject}-{band}-cov.fif")
def createInv(subj): # subjdir = os.environ['SUBJECTS_DIR'] file_path = '/m/nbe/scratch/restmeg/data/camcan/cc700/mri/pipeline/release004/BIDSsep/megraw/sub-' + subj + '/meg/' raw = mne.io.fiff.Raw(file_path + 'rest_raw.fif') fname_trans = file_path + 'rest_raw-trans.fif' src = mne.read_source_spaces( '/m/nbe/scratch/restmeg/data/camcan/subjects/' + subj + '/bem/' + subj + '-oct-6-src.fif') bem_sol = mne.read_bem_solution(subj + '-5120-5120-5120-bem-sol.fif') fwd = mne.make_forward_solution(raw.info, fname_trans, src, bem_sol) cov = mne.compute_raw_covariance(raw) inv = mne.minimum_norm.make_inverse_operator(raw.info, fwd, cov, loose=0.2) mne.minimum_norm.write_inverse_operator(subj + '-inv.fif', inv)
def test_explicit_bads_pick(): """Test when bads channels are explicitly passed + default picks=None.""" raw = read_raw_fif(raw_fname, preload=True) raw.pick_types(eeg=True, meg=True, ref_meg=True) # Covariance # Default picks=None raw.info['bads'] = list() noise_cov_1 = compute_raw_covariance(raw, picks=None) rank = compute_rank(noise_cov_1, info=raw.info) assert rank == dict(meg=303, eeg=60) assert raw.info['bads'] == [] raw.info['bads'] = ['EEG 002', 'EEG 012', 'EEG 015', 'MEG 0122'] noise_cov = compute_raw_covariance(raw, picks=None) rank = compute_rank(noise_cov, info=raw.info) assert rank == dict(meg=302, eeg=57) assert raw.info['bads'] == ['EEG 002', 'EEG 012', 'EEG 015', 'MEG 0122'] # Explicit picks picks = pick_types(raw.info, meg=True, eeg=True, exclude=[]) noise_cov_2 = compute_raw_covariance(raw, picks=picks) rank = compute_rank(noise_cov_2, info=raw.info) assert rank == dict(meg=303, eeg=60) assert raw.info['bads'] == ['EEG 002', 'EEG 012', 'EEG 015', 'MEG 0122'] assert_array_equal(noise_cov_1['data'], noise_cov_2['data']) assert noise_cov_1['names'] == noise_cov_2['names'] # Raw raw.info['bads'] = list() rank = compute_rank(raw) assert rank == dict(meg=303, eeg=60) raw.info['bads'] = ['EEG 002', 'EEG 012', 'EEG 015', 'MEG 0122'] rank = compute_rank(raw) assert rank == dict(meg=302, eeg=57)
def test_cov_estimation_on_raw(): """Test estimation from raw (typically empty room)""" tempdir = _TempDir() raw = Raw(raw_fname, preload=True) cov_mne = read_cov(erm_cov_fname) cov = compute_raw_covariance(raw, tstep=None) assert_equal(cov.ch_names, cov_mne.ch_names) assert_equal(cov.nfree, cov_mne.nfree) assert_snr(cov.data, cov_mne.data, 1e4) cov = compute_raw_covariance(raw) # tstep=0.2 (default) assert_equal(cov.nfree, cov_mne.nfree - 119) # cutoff some samples assert_snr(cov.data, cov_mne.data, 1e2) # test IO when computation done in Python cov.save(op.join(tempdir, 'test-cov.fif')) # test saving cov_read = read_cov(op.join(tempdir, 'test-cov.fif')) assert_true(cov_read.ch_names == cov.ch_names) assert_true(cov_read.nfree == cov.nfree) assert_array_almost_equal(cov.data, cov_read.data) # test with a subset of channels picks = pick_channels(raw.ch_names, include=raw.ch_names[:5]) raw.pick_channels([raw.ch_names[pick] for pick in picks]) raw.info.normalize_proj() cov = compute_raw_covariance(raw, picks=picks, tstep=None) assert_true(cov_mne.ch_names[:5] == cov.ch_names) assert_snr(cov.data, cov_mne.data[picks][:, picks], 1e4) cov = compute_raw_covariance(raw, picks=picks) assert_snr(cov.data, cov_mne.data[picks][:, picks], 90) # cutoff samps # make sure we get a warning with too short a segment raw_2 = Raw(raw_fname).crop(0, 1) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') cov = compute_raw_covariance(raw_2) assert_true(any('Too few samples' in str(ww.message) for ww in w))
def spatial_filter(raw, epochs): print('spatial filtering using xDawn') picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') signal_cov = compute_raw_covariance(raw, picks=picks) xd = Xdawn(n_components=2, signal_cov=signal_cov) print('fiting xDawn') xd.fit(epochs) print('epoch denoising started') epochs_denoised = xd.apply(epochs) print('epoch_denoise complete') return epochs_denoised
def test_bad_proj(): """Test dealing with bad projection application.""" raw = read_raw_fif(raw_fname, preload=True) events = read_events(event_fname) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') picks = picks[2:9:3] _check_warnings(raw, events, picks) # still bad raw.pick_channels([raw.ch_names[ii] for ii in picks]) _check_warnings(raw, events) # "fixed" raw.info.normalize_proj() # avoid projection warnings _check_warnings(raw, events, count=0) # eeg avg ref is okay raw = read_raw_fif(raw_fname, preload=True).pick_types(meg=False, eeg=True) raw.set_eeg_reference() _check_warnings(raw, events, count=0) raw.info['bads'] = raw.ch_names[:10] _check_warnings(raw, events, count=0) raw = read_raw_fif(raw_fname) assert_raises(ValueError, raw.del_proj, 'foo') n_proj = len(raw.info['projs']) raw.del_proj(0) assert_equal(len(raw.info['projs']), n_proj - 1) raw.del_proj() assert_equal(len(raw.info['projs']), 0) # Ensure we deal with newer-style Neuromag projs properly, were getting: # # Projection vector "PCA-v2" has magnitude 1.00 (should be unity), # applying projector with 101/306 of the original channels available # may be dangerous. raw = read_raw_fif(raw_fname).crop(0, 1) raw.info['bads'] = ['MEG 0111'] meg_picks = mne.pick_types(raw.info, meg=True, exclude=()) ch_names = [raw.ch_names[pick] for pick in meg_picks] for p in raw.info['projs'][:-1]: data = np.zeros((1, len(ch_names))) idx = [ch_names.index(ch_name) for ch_name in p['data']['col_names']] data[:, idx] = p['data']['data'] p['data'].update(ncol=len(meg_picks), col_names=ch_names, data=data) with warnings.catch_warnings(record=True) as w: mne.cov.regularize(mne.compute_raw_covariance(raw, verbose='error'), raw.info) assert_equal(len(w), 0)
def test_xdawn_fit(): """Test Xdawn fit.""" # Get data raw, events, picks = _get_data() epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, baseline=None, verbose=False, add_eeg_ref=False) # =========== Basic Fit test ================= # Test base xdawn xd = Xdawn(n_components=2, correct_overlap='auto') xd.fit(epochs) # With these parameters, the overlap correction must be False assert_equal(xd.correct_overlap_, False) # No overlap correction should give averaged evoked evoked = epochs['cond2'].average() assert_array_equal(evoked.data, xd.evokeds_['cond2'].data) # ========== with signal cov provided ==================== # Provide covariance object signal_cov = compute_raw_covariance(raw, picks=picks) xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov) xd.fit(epochs) # Provide ndarray signal_cov = np.eye(len(picks)) xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov) xd.fit(epochs) # Provide ndarray of bad shape signal_cov = np.eye(len(picks) - 1) xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov) assert_raises(ValueError, xd.fit, epochs) # Provide another type signal_cov = 42 xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov) assert_raises(ValueError, xd.fit, epochs) # Fit with baseline correction and overlap correction should throw an # error # XXX This is a buggy test, the epochs here don't overlap epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, baseline=(None, 0), verbose=False, add_eeg_ref=False) xd = Xdawn(n_components=2, correct_overlap=True) assert_raises(ValueError, xd.fit, epochs)
def test_xdawn_fit(): """Test Xdawn fit.""" # get data raw, events, picks = _get_data() epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, baseline=None, verbose=False) # =========== Basic Fit test ================= # test base xdawn xd = Xdawn(n_components=2, correct_overlap='auto', signal_cov=None, reg=None) xd.fit(epochs) # with this parameters, the overlapp correction must be False assert_equal(xd.correct_overlap, False) # no overlapp correction should give averaged evoked evoked = epochs['cond2'].average() assert_array_equal(evoked.data, xd.evokeds_['cond2'].data) # ========== with signal cov provided ==================== # provide covariance object signal_cov = compute_raw_covariance(raw, picks=picks) xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov, reg=None) xd.fit(epochs) # provide ndarray signal_cov = np.eye(len(picks)) xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov, reg=None) xd.fit(epochs) # provide ndarray of bad shape signal_cov = np.eye(len(picks) - 1) xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov, reg=None) assert_raises(ValueError, xd.fit, epochs) # provide another type signal_cov = 42 xd = Xdawn(n_components=2, correct_overlap=False, signal_cov=signal_cov, reg=None) assert_raises(ValueError, xd.fit, epochs) # fit with baseline correction and ovverlapp correction should throw an # error epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, baseline=(None, 0), verbose=False) xd = Xdawn(n_components=2, correct_overlap=True) assert_raises(ValueError, xd.fit, epochs)
def compute_noise_cov(cov_fname, raw): """Compute noise covariance data from a continuous segment of raw data. Employ empty room data (collected without the subject) to calculate the full noise covariance matrix. This is recommended for analyzing ongoing spontaneous activity. Parameters ---------- cov_fname : str noise covariance file name raw : Raw the raw data Returns ------- cov_fname : str noise covariance file name in which is saved the noise covariance matrix """ import os.path as op from mne import compute_raw_covariance, pick_types, write_cov from nipype.utils.filemanip import split_filename as split_f from ephypype.preproc import create_reject_dict print(('***** COMPUTE RAW COV *****' + cov_fname)) if not op.isfile(cov_fname): data_path, basename, ext = split_f(raw.info['filename']) fname = op.join(data_path, '%s-cov.fif' % basename) reject = create_reject_dict(raw.info) picks = pick_types(raw.info, meg=True, ref_meg=False, exclude='bads') noise_cov = compute_raw_covariance(raw, picks=picks, reject=reject) write_cov(fname, noise_cov) else: print(('*** NOISE cov file %s exists!!!' % cov_fname)) return cov_fname
def compute_noise_cov(fname_template, raw_filename): """ Compute noise covariance data from a continuous segment of raw data. Employ empty room data (collected without the subject) to calculate the full noise covariance matrix. This is recommended for analyzing ongoing spontaneous activity. Inputs cov_fname : str noise covariance file name template raw_filename : str raw filename Output cov_fname : str noise covariance file name in which is saved the noise covariance matrix """ # Check if cov matrix exists cov_fname = _get_cov_fname(fname_template) if not op.isfile(cov_fname): er_raw, cov_fname = _get_er_data(fname_template) if not op.isfile(cov_fname) and er_raw: reject = _create_reject_dict(er_raw.info) picks = pick_types(er_raw.info, meg=True, ref_meg=False, exclude='bads') noise_cov = compute_raw_covariance(er_raw, picks=picks, reject=reject) write_cov(cov_fname, noise_cov) elif op.isfile(cov_fname): print(('*** NOISE cov file {} exists!!!'.format(cov_fname))) elif not er_raw: cov_fname = compute_cov_identity(raw_filename) else: print(('*** NOISE cov file {} exists!!!'.format(cov_fname))) return cov_fname
def test_cov_estimation_on_raw(): """Test estimation from raw (typically empty room).""" tempdir = _TempDir() raw = read_raw_fif(raw_fname, preload=True) cov_mne = read_cov(erm_cov_fname) # The pure-string uses the more efficient numpy-based method, the # the list gets triaged to compute_covariance (should be equivalent # but use more memory) for method in (None, ['empirical']): # None is cast to 'empirical' cov = compute_raw_covariance(raw, tstep=None, method=method) assert_equal(cov.ch_names, cov_mne.ch_names) assert_equal(cov.nfree, cov_mne.nfree) assert_snr(cov.data, cov_mne.data, 1e4) cov = compute_raw_covariance(raw, method=method) # tstep=0.2 (default) assert_equal(cov.nfree, cov_mne.nfree - 119) # cutoff some samples assert_snr(cov.data, cov_mne.data, 1e2) # test IO when computation done in Python cov.save(op.join(tempdir, 'test-cov.fif')) # test saving cov_read = read_cov(op.join(tempdir, 'test-cov.fif')) assert_true(cov_read.ch_names == cov.ch_names) assert_true(cov_read.nfree == cov.nfree) assert_array_almost_equal(cov.data, cov_read.data) # test with a subset of channels raw_pick = raw.copy().pick_channels(raw.ch_names[:5]) raw_pick.info.normalize_proj() cov = compute_raw_covariance(raw_pick, tstep=None, method=method) assert_true(cov_mne.ch_names[:5] == cov.ch_names) assert_snr(cov.data, cov_mne.data[:5, :5], 1e4) cov = compute_raw_covariance(raw_pick, method=method) assert_snr(cov.data, cov_mne.data[:5, :5], 90) # cutoff samps # make sure we get a warning with too short a segment raw_2 = read_raw_fif(raw_fname).crop(0, 1) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') cov = compute_raw_covariance(raw_2, method=method) assert_true(any('Too few samples' in str(ww.message) for ww in w)) # no epochs found due to rejection assert_raises(ValueError, compute_raw_covariance, raw, tstep=None, method='empirical', reject=dict(eog=200e-6)) # but this should work cov = compute_raw_covariance(raw.copy().crop(0, 10.), tstep=None, method=method, reject=dict(eog=1000e-6))
def test_xdawntransformer_fit(): """Test Xdawn fit.""" # get data raw, events, picks = _get_data() epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, baseline=None, verbose=False) X = epochs._data y = epochs.events[:, -1] # =========== Basic Fit test ================= # test base xdawn xd = XdawnTransformer() xd.fit(X, y) # ========== with signal cov provided ==================== # provide covariance object signal_cov = compute_raw_covariance(raw, picks=picks) xd = XdawnTransformer(signal_cov=signal_cov) xd.fit(X, y) # provide ndarray signal_cov = np.eye(len(picks)) xd = XdawnTransformer(signal_cov=signal_cov) xd.fit(X, y) # provide ndarray of bad shape signal_cov = np.eye(len(picks) - 1) xd = XdawnTransformer(signal_cov=signal_cov) assert_raises(ValueError, xd.fit, X, y) # provide another type signal_cov = 42 xd = XdawnTransformer(signal_cov=signal_cov) assert_raises(ValueError, xd.fit, X, y) # fit with y as None results in error xd = XdawnTransformer() assert_raises(ValueError, xd.fit, X, None) # compare xdawn and XdawnTransformer xd = Xdawn() xd.fit(epochs) xdt = XdawnTransformer() xdt.fit(X, y) assert_array_equal(xdt.filters_['cond2'], xd.filters_['cond2'])
def get_epochs_and_cov(raw_data, picks, window=500): events = list() events_id = dict() for j, eid in enumerate(getEventNames()): tmp = find_events(raw_data, stim_channel=eid, verbose=False) tmp[:, -1] = j + 1 events.append(tmp) events_id[eid] = j + 1 events = np.concatenate(events, axis=0) order_ev = np.argsort(events[:, 0]) events = events[order_ev] epochs = Epochs(raw_data, events, events_id, tmin=-(window / 500.0) + 1 / 500.0 + 0.150, tmax=0.150, proj=False, picks=picks, baseline=None, preload=True, add_eeg_ref=False, verbose=False) cov_signal = compute_raw_covariance(raw_data, verbose=False) return epochs, cov_signal
def get_epochs(raw, events, event_id): event_id_ = dict(normal=event_id) tmin = -0.3 tmax = 0.8 picks = mne.pick_types(raw.info, meg=False, eeg=True, eog=True, exclude='bads') baseline = (tmin, 0) # means from the first instant to t = 0 epochs = mne.Epochs(raw, events, event_id_, tmin, tmax, proj=True, picks=picks, baseline=baseline, preload=True) epochs.decimate(9) tmax = (events[0, 0] - 10000) / raw.info['sfreq'] return epochs, mne.compute_raw_covariance(raw, tmin=0, tmax=tmax, tstep=1, reject=None, flat=None, picks=picks, method='shrunk', method_params=None, cv=3, scalings=None, n_jobs=1, return_estimators=False, reject_by_annotation=True, rank=None, verbose=None)
def test_apply_inverse_cov(method, pick_ori): """Test MNE with precomputed inverse operator on cov.""" raw = read_raw_fif(fname_raw, preload=True) # use 10 sec of data raw.crop(0, 10) raw.filter(1, None) label_lh = read_label(fname_label % 'Aud-lh') # test with a free ori inverse inverse_operator = read_inverse_operator(fname_inv) data_cov = compute_raw_covariance(raw, tstep=None) with pytest.raises(ValueError, match='has not been prepared'): apply_inverse_cov(data_cov, raw.info, inverse_operator, lambda2=lambda2, prepared=True) this_inv_op = prepare_inverse_operator(inverse_operator, nave=1, lambda2=lambda2, method=method) raw_ori = 'normal' if pick_ori == 'normal' else 'vector' stc_raw = apply_inverse_raw( raw, this_inv_op, lambda2, method, label=label_lh, nave=1, pick_ori=raw_ori, prepared=True) stc_cov = apply_inverse_cov( data_cov, raw.info, this_inv_op, method=method, pick_ori=pick_ori, label=label_lh, prepared=True, lambda2=lambda2) n_sources = np.prod(stc_cov.data.shape[:-1]) raw_data = stc_raw.data.reshape(n_sources, -1) exp_res = np.diag(np.cov(raw_data, ddof=1)).copy() exp_res *= 1 if raw_ori == pick_ori else 3. # There seems to be some precision penalty when combining orientations, # but it's probably acceptable rtol = 5e-4 if pick_ori is None else 1e-12 assert_allclose(exp_res, stc_cov.data.ravel(), rtol=rtol) with pytest.raises(ValueError, match='Invalid value'): apply_inverse_cov( data_cov, raw.info, this_inv_op, method=method, pick_ori='vector')
def test_lcmv_raw(): """Test LCMV with raw data.""" raw, _, _, _, noise_cov, label, forward, _, _, _ =\ _get_data(all_forward=False, epochs=False, data_cov=False) tmin, tmax = 0, 20 start, stop = raw.time_as_index([tmin, tmax]) # use only the left-temporal MEG channels for LCMV data_cov = mne.compute_raw_covariance(raw, tmin=tmin, tmax=tmax) stc = lcmv_raw(raw, forward, noise_cov, data_cov, reg=0.01, label=label, start=start, stop=stop, max_ori_out='signed') assert_array_almost_equal(np.array([tmin, tmax]), np.array([stc.times[0], stc.times[-1]]), decimal=2) # make sure we get an stc with vertices only in the lh vertno = [forward['src'][0]['vertno'], forward['src'][1]['vertno']] assert_true(len(stc.vertices[0]) == len(np.intersect1d(vertno[0], label.vertices))) assert_true(len(stc.vertices[1]) == 0)
# field is called "MNE_LOGGING_LEVEL". mne.set_config('MNE_LOGGING_LEVEL', 'INFO') print(mne.get_config(key='MNE_LOGGING_LEVEL')) ############################################################################### # The default value is now set to INFO. This level will now be used by default # every time we call a function in MNE. We can set the global logging level for # only this session by calling :func:`mne.set_log_level` function. mne.set_log_level('WARNING') print(mne.get_config(key='MNE_LOGGING_LEVEL')) ############################################################################### # Notice how the value in the config file was not changed. Logging level of # WARNING only applies for this session. Let's see what logging level of # WARNING prints for :func:`mne.compute_raw_covariance`. cov_raw = mne.compute_raw_covariance(raw) ############################################################################### # Nothing. This means that no warnings were emitted during the computation. If # you look at the documentation of :func:`mne.compute_raw_covariance`, you # notice the ``verbose`` keyword. Setting this parameter does not touch the # configurations, but sets the logging level for just this one function call. # Let's see what happens with logging level of INFO. cov = mne.compute_raw_covariance(raw, verbose=True) ############################################################################### # As you see there is some info about what the function is doing. The logging # level can be set to 'DEBUG', 'INFO', 'WARNING', 'ERROR' or 'CRITICAL'. It can # also be set to an integer or a boolean value. The correspondence to string # values can be seen in the table below. ``verbose=None`` uses the default # value from the configuration file.
def test_rank(): """Test cov rank estimation.""" # Test that our rank estimation works properly on a simple case evoked = read_evokeds(ave_fname, condition=0, baseline=(None, 0), proj=False) cov = read_cov(cov_fname) ch_names = [ch for ch in evoked.info['ch_names'] if '053' not in ch and ch.startswith('EEG')] cov = prepare_noise_cov(cov, evoked.info, ch_names, None) assert_equal(cov['eig'][0], 0.) # avg projector should set this to zero assert_true((cov['eig'][1:] > 0).all()) # all else should be > 0 # Now do some more comprehensive tests raw_sample = read_raw_fif(raw_fname) raw_sss = read_raw_fif(hp_fif_fname) raw_sss.add_proj(compute_proj_raw(raw_sss)) cov_sample = compute_raw_covariance(raw_sample) cov_sample_proj = compute_raw_covariance( raw_sample.copy().apply_proj()) cov_sss = compute_raw_covariance(raw_sss) cov_sss_proj = compute_raw_covariance( raw_sss.copy().apply_proj()) picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True) picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True) info_sample = pick_info(raw_sample.info, picks_all_sample) picks_stack_sample = [('eeg', pick_types(info_sample, meg=False, eeg=True))] picks_stack_sample += [('meg', pick_types(info_sample, meg=True))] picks_stack_sample += [('all', pick_types(info_sample, meg=True, eeg=True))] info_sss = pick_info(raw_sss.info, picks_all_sss) picks_stack_somato = [('eeg', pick_types(info_sss, meg=False, eeg=True))] picks_stack_somato += [('meg', pick_types(info_sss, meg=True))] picks_stack_somato += [('all', pick_types(info_sss, meg=True, eeg=True))] iter_tests = list(itt.product( [(cov_sample, picks_stack_sample, info_sample), (cov_sample_proj, picks_stack_sample, info_sample), (cov_sss, picks_stack_somato, info_sss), (cov_sss_proj, picks_stack_somato, info_sss)], # sss [dict(mag=1e15, grad=1e13, eeg=1e6)] )) for (cov, picks_list, this_info), scalings in iter_tests: for ch_type, picks in picks_list: this_very_info = pick_info(this_info, picks) # compute subset of projs this_projs = [c['active'] and len(set(c['data']['col_names']) .intersection(set(this_very_info['ch_names']))) > 0 for c in cov['projs']] n_projs = sum(this_projs) # count channel types ch_types = [channel_type(this_very_info, idx) for idx in range(len(picks))] n_eeg, n_mag, n_grad = [ch_types.count(k) for k in ['eeg', 'mag', 'grad']] n_meg = n_mag + n_grad if ch_type in ('all', 'eeg'): n_projs_eeg = 1 else: n_projs_eeg = 0 # check sss if len(this_very_info['proc_history']) > 0: mf = this_very_info['proc_history'][0]['max_info'] n_free = _get_sss_rank(mf) if 'mag' not in ch_types and 'grad' not in ch_types: n_free = 0 # - n_projs XXX clarify expected_rank = n_free + n_eeg if n_projs > 0 and ch_type in ('all', 'eeg'): expected_rank -= n_projs_eeg else: expected_rank = n_meg + n_eeg - n_projs C = cov['data'][np.ix_(picks, picks)] est_rank = _estimate_rank_meeg_cov(C, this_very_info, scalings=scalings) assert_equal(expected_rank, est_rank)
# alignement and the sphere location. sphere = mne.make_sphere_model(r0=(0., 0., 0.), head_radius=None) mne.viz.plot_alignment(raw.info, subject='sample', meg='helmet', bem=sphere, dig=True, surfaces=['brain']) del raw, epochs ############################################################################### # To do a dipole fit, let's use the covariance provided by the empty room # recording. raw_erm = read_raw_ctf(erm_path).apply_gradient_compensation(0) raw_erm = mne.preprocessing.maxwell_filter(raw_erm, coord_frame='meg', **mf_kwargs) cov = mne.compute_raw_covariance(raw_erm) del raw_erm dip, residual = fit_dipole(evoked, cov, sphere) ############################################################################### # Compare the actual position with the estimated one. expected_pos = np.array([18., 0., 49.]) diff = np.sqrt(np.sum((dip.pos[0] * 1000 - expected_pos) ** 2)) print('Actual pos: %s mm' % np.array_str(expected_pos, precision=1)) print('Estimated pos: %s mm' % np.array_str(dip.pos[0] * 1000, precision=1)) print('Difference: %0.1f mm' % diff) print('Amplitude: %0.1f nAm' % (1e9 * dip.amplitude[0])) print('GOF: %0.1f %%' % dip.gof[0])
############################################################################## # Compute and apply inverse to PSD estimated using multitaper + Welch. # Group into frequency bands, then normalize each source point and sensor # independently. This makes the value of each sensor point and source location # in each frequency band the percentage of the PSD accounted for by that band. freq_bands = dict( delta=(2, 4), theta=(5, 7), alpha=(8, 12), beta=(15, 29), gamma=(30, 45)) topos = dict(vv=dict(), opm=dict()) stcs = dict(vv=dict(), opm=dict()) snr = 3. lambda2 = 1. / snr ** 2 for kind in kinds: noise_cov = mne.compute_raw_covariance(raw_erms[kind]) inverse_operator = mne.minimum_norm.make_inverse_operator( raws[kind].info, forward=fwd[kind], noise_cov=noise_cov, verbose=True) stc_psd, sensor_psd = mne.minimum_norm.compute_source_psd( raws[kind], inverse_operator, lambda2=lambda2, n_fft=n_fft, dB=False, return_sensor=True, verbose=True) topo_norm = sensor_psd.data.sum(axis=1, keepdims=True) stc_norm = stc_psd.sum() # same operation on MNE object, sum across freqs # Normalize each source point by the total power across freqs for band, limits in freq_bands.items(): data = sensor_psd.copy().crop(*limits).data.sum(axis=1, keepdims=True) topos[kind][band] = mne.EvokedArray( 100 * data / topo_norm, sensor_psd.info) stcs[kind][band] = \ 100 * stc_psd.copy().crop(*limits).sum() / stc_norm.data
def test_rank(): """Test cov rank estimation""" raw_sample = Raw(raw_fname) raw_sss = Raw(hp_fif_fname) raw_sss.add_proj(compute_proj_raw(raw_sss)) cov_sample = compute_raw_covariance(raw_sample) cov_sample_proj = compute_raw_covariance( raw_sample.copy().apply_proj()) cov_sss = compute_raw_covariance(raw_sss) cov_sss_proj = compute_raw_covariance( raw_sss.copy().apply_proj()) picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True) picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True) info_sample = pick_info(raw_sample.info, picks_all_sample) picks_stack_sample = [('eeg', pick_types(info_sample, meg=False, eeg=True))] picks_stack_sample += [('meg', pick_types(info_sample, meg=True))] picks_stack_sample += [('all', pick_types(info_sample, meg=True, eeg=True))] info_sss = pick_info(raw_sss.info, picks_all_sss) picks_stack_somato = [('eeg', pick_types(info_sss, meg=False, eeg=True))] picks_stack_somato += [('meg', pick_types(info_sss, meg=True))] picks_stack_somato += [('all', pick_types(info_sss, meg=True, eeg=True))] iter_tests = list(itt.product( [(cov_sample, picks_stack_sample, info_sample), (cov_sample_proj, picks_stack_sample, info_sample), (cov_sss, picks_stack_somato, info_sss), (cov_sss_proj, picks_stack_somato, info_sss)], # sss [dict(mag=1e15, grad=1e13, eeg=1e6)] )) for (cov, picks_list, this_info), scalings in iter_tests: for ch_type, picks in picks_list: this_very_info = pick_info(this_info, picks) # compute subset of projs this_projs = [c['active'] and len(set(c['data']['col_names']) .intersection(set(this_very_info['ch_names']))) > 0 for c in cov['projs']] n_projs = sum(this_projs) # count channel types ch_types = [channel_type(this_very_info, idx) for idx in range(len(picks))] n_eeg, n_mag, n_grad = [ch_types.count(k) for k in ['eeg', 'mag', 'grad']] n_meg = n_mag + n_grad if ch_type in ('all', 'eeg'): n_projs_eeg = 1 else: n_projs_eeg = 0 # check sss if 'proc_history' in this_very_info: mf = this_very_info['proc_history'][0]['max_info'] n_free = _get_sss_rank(mf) if 'mag' not in ch_types and 'grad' not in ch_types: n_free = 0 # - n_projs XXX clarify expected_rank = n_free + n_eeg if n_projs > 0 and ch_type in ('all', 'eeg'): expected_rank -= n_projs_eeg else: expected_rank = n_meg + n_eeg - n_projs C = cov['data'][np.ix_(picks, picks)] est_rank = _estimate_rank_meeg_cov(C, this_very_info, scalings=scalings) assert_equal(expected_rank, est_rank)
# as the end of the recording, see :func:`mne.compute_raw_covariance`). # # Keep in mind that you want to match your empty room dataset to your # actual MEG data, processing-wise. Ensure that filters # are all the same and if you use ICA, apply it to your empty-room and subject # data equivalently. In this case we did not filter the data and # we don't use ICA. However, we do have bad channels and projections in # the MEG data, and, hence, we want to make sure they get stored in the # covariance object. raw_empty_room.info['bads'] = [ bb for bb in raw.info['bads'] if 'EEG' not in bb] raw_empty_room.add_proj( [pp.copy() for pp in raw.info['projs'] if 'EEG' not in pp['desc']]) noise_cov = mne.compute_raw_covariance( raw_empty_room, tmin=0, tmax=None) ############################################################################### # Now that you have the covariance matrix in an MNE-Python object you can # save it to a file with :func:`mne.write_cov`. Later you can read it back # using :func:`mne.read_cov`. # # You can also use the pre-stimulus baseline to estimate the noise covariance. # First we have to construct the epochs. When computing the covariance, you # should use baseline correction when constructing the epochs. Otherwise the # covariance matrix will be inaccurate. In MNE this is done by default, but # just to be sure, we define it here manually. events = mne.find_events(raw) epochs = mne.Epochs(raw, events, event_id=1, tmin=-0.2, tmax=0.5, baseline=(-0.2, 0.0), decim=3, # we'll decimate for speed verbose='error') # and ignore the warning about aliasing
evoked_std.plot_topomap(times=times, title='Standard') evoked_dev.plot_topomap(times=times, title='Deviant') ############################################################################### # We can see the MMN effect more clearly by looking at the difference between # the two conditions. P50 and N100 are no longer visible, but MMN/P200 and # P300 are emphasised. evoked_difference = combine_evoked([evoked_dev, evoked_std], weights=[1, -1]) evoked_difference.plot(window_title='Difference', gfp=True) ############################################################################### # Source estimation. # We compute the noise covariance matrix from the empty room measurement # and use it for the other runs. reject = dict(mag=4e-12) cov = mne.compute_raw_covariance(raw_erm, reject=reject) cov.plot(raw_erm.info) del raw_erm ############################################################################### # The transformation is read from a file. More information about coregistering # the data, see :ref:`ch_interactive_analysis` or # :func:`mne.gui.coregistration`. trans_fname = op.join(data_path, 'MEG', 'bst_auditory', 'bst_auditory-trans.fif') trans = mne.read_trans(trans_fname) ############################################################################### # To save time and memory, the forward solution is read from a file. Set # ``use_precomputed=False`` in the beginning of this script to build the # forward solution from scratch. The head surfaces for constructing a BEM
data_type='noise_empty_room') raw_noise.load_data() # apply ref channel correction and drop ref channels preproc.apply_ref_correction(raw_noise) # Note: MNE complains on Python 2.7 raw_noise.filter(0.50, None, method='iir', iir_params=dict(order=4, ftype='butter'), n_jobs=1) raw_noise.filter(None, 60, method='iir', iir_params=dict(order=4, ftype='butter'), n_jobs=1) ############################################################################## # Note that using the empty room noise covariance will inflate the SNR of the # evkoked and renders comparisons to `baseline` rather uninformative. noise_cov = mne.compute_raw_covariance(raw_noise, method='empirical') ############################################################################## # Now we assemble the inverse operator, project the data and show the results # on the `fsaverage` surface, the freesurfer average brain. inv_op = mne.minimum_norm.make_inverse_operator( evoked.info, fwd, noise_cov=noise_cov) stc = mne.minimum_norm.apply_inverse( # these data have a pretty high SNR and evoked, inv_op, method='MNE', lambda2=1./9.**2) # 9 is a lovely number. stc = stc.to_original_src( src_outputs['src_fsaverage'], subjects_dir=subjects_dir)