def test_saving_picked(): """Test saving picked CTF instances.""" temp_dir = _TempDir() out_fname = op.join(temp_dir, 'test_py_raw.fif') raw = read_raw_ctf(op.join(ctf_dir, ctf_fname_1_trial)) raw.crop(0, 1).load_data() assert raw.compensation_grade == get_current_comp(raw.info) == 0 assert len(raw.info['comps']) == 5 pick_kwargs = dict(meg=True, ref_meg=False, verbose=True) for comp_grade in [0, 1]: raw.apply_gradient_compensation(comp_grade) with catch_logging() as log: raw_pick = raw.copy().pick_types(**pick_kwargs) assert len(raw.info['comps']) == 5 assert len(raw_pick.info['comps']) == 0 log = log.getvalue() assert 'Removing 5 compensators' in log raw_pick.save(out_fname, overwrite=True) # should work raw2 = read_raw_fif(out_fname) assert (raw_pick.ch_names == raw2.ch_names) assert_array_equal(raw_pick.times, raw2.times) assert_allclose(raw2[0:20][0], raw_pick[0:20][0], rtol=1e-6, atol=1e-20) # atol is very small but > 0 raw2 = read_raw_fif(out_fname, preload=True) assert (raw_pick.ch_names == raw2.ch_names) assert_array_equal(raw_pick.times, raw2.times) assert_allclose(raw2[0:20][0], raw_pick[0:20][0], rtol=1e-6, atol=1e-20) # atol is very small but > 0
def test_rawctf_clean_names(): """Test RawCTF _clean_names method.""" # read test data with pytest.warns(RuntimeWarning, match='ref channel RMSP did not'): raw = read_raw_ctf(op.join(ctf_dir, ctf_fname_catch)) raw_cleaned = read_raw_ctf(op.join(ctf_dir, ctf_fname_catch), clean_names=True) test_channel_names = _clean_names(raw.ch_names) test_info_comps = copy.deepcopy(raw.info['comps']) # channel names should not be cleaned by default assert raw.ch_names != test_channel_names chs_ch_names = [ch['ch_name'] for ch in raw.info['chs']] assert chs_ch_names != test_channel_names for test_comp, comp in zip(test_info_comps, raw.info['comps']): for key in ('row_names', 'col_names'): assert not array_equal(_clean_names(test_comp['data'][key]), comp['data'][key]) # channel names should be cleaned if clean_names=True assert raw_cleaned.ch_names == test_channel_names for ch, test_ch_name in zip(raw_cleaned.info['chs'], test_channel_names): assert ch['ch_name'] == test_ch_name for test_comp, comp in zip(test_info_comps, raw_cleaned.info['comps']): for key in ('row_names', 'col_names'): assert _clean_names(test_comp['data'][key]) == comp['data'][key]
def convert_ds_to_raw_fif(ds_file): import os import os.path as op from nipype.utils.filemanip import split_filename as split_f from mne.io import read_raw_ctf subj_path,basename,ext = split_f(ds_file) print subj_path,basename,ext raw = read_raw_ctf(ds_file) #raw_fif_file = os.path.abspath(basename + "_raw.fif") #raw.save(raw_fif_file) #return raw_fif_file raw_fif_file = os.path.join(subj_path,basename + "_raw.fif") if not op.isfile(raw_fif_file): raw = read_raw_ctf(ds_file) raw.save(raw_fif_file) else: print '*** RAW FIF file %s exists!!!' % raw_fif_file return raw_fif_file
def test_cov_ctf(): """Test basic cov computation on ctf data with/without compensation.""" raw = read_raw_ctf(ctf_fname).crop(0., 2.).load_data() events = make_fixed_length_events(raw, 99999) assert len(events) == 2 ch_names = [raw.info['ch_names'][pick] for pick in pick_types(raw.info, meg=True, eeg=False, ref_meg=False)] for comp in [0, 1]: raw.apply_gradient_compensation(comp) epochs = Epochs(raw, events, None, -0.2, 0.2, preload=True) with pytest.warns(RuntimeWarning, match='Too few samples'): noise_cov = compute_covariance(epochs, tmax=0., method=['empirical']) prepare_noise_cov(noise_cov, raw.info, ch_names) raw.apply_gradient_compensation(0) epochs = Epochs(raw, events, None, -0.2, 0.2, preload=True) with pytest.warns(RuntimeWarning, match='Too few samples'): noise_cov = compute_covariance(epochs, tmax=0., method=['empirical']) raw.apply_gradient_compensation(1) # TODO This next call in principle should fail. prepare_noise_cov(noise_cov, raw.info, ch_names) # make sure comps matrices was not removed from raw assert raw.info['comps'], 'Comps matrices removed'
def test_check_compensation_consistency(): """Test check picks compensation.""" raw = read_raw_ctf(ctf_fname, preload=False) events = make_fixed_length_events(raw, 99999) picks = pick_types(raw.info, meg=True, exclude=[], ref_meg=True) pick_ch_names = [raw.info['ch_names'][idx] for idx in picks] for (comp, expected_result) in zip([0, 1], [False, False]): raw.apply_gradient_compensation(comp) ret, missing = _bad_chans_comp(raw.info, pick_ch_names) assert ret == expected_result assert len(missing) == 0 Epochs(raw, events, None, -0.2, 0.2, preload=False, picks=picks) picks = pick_types(raw.info, meg=True, exclude=[], ref_meg=False) pick_ch_names = [raw.info['ch_names'][idx] for idx in picks] for (comp, expected_result) in zip([0, 1], [False, True]): raw.apply_gradient_compensation(comp) ret, missing = _bad_chans_comp(raw.info, pick_ch_names) assert ret == expected_result assert len(missing) == 17 with catch_logging() as log: Epochs(raw, events, None, -0.2, 0.2, preload=False, picks=picks, verbose=True) assert'Removing 5 compensators' in log.getvalue()
def test_plot_trans(): """Test plotting of -trans.fif files and MEG sensor layouts """ evoked = read_evokeds(evoked_fname)[0] with warnings.catch_warnings(record=True): # 4D weight tables bti = read_raw_bti(pdf_fname, config_fname, hs_fname, convert=True, preload=False).info infos = dict( Neuromag=evoked.info, CTF=read_raw_ctf(ctf_fname).info, BTi=bti, KIT=read_raw_kit(sqd_fname).info, ) for system, info in infos.items(): ref_meg = False if system == 'KIT' else True plot_trans(info, trans_fname, subject='sample', meg_sensors=True, subjects_dir=subjects_dir, ref_meg=ref_meg) # KIT ref sensor coil def not defined assert_raises(RuntimeError, plot_trans, infos['KIT'], None, meg_sensors=True, ref_meg=True) info = infos['Neuromag'] assert_raises(ValueError, plot_trans, info, trans_fname, subject='sample', subjects_dir=subjects_dir, ch_type='bad-chtype') assert_raises(TypeError, plot_trans, 'foo', trans_fname, subject='sample', subjects_dir=subjects_dir) # no-head version plot_trans(info, None, meg_sensors=True, dig=True, coord_frame='head') # EEG only with strange options with warnings.catch_warnings(record=True) as w: plot_trans(evoked.copy().pick_types(meg=False, eeg=True).info, trans=trans_fname, meg_sensors=True) assert_true(['Cannot plot MEG' in str(ww.message) for ww in w])
def test_plot_ref_meg(): """Test plotting ref_meg.""" import matplotlib.pyplot as plt raw_ctf = read_raw_ctf(ctf_fname_continuous).crop(0, 1).load_data() raw_ctf.plot() plt.close('all') assert_raises(ValueError, raw_ctf.plot, group_by='selection')
def test_find_ch_connectivity(): """Test computing the connectivity matrix.""" data_path = testing.data_path() raw = read_raw_fif(raw_fname, preload=True) sizes = {'mag': 828, 'grad': 1700, 'eeg': 386} nchans = {'mag': 102, 'grad': 204, 'eeg': 60} for ch_type in ['mag', 'grad', 'eeg']: conn, ch_names = find_ch_connectivity(raw.info, ch_type) # Silly test for checking the number of neighbors. assert_equal(conn.getnnz(), sizes[ch_type]) assert_equal(len(ch_names), nchans[ch_type]) pytest.raises(ValueError, find_ch_connectivity, raw.info, None) # Test computing the conn matrix with gradiometers. conn, ch_names = _compute_ch_connectivity(raw.info, 'grad') assert_equal(conn.getnnz(), 2680) # Test ch_type=None. raw.pick_types(meg='mag') find_ch_connectivity(raw.info, None) bti_fname = op.join(data_path, 'BTi', 'erm_HFH', 'c,rfDC') bti_config_name = op.join(data_path, 'BTi', 'erm_HFH', 'config') raw = read_raw_bti(bti_fname, bti_config_name, None) _, ch_names = find_ch_connectivity(raw.info, 'mag') assert 'A1' in ch_names ctf_fname = op.join(data_path, 'CTF', 'testdata_ctf_short.ds') raw = read_raw_ctf(ctf_fname) _, ch_names = find_ch_connectivity(raw.info, 'mag') assert 'MLC11' in ch_names pytest.raises(ValueError, find_ch_connectivity, raw.info, 'eog')
def test_check_compensation_consistency(): """Test check picks compensation.""" raw = read_raw_ctf(ctf_fname, preload=False) events = make_fixed_length_events(raw, 99999) picks = pick_types(raw.info, meg=True, exclude=[], ref_meg=True) pick_ch_names = [raw.info['ch_names'][idx] for idx in picks] for (comp, expected_result) in zip([0, 1], [False, False]): raw.apply_gradient_compensation(comp) ret, missing = _bad_chans_comp(raw.info, pick_ch_names) assert ret == expected_result assert len(missing) == 0 Epochs(raw, events, None, -0.2, 0.2, preload=False, picks=picks) picks = pick_types(raw.info, meg=True, exclude=[], ref_meg=False) pick_ch_names = [raw.info['ch_names'][idx] for idx in picks] for (comp, expected_result) in zip([0, 1], [False, True]): raw.apply_gradient_compensation(comp) ret, missing = _bad_chans_comp(raw.info, pick_ch_names) assert ret == expected_result assert len(missing) == 17 if comp != 0: with pytest.raises(RuntimeError, match='Compensation grade 1 has been applied'): Epochs(raw, events, None, -0.2, 0.2, preload=False, picks=picks) else: Epochs(raw, events, None, -0.2, 0.2, preload=False, picks=picks)
def test_ica_ctf(): """Test run ICA computation on ctf data with/without compensation.""" method = 'fastica' raw = read_raw_ctf(ctf_fname, preload=True) events = make_fixed_length_events(raw, 99999) for comp in [0, 1]: raw.apply_gradient_compensation(comp) epochs = Epochs(raw, events, None, -0.2, 0.2, preload=True) evoked = epochs.average() # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(UserWarning, match='did not converge'): ica.fit(inst) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst) # test mixed compensation case raw.apply_gradient_compensation(0) ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw) raw.apply_gradient_compensation(1) epochs = Epochs(raw, events, None, -0.2, 0.2, preload=True) evoked = epochs.average() for inst in [raw, epochs, evoked]: with pytest.raises(RuntimeError, match='Compensation grade of ICA'): ica.apply(inst) with pytest.raises(RuntimeError, match='Compensation grade of ICA'): ica.get_sources(inst)
def test_ica_eeg(): """Test ICA on EEG.""" method = 'fastica' raw_fif = read_raw_fif(fif_fname, preload=True) with pytest.warns(RuntimeWarning, match='events'): raw_eeglab = read_raw_eeglab(input_fname=eeglab_fname, montage=eeglab_montage, preload=True) for raw in [raw_fif, raw_eeglab]: events = make_fixed_length_events(raw, 99999, start=0, stop=0.3, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = [] picks_all.extend(picks_meg) picks_all.extend(picks_eeg) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(None): ica.fit(inst, picks=picks) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst) with pytest.warns(RuntimeWarning, match='MISC channel'): raw = read_raw_ctf(ctf_fname2, preload=True) events = make_fixed_length_events(raw, 99999, start=0, stop=0.2, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = picks_meg + picks_eeg for comp in [0, 1]: raw.apply_gradient_compensation(comp) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(None): ica.fit(inst) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst)
def test_interpolation_ctf_comp(): """Test interpolation with compensated CTF data.""" ctf_dir = op.join(testing.data_path(download=False), 'CTF') raw_fname = op.join(ctf_dir, 'somMDYO-18av.ds') raw = io.read_raw_ctf(raw_fname, preload=True) raw.info['bads'] = [raw.ch_names[5], raw.ch_names[-5]] raw.interpolate_bads(mode='fast') assert raw.info['bads'] == []
def test_read_spm_ctf(): """Test CTF reader with omitted samples.""" data_path = spm_face.data_path() raw_fname = op.join(data_path, 'MEG', 'spm', 'SPM_CTF_MEG_example_faces1_3D.ds') raw = read_raw_ctf(raw_fname) extras = raw._raw_extras[0] assert_equal(extras['n_samp'], raw.n_times) assert_false(extras['n_samp'] == extras['n_samp_tot'])
def test_calculate_head_pos_ctf(): """Test extracting of cHPI positions from ctf data.""" raw = read_raw_ctf(ctf_chpi_fname) quats = _calculate_head_pos_ctf(raw) mc_quats = read_head_pos(ctf_chpi_pos_fname) _assert_quats(quats, mc_quats, dist_tol=0.004, angle_tol=2.5) raw = read_raw_fif(ctf_fname) pytest.raises(RuntimeError, _calculate_head_pos_ctf, raw)
def test_dipole_fitting_ctf(): """Test dipole fitting with CTF data.""" raw_ctf = read_raw_ctf(fname_ctf).set_eeg_reference() events = make_fixed_length_events(raw_ctf, 1) evoked = Epochs(raw_ctf, events, 1, 0, 0, baseline=None).average() cov = make_ad_hoc_cov(evoked.info) sphere = make_sphere_model((0., 0., 0.)) # XXX Eventually we should do some better checks about accuracy, but # for now our CTF phantom fitting tutorials will have to do # (otherwise we need to add that to the testing dataset, which is # a bit too big) fit_dipole(evoked, cov, sphere)
def test_ica_labels(): """Test ICA labels.""" # The CTF data are uniquely well suited to testing the ICA.find_bads_ # methods raw = read_raw_ctf(ctf_fname, preload=True) # derive reference ICA components and append them to raw icarf = ICA(n_components=2, random_state=0, max_iter=2, allow_ref_meg=True) with pytest.warns(UserWarning, match='did not converge'): icarf.fit(raw.copy().pick_types(meg=False, ref_meg=True)) icacomps = icarf.get_sources(raw) # rename components so they are auto-detected by find_bads_ref icacomps.rename_channels({c: 'REF_' + c for c in icacomps.ch_names}) # and add them to raw raw.add_channels([icacomps]) # set the appropriate EEG channels to EOG and ECG raw.set_channel_types({'EEG057': 'eog', 'EEG058': 'eog', 'EEG059': 'ecg'}) ica = ICA(n_components=4, random_state=0, max_iter=2, method='fastica') with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw) ica.find_bads_eog(raw, l_freq=None, h_freq=None) picks = list(pick_types(raw.info, meg=False, eog=True)) for idx, ch in enumerate(picks): assert '{}/{}/{}'.format('eog', idx, raw.ch_names[ch]) in ica.labels_ assert 'eog' in ica.labels_ for key in ('ecg', 'ref_meg', 'ecg/ECG-MAG'): assert key not in ica.labels_ ica.find_bads_ecg(raw, l_freq=None, h_freq=None, method='correlation') picks = list(pick_types(raw.info, meg=False, ecg=True)) for idx, ch in enumerate(picks): assert '{}/{}/{}'.format('ecg', idx, raw.ch_names[ch]) in ica.labels_ for key in ('ecg', 'eog'): assert key in ica.labels_ for key in ('ref_meg', 'ecg/ECG-MAG'): assert key not in ica.labels_ ica.find_bads_ref(raw, l_freq=None, h_freq=None) picks = pick_channels_regexp(raw.ch_names, 'REF_ICA*') for idx, ch in enumerate(picks): assert '{}/{}/{}'.format('ref_meg', idx, raw.ch_names[ch]) in ica.labels_ for key in ('ecg', 'eog', 'ref_meg'): assert key in ica.labels_ assert 'ecg/ECG-MAG' not in ica.labels_ ica.find_bads_ecg(raw, l_freq=None, h_freq=None) for key in ('ecg', 'eog', 'ref_meg', 'ecg/ECG-MAG'): assert key in ica.labels_
def test_plot_trans(): """Test plotting of -trans.fif files and MEG sensor layouts.""" from mayavi import mlab evoked = read_evokeds(evoked_fname)[0] with warnings.catch_warnings(record=True): # 4D weight tables bti = read_raw_bti(pdf_fname, config_fname, hs_fname, convert=True, preload=False).info infos = dict( Neuromag=evoked.info, CTF=read_raw_ctf(ctf_fname).info, BTi=bti, KIT=read_raw_kit(sqd_fname).info, ) for system, info in infos.items(): ref_meg = False if system == 'KIT' else True plot_trans(info, trans_fname, subject='sample', meg_sensors=True, subjects_dir=subjects_dir, ref_meg=ref_meg) mlab.close(all=True) # KIT ref sensor coil def is defined plot_trans(infos['KIT'], None, meg_sensors=True, ref_meg=True) mlab.close(all=True) info = infos['Neuromag'] assert_raises(ValueError, plot_trans, info, trans_fname, subject='sample', subjects_dir=subjects_dir, ch_type='bad-chtype') assert_raises(TypeError, plot_trans, 'foo', trans_fname, subject='sample', subjects_dir=subjects_dir) # no-head version plot_trans(info, None, meg_sensors=True, dig=True, coord_frame='head') mlab.close(all=True) # all coord frames for coord_frame in ('meg', 'head', 'mri'): plot_trans(info, meg_sensors=True, dig=True, coord_frame=coord_frame, trans=trans_fname, subject='sample', subjects_dir=subjects_dir) mlab.close(all=True) # EEG only with strange options evoked_eeg_ecog = evoked.copy().pick_types(meg=False, eeg=True) evoked_eeg_ecog.info['projs'] = [] # "remove" avg proj evoked_eeg_ecog.set_channel_types({'EEG 001': 'ecog'}) with warnings.catch_warnings(record=True) as w: plot_trans(evoked_eeg_ecog.info, subject='sample', trans=trans_fname, source='outer_skin', meg_sensors=True, skull=True, eeg_sensors=['original', 'projected'], ecog_sensors=True, brain='white', head=True, subjects_dir=subjects_dir) mlab.close(all=True) assert_true(['Cannot plot MEG' in str(ww.message) for ww in w])
def test_read_spm_ctf(): """Test CTF reader with omitted samples.""" data_path = spm_face.data_path() raw_fname = op.join(data_path, 'MEG', 'spm', 'SPM_CTF_MEG_example_faces1_3D.ds') raw = read_raw_ctf(raw_fname) extras = raw._raw_extras[0] assert_equal(extras['n_samp'], raw.n_times) assert extras['n_samp'] != extras['n_samp_tot'] # Test that LPA, nasion and RPA are correct. coord_frames = np.array([d['coord_frame'] for d in raw.info['dig']]) assert np.all(coord_frames == FIFF.FIFFV_COORD_HEAD) cardinals = {d['ident']: d['r'] for d in raw.info['dig']} assert cardinals[1][0] < cardinals[2][0] < cardinals[3][0] # x coord assert cardinals[1][1] < cardinals[2][1] # y coord assert cardinals[3][1] < cardinals[2][1] # y coord for key in cardinals.keys(): assert_allclose(cardinals[key][2], 0, atol=1e-6) # z coord
def test_saving_picked(): """Test saving picked CTF instances.""" temp_dir = _TempDir() out_fname = op.join(temp_dir, 'test_py_raw.fif') raw = read_raw_ctf(op.join(ctf_dir, ctf_fname_1_trial)) raw.crop(0, 1).load_data() assert raw.compensation_grade == get_current_comp(raw.info) == 0 assert len(raw.info['comps']) == 5 pick_kwargs = dict(meg=True, ref_meg=False, verbose=True) with catch_logging() as log: raw_pick = raw.copy().pick_types(**pick_kwargs) assert len(raw.info['comps']) == 5 assert len(raw_pick.info['comps']) == 0 log = log.getvalue() assert 'Removing 5 compensators' in log raw_pick.save(out_fname) # should work read_raw_fif(out_fname) read_raw_fif(out_fname, preload=True) # If comp is applied, picking should error raw.apply_gradient_compensation(1) assert raw.compensation_grade == get_current_comp(raw.info) == 1 with pytest.raises(RuntimeError, match='Compensation grade 1 has been'): raw.copy().pick_types(**pick_kwargs)
def test_compute_proj_ctf(): """Test to show that projector code completes on CTF data.""" raw = read_raw_ctf(ctf_fname) raw.load_data() # expected channels per projector type n_mags = len(pick_types(raw.info, meg='mag', ref_meg=False, exclude='bads')) n_grads = len(pick_types(raw.info, meg='grad', ref_meg=False, exclude='bads')) n_eegs = len(pick_types(raw.info, meg=False, eeg=True, ref_meg=False, exclude='bads')) # Test with and without gradient compensation for c in [0, 1]: raw.apply_gradient_compensation(c) 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, average=average, ch_name='EEG059', avg_ref=True, no_proj=False, l_freq=None, h_freq=None, reject=None, tmax=dur_use, filter_length=6000) _check_projs_for_expected_channels(projs, n_mags, n_grads, n_eegs) assert len(projs) == (5 + n_projs_init) projs, events = compute_proj_ecg(raw, n_mag=1, n_grad=1, n_eeg=2, average=average, ch_name='EEG059', avg_ref=True, no_proj=False, l_freq=None, h_freq=None, reject=None, tmax=dur_use, filter_length=6000) _check_projs_for_expected_channels(projs, n_mags, n_grads, n_eegs) assert len(projs) == (4 + n_projs_init)
# filtered at 600 Hz. Here the data and empty room data files are read to # construct instances of :class:`mne.io.Raw`. data_path = bst_auditory.data_path() subject = 'bst_auditory' subjects_dir = op.join(data_path, 'subjects') raw_fname1 = op.join(data_path, 'MEG', subject, 'S01_AEF_20131218_01.ds') raw_fname2 = op.join(data_path, 'MEG', subject, 'S01_AEF_20131218_02.ds') erm_fname = op.join(data_path, 'MEG', subject, 'S01_Noise_20131218_01.ds') # %% # In the memory saving mode we use ``preload=False`` and use the memory # efficient IO which loads the data on demand. However, filtering and some # other functions require the data to be preloaded into memory. raw = read_raw_ctf(raw_fname1) n_times_run1 = raw.n_times # Here we ignore that these have different device<->head transforms mne.io.concatenate_raws([raw, read_raw_ctf(raw_fname2)], on_mismatch='ignore') raw_erm = read_raw_ctf(erm_fname) # %% # The data array consists of 274 MEG axial gradiometers, 26 MEG reference # sensors and 2 EEG electrodes (Cz and Pz). In addition: # # - 1 stim channel for marking presentation times for the stimuli # - 1 audio channel for the sent signal # - 1 response channel for recording the button presses # - 1 ECG bipolar # - 2 EOG bipolar (vertical and horizontal)
import os.path as op from mayavi import mlab import mne from mne.io import Raw, read_raw_ctf, read_raw_bti, read_raw_kit from mne.datasets import sample, spm_face from mne.viz import plot_trans print(__doc__) bti_path = op.abspath(op.dirname(mne.__file__)) + '/io/bti/tests/data/' kit_path = op.abspath(op.dirname(mne.__file__)) + '/io/kit/tests/data/' raws = dict( Neuromag=Raw(sample.data_path() + '/MEG/sample/sample_audvis_raw.fif'), CTF_275=read_raw_ctf(spm_face.data_path() + '/MEG/spm/SPM_CTF_MEG_example_faces1_3D.ds'), Magnes_3600wh=read_raw_bti(bti_path + 'test_pdf_linux', bti_path + 'test_config_linux', bti_path + 'test_hs_linux'), KIT=read_raw_kit(kit_path + 'test.sqd'), ) for system, raw in raws.items(): # We don't have coil definitions for KIT refs, so exclude them ref_meg = False if system == 'KIT' else True fig = plot_trans(raw.info, trans=None, dig=False, eeg_sensors=False, meg_sensors=True, coord_frame='meg', ref_meg=ref_meg) mlab.title(system)
import mne from mne.datasets.brainstorm import bst_auditory from mne.io import read_raw_ctf from mne.preprocessing import annotate_movement, compute_average_dev_head_t # Load data data_path = bst_auditory.data_path() data_path_MEG = op.join(data_path, 'MEG') subject = 'bst_auditory' subjects_dir = op.join(data_path, 'subjects') trans_fname = op.join(data_path, 'MEG', 'bst_auditory', 'bst_auditory-trans.fif') raw_fname1 = op.join(data_path_MEG, 'bst_auditory', 'S01_AEF_20131218_01.ds') raw_fname2 = op.join(data_path_MEG, 'bst_auditory', 'S01_AEF_20131218_02.ds') raw = read_raw_ctf(raw_fname1, preload=False) mne.io.concatenate_raws([raw, read_raw_ctf(raw_fname2, preload=False)]) raw.crop(350, 410).load_data() raw.resample(100, npad="auto") ############################################################################### # Plot continuous head position with respect to the mean recording position # -------------------------------------------------------------------------- # get cHPI time series and compute average chpi_locs = mne.chpi.extract_chpi_locs_ctf(raw) head_pos = mne.chpi.compute_head_pos(raw.info, chpi_locs) original_head_dev_t = mne.transforms.invert_transform( raw.info['dev_head_t']) average_head_dev_t = mne.transforms.invert_transform(
def _run_interface(self, runtime): raw_filename = self.inputs.raw_filename cov_fname_in = self.inputs.cov_fname_in is_epoched = self.inputs.is_epoched is_evoked = self.inputs.is_evoked events_id = self.inputs.events_id t_min = self.inputs.t_min t_max = self.inputs.t_max data_path, basename, ext = split_f(raw_filename) self.cov_fname_out = op.join(data_path, '%s-cov.fif' % basename) if not op.isfile(cov_fname_in): if is_epoched and is_evoked: raw = read_raw_fif(raw_filename) events = find_events(raw) if not op.isfile(self.cov_fname_out): print(('\n*** COMPUTE COV FROM EPOCHS ***\n' + self.cov_fname_out)) reject = create_reject_dict(raw.info) picks = pick_types(raw.info, meg=True, ref_meg=False, exclude='bads') epochs = Epochs(raw, events, events_id, t_min, t_max, picks=picks, baseline=(None, 0), reject=reject) # TODO method='auto'? too long!!! noise_cov = compute_covariance(epochs, tmax=0, method='diagonal_fixed') write_cov(self.cov_fname_out, noise_cov) else: print(('\n *** NOISE cov file %s exists!!! \n' % self.cov_fname_out)) else: '\n *** RAW DATA \n' for er_fname in glob.glob(op.join(data_path, cov_fname_in)): print(('\n found file name %s \n' % er_fname)) try: if er_fname.rfind('cov.fif') > -1: print("\n *** NOISE cov file %s exists!! " "\n" % er_fname) self.cov_fname_out = er_fname else: if er_fname.rfind('.fif') > -1: er_raw = read_raw_fif(er_fname) er_fname = er_fname.replace('.fif', '-raw-cov.fif') elif er_fname.rfind('.ds') > -1: er_raw = read_raw_ctf(er_fname) er_fname = er_fname.replace('.ds', '-raw-cov.fif') self.cov_fname_out = op.join(data_path, er_fname) if not op.isfile(self.cov_fname_out): reject = create_reject_dict(er_raw.info) picks = pick_types(er_raw.info, meg=True, ref_meg=False, exclude='bads') noise_cov = compute_raw_covariance(er_raw, picks=picks, reject=reject) write_cov(self.cov_fname_out, noise_cov) else: print(('\n *** NOISE cov file %s exists!!! \n' % self.cov_fname_out)) except NameError: sys.exit("No covariance matrix as input!") # TODO creare una matrice diagonale? else: print(('\n *** NOISE cov file %s exists!!! \n' % cov_fname_in)) self.cov_fname_out = cov_fname_in return runtime
def test_plot_alignment(tmpdir): """Test plotting of -trans.fif files and MEG sensor layouts.""" # generate fiducials file for testing tempdir = str(tmpdir) fiducials_path = op.join(tempdir, 'fiducials.fif') fid = [{'coord_frame': 5, 'ident': 1, 'kind': 1, 'r': [-0.08061612, -0.02908875, -0.04131077]}, {'coord_frame': 5, 'ident': 2, 'kind': 1, 'r': [0.00146763, 0.08506715, -0.03483611]}, {'coord_frame': 5, 'ident': 3, 'kind': 1, 'r': [0.08436285, -0.02850276, -0.04127743]}] write_dig(fiducials_path, fid, 5) mlab = _import_mlab() evoked = read_evokeds(evoked_fname)[0] sample_src = read_source_spaces(src_fname) bti = read_raw_bti(pdf_fname, config_fname, hs_fname, convert=True, preload=False).info infos = dict( Neuromag=evoked.info, CTF=read_raw_ctf(ctf_fname).info, BTi=bti, KIT=read_raw_kit(sqd_fname).info, ) for system, info in infos.items(): meg = ['helmet', 'sensors'] if system == 'KIT': meg.append('ref') plot_alignment(info, trans_fname, subject='sample', subjects_dir=subjects_dir, meg=meg) mlab.close(all=True) # KIT ref sensor coil def is defined mlab.close(all=True) info = infos['Neuromag'] pytest.raises(TypeError, plot_alignment, 'foo', trans_fname, subject='sample', subjects_dir=subjects_dir) pytest.raises(TypeError, plot_alignment, info, trans_fname, subject='sample', subjects_dir=subjects_dir, src='foo') pytest.raises(ValueError, plot_alignment, info, trans_fname, subject='fsaverage', subjects_dir=subjects_dir, src=sample_src) sample_src.plot(subjects_dir=subjects_dir, head=True, skull=True, brain='white') mlab.close(all=True) # no-head version mlab.close(all=True) # all coord frames pytest.raises(ValueError, plot_alignment, info) plot_alignment(info, surfaces=[]) for coord_frame in ('meg', 'head', 'mri'): plot_alignment(info, meg=['helmet', 'sensors'], dig=True, coord_frame=coord_frame, trans=trans_fname, subject='sample', mri_fiducials=fiducials_path, subjects_dir=subjects_dir, src=sample_src) mlab.close(all=True) # EEG only with strange options evoked_eeg_ecog_seeg = evoked.copy().pick_types(meg=False, eeg=True) evoked_eeg_ecog_seeg.info['projs'] = [] # "remove" avg proj evoked_eeg_ecog_seeg.set_channel_types({'EEG 001': 'ecog', 'EEG 002': 'seeg'}) with pytest.warns(RuntimeWarning, match='Cannot plot MEG'): plot_alignment(evoked_eeg_ecog_seeg.info, subject='sample', trans=trans_fname, subjects_dir=subjects_dir, surfaces=['white', 'outer_skin', 'outer_skull'], meg=['helmet', 'sensors'], eeg=['original', 'projected'], ecog=True, seeg=True) mlab.close(all=True) sphere = make_sphere_model(info=evoked.info, r0='auto', head_radius='auto') bem_sol = read_bem_solution(op.join(subjects_dir, 'sample', 'bem', 'sample-1280-1280-1280-bem-sol.fif')) bem_surfs = read_bem_surfaces(op.join(subjects_dir, 'sample', 'bem', 'sample-1280-1280-1280-bem.fif')) sample_src[0]['coord_frame'] = 4 # hack for coverage plot_alignment(info, subject='sample', eeg='projected', meg='helmet', bem=sphere, dig=True, surfaces=['brain', 'inner_skull', 'outer_skull', 'outer_skin']) plot_alignment(info, trans_fname, subject='sample', meg='helmet', subjects_dir=subjects_dir, eeg='projected', bem=sphere, surfaces=['head', 'brain'], src=sample_src) assert all(surf['coord_frame'] == FIFF.FIFFV_COORD_MRI for surf in bem_sol['surfs']) plot_alignment(info, trans_fname, subject='sample', meg=[], subjects_dir=subjects_dir, bem=bem_sol, eeg=True, surfaces=['head', 'inflated', 'outer_skull', 'inner_skull']) assert all(surf['coord_frame'] == FIFF.FIFFV_COORD_MRI for surf in bem_sol['surfs']) plot_alignment(info, trans_fname, subject='sample', meg=True, subjects_dir=subjects_dir, surfaces=['head', 'inner_skull'], bem=bem_surfs) sphere = make_sphere_model('auto', 'auto', evoked.info) src = setup_volume_source_space(sphere=sphere) plot_alignment(info, eeg='projected', meg='helmet', bem=sphere, src=src, dig=True, surfaces=['brain', 'inner_skull', 'outer_skull', 'outer_skin']) sphere = make_sphere_model('auto', None, evoked.info) # one layer plot_alignment(info, trans_fname, subject='sample', meg=False, coord_frame='mri', subjects_dir=subjects_dir, surfaces=['brain'], bem=sphere, show_axes=True) # 3D coil with no defined draw (ConvexHull) info_cube = pick_info(info, [0]) info['dig'] = None info_cube['chs'][0]['coil_type'] = 9999 with pytest.raises(RuntimeError, match='coil definition not found'): plot_alignment(info_cube, meg='sensors', surfaces=()) coil_def_fname = op.join(tempdir, 'temp') with open(coil_def_fname, 'w') as fid: fid.write(coil_3d) with use_coil_def(coil_def_fname): plot_alignment(info_cube, meg='sensors', surfaces=(), dig=True) # one layer bem with skull surfaces: pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['brain', 'head', 'inner_skull'], bem=sphere) # wrong eeg value: pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, eeg='foo') # wrong meg value: pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, meg='bar') # multiple brain surfaces: pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['white', 'pial']) pytest.raises(TypeError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=[1]) pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['foo']) mlab.close(all=True)
from mne.datasets.brainstorm import bst_phantom_ctf from mne.io import read_raw_ctf print(__doc__) ############################################################################### # The data were collected with a CTF system at 2400 Hz. data_path = bst_phantom_ctf.data_path(verbose=True) # Switch to these to use the higher-SNR data: # raw_path = op.join(data_path, 'phantom_200uA_20150709_01.ds') # dip_freq = 7. raw_path = op.join(data_path, 'phantom_20uA_20150603_03.ds') dip_freq = 23. erm_path = op.join(data_path, 'emptyroom_20150709_01.ds') raw = read_raw_ctf(raw_path, preload=True) ############################################################################### # The sinusoidal signal is generated on channel HDAC006, so we can use # that to obtain precise timing. sinusoid, times = raw[raw.ch_names.index('HDAC006-4408')] plt.figure() plt.plot(times[times < 1.], sinusoid.T[times < 1.]) ############################################################################### # Let's create some events using this signal by thresholding the sinusoid. events = np.where(np.diff(sinusoid > 0.5) > 0)[1] + raw.first_samp events = np.vstack((events, np.zeros_like(events), np.ones_like(events))).T
def test_plot_trans(): """Test plotting of -trans.fif files and MEG sensor layouts.""" # generate fiducials file for testing tempdir = _TempDir() fiducials_path = op.join(tempdir, 'fiducials.fif') fid = [{ 'coord_frame': 5, 'ident': 1, 'kind': 1, 'r': [-0.08061612, -0.02908875, -0.04131077] }, { 'coord_frame': 5, 'ident': 2, 'kind': 1, 'r': [0.00146763, 0.08506715, -0.03483611] }, { 'coord_frame': 5, 'ident': 3, 'kind': 1, 'r': [0.08436285, -0.02850276, -0.04127743] }] write_dig(fiducials_path, fid, 5) mlab = _import_mlab() evoked = read_evokeds(evoked_fname)[0] sample_src = read_source_spaces(src_fname) with warnings.catch_warnings(record=True): # 4D weight tables bti = read_raw_bti(pdf_fname, config_fname, hs_fname, convert=True, preload=False).info infos = dict( Neuromag=evoked.info, CTF=read_raw_ctf(ctf_fname).info, BTi=bti, KIT=read_raw_kit(sqd_fname).info, ) for system, info in infos.items(): ref_meg = False if system == 'KIT' else True plot_trans(info, trans_fname, subject='sample', meg_sensors=True, subjects_dir=subjects_dir, ref_meg=ref_meg) mlab.close(all=True) # KIT ref sensor coil def is defined plot_trans(infos['KIT'], None, meg_sensors=True, ref_meg=True) mlab.close(all=True) info = infos['Neuromag'] assert_raises(TypeError, plot_trans, 'foo', trans_fname, subject='sample', subjects_dir=subjects_dir) assert_raises(TypeError, plot_trans, info, trans_fname, subject='sample', subjects_dir=subjects_dir, src='foo') assert_raises(ValueError, plot_trans, info, trans_fname, subject='fsaverage', subjects_dir=subjects_dir, src=sample_src) sample_src.plot(subjects_dir=subjects_dir) mlab.close(all=True) # no-head version plot_trans(info, None, meg_sensors=True, dig=True, coord_frame='head') mlab.close(all=True) # all coord frames for coord_frame in ('meg', 'head', 'mri'): plot_trans(info, meg_sensors=True, dig=True, coord_frame=coord_frame, trans=trans_fname, subject='sample', mri_fiducials=fiducials_path, subjects_dir=subjects_dir) mlab.close(all=True) # EEG only with strange options evoked_eeg_ecog = evoked.copy().pick_types(meg=False, eeg=True) evoked_eeg_ecog.info['projs'] = [] # "remove" avg proj evoked_eeg_ecog.set_channel_types({'EEG 001': 'ecog'}) with warnings.catch_warnings(record=True) as w: plot_trans(evoked_eeg_ecog.info, subject='sample', trans=trans_fname, source='outer_skin', meg_sensors=True, skull=True, eeg_sensors=['original', 'projected'], ecog_sensors=True, brain='white', head=True, subjects_dir=subjects_dir) mlab.close(all=True) assert_true(['Cannot plot MEG' in str(ww.message) for ww in w])
from mne import io from mne.datasets import spm_face from mne.minimum_norm import apply_inverse, make_inverse_operator from mne.cov import compute_covariance print(__doc__) ############################################################################## # Get data data_path = spm_face.data_path() subjects_dir = data_path + '/subjects' raw_fname = data_path + '/MEG/spm/SPM_CTF_MEG_example_faces%d_3D.ds' raw = io.read_raw_ctf(raw_fname % 1) # Take first run # To save time and memory for this demo, we'll just use the first # 2.5 minutes (all we need to get 30 total events) and heavily # resample 480->60 Hz (usually you wouldn't do either of these!) raw = raw.crop(0, 150.).load_data().resample(60, npad='auto') picks = mne.pick_types(raw.info, meg=True, exclude='bads') raw.filter(1, None, n_jobs=1) events = mne.find_events(raw, stim_channel='UPPT001') event_ids = {"faces": 1, "scrambled": 2} tmin, tmax = -0.2, 0.5 baseline = None # no baseline as high-pass is applied reject = dict(mag=3e-12)
# %% # Neuromag # -------- kwargs = dict(eeg=False, coord_frame='meg', show_axes=True, verbose=True) raw = read_raw_fif(sample.data_path() / 'MEG' / 'sample' / 'sample_audvis_raw.fif') fig = plot_alignment(raw.info, meg=('helmet', 'sensors'), **kwargs) set_3d_title(figure=fig, title='Neuromag') # %% # CTF # --- raw = read_raw_ctf(spm_face.data_path() / 'MEG' / 'spm' / 'SPM_CTF_MEG_example_faces1_3D.ds') fig = plot_alignment(raw.info, meg=('helmet', 'sensors', 'ref'), **kwargs) set_3d_title(figure=fig, title='CTF 275') # %% # BTi # --- bti_path = op.abspath(op.dirname(mne.__file__)) + '/io/bti/tests/data/' raw = read_raw_bti(op.join(bti_path, 'test_pdf_linux'), op.join(bti_path, 'test_config_linux'), op.join(bti_path, 'test_hs_linux')) fig = plot_alignment(raw.info, meg=('helmet', 'sensors', 'ref'), **kwargs) set_3d_title(figure=fig, title='Magnes 3600wh') # %%
#! /usr/bin/env python import os import numpy as np import pyctf import mne from mne.io import read_raw_ctf tmin, tmax = -0.1, 0.5 raw_path = os.getenv('ds') raw = read_raw_ctf(raw_path, clean_names=True, preload=True) ds = pyctf.dsopen(raw_path) # pick MEG channels picks = mne.pick_types(raw.info, meg='mag', eeg=False, stim=True) # Compute epochs events = mne.find_events(raw, stim_channel='UPPT001') m = 'pic1' srate = raw.info['sfreq'] tlen = ds.getNumberOfSamples() ev = [] for tr, t in ds.marks[m]: s = tr * tlen + int(t * srate + .5) ev.append([s, 0, 1]) epochs = mne.Epochs(raw, ev, {'pic1': 1}, tmin, tmax, picks=picks, baseline=(None, 0), reject=None, preload=False)
from saflow_utils import array_topoplot, get_ch_pos, create_pval_mask from saflow_params import IMG_DIR, SUBJ_LIST, RESULTS_PATH, FREQS_NAMES import itertools from mlneurotools.stats import compute_pval RESULTS_PATH = RESULTS_PATH + 'LDA_singlesubj_L1SO_PSD_VTC2575' MODEL = 'LDA' NPERM = 1001 CV = '10FOLD' COND = 2575 if __name__ == "__main__": ##### obtain ch_pos filename = '/storage/Yann/saflow_DATA/alldata/SA04_SAflow-yharel_20190411_01.ds' raw = read_raw_ctf(filename, preload=False, verbose=False) ch_xy = get_ch_pos(raw) raw.close() for SUBJ in SUBJ_LIST: all_acc = [] all_pval = [] all_masks = [] for FREQ in FREQS_NAMES: freq_acc = [] freq_pval = [] freq_perms_acc = [] for CHAN in range(270): savepath = '{}/classif_sub-{}_{}_{}.mat'.format(RESULTS_PATH, SUBJ, FREQ, CHAN) data_acc = loadmat(savepath)['acc_score'] data_pval = loadmat(savepath)['acc_pvalue'] data_perms_acc = loadmat(savepath)['acc_pscores']
def test_ica_eeg(): """Test ICA on EEG.""" method = 'fastica' raw_fif = read_raw_fif(fif_fname, preload=True) raw_eeglab = read_raw_eeglab(input_fname=eeglab_fname, montage=eeglab_montage, preload=True) for raw in [raw_fif, raw_eeglab]: events = make_fixed_length_events(raw, 99999, start=0, stop=0.3, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = [] picks_all.extend(picks_meg) picks_all.extend(picks_eeg) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(None): ica.fit(inst, picks=picks) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst) with pytest.warns(RuntimeWarning, match='MISC channel'): raw = read_raw_ctf(ctf_fname2, preload=True) events = make_fixed_length_events(raw, 99999, start=0, stop=0.2, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = picks_meg + picks_eeg for comp in [0, 1]: raw.apply_gradient_compensation(comp) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(None): ica.fit(inst) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst)
def test_channel_name_limit(tmpdir, monkeypatch, fname): """Test that our remapping works properly.""" # # raw # if fname.endswith('fif'): raw = read_raw_fif(fname) raw.pick_channels(raw.ch_names[:3]) ref_names = [] data_names = raw.ch_names else: assert fname.endswith('.ds') raw = read_raw_ctf(fname) ref_names = [ raw.ch_names[pick] for pick in pick_types(raw.info, meg=False, ref_meg=True) ] data_names = raw.ch_names[32:35] proj = dict(data=np.ones((1, len(data_names))), col_names=data_names[:2].copy(), row_names=None, nrow=1) proj = Projection(data=proj, active=False, desc='test', kind=0, explained_var=0.) raw.add_proj(proj, remove_existing=True) raw.info.normalize_proj() raw.pick_channels(data_names + ref_names).crop(0, 2) long_names = ['123456789abcdefg' + name for name in raw.ch_names] fname = tmpdir.join('test-raw.fif') with catch_logging() as log: raw.save(fname) log = log.getvalue() assert 'truncated' not in log rename = dict(zip(raw.ch_names, long_names)) long_data_names = [rename[name] for name in data_names] long_proj_names = long_data_names[:2] raw.rename_channels(rename) for comp in raw.info['comps']: for key in ('row_names', 'col_names'): for name in comp['data'][key]: assert name in raw.ch_names if raw.info['comps']: assert raw.compensation_grade == 0 raw.apply_gradient_compensation(3) assert raw.compensation_grade == 3 assert len(raw.info['projs']) == 1 assert raw.info['projs'][0]['data']['col_names'] == long_proj_names raw.info['bads'] = bads = long_data_names[2:3] good_long_data_names = [ name for name in long_data_names if name not in bads ] with catch_logging() as log: raw.save(fname, overwrite=True, verbose=True) log = log.getvalue() assert 'truncated to 15' in log for name in raw.ch_names: assert len(name) > 15 # first read the full way with catch_logging() as log: raw_read = read_raw_fif(fname, verbose=True) log = log.getvalue() assert 'Reading extended channel information' in log for ra in (raw, raw_read): assert ra.ch_names == long_names assert raw_read.info['projs'][0]['data']['col_names'] == long_proj_names del raw_read # next read as if no longer names could be read monkeypatch.setattr(meas_info, '_read_extended_ch_info', lambda x, y, z: None) with catch_logging() as log: raw_read = read_raw_fif(fname, verbose=True) log = log.getvalue() assert 'extended' not in log if raw.info['comps']: assert raw_read.compensation_grade == 3 raw_read.apply_gradient_compensation(0) assert raw_read.compensation_grade == 0 monkeypatch.setattr( # restore meas_info, '_read_extended_ch_info', _read_extended_ch_info) short_proj_names = [ f'{name[:13 - bool(len(ref_names))]}-{len(ref_names) + ni}' for ni, name in enumerate(long_data_names[:2]) ] assert raw_read.info['projs'][0]['data']['col_names'] == short_proj_names # # epochs # epochs = Epochs(raw, make_fixed_length_events(raw)) fname = tmpdir.join('test-epo.fif') epochs.save(fname) epochs_read = read_epochs(fname) for ep in (epochs, epochs_read): assert ep.info['ch_names'] == long_names assert ep.ch_names == long_names del raw, epochs_read # cov epochs.info['bads'] = [] cov = compute_covariance(epochs, verbose='error') fname = tmpdir.join('test-cov.fif') write_cov(fname, cov) cov_read = read_cov(fname) for co in (cov, cov_read): assert co['names'] == long_data_names assert co['bads'] == [] del cov_read # # evoked # evoked = epochs.average() evoked.info['bads'] = bads assert evoked.nave == 1 fname = tmpdir.join('test-ave.fif') evoked.save(fname) evoked_read = read_evokeds(fname)[0] for ev in (evoked, evoked_read): assert ev.ch_names == long_names assert ev.info['bads'] == bads del evoked_read, epochs # # forward # with pytest.warns(None): # not enough points for CTF sphere = make_sphere_model('auto', 'auto', evoked.info) src = setup_volume_source_space( pos=dict(rr=[[0, 0, 0.04]], nn=[[0, 1., 0.]])) fwd = make_forward_solution(evoked.info, None, src, sphere) fname = tmpdir.join('temp-fwd.fif') write_forward_solution(fname, fwd) fwd_read = read_forward_solution(fname) for fw in (fwd, fwd_read): assert fw['sol']['row_names'] == long_data_names assert fw['info']['ch_names'] == long_data_names assert fw['info']['bads'] == bads del fwd_read # # inv # inv = make_inverse_operator(evoked.info, fwd, cov) fname = tmpdir.join('test-inv.fif') write_inverse_operator(fname, inv) inv_read = read_inverse_operator(fname) for iv in (inv, inv_read): assert iv['info']['ch_names'] == good_long_data_names apply_inverse(evoked, inv) # smoke test
subject = 'bst_auditory' subjects_dir = op.join(data_path, 'subjects') raw_fname1 = op.join(data_path, 'MEG', 'bst_auditory', 'S01_AEF_20131218_01.ds') raw_fname2 = op.join(data_path, 'MEG', 'bst_auditory', 'S01_AEF_20131218_02.ds') erm_fname = op.join(data_path, 'MEG', 'bst_auditory', 'S01_Noise_20131218_01.ds') ############################################################################### # In the memory saving mode we use ``preload=False`` and use the memory # efficient IO which loads the data on demand. However, filtering and some # other functions require the data to be preloaded in the memory. raw = read_raw_ctf(raw_fname1) n_times_run1 = raw.n_times mne.io.concatenate_raws([raw, read_raw_ctf(raw_fname2)]) raw_erm = read_raw_ctf(erm_fname) ############################################################################### # Data channel array consisted of 274 MEG axial gradiometers, 26 MEG reference # sensors and 2 EEG electrodes (Cz and Pz). # In addition: # # - 1 stim channel for marking presentation times for the stimuli # - 1 audio channel for the sent signal # - 1 response channel for recording the button presses # - 1 ECG bipolar # - 2 EOG bipolar (vertical and horizontal) # - 12 head tracking channels
import numpy as np import mne from mne.datasets.brainstorm import bst_raw from mne.io import read_raw_ctf print(__doc__) tmin, tmax, event_id = -0.1, 0.3, 2 # take right-hand somato reject = dict(mag=4e-12, eog=250e-6) data_path = bst_raw.data_path() raw_path = (data_path + '/MEG/bst_raw/' + 'subj001_somatosensory_20111109_01_AUX-f.ds') raw = read_raw_ctf(raw_path, preload=True) raw.plot() # set EOG channel raw.set_channel_types({'EEG058': 'eog'}) raw.set_eeg_reference('average', projection=True) # show power line interference and remove it raw.plot_psd(tmax=60., average=False) raw.notch_filter(np.arange(60, 181, 60), fir_design='firwin') events = mne.find_events(raw, stim_channel='UPPT001') # pick MEG channels picks = mne.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, exclude='bads')
# Behavioral data path fname_bhv = path(op.join(path_data, subject, 'behavdata')).glob('*.csv') # Read behavioral file for fname_behavior in fname_bhv: events_behavior = get_events_from_mat(fname_behavior) # Read all raw to extract MEG event triggers runs = path(fname_raw).glob('*.ds') events_meg = list() run_number = 1 for run in runs: fname_raw = op.join(path_data, subject, run) print(fname_raw) # get raw and events from raw file (need non standard manipulation # because trigger baseline is 255 and not 0) raw = read_raw_ctf(fname_raw, preload=True, system_clock='ignore') channel_trigger = np.where(np.array(raw.ch_names) == 'USPT001')[0][0] # find where the trigger == 255 trigger_baseline = np.where(raw._data[channel_trigger, :] == 255)[0] # replace 255 values with 0 raw._data[channel_trigger, trigger_baseline] = 0. # find correct triggers events_meg_ = mne.find_events(raw) # to keep the run from which the event was found events_meg_[:, 1] = run_number events_meg.append(events_meg_) run_number = run_number + 1 events_meg = np.vstack(events_meg) # concatenate all meg events
def test_read_ctf(): """Test CTF reader""" temp_dir = _TempDir() out_fname = op.join(temp_dir, 'test_py_raw.fif') # Create a dummy .eeg file so we can test our reading/application of it os.mkdir(op.join(temp_dir, 'randpos')) ctf_eeg_fname = op.join(temp_dir, 'randpos', ctf_fname_catch) shutil.copytree(op.join(ctf_dir, ctf_fname_catch), ctf_eeg_fname) with warnings.catch_warnings(record=True) as w: # reclassified ch raw = _test_raw_reader(read_raw_ctf, directory=ctf_eeg_fname) assert_true(all('MISC channel' in str(ww.message) for ww in w)) picks = pick_types(raw.info, meg=False, eeg=True) pos = np.random.RandomState(42).randn(len(picks), 3) fake_eeg_fname = op.join(ctf_eeg_fname, 'catch-alp-good-f.eeg') # Create a bad file with open(fake_eeg_fname, 'wb') as fid: fid.write('foo\n'.encode('ascii')) assert_raises(RuntimeError, read_raw_ctf, ctf_eeg_fname) # Create a good file with open(fake_eeg_fname, 'wb') as fid: for ii, ch_num in enumerate(picks): args = (str(ch_num + 1), raw.ch_names[ch_num],) + tuple( '%0.5f' % x for x in 100 * pos[ii]) # convert to cm fid.write(('\t'.join(args) + '\n').encode('ascii')) pos_read_old = np.array([raw.info['chs'][p]['loc'][:3] for p in picks]) with warnings.catch_warnings(record=True) as w: # reclassified channel raw = read_raw_ctf(ctf_eeg_fname) # read modified data assert_true(all('MISC channel' in str(ww.message) for ww in w)) pos_read = np.array([raw.info['chs'][p]['loc'][:3] for p in picks]) assert_allclose(apply_trans(raw.info['ctf_head_t'], pos), pos_read, rtol=1e-5, atol=1e-5) assert_true((pos_read == pos_read_old).mean() < 0.1) shutil.copy(op.join(ctf_dir, 'catch-alp-good-f.ds_randpos_raw.fif'), op.join(temp_dir, 'randpos', 'catch-alp-good-f.ds_raw.fif')) # Create a version with no hc, starting out *with* EEG pos (error) os.mkdir(op.join(temp_dir, 'nohc')) ctf_no_hc_fname = op.join(temp_dir, 'no_hc', ctf_fname_catch) shutil.copytree(ctf_eeg_fname, ctf_no_hc_fname) remove_base = op.join(ctf_no_hc_fname, op.basename(ctf_fname_catch[:-3])) os.remove(remove_base + '.hc') with warnings.catch_warnings(record=True): # no coord tr assert_raises(RuntimeError, read_raw_ctf, ctf_no_hc_fname) os.remove(remove_base + '.eeg') shutil.copy(op.join(ctf_dir, 'catch-alp-good-f.ds_nohc_raw.fif'), op.join(temp_dir, 'no_hc', 'catch-alp-good-f.ds_raw.fif')) # All our files use_fnames = [op.join(ctf_dir, c) for c in ctf_fnames] for fname in use_fnames: raw_c = Raw(fname + '_raw.fif', add_eeg_ref=False, preload=True) with warnings.catch_warnings(record=True) as w: # reclassified ch raw = read_raw_ctf(fname) assert_true(all('MISC channel' in str(ww.message) for ww in w)) # check info match assert_array_equal(raw.ch_names, raw_c.ch_names) assert_allclose(raw.times, raw_c.times) assert_allclose(raw._cals, raw_c._cals) for key in ('version', 'usecs'): assert_equal(raw.info['meas_id'][key], raw_c.info['meas_id'][key]) py_time = raw.info['meas_id']['secs'] c_time = raw_c.info['meas_id']['secs'] max_offset = 24 * 60 * 60 # probably overkill but covers timezone assert_true(c_time - max_offset <= py_time <= c_time) for t in ('dev_head_t', 'dev_ctf_t', 'ctf_head_t'): assert_allclose(raw.info[t]['trans'], raw_c.info[t]['trans'], rtol=1e-4, atol=1e-7) for key in ('acq_pars', 'acq_stim', 'bads', 'ch_names', 'custom_ref_applied', 'description', 'events', 'experimenter', 'highpass', 'line_freq', 'lowpass', 'nchan', 'proj_id', 'proj_name', 'projs', 'sfreq', 'subject_info'): assert_equal(raw.info[key], raw_c.info[key], key) if op.basename(fname) not in single_trials: # We don't force buffer size to be smaller like MNE-C assert_equal(raw.info['buffer_size_sec'], raw_c.info['buffer_size_sec']) assert_equal(len(raw.info['comps']), len(raw_c.info['comps'])) for c1, c2 in zip(raw.info['comps'], raw_c.info['comps']): for key in ('colcals', 'rowcals'): assert_allclose(c1[key], c2[key]) assert_equal(c1['save_calibrated'], c2['save_calibrated']) for key in ('row_names', 'col_names', 'nrow', 'ncol'): assert_array_equal(c1['data'][key], c2['data'][key]) assert_allclose(c1['data']['data'], c2['data']['data'], atol=1e-7, rtol=1e-5) assert_allclose(raw.info['hpi_results'][0]['coord_trans']['trans'], raw_c.info['hpi_results'][0]['coord_trans']['trans'], rtol=1e-5, atol=1e-7) assert_equal(len(raw.info['chs']), len(raw_c.info['chs'])) for ii, (c1, c2) in enumerate(zip(raw.info['chs'], raw_c.info['chs'])): for key in ('kind', 'scanno', 'unit', 'ch_name', 'unit_mul', 'range', 'coord_frame', 'coil_type', 'logno'): if c1['ch_name'] == 'RMSP' and \ 'catch-alp-good-f' in fname and \ key in ('kind', 'unit', 'coord_frame', 'coil_type', 'logno'): continue # XXX see below... assert_equal(c1[key], c2[key], err_msg=key) for key in ('cal',): assert_allclose(c1[key], c2[key], atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) # XXX 2016/02/24: fixed bug with normal computation that used # to exist, once mne-C tools are updated we should update our FIF # conversion files, then the slices can go away (and the check # can be combined with that for "cal") for key in ('loc',): if c1['ch_name'] == 'RMSP' and 'catch-alp-good-f' in fname: continue assert_allclose(c1[key][:3], c2[key][:3], atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) assert_allclose(c1[key][9:12], c2[key][9:12], atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) if fname.endswith('catch-alp-good-f.ds'): # omit points from .pos file raw.info['dig'] = raw.info['dig'][:-10] assert_dig_allclose(raw.info, raw_c.info) # check data match raw_c.save(out_fname, overwrite=True, buffer_size_sec=1.) raw_read = Raw(out_fname, add_eeg_ref=False) # so let's check tricky cases based on sample boundaries rng = np.random.RandomState(0) pick_ch = rng.permutation(np.arange(len(raw.ch_names)))[:10] bnd = int(round(raw.info['sfreq'] * raw.info['buffer_size_sec'])) assert_equal(bnd, raw._raw_extras[0]['block_size']) assert_equal(bnd, block_sizes[op.basename(fname)]) slices = (slice(0, bnd), slice(bnd - 1, bnd), slice(3, bnd), slice(3, 300), slice(None)) if len(raw.times) >= 2 * bnd: # at least two complete blocks slices = slices + (slice(bnd, 2 * bnd), slice(bnd, bnd + 1), slice(0, bnd + 100)) for sl_time in slices: assert_allclose(raw[pick_ch, sl_time][0], raw_c[pick_ch, sl_time][0]) assert_allclose(raw_read[pick_ch, sl_time][0], raw_c[pick_ch, sl_time][0]) # all data / preload with warnings.catch_warnings(record=True) as w: # reclassified ch raw = read_raw_ctf(fname, preload=True) assert_true(all('MISC channel' in str(ww.message) for ww in w)) assert_allclose(raw[:][0], raw_c[:][0]) assert_raises(TypeError, read_raw_ctf, 1) assert_raises(ValueError, read_raw_ctf, ctf_fname_continuous + 'foo.ds') # test ignoring of system clock read_raw_ctf(op.join(ctf_dir, ctf_fname_continuous), 'ignore') assert_raises(ValueError, read_raw_ctf, op.join(ctf_dir, ctf_fname_continuous), 'foo')
def raw_ctf(): """Get ctf raw data from mne.io.tests.data.""" raw_ctf = read_raw_ctf(fname_ctf_continuous, preload=True) return raw_ctf
print('*** COMPUTE RAW COV ***') #locale.setlocale(locale.LC_ALL, "en_US.utf8") noise_fname_template = 'lyon_Noise*.ds' for sbj in subject_ids: print('\nsbj %s \n' %sbj) ds_path = op.join(noise_path, sbj, '*misc', noise_fname_template) for noise_ds_fname in glob.glob(ds_path): print('*** Empty room data file %s found!!!\n' % noise_ds_fname) if not op.isdir(noise_ds_fname): raise RuntimeError('*** Empty room data file %s NOT found!!!' % noise_ds_fname) raw = read_raw_ctf(noise_ds_fname) noise_fname = sbj + '_' + noise_fname_template.replace('*.ds', '-raw.fif') noise_fif_fname = op.join(data_path, sbj, noise_fname) print('*** Raw fif file name %s ***\n' % noise_fif_fname) raw.save(noise_fif_fname, overwrite=True) noise_cov_fname = noise_fif_fname.replace('-raw.fif', '-raw-cov.fif') print('*** Noise Cov file name %s ***\n' % noise_cov_fname) reject = create_reject_dict(raw.info) picks = pick_types(raw.info, meg=True, ref_meg=False, exclude='bads') noise_cov = compute_raw_covariance(raw, picks=picks, reject=reject) write_cov(noise_cov_fname, noise_cov)
from mne.datasets import spm_face from mne.preprocessing import ICA, create_eog_epochs from mne import io, combine_evoked from mne.minimum_norm import make_inverse_operator, apply_inverse print(__doc__) data_path = spm_face.data_path() subjects_dir = data_path + '/subjects' ############################################################################### # Load and filter data, set up epochs raw_fname = data_path + '/MEG/spm/SPM_CTF_MEG_example_faces%d_3D.ds' raw = io.read_raw_ctf(raw_fname % 1, preload=True) # Take first run # Here to save memory and time we'll downsample heavily -- this is not # advised for real data as it can effectively jitter events! raw.resample(120., npad='auto') picks = mne.pick_types(raw.info, meg=True, exclude='bads') raw.filter(1, 30, method='fir', fir_design='firwin') events = mne.find_events(raw, stim_channel='UPPT001') # plot the events to get an idea of the paradigm mne.viz.plot_events(events, raw.info['sfreq']) event_ids = {"faces": 1, "scrambled": 2} tmin, tmax = -0.2, 0.6
def test_read_ctf_annotations(): """Test reading CTF marker file.""" EXPECTED_LATENCIES = np.array([ 5640, 7950, 9990, 12253, 14171, 16557, 18896, 20846, # noqa 22702, 24990, 26830, 28974, 30906, 33077, 34985, 36907, # noqa 38922, 40760, 42881, 45222, 47457, 49618, 51802, 54227, # noqa 56171, 58274, 60394, 62375, 64444, 66767, 68827, 71109, # noqa 73499, 75807, 78146, 80415, 82554, 84508, 86403, 88426, # noqa 90746, 92893, 94779, 96822, 98996, 99001, 100949, 103325, # noqa 105322, 107678, 109667, 111844, 113682, 115817, 117691, 119663, # noqa 121966, 123831, 126110, 128490, 130521, 132808, 135204, 137210, # noqa 139130, 141390, 143660, 145748, 147889, 150205, 152528, 154646, # noqa 156897, 159191, 161446, 163722, 166077, 168467, 170624, 172519, # noqa 174719, 176886, 179062, 181405, 183709, 186034, 188454, 190330, # noqa 192660, 194682, 196834, 199161, 201035, 203008, 204999, 207409, # noqa 209661, 211895, 213957, 216005, 218040, 220178, 222137, 224305, # noqa 226297, 228654, 230755, 232909, 235205, 237373, 239723, 241762, # noqa 243748, 245762, 247801, 250055, 251886, 254252, 256441, 258354, # noqa 260680, 263026, 265048, 267073, 269235, 271556, 273927, 276197, # noqa 278436, 280536, 282691, 284933, 287061, 288936, 290941, 293183, # noqa 295369, 297729, 299626, 301546, 303449, 305548, 307882, 310124, # noqa 312374, 314509, 316815, 318789, 320981, 322879, 324878, 326959, # noqa 329341, 331200, 331201, 333469, 335584, 337984, 340143, 342034, # noqa 344360, 346309, 348544, 350970, 353052, 355227, 357449, 359603, # noqa 361725, 363676, 365735, 367799, 369777, 371904, 373856, 376204, # noqa 378391, 380800, 382859, 385161, 387093, 389434, 391624, 393785, # noqa 396093, 398214, 400198, 402166, 404104, 406047, 408372, 410686, # noqa 413029, 414975, 416850, 418797, 420824, 422959, 425026, 427215, # noqa 429278, 431668 # noqa ]) - 1 # Fieldtrip has 1 sample difference with MNE raw = RawArray(data=np.empty((1, 432000), dtype=np.float64), info=create_info(ch_names=1, sfreq=1200.0)) raw.set_meas_date(read_raw_ctf(somato_fname).info['meas_date']) raw.set_annotations(read_annotations(somato_fname)) events, _ = events_from_annotations(raw) latencies = np.sort(events[:, 0]) assert_allclose(latencies, EXPECTED_LATENCIES, atol=1e-6)
def test_plot_alignment(tmpdir, backends_3d): """Test plotting of -trans.fif files and MEG sensor layouts.""" from mne.viz.backends.renderer import _Renderer backend_name = get_3d_backend() # generate fiducials file for testing tempdir = str(tmpdir) fiducials_path = op.join(tempdir, 'fiducials.fif') fid = [{ 'coord_frame': 5, 'ident': 1, 'kind': 1, 'r': [-0.08061612, -0.02908875, -0.04131077] }, { 'coord_frame': 5, 'ident': 2, 'kind': 1, 'r': [0.00146763, 0.08506715, -0.03483611] }, { 'coord_frame': 5, 'ident': 3, 'kind': 1, 'r': [0.08436285, -0.02850276, -0.04127743] }] write_dig(fiducials_path, fid, 5) if backend_name == 'mayavi': mlab = _import_mlab() evoked = read_evokeds(evoked_fname)[0] sample_src = read_source_spaces(src_fname) bti = read_raw_bti(pdf_fname, config_fname, hs_fname, convert=True, preload=False).info infos = dict( Neuromag=evoked.info, CTF=read_raw_ctf(ctf_fname).info, BTi=bti, KIT=read_raw_kit(sqd_fname).info, ) for system, info in infos.items(): meg = ['helmet', 'sensors'] if system == 'KIT': meg.append('ref') fig = plot_alignment(info, trans_fname, subject='sample', subjects_dir=subjects_dir, meg=meg) renderer = _Renderer(fig=fig) renderer.close() # KIT ref sensor coil def is defined if backend_name == 'mayavi': mlab.close(all=True) info = infos['Neuromag'] pytest.raises(TypeError, plot_alignment, 'foo', trans_fname, subject='sample', subjects_dir=subjects_dir) pytest.raises(OSError, plot_alignment, info, trans_fname, subject='sample', subjects_dir=subjects_dir, src='foo') pytest.raises(ValueError, plot_alignment, info, trans_fname, subject='fsaverage', subjects_dir=subjects_dir, src=sample_src) sample_src.plot(subjects_dir=subjects_dir, head=True, skull=True, brain='white') if backend_name == 'mayavi': mlab.close(all=True) # no-head version if backend_name == 'mayavi': mlab.close(all=True) # all coord frames pytest.raises(ValueError, plot_alignment, info) plot_alignment(info, surfaces=[]) for coord_frame in ('meg', 'head', 'mri'): fig = plot_alignment(info, meg=['helmet', 'sensors'], dig=True, coord_frame=coord_frame, trans=trans_fname, subject='sample', mri_fiducials=fiducials_path, subjects_dir=subjects_dir, src=src_fname) renderer = _Renderer(fig=fig) renderer.close() # EEG only with strange options evoked_eeg_ecog_seeg = evoked.copy().pick_types(meg=False, eeg=True) evoked_eeg_ecog_seeg.info['projs'] = [] # "remove" avg proj evoked_eeg_ecog_seeg.set_channel_types({ 'EEG 001': 'ecog', 'EEG 002': 'seeg' }) with pytest.warns(RuntimeWarning, match='Cannot plot MEG'): plot_alignment(evoked_eeg_ecog_seeg.info, subject='sample', trans=trans_fname, subjects_dir=subjects_dir, surfaces=['white', 'outer_skin', 'outer_skull'], meg=['helmet', 'sensors'], eeg=['original', 'projected'], ecog=True, seeg=True) if backend_name == 'mayavi': mlab.close(all=True) sphere = make_sphere_model(info=evoked.info, r0='auto', head_radius='auto') bem_sol = read_bem_solution( op.join(subjects_dir, 'sample', 'bem', 'sample-1280-1280-1280-bem-sol.fif')) bem_surfs = read_bem_surfaces( op.join(subjects_dir, 'sample', 'bem', 'sample-1280-1280-1280-bem.fif')) sample_src[0]['coord_frame'] = 4 # hack for coverage plot_alignment( info, subject='sample', eeg='projected', meg='helmet', bem=sphere, dig=True, surfaces=['brain', 'inner_skull', 'outer_skull', 'outer_skin']) plot_alignment(info, trans_fname, subject='sample', meg='helmet', subjects_dir=subjects_dir, eeg='projected', bem=sphere, surfaces=['head', 'brain'], src=sample_src) assert all(surf['coord_frame'] == FIFF.FIFFV_COORD_MRI for surf in bem_sol['surfs']) plot_alignment(info, trans_fname, subject='sample', meg=[], subjects_dir=subjects_dir, bem=bem_sol, eeg=True, surfaces=['head', 'inflated', 'outer_skull', 'inner_skull']) assert all(surf['coord_frame'] == FIFF.FIFFV_COORD_MRI for surf in bem_sol['surfs']) plot_alignment(info, trans_fname, subject='sample', meg=True, subjects_dir=subjects_dir, surfaces=['head', 'inner_skull'], bem=bem_surfs) sphere = make_sphere_model('auto', 'auto', evoked.info) src = setup_volume_source_space(sphere=sphere) plot_alignment( info, eeg='projected', meg='helmet', bem=sphere, src=src, dig=True, surfaces=['brain', 'inner_skull', 'outer_skull', 'outer_skin']) sphere = make_sphere_model('auto', None, evoked.info) # one layer # no info is permitted fig = plot_alignment(trans=trans_fname, subject='sample', meg=False, coord_frame='mri', subjects_dir=subjects_dir, surfaces=['brain'], bem=sphere, show_axes=True) if backend_name == 'mayavi': import mayavi # noqa: F401 analysis:ignore assert isinstance(fig, mayavi.core.scene.Scene) # 3D coil with no defined draw (ConvexHull) info_cube = pick_info(info, [0]) info['dig'] = None info_cube['chs'][0]['coil_type'] = 9999 with pytest.raises(RuntimeError, match='coil definition not found'): plot_alignment(info_cube, meg='sensors', surfaces=()) coil_def_fname = op.join(tempdir, 'temp') with open(coil_def_fname, 'w') as fid: fid.write(coil_3d) with use_coil_def(coil_def_fname): plot_alignment(info_cube, meg='sensors', surfaces=(), dig=True) # one layer bem with skull surfaces: pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['brain', 'head', 'inner_skull'], bem=sphere) # wrong eeg value: pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, eeg='foo') # wrong meg value: pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, meg='bar') # multiple brain surfaces: pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['white', 'pial']) pytest.raises(TypeError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=[1]) pytest.raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['foo']) if backend_name == 'mayavi': mlab.close(all=True)
from mne.datasets.brainstorm import bst_phantom_ctf from mne.io import read_raw_ctf print(__doc__) ############################################################################### # The data were collected with a CTF system at 2400 Hz. data_path = bst_phantom_ctf.data_path() # Switch to these to use the higher-SNR data: # raw_path = op.join(data_path, 'phantom_200uA_20150709_01.ds') # dip_freq = 7. raw_path = op.join(data_path, 'phantom_20uA_20150603_03.ds') dip_freq = 23. erm_path = op.join(data_path, 'emptyroom_20150709_01.ds') raw = read_raw_ctf(raw_path, preload=True) ############################################################################### # The sinusoidal signal is generated on channel HDAC006, so we can use # that to obtain precise timing. sinusoid, times = raw[raw.ch_names.index('HDAC006-4408')] plt.figure() plt.plot(times[times < 1.], sinusoid.T[times < 1.]) ############################################################################### # Let's create some events using this signal by thresholding the sinusoid. events = np.where(np.diff(sinusoid > 0.5) > 0)[1] + raw.first_samp events = np.vstack((events, np.zeros_like(events), np.ones_like(events))).T
def test_read_ctf(): """Test CTF reader""" temp_dir = _TempDir() out_fname = op.join(temp_dir, 'test_py_raw.fif') # Create a dummy .eeg file so we can test our reading/application of it os.mkdir(op.join(temp_dir, 'randpos')) ctf_eeg_fname = op.join(temp_dir, 'randpos', ctf_fname_catch) shutil.copytree(op.join(ctf_dir, ctf_fname_catch), ctf_eeg_fname) with warnings.catch_warnings(record=True) as w: # reclassified ch raw = _test_raw_reader(read_raw_ctf, directory=ctf_eeg_fname) assert_true(all('MISC channel' in str(ww.message) for ww in w)) picks = pick_types(raw.info, meg=False, eeg=True) pos = np.random.RandomState(42).randn(len(picks), 3) fake_eeg_fname = op.join(ctf_eeg_fname, 'catch-alp-good-f.eeg') # Create a bad file with open(fake_eeg_fname, 'wb') as fid: fid.write('foo\n'.encode('ascii')) assert_raises(RuntimeError, read_raw_ctf, ctf_eeg_fname) # Create a good file with open(fake_eeg_fname, 'wb') as fid: for ii, ch_num in enumerate(picks): args = ( str(ch_num + 1), raw.ch_names[ch_num], ) + tuple('%0.5f' % x for x in 100 * pos[ii]) # convert to cm fid.write(('\t'.join(args) + '\n').encode('ascii')) pos_read_old = np.array([raw.info['chs'][p]['loc'][:3] for p in picks]) with warnings.catch_warnings(record=True) as w: # reclassified channel raw = read_raw_ctf(ctf_eeg_fname) # read modified data assert_true(all('MISC channel' in str(ww.message) for ww in w)) pos_read = np.array([raw.info['chs'][p]['loc'][:3] for p in picks]) assert_allclose(apply_trans(raw.info['ctf_head_t'], pos), pos_read, rtol=1e-5, atol=1e-5) assert_true((pos_read == pos_read_old).mean() < 0.1) shutil.copy(op.join(ctf_dir, 'catch-alp-good-f.ds_randpos_raw.fif'), op.join(temp_dir, 'randpos', 'catch-alp-good-f.ds_raw.fif')) # Create a version with no hc, starting out *with* EEG pos (error) os.mkdir(op.join(temp_dir, 'nohc')) ctf_no_hc_fname = op.join(temp_dir, 'no_hc', ctf_fname_catch) shutil.copytree(ctf_eeg_fname, ctf_no_hc_fname) remove_base = op.join(ctf_no_hc_fname, op.basename(ctf_fname_catch[:-3])) os.remove(remove_base + '.hc') with warnings.catch_warnings(record=True): # no coord tr assert_raises(RuntimeError, read_raw_ctf, ctf_no_hc_fname) os.remove(remove_base + '.eeg') shutil.copy(op.join(ctf_dir, 'catch-alp-good-f.ds_nohc_raw.fif'), op.join(temp_dir, 'no_hc', 'catch-alp-good-f.ds_raw.fif')) # All our files use_fnames = [op.join(ctf_dir, c) for c in ctf_fnames] for fname in use_fnames: raw_c = Raw(fname + '_raw.fif', add_eeg_ref=False, preload=True) with warnings.catch_warnings(record=True) as w: # reclassified ch raw = read_raw_ctf(fname) assert_true(all('MISC channel' in str(ww.message) for ww in w)) # check info match assert_array_equal(raw.ch_names, raw_c.ch_names) assert_allclose(raw.times, raw_c.times) assert_allclose(raw._cals, raw_c._cals) for key in ('version', 'usecs'): assert_equal(raw.info['meas_id'][key], raw_c.info['meas_id'][key]) py_time = raw.info['meas_id']['secs'] c_time = raw_c.info['meas_id']['secs'] max_offset = 24 * 60 * 60 # probably overkill but covers timezone assert_true(c_time - max_offset <= py_time <= c_time) for t in ('dev_head_t', 'dev_ctf_t', 'ctf_head_t'): assert_allclose(raw.info[t]['trans'], raw_c.info[t]['trans'], rtol=1e-4, atol=1e-7) for key in ('acq_pars', 'acq_stim', 'bads', 'ch_names', 'custom_ref_applied', 'description', 'events', 'experimenter', 'highpass', 'line_freq', 'lowpass', 'nchan', 'proj_id', 'proj_name', 'projs', 'sfreq', 'subject_info'): assert_equal(raw.info[key], raw_c.info[key], key) if op.basename(fname) not in single_trials: # We don't force buffer size to be smaller like MNE-C assert_equal(raw.info['buffer_size_sec'], raw_c.info['buffer_size_sec']) assert_equal(len(raw.info['comps']), len(raw_c.info['comps'])) for c1, c2 in zip(raw.info['comps'], raw_c.info['comps']): for key in ('colcals', 'rowcals'): assert_allclose(c1[key], c2[key]) assert_equal(c1['save_calibrated'], c2['save_calibrated']) for key in ('row_names', 'col_names', 'nrow', 'ncol'): assert_array_equal(c1['data'][key], c2['data'][key]) assert_allclose(c1['data']['data'], c2['data']['data'], atol=1e-7, rtol=1e-5) assert_allclose(raw.info['hpi_results'][0]['coord_trans']['trans'], raw_c.info['hpi_results'][0]['coord_trans']['trans'], rtol=1e-5, atol=1e-7) assert_equal(len(raw.info['chs']), len(raw_c.info['chs'])) for ii, (c1, c2) in enumerate(zip(raw.info['chs'], raw_c.info['chs'])): for key in ('kind', 'scanno', 'unit', 'ch_name', 'unit_mul', 'range', 'coord_frame', 'coil_type', 'logno'): if c1['ch_name'] == 'RMSP' and \ 'catch-alp-good-f' in fname and \ key in ('kind', 'unit', 'coord_frame', 'coil_type', 'logno'): continue # XXX see below... assert_equal(c1[key], c2[key], err_msg=key) for key in ('cal', ): assert_allclose(c1[key], c2[key], atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) # XXX 2016/02/24: fixed bug with normal computation that used # to exist, once mne-C tools are updated we should update our FIF # conversion files, then the slices can go away (and the check # can be combined with that for "cal") for key in ('loc', ): if c1['ch_name'] == 'RMSP' and 'catch-alp-good-f' in fname: continue assert_allclose(c1[key][:3], c2[key][:3], atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) assert_allclose(c1[key][9:12], c2[key][9:12], atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) if fname.endswith('catch-alp-good-f.ds'): # omit points from .pos file raw.info['dig'] = raw.info['dig'][:-10] assert_dig_allclose(raw.info, raw_c.info) # check data match raw_c.save(out_fname, overwrite=True, buffer_size_sec=1.) raw_read = Raw(out_fname, add_eeg_ref=False) # so let's check tricky cases based on sample boundaries rng = np.random.RandomState(0) pick_ch = rng.permutation(np.arange(len(raw.ch_names)))[:10] bnd = int(round(raw.info['sfreq'] * raw.info['buffer_size_sec'])) assert_equal(bnd, raw._raw_extras[0]['block_size']) assert_equal(bnd, block_sizes[op.basename(fname)]) slices = (slice(0, bnd), slice(bnd - 1, bnd), slice(3, bnd), slice(3, 300), slice(None)) if len(raw.times) >= 2 * bnd: # at least two complete blocks slices = slices + (slice(bnd, 2 * bnd), slice( bnd, bnd + 1), slice(0, bnd + 100)) for sl_time in slices: assert_allclose(raw[pick_ch, sl_time][0], raw_c[pick_ch, sl_time][0]) assert_allclose(raw_read[pick_ch, sl_time][0], raw_c[pick_ch, sl_time][0]) # all data / preload with warnings.catch_warnings(record=True) as w: # reclassified ch raw = read_raw_ctf(fname, preload=True) assert_true(all('MISC channel' in str(ww.message) for ww in w)) assert_allclose(raw[:][0], raw_c[:][0]) raw.plot(show=False) # Test plotting with ref_meg channels. assert_raises(TypeError, read_raw_ctf, 1) assert_raises(ValueError, read_raw_ctf, ctf_fname_continuous + 'foo.ds') # test ignoring of system clock read_raw_ctf(op.join(ctf_dir, ctf_fname_continuous), 'ignore') assert_raises(ValueError, read_raw_ctf, op.join(ctf_dir, ctf_fname_continuous), 'foo')
# License: BSD-3-Clause # %% import os.path as op import matplotlib.pyplot as plt import numpy as np from mne.datasets.brainstorm import bst_auditory from mne.io import read_raw_ctf from mne.preprocessing import annotate_muscle_zscore # Load data data_path = bst_auditory.data_path() raw_fname = op.join(data_path, 'MEG', 'bst_auditory', 'S01_AEF_20131218_01.ds') raw = read_raw_ctf(raw_fname, preload=False) raw.crop(130, 160).load_data() # just use a fraction of data for speed here raw.resample(300, npad="auto") # %% # Notch filter the data: # # .. note:: # If line noise is present, you should perform notch-filtering *before* # detecting muscle artifacts. See :ref:`tut-section-line-noise` for an # example. raw.notch_filter([50, 100]) # %%
def test_plot_alignment(): """Test plotting of -trans.fif files and MEG sensor layouts.""" # generate fiducials file for testing tempdir = _TempDir() fiducials_path = op.join(tempdir, 'fiducials.fif') fid = [{ 'coord_frame': 5, 'ident': 1, 'kind': 1, 'r': [-0.08061612, -0.02908875, -0.04131077] }, { 'coord_frame': 5, 'ident': 2, 'kind': 1, 'r': [0.00146763, 0.08506715, -0.03483611] }, { 'coord_frame': 5, 'ident': 3, 'kind': 1, 'r': [0.08436285, -0.02850276, -0.04127743] }] write_dig(fiducials_path, fid, 5) mlab = _import_mlab() evoked = read_evokeds(evoked_fname)[0] sample_src = read_source_spaces(src_fname) with warnings.catch_warnings(record=True): # 4D weight tables bti = read_raw_bti(pdf_fname, config_fname, hs_fname, convert=True, preload=False).info infos = dict( Neuromag=evoked.info, CTF=read_raw_ctf(ctf_fname).info, BTi=bti, KIT=read_raw_kit(sqd_fname).info, ) for system, info in infos.items(): meg = ['helmet', 'sensors'] if system == 'KIT': meg.append('ref') plot_alignment(info, trans_fname, subject='sample', subjects_dir=subjects_dir, meg=meg) mlab.close(all=True) # KIT ref sensor coil def is defined plot_trans(infos['KIT'], None, meg_sensors=True, ref_meg=True) mlab.close(all=True) info = infos['Neuromag'] assert_raises(TypeError, plot_alignment, 'foo', trans_fname, subject='sample', subjects_dir=subjects_dir) assert_raises(TypeError, plot_alignment, info, trans_fname, subject='sample', subjects_dir=subjects_dir, src='foo') assert_raises(ValueError, plot_alignment, info, trans_fname, subject='fsaverage', subjects_dir=subjects_dir, src=sample_src) sample_src.plot(subjects_dir=subjects_dir, head=True, skull=True, brain='white') mlab.close(all=True) # no-head version plot_trans(info, None, meg_sensors=True, dig=True, coord_frame='head') mlab.close(all=True) # all coord frames for coord_frame in ('meg', 'head', 'mri'): plot_alignment(info, meg=['helmet', 'sensors'], dig=True, coord_frame=coord_frame, trans=trans_fname, subject='sample', mri_fiducials=fiducials_path, subjects_dir=subjects_dir, src=sample_src) mlab.close(all=True) # EEG only with strange options evoked_eeg_ecog = evoked.copy().pick_types(meg=False, eeg=True) evoked_eeg_ecog.info['projs'] = [] # "remove" avg proj evoked_eeg_ecog.set_channel_types({'EEG 001': 'ecog'}) with warnings.catch_warnings(record=True) as w: plot_alignment(evoked_eeg_ecog.info, subject='sample', trans=trans_fname, subjects_dir=subjects_dir, surfaces=['white', 'outer_skin', 'outer_skull'], meg=['helmet', 'sensors'], eeg=['original', 'projected'], ecog=True) mlab.close(all=True) assert_true(['Cannot plot MEG' in str(ww.message) for ww in w]) sphere = make_sphere_model(info=evoked.info, r0='auto', head_radius='auto') bem_sol = read_bem_solution( op.join(subjects_dir, 'sample', 'bem', 'sample-1280-1280-1280-bem-sol.fif')) bem_surfs = read_bem_surfaces( op.join(subjects_dir, 'sample', 'bem', 'sample-1280-1280-1280-bem.fif')) sample_src[0]['coord_frame'] = 4 # hack for coverage plot_alignment(info, trans_fname, subject='sample', meg='helmet', subjects_dir=subjects_dir, eeg='projected', bem=sphere, surfaces=['head', 'brain', 'inner_skull', 'outer_skull'], src=sample_src) plot_alignment(info, trans_fname, subject='sample', meg=[], subjects_dir=subjects_dir, bem=bem_sol, eeg=True, surfaces=['head', 'inflated', 'outer_skull', 'inner_skull']) plot_alignment(info, trans_fname, subject='sample', meg=True, subjects_dir=subjects_dir, surfaces=['head', 'inner_skull'], bem=bem_surfs) sphere = make_sphere_model('auto', None, evoked.info) # one layer plot_alignment(info, trans_fname, subject='sample', meg=False, coord_frame='mri', subjects_dir=subjects_dir, surfaces=['brain'], bem=sphere) # one layer bem with skull surfaces: assert_raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['brain', 'head', 'inner_skull'], bem=sphere) # wrong eeg value: assert_raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, eeg='foo') # wrong meg value: assert_raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, meg='bar') # multiple brain surfaces: assert_raises(ValueError, plot_alignment, info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['white', 'pial'])
from mne import io from mne.datasets import spm_face from mne.minimum_norm import apply_inverse, make_inverse_operator from mne.cov import compute_covariance print(__doc__) ############################################################################## # Get data data_path = spm_face.data_path() subjects_dir = data_path + '/subjects' raw_fname = data_path + '/MEG/spm/SPM_CTF_MEG_example_faces%d_3D.ds' raw = io.read_raw_ctf(raw_fname % 1) # Take first run # To save time and memory for this demo, we'll just use the first # 2.5 minutes (all we need to get 30 total events) and heavily # resample 480->60 Hz (usually you wouldn't do either of these!) raw = raw.crop(0, 150.).load_data() picks = mne.pick_types(raw.info, meg=True, exclude='bads') raw.filter(1, 20., n_jobs=1, fir_design='firwin') events = mne.find_events(raw, stim_channel='UPPT001') event_ids = {"faces": 1, "scrambled": 2} tmin, tmax = -0.2, 0.5 baseline = None # no baseline as high-pass is applied reject = dict(mag=3e-12)
def test_read_ctf(tmpdir): """Test CTF reader.""" temp_dir = str(tmpdir) out_fname = op.join(temp_dir, 'test_py_raw.fif') # Create a dummy .eeg file so we can test our reading/application of it os.mkdir(op.join(temp_dir, 'randpos')) ctf_eeg_fname = op.join(temp_dir, 'randpos', ctf_fname_catch) shutil.copytree(op.join(ctf_dir, ctf_fname_catch), ctf_eeg_fname) with pytest.warns(RuntimeWarning, match='RMSP .* changed to a MISC ch'): raw = _test_raw_reader(read_raw_ctf, directory=ctf_eeg_fname) picks = pick_types(raw.info, meg=False, eeg=True) pos = np.random.RandomState(42).randn(len(picks), 3) fake_eeg_fname = op.join(ctf_eeg_fname, 'catch-alp-good-f.eeg') # Create a bad file with open(fake_eeg_fname, 'wb') as fid: fid.write('foo\n'.encode('ascii')) pytest.raises(RuntimeError, read_raw_ctf, ctf_eeg_fname) # Create a good file with open(fake_eeg_fname, 'wb') as fid: for ii, ch_num in enumerate(picks): args = (str(ch_num + 1), raw.ch_names[ch_num],) + tuple( '%0.5f' % x for x in 100 * pos[ii]) # convert to cm fid.write(('\t'.join(args) + '\n').encode('ascii')) pos_read_old = np.array([raw.info['chs'][p]['loc'][:3] for p in picks]) with pytest.warns(RuntimeWarning, match='RMSP .* changed to a MISC ch'): raw = read_raw_ctf(ctf_eeg_fname) # read modified data pos_read = np.array([raw.info['chs'][p]['loc'][:3] for p in picks]) assert_allclose(apply_trans(raw.info['ctf_head_t'], pos), pos_read, rtol=1e-5, atol=1e-5) assert (pos_read == pos_read_old).mean() < 0.1 shutil.copy(op.join(ctf_dir, 'catch-alp-good-f.ds_randpos_raw.fif'), op.join(temp_dir, 'randpos', 'catch-alp-good-f.ds_raw.fif')) # Create a version with no hc, starting out *with* EEG pos (error) os.mkdir(op.join(temp_dir, 'nohc')) ctf_no_hc_fname = op.join(temp_dir, 'no_hc', ctf_fname_catch) shutil.copytree(ctf_eeg_fname, ctf_no_hc_fname) remove_base = op.join(ctf_no_hc_fname, op.basename(ctf_fname_catch[:-3])) os.remove(remove_base + '.hc') with pytest.warns(RuntimeWarning, match='MISC channel'): pytest.raises(RuntimeError, read_raw_ctf, ctf_no_hc_fname) os.remove(remove_base + '.eeg') shutil.copy(op.join(ctf_dir, 'catch-alp-good-f.ds_nohc_raw.fif'), op.join(temp_dir, 'no_hc', 'catch-alp-good-f.ds_raw.fif')) # All our files use_fnames = [op.join(ctf_dir, c) for c in ctf_fnames] for fname in use_fnames: raw_c = read_raw_fif(fname + '_raw.fif', preload=True) with pytest.warns(None): # sometimes matches "MISC channel" raw = read_raw_ctf(fname) # check info match assert_array_equal(raw.ch_names, raw_c.ch_names) assert_allclose(raw.times, raw_c.times) assert_allclose(raw._cals, raw_c._cals) assert (raw.info['meas_id']['version'] == raw_c.info['meas_id']['version'] + 1) for t in ('dev_head_t', 'dev_ctf_t', 'ctf_head_t'): assert_allclose(raw.info[t]['trans'], raw_c.info[t]['trans'], rtol=1e-4, atol=1e-7) # XXX 2019/11/29 : MNC-C FIF conversion files don't have meas_date set. # Consider adding meas_date to below checks once this is addressed in # MNE-C for key in ('acq_pars', 'acq_stim', 'bads', 'ch_names', 'custom_ref_applied', 'description', 'events', 'experimenter', 'highpass', 'line_freq', 'lowpass', 'nchan', 'proj_id', 'proj_name', 'projs', 'sfreq', 'subject_info'): assert raw.info[key] == raw_c.info[key], key if op.basename(fname) not in single_trials: # We don't force buffer size to be smaller like MNE-C assert raw.buffer_size_sec == raw_c.buffer_size_sec assert len(raw.info['comps']) == len(raw_c.info['comps']) for c1, c2 in zip(raw.info['comps'], raw_c.info['comps']): for key in ('colcals', 'rowcals'): assert_allclose(c1[key], c2[key]) assert c1['save_calibrated'] == c2['save_calibrated'] for key in ('row_names', 'col_names', 'nrow', 'ncol'): assert_array_equal(c1['data'][key], c2['data'][key]) assert_allclose(c1['data']['data'], c2['data']['data'], atol=1e-7, rtol=1e-5) assert_allclose(raw.info['hpi_results'][0]['coord_trans']['trans'], raw_c.info['hpi_results'][0]['coord_trans']['trans'], rtol=1e-5, atol=1e-7) assert len(raw.info['chs']) == len(raw_c.info['chs']) for ii, (c1, c2) in enumerate(zip(raw.info['chs'], raw_c.info['chs'])): for key in ('kind', 'scanno', 'unit', 'ch_name', 'unit_mul', 'range', 'coord_frame', 'coil_type', 'logno'): if c1['ch_name'] == 'RMSP' and \ 'catch-alp-good-f' in fname and \ key in ('kind', 'unit', 'coord_frame', 'coil_type', 'logno'): continue # XXX see below... if key == 'coil_type' and c1[key] == FIFF.FIFFV_COIL_EEG: # XXX MNE-C bug that this is not set assert c2[key] == FIFF.FIFFV_COIL_NONE continue assert c1[key] == c2[key], key for key in ('cal',): assert_allclose(c1[key], c2[key], atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) # XXX 2016/02/24: fixed bug with normal computation that used # to exist, once mne-C tools are updated we should update our FIF # conversion files, then the slices can go away (and the check # can be combined with that for "cal") for key in ('loc',): if c1['ch_name'] == 'RMSP' and 'catch-alp-good-f' in fname: continue if (c2[key][:3] == 0.).all(): check = [np.nan] * 3 else: check = c2[key][:3] assert_allclose(c1[key][:3], check, atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) if (c2[key][3:] == 0.).all(): check = [np.nan] * 3 else: check = c2[key][9:12] assert_allclose(c1[key][9:12], check, atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) # Make sure all digitization points are in the MNE head coord frame for p in raw.info['dig']: assert p['coord_frame'] == FIFF.FIFFV_COORD_HEAD, \ 'dig points must be in FIFF.FIFFV_COORD_HEAD' if fname.endswith('catch-alp-good-f.ds'): # omit points from .pos file raw.info['dig'] = raw.info['dig'][:-10] # XXX: Next test would fail because c-tools assign the fiducials from # CTF data as HPI. Should eventually clarify/unify with Matti. # assert_dig_allclose(raw.info, raw_c.info) # check data match raw_c.save(out_fname, overwrite=True, buffer_size_sec=1.) raw_read = read_raw_fif(out_fname) # so let's check tricky cases based on sample boundaries rng = np.random.RandomState(0) pick_ch = rng.permutation(np.arange(len(raw.ch_names)))[:10] bnd = int(round(raw.info['sfreq'] * raw.buffer_size_sec)) assert bnd == raw._raw_extras[0]['block_size'] assert bnd == block_sizes[op.basename(fname)] slices = (slice(0, bnd), slice(bnd - 1, bnd), slice(3, bnd), slice(3, 300), slice(None)) if len(raw.times) >= 2 * bnd: # at least two complete blocks slices = slices + (slice(bnd, 2 * bnd), slice(bnd, bnd + 1), slice(0, bnd + 100)) for sl_time in slices: assert_allclose(raw[pick_ch, sl_time][0], raw_c[pick_ch, sl_time][0]) assert_allclose(raw_read[pick_ch, sl_time][0], raw_c[pick_ch, sl_time][0]) # all data / preload raw.load_data() assert_allclose(raw[:][0], raw_c[:][0], atol=1e-15) # test bad segment annotations if 'testdata_ctf_short.ds' in fname: assert 'bad' in raw.annotations.description[0] assert_allclose(raw.annotations.onset, [2.15]) assert_allclose(raw.annotations.duration, [0.0225]) pytest.raises(TypeError, read_raw_ctf, 1) pytest.raises(ValueError, read_raw_ctf, ctf_fname_continuous + 'foo.ds') # test ignoring of system clock read_raw_ctf(op.join(ctf_dir, ctf_fname_continuous), 'ignore') pytest.raises(ValueError, read_raw_ctf, op.join(ctf_dir, ctf_fname_continuous), 'foo')
def test_compute_proj_ctf(): """Test to show that projector code completes on CTF data.""" raw = read_raw_ctf(ctf_fname, preload=True) # expected channels per projector type mag_picks = pick_types(raw.info, meg='mag', ref_meg=False, exclude='bads')[::10] n_mags = len(mag_picks) grad_picks = pick_types(raw.info, meg='grad', ref_meg=False, exclude='bads')[::10] n_grads = len(grad_picks) eeg_picks = pick_types(raw.info, meg=False, eeg=True, ref_meg=False, exclude='bads')[2::3] n_eegs = len(eeg_picks) ref_picks = pick_types(raw.info, meg=False, ref_meg=True) raw.pick( np.sort(np.concatenate([mag_picks, grad_picks, eeg_picks, ref_picks]))) del mag_picks, grad_picks, eeg_picks, ref_picks # Test with and without gradient compensation raw.apply_gradient_compensation(0) n_projs_init = len(raw.info['projs']) with pytest.warns(RuntimeWarning, match='Attenuation'): projs, _ = compute_proj_eog(raw, n_mag=2, n_grad=2, n_eeg=2, average=True, ch_name='EEG059', avg_ref=True, no_proj=False, l_freq=None, h_freq=None, reject=None, tmax=dur_use, filter_length=1000) _check_projs_for_expected_channels(projs, n_mags, n_grads, n_eegs) assert len(projs) == (5 + n_projs_init) raw.apply_gradient_compensation(1) with pytest.warns(RuntimeWarning, match='Attenuation'): projs, _ = compute_proj_ecg(raw, n_mag=1, n_grad=1, n_eeg=2, average=True, ch_name='EEG059', avg_ref=True, no_proj=False, l_freq=None, h_freq=None, reject=None, tmax=dur_use, filter_length=1000) _check_projs_for_expected_channels(projs, n_mags, n_grads, n_eegs) assert len(projs) == (4 + n_projs_init)
def test_plot_alignment(tmpdir, renderer): """Test plotting of -trans.fif files and MEG sensor layouts.""" # generate fiducials file for testing tempdir = str(tmpdir) fiducials_path = op.join(tempdir, 'fiducials.fif') fid = [{ 'coord_frame': 5, 'ident': 1, 'kind': 1, 'r': [-0.08061612, -0.02908875, -0.04131077] }, { 'coord_frame': 5, 'ident': 2, 'kind': 1, 'r': [0.00146763, 0.08506715, -0.03483611] }, { 'coord_frame': 5, 'ident': 3, 'kind': 1, 'r': [0.08436285, -0.02850276, -0.04127743] }] write_dig(fiducials_path, fid, 5) renderer._close_all() evoked = read_evokeds(evoked_fname)[0] sample_src = read_source_spaces(src_fname) bti = read_raw_bti(pdf_fname, config_fname, hs_fname, convert=True, preload=False).info infos = dict( Neuromag=evoked.info, CTF=read_raw_ctf(ctf_fname).info, BTi=bti, KIT=read_raw_kit(sqd_fname).info, ) for system, info in infos.items(): meg = ['helmet', 'sensors'] if system == 'KIT': meg.append('ref') fig = plot_alignment(info, read_trans(trans_fname), subject='sample', subjects_dir=subjects_dir, meg=meg) rend = renderer._Renderer(fig=fig) rend.close() # KIT ref sensor coil def is defined renderer._close_all() info = infos['Neuromag'] pytest.raises(TypeError, plot_alignment, 'foo', trans_fname, subject='sample', subjects_dir=subjects_dir) pytest.raises(OSError, plot_alignment, info, trans_fname, subject='sample', subjects_dir=subjects_dir, src='foo') pytest.raises(ValueError, plot_alignment, info, trans_fname, subject='fsaverage', subjects_dir=subjects_dir, src=sample_src) sample_src.plot(subjects_dir=subjects_dir, head=True, skull=True, brain='white') renderer._close_all() # no-head version renderer._close_all() # all coord frames pytest.raises(ValueError, plot_alignment, info) plot_alignment(info, surfaces=[]) for coord_frame in ('meg', 'head', 'mri'): fig = plot_alignment(info, meg=['helmet', 'sensors'], dig=True, coord_frame=coord_frame, trans=Path(trans_fname), subject='sample', mri_fiducials=fiducials_path, subjects_dir=subjects_dir, src=src_fname) renderer._close_all() # EEG only with strange options evoked_eeg_ecog_seeg = evoked.copy().pick_types(meg=False, eeg=True) evoked_eeg_ecog_seeg.info['projs'] = [] # "remove" avg proj evoked_eeg_ecog_seeg.set_channel_types({ 'EEG 001': 'ecog', 'EEG 002': 'seeg' }) with pytest.warns(RuntimeWarning, match='Cannot plot MEG'): plot_alignment(evoked_eeg_ecog_seeg.info, subject='sample', trans=trans_fname, subjects_dir=subjects_dir, surfaces=['white', 'outer_skin', 'outer_skull'], meg=['helmet', 'sensors'], eeg=['original', 'projected'], ecog=True, seeg=True) renderer._close_all() sphere = make_sphere_model(info=evoked.info, r0='auto', head_radius='auto') bem_sol = read_bem_solution( op.join(subjects_dir, 'sample', 'bem', 'sample-1280-1280-1280-bem-sol.fif')) bem_surfs = read_bem_surfaces( op.join(subjects_dir, 'sample', 'bem', 'sample-1280-1280-1280-bem.fif')) sample_src[0]['coord_frame'] = 4 # hack for coverage plot_alignment( info, subject='sample', eeg='projected', meg='helmet', bem=sphere, dig=True, surfaces=['brain', 'inner_skull', 'outer_skull', 'outer_skin']) plot_alignment(info, trans_fname, subject='sample', meg='helmet', subjects_dir=subjects_dir, eeg='projected', bem=sphere, surfaces=['head', 'brain'], src=sample_src) assert all(surf['coord_frame'] == FIFF.FIFFV_COORD_MRI for surf in bem_sol['surfs']) plot_alignment(info, trans_fname, subject='sample', meg=[], subjects_dir=subjects_dir, bem=bem_sol, eeg=True, surfaces=['head', 'inflated', 'outer_skull', 'inner_skull']) assert all(surf['coord_frame'] == FIFF.FIFFV_COORD_MRI for surf in bem_sol['surfs']) plot_alignment(info, trans_fname, subject='sample', meg=True, subjects_dir=subjects_dir, surfaces=['head', 'inner_skull'], bem=bem_surfs) # single-layer BEM can still plot head surface assert bem_surfs[-1]['id'] == FIFF.FIFFV_BEM_SURF_ID_BRAIN bem_sol_homog = read_bem_solution( op.join(subjects_dir, 'sample', 'bem', 'sample-1280-bem-sol.fif')) for use_bem in (bem_surfs[-1:], bem_sol_homog): with catch_logging() as log: plot_alignment(info, trans_fname, subject='sample', meg=True, subjects_dir=subjects_dir, surfaces=['head', 'inner_skull'], bem=use_bem, verbose=True) log = log.getvalue() assert 'not find the surface for head in the provided BEM model' in log # sphere model sphere = make_sphere_model('auto', 'auto', evoked.info) src = setup_volume_source_space(sphere=sphere) plot_alignment( info, eeg='projected', meg='helmet', bem=sphere, src=src, dig=True, surfaces=['brain', 'inner_skull', 'outer_skull', 'outer_skin']) sphere = make_sphere_model('auto', None, evoked.info) # one layer # no info is permitted fig = plot_alignment(trans=trans_fname, subject='sample', meg=False, coord_frame='mri', subjects_dir=subjects_dir, surfaces=['brain'], bem=sphere, show_axes=True) renderer._close_all() if renderer.get_3d_backend() == 'mayavi': import mayavi # noqa: F401 analysis:ignore assert isinstance(fig, mayavi.core.scene.Scene) # 3D coil with no defined draw (ConvexHull) info_cube = pick_info(info, [0]) info['dig'] = None info_cube['chs'][0]['coil_type'] = 9999 with pytest.raises(RuntimeError, match='coil definition not found'): plot_alignment(info_cube, meg='sensors', surfaces=()) coil_def_fname = op.join(tempdir, 'temp') with open(coil_def_fname, 'w') as fid: fid.write(coil_3d) with use_coil_def(coil_def_fname): plot_alignment(info_cube, meg='sensors', surfaces=(), dig=True) # one layer bem with skull surfaces: with pytest.raises(ValueError, match='sphere conductor model must have'): plot_alignment(info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['brain', 'head', 'inner_skull'], bem=sphere) # wrong eeg value: with pytest.raises(ValueError, match='eeg must only contain'): plot_alignment(info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, eeg='foo') # wrong meg value: with pytest.raises(ValueError, match='meg must only contain'): plot_alignment(info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, meg='bar') # multiple brain surfaces: with pytest.raises(ValueError, match='Only one brain surface can be plot'): plot_alignment(info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['white', 'pial']) with pytest.raises(TypeError, match='all entries in surfaces must be'): plot_alignment(info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=[1]) with pytest.raises(ValueError, match='Unknown surface type'): plot_alignment(info=info, trans=trans_fname, subject='sample', subjects_dir=subjects_dir, surfaces=['foo']) fwd_fname = op.join(data_dir, 'MEG', 'sample', 'sample_audvis_trunc-meg-eeg-oct-4-fwd.fif') fwd = read_forward_solution(fwd_fname) plot_alignment(subject='sample', subjects_dir=subjects_dir, trans=trans_fname, fwd=fwd, surfaces='white', coord_frame='head') fwd = convert_forward_solution(fwd, force_fixed=True) plot_alignment(subject='sample', subjects_dir=subjects_dir, trans=trans_fname, fwd=fwd, surfaces='white', coord_frame='head') # fNIRS info = read_raw_nirx(nirx_fname).info with catch_logging() as log: plot_alignment(info, subject='fsaverage', surfaces=(), verbose=True) log = log.getvalue() assert '26 fnirs locations' in log renderer._close_all()
import numpy as np import sys from mne.io import read_raw_ctf from mne import pick_types from mne.chpi import _calculate_head_pos_ctf, read_head_pos sys.path.append( '/Users/nkozhemi/Documents/Github/mne_chop_tools-master/mne_pipes') from annotate_artifacts import (annotate_gfp_artifacts, annotate_motion_artifacts, plot_artifacts, annotate_muscle_artifacts) top_dir = '/Users/nkozhemi/Desktop/MEG_repo/MEG_children/18011010C/' ds_fname = op.join(top_dir, 'MEG_Movie_20190319_09.ds') raw = read_raw_ctf(ds_fname, preload=True) #raw.set_channel_types({'EEG057': 'eog', 'EEG058': 'ecg'}) #raw.info['bads'] = [] if raw.compensation_grade != 3: raw.apply_gradient_compensation(3) # muscle artifacts mus_annot, mus_raw = annotate_muscle_artifacts(raw, art_thresh=2.5, return_stat_raw=True) # motion artifacts pos = _calculate_head_pos_ctf(raw) mov_annot, raw_hpi = annotate_motion_artifacts(raw, pos, return_stat_raw=True)
def test_plot_ref_meg(): """Test plotting ref_meg.""" raw_ctf = read_raw_ctf(ctf_fname_continuous).crop(0, 1).load_data() raw_ctf.plot() plt.close('all') pytest.raises(ValueError, raw_ctf.plot, group_by='selection')
import mne from mne.datasets.brainstorm import bst_raw from mne.io import read_raw_ctf print(__doc__) tmin, tmax, event_id = -0.1, 0.3, 2 # take right-hand somato reject = dict(mag=4e-12, eog=250e-6) data_path = bst_raw.data_path() raw_path = (data_path + '/MEG/bst_raw/' + 'subj001_somatosensory_20111109_01_AUX-f.ds') # Here we crop to half the length to save memory raw = read_raw_ctf(raw_path).crop(0, 180).load_data() raw.plot() # set EOG channel raw.set_channel_types({'EEG058': 'eog'}) raw.set_eeg_reference('average', projection=True) # show power line interference and remove it raw.plot_psd(tmax=60., average=False) raw.notch_filter(np.arange(60, 181, 60), fir_design='firwin') events = mne.find_events(raw, stim_channel='UPPT001') # pick MEG channels picks = mne.pick_types(raw.info, meg=True,
subject = 'bst_auditory' subjects_dir = op.join(data_path, 'subjects') raw_fname1 = op.join(data_path, 'MEG', 'bst_auditory', 'S01_AEF_20131218_01.ds') raw_fname2 = op.join(data_path, 'MEG', 'bst_auditory', 'S01_AEF_20131218_02.ds') erm_fname = op.join(data_path, 'MEG', 'bst_auditory', 'S01_Noise_20131218_01.ds') ############################################################################### # In the memory saving mode we use ``preload=False`` and use the memory # efficient IO which loads the data on demand. However, filtering and some # other functions require the data to be preloaded in the memory. preload = not use_precomputed raw = read_raw_ctf(raw_fname1, preload=preload) n_times_run1 = raw.n_times mne.io.concatenate_raws([raw, read_raw_ctf(raw_fname2, preload=preload)]) raw_erm = read_raw_ctf(erm_fname, preload=preload) ############################################################################### # Data channel array consisted of 274 MEG axial gradiometers, 26 MEG reference # sensors and 2 EEG electrodes (Cz and Pz). # In addition: # # - 1 stim channel for marking presentation times for the stimuli # - 1 audio channel for the sent signal # - 1 response channel for recording the button presses # - 1 ECG bipolar # - 2 EOG bipolar (vertical and horizontal) # - 12 head tracking channels
def test_read_ctf(): """Test CTF reader.""" temp_dir = _TempDir() out_fname = op.join(temp_dir, 'test_py_raw.fif') # Create a dummy .eeg file so we can test our reading/application of it os.mkdir(op.join(temp_dir, 'randpos')) ctf_eeg_fname = op.join(temp_dir, 'randpos', ctf_fname_catch) shutil.copytree(op.join(ctf_dir, ctf_fname_catch), ctf_eeg_fname) with pytest.warns(RuntimeWarning, match='RMSP .* changed to a MISC ch'): raw = _test_raw_reader(read_raw_ctf, directory=ctf_eeg_fname) picks = pick_types(raw.info, meg=False, eeg=True) pos = np.random.RandomState(42).randn(len(picks), 3) fake_eeg_fname = op.join(ctf_eeg_fname, 'catch-alp-good-f.eeg') # Create a bad file with open(fake_eeg_fname, 'wb') as fid: fid.write('foo\n'.encode('ascii')) pytest.raises(RuntimeError, read_raw_ctf, ctf_eeg_fname) # Create a good file with open(fake_eeg_fname, 'wb') as fid: for ii, ch_num in enumerate(picks): args = (str(ch_num + 1), raw.ch_names[ch_num],) + tuple( '%0.5f' % x for x in 100 * pos[ii]) # convert to cm fid.write(('\t'.join(args) + '\n').encode('ascii')) pos_read_old = np.array([raw.info['chs'][p]['loc'][:3] for p in picks]) with pytest.warns(RuntimeWarning, match='RMSP .* changed to a MISC ch'): raw = read_raw_ctf(ctf_eeg_fname) # read modified data pos_read = np.array([raw.info['chs'][p]['loc'][:3] for p in picks]) assert_allclose(apply_trans(raw.info['ctf_head_t'], pos), pos_read, rtol=1e-5, atol=1e-5) assert (pos_read == pos_read_old).mean() < 0.1 shutil.copy(op.join(ctf_dir, 'catch-alp-good-f.ds_randpos_raw.fif'), op.join(temp_dir, 'randpos', 'catch-alp-good-f.ds_raw.fif')) # Create a version with no hc, starting out *with* EEG pos (error) os.mkdir(op.join(temp_dir, 'nohc')) ctf_no_hc_fname = op.join(temp_dir, 'no_hc', ctf_fname_catch) shutil.copytree(ctf_eeg_fname, ctf_no_hc_fname) remove_base = op.join(ctf_no_hc_fname, op.basename(ctf_fname_catch[:-3])) os.remove(remove_base + '.hc') with pytest.warns(RuntimeWarning, match='MISC channel'): pytest.raises(RuntimeError, read_raw_ctf, ctf_no_hc_fname) os.remove(remove_base + '.eeg') shutil.copy(op.join(ctf_dir, 'catch-alp-good-f.ds_nohc_raw.fif'), op.join(temp_dir, 'no_hc', 'catch-alp-good-f.ds_raw.fif')) # All our files use_fnames = [op.join(ctf_dir, c) for c in ctf_fnames] for fname in use_fnames: raw_c = read_raw_fif(fname + '_raw.fif', preload=True) with pytest.warns(None): # sometimes matches "MISC channel" raw = read_raw_ctf(fname) # check info match assert_array_equal(raw.ch_names, raw_c.ch_names) assert_allclose(raw.times, raw_c.times) assert_allclose(raw._cals, raw_c._cals) assert_equal(raw.info['meas_id']['version'], raw_c.info['meas_id']['version'] + 1) for t in ('dev_head_t', 'dev_ctf_t', 'ctf_head_t'): assert_allclose(raw.info[t]['trans'], raw_c.info[t]['trans'], rtol=1e-4, atol=1e-7) for key in ('acq_pars', 'acq_stim', 'bads', 'ch_names', 'custom_ref_applied', 'description', 'events', 'experimenter', 'highpass', 'line_freq', 'lowpass', 'nchan', 'proj_id', 'proj_name', 'projs', 'sfreq', 'subject_info'): assert_equal(raw.info[key], raw_c.info[key], key) if op.basename(fname) not in single_trials: # We don't force buffer size to be smaller like MNE-C assert raw.buffer_size_sec == raw_c.buffer_size_sec assert_equal(len(raw.info['comps']), len(raw_c.info['comps'])) for c1, c2 in zip(raw.info['comps'], raw_c.info['comps']): for key in ('colcals', 'rowcals'): assert_allclose(c1[key], c2[key]) assert_equal(c1['save_calibrated'], c2['save_calibrated']) for key in ('row_names', 'col_names', 'nrow', 'ncol'): assert_array_equal(c1['data'][key], c2['data'][key]) assert_allclose(c1['data']['data'], c2['data']['data'], atol=1e-7, rtol=1e-5) assert_allclose(raw.info['hpi_results'][0]['coord_trans']['trans'], raw_c.info['hpi_results'][0]['coord_trans']['trans'], rtol=1e-5, atol=1e-7) assert_equal(len(raw.info['chs']), len(raw_c.info['chs'])) for ii, (c1, c2) in enumerate(zip(raw.info['chs'], raw_c.info['chs'])): for key in ('kind', 'scanno', 'unit', 'ch_name', 'unit_mul', 'range', 'coord_frame', 'coil_type', 'logno'): if c1['ch_name'] == 'RMSP' and \ 'catch-alp-good-f' in fname and \ key in ('kind', 'unit', 'coord_frame', 'coil_type', 'logno'): continue # XXX see below... assert_equal(c1[key], c2[key], err_msg=key) for key in ('cal',): assert_allclose(c1[key], c2[key], atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) # XXX 2016/02/24: fixed bug with normal computation that used # to exist, once mne-C tools are updated we should update our FIF # conversion files, then the slices can go away (and the check # can be combined with that for "cal") for key in ('loc',): if c1['ch_name'] == 'RMSP' and 'catch-alp-good-f' in fname: continue if (c2[key][:3] == 0.).all(): check = [np.nan] * 3 else: check = c2[key][:3] assert_allclose(c1[key][:3], check, atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) if (c2[key][3:] == 0.).all(): check = [np.nan] * 3 else: check = c2[key][9:12] assert_allclose(c1[key][9:12], check, atol=1e-6, rtol=1e-4, err_msg='raw.info["chs"][%d][%s]' % (ii, key)) # Make sure all digitization points are in the MNE head coord frame for p in raw.info['dig']: assert_equal(p['coord_frame'], FIFF.FIFFV_COORD_HEAD, err_msg='dig points must be in FIFF.FIFFV_COORD_HEAD') if fname.endswith('catch-alp-good-f.ds'): # omit points from .pos file raw.info['dig'] = raw.info['dig'][:-10] # XXX: Next test would fail because c-tools assign the fiducials from # CTF data as HPI. Should eventually clarify/unify with Matti. # assert_dig_allclose(raw.info, raw_c.info) # check data match raw_c.save(out_fname, overwrite=True, buffer_size_sec=1.) raw_read = read_raw_fif(out_fname) # so let's check tricky cases based on sample boundaries rng = np.random.RandomState(0) pick_ch = rng.permutation(np.arange(len(raw.ch_names)))[:10] bnd = int(round(raw.info['sfreq'] * raw.buffer_size_sec)) assert_equal(bnd, raw._raw_extras[0]['block_size']) assert_equal(bnd, block_sizes[op.basename(fname)]) slices = (slice(0, bnd), slice(bnd - 1, bnd), slice(3, bnd), slice(3, 300), slice(None)) if len(raw.times) >= 2 * bnd: # at least two complete blocks slices = slices + (slice(bnd, 2 * bnd), slice(bnd, bnd + 1), slice(0, bnd + 100)) for sl_time in slices: assert_allclose(raw[pick_ch, sl_time][0], raw_c[pick_ch, sl_time][0]) assert_allclose(raw_read[pick_ch, sl_time][0], raw_c[pick_ch, sl_time][0]) # all data / preload with pytest.warns(None): # sometimes MISC raw = read_raw_ctf(fname, preload=True) assert_allclose(raw[:][0], raw_c[:][0], atol=1e-15) # test bad segment annotations if 'testdata_ctf_short.ds' in fname: assert 'bad' in raw.annotations.description[0] assert_allclose(raw.annotations.onset, [2.15]) assert_allclose(raw.annotations.duration, [0.0225]) pytest.raises(TypeError, read_raw_ctf, 1) pytest.raises(ValueError, read_raw_ctf, ctf_fname_continuous + 'foo.ds') # test ignoring of system clock read_raw_ctf(op.join(ctf_dir, ctf_fname_continuous), 'ignore') pytest.raises(ValueError, read_raw_ctf, op.join(ctf_dir, ctf_fname_continuous), 'foo')
############################################################################### # Neuromag # -------- kwargs = dict(eeg=False, coord_frame='meg', show_axes=True, verbose=True) raw = read_raw_fif(sample.data_path() + '/MEG/sample/sample_audvis_raw.fif') fig = plot_alignment(raw.info, meg=('helmet', 'sensors'), **kwargs) set_3d_title(figure=fig, title='Neuromag') ############################################################################### # CTF # --- raw = read_raw_ctf(spm_face.data_path() + '/MEG/spm/SPM_CTF_MEG_example_faces1_3D.ds') fig = plot_alignment(raw.info, meg=('helmet', 'sensors', 'ref'), **kwargs) set_3d_title(figure=fig, title='CTF 275') ############################################################################### # BTi # --- bti_path = op.abspath(op.dirname(mne.__file__)) + '/io/bti/tests/data/' raw = read_raw_bti(op.join(bti_path, 'test_pdf_linux'), op.join(bti_path, 'test_config_linux'), op.join(bti_path, 'test_hs_linux')) fig = plot_alignment(raw.info, meg=('helmet', 'sensors', 'ref'), **kwargs) set_3d_title(figure=fig, title='Magnes 3600wh') ###############################################################################
fname_bhv = list() files = os.listdir(op.join(path_data, subject, 'behavdata')) fname_bhv.extend(([ op.join(fname_raw + '/behavdata/') + f for f in files if 'WorkMem' in f ])) for fname_behavior in fname_bhv: events_behavior = get_events_from_mat(fname_behavior) # Read raw MEG data and extract event triggers runs = list() files = os.listdir(fname_raw) runs.extend(([op.join(fname_raw + '/') + f for f in files if '.ds' in f])) events_meg = list() for run_number, this_run in enumerate(runs): fname_raw = op.join(path_data, subject, this_run) print(fname_raw) raw = read_raw_ctf(fname_raw, preload=True, system_clock='ignore') channel_trigger = np.where(np.array(raw.ch_names) == 'USPT001')[0][0] # trigger baseline is 255 # Replace 255 values with 0 for easier reading of events trigger_baseline = np.where(raw._data[channel_trigger, :] == 255)[0] raw._data[channel_trigger, trigger_baseline] = 0. # find triggers events_meg_ = mne.find_events(raw) # Add 48ms to the trigger events (according to delay with photodiod) events_meg_ = np.array(events_meg_, float) events_meg_[:, 0] += round(.048 * raw.info['sfreq']) events_meg_ = np.array(events_meg_, int) # to keep the run from which the event was found events_meg_[:, 1] = run_number events_meg.append(events_meg_) # concatenate all meg events
if len(sys.argv) != 3: print("usage: {} subject $ds".format(sys.argv[0])) sys.exit(1) subject = sys.argv[1] dsname = sys.argv[2] try: FShome = os.environ['FREESURFER_HOME'] except KeyError: print("You must set the FREESURFER_HOME environment variable!") sys.exit(1) try: Subjdir = os.environ['SUBJECTS_DIR'] except KeyError: Subjdir = op.join(FShome, "subjects") print("Note: Using the default SUBJECTS_DIR:", Subjdir) name = op.join(Subjdir, subject, "bem", "{}-fiducials.fif".format(subject)) fids = read_fiducials(name) fidc = _fiducial_coords(fids[0]) raw = read_raw_ctf(dsname, clean_names = True, preload = False) fidd = _fiducial_coords(raw.info['dig']) xform = fit_matched_points(fidd, fidc, weights = [1, 10, 1]) t = Transform(FIFF.FIFFV_COORD_HEAD, FIFF.FIFFV_COORD_MRI, xform) name = op.join(Subjdir, subject, "bem", "{}-trans.fif".format(subject)) write_trans(name, t)