def _load_raw(mb, keyword=None): if op.isfile(_fname(mb, 'raw', 'raw', 'fif', keyword)): raw = Raw(_fname(mb, 'raw', 'raw', 'fif', keyword), verbose=False, preload=True) print('%s' % (keyword) * (keyword is not None) + ' raw data loaded.') elif keyword is None: f = mb.fdata[0] print(f) raw = Raw(f, preload=True, verbose=False) raw.info['bads'] = [] for f in mb.fdata[1:]: print(f) r = Raw(f, preload=True, verbose=False) r.info['bads'] = [] raw.append(r) if mb.eeg: raw = raw.set_eeg_reference(ref_channels=[], projection=False) raw = raw.pick_types(meg=mb.meg, eeg=mb.eeg, stim=True, eog=True, ecg=True, emg=True) else: raise ValueError('No raw data file found ' + 'for %s' % (keyword) * (keyword is not None)) return raw
def test_copy_append(): """Test raw copying and appending combinations """ raw = Raw(fif_fname, preload=True).copy() raw_full = Raw(fif_fname) raw_full.append(raw) data = raw_full[:, :][0] assert_equal(data.shape[1], 2 * raw._data.shape[1])
def test_copy_append(): """Test raw copying and appending combinations """ raw = Raw(fif_fname, preload=True).copy() raw_full = Raw(fif_fname) raw_full.append(raw) data = raw_full[:, :][0] assert_equal(data.shape[1], 2 * raw._data.shape[1])
def merge_fif_from_eo_ec(Subj_ID): from mne.io import Raw import pandas as pd from table_handling import get_eo_ec_by_name from params import main_path Subj_ID_ec = Subj_ID + ' CLOSED' Subj_ID_eo = Subj_ID + ' OPEN' eo_ec_table_fname = main_path + 'Aut_gamma_EO_EC_Timing.xls' suffix_open = '_rest_open_raw_tsss_mc_trans.fif' suffix_closed = '_rest_closed_raw_tsss_mc_trans.fif' suffix_merged = '_rest_raw_tsss_mc_trans.fif' filename_ec = main_path + 'MEG/' + Subj_ID + '/' + Subj_ID + suffix_closed filename_eo = main_path + 'MEG/' + Subj_ID + '/' + Subj_ID + suffix_open filename_merged = main_path + 'MEG/' + Subj_ID + '/' + Subj_ID + suffix_merged if Subj_ID[0] == 'K': sheetname = 'Control' elif Subj_ID[0] == 'R': sheetname = 'ASD' eo_ec_table = pd.read_excel(eo_ec_table_fname, skiprows=[1], sheetname=sheetname) idx_Subj_ID_ec = eo_ec_table.Subj_ID == Subj_ID_ec idx_Subj_ID_eo = eo_ec_table.Subj_ID == Subj_ID_eo first_samp_ec = eo_ec_table[idx_Subj_ID_ec]['#f'].get_values()[0] first_samp_eo = eo_ec_table[idx_Subj_ID_eo]['#f'].get_values()[0] raw_eo = Raw(filename_eo) raw_ec = Raw(filename_ec) ec_length = round(raw_ec.times[-1], 0) raw_ec.append(raw_eo) raw_ec.save(filename_merged, overwrite=True) # lCondStart, lCondEnd, first_samp, cond = \ # get_eo_ec_by_name(Subj_ID_ec, 'ec') lCondStart_eo, lCondEnd_eo, _, _ = \ get_eo_ec_by_name(Subj_ID_eo, 'eo') lCondStart_eo = [time + ec_length - first_samp_eo + first_samp_ec for time in lCondStart_eo] lCondEnd_eo = [time + ec_length - first_samp_eo + first_samp_ec for time in lCondEnd_eo] print 'EO start times : {}'.format(lCondStart_eo) print 'EO end times : {}'.format(lCondEnd_eo) print 'Merged file length: {} seconds'.format(round(raw_ec.times[-1], 0)) return
def test_multiple_files(): """Test loading multiple files simultaneously """ # split file tempdir = _TempDir() raw = Raw(fif_fname).crop(0, 10) raw.load_data() raw.load_data() # test no operation split_size = 3. # in seconds sfreq = raw.info['sfreq'] nsamp = (raw.last_samp - raw.first_samp) tmins = np.round(np.arange(0., nsamp, split_size * sfreq)) tmaxs = np.concatenate((tmins[1:] - 1, [nsamp])) tmaxs /= sfreq tmins /= sfreq assert_equal(raw.n_times, len(raw.times)) # going in reverse order so the last fname is the first file (need later) raws = [None] * len(tmins) for ri in range(len(tmins) - 1, -1, -1): fname = op.join(tempdir, 'test_raw_split-%d_raw.fif' % ri) raw.save(fname, tmin=tmins[ri], tmax=tmaxs[ri]) raws[ri] = Raw(fname) events = [find_events(r, stim_channel='STI 014') for r in raws] last_samps = [r.last_samp for r in raws] first_samps = [r.first_samp for r in raws] # test concatenation of split file assert_raises(ValueError, concatenate_raws, raws, True, events[1:]) all_raw_1, events1 = concatenate_raws(raws, preload=False, events_list=events) assert_equal(raw.first_samp, all_raw_1.first_samp) assert_equal(raw.last_samp, all_raw_1.last_samp) assert_allclose(raw[:, :][0], all_raw_1[:, :][0]) raws[0] = Raw(fname) all_raw_2 = concatenate_raws(raws, preload=True) assert_allclose(raw[:, :][0], all_raw_2[:, :][0]) # test proper event treatment for split files events2 = concatenate_events(events, first_samps, last_samps) events3 = find_events(all_raw_2, stim_channel='STI 014') assert_array_equal(events1, events2) assert_array_equal(events1, events3) # test various methods of combining files raw = Raw(fif_fname, preload=True) n_times = raw.n_times # make sure that all our data match times = list(range(0, 2 * n_times, 999)) # add potentially problematic points times.extend([n_times - 1, n_times, 2 * n_times - 1]) raw_combo0 = Raw([fif_fname, fif_fname], preload=True) _compare_combo(raw, raw_combo0, times, n_times) raw_combo = Raw([fif_fname, fif_fname], preload=False) _compare_combo(raw, raw_combo, times, n_times) raw_combo = Raw([fif_fname, fif_fname], preload='memmap8.dat') _compare_combo(raw, raw_combo, times, n_times) assert_raises(ValueError, Raw, [fif_fname, ctf_fname]) assert_raises(ValueError, Raw, [fif_fname, fif_bad_marked_fname]) assert_equal(raw[:, :][0].shape[1] * 2, raw_combo0[:, :][0].shape[1]) assert_equal(raw_combo0[:, :][0].shape[1], raw_combo0.n_times) # with all data preloaded, result should be preloaded raw_combo = Raw(fif_fname, preload=True) raw_combo.append(Raw(fif_fname, preload=True)) assert_true(raw_combo.preload is True) assert_equal(raw_combo.n_times, raw_combo._data.shape[1]) _compare_combo(raw, raw_combo, times, n_times) # with any data not preloaded, don't set result as preloaded raw_combo = concatenate_raws( [Raw(fif_fname, preload=True), Raw(fif_fname, preload=False)]) assert_true(raw_combo.preload is False) assert_array_equal(find_events(raw_combo, stim_channel='STI 014'), find_events(raw_combo0, stim_channel='STI 014')) _compare_combo(raw, raw_combo, times, n_times) # user should be able to force data to be preloaded upon concat raw_combo = concatenate_raws( [Raw(fif_fname, preload=False), Raw(fif_fname, preload=True)], preload=True) assert_true(raw_combo.preload is True) _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws( [Raw(fif_fname, preload=False), Raw(fif_fname, preload=True)], preload='memmap3.dat') _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws( [Raw(fif_fname, preload=True), Raw(fif_fname, preload=True)], preload='memmap4.dat') _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws( [Raw(fif_fname, preload=False), Raw(fif_fname, preload=False)], preload='memmap5.dat') _compare_combo(raw, raw_combo, times, n_times) # verify that combining raws with different projectors throws an exception raw.add_proj([], remove_existing=True) assert_raises(ValueError, raw.append, Raw(fif_fname, preload=True)) # now test event treatment for concatenated raw files events = [ find_events(raw, stim_channel='STI 014'), find_events(raw, stim_channel='STI 014') ] last_samps = [raw.last_samp, raw.last_samp] first_samps = [raw.first_samp, raw.first_samp] events = concatenate_events(events, first_samps, last_samps) events2 = find_events(raw_combo0, stim_channel='STI 014') assert_array_equal(events, events2) # check out the len method assert_equal(len(raw), raw.n_times) assert_equal(len(raw), raw.last_samp - raw.first_samp + 1)
def test_multiple_files(): """Test loading multiple files simultaneously """ # split file tempdir = _TempDir() raw = Raw(fif_fname).crop(0, 10, False) raw.load_data() raw.load_data() # test no operation split_size = 3. # in seconds sfreq = raw.info['sfreq'] nsamp = (raw.last_samp - raw.first_samp) tmins = np.round(np.arange(0., nsamp, split_size * sfreq)) tmaxs = np.concatenate((tmins[1:] - 1, [nsamp])) tmaxs /= sfreq tmins /= sfreq assert_equal(raw.n_times, len(raw.times)) # going in reverse order so the last fname is the first file (need later) raws = [None] * len(tmins) for ri in range(len(tmins) - 1, -1, -1): fname = op.join(tempdir, 'test_raw_split-%d_raw.fif' % ri) raw.save(fname, tmin=tmins[ri], tmax=tmaxs[ri]) raws[ri] = Raw(fname) events = [find_events(r, stim_channel='STI 014') for r in raws] last_samps = [r.last_samp for r in raws] first_samps = [r.first_samp for r in raws] # test concatenation of split file assert_raises(ValueError, concatenate_raws, raws, True, events[1:]) all_raw_1, events1 = concatenate_raws(raws, preload=False, events_list=events) assert_equal(raw.first_samp, all_raw_1.first_samp) assert_equal(raw.last_samp, all_raw_1.last_samp) assert_allclose(raw[:, :][0], all_raw_1[:, :][0]) raws[0] = Raw(fname) all_raw_2 = concatenate_raws(raws, preload=True) assert_allclose(raw[:, :][0], all_raw_2[:, :][0]) # test proper event treatment for split files events2 = concatenate_events(events, first_samps, last_samps) events3 = find_events(all_raw_2, stim_channel='STI 014') assert_array_equal(events1, events2) assert_array_equal(events1, events3) # test various methods of combining files raw = Raw(fif_fname, preload=True) n_times = raw.n_times # make sure that all our data match times = list(range(0, 2 * n_times, 999)) # add potentially problematic points times.extend([n_times - 1, n_times, 2 * n_times - 1]) raw_combo0 = Raw([fif_fname, fif_fname], preload=True) _compare_combo(raw, raw_combo0, times, n_times) raw_combo = Raw([fif_fname, fif_fname], preload=False) _compare_combo(raw, raw_combo, times, n_times) raw_combo = Raw([fif_fname, fif_fname], preload='memmap8.dat') _compare_combo(raw, raw_combo, times, n_times) assert_raises(ValueError, Raw, [fif_fname, ctf_fname]) assert_raises(ValueError, Raw, [fif_fname, fif_bad_marked_fname]) assert_equal(raw[:, :][0].shape[1] * 2, raw_combo0[:, :][0].shape[1]) assert_equal(raw_combo0[:, :][0].shape[1], raw_combo0.n_times) # with all data preloaded, result should be preloaded raw_combo = Raw(fif_fname, preload=True) raw_combo.append(Raw(fif_fname, preload=True)) assert_true(raw_combo.preload is True) assert_equal(raw_combo.n_times, raw_combo._data.shape[1]) _compare_combo(raw, raw_combo, times, n_times) # with any data not preloaded, don't set result as preloaded raw_combo = concatenate_raws([Raw(fif_fname, preload=True), Raw(fif_fname, preload=False)]) assert_true(raw_combo.preload is False) assert_array_equal(find_events(raw_combo, stim_channel='STI 014'), find_events(raw_combo0, stim_channel='STI 014')) _compare_combo(raw, raw_combo, times, n_times) # user should be able to force data to be preloaded upon concat raw_combo = concatenate_raws([Raw(fif_fname, preload=False), Raw(fif_fname, preload=True)], preload=True) assert_true(raw_combo.preload is True) _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws([Raw(fif_fname, preload=False), Raw(fif_fname, preload=True)], preload='memmap3.dat') _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws([Raw(fif_fname, preload=True), Raw(fif_fname, preload=True)], preload='memmap4.dat') _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws([Raw(fif_fname, preload=False), Raw(fif_fname, preload=False)], preload='memmap5.dat') _compare_combo(raw, raw_combo, times, n_times) # verify that combining raws with different projectors throws an exception raw.add_proj([], remove_existing=True) assert_raises(ValueError, raw.append, Raw(fif_fname, preload=True)) # now test event treatment for concatenated raw files events = [find_events(raw, stim_channel='STI 014'), find_events(raw, stim_channel='STI 014')] last_samps = [raw.last_samp, raw.last_samp] first_samps = [raw.first_samp, raw.first_samp] events = concatenate_events(events, first_samps, last_samps) events2 = find_events(raw_combo0, stim_channel='STI 014') assert_array_equal(events, events2) # check out the len method assert_equal(len(raw), raw.n_times) assert_equal(len(raw), raw.last_samp - raw.first_samp + 1)