def test_read_segment(): """Test writing raw edf files when preload is False """ tempdir = _TempDir() raw1 = read_raw_edf(edf_path, stim_channel=139, preload=False) raw1_file = op.join(tempdir, 'test1-raw.fif') raw1.save(raw1_file, overwrite=True, buffer_size_sec=1) raw11 = Raw(raw1_file, preload=True) data1, times1 = raw1[:139, :] data11, times11 = raw11[:139, :] assert_allclose(data1, data11, rtol=1e-6) assert_array_almost_equal(times1, times11) assert_equal(sorted(raw1.info.keys()), sorted(raw11.info.keys())) raw2 = read_raw_edf(edf_path, stim_channel=139, preload=True) raw2_file = op.join(tempdir, 'test2-raw.fif') raw2.save(raw2_file, overwrite=True) data2, times2 = raw2[:139, :] assert_allclose(data1, data2, rtol=1e-6) assert_array_equal(times1, times2) raw1 = Raw(raw1_file, preload=True) raw2 = Raw(raw2_file, preload=True) assert_array_equal(raw1._data, raw2._data) # test the _read_segment function by only loading some of the data raw1 = read_raw_edf(edf_path, preload=False) raw2 = read_raw_edf(edf_path, preload=True) # select some random range of data to compare data1, times1 = raw1[:, 345:417] data2, times2 = raw2[:, 345:417] assert_array_equal(data1, data2) assert_array_equal(times1, times2)
def test_stim_channel(): """Test reading raw edf files with stim channel""" raw_py = read_raw_edf(edf_path, misc=range(-4, 0), stim_channel=139, preload=True) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude=['EDF Annotations']) data_py, _ = raw_py[picks] print(raw_py) # to test repr print(raw_py.info) # to test Info repr # this .mat was generated using the EEG Lab Biosemi Reader raw_eeglab = io.loadmat(edf_eeglab_path) raw_eeglab = raw_eeglab['data'] * 1e-6 # data are stored in microvolts data_eeglab = raw_eeglab[picks] assert_array_almost_equal(data_py, data_eeglab, 10) # Test uneven sampling raw_py = read_raw_edf(edf_uneven_path, stim_channel=None) data_py, _ = raw_py[0] # this .mat was generated using the EEG Lab Biosemi Reader raw_eeglab = io.loadmat(edf_uneven_eeglab_path) raw_eeglab = raw_eeglab['data'] data_eeglab = raw_eeglab[0] # match upsampling upsample = len(data_eeglab) / len(raw_py) data_py = np.repeat(data_py, repeats=upsample) assert_array_equal(data_py, data_eeglab) assert_raises(RuntimeError, read_raw_edf, edf_path, preload=False)
def test_bdf_stim_channel(): """Test BDF stim channel.""" # test if last channel is detected as STIM by default raw_py = _test_raw_reader(read_raw_edf, input_fname=bdf_path, stim_channel='auto') assert channel_type(raw_py.info, raw_py.info["nchan"] - 1) == 'stim' # test BDF file with wrong scaling info in header - this should be ignored # for BDF stim channels events = [[242, 0, 4], [310, 0, 2], [952, 0, 1], [1606, 0, 1], [2249, 0, 1], [2900, 0, 1], [3537, 0, 1], [4162, 0, 1], [4790, 0, 1]] with pytest.deprecated_call(match='stim_channel'): raw = read_raw_edf(bdf_stim_channel_path, preload=True) bdf_events = find_events(raw) assert_array_equal(events, bdf_events) raw = read_raw_edf(bdf_stim_channel_path, preload=False, stim_channel='auto') bdf_events = find_events(raw) assert_array_equal(events, bdf_events)
def test_gdf2_data(): """Test reading raw GDF 2.x files.""" raw = read_raw_edf(gdf2_path + '.gdf', eog=None, misc=None, preload=True, stim_channel='STATUS') nchan = raw.info['nchan'] ch_names = raw.ch_names # Renamed STATUS -> STI 014. picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') data, _ = raw[picks] # This .mat was generated using the official biosig matlab package mat = sio.loadmat(gdf2_path + '_biosig.mat') data_biosig = mat['dat'] * 1e-6 # data are stored in microvolts data_biosig = data_biosig[picks] # Assert data are almost equal assert_array_almost_equal(data, data_biosig, 8) # Find events events = find_events(raw, verbose=1) events[:, 2] >>= 8 # last 8 bits are system events in biosemi files assert_equal(events.shape[0], 2) # 2 events in file assert_array_equal(events[:, 2], [20, 28]) with pytest.warns(RuntimeWarning, match='No events found'): # header contains no events raw = read_raw_edf(gdf2_path + '.gdf', stim_channel='auto') assert_equal(nchan, raw.info['nchan']) # stim channel not constructed assert_array_equal(ch_names[1:], raw.ch_names[1:]) # gh-5604 assert raw.info['meas_date'] == DATE_NONE _test_raw_reader(read_raw_edf, input_fname=gdf2_path + '.gdf', eog=None, misc=None, stim_channel='STATUS')
def test_edf_overlapping_annotations(): """Test EDF with overlapping annotations.""" n_warning = 2 with warnings.catch_warnings(record=True) as w: read_raw_edf(edf_overlap_annot_path, preload=True, verbose=True) assert_equal(sum('overlapping' in str(ww.message) for ww in w), n_warning)
def test_edf_data(): """Test reading raw edf files""" raw_py = read_raw_edf(edf_path, misc=range(-4, 0), stim_channel=139, preload=True) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude=['EDF Annotations']) data_py, _ = raw_py[picks] print(raw_py) # to test repr print(raw_py.info) # to test Info repr # this .mat was generated using the EEG Lab Biosemi Reader raw_eeglab = io.loadmat(edf_eeglab_path) raw_eeglab = raw_eeglab['data'] * 1e-6 # data are stored in microvolts data_eeglab = raw_eeglab[picks] assert_array_almost_equal(data_py, data_eeglab, 10) # Make sure concatenation works raw_concat = concatenate_raws([raw_py.copy(), raw_py]) assert_equal(raw_concat.n_times, 2 * raw_py.n_times) # Test uneven sampling raw_py = read_raw_edf(edf_uneven_path, stim_channel=None) data_py, _ = raw_py[0] # this .mat was generated using the EEG Lab Biosemi Reader raw_eeglab = io.loadmat(edf_uneven_eeglab_path) raw_eeglab = raw_eeglab['data'] data_eeglab = raw_eeglab[0] # match upsampling upsample = len(data_eeglab) / len(raw_py) data_py = np.repeat(data_py, repeats=upsample) assert_array_equal(data_py, data_eeglab)
def test_gdf2_data(): """Test reading raw GDF 2.x files.""" raw = read_raw_edf(gdf2_path + '.gdf', eog=None, misc=None, preload=True, stim_channel='STATUS') nchan = raw.info['nchan'] ch_names = raw.ch_names # Renamed STATUS -> STI 014. picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') data, _ = raw[picks] # This .mat was generated using the official biosig matlab package mat = sio.loadmat(gdf2_path + '_biosig.mat') data_biosig = mat['dat'] * 1e-6 # data are stored in microvolts data_biosig = data_biosig[picks] # Assert data are almost equal assert_array_almost_equal(data, data_biosig, 8) # Find events events = find_events(raw, verbose=1) events[:, 2] >>= 8 # last 8 bits are system events in biosemi files assert_equal(events.shape[0], 2) # 2 events in file assert_array_equal(events[:, 2], [20, 28]) with warnings.catch_warnings(record=True) as w: # header contains no events raw = read_raw_edf(gdf2_path + '.gdf', stim_channel='auto') assert_equal(len(w), 1) assert_true(str(w[0].message).startswith('No events found.')) assert_equal(nchan, raw.info['nchan']) # stim channel not constructed assert_array_equal(ch_names[1:], raw.ch_names[1:])
def test_read_raw_edf_stim_channel_input_parameters(): """Test edf raw reader deprecation.""" _MSG = "`read_raw_edf` is not supposed to trigger a deprecation warning" with pytest.warns(None) as recwarn: read_raw_edf(edf_path) assert all([w.category != DeprecationWarning for w in recwarn.list]), _MSG for invalid_stim_parameter in ['EDF Annotations', 'BDF Annotations']: with pytest.raises(ValueError, match="stim channel is not supported"): read_raw_edf(edf_path, stim_channel=invalid_stim_parameter)
def test_stim_channel(): """Test reading raw edf files with stim channel.""" raw_py = read_raw_edf(edf_path, misc=range(-4, 0), stim_channel=139, preload=True) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude=['EDF Annotations']) data_py, _ = raw_py[picks] print(raw_py) # to test repr print(raw_py.info) # to test Info repr # this .mat was generated using the EEG Lab Biosemi Reader raw_eeglab = io.loadmat(edf_eeglab_path) raw_eeglab = raw_eeglab['data'] * 1e-6 # data are stored in microvolts data_eeglab = raw_eeglab[picks] assert_array_almost_equal(data_py, data_eeglab, 10) events = find_edf_events(raw_py) assert_true(len(events) - 1 == len(find_events(raw_py))) # start not found # Test uneven sampling raw_py = read_raw_edf(edf_uneven_path, stim_channel=None) data_py, _ = raw_py[0] # this .mat was generated using the EEG Lab Biosemi Reader raw_eeglab = io.loadmat(edf_uneven_eeglab_path) raw_eeglab = raw_eeglab['data'] data_eeglab = raw_eeglab[0] # match upsampling upsample = len(data_eeglab) / len(raw_py) data_py = np.repeat(data_py, repeats=upsample) assert_array_equal(data_py, data_eeglab) assert_raises(RuntimeError, read_raw_edf, edf_path, preload=False, stim_channel=-1) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') raw = read_raw_edf(edf_stim_resamp_path, verbose=True, stim_channel=-1) assert_equal(len(w), 2) assert_true(any('Events may jitter' in str(ww.message) for ww in w)) assert_true(any('truncated' in str(ww.message) for ww in w)) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') raw[:] assert_equal(len(w), 0) events = raw_py.find_edf_events() assert_true(len(events) == 0)
def test_read_raw_edf_deprecation_of_annot_annotmap(tmpdir): """Test deprecation of annot and annotmap.""" annot = (b'+0.1344\x150.2560\x14two\x14\x00\x00\x00\x00' b'+0.3904\x151.0\x14two\x14\x00\x00\x00\x00' b'+2.0\x14three\x14\x00\x00\x00\x00\x00\x00\x00\x00' b'+2.5\x152.5\x14two\x14\x00\x00\x00\x00') annot_file = tmpdir.join('annotations.txt') annot_file.write(annot) annotmap_file = tmpdir.join('annotations_map.txt') annotmap_file.write('two:2,three:3') with pytest.warns(DeprecationWarning, match="annot.*annotmap.*"): read_raw_edf(input_fname=edf_path, annot=str(annot_file), annotmap=str(annotmap_file), preload=True)
def test_edf_data(): """Test edf files.""" raw = _test_raw_reader(read_raw_edf, input_fname=edf_path, stim_channel=None, exclude=['Ergo-Left', 'H10'], verbose='error') raw_py = read_raw_edf(edf_path, stim_channel='auto', preload=True) assert_equal(len(raw.ch_names) + 2, len(raw_py.ch_names)) # Test saving and loading when annotations were parsed. edf_events = find_events(raw_py, output='step', shortest_event=0, stim_channel='STI 014') # onset, duration, id events = [[0.1344, 0.2560, 2], [0.3904, 1.0000, 2], [2.0000, 0.0000, 3], [2.5000, 2.5000, 2]] events = np.array(events) events[:, :2] *= 512 # convert time to samples events = np.array(events, dtype=int) events[:, 1] -= 1 events[events[:, 1] <= 0, 1] = 1 events[:, 1] += events[:, 0] onsets = events[:, [0, 2]] offsets = events[:, [1, 2]] events = np.zeros((2 * events.shape[0], 3), dtype=int) events[0::2, [0, 2]] = onsets events[1::2, [0, 1]] = offsets assert_array_equal(edf_events, events) # Test with number of records not in header (-1). tempdir = _TempDir() broken_fname = op.join(tempdir, 'broken.edf') with open(edf_path, 'rb') as fid_in: fid_in.seek(0, 2) n_bytes = fid_in.tell() fid_in.seek(0, 0) rbytes = fid_in.read(int(n_bytes * 0.4)) with open(broken_fname, 'wb') as fid_out: fid_out.write(rbytes[:236]) fid_out.write(b'-1 ') fid_out.write(rbytes[244:]) with pytest.warns(RuntimeWarning, match='records .* not match the file size'): raw = read_raw_edf(broken_fname, preload=True, stim_channel='auto') read_raw_edf(broken_fname, exclude=raw.ch_names[:132], preload=True, stim_channel='auto')
def test_gdf_data(): """Test reading raw GDF 1.x files.""" with pytest.warns(RuntimeWarning, match='Overlapping events'): raw = read_raw_edf(gdf1_path + '.gdf', eog=None, misc=None, preload=True, stim_channel='auto') picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') data, _ = raw[picks] # this .npy was generated using the official biosig python package raw_biosig = np.load(gdf1_path + '_biosig.npy') raw_biosig = raw_biosig * 1e-6 # data are stored in microvolts data_biosig = raw_biosig[picks] # Assert data are almost equal assert_array_almost_equal(data, data_biosig, 8) # Test for stim channel events = find_events(raw, shortest_event=1) # The events are overlapping. assert_array_equal(events[:, 0], raw._raw_extras[0]['events'][1][::2]) # Test events are encoded to stim channel. events = find_events(raw) evs = raw.find_edf_events() assert (all([event in evs[1] for event in events[:, 0]])) # gh-5604 assert raw.info['meas_date'] == DATE_NONE with pytest.warns(RuntimeWarning, match='Overlapping events'): _test_raw_reader(read_raw_edf, input_fname=gdf1_path + '.gdf', eog=None, misc=None, stim_channel='auto')
def test_bdf_data(): """Test reading raw bdf files """ raw_py = read_raw_edf(bdf_path, montage=montage_path, eog=eog, misc=misc, preload=True) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude='bads') data_py, _ = raw_py[picks] print(raw_py) # to test repr print(raw_py.info) # to test Info repr # this .mat was generated using the EEG Lab Biosemi Reader raw_eeglab = io.loadmat(bdf_eeglab_path) raw_eeglab = raw_eeglab['data'] * 1e-6 # data are stored in microvolts data_eeglab = raw_eeglab[picks] assert_array_almost_equal(data_py, data_eeglab) # Manually checking that float coordinates are imported assert_true((raw_py.info['chs'][0]['eeg_loc']).any()) assert_true((raw_py.info['chs'][25]['eeg_loc']).any()) assert_true((raw_py.info['chs'][63]['eeg_loc']).any()) # Make sure concatenation works raw_concat = concatenate_raws([raw_py.copy(), raw_py]) assert_equal(raw_concat.n_times, 2 * raw_py.n_times)
def test_edf_data(): """Test edf files""" _test_raw_reader(read_raw_edf, input_fname=edf_path, stim_channel=None) raw_py = read_raw_edf(edf_path, preload=True) # Test saving and loading when annotations were parsed. tempdir = _TempDir() raw_file = op.join(tempdir, 'test-raw.fif') raw_py.save(raw_file, overwrite=True, buffer_size_sec=1) Raw(raw_file, preload=True) edf_events = find_events(raw_py, output='step', shortest_event=0, stim_channel='STI 014') # onset, duration, id events = [[0.1344, 0.2560, 2], [0.3904, 1.0000, 2], [2.0000, 0.0000, 3], [2.5000, 2.5000, 2]] events = np.array(events) events[:, :2] *= 512 # convert time to samples events = np.array(events, dtype=int) events[:, 1] -= 1 events[events[:, 1] <= 0, 1] = 1 events[:, 1] += events[:, 0] onsets = events[:, [0, 2]] offsets = events[:, [1, 2]] events = np.zeros((2 * events.shape[0], 3), dtype=int) events[0::2, [0, 2]] = onsets events[1::2, [0, 1]] = offsets assert_array_equal(edf_events, events)
def test_gdf_data(): """Test reading raw GDF 1.x files.""" with warnings.catch_warnings(record=True): # interpolate / overlap events raw = read_raw_edf(gdf1_path + '.gdf', eog=None, misc=None, preload=True, stim_channel='auto') picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') data, _ = raw[picks] # this .npy was generated using the official biosig python package raw_biosig = np.load(gdf1_path + '_biosig.npy') raw_biosig = raw_biosig * 1e-6 # data are stored in microvolts data_biosig = raw_biosig[picks] # Assert data are almost equal assert_array_almost_equal(data, data_biosig, 8) # Test for stim channel events = find_events(raw, shortest_event=1) # The events are overlapping. assert_array_equal(events[:, 0], raw._raw_extras[0]['events'][1][::2]) # Test events are encoded to stim channel. events = find_events(raw) evs = raw.find_edf_events() assert_true(all([event in evs[1] for event in events[:, 0]]))
def test_gdf_data(): """Test reading raw GDF 1.x files.""" raw = read_raw_edf(gdf1_path + '.gdf', eog=None, misc=None, preload=True) picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') data, _ = raw[picks] # Test Status is added as event EXPECTED_EVS_ONSETS = raw._raw_extras[0]['events'][1] evs, evs_id = events_from_annotations(raw) assert_array_equal(evs[:, 0], EXPECTED_EVS_ONSETS) assert evs_id == {'Unknown': 1} # this .npy was generated using the official biosig python package raw_biosig = np.load(gdf1_path + '_biosig.npy') raw_biosig = raw_biosig * 1e-6 # data are stored in microvolts data_biosig = raw_biosig[picks] # Assert data are almost equal assert_array_almost_equal(data, data_biosig, 8) # Test for events assert len(raw.annotations.duration == 963) # gh-5604 assert raw.info['meas_date'] == DATE_NONE
def test_gdf2_data(): """Test reading raw GDF 2.x files.""" raw = read_raw_edf(gdf2_path + '.gdf', eog=None, misc=None, preload=True) picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') data, _ = raw[picks] # This .mat was generated using the official biosig matlab package mat = sio.loadmat(gdf2_path + '_biosig.mat') data_biosig = mat['dat'] * 1e-6 # data are stored in microvolts data_biosig = data_biosig[picks] # Assert data are almost equal assert_array_almost_equal(data, data_biosig, 8) # Find events events = find_events(raw, verbose=1) events[:, 2] >>= 8 # last 8 bits are system events in biosemi files assert_equal(events.shape[0], 2) # 2 events in file assert_array_equal(events[:, 2], [20, 28]) # gh-5604 assert raw.info['meas_date'] == DATE_NONE _test_raw_reader(read_raw_edf, input_fname=gdf2_path + '.gdf', eog=None, misc=None)
def test_duplicate_channel_labels_edf(): """Test reading edf file with duplicate channel names.""" EXPECTED_CHANNEL_NAMES = ['EEG F1-Ref-0', 'EEG F2-Ref', 'EEG F1-Ref-1'] with pytest.warns(RuntimeWarning, match='Channel names are not unique'): raw = read_raw_edf(duplicate_channel_labels_path, preload=False) assert raw.ch_names == EXPECTED_CHANNEL_NAMES
def test_edf_data(): """Test edf files.""" raw = _test_raw_reader(read_raw_edf, input_fname=edf_path, stim_channel=None, exclude=['Ergo-Left', 'H10']) raw_py = read_raw_edf(edf_path, preload=True) assert_equal(len(raw.ch_names) + 2, len(raw_py.ch_names)) # Test saving and loading when annotations were parsed. edf_events = find_events(raw_py, output='step', shortest_event=0, stim_channel='STI 014') # onset, duration, id events = [[0.1344, 0.2560, 2], [0.3904, 1.0000, 2], [2.0000, 0.0000, 3], [2.5000, 2.5000, 2]] events = np.array(events) events[:, :2] *= 512 # convert time to samples events = np.array(events, dtype=int) events[:, 1] -= 1 events[events[:, 1] <= 0, 1] = 1 events[:, 1] += events[:, 0] onsets = events[:, [0, 2]] offsets = events[:, [1, 2]] events = np.zeros((2 * events.shape[0], 3), dtype=int) events[0::2, [0, 2]] = onsets events[1::2, [0, 1]] = offsets assert_array_equal(edf_events, events)
def test_edf_annotations(): """Test if events are detected correctly in a typical MNE workflow.""" # test an actual file raw = read_raw_edf(edf_path, preload=True) edf_events = find_events(raw, output='step', shortest_event=0, stim_channel='STI 014') # onset, duration, id events = [[0.1344, 0.2560, 2], [0.3904, 1.0000, 2], [2.0000, 0.0000, 3], [2.5000, 2.5000, 2]] events = np.array(events) events[:, :2] *= 512 # convert time to samples events = np.array(events, dtype=int) events[:, 1] -= 1 events[events[:, 1] <= 0, 1] = 1 events[:, 1] += events[:, 0] onsets = events[:, [0, 2]] offsets = events[:, [1, 2]] events = np.zeros((2 * events.shape[0], 3), dtype=int) events[0::2, [0, 2]] = onsets events[1::2, [0, 1]] = offsets assert_array_equal(edf_events, events)
def test_append(): """Test appending raw edf objects using Raw.append""" for preload in (True, False): raw = read_raw_edf(bdf_path, preload=False) raw0 = raw.copy() raw1 = raw.copy() raw0.append(raw1) assert_true(2 * len(raw) == len(raw0)) assert_allclose(np.tile(raw[:, :][0], (1, 2)), raw0[:, :][0]) # different types can't combine raw = read_raw_edf(bdf_path, preload=True) raw0 = raw.copy() raw1 = raw.copy() raw2 = RawArray(raw[:, :][0], raw.info) assert_raises(ValueError, raw.append, raw2)
def test_edf_stim_ch_pick_up(test_input, EXPECTED): """Test stim_channel.""" TYPE_LUT = {v[0]: k for k, v in _KIND_DICT.items()} fname = op.join(data_dir, 'test_stim_channel.edf') raw = read_raw_edf(fname, stim_channel=test_input) ch_types = {ch['ch_name']: TYPE_LUT[ch['kind']] for ch in raw.info['chs']} assert ch_types == EXPECTED
def test_orig_units(): """Test exposure of original channel units.""" raw = read_raw_edf(edf_path, preload=True) # Test original units orig_units = raw._orig_units assert len(orig_units) == 140 assert orig_units['A1'] == u'µV' # formerly 'uV' edit by _check_orig_units
def test_find_edf_events_deprecation(): """Test find_edf_events deprecation.""" raw = read_raw_edf(edf_path) with pytest.deprecated_call(match="find_edf_events"): raw.find_edf_events() with pytest.deprecated_call(match="find_edf_events"): find_edf_events(raw)
def test_stim_channel(): """Test reading raw edf files with stim channel.""" raw_py = read_raw_edf(edf_path, misc=range(-4, 0), stim_channel=139, preload=True) picks = pick_types(raw_py.info, meg=False, eeg=True, exclude=['EDF Annotations']) data_py, _ = raw_py[picks] print(raw_py) # to test repr print(raw_py.info) # to test Info repr # this .mat was generated using the EEG Lab Biosemi Reader raw_eeglab = io.loadmat(edf_eeglab_path) raw_eeglab = raw_eeglab['data'] * 1e-6 # data are stored in microvolts data_eeglab = raw_eeglab[picks] assert_array_almost_equal(data_py, data_eeglab, 10) events = find_edf_events(raw_py) assert (len(events) - 1 == len(find_events(raw_py))) # start not found # Test uneven sampling raw_py = read_raw_edf(edf_uneven_path, stim_channel=None) data_py, _ = raw_py[0] # this .mat was generated using the EEG Lab Biosemi Reader raw_eeglab = io.loadmat(edf_uneven_eeglab_path) raw_eeglab = raw_eeglab['data'] data_eeglab = raw_eeglab[0] # match upsampling upsample = len(data_eeglab) / len(raw_py) data_py = np.repeat(data_py, repeats=upsample) assert_array_equal(data_py, data_eeglab) pytest.raises(RuntimeError, read_raw_edf, edf_path, preload=False, stim_channel=-1) with pytest.warns(RuntimeWarning, match='Interpolating stim .* Events may jitter'): raw = read_raw_edf(edf_stim_resamp_path, verbose=True, stim_channel=-1) with pytest.warns(None) as w: raw[:] assert len(w) == 0 events = raw_py.find_edf_events() assert (len(events) == 0)
def test_append(): """Test appending raw edf objects using Raw.append """ # Author: Alan Leggitt <*****@*****.**> raw = read_raw_edf(bdf_path, preload=True) raw0 = raw.copy() raw1 = raw.copy() raw0.append(raw1) assert_true(2 * len(raw) == len(raw0)) # XXX This is currently failing b/c of non-preload! # assert_allclose(np.tile(raw[:, :][0], (1, 2)), raw0[:, :][0]) raw = read_raw_edf(bdf_path, preload=False) raw0 = raw.copy() raw1 = raw.copy() assert_raises(RuntimeError, raw0.append, raw1) raw2 = RawArray(raw[:, :][0], raw.info) assert_raises(ValueError, raw.append, raw2)
def test_append(): """Test appending raw edf objects using Raw.append """ # Author: Alan Leggitt <*****@*****.**> raw = read_raw_edf(bdf_path, preload=False) raw0 = raw.copy() raw1 = raw.copy() raw0.append(raw1) assert_true(2 * len(raw) == len(raw0))
def load_subject(id_num, runs): ''' Loads raw EEG recordings for one subject and at least one run of experiments. Arguments: id_num: int, the subject's ID number runs: int or list of ints -- which experiment(s) to read data from Returns: MNE Raw object ''' edf_files = load_data(id_num, runs) if len(edf_files) > 1: raw_objects = [read_raw_edf(file, preload=True) for file in edf_files] mne_raw = concatenate_raws(raw_objects, preload=True) else: mne_raw = read_raw_edf(edf_files[0], preload=True) return mne_raw
def test_edf_stim_channel(): """Test stim channel for edf file.""" # test if stim channel is automatically detected raw = read_raw_edf(edf_path, preload=True) assert_true(channel_type(raw.info, raw.info["nchan"] - 1) == 'stim') raw = read_raw_edf(edf_stim_channel_path, preload=True, stim_channel=-1) true_data = np.loadtxt(edf_txt_stim_channel_path).T # EDF writer pad data if file to small _, ns = true_data.shape edf_data = raw._data[:, :ns] # assert stim channels are equal assert_array_equal(true_data[-1], edf_data[-1]) # assert data are equal assert_array_almost_equal(true_data[0:-1] * 1e-6, edf_data[0:-1])
def test_to_data_frame(): """Test edf Raw Pandas exporter""" for path in [edf_path, bdf_path]: raw = read_raw_edf(path, stim_channel=None, preload=True) _, times = raw[0, :10] df = raw.to_data_frame() assert_true((df.columns == raw.ch_names).all()) assert_array_equal(np.round(times * 1e3), df.index.values[:10]) df = raw.to_data_frame(index=None, scalings={'eeg': 1e13}) assert_true('time' in df.index.names) assert_array_equal(df.values[:, 0], raw._data[0] * 1e13)
def test_convert_defaults(self): """Test default behavior""" path = convert_to_edf(self.temp_dir) self.assertTrue(os.path.exists(path)) with warnings.catch_warnings(): warnings.simplefilter('ignore') edf = read_raw_edf(path, preload=True) self.assertTrue(len(edf.get_data()) > 0) for ch_name in ['c1', 'c2', 'c3']: self.assertTrue(ch_name in edf.ch_names)
def test_subject_info(tmpdir): """Test exposure of original channel units.""" raw = read_raw_edf(edf_path) assert raw.info['subject_info'] is None # XXX this is arguably a bug edf_info = raw._raw_extras[0] assert edf_info['subject_info'] is not None want = {'id': 'X', 'sex': 'X', 'birthday': 'X', 'name': 'X'} for key, val in want.items(): assert edf_info['subject_info'][key] == val, key fname = tmpdir.join('test_raw.fif') raw.save(fname) raw = read_raw_fif(fname) assert raw.info['subject_info'] is None # XXX should eventually round-trip
def test_edf_stim_ch_pick_up(test_input, EXPECTED): """Test stim_channel.""" # This is fragile for EEG/EEG-CSD, so just omit csd KIND_DICT = get_channel_type_constants() TYPE_LUT = { v['kind']: k for k, v in KIND_DICT.items() if k not in ('csd', 'chpi') } # chpi not needed, and unhashable (a list) fname = op.join(data_dir, 'test_stim_channel.edf') raw = read_raw_edf(fname, stim_channel=test_input) ch_types = {ch['ch_name']: TYPE_LUT[ch['kind']] for ch in raw.info['chs']} assert ch_types == EXPECTED
def test_double_export_edf(tmp_path): """Test exporting an EDF file multiple times.""" rng = np.random.RandomState(123456) format = 'edf' ch_types = ['eeg', 'eeg', 'stim', 'ecog', 'ecog', 'seeg', 'eog', 'ecg', 'emg', 'dbs', 'bio'] info = create_info(len(ch_types), sfreq=1000, ch_types=ch_types) data = rng.random(size=(len(ch_types), 1000)) * 1e-5 # include subject info and measurement date info['subject_info'] = dict(first_name='mne', last_name='python', birthday=(1992, 1, 20), sex=1, hand=3) raw = RawArray(data, info) # export once temp_fname = tmp_path / f'test.{format}' raw.export(temp_fname, add_ch_type=True) raw_read = read_raw_edf(temp_fname, infer_types=True, preload=True) # export again raw_read.load_data() raw_read.export(temp_fname, add_ch_type=True, overwrite=True) raw_read = read_raw_edf(temp_fname, infer_types=True, preload=True) # stim channel should be dropped raw.drop_channels('2') assert raw.ch_names == raw_read.ch_names # only compare the original length, since extra zeros are appended orig_raw_len = len(raw) assert_array_almost_equal( raw.get_data(), raw_read.get_data()[:, :orig_raw_len], decimal=4) assert_allclose( raw.times, raw_read.times[:orig_raw_len], rtol=0, atol=1e-5) # check channel types except for 'bio', which loses its type orig_ch_types = raw.get_channel_types() read_ch_types = raw_read.get_channel_types() assert_array_equal(orig_ch_types, read_ch_types)
def get_raw(subject, runs=[6, 10, 14]): raw_fnames = eegbci.load_data(subject, runs) raw_files = [ read_raw_edf(f, preload=True, stim_channel='auto') for f in raw_fnames ] raw = concatenate_raws(raw_files) raw.rename_channels(lambda x: x.strip('.')) raw.pick_channels(ch_names) raw.filter(low_freq, high_freq, fir_design='firwin', skip_by_annotation='edge') return raw
def __init__(self, basedir, subj): self.basedir = basedir pardir = os.path.abspath(os.path.join(self.basedir, os.pardir)) self.traindir = self.basedir + '//' + subj + '//' self.testdir = self.basedir + '//' + subj + '//' self.icaDir = self.basedir + '//' + subj + '//TPython_100.mat' self.labFile = pardir + '//labels.txt' trainfiles = [self.traindir + 'Session1.edf'] testfiles = [self.traindir + 'Session2.edf'] self.trainfiles = [read_raw_edf(f, preload=True, stim_channel=None) for f in trainfiles] self.testfiles = [read_raw_edf(f, preload=True, stim_channel=None) for f in testfiles] for raw in self.trainfiles: try: raw.drop_channels(['Diff 2', 'Diff 3', 'Diff 4', 'EDF Annotations']) except: raw.drop_channels(['EDF Annotations']) for raw in self.testfiles: try: raw.drop_channels(['Diff 2', 'Diff 3', 'Diff 4', 'EDF Annotations']) except: raw.drop_channels(['EDF Annotations'])
def read_EDF(psg, hyp): data = read_raw_edf( 'D:/Project/Dataset/Sleep EDF Database Expanded/sleep-cassette/' + psg + '.edf') raw_data = data.get_data() data_hyp = read_annotations( 'D:/Project/Dataset/Sleep EDF Database Expanded/sleep-cassette/' + hyp + '.edf') l1 = raw_data[0] l2 = raw_data[1] l3 = raw_data[2] l4 = raw_data[4] l5 = hyp_reader(data_hyp) return l1, l2, l3, l4, l5
def __init__(self,path): self.raw = None self.baseline = None self.bandpass = None self.corrected = None # Raw data & meta data self.path_edf, self.path_stage = path raw = read_raw_edf(self.path_edf, preload=True, verbose=0) raw.pick_channels(['EEG Fp1-A2','EEG F7-A2','EEG F3-A2','EEG T5-A2', \ 'EEG O1-A2','EEG Fp2-A1','EEG F4-A1','EEG F8-A1','EEG T6-A1','EEG O2-A1']) # Rename channel name to standard1005 raw.rename_channels({'EEG Fp1-A2': 'Fp1','EEG F7-A2': 'F7', 'EEG F3-A2': 'F3', 'EEG T5-A2': 'T5','EEG O1-A2': 'O1', 'EEG Fp2-A1': 'Fp2', 'EEG F4-A1': 'F4', 'EEG F8-A1': 'F8', 'EEG T6-A1': 'T6', 'EEG O2-A1': 'O2'}) raw.set_montage('standard_1005', raise_if_subset=False) self.raw = raw.copy() self.meas_date, _ = self.raw.info['meas_date'] self.ch_names = self.raw.info['ch_names'] # Stage file: with open(self.path_stage, 'r') as f: stages = f.read().splitlines() fname, lname, subject, start_date, start_time, \ end_date, end_time = stages[0].split(',') stages = stages[1:] # Stages and stages indices self.stages = stages self.subject = subject task_indices = [idx for idx, _ in enumerate(self.stages) if _ == '13'] self.tasks = group_consecutives(task_indices) rest_indices = [idx for idx, _ in enumerate(self.stages) if _ == '12'] self.rests = group_consecutives(rest_indices) self.start_timestamp = time.mktime( datetime.strptime(f"{start_date} {start_time}", \ "%m/%d/%Y %I:%M:%S %p").timetuple()) self.DIFFTIME = self.start_timestamp - self.meas_date
def test_gdf_data(): """Test reading raw GDF 1.x files.""" raw = read_raw_edf(gdf1_path + '.gdf', eog=None, misc=None, preload=True, stim_channel=None) picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads') data, _ = raw[picks] # this .npy was generated using the official biosig python package raw_biosig = np.load(gdf1_path + '_biosig.npy') raw_biosig = raw_biosig * 1e-6 # data are stored in microvolts data_biosig = raw_biosig[picks] # Assert data are almost equal assert_array_almost_equal(data, data_biosig, 8)
def test_to_data_frame(fname): """Test EDF/BDF Raw Pandas exporter.""" ext = op.splitext(fname)[1].lstrip('.').lower() if ext == 'edf': raw = read_raw_edf(fname, preload=True, verbose='error') elif ext == 'bdf': raw = read_raw_bdf(fname, preload=True, verbose='error') _, times = raw[0, :10] df = raw.to_data_frame(index='time') assert (df.columns == raw.ch_names).all() assert_array_equal(np.round(times * 1e3), df.index.values[:10]) df = raw.to_data_frame(index=None, scalings={'eeg': 1e13}) assert 'time' in df.columns assert_array_equal(df.values[:, 1], raw._data[0] * 1e13)
def test_to_data_frame(): """Test edf Raw Pandas exporter.""" for path in [edf_path, bdf_path]: raw = read_raw_edf(path, stim_channel=None, preload=True, verbose='error') _, times = raw[0, :10] df = raw.to_data_frame() assert (df.columns == raw.ch_names).all() assert_array_equal(np.round(times * 1e3), df.index.values[:10]) df = raw.to_data_frame(index=None, scalings={'eeg': 1e13}) assert 'time' in df.index.names assert_array_equal(df.values[:, 0], raw._data[0] * 1e13)
def test_edf_stim_channel(): """Test stim channel for edf file.""" raw = read_raw_edf(edf_stim_channel_path, preload=True, stim_channel=-1) true_data = np.loadtxt(edf_txt_stim_channel_path).T # EDF writer pad data if file to small _, ns = true_data.shape edf_data = raw._data[:, :ns] # assert stim channels are equal assert_array_equal(true_data[-1], edf_data[-1]) # assert data are equal assert_array_almost_equal(true_data[0:-1] * 1e-6, edf_data[0:-1])
def test_find_events_backward_compatibility(): """Test if events are detected correctly in a typical MNE workflow.""" EXPECTED_EVENTS = [[68, 0, 2], [199, 0, 2], [1024, 0, 3], [1280, 0, 2]] # test an actual file raw = read_raw_edf(edf_path, preload=True) event_id = _get_edf_default_event_id(raw.annotations.description) event_id.pop('start') events_from_EFA, _ = events_from_annotations(raw, event_id=event_id, use_rounding=False) assert_array_equal(events_from_EFA, EXPECTED_EVENTS)
def test_edf_set_montage_none(): """Test that using montage=None in init and set_montage differs.""" raw = read_raw_edf(edf_path, montage=None) original_chs = deepcopy(raw.info['chs']) assert raw.info['dig'] is None raw.set_montage(None) assert object_diff(raw.info['chs'], original_chs) # They differ # read_raw_edf initializes 0s and set_montage NaNs all_loc = np.array([ch['loc'] for ch in original_chs]) assert_array_equal(all_loc, np.zeros_like(all_loc)) assert_array_equal(np.array([ch['loc'] for ch in raw.info['chs']]), np.full_like(all_loc, np.NaN))
def get_classification_dataset(subject=1, typeInt=4): #TypeInt: #Task 1 (open and close left or right fist) #Task 2 (imagine opening and closing left or right fist) #Task 3 (open and close both fists or both feet) #Task 4 (imagine opening and closing both fists or both feet) assert (typeInt >= 1) assert (typeInt <= 4) from mne.io import concatenate_raws, read_raw_edf from mne.datasets import eegbci tmin, tmax = -1., 4. runs = [3, 7, 11] runs = [r + typeInt - 1 for r in runs] print("loading subject {} with runs {}".format(subject, runs)) if typeInt <= 1: event_id = dict(left=2, right=3) else: event_id = dict(hands=2, feet=3) raw_fnames = eegbci.load_data(subject, runs) raws = [read_raw_edf(f, preload=True) for f in raw_fnames] raw = concatenate_raws(raws) raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge') eegbci.standardize(raw) # set channel names montage = mne.channels.make_standard_montage('standard_1005') raw.set_montage(montage) raw.rename_channels(lambda x: x.strip('.')) events, _ = mne.events_from_annotations(raw, event_id=dict(T1=2, T2=3)) picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') # Read epochs (train will be done only between 1 and 2s) # Testing will be done with a running classifier epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) return (epochs)
def load_raw_edf(path, channels): ''' Input: Path to edf data, array of montage info (what EEG channels were used) Output: Edf data as numpy arrays ''' logging.debug('Loading edf data from path: ' + str(path)) if not os.path.exists(path): logging.debug('ERROR: Given path <' + path + '> does not exist.') return None raw_edf = read_raw_edf(path, verbose=False, preload=True) raw_edf.pick_channels(channels) tmp = raw_edf.to_data_frame() edf_numpy = tmp.to_numpy() return edf_numpy
def test_edf_data(): """Test edf files.""" raw = _test_raw_reader(read_raw_edf, input_fname=edf_path, exclude=['Ergo-Left', 'H10'], verbose='error') raw_py = read_raw_edf(edf_path, preload=True) assert_equal(len(raw.ch_names) + 2, len(raw_py.ch_names)) # Test with number of records not in header (-1). tempdir = _TempDir() broken_fname = op.join(tempdir, 'broken.edf') with open(edf_path, 'rb') as fid_in: fid_in.seek(0, 2) n_bytes = fid_in.tell() fid_in.seek(0, 0) rbytes = fid_in.read(int(n_bytes * 0.4)) with open(broken_fname, 'wb') as fid_out: fid_out.write(rbytes[:236]) fid_out.write(b'-1 ') fid_out.write(rbytes[244:]) with pytest.warns(RuntimeWarning, match='records .* not match the file size'): raw = read_raw_edf(broken_fname, preload=True) read_raw_edf(broken_fname, exclude=raw.ch_names[:132], preload=True)
def load_nme_data(self): raw_fnames = eegbci.load_data(1, self.RUNS) raw_files = [] raw_files.extend([ read_raw_edf(f, preload=True, stim_channel='auto') for f in raw_fnames ]) raw = concatenate_raws(raw_files) raw.rename_channels(lambda x: x.strip('.')) self.raw = raw self.data = raw.get_data() self.times = self.raw[-1, :][-1] * 1000
def plot_edf(edf_path: str, auto_scale: bool = False): """Plot data from the raw edf file. Note: this works from an iPython session but seems to throw errors when provided in a script. Parameters ---------- edf_path - full path to the generated edf file auto_scale - optional; if True will scale the EEG data; this is useful for fake (random) data but makes real data hard to read. """ edf = read_raw_edf(edf_path, preload=True) if auto_scale: edf.plot(scalings='auto') else: edf.plot()
def test_ch_types(): """Test reading of channel types from EDF channel label.""" raw = read_raw_edf(edf_chtypes_path) # infer_types=False labels = ['EEG Fp1-Ref', 'EEG Fp2-Ref', 'EEG F3-Ref', 'EEG F4-Ref', 'EEG C3-Ref', 'EEG C4-Ref', 'EEG P3-Ref', 'EEG P4-Ref', 'EEG O1-Ref', 'EEG O2-Ref', 'EEG F7-Ref', 'EEG F8-Ref', 'EEG T7-Ref', 'EEG T8-Ref', 'EEG P7-Ref', 'EEG P8-Ref', 'EEG Fz-Ref', 'EEG Cz-Ref', 'EEG Pz-Ref', 'POL E', 'POL PG1', 'POL PG2', 'EEG A1-Ref', 'EEG A2-Ref', 'POL T1', 'POL T2', 'ECG ECG1', 'ECG ECG2', 'EEG F9-Ref', 'EEG T9-Ref', 'EEG P9-Ref', 'EEG F10-Ref', 'EEG T10-Ref', 'EEG P10-Ref', 'SaO2 X9', 'SaO2 X10', 'POL DC01', 'POL DC02', 'POL DC03', 'POL DC04', 'POL $A1', 'POL $A2'] # by default all types are 'eeg' assert(all(t == 'eeg' for t in raw.get_channel_types())) assert raw.ch_names == labels raw = read_raw_edf(edf_chtypes_path, infer_types=True) labels = ['Fp1-Ref', 'Fp2-Ref', 'F3-Ref', 'F4-Ref', 'C3-Ref', 'C4-Ref', 'P3-Ref', 'P4-Ref', 'O1-Ref', 'O2-Ref', 'F7-Ref', 'F8-Ref', 'T7-Ref', 'T8-Ref', 'P7-Ref', 'P8-Ref', 'Fz-Ref', 'Cz-Ref', 'Pz-Ref', 'POL E', 'POL PG1', 'POL PG2', 'A1-Ref', 'A2-Ref', 'POL T1', 'POL T2', 'ECG1', 'ECG2', 'F9-Ref', 'T9-Ref', 'P9-Ref', 'F10-Ref', 'T10-Ref', 'P10-Ref', 'X9', 'X10', 'POL DC01', 'POL DC02', 'POL DC03', 'POL DC04', 'POL $A1', 'POL $A2'] types = ['eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'ecg', 'ecg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'bio', 'bio', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg'] assert raw.get_channel_types() == types assert raw.ch_names == labels
def test_write_annotations(): """Test writing raw files when annotations were parsed.""" tempdir = _TempDir() raw1 = read_raw_edf(edf_path, preload=True) raw1_file = op.join(tempdir, 'test1-raw.fif') raw1.save(raw1_file, overwrite=True, buffer_size_sec=1) raw11 = Raw(raw1_file, preload=True) data1, times1 = raw1[:, :] data11, times11 = raw11[:, :] assert_array_almost_equal(data1, data11) assert_array_almost_equal(times1, times11) assert_equal(sorted(raw1.info.keys()), sorted(raw11.info.keys())) assert_raises(RuntimeError, read_raw_edf, edf_path, preload=False)
def readRawEdf(edfDict=None, saveDir='', tWindow=120, tStep=30, read_raw_edf_param={ 'preload': True, "stim_channel": "auto" }): try: edfDict["rawData"] = read_raw_edf(saveDir + edfDict["path"][-1], **read_raw_edf_param) edfDict["fS"] = edfDict["rawData"].info["sfreq"] t_start = edfDict["rawData"].annotations.orig_time if t_start.timestamp() <= 0: edfDict["t0"] = datetime.fromtimestamp(0, tz=timezone.utc) t_last = edfDict["t0"].timestamp( ) + edfDict["rawData"]._last_time + 1 / edfDict["fS"] edfDict["tN"] = datetime.fromtimestamp(t_last, tz=timezone.utc) else: t_last = t_start.timestamp( ) + edfDict["rawData"]._last_time + 1 / edfDict["fS"] edfDict[ "t0"] = t_start # datetime.fromtimestamp(t_start.timestamp(), tz=timezone.utc) edfDict["tN"] = datetime.fromtimestamp(t_last, tz=timezone.utc) edfDict["tWindow"] = float( tWindow) # width of EEG sample window, given in (sec) edfDict["tStep"] = float( tStep) # step/overlap between EEG sample windows, given in (sec) header = mne.io.edf.edf._read_edf_header( fname=edfDict["rawData"].filenames[0], exclude=[]) id_tmp = header[0]["subject_info"]["id"] with open(saveDir + edfDict["path"][-1], encoding="latin-1") as fp: first_line = fp.readline() # regex_found = re.findall(r"%s \S+" % id_tmp, first_line) subject_gender = re.search(r"%s \w+" % id_tmp, first_line).group().split(" ")[1] subject_age = re.search(r"%s (Age:\d+)" % id_tmp, first_line).group().split(":")[1] edfDict["gender"] = subject_gender edfDict["age"] = int(subject_age) except: print("error break plese inspect:\n %s\n~~~~~~~~~~~~" % edfDict["rawData"].filenames[0]) return edfDict
def write_TF(Name_begin, patient_f, patient_idx): channel_df = pd.read_csv(os.path.join(PATH, patient_f)[:-9] + "_channels.tsv", sep="\t", encoding='unicode_escape') dat = io.read_raw_edf(os.path.join(PATH, patient_f)) ttl_idx = [idx for idx, ch in enumerate(dat.ch_names) if "POL DC10" in ch][0] mov_bin = preprocess_mov(dat.get_data()[ttl_idx, :]) idx_ecog = [] for ch in channel_df["name"]: PATH_save = 'C:\\Users\\ICN_admin\\Dropbox (Brain Modulation Lab)\\Shared Lab Folders\\CRCNS\\PD_ButtonPress\\derivatives\\'+\ "sub_"+subjects[patient_idx] +"_ch_"+ch[4:]+'.npy' if os.path.exists(PATH_save) is True: continue if ch.startswith(Name_begin) is False or \ channel_df.iloc[channel_df[channel_df.name == ch].index[0]]["status"] != "good": continue idx_ecog = [ idx for idx, ch_ in enumerate(dat.ch_names) if ch_.startswith(ch) ][0] info = create_info(ch_names=[ch[4:]], sfreq=2000, ch_types='ecog') epochs = calc_epochs(dat.get_data()[idx_ecog, :], mov_bin, info, threshold=0.5, epoch_lim=2500 * 2) freqs = np.arange(7, 200, 1) power = tfr_morlet(epochs, freqs=freqs, n_cycles=5, return_itc=False, zero_mean=True, picks=0) dat_ = power.data[ 0, :, 1000: 9000] # cut off borders due to Wavelet transform; 500ms till 4s post movement dat_z = stats.zscore(dat_, axis=1) #plt.imshow(dat_z, aspect='auto', extent=[-2,2,200,0])#, cmap='hot') #cbar = plt.colorbar() #cbar.set_label('Normalized spectral power [VAR]') #plt.clim(-1.5,1.5) #plt.gca().invert_yaxis() #plt.title(Name_begin[4:] + " " + subjects[patient_idx]) #plt.show() np.save(PATH_save, dat_z)
def test_load_generator(fname, recwarn): """Test IO of annotations from edf and bdf files with raw info.""" raw = read_raw_edf(fname) assert len(raw.annotations.onset) == 2 found_types = [k for k, v in channel_indices_by_type(raw.info, picks=None).items() if v] assert len(found_types) == 1 events, event_id = events_from_annotations(raw) ch_names = ['squarewave', 'ramp', 'pulse', 'ECG', 'noise', 'sine 1 Hz', 'sine 8 Hz', 'sine 8.5 Hz', 'sine 15 Hz', 'sine 17 Hz', 'sine 50 Hz'] assert raw.get_data().shape == (11, 120000) assert raw.ch_names == ch_names assert event_id == {'RECORD START': 1, 'REC STOP': 2} assert_array_equal(events, [[0, 0, 1], [120000, 0, 2]])
def read_data(data_path, eeg_signals, ecg_signals, res_signals): rawData = read_raw_edf(data_path) tmp = rawData.to_data_frame() if data_path[-10:-4] in F_H_patients: eeg_data = signal_transform(tmp[eeg_signals], 500) ecg_data = signal_transform(tmp[ecg_signals], 500) res_data = signal_transform(tmp[res_signals], 500) # downsample to 250 HZ (sample one point per two points) eeg_data = eeg_data.iloc[list(range(0, eeg_data.shape[0], 2))] ecg_data = ecg_data.iloc[list(range(0, ecg_data.shape[0], 2))] res_data = res_data.iloc[list(range(0, res_data.shape[0], 2))] else: eeg_data = signal_transform(tmp[eeg_signals], 250) ecg_data = signal_transform(tmp[ecg_signals], 250) res_data = signal_transform(tmp[res_signals], 250) return eeg_data, ecg_data, res_data
def process_single_point(psf_fname, ann_fname, args, select_ch): raw = read_raw_edf(psf_fname, preload=True, stim_channel=None) sampling_rate = raw.info['sfreq'] print(sampling_rate) raw_ch_df = raw.to_data_frame()[select_ch] raw_ch_df = raw_ch_df.to_frame() raw_ch_df.set_index(np.arange(len(raw_ch_df))) raw_ch_df['patientID'] = psf_fname.replace("-PSG.edf", "").split("/")[-1] raw_ch_df.index.name = "time" # Get raw header with open(psf_fname, 'rb') as f: reader_raw = dhedfreader.BaseEDFReader(f) reader_raw.read_header() h_raw = reader_raw.header raw_start_dt = datetime.strptime(h_raw['date_time'], "%Y-%m-%d %H:%M:%S") # Read annotation and its header with open(ann_fname, 'rb') as f: reader_ann = dhedfreader.BaseEDFReader(f) reader_ann.read_header() h_ann = reader_ann.header _, _, ann = zip(*reader_ann.records()) csvList = [] with open(psf_fname.replace("-PSG.edf", "L.csv"), "w") as fout: csvWriter = csv.writer(fout) csvWriter.writerow(["patientID", "startTime", "time", "stage"]) for a in ann[0]: onset_sec, duration_sec, ann_char = a current = onset_sec * sampling_rate while current < onset_sec * sampling_rate + duration_sec * sampling_rate: csvWriter.writerow([ psf_fname.replace("-PSG.edf", "").split("/")[-1], int(onset_sec), int(current), "".join(ann_char).split(" ")[-1] ]) current += 1 print("start") raw_ch_df.to_csv( psf_fname.replace("-PSG.edf", "%sR.csv" % outputDict[select_ch])) print("end") print("\n=======================================\n")
def test_integer_sfreq_edf(tmp_path): """Test saving a Raw array with integer sfreq to EDF.""" np.random.RandomState(12345) format = 'edf' info = create_info(['1', '2', '3'], sfreq=1000, ch_types=['eeg', 'eeg', 'stim']) data = np.random.random(size=(3, 1000)) * 1e-6 # include subject info and measurement date subject_info = dict(first_name='mne', last_name='python', birthday=(1992, 1, 20), sex=1, hand=3) info['subject_info'] = subject_info raw = RawArray(data, info) raw.set_meas_date(datetime.now(tz=timezone.utc)) temp_fname = op.join(str(tmp_path), f'test.{format}') raw.export(temp_fname) raw_read = read_raw_edf(temp_fname, preload=True) # stim channel should be dropped raw.drop_channels('3') assert raw.ch_names == raw_read.ch_names # only compare the original length, since extra zeros are appended orig_raw_len = len(raw) assert_array_almost_equal( raw.get_data(), raw_read.get_data()[:, :orig_raw_len], decimal=4) assert_allclose( raw.times, raw_read.times[:orig_raw_len], rtol=0, atol=1e-5) # export now by hard-coding physical range raw.export(temp_fname, physical_range=(-3200, 3200)) # include bad birthday that is non-EDF compliant bad_info = info.copy() bad_info['subject_info']['birthday'] = (1700, 1, 20) raw = RawArray(data, bad_info) with pytest.raises(RuntimeError, match='Setting patient birth date'): raw.export(temp_fname) # include bad measurement date that is non-EDF compliant raw = RawArray(data, info) meas_date = datetime(year=1984, month=1, day=1, tzinfo=timezone.utc) raw.set_meas_date(meas_date) with pytest.raises(RuntimeError, match='Setting start date time'): raw.export(temp_fname)