def test_proj_raw_duration(duration, sfreq): """Test equivalence of `duration` options.""" n_ch, n_dim = 30, 3 rng = np.random.RandomState(0) signals = rng.randn(n_dim, 10000) mixing = rng.randn(n_ch, n_dim) + [0, 1, 2] data = np.dot(mixing, signals) raw = RawArray(data, create_info(n_ch, sfreq, 'eeg')) raw.set_eeg_reference(projection=True) n_eff = int(round(raw.info['sfreq'] * duration)) # crop to an even "duration" number of epochs stop = ((len(raw.times) // n_eff) * n_eff - 1) / raw.info['sfreq'] raw.crop(0, stop) proj_def = compute_proj_raw(raw, n_eeg=n_dim) proj_dur = compute_proj_raw(raw, duration=duration, n_eeg=n_dim) proj_none = compute_proj_raw(raw, duration=None, n_eeg=n_dim) assert len(proj_dur) == len(proj_none) == len(proj_def) == n_dim # proj_def is not in here because it does not necessarily evenly divide # the signal length: for pu, pn in zip(proj_dur, proj_none): assert_allclose(pu['data']['data'], pn['data']['data']) # but we can test it here since it should still be a small subspace angle: for proj in (proj_dur, proj_none, proj_def): computed = np.concatenate([p['data']['data'] for p in proj], 0) angle = np.rad2deg(linalg.subspace_angles(computed.T, mixing)[0]) assert angle < 1e-5
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()
events, event_id = events_from_annotations(debug_pilot, event_id={'Stimulus/left': 0, 'Stimulus/right': 1, 'Stimulus/feet': 2}) picks = pick_types(debug_pilot.info, meg=False, eeg=True, stim=False, eog=False) 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)
from mne import create_info from integration import stream_channels raw = RawArray(data=debug_data[0], info=create_info(stream_channels, 500, 'eeg')) 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])