def test_raw_reject(): """Test raw data getter with annotation reject.""" info = create_info(['a', 'b', 'c', 'd', 'e'], 100, ch_types='eeg') raw = RawArray(np.ones((5, 15000)), info) with warnings.catch_warnings(record=True): # one outside range raw.annotations = Annotations([2, 100, 105, 148], [2, 8, 5, 8], 'BAD') data = raw.get_data([0, 1, 3, 4], 100, 11200, 'omit') assert_array_equal(data.shape, (4, 9900)) # with orig_time and complete overlap raw = read_raw_fif(fif_fname) raw.annotations = Annotations([44, 47, 48], [1, 3, 1], 'BAD', raw.info['meas_date']) data, times = raw.get_data(range(10), 0, 6000, 'omit', True) assert_array_equal(data.shape, (10, 4799)) assert_equal(times[-1], raw.times[5999]) assert_array_equal(data[:, -100:], raw[:10, 5900:6000][0]) data, times = raw.get_data(range(10), 0, 6000, 'NaN', True) assert_array_equal(data.shape, (10, 6000)) assert_equal(times[-1], raw.times[5999]) assert_true(np.isnan(data[:, 313:613]).all()) # 1s -2s assert_true(not np.isnan(data[:, 614].any())) assert_array_equal(data[:, -100:], raw[:10, 5900:6000][0]) assert_array_equal(raw.get_data(), raw[:][0]) # Test _sync_onset times = [10, -88, 190] onsets = _sync_onset(raw, times) assert_array_almost_equal(onsets, times - raw.first_samp / raw.info['sfreq']) assert_array_almost_equal(times, _sync_onset(raw, onsets, True))
def test_filter_picks(): """Test filter picking.""" data = np.random.RandomState(0).randn(3, 1000) fs = 1000. kwargs = dict(l_freq=None, h_freq=40.) filt = filter_data(data, fs, **kwargs) # don't include seeg, dbs or stim in this list because they are in the one # below to ensure default cases are treated properly for kind in ('eeg', 'grad', 'emg', 'misc', 'dbs'): for picks in (None, [-2], kind, 'k'): # With always at least one data channel info = create_info(['s', 'k', 't'], fs, ['seeg', kind, 'stim']) raw = RawArray(data.copy(), info) raw.filter(picks=picks, **kwargs) if picks is None: if kind in _DATA_CH_TYPES_SPLIT: # should be included want = np.concatenate((filt[:2], data[2:])) else: # shouldn't want = np.concatenate((filt[:1], data[1:])) else: # just the kind of interest ([-2], kind, 'j' should be eq.) want = np.concatenate((data[:1], filt[1:2], data[2:])) assert_allclose(raw.get_data(), want) # Now with sometimes no data channels info = create_info(['k', 't'], fs, [kind, 'stim']) raw = RawArray(data[1:].copy(), info.copy()) if picks is None and kind not in _DATA_CH_TYPES_SPLIT: with pytest.raises(ValueError, match='yielded no channels'): raw.filter(picks=picks, **kwargs) else: raw.filter(picks=picks, **kwargs) want = want[1:] assert_allclose(raw.get_data(), want)
def test_annotation_omit(): """Test raw.get_data with annotations.""" data = np.concatenate([np.ones((1, 1000)), 2 * np.ones((1, 1000))], -1) info = create_info(1, 1000., 'eeg') raw = RawArray(data, info) raw.annotations = Annotations([0.5], [1], ['bad']) expected = raw[0][0] assert_allclose(raw.get_data(reject_by_annotation=None), expected) # nan expected[0, 500:1500] = np.nan assert_allclose(raw.get_data(reject_by_annotation='nan'), expected) got = np.concatenate([ raw.get_data(start=start, stop=stop, reject_by_annotation='nan') for start, stop in ((0, 1000), (1000, 2000)) ], -1) assert_allclose(got, expected) # omit expected = expected[:, np.isfinite(expected[0])] assert_allclose(raw.get_data(reject_by_annotation='omit'), expected) got = np.concatenate([ raw.get_data(start=start, stop=stop, reject_by_annotation='omit') for start, stop in ((0, 1000), (1000, 2000)) ], -1) assert_allclose(got, expected) assert_raises(ValueError, raw.get_data, reject_by_annotation='foo')
def test_raw_reject(): """Test raw data getter with annotation reject.""" sfreq = 100. info = create_info(['a', 'b', 'c', 'd', 'e'], sfreq, ch_types='eeg') raw = RawArray(np.ones((5, 15000)), info) with pytest.warns(RuntimeWarning, match='outside the data range'): raw.set_annotations( Annotations([2, 100, 105, 148], [2, 8, 5, 8], 'BAD')) data, times = raw.get_data( [0, 1, 3, 4], 100, 11200, # 1-112 sec 'omit', return_times=True) bad_times = np.concatenate([ np.arange(200, 400), np.arange(10000, 10800), np.arange(10500, 11000) ]) expected_times = np.setdiff1d(np.arange(100, 11200), bad_times) / sfreq assert_allclose(times, expected_times) # with orig_time and complete overlap raw = read_raw_fif(fif_fname) raw.set_annotations( Annotations(onset=[1, 4, 5] + raw._first_time, duration=[1, 3, 1], description='BAD', orig_time=raw.info['meas_date'])) t_stop = 18. assert raw.times[-1] > t_stop n_stop = int(round(t_stop * raw.info['sfreq'])) n_drop = int(round(4 * raw.info['sfreq'])) assert len(raw.times) >= n_stop data, times = raw.get_data(range(10), 0, n_stop, 'omit', True) assert data.shape == (10, n_stop - n_drop) assert times[-1] == raw.times[n_stop - 1] assert_array_equal(data[:, -100:], raw[:10, n_stop - 100:n_stop][0]) data, times = raw.get_data(range(10), 0, n_stop, 'NaN', True) assert_array_equal(data.shape, (10, n_stop)) assert times[-1] == raw.times[n_stop - 1] t_1, t_2 = raw.time_as_index([1, 2], use_rounding=True) assert np.isnan(data[:, t_1:t_2]).all() # 1s -2s assert not np.isnan(data[:, :t_1].any()) assert not np.isnan(data[:, t_2:].any()) assert_array_equal(data[:, -100:], raw[:10, n_stop - 100:n_stop][0]) assert_array_equal(raw.get_data(), raw[:][0]) # Test _sync_onset times = [10, -88, 190] onsets = _sync_onset(raw, times) assert_array_almost_equal(onsets, times - raw.first_samp / raw.info['sfreq']) assert_array_almost_equal(times, _sync_onset(raw, onsets, True))
def test_raw_reject(): """Test raw data getter with annotation reject.""" sfreq = 100. info = create_info(['a', 'b', 'c', 'd', 'e'], sfreq, ch_types='eeg') raw = RawArray(np.ones((5, 15000)), info) with pytest.warns(RuntimeWarning, match='outside the data range'): raw.set_annotations(Annotations([2, 100, 105, 148], [2, 8, 5, 8], 'BAD')) data, times = raw.get_data([0, 1, 3, 4], 100, 11200, # 1-112 sec 'omit', return_times=True) bad_times = np.concatenate([np.arange(200, 400), np.arange(10000, 10800), np.arange(10500, 11000)]) expected_times = np.setdiff1d(np.arange(100, 11200), bad_times) / sfreq assert_allclose(times, expected_times) # with orig_time and complete overlap raw = read_raw_fif(fif_fname) raw.set_annotations(Annotations(onset=[1, 4, 5] + raw._first_time, duration=[1, 3, 1], description='BAD', orig_time=raw.info['meas_date'])) t_stop = 18. assert raw.times[-1] > t_stop n_stop = int(round(t_stop * raw.info['sfreq'])) n_drop = int(round(4 * raw.info['sfreq'])) assert len(raw.times) >= n_stop data, times = raw.get_data(range(10), 0, n_stop, 'omit', True) assert data.shape == (10, n_stop - n_drop) assert times[-1] == raw.times[n_stop - 1] assert_array_equal(data[:, -100:], raw[:10, n_stop - 100:n_stop][0]) data, times = raw.get_data(range(10), 0, n_stop, 'NaN', True) assert_array_equal(data.shape, (10, n_stop)) assert times[-1] == raw.times[n_stop - 1] t_1, t_2 = raw.time_as_index([1, 2], use_rounding=True) assert np.isnan(data[:, t_1:t_2]).all() # 1s -2s assert not np.isnan(data[:, :t_1].any()) assert not np.isnan(data[:, t_2:].any()) assert_array_equal(data[:, -100:], raw[:10, n_stop - 100:n_stop][0]) assert_array_equal(raw.get_data(), raw[:][0]) # Test _sync_onset times = [10, -88, 190] onsets = _sync_onset(raw, times) assert_array_almost_equal(onsets, times - raw.first_samp / raw.info['sfreq']) assert_array_almost_equal(times, _sync_onset(raw, onsets, True))
def test_resample_below_1_sample(): """Test resampling doesn't yield datapoints.""" # Raw x = np.zeros((1, 100)) sfreq = 1000. raw = RawArray(x, create_info(1, sfreq, 'eeg')) raw.resample(5) assert len(raw.times) == 1 assert raw.get_data().shape[1] == 1 # Epochs x = np.zeros((1, 10000)) sfreq = 1000. raw = RawArray(x, create_info(1, sfreq, 'eeg')) events = np.array([[400, 0, 1], [2000, 0, 1], [3000, 0, 1]]) epochs = Epochs(raw, events, {'test': 1}, 0, 0.2, proj=False, picks='eeg', baseline=None, preload=True, verbose=False) epochs.resample(1) assert len(epochs.times) == 1 assert epochs.get_data().shape[2] == 1
def test_resample_raw(): """Test resampling using RawArray.""" x = np.zeros((1, 1001)) sfreq = 2048. raw = RawArray(x, create_info(1, sfreq, 'eeg')) raw.resample(128, npad=10) data = raw.get_data() assert data.shape == (1, 63)
def test_get_data_reject(): """Test if reject_by_annotation is working correctly.""" fs = 256 ch_names = ["C3", "Cz", "C4"] info = create_info(ch_names, sfreq=fs) raw = RawArray(np.zeros((len(ch_names), 10 * fs)), info) raw.set_annotations(Annotations(onset=[2, 4], duration=[3, 2], description="bad")) with catch_logging() as log: data = raw.get_data(reject_by_annotation="omit", verbose=True) msg = ('Omitting 1024 of 2560 (40.00%) samples, retaining 1536' + ' (60.00%) samples.') assert log.getvalue().strip() == msg assert data.shape == (len(ch_names), 1536) with catch_logging() as log: data = raw.get_data(reject_by_annotation="nan", verbose=True) msg = ('Setting 1024 of 2560 (40.00%) samples to NaN, retaining 1536' + ' (60.00%) samples.') assert log.getvalue().strip() == msg assert data.shape == (len(ch_names), 2560) # shape doesn't change assert np.isnan(data).sum() == 3072 # but NaNs are introduced instead
def test_annotation_omit(): """Test raw.get_data with annotations.""" data = np.concatenate([np.ones((1, 1000)), 2 * np.ones((1, 1000))], -1) info = create_info(1, 1000., 'eeg') raw = RawArray(data, info) raw.set_annotations(Annotations([0.5], [1], ['bad'])) expected = raw[0][0] assert_allclose(raw.get_data(reject_by_annotation=None), expected) # nan expected[0, 500:1500] = np.nan assert_allclose(raw.get_data(reject_by_annotation='nan'), expected) got = np.concatenate([raw.get_data(start=start, stop=stop, reject_by_annotation='nan') for start, stop in ((0, 1000), (1000, 2000))], -1) assert_allclose(got, expected) # omit expected = expected[:, np.isfinite(expected[0])] assert_allclose(raw.get_data(reject_by_annotation='omit'), expected) got = np.concatenate([raw.get_data(start=start, stop=stop, reject_by_annotation='omit') for start, stop in ((0, 1000), (1000, 2000))], -1) assert_allclose(got, expected) pytest.raises(ValueError, raw.get_data, reject_by_annotation='foo')
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' ] ch_names = np.arange(len(ch_types)).astype(str).tolist() info = create_info(ch_names, sfreq=1000, ch_types=ch_types) data = rng.random(size=(len(ch_names), 1000)) * 1.e-5 # 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) # export once temp_fname = op.join(str(tmp_path), f'test.{format}') raw.export(temp_fname, add_ch_type=True) raw_read = read_raw_edf(temp_fname, preload=True) # export again raw_read.load_data() raw_read.export(temp_fname, add_ch_type=True) raw_read = read_raw_edf(temp_fname, 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 test_raw_reject(): """Test raw data getter with annotation reject.""" info = create_info(['a', 'b', 'c', 'd', 'e'], 100, ch_types='eeg') raw = RawArray(np.ones((5, 15000)), info) raw.annotations = Annotations([2, 100, 105, 148], [2, 8, 5, 8], 'BAD') data = raw.get_data([0, 1, 3, 4], 100, 11200, 'omit') assert_array_equal(data.shape, (4, 9900)) # with orig_time and complete overlap raw = read_raw_fif(fif_fname) raw.annotations = Annotations([44, 47, 48], [1, 3, 1], 'BAD', raw.info['meas_date']) data, times = raw.get_data(range(10), 0, 6000, 'omit', True) assert_array_equal(data.shape, (10, 4799)) assert_equal(times[-1], raw.times[5999]) assert_array_equal(data[:, -100:], raw[:10, 5900:6000][0]) data, times = raw.get_data(range(10), 0, 6000, 'NaN', True) assert_array_equal(data.shape, (10, 6000)) assert_equal(times[-1], raw.times[5999]) assert_true(np.isnan(data[:, 313:613]).all()) # 1s -2s assert_true(not np.isnan(data[:, 614].any())) assert_array_equal(data[:, -100:], raw[:10, 5900:6000][0]) assert_array_equal(raw.get_data(), raw[:][0])
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)
epochs = Epochs(debug_pilot, events, event_id, proj=False, picks=picks, baseline=None, preload=True, verbose=False, tmin=-1.5, tmax=2.5) debug_data = epochs.get_data() debug_data = debug_data[:,:,:-1] # print(_pilotX[0].shape) # print(debug_data[0].shape) # print(debug_data[0]) # print() stream_info = create_info(debug_pilot.ch_names[:-3], 500, 'eeg') signal = debug_data[4] raw = RawArray(data=signal, info=stream_info) raw = raw.filter(LO_FREQ, HI_FREQ, method='fir', fir_design='firwin', phase='zero') raw = raw.crop(tmin=2.) raw = raw.resample(125) realtime = raw.get_data(picks=sorted(GOODS))*1000 print(sorted(GOODS)) print(realtime.shape) print(realtime) print() import numpy as np from matplotlib import pyplot as plt t = np.arange(0, 2, 0.008) fig, axs = plt.subplots(3, 3) p = 0
inlet = lsl_connect() seconds = 20 chunk, timestamps = inlet.pull_chunk(max_samples=2000) ts = np.zeros((0, 64)) for i in range(seconds): sleep(1) chunk, timestamps = inlet.pull_chunk(max_samples=2000) chunk = np.array(chunk) print(ts.shape) ts = np.concatenate([ts, chunk], axis=0) print(chunk.shape, ts.shape) ts = ts.T print(ts.shape) raw = RawArray(data=ts, info=stream_info) raw = raw.filter(LO_FREQ, HI_FREQ, method='fir', fir_design='firwin', phase='zero') raw.resample(125) print(raw) raw_data = raw.get_data(picks=sorted(GOODS)) / 1000 print(raw_data.shape) for i in range(9): print(min(raw_data[i]), max(raw_data[i]), np.mean(raw_data[i]))
def stream(self): print("starting stream thread") # pull chunk because the first doesn't work? self.eeg.pull_chunk() while True and self.eeg is not None: while self.target is None: pass # sleep for 2.5 seconds sleep(2.5) # grab the last chunk of samples - high due to filter length requirements on notch filter chunk, timestamps = self.eeg.pull_chunk(max_samples=2000) print(np.asarray(chunk).shape) chunk = np.asarray(chunk).T # for i in range(64): # print(np.mean(chunk[i,:])) # turn into mne object with RawArray # apply info from self.stream_info above to get channel info raw = RawArray(data=chunk, info=self.stream_info) # print(raw) # bandpass filter raw = raw.filter(LO_FREQ, HI_FREQ, method='fir', fir_design='firwin', phase='zero') # remove bad channels # raw = filter_channels(raw, GOODS) # raw.info['bads'] = [x for x in raw.ch_names if x not in GOODS] # raw = raw.reorder_channels(sorted(raw.ch_names)) # crop to the final 1024 samples - change to 1000 eventually # split into four 250 sample blocks with no shared samples raw.resample(125) # rescale to the same unit as the pilot data uV -> mV raw_data = raw.get_data(picks=sorted(GOODS), start=250) / 1000 to_classify = np.stack([raw_data]) # to_classify = np.stack([raw_data[:,i::4] for i in range(4)]) # 4 distinct windowx # to_classify = np.stack([raw_data[:,0::4]]) # 1 window resample print(to_classify.shape) # print(to_classify) for i in range(9): print(min(to_classify[0, i, :]), max(to_classify[0, i, :]), np.mean(to_classify[0, i, :])) # print(to_classify) # classify each individually # reshape to [epochs (4), kernels (1), channels (?), samples (1000)] probs = self.model.predict( to_classify.reshape(to_classify.shape[0], 1, to_classify.shape[1], to_classify.shape[2])) # print(probs) probs = np.sum(probs, axis=0) / 1 print(probs) confidences = np.sort(probs, axis=0)[::-1] confidence = confidences[0] - confidences[1] prediction = probs.argmax(axis=0) print("classification:", prediction, f"({debug_label_map[prediction]})") print("confidence:", confidence) if confidence < 0.20: # send unknown print('unknown') self.game.sendall(bytes([25])) else: # send index of result print(self.target, prediction + 1) self.game.sendall(bytes([prediction + 1])) # if prediction == 0: # self.game.sendall(bytes([1])) # elif prediction == 1: # self.game.sendall(bytes([3])) # else: # self.game.sendall(bytes([2])) # average result and assess probabilities # return predicted class to ForestShepherd # return self.target to None and continue self.target = None print('quitting stream and cleaning up') self.to_classify.clear()
def test_realign(ratio_other, start_raw, start_other, stop_raw, stop_other): """Test realigning raw.""" # construct a true signal sfreq = 100. duration = 50 stop_raw = duration - stop_raw stop_other = duration - stop_other signal = np.zeros(int(round((duration + 1) * sfreq))) orig_events = np.round( np.arange( max(start_raw, start_other) + 2, min(stop_raw, stop_other) - 2) * sfreq).astype(int) signal[orig_events] = 1. n_events = len(orig_events) times = np.arange(len(signal)) / sfreq stim = np.convolve(signal, np.ones(int(round(0.02 * sfreq))))[:len(times)] signal = np.convolve(signal, np.hanning(int(round(0.2 * sfreq))))[:len(times)] # construct our sampled versions of these signals (linear interp is fine) sfreq_raw = sfreq sfreq_other = ratio_other * sfreq raw_times = np.arange(start_raw, stop_raw, 1. / sfreq_raw) other_times = np.arange(start_other, stop_other, 1. / sfreq_other) assert raw_times[0] >= times[0] assert raw_times[-1] <= times[-1] assert other_times[0] >= times[0] assert other_times[-1] <= times[-1] data_raw = np.array([ interp1d(times, d, kind)(raw_times) for d, kind in ((signal, 'linear'), (stim, 'nearest')) ]) data_other = np.array([ interp1d(times, d, kind)(other_times) for d, kind in ((signal, 'linear'), (stim, 'nearest')) ]) info_raw = create_info(['raw_data', 'raw_stim'], sfreq, ['eeg', 'stim']) info_other = create_info(['other_data', 'other_stim'], sfreq, ['eeg', 'stim']) raw = RawArray(data_raw, info_raw, first_samp=111) other = RawArray(data_other, info_other, first_samp=222) # naive processing evoked_raw, events_raw, _, events_other = _assert_similarity( raw, other, n_events) if start_raw == start_other: # can just naively crop a, b = data_raw[0], data_other[0] n = min(len(a), len(b)) corr = np.corrcoef(a[:n], b[:n])[0, 1] min_, max_ = (0.99999, 1.) if sfreq_raw == sfreq_other else (0.8, 0.9) assert min_ <= corr <= max_ # realign t_raw = (events_raw[:, 0] - raw.first_samp) / other.info['sfreq'] t_other = (events_other[:, 0] - other.first_samp) / other.info['sfreq'] assert duration - 10 <= len(events_raw) < duration raw_orig, other_orig = raw.copy(), other.copy() realign_raw(raw, other, t_raw, t_other) # old events should still work for raw and produce the same result evoked_raw_2, _, _, _ = _assert_similarity(raw, other, n_events, events_raw=events_raw) assert_allclose(evoked_raw.data, evoked_raw_2.data) assert_allclose(raw.times, other.times) # raw data now aligned corr = np.corrcoef(raw.get_data([0])[0], other.get_data([0])[0])[0, 1] assert 0.99 < corr <= 1. # Degenerate conditions -- only test in one run test_degenerate = (start_raw == start_other and stop_raw == stop_other and ratio_other == 1) if not test_degenerate: return # these alignments will not be correct but it shouldn't matter with pytest.warns(RuntimeWarning, match='^Fewer.*may be unreliable.*'): realign_raw(raw, other, raw_times[:5], other_times[:5]) with pytest.raises(ValueError, match='same shape'): realign_raw(raw_orig, other_orig, raw_times[:5], other_times) rand_times = np.random.RandomState(0).randn(len(other_times)) with pytest.raises(ValueError, match='cannot resample safely'): realign_raw(raw_orig, other_orig, rand_times, other_times) with pytest.warns(RuntimeWarning, match='.*computed as R=.*unreliable'): realign_raw(raw_orig, other_orig, raw_times + rand_times * 1000, other_times)
def stream(self): print("starting debug stream thread") debug_index = 0 while True: while self.target is None: pass sleep(2.5) # grab debug epoch chunk = debug_data[debug_index] debug_index += 1 # turn into mne object with RawArray # apply info from self.stream_info above to get channel info raw = RawArray(data=chunk, info=create_info(debug_pilot.ch_names[:-3], 500, 'eeg')) # print(raw.info) # print(debug_pilot.info) # bandpass filter raw = raw.filter(LO_FREQ, HI_FREQ, method='fir', fir_design='firwin', phase='zero') # raw = raw.notch_filter(50, method='iir') # raw = raw.reorder_channels(sorted(raw.ch_names)) # get processed data and split into 4 # raw.crop(tmin=2.) # raw_data = raw.get_data(picks=sorted(GOODS))*1000 # to_classify = np.stack([raw_data[:,i::4] for i in range(4)]) # or resample raw.crop(tmin=2.) raw = raw.resample(125) to_classify = np.stack([raw.get_data(picks=sorted(GOODS))*1000]) print(to_classify.shape) # print(to_classify) # print(to_classify) # classify each individually # reshape to [epochs (4), kernels (1), channels (?), samples (1000)] probs = self.model.predict(to_classify.reshape(to_classify.shape[0], 1, to_classify.shape[1], to_classify.shape[2])) # print(probs) probs = np.sum(probs, axis=0) / 1 print(probs) result = np.where(probs > 0.66) print("debug target:", debug_labels[debug_index], f"({debug_label_map[debug_labels[debug_index]]})") # self.game.sendall(bytes([self.target + 1])) if len(result[0]) == 0: # send unknown print('unknown') self.game.sendall(bytes([25])) else: # send index of result print('classified:', result[0][0], f"({debug_label_map[result[0][0]]})") self.game.sendall(bytes([result[0][0] + 1])) self.target = None print('quitting stream and cleaning up') self.to_classify.clear()
show_scalebars=False) ############################################################################### # Artifact identification # ----------------------- # Identify artifact by eye: blinks are well separated in source S0 blink_idx = 0 # Get normal spectrum, ie power spectrum after trace-normalization blink_spectrum_norm = ajdc._cosp_sources[:, blink_idx, blink_idx] blink_spectrum_norm /= np.linalg.norm(blink_spectrum_norm) # Get absolute spectrum, ie raw power spectrum of the source f, spectrum = welch(source.get_data(picks=[blink_idx]), fs=sfreq, nperseg=window, noverlap=int(window * overlap)) blink_spectrum_abs = spectrum[0, (f >= fmin) & (f <= fmax)] blink_spectrum_abs /= np.linalg.norm(blink_spectrum_abs) # Get topographic map blink_filter = ajdc.backward_filters_[:, blink_idx] # Plot spectrum and topographic map of the blink source separated by AJDC fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(12, 5)) axs[0].set(title='Power spectrum of the blink source estimated by AJDC', xlabel='Frequency (Hz)', ylabel='Power spectral density') axs[0].plot(freqs, blink_spectrum_abs, label='Absolute power') axs[0].plot(freqs, blink_spectrum_norm, label='Normal power') axs[0].legend() axs[1].set_title('Topographic map of the blink source estimated by AJDC')
def test_rawarray_edf(tmp_path): """Test saving a Raw array with integer sfreq to EDF.""" rng = np.random.RandomState(12345) format = 'edf' ch_types = [ 'eeg', 'eeg', 'stim', 'ecog', 'seeg', 'eog', 'ecg', 'emg', 'dbs', 'bio' ] ch_names = np.arange(len(ch_types)).astype(str).tolist() info = create_info(ch_names, sfreq=1000, ch_types=ch_types) data = rng.random(size=(len(ch_names), 1000)) * 1e-5 # 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) time_now = datetime.now() meas_date = datetime(year=time_now.year, month=time_now.month, day=time_now.day, hour=time_now.hour, minute=time_now.minute, second=time_now.second, tzinfo=timezone.utc) raw.set_meas_date(meas_date) temp_fname = op.join(str(tmp_path), f'test.{format}') raw.export(temp_fname, add_ch_type=True) raw_read = read_raw_edf(temp_fname, 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) assert raw.info['meas_date'] == raw_read.info['meas_date'] # channel name can't be longer than 16 characters with the type added raw_bad = raw.copy() raw_bad.rename_channels({'1': 'abcdefghijklmnopqrstuvwxyz'}) with pytest.raises(RuntimeError, match='Signal label'), \ pytest.warns(RuntimeWarning, match='Data has a non-integer'): raw_bad.export(temp_fname) # 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) # test that warning is raised if there are non-voltage based channels raw = RawArray(data, info) with pytest.warns(RuntimeWarning, match='The unit'): raw.set_channel_types({'9': 'hbr'}) with pytest.warns(RuntimeWarning, match='Non-voltage channels'): raw.export(temp_fname) # data should match up to the non-accepted channel raw_read = read_raw_edf(temp_fname, preload=True) orig_raw_len = len(raw) assert_array_almost_equal(raw.get_data()[:-1, :], raw_read.get_data()[:, :orig_raw_len], decimal=4) assert_allclose(raw.times, raw_read.times[:orig_raw_len], rtol=0, atol=1e-5) # the data should still match though raw_read = read_raw_edf(temp_fname, preload=True) raw.drop_channels('2') assert raw.ch_names == raw_read.ch_names 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)
raw = raw.filter(LO_FREQ, HI_FREQ, method='fir', fir_design='firwin', phase='zero') raw = raw.notch_filter(50) # raw = raw.drop_channels([ch for ch in raw.ch_names if ch not in GOODS]) # print(raw.ch_names) # raw = raw.reorder_channels(sorted(raw.ch_names)) # print(raw.ch_names) raw = raw.crop(tmin=11.) raw = raw.resample(125) # raw = raw.reorder_channels(sorted(raw.ch_names)) # print(raw.ch_names) realtime = raw.get_data(picks=sorted(GOODS)) * 1000 raw = raw.reorder_channels(sorted(raw.ch_names)) # print(raw.ch_names) ordered = raw.get_data(picks=GOODS) * 1000 # print(realtime[0]) # print(ordered[0]) # print(_pilotX[0].shape, _pilotX[0][0]) # print(realtime.shape, realtime[0]) import numpy as np import matplotlib.pyplot as plt t = np.arange(0, 2, 0.008) for i in range(9):
def test_csd_degenerate(evoked_csd_sphere): """Test degenerate conditions.""" evoked, csd, sphere = evoked_csd_sphere warn_evoked = evoked.copy() warn_evoked.info['bads'].append(warn_evoked.ch_names[3]) with pytest.raises(ValueError, match='Either drop.*or interpolate'): compute_current_source_density(warn_evoked) with pytest.raises(TypeError, match='must be an instance of'): compute_current_source_density(None) fail_evoked = evoked.copy() with pytest.raises(ValueError, match='Zero or infinite position'): for ch in fail_evoked.info['chs']: ch['loc'][:3] = np.array([0, 0, 0]) compute_current_source_density(fail_evoked, sphere=sphere) with pytest.raises(ValueError, match='Zero or infinite position'): fail_evoked.info['chs'][3]['loc'][:3] = np.inf compute_current_source_density(fail_evoked, sphere=sphere) with pytest.raises(ValueError, match='No EEG channels found.'): fail_evoked = evoked.copy() fail_evoked.set_channel_types({ch_name: 'ecog' for ch_name in fail_evoked.ch_names}) compute_current_source_density(fail_evoked, sphere=sphere) with pytest.raises(TypeError, match='lambda2'): compute_current_source_density(evoked, lambda2='0', sphere=sphere) with pytest.raises(ValueError, match='lambda2 must be between 0 and 1'): compute_current_source_density(evoked, lambda2=2, sphere=sphere) with pytest.raises(TypeError, match='stiffness must be'): compute_current_source_density(evoked, stiffness='0', sphere=sphere) with pytest.raises(ValueError, match='stiffness must be non-negative'): compute_current_source_density(evoked, stiffness=-2, sphere=sphere) with pytest.raises(TypeError, match='n_legendre_terms must be'): compute_current_source_density(evoked, n_legendre_terms=0.1, sphere=sphere) with pytest.raises(ValueError, match=('n_legendre_terms must be ' 'greater than 0')): compute_current_source_density(evoked, n_legendre_terms=0, sphere=sphere) with pytest.raises(ValueError, match='sphere must be'): compute_current_source_density(evoked, sphere=-0.1) with pytest.raises(ValueError, match=('sphere radius must be ' 'greater than 0')): compute_current_source_density(evoked, sphere=(-0.1, 0., 0., -1.)) with pytest.raises(TypeError): compute_current_source_density(evoked, copy=2, sphere=sphere) # gh-7859 raw = RawArray(evoked.data, evoked.info) epochs = Epochs( raw, [[0, 0, 1]], tmin=0, tmax=evoked.times[-1] - evoked.times[0], baseline=None, preload=False, proj=False) epochs.drop_bad() assert len(epochs) == 1 assert_allclose(epochs.get_data()[0], evoked.data) with pytest.raises(RuntimeError, match='Computing CSD requires.*preload'): compute_current_source_density(epochs) epochs.load_data() raw = compute_current_source_density(raw) assert not np.allclose(raw.get_data(), evoked.data) evoked = compute_current_source_density(evoked) assert_allclose(raw.get_data(), evoked.data) epochs = compute_current_source_density(epochs) assert_allclose(epochs.get_data()[0], evoked.data)