def _get_data(): """Helper to get some starting data""" # raw with ECG channel raw = Raw(raw_fname).crop(0., 5.0, copy=False).load_data() data_picks = pick_types(raw.info, meg=True, eeg=True) other_picks = pick_types(raw.info, meg=False, stim=True, eog=True) picks = np.sort(np.concatenate((data_picks[::16], other_picks))) raw = raw.pick_channels([raw.ch_names[p] for p in picks]) raw.info.normalize_proj() ecg = RawArray(np.zeros((1, len(raw.times))), create_info(['ECG 063'], raw.info['sfreq'], 'ecg')) for key in ('dev_head_t', 'buffer_size_sec', 'highpass', 'lowpass', 'filename', 'dig'): ecg.info[key] = raw.info[key] raw.add_channels([ecg]) src = read_source_spaces(src_fname) trans = read_trans(trans_fname) sphere = make_sphere_model('auto', 'auto', raw.info) stc = _make_stc(raw, src) return raw, src, stc, trans, sphere
def test_raw_index_as_time(): """ Test index as time conversion""" raw = Raw(fif_fname, preload=True) t0 = raw.index_as_time([0], True)[0] t1 = raw.index_as_time([100], False)[0] t2 = raw.index_as_time([100], True)[0] assert_true((t2 - t1) == t0) # ensure we can go back and forth t3 = raw.index_as_time(raw.time_as_index([0], True), True) assert_array_almost_equal(t3, [0.0], 2) t3 = raw.index_as_time(raw.time_as_index(raw.info['sfreq'], True), True) assert_array_almost_equal(t3, [raw.info['sfreq']], 2) t3 = raw.index_as_time(raw.time_as_index(raw.info['sfreq'], False), False) assert_array_almost_equal(t3, [raw.info['sfreq']], 2) i0 = raw.time_as_index(raw.index_as_time([0], True), True) assert_true(i0[0] == 0) i1 = raw.time_as_index(raw.index_as_time([100], True), True) assert_true(i1[0] == 100) # Have to add small amount of time because we truncate via int casting i1 = raw.time_as_index(raw.index_as_time([100.0001], False), False) assert_true(i1[0] == 100)
def test_brainvision_data(): """Test reading raw Brain Vision files """ assert_raises(TypeError, read_raw_brainvision, vhdr_path, montage, preload=True, scale="0") raw_py = read_raw_brainvision(vhdr_path, montage, eog=eog, preload=True) assert_equal(raw_py.info['highpass'], 0.) assert_equal(raw_py.info['lowpass'], 250.) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads') data_py, times_py = raw_py[picks] print(raw_py) # to test repr print(raw_py.info) # to test Info repr # compare with a file that was generated using MNE-C raw_bin = Raw(eeg_bin, preload=True) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads') data_bin, times_bin = raw_bin[picks] assert_array_almost_equal(data_py, data_bin) assert_array_almost_equal(times_py, times_bin) # Make sure EOG channels are marked correctly raw_py = read_raw_brainvision(vhdr_path, montage, eog=eog, preload=True) for ch in raw_py.info['chs']: if ch['ch_name'] in eog: assert_equal(ch['kind'], FIFF.FIFFV_EOG_CH) elif ch['ch_name'] == 'STI 014': assert_equal(ch['kind'], FIFF.FIFFV_STIM_CH) elif ch['ch_name'] in raw_py.info['ch_names']: assert_equal(ch['kind'], FIFF.FIFFV_EEG_CH) else: raise RuntimeError("Unknown Channel: %s" % ch['ch_name']) # Make sure concatenation works raw_concat = concatenate_raws([raw_py.copy(), raw_py]) assert_equal(raw_concat.n_times, 2 * raw_py.n_times)
def test_simulate_raw_chpi(): """Test simulation of raw data with cHPI""" with warnings.catch_warnings(record=True): # MaxShield raw = Raw(raw_chpi_fname, allow_maxshield=True) sphere = make_sphere_model('auto', 'auto', raw.info) # make sparse spherical source space sphere_vol = tuple(sphere['r0'] * 1000.) + (sphere.radius * 1000., ) src = setup_volume_source_space('sample', sphere=sphere_vol, pos=70.) stc = _make_stc(raw, src) # simulate data with cHPI on raw_sim = simulate_raw(raw, stc, None, src, sphere, cov=None, chpi=False) # need to trim extra samples off this one raw_chpi = simulate_raw(raw, stc, None, src, sphere, cov=None, chpi=True, head_pos=pos_fname) # test cHPI indication hpi_freqs, _, hpi_pick, hpi_on, _ = _get_hpi_info(raw.info) assert_allclose(raw_sim[hpi_pick][0], 0.) assert_allclose(raw_chpi[hpi_pick][0], hpi_on) # test that the cHPI signals make some reasonable values psd_sim, freqs_sim = compute_raw_psd(raw_sim) psd_chpi, freqs_chpi = compute_raw_psd(raw_chpi) assert_array_equal(freqs_sim, freqs_chpi) freq_idx = np.sort([np.argmin(np.abs(freqs_sim - f)) for f in hpi_freqs]) picks_meg = pick_types(raw.info, meg=True, eeg=False) picks_eeg = pick_types(raw.info, meg=False, eeg=True) assert_allclose(psd_sim[picks_eeg], psd_chpi[picks_eeg], atol=1e-20) assert_true((psd_chpi[picks_meg][:, freq_idx] > 100 * psd_sim[picks_meg][:, freq_idx]).all()) # test localization based on cHPI information trans_sim, rot_sim, t_sim = _calculate_chpi_positions(raw_chpi) trans, rot, t = get_chpi_positions(pos_fname) t -= raw.first_samp / raw.info['sfreq'] _compare_positions((trans, rot, t), (trans_sim, rot_sim, t_sim), max_dist=0.005)
def test_simulate_evoked(): """ Test simulation of evoked data """ raw = Raw(raw_fname) fwd = read_forward_solution(fwd_fname, force_fixed=True) fwd = pick_types_forward(fwd, meg=True, eeg=True, exclude=raw.info['bads']) cov = read_cov(cov_fname) evoked_template = read_evokeds(ave_fname, condition=0, baseline=None) evoked_template.pick_types(meg=True, eeg=True, exclude=raw.info['bads']) snr = 6 # dB tmin = -0.1 sfreq = 1000. # Hz tstep = 1. / sfreq n_samples = 600 times = np.linspace(tmin, tmin + n_samples * tstep, n_samples) # Generate times series for 2 dipoles stc = simulate_sparse_stc(fwd['src'], n_dipoles=2, times=times) stc._data *= 1e-9 # Generate noisy evoked data iir_filter = [1, -0.9] evoked = simulate_evoked(fwd, stc, evoked_template.info, cov, snr, tmin=0.0, tmax=0.2, iir_filter=iir_filter) assert_array_almost_equal(evoked.times, stc.times) assert_true(len(evoked.data) == len(fwd['sol']['data'])) # make a vertex that doesn't exist in fwd, should throw error stc_bad = stc.copy() mv = np.max(fwd['src'][0]['vertno'][fwd['src'][0]['inuse']]) stc_bad.vertices[0][0] = mv + 1 assert_raises(RuntimeError, simulate_evoked, fwd, stc_bad, evoked_template.info, cov, snr, tmin=0.0, tmax=0.2) evoked_1 = simulate_evoked(fwd, stc, evoked_template.info, cov, np.inf, tmin=0.0, tmax=0.2) evoked_2 = simulate_evoked(fwd, stc, evoked_template.info, cov, np.inf, tmin=0.0, tmax=0.2) assert_array_equal(evoked_1.data, evoked_2.data)
def test_ch_loc(): """Test raw kit loc """ raw_py = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path, stim='<') raw_bin = Raw(op.join(data_dir, 'test_bin_raw.fif')) ch_py = raw_py._raw_extras[0]['sensor_locs'][:, :5] # ch locs stored as m, not mm ch_py[:, :3] *= 1e3 ch_sns = read_sns(op.join(data_dir, 'sns.txt')) assert_array_almost_equal(ch_py, ch_sns, 2) assert_array_almost_equal(raw_py.info['dev_head_t']['trans'], raw_bin.info['dev_head_t']['trans'], 4) for py_ch, bin_ch in zip(raw_py.info['chs'], raw_bin.info['chs']): if bin_ch['ch_name'].startswith('MEG'): # the stored ch locs have more precision than the sns.txt assert_array_almost_equal(py_ch['loc'], bin_ch['loc'], decimal=2) # test when more than one marker file provided mrks = [mrk_path, mrk2_path, mrk3_path] read_raw_kit(sqd_path, mrks, elp_path, hsp_path, preload=False)
def test_compute_proj_ecg(): """Test computation of ECG SSP projectors""" raw = Raw(raw_fname).crop(0, 10, copy=False) raw.load_data() for average in [False, True]: # For speed, let's not filter here (must also not reject then) projs, events = compute_proj_ecg(raw, n_mag=2, n_grad=2, n_eeg=2, ch_name='MEG 1531', bads=['MEG 2443'], average=average, avg_ref=True, no_proj=True, l_freq=None, h_freq=None, reject=None, tmax=dur_use, qrs_threshold=0.5) assert_true(len(projs) == 7) # heart rate at least 0.5 Hz, but less than 3 Hz assert_true(events.shape[0] > 0.5 * dur_use and events.shape[0] < 3 * dur_use) ssp_ecg = [proj for proj in projs if proj['desc'].startswith('ECG')] # check that the first principal component have a certain minimum ssp_ecg = [proj for proj in ssp_ecg if 'PCA-01' in proj['desc']] thresh_eeg, thresh_axial, thresh_planar = .9, .3, .1 for proj in ssp_ecg: if 'planar' in proj['desc']: assert_true(proj['explained_var'] > thresh_planar) elif 'axial' in proj['desc']: assert_true(proj['explained_var'] > thresh_axial) elif 'eeg' in proj['desc']: assert_true(proj['explained_var'] > thresh_eeg) # XXX: better tests # without setting a bad channel, this should throw a warning with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') projs, events = compute_proj_ecg(raw, n_mag=2, n_grad=2, n_eeg=2, ch_name='MEG 1531', bads=[], average=average, avg_ref=True, no_proj=True, l_freq=None, h_freq=None, tmax=dur_use) assert_equal(len(w), 1) assert_equal(projs, None)
def test_compute_proj_eog(): """Test computation of EOG SSP projectors""" raw = Raw(raw_fname).crop(0, 10, False) raw.preload_data() for average in [False, True]: n_projs_init = len(raw.info['projs']) projs, events = compute_proj_eog(raw, n_mag=2, n_grad=2, n_eeg=2, bads=['MEG 2443'], average=average, avg_ref=True, no_proj=False, l_freq=None, h_freq=None, reject=None, tmax=dur_use) assert_true(len(projs) == (7 + n_projs_init)) assert_true( np.abs(events.shape[0] - np.sum(np.less(eog_times, dur_use))) <= 1) # XXX: better tests # This will throw a warning b/c simplefilter('always') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') projs, events = compute_proj_eog(raw, n_mag=2, n_grad=2, n_eeg=2, average=average, bads=[], avg_ref=True, no_proj=False, l_freq=None, h_freq=None, tmax=dur_use) assert_equal(len(w), 1) assert_equal(projs, None)
def _is_file_unfixed(fname, anon=None): """Determine if a file needs reordering or anonymization""" order = np.array([ 1, 2, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 30, 31, 32, 33, 34, 35, 36, 37, 38, 41, 42, 43, 44, 45, 46, 47, 48, 49, 51, 52, 54, 55, 56, 57, 58, 60, 39, 29, 18, 4, 8, 28, 40, 59, 50, 53 ]) - 1 assert len(order) == 60 write_key = 'LABSN_EEG_REORDER:' + ','.join([str(o) for o in order]) anon_key = '' if anon is None else ';anonymized' with warnings.catch_warnings(record=True): raw = Raw(fname, preload=False, allow_maxshield=True) picks = pick_types(raw.info, meg=False, eeg=True, exclude=[]) if len(picks) == 0: return False, False, None, None, None, None if not len(picks) == len(order): raise RuntimeError('Incorrect number of EEG channels (%i) found ' 'in %s' % (len(picks), op.basename(fname))) need_reorder = (write_key not in raw.info['description']) need_anon = (anon_key not in raw.info['description']) return need_reorder, need_anon, write_key, anon_key, picks, order
def test_ica_twice(): """Test running ICA twice""" raw = Raw(raw_fname).crop(1.5, stop, copy=False) raw.load_data() picks = pick_types(raw.info, meg='grad', exclude='bads') n_components = 0.9 max_pca_components = None n_pca_components = 1.1 with warnings.catch_warnings(record=True): ica1 = ICA(n_components=n_components, max_pca_components=max_pca_components, n_pca_components=n_pca_components, random_state=0) ica1.fit(raw, picks=picks, decim=3) raw_new = ica1.apply(raw, n_pca_components=n_pca_components) ica2 = ICA(n_components=n_components, max_pca_components=max_pca_components, n_pca_components=1.0, random_state=0) ica2.fit(raw_new, picks=picks, decim=3) assert_equal(ica1.n_components_, ica2.n_components_)
def test_compensation_raw_mne(): """Test Raw compensation by comparing with MNE """ tempdir = _TempDir() def compensate_mne(fname, grad): tmp_fname = op.join(tempdir, 'mne_ctf_test_raw.fif') cmd = ['mne_process_raw', '--raw', fname, '--save', tmp_fname, '--grad', str(grad), '--projoff', '--filteroff'] run_subprocess(cmd) return Raw(tmp_fname, preload=True) for grad in [0, 2, 3]: raw_py = Raw(ctf_comp_fname, preload=True, compensation=grad) raw_c = compensate_mne(ctf_comp_fname, grad) assert_allclose(raw_py._data, raw_c._data, rtol=1e-6, atol=1e-17) assert_equal(raw_py.info['nchan'], raw_c.info['nchan']) for ch_py, ch_c in zip(raw_py.info['chs'], raw_c.info['chs']): for key in ('ch_name', 'coil_type', 'scanno', 'logno', 'unit', 'coord_frame', 'kind'): assert_equal(ch_py[key], ch_c[key]) for key in ('loc', 'unit_mul', 'range', 'cal'): assert_allclose(ch_py[key], ch_c[key])
def test_compute_proj_parallel(): """Test computation of ExG projectors using parallelization""" raw_0 = Raw(raw_fname).crop(0, 10, copy=False) raw_0.load_data() raw = raw_0.copy() projs, _ = compute_proj_eog(raw, n_mag=2, n_grad=2, n_eeg=2, bads=['MEG 2443'], average=False, avg_ref=True, no_proj=False, n_jobs=1, l_freq=None, h_freq=None, reject=None, tmax=dur_use) raw_2 = raw_0.copy() projs_2, _ = compute_proj_eog(raw_2, n_mag=2, n_grad=2, n_eeg=2, bads=['MEG 2443'], average=False, avg_ref=True, no_proj=False, n_jobs=2, l_freq=None, h_freq=None, reject=None, tmax=dur_use) projs = activate_proj(projs) projs_2 = activate_proj(projs_2) projs, _, _ = make_projector(projs, raw_2.info['ch_names'], bads=['MEG 2443']) projs_2, _, _ = make_projector(projs_2, raw_2.info['ch_names'], bads=['MEG 2443']) assert_array_almost_equal(projs, projs_2, 10)
def test_ica_reject_buffer(): """Test ICA data raw buffer rejection""" raw = Raw(raw_fname).crop(1.5, stop, copy=False) raw.load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw._data[2, 1000:1005] = 5e-12 with catch_logging() as drop_log: with warnings.catch_warnings(record=True): ica.fit(raw, picks[:5], reject=dict(mag=2.5e-12), decim=2, tstep=0.01, verbose=True) assert_true(raw._data[:5, ::2].shape[1] - 4 == ica.n_samples_) log = [l for l in drop_log.getvalue().split('\n') if 'detected' in l] assert_equal(len(log), 1)
def test_plot_mne_epochs(): "Test plotting epochs from the mne sample dataset" # find paths data_path = mne.datasets.sample.data_path() raw_path = os.path.join(data_path, 'MEG', 'sample', 'sample_audvis_filt-0-40_raw.fif') events_path = os.path.join(data_path, 'MEG', 'sample', 'sample_audvis_filt-0-40_raw-eve.fif') # read epochs raw = Raw(raw_path) events = mne.read_events(events_path) idx = np.logical_or(events[:, 2] == 5, events[:, 2] == 32) events = events[idx] epochs = mne.Epochs(raw, events, None, -0.1, 0.3) # grand average p = plot.Array(epochs) p.close() # with model p = plot.Array(epochs, events[:, 2]) p.close()
def test_pick_seeg(): """Test picking with SEEG """ names = 'A1 A2 Fz O OTp1 OTp2 OTp3'.split() types = 'mag mag eeg eeg seeg seeg seeg'.split() info = create_info(names, 1024., types) idx = channel_indices_by_type(info) assert_array_equal(idx['mag'], [0, 1]) assert_array_equal(idx['eeg'], [2, 3]) assert_array_equal(idx['seeg'], [4, 5, 6]) assert_array_equal(pick_types(info, meg=False, seeg=True), [4, 5, 6]) for i, t in enumerate(types): assert_equal(channel_type(info, i), types[i]) raw = RawArray(np.zeros((len(names), 10)), info) events = np.array([[1, 0, 0], [2, 0, 0]]) epochs = Epochs(raw, events, {'event': 0}, -1e-5, 1e-5) evoked = epochs.average(pick_types(epochs.info, meg=True, seeg=True)) e_seeg = evoked.pick_types(meg=False, seeg=True, copy=True) for l, r in zip(e_seeg.ch_names, names[4:]): assert_equal(l, r) # Deal with constant debacle raw = Raw(fname_mc) assert_equal(len(pick_types(raw.info, meg=False, seeg=True)), 0)
def test_fix_types(): """Test fixing of channel types """ for fname, change in ((hp_fif_fname, True), (test_fif_fname, False), (ctf_fname, False)): raw = Raw(fname) mag_picks = pick_types(raw.info, meg='mag') other_picks = np.setdiff1d(np.arange(len(raw.ch_names)), mag_picks) # we don't actually have any files suffering from this problem, so # fake it if change: for ii in mag_picks: raw.info['chs'][ii]['coil_type'] = FIFF.FIFFV_COIL_VV_MAG_T2 orig_types = np.array([ch['coil_type'] for ch in raw.info['chs']]) raw.fix_mag_coil_types() new_types = np.array([ch['coil_type'] for ch in raw.info['chs']]) if not change: assert_array_equal(orig_types, new_types) else: assert_array_equal(orig_types[other_picks], new_types[other_picks]) assert_true((orig_types[mag_picks] != new_types[mag_picks]).all()) assert_true( (new_types[mag_picks] == FIFF.FIFFV_COIL_VV_MAG_T3).all())
def test_apply_mne_inverse_fixed_raw(): """Test MNE with fixed-orientation inverse operator on Raw """ raw = Raw(fname_raw) start = 3 stop = 10 _, times = raw[0, start:stop] label_lh = read_label(fname_label % 'Aud-lh') # create a fixed-orientation inverse operator fwd = read_forward_solution_meg(fname_fwd, force_fixed=False, surf_ori=True) noise_cov = read_cov(fname_cov) inv_op = make_inverse_operator(raw.info, fwd, noise_cov, loose=None, depth=0.8, fixed=True) inv_op2 = prepare_inverse_operator(inv_op, nave=1, lambda2=lambda2, method="dSPM") stc = apply_inverse_raw(raw, inv_op2, lambda2, "dSPM", label=label_lh, start=start, stop=stop, nave=1, pick_ori=None, buffer_size=None, prepared=True) stc2 = apply_inverse_raw(raw, inv_op2, lambda2, "dSPM", label=label_lh, start=start, stop=stop, nave=1, pick_ori=None, buffer_size=3, prepared=True) stc3 = apply_inverse_raw(raw, inv_op, lambda2, "dSPM", label=label_lh, start=start, stop=stop, nave=1, pick_ori=None, buffer_size=None) assert_true(stc.subject == 'sample') assert_true(stc2.subject == 'sample') assert_array_almost_equal(stc.times, times) assert_array_almost_equal(stc2.times, times) assert_array_almost_equal(stc3.times, times) assert_array_almost_equal(stc.data, stc2.data) assert_array_almost_equal(stc.data, stc3.data)
def test_compute_proj_eog(): """Test computation of EOG SSP projectors""" raw = Raw(raw_fname).crop(0, 10, copy=False) raw.load_data() for average in [False, True]: n_projs_init = len(raw.info['projs']) projs, events = compute_proj_eog(raw, n_mag=2, n_grad=2, n_eeg=2, bads=['MEG 2443'], average=average, avg_ref=True, no_proj=False, l_freq=None, h_freq=None, reject=None, tmax=dur_use) assert_true(len(projs) == (7 + n_projs_init)) assert_true(np.abs(events.shape[0] - np.sum(np.less(eog_times, dur_use))) <= 1) ssp_eog = [proj for proj in projs if proj['desc'].startswith('EOG')] # check that the first principal component have a certain minimum ssp_eog = [proj for proj in ssp_eog if 'PCA-01' in proj['desc']] thresh_eeg, thresh_axial, thresh_planar = .9, .3, .1 for proj in ssp_eog: if 'planar' in proj['desc']: assert_true(proj['explained_var'] > thresh_planar) elif 'axial' in proj['desc']: assert_true(proj['explained_var'] > thresh_axial) elif 'eeg' in proj['desc']: assert_true(proj['explained_var'] > thresh_eeg) # XXX: better tests # This will throw a warning b/c simplefilter('always') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') projs, events = compute_proj_eog(raw, n_mag=2, n_grad=2, n_eeg=2, average=average, bads=[], avg_ref=True, no_proj=False, l_freq=None, h_freq=None, tmax=dur_use) assert_equal(len(w), 1) assert_equal(projs, None)
def test_clean_info_bads(): """Test cleaning info['bads'] when bad_channels are excluded """ raw_file = op.join(op.dirname(__file__), 'io', 'tests', 'data', 'test_raw.fif') raw = Raw(raw_file) # select eeg channels picks_eeg = pick_types(raw.info, meg=False, eeg=True) # select 3 eeg channels as bads idx_eeg_bad_ch = picks_eeg[[1, 5, 14]] eeg_bad_ch = [raw.info['ch_names'][k] for k in idx_eeg_bad_ch] # select meg channels picks_meg = pick_types(raw.info, meg=True, eeg=False) # select randomly 3 meg channels as bads idx_meg_bad_ch = picks_meg[[0, 15, 34]] meg_bad_ch = [raw.info['ch_names'][k] for k in idx_meg_bad_ch] # simulate the bad channels raw.info['bads'] = eeg_bad_ch + meg_bad_ch # simulate the call to pick_info excluding the bad eeg channels info_eeg = pick_info(raw.info, picks_eeg) # simulate the call to pick_info excluding the bad meg channels info_meg = pick_info(raw.info, picks_meg) assert_equal(info_eeg['bads'], eeg_bad_ch) assert_equal(info_meg['bads'], meg_bad_ch) info = pick_info(raw.info, picks_meg) info._check_consistency() info['bads'] += ['EEG 053'] assert_raises(RuntimeError, info._check_consistency)
def create_epochs(fif_file, ep_length): """Split raw .fif file into epochs. Splitted epochs have a length ep_length with rejection criteria. """ import os from mne.io import Raw from mne import Epochs from mne import pick_types from nipype.utils.filemanip import split_filename as split_f # flat = dict(mag=0.1e-12, grad=1e-13) # reject = dict(mag=6e-12, grad=25e-11) flat = None reject = None raw = Raw(fif_file) picks = pick_types(raw.info, ref_meg=False, eeg=False) if raw.times[-1] >= ep_length: events = create_events(raw, ep_length) else: raise Exception('File {} is too short!'.format(fif_file)) epochs = Epochs(raw, events=events, tmin=0, tmax=ep_length, preload=True, picks=picks, proj=False, flat=flat, reject=reject) _, base, ext = split_f(fif_file) savename = os.path.abspath(base + '-epo' + ext) epochs.save(savename) return savename
def test_ica_reset(): """Test ICA resetting""" raw = Raw(raw_fname).crop(0.5, stop, copy=False) raw.load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:10] run_time_attrs = ('_pre_whitener', 'unmixing_matrix_', 'mixing_matrix_', 'n_components_', 'n_samples_', 'pca_components_', 'pca_explained_variance_', 'pca_mean_') with warnings.catch_warnings(record=True): ica = ICA(n_components=3, max_pca_components=3, n_pca_components=3, method='fastica', max_iter=1).fit(raw, picks=picks) assert_true(all(hasattr(ica, attr) for attr in run_time_attrs)) ica._reset() assert_true(not any(hasattr(ica, attr) for attr in run_time_attrs))
def test_write_annotations(): """Test writing raw files when annotations were parsed. """ tempdir = _TempDir() with warnings.catch_warnings(record=True): # tal_channel dep warnings.simplefilter('always') raw1 = read_raw_edf(edf_path, tal_channel=-1, preload=True) raw1_file = op.join(tempdir, 'test1-raw.fif') raw1.save(raw1_file, overwrite=True, buffer_size_sec=1) raw11 = Raw(raw1_file, preload=True) data1, times1 = raw1[:, :] data11, times11 = raw11[:, :] assert_array_almost_equal(data1, data11) assert_array_almost_equal(times1, times11) assert_equal(sorted(raw1.info.keys()), sorted(raw11.info.keys())) with warnings.catch_warnings(record=True): # tal_channel dep warnings.simplefilter('always') assert_raises(RuntimeError, read_raw_edf, edf_path, tal_channel=-1, preload=False)
def generate_data_for_comparing_against_eeglab_infomax(ch_type, random_state): data_dir = op.join(testing.data_path(download=False), 'MEG', 'sample') raw_fname = op.join(data_dir, 'sample_audvis_trunc_raw.fif') raw = Raw(raw_fname, preload=True) if ch_type == 'eeg': picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') else: picks = pick_types(raw.info, meg=ch_type, eeg=False, exclude='bads') # select a small number of channels for the test number_of_channels_to_use = 5 idx_perm = random_permutation(picks.shape[0], random_state) picks = picks[idx_perm[:number_of_channels_to_use]] raw.filter(1, 45, n_jobs=2) X = raw[picks, :][0][:, ::20] # Subtract the mean mean_X = X.mean(axis=1) X -= mean_X[:, None] # pre_whitening: z-score X /= np.std(X) T = X.shape[1] cov_X = np.dot(X, X.T) / T # Let's whiten the data U, D, _ = svd(cov_X) W = np.dot(U, U.T / np.sqrt(D)[:, None]) Y = np.dot(W, X) return Y
def test_make_eeg_layout(): """Test creation of EEG layout""" tempdir = _TempDir() tmp_name = 'foo' lout_name = 'test_raw' lout_orig = read_layout(kind=lout_name, path=lout_path) info = Raw(fif_fname).info info['bads'].append(info['ch_names'][360]) layout = make_eeg_layout(info, exclude=[]) assert_array_equal(len(layout.names), len([ch for ch in info['ch_names'] if ch.startswith('EE')])) layout.save(op.join(tempdir, tmp_name + '.lout')) lout_new = read_layout(kind=tmp_name, path=tempdir, scale=False) assert_array_equal(lout_new.kind, tmp_name) assert_allclose(layout.pos, lout_new.pos, atol=0.1) assert_array_equal(lout_orig.names, lout_new.names) # Test input validation assert_raises(ValueError, make_eeg_layout, info, radius=-0.1) assert_raises(ValueError, make_eeg_layout, info, radius=0.6) assert_raises(ValueError, make_eeg_layout, info, width=-0.1) assert_raises(ValueError, make_eeg_layout, info, width=1.1) assert_raises(ValueError, make_eeg_layout, info, height=-0.1) assert_raises(ValueError, make_eeg_layout, info, height=1.1)
def test_set_eeg_reference(): """Test rereference eeg data""" raw = Raw(fif_fname, preload=True) raw.info['projs'] = [] # Test setting an average reference assert_true(not _has_eeg_average_ref_proj(raw.info['projs'])) reref, ref_data = set_eeg_reference(raw) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_true(ref_data is None) # Test setting an average reference when one was already present reref, ref_data = set_eeg_reference(raw, copy=False) assert_true(ref_data is None) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref)
def test_pick_seeg_ecog(): """Test picking with sEEG and ECoG """ names = 'A1 A2 Fz O OTp1 OTp2 E1 OTp3 E2 E3'.split() types = 'mag mag eeg eeg seeg seeg ecog seeg ecog ecog'.split() info = create_info(names, 1024., types) idx = channel_indices_by_type(info) assert_array_equal(idx['mag'], [0, 1]) assert_array_equal(idx['eeg'], [2, 3]) assert_array_equal(idx['seeg'], [4, 5, 7]) assert_array_equal(idx['ecog'], [6, 8, 9]) assert_array_equal(pick_types(info, meg=False, seeg=True), [4, 5, 7]) for i, t in enumerate(types): assert_equal(channel_type(info, i), types[i]) raw = RawArray(np.zeros((len(names), 10)), info) events = np.array([[1, 0, 0], [2, 0, 0]]) epochs = Epochs(raw, events, {'event': 0}, -1e-5, 1e-5) evoked = epochs.average(pick_types(epochs.info, meg=True, seeg=True)) e_seeg = evoked.copy().pick_types(meg=False, seeg=True) for l, r in zip(e_seeg.ch_names, [names[4], names[5], names[7]]): assert_equal(l, r) # Deal with constant debacle raw = Raw(op.join(io_dir, 'tests', 'data', 'test_chpi_raw_sss.fif')) assert_equal(len(pick_types(raw.info, meg=False, seeg=True, ecog=True)), 0)
def test_min_distance_fit_dipole(): """Test dipole min_dist to inner_skull""" subject = 'sample' raw = Raw(fname_raw, preload=True) # select eeg data picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') info = pick_info(raw.info, picks) # Let's use cov = Identity cov = read_cov(fname_cov) cov['data'] = np.eye(cov['data'].shape[0]) # Simulated scal map simulated_scalp_map = np.zeros(picks.shape[0]) simulated_scalp_map[27:34] = 1 simulated_scalp_map = simulated_scalp_map[:, None] evoked = EvokedArray(simulated_scalp_map, info, tmin=0) min_dist = 5. # distance in mm dip, residual = fit_dipole(evoked, cov, fname_bem, fname_trans, min_dist=min_dist) dist = _compute_depth(dip, fname_bem, fname_trans, subject, subjects_dir) # Constraints are not exact, so bump the minimum slightly assert_true(min_dist - 0.1 < (dist[0] * 1000.) < (min_dist + 1.)) assert_raises(ValueError, fit_dipole, evoked, cov, fname_bem, fname_trans, -1.)
def test_set_eeg_reference(): """Test rereference eeg data""" raw = Raw(fif_fname, preload=True) raw.info['projs'] = [] # Test setting an average reference assert_true(not _has_eeg_average_ref_proj(raw.info['projs'])) reref, ref_data = set_eeg_reference(raw) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_true(ref_data is None) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref) # Setting an average reference on a dataset that already contains a custom # reference should fail assert_raises(RuntimeError, set_eeg_reference, reref)
def test_apply_mne_inverse_epochs(): """Test MNE with precomputed inverse operator on Epochs """ inverse_operator = read_inverse_operator(fname_inv) label_lh = read_label(fname_label % 'Aud-lh') label_rh = read_label(fname_label % 'Aud-rh') event_id, tmin, tmax = 1, -0.2, 0.5 raw = Raw(fname_raw) picks = pick_types(raw.info, meg=True, eeg=False, stim=True, ecg=True, eog=True, include=['STI 014'], exclude='bads') reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6) flat = dict(grad=1e-15, mag=1e-15) events = read_events(fname_event)[:15] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject, flat=flat) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_lh, pick_ori="normal") assert_true(len(stcs) == 4) assert_true(3 < stcs[0].data.max() < 10) assert_true(stcs[0].subject == 'sample') data = sum(stc.data for stc in stcs) / len(stcs) flip = label_sign_flip(label_lh, inverse_operator['src']) label_mean = np.mean(data, axis=0) label_mean_flip = np.mean(flip[:, np.newaxis] * data, axis=0) assert_true(label_mean.max() < label_mean_flip.max()) # test extracting a BiHemiLabel stcs_rh = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_rh, pick_ori="normal") stcs_bh = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_lh + label_rh, pick_ori="normal") n_lh = len(stcs[0].data) assert_array_almost_equal(stcs[0].data, stcs_bh[0].data[:n_lh]) assert_array_almost_equal(stcs_rh[0].data, stcs_bh[0].data[n_lh:]) # test without using a label (so delayed computation is used) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", pick_ori="normal") assert_true(stcs[0].subject == 'sample') label_stc = stcs[0].in_label(label_rh) assert_true(label_stc.subject == 'sample') assert_array_almost_equal(stcs_rh[0].data, label_stc.data)
def test_ica_additional(): """Test additional ICA functionality""" tempdir = _TempDir() stop2 = 500 raw = Raw(raw_fname).crop(1.5, stop, False) raw.load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') test_cov = read_cov(test_cov_name) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) # test if n_components=None works with warnings.catch_warnings(record=True): ica = ICA(n_components=None, max_pca_components=None, n_pca_components=None, random_state=0) ica.fit(epochs, picks=picks, decim=3) # for testing eog functionality picks2 = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=True, exclude='bads') epochs_eog = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks2, baseline=(None, 0), preload=True) test_cov2 = test_cov.copy() ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4, n_pca_components=4) assert_true(ica.info is None) with warnings.catch_warnings(record=True): ica.fit(raw, picks[:5]) assert_true(isinstance(ica.info, Info)) assert_true(ica.n_components_ < 5) ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) assert_raises(RuntimeError, ica.save, '') with warnings.catch_warnings(record=True): ica.fit(raw, picks=[1, 2, 3, 4, 5], start=start, stop=stop2) # test corrmap ica2 = ica.copy() corrmap([ica, ica2], (0, 0), threshold='auto', label='blinks', plot=True, ch_type="mag") corrmap([ica, ica2], (0, 0), threshold=2, plot=False, show=False) assert_true(ica.labels_["blinks"] == ica2.labels_["blinks"]) assert_true(0 in ica.labels_["blinks"]) plt.close('all') # test warnings on bad filenames with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ica_badname = op.join(op.dirname(tempdir), 'test-bad-name.fif.gz') ica.save(ica_badname) read_ica(ica_badname) assert_naming(w, 'test_ica.py', 2) # test decim ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw_ = raw.copy() for _ in range(3): raw_.append(raw_) n_samples = raw_._data.shape[1] with warnings.catch_warnings(record=True): ica.fit(raw, picks=None, decim=3) assert_true(raw_._data.shape[1], n_samples) # test expl var ica = ICA(n_components=1.0, max_pca_components=4, n_pca_components=4) with warnings.catch_warnings(record=True): ica.fit(raw, picks=None, decim=3) assert_true(ica.n_components_ == 4) # epochs extraction from raw fit assert_raises(RuntimeError, ica.get_sources, epochs) # test reading and writing test_ica_fname = op.join(op.dirname(tempdir), 'test-ica.fif') for cov in (None, test_cov): ica = ICA(noise_cov=cov, n_components=2, max_pca_components=4, n_pca_components=4) with warnings.catch_warnings(record=True): # ICA does not converge ica.fit(raw, picks=picks, start=start, stop=stop2) sources = ica.get_sources(epochs).get_data() assert_true(ica.mixing_matrix_.shape == (2, 2)) assert_true(ica.unmixing_matrix_.shape == (2, 2)) assert_true(ica.pca_components_.shape == (4, len(picks))) assert_true(sources.shape[1] == ica.n_components_) for exclude in [[], [0]]: ica.exclude = [0] ica.labels_ = {'foo': [0]} ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert_true(ica.exclude == ica_read.exclude) assert_equal(ica.labels_, ica_read.labels_) ica.exclude = [] ica.apply(raw, exclude=[1]) assert_true(ica.exclude == []) ica.exclude = [0, 1] ica.apply(raw, exclude=[1]) assert_true(ica.exclude == [0, 1]) ica_raw = ica.get_sources(raw) assert_true(ica.exclude == [ica_raw.ch_names.index(e) for e in ica_raw.info['bads']]) # test filtering d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.filter(4, 20) assert_true((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.notch_filter([10]) assert_true((d1 != ica_raw._data[0]).any()) ica.n_pca_components = 2 ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert_true(ica.n_pca_components == ica_read.n_pca_components) # check type consistency attrs = ('mixing_matrix_ unmixing_matrix_ pca_components_ ' 'pca_explained_variance_ _pre_whitener') def f(x, y): return getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) ica.n_pca_components = 4 ica_read.n_pca_components = 4 ica.exclude = [] ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) for attr in ['mixing_matrix_', 'unmixing_matrix_', 'pca_components_', 'pca_mean_', 'pca_explained_variance_', '_pre_whitener']: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert_true(ica.ch_names == ica_read.ch_names) assert_true(isinstance(ica_read.info, Info)) sources = ica.get_sources(raw)[:, :][0] sources2 = ica_read.get_sources(raw)[:, :][0] assert_array_almost_equal(sources, sources2) _raw1 = ica.apply(raw, exclude=[1]) _raw2 = ica_read.apply(raw, exclude=[1]) assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0]) os.remove(test_ica_fname) # check scrore funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(raw, target='EOG 061', score_func=func, start=0, stop=10) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(raw, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.score_sources, raw, target=np.arange(1)) params = [] params += [(None, -1, slice(2), [0, 1])] # varicance, kurtosis idx params params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) with warnings.catch_warnings(record=True): idx, scores = ica.find_bads_ecg(raw, method='ctps') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(raw, method='correlation') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(epochs, method='ctps') assert_equal(len(scores), ica.n_components_) assert_raises(ValueError, ica.find_bads_ecg, epochs.average(), method='ctps') assert_raises(ValueError, ica.find_bads_ecg, raw, method='crazy-coupling') idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) raw.info['chs'][raw.ch_names.index('EOG 061') - 1]['kind'] = 202 idx, scores = ica.find_bads_eog(raw) assert_true(isinstance(scores, list)) assert_equal(len(scores[0]), ica.n_components_) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(epochs_eog, target='EOG 061', score_func=func) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(epochs, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.score_sources(raw, target='MEG 1531', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert_true(ecg_events.ndim == 2) # eog functionality eog_scores = ica.score_sources(raw, target='EOG 061', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert_true(eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.get_sources(raw, start=0, stop=100) assert_true(ica_raw.last_samp - ica_raw.first_samp == 100) assert_true(len(ica_raw._filenames) == 0) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test-ica_raw.fif') ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = Raw(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.get_sources(epochs) assert_true(ica_epochs.events.shape == epochs.events.shape) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) assert_true(ica.n_components_ == ica_epochs.get_data().shape[1]) assert_true(ica_epochs._raw is None) assert_true(ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = ica._check_n_pca_components(ncomps) assert_true(ncomps_ == expected)