def test_load_fiff_from_raw(): "Test loading data from a fiff raw file" ds = load.fiff.events(raw_path) # test separate events ds_evt = load.fiff.events(events=evt_path) ds_evt.name = ds.name assert_dataobj_equal(ds_evt, ds) # add epochs as ndvar ds = ds.sub('trigger == 32') ds_ndvar = load.fiff.add_epochs(ds, -0.1, 0.3, decim=10, data='mag', proj=False, reject=2e-12) meg = ds_ndvar['meg'] eq_(meg.ndim, 3) data = meg.get_data(('case', 'sensor', 'time')) eq_(data.shape, (14, 102, 6)) # compare with mne epochs ds_mne = load.fiff.add_mne_epochs(ds, -0.1, 0.3, decim=10, proj=False, reject={'mag': 2e-12}) epochs = ds_mne['epochs'] picks = pick_types(epochs.info, meg='mag') mne_data = epochs.get_data()[:, picks] eq_(meg.sensor.names, [epochs.info['ch_names'][i] for i in picks]) assert_array_equal(data, mne_data) # with proj meg = load.fiff.epochs(ds, -0.1, 0.3, decim=10, data='mag', proj=True, reject=2e-12) epochs = load.fiff.mne_epochs(ds, -0.1, 0.3, decim=10, proj=True, reject={'mag': 2e-12}) picks = pick_types(epochs.info, meg='mag') mne_data = epochs.get_data()[:, picks] assert_array_almost_equal(meg.x, mne_data, 10)
def _picks_by_type_old(info, meg_combined=False, ref_meg=False, exclude='bads'): """Use the old, slower _picks_by_type code.""" picks_list = [] has = [_contains_ch_type(info, k) for k in _DATA_CH_TYPES_SPLIT] has = dict(zip(_DATA_CH_TYPES_SPLIT, has)) if has['mag'] and (meg_combined is not True or not has['grad']): picks_list.append( ('mag', pick_types(info, meg='mag', eeg=False, stim=False, ref_meg=ref_meg, exclude=exclude)) ) if has['grad'] and (meg_combined is not True or not has['mag']): picks_list.append( ('grad', pick_types(info, meg='grad', eeg=False, stim=False, ref_meg=ref_meg, exclude=exclude)) ) if has['mag'] and has['grad'] and meg_combined is True: picks_list.append( ('meg', pick_types(info, meg=True, eeg=False, stim=False, ref_meg=ref_meg, exclude=exclude)) ) for ch_type in _DATA_CH_TYPES_SPLIT: if ch_type in ['grad', 'mag']: # exclude just MEG channels continue if has[ch_type]: picks_list.append( (ch_type, pick_types(info, meg=False, stim=False, ref_meg=ref_meg, exclude=exclude, **{ch_type: True})) ) return picks_list
def test_clean_info_bads(): """Test cleaning info['bads'] when bad_channels are excluded """ raw_file = op.join(op.dirname(__file__), "io", "tests", "data", "test_raw.fif") raw = Raw(raw_file) # select eeg channels picks_eeg = pick_types(raw.info, meg=False, eeg=True) # select 3 eeg channels as bads idx_eeg_bad_ch = picks_eeg[[1, 5, 14]] eeg_bad_ch = [raw.info["ch_names"][k] for k in idx_eeg_bad_ch] # select meg channels picks_meg = pick_types(raw.info, meg=True, eeg=False) # select randomly 3 meg channels as bads idx_meg_bad_ch = picks_meg[[0, 15, 34]] meg_bad_ch = [raw.info["ch_names"][k] for k in idx_meg_bad_ch] # simulate the bad channels raw.info["bads"] = eeg_bad_ch + meg_bad_ch # simulate the call to pick_info excluding the bad eeg channels info_eeg = pick_info(raw.info, picks_eeg) # simulate the call to pick_info excluding the bad meg channels info_meg = pick_info(raw.info, picks_meg) assert_equal(info_eeg["bads"], eeg_bad_ch) assert_equal(info_meg["bads"], meg_bad_ch)
def test_check_compensation_consistency(): """Test check picks compensation.""" raw = read_raw_ctf(ctf_fname, preload=False) events = make_fixed_length_events(raw, 99999) picks = pick_types(raw.info, meg=True, exclude=[], ref_meg=True) pick_ch_names = [raw.info['ch_names'][idx] for idx in picks] for (comp, expected_result) in zip([0, 1], [False, False]): raw.apply_gradient_compensation(comp) ret, missing = _bad_chans_comp(raw.info, pick_ch_names) assert ret == expected_result assert len(missing) == 0 Epochs(raw, events, None, -0.2, 0.2, preload=False, picks=picks) picks = pick_types(raw.info, meg=True, exclude=[], ref_meg=False) pick_ch_names = [raw.info['ch_names'][idx] for idx in picks] for (comp, expected_result) in zip([0, 1], [False, True]): raw.apply_gradient_compensation(comp) ret, missing = _bad_chans_comp(raw.info, pick_ch_names) assert ret == expected_result assert len(missing) == 17 if comp != 0: with pytest.raises(RuntimeError, match='Compensation grade 1 has been applied'): Epochs(raw, events, None, -0.2, 0.2, preload=False, picks=picks) else: Epochs(raw, events, None, -0.2, 0.2, preload=False, picks=picks)
def test_find_ecg(): """Test find ECG peaks""" raw = Raw(raw_fname) # once with mag-trick # once with characteristic channel for ch_name in ['MEG 1531', None]: events, ch_ECG, average_pulse, ecg = find_ecg_events( raw, event_id=999, ch_name=None, return_ecg=True) assert_equal(len(raw.times), len(ecg)) n_events = len(events) _, times = raw[0, :] assert_true(55 < average_pulse < 60) picks = pick_types( raw.info, meg='grad', eeg=False, stim=False, eog=False, ecg=True, emg=False, ref_meg=False, exclude='bads') raw.load_data() ecg_epochs = create_ecg_epochs(raw, picks=picks, keep_ecg=True) assert_equal(len(ecg_epochs.events), n_events) assert_true('ECG-SYN' not in raw.ch_names) assert_true('ECG-SYN' in ecg_epochs.ch_names) picks = pick_types( ecg_epochs.info, meg=False, eeg=False, stim=False, eog=False, ecg=True, emg=False, ref_meg=False, exclude='bads') assert_true(len(picks) == 1)
def test_simulate_raw_chpi(): """Test simulation of raw data with cHPI""" with warnings.catch_warnings(record=True): # MaxShield raw = Raw(raw_chpi_fname, allow_maxshield=True) sphere = make_sphere_model('auto', 'auto', raw.info) # make sparse spherical source space sphere_vol = tuple(sphere['r0'] * 1000.) + (sphere.radius * 1000.,) src = setup_volume_source_space('sample', sphere=sphere_vol, pos=70.) stc = _make_stc(raw, src) # simulate data with cHPI on raw_sim = simulate_raw(raw, stc, None, src, sphere, cov=None, chpi=False) raw_chpi = simulate_raw(raw, stc, None, src, sphere, cov=None, chpi=True) # XXX we need to test that the cHPI signals are actually in the correct # place, but that should be a subsequent enhancement (not trivial to do so) psd_sim, freqs_sim = compute_raw_psd(raw_sim) psd_chpi, freqs_chpi = compute_raw_psd(raw_chpi) assert_array_equal(freqs_sim, freqs_chpi) hpi_freqs = np.array([x['custom_ref'][0] for x in raw.info['hpi_meas'][0]['hpi_coils']]) freq_idx = np.sort([np.argmin(np.abs(freqs_sim - f)) for f in hpi_freqs]) picks_meg = pick_types(raw.info, meg=True, eeg=False) picks_eeg = pick_types(raw.info, meg=False, eeg=True) assert_allclose(psd_sim[picks_eeg], psd_chpi[picks_eeg]) assert_true((psd_chpi[picks_meg][:, freq_idx] > 100 * psd_sim[picks_meg][:, freq_idx]).all())
def test_raw_to_nitime(): """ Test nitime export """ raw = Raw(fif_fname, preload=True) picks_meg = pick_types(raw.info, meg=True, exclude='bads') picks = picks_meg[:4] raw_ts = raw.to_nitime(picks=picks) assert_true(raw_ts.data.shape[0] == len(picks)) raw = Raw(fif_fname, preload=False) picks_meg = pick_types(raw.info, meg=True, exclude='bads') picks = picks_meg[:4] raw_ts = raw.to_nitime(picks=picks) assert_true(raw_ts.data.shape[0] == len(picks)) raw = Raw(fif_fname, preload=True) picks_meg = pick_types(raw.info, meg=True, exclude='bads') picks = picks_meg[:4] raw_ts = raw.to_nitime(picks=picks, copy=False) assert_true(raw_ts.data.shape[0] == len(picks)) raw = Raw(fif_fname, preload=False) picks_meg = pick_types(raw.info, meg=True, exclude='bads') picks = picks_meg[:4] raw_ts = raw.to_nitime(picks=picks, copy=False) assert_true(raw_ts.data.shape[0] == len(picks))
def test_raw_to_nitime(): """ Test nitime export """ raw = Raw(fif_fname, preload=True) picks_meg = pick_types(raw.info, meg=True, exclude='bads') picks = picks_meg[:4] with warnings.catch_warnings(record=True): raw_ts = raw.to_nitime(picks=picks) assert_equal(raw_ts.data.shape[0], len(picks)) raw = Raw(fif_fname, preload=False) picks_meg = pick_types(raw.info, meg=True, exclude='bads') picks = picks_meg[:4] with warnings.catch_warnings(record=True): raw_ts = raw.to_nitime(picks=picks) assert_equal(raw_ts.data.shape[0], len(picks)) raw = Raw(fif_fname, preload=True) picks_meg = pick_types(raw.info, meg=True, exclude='bads') picks = picks_meg[:4] with warnings.catch_warnings(record=True): raw_ts = raw.to_nitime(picks=picks, copy=False) assert_equal(raw_ts.data.shape[0], len(picks)) raw = Raw(fif_fname, preload=False) picks_meg = pick_types(raw.info, meg=True, exclude='bads') picks = picks_meg[:4] with warnings.catch_warnings(record=True): raw_ts = raw.to_nitime(picks=picks, copy=False) assert_equal(raw_ts.data.shape[0], len(picks))
def test_data(): """Test reading raw kit files """ raw_py = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path, stim=list(range(167, 159, -1)), slope='+', stimthresh=1, preload=True) print(repr(raw_py)) # Binary file only stores the sensor channels py_picks = pick_types(raw_py.info, exclude='bads') raw_bin = op.join(data_dir, 'test_bin_raw.fif') raw_bin = Raw(raw_bin, preload=True) bin_picks = pick_types(raw_bin.info, stim=True, exclude='bads') data_bin, _ = raw_bin[bin_picks] data_py, _ = raw_py[py_picks] # this .mat was generated using the Yokogawa MEG Reader data_Ykgw = op.join(data_dir, 'test_Ykgw.mat') data_Ykgw = scipy.io.loadmat(data_Ykgw)['data'] data_Ykgw = data_Ykgw[py_picks] assert_array_almost_equal(data_py, data_Ykgw) py_picks = pick_types(raw_py.info, stim=True, ref_meg=False, exclude='bads') data_py, _ = raw_py[py_picks] assert_array_almost_equal(data_py, data_bin) # Make sure concatenation works raw_concat = concatenate_raws([raw_py.copy(), raw_py]) assert_equal(raw_concat.n_times, 2 * raw_py.n_times)
def test_bad_channels(): """Test exception when unsupported channels are used.""" chs = [i for i in _kind_dict] data_chs = _DATA_CH_TYPES_SPLIT + ['eog'] chs_bad = list(set(chs) - set(data_chs)) info = create_info(len(chs), 500, chs) data = np.random.rand(len(chs), 50) raw = RawArray(data, info) data = np.random.rand(100, len(chs), 50) epochs = EpochsArray(data, info) n_components = 0.9 ica = ICA(n_components=n_components, method='fastica') for inst in [raw, epochs]: for ch in chs_bad: # Test case for only bad channels picks_bad1 = pick_types(inst.info, meg=False, **{str(ch): True}) # Test case for good and bad channels picks_bad2 = pick_types(inst.info, meg=True, **{str(ch): True}) assert_raises(ValueError, ica.fit, inst, picks=picks_bad1) assert_raises(ValueError, ica.fit, inst, picks=picks_bad2) assert_raises(ValueError, ica.fit, inst, picks=[])
def test_eog_channel(method): """Test that EOG channel is included when performing ICA.""" _skip_check_picard(method) raw = read_raw_fif(raw_fname, preload=True) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=True, ecg=False, eog=True, exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) n_components = 0.9 ica = ICA(n_components=n_components, method=method) # Test case for MEG and EOG data. Should have EOG channel for inst in [raw, epochs]: picks1a = pick_types(inst.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:4] picks1b = pick_types(inst.info, meg=False, stim=False, ecg=False, eog=True, exclude='bads') picks1 = np.append(picks1a, picks1b) ica.fit(inst, picks=picks1) assert (any('EOG' in ch for ch in ica.ch_names)) # Test case for MEG data. Should have no EOG channel for inst in [raw, epochs]: picks1 = pick_types(inst.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:5] ica.fit(inst, picks=picks1) assert not any('EOG' in ch for ch in ica.ch_names)
def test_epochs_proj(): """Test handling projection (apply proj in Raw or in Epochs) """ exclude = raw.info["bads"] + ["MEG 2443", "EEG 053"] # bads + 2 more this_picks = pick_types(raw.info, meg=True, eeg=False, stim=True, eog=True, exclude=exclude) epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=this_picks, baseline=(None, 0), proj=True) assert_true(all(p["active"] is True for p in epochs.info["projs"])) evoked = epochs.average() assert_true(all(p["active"] is True for p in evoked.info["projs"])) data = epochs.get_data() raw_proj = io.Raw(raw_fname, proj=True) epochs_no_proj = Epochs( raw_proj, events[:4], event_id, tmin, tmax, picks=this_picks, baseline=(None, 0), proj=False ) data_no_proj = epochs_no_proj.get_data() assert_true(all(p["active"] is True for p in epochs_no_proj.info["projs"])) evoked_no_proj = epochs_no_proj.average() assert_true(all(p["active"] is True for p in evoked_no_proj.info["projs"])) assert_true(epochs_no_proj.proj is True) # as projs are active from Raw assert_array_almost_equal(data, data_no_proj, decimal=8) # make sure we can exclude avg ref this_picks = pick_types(raw.info, meg=True, eeg=True, stim=True, eog=True, exclude=exclude) epochs = Epochs( raw, events[:4], event_id, tmin, tmax, picks=this_picks, baseline=(None, 0), proj=True, add_eeg_ref=True ) assert_true(_has_eeg_average_ref_proj(epochs.info["projs"])) epochs = Epochs( raw, events[:4], event_id, tmin, tmax, picks=this_picks, baseline=(None, 0), proj=True, add_eeg_ref=False ) assert_true(not _has_eeg_average_ref_proj(epochs.info["projs"]))
def get_data_picks(inst, meg_combined=False): """Get data channel indices as separate list of tuples Parameters ---------- inst : instance of mne.measuerment_info.Info The info meg_combined : bool Whether to return combined picks for grad and mag. Returns ------- picks_list : list of tuples The list of tuples of picks and the type string. """ info = inst.info picks_list = [] has_mag, has_grad, has_eeg = [k in inst for k in ('mag', 'grad', 'eeg')] if has_mag and (meg_combined is not True or not has_grad): picks_list.append( (pick_types(info, meg='mag', eeg=False, stim=False), 'mag') ) if has_grad and (meg_combined is not True or not has_mag): picks_list.append( (pick_types(info, meg='grad', eeg=False, stim=False), 'grad') ) if has_mag and has_grad and meg_combined is True: picks_list.append( (pick_types(info, meg=True, eeg=False, stim=False), 'meg') ) if has_eeg: picks_list.append( (pick_types(info, meg=False, eeg=True, stim=False), 'eeg') ) return picks_list
def test_simulate_raw_chpi(): """Test simulation of raw data with cHPI""" with warnings.catch_warnings(record=True): # MaxShield raw = Raw(raw_chpi_fname, allow_maxshield=True) sphere = make_sphere_model('auto', 'auto', raw.info) # make sparse spherical source space sphere_vol = tuple(sphere['r0'] * 1000.) + (sphere.radius * 1000.,) src = setup_volume_source_space('sample', sphere=sphere_vol, pos=70.) stc = _make_stc(raw, src) # simulate data with cHPI on raw_sim = simulate_raw(raw, stc, None, src, sphere, cov=None, chpi=False) # need to trim extra samples off this one raw_chpi = simulate_raw(raw, stc, None, src, sphere, cov=None, chpi=True, head_pos=pos_fname) # test that the cHPI signals make some reasonable values psd_sim, freqs_sim = compute_raw_psd(raw_sim) psd_chpi, freqs_chpi = compute_raw_psd(raw_chpi) assert_array_equal(freqs_sim, freqs_chpi) hpi_freqs = _get_hpi_info(raw.info)[0] freq_idx = np.sort([np.argmin(np.abs(freqs_sim - f)) for f in hpi_freqs]) picks_meg = pick_types(raw.info, meg=True, eeg=False) picks_eeg = pick_types(raw.info, meg=False, eeg=True) assert_allclose(psd_sim[picks_eeg], psd_chpi[picks_eeg], atol=1e-20) assert_true((psd_chpi[picks_meg][:, freq_idx] > 100 * psd_sim[picks_meg][:, freq_idx]).all()) # test localization based on cHPI information trans_sim, rot_sim, t_sim = _calculate_chpi_positions(raw_chpi) trans, rot, t = get_chpi_positions(pos_fname) t -= raw.first_samp / raw.info['sfreq'] _compare_positions((trans, rot, t), (trans_sim, rot_sim, t_sim), max_dist=0.005)
def test_compensation_mne(): """Test comensation by comparing with MNE """ def make_evoked(fname, comp): raw = Raw(fname, compensation=comp) picks = pick_types(raw.info, meg=True, ref_meg=True) events = np.array([[0, 0, 1]], dtype=np.int) evoked = Epochs(raw, events, 1, 0, 20e-3, picks=picks).average() return evoked def compensate_mne(fname, comp): tmp_fname = '%s-%d-ave.fif' % (fname[:-4], comp) cmd = ['mne_compensate_data', '--in', fname, '--out', tmp_fname, '--grad', str(comp)] run_subprocess(cmd) return read_evokeds(tmp_fname)[0] # save evoked response with default compensation fname_default = op.join(tempdir, 'ctf_default-ave.fif') make_evoked(ctf_comp_fname, None).save(fname_default) for comp in [0, 1, 2, 3]: evoked_py = make_evoked(ctf_comp_fname, comp) evoked_c = compensate_mne(fname_default, comp) picks_py = pick_types(evoked_py.info, meg=True, ref_meg=True) picks_c = pick_types(evoked_c.info, meg=True, ref_meg=True) assert_allclose(evoked_py.data[picks_py], evoked_c.data[picks_c], rtol=1e-3, atol=1e-17)
def _load_data(): """Helper function to load data.""" # It is more memory efficient to load data in a separate # function so it's loaded on-demand raw = io.read_raw_fif(raw_fname, add_eeg_ref=False) events = read_events(event_name) picks_eeg = pick_types(raw.info, meg=False, eeg=True, exclude=[]) # select every second channel for faster speed but compensate by using # mode='accurate'. picks_meg = pick_types(raw.info, meg=True, eeg=False, exclude=[])[1::2] picks = pick_types(raw.info, meg=True, eeg=True, exclude=[]) with warnings.catch_warnings(record=True): # proj epochs_eeg = Epochs(raw, events, event_id, tmin, tmax, picks=picks_eeg, preload=True, reject=dict(eeg=80e-6)) epochs_meg = Epochs( raw, events, event_id, tmin, tmax, picks=picks_meg, preload=True, reject=dict(grad=1000e-12, mag=4e-12) ) epochs = Epochs( raw, events, event_id, tmin, tmax, picks=picks, preload=True, reject=dict(eeg=80e-6, grad=1000e-12, mag=4e-12), ) return raw, epochs, epochs_eeg, epochs_meg
def test_utils(): """Test utils.""" event_id = {'Visual/Left': 3} tmin, tmax = -0.2, 0.5 events = mne.find_events(raw) picks = mne.pick_channels(raw.info['ch_names'], ['MEG 2443', 'MEG 2442', 'MEG 2441']) epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=None, preload=True) this_epoch = epochs.copy() epochs_clean = clean_by_interp(this_epoch) assert_array_equal(this_epoch.get_data(), epochs.get_data()) assert_raises(AssertionError, assert_array_equal, epochs_clean.get_data(), this_epoch.get_data()) picks_meg = mne.pick_types(evoked.info, meg='grad', eeg=False, exclude=[]) picks_eeg = mne.pick_types(evoked.info, meg=False, eeg=True, exclude=[]) picks_bad_meg = mne.pick_channels(evoked.ch_names, include=['MEG 2443']) picks_bad_eeg = mne.pick_channels(evoked.ch_names, include=['EEG 053']) evoked_orig = evoked.copy() for picks, picks_bad in zip([picks_meg, picks_eeg], [picks_bad_meg, picks_bad_eeg]): evoked_autoreject = interpolate_bads(evoked, picks=picks, reset_bads=False) evoked.interpolate_bads(reset_bads=False) assert_array_equal(evoked.data[picks_bad], evoked_autoreject.data[picks_bad]) assert_raises(AssertionError, assert_array_equal, evoked_orig.data[picks_bad], evoked.data[picks_bad])
def test_check_compensation_consistency(): """Test check picks compensation.""" raw = read_raw_ctf(ctf_fname, preload=False) events = make_fixed_length_events(raw, 99999) picks = pick_types(raw.info, meg=True, exclude=[], ref_meg=True) pick_ch_names = [raw.info['ch_names'][idx] for idx in picks] for (comp, expected_result) in zip([0, 1], [False, False]): raw.apply_gradient_compensation(comp) ret, missing = _bad_chans_comp(raw.info, pick_ch_names) assert ret == expected_result assert len(missing) == 0 Epochs(raw, events, None, -0.2, 0.2, preload=False, picks=picks) picks = pick_types(raw.info, meg=True, exclude=[], ref_meg=False) pick_ch_names = [raw.info['ch_names'][idx] for idx in picks] for (comp, expected_result) in zip([0, 1], [False, True]): raw.apply_gradient_compensation(comp) ret, missing = _bad_chans_comp(raw.info, pick_ch_names) assert ret == expected_result assert len(missing) == 17 with catch_logging() as log: Epochs(raw, events, None, -0.2, 0.2, preload=False, picks=picks, verbose=True) assert'Removing 5 compensators' in log.getvalue()
def _check_dipoles(dipoles, fwd, stc, evoked, residual=None): src = fwd['src'] pos1 = fwd['source_rr'][np.where(src[0]['vertno'] == stc.vertices[0])] pos2 = fwd['source_rr'][np.where(src[1]['vertno'] == stc.vertices[1])[0] + len(src[0]['vertno'])] # Check the position of the two dipoles assert_true(dipoles[0].pos[0] in np.array([pos1, pos2])) assert_true(dipoles[1].pos[0] in np.array([pos1, pos2])) ori1 = fwd['source_nn'][np.where(src[0]['vertno'] == stc.vertices[0])[0]][0] ori2 = fwd['source_nn'][np.where(src[1]['vertno'] == stc.vertices[1])[0] + len(src[0]['vertno'])][0] # Check the orientation of the dipoles assert_true(np.max(np.abs(np.dot(dipoles[0].ori[0], np.array([ori1, ori2]).T))) > 0.99) assert_true(np.max(np.abs(np.dot(dipoles[1].ori[0], np.array([ori1, ori2]).T))) > 0.99) if residual is not None: picks_grad = mne.pick_types(residual.info, meg='grad') picks_mag = mne.pick_types(residual.info, meg='mag') rel_tol = 0.02 for picks in [picks_grad, picks_mag]: assert_true(linalg.norm(residual.data[picks], ord='fro') < rel_tol * linalg.norm(evoked.data[picks], ord='fro'))
def test_brainvision_data(): """Test reading raw Brain Vision files """ assert_raises(IOError, read_raw_brainvision, vmrk_path) assert_raises(ValueError, read_raw_brainvision, vhdr_path, montage, preload=True, scale="foo") raw_py = _test_raw_reader(read_raw_brainvision, test_preloading=True, vhdr_fname=vhdr_path, montage=montage, eog=eog) assert_true('RawBrainVision' in repr(raw_py)) assert_equal(raw_py.info['highpass'], 0.) assert_equal(raw_py.info['lowpass'], 250.) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads') data_py, times_py = raw_py[picks] # compare with a file that was generated using MNE-C raw_bin = Raw(eeg_bin, preload=True) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads') data_bin, times_bin = raw_bin[picks] assert_array_almost_equal(data_py, data_bin) assert_array_almost_equal(times_py, times_bin) # Make sure EOG channels are marked correctly for ch in raw_py.info['chs']: if ch['ch_name'] in eog: assert_equal(ch['kind'], FIFF.FIFFV_EOG_CH) elif ch['ch_name'] == 'STI 014': assert_equal(ch['kind'], FIFF.FIFFV_STIM_CH) elif ch['ch_name'] in raw_py.info['ch_names']: assert_equal(ch['kind'], FIFF.FIFFV_EEG_CH) else: raise RuntimeError("Unknown Channel: %s" % ch['ch_name'])
def test_set_eeg_reference(): """ Test rereference eeg data""" raw = Raw(fif_fname, preload=True) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) # Separate EEG channels from other channel types picks_eeg = pick_types(raw.info, meg=False, eeg=True, exclude='bads') picks_other = pick_types(raw.info, meg=True, eeg=False, eog=True, stim=True, exclude='bads') # Get the raw EEG data and other channel data raw_eeg_data = raw[picks_eeg][0] raw_other_data = raw[picks_other][0] # Get the rereferenced EEG data and channel other reref_eeg_data = reref[picks_eeg][0] unref_eeg_data = reref_eeg_data + ref_data # Undo rereferencing of EEG channels reref_other_data = reref[picks_other][0] # Check that both EEG data and other data is the same assert_allclose(raw_eeg_data, unref_eeg_data, 1e-6, atol=1e-15) assert_allclose(raw_other_data, reref_other_data, 1e-6, atol=1e-15) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref)
def test_ransac(): """Some basic tests for ransac.""" event_id = {'Visual/Left': 3} tmin, tmax = -0.2, 0.5 events = mne.find_events(raw) epochs = mne.Epochs(raw, events, event_id, tmin, tmax, baseline=(None, 0), decim=8, reject=None, preload=True) # normal case picks = mne.pick_types(epochs.info, meg='mag', eeg=False, stim=False, eog=False, exclude=[]) ransac = Ransac(picks=picks) epochs_clean = ransac.fit_transform(epochs) assert_true(len(epochs_clean) == len(epochs)) # Pass numpy instead of epochs X = epochs.get_data() assert_raises(AttributeError, ransac.fit, X) # # should not contain both channel types picks = mne.pick_types(epochs.info, meg=True, eeg=False, stim=False, eog=False, exclude=[]) ransac = Ransac(picks=picks) assert_raises(ValueError, ransac.fit, epochs) # # should not contain other channel types. picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=True, eog=False, exclude=[]) ransac = Ransac(picks=picks) assert_raises(ValueError, ransac.fit, epochs)
def test_brainvision_data(): """Test reading raw Brain Vision files """ raw_py = read_raw_brainvision(vhdr_path, elp_path, elp_names, preload=True) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads') data_py, times_py = raw_py[picks] print(raw_py) # to test repr print(raw_py.info) # to test Info repr # compare with a file that was generated using MNE-C raw_bin = Raw(eeg_bin, preload=True) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads') data_bin, times_bin = raw_bin[picks] assert_array_almost_equal(data_py, data_bin) assert_array_almost_equal(times_py, times_bin) # Make sure EOG channels are marked correctly raw_py = read_raw_brainvision(vhdr_path, elp_path, elp_names, eog=eog, preload=True) for ch in raw_py.info['chs']: if ch['ch_name'] in eog: assert_equal(ch['kind'], FIFF.FIFFV_EOG_CH) elif ch['ch_name'] in elp_names: assert_equal(ch['kind'], FIFF.FIFFV_EEG_CH) elif ch['ch_name'] == 'STI 014': assert_equal(ch['kind'], FIFF.FIFFV_STIM_CH) else: raise RuntimeError("Unknown Channel: %s" % ch['ch_name'])
def test_pick_seeg_ecog(): """Test picking with sEEG and ECoG """ names = 'A1 A2 Fz O OTp1 OTp2 E1 OTp3 E2 E3'.split() types = 'mag mag eeg eeg seeg seeg ecog seeg ecog ecog'.split() info = create_info(names, 1024., types) idx = channel_indices_by_type(info) assert_array_equal(idx['mag'], [0, 1]) assert_array_equal(idx['eeg'], [2, 3]) assert_array_equal(idx['seeg'], [4, 5, 7]) assert_array_equal(idx['ecog'], [6, 8, 9]) assert_array_equal(pick_types(info, meg=False, seeg=True), [4, 5, 7]) for i, t in enumerate(types): assert_equal(channel_type(info, i), types[i]) raw = RawArray(np.zeros((len(names), 10)), info) events = np.array([[1, 0, 0], [2, 0, 0]]) epochs = Epochs(raw, events, {'event': 0}, -1e-5, 1e-5, add_eeg_ref=False) evoked = epochs.average(pick_types(epochs.info, meg=True, seeg=True)) e_seeg = evoked.copy().pick_types(meg=False, seeg=True) for l, r in zip(e_seeg.ch_names, [names[4], names[5], names[7]]): assert_equal(l, r) # Deal with constant debacle raw = read_raw_fif(op.join(io_dir, 'tests', 'data', 'test_chpi_raw_sss.fif'), add_eeg_ref=False) assert_equal(len(pick_types(raw.info, meg=False, seeg=True, ecog=True)), 0)
def interpolate_bads(inst, picks, dots=None, reset_bads=True, mode='accurate'): """Interpolate bad MEG and EEG channels.""" import mne # to prevent cobyla printf error # XXX putting to critical for now unless better solution # emerges verbose = mne.set_log_level('CRITICAL', return_old_level=True) eeg_picks = set(pick_types(inst.info, meg=False, eeg=True, exclude=[])) eeg_picks_interp = [p for p in picks if p in eeg_picks] if len(eeg_picks_interp) > 0: _interpolate_bads_eeg(inst, picks=eeg_picks_interp) meg_picks = set(pick_types(inst.info, meg=True, eeg=False, exclude=[])) meg_picks_interp = [p for p in picks if p in meg_picks] if len(meg_picks_interp) > 0: _interpolate_bads_meg_fast(inst, picks=meg_picks_interp, dots=dots, mode=mode) if reset_bads is True: inst.info['bads'] = [] mne.set_log_level(verbose) return inst
def test_events_long(): """Test events.""" data_path = testing.data_path() raw_fname = data_path + '/MEG/sample/sample_audvis_trunc_raw.fif' raw = read_raw_fif(raw_fname, preload=True) raw_tmin, raw_tmax = 0, 90 tmin, tmax = -0.2, 0.5 event_id = dict(aud_l=1, vis_l=3) # select gradiometers picks = pick_types(raw.info, meg='grad', eeg=False, eog=True, stim=True, exclude=raw.info['bads']) # load data with usual Epochs for later verification raw = concatenate_raws([raw, raw.copy(), raw.copy(), raw.copy(), raw.copy(), raw.copy()]) assert 110 < raw.times[-1] < 130 raw_cropped = raw.copy().crop(raw_tmin, raw_tmax) events_offline = find_events(raw_cropped) epochs_offline = Epochs(raw_cropped, events_offline, event_id=event_id, tmin=tmin, tmax=tmax, picks=picks, decim=1, reject=dict(grad=4000e-13, eog=150e-6), baseline=None) epochs_offline.drop_bad() # create the mock-client object rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, decim=1, reject=dict(grad=4000e-13, eog=150e-6), baseline=None, isi_max=1.) rt_epochs.start() rt_client.send_data(rt_epochs, picks, tmin=raw_tmin, tmax=raw_tmax, buffer_size=1000) expected_events = epochs_offline.events.copy() expected_events[:, 0] = expected_events[:, 0] - raw_cropped.first_samp assert np.all(expected_events[:, 0] <= (raw_tmax - tmax) * raw.info['sfreq']) assert_array_equal(rt_epochs.events, expected_events) assert len(rt_epochs) == len(epochs_offline) data_picks = pick_types(epochs_offline.info, meg='grad', eeg=False, eog=True, stim=False, exclude=raw.info['bads']) for ev_num, ev in enumerate(rt_epochs.iter_evoked()): if ev_num == 0: X_rt = ev.data[None, data_picks, :] y_rt = int(ev.comment) # comment attribute contains the event_id else: X_rt = np.concatenate((X_rt, ev.data[None, data_picks, :]), axis=0) y_rt = np.append(y_rt, int(ev.comment)) X_offline = epochs_offline.get_data()[:, data_picks, :] y_offline = epochs_offline.events[:, 2] assert_array_equal(X_rt, X_offline) assert_array_equal(y_rt, y_offline)
def test_scaler(): """Test methods of Scaler.""" raw = io.read_raw_fif(raw_fname) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') picks = picks[1:13:3] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) epochs_data = epochs.get_data() y = epochs.events[:, -1] methods = (None, dict(mag=5, grad=10, eeg=20), 'mean', 'median') infos = (epochs.info, epochs.info, None, None) epochs_data_t = epochs_data.transpose([1, 0, 2]) for method, info in zip(methods, infos): if method == 'median' and not check_version('sklearn', '0.17'): assert_raises(ValueError, Scaler, info, method) continue if method == 'mean' and not check_version('sklearn', ''): assert_raises(ImportError, Scaler, info, method) continue scaler = Scaler(info, method) X = scaler.fit_transform(epochs_data, y) assert_equal(X.shape, epochs_data.shape) if method is None or isinstance(method, dict): sd = DEFAULTS['scalings'] if method is None else method stds = np.zeros(len(picks)) for key in ('mag', 'grad'): stds[pick_types(epochs.info, meg=key)] = 1. / sd[key] stds[pick_types(epochs.info, meg=False, eeg=True)] = 1. / sd['eeg'] means = np.zeros(len(epochs.ch_names)) elif method == 'mean': stds = np.array([np.std(ch_data) for ch_data in epochs_data_t]) means = np.array([np.mean(ch_data) for ch_data in epochs_data_t]) else: # median percs = np.array([np.percentile(ch_data, [25, 50, 75]) for ch_data in epochs_data_t]) stds = percs[:, 2] - percs[:, 0] means = percs[:, 1] assert_allclose(X * stds[:, np.newaxis] + means[:, np.newaxis], epochs_data, rtol=1e-12, atol=1e-20, err_msg=method) X2 = scaler.fit(epochs_data, y).transform(epochs_data) assert_array_equal(X, X2) # inverse_transform Xi = scaler.inverse_transform(X) assert_array_almost_equal(epochs_data, Xi) # Test init exception assert_raises(ValueError, Scaler, None, None) assert_raises(ValueError, scaler.fit, epochs, y) assert_raises(ValueError, scaler.transform, epochs) epochs_bad = Epochs(raw, events, event_id, 0, 0.01, picks=np.arange(len(raw.ch_names))) # non-data chs scaler = Scaler(epochs_bad.info, None) assert_raises(ValueError, scaler.fit, epochs_bad.get_data(), y)
def test_info_no_rename_no_reorder_no_pdf(): """Test private renaming, reordering and partial construction option.""" for pdf, config, hs in zip(pdf_fnames, config_fnames, hs_fnames): info, bti_info = _get_bti_info( pdf_fname=pdf, config_fname=config, head_shape_fname=hs, rotation_x=0.0, translation=(0.0, 0.02, 0.11), convert=False, ecg_ch='E31', eog_ch=('E63', 'E64'), rename_channels=False, sort_by_ch_name=False) info2, bti_info = _get_bti_info( pdf_fname=None, config_fname=config, head_shape_fname=hs, rotation_x=0.0, translation=(0.0, 0.02, 0.11), convert=False, ecg_ch='E31', eog_ch=('E63', 'E64'), rename_channels=False, sort_by_ch_name=False) assert_equal(info['ch_names'], [ch['ch_name'] for ch in info['chs']]) assert_equal([n for n in info['ch_names'] if n.startswith('A')][:5], ['A22', 'A2', 'A104', 'A241', 'A138']) assert_equal([n for n in info['ch_names'] if n.startswith('A')][-5:], ['A133', 'A158', 'A44', 'A134', 'A216']) info = pick_info(info, pick_types(info, meg=True, stim=True, resp=True)) info2 = pick_info(info2, pick_types(info2, meg=True, stim=True, resp=True)) assert_true(info['sfreq'] is not None) assert_true(info['lowpass'] is not None) assert_true(info['highpass'] is not None) assert_true(info['meas_date'] is not None) assert_equal(info2['sfreq'], None) assert_equal(info2['lowpass'], None) assert_equal(info2['highpass'], None) assert_equal(info2['meas_date'], None) assert_equal(info['ch_names'], info2['ch_names']) assert_equal(info['ch_names'], info2['ch_names']) for key in ['dev_ctf_t', 'dev_head_t', 'ctf_head_t']: assert_array_equal(info[key]['trans'], info2[key]['trans']) assert_array_equal( np.array([ch['loc'] for ch in info['chs']]), np.array([ch['loc'] for ch in info2['chs']])) # just check reading data | corner case raw1 = read_raw_bti( pdf_fname=pdf, config_fname=config, head_shape_fname=None, sort_by_ch_name=False, preload=True) # just check reading data | corner case raw2 = read_raw_bti( pdf_fname=pdf, config_fname=config, head_shape_fname=None, rename_channels=False, sort_by_ch_name=True, preload=True) sort_idx = [raw1.bti_ch_labels.index(ch) for ch in raw2.bti_ch_labels] raw1._data = raw1._data[sort_idx] assert_array_equal(raw1._data, raw2._data) assert_array_equal(raw2.bti_ch_labels, raw2.ch_names)
def test_ica_eeg(): """Test ICA on EEG.""" method = 'fastica' raw_fif = read_raw_fif(fif_fname, preload=True) with pytest.warns(RuntimeWarning, match='events'): raw_eeglab = read_raw_eeglab(input_fname=eeglab_fname, montage=eeglab_montage, preload=True) for raw in [raw_fif, raw_eeglab]: events = make_fixed_length_events(raw, 99999, start=0, stop=0.3, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = [] picks_all.extend(picks_meg) picks_all.extend(picks_eeg) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(None): ica.fit(inst, picks=picks) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst) with pytest.warns(RuntimeWarning, match='MISC channel'): raw = read_raw_ctf(ctf_fname2, preload=True) events = make_fixed_length_events(raw, 99999, start=0, stop=0.2, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = picks_meg + picks_eeg for comp in [0, 1]: raw.apply_gradient_compensation(comp) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(None): ica.fit(inst) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst)
def test_no_conversion(): """ Test bti no-conversion option """ get_info = partial( _get_bti_info, rotation_x=0.0, translation=(0.0, 0.02, 0.11), convert=False, ecg_ch='E31', eog_ch=('E63', 'E64'), rename_channels=False, sort_by_ch_name=False) for pdf, config, hs in zip(pdf_fnames, config_fnames, hs_fnames): with warnings.catch_warnings(record=True): # weight tables raw_info, _ = get_info(pdf, config, hs, convert=False) with warnings.catch_warnings(record=True): # weight tables raw_info_con = read_raw_bti( pdf_fname=pdf, config_fname=config, head_shape_fname=hs, convert=True, preload=False).info pick_info(raw_info_con, pick_types(raw_info_con, meg=True, ref_meg=True), copy=False) pick_info(raw_info, pick_types(raw_info, meg=True, ref_meg=True), copy=False) bti_info = _read_bti_header(pdf, config) dev_ctf_t = _correct_trans(bti_info['bti_transform'][0]) assert_array_equal(dev_ctf_t, raw_info['dev_ctf_t']['trans']) assert_array_equal(raw_info['dev_head_t']['trans'], np.eye(4)) assert_array_equal(raw_info['ctf_head_t']['trans'], np.eye(4)) dig, t = _process_bti_headshape(hs, convert=False, use_hpi=False) assert_array_equal(t['trans'], np.eye(4)) for ii, (old, new, con) in enumerate(zip( dig, raw_info['dig'], raw_info_con['dig'])): assert_equal(old['ident'], new['ident']) assert_array_equal(old['r'], new['r']) assert_true(not np.allclose(old['r'], con['r'])) if ii > 10: break ch_map = dict((ch['chan_label'], ch['loc']) for ch in bti_info['chs']) for ii, ch_label in enumerate(raw_info['ch_names']): if not ch_label.startswith('A'): continue t1 = ch_map[ch_label] # correction already performed in bti_info t2 = raw_info['chs'][ii]['loc'] t3 = raw_info_con['chs'][ii]['loc'] assert_allclose(t1, t2, atol=1e-15) assert_true(not np.allclose(t1, t3)) idx_a = raw_info_con['ch_names'].index('MEG 001') idx_b = raw_info['ch_names'].index('A22') assert_equal( raw_info_con['chs'][idx_a]['coord_frame'], FIFF.FIFFV_COORD_DEVICE) assert_equal( raw_info['chs'][idx_b]['coord_frame'], FIFF.FIFFV_MNE_COORD_4D_HEAD)
def test_compute_covariance_auto_reg(rank): """Test automated regularization.""" raw = read_raw_fif(raw_fname, preload=True) raw.resample(100, npad='auto') # much faster estimation events = find_events(raw, stim_channel='STI 014') event_ids = [1, 2, 3, 4] reject = dict(mag=4e-12) # cov with merged events and keep_sample_mean=True events_merged = merge_events(events, event_ids, 1234) # we need a few channels for numerical reasons in PCA/FA picks = pick_types(raw.info, meg='mag', eeg=False)[:10] raw.pick_channels([raw.ch_names[pick] for pick in picks]) raw.info.normalize_proj() epochs = Epochs(raw, events_merged, 1234, tmin=-0.2, tmax=0, baseline=(-0.2, -0.1), proj=True, reject=reject, preload=True) epochs = epochs.crop(None, 0)[:5] method_params = dict(factor_analysis=dict(iter_n_components=[3]), pca=dict(iter_n_components=[3])) covs = compute_covariance(epochs, method='auto', method_params=method_params, return_estimators=True, rank=rank) # make sure regularization produces structured differencess diag_mask = np.eye(len(epochs.ch_names)).astype(bool) off_diag_mask = np.invert(diag_mask) for cov_a, cov_b in itt.combinations(covs, 2): if (cov_a['method'] == 'diagonal_fixed' and # here we have diagnoal or no regularization. cov_b['method'] == 'empirical' and rank == 'full'): assert not np.any( cov_a['data'][diag_mask] == cov_b['data'][diag_mask]) # but the rest is the same assert_array_equal(cov_a['data'][off_diag_mask], cov_b['data'][off_diag_mask]) else: # and here we have shrinkage everywhere. assert not np.any( cov_a['data'][diag_mask] == cov_b['data'][diag_mask]) assert not np.any( cov_a['data'][diag_mask] == cov_b['data'][diag_mask]) logliks = [c['loglik'] for c in covs] assert np.diff(logliks).max() <= 0 # descending order methods = ['empirical', 'ledoit_wolf', 'oas', 'shrunk', 'shrinkage'] if rank == 'full': methods.extend(['factor_analysis', 'pca']) with catch_logging() as log: cov3 = compute_covariance(epochs, method=methods, method_params=method_params, projs=None, return_estimators=True, rank=rank, verbose=True) log = log.getvalue().split('\n') if rank is None: assert ' Setting small MAG eigenvalues to zero (without PCA)' in log assert 'Reducing data rank from 10 -> 7' in log else: assert 'Reducing' not in log method_names = [cov['method'] for cov in cov3] best_bounds = [-45, -35] bounds = [-55, -45] if rank == 'full' else best_bounds for method in set(methods) - {'empirical', 'shrunk'}: this_lik = cov3[method_names.index(method)]['loglik'] assert bounds[0] < this_lik < bounds[1] this_lik = cov3[method_names.index('shrunk')]['loglik'] assert best_bounds[0] < this_lik < best_bounds[1] this_lik = cov3[method_names.index('empirical')]['loglik'] bounds = [-110, -100] if rank == 'full' else best_bounds assert bounds[0] < this_lik < bounds[1] assert_equal({c['method'] for c in cov3}, set(methods)) cov4 = compute_covariance(epochs, method=methods, method_params=method_params, projs=None, return_estimators=False, rank=rank) assert cov3[0]['method'] == cov4['method'] # ordering # invalid prespecified method pytest.raises(ValueError, compute_covariance, epochs, method='pizza') # invalid scalings pytest.raises(ValueError, compute_covariance, epochs, method='shrunk', scalings=dict(misc=123))
def test_plot_ica_sources(): """Test plotting of ICA panel.""" raw = read_raw_fif(raw_fname).crop(0, 1).load_data() picks = _get_picks(raw) epochs = _get_epochs() raw.pick_channels([raw.ch_names[k] for k in picks]) ica_picks = pick_types(raw.info, meg=True, eeg=False, stim=False, ecg=False, eog=False, exclude='bads') ica = ICA(n_components=2) ica.fit(raw, picks=ica_picks) ica.exclude = [1] fig = ica.plot_sources(raw) assert len(plt.get_fignums()) == 1 # change which component is in ICA.exclude (click data trace to remove # current one; click name to add other one) fig.canvas.draw() x = fig.mne.traces[1].get_xdata()[5] y = fig.mne.traces[1].get_ydata()[5] _fake_click(fig, fig.mne.ax_main, (x, y), xform='data') # exclude = [] _click_ch_name(fig, ch_index=0, button=1) # exclude = [0] fig.canvas.key_press_event(fig.mne.close_key) _close_event(fig) assert len(plt.get_fignums()) == 0 assert_array_equal(ica.exclude, [0]) # test when picks does not include ica.exclude. fig = ica.plot_sources(raw, picks=[1]) assert len(plt.get_fignums()) == 1 plt.close('all') # dtype can change int->np.int64 after load, test it explicitly ica.n_components_ = np.int64(ica.n_components_) # test clicks on y-label (need >2 secs for plot_properties() to work) long_raw = read_raw_fif(raw_fname).crop(0, 5).load_data() fig = ica.plot_sources(long_raw) assert len(plt.get_fignums()) == 1 fig.canvas.draw() _click_ch_name(fig, ch_index=0, button=3) assert len(fig.mne.child_figs) == 1 assert len(plt.get_fignums()) == 2 # close child fig directly (workaround for mpl issue #18609) fig.mne.child_figs[0].canvas.key_press_event('escape') assert len(plt.get_fignums()) == 1 fig.canvas.key_press_event(fig.mne.close_key) assert len(plt.get_fignums()) == 0 del long_raw # test with annotations orig_annot = raw.annotations raw.set_annotations(Annotations([0.2], [0.1], 'Test')) fig = ica.plot_sources(raw) assert len(fig.mne.ax_main.collections) == 1 assert len(fig.mne.ax_hscroll.collections) == 1 raw.set_annotations(orig_annot) # test error handling raw.info['bads'] = ['MEG 0113'] with pytest.raises(RuntimeError, match="Raw doesn't match fitted data"): ica.plot_sources(inst=raw) epochs.info['bads'] = ['MEG 0113'] with pytest.raises(RuntimeError, match="Epochs don't match fitted data"): ica.plot_sources(inst=epochs) epochs.info['bads'] = [] # test w/ epochs and evokeds ica.plot_sources(epochs) ica.plot_sources(epochs.average()) evoked = epochs.average() fig = ica.plot_sources(evoked) # Test a click ax = fig.get_axes()[0] line = ax.lines[0] _fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], 'data') _fake_click(fig, ax, [ax.get_xlim()[0], ax.get_ylim()[1]], 'data') # plot with bad channels excluded ica.exclude = [0] ica.plot_sources(evoked) ica.labels_ = dict(eog=[0]) ica.labels_['eog/0/crazy-channel'] = [0] ica.plot_sources(evoked) # now with labels with pytest.raises(ValueError, match='must be of Raw or Epochs type'): ica.plot_sources('meeow')
def test_time_frequency(): """Test the to-be-deprecated time-frequency transform (PSD and ITC).""" # Set parameters event_id = 1 tmin = -0.2 tmax = 0.498 # Allows exhaustive decimation testing # Setup for reading the raw data raw = read_raw_fif(raw_fname) events = read_events(event_fname) include = [] exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more # picks MEG gradiometers picks = pick_types(raw.info, meg='grad', eeg=False, stim=False, include=include, exclude=exclude) picks = picks[:2] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks) data = epochs.get_data() times = epochs.times nave = len(data) epochs_nopicks = Epochs(raw, events, event_id, tmin, tmax) freqs = np.arange(6, 20, 5) # define frequencies of interest n_cycles = freqs / 4. # Test first with a single epoch power, itc = tfr_morlet(epochs[0], freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=True) # Now compute evoked evoked = epochs.average() power_evoked = tfr_morlet(evoked, freqs, n_cycles, use_fft=True, return_itc=False) assert_raises(ValueError, tfr_morlet, evoked, freqs, 1., return_itc=True) power, itc = tfr_morlet(epochs, freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=True) power_, itc_ = tfr_morlet(epochs, freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=True, decim=slice(0, 2)) # Test picks argument and average parameter assert_raises(ValueError, tfr_morlet, epochs, freqs=freqs, n_cycles=n_cycles, return_itc=True, average=False) power_picks, itc_picks = \ tfr_morlet(epochs_nopicks, freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=True, picks=picks, average=True) epochs_power_picks = \ tfr_morlet(epochs_nopicks, freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=False, picks=picks, average=False) power_picks_avg = epochs_power_picks.average() # the actual data arrays here are equivalent, too... assert_array_almost_equal(power.data, power_picks.data) assert_array_almost_equal(power.data, power_picks_avg.data) assert_array_almost_equal(itc.data, itc_picks.data) assert_array_almost_equal(power.data, power_evoked.data) print(itc) # test repr print(itc.ch_names) # test property itc += power # test add itc -= power # test sub power = power.apply_baseline(baseline=(-0.1, 0), mode='logratio') assert_true('meg' in power) assert_true('grad' in power) assert_false('mag' in power) assert_false('eeg' in power) assert_equal(power.nave, nave) assert_equal(itc.nave, nave) assert_true(power.data.shape == (len(picks), len(freqs), len(times))) assert_true(power.data.shape == itc.data.shape) assert_true(power_.data.shape == (len(picks), len(freqs), 2)) assert_true(power_.data.shape == itc_.data.shape) assert_true(np.sum(itc.data >= 1) == 0) assert_true(np.sum(itc.data <= 0) == 0) # grand average itc2 = itc.copy() itc2.info['bads'] = [itc2.ch_names[0]] # test channel drop gave = grand_average([itc2, itc]) assert_equal(gave.data.shape, (itc2.data.shape[0] - 1, itc2.data.shape[1], itc2.data.shape[2])) assert_equal(itc2.ch_names[1:], gave.ch_names) assert_equal(gave.nave, 2) itc2.drop_channels(itc2.info["bads"]) assert_array_almost_equal(gave.data, itc2.data) itc2.data = np.ones(itc2.data.shape) itc.data = np.zeros(itc.data.shape) itc2.nave = 2 itc.nave = 1 itc.drop_channels([itc.ch_names[0]]) combined_itc = combine_tfr([itc2, itc]) assert_array_almost_equal(combined_itc.data, np.ones(combined_itc.data.shape) * 2 / 3) # more tests power, itc = tfr_morlet(epochs, freqs=freqs, n_cycles=2, use_fft=False, return_itc=True) assert_true(power.data.shape == (len(picks), len(freqs), len(times))) assert_true(power.data.shape == itc.data.shape) assert_true(np.sum(itc.data >= 1) == 0) assert_true(np.sum(itc.data <= 0) == 0) tfr = tfr_morlet(epochs[0], freqs, use_fft=True, n_cycles=2, average=False, return_itc=False).data[0] assert_true(tfr.shape == (len(picks), len(freqs), len(times))) tfr2 = tfr_morlet(epochs[0], freqs, use_fft=True, n_cycles=2, decim=slice(0, 2), average=False, return_itc=False).data[0] assert_true(tfr2.shape == (len(picks), len(freqs), 2)) single_power = tfr_morlet(epochs, freqs, 2, average=False, return_itc=False).data single_power2 = tfr_morlet(epochs, freqs, 2, decim=slice(0, 2), average=False, return_itc=False).data single_power3 = tfr_morlet(epochs, freqs, 2, decim=slice(1, 3), average=False, return_itc=False).data single_power4 = tfr_morlet(epochs, freqs, 2, decim=slice(2, 4), average=False, return_itc=False).data assert_array_almost_equal(np.mean(single_power, axis=0), power.data) assert_array_almost_equal(np.mean(single_power2, axis=0), power.data[:, :, :2]) assert_array_almost_equal(np.mean(single_power3, axis=0), power.data[:, :, 1:3]) assert_array_almost_equal(np.mean(single_power4, axis=0), power.data[:, :, 2:4]) power_pick = power.pick_channels(power.ch_names[:10:2]) assert_equal(len(power_pick.ch_names), len(power.ch_names[:10:2])) assert_equal(power_pick.data.shape[0], len(power.ch_names[:10:2])) power_drop = power.drop_channels(power.ch_names[1:10:2]) assert_equal(power_drop.ch_names, power_pick.ch_names) assert_equal(power_pick.data.shape[0], len(power_drop.ch_names)) mne.equalize_channels([power_pick, power_drop]) assert_equal(power_pick.ch_names, power_drop.ch_names) assert_equal(power_pick.data.shape, power_drop.data.shape) # Test decimation: # 2: multiple of len(times) even # 3: multiple odd # 8: not multiple, even # 9: not multiple, odd for decim in [2, 3, 8, 9]: for use_fft in [True, False]: power, itc = tfr_morlet(epochs, freqs=freqs, n_cycles=2, use_fft=use_fft, return_itc=True, decim=decim) assert_equal(power.data.shape[2], np.ceil(float(len(times)) / decim)) freqs = list(range(50, 55)) decim = 2 _, n_chan, n_time = data.shape tfr = tfr_morlet(epochs[0], freqs, 2., decim=decim, average=False, return_itc=False).data[0] assert_equal(tfr.shape, (n_chan, len(freqs), n_time // decim)) # Test cwt modes Ws = morlet(512, [10, 20], n_cycles=2) assert_raises(ValueError, cwt, data[0, :, :], Ws, mode='foo') for use_fft in [True, False]: for mode in ['same', 'valid', 'full']: # XXX JRK: full wavelet decomposition needs to be implemented if (not use_fft) and mode == 'full': assert_raises(ValueError, cwt, data[0, :, :], Ws, use_fft=use_fft, mode=mode) continue cwt(data[0, :, :], Ws, use_fft=use_fft, mode=mode) # Test decim parameter checks assert_raises(TypeError, tfr_morlet, epochs, freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=True, decim='decim')
from mne.stats.regression import linear_regression_raw # Load and preprocess data data_path = sample.data_path() raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' raw = mne.io.read_raw_fif(raw_fname) raw.pick_types(meg='grad', stim=True, eeg=False).load_data() raw.filter(1, None, fir_design='firwin') # high-pass # Set up events events = mne.find_events(raw) event_id = {'Aud/L': 1, 'Aud/R': 2} tmin, tmax = -.1, .5 # regular epoching picks = mne.pick_types(raw.info, meg=True) epochs = mne.Epochs(raw, events, event_id, tmin, tmax, reject=None, baseline=None, preload=True, verbose=False) # rERF evokeds = linear_regression_raw(raw, events=events, event_id=event_id, reject=None,
# Set parameters raw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif' # Setup for reading the raw data raw = io.read_raw_fif(raw_fname) event_id = 999 ecg_events, _, _ = mne.preprocessing.find_ecg_events(raw, event_id, ch_name='MEG 1531') # Read epochs picks = mne.pick_types(raw.info, meg=False, eeg=False, stim=False, eog=False, include=['MEG 1531'], exclude='bads') tmin, tmax = -0.1, 0.1 epochs = mne.Epochs(raw, ecg_events, event_id, tmin, tmax, picks=picks, proj=False) data = epochs.get_data() print("Number of detected ECG artifacts : %d" % len(data))
def __init__(self, data_path=DATA_PATH, subjects_dir=SUBJECTS_DIR, subject_name=SUBJECT_NAME, raw_path=RAW_PATH, bem_path=BEM_PATH, cov_path=COV_PATH, trans_path=TRANS_PATH, src_path=SRC_PATH, raw_empty_room_path=RAW_EMPTY_ROOM_PATH, spacing=SPACING, save_folder=SIMULATION_SAVE_PATH, bad_channels=None, hemisphere=HEMISPHERE, empty_signal=EMPTY_SIGNAL, **kwargs): # TODO: Add option to compute covariance based on simulated data """ Makes a data model of raw MEG data and enables simulation of new data based on MEG data templates. :param data_path: str The path where MEG data is. If None, then mri_dir, meg_dir, subject_name and meg_file inputs are ignored. :param subjects_dir: str X :param mri_folder: str The directory under data_path where MRI data of subjects is. :param meg_folder: str The directory under data_path where MEG data of subjects is. :param subject_name: str The name of the subject. :param meg_file: str The name of the raw data file containing the channel location and types. :param bem_file: :param trans_file: str The transformation file used for co-registration of head and sensors. :param spacing: str Default source space grid point spacing. Supported types: ico* and oct* Defaults to oct6. :param save_folder: :return: """ # Initialize some default parameters self.l_freq = self.h_freq = self.phase = self.fir_window = self.channel_types = \ self.verbose = self.reject = self.loose = self.lambda2 = self.iir_filter = self.ecg = \ self.n_dipoles = self.samples_per_dipole = self.depth = self.blink = self.n_simulations = \ self.fir_design = self.times_per_dipole = None for key in kwargs: # Initialize kwargs into SimulationModel object setattr(self, key, kwargs[key]) self.spacing = spacing if not all([ data_path, subjects_dir, subject_name, raw_path, bem_path, cov_path, trans_path, src_path ]): # TODO: raw_empty_room print( 'You need to set data_path, subject_name, raw_path, bem_path, cov_path, trans_path and src_path in ' 'order to use custom subject data.') print('Initializing with MNE sample subject data instead...') self.subjects_dir = subjects_dir self.__init_with_mne_sample_data() else: self.data_path = data_path self.subjects_dir = subjects_dir self.subject_name = subject_name self.raw_path = raw_path self.bem_path = bem_path self.cov_path = cov_path self.trans_path = trans_path self.src_path = src_path self.raw_empty_room_path = raw_empty_room_path if not spacing or re.compile('([i-o])\w{2}\d').match(spacing) is None: raise ValueError( "Incorrect spacing geometry. " "Use ico3, ico4, ico5, oct3, oct4, oct5 or oct6 instead.") self.hemisphere = hemisphere self.empty_signal = empty_signal # Load data self.orig_data = mne.io.read_raw_fif(self.raw_path, preload=True) # load raw fif data self.orig_data.set_eeg_reference('average', projection=True) self.orig_data.apply_proj() # Apply SSP self.bad_channels = ['MEG 2443', 'EEG 053'] # Mark bad channels if self.bad_channels: # Bad channels have to be in format: ['MEG 2443', 'EEG 053'] self.orig_data.info['bads'] = self.bad_channels self.picks = mne.pick_types(self.orig_data.info, meg=self.channel_types['meg'], eeg=self.channel_types['eeg'], eog=self.channel_types['eog'], stim=self.channel_types['stim'], ecg=self.ecg, exclude='bads') # Filter data self.orig_data.filter(l_freq=self.l_freq, h_freq=self.h_freq, phase=self.phase, fir_window=self.fir_window, fir_design=self.fir_design) # Init basic parameters for simulation self.src = mne.read_source_spaces(self.src_path) self.rh_labels = np.where(self.src[1]["inuse"] == 1)[0] self.lh_labels = np.where(self.src[0]["inuse"] == 1)[0] self.fwd = self.sim_data = self.sim_stc = None self.spacing_t_steps = self.__get_spacing_t_steps() # Setup parameters for generating simulated raw data self.sim_vertices = np.array([]) # Inverse solutions self.mne = self.dspm = self.sloreta = None self.save_path = save_folder self.raw_empty_room = mne.io.read_raw_fif(self.raw_empty_room_path, preload=True) self.raw_empty_room.info['bads'] = [ bb for bb in self.orig_data.info['bads'] if 'EEG' not in bb ] self.raw_empty_room.add_proj([ pp.copy() for pp in self.orig_data.info['projs'] if 'EEG' not in pp['desc'] ]) # self.raw_empty_room.set_eeg_reference() # self.cov = mne.read_cov(self.cov_path) #self.cov = None self.cov = mne.compute_raw_covariance(self.raw_empty_room, tmin=0, tmax=None, n_jobs=CPU_THREADS) # self.cov = self.compute_covariance(self.orig_data) # self.cov = 'simple' self.data_template = None
def raw_preprocessing(self, raw, use_ica=True, use_ssp=True): # Filter raw.info['bads'] = self.bad_channels # Remove power-line noise raw.notch_filter(np.arange(60, 241, 60), picks=self.picks, filter_length='auto', phase=self.phase) raw.filter(l_freq=self.l_freq, h_freq=self.h_freq, phase=self.phase, fir_window=self.fir_window, fir_design=self.fir_design) # Add EEG reference raw.set_eeg_reference(projection=True) # TODO: some mechanism to control this if use_ica: # supported ica_channels = ['mag', 'grad', 'eeg', 'seeg', 'ecog', 'hbo', 'hbr', 'eog'] ica_picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False, stim=False, exclude='bads') n_components = 25 decim = 3 # maximum number of components to reject n_max_ecg, n_max_eog = 3, 1 # here we don't expect horizontal EOG components # ica = run_ica(raw, n_components=0.95) ica = ICA(n_components=n_components, method='fastica', noise_cov=None) ica.fit(raw, decim=decim, picks=ica_picks, reject=self.reject) # generate ECG epochs use detection via phase statistics ecg_epochs = create_ecg_epochs(raw, reject=self.reject) ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps') ecg_inds = ecg_inds[:n_max_ecg] ica.exclude += ecg_inds # detect EOG by correlation eog_inds, scores = ica.find_bads_eog(raw) eog_inds = eog_inds[:n_max_eog] ica.exclude += eog_inds ica.apply(raw) if use_ssp: if self.ecg: ecg_projs, _ = compute_proj_ecg(raw) raw.info['projs'] += ecg_projs if self.blink: eog_projs, _ = compute_proj_eog(raw) raw.info['projs'] += eog_projs raw.apply_proj() return raw
def test_pick_refs(): """Test picking of reference sensors.""" infos = list() # KIT kit_dir = op.join(io_dir, 'kit', 'tests', 'data') sqd_path = op.join(kit_dir, 'test.sqd') mrk_path = op.join(kit_dir, 'test_mrk.sqd') elp_path = op.join(kit_dir, 'test_elp.txt') hsp_path = op.join(kit_dir, 'test_hsp.txt') raw_kit = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path) infos.append(raw_kit.info) # BTi bti_dir = op.join(io_dir, 'bti', 'tests', 'data') bti_pdf = op.join(bti_dir, 'test_pdf_linux') bti_config = op.join(bti_dir, 'test_config_linux') bti_hs = op.join(bti_dir, 'test_hs_linux') raw_bti = read_raw_bti(bti_pdf, bti_config, bti_hs, preload=False) infos.append(raw_bti.info) # CTF fname_ctf_raw = op.join(io_dir, 'tests', 'data', 'test_ctf_comp_raw.fif') raw_ctf = read_raw_fif(fname_ctf_raw) raw_ctf.apply_gradient_compensation(2) for info in infos: info['bads'] = [] pytest.raises(ValueError, pick_types, info, meg='foo') pytest.raises(ValueError, pick_types, info, ref_meg='foo') picks_meg_ref = pick_types(info, meg=True, ref_meg=True) picks_meg = pick_types(info, meg=True, ref_meg=False) picks_ref = pick_types(info, meg=False, ref_meg=True) assert_array_equal(picks_meg_ref, np.sort(np.concatenate([picks_meg, picks_ref]))) picks_grad = pick_types(info, meg='grad', ref_meg=False) picks_ref_grad = pick_types(info, meg=False, ref_meg='grad') picks_meg_ref_grad = pick_types(info, meg='grad', ref_meg='grad') assert_array_equal(picks_meg_ref_grad, np.sort(np.concatenate([picks_grad, picks_ref_grad]))) picks_mag = pick_types(info, meg='mag', ref_meg=False) picks_ref_mag = pick_types(info, meg=False, ref_meg='mag') picks_meg_ref_mag = pick_types(info, meg='mag', ref_meg='mag') assert_array_equal(picks_meg_ref_mag, np.sort(np.concatenate([picks_mag, picks_ref_mag]))) assert_array_equal(picks_meg, np.sort(np.concatenate([picks_mag, picks_grad]))) assert_array_equal(picks_ref, np.sort(np.concatenate([picks_ref_mag, picks_ref_grad]))) assert_array_equal(picks_meg_ref, np.sort(np.concatenate( [picks_grad, picks_mag, picks_ref_grad, picks_ref_mag]))) for pick in (picks_meg_ref, picks_meg, picks_ref, picks_grad, picks_ref_grad, picks_meg_ref_grad, picks_mag, picks_ref_mag, picks_meg_ref_mag): if len(pick) > 0: pick_info(info, pick) # test CTF expected failures directly info = raw_ctf.info info['bads'] = [] picks_meg_ref = pick_types(info, meg=True, ref_meg=True) picks_meg = pick_types(info, meg=True, ref_meg=False) picks_ref = pick_types(info, meg=False, ref_meg=True) picks_mag = pick_types(info, meg='mag', ref_meg=False) picks_ref_mag = pick_types(info, meg=False, ref_meg='mag') picks_meg_ref_mag = pick_types(info, meg='mag', ref_meg='mag') for pick in (picks_meg_ref, picks_ref, picks_ref_mag, picks_meg_ref_mag): if len(pick) > 0: pick_info(info, pick) for pick in (picks_meg, picks_mag): if len(pick) > 0: with catch_logging() as log: pick_info(info, pick, verbose=True) assert ('Removing {} compensators'.format(len(info['comps'])) in log.getvalue())
preload=False, reject=None, decim=4) epochs.append(epoch) # Same `dev_head_t` for all runs so that we can concatenate them. epoch.info['dev_head_t'] = epochs[0].info['dev_head_t'] epochs = mne.epochs.concatenate_epochs(epochs) ############################################################################### # Now, we apply autoreject from autoreject import AutoReject, compute_thresholds # noqa this_epoch = epochs['famous'] exclude = [] # XXX picks = mne.pick_types(epochs.info, meg=False, eeg=True, stim=False, eog=False, exclude=exclude) ############################################################################### # Note that :class:`autoreject.AutoReject` by design supports multiple # channels. If no picks are passed separate solutions will be computed for each # channel type and internally combines. This then readily supports cleaning # unseen epochs from the different channel types used during fit. # Here we only use a subset of channels to save time. ############################################################################### # Also note that once the parameters are learned, any data can be repaired # that contains channels that were used during fit. This also means that time # may be saved by fitting :class:`autoreject.AutoReject` on a # representative subsample of the data. ar = AutoReject(picks=picks, random_state=42, n_jobs=1, verbose='tqdm')
def test_io_raw(): """Test IO for raw data (Neuromag + CTF + gz) """ tempdir = _TempDir() # test unicode io for chars in [b'\xc3\xa4\xc3\xb6\xc3\xa9', b'a']: with Raw(fif_fname) as r: assert_true('Raw' in repr(r)) assert_true(op.basename(fif_fname) in repr(r)) desc1 = r.info['description'] = chars.decode('utf-8') temp_file = op.join(tempdir, 'raw.fif') r.save(temp_file, overwrite=True) with Raw(temp_file) as r2: desc2 = r2.info['description'] assert_equal(desc1, desc2) # Let's construct a simple test for IO first raw = Raw(fif_fname).crop(0, 3.5, False) raw.load_data() # put in some data that we know the values of data = rng.randn(raw._data.shape[0], raw._data.shape[1]) raw._data[:, :] = data # save it somewhere fname = op.join(tempdir, 'test_copy_raw.fif') raw.save(fname, buffer_size_sec=1.0) # read it in, make sure the whole thing matches raw = Raw(fname) assert_allclose(data, raw[:, :][0], rtol=1e-6, atol=1e-20) # let's read portions across the 1-sec tag boundary, too inds = raw.time_as_index([1.75, 2.25]) sl = slice(inds[0], inds[1]) assert_allclose(data[:, sl], raw[:, sl][0], rtol=1e-6, atol=1e-20) # now let's do some real I/O fnames_in = [fif_fname, test_fif_gz_fname, ctf_fname] fnames_out = ['raw.fif', 'raw.fif.gz', 'raw.fif'] for fname_in, fname_out in zip(fnames_in, fnames_out): fname_out = op.join(tempdir, fname_out) raw = Raw(fname_in) nchan = raw.info['nchan'] ch_names = raw.info['ch_names'] meg_channels_idx = [k for k in range(nchan) if ch_names[k][0] == 'M'] n_channels = 100 meg_channels_idx = meg_channels_idx[:n_channels] start, stop = raw.time_as_index([0, 5]) data, times = raw[meg_channels_idx, start:(stop + 1)] meg_ch_names = [ch_names[k] for k in meg_channels_idx] # Set up pick list: MEG + STI 014 - bad channels include = ['STI 014'] include += meg_ch_names picks = pick_types(raw.info, meg=True, eeg=False, stim=True, misc=True, ref_meg=True, include=include, exclude='bads') # Writing with drop_small_buffer True raw.save(fname_out, picks, tmin=0, tmax=4, buffer_size_sec=3, drop_small_buffer=True, overwrite=True) raw2 = Raw(fname_out) sel = pick_channels(raw2.ch_names, meg_ch_names) data2, times2 = raw2[sel, :] assert_true(times2.max() <= 3) # Writing raw.save(fname_out, picks, tmin=0, tmax=5, overwrite=True) if fname_in == fif_fname or fname_in == fif_fname + '.gz': assert_equal(len(raw.info['dig']), 146) raw2 = Raw(fname_out) sel = pick_channels(raw2.ch_names, meg_ch_names) data2, times2 = raw2[sel, :] assert_allclose(data, data2, rtol=1e-6, atol=1e-20) assert_allclose(times, times2) assert_allclose(raw.info['sfreq'], raw2.info['sfreq'], rtol=1e-5) # check transformations for trans in ['dev_head_t', 'dev_ctf_t', 'ctf_head_t']: if raw.info[trans] is None: assert_true(raw2.info[trans] is None) else: assert_array_equal(raw.info[trans]['trans'], raw2.info[trans]['trans']) # check transformation 'from' and 'to' if trans.startswith('dev'): from_id = FIFF.FIFFV_COORD_DEVICE else: from_id = FIFF.FIFFV_MNE_COORD_CTF_HEAD if trans[4:8] == 'head': to_id = FIFF.FIFFV_COORD_HEAD else: to_id = FIFF.FIFFV_MNE_COORD_CTF_HEAD for raw_ in [raw, raw2]: assert_equal(raw_.info[trans]['from'], from_id) assert_equal(raw_.info[trans]['to'], to_id) if fname_in == fif_fname or fname_in == fif_fname + '.gz': assert_allclose(raw.info['dig'][0]['r'], raw2.info['dig'][0]['r']) # test warnings on bad filenames with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") raw_badname = op.join(tempdir, 'test-bad-name.fif.gz') raw.save(raw_badname) Raw(raw_badname) assert_naming(w, 'test_raw_fiff.py', 2)
data_path = sample.data_path() fname = data_path + '/MEG/sample/sample_audvis_raw.fif' raw = mne.io.Raw(fname) # Set up pick list: MEG + STI 014 - bad channels want_meg = True want_eeg = False want_stim = False include = ['STI 014'] raw.info['bads'] += ['MEG 2443', 'EEG 053'] # bad channels + 2 more picks = mne.pick_types(raw.info, meg=want_meg, eeg=want_eeg, stim=want_stim, include=include, exclude='bads') some_picks = picks[:5] # take 5 first start, stop = raw.time_as_index([0, 15]) # read the first 15s of data data, times = raw[some_picks, start:(stop + 1)] # save 150s of MEG data in FIF file raw.save('sample_audvis_meg_raw.fif', tmin=0, tmax=150, picks=picks, overwrite=True) ###############################################################################
# they need to be projected to the flatten manifold span by magnetometer # or radial gradiometers before taking the gradients in the 2D Cartesian # coordinate system for visualization on the 2D topoplot. You can use the # ``info_from`` and ``info_to`` parameters to interpolate from # gradiometer data to magnetometer data. # %% # Plot gradiometer data as an arrowmap along with the topoplot at the time # of the maximum sensor space activity: plot_arrowmap(evoked_grad.data[:, max_time_idx], info_from=evoked_grad.info, info_to=evoked_mag.info) # %% # Since Vectorview 102 system perform sparse spatial sampling of the magnetic # field, data from the Vectorview (info_from) can be projected to the high # density CTF 272 system (info_to) for visualization # # Plot gradiometer data as an arrowmap along with the topoplot at the time # of the maximum sensor space activity: path = bst_raw.data_path() raw_fname = path + ('/MEG/bst_raw/' 'subj001_somatosensory_20111109_01_AUX-f.ds') raw_ctf = mne.io.read_raw_ctf(raw_fname) raw_ctf_info = mne.pick_info( raw_ctf.info, mne.pick_types(raw_ctf.info, meg=True, ref_meg=False)) plot_arrowmap(evoked_grad.data[:, max_time_idx], info_from=evoked_grad.info, info_to=raw_ctf_info, scale=6e-10)
mne.pick_channels(info['ch_names'], include=[], exclude=['MEG 0312', 'EEG 005'])) # %% # :func:`~mne.pick_types` works differently, since channel type cannot always # be reliably determined from channel name alone. Consequently, # :func:`~mne.pick_types` needs an :class:`~mne.Info` object instead of just a # list of channel names, and has boolean keyword arguments for each channel # type. Default behavior is to pick only MEG channels (and MEG reference # channels if present) and exclude any channels already marked as "bad" in the # ``bads`` field of the :class:`~mne.Info` object. Therefore, to get *all* and # *only* the EEG channel indices (including the "bad" EEG channels) we must # pass ``meg=False`` and ``exclude=[]``: print(mne.pick_types(info, meg=False, eeg=True, exclude=[])) # %% # Note that the ``meg`` and ``fnirs`` parameters of :func:`~mne.pick_types` # accept strings as well as boolean values, to allow selecting only # magnetometer or gradiometer channels (via ``meg='mag'`` or ``meg='grad'``) or # to pick only oxyhemoglobin or deoxyhemoglobin channels (via ``fnirs='hbo'`` # or ``fnirs='hbr'``, respectively). # # A third way to pick channels from an :class:`~mne.Info` object is to apply # `regular expression`_ matching to the channel names using # :func:`mne.pick_channels_regexp`. Here the ``^`` represents the beginning of # the string and ``.`` character matches any single character, so both EEG and # EOG channels will be selected: print(mne.pick_channels_regexp(info['ch_names'], '^E.G'))
def process_raw(self, raw, dataset): """ Process one raw data file. This function apply the preprocessing and eventual epoching on the individual run, and return the data, labels and a dataframe with metadata. metadata is a dataframe with as many row as the length of the data and labels. Parameters ---------- raw: mne.Raw instance the raw EEG data. dataset : dataset instance The dataset corresponding to the raw file. mainly use to access dataset specific information. returns ------- X : np.ndarray the data that will be used as features for the model labels: np.ndarray the labels for training / evaluating the model metadata: pd.DataFrame A dataframe containing the metadata """ # find the events, first check stim_channels then annotations stim_channels = mne.utils._get_stim_channel(None, raw.info, raise_error=False) if len(stim_channels) > 0: events = mne.find_events(raw, shortest_event=0, verbose=False) else: events, _ = mne.events_from_annotations(raw, verbose=False) channels = () if self.channels is None else self.channels # picks channels picks = mne.pick_types(raw.info, eeg=True, stim=False, include=channels) # get events id event_id = self.used_events(dataset) # pick events, based on event_id try: events = mne.pick_events(events, include=list(event_id.values())) except RuntimeError: # skip raw if no event found return # get interval tmin = self.tmin + dataset.interval[0] if self.tmax is None: tmax = dataset.interval[1] else: tmax = self.tmax + dataset.interval[0] X = [] for bandpass in self.filters: fmin, fmax = bandpass # filter data raw_f = raw.copy().filter(fmin, fmax, method='iir', picks=picks, verbose=False) # epoch data epochs = mne.Epochs(raw_f, events, event_id=event_id, tmin=tmin, tmax=tmax, proj=False, baseline=None, preload=True, verbose=False, picks=picks, on_missing='ignore') if self.resample is not None: epochs = epochs.resample(self.resample) # rescale to work with uV X.append(dataset.unit_factor * epochs.get_data()) inv_events = {k: v for v, k in event_id.items()} labels = np.array([inv_events[e] for e in epochs.events[:, -1]]) # if only one band, return a 3D array, otherwise return a 4D if len(self.filters) == 1: X = X[0] else: X = np.array(X).transpose((1, 2, 3, 0)) metadata = pd.DataFrame(index=range(len(labels))) return X, labels, metadata
def test_simulate_raw_sphere(): """Test simulation of raw data with sphere model.""" seed = 42 raw, src, stc, trans, sphere = _get_data() assert len(pick_types(raw.info, meg=False, ecg=True)) == 1 # head pos head_pos_sim = dict() # these will be at 1., 2., ... sec shifts = [[0.001, 0., -0.001], [-0.001, 0.001, 0.]] for time_key, shift in enumerate(shifts): # Create 4x4 matrix transform and normalize temp_trans = deepcopy(raw.info['dev_head_t']) temp_trans['trans'][:3, 3] += shift head_pos_sim[time_key + 1.] = temp_trans['trans'] # # Test raw simulation with basic parameters # raw.info.normalize_proj() cov = read_cov(cov_fname) cov['projs'] = raw.info['projs'] raw.info['bads'] = raw.ch_names[:1] raw_sim = simulate_raw(raw, stc, trans, src, sphere, cov, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) with pytest.warns(RuntimeWarning, match='applying projector with'): raw_sim_2 = simulate_raw(raw, stc, trans_fname, src_fname, sphere, cov_fname, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) std = dict(grad=2e-13, mag=10e-15, eeg=0.1e-6) raw_sim = simulate_raw(raw, stc, trans, src, sphere, make_ad_hoc_cov(raw.info, std=std), head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) raw_sim_2 = simulate_raw(raw, stc, trans_fname, src_fname, sphere, cov=std, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) sphere_norad = make_sphere_model('auto', None, raw.info) raw_meg = raw.copy().pick_types() raw_sim = simulate_raw(raw_meg, stc, trans, src, sphere_norad, make_ad_hoc_cov(raw.info, std=None), head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) raw_sim_2 = simulate_raw(raw_meg, stc, trans_fname, src_fname, sphere_norad, cov='simple', head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) # Test IO on processed data tempdir = _TempDir() test_outname = op.join(tempdir, 'sim_test_raw.fif') raw_sim.save(test_outname) raw_sim_loaded = read_raw_fif(test_outname, preload=True) assert_allclose(raw_sim_loaded[:][0], raw_sim[:][0], rtol=1e-6, atol=1e-20) del raw_sim, raw_sim_2 # with no cov (no noise) but with artifacts, most time periods should match # but the EOG/ECG channels should not for ecg, eog in ((True, False), (False, True), (True, True)): raw_sim_3 = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, blink=eog, ecg=ecg, random_state=seed) raw_sim_4 = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, blink=False, ecg=False, random_state=seed) picks = np.arange(len(raw.ch_names)) diff_picks = pick_types(raw.info, meg=False, ecg=ecg, eog=eog) these_picks = np.setdiff1d(picks, diff_picks) close = np.isclose(raw_sim_3[these_picks][0], raw_sim_4[these_picks][0], atol=1e-20) assert np.mean(close) > 0.7 far = ~np.isclose( raw_sim_3[diff_picks][0], raw_sim_4[diff_picks][0], atol=1e-20) assert np.mean(far) > 0.99 del raw_sim_3, raw_sim_4 # make sure it works with EEG-only and MEG-only raw_sim_meg = simulate_raw(raw.copy().pick_types(meg=True, eeg=False), stc, trans, src, sphere, cov=None, ecg=True, blink=True, random_state=seed) raw_sim_eeg = simulate_raw(raw.copy().pick_types(meg=False, eeg=True), stc, trans, src, sphere, cov=None, ecg=True, blink=True, random_state=seed) raw_sim_meeg = simulate_raw(raw.copy().pick_types(meg=True, eeg=True), stc, trans, src, sphere, cov=None, ecg=True, blink=True, random_state=seed) assert_allclose(np.concatenate((raw_sim_meg[:][0], raw_sim_eeg[:][0])), raw_sim_meeg[:][0], rtol=1e-7, atol=1e-20) del raw_sim_meg, raw_sim_eeg, raw_sim_meeg # check that raw-as-info is supported raw_sim = simulate_raw(raw, stc, trans, src, sphere, cov=None) n_samp = int(round(raw.info['sfreq'])) for use_raw in (raw, raw.info): raw_sim_2 = simulate_raw(use_raw, stc, trans, src, sphere, cov=None, duration=1.) assert len(raw_sim_2.times) == n_samp assert_allclose(raw_sim[:, :n_samp][0], raw_sim_2[:, :n_samp][0], rtol=1e-5, atol=1e-30) del raw_sim, raw_sim_2 # check that different interpolations are similar given small movements raw_sim = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, interp='linear') raw_sim_hann = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, interp='hann') assert_allclose(raw_sim[:][0], raw_sim_hann[:][0], rtol=1e-1, atol=1e-14) del raw_sim, raw_sim_hann # Make impossible transform (translate up into helmet) and ensure failure head_pos_sim_err = deepcopy(head_pos_sim) head_pos_sim_err[1.][2, 3] -= 0.1 # z trans upward 10cm pytest.raises(RuntimeError, simulate_raw, raw, stc, trans, src, sphere, ecg=False, blink=False, head_pos=head_pos_sim_err) pytest.raises(RuntimeError, simulate_raw, raw, stc, trans, src, bem_fname, ecg=False, blink=False, head_pos=head_pos_sim_err) # other degenerate conditions pytest.raises(TypeError, simulate_raw, 'foo', stc, trans, src, sphere) pytest.raises(TypeError, simulate_raw, raw, 'foo', trans, src, sphere) pytest.raises(ValueError, simulate_raw, raw, stc.copy().crop(0, 0), trans, src, sphere) with pytest.raises(ValueError, match='duration cannot be None'): simulate_raw(raw.info, stc, trans, src, sphere) with pytest.raises(TypeError, match='must be an instance of Raw or Info'): simulate_raw(0, stc, trans, src, sphere) stc_bad = stc.copy() stc_bad.tstep += 0.1 pytest.raises(ValueError, simulate_raw, raw, stc_bad, trans, src, sphere) pytest.raises(TypeError, simulate_raw, raw, stc, trans, src, sphere, cov=0) # wrong covariance type pytest.raises(RuntimeError, simulate_raw, raw, stc, trans, src, sphere, chpi=True) # no cHPI info pytest.raises(ValueError, simulate_raw, raw, stc, trans, src, sphere, interp='foo') pytest.raises(TypeError, simulate_raw, raw, stc, trans, src, sphere, head_pos=1.) pytest.raises(RuntimeError, simulate_raw, raw, stc, trans, src, sphere, head_pos=pos_fname) # ends up with t>t_end head_pos_sim_err = deepcopy(head_pos_sim) head_pos_sim_err[-1.] = head_pos_sim_err[1.] # negative time pytest.raises(RuntimeError, simulate_raw, raw, stc, trans, src, sphere, head_pos=head_pos_sim_err) raw_bad = raw.copy() raw_bad.info['dig'] = None pytest.raises(RuntimeError, simulate_raw, raw_bad, stc, trans, src, sphere, blink=True)
def test_simulate_calculate_chpi_positions(): """Test calculation of cHPI positions with simulated data.""" # Read info dict from raw FIF file info = read_info(raw_fname) # Tune the info structure chpi_channel = u'STI201' ncoil = len(info['hpi_results'][0]['order']) coil_freq = 10 + np.arange(ncoil) * 5 hpi_subsystem = {'event_channel': chpi_channel, 'hpi_coils': [{'event_bits': np.array([256, 0, 256, 256], dtype=np.int32)}, {'event_bits': np.array([512, 0, 512, 512], dtype=np.int32)}, {'event_bits': np.array([1024, 0, 1024, 1024], dtype=np.int32)}, {'event_bits': np.array([2048, 0, 2048, 2048], dtype=np.int32)}], 'ncoil': ncoil} info['hpi_subsystem'] = hpi_subsystem for l, freq in enumerate(coil_freq): info['hpi_meas'][0]['hpi_coils'][l]['coil_freq'] = freq picks = pick_types(info, meg=True, stim=True, eeg=False, exclude=[]) info['sfreq'] = 100. # this will speed it up a lot info = pick_info(info, picks) info['chs'][info['ch_names'].index('STI 001')]['ch_name'] = 'STI201' info._update_redundant() info['projs'] = [] info_trans = info['dev_head_t']['trans'].copy() dev_head_pos_ini = np.concatenate([rot_to_quat(info_trans[:3, :3]), info_trans[:3, 3]]) ez = np.array([0, 0, 1]) # Unit vector in z-direction of head coordinates # Define some constants duration = 30 # Time / s # Quotient of head position sampling frequency # and raw sampling frequency head_pos_sfreq_quotient = 0.1 # Round number of head positions to the next integer S = int(duration / (info['sfreq'] * head_pos_sfreq_quotient)) dz = 0.001 # Shift in z-direction is 0.1mm for each step dev_head_pos = np.zeros((S, 10)) dev_head_pos[:, 0] = np.arange(S) * info['sfreq'] * head_pos_sfreq_quotient dev_head_pos[:, 1:4] = dev_head_pos_ini[:3] dev_head_pos[:, 4:7] = dev_head_pos_ini[3:] + \ np.outer(np.arange(S) * dz, ez) dev_head_pos[:, 7] = 1.0 # cm/s dev_head_pos[:, 9] = 100 * dz / (info['sfreq'] * head_pos_sfreq_quotient) # Round number of samples to the next integer raw_data = np.zeros((len(picks), int(duration * info['sfreq'] + 0.5))) raw = RawArray(raw_data, info) dip = Dipole(np.array([0.0, 0.1, 0.2]), np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), np.array([1e-9, 1e-9, 1e-9]), np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]), np.array([1.0, 1.0, 1.0]), 'dip') sphere = make_sphere_model('auto', 'auto', info=info, relative_radii=(1.0, 0.9), sigmas=(0.33, 0.3)) fwd, stc = make_forward_dipole(dip, sphere, info) stc.resample(info['sfreq']) raw = simulate_raw(raw, stc, None, fwd['src'], sphere, cov=None, blink=False, ecg=False, chpi=True, head_pos=dev_head_pos, mindist=1.0, interp='zero', verbose=None) quats = _calculate_chpi_positions( raw, t_step_min=raw.info['sfreq'] * head_pos_sfreq_quotient, t_step_max=raw.info['sfreq'] * head_pos_sfreq_quotient, t_window=1.0) _assert_quats(quats, dev_head_pos, dist_tol=0.001, angle_tol=1.)
def test_brainvision_data(): """Test reading raw Brain Vision files.""" pytest.raises(IOError, read_raw_brainvision, vmrk_path) pytest.raises(ValueError, read_raw_brainvision, vhdr_path, preload=True, scale="foo") raw_py = _test_raw_reader(read_raw_brainvision, vhdr_fname=vhdr_path, eog=eog, misc='auto') assert ('RawBrainVision' in repr(raw_py)) assert_equal(raw_py.info['highpass'], 0.) assert_equal(raw_py.info['lowpass'], 250.) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads') data_py, times_py = raw_py[picks] # compare with a file that was generated using MNE-C raw_bin = read_raw_fif(eeg_bin, preload=True) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads') data_bin, times_bin = raw_bin[picks] assert_array_almost_equal(data_py, data_bin) assert_array_almost_equal(times_py, times_bin) # Make sure EOG channels are marked correctly for ch in raw_py.info['chs']: if ch['ch_name'] in eog: assert_equal(ch['kind'], FIFF.FIFFV_EOG_CH) elif ch['ch_name'] == 'STI 014': assert_equal(ch['kind'], FIFF.FIFFV_STIM_CH) elif ch['ch_name'] in ('CP5', 'CP6'): assert_equal(ch['kind'], FIFF.FIFFV_MISC_CH) assert_equal(ch['unit'], FIFF.FIFF_UNIT_NONE) elif ch['ch_name'] == 'ReRef': assert_equal(ch['kind'], FIFF.FIFFV_MISC_CH) assert_equal(ch['unit'], FIFF.FIFF_UNIT_CEL) elif ch['ch_name'] in raw_py.info['ch_names']: assert_equal(ch['kind'], FIFF.FIFFV_EEG_CH) assert_equal(ch['unit'], FIFF.FIFF_UNIT_V) else: raise RuntimeError("Unknown Channel: %s" % ch['ch_name']) # test loading v2 read_raw_brainvision(vhdr_v2_path, eog=eog, preload=True, verbose='error') # test different units with alternative header file raw_units = _test_raw_reader(read_raw_brainvision, vhdr_fname=vhdr_units_path, eog=eog, misc='auto') assert_equal(raw_units.info['chs'][0]['ch_name'], 'FP1') assert_equal(raw_units.info['chs'][0]['kind'], FIFF.FIFFV_EEG_CH) data_units, _ = raw_units[0] assert_array_almost_equal(data_py[0, :], data_units.squeeze()) assert_equal(raw_units.info['chs'][1]['ch_name'], 'FP2') assert_equal(raw_units.info['chs'][1]['kind'], FIFF.FIFFV_EEG_CH) data_units, _ = raw_units[1] assert_array_almost_equal(data_py[1, :], data_units.squeeze()) assert_equal(raw_units.info['chs'][2]['ch_name'], 'F3') assert_equal(raw_units.info['chs'][2]['kind'], FIFF.FIFFV_EEG_CH) data_units, _ = raw_units[2] assert_array_almost_equal(data_py[2, :], data_units.squeeze())
# sphinx_gallery_thumbnail_number = 9 ############################################################################### # Process MEG data data_path = sample.data_path() raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' raw = mne.io.read_raw_fif(raw_fname) # already has an average reference events = mne.find_events(raw, stim_channel='STI 014') event_id = dict(aud_r=1) # event trigger and conditions tmin = -0.2 # start of each epoch (200ms before the trigger) tmax = 0.5 # end of each epoch (500ms after the trigger) raw.info['bads'] = ['MEG 2443', 'EEG 053'] picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=True, exclude='bads') baseline = (None, 0) # means from the first instant to t = 0 reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6) epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=baseline, reject=reject) ############################################################################### # Compute regularized noise covariance
def test_plot_ica_properties(): """Test plotting of ICA properties.""" import matplotlib.pyplot as plt res = 8 raw = _get_raw(preload=True) raw.add_proj([], remove_existing=True) events = _get_events() picks = _get_picks(raw)[:6] pick_names = [raw.ch_names[k] for k in picks] raw.pick_channels(pick_names) epochs = Epochs(raw, events[:10], event_id, tmin, tmax, baseline=(None, 0), preload=True) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=2, n_pca_components=2) with pytest.warns(RuntimeWarning, match='projection'): ica.fit(raw) # test _create_properties_layout fig, ax = _create_properties_layout() assert_equal(len(ax), 5) topoargs = dict(topomap_args={'res': res, 'contours': 0, "sensors": False}) ica.plot_properties(raw, picks=0, **topoargs) ica.plot_properties(epochs, picks=1, dB=False, plot_std=1.5, **topoargs) ica.plot_properties(epochs, picks=1, image_args={'sigma': 1.5}, topomap_args={ 'res': 10, 'colorbar': True }, psd_args={'fmax': 65.}, plot_std=False, figsize=[4.5, 4.5]) plt.close('all') pytest.raises(TypeError, ica.plot_properties, epochs, dB=list('abc')) pytest.raises(TypeError, ica.plot_properties, ica) pytest.raises(TypeError, ica.plot_properties, [0.2]) pytest.raises(TypeError, plot_ica_properties, epochs, epochs) pytest.raises(TypeError, ica.plot_properties, epochs, psd_args='not dict') pytest.raises(ValueError, ica.plot_properties, epochs, plot_std=[]) fig, ax = plt.subplots(2, 3) ax = ax.ravel()[:-1] ica.plot_properties(epochs, picks=1, axes=ax, **topoargs) fig = ica.plot_properties(raw, picks=[0, 1], **topoargs) assert_equal(len(fig), 2) pytest.raises(TypeError, plot_ica_properties, epochs, ica, picks=[0, 1], axes=ax) pytest.raises(ValueError, ica.plot_properties, epochs, axes='not axes') plt.close('all') # Test merging grads. raw = _get_raw(preload=True) picks = pick_types(raw.info, meg='grad')[:10] ica = ICA(n_components=2) ica.fit(raw, picks=picks) ica.plot_properties(raw) plt.close('all')
def test_dipole_fitting(tmpdir): """Test dipole fitting.""" amp = 100e-9 tempdir = str(tmpdir) rng = np.random.RandomState(0) fname_dtemp = op.join(tempdir, 'test.dip') fname_sim = op.join(tempdir, 'test-ave.fif') fwd = convert_forward_solution(read_forward_solution(fname_fwd), surf_ori=False, force_fixed=True, use_cps=True) evoked = read_evokeds(fname_evo)[0] cov = read_cov(fname_cov) n_per_hemi = 5 vertices = [ np.sort(rng.permutation(s['vertno'])[:n_per_hemi]) for s in fwd['src'] ] nv = sum(len(v) for v in vertices) stc = SourceEstimate(amp * np.eye(nv), vertices, 0, 0.001) evoked = simulate_evoked(fwd, stc, evoked.info, cov, nave=evoked.nave, random_state=rng) # For speed, let's use a subset of channels (strange but works) picks = np.sort( np.concatenate([ pick_types(evoked.info, meg=True, eeg=False)[::2], pick_types(evoked.info, meg=False, eeg=True)[::2] ])) evoked.pick_channels([evoked.ch_names[p] for p in picks]) evoked.add_proj(make_eeg_average_ref_proj(evoked.info)) write_evokeds(fname_sim, evoked) # Run MNE-C version run_subprocess([ 'mne_dipole_fit', '--meas', fname_sim, '--meg', '--eeg', '--noise', fname_cov, '--dip', fname_dtemp, '--mri', fname_fwd, '--reg', '0', '--tmin', '0', ]) dip_c = read_dipole(fname_dtemp) # Run mne-python version sphere = make_sphere_model(head_radius=0.1) with pytest.warns(RuntimeWarning, match='projection'): dip, residual = fit_dipole(evoked, cov, sphere, fname_fwd, rank='info') # just to test rank support assert isinstance(residual, Evoked) # Sanity check: do our residuals have less power than orig data? data_rms = np.sqrt(np.sum(evoked.data**2, axis=0)) resi_rms = np.sqrt(np.sum(residual.data**2, axis=0)) assert (data_rms > resi_rms * 0.95).all(), \ '%s (factor: %s)' % ((data_rms / resi_rms).min(), 0.95) # Compare to original points transform_surface_to(fwd['src'][0], 'head', fwd['mri_head_t']) transform_surface_to(fwd['src'][1], 'head', fwd['mri_head_t']) assert fwd['src'][0]['coord_frame'] == FIFF.FIFFV_COORD_HEAD src_rr = np.concatenate([s['rr'][v] for s, v in zip(fwd['src'], vertices)], axis=0) src_nn = np.concatenate([s['nn'][v] for s, v in zip(fwd['src'], vertices)], axis=0) # MNE-C skips the last "time" point :( out = dip.crop(dip_c.times[0], dip_c.times[-1]) assert (dip is out) src_rr, src_nn = src_rr[:-1], src_nn[:-1] # check that we did about as well corrs, dists, gc_dists, amp_errs, gofs = [], [], [], [], [] for d in (dip_c, dip): new = d.pos diffs = new - src_rr corrs += [np.corrcoef(src_rr.ravel(), new.ravel())[0, 1]] dists += [np.sqrt(np.mean(np.sum(diffs * diffs, axis=1)))] gc_dists += [ 180 / np.pi * np.mean(np.arccos(np.sum(src_nn * d.ori, axis=1))) ] amp_errs += [np.sqrt(np.mean((amp - d.amplitude)**2))] gofs += [np.mean(d.gof)] # XXX possibly some OpenBLAS numerical differences make # things slightly worse for us factor = 0.7 assert dists[0] / factor >= dists[1], 'dists: %s' % dists assert corrs[0] * factor <= corrs[1], 'corrs: %s' % corrs assert gc_dists[0] / factor >= gc_dists[1] * 0.8, \ 'gc-dists (ori): %s' % gc_dists assert amp_errs[0] / factor >= amp_errs[1],\ 'amplitude errors: %s' % amp_errs # This one is weird because our cov/sim/picking is weird assert gofs[0] * factor <= gofs[1] * 2, 'gof: %s' % gofs
def test_compute_tfr(): """Test _compute_tfr function.""" # Set parameters event_id = 1 tmin = -0.2 tmax = 0.498 # Allows exhaustive decimation testing # Setup for reading the raw data raw = read_raw_fif(raw_fname) events = read_events(event_fname) exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053'] # bads + 2 more # picks MEG gradiometers picks = pick_types(raw.info, meg='grad', eeg=False, stim=False, include=[], exclude=exclude) picks = picks[:2] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks) data = epochs.get_data() sfreq = epochs.info['sfreq'] freqs = np.arange(10, 20, 3).astype(float) # Check all combination of options for method, use_fft, zero_mean, output in product( ('multitaper', 'morlet'), (False, True), (False, True), ('complex', 'power', 'phase', 'avg_power_itc', 'avg_power', 'itc')): # Check exception if (method == 'multitaper') and (output == 'phase'): assert_raises(NotImplementedError, _compute_tfr, data, freqs, sfreq, method=method, output=output) continue # Check runs out = _compute_tfr(data, freqs, sfreq, method=method, use_fft=use_fft, zero_mean=zero_mean, n_cycles=2., output=output) # Check shapes shape = np.r_[data.shape[:2], len(freqs), data.shape[2]] if ('avg' in output) or ('itc' in output): assert_array_equal(shape[1:], out.shape) else: assert_array_equal(shape, out.shape) # Check types if output in ('complex', 'avg_power_itc'): assert_equal(np.complex, out.dtype) else: assert_equal(np.float, out.dtype) assert_true(np.all(np.isfinite(out))) # Check errors params for _data in (None, 'foo', data[0]): assert_raises(ValueError, _compute_tfr, _data, freqs, sfreq) for _freqs in (None, 'foo', [[0]]): assert_raises(ValueError, _compute_tfr, data, _freqs, sfreq) for _sfreq in (None, 'foo'): assert_raises(ValueError, _compute_tfr, data, freqs, _sfreq) for key in ('output', 'method', 'use_fft', 'decim', 'n_jobs'): for value in (None, 'foo'): kwargs = {key: value} # FIXME pep8 assert_raises(ValueError, _compute_tfr, data, freqs, sfreq, **kwargs) # No time_bandwidth param in morlet assert_raises(ValueError, _compute_tfr, data, freqs, sfreq, method='morlet', time_bandwidth=1) # No phase in multitaper XXX Check ? assert_raises(NotImplementedError, _compute_tfr, data, freqs, sfreq, method='multitaper', output='phase') # Inter-trial coherence tests out = _compute_tfr(data, freqs, sfreq, output='itc', n_cycles=2.) assert_true(np.sum(out >= 1) == 0) assert_true(np.sum(out <= 0) == 0) # Check decim shapes # 2: multiple of len(times) even # 3: multiple odd # 8: not multiple, even # 9: not multiple, odd for decim in (2, 3, 8, 9, slice(0, 2), slice(1, 3), slice(2, 4)): _decim = slice(None, None, decim) if isinstance(decim, int) else decim n_time = len(np.arange(data.shape[2])[_decim]) shape = np.r_[data.shape[:2], len(freqs), n_time] for method in ('multitaper', 'morlet'): # Single trials out = _compute_tfr(data, freqs, sfreq, method=method, decim=decim, n_cycles=2.) assert_array_equal(shape, out.shape) # Averages out = _compute_tfr(data, freqs, sfreq, method=method, decim=decim, output='avg_power', n_cycles=2.) assert_array_equal(shape[1:], out.shape)
event_id = 1 tmin = -0.2 tmax = 0.5 # Setup for reading the raw data raw = io.read_raw_fif(raw_fname) events = mne.read_events(event_fname) include = [] raw.info['bads'] += ['MEG 2443', 'EEG 053'] # bads + 2 more # picks MEG gradiometers picks = mne.pick_types(raw.info, meg='grad', eeg=False, eog=True, stim=False, include=include, exclude='bads') ch_name = raw.info['ch_names'][picks[0]] # Load condition 1 reject = dict(grad=4000e-13, eog=150e-6) event_id = 1 epochs_condition_1 = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,
def get_eeg_inds(raw): """ locate the indices in the data corresponding to EEG channels :param raw: raw data """ eeg_inds = mne.pick_types(raw.info, meg=False, eeg=True) return eeg_inds
n_components = 6 repeat_times = 10 n_folder = 10 # multi cores n_jobs = 12 # prepare rawobject raw_files = [ mne.io.read_raw_ctf(file_dir % (subject_name, subject_idx, j), preload=True) for j in run_idx ] raw = mne.concatenate_raws(raw_files) raw.filter(freq_l, freq_h, fir_design=fir_design) # choose channel type picks = mne.pick_types(raw.info, meg=meg, ref_meg=ref_meg, exclude=exclude) ############# # Let it go # ############# # Get epochs print('Getting epochs.') events = mne.find_events(raw, stim_channel=stim_channel) baseline = (tmin, t0) epochs = mne.Epochs(raw, event_id=event_id, events=events, decim=decim, tmin=tmin, tmax=tmax, picks=picks,
# created from sin and cos data. # Any data in shape (n_epochs, n_channels, n_times) can be used. epochs_data = np.array([[sin[:700], cos[:700]], [sin[1000:1700], cos[1000:1700]], [sin[1800:2500], cos[1800:2500]]]) ch_names = ['sin', 'cos'] ch_types = ['mag', 'mag'] info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) epochs = mne.EpochsArray(epochs_data, info=info, events=events, event_id={'arbitrary': 1}) picks = mne.pick_types(info, meg=True, eeg=False, misc=False) epochs.plot(picks=picks, scalings='auto', show=True, block=True) ############################################################################### # EvokedArray nave = len(epochs_data) # Number of averaged epochs evoked_data = np.mean(epochs_data, axis=0) evokeds = mne.EvokedArray(evoked_data, info=info, tmin=-0.2, comment='Arbitrary', nave=nave) evokeds.plot(picks=picks,
def SourceReconstructionv3(condarray, condrenames, ListSubj, modality, Method, covmatsource): # condarray = (['EtPre'],['EtPast','EtFut']) # condrenames = ('NoProjT','ProjT') # ListSubj = ('jm100109',) # modality = 'MEG' # Method = 'dSPM' # covmatsource = 'EV' import mne from mne.minimum_norm import apply_inverse, make_inverse_operator from mne.beamformer import lcmv import os os.chdir('/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/SCRIPTS/MNE_PYTHON') os.environ['SUBJECTS_DIR'] = '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/mri' os.environ['MNE_ROOT'] = '/neurospin/local/mne' wdir = "/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/" for c in range(len(condarray)): for i in range(len(ListSubj)): # which modality? if modality == 'MEG': megtag = True eegtag = False fname_fwd = (wdir + ListSubj[i] + "/mne_python/run3_ico5_megonly_icacorr_-fwd.fif") elif modality == 'EEG': megtag = False eegtag = True fname_fwd = (wdir + ListSubj[i] + "/mne_python/run3_ico5_eegonly_icacorr_-fwd.fif") elif modality == 'MEEG': megtag = True eegtag = True fname_fwd = (wdir + ListSubj[i] + "/mne_python/run3_ico5_meeg_icacorr_-fwd.fif") # load noise covariance matrice fname_noisecov = (wdir + ListSubj[i] + "/mne_python/COVMATS/" + modality + "_noisecov_icacorr_" + covmatsource + "_" + ListSubj[i] + "-cov.fif") NOISE_COV1 = mne.read_cov(fname_noisecov) # concatenate epochs if several conditions if len(condarray[c]) == 1: condnames = [] condnames = condarray[c][0] # load MEEG epochs, then pick fname_epochs = (wdir + ListSubj[i] + "/mne_python/EPOCHS/MEEG_epochs_icacorr_" + condnames + '_' + ListSubj[i] + "-epo.fif") epochs = mne.read_epochs(fname_epochs) picks = mne.pick_types(epochs.info, meg=megtag, eeg=eegtag, stim=False, eog=False) # compute evoked evokedcond1 = epochs.average(picks=picks) forward = mne.read_forward_solution(fname_fwd, surf_ori=True) inverse_operator1 = make_inverse_operator(evokedcond1.info, forward, NOISE_COV1, loose=0.2, depth=0.8) elif len(condarray[c]) > 1: epochlist = [] for ca in range(len(condarray[c])): condnames = [] condnames = condarray[c][ca] # load MEEG epochs, then pick fname_epochs = (wdir + ListSubj[i] + "/mne_python/EPOCHS/MEEG_epochs_icacorr_" + condnames + '_' + ListSubj[i] + "-epo.fif") epochs = mne.read_epochs(fname_epochs) picks = mne.pick_types(epochs.info, meg=megtag, eeg=eegtag, stim=False, eog=False) epochlist.append(epochs) epochs = [] epochs = mne.epochs.concatenate_epochs(epochlist) # compute evoked evokedcond1 = epochs.average(picks=picks) forward = mne.read_forward_solution(fname_fwd, surf_ori=True) inverse_operator1 = make_inverse_operator(evokedcond1.info, forward, NOISE_COV1, loose=0.2, depth=0.8) if Method == 'LCMV': fname_datacov = (wdir + ListSubj[i] + "/mne_python/COVMATS/" + modality + "datacov_" + condnames[c] + "_" + ListSubj[i] + "-cov.fif") DATA_COV1 = mne.read_cov(fname_datacov) ############################### # dSPM/ MNE/ sLORETA solution # ############################### if Method == 'dSPM' or Method == 'MNE' or Method == 'sLORETA': snr = 3.0 lambda2 = 1.0 / snr**2 # MEG source reconstruction stccond1 = apply_inverse(evokedcond1, inverse_operator1, lambda2, method=Method, pick_ori=None) stccond1.save(wdir + ListSubj[i] + "/mne_python/STCS/IcaCorr_" + modality + "_" + ListSubj[i] + "_" + condrenames[c] + "_pick_oriNone_" + Method + "_ico-5-fwd.fif") stccond1norm = apply_inverse(evokedcond1, inverse_operator1, lambda2, method=Method, pick_ori="normal") stccond1norm.save(wdir + ListSubj[i] + "/mne_python/STCS/IcaCorr_" + modality + "_" + ListSubj[i] + "_" + condrenames[c] + "_pick_orinormal_" + Method + "_ico-5-fwd.fif") # morphing to fsaverage stc_fsaverage_cond1 = mne.morph_data(ListSubj[i], 'fsaverage', stccond1, smooth=20) stc_fsaverage_cond1.save(wdir + ListSubj[i] + "/mne_python/STCS/IcaCorr_" + modality + "_" + ListSubj[i] + "_" + condrenames[c] + "_pick_oriNone_" + Method + "_ico-5-fwd-fsaverage.fif") stc_fsaverage_cond1norm = mne.morph_data(ListSubj[i], 'fsaverage', stccond1norm, smooth=20) stc_fsaverage_cond1norm.save(wdir + ListSubj[i] + "/mne_python/STCS/IcaCorr_" + modality + "_" + ListSubj[i] + "_" + condrenames[c] + "_pick_orinormal_" + Method + "_ico-5-fwd-fsaverage.fif") ################### # LCMV Beamformer # ################### elif Method == 'LCMV': # MEG source reconstruction stccond1 = lcmv(evokedcond1, forward, NOISE_COV1, DATA_COV1, reg=0.01, pick_ori=None) stccond1.save(wdir + ListSubj[i] + "/mne_python/STCS/" + modality + "_" + ListSubj[i] + "_" + condrenames[c] + "_pick_oriNone_" + Method + "_ico-5-fwd.fif") stccond1norm = lcmv(evokedcond1, forward, NOISE_COV1, DATA_COV1, reg=0.01, pick_ori="normal") stccond1norm.save(wdir + ListSubj[i] + "/mne_python/STCS/" + modality + "_" + ListSubj[i] + "_" + condrenames[c] + "_pick_orinormal_" + Method + "_ico-5-fwd.fif") # morphing to fsaverage stc_fsaverage_cond1 = mne.morph_data(ListSubj[i], 'fsaverage', stccond1, smooth=20) stc_fsaverage_cond1.save(wdir + ListSubj[i] + "/mne_python/STCS/" + modality + "_" + ListSubj[i] + "_" + condrenames[c] + "_pick_oriNone_" + Method + "_ico-5-fwd-fsaverage.fif") stc_fsaverage_cond1norm = mne.morph_data(ListSubj[i], 'fsaverage', stccond1norm, smooth=20) stc_fsaverage_cond1norm.save(wdir + ListSubj[i] + "/mne_python/STCS/" + modality + "_" + ListSubj[i] + "_" + condrenames[c] + "_pick_orinormal_" + Method + "_ico-5-fwd-fsaverage.fif")
def test_cov_order(): """Test covariance ordering.""" raw = read_raw_fif(raw_fname) raw.set_eeg_reference(projection=True) info = raw.info # add MEG channel with low enough index number to affect EEG if # order is incorrect info['bads'] += ['MEG 0113'] ch_names = [ info['ch_names'][pick] for pick in pick_types(info, meg=False, eeg=True) ] cov = read_cov(cov_fname) # no avg ref present warning prepare_noise_cov(cov, info, ch_names, verbose='error') # big reordering cov_reorder = cov.copy() order = np.random.RandomState(0).permutation(np.arange(len(cov.ch_names))) cov_reorder['names'] = [cov['names'][ii] for ii in order] cov_reorder['data'] = cov['data'][order][:, order] # Make sure we did this properly _assert_reorder(cov_reorder, cov, order) # Now check some functions that should get the same result for both # regularize with pytest.raises(ValueError, match='rank, if str'): regularize(cov, info, rank='foo') with pytest.raises(TypeError, match='rank must be'): regularize(cov, info, rank=False) with pytest.raises(TypeError, match='rank must be'): regularize(cov, info, rank=1.) cov_reg = regularize(cov, info, rank='full') cov_reg_reorder = regularize(cov_reorder, info, rank='full') _assert_reorder(cov_reg_reorder, cov_reg, order) # prepare_noise_cov cov_prep = prepare_noise_cov(cov, info, ch_names) cov_prep_reorder = prepare_noise_cov(cov, info, ch_names) _assert_reorder(cov_prep, cov_prep_reorder, order=np.arange(len(cov_prep['names']))) # compute_whitener whitener, w_ch_names, n_nzero = compute_whitener(cov, info, return_rank=True) assert whitener.shape[0] == whitener.shape[1] whitener_2, w_ch_names_2, n_nzero_2 = compute_whitener(cov_reorder, info, return_rank=True) assert_array_equal(w_ch_names_2, w_ch_names) assert_allclose(whitener_2, whitener) assert n_nzero == n_nzero_2 # with pca assert n_nzero < whitener.shape[0] whitener_pca, w_ch_names_pca, n_nzero_pca = compute_whitener( cov, info, pca=True, return_rank=True) assert_array_equal(w_ch_names_pca, w_ch_names) assert n_nzero_pca == n_nzero assert whitener_pca.shape == (n_nzero_pca, len(w_ch_names)) # whiten_evoked evoked = read_evokeds(ave_fname)[0] evoked_white = whiten_evoked(evoked, cov) evoked_white_2 = whiten_evoked(evoked, cov_reorder) assert_allclose(evoked_white_2.data, evoked_white.data)
def test_dipole_fitting(): """Test dipole fitting.""" amp = 10e-9 tempdir = _TempDir() rng = np.random.RandomState(0) fname_dtemp = op.join(tempdir, 'test.dip') fname_sim = op.join(tempdir, 'test-ave.fif') fwd = convert_forward_solution(read_forward_solution(fname_fwd), surf_ori=False, force_fixed=True) evoked = read_evokeds(fname_evo)[0] cov = read_cov(fname_cov) n_per_hemi = 5 vertices = [ np.sort(rng.permutation(s['vertno'])[:n_per_hemi]) for s in fwd['src'] ] nv = sum(len(v) for v in vertices) stc = SourceEstimate(amp * np.eye(nv), vertices, 0, 0.001) evoked = simulate_evoked(fwd, stc, evoked.info, cov, snr=20, random_state=rng) # For speed, let's use a subset of channels (strange but works) picks = np.sort( np.concatenate([ pick_types(evoked.info, meg=True, eeg=False)[::2], pick_types(evoked.info, meg=False, eeg=True)[::2] ])) evoked.pick_channels([evoked.ch_names[p] for p in picks]) evoked.add_proj(make_eeg_average_ref_proj(evoked.info)) write_evokeds(fname_sim, evoked) # Run MNE-C version run_subprocess([ 'mne_dipole_fit', '--meas', fname_sim, '--meg', '--eeg', '--noise', fname_cov, '--dip', fname_dtemp, '--mri', fname_fwd, '--reg', '0', '--tmin', '0', ]) dip_c = read_dipole(fname_dtemp) # Run mne-python version sphere = make_sphere_model(head_radius=0.1) dip, residuals = fit_dipole(evoked, fname_cov, sphere, fname_fwd) # Sanity check: do our residuals have less power than orig data? data_rms = np.sqrt(np.sum(evoked.data**2, axis=0)) resi_rms = np.sqrt(np.sum(residuals**2, axis=0)) factor = 1. # XXX weird, inexplicable differenc for 3.5 build we'll assume is due to # Anaconda bug for now... if os.getenv('TRAVIS', 'false') == 'true' and \ sys.version[:3] in ('3.5', '2.7'): factor = 0.8 assert_true((data_rms > factor * resi_rms).all(), msg='%s (factor: %s)' % ((data_rms / resi_rms).min(), factor)) # Compare to original points transform_surface_to(fwd['src'][0], 'head', fwd['mri_head_t']) transform_surface_to(fwd['src'][1], 'head', fwd['mri_head_t']) src_rr = np.concatenate([s['rr'][v] for s, v in zip(fwd['src'], vertices)], axis=0) src_nn = np.concatenate([s['nn'][v] for s, v in zip(fwd['src'], vertices)], axis=0) # MNE-C skips the last "time" point :( dip.crop(dip_c.times[0], dip_c.times[-1]) src_rr, src_nn = src_rr[:-1], src_nn[:-1] # check that we did at least as well corrs, dists, gc_dists, amp_errs, gofs = [], [], [], [], [] for d in (dip_c, dip): new = d.pos diffs = new - src_rr corrs += [np.corrcoef(src_rr.ravel(), new.ravel())[0, 1]] dists += [np.sqrt(np.mean(np.sum(diffs * diffs, axis=1)))] gc_dists += [ 180 / np.pi * np.mean(np.arccos(np.sum(src_nn * d.ori, axis=1))) ] amp_errs += [np.sqrt(np.mean((amp - d.amplitude)**2))] gofs += [np.mean(d.gof)] assert_true(dists[0] >= dists[1] * factor, 'dists: %s' % dists) assert_true(corrs[0] <= corrs[1] / factor, 'corrs: %s' % corrs) assert_true(gc_dists[0] >= gc_dists[1] * factor, 'gc-dists (ori): %s' % gc_dists) assert_true(amp_errs[0] >= amp_errs[1] * factor, 'amplitude errors: %s' % amp_errs) assert_true(gofs[0] <= gofs[1] / factor, 'gof: %s' % gofs)
data = data / 1e6 ch_names = EEG_channels ch_types = ["eeg"] * len(ch_names) # It is also possible to use info from another raw object. info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) raw = mne.io.RawArray(data, info) raw = raw.crop(0, 60).load_data() # Rename channels to fit with standard conventions raw.rename_channels(chn_name_mapping) # Add a montage to the data montage_kind = "standard_1005" montage = mne.channels.make_standard_montage(montage_kind) # Extract some info eeg_index = mne.pick_types(raw.info, eeg=True, eog=False, meg=False) ch_names = raw.info["ch_names"] ch_names_eeg = list(np.asarray(ch_names)[eeg_index]) sample_rate = raw.info["sfreq"] # Make a copy of the data raw_copy = raw.copy() #Filter data # ch = np.arange(0,3) # filtered = raw.filter(0.5,30) # # scalings = {'eeg': 0.00005} # Could also pass a dictionary with some value == 'auto' # plot1 = filtered.plot(n_channels=32, scalings=scalings, title='Original', # show=False, block=False)
def test_simulate_raw_sphere(raw_data, tmpdir): """Test simulation of raw data with sphere model.""" seed = 42 raw, src, stc, trans, sphere = raw_data assert len(pick_types(raw.info, meg=False, ecg=True)) == 1 tempdir = str(tmpdir) # head pos head_pos_sim = _get_head_pos_sim(raw) # # Test raw simulation with basic parameters # raw.info.normalize_proj() cov = read_cov(cov_fname) cov['projs'] = raw.info['projs'] raw.info['bads'] = raw.ch_names[:1] with pytest.deprecated_call(match='cov is deprecated'): raw_sim = simulate_raw(raw, stc, trans, src, sphere, cov, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed, verbose=True) with pytest.warns(RuntimeWarning, match='applying projector with'): raw_sim_2 = simulate_raw(raw, stc, trans_fname, src_fname, sphere, cov_fname, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) with pytest.raises(RuntimeError, match='Maximum number of STC iterations'): simulate_raw(raw.info, [stc] * 5, trans_fname, src_fname, sphere, cov=None, max_iter=1) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) std = dict(grad=2e-13, mag=10e-15, eeg=0.1e-6) with pytest.deprecated_call(): raw_sim = simulate_raw(raw, stc, trans, src, sphere, make_ad_hoc_cov(raw.info, std=std), head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) with pytest.deprecated_call(): raw_sim_2 = simulate_raw(raw, stc, trans_fname, src_fname, sphere, cov=std, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) sphere_norad = make_sphere_model('auto', None, raw.info) raw_meg = raw.copy().pick_types() with pytest.deprecated_call(): raw_sim = simulate_raw(raw_meg, stc, trans, src, sphere_norad, cov=None, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) with pytest.deprecated_call(): raw_sim_2 = simulate_raw(raw_meg, stc, trans_fname, src_fname, sphere_norad, cov=None, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) # Test IO on processed data test_outname = op.join(tempdir, 'sim_test_raw.fif') raw_sim.save(test_outname) raw_sim_loaded = read_raw_fif(test_outname, preload=True) assert_allclose(raw_sim_loaded[:][0], raw_sim[:][0], rtol=1e-6, atol=1e-20) del raw_sim, raw_sim_2 # with no cov (no noise) but with artifacts, most time periods should match # but the EOG/ECG channels should not for ecg, eog in ((True, False), (False, True), (True, True)): with pytest.deprecated_call(): raw_sim_3 = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, blink=eog, ecg=ecg, random_state=seed) with pytest.deprecated_call(): raw_sim_4 = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, blink=False, ecg=False, random_state=seed) picks = np.arange(len(raw.ch_names)) diff_picks = pick_types(raw.info, meg=False, ecg=ecg, eog=eog) these_picks = np.setdiff1d(picks, diff_picks) close = np.isclose(raw_sim_3[these_picks][0], raw_sim_4[these_picks][0], atol=1e-20) assert np.mean(close) > 0.7 far = ~np.isclose(raw_sim_3[diff_picks][0], raw_sim_4[diff_picks][0], atol=1e-20) assert np.mean(far) > 0.99 del raw_sim_3, raw_sim_4 # make sure it works with EEG-only and MEG-only with pytest.deprecated_call(): raw_sim_meg = simulate_raw(raw.copy().pick_types(meg=True, eeg=False), stc, trans, src, sphere, cov=None) raw_sim_eeg = simulate_raw(raw.copy().pick_types(meg=False, eeg=True), stc, trans, src, sphere, cov=None) raw_sim_meeg = simulate_raw(raw.copy().pick_types(meg=True, eeg=True), stc, trans, src, sphere, cov=None) for this_raw in (raw_sim_meg, raw_sim_eeg, raw_sim_meeg): add_eog(this_raw, random_state=seed) for this_raw in (raw_sim_meg, raw_sim_meeg): add_ecg(this_raw, random_state=seed) with pytest.raises(RuntimeError, match='only add ECG artifacts if MEG'): add_ecg(raw_sim_eeg) assert_allclose(np.concatenate((raw_sim_meg[:][0], raw_sim_eeg[:][0])), raw_sim_meeg[:][0], rtol=1e-7, atol=1e-20) del raw_sim_meg, raw_sim_eeg, raw_sim_meeg # check that raw-as-info is supported n_samp = len(stc.times) raw_crop = raw.copy().crop(0., (n_samp - 1.) / raw.info['sfreq']) assert len(raw_crop.times) == len(stc.times) with pytest.deprecated_call(): raw_sim = simulate_raw(raw_crop, stc, trans, src, sphere, cov=None) with catch_logging() as log: raw_sim_2 = simulate_raw(raw_crop.info, stc, trans, src, sphere, cov=None, verbose=True) log = log.getvalue() assert '1 STC iteration provided' in log assert len(raw_sim_2.times) == n_samp assert_allclose(raw_sim[:, :n_samp][0], raw_sim_2[:, :n_samp][0], rtol=1e-5, atol=1e-30) del raw_sim, raw_sim_2 # check that different interpolations are similar given small movements with pytest.deprecated_call(): raw_sim = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, interp='linear') with pytest.deprecated_call(): raw_sim_hann = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, interp='hann') assert_allclose(raw_sim[:][0], raw_sim_hann[:][0], rtol=1e-1, atol=1e-14) del raw_sim, raw_sim_hann