def stopRecording(self): self.recording = False print(self.eeg_raw) eeg_epochs = BCI.epoch_array(self.eeg_raw, self.EPOCH_LENGTH, self.OVERLAP_LENGTH * self.freq, self.freq) print(eeg_epochs.shape) feat_matrix = BCI.compute_feature_matrix(eeg_epochs, self.freq) percent_change = BCI.calc_ratio(feat_matrix, self.baseline) print(percent_change) q75, q25 = np.percentile(percent_change, [75, 25]) iqr = q75 - q25 lower_bound = q25 - (1.5 * iqr) upper_bound = q75 + (1.5 * iqr) for x in range(0,len(percent_change)): if (percent_change[x] > upper_bound): percent_change[x] = np.median(percent_change) if (percent_change[x] < lower_bound): percent_change[x] = np.median(percent_change) # return percent_change # data = self.processEEG(self.eeg_raw) self.eeg_raw = np.ndarray(shape = (8,4)) return jsonify(percent_change)
def __init__(self): print('Connecting...') streams = resolve_byprop('type', 'EEG', timeout=2) if len(streams) == 0: raise RuntimeError('Can\'t find EEG stream.') # set up Inlet inlet = StreamInlet(streams[0], max_chunklen=12) eeg_time_correction = inlet.time_correction() # Pull relevant information info = inlet.info() self.desc = info.desc() self.freq = int(info.nominal_srate()) ## TRAIN DATASET print('Recording Baseline') eeg_data_baseline = BCI.record_eeg_filtered( self.TRAINING_LENGTH, self.freq, self.INDEX_CHANNEL, True, ) eeg_epochs_baseline = BCI.epoch_array( eeg_data_baseline, self.EPOCH_LENGTH, self.OVERLAP_LENGTH * self.freq, self.freq) feat_matrix_baseline = BCI.compute_feature_matrix( eeg_epochs_baseline, self.freq) self.baseline = BCI.calc_baseline(feat_matrix_baseline)
def processEEG(eeg_raw): eeg_epochs = BCI.epoch_array(eeg_raw, self.EPOCH_LENGTH, self.OVERLAP_LENGTH * self.freq, self.freq) feat_matrix = BCI.compute_feature_matrix(eeg_epochs, self.freq) percent_change = BCI.calc_ratio(feat_matrix, self.baseline) q75, q25 = np.percentile(percent_change, [75, 25]) iqr = q75 - q25 lower_bound = q25 - (1.5 * iqr) upper_bound = q75 + (1.5 * iqr) for x in range(0, len(percent_change)): if (percent_change[x] > upper_bound): percent_change[x] = np.median(percent_change) if (percent_change[x] < lower_bound): percent_change[x] = np.median(percent_change) return percent_change
# Record each feature print('Keep Eyes Open') eeg_data0 = BCI.record_eeg(TRAINING_LENGTH, freq, INDEX_CHANNEL) print("Move Eyes Side to Side") eeg_data1 = BCI.record_eeg(TRAINING_LENGTH, freq, INDEX_CHANNEL) print("Move Eyes Up and Down") eeg_data2 = BCI.record_eeg(TRAINING_LENGTH, freq, INDEX_CHANNEL) print("Blink Rapidly ") eeg_data3 = BCI.record_eeg(TRAINING_LENGTH, freq, INDEX_CHANNEL) # Divide data into epochs eeg_epochs0 = BCI.epoch_array(eeg_data0, EPOCH_LENGTH, OVERLAP_LENGTH * freq, freq) eeg_epochs1 = BCI.epoch_array(eeg_data0, EPOCH_LENGTH, OVERLAP_LENGTH * freq, freq) eeg_epochs2 = BCI.epoch_array(eeg_data0, EPOCH_LENGTH, OVERLAP_LENGTH * freq, freq) eeg_epochs3 = BCI.epoch_array(eeg_data0, EPOCH_LENGTH, OVERLAP_LENGTH * freq, freq) # Computer corresponding features feat_matrix0 = BCI.compute_feature_matrix(eeg_epochs0, freq) feat_matrix1 = BCI.compute_feature_matrix(eeg_epochs1, freq) feat_matrix2 = BCI.compute_feature_matrix(eeg_epochs2, freq) feat_matrix3 = BCI.compute_feature_matrix(eeg_epochs3, freq) # Train Classifier [classifier, mu_ft, std_ft, score] = BCI.train_classifier( feat_matrix0,
freq = int(info.nominal_srate()) """ RECORD DATA """ # Record each feature print('Recording Baseline') eeg_data_baseline = BCI.record_eeg_filtered( 20, freq, INDEX_CHANNEL, True, ) print('Recording Main') eeg_data_main = BCI.record_eeg_filtered(TRAINING_LENGTH, freq, INDEX_CHANNEL) # Divide data into epochs eeg_epochs_baseline = BCI.epoch_array(eeg_data_baseline, EPOCH_LENGTH, OVERLAP_LENGTH * freq, freq) eeg_epochs_main = BCI.epoch_array(eeg_data_main, EPOCH_LENGTH, OVERLAP_LENGTH * freq, freq) # Computer corresponding features feat_matrix_baseline = BCI.compute_feature_matrix(eeg_epochs_baseline, freq) feat_matrix_main = BCI.compute_feature_matrix(eeg_epochs_main, freq) baseline = BCI.calc_baseline(feat_matrix_baseline) percent_change = BCI.calc_ratio(feat_matrix_main, baseline) q75, q25 = np.percentile(percent_change, [75, 25]) iqr = q75 - q25 lower_bound = q25 - (1.5 * iqr) upper_bound = q75 + (1.5 * iqr)