def test_ch_loc(): """Test raw kit loc """ raw_py = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path, stim='<') raw_bin = Raw(op.join(data_dir, 'test_bin.fif')) ch_py = raw_py._sqd_params['sensor_locs'][:, :5] # ch locs stored as m, not mm ch_py[:, :3] *= 1e3 ch_sns = read_sns(op.join(data_dir, 'sns.txt')) assert_array_almost_equal(ch_py, ch_sns, 2) assert_array_almost_equal(raw_py.info['dev_head_t']['trans'], raw_bin.info['dev_head_t']['trans'], 4) for py_ch, bin_ch in zip(raw_py.info['chs'], raw_bin.info['chs']): if bin_ch['ch_name'].startswith('MEG'): # the stored ch locs have more precision than the sns.txt assert_array_almost_equal(py_ch['loc'], bin_ch['loc'], decimal=2) assert_array_almost_equal(py_ch['coil_trans'], bin_ch['coil_trans'], decimal=2) # test when more than one marker file provided mrks = [mrk_path, mrk2_path, mrk3_path] _ = read_raw_kit(sqd_path, mrks, elp_path, hsp_path, preload=False)
def test_read_segment(): """Test writing raw kit files when preload is False """ raw1 = kit.read_raw_kit(input_fname=op.join(data_dir, 'test.sqd'), mrk_fname=op.join(data_dir, 'test_mrk.sqd'), elp_fname=op.join(data_dir, 'test_elp.txt'), hsp_fname=op.join(data_dir, 'test_hsp.txt'), sns_fname=op.join(data_dir, 'sns.txt'), stim=range(167, 159, -1), preload=False) raw1_file = op.join(tempdir, 'raw1.fif') raw1.save(raw1_file, buffer_size_sec=.1, overwrite=True) raw2 = kit.read_raw_kit(input_fname=op.join(data_dir, 'test.sqd'), mrk_fname=op.join(data_dir, 'test_mrk.sqd'), elp_fname=op.join(data_dir, 'test_elp.txt'), hsp_fname=op.join(data_dir, 'test_hsp.txt'), sns_fname=op.join(data_dir, 'sns.txt'), stim=range(167, 159, -1), preload=True) raw2_file = op.join(tempdir, 'raw2.fif') raw2.save(raw2_file, buffer_size_sec=.1, overwrite=True) raw1 = Raw(raw1_file, preload=True) raw2 = Raw(raw2_file, preload=True) assert_array_equal(raw1._data, raw2._data) raw3 = kit.read_raw_kit(input_fname=op.join(data_dir, 'test.sqd'), mrk_fname=op.join(data_dir, 'test_mrk.sqd'), elp_fname=op.join(data_dir, 'test_elp.txt'), hsp_fname=op.join(data_dir, 'test_hsp.txt'), sns_fname=op.join(data_dir, 'sns.txt'), stim=range(167, 159, -1), preload=True) assert_array_almost_equal(raw1._data, raw3._data)
def test_read_segment(): """Test writing raw kit files when preload is False """ raw1 = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path, stim='<', preload=False) raw1_file = op.join(tempdir, 'raw1.fif') raw1.save(raw1_file, buffer_size_sec=.1, overwrite=True) raw2 = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path, stim='<', preload=True) raw2_file = op.join(tempdir, 'raw2.fif') raw2.save(raw2_file, buffer_size_sec=.1, overwrite=True) raw1 = Raw(raw1_file, preload=True) raw2 = Raw(raw2_file, preload=True) assert_array_equal(raw1._data, raw2._data) raw3 = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path, stim='<', preload=True) assert_array_almost_equal(raw1._data, raw3._data)
def test_data(): """Test reading raw kit files """ raw_py = kit.read_raw_kit(input_fname=op.join(data_dir, 'test.sqd'), mrk_fname=op.join(data_dir, 'test_mrk.sqd'), elp_fname=op.join(data_dir, 'test_elp.txt'), hsp_fname=op.join(data_dir, 'test_hsp.txt'), sns_fname=op.join(data_dir, 'sns.txt'), stim=range(167, 159, -1), stimthresh=1, preload=True) # Binary file only stores the sensor channels py_picks = pick_types(raw_py.info, exclude='bads') raw_bin = op.join(data_dir, 'test_bin.fif') raw_bin = Raw(raw_bin, preload=True) bin_picks = pick_types(raw_bin.info, stim=True, exclude='bads') data_bin, _ = raw_bin[bin_picks] data_py, _ = raw_py[py_picks] # this .mat was generated using the Yokogawa MEG Reader data_Ykgw = op.join(data_dir, 'test_Ykgw.mat') data_Ykgw = scipy.io.loadmat(data_Ykgw)['data'] data_Ykgw = data_Ykgw[py_picks] assert_array_almost_equal(data_py, data_Ykgw) py_picks = pick_types(raw_py.info, stim=True, exclude='bads') data_py, _ = raw_py[py_picks] assert_array_almost_equal(data_py, data_bin)
def test_data(): """Test reading raw kit files """ raw_py = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path, stim=list(range(167, 159, -1)), slope='+', stimthresh=1, preload=True) print(repr(raw_py)) # Binary file only stores the sensor channels py_picks = pick_types(raw_py.info, exclude='bads') raw_bin = op.join(data_dir, 'test_bin.fif') raw_bin = Raw(raw_bin, preload=True) bin_picks = pick_types(raw_bin.info, stim=True, exclude='bads') data_bin, _ = raw_bin[bin_picks] data_py, _ = raw_py[py_picks] # this .mat was generated using the Yokogawa MEG Reader data_Ykgw = op.join(data_dir, 'test_Ykgw.mat') data_Ykgw = scipy.io.loadmat(data_Ykgw)['data'] data_Ykgw = data_Ykgw[py_picks] assert_array_almost_equal(data_py, data_Ykgw) py_picks = pick_types(raw_py.info, stim=True, ref_meg=False, exclude='bads') data_py, _ = raw_py[py_picks] assert_array_almost_equal(data_py, data_bin)
def test_stim_ch(): """Test raw kit stim ch """ raw = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path, stim='<', slope='+', preload=True) stim_pick = pick_types(raw.info, meg=False, stim=True, exclude='bads') stim1, _ = raw[stim_pick] stim2 = np.array(raw.read_stim_ch(), ndmin=2) assert_array_equal(stim1, stim2)
def test_ch_loc(): raw_py = kit.read_raw_kit(input_fname=os.path.join(data_dir, 'test.sqd'), mrk_fname=os.path.join(data_dir, 'test_mrk.sqd'), elp_fname=os.path.join(data_dir, 'test_elp.txt'), hsp_fname=os.path.join(data_dir, 'test_hsp.txt'), sns_fname=os.path.join(data_dir, 'sns.txt'), stim=range(167, 159, -1)) raw_bin = Raw(os.path.join(data_dir, 'test_bin.fif')) for py_ch, bin_ch in zip(raw_py.info['chs'], raw_bin.info['chs']): if bin_ch['ch_name'].startswith('MEG'): # the mne_kit2fiff_bin has a different representation of pi. assert_array_almost_equal(py_ch['loc'], bin_ch['loc'], decimal=5)
def test_stim_ch(): """Test raw kit stim ch """ raw = kit.read_raw_kit(input_fname=op.join(data_dir, 'test.sqd'), mrk_fname=op.join(data_dir, 'test_mrk.sqd'), elp_fname=op.join(data_dir, 'test_elp.txt'), hsp_fname=op.join(data_dir, 'test_hsp.txt'), sns_fname=op.join(data_dir, 'sns.txt'), stim=range(167, 159, -1), preload=True) stim_pick = pick_types(raw.info, meg=False, stim=True, exclude='bads') stim1, _ = raw[stim_pick] stim2 = np.array(raw.read_stim_ch(), ndmin=2) assert_array_equal(stim1, stim2)
def test_ch_loc(): """Test raw kit loc """ raw_py = kit.read_raw_kit(input_fname=op.join(data_dir, 'test.sqd'), mrk_fname=op.join(data_dir, 'test_mrk.sqd'), elp_fname=op.join(data_dir, 'test_elp.txt'), hsp_fname=op.join(data_dir, 'test_hsp.txt'), sns_fname=op.join(data_dir, 'sns.txt'), stim=range(167, 159, -1)) raw_bin = Raw(op.join(data_dir, 'test_bin.fif')) for py_ch, bin_ch in zip(raw_py.info['chs'], raw_bin.info['chs']): if bin_ch['ch_name'].startswith('MEG'): # the mne_kit2fiff_bin has a different representation of pi. assert_array_almost_equal(py_ch['loc'], bin_ch['loc'], decimal=5)
def test_ch_loc(): """Test raw kit loc """ raw_py = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path, stim='<') raw_bin = Raw(op.join(data_dir, 'test_bin.fif')) ch_py = raw_py._sqd_params['sensor_locs'][:, :5] # ch locs stored as m, not mm ch_py[:, :3] *= 1e3 ch_sns = read_sns(op.join(data_dir, 'sns.txt')) assert_array_almost_equal(ch_py, ch_sns, 2) for py_ch, bin_ch in zip(raw_py.info['chs'], raw_bin.info['chs']): if bin_ch['ch_name'].startswith('MEG'): # the stored ch locs have more precision than the sns.txt assert_array_almost_equal(py_ch['loc'], bin_ch['loc'], decimal=2)
input_fname = options.input_fname if input_fname is None: parser.print_help() sys.exit(-1) sns_fname = options.sns_fname hsp_fname = options.hsp_fname elp_fname = options.elp_fname mrk_fname = options.mrk_fname stim = options.stim stimthresh = options.stimthresh out_fname = options.out_fname if isinstance(stim, str): stim = stim.split(":") raw = read_raw_kit( input_fname=input_fname, mrk_fname=mrk_fname, elp_fname=elp_fname, hsp_fname=hsp_fname, sns_fname=sns_fname, stim=stim, stimthresh=stimthresh, ) raw.save(out_fname) raw.close() sys.exit(0)
options, args = parser.parse_args() input_fname = options.input_fname if input_fname is None: parser.print_help() sys.exit(-1) sns_fname = options.sns_fname hsp_fname = options.hsp_fname elp_fname = options.elp_fname mrk_fname = options.mrk_fname stim = options.stim stimthresh = options.stimthresh out_fname = options.out_fname if isinstance(stim, str): stim = stim.split(':') raw = read_raw_kit(input_fname=input_fname, mrk_fname=mrk_fname, elp_fname=elp_fname, hsp_fname=hsp_fname, sns_fname=sns_fname, stim=stim, stimthresh=stimthresh) raw.save(out_fname) raw.close() sys.exit(0)
def test_find_layout(): """Test finding layout""" with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') find_layout(chs=test_info['chs']) assert_true(w[0].category == DeprecationWarning) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') find_layout(test_info['chs']) assert_true(w[0].category == DeprecationWarning) assert_raises(ValueError, find_layout, dict()) assert_raises(ValueError, find_layout, test_info, ch_type='meep') sample_info = Raw(fif_fname).info grads = pick_types(sample_info, meg='grad') sample_info2 = pick_info(sample_info, grads) mags = pick_types(sample_info, meg='mag') sample_info3 = pick_info(sample_info, mags) # mock new convention sample_info4 = copy.deepcopy(sample_info) for ii, name in enumerate(sample_info4['ch_names']): new = name.replace(' ', '') sample_info4['ch_names'][ii] = new sample_info4['chs'][ii]['ch_name'] = new mags = pick_types(sample_info, meg=False, eeg=True) sample_info5 = pick_info(sample_info, mags) lout = find_layout(sample_info, ch_type=None) assert_true(lout.kind == 'Vectorview-all') assert_true(all(' ' in k for k in lout.names)) lout = find_layout(sample_info2, ch_type='meg') assert_true(lout.kind == 'Vectorview-all') # test new vector-view lout = find_layout(sample_info4, ch_type=None) assert_true(lout.kind == 'Vectorview-all') assert_true(all(not ' ' in k for k in lout.names)) lout = find_layout(sample_info, ch_type='grad') assert_true(lout.kind == 'Vectorview-grad') lout = find_layout(sample_info2) assert_true(lout.kind == 'Vectorview-grad') lout = find_layout(sample_info2, ch_type='grad') assert_true(lout.kind == 'Vectorview-grad') lout = find_layout(sample_info2, ch_type='meg') assert_true(lout.kind == 'Vectorview-all') lout = find_layout(sample_info, ch_type='mag') assert_true(lout.kind == 'Vectorview-mag') lout = find_layout(sample_info3) assert_true(lout.kind == 'Vectorview-mag') lout = find_layout(sample_info3, ch_type='mag') assert_true(lout.kind == 'Vectorview-mag') lout = find_layout(sample_info3, ch_type='meg') assert_true(lout.kind == 'Vectorview-all') # lout = find_layout(sample_info, ch_type='eeg') assert_true(lout.kind == 'EEG') lout = find_layout(sample_info5) assert_true(lout.kind == 'EEG') lout = find_layout(sample_info5, ch_type='eeg') assert_true(lout.kind == 'EEG') # no common layout, 'meg' option not supported fname_bti_raw = op.join(bti_dir, 'exported4D_linux.fif') lout = find_layout(Raw(fname_bti_raw).info) assert_true(lout.kind == 'magnesWH3600') lout = find_layout(Raw(fname_ctf_raw).info) assert_true(lout.kind == 'CTF-275') lout = find_layout(read_raw_kit(fname_kit_157).info) assert_true(lout.kind == 'KIT-157') sample_info5['dig'] = [] assert_raises(RuntimeError, find_layout, sample_info5)
def test_make_forward_solution_kit(): """Test making fwd using KIT, BTI, and CTF (compensated) files """ fname_bem = op.join(subjects_dir, 'sample', 'bem', 'sample-5120-bem-sol.fif') kit_dir = op.join(op.dirname(__file__), '..', '..', 'fiff', 'kit', 'tests', 'data') sqd_path = op.join(kit_dir, 'test.sqd') mrk_path = op.join(kit_dir, 'test_mrk.sqd') elp_path = op.join(kit_dir, 'test_elp.txt') hsp_path = op.join(kit_dir, 'test_hsp.txt') mri_path = op.join(kit_dir, 'trans-sample.fif') fname_kit_raw = op.join(kit_dir, 'test_bin.fif') bti_dir = op.join(op.dirname(__file__), '..', '..', 'fiff', 'bti', 'tests', 'data') bti_pdf = op.join(bti_dir, 'test_pdf_linux') bti_config = op.join(bti_dir, 'test_config_linux') bti_hs = op.join(bti_dir, 'test_hs_linux') fname_bti_raw = op.join(bti_dir, 'exported4D_linux.fif') fname_ctf_raw = op.join(op.dirname(__file__), '..', '..', 'fiff', 'tests', 'data', 'test_ctf_comp_raw.fif') # first set up a testing source space fname_src = op.join(temp_dir, 'oct2-src.fif') src = setup_source_space('sample', fname_src, 'oct2', subjects_dir=subjects_dir) # first use mne-C: convert file, make forward solution fwd = do_forward_solution('sample', fname_kit_raw, src=fname_src, mindist=0.0, bem=fname_bem, mri=mri_path, eeg=False, meg=True, subjects_dir=subjects_dir) # now let's use python with the same raw file fwd_py = make_forward_solution(fname_kit_raw, mindist=0.0, src=src, eeg=False, meg=True, bem=fname_bem, mri=mri_path) _compare_forwards(fwd, fwd_py, 157, 108) # now let's use mne-python all the way raw_py = read_raw_kit(sqd_path, mrk_path, elp_path, hsp_path) # without ignore_ref=True, this should throw an error: assert_raises(NotImplementedError, make_forward_solution, raw_py.info, mindist=0.0, src=src, eeg=False, meg=True, bem=fname_bem, mri=mri_path) fwd_py = make_forward_solution(raw_py.info, mindist=0.0, src=src, eeg=False, meg=True, bem=fname_bem, mri=mri_path, ignore_ref=True) _compare_forwards(fwd, fwd_py, 157, 108, meg_rtol=1e-3, meg_atol=1e-7) # BTI python end-to-end versus C fwd = do_forward_solution('sample', fname_bti_raw, src=fname_src, mindist=0.0, bem=fname_bem, mri=mri_path, eeg=False, meg=True, subjects_dir=subjects_dir) raw_py = read_raw_bti(bti_pdf, bti_config, bti_hs) fwd_py = make_forward_solution(raw_py.info, mindist=0.0, src=src, eeg=False, meg=True, bem=fname_bem, mri=mri_path) _compare_forwards(fwd, fwd_py, 248, 108) # now let's test CTF w/compensation fwd_py = make_forward_solution(fname_ctf_raw, mindist=0.0, src=src, eeg=False, meg=True, bem=fname_bem, mri=fname_mri) fwd = do_forward_solution('sample', fname_ctf_raw, src=fname_src, mindist=0.0, bem=fname_bem, mri=fname_mri, eeg=False, meg=True, subjects_dir=subjects_dir) _compare_forwards(fwd, fwd_py, 274, 108) # CTF with compensation changed in python ctf_raw = Raw(fname_ctf_raw, compensation=2) fwd_py = make_forward_solution(ctf_raw.info, mindist=0.0, src=src, eeg=False, meg=True, bem=fname_bem, mri=fname_mri) fwd = do_forward_solution('sample', ctf_raw, src=fname_src, mindist=0.0, bem=fname_bem, mri=fname_mri, eeg=False, meg=True, subjects_dir=subjects_dir) _compare_forwards(fwd, fwd_py, 274, 108)
def mne_raw(path=None, proj=False, **kwargs): """ Returns a mne.fiff.Raw object with added projections if appropriate. Parameters ---------- path : None | str(path) path to the raw fiff file. If ``None``, a file can be chosen form a file dialog. proj : bool | str(path) Add projections from a separate file to the Raw object. **``False``**: No proj file will be added. **``True``**: ``'{raw}*proj.fif'`` will be used. ``'{raw}'`` will be replaced with the raw file's path minus '_raw.fif', and '*' will be expanded using fnmatch. If multiple files match the pattern, a ValueError will be raised. **``str``**: A custom path template can be provided, ``'{raw}'`` and ``'*'`` will be treated as with ``True``. kwargs Additional keyword arguments are forwarded to mne.fiff.Raw initialization. """ if path is None: path = ui.ask_file("Pick a Raw Fiff File", "Pick a Raw Fiff File", [('Functional image file (*.fif)', '*.fif'), ('KIT Raw File (*.sqd,*.con', '*.sqd;*.con')]) if not path: return if not os.path.isfile(path): raise IOError("%r is not a file" % path) if isinstance(path, basestring): _, ext = os.path.splitext(path) if ext.startswith('.fif'): raw = mne.fiff.Raw(path, **kwargs) elif ext in ('.sqd', '.con'): from mne.fiff.kit import read_raw_kit raw = read_raw_kit(path, **kwargs) else: raise ValueError("Unknown extension: %r" % ext) else: raw = mne.fiff.Raw(path, **kwargs) if proj: if proj == True: proj = '{raw}*proj.fif' if '{raw}' in proj: raw_file = raw.info['filename'] raw_root, _ = os.path.splitext(raw_file) raw_root = raw_root.rstrip('raw') proj = proj.format(raw=raw_root) if '*' in proj: head, tail = os.path.split(proj) names = fnmatch.filter(os.listdir(head), tail) if len(names) == 1: proj = os.path.join(head, names[0]) else: if len(names) == 0: err = "No file matching %r" else: err = "Multiple files matching %r" raise ValueError(err % proj) # add the projections to the raw file proj = mne.read_proj(proj) raw.add_proj(proj, remove_existing=True) return raw
metavar='value') parser.add_option('--output', dest='out_fname', help='Name of the resulting fiff file', metavar='filename') options, args = parser.parse_args() input_fname = options.input_fname if input_fname is None: parser.print_help() sys.exit(1) hsp_fname = options.hsp_fname elp_fname = options.elp_fname mrk_fname = options.mrk_fname stim = options.stim slope = options.slope stimthresh = options.stimthresh out_fname = options.out_fname if isinstance(stim, str): stim = stim.split(':') raw = read_raw_kit(input_fname=input_fname, mrk=mrk_fname, elp=elp_fname, hsp=hsp_fname, stim=stim, slope=slope, stimthresh=stimthresh) raw.save(out_fname) raw.close() sys.exit(0)