def test_io_egi(): """Test importing EGI simple binary files""" # test default tempdir = _TempDir() with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', category=RuntimeWarning) raw = read_raw_egi(egi_fname, include=None) assert_true('RawEGI' in repr(raw)) raw.load_data() # currently does nothing assert_equal(len(w), 1) assert_true(w[0].category == RuntimeWarning) msg = 'Did not find any event code with more than one event.' assert_true(msg in '%s' % w[0].message) include = ['TRSP', 'XXX1'] raw = read_raw_egi(egi_fname, include=include) repr(raw) repr(raw.info) assert_equal('eeg' in raw, True) out_fname = op.join(tempdir, 'test_egi_raw.fif') raw.save(out_fname) raw2 = Raw(out_fname, preload=True) data1, times1 = raw[:10, :] data2, times2 = raw2[:10, :] assert_array_almost_equal(data1, data2, 9) assert_array_almost_equal(times1, times2) eeg_chan = [c for c in raw.ch_names if 'EEG' in c] assert_equal(len(eeg_chan), 256) picks = pick_types(raw.info, eeg=True) assert_equal(len(picks), 256) assert_equal('STI 014' in raw.ch_names, True) events = find_events(raw, stim_channel='STI 014') assert_equal(len(events), 2) # ground truth assert_equal(np.unique(events[:, 1])[0], 0) assert_true(np.unique(events[:, 0])[0] != 0) assert_true(np.unique(events[:, 2])[0] != 0) triggers = np.array([[0, 1, 1, 0], [0, 0, 1, 0]]) # test trigger functionality assert_raises(RuntimeError, _combine_triggers, triggers, None) triggers = np.array([[0, 1, 0, 0], [0, 0, 1, 0]]) events_ids = [12, 24] new_trigger = _combine_triggers(triggers, events_ids) assert_array_equal(np.unique(new_trigger), np.unique([0, 12, 24])) assert_raises(ValueError, read_raw_egi, egi_fname, include=['Foo']) assert_raises(ValueError, read_raw_egi, egi_fname, exclude=['Bar']) for ii, k in enumerate(include, 1): assert_true(k in raw.event_id) assert_true(raw.event_id[k] == ii) # Make sure concatenation works raw_concat = concatenate_raws([raw.copy(), raw]) assert_equal(raw_concat.n_times, 2 * raw.n_times)
def test_io_egi_crop_no_preload(): """Test crop non-preloaded EGI MFF data (BUG).""" raw = read_raw_egi(egi_mff_fname, preload=False) raw.crop(17.5, 20.5) raw.load_data() raw_preload = read_raw_egi(egi_mff_fname, preload=True) raw_preload.crop(17.5, 20.5) raw_preload.load_data() assert_allclose(raw._data, raw_preload._data)
def test_io_egi_crop_no_preload(): """Test crop non-preloaded EGI MFF data (BUG).""" egi_fname_mff = op.join(data_path(), 'EGI', 'test_egi.mff') raw = read_raw_egi(egi_fname_mff, preload=False) raw.crop(17.5, 20.5) raw.load_data() raw_preload = read_raw_egi(egi_fname_mff, preload=True) raw_preload.crop(17.5, 20.5) raw_preload.load_data() assert_allclose(raw._data, raw_preload._data)
def test_egi_mff_pause_chunks(fname, tmp_path): """Test that on-demand of all short segments works (via I/O).""" fname_temp = tmp_path / 'test_raw.fif' raw_data = read_raw_egi(fname, preload=True).get_data() raw = read_raw_egi(fname) with pytest.warns(RuntimeWarning, match='Acquisition skips detected'): raw.save(fname_temp) del raw raw_data_2 = read_raw_fif(fname_temp).get_data() assert_allclose(raw_data, raw_data_2)
def test_io_egi(): """Test importing EGI simple binary files.""" # test default with open(egi_txt_fname) as fid: data = np.loadtxt(fid) t = data[0] data = data[1:] data *= 1e-6 # µV with pytest.warns(RuntimeWarning, match='Did not find any event code'): raw = read_raw_egi(egi_fname, include=None) # The reader should accept a Path, too. with pytest.warns(RuntimeWarning, match='Did not find any event code'): raw = read_raw_egi(Path(egi_fname), include=None) assert 'RawEGI' in repr(raw) data_read, t_read = raw[:256] assert_allclose(t_read, t) assert_allclose(data_read, data, atol=1e-10) include = ['TRSP', 'XXX1'] raw = _test_raw_reader(read_raw_egi, input_fname=egi_fname, include=include, test_rank='less', test_scaling=False, # XXX probably some bug ) assert 'eeg' in raw assert raw.orig_format == "single" eeg_chan = [c for c in raw.ch_names if c.startswith('E')] assert len(eeg_chan) == 256 picks = pick_types(raw.info, eeg=True) assert len(picks) == 256 assert 'STI 014' in raw.ch_names events = find_events(raw, stim_channel='STI 014') assert len(events) == 2 # ground truth assert np.unique(events[:, 1])[0] == 0 assert np.unique(events[:, 0])[0] != 0 assert np.unique(events[:, 2])[0] != 0 triggers = np.array([[0, 1, 1, 0], [0, 0, 1, 0]]) # test trigger functionality triggers = np.array([[0, 1, 0, 0], [0, 0, 1, 0]]) events_ids = [12, 24] new_trigger = _combine_triggers(triggers, events_ids) assert_array_equal(np.unique(new_trigger), np.unique([0, 12, 24])) pytest.raises(ValueError, read_raw_egi, egi_fname, include=['Foo'], preload=False) pytest.raises(ValueError, read_raw_egi, egi_fname, exclude=['Bar'], preload=False) for ii, k in enumerate(include, 1): assert (k in raw.event_id) assert (raw.event_id[k] == ii)
def test_io_egi_pns_mff(tmpdir): """Test importing EGI MFF with PNS data.""" raw = read_raw_egi(egi_mff_pns_fname, include=None, preload=True, verbose='error') assert ('RawMff' in repr(raw)) pns_chans = pick_types(raw.info, ecg=True, bio=True, emg=True) assert_equal(len(pns_chans), 7) names = [raw.ch_names[x] for x in pns_chans] pns_names = ['Resp. Temperature'[:15], 'Resp. Pressure', 'ECG', 'Body Position', 'Resp. Effort Chest'[:15], 'Resp. Effort Abdomen'[:15], 'EMG-Leg'] _test_raw_reader(read_raw_egi, input_fname=egi_mff_pns_fname, channel_naming='EEG %03d', verbose='error', test_rank='less', test_scaling=False, # XXX probably some bug ) assert_equal(names, pns_names) mat_names = [ 'Resp_Temperature'[:15], 'Resp_Pressure', 'ECG', 'Body_Position', 'Resp_Effort_Chest'[:15], 'Resp_Effort_Abdomen'[:15], 'EMGLeg' ] egi_fname_mat = op.join(data_path(), 'EGI', 'test_egi_pns.mat') mc = sio.loadmat(egi_fname_mat) for ch_name, ch_idx, mat_name in zip(pns_names, pns_chans, mat_names): print('Testing {}'.format(ch_name)) mc_key = [x for x in mc.keys() if mat_name in x][0] cal = raw.info['chs'][ch_idx]['cal'] mat_data = mc[mc_key] * cal raw_data = raw[ch_idx][0] assert_array_equal(mat_data, raw_data) # EEG missing new_mff = str(tmpdir.join('temp.mff')) shutil.copytree(egi_mff_pns_fname, new_mff) read_raw_egi(new_mff, verbose='error') os.remove(op.join(new_mff, 'info1.xml')) os.remove(op.join(new_mff, 'signal1.bin')) with pytest.raises(FileNotFoundError, match='Could not find any EEG'): read_raw_egi(new_mff, verbose='error')
def test_egi_dig_montage(): """Test EGI MFF XML dig montage support.""" dig_montage = read_dig_montage(egi=egi_dig_montage_fname, unit='m') # # test round-trip IO temp_dir = _TempDir() fname_temp = op.join(temp_dir, 'egi_test.fif') _check_roundtrip(dig_montage, fname_temp) # Test coordinate transform dig_montage.transform_to_head() # nasion assert_almost_equal(dig_montage.nasion[0], 0) assert_almost_equal(dig_montage.nasion[2], 0) # lpa and rpa assert_allclose(dig_montage.lpa[1:], 0, atol=1e-16) assert_allclose(dig_montage.rpa[1:], 0, atol=1e-16) # Test accuracy and embedding within raw object raw_egi = read_raw_egi(egi_raw_fname, channel_naming='EEG %03d') raw_egi.set_montage(dig_montage) test_raw_egi = read_raw_fif(egi_fif_fname) assert_equal(len(raw_egi.ch_names), len(test_raw_egi.ch_names)) for ch_raw, ch_test_raw in zip(raw_egi.info['chs'], test_raw_egi.info['chs']): assert_equal(ch_raw['ch_name'], ch_test_raw['ch_name']) assert_equal(ch_raw['coord_frame'], FIFF.FIFFV_COORD_HEAD) assert_allclose(ch_raw['loc'], ch_test_raw['loc'], atol=1e-7) assert_dig_allclose(raw_egi.info, test_raw_egi.info)
def test_io_egi_mff(): """Test importing EGI MFF simple binary files.""" raw = read_raw_egi(egi_mff_fname, include=None) assert ('RawMff' in repr(raw)) include = ['DIN1', 'DIN2', 'DIN3', 'DIN4', 'DIN5', 'DIN7'] raw = _test_raw_reader(read_raw_egi, input_fname=egi_mff_fname, include=include, channel_naming='EEG %03d', test_scaling=False, # XXX probably some bug ) assert raw.info['sfreq'] == 1000. assert_equal('eeg' in raw, True) eeg_chan = [c for c in raw.ch_names if 'EEG' in c] assert_equal(len(eeg_chan), 129) picks = pick_types(raw.info, eeg=True) assert_equal(len(picks), 129) assert_equal('STI 014' in raw.ch_names, True) events = find_events(raw, stim_channel='STI 014') assert_equal(len(events), 8) assert_equal(np.unique(events[:, 1])[0], 0) assert (np.unique(events[:, 0])[0] != 0) assert (np.unique(events[:, 2])[0] != 0) pytest.raises(ValueError, read_raw_egi, egi_mff_fname, include=['Foo'], preload=False) pytest.raises(ValueError, read_raw_egi, egi_mff_fname, exclude=['Bar'], preload=False) for ii, k in enumerate(include, 1): assert (k in raw.event_id) assert (raw.event_id[k] == ii)
def test_io_egi_pns_mff(): """Test importing EGI MFF with PNS data""" egi_fname_mff = op.join(data_path(), 'EGI', 'test_egi_pns.mff') raw = read_raw_egi(egi_fname_mff, include=None, preload=True) assert_true('RawMff' in repr(raw)) pns_chans = pick_types(raw.info, ecg=True, bio=True, emg=True) assert_equal(len(pns_chans), 7) names = [raw.ch_names[x] for x in pns_chans] pns_names = [ 'Resp. Temperature', 'Resp. Pressure', 'ECG', 'Body Position', 'Resp. Effort Chest', 'Resp. Effort Abdomen', 'EMG-Leg' ] _test_raw_reader(read_raw_egi, input_fname=egi_fname_mff, channel_naming='EEG %03d') assert_equal(names, pns_names) mat_names = [ 'Resp_Temperature', 'Resp_Pressure', 'ECG', 'Body_Position', 'Resp_Effort_Chest', 'Resp_Effort_Abdomen', 'EMGLeg' ] egi_fname_mat = op.join(data_path(), 'EGI', 'test_egi_pns.mat') mc = sio.loadmat(egi_fname_mat) for ch_name, ch_idx, mat_name in zip(pns_names, pns_chans, mat_names): print('Testing {}'.format(ch_name)) mc_key = [x for x in mc.keys() if mat_name in x][0] cal = raw.info['chs'][ch_idx]['cal'] mat_data = np.squeeze(mc[mc_key]) * cal raw_data = np.squeeze(raw._data[ch_idx, :]) assert_array_equal(mat_data, raw_data)
def test_egi_coord_frame(): """Test that EGI coordinate frame is changed to head.""" info = read_raw_egi(egi_mff_fname).info want_idents = ( FIFF.FIFFV_POINT_LPA, FIFF.FIFFV_POINT_NASION, FIFF.FIFFV_POINT_RPA, ) for ii, want in enumerate(want_idents): d = info['dig'][ii] assert d['kind'] == FIFF.FIFFV_POINT_CARDINAL assert d['ident'] == want loc = d['r'] if ii == 0: assert 0.05 < -loc[0] < 0.1, 'LPA' assert_allclose(loc[1:], 0, atol=1e-7, err_msg='LPA') elif ii == 1: assert 0.05 < loc[1] < 0.11, 'Nasion' assert_allclose(loc[::2], 0, atol=1e-7, err_msg='Nasion') else: assert ii == 2 assert 0.05 < loc[0] < 0.1, 'RPA' assert_allclose(loc[1:], 0, atol=1e-7, err_msg='RPA') for d in info['dig'][3:]: assert d['kind'] == FIFF.FIFFV_POINT_EEG
def test_io_egi_pns_mff_bug(): """Test importing EGI MFF with PNS data (BUG)""" egi_fname_mff = op.join(data_path(), 'EGI', 'test_egi_pns_bug.mff') with warnings.catch_warnings(record=True) as w: raw = read_raw_egi(egi_fname_mff, include=None, preload=True, verbose='warning') assert any('EGI PSG sample bug' in str(ww.message) for ww in w) egi_fname_mat = op.join(data_path(), 'EGI', 'test_egi_pns.mat') mc = sio.loadmat(egi_fname_mat) pns_chans = pick_types(raw.info, ecg=True, bio=True, emg=True) pns_names = [ 'Resp. Temperature'[:15], 'Resp. Pressure', 'ECG', 'Body Position', 'Resp. Effort Chest'[:15], 'Resp. Effort Abdomen'[:15], 'EMG-Leg' ] mat_names = [ 'Resp_Temperature'[:15], 'Resp_Pressure', 'ECG', 'Body_Position', 'Resp_Effort_Chest'[:15], 'Resp_Effort_Abdomen'[:15], 'EMGLeg' ] for ch_name, ch_idx, mat_name in zip(pns_names, pns_chans, mat_names): print('Testing {}'.format(ch_name)) mc_key = [x for x in mc.keys() if mat_name in x][0] cal = raw.info['chs'][ch_idx]['cal'] mat_data = mc[mc_key] * cal mat_data[:, -1] = 0 # The MFF has one less sample, the last one raw_data = raw[ch_idx][0] assert_array_equal(mat_data, raw_data)
def test_io_egi_mff(): """Test importing EGI MFF simple binary files.""" egi_fname_mff = op.join(data_path(), 'EGI', 'test_egi.mff') raw = read_raw_egi(egi_fname_mff, include=None) assert ('RawMff' in repr(raw)) include = ['DIN1', 'DIN2', 'DIN3', 'DIN4', 'DIN5', 'DIN7'] raw = _test_raw_reader(read_raw_egi, input_fname=egi_fname_mff, include=include, channel_naming='EEG %03d') assert_equal('eeg' in raw, True) eeg_chan = [c for c in raw.ch_names if 'EEG' in c] assert_equal(len(eeg_chan), 129) picks = pick_types(raw.info, eeg=True) assert_equal(len(picks), 129) assert_equal('STI 014' in raw.ch_names, True) events = find_events(raw, stim_channel='STI 014') assert_equal(len(events), 8) assert_equal(np.unique(events[:, 1])[0], 0) assert (np.unique(events[:, 0])[0] != 0) assert (np.unique(events[:, 2])[0] != 0) pytest.raises(ValueError, read_raw_egi, egi_fname_mff, include=['Foo'], preload=False) pytest.raises(ValueError, read_raw_egi, egi_fname_mff, exclude=['Bar'], preload=False) for ii, k in enumerate(include, 1): assert (k in raw.event_id) assert (raw.event_id[k] == ii)
def test_io_egi_pns_mff_bug(): """Test importing EGI MFF with PNS data (BUG).""" egi_fname_mff = op.join(data_path(), 'EGI', 'test_egi_pns_bug.mff') with pytest.warns(RuntimeWarning, match='EGI PSG sample bug'): raw = read_raw_egi(egi_fname_mff, include=None, preload=True, verbose='warning') egi_fname_mat = op.join(data_path(), 'EGI', 'test_egi_pns.mat') mc = sio.loadmat(egi_fname_mat) pns_chans = pick_types(raw.info, ecg=True, bio=True, emg=True) pns_names = ['Resp. Temperature'[:15], 'Resp. Pressure', 'ECG', 'Body Position', 'Resp. Effort Chest'[:15], 'Resp. Effort Abdomen'[:15], 'EMG-Leg'] mat_names = [ 'Resp_Temperature'[:15], 'Resp_Pressure', 'ECG', 'Body_Position', 'Resp_Effort_Chest'[:15], 'Resp_Effort_Abdomen'[:15], 'EMGLeg' ] for ch_name, ch_idx, mat_name in zip(pns_names, pns_chans, mat_names): print('Testing {}'.format(ch_name)) mc_key = [x for x in mc.keys() if mat_name in x][0] cal = raw.info['chs'][ch_idx]['cal'] mat_data = mc[mc_key] * cal mat_data[:, -1] = 0 # The MFF has one less sample, the last one raw_data = raw[ch_idx][0] assert_array_equal(mat_data, raw_data)
def test_io_egi_pns_mff_bug(preload): """Test importing EGI MFF with PNS data (BUG).""" egi_fname_mff = op.join(data_path(), 'EGI', 'test_egi_pns_bug.mff') with pytest.warns(RuntimeWarning, match='EGI PSG sample bug'): raw = read_raw_egi(egi_fname_mff, include=None, preload=preload, verbose='warning') assert len(raw.annotations) == 1 assert_allclose(raw.annotations.duration, [0.004]) assert_allclose(raw.annotations.onset, [13.948]) egi_fname_mat = op.join(data_path(), 'EGI', 'test_egi_pns.mat') mc = sio.loadmat(egi_fname_mat) pns_chans = pick_types(raw.info, ecg=True, bio=True, emg=True) pns_names = [ 'Resp. Temperature'[:15], 'Resp. Pressure', 'ECG', 'Body Position', 'Resp. Effort Chest'[:15], 'Resp. Effort Abdomen'[:15], 'EMG-Leg' ] mat_names = [ 'Resp_Temperature'[:15], 'Resp_Pressure', 'ECG', 'Body_Position', 'Resp_Effort_Chest'[:15], 'Resp_Effort_Abdomen'[:15], 'EMGLeg' ] for ch_name, ch_idx, mat_name in zip(pns_names, pns_chans, mat_names): print('Testing {}'.format(ch_name)) mc_key = [x for x in mc.keys() if mat_name in x][0] cal = raw.info['chs'][ch_idx]['cal'] mat_data = mc[mc_key] * cal mat_data[:, -1] = 0 # The MFF has one less sample, the last one raw_data = raw[ch_idx][0] assert_array_equal(mat_data, raw_data)
def mne_read_sleep(file, ext, *args, **kwargs): """Read sleep datasets using mne.io. Args: file: string Filename. ext: string File extension. arg: tuple Further arguments. Kargs: kargs: dict, optional, (def: {}) Further optional arguments. """ from mne.io import (read_raw_egi, read_raw_cnt) raise ValueError("ASSURER COMPATIBILITE MNE") # Get full path : path = file + ext if ext == '.egi': # EGI return read_raw_egi(path, *args, **kwargs) elif ext == '.cnt': # EGI return read_raw_cnt(path, *args, **kwargs)
def process_row(row, path_to_epoch_1, path_to_epoch_2, path_to_epoch_3, only_10_20=True): sample = read_raw_egi(row['full_path'], verbose=0) df = sample.to_data_frame() n_epoches = 3 epoch_types = None if row['fn'][:-4] == 'Smirnova rest 24-03': n_epoches = 4 epoch_types = [0, 1, 0, 1] elif row['fn'][:-4] == 'Saradjev rest 16-11': n_epoches = 5 epoch_types = [0, 1, 0, 0, 0] events = get_events(df, n_epoches=n_epoches, epoch_types=epoch_types) picks = mne.pick_types(sample.info, meg=False, eeg=True) df = get_epoches(df, picks, events) columns_to_use = [col for col in df.columns if col[0] == 'E'] df = df.loc[:, columns_to_use] def replace_if_possible(col): if col in electrode_mapping: return electrode_mapping[col] else: return col df.columns = [replace_if_possible(col) for col in df.columns] df['cz'] = 0 if only_10_20: df = df[electrodes] # save epoch 1 df_epoch = get_epoch(df, 0) df_epoch = df_epoch.iloc[:30000][::4] df_epoch.to_csv(join(path_to_epoch_1, row['fn'])) # save epoch 2 idx = 1 if row['fn'][:-4] == 'Smirnova rest 24-03': idx = 3 df_epoch = get_epoch(df, idx) df_epoch = df_epoch.iloc[:30000][::4] df_epoch.to_csv(join(path_to_epoch_2, row['fn'])) # save epoch 3 idx = 2 if row['fn'][:-4] == 'Saradjev rest 16-11': idx = 4 df_epoch = get_epoch(df, idx) df_epoch = df_epoch.iloc[:30000][::4] df_epoch.to_csv(join(path_to_epoch_3, row['fn']))
def test_io_egi_mff(): """Test importing EGI MFF simple binary files.""" raw = read_raw_egi(egi_mff_fname, include=None) assert ('RawMff' in repr(raw)) include = ['DIN1', 'DIN2', 'DIN3', 'DIN4', 'DIN5', 'DIN7'] raw = _test_raw_reader( read_raw_egi, input_fname=egi_mff_fname, include=include, channel_naming='EEG %03d', test_scaling=False, # XXX probably some bug ) assert raw.info['sfreq'] == 1000. # The ref here is redundant, but we don't currently have a way in # DigMontage to mark that a given channel is actually the ref so... assert len(raw.info['dig']) == 133 # 129 eeg + 1 ref + 3 cardinal points assert raw.info['dig'][0]['ident'] == 1 # EEG channel E1 assert raw.info['dig'][3]['ident'] == 0 # Reference channel assert raw.info['dig'][-1]['ident'] == 129 # Reference channel ref_loc = raw.info['dig'][3]['r'] eeg_picks = pick_types(raw.info, eeg=True) assert len(eeg_picks) == 129 for i in eeg_picks: loc = raw.info['chs'][i]['loc'] assert loc[:3].any(), loc[:3] assert_array_equal(loc[3:6], ref_loc, err_msg=f'{i}') assert raw.info['device_info']['type'] == 'HydroCel GSN 128 1.0' assert 'eeg' in raw eeg_chan = [c for c in raw.ch_names if 'EEG' in c] assert len(eeg_chan) == 129 assert 'STI 014' in raw.ch_names events = find_events(raw, stim_channel='STI 014') assert len(events) == 8 assert np.unique(events[:, 1])[0] == 0 assert np.unique(events[:, 0])[0] != 0 assert np.unique(events[:, 2])[0] != 0 with pytest.raises(ValueError, match='Could not find event'): read_raw_egi(egi_mff_fname, include=['Foo']) with pytest.raises(ValueError, match='Could not find event'): read_raw_egi(egi_mff_fname, exclude=['Bar']) for ii, k in enumerate(include, 1): assert k in raw.event_id assert raw.event_id[k] == ii
def test_io_egi(): """Test importing EGI simple binary files""" # test default with open(egi_txt_fname) as fid: data = np.loadtxt(fid) t = data[0] data = data[1:] data *= 1e-6 # μV with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') raw = read_raw_egi(egi_fname, include=None) assert_true('RawEGI' in repr(raw)) assert_equal(len(w), 1) assert_true(w[0].category == RuntimeWarning) msg = 'Did not find any event code with more than one event.' assert_true(msg in '%s' % w[0].message) data_read, t_read = raw[:256] assert_allclose(t_read, t) assert_allclose(data_read, data, atol=1e-10) include = ['TRSP', 'XXX1'] with warnings.catch_warnings(record=True): # preload=None raw = _test_raw_reader(read_raw_egi, input_fname=egi_fname, include=include) assert_equal('eeg' in raw, True) eeg_chan = [c for c in raw.ch_names if 'EEG' in c] assert_equal(len(eeg_chan), 256) picks = pick_types(raw.info, eeg=True) assert_equal(len(picks), 256) assert_equal('STI 014' in raw.ch_names, True) events = find_events(raw, stim_channel='STI 014') assert_equal(len(events), 2) # ground truth assert_equal(np.unique(events[:, 1])[0], 0) assert_true(np.unique(events[:, 0])[0] != 0) assert_true(np.unique(events[:, 2])[0] != 0) triggers = np.array([[0, 1, 1, 0], [0, 0, 1, 0]]) # test trigger functionality triggers = np.array([[0, 1, 0, 0], [0, 0, 1, 0]]) events_ids = [12, 24] new_trigger = _combine_triggers(triggers, events_ids) assert_array_equal(np.unique(new_trigger), np.unique([0, 12, 24])) assert_raises(ValueError, read_raw_egi, egi_fname, include=['Foo'], preload=False) assert_raises(ValueError, read_raw_egi, egi_fname, exclude=['Bar'], preload=False) for ii, k in enumerate(include, 1): assert_true(k in raw.event_id) assert_true(raw.event_id[k] == ii)
def test_meas_date(fname, timestamp, utc_offset): """Test meas date conversion.""" raw = read_raw_egi(fname, verbose='warning') dt = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%f%z') measdate = dt.astimezone(timezone.utc) hour_local = int(dt.strftime('%H')) hour_utc = int(raw.info['meas_date'].strftime('%H')) local_utc_diff = hour_local - hour_utc assert raw.info['meas_date'] == measdate assert raw.info['utc_offset'] == utc_offset assert local_utc_diff == int(utc_offset[:-2])
def test_io_egi(): """Test importing EGI simple binary files.""" # test default with open(egi_txt_fname) as fid: data = np.loadtxt(fid) t = data[0] data = data[1:] data *= 1e-6 # μV with pytest.warns(RuntimeWarning, match='Did not find any event code'): raw = read_raw_egi(egi_fname, include=None) assert 'RawEGI' in repr(raw) data_read, t_read = raw[:256] assert_allclose(t_read, t) assert_allclose(data_read, data, atol=1e-10) include = ['TRSP', 'XXX1'] raw = _test_raw_reader(read_raw_egi, input_fname=egi_fname, include=include) assert_equal('eeg' in raw, True) eeg_chan = [c for c in raw.ch_names if c.startswith('E')] assert_equal(len(eeg_chan), 256) picks = pick_types(raw.info, eeg=True) assert_equal(len(picks), 256) assert_equal('STI 014' in raw.ch_names, True) events = find_events(raw, stim_channel='STI 014') assert_equal(len(events), 2) # ground truth assert_equal(np.unique(events[:, 1])[0], 0) assert (np.unique(events[:, 0])[0] != 0) assert (np.unique(events[:, 2])[0] != 0) triggers = np.array([[0, 1, 1, 0], [0, 0, 1, 0]]) # test trigger functionality triggers = np.array([[0, 1, 0, 0], [0, 0, 1, 0]]) events_ids = [12, 24] new_trigger = _combine_triggers(triggers, events_ids) assert_array_equal(np.unique(new_trigger), np.unique([0, 12, 24])) pytest.raises(ValueError, read_raw_egi, egi_fname, include=['Foo'], preload=False) pytest.raises(ValueError, read_raw_egi, egi_fname, exclude=['Bar'], preload=False) for ii, k in enumerate(include, 1): assert (k in raw.event_id) assert (raw.event_id[k] == ii)
def main(): # Set MNE logging to only print critical issues set_log_level('CRITICAL') # Get list of all available subjects subj_nums = [file for file in os.listdir(DAT_PATH) if file[0] is not '.'] file_labels = [] for sub_num in subj_nums: subj_path = os.path.join(DAT_PATH, sub_num, 'EEG', 'raw', 'raw_format') subj_files = [file for file in os.listdir(subj_path) if ('A0' in file and file.endswith('.raw'))] print('\nRunning Subject #', sub_num) for ind, subj_file in enumerate(subj_files): raw = read_raw_egi(os.path.join(subj_path, subj_file), preload=False) cur_task = None # Special case: weird movie blocks if len(raw.event_id.keys()) > 25: cur_task = 'movie' print('\tSet event as misc / movie') continue for key, vals in EVENTS.items(): for val in vals: if cln_dict(raw.event_id) == val: print('\tFound event:', key) cur_task = key break if not cur_task: print('\tNo match found for file.') file_labels.append((subj_file[0:9], subj_file, cur_task)) # Save out a file of all the mappings with open('file_mappings.csv', 'w') as csvfile: writer = csv.writer(csvfile) for f_info in file_labels: writer.writerow(list(f_info))
def test_egi_mff_pause(fname, skip_times, event_times): """Test EGI MFF with pauses.""" if fname == egi_pause_w1337_fname: # too slow to _test_raw_reader raw = read_raw_egi(fname).load_data() else: with pytest.warns(RuntimeWarning, match='Acquisition skips detected'): raw = _test_raw_reader( read_raw_egi, input_fname=fname, test_scaling=False, # XXX probably some bug test_rank='less', ) assert raw.info['sfreq'] == 250. # true for all of these files assert len(raw.annotations) == len(skip_times) # assert event onsets match expected times if event_times is None: with pytest.raises(ValueError, match='Consider using .*events_from'): find_events(raw) else: events = find_events(raw) for event_type in event_times.keys(): ns_samples = np.floor( np.array(event_times[event_type]) * raw.info['sfreq']) assert_array_equal( events[events[:, 2] == raw.event_id[event_type], 0], ns_samples) # read some data from the middle of the skip, assert it's all zeros stim_picks = pick_types(raw.info, meg=False, stim=True, exclude=()) other_picks = np.setdiff1d(np.arange(len(raw.ch_names)), stim_picks) for ii, annot in enumerate(raw.annotations): assert annot['description'] == 'BAD_ACQ_SKIP' start, stop = raw.time_as_index( [annot['onset'], annot['onset'] + annot['duration']]) data, _ = raw[:, start:stop] assert_array_equal(data[other_picks], 0.) if event_times is not None: assert raw.ch_names[-1] == 'STI 014' assert not np.array_equal(data[stim_picks], 0.) # assert skips match expected onset and duration skip = ((start + 1) / raw.info['sfreq'] * 1e6, (stop + 1) / raw.info['sfreq'] * 1e6) assert skip == skip_times[ii]
def test_egi_dig_montage(): """Test EGI MFF XML dig montage support.""" dig_montage = read_dig_egi(egi_dig_montage_fname) fid, coord = _get_fid_coords(dig_montage.dig) assert coord == FIFF.FIFFV_COORD_UNKNOWN assert_allclose( actual=np.array([fid[key] for key in ['nasion', 'lpa', 'rpa']]), desired=[ [0., 10.564, -2.051], # noqa [-8.592, 0.498, -4.128], # noqa [8.592, 0.498, -4.128] ], # noqa ) # Test accuracy and embedding within raw object raw_egi = read_raw_egi(egi_raw_fname, channel_naming='EEG %03d') raw_egi.set_montage(dig_montage) test_raw_egi = read_raw_fif(egi_fif_fname) assert_equal(len(raw_egi.ch_names), len(test_raw_egi.ch_names)) for ch_raw, ch_test_raw in zip(raw_egi.info['chs'], test_raw_egi.info['chs']): assert_equal(ch_raw['ch_name'], ch_test_raw['ch_name']) assert_equal(ch_raw['coord_frame'], FIFF.FIFFV_COORD_HEAD) assert_allclose(ch_raw['loc'], ch_test_raw['loc'], atol=1e-7) assert_dig_allclose(raw_egi.info, test_raw_egi.info) dig_montage_in_head = transform_to_head(dig_montage.copy()) fid, coord = _get_fid_coords(dig_montage_in_head.dig) assert coord == FIFF.FIFFV_COORD_HEAD assert_allclose( actual=np.array([fid[key] for key in ['nasion', 'lpa', 'rpa']]), desired=[[0., 10.278, 0.], [-8.592, 0., 0.], [8.592, 0., 0.]], atol=1e-4, ) # test round-trip IO temp_dir = _TempDir() fname_temp = op.join(temp_dir, 'egi_test.fif') _check_roundtrip(dig_montage_in_head, fname_temp) # XXX: write forces head
def test_io_egi(): """Test importing EGI simple binary files""" # test default with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always", category=RuntimeWarning) raw = read_raw_egi(egi_fname, include=None) assert_true("RawEGI" in repr(raw)) assert_equal(len(w), 1) assert_true(w[0].category == RuntimeWarning) msg = "Did not find any event code with more than one event." assert_true(msg in "%s" % w[0].message) include = ["TRSP", "XXX1"] raw = _test_raw_reader(read_raw_egi, False, True, input_fname=egi_fname, include=include) assert_equal("eeg" in raw, True) eeg_chan = [c for c in raw.ch_names if "EEG" in c] assert_equal(len(eeg_chan), 256) picks = pick_types(raw.info, eeg=True) assert_equal(len(picks), 256) assert_equal("STI 014" in raw.ch_names, True) events = find_events(raw, stim_channel="STI 014") assert_equal(len(events), 2) # ground truth assert_equal(np.unique(events[:, 1])[0], 0) assert_true(np.unique(events[:, 0])[0] != 0) assert_true(np.unique(events[:, 2])[0] != 0) triggers = np.array([[0, 1, 1, 0], [0, 0, 1, 0]]) # test trigger functionality assert_raises(RuntimeError, _combine_triggers, triggers, None) triggers = np.array([[0, 1, 0, 0], [0, 0, 1, 0]]) events_ids = [12, 24] new_trigger = _combine_triggers(triggers, events_ids) assert_array_equal(np.unique(new_trigger), np.unique([0, 12, 24])) assert_raises(ValueError, read_raw_egi, egi_fname, include=["Foo"]) assert_raises(ValueError, read_raw_egi, egi_fname, exclude=["Bar"]) for ii, k in enumerate(include, 1): assert_true(k in raw.event_id) assert_true(raw.event_id[k] == ii)
def GetMffData(mff_file, seglen): sys.stdout = open(os.devnull, 'w') #suppress text output seglen = int(seglen) startload = dt.datetime.now() #Read mff and get last seglen of data raw = read_raw_egi(mff_file, include=None, preload=False) sfreq = raw.info['sfreq'] newdat = raw[:, raw.n_times - (seglen * int(sfreq)):raw.n_times - 1] data = newdat[0] times = newdat[1] #Save raw data and sample times to csv files np.savetxt('Power.csv', data, delimiter=',') np.savetxt('Times.csv', times, delimiter=',') #return the amount of time it took to complete task endload = dt.datetime.now() loadtime = endload.timestamp() - startload.timestamp() sys.stdout = sys.__stdout__ #re-enable text output return loadtime
def test_io_egi_pns_mff(): """Test importing EGI MFF with PNS data.""" egi_fname_mff = op.join(data_path(), 'EGI', 'test_egi_pns.mff') raw = read_raw_egi(egi_fname_mff, include=None, preload=True, verbose='error') assert ('RawMff' in repr(raw)) pns_chans = pick_types(raw.info, ecg=True, bio=True, emg=True) assert_equal(len(pns_chans), 7) names = [raw.ch_names[x] for x in pns_chans] pns_names = ['Resp. Temperature'[:15], 'Resp. Pressure', 'ECG', 'Body Position', 'Resp. Effort Chest'[:15], 'Resp. Effort Abdomen'[:15], 'EMG-Leg'] _test_raw_reader(read_raw_egi, input_fname=egi_fname_mff, channel_naming='EEG %03d', verbose='error') assert_equal(names, pns_names) mat_names = [ 'Resp_Temperature'[:15], 'Resp_Pressure', 'ECG', 'Body_Position', 'Resp_Effort_Chest'[:15], 'Resp_Effort_Abdomen'[:15], 'EMGLeg' ] egi_fname_mat = op.join(data_path(), 'EGI', 'test_egi_pns.mat') mc = sio.loadmat(egi_fname_mat) for ch_name, ch_idx, mat_name in zip(pns_names, pns_chans, mat_names): print('Testing {}'.format(ch_name)) mc_key = [x for x in mc.keys() if mat_name in x][0] cal = raw.info['chs'][ch_idx]['cal'] mat_data = mc[mc_key] * cal raw_data = raw[ch_idx][0] assert_array_equal(mat_data, raw_data)
def test_io_egi_mff(): """Test importing EGI MFF simple binary files""" egi_fname_mff = op.join(data_path(), 'EGI', 'test_egi.mff') raw = read_raw_egi(egi_fname_mff, include=None) assert_true('RawMff' in repr(raw)) include = ['DIN1', 'DIN2', 'DIN3', 'DIN4', 'DIN5', 'DIN7'] raw = _test_raw_reader(read_raw_egi, input_fname=egi_fname_mff, include=include, channel_naming='EEG %03d') assert_equal('eeg' in raw, True) eeg_chan = [c for c in raw.ch_names if 'EEG' in c] assert_equal(len(eeg_chan), 129) picks = pick_types(raw.info, eeg=True) assert_equal(len(picks), 129) assert_equal('STI 014' in raw.ch_names, True) events = find_events(raw, stim_channel='STI 014') assert_equal(len(events), 8) assert_equal(np.unique(events[:, 1])[0], 0) assert_true(np.unique(events[:, 0])[0] != 0) assert_true(np.unique(events[:, 2])[0] != 0) assert_raises(ValueError, read_raw_egi, egi_fname_mff, include=['Foo'], preload=False) assert_raises(ValueError, read_raw_egi, egi_fname_mff, exclude=['Bar'], preload=False) for ii, k in enumerate(include, 1): assert_true(k in raw.event_id) assert_true(raw.event_id[k] == ii)
def mne_switch(file, ext, downsample, preload=True, **kwargs): """Read sleep datasets using mne.io. Parameters ---------- file : string Filename (without extension). ext : string File extension (e.g. '.edf''). preload : bool | True Preload data in memory. kwargs : dict | {} Further arguments to pass to the mne.io.read function. Returns ------- sf : float The original sampling-frequency. downsample : float The down-sampling frequency used. dsf : int The down-sampling factor. data : array_like The raw data of shape (n_channels, n_points) channels : list List of channel names. n : int Number of time points before down-sampling. start_time : datetime.time The time offset. """ from mne import io # Get full path : path = file + ext # Preload : if preload is False: preload = 'temp.dat' kwargs['preload'] = preload if ext.lower() in ['.edf', '.bdf', '.gdf']: # EDF / BDF / GDF raw = io.read_raw_edf(path, **kwargs) elif ext.lower == '.set': # EEGLAB raw = io.read_raw_eeglab(path, **kwargs) elif ext.lower() in ['.egi', '.mff']: # EGI / MFF raw = io.read_raw_egi(path, **kwargs) elif ext.lower() == '.cnt': # CNT raw = io.read_raw_cnt(path, **kwargs) elif ext.lower() == '.vhdr': # BrainVision raw = io.read_raw_brainvision(path, **kwargs) else: raise IOError("File not supported by mne-python.") raw.pick_types(meg=True, eeg=True, ecg=True, emg=True) # Remove stim lines sf = raw.info['sfreq'] dsf, downsample = get_dsf(downsample, sf) channels = raw.info['ch_names'] data = raw._data # Conversion Volt (MNE) to microVolt (Visbrain): if raw._raw_extras[0] is not None and 'units' in raw._raw_extras[0]: units = raw._raw_extras[0]['units'][0:data.shape[0]] data /= np.array(units).reshape(-1, 1) n = data.shape[1] start_time = datetime.time(0, 0, 0) # raw.info['meas_date'] anot = raw.annotations return sf, downsample, dsf, data[:, ::dsf], channels, n, start_time, anot
def test_io_egi(): """Test importing EGI simple binary files.""" # test default with open(egi_txt_fname) as fid: data = np.loadtxt(fid) t = data[0] data = data[1:] data *= 1e-6 # μV with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') raw = read_raw_egi(egi_fname, include=None) assert_true('RawEGI' in repr(raw)) assert_equal(len(w), 1) assert_true(w[0].category == RuntimeWarning) msg = 'Did not find any event code with more than one event.' assert_true(msg in '%s' % w[0].message) data_read, t_read = raw[:256] assert_allclose(t_read, t) assert_allclose(data_read, data, atol=1e-10) include = ['TRSP', 'XXX1'] with warnings.catch_warnings(record=True): # preload=None raw = _test_raw_reader(read_raw_egi, input_fname=egi_fname, include=include) assert_equal('eeg' in raw, True) eeg_chan = [c for c in raw.ch_names if c.startswith('E')] assert_equal(len(eeg_chan), 256) picks = pick_types(raw.info, eeg=True) assert_equal(len(picks), 256) assert_equal('STI 014' in raw.ch_names, True) events = find_events(raw, stim_channel='STI 014') assert_equal(len(events), 2) # ground truth assert_equal(np.unique(events[:, 1])[0], 0) assert_true(np.unique(events[:, 0])[0] != 0) assert_true(np.unique(events[:, 2])[0] != 0) triggers = np.array([[0, 1, 1, 0], [0, 0, 1, 0]]) # test trigger functionality triggers = np.array([[0, 1, 0, 0], [0, 0, 1, 0]]) events_ids = [12, 24] new_trigger = _combine_triggers(triggers, events_ids) assert_array_equal(np.unique(new_trigger), np.unique([0, 12, 24])) assert_raises(ValueError, read_raw_egi, egi_fname, include=['Foo'], preload=False) assert_raises(ValueError, read_raw_egi, egi_fname, exclude=['Bar'], preload=False) for ii, k in enumerate(include, 1): assert_true(k in raw.event_id) assert_true(raw.event_id[k] == ii)
import sys from mne.channels import read_dig_montage from mne.io import read_raw_egi # Command Line Arguments # 1. The path to the EGI simple binary (.raw) file # 2. The path to the corresponding coordinates (.xml) file # 3. The output path to where you'd like to write the fif file # Note that the output fif file should end with _raw.fif to keep with MNE # convetion # Example of usage: # python convert_egi_raw.py ./egi_raw/test_data.raw ./coordinates/ # test_data_coordinates.xml ./fif_raw/test_raw.fif arguments = sys.argv raw_input_path = arguments[1] coordinates_path = arguments[2] raw_output_path = arguments[3] raw = read_raw_egi(raw_input_path, misc=['EEG 257']) dig_montage = read_dig_montage(egi=coordinates_path) raw.set_montage(dig_montage) raw.save(raw_output_path)