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)
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 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 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 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()
print(raw) pilot = pilot.filter(LO_FREQ, HI_FREQ, method='fir', fir_design='firwin', phase='zero') raw = raw.filter(LO_FREQ, HI_FREQ, method='fir', fir_design='firwin', phase='zero') # raw = raw.crop(tmin=2.) pilot = pilot.resample(125) raw = raw.resample(125) pilot_data = pilot.get_data(picks=sorted(GOODS)) * 1000 raw_data = raw.get_data(picks=sorted(GOODS)) * 1000 # print(pilot.ch_names) # print(stream_channels) print(pilot_data.shape) print(raw_data.shape) print(sorted(GOODS)) import numpy as np from matplotlib import pyplot as plt t = np.arange(0, 100) # plt.plot(t, pilot_data[0,0:100], t, raw_data[0,0:100])