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_set_channel_types(): """Test set_channel_types""" raw = read_raw_fif(raw_fname) # Error Tests # Test channel name exists in ch_names mapping = {'EEG 160': 'EEG060'} assert_raises(ValueError, raw.set_channel_types, mapping) # Test change to illegal channel type mapping = {'EOG 061': 'xxx'} assert_raises(ValueError, raw.set_channel_types, mapping) # Test changing type if in proj (avg eeg ref here) mapping = {'EEG 058': 'ecog', 'EEG 059': 'ecg', 'EEG 060': 'eog', 'EOG 061': 'seeg', 'MEG 2441': 'eeg', 'MEG 2443': 'eeg', 'MEG 2442': 'hbo'} assert_raises(RuntimeError, raw.set_channel_types, mapping) # Test type change raw2 = read_raw_fif(raw_fname) raw2.info['bads'] = ['EEG 059', 'EEG 060', 'EOG 061'] with warnings.catch_warnings(record=True): # MEG channel change assert_raises(RuntimeError, raw2.set_channel_types, mapping) # has prj raw2.add_proj([], remove_existing=True) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') raw2.set_channel_types(mapping) assert_true(len(w) >= 1, msg=[str(ww.message) for ww in w]) assert_true(all('The unit for channel' in str(ww.message) for ww in w)) info = raw2.info assert_true(info['chs'][372]['ch_name'] == 'EEG 058') assert_true(info['chs'][372]['kind'] == FIFF.FIFFV_ECOG_CH) assert_true(info['chs'][372]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][372]['coil_type'] == FIFF.FIFFV_COIL_EEG) assert_true(info['chs'][373]['ch_name'] == 'EEG 059') assert_true(info['chs'][373]['kind'] == FIFF.FIFFV_ECG_CH) assert_true(info['chs'][373]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][373]['coil_type'] == FIFF.FIFFV_COIL_NONE) assert_true(info['chs'][374]['ch_name'] == 'EEG 060') assert_true(info['chs'][374]['kind'] == FIFF.FIFFV_EOG_CH) assert_true(info['chs'][374]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][374]['coil_type'] == FIFF.FIFFV_COIL_NONE) assert_true(info['chs'][375]['ch_name'] == 'EOG 061') assert_true(info['chs'][375]['kind'] == FIFF.FIFFV_SEEG_CH) assert_true(info['chs'][375]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][375]['coil_type'] == FIFF.FIFFV_COIL_EEG) for idx in pick_channels(raw.ch_names, ['MEG 2441', 'MEG 2443']): assert_true(info['chs'][idx]['kind'] == FIFF.FIFFV_EEG_CH) assert_true(info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_EEG) idx = pick_channels(raw.ch_names, ['MEG 2442'])[0] assert_true(info['chs'][idx]['kind'] == FIFF.FIFFV_FNIRS_CH) assert_true(info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_MOL) assert_true(info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_FNIRS_HBO) # Test meaningful error when setting channel type with unknown unit raw.info['chs'][0]['unit'] = 0. ch_types = {raw.ch_names[0]: 'misc'} assert_raises(ValueError, raw.set_channel_types, ch_types)
def test_set_channel_types(): """Test set_channel_types.""" raw = read_raw_fif(raw_fname) # Error Tests # Test channel name exists in ch_names mapping = {'EEG 160': 'EEG060'} pytest.raises(ValueError, raw.set_channel_types, mapping) # Test change to illegal channel type mapping = {'EOG 061': 'xxx'} pytest.raises(ValueError, raw.set_channel_types, mapping) # Test changing type if in proj (avg eeg ref here) mapping = {'EEG 058': 'ecog', 'EEG 059': 'ecg', 'EEG 060': 'eog', 'EOG 061': 'seeg', 'MEG 2441': 'eeg', 'MEG 2443': 'eeg', 'MEG 2442': 'hbo'} pytest.raises(RuntimeError, raw.set_channel_types, mapping) # Test type change raw2 = read_raw_fif(raw_fname) raw2.info['bads'] = ['EEG 059', 'EEG 060', 'EOG 061'] pytest.raises(RuntimeError, raw2.set_channel_types, mapping) # has prj raw2.add_proj([], remove_existing=True) with pytest.warns(RuntimeWarning, match='The unit for channel'): raw2.set_channel_types(mapping) info = raw2.info assert info['chs'][372]['ch_name'] == 'EEG 058' assert info['chs'][372]['kind'] == FIFF.FIFFV_ECOG_CH assert info['chs'][372]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][372]['coil_type'] == FIFF.FIFFV_COIL_EEG assert info['chs'][373]['ch_name'] == 'EEG 059' assert info['chs'][373]['kind'] == FIFF.FIFFV_ECG_CH assert info['chs'][373]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][373]['coil_type'] == FIFF.FIFFV_COIL_NONE assert info['chs'][374]['ch_name'] == 'EEG 060' assert info['chs'][374]['kind'] == FIFF.FIFFV_EOG_CH assert info['chs'][374]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][374]['coil_type'] == FIFF.FIFFV_COIL_NONE assert info['chs'][375]['ch_name'] == 'EOG 061' assert info['chs'][375]['kind'] == FIFF.FIFFV_SEEG_CH assert info['chs'][375]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][375]['coil_type'] == FIFF.FIFFV_COIL_EEG for idx in pick_channels(raw.ch_names, ['MEG 2441', 'MEG 2443']): assert info['chs'][idx]['kind'] == FIFF.FIFFV_EEG_CH assert info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_EEG idx = pick_channels(raw.ch_names, ['MEG 2442'])[0] assert info['chs'][idx]['kind'] == FIFF.FIFFV_FNIRS_CH assert info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_MOL assert info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_FNIRS_HBO # Test meaningful error when setting channel type with unknown unit raw.info['chs'][0]['unit'] = 0. ch_types = {raw.ch_names[0]: 'misc'} pytest.raises(ValueError, raw.set_channel_types, ch_types)
def test_picks_by_channels(): """Test creating pick_lists.""" rng = np.random.RandomState(909) test_data = rng.random_sample((4, 2000)) ch_names = ['MEG %03d' % i for i in [1, 2, 3, 4]] ch_types = ['grad', 'mag', 'mag', 'eeg'] sfreq = 250.0 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) _assert_channel_types(info) raw = RawArray(test_data, info) pick_list = _picks_by_type(raw.info) assert_equal(len(pick_list), 3) assert_equal(pick_list[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=False) assert_equal(len(pick_list), len(pick_list2)) assert_equal(pick_list2[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=True) assert_equal(len(pick_list), len(pick_list2) + 1) assert_equal(pick_list2[0][0], 'meg') test_data = rng.random_sample((4, 2000)) ch_names = ['MEG %03d' % i for i in [1, 2, 3, 4]] ch_types = ['mag', 'mag', 'mag', 'mag'] sfreq = 250.0 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) raw = RawArray(test_data, info) # This acts as a set, not an order assert_array_equal(pick_channels(info['ch_names'], ['MEG 002', 'MEG 001']), [0, 1]) # Make sure checks for list input work. pytest.raises(ValueError, pick_channels, ch_names, 'MEG 001') pytest.raises(ValueError, pick_channels, ch_names, ['MEG 001'], 'hi') pick_list = _picks_by_type(raw.info) assert_equal(len(pick_list), 1) assert_equal(pick_list[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=True) assert_equal(len(pick_list), len(pick_list2)) assert_equal(pick_list2[0][0], 'mag') # pick_types type check pytest.raises(ValueError, raw.pick_types, eeg='string') # duplicate check names = ['MEG 002', 'MEG 002'] assert len(pick_channels(raw.info['ch_names'], names)) == 1 assert len(raw.copy().pick_channels(names)[0][0]) == 1
def test_cov_estimation_on_raw(): """Test estimation from raw (typically empty room)""" tempdir = _TempDir() raw = Raw(raw_fname, preload=False) cov_mne = read_cov(erm_cov_fname) cov = compute_raw_covariance(raw, tstep=None) assert_equal(cov.ch_names, cov_mne.ch_names) assert_equal(cov.nfree, cov_mne.nfree) assert_snr(cov.data, cov_mne.data, 1e4) cov = compute_raw_covariance(raw) # tstep=0.2 (default) assert_equal(cov.nfree, cov_mne.nfree - 119) # cutoff some samples assert_snr(cov.data, cov_mne.data, 1e2) # test IO when computation done in Python cov.save(op.join(tempdir, 'test-cov.fif')) # test saving cov_read = read_cov(op.join(tempdir, 'test-cov.fif')) assert_true(cov_read.ch_names == cov.ch_names) assert_true(cov_read.nfree == cov.nfree) assert_array_almost_equal(cov.data, cov_read.data) # test with a subset of channels picks = pick_channels(raw.ch_names, include=raw.ch_names[:5]) cov = compute_raw_covariance(raw, picks=picks, tstep=None) assert_true(cov_mne.ch_names[:5] == cov.ch_names) assert_snr(cov.data, cov_mne.data[picks][:, picks], 1e4) cov = compute_raw_covariance(raw, picks=picks) assert_snr(cov.data, cov_mne.data[picks][:, picks], 90) # cutoff samps # make sure we get a warning with too short a segment raw_2 = raw.crop(0, 1) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') cov = compute_raw_covariance(raw_2) assert_true(any('Too few samples' in str(ww.message) for ww in w))
def test_cov_estimation_on_raw_segment(): """Test estimation from raw on continuous recordings (typically empty room) """ tempdir = _TempDir() raw = Raw(raw_fname, preload=False) cov = compute_raw_data_covariance(raw) cov_mne = read_cov(erm_cov_fname) assert_true(cov_mne.ch_names == cov.ch_names) assert_true(linalg.norm(cov.data - cov_mne.data, ord='fro') / linalg.norm(cov.data, ord='fro') < 1e-4) # test IO when computation done in Python cov.save(op.join(tempdir, 'test-cov.fif')) # test saving cov_read = read_cov(op.join(tempdir, 'test-cov.fif')) assert_true(cov_read.ch_names == cov.ch_names) assert_true(cov_read.nfree == cov.nfree) assert_array_almost_equal(cov.data, cov_read.data) # test with a subset of channels picks = pick_channels(raw.ch_names, include=raw.ch_names[:5]) cov = compute_raw_data_covariance(raw, picks=picks) assert_true(cov_mne.ch_names[:5] == cov.ch_names) assert_true(linalg.norm(cov.data - cov_mne.data[picks][:, picks], ord='fro') / linalg.norm(cov.data, ord='fro') < 1e-4) # make sure we get a warning with too short a segment raw_2 = raw.crop(0, 1) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') cov = compute_raw_data_covariance(raw_2) assert_true(len(w) == 1)
def test_interpolate_meg_ctf(): """Test interpolation of MEG channels from CTF system.""" thresh = .7 tol = .05 # assert the new interpol correlates at least .05 "better" bad = 'MLC22-2622' # select a good channel to test the interpolation raw = io.read_raw_fif(raw_fname_ctf, preload=True) # 3 secs raw.apply_gradient_compensation(3) # Show that we have to exclude ref_meg for interpolating CTF MEG-channels # (fixed in #5965): raw.info['bads'] = [bad] pick_bad = pick_channels(raw.info['ch_names'], raw.info['bads']) data_orig = raw[pick_bad, :][0] # mimic old behavior (the ref_meg-arg in _interpolate_bads_meg only serves # this purpose): data_interp_refmeg = _this_interpol(raw, ref_meg=True)[pick_bad, :][0] # new: data_interp_no_refmeg = _this_interpol(raw, ref_meg=False)[pick_bad, :][0] R = dict() R['no_refmeg'] = np.corrcoef(data_orig, data_interp_no_refmeg)[0, 1] R['with_refmeg'] = np.corrcoef(data_orig, data_interp_refmeg)[0, 1] print('Corrcoef of interpolated with original channel: ', R) assert R['no_refmeg'] > R['with_refmeg'] + tol assert R['no_refmeg'] > thresh
def apply_ica_hcp(raw, ica_mat, exclude): """ Apply the HCP ICA. Operates in place. Parameters ---------- raw : instance of Raw the hcp raw data. ica_mat : numpy structured array The hcp ICA solution exclude : array-like the components to be excluded. """ ch_names = ica_mat['topolabel'].tolist().tolist() picks = mne.pick_channels(raw.info['ch_names'], include=ch_names) assert ch_names == [raw.ch_names[p] for p in picks] unmixing_matrix = np.array(ica_mat['unmixing'].tolist()) n_components, n_channels = unmixing_matrix.shape mixing = np.array(ica_mat['topo'].tolist()) proj_mat = (np.eye(n_channels) - np.dot( mixing[:, exclude], unmixing_matrix[exclude])) raw._data *= 1e15 raw._data[picks] = np.dot(proj_mat, raw._data[picks]) raw._data /= 1e15
def _check_channel_names(inst, ref_names): if isinstance(ref_names, str): ref_names = [ref_names] # Test that the names of the reference channels are present in `ch_names` ref_idx = pick_channels(inst.info['ch_names'], ref_names) assert_true(len(ref_idx), len(ref_names)) # Test that the names of the reference channels are present in the `chs` # list inst.info._check_consistency() # Should raise no exceptions
def test_comparision_with_c(): """Test of average obtained vs C code """ c_evoked = read_evokeds(evoked_nf_name, condition=0) epochs = Epochs(raw, events, event_id, tmin, tmax, baseline=None, preload=True, reject=None, flat=None) evoked = epochs.average() sel = pick_channels(c_evoked.ch_names, evoked.ch_names) evoked_data = evoked.data c_evoked_data = c_evoked.data[sel] assert_true(evoked.nave == c_evoked.nave) assert_array_almost_equal(evoked_data, c_evoked_data, 10) assert_array_almost_equal(evoked.times, c_evoked.times, 12)
def test_cov_estimation_on_raw(): """Test estimation from raw (typically empty room)""" tempdir = _TempDir() raw = read_raw_fif(raw_fname, preload=True) cov_mne = read_cov(erm_cov_fname) # The pure-string uses the more efficient numpy-based method, the # the list gets triaged to compute_covariance (should be equivalent # but use more memory) for method in (None, ['empirical']): # None is cast to 'empirical' cov = compute_raw_covariance(raw, tstep=None, method=method) assert_equal(cov.ch_names, cov_mne.ch_names) assert_equal(cov.nfree, cov_mne.nfree) assert_snr(cov.data, cov_mne.data, 1e4) cov = compute_raw_covariance(raw, method=method) # tstep=0.2 (default) assert_equal(cov.nfree, cov_mne.nfree - 119) # cutoff some samples assert_snr(cov.data, cov_mne.data, 1e2) # test IO when computation done in Python cov.save(op.join(tempdir, 'test-cov.fif')) # test saving cov_read = read_cov(op.join(tempdir, 'test-cov.fif')) assert_true(cov_read.ch_names == cov.ch_names) assert_true(cov_read.nfree == cov.nfree) assert_array_almost_equal(cov.data, cov_read.data) # test with a subset of channels picks = pick_channels(raw.ch_names, include=raw.ch_names[:5]) raw_pick = raw.copy().pick_channels( [raw.ch_names[pick] for pick in picks]) raw_pick.info.normalize_proj() cov = compute_raw_covariance(raw_pick, picks=picks, tstep=None, method=method) assert_true(cov_mne.ch_names[:5] == cov.ch_names) assert_snr(cov.data, cov_mne.data[picks][:, picks], 1e4) cov = compute_raw_covariance(raw_pick, picks=picks, method=method) assert_snr(cov.data, cov_mne.data[picks][:, picks], 90) # cutoff samps # make sure we get a warning with too short a segment raw_2 = read_raw_fif(raw_fname).crop(0, 1, copy=False) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') cov = compute_raw_covariance(raw_2, method=method) assert_true(any('Too few samples' in str(ww.message) for ww in w)) # no epochs found due to rejection assert_raises(ValueError, compute_raw_covariance, raw, tstep=None, method='empirical', reject=dict(eog=200e-6)) # but this should work cov = compute_raw_covariance(raw.copy().crop(0, 10., copy=False), tstep=None, method=method, reject=dict(eog=1000e-6))
def test_interpolation_meg(): """Test interpolation of MEG channels.""" # speed accuracy tradeoff: channel subselection is faster but the # correlation drops thresh = 0.7 raw, epochs_meg = _load_data('meg') # check that interpolation works when non M/EEG channels are present # before MEG channels raw.crop(0, 0.1).load_data().pick_channels(epochs_meg.ch_names) raw.info.normalize_proj() with pytest.warns(RuntimeWarning, match='unit .* changed from .* to .*'): raw.set_channel_types({raw.ch_names[0]: 'stim'}) raw.info['bads'] = [raw.ch_names[1]] raw.load_data() raw.interpolate_bads(mode='fast') del raw # check that interpolation works for MEG epochs_meg.info['bads'] = ['MEG 0141'] evoked = epochs_meg.average() pick = pick_channels(epochs_meg.info['ch_names'], epochs_meg.info['bads']) # MEG -- raw raw_meg = io.RawArray(data=epochs_meg._data[0], info=epochs_meg.info) raw_meg.info['bads'] = ['MEG 0141'] data1 = raw_meg[pick, :][0][0] raw_meg.info.normalize_proj() data2 = raw_meg.interpolate_bads(reset_bads=False, mode='fast')[pick, :][0][0] assert np.corrcoef(data1, data2)[0, 1] > thresh # the same number of bads as before assert len(raw_meg.info['bads']) == len(raw_meg.info['bads']) # MEG -- epochs data1 = epochs_meg.get_data()[:, pick, :].ravel() epochs_meg.info.normalize_proj() epochs_meg.interpolate_bads(mode='fast') data2 = epochs_meg.get_data()[:, pick, :].ravel() assert np.corrcoef(data1, data2)[0, 1] > thresh assert len(epochs_meg.info['bads']) == 0 # MEG -- evoked (plus auto origin) data1 = evoked.data[pick] evoked.info.normalize_proj() data2 = evoked.interpolate_bads(origin='auto').data[pick] assert np.corrcoef(data1, data2)[0, 1] > thresh
def clean_by_interp(inst, picks=None, dots=None, verbose='progressbar'): """Clean epochs/evoked by LOOCV. Parameters ---------- inst : instance of mne.Evoked or mne.Epochs The evoked or epochs object. picks : ndarray, shape(n_channels,) | None The channels to be considered for autoreject. If None, defaults to data channels {'meg', 'eeg'}. dots : tuple of ndarray The self dots and cross dots. verbose : 'tqdm', 'tqdm_notebook', 'progressbar' or False The verbosity of progress messages. If `'progressbar'`, use `mne.utils.ProgressBar`. If `'tqdm'`, use `tqdm.tqdm`. If `'tqdm_notebook'`, use `tqdm.tqdm_notebook`. If False, suppress all output messages. Returns ------- inst_clean : instance of mne.Evoked or mne.Epochs Instance after interpolation of bad channels. """ inst_interp = inst.copy() mesg = 'Creating augmented epochs' picks = _handle_picks(info=inst_interp.info, picks=picks) BaseEpochs = _get_epochs_type() ch_names = [inst.info['ch_names'][p] for p in picks] for ch_idx, (pick, ch) in enumerate(_pbar(list(zip(picks, ch_names)), desc=mesg, verbose=verbose)): inst.info['bads'] = [ch] pick_interp = mne.pick_channels(inst.info['ch_names'], [ch])[0] data_orig = inst._data[:, pick_interp].copy() interpolate_bads(inst, picks=picks, dots=dots, reset_bads=True, mode='fast') if isinstance(inst, mne.Evoked): inst_interp.data[pick] = inst.data[pick_interp] elif isinstance(inst, BaseEpochs): inst_interp._data[:, pick] = inst._data[:, pick_interp] else: raise ValueError('Unrecognized type for inst') inst._data[:, pick_interp] = data_orig.copy() return inst_interp
def test_set_channel_types(): """Test set_channel_types """ raw = Raw(raw_fname) # Error Tests # Test channel name exists in ch_names mapping = {'EEG 160': 'EEG060'} assert_raises(ValueError, raw.set_channel_types, mapping) # Test change to illegal channel type mapping = {'EOG 061': 'xxx'} assert_raises(ValueError, raw.set_channel_types, mapping) # Test changing type if in proj (avg eeg ref here) mapping = {'EEG 060': 'eog', 'EEG 059': 'ecg', 'EOG 061': 'seeg', 'MEG 2441': 'eeg', 'MEG 2443': 'eeg'} assert_raises(RuntimeError, raw.set_channel_types, mapping) # Test type change raw2 = Raw(raw_fname, add_eeg_ref=False) raw2.info['bads'] = ['EEG 059', 'EEG 060', 'EOG 061'] assert_raises(RuntimeError, raw2.set_channel_types, mapping) # has proj raw2.add_proj([], remove_existing=True) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') raw2.set_channel_types(mapping) assert_true(len(w) >= 1, msg=[str(ww.message) for ww in w]) assert_true(all('The unit for channel' in str(ww.message) for ww in w)) info = raw2.info assert_true(info['chs'][374]['ch_name'] == 'EEG 060') assert_true(info['chs'][374]['kind'] == FIFF.FIFFV_EOG_CH) assert_true(info['chs'][374]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][374]['coil_type'] == FIFF.FIFFV_COIL_NONE) assert_true(info['chs'][373]['ch_name'] == 'EEG 059') assert_true(info['chs'][373]['kind'] == FIFF.FIFFV_ECG_CH) assert_true(info['chs'][373]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][373]['coil_type'] == FIFF.FIFFV_COIL_NONE) assert_true(info['chs'][375]['ch_name'] == 'EOG 061') assert_true(info['chs'][375]['kind'] == FIFF.FIFFV_SEEG_CH) assert_true(info['chs'][375]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][375]['coil_type'] == FIFF.FIFFV_COIL_EEG) for idx in pick_channels(raw.ch_names, ['MEG 2441', 'MEG 2443']): assert_true(info['chs'][idx]['kind'] == FIFF.FIFFV_EEG_CH) assert_true(info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_EEG)
def test_viz(): """Test viz.""" import matplotlib.pyplot as plt events = mne.find_events(raw) picks = mne.pick_channels(raw.info['ch_names'], ['MEG 2443', 'MEG 2442', 'MEG 2441']) epochs = mne.Epochs(raw, events, picks=picks, baseline=(None, 0), reject=None, preload=True, event_id={'1': 1, '2': 2, '3': 3, '4': 4}) bad_epochs_idx = [0, 1, 3] n_epochs, n_channels, _ = epochs.get_data().shape bad_epochs = np.zeros(n_epochs, dtype=bool) bad_epochs[bad_epochs_idx] = True labels = np.zeros((n_epochs, n_channels)) reject_log = autoreject.RejectLog(bad_epochs, labels, epochs.ch_names) reject_log.plot_epochs(epochs) assert_raises(ValueError, reject_log.plot_epochs, epochs[:2]) plt.close('all')
def test_viz(): """Test viz.""" import matplotlib.pyplot as plt events = mne.find_events(raw) picks = mne.pick_channels(raw.info['ch_names'], ['MEG 2443', 'MEG 2442', 'MEG 2441']) epochs = mne.Epochs(raw, events, picks=picks, baseline=(None, 0), reject=None, preload=True, event_id={'1': 1, '2': 2, '3': 3, '4': 4}) bad_epochs_idx = [0, 1, 3] n_epochs, n_channels, _ = epochs.get_data().shape fix_log = np.zeros((n_epochs, n_channels)) print(bad_epochs_idx) plot_epochs(epochs, bad_epochs_idx=bad_epochs_idx, fix_log=fix_log) plot_epochs(epochs, bad_epochs_idx=bad_epochs_idx) plot_epochs(epochs, fix_log=fix_log) assert_raises(ValueError, plot_epochs, epochs[:2], bad_epochs_idx=bad_epochs_idx, fix_log=fix_log) plt.close('all')
def _interpolate_bads_meg_fast(inst, mode='accurate', verbose=None): """Interpolate bad channels from data in good channels. """ from mne.channels.interpolation import _do_interp_dots from mne import pick_types, pick_channels picks_meg = pick_types(inst.info, meg=True, eeg=False, exclude=[]) ch_names = [inst.info['ch_names'][p] for p in picks_meg] picks_good = pick_types(inst.info, meg=True, eeg=False, exclude='bads') # select the bad meg channel to be interpolated if len(inst.info['bads']) == 0: picks_bad = [] else: picks_bad = pick_channels(ch_names, inst.info['bads'], exclude=[]) # return without doing anything if there are no meg channels if len(picks_meg) == 0 or len(picks_bad) == 0: return mapping = _fast_map_meg_channels(inst, picks_good, picks_bad, mode=mode) _do_interp_dots(inst, mapping, picks_good, picks_bad)
def find_bad_by_ransac(self, n_samples=50, fraction_good=0.25, corr_thresh=0.75, fraction_bad=0.4, corr_window_secs=4.): """Detect channels that are not predicted well by other channels. Here, a ransac approach (see [1], and a short discussion in [2]) is adopted to predict a "clean EEG" dataset. After identifying clean EEG channels through the other methods, the clean EEG dataset is constructed by repeatedly sampling a small subset of clean EEG channels and interpolation the complete data. The median of all those repetitions forms the clean EEG dataset. In a second step, the original and the ransac predicted data are correlated and channels, which do not correlate well with themselves across the two datasets are considered `bad_by_ransac`. Parameters ---------- n_samples : int Number of samples used for computation of ransac. fraction_good : float Fraction of channels used for robust reconstruction of the signal. This needs to be in the range [0, 1], where obviously neither 0 nor 1 would make sense. corr_thresh : float The minimum correlation threshold that should be attained within a data window. fraction_bad : float If this percentage of all data windows in which the correlation threshold was not surpassed is exceeded, classify a channel as `bad_by_ransac`. corr_window_secs : float Size of the correlation window in seconds. References ---------- .. [1] Fischler, M.A., Bolles, R.C. (1981). Random rample consensus: A Paradigm for Model Fitting with Applications to Image Analysis and Automated Cartography. Communications of the ACM, 24, 381-395 .. [2] Jas, M., Engemann, D.A., Bekhti, Y., Raimondo, F., Gramfort, A. (2017). Autoreject: Automated Artifact Rejection for MEG and EEG Data. NeuroImage, 159, 417-429 """ # First, identify all bad channels by other means: self.find_all_bads(ransac=False) bads = self.get_bads() # Get all channel positions and the position subset of "clean channels" good_idx = mne.pick_channels(self.ch_names, include=[], exclude=bads) good_chn_labs = self.ch_names[good_idx] n_chans_good = good_idx.shape[0] chn_pos_good = self.chn_pos[good_idx, :] # Check if we have enough remaning channels # after exclusion of bad channels n_pred_chns = int(np.ceil(fraction_good * n_chans_good)) if n_pred_chns <= 3: raise IOError('Too few channels available to reliably perform' ' ransac. Perhaps, too many channels have failed' ' quality tests. You could call `.find_all_bads`' ' with the ransac=False option.') # Make the ransac predictions ransac_eeg = self._run_ransac(chn_pos=self.chn_pos, chn_pos_good=chn_pos_good, good_chn_labs=good_chn_labs, n_pred_chns=n_pred_chns, data=self.x_bp, n_samples=n_samples) # Correlate ransac prediction and eeg data correlation_frames = corr_window_secs * self.sfreq correlation_window = np.arange(correlation_frames) n = correlation_window.shape[0] correlation_offsets = np.arange(0, (self.signal_len - correlation_frames), correlation_frames) w_correlation = correlation_offsets.shape[0] # For the actual data data_window = self.x_bp[:self.n_chans, :n * w_correlation] data_window = data_window.reshape(self.n_chans, n, w_correlation) # For the ransac predicted eeg pred_window = ransac_eeg[:self.n_chans, :n * w_correlation] pred_window = pred_window.reshape(self.n_chans, n, w_correlation) # Preallocate channel_correlations = np.ones((w_correlation, self.n_chans)) # Perform correlations for k in range(w_correlation): data_portion = data_window[:, :, k] pred_portion = pred_window[:, :, k] R = np.corrcoef(data_portion, pred_portion) # Take only correlations of data with pred # and use diag to exctract correlation of # data_i with pred_i R = np.diag(R[0:self.n_chans, self.n_chans:]) channel_correlations[k, :] = R # Thresholding thresholded_correlations = channel_correlations < corr_thresh frac_bad_corr_windows = np.mean(thresholded_correlations, axis=0) # find the corresponding channel names and return bad_idxs_bool = frac_bad_corr_windows > fraction_bad bad_idxs = np.argwhere(bad_idxs_bool) bads = self.ch_names[bad_idxs.astype(int)] bads = [i[0] for i in bads] bads.sort() self.bad_by_ransac = bads self._ransac_channel_correlations = channel_correlations return None
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() ############################################################################### # Set parameters raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' # Setup for reading the raw data raw = io.read_raw_fif(raw_fname, preload=True) events = mne.find_events(raw, 'STI 014') eog_event_id = 512 eog_events = mne.preprocessing.find_eog_events(raw, eog_event_id) raw.add_events(eog_events, 'STI 014') # Read epochs picks = mne.pick_types(raw.info, meg=False, eeg=False, stim=True, eog=False) tmin, tmax = -0.2, 0.5 event_ids = {'AudL': 1, 'AudR': 2, 'VisL': 3, 'VisR': 4} epochs = mne.Epochs(raw, events, event_ids, tmin, tmax, picks=picks) # Get the stim channel data pick_ch = mne.pick_channels(epochs.ch_names, ['STI 014'])[0] data = epochs.get_data()[:, pick_ch, :].astype(int) data = np.sum((data.astype(int) & 512) == 512, axis=0) ############################################################################### # Plot EOG artifact distribution plt.stem(1e3 * epochs.times, data) plt.xlabel('Times (ms)') plt.ylabel('Blink counts (from %s trials)' % len(epochs)) plt.show()
# We create an :class:`mne.Epochs` object containing two trials: one with # both noise and signal and one with just noise t0 = raw.first_samp # First sample in the data t1 = t0 + n_times - 1 # Sample just before the second trial epochs = mne.Epochs( raw, events=np.array([[t0, 0, 1], [t1, 0, 2]]), event_id=dict(signal=1, noise=2), tmin=0, tmax=10, preload=True, ) # Plot some of the channels of the simulated data that are situated above one # of our simulated sources. picks = mne.pick_channels(epochs.ch_names, mne.read_selection('Left-frontal')) epochs.plot(picks=picks) ############################################################################### # Power mapping # ------------- # With our simulated dataset ready, we can now pretend to be researchers that # have just recorded this from a real subject and are going to study what parts # of the brain communicate with each other. # # First, we'll create a source estimate of the MEG data. We'll use both a # straightforward MNE-dSPM inverse solution for this, and the DICS beamformer # which is specifically designed to work with oscillatory data. ############################################################################### # Computing the inverse using MNE-dSPM:
def test_interpolation(): """Test interpolation""" raw, epochs, epochs_eeg, epochs_meg = _load_data() # It's a trade of between speed and accuracy. If every second channel is # selected the tests are more than 3x faster but the correlation # drops to 0.8 thresh = 0.80 # create good and bad channels for EEG epochs_eeg.info['bads'] = [] goods_idx = np.ones(len(epochs_eeg.ch_names), dtype=bool) goods_idx[epochs_eeg.ch_names.index('EEG 012')] = False bads_idx = ~goods_idx evoked_eeg = epochs_eeg.average() ave_before = evoked_eeg.data[bads_idx] # interpolate bad channels for EEG pos = epochs_eeg._get_channel_positions() pos_good = pos[goods_idx] pos_bad = pos[bads_idx] interpolation = _make_interpolation_matrix(pos_good, pos_bad) assert_equal(interpolation.shape, (1, len(epochs_eeg.ch_names) - 1)) ave_after = np.dot(interpolation, evoked_eeg.data[goods_idx]) epochs_eeg.info['bads'] = ['EEG 012'] evoked_eeg = epochs_eeg.average() assert_array_equal(ave_after, evoked_eeg.interpolate_bads().data[bads_idx]) assert_allclose(ave_before, ave_after, atol=2e-6) # check that interpolation fails when preload is False epochs_eeg.preload = False assert_raises(ValueError, epochs_eeg.interpolate_bads) epochs_eeg.preload = True # check that interpolation changes the data in raw raw_eeg = io.RawArray(data=epochs_eeg._data[0], info=epochs_eeg.info) raw_before = raw_eeg._data[bads_idx] raw_after = raw_eeg.interpolate_bads()._data[bads_idx] assert_equal(np.all(raw_before == raw_after), False) # check that interpolation fails when preload is False for inst in [raw, epochs]: assert hasattr(inst, 'preload') inst.preload = False inst.info['bads'] = [inst.ch_names[1]] assert_raises(ValueError, inst.interpolate_bads) # check that interpolation works with few channels raw_few = raw.copy().crop(0, 0.1).load_data() raw_few.pick_channels(raw_few.ch_names[:1] + raw_few.ch_names[3:4]) assert_equal(len(raw_few.ch_names), 2) raw_few.del_proj() raw_few.info['bads'] = [raw_few.ch_names[-1]] orig_data = raw_few[1][0] raw_few.interpolate_bads(reset_bads=False) new_data = raw_few[1][0] assert_true((new_data == 0).mean() < 0.5) assert_true(np.corrcoef(new_data, orig_data)[0, 1] > 0.1) # check that interpolation works when non M/EEG channels are present # before MEG channels with warnings.catch_warnings(record=True): # change of units raw.rename_channels({'MEG 0113': 'TRIGGER'}) raw.set_channel_types({'TRIGGER': 'stim'}) raw.info['bads'] = [raw.info['ch_names'][1]] raw.load_data() raw.interpolate_bads() # check that interpolation works for MEG epochs_meg.info['bads'] = ['MEG 0141'] evoked = epochs_meg.average() pick = pick_channels(epochs_meg.info['ch_names'], epochs_meg.info['bads']) # MEG -- raw raw_meg = io.RawArray(data=epochs_meg._data[0], info=epochs_meg.info) raw_meg.info['bads'] = ['MEG 0141'] data1 = raw_meg[pick, :][0][0] raw_meg.info.normalize_proj() data2 = raw_meg.interpolate_bads(reset_bads=False)[pick, :][0][0] assert_true(np.corrcoef(data1, data2)[0, 1] > thresh) # the same number of bads as before assert_true(len(raw_meg.info['bads']) == len(raw_meg.info['bads'])) # MEG -- epochs data1 = epochs_meg.get_data()[:, pick, :].ravel() epochs_meg.info.normalize_proj() epochs_meg.interpolate_bads() data2 = epochs_meg.get_data()[:, pick, :].ravel() assert_true(np.corrcoef(data1, data2)[0, 1] > thresh) assert_true(len(epochs_meg.info['bads']) == 0) # MEG -- evoked data1 = evoked.data[pick] evoked.info.normalize_proj() data2 = evoked.interpolate_bads().data[pick] assert_true(np.corrcoef(data1, data2)[0, 1] > thresh)
fname=op.join(MEG_data_path,subject,task+'_%d'%(day+subject_id)+'_tsss_mc.fif') raw_tsss_mc=mne.io.read_raw_fif(fname,preload=True) if op.isfile(fname.replace("tsss_mc", "annot")): raw_tsss_mc.set_annotations(mne.read_annotations(fname.replace("tsss_mc", "annot"))) print("Annotion loaded!") raw_tsss_mc.filter(l_freq=None,h_freq=40.0,fir_design='firwin',n_jobs=-1) # band-pass filter data df=pd.read_csv(fname.replace('tsss_mc.fif','events.csv')) events = df[["stim_onset", "button_press", "trigger_code"]].astype(int).values epochs=mne.Epochs(raw_tsss_mc, events=events, tmin=-0.2, tmax=1,preload=True,baseline=None) ica=read_ica(op.join(MEG_data_path,subject,task+'_%d'%(day+subject_id)+'-ica.fif')) ica.exclude = np.load(fname.replace("tsss_mc.fif",'ICA_excludes.npy')).tolist() ica.apply(epochs,exclude=ica.exclude) picks=mne.pick_channels(epochs.ch_names,[epochs.ch_names[0]]+epochs.ch_names[1::9]+epochs.ch_names[5::9]+epochs.ch_names[6::9]) epochs.drop_bad(reject=dict(grad=1500e-13, mag=5e-12)) #manually remove bad trials after ICA epochs.plot(n_channels=103,n_epochs=40,block = True,picks=picks, scalings=dict(mag=8e-12, grad=20e-11, eog=400e-5)) epochs.plot_drop_log(subject=subject).savefig(fname.replace("tsss_mc.fif",'drop_bads_Manual.png')) df['artifacts']=~df['stim_onset'].isin(epochs.events[:,0]) df.to_csv(fname.replace('tsss_mc.fif','events_with_artifacts_marked.csv'),index=False)
def test_find_events(): """Test find_events in rt_epochs.""" raw = read_raw_fif(raw_fname, preload=True, verbose=False) picks = pick_types(raw.info, meg='grad', eeg=False, eog=True, stim=True, exclude=raw.info['bads']) event_id = [0, 5, 6] tmin, tmax = -0.2, 0.5 stim_channel = 'STI 014' stim_channel_idx = pick_channels(raw.info['ch_names'], include=[stim_channel]) # Reset some data for ease of comparison raw._first_samps[0] = 0 raw.info['sfreq'] = 1000 # Test that we can handle consecutive events with no gap raw._data[stim_channel_idx, :] = 0 raw._data[stim_channel_idx, 500:520] = 5 raw._data[stim_channel_idx, 520:530] = 6 raw._data[stim_channel_idx, 530:532] = 5 raw._data[stim_channel_idx, 540] = 6 raw._update_times() # consecutive=False find_events = dict(consecutive=False) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, stim_channel='STI 014', isi_max=0.5, find_events=find_events) rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_epochs.start() # make sure next() works even if no iter-method has been called before rt_epochs.next() events = [5, 6] for ii, ev in enumerate(rt_epochs.iter_evoked()): assert ev.comment == str(events[ii]) assert ii == 1 # consecutive=True find_events = dict(consecutive=True) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, stim_channel='STI 014', isi_max=0.5, find_events=find_events) rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_epochs.start() events = [5, 6, 5, 6] for ii, ev in enumerate(rt_epochs.iter_evoked()): assert ev.comment == str(events[ii]) assert ii == 3 # min_duration=0.002 find_events = dict(consecutive=False, min_duration=0.002) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, stim_channel='STI 014', isi_max=0.5, find_events=find_events) rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_epochs.start() events = [5] for ii, ev in enumerate(rt_epochs.iter_evoked()): assert ev.comment == str(events[ii]) assert ii == 0 # output='step', consecutive=True find_events = dict(output='step', consecutive=True) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, stim_channel='STI 014', isi_max=0.5, find_events=find_events) rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_epochs.start() events = [5, 6, 5, 0, 6, 0] for ii, ev in enumerate(rt_epochs.iter_evoked()): assert ev.comment == str(events[ii]) assert ii == 5 # Reset some data for ease of comparison raw._first_samps[0] = 0 raw.info['sfreq'] = 1000 # Test that we can handle events at the beginning of the buffer raw._data[stim_channel_idx, :] = 0 raw._data[stim_channel_idx, 1000:1005] = 5 raw._update_times() # Check that we find events that start at the beginning of the buffer find_events = dict(consecutive=False) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, stim_channel='STI 014', isi_max=0.5, find_events=find_events) rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_epochs.start() events = [5] for ii, ev in enumerate(rt_epochs.iter_evoked()): assert ev.comment == str(events[ii]) assert ii == 0 # Reset some data for ease of comparison raw._first_samps[0] = 0 raw.info['sfreq'] = 1000 # Test that we can handle events over different buffers raw._data[stim_channel_idx, :] = 0 raw._data[stim_channel_idx, 997:1003] = 5 raw._update_times() for min_dur in [0.002, 0.004]: find_events = dict(consecutive=False, min_duration=min_dur) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, stim_channel='STI 014', isi_max=0.5, find_events=find_events) rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_epochs.start() events = [5] for ii, ev in enumerate(rt_epochs.iter_evoked()): assert ev.comment == str(events[ii]) assert ii == 0
############################################################################### # List all information about the first data channel print(info['chs'][0]) ############################################################################### # .. _picking_channels: # # Obtaining subsets of channels # ----------------------------- # # There are a number of convenience functions to obtain channel indices, given # an :class:`mne.Info` object. ############################################################################### # Get channel indices by name channel_indices = mne.pick_channels(info['ch_names'], ['MEG 0312', 'EEG 005']) ############################################################################### # Get channel indices by regular expression channel_indices = mne.pick_channels_regexp(info['ch_names'], 'MEG *') ############################################################################### # Channel types # ------------- # # MNE supports different channel types: # # - eeg : For EEG channels with data stored in Volts (V) # - meg (mag) : For MEG magnetometers channels stored in Tesla (T) # - meg (grad) : For MEG gradiometers channels stored in Tesla/Meter (T/m) # - ecg : For ECG channels stored in Volts (V)
def test_find_events(): """Test find events in raw file.""" events = read_events(fname) raw = read_raw_fif(raw_fname, preload=True) # let's test the defaulting behavior while we're at it extra_ends = ['', '_1'] orig_envs = [os.getenv('MNE_STIM_CHANNEL%s' % s) for s in extra_ends] os.environ['MNE_STIM_CHANNEL'] = 'STI 014' if 'MNE_STIM_CHANNEL_1' in os.environ: del os.environ['MNE_STIM_CHANNEL_1'] events2 = find_events(raw) assert_array_almost_equal(events, events2) # now test with mask events11 = find_events(raw, mask=3, mask_type='not_and') with pytest.warns(RuntimeWarning, match='events masked'): events22 = read_events(fname, mask=3, mask_type='not_and') assert_array_equal(events11, events22) # Reset some data for ease of comparison raw._first_samps[0] = 0 raw.info['sfreq'] = 1000 stim_channel = 'STI 014' stim_channel_idx = pick_channels(raw.info['ch_names'], include=[stim_channel]) # test digital masking raw._data[stim_channel_idx, :5] = np.arange(5) raw._data[stim_channel_idx, 5:] = 0 # 1 == '0b1', 2 == '0b10', 3 == '0b11', 4 == '0b100' pytest.raises(TypeError, find_events, raw, mask="0", mask_type='and') pytest.raises(ValueError, find_events, raw, mask=0, mask_type='blah') # testing mask_type. default = 'not_and' assert_array_equal( find_events(raw, shortest_event=1, mask=1, mask_type='not_and'), [[2, 0, 2], [4, 2, 4]]) assert_array_equal( find_events(raw, shortest_event=1, mask=2, mask_type='not_and'), [[1, 0, 1], [3, 0, 1], [4, 1, 4]]) assert_array_equal( find_events(raw, shortest_event=1, mask=3, mask_type='not_and'), [[4, 0, 4]]) assert_array_equal( find_events(raw, shortest_event=1, mask=4, mask_type='not_and'), [[1, 0, 1], [2, 1, 2], [3, 2, 3]]) # testing with mask_type = 'and' assert_array_equal( find_events(raw, shortest_event=1, mask=1, mask_type='and'), [[1, 0, 1], [3, 0, 1]]) assert_array_equal( find_events(raw, shortest_event=1, mask=2, mask_type='and'), [[2, 0, 2]]) assert_array_equal( find_events(raw, shortest_event=1, mask=3, mask_type='and'), [[1, 0, 1], [2, 1, 2], [3, 2, 3]]) assert_array_equal( find_events(raw, shortest_event=1, mask=4, mask_type='and'), [[4, 0, 4]]) # test empty events channel raw._data[stim_channel_idx, :] = 0 assert_array_equal(find_events(raw), np.empty((0, 3), dtype='int32')) raw._data[stim_channel_idx, :4] = 1 assert_array_equal(find_events(raw), np.empty((0, 3), dtype='int32')) raw._data[stim_channel_idx, -1:] = 9 assert_array_equal(find_events(raw), [[14399, 0, 9]]) # Test that we can handle consecutive events with no gap raw._data[stim_channel_idx, 10:20] = 5 raw._data[stim_channel_idx, 20:30] = 6 raw._data[stim_channel_idx, 30:32] = 5 raw._data[stim_channel_idx, 40] = 6 assert_array_equal(find_events(raw, consecutive=False), [[10, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal( find_events(raw, consecutive=True), [[10, 0, 5], [20, 5, 6], [30, 6, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw), [[10, 0, 5], [20, 5, 6], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, output='offset', consecutive=False), [[31, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal( find_events(raw, output='offset', consecutive=True), [[19, 6, 5], [29, 5, 6], [31, 0, 5], [40, 0, 6], [14399, 0, 9]]) pytest.raises(ValueError, find_events, raw, output='step', consecutive=True) assert_array_equal( find_events(raw, output='step', consecutive=True, shortest_event=1), [[10, 0, 5], [20, 5, 6], [30, 6, 5], [32, 5, 0], [40, 0, 6], [41, 6, 0], [14399, 0, 9], [14400, 9, 0]]) assert_array_equal(find_events(raw, output='offset'), [[19, 6, 5], [31, 0, 6], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, consecutive=False, min_duration=0.002), [[10, 0, 5]]) assert_array_equal(find_events(raw, consecutive=True, min_duration=0.002), [[10, 0, 5], [20, 5, 6], [30, 6, 5]]) assert_array_equal( find_events(raw, output='offset', consecutive=False, min_duration=0.002), [[31, 0, 5]]) assert_array_equal( find_events(raw, output='offset', consecutive=True, min_duration=0.002), [[19, 6, 5], [29, 5, 6], [31, 0, 5]]) assert_array_equal(find_events(raw, consecutive=True, min_duration=0.003), [[10, 0, 5], [20, 5, 6]]) # test find_stim_steps merge parameter raw._data[stim_channel_idx, :] = 0 raw._data[stim_channel_idx, 0] = 1 raw._data[stim_channel_idx, 10] = 4 raw._data[stim_channel_idx, 11:20] = 5 assert_array_equal( find_stim_steps(raw, pad_start=0, merge=0, stim_channel=stim_channel), [[0, 0, 1], [1, 1, 0], [10, 0, 4], [11, 4, 5], [20, 5, 0]]) assert_array_equal( find_stim_steps(raw, merge=-1, stim_channel=stim_channel), [[1, 1, 0], [10, 0, 5], [20, 5, 0]]) assert_array_equal( find_stim_steps(raw, merge=1, stim_channel=stim_channel), [[1, 1, 0], [11, 0, 5], [20, 5, 0]]) # put back the env vars we trampled on for s, o in zip(extra_ends, orig_envs): if o is not None: os.environ['MNE_STIM_CHANNEL%s' % s] = o # Test with list of stim channels raw._data[stim_channel_idx, 1:101] = np.zeros(100) raw._data[stim_channel_idx, 10:11] = 1 raw._data[stim_channel_idx, 30:31] = 3 stim_channel2 = 'STI 015' stim_channel2_idx = pick_channels(raw.info['ch_names'], include=[stim_channel2]) raw._data[stim_channel2_idx, :] = 0 raw._data[stim_channel2_idx, :100] = raw._data[stim_channel_idx, 5:105] events1 = find_events(raw, stim_channel='STI 014') events2 = events1.copy() events2[:, 0] -= 5 events = find_events(raw, stim_channel=['STI 014', stim_channel2]) assert_array_equal(events[::2], events2) assert_array_equal(events[1::2], events1) # test initial_event argument info = create_info(['MYSTI'], 1000, 'stim') data = np.zeros((1, 1000)) raw = RawArray(data, info) data[0, :10] = 100 data[0, 30:40] = 200 assert_array_equal(find_events(raw, 'MYSTI'), [[30, 0, 200]]) assert_array_equal(find_events(raw, 'MYSTI', initial_event=True), [[0, 0, 100], [30, 0, 200]]) # test error message for raw without stim channels raw = read_raw_fif(raw_fname, preload=True) raw.pick_types(meg=True, stim=False) # raw does not have annotations with pytest.raises(ValueError, match="'stim_channel'"): find_events(raw) # if raw has annotations, we show a different error message raw.set_annotations(Annotations(0, 2, "test")) with pytest.raises(ValueError, match="mne.events_from_annotations"): find_events(raw)
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)) 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 = np.random.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_true(len(w) > 0) # len(w) should be 2 but Travis sometimes has more
def simulate_movement(raw, pos, stc, trans, src, bem, cov='simple', mindist=1.0, interp='linear', random_state=None, n_jobs=1, verbose=None): """Simulate raw data with head movements Parameters ---------- raw : instance of Raw The raw instance to use. The measurement info, including the head positions, will be used to simulate data. pos : str | dict | None Name of the position estimates file. Should be in the format of the files produced by maxfilter-produced. If dict, keys should be the time points and entries should be 4x3 ``dev_head_t`` matrices. If None, the original head position (from ``raw.info['dev_head_t']``) will be used. stc : instance of SourceEstimate The source estimate to use to simulate data. Must have the same sample rate as the raw data. trans : dict | str Either a transformation filename (usually made using mne_analyze) or an info dict (usually opened using read_trans()). If string, an ending of `.fif` or `.fif.gz` will be assumed to be in FIF format, any other ending will be assumed to be a text file with a 4x4 transformation matrix (like the `--trans` MNE-C option). src : str | instance of SourceSpaces If string, should be a source space filename. Can also be an instance of loaded or generated SourceSpaces. bem : str Filename of the BEM (e.g., "sample-5120-5120-5120-bem-sol.fif"). cov : instance of Covariance | 'simple' | None The sensor covariance matrix used to generate noise. If None, no noise will be added. If 'simple', a basic (diagonal) ad-hoc noise covariance will be used. mindist : float Minimum distance between sources and the inner skull boundary to use during forward calculation. interp : str Either 'linear' or 'zero', the type of forward-solution interpolation to use between provided time points. random_state : None | int | np.random.RandomState To specify the random generator state. n_jobs : int Number of jobs to use. verbose : bool, str, int, or None If not None, override default verbose level (see mne.verbose). Returns ------- raw : instance of Raw The simulated raw file. Notes ----- Events coded with the number of the forward solution used will be placed in the raw files in the trigger channel STI101 at the t=0 times of the SourceEstimates. The resulting SNR will be determined by the structure of the noise covariance, and the amplitudes of the SourceEstimate. Note that this will vary as a function of position. """ if isinstance(raw, string_types): with warnings.catch_warnings(record=True): raw = Raw(raw, allow_maxshield=True, preload=True, verbose=False) else: raw = raw.copy() if not isinstance(stc, _BaseSourceEstimate): raise TypeError('stc must be a SourceEstimate') if not np.allclose(raw.info['sfreq'], 1. / stc.tstep): raise ValueError('stc and raw must have same sample rate') rng = check_random_state(random_state) if interp not in ('linear', 'zero'): raise ValueError('interp must be "linear" or "zero"') if pos is None: # use pos from file dev_head_ts = [raw.info['dev_head_t']] * 2 offsets = np.array([0, raw.n_times]) interp = 'zero' else: if isinstance(pos, string_types): pos = get_chpi_positions(pos, verbose=False) if isinstance(pos, tuple): # can be an already-loaded pos file transs, rots, ts = pos ts -= raw.first_samp / raw.info['sfreq'] # MF files need reref dev_head_ts = [ np.r_[np.c_[r, t[:, np.newaxis]], [[0, 0, 0, 1]]] for r, t in zip(rots, transs) ] del transs, rots elif isinstance(pos, dict): ts = np.array(list(pos.keys()), float) ts.sort() dev_head_ts = [pos[float(tt)] for tt in ts] else: raise TypeError('unknown pos type %s' % type(pos)) if not (ts >= 0).all(): # pathological if not raise RuntimeError('Cannot have t < 0 in transform file') tend = raw.times[-1] assert not (ts < 0).any() assert not (ts > tend).any() if ts[0] > 0: ts = np.r_[[0.], ts] dev_head_ts.insert(0, raw.info['dev_head_t']['trans']) dev_head_ts = [{ 'trans': d, 'to': raw.info['dev_head_t']['to'], 'from': raw.info['dev_head_t']['from'] } for d in dev_head_ts] if ts[-1] < tend: dev_head_ts.append(dev_head_ts[-1]) ts = np.r_[ts, [tend]] offsets = raw.time_as_index(ts) offsets[-1] = raw.n_times # fix for roundoff error assert offsets[-2] != offsets[-1] del ts if isinstance(cov, string_types): assert cov == 'simple' cov = make_ad_hoc_cov(raw.info, verbose=False) assert np.array_equal(offsets, np.unique(offsets)) assert len(offsets) == len(dev_head_ts) approx_events = int( (raw.n_times / raw.info['sfreq']) / (stc.times[-1] - stc.times[0])) logger.info('Provided parameters will provide approximately %s event%s' % (approx_events, '' if approx_events == 1 else 's')) # get HPI freqs and reorder hpi_freqs = np.array( [x['custom_ref'][0] for x in raw.info['hpi_meas'][0]['hpi_coils']]) n_freqs = len(hpi_freqs) order = [x['number'] - 1 for x in raw.info['hpi_meas'][0]['hpi_coils']] assert np.array_equal(np.unique(order), np.arange(n_freqs)) hpi_freqs = hpi_freqs[order] hpi_order = raw.info['hpi_results'][0]['order'] - 1 assert np.array_equal(np.unique(hpi_order), np.arange(n_freqs)) hpi_freqs = hpi_freqs[hpi_order] # extract necessary info picks = pick_types(raw.info, meg=True, eeg=True) # for simulation meg_picks = pick_types(raw.info, meg=True, eeg=False) # for CHPI fwd_info = pick_info(raw.info, picks) fwd_info['projs'] = [] logger.info('Setting up raw data simulation using %s head position%s' % (len(dev_head_ts), 's' if len(dev_head_ts) != 1 else '')) raw.preload_data(verbose=False) if isinstance(stc, VolSourceEstimate): verts = [stc.vertices] else: verts = stc.vertices src = _restrict_source_space_to(src, verts) # figure out our cHPI, ECG, and EOG dipoles dig = raw.info['dig'] assert all([ d['coord_frame'] == FIFF.FIFFV_COORD_HEAD for d in dig if d['kind'] == FIFF.FIFFV_POINT_HPI ]) chpi_rrs = [d['r'] for d in dig if d['kind'] == FIFF.FIFFV_POINT_HPI] R, r0 = fit_sphere_to_headshape(raw.info, verbose=False)[:2] R /= 1000. r0 /= 1000. ecg_rr = np.array([[-R, 0, -3 * R]]) eog_rr = [ d['r'] for d in raw.info['dig'] if d['ident'] == FIFF.FIFFV_POINT_NASION ][0] eog_rr = eog_rr - r0 eog_rr = (eog_rr / np.sqrt(np.sum(eog_rr * eog_rr)) * 0.98 * R)[np.newaxis, :] eog_rr += r0 eog_bem = make_sphere_model(r0, head_radius=R, relative_radii=(0.99, 1.), sigmas=(0.33, 0.33), verbose=False) # let's oscillate between resting (17 bpm) and reading (4.5 bpm) rate # http://www.ncbi.nlm.nih.gov/pubmed/9399231 blink_rate = np.cos(2 * np.pi * 1. / 60. * raw.times) blink_rate *= 12.5 / 60. blink_rate += 4.5 / 60. blink_data = rng.rand(raw.n_times) < blink_rate / raw.info['sfreq'] blink_data = blink_data * (rng.rand(raw.n_times) + 0.5) # vary amplitudes blink_kernel = np.hanning(int(0.25 * raw.info['sfreq'])) eog_data = np.convolve(blink_data, blink_kernel, 'same')[np.newaxis, :] eog_data += rng.randn(eog_data.shape[1]) * 0.05 eog_data *= 100e-6 del blink_data, max_beats = int(np.ceil(raw.times[-1] * 70. / 60.)) cardiac_idx = np.cumsum( rng.uniform(60. / 70., 60. / 50., max_beats) * raw.info['sfreq']).astype(int) cardiac_idx = cardiac_idx[cardiac_idx < raw.n_times] cardiac_data = np.zeros(raw.n_times) cardiac_data[cardiac_idx] = 1 cardiac_kernel = np.concatenate([ 2 * np.hanning(int(0.04 * raw.info['sfreq'])), -0.3 * np.hanning(int(0.05 * raw.info['sfreq'])), 0.2 * np.hanning(int(0.26 * raw.info['sfreq'])) ], axis=-1) ecg_data = np.convolve(cardiac_data, cardiac_kernel, 'same')[np.newaxis, :] ecg_data += rng.randn(ecg_data.shape[1]) * 0.05 ecg_data *= 3e-4 del cardiac_data # Add to data file, then rescale for simulation for data, scale, exg_ch in zip([eog_data, ecg_data], [1e-3, 5e-4], ['EOG062', 'ECG063']): ch = pick_channels(raw.ch_names, [exg_ch]) if len(ch) == 1: raw._data[ch[0], :] = data data *= scale evoked = EvokedArray(np.zeros((len(picks), len(stc.times))), fwd_info, stc.tmin, verbose=False) stc_event_idx = np.argmin(np.abs(stc.times)) event_ch = pick_channels(raw.info['ch_names'], ['STI101'])[0] used = np.zeros(raw.n_times, bool) stc_indices = np.arange(raw.n_times) % len(stc.times) raw._data[event_ch, ].fill(0) hpi_mag = 25e-9 last_fwd = last_fwd_chpi = last_fwd_eog = last_fwd_ecg = src_sel = None for fi, (fwd, fwd_eog, fwd_ecg, fwd_chpi) in \ enumerate(_make_forward_solutions( fwd_info, trans, src, bem, eog_bem, dev_head_ts, mindist, chpi_rrs, eog_rr, ecg_rr, n_jobs)): # must be fixed orientation fwd = convert_forward_solution(fwd, surf_ori=True, force_fixed=True, verbose=False) # just use one arbitrary direction fwd_eog = fwd_eog['sol']['data'][:, ::3] fwd_ecg = fwd_ecg['sol']['data'][:, ::3] fwd_chpi = fwd_chpi[:, ::3] if src_sel is None: src_sel = _stc_src_sel(fwd['src'], stc) if isinstance(stc, VolSourceEstimate): verts = [stc.vertices] else: verts = stc.vertices diff_ = sum([len(v) for v in verts]) - len(src_sel) if diff_ != 0: warnings.warn( '%s STC vertices omitted due to fwd calculation' % (diff_, )) if last_fwd is None: last_fwd, last_fwd_eog, last_fwd_ecg, last_fwd_chpi = \ fwd, fwd_eog, fwd_ecg, fwd_chpi continue n_time = offsets[fi] - offsets[fi - 1] time_slice = slice(offsets[fi - 1], offsets[fi]) assert not used[time_slice].any() stc_idxs = stc_indices[time_slice] event_idxs = np.where(stc_idxs == stc_event_idx)[0] + offsets[fi - 1] used[time_slice] = True logger.info(' Simulating data for %0.3f-%0.3f sec with %s event%s' % (tuple(offsets[fi - 1:fi + 1] / raw.info['sfreq']) + (len(event_idxs), '' if len(event_idxs) == 1 else 's'))) # simulate brain data stc_data = stc.data[:, stc_idxs][src_sel] data = _interp(last_fwd['sol']['data'], fwd['sol']['data'], stc_data, interp) simulated = EvokedArray(data, evoked.info, 0) if cov is not None: noise = generate_noise_evoked(simulated, cov, [1, -1, 0.2], rng) simulated.data += noise.data assert simulated.data.shape[0] == len(picks) assert simulated.data.shape[1] == len(stc_idxs) raw._data[picks, time_slice] = simulated.data # add ECG, EOG, and CHPI traces raw._data[picks, time_slice] += \ _interp(last_fwd_eog, fwd_eog, eog_data[:, time_slice], interp) raw._data[meg_picks, time_slice] += \ _interp(last_fwd_ecg, fwd_ecg, ecg_data[:, time_slice], interp) this_t = np.arange(offsets[fi - 1], offsets[fi]) / raw.info['sfreq'] sinusoids = np.zeros((n_freqs, n_time)) for fi, freq in enumerate(hpi_freqs): sinusoids[fi] = 2 * np.pi * freq * this_t sinusoids[fi] = hpi_mag * np.sin(sinusoids[fi]) raw._data[meg_picks, time_slice] += \ _interp(last_fwd_chpi, fwd_chpi, sinusoids, interp) # add events raw._data[event_ch, event_idxs] = fi # prepare for next iteration last_fwd, last_fwd_eog, last_fwd_ecg, last_fwd_chpi = \ fwd, fwd_eog, fwd_ecg, fwd_chpi assert used.all() logger.info('Done') return raw
def test_set_channel_types(): """Test set_channel_types.""" raw = read_raw_fif(raw_fname) # Error Tests # Test channel name exists in ch_names mapping = {'EEG 160': 'EEG060'} with pytest.raises(ValueError, match=r"name \(EEG 160\) doesn't exist"): raw.set_channel_types(mapping) # Test change to illegal channel type mapping = {'EOG 061': 'xxx'} with pytest.raises(ValueError, match='cannot change to this channel type'): raw.set_channel_types(mapping) # Test changing type if in proj mapping = {'EEG 057': 'dbs', 'EEG 058': 'ecog', 'EEG 059': 'ecg', 'EEG 060': 'eog', 'EOG 061': 'seeg', 'MEG 2441': 'eeg', 'MEG 2443': 'eeg', 'MEG 2442': 'hbo', 'EEG 001': 'resp'} raw2 = read_raw_fif(raw_fname) raw2.info['bads'] = ['EEG 059', 'EEG 060', 'EOG 061'] with pytest.raises(RuntimeError, match='type .* in projector "PCA-v1"'): raw2.set_channel_types(mapping) # has prj raw2.add_proj([], remove_existing=True) with pytest.warns(RuntimeWarning, match='unit for channel.* has changed'): raw2 = raw2.set_channel_types(mapping) info = raw2.info assert info['chs'][371]['ch_name'] == 'EEG 057' assert info['chs'][371]['kind'] == FIFF.FIFFV_DBS_CH assert info['chs'][371]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][371]['coil_type'] == FIFF.FIFFV_COIL_EEG assert info['chs'][372]['ch_name'] == 'EEG 058' assert info['chs'][372]['kind'] == FIFF.FIFFV_ECOG_CH assert info['chs'][372]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][372]['coil_type'] == FIFF.FIFFV_COIL_EEG assert info['chs'][373]['ch_name'] == 'EEG 059' assert info['chs'][373]['kind'] == FIFF.FIFFV_ECG_CH assert info['chs'][373]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][373]['coil_type'] == FIFF.FIFFV_COIL_NONE assert info['chs'][374]['ch_name'] == 'EEG 060' assert info['chs'][374]['kind'] == FIFF.FIFFV_EOG_CH assert info['chs'][374]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][374]['coil_type'] == FIFF.FIFFV_COIL_NONE assert info['chs'][375]['ch_name'] == 'EOG 061' assert info['chs'][375]['kind'] == FIFF.FIFFV_SEEG_CH assert info['chs'][375]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][375]['coil_type'] == FIFF.FIFFV_COIL_EEG for idx in pick_channels(raw.ch_names, ['MEG 2441', 'MEG 2443']): assert info['chs'][idx]['kind'] == FIFF.FIFFV_EEG_CH assert info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_EEG idx = pick_channels(raw.ch_names, ['MEG 2442'])[0] assert info['chs'][idx]['kind'] == FIFF.FIFFV_FNIRS_CH assert info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_MOL assert info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_FNIRS_HBO # resp channel type idx = pick_channels(raw.ch_names, ['EEG 001'])[0] assert info['chs'][idx]['kind'] == FIFF.FIFFV_RESP_CH assert info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_V assert info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_NONE # Test meaningful error when setting channel type with unknown unit raw.info['chs'][0]['unit'] = 0. ch_types = {raw.ch_names[0]: 'misc'} pytest.raises(ValueError, raw.set_channel_types, ch_types)
data_path = sample.data_path() ############################################################################### # Set parameters raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' # Setup for reading the raw data raw = io.Raw(raw_fname, preload=True) events = mne.find_events(raw, 'STI 014') eog_event_id = 512 eog_events = mne.preprocessing.find_eog_events(raw, eog_event_id) raw.add_events(eog_events, 'STI 014') # Read epochs picks = mne.pick_types(raw.info, meg=False, eeg=False, stim=True, eog=False) tmin, tmax = -0.2, 0.5 event_ids = {'AudL': 1, 'AudR': 2, 'VisL': 3, 'VisR': 4} epochs = mne.Epochs(raw, events, event_ids, tmin, tmax, picks=picks) # Get the stim channel data pick_ch = mne.pick_channels(epochs.ch_names, 'STI 014')[0] data = epochs.get_data()[:, pick_ch, :].astype(int) data = np.sum((data.astype(int) & 512) == 512, axis=0) ############################################################################### # Plot EOG artifact distribution plt.stem(1e3 * epochs.times, data) plt.xlabel('Times (ms)') plt.ylabel('Blink counts (from %s trials)' % len(epochs)) plt.show()
significant_sensors = picks[p_values <= 0.05] significant_sensors_names = [raw.ch_names[k] for k in significant_sensors] print("Number of significant sensors : %d" % len(significant_sensors)) print("Sensors names : %s" % significant_sensors_names) ############################################################################### # View location of significantly active sensors evoked = mne.EvokedArray(-np.log10(p_values)[:, np.newaxis], epochs.info, tmin=0.) # Extract mask and indices of active sensors in layout stats_picks = mne.pick_channels(evoked.ch_names, significant_sensors_names) mask = p_values[:, np.newaxis] <= 0.05 evoked.plot_topomap(ch_type='grad', times=[0], scalings=1, time_format=None, cmap='Reds', vmin=0., vmax=np.max, units='-log10(p)', cbar_fmt='-%0.1f', mask=mask, size=3, show_names=lambda x: x[4:] + ' ' * 20)
from scripts.old_gui import * import os import mne import cognigraph.nodes.node as node from cognigraph.helpers.brainvision import read_brain_vision_data import numpy as np np.warnings.filterwarnings('ignore') # Set bad channels and calculate interpolation matrix manually # bad_channel_labels = ['Fp2', 'F5', 'C5', 'F2', 'PPO10h', 'POO1', 'FCC2h'] bad_channel_labels = ['F5', 'PPO10h', 'C5', 'FCC2h', 'F2', 'VEOG'] preprocessing._bad_channel_indices = mne.pick_channels(source.mne_info['ch_names'], include=bad_channel_labels) preprocessing._samples_to_be_collected = 0 preprocessing._enough_collected = True # preprocessing._interpolation_matrix = preprocessing._calculate_interpolation_matrix() message = node.Message(there_has_been_a_change=True, output_history_is_no_longer_valid=True) preprocessing._deliver_a_message_to_receivers(message) # Set the data in source to the (80, 100) s time interval vhdr_file_path = os.path.splitext(source.file_path)[0] + '.vhdr' start_s, stop_s = 75, 95 with source.not_triggering_reset(): source.data, _ = read_brain_vision_data(vhdr_file_path, time_axis=TIME_AXIS,
def camcan_preproc(subject): data_folder = '/Users/work/camcan' data_file = os.path.join(data_folder, subject, '_ses-rest_task-rest_proc-raw.fif') #data_file = os.path.join(data_folder, # 'sub-CC _ses-rest_task-rest_proc-raw_sss.fif') report = mne.Report(verbose=True) report.save('report_basic.html', overwrite=True) # read file raw = mne.io.read_raw_fif(data_file, preload=True) print(raw.info) fig = raw.plot_psd(fmax=50, show=False) report.add_figs_to_section(fig, captions='Raw', section='PSD') meg_channels = mne.pick_types(raw.info, meg=True, stim=False, ref_meg=False) eog1 = mne.pick_channels(raw.ch_names, ['EOG061']) eog2 = mne.pick_channels(raw.ch_names, ['EOG062']) ecg = mne.pick_channels(raw.ch_names, ['ECG063']) mag_channels = mne.pick_types(raw.info, meg='mag') # Bandpass between 1 and 45 Hz, bandstop between 50 and 100 raw.filter(1, 45) freqs = (50, 100) raw.notch_filter(freqs=freqs) fig = raw.plot_psd(fmax=50, show=False) report.add_figs_to_section(fig, captions='After bandpass and notch filters', section='PSD') # 2s epochs events = mne.make_fixed_length_events(raw, start=0, stop=None, duration=2.0, overlap=0) reject = dict(grad=4000e-13, mag=4e-12, eog=250e-6) #from mne tutorial epochs = mne.Epochs(raw, events, baseline=None, preload=True, reject=reject) # Topoplot before ICA fig = epochs.plot_psd_topomap(ch_type='mag', normalize=True, show=False) report.add_figs_to_section(fig, captions='Topoplot before ICA', section='Topoplots') # ICA to remove artefacts from mne.preprocessing import ICA, create_ecg_epochs, create_eog_epochs ica = ICA(n_components=0.95, method='fastica').fit(epochs) fig = ica.plot_components(show=False) ica.exclude = [] report.add_figs_to_section(fig, captions=('ICA components', ' '), section='ICA') # Find ECG artefacts ecg_epochs = create_ecg_epochs(raw) ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, threshold='auto') fig = ica.plot_components(ecg_inds, show=False) ica.exclude += ecg_inds report.add_figs_to_section(fig, captions='ECG components', section='ICA') # Find EOG artefacts eog_epochs = create_eog_epochs(raw, tmin=-.5, tmax=.5) eog_inds, scores = ica.find_bads_eog(eog_epochs) fig = ica.plot_components(eog_inds, show=False) ica.exclude += eog_inds report.add_figs_to_section(fig, captions='EOG components', section='ICA') # Apply ICA cleaned = epochs.copy() ica.apply(cleaned) fig = cleaned.plot_psd_topomap(ch_type='mag', normalize=True, show=False) report.add_figs_to_section(fig, captions='Topoplot after artefact rejection', section='Preprocessed') report.save('report.html', overwrite=True) # save fif file clean_file = os.path.join(data_folder, subject, '_cleaned.fif') cleaned.save(clean_file, overwrite=True)
def test_interpolation(): """Test interpolation""" raw, epochs, epochs_eeg, epochs_meg = _load_data() # It's a trade of between speed and accuracy. If every second channel is # selected the tests are more than 3x faster but the correlation # drops to 0.8 thresh = 0.80 # check that interpolation does nothing if no bads are marked epochs_eeg.info['bads'] = [] evoked_eeg = epochs_eeg.average() with warnings.catch_warnings(record=True) as w: evoked_eeg.interpolate_bads() assert_true(any('Doing nothing' in str(ww.message) for ww in w)) # create good and bad channels for EEG epochs_eeg.info['bads'] = [] goods_idx = np.ones(len(epochs_eeg.ch_names), dtype=bool) goods_idx[epochs_eeg.ch_names.index('EEG 012')] = False bads_idx = ~goods_idx evoked_eeg = epochs_eeg.average() ave_before = evoked_eeg.data[bads_idx] # interpolate bad channels for EEG pos = epochs_eeg._get_channel_positions() pos_good = pos[goods_idx] pos_bad = pos[bads_idx] interpolation = _make_interpolation_matrix(pos_good, pos_bad) assert_equal(interpolation.shape, (1, len(epochs_eeg.ch_names) - 1)) ave_after = np.dot(interpolation, evoked_eeg.data[goods_idx]) epochs_eeg.info['bads'] = ['EEG 012'] evoked_eeg = epochs_eeg.average() assert_array_equal(ave_after, evoked_eeg.interpolate_bads().data[bads_idx]) assert_allclose(ave_before, ave_after, atol=2e-6) # check that interpolation fails when preload is False epochs_eeg.preload = False assert_raises(ValueError, epochs_eeg.interpolate_bads) epochs_eeg.preload = True # check that interpolation changes the data in raw raw_eeg = io.RawArray(data=epochs_eeg._data[0], info=epochs_eeg.info) raw_before = raw_eeg._data[bads_idx] raw_after = raw_eeg.interpolate_bads()._data[bads_idx] assert_equal(np.all(raw_before == raw_after), False) # check that interpolation fails when preload is False for inst in [raw, epochs]: assert hasattr(inst, 'preload') inst.preload = False inst.info['bads'] = [inst.ch_names[1]] assert_raises(ValueError, inst.interpolate_bads) # check that interpolation works with few channels raw_few = raw.copy().crop(0, 0.1).load_data() raw_few.pick_channels(raw_few.ch_names[:1] + raw_few.ch_names[3:4]) assert_equal(len(raw_few.ch_names), 2) raw_few.del_proj() raw_few.info['bads'] = [raw_few.ch_names[-1]] orig_data = raw_few[1][0] with warnings.catch_warnings(record=True) as w: raw_few.interpolate_bads(reset_bads=False) assert len(w) == 0 new_data = raw_few[1][0] assert_true((new_data == 0).mean() < 0.5) assert_true(np.corrcoef(new_data, orig_data)[0, 1] > 0.1) # check that interpolation works when non M/EEG channels are present # before MEG channels with warnings.catch_warnings(record=True): # change of units raw.rename_channels({'MEG 0113': 'TRIGGER'}) raw.set_channel_types({'TRIGGER': 'stim'}) raw.info['bads'] = [raw.info['ch_names'][1]] raw.load_data() raw.interpolate_bads() # check that interpolation works for MEG epochs_meg.info['bads'] = ['MEG 0141'] evoked = epochs_meg.average() pick = pick_channels(epochs_meg.info['ch_names'], epochs_meg.info['bads']) # MEG -- raw raw_meg = io.RawArray(data=epochs_meg._data[0], info=epochs_meg.info) raw_meg.info['bads'] = ['MEG 0141'] data1 = raw_meg[pick, :][0][0] raw_meg.info.normalize_proj() data2 = raw_meg.interpolate_bads(reset_bads=False)[pick, :][0][0] assert_true(np.corrcoef(data1, data2)[0, 1] > thresh) # the same number of bads as before assert_true(len(raw_meg.info['bads']) == len(raw_meg.info['bads'])) # MEG -- epochs data1 = epochs_meg.get_data()[:, pick, :].ravel() epochs_meg.info.normalize_proj() epochs_meg.interpolate_bads() data2 = epochs_meg.get_data()[:, pick, :].ravel() assert_true(np.corrcoef(data1, data2)[0, 1] > thresh) assert_true(len(epochs_meg.info['bads']) == 0) # MEG -- evoked data1 = evoked.data[pick] evoked.info.normalize_proj() data2 = evoked.interpolate_bads().data[pick] assert_true(np.corrcoef(data1, data2)[0, 1] > thresh)
'MEG2642', 'MEG2643', 'MEG1123', 'MEG1122', 'MEG1133', 'MEG1132', 'MEG1312', 'MEG1313', 'MEG1323', 'MEG1322', 'MEG1333', 'MEG1332', 'MEG1342', 'MEG1343', 'MEG1433', 'MEG1432', 'MEG1442', 'MEG1443', 'MEG1512', 'MEG1513', 'MEG1522', 'MEG1523', 'MEG1533', 'MEG1532', 'MEG1543', 'MEG1542', 'MEG1613', 'MEG1612', 'MEG1622', 'MEG1623', 'MEG1632', 'MEG1633', 'MEG1643', 'MEG1642', 'MEG1722', 'MEG1723' ] eeg_roi = [ 'EEG005', 'EEG006', 'EEG009', 'EEG010', 'EEG011', 'EEG012', 'EEG013', 'EEG014', 'EEG015', 'EEG019', 'EEG020', 'EEG021', 'EEG022', 'EEG028', 'EEG029', 'EEG030', 'EEG031', 'EEG032' ] # Show sensors of interest on data # - mag picks = mne.pick_channels(item_ev_mean.ch_names, mag_roi) mask = np.zeros((len(item_ev_mean.ch_names), 1), dtype=bool) mask[picks, :] = True timepoint = .072 fig = item_ev_mean.copy().crop(tmin=timepoint, tmax=timepoint).plot_topomap( ch_type='mag', times=timepoint, show_names=False, mask=mask, mask_params=dict(marker='o', markerfacecolor='w', markeredgecolor='k', linewidth=0, markersize=8)) timepoint = .136
def test_find_events(): """Test find events in raw file """ events = read_events(fname) raw = io.Raw(raw_fname, preload=True) # let's test the defaulting behavior while we're at it extra_ends = ['', '_1'] orig_envs = [os.getenv('MNE_STIM_CHANNEL%s' % s) for s in extra_ends] os.environ['MNE_STIM_CHANNEL'] = 'STI 014' if 'MNE_STIM_CHANNEL_1' in os.environ: del os.environ['MNE_STIM_CHANNEL_1'] events2 = find_events(raw) assert_array_almost_equal(events, events2) # Reset some data for ease of comparison raw.first_samp = 0 raw.info['sfreq'] = 1000 stim_channel = 'STI 014' stim_channel_idx = pick_channels(raw.info['ch_names'], include=stim_channel) # test empty events channel raw._data[stim_channel_idx, :] = 0 assert_array_equal(find_events(raw), np.empty((0, 3), dtype='int32')) raw._data[stim_channel_idx, :4] = 1 assert_array_equal(find_events(raw), np.empty((0, 3), dtype='int32')) raw._data[stim_channel_idx, -1:] = 9 assert_array_equal(find_events(raw), [[14399, 0, 9]]) # Test that we can handle consecutive events with no gap raw._data[stim_channel_idx, 10:20] = 5 raw._data[stim_channel_idx, 20:30] = 6 raw._data[stim_channel_idx, 30:32] = 5 raw._data[stim_channel_idx, 40] = 6 assert_array_equal(find_events(raw, consecutive=False), [[10, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, consecutive=True), [[10, 0, 5], [20, 5, 6], [30, 6, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw), [[10, 0, 5], [20, 5, 6], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, output='offset', consecutive=False), [[31, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, output='offset', consecutive=True), [[19, 6, 5], [29, 5, 6], [31, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_raises(ValueError,find_events,raw, output='step', consecutive=True) assert_array_equal(find_events(raw, output='step', consecutive=True, shortest_event=1), [[10, 0, 5], [20, 5, 6], [30, 6, 5], [32, 5, 0], [40, 0, 6], [41, 6, 0], [14399, 0, 9], [14400, 9, 0]]) assert_array_equal(find_events(raw, output='offset'), [[19, 6, 5], [31, 0, 6], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, consecutive=False, min_duration=0.002), [[10, 0, 5]]) assert_array_equal(find_events(raw, consecutive=True, min_duration=0.002), [[10, 0, 5], [20, 5, 6], [30, 6, 5]]) assert_array_equal(find_events(raw, output='offset', consecutive=False, min_duration=0.002), [[31, 0, 5]]) assert_array_equal(find_events(raw, output='offset', consecutive=True, min_duration=0.002), [[19, 6, 5], [29, 5, 6], [31, 0, 5]]) assert_array_equal(find_events(raw, consecutive=True, min_duration=0.003), [[10, 0, 5], [20, 5, 6]]) # test find_stim_steps merge parameter raw._data[stim_channel_idx, :] = 0 raw._data[stim_channel_idx, 0] = 1 raw._data[stim_channel_idx, 10] = 4 raw._data[stim_channel_idx, 11:20] = 5 assert_array_equal(find_stim_steps(raw, pad_start=0, merge=0, stim_channel=stim_channel), [[0, 0, 1], [1, 1, 0], [10, 0, 4], [11, 4, 5], [20, 5, 0]]) assert_array_equal(find_stim_steps(raw, merge=-1, stim_channel=stim_channel), [[1, 1, 0], [10, 0, 5], [20, 5, 0]]) assert_array_equal(find_stim_steps(raw, merge=1, stim_channel=stim_channel), [[1, 1, 0], [11, 0, 5], [20, 5, 0]]) # put back the env vars we trampled on for s, o in zip(extra_ends, orig_envs): if o is not None: os.environ['MNE_STIM_CHANNEL%s' % s] = o
############################################################################### # .. _picking_channels: # # Obtaining subsets of channels # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # It is often useful to convert between channel names and the integer indices # identifying rows of the data array where those channels' measurements are # stored. The :class:`~mne.Info` object is useful for this task; two # convenience functions that rely on the :class:`mne.Info` object for picking # channels are :func:`mne.pick_channels` and :func:`mne.pick_types`. # :func:`~mne.pick_channels` minimally takes a list of all channel names and a # list of channel names to include; it is also possible to provide an empty # list to ``include`` and specify which channels to ``exclude`` instead: print(mne.pick_channels(info['ch_names'], include=['MEG 0312', 'EEG 005'])) print( 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
raw = raw.pick_types(meg=False, eeg=True, eog=False, exclude='bads') # Grab the sampling rate from the data fs = raw.info['sfreq'] ################################################################################################### # Settings for exploring an example channel of data ch_label = 'EEG 058' t_start = 20000 t_stop = int(t_start + (10 * fs)) ################################################################################################### # Extract an example channel to explore sig, times = raw.get_data(mne.pick_channels(raw.ch_names, [ch_label]), start=t_start, stop=t_stop, return_times=True) sig = np.squeeze(sig) ################################################################################################### # Plot a segment of the extracted time series data plot_time_series(times, sig) ################################################################################################### # Calculate Power Spectra # ----------------------- # # Next lets check the data in the frequency domain, calculating a power spectrum
def test_find_events(): """Test find_events in rt_epochs.""" raw = mne.io.read_raw_fif(raw_fname, preload=True, verbose=False) picks = mne.pick_types(raw.info, meg='grad', eeg=False, eog=True, stim=True, exclude=raw.info['bads']) event_id = [0, 5, 6] tmin, tmax = -0.2, 0.5 stim_channel = 'STI 014' stim_channel_idx = pick_channels(raw.info['ch_names'], include=[stim_channel]) # Reset some data for ease of comparison raw._first_samps[0] = 0 raw.info['sfreq'] = 1000 # Test that we can handle consecutive events with no gap raw._data[stim_channel_idx, :] = 0 raw._data[stim_channel_idx, 500:520] = 5 raw._data[stim_channel_idx, 520:530] = 6 raw._data[stim_channel_idx, 530:532] = 5 raw._data[stim_channel_idx, 540] = 6 raw._update_times() # consecutive=False find_events = dict(consecutive=False) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, stim_channel='STI 014', isi_max=0.5, find_events=find_events) rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_epochs.start() events = [5, 6] for ii, ev in enumerate(rt_epochs.iter_evoked()): assert_true(ev.comment == str(events[ii])) assert_true(ii == 1) # consecutive=True find_events = dict(consecutive=True) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, stim_channel='STI 014', isi_max=0.5, find_events=find_events) rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_epochs.start() events = [5, 6, 5, 6] for ii, ev in enumerate(rt_epochs.iter_evoked()): assert_true(ev.comment == str(events[ii])) assert_true(ii == 3) # min_duration=0.002 find_events = dict(consecutive=False, min_duration=0.002) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, stim_channel='STI 014', isi_max=0.5, find_events=find_events) rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_epochs.start() events = [5] for ii, ev in enumerate(rt_epochs.iter_evoked()): assert_true(ev.comment == str(events[ii])) assert_true(ii == 0) # ouput='step', consecutive=True find_events = dict(output='step', consecutive=True) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, stim_channel='STI 014', isi_max=0.5, find_events=find_events) rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_epochs.start() events = [5, 6, 5, 0, 6, 0] for ii, ev in enumerate(rt_epochs.iter_evoked()): assert_true(ev.comment == str(events[ii])) assert_true(ii == 5)
'Illum_(lux)': 'syst', 'Pressure_(bars)': 'misc', 'Temp_(C)': 'syst', 'Heart_Rate': 'bio' }) print(raw.copy().pick_types(eeg=True, eog=True, ecog=True, bio=True, resp=True, misc=True).ch_names) # Generating channel indices to subset data with subset_index1 = mne.pick_channels(raw.ch_names, include=[], exclude=[ 'Accel_X', 'Accel_Z', 'Illum_(lux)', 'Pressure_(bars)', 'Temp_(C)' ]) subset_index2 = mne.pick_types(raw.info, eeg=True, eog=True, emg=True, ecg=True) #Cropping data raw_selection = raw.copy().crop(tmin=100, tmax=150) print(raw_selection) #Preliminary 10s plot of raw data mne.viz.plot_raw(raw_selection) mne.viz.plot_raw(raw)
def mastoidReref(raw): ref_idx = pick_channels(raw.info['ch_names'],['M2']) eeg_idx = pick_types(raw.info,eeg=True) raw._data[eeg_idx,:] = raw._data[eeg_idx,:] - raw._data[ref_idx,:] * .5 ; return raw
def test_find_events(): """Test find events in raw file.""" events = read_events(fname) raw = read_raw_fif(raw_fname, preload=True) # let's test the defaulting behavior while we're at it extra_ends = ['', '_1'] orig_envs = [os.getenv('MNE_STIM_CHANNEL%s' % s) for s in extra_ends] os.environ['MNE_STIM_CHANNEL'] = 'STI 014' if 'MNE_STIM_CHANNEL_1' in os.environ: del os.environ['MNE_STIM_CHANNEL_1'] events2 = find_events(raw) assert_array_almost_equal(events, events2) # now test with mask events11 = find_events(raw, mask=3, mask_type='not_and') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') events22 = read_events(fname, mask=3, mask_type='not_and') assert_true(sum('events masked' in str(ww.message) for ww in w) == 1) assert_array_equal(events11, events22) # Reset some data for ease of comparison raw._first_samps[0] = 0 raw.info['sfreq'] = 1000 raw._update_times() stim_channel = 'STI 014' stim_channel_idx = pick_channels(raw.info['ch_names'], include=[stim_channel]) # test digital masking raw._data[stim_channel_idx, :5] = np.arange(5) raw._data[stim_channel_idx, 5:] = 0 # 1 == '0b1', 2 == '0b10', 3 == '0b11', 4 == '0b100' assert_raises(TypeError, find_events, raw, mask="0", mask_type='and') assert_raises(ValueError, find_events, raw, mask=0, mask_type='blah') # testing mask_type. default = 'not_and' assert_array_equal(find_events(raw, shortest_event=1, mask=1, mask_type='not_and'), [[2, 0, 2], [4, 2, 4]]) assert_array_equal(find_events(raw, shortest_event=1, mask=2, mask_type='not_and'), [[1, 0, 1], [3, 0, 1], [4, 1, 4]]) assert_array_equal(find_events(raw, shortest_event=1, mask=3, mask_type='not_and'), [[4, 0, 4]]) assert_array_equal(find_events(raw, shortest_event=1, mask=4, mask_type='not_and'), [[1, 0, 1], [2, 1, 2], [3, 2, 3]]) # testing with mask_type = 'and' assert_array_equal(find_events(raw, shortest_event=1, mask=1, mask_type='and'), [[1, 0, 1], [3, 0, 1]]) assert_array_equal(find_events(raw, shortest_event=1, mask=2, mask_type='and'), [[2, 0, 2]]) assert_array_equal(find_events(raw, shortest_event=1, mask=3, mask_type='and'), [[1, 0, 1], [2, 1, 2], [3, 2, 3]]) assert_array_equal(find_events(raw, shortest_event=1, mask=4, mask_type='and'), [[4, 0, 4]]) # test empty events channel raw._data[stim_channel_idx, :] = 0 assert_array_equal(find_events(raw), np.empty((0, 3), dtype='int32')) raw._data[stim_channel_idx, :4] = 1 assert_array_equal(find_events(raw), np.empty((0, 3), dtype='int32')) raw._data[stim_channel_idx, -1:] = 9 assert_array_equal(find_events(raw), [[14399, 0, 9]]) # Test that we can handle consecutive events with no gap raw._data[stim_channel_idx, 10:20] = 5 raw._data[stim_channel_idx, 20:30] = 6 raw._data[stim_channel_idx, 30:32] = 5 raw._data[stim_channel_idx, 40] = 6 assert_array_equal(find_events(raw, consecutive=False), [[10, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, consecutive=True), [[10, 0, 5], [20, 5, 6], [30, 6, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw), [[10, 0, 5], [20, 5, 6], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, output='offset', consecutive=False), [[31, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, output='offset', consecutive=True), [[19, 6, 5], [29, 5, 6], [31, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_raises(ValueError, find_events, raw, output='step', consecutive=True) assert_array_equal(find_events(raw, output='step', consecutive=True, shortest_event=1), [[10, 0, 5], [20, 5, 6], [30, 6, 5], [32, 5, 0], [40, 0, 6], [41, 6, 0], [14399, 0, 9], [14400, 9, 0]]) assert_array_equal(find_events(raw, output='offset'), [[19, 6, 5], [31, 0, 6], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, consecutive=False, min_duration=0.002), [[10, 0, 5]]) assert_array_equal(find_events(raw, consecutive=True, min_duration=0.002), [[10, 0, 5], [20, 5, 6], [30, 6, 5]]) assert_array_equal(find_events(raw, output='offset', consecutive=False, min_duration=0.002), [[31, 0, 5]]) assert_array_equal(find_events(raw, output='offset', consecutive=True, min_duration=0.002), [[19, 6, 5], [29, 5, 6], [31, 0, 5]]) assert_array_equal(find_events(raw, consecutive=True, min_duration=0.003), [[10, 0, 5], [20, 5, 6]]) # test find_stim_steps merge parameter raw._data[stim_channel_idx, :] = 0 raw._data[stim_channel_idx, 0] = 1 raw._data[stim_channel_idx, 10] = 4 raw._data[stim_channel_idx, 11:20] = 5 assert_array_equal(find_stim_steps(raw, pad_start=0, merge=0, stim_channel=stim_channel), [[0, 0, 1], [1, 1, 0], [10, 0, 4], [11, 4, 5], [20, 5, 0]]) assert_array_equal(find_stim_steps(raw, merge=-1, stim_channel=stim_channel), [[1, 1, 0], [10, 0, 5], [20, 5, 0]]) assert_array_equal(find_stim_steps(raw, merge=1, stim_channel=stim_channel), [[1, 1, 0], [11, 0, 5], [20, 5, 0]]) # put back the env vars we trampled on for s, o in zip(extra_ends, orig_envs): if o is not None: os.environ['MNE_STIM_CHANNEL%s' % s] = o # Test with list of stim channels raw._data[stim_channel_idx, 1:101] = np.zeros(100) raw._data[stim_channel_idx, 10:11] = 1 raw._data[stim_channel_idx, 30:31] = 3 stim_channel2 = 'STI 015' stim_channel2_idx = pick_channels(raw.info['ch_names'], include=[stim_channel2]) raw._data[stim_channel2_idx, :] = 0 raw._data[stim_channel2_idx, :100] = raw._data[stim_channel_idx, 5:105] events1 = find_events(raw, stim_channel='STI 014') events2 = events1.copy() events2[:, 0] -= 5 events = find_events(raw, stim_channel=['STI 014', stim_channel2]) assert_array_equal(events[::2], events2) assert_array_equal(events[1::2], events1)
info = mne.io.read_info(epo_info_path) ch_subset = mne.pick_types(info, ref_meg=False, eog=False, stim=False) info = mne.pick_info(info, ch_subset) sensor_groupings = { "A": [i for i in info["ch_names"] if "MLO" in i], "B": [i for i in info["ch_names"] if "MRO" in i], "OC": [i for i in info["ch_names"] if "MZO" in i], "C": [i for i in info["ch_names"] if "MLC" in i], "D": [i for i in info["ch_names"] if "MRC" in i], "PC": [i for i in info["ch_names"] if "MZC" in i] } dummy_data = np.zeros(274) ch_selection = np.zeros(274, dtype=bool) ch_selection[mne.pick_channels(info["ch_names"], sensor_groupings[key])] = True mask_params = dict(marker='o', markerfacecolor='w', markeredgecolor='r', linewidth=0, markersize=4) reg_colour = "#00A4CC" odd_colour = "#F95700" sign_colour = "#00ff00" non_sign_colour = "#cccccc" reg_data = np.array(regular[key]) * 1e14 odd_data = np.array(odd[key]) * 1e14
# Obtain the sampling rate of the data print(info['sfreq'], 'Hz') # List all information about the first data channel print(info['chs'][0]) ############################################################################### # Obtaining subsets of channels # ----------------------------- # # There are a number of convenience functions to obtain channel indices, given # an :class:`mne.io.meas_info.Info` object. # Get channel indices by name channel_indices = mne.pick_channels(info['ch_names'], ['MEG 0312', 'EEG 005']) # Get channel indices by regular expression channel_indices = mne.pick_channels_regexp(info['ch_names'], 'MEG *') # Get channel indices by type channel_indices = mne.pick_types(info, meg=True) # MEG only channel_indices = mne.pick_types(info, eeg=True) # EEG only # MEG gradiometers and EEG channels channel_indices = mne.pick_types(info, meg='grad', eeg=True) # Get a dictionary of channel indices, grouped by channel type channel_indices_by_type = mne.io.pick.channel_indices_by_type(info) print('The first three magnetometers:', channel_indices_by_type['mag'][:3]) ###############################################################################
def test_interpolation_meg(): """Test interpolation of MEG channels.""" # speed accuracy tradeoff: channel subselection is faster but the # correlation drops thresh = 0.68 raw, epochs_meg = _load_data('meg') # check that interpolation works when non M/EEG channels are present # before MEG channels raw.crop(0, 0.1).load_data().pick_channels(epochs_meg.ch_names) raw.info.normalize_proj() with pytest.warns(RuntimeWarning, match='unit .* changed from .* to .*'): raw.set_channel_types({raw.ch_names[0]: 'stim'}) raw.info['bads'] = [raw.ch_names[1]] raw.load_data() raw.interpolate_bads(mode='fast') del raw # check that interpolation works for MEG epochs_meg.info['bads'] = ['MEG 0141'] evoked = epochs_meg.average() pick = pick_channels(epochs_meg.info['ch_names'], epochs_meg.info['bads']) # MEG -- raw raw_meg = io.RawArray(data=epochs_meg._data[0], info=epochs_meg.info) raw_meg.info['bads'] = ['MEG 0141'] data1 = raw_meg[pick, :][0][0] raw_meg.info.normalize_proj() data2 = raw_meg.interpolate_bads(reset_bads=False, mode='fast')[pick, :][0][0] assert np.corrcoef(data1, data2)[0, 1] > thresh # the same number of bads as before assert len(raw_meg.info['bads']) == len(raw_meg.info['bads']) # MEG -- epochs data1 = epochs_meg.get_data()[:, pick, :].ravel() epochs_meg.info.normalize_proj() epochs_meg.interpolate_bads(mode='fast') data2 = epochs_meg.get_data()[:, pick, :].ravel() assert np.corrcoef(data1, data2)[0, 1] > thresh assert len(epochs_meg.info['bads']) == 0 # MEG -- evoked (plus auto origin) data1 = evoked.data[pick] evoked.info.normalize_proj() data2 = evoked.interpolate_bads(origin='auto').data[pick] assert np.corrcoef(data1, data2)[0, 1] > thresh # MEG -- with exclude evoked.info['bads'] = ['MEG 0141', 'MEG 0121'] pick = pick_channels(evoked.ch_names, evoked.info['bads'], ordered=True) evoked.data[pick[-1]] = 1e10 data1 = evoked.data[pick] evoked.info.normalize_proj() data2 = evoked.interpolate_bads( origin='auto', exclude=['MEG 0121'] ).data[pick] assert np.corrcoef(data1[0], data2[0])[0, 1] > thresh assert np.all(data2[1] == 1e10)
from mne.viz import tight_layout from mne.datasets import sample print(__doc__) data_path = sample.data_path() fname = data_path + '/MEG/sample/sample_audvis-ave.fif' # Reading evoked data condition = 'Left Auditory' evoked = mne.read_evokeds(fname, condition=condition, baseline=(None, 0), proj=True) ch_names = evoked.info['ch_names'] picks = mne.pick_channels(ch_names=ch_names, include=["MEG 2332"]) # Create subplots f, (ax1, ax2, ax3) = plt.subplots(3) evoked.plot(exclude=[], picks=picks, axes=ax1, titles=dict(grad='Before time shifting'), time_unit='s') # Apply relative time-shift of 500 ms evoked.shift_time(0.5, relative=True) evoked.plot(exclude=[], picks=picks, axes=ax2, titles=dict(grad='Relative shift: 500 ms'), time_unit='s') # Apply absolute time-shift of 500 ms evoked.shift_time(0.5, relative=False)
def test_find_events(): """Test find events in raw file.""" events = read_events(fname) raw = read_raw_fif(raw_fname, preload=True) # let's test the defaulting behavior while we're at it extra_ends = ['', '_1'] orig_envs = [os.getenv('MNE_STIM_CHANNEL%s' % s) for s in extra_ends] os.environ['MNE_STIM_CHANNEL'] = 'STI 014' if 'MNE_STIM_CHANNEL_1' in os.environ: del os.environ['MNE_STIM_CHANNEL_1'] events2 = find_events(raw) assert_array_almost_equal(events, events2) # now test with mask events11 = find_events(raw, mask=3, mask_type='not_and') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') events22 = read_events(fname, mask=3) assert_true(sum('events masked' in str(ww.message) for ww in w) == 1) assert_array_equal(events11, events22) # Reset some data for ease of comparison raw._first_samps[0] = 0 raw.info['sfreq'] = 1000 raw._update_times() stim_channel = 'STI 014' stim_channel_idx = pick_channels(raw.info['ch_names'], include=[stim_channel]) # test digital masking raw._data[stim_channel_idx, :5] = np.arange(5) raw._data[stim_channel_idx, 5:] = 0 # 1 == '0b1', 2 == '0b10', 3 == '0b11', 4 == '0b100' assert_raises(TypeError, find_events, raw, mask="0") assert_raises(ValueError, find_events, raw, mask=0, mask_type='blah') # testing mask_type. default = 'not_and' assert_array_equal(find_events(raw, shortest_event=1, mask=1, mask_type='not_and'), [[2, 0, 2], [4, 2, 4]]) assert_array_equal(find_events(raw, shortest_event=1, mask=2, mask_type='not_and'), [[1, 0, 1], [3, 0, 1], [4, 1, 4]]) assert_array_equal(find_events(raw, shortest_event=1, mask=3, mask_type='not_and'), [[4, 0, 4]]) assert_array_equal(find_events(raw, shortest_event=1, mask=4, mask_type='not_and'), [[1, 0, 1], [2, 1, 2], [3, 2, 3]]) # testing with mask_type = 'and' assert_array_equal(find_events(raw, shortest_event=1, mask=1, mask_type='and'), [[1, 0, 1], [3, 0, 1]]) assert_array_equal(find_events(raw, shortest_event=1, mask=2, mask_type='and'), [[2, 0, 2]]) assert_array_equal(find_events(raw, shortest_event=1, mask=3, mask_type='and'), [[1, 0, 1], [2, 1, 2], [3, 2, 3]]) assert_array_equal(find_events(raw, shortest_event=1, mask=4, mask_type='and'), [[4, 0, 4]]) # test empty events channel raw._data[stim_channel_idx, :] = 0 assert_array_equal(find_events(raw), np.empty((0, 3), dtype='int32')) raw._data[stim_channel_idx, :4] = 1 assert_array_equal(find_events(raw), np.empty((0, 3), dtype='int32')) raw._data[stim_channel_idx, -1:] = 9 assert_array_equal(find_events(raw), [[14399, 0, 9]]) # Test that we can handle consecutive events with no gap raw._data[stim_channel_idx, 10:20] = 5 raw._data[stim_channel_idx, 20:30] = 6 raw._data[stim_channel_idx, 30:32] = 5 raw._data[stim_channel_idx, 40] = 6 assert_array_equal(find_events(raw, consecutive=False), [[10, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, consecutive=True), [[10, 0, 5], [20, 5, 6], [30, 6, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw), [[10, 0, 5], [20, 5, 6], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, output='offset', consecutive=False), [[31, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, output='offset', consecutive=True), [[19, 6, 5], [29, 5, 6], [31, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_raises(ValueError, find_events, raw, output='step', consecutive=True) assert_array_equal(find_events(raw, output='step', consecutive=True, shortest_event=1), [[10, 0, 5], [20, 5, 6], [30, 6, 5], [32, 5, 0], [40, 0, 6], [41, 6, 0], [14399, 0, 9], [14400, 9, 0]]) assert_array_equal(find_events(raw, output='offset'), [[19, 6, 5], [31, 0, 6], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, consecutive=False, min_duration=0.002), [[10, 0, 5]]) assert_array_equal(find_events(raw, consecutive=True, min_duration=0.002), [[10, 0, 5], [20, 5, 6], [30, 6, 5]]) assert_array_equal(find_events(raw, output='offset', consecutive=False, min_duration=0.002), [[31, 0, 5]]) assert_array_equal(find_events(raw, output='offset', consecutive=True, min_duration=0.002), [[19, 6, 5], [29, 5, 6], [31, 0, 5]]) assert_array_equal(find_events(raw, consecutive=True, min_duration=0.003), [[10, 0, 5], [20, 5, 6]]) # test find_stim_steps merge parameter raw._data[stim_channel_idx, :] = 0 raw._data[stim_channel_idx, 0] = 1 raw._data[stim_channel_idx, 10] = 4 raw._data[stim_channel_idx, 11:20] = 5 assert_array_equal(find_stim_steps(raw, pad_start=0, merge=0, stim_channel=stim_channel), [[0, 0, 1], [1, 1, 0], [10, 0, 4], [11, 4, 5], [20, 5, 0]]) assert_array_equal(find_stim_steps(raw, merge=-1, stim_channel=stim_channel), [[1, 1, 0], [10, 0, 5], [20, 5, 0]]) assert_array_equal(find_stim_steps(raw, merge=1, stim_channel=stim_channel), [[1, 1, 0], [11, 0, 5], [20, 5, 0]]) # put back the env vars we trampled on for s, o in zip(extra_ends, orig_envs): if o is not None: os.environ['MNE_STIM_CHANNEL%s' % s] = o
from mne.viz.utils import center_cmap # load and preprocess data #################################################### subject = 1 # use data from subject 1 runs = [6, 10, 14] # use only hand and feet motor imagery runs fnames = eegbci.load_data(subject, runs) raws = [read_raw_edf(f, preload=True) for f in fnames] raw = concatenate_raws(raws) raw.rename_channels(lambda x: x.strip('.')) # remove dots from channel names events, _ = mne.events_from_annotations(raw) picks = mne.pick_channels(raw.info["ch_names"], ["C3", "Cz", "C4"]) # epoch data ################################################################## tmin, tmax = -1, 4 # define epochs around events (in s) event_ids = dict(hands=2, feet=3) # map event IDs to tasks epochs = mne.Epochs(raw, events, event_ids, tmin - 0.5, tmax + 0.5, picks=picks, baseline=None, preload=True) # compute ERDS maps ########################################################### freqs = np.arange(2, 36, 1) # frequencies from 2-35Hz n_cycles = freqs # use constant t/f resolution vmin, vmax = -1, 1.5 # set min and max ERDS values in plot baseline = [-1, 0] # baseline interval (in s) cmap = center_cmap(plt.cm.RdBu, vmin, vmax) # zero maps to white kwargs = dict(n_permutations=100, step_down_p=0.05, seed=1,
# both noise and signal and one with just noise events = mne.find_events(raw, initial_event=True) tmax = (len(stc_signal.times) - 1) / sfreq epochs = mne.Epochs(raw, events, event_id=dict(signal=1, noise=2), tmin=0, tmax=tmax, baseline=None, preload=True) assert len(epochs) == 2 # ensure that we got the two expected events # Plot some of the channels of the simulated data that are situated above one # of our simulated sources. picks = mne.pick_channels(epochs.ch_names, mne.read_selection('Left-frontal')) epochs.plot(picks=picks) ############################################################################### # Power mapping # ------------- # With our simulated dataset ready, we can now pretend to be researchers that # have just recorded this from a real subject and are going to study what parts # of the brain communicate with each other. # # First, we'll create a source estimate of the MEG data. We'll use both a # straightforward MNE-dSPM inverse solution for this, and the DICS beamformer # which is specifically designed to work with oscillatory data. ############################################################################### # Computing the inverse using MNE-dSPM:
clf1 = svm.SVC(kernel='linear', class_weight='balanced', probability=True) clf2 = svm.SVC(kernel='poly', degree=2, class_weight='balanced', probability=True) clf3 = svm.SVC(class_weight='balanced', probability=True) eclf1 = VotingClassifier(estimators=[('deg3', clf1), ('quad', clf2), ('rbf', clf3)], voting='soft')#eclf1 = #eclf1.fit(np.matrix(X),np.array(y)) classifier = eclf1 print "Cross Validating Ensemble" scores = cross_val_score(classifier, X, y, cv=2) print "Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2) print "Training classifier" classifier.fit(X,y) print "Done" print "Exporting classifiers" outfile = open("mne_classifier.pkl", 'wb+') pickle.dump(classifier, outfile) outfile.close() print "Done" if args.plot_epochs: picks = mne.pick_types(filtered_data.info, eeg=True, stim=False, eog=False) evoked = epochs.average() picks = mne.pick_channels(evoked.ch_names, ['O1']) evoked_target = epochs['target'].average() evoked_nontarget = epochs['nontarget'].average() difference = mne.combine_evoked([evoked_target, evoked_nontarget], [-1, 1]) mne.viz.plot_compare_evokeds({'target':evoked_target, 'nontarget':evoked_nontarget, 'difference':difference}, picks=picks)
t_po = 2 duration = 2; # events duration path_visual = cf_load.visual_path() df_visual = pd.read_csv(path_visual) cf_subjects = ['AnRa', 'AnRi', 'ArLa', 'BeFe', 'DiAs', 'FaWa', 'JuRo', 'NeLa', 'SoGi'] tasks = ['stimuli', 'rest_baseline'] runs = ['1','2'] for sub in cf_subjects: for task in tasks: for run in runs: #%% subject = cf_load.Subject(name=sub, task= task, run = run) fpath = subject.fpath(preproc = preproc, suffix='lnrmv') raw = subject.import_data(fpath) bands = HFB_test.freq_bands() # Select Bands of interests HFB = HFB_test.extract_HFB(raw, bands) epochs = HFB_test.epoch_HFB(HFB, raw, t_pr = t_pr, t_po = t_po) visual_chan = list(df_visual['chan_name'].loc[df_visual['subject_id']== sub]) category = list(df_visual['category'].loc[df_visual['subject_id']== sub]) brodman = list(df_visual['brodman'].loc[df_visual['subject_id']== sub]) data = epochs.get_data(picks=visual_chan) ch_idx = mne.pick_channels(raw.info['ch_names'], include=visual_chan) visual_dict = dict(data=data, chan=visual_chan, category=category, brodman=brodman, ch_idx=ch_idx) fpath2save = subject.fpath(preproc = preproc, suffix = suffix2save, ext=ext2save) sp.io.savemat(fpath2save, visual_dict)
def test_sns(): """Test sensor noise suppression""" # artificial (IID) data data = np.random.RandomState(0).randn(102, 5000) info = create_info(len(data), 1000., 'mag') raw = io.RawArray(data, info) assert_raises(ValueError, SensorNoiseSuppression, 'foo') assert_raises(TypeError, SensorNoiseSuppression(10).fit, 'foo') assert_raises(ValueError, SensorNoiseSuppression, -1) raw.info['bads'] = [raw.ch_names[1]] assert_raises(ValueError, SensorNoiseSuppression(101).fit, raw) for n_neighbors, bounds in ((2, (17, 20)), (5, (11, 15),), (10, (9, 12)), (20, (7, 10)), (50, (5, 9)), (100, (5, 8)), ): sns = SensorNoiseSuppression(n_neighbors) sns.fit(raw) raw_sns = sns.apply(raw.copy()) operator = sns.operator # bad channels are modified but not used assert_allclose(operator[:, 1], 0.) assert_true(np.sum(np.abs(operator)) > 0) assert_equal(operator[0].astype(bool).sum(), n_neighbors) assert_allclose(np.diag(operator), 0.) picks = pick_types(raw.info) orig_power = np.linalg.norm(raw[picks][0]) # Test the suppression factor factor = orig_power / np.linalg.norm(raw_sns[picks][0]) assert_true(bounds[0] < factor < bounds[1], msg='%s: %s < %s < %s' % (n_neighbors, bounds[0], factor, bounds[1])) # degenerate conditions assert_raises(TypeError, sns.apply, 'foo') sub_raw = raw.copy().pick_channels(raw.ch_names[:-1]) assert_raises(RuntimeError, sns.apply, sub_raw) # not all orig chs sub_sns = SensorNoiseSuppression(8) sub_sns.fit(sub_raw) assert_raises(RuntimeError, sub_sns.apply, raw) # not all new chs # sample data raw = io.read_raw_fif(raw_fname) n_neighbors = 8 sns = SensorNoiseSuppression(n_neighbors=n_neighbors) sns.fit(raw) raw_sns = sns.apply(raw.copy().load_data()) operator = sns.operator # bad channels are modified assert_equal(len(raw.info['bads']), 2) for pick in pick_channels(raw.ch_names, raw.info['bads']): expected = np.zeros(operator.shape[0]) sub_pick = sns._used_chs.index(raw.ch_names[pick]) expected[sub_pick] = 1. assert_raises(AssertionError, assert_allclose, operator[sub_pick], expected) assert_raises(AssertionError, assert_allclose, raw[pick][0], raw_sns[pick][0]) assert_equal(operator[0].astype(bool).sum(), n_neighbors) assert_equal(operator[0, 0], 0.) picks = pick_types(raw.info) orig_power = np.linalg.norm(raw[picks][0]) # Test the suppression factor factor = orig_power / np.linalg.norm(raw_sns[picks][0]) bounds = (1.3, 1.7) assert_true(bounds[0] < factor < bounds[1], msg='%s: %s < %s < %s' % (n_neighbors, bounds[0], factor, bounds[1])) # degenerate conditions assert_raises(RuntimeError, sns.apply, raw) # not preloaded # Test against NoiseTools rng = np.random.RandomState(0) n_channels = 9 x = rng.randn(n_channels, 10000) # make some correlations x = np.dot(rng.randn(n_channels, n_channels), x) x -= x.mean(axis=-1, keepdims=True) # savemat('test.mat', dict(x=x)) # --> run through nt_sns in MATLAB nt_op = np.array([ # Obtained from NoiseTools 18-Nov-2016 [0, 0, -0.3528, 0, 0.6152, 0, 0, -0.3299, 0.1914], [0, 0, 0, 0.0336, 0, 0, -0.4284, 0, 0], [-0.2928, 0.2463, 0, 0, -0.0891, 0, 0, 0.2200, 0], [0, 0.0191, 0, 0, -0.3756, -0.3253, 0.4047, -0.4608, 0], [0.3184, 0, -0.0878, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0.5865, 0, -0.2137], [0, -0.5190, 0, 0.5059, 0, 0.8271, 0, 0, -0.0771], [-0.3953, 0, 0.3092, -0.5018, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -0.2050, 0, 0, 0]]).T raw = RawArray(x, create_info(n_channels, 1000., ['grad', 'grad', 'mag'] * 3)) sns = SensorNoiseSuppression(3) sns.fit(raw) # this isn't perfect, but it's close np.testing.assert_allclose(sns.operator, nt_op, rtol=5e-2, atol=1e-3)
def test_find_events(): """Test find events in raw file """ events = read_events(fname) raw = io.Raw(raw_fname, preload=True) # let's test the defaulting behavior while we're at it extra_ends = ['', '_1'] orig_envs = [os.getenv('MNE_STIM_CHANNEL%s' % s) for s in extra_ends] os.environ['MNE_STIM_CHANNEL'] = 'STI 014' if 'MNE_STIM_CHANNEL_1' in os.environ: del os.environ['MNE_STIM_CHANNEL_1'] events2 = find_events(raw) assert_array_almost_equal(events, events2) # now test with mask events11 = find_events(raw, mask=3) events22 = read_events(fname, mask=3) assert_array_equal(events11, events22) # Reset some data for ease of comparison raw._first_samps[0] = 0 raw.info['sfreq'] = 1000 raw._update_times() stim_channel = 'STI 014' stim_channel_idx = pick_channels(raw.info['ch_names'], include=[stim_channel]) # test digital masking raw._data[stim_channel_idx, :5] = np.arange(5) raw._data[stim_channel_idx, 5:] = 0 # 1 == '0b1', 2 == '0b10', 3 == '0b11', 4 == '0b100' assert_raises(TypeError, find_events, raw, mask="0") assert_array_equal(find_events(raw, shortest_event=1, mask=1), [[2, 0, 2], [4, 2, 4]]) assert_array_equal(find_events(raw, shortest_event=1, mask=2), [[1, 0, 1], [3, 0, 1], [4, 1, 4]]) assert_array_equal(find_events(raw, shortest_event=1, mask=3), [[4, 0, 4]]) assert_array_equal(find_events(raw, shortest_event=1, mask=4), [[1, 0, 1], [2, 1, 2], [3, 2, 3]]) # test empty events channel raw._data[stim_channel_idx, :] = 0 assert_array_equal(find_events(raw), np.empty((0, 3), dtype='int32')) raw._data[stim_channel_idx, :4] = 1 assert_array_equal(find_events(raw), np.empty((0, 3), dtype='int32')) raw._data[stim_channel_idx, -1:] = 9 assert_array_equal(find_events(raw), [[14399, 0, 9]]) # Test that we can handle consecutive events with no gap raw._data[stim_channel_idx, 10:20] = 5 raw._data[stim_channel_idx, 20:30] = 6 raw._data[stim_channel_idx, 30:32] = 5 raw._data[stim_channel_idx, 40] = 6 assert_array_equal(find_events(raw, consecutive=False), [[10, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal( find_events(raw, consecutive=True), [[10, 0, 5], [20, 5, 6], [30, 6, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw), [[10, 0, 5], [20, 5, 6], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, output='offset', consecutive=False), [[31, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_array_equal( find_events(raw, output='offset', consecutive=True), [[19, 6, 5], [29, 5, 6], [31, 0, 5], [40, 0, 6], [14399, 0, 9]]) assert_raises(ValueError, find_events, raw, output='step', consecutive=True) assert_array_equal( find_events(raw, output='step', consecutive=True, shortest_event=1), [[10, 0, 5], [20, 5, 6], [30, 6, 5], [32, 5, 0], [40, 0, 6], [41, 6, 0], [14399, 0, 9], [14400, 9, 0]]) assert_array_equal(find_events(raw, output='offset'), [[19, 6, 5], [31, 0, 6], [40, 0, 6], [14399, 0, 9]]) assert_array_equal(find_events(raw, consecutive=False, min_duration=0.002), [[10, 0, 5]]) assert_array_equal(find_events(raw, consecutive=True, min_duration=0.002), [[10, 0, 5], [20, 5, 6], [30, 6, 5]]) assert_array_equal( find_events(raw, output='offset', consecutive=False, min_duration=0.002), [[31, 0, 5]]) assert_array_equal( find_events(raw, output='offset', consecutive=True, min_duration=0.002), [[19, 6, 5], [29, 5, 6], [31, 0, 5]]) assert_array_equal(find_events(raw, consecutive=True, min_duration=0.003), [[10, 0, 5], [20, 5, 6]]) # test find_stim_steps merge parameter raw._data[stim_channel_idx, :] = 0 raw._data[stim_channel_idx, 0] = 1 raw._data[stim_channel_idx, 10] = 4 raw._data[stim_channel_idx, 11:20] = 5 assert_array_equal( find_stim_steps(raw, pad_start=0, merge=0, stim_channel=stim_channel), [[0, 0, 1], [1, 1, 0], [10, 0, 4], [11, 4, 5], [20, 5, 0]]) assert_array_equal( find_stim_steps(raw, merge=-1, stim_channel=stim_channel), [[1, 1, 0], [10, 0, 5], [20, 5, 0]]) assert_array_equal( find_stim_steps(raw, merge=1, stim_channel=stim_channel), [[1, 1, 0], [11, 0, 5], [20, 5, 0]]) # put back the env vars we trampled on for s, o in zip(extra_ends, orig_envs): if o is not None: os.environ['MNE_STIM_CHANNEL%s' % s] = o
def test_interpolation(): """Test interpolation""" raw, epochs, epochs_eeg, epochs_meg = _load_data() # It's a trade of between speed and accuracy. If every second channel is # selected the tests are more than 3x faster but the correlation # drops to 0.8 thresh = 0.80 # create good and bad channels for EEG epochs_eeg.info['bads'] = [] goods_idx = np.ones(len(epochs_eeg.ch_names), dtype=bool) goods_idx[epochs_eeg.ch_names.index('EEG 012')] = False bads_idx = ~goods_idx evoked_eeg = epochs_eeg.average() ave_before = evoked_eeg.data[bads_idx] # interpolate bad channels for EEG pos = epochs_eeg._get_channel_positions() pos_good = pos[goods_idx] pos_bad = pos[bads_idx] interpolation = _make_interpolation_matrix(pos_good, pos_bad) assert_equal(interpolation.shape, (1, len(epochs_eeg.ch_names) - 1)) ave_after = np.dot(interpolation, evoked_eeg.data[goods_idx]) epochs_eeg.info['bads'] = ['EEG 012'] evoked_eeg = epochs_eeg.average() assert_array_equal(ave_after, evoked_eeg.interpolate_bads().data[bads_idx]) assert_allclose(ave_before, ave_after, atol=2e-6) # check that interpolation fails when preload is False epochs_eeg.preload = False assert_raises(ValueError, epochs_eeg.interpolate_bads) epochs_eeg.preload = True # check that interpolation changes the data in raw raw_eeg = io.RawArray(data=epochs_eeg._data[0], info=epochs_eeg.info) raw_before = raw_eeg._data[bads_idx] raw_after = raw_eeg.interpolate_bads()._data[bads_idx] assert_equal(np.all(raw_before == raw_after), False) # check that interpolation fails when preload is False for inst in [raw, epochs]: assert hasattr(inst, 'preload') inst.preload = False inst.info['bads'] = [inst.ch_names[1]] assert_raises(ValueError, inst.interpolate_bads) # check that interpolation works for MEG epochs_meg.info['bads'] = ['MEG 0141'] evoked = epochs_meg.average() pick = pick_channels(epochs_meg.info['ch_names'], epochs_meg.info['bads']) # MEG -- raw raw_meg = io.RawArray(data=epochs_meg._data[0], info=epochs_meg.info) raw_meg.info['bads'] = ['MEG 0141'] data1 = raw_meg[pick, :][0][0] # reset_bads=False here because epochs_meg appears to share the same info # dict with raw and we want to test the epochs functionality too data2 = raw_meg.interpolate_bads(reset_bads=False)[pick, :][0][0] assert_true(np.corrcoef(data1, data2)[0, 1] > thresh) # the same number of bads as before assert_true(len(raw_meg.info['bads']) == len(raw_meg.info['bads'])) # MEG -- epochs data1 = epochs_meg.get_data()[:, pick, :].ravel() epochs_meg.interpolate_bads() data2 = epochs_meg.get_data()[:, pick, :].ravel() assert_true(np.corrcoef(data1, data2)[0, 1] > thresh) assert_true(len(raw_meg.info['bads']) == 0) # MEG -- evoked data1 = evoked.data[pick] data2 = evoked.interpolate_bads().data[pick] assert_true(np.corrcoef(data1, data2)[0, 1] > thresh)
title = 'EEG Custom reference' evoked_custom.plot(titles=dict(eeg=title), time_unit='s') evoked_custom.plot_topomap(times=[0.1], size=3., title=title, time_unit='s') ############################################################################### # Evoked response averaged across channels by ROI # ----------------------------------------------- # # It is possible to average channels by region of interest (for example left # and right) when studying the response to this left auditory stimulus. Here we # use our Raw object on which the average reference projection has been added # back. evoked = mne.Epochs(raw, **epochs_params).average() left_idx = mne.pick_channels(evoked.info['ch_names'], ['EEG 017', 'EEG 018', 'EEG 025', 'EEG 026']) right_idx = mne.pick_channels(evoked.info['ch_names'], ['EEG 023', 'EEG 024', 'EEG 034', 'EEG 035']) roi_dict = dict(Left=left_idx, Right=right_idx) evoked_combined = combine_channels(evoked, roi_dict, method='mean') title = 'Evoked response averaged by side' evoked_combined.plot(titles=dict(eeg=title), time_unit='s') ############################################################################### # Evoked arithmetic (e.g. differences) # ------------------------------------ # # Trial subsets from Epochs can be selected using 'tags' separated by '/'. # Evoked objects support basic arithmetic. # First, we create an Epochs object containing 4 conditions.
def test_interpolation_eeg(offset, avg_proj, ctol, atol, method): """Test interpolation of EEG channels.""" raw, epochs_eeg = _load_data('eeg') epochs_eeg = epochs_eeg.copy() assert not _has_eeg_average_ref_proj(epochs_eeg.info['projs']) # Offsetting the coordinate frame should have no effect on the output for inst in (raw, epochs_eeg): for ch in inst.info['chs']: if ch['kind'] == io.constants.FIFF.FIFFV_EEG_CH: ch['loc'][:3] += offset ch['loc'][3:6] += offset for d in inst.info['dig']: d['r'] += offset # check that interpolation does nothing if no bads are marked epochs_eeg.info['bads'] = [] evoked_eeg = epochs_eeg.average() kw = dict(method=method) with pytest.warns(RuntimeWarning, match='Doing nothing'): evoked_eeg.interpolate_bads(**kw) # create good and bad channels for EEG epochs_eeg.info['bads'] = [] goods_idx = np.ones(len(epochs_eeg.ch_names), dtype=bool) goods_idx[epochs_eeg.ch_names.index('EEG 012')] = False bads_idx = ~goods_idx pos = epochs_eeg._get_channel_positions() evoked_eeg = epochs_eeg.average() if avg_proj: evoked_eeg.set_eeg_reference(projection=True).apply_proj() assert_allclose(evoked_eeg.data.mean(0), 0., atol=1e-20) ave_before = evoked_eeg.data[bads_idx] # interpolate bad channels for EEG epochs_eeg.info['bads'] = ['EEG 012'] evoked_eeg = epochs_eeg.average() if avg_proj: evoked_eeg.set_eeg_reference(projection=True).apply_proj() good_picks = pick_types(evoked_eeg.info, meg=False, eeg=True) assert_allclose(evoked_eeg.data[good_picks].mean(0), 0., atol=1e-20) evoked_eeg_bad = evoked_eeg.copy() bads_picks = pick_channels(epochs_eeg.ch_names, include=epochs_eeg.info['bads'], ordered=True) evoked_eeg_bad.data[bads_picks, :] = 1e10 # Test first the exclude parameter evoked_eeg_2_bads = evoked_eeg_bad.copy() evoked_eeg_2_bads.info['bads'] = ['EEG 004', 'EEG 012'] evoked_eeg_2_bads.data[pick_channels(evoked_eeg_bad.ch_names, ['EEG 004', 'EEG 012'])] = 1e10 evoked_eeg_interp = evoked_eeg_2_bads.interpolate_bads(origin=(0., 0., 0.), exclude=['EEG 004'], **kw) assert evoked_eeg_interp.info['bads'] == ['EEG 004'] assert np.all(evoked_eeg_interp.get_data('EEG 004') == 1e10) assert np.all(evoked_eeg_interp.get_data('EEG 012') != 1e10) # Now test without exclude parameter evoked_eeg_bad.info['bads'] = ['EEG 012'] evoked_eeg_interp = evoked_eeg_bad.copy().interpolate_bads(origin=(0., 0., 0.), **kw) if avg_proj: assert_allclose(evoked_eeg_interp.data.mean(0), 0., atol=1e-6) interp_zero = evoked_eeg_interp.data[bads_idx] if method is None: # using pos_good = pos[goods_idx] pos_bad = pos[bads_idx] interpolation = _make_interpolation_matrix(pos_good, pos_bad) assert interpolation.shape == (1, len(epochs_eeg.ch_names) - 1) interp_manual = np.dot(interpolation, evoked_eeg_bad.data[goods_idx]) assert_array_equal(interp_manual, interp_zero) del interp_manual, interpolation, pos, pos_good, pos_bad assert_allclose(ave_before, interp_zero, atol=atol) assert ctol[0] < np.corrcoef(ave_before, interp_zero)[0, 1] < ctol[1] interp_fit = evoked_eeg_bad.copy().interpolate_bads(**kw).data[bads_idx] assert_allclose(ave_before, interp_fit, atol=2.5e-6) assert ctol[1] < np.corrcoef(ave_before, interp_fit)[0, 1] # better # check that interpolation fails when preload is False epochs_eeg.preload = False with pytest.raises(RuntimeError, match='requires epochs data to be loade'): epochs_eeg.interpolate_bads(**kw) epochs_eeg.preload = True # check that interpolation changes the data in raw raw_eeg = io.RawArray(data=epochs_eeg._data[0], info=epochs_eeg.info) raw_before = raw_eeg._data[bads_idx] raw_after = raw_eeg.interpolate_bads(**kw)._data[bads_idx] assert not np.all(raw_before == raw_after) # check that interpolation fails when preload is False for inst in [raw, epochs_eeg]: assert hasattr(inst, 'preload') inst.preload = False inst.info['bads'] = [inst.ch_names[1]] with pytest.raises(RuntimeError, match='requires.*data to be loaded'): inst.interpolate_bads(**kw) # check that interpolation works with few channels raw_few = raw.copy().crop(0, 0.1).load_data() raw_few.pick_channels(raw_few.ch_names[:1] + raw_few.ch_names[3:4]) assert len(raw_few.ch_names) == 2 raw_few.del_proj() raw_few.info['bads'] = [raw_few.ch_names[-1]] orig_data = raw_few[1][0] with _record_warnings() as w: raw_few.interpolate_bads(reset_bads=False, **kw) assert len([ww for ww in w if 'more than' not in str(ww.message)]) == 0 new_data = raw_few[1][0] assert (new_data == 0).mean() < 0.5 assert np.corrcoef(new_data, orig_data)[0, 1] > 0.2
baseline=(None, 0), reject=dict(grad=4000e-13, eog=150e-6)) data = epochs.get_data() times = epochs.times temporal_mask = np.logical_and(0.04 <= times, times <= 0.06) data = np.mean(data[:, :, temporal_mask], axis=2) n_permutations = 50000 T0, p_values, H0 = permutation_t_test(data, n_permutations, n_jobs=2) significant_sensors = picks[p_values <= 0.05] significant_sensors_names = [raw.ch_names[k] for k in significant_sensors] print("Number of significant sensors : %d" % len(significant_sensors)) print("Sensors names : %s" % significant_sensors_names) ############################################################################### # View location of significantly active sensors evoked = mne.EvokedArray(-np.log10(p_values)[:, np.newaxis], epochs.info, tmin=0.) # Extract mask and indices of active sensors in layout stats_picks = mne.pick_channels(evoked.ch_names, significant_sensors_names) mask = p_values[:, np.newaxis] <= 0.05 evoked.plot_topomap(ch_type='grad', times=[0], scale=1, time_format=None, cmap='Reds', vmin=0., vmax=np.max, unit='-log10(p)', format='-%0.1f', mask=mask, size=3, show_names=lambda x: x[4:] + ' ' * 20)
from mne.stats import permutation_cluster_1samp_test as pcluster_test from mne.viz.utils import center_cmap # load and preprocess data #################################################### subject = 1 # use data from subject 1 runs = [6, 10, 14] # use only hand and feet motor imagery runs fnames = eegbci.load_data(subject, runs) raws = [read_raw_edf(f, preload=True) for f in fnames] raw = concatenate_raws(raws) raw.rename_channels(lambda x: x.strip('.')) # remove dots from channel names events, _ = mne.events_from_annotations(raw, event_id=dict(T1=2, T2=3)) picks = mne.pick_channels(raw.info["ch_names"], ["C3", "Cz", "C4"]) # epoch data ################################################################## tmin, tmax = -1, 4 # define epochs around events (in s) event_ids = dict(hands=2, feet=3) # map event IDs to tasks epochs = mne.Epochs(raw, events, event_ids, tmin - 0.5, tmax + 0.5, picks=picks, baseline=None, preload=True) # compute ERDS maps ###########################################################