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)
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 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()
def preprocess_ts(ts_file,orig_channel_names_file,orig_channel_coords_file, h_freq, orig_sfreq, down_sfreq ,prefiltered = False): from mne.io import RawArray from mne import create_info import os import numpy as np #### load electrode names elec_names = [line.strip() for line in open(orig_channel_names_file)] #print elec_names ### save electrode locations elec_loc = np.loadtxt(orig_channel_coords_file) #print elec_loc ### no modification on electrode names and locations correct_elec_loc = elec_loc correct_elec_names = elec_names print len(correct_elec_names) print len(correct_elec_loc) ### save electrode locations channel_coords_file = os.path.abspath("correct_channel_coords.txt") np.savetxt(channel_coords_file ,correct_elec_loc , fmt = '%s') #### save electrode names channel_names_file = os.path.abspath("correct_channel_names.txt") np.savetxt(channel_names_file,correct_elec_names , fmt = '%s') ##### downsampling on data if orig_sfreq != down_sfreq: ts = np.load(ts_file) print ts.shape raw = RawArray(ts, info = create_info(ch_names = elec_names, sfreq = orig_sfreq)) indexes_good_elec = np.arange(len(elec_names)) print indexes_good_elec if prefiltered == False: raw.filter(l_freq = None, h_freq = down_sfreq, picks = indexes_good_elec) raw.resample(sfreq = down_sfreq,npad = 100) downsampled_ts,times = raw[:,:] print downsampled_ts.shape downsampled_ts_file = os.path.abspath("downsampled_ts.npy") np.save(downsampled_ts_file,downsampled_ts) print raw.info['sfreq'] return downsampled_ts_file,channel_coords_file,channel_names_file,raw.info['sfreq'] else: print "No downsampling was applied as orig_sfreq and down_sfreq are identical" return ts_file,channel_coords_file,channel_names_file,orig_sfreq
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()