def live_plot(i, buff, ax, channel, update=False): if update: eeg_data, timestamp = inlet.pull_chunk(timeout=1, max_samples=int(SHIFT_LENGTH * fs)) ch_data_left = np.array(eeg_data)[:, INDEX_CHANNEL_LEFT] ch_data_right = np.array(eeg_data)[:, INDEX_CHANNEL_RIGHT] timestamp = np.reshape(timestamp, (len(timestamp), 1)) buff.eeg_buffer[str( INDEX_CHANNEL_LEFT)], filter_state = utils.update_buffer( buff.eeg_buffer[str(INDEX_CHANNEL_LEFT)], ch_data_left) buff.eeg_buffer[str( INDEX_CHANNEL_RIGHT)], filter_state = utils.update_buffer( buff.eeg_buffer[str(INDEX_CHANNEL_RIGHT)], ch_data_right) buff.timestamp_buffer, _ = utils.update_buffer(buff.timestamp_buffer, timestamp) xs = (buff.timestamp_buffer) ys = (buff.eeg_buffer[str(channel)]) xs = xs[-500:] ys = ys[-500:] ax.clear() ax.set_ylim(-3000, 3000) ax.plot(xs, ys) plt.subplots_adjust(bottom=0.30)
def train(self, learning_rate, buffered=False): output_feed, input_feed = [], {} # update policy network input_feed = {} for i in range(len(self.grad_policy)): input_feed[self.grad_policy_in[i]] = self.grad_policy[i] input_feed[self.learning_rate] = learning_rate output_feed.append(self.train_op) # update structured encoder if self.use_structured: output_feed, input_feed = update_buffer( output_feed, input_feed, self.s_encoder_general.train(self.grad_s_encoder_general, learning_rate, buffered=True) ) if self.use_speaker_attn: output_feed, input_feed = update_buffer( output_feed, input_feed, self.s_encoder_attn.train(self.grad_s_encoder_attn, learning_rate, buffered=True) ) # update ns encoder output_feed, input_feed = update_buffer( output_feed, input_feed, self.ns_encoder.train(self.grad_ns_encoder, learning_rate, buffered=True) ) if buffered: return (output_feed, input_feed) else: self.sess.run(output_feed, input_feed)
def dataStream(): global delta_waves global alpha_waves global beta_waves global theta_waves try: # The following loop acquires data, computes band powers, and calculates neurofeedback metrics based on those band powers while True: eeg_data, timestamp = inlet.pull_chunk(timeout=1, max_samples=int( SHIFT_LENGTH * fs)) ch_data = np.array(eeg_data)[:, INDEX_CHANNEL] eeg_buffer, filter_state = utils.update_buffer( eeg_buffer, ch_data, notch=True, filter_state=filter_state) data_epoch = utils.get_last_data(eeg_buffer, EPOCH_LENGTH * fs) band_powers = utils.compute_band_powers(data_epoch, fs) band_buffer, _ = utils.update_buffer(band_buffer, np.asarray([band_powers])) smooth_band_powers = np.mean(band_buffer, axis=0) delta_waves = band_powers[Band.Delta] theta_waves = band_powers[Band.Theta] alpha_waves = band_powers[Band.Alpha] beta_waves = band_powers[Band.Beta] except Exception as e: print(e)
def get_hs(self, batch): self.max_num_edus = max([len(dialog["edus"]) for dialog in batch]) self.edus, self.num_posts = [], [] for dialog in batch: self.edus.append([]) for edu in dialog["edus"]: self.edus[-1].append(edu["tokens"]) for i in range(self.max_num_edus - len(dialog["edus"])): self.edus[-1].append([]) self.num_posts.append(len(dialog["edus"])) o_feed, i_feed = [], {} o_feed, i_feed = update_buffer( o_feed, i_feed, self.agent_bi.ns_encoder.infer(self.edus, self.num_posts, is_train=self.is_train, buffered=True)) o_feed, i_feed = update_buffer( o_feed, i_feed, self.agent_multi.ns_encoder.infer(self.edus, self.num_posts, is_train=self.is_train, buffered=True)) res = self.sess.run(o_feed, i_feed) self.sentences = [] self.sentence_idx = [] for dialog in batch: idx = [] for edu in dialog["edus"]: self.sentences.append(edu["tokens"]) idx.append(len(self.sentences) - 1) self.sentence_idx.append(idx) self.hs_bi, self.hs_multi, self.hs_idp, self.hc_bi, self.hc_multi = [], [], [], [], [] for i, dialog in enumerate(batch): for j in range(len(dialog["edus"])): idx = i * self.max_num_edus + j self.hs_bi.append(res[0][idx]) self.hs_multi.append(res[3][idx]) self.hc_bi.append(res[1][idx]) self.hc_multi.append(res[4][idx]) self.agent_bi.ns_encoder.recurrent_noise = res[2] self.agent_multi.ns_encoder.recurrent_noise = res[5] self.hs_bi = np.array(self.hs_bi) self.hs_multi = np.array(self.hs_multi) self.grad_hs_bi = np.zeros(self.hs_bi.shape) self.grad_hs_multi = np.zeros(self.hs_multi.shape) self.hc_bi = np.array(self.hc_bi) self.hc_multi = np.array(self.hc_multi) self.grad_hc_bi = np.zeros(self.hc_bi.shape) self.grad_hc_multi = np.zeros(self.hc_multi.shape)
def get_dada(): """ 3.1 ACQUIRE DATA """ # Obtain EEG data from the LSL stream global eeg_buffer global filter_state global band_buffer global file eeg_data, timestamp = inlet.pull_chunk( timeout=1, max_samples=int(SHIFT_LENGTH * fs)) # Only keep the channel we're interested in ret = [None]*4 for i in range(4): ch_data = np.array(eeg_data)[:, i]#he tocat aixo # Update EEG buffer with the new data eeg_buffer, filter_state = utils.update_buffer( eeg_buffer, ch_data, notch=True, filter_state=filter_state) """ 3.2 COMPUTE BAND POWERS """ # Get newest samples from the buffer data_epoch = utils.get_last_data(eeg_buffer, EPOCH_LENGTH * fs) # Compute band powers band_powers = utils.compute_band_powers(data_epoch, fs) band_buffer, _ = utils.update_buffer(band_buffer, np.asarray([band_powers])) # Compute the average band powers for all epochs in buffer # This helps to smooth out noise smooth_band_powers = np.mean(band_buffer, axis=0) # print('Delta: ', band_powers[Band.Delta], ' Theta: ', band_powers[Band.Theta], # ' Alpha: ', band_powers[Band.Alpha], ' Beta: ', band_powers[Band.Beta]) #file.write("%lf,%lf,%lf,%lf\n" % (band_powers[Band.Alpha],band_powers[Band.Beta],band_powers[Band.Delta],band_powers[Band.Theta])) """ 3.3 COMPUTE NEUROFEEDBACK METRICS """ # These metrics could also be used to drive brain-computer interfaces # Alpha Protocol: # Simple redout of alpha power, divided by delta waves in order to rule out noise #return (10 + (smooth_band_powers[Band.Alpha] / \ # smooth_band_powers[Band.Delta]))**5 ret[i] = (smooth_band_powers[Band.Alpha] / smooth_band_powers[Band.Delta]) #acum += (smooth_band_powers[Band.Alpha] - smooth_band_powers[Band.Delta]) return ret
def append_metrics(self): """ :param index_channel: the channel to work on. Example: [0] :return: smooth_band_powers """ res = dict() for channel in utils.Channel: # Iterate through all separate channels # Initialize the band power buffer (for plotting) # bands will be ordered: [delta, theta, alpha, beta] band_powers = self._get_band_powers(channel.value) band_buffer = np.zeros((self.n_win_test, 4)) band_buffer, _ = utils.update_buffer(band_buffer, np.asarray([band_powers])) # Record channel smooth band power csbp = self.get_smooth_band_powers(band_buffer) # Aquires power values for interative channel for band in utils.Band: res[(channel.name, band.name)] = csbp[band.value] # Add Ratio measures ratios_measures = Metrics.get_ratios(csbp) for ratio in utils.Ratios: res[(channel.name, ratio.name)] = ratios_measures[ratio.value] self.df = self.df.append(res, ignore_index=True)
def train(self, batch): grad_hs_bi = np.zeros( (len(batch) * self.max_num_edus, self.hs_bi.shape[1])) grad_hc_bi = np.zeros( (len(batch) * self.max_num_edus, self.hc_bi.shape[1])) grad_hs_multi = np.zeros( (len(batch) * self.max_num_edus, self.hs_multi.shape[1])) grad_hc_multi = np.zeros( (len(batch) * self.max_num_edus, self.hc_multi.shape[1])) cur = 0 for i, dialog in enumerate(batch): grad_hs_bi[i * self.max_num_edus: i * self.max_num_edus + len(dialog["edus"]), :] = \ self.grad_hs_bi[cur : cur + len(dialog["edus"]), :] grad_hs_multi[i * self.max_num_edus: i * self.max_num_edus + len(dialog["edus"]), :] = \ self.grad_hs_multi[cur : cur + len(dialog["edus"]), :] grad_hc_bi[i * self.max_num_edus: i * self.max_num_edus + len(dialog["edus"]), :] = \ self.grad_hc_bi[cur : cur + len(dialog["edus"]), :] grad_hc_multi[i * self.max_num_edus: i * self.max_num_edus + len(dialog["edus"]), :] = \ self.grad_hc_multi[cur : cur + len(dialog["edus"]), :] cur += len(dialog["edus"]) o_feed, i_feed = self.agent_bi.ns_encoder\ .get_gradients(self.edus, self.num_posts, grad_hs_bi, grad_hc_bi, buffered=True) o_feed, i_feed = update_buffer( o_feed, i_feed, self.agent_multi.ns_encoder.get_gradients(self.edus, self.num_posts, grad_hs_multi, grad_hc_multi, buffered=True)) res = self.sess.run(o_feed, i_feed) self.agent_bi.grad_ns_encoder = res[0] self.agent_multi.grad_ns_encoder = res[1] self.clip_gradients() output_feed, input_feed = self.agent_bi.train( self.learning_rate.eval(), buffered=True) output_feed, input_feed = update_buffer( output_feed, input_feed, self.agent_multi.train(self.learning_rate.eval(), buffered=True)) self.sess.run(output_feed, input_feed)
def update_gradients_buffer(o_feed, i_feed, agent, name): return update_buffer( o_feed, i_feed, (agent.s_encoder_attn if attn else agent.s_encoder_general).get_gradients( self.hp_bp_buf[attn]["grad_%s" % name], self.hp_bp_buf[attn]["parent_%s" % name], self.hp_bp_buf[attn]["current_%s" % name], self.hp_bp_buf[attn]["relation"], buffered=True))
def get_average_blink(eye_index): eeg_buffer = np.zeros((int(fs * BUFFER_LENGTH), 1)) filter_state = None eye = "left" if eye_index == INDEX_CHANNEL_LEFT else "right" print("Please blink your " + eye + " eye. Press enter when done.") maxMatches = [] stop_flag = [] _thread.start_new_thread(input_thread, (stop_flag, )) i = 10 # number of times to run after user presses enter while i > 0: if stop_flag: i = i - 1 eeg_data, timestamp = inlet.pull_chunk(timeout=1, max_samples=int(SHIFT_LENGTH * fs)) # Only keep the channel we're interested in ch_data = np.array(eeg_data)[:, eye_index] # Update EEG buffer with the new data eeg_buffer, filter_state = utils.update_buffer( eeg_buffer, ch_data, notch=True, filter_state=filter_state) """ 3.2 COMPUTE BAND POWERS """ # Get newest samples from the buffer data_epoch = utils.get_last_data(eeg_buffer, int(EPOCH_LENGTH * fs)) matchFilt = signal.hilbert(filt) matches = signal.correlate(matchFilt, data_epoch[:, 0]) matchesAbs = np.abs(matches[:]) maxMatches.append((np.max(matchesAbs) / 1e5).astype(int)) maxMatchesIdx = np.argmax(maxMatches) idx = maxMatchesIdx peakValues = [] stop_flag = 1 check_right_side = False while True: if idx > 0 and idx < len(maxMatches) - 1: idx = idx + (1 if stop_flag == 2 else -1) possPeak = maxMatches[idx] if maxMatches[maxMatchesIdx] * 0.6 < possPeak: peakValues.append(possPeak) elif stop_flag == 2: break else: check_right_side = True elif stop_flag == 2: break else: check_right_side = True if check_right_side: idx = maxMatchesIdx stop_flag = 2 check_right_side = False return peakValues
def update_hp_buffer(o_feed, i_feed, agent, name, hp): self.hp_new_buf[attn]["parent"] = np.array( self.hp_new_buf[attn]["parent_%s" % name]) self.hp_new_buf[attn]["current"] = np.array( self.hp_new_buf[attn]["current_%s" % name]) self.hp_new_buf[attn]["relation"] = np.array( self.hp_new_buf[attn]["relation"]) return update_buffer( o_feed, i_feed, (agent.s_encoder_attn if attn else agent.s_encoder_general)\ .infer(self.hp_new_buf[attn], fixed_noise, buffered=True) )
def readData(eeg_buffer, filter_state, n_win_test, band_buffer): """ ACQUIRE DATA """ eeg_data, timestamp = inlet.pull_chunk(timeout=1, max_samples=int(SHIFT_LENGTH * fs)) ch_data = np.array(eeg_data)[:, INDEX_CHANNEL] eeg_buffer, filter_state = utils.update_buffer(eeg_buffer, ch_data, notch=True, filter_state=filter_state) """ COMPUTE BAND POWERS """ data_epoch = utils.get_last_data(eeg_buffer, EPOCH_LENGTH * fs) band_powers = utils.compute_band_powers(data_epoch, fs) return band_powers
def _retrieve_channel_data(self, eeg_data, index_channel): # Only keep the channel we're interested in ch_data = np.array(eeg_data)[:, index_channel] # Update EEG buffer with the new data new_eeg_buffer, new_filter_state = utils.update_buffer( self.eeg_buffer, ch_data, notch=True, filter_state=self.filter_state) return new_eeg_buffer, new_filter_state
def animate(i, eeg_buffer, filter_state, n_win_test, band_buffer): global t, start_time """ 3.1 ACQUIRE DATA """ eeg_data, timestamp = inlet.pull_chunk(timeout=1, max_samples=int(SHIFT_LENGTH * fs)) ch_data = np.array(eeg_data)[:, INDEX_CHANNEL] eeg_buffer, filter_state = utils.update_buffer(eeg_buffer, ch_data, notch=True, filter_state=filter_state) """ 3.2 COMPUTE BAND POWERS """ data_epoch = utils.get_last_data(eeg_buffer, EPOCH_LENGTH * fs) band_powers = utils.compute_band_powers(data_epoch, fs) # Add x and y to lists sensor['A'].append(band_powers[0]) sensor['B'].append(band_powers[1]) sensor['G'].append(band_powers[2]) sensor['T'].append(band_powers[3]) t.append(((time.time()) - start_time)) # Limit x and y lists to 20 items sensor['A'] = sensor['A'][-40:] sensor['B'] = sensor['B'][-40:] sensor['G'] = sensor['G'][-40:] sensor['T'] = sensor['T'][-40:] t = t[-40:] # Draw x and y lists ax.clear() ax.plot(t, sensor['A'], color="blue") ax.plot(t, sensor['B'], color="red") ax.plot(t, sensor['G'], color="green") ax.plot(t, sensor['T'], color="black") # Format plot plt.title('sensor') plt.ylabel('data') plt.xlabel('time') plt.ylim(-2, 4)
raise RuntimeError('Can\'t find EEG stream.') print("Start acquiring data") inlet = StreamInlet(streams[0], max_chunklen=12) eeg_time_correction = inlet.time_correction() info = inlet.info() description = info.desc() fs = int(info.nominal_srate()) """ 2. INITIALIZE BUFFERS """ eeg_buffer = np.zeros((int(fs * BUFFER_LENGTH), 1)) filter_state = None # for use with the notch filter n_win_test = int( np.floor((BUFFER_LENGTH - EPOCH_LENGTH) / SHIFT_LENGTH + 1)) band_buffer = np.zeros((n_win_test, 4)) """ 3. GET DATA """ try: while True: """ 3.1 ACQUIRE DATA """ eeg_data, timestamp = inlet.pull_chunk(timeout=1, max_samples=int( SHIFT_LENGTH * fs)) ch_data = np.array(eeg_data)[:, INDEX_CHANNEL] eeg_buffer, filter_state = utils.update_buffer( eeg_buffer, ch_data, notch=True, filter_state=filter_state) """ 3.2 COMPUTE BAND POWERS """ data_epoch = utils.get_last_data(eeg_buffer, EPOCH_LENGTH * fs) band_powers = utils.compute_band_powers(data_epoch, fs) print(band_powers) except KeyboardInterrupt: print('Closing!')
keys = ["up", "down", "left", "right"] try: # The following loop acquires data, computes band powers, and calculates neurofeedback metrics based on those band powers while True: """ 3.1 ACQUIRE DATA """ # Obtain EEG data from the LSL stream eeg_data, timestamp = inlet.pull_chunk(timeout=1, max_samples=int( SHIFT_LENGTH * fs)) # Only keep the channel we're interested in ch_data = np.array(eeg_data)[:, INDEX_CHANNEL] # Update EEG buffer with the new data eeg_buffer, filter_state = utils.update_buffer( eeg_buffer, ch_data, notch=True, filter_state=filter_state) """ 3.2 COMPUTE BAND POWERS """ # Get newest samples from the buffer data_epoch = utils.get_last_data(eeg_buffer, int(EPOCH_LENGTH * fs)) # Compute band powers band_powers = utils.compute_emg_powers(data_epoch, fs) band_buffer, _ = utils.update_buffer(band_buffer, band_powers) # Compute the average band powers for all epochs in buffer # This helps to smooth out noise smooth_band_powers = np.mean(band_buffer, axis=0) for i in range(len(INDEX_CHANNEL)): if not state[i] and smooth_band_powers[4 + i] > 1:
def recordData(): global timeReadings """ 1. CONNECT TO EEG STREAM """ # Search for active LSL streams print('Looking for an EEG stream...') streams = resolve_byprop('type', 'EEG', timeout=2) if len(streams) == 0: raise RuntimeError('Can\'t find EEG stream.') # Set active EEG stream to inlet and apply time correction print("Start acquiring data") inlet = StreamInlet(streams[0], max_chunklen=12) eeg_time_correction = inlet.time_correction() # Get the stream info and description info = inlet.info() description = info.desc() # Get the sampling frequency # This is an important value that represents how many EEG data points are # collected in a second. This influences our frequency band calculation. # for the Muse 2016, this should always be 256 fs = int(info.nominal_srate()) """ 2. INITIALIZE BUFFERS """ # Initialize raw EEG data buffer eeg_buffer = np.zeros((int(fs * BUFFER_LENGTH), 1)) filter_state = None # for use with the notch filter # Compute the number of epochs in "buffer_length" n_win_test = int( np.floor((BUFFER_LENGTH - EPOCH_LENGTH) / SHIFT_LENGTH + 1)) # Initialize the band power buffer (for plotting) # bands will be ordered: [delta, theta, alpha, beta] band_buffer = np.zeros((n_win_test, 4)) """ 3. GET DATA """ # The try/except structure allows to quit the while loop by aborting the # script with <Ctrl-C> # print('Press Ctrl-C in the console to break the while loop.') i = 0 try: # # The following loop acquires data, computes band powers, and calculates neurofeedback metrics based on those band powers while temp: """ 3.1 ACQUIRE DATA """ # Obtain EEG data from the LSL stream eeg_data, timestamp = inlet.pull_chunk(timeout=1, max_samples=int( SHIFT_LENGTH * fs)) # Only keep the channel we're interested in ch_data = np.array(eeg_data)[:, INDEX_CHANNEL] # Update EEG buffer with the new data eeg_buffer, filter_state = utils.update_buffer( eeg_buffer, ch_data, notch=True, filter_state=filter_state) """ 3.2 COMPUTE BAND POWERS """ # Get newest samples from the buffer data_epoch = utils.get_last_data(eeg_buffer, EPOCH_LENGTH * fs) # Compute band powers band_powers = utils.compute_band_powers(data_epoch, fs) band_buffer, _ = utils.update_buffer(band_buffer, np.asarray([band_powers])) # Compute the average band powers for all epochs in buffer # This helps to smooth out noise smooth_band_powers = np.mean(band_buffer, axis=0) # print('Delta: ', band_powers[Band.Delta], ' Theta: ', band_powers[Band.Theta], # ' Alpha: ', band_powers[Band.Alpha], ' Beta: ', band_powers[Band.Beta]) """ 3.3 COMPUTE NEUROFEEDBACK METRICS """ # These metrics could also be used to drive brain-computer interfaces # Alpha Protocol: # Simple redout of alpha power, divided by delta waves in order to rule out noise alpha_metric = smooth_band_powers[Band.Alpha] / \ smooth_band_powers[Band.Delta] alphaReadings.append(alpha_metric) # print('Alpha Relaxation: ', alpha_metric) # Beta Protocol: # Beta waves have been used as a measure of mental activity and concentration # This beta over theta ratio is commonly used as neurofeedback for ADHD beta_metric = smooth_band_powers[Band.Beta] / \ smooth_band_powers[Band.Theta] betaReadings.append(beta_metric) # print('Beta Concentration: ', beta_metric) # Alpha/Theta Protocol: # This is another popular neurofeedback metric for stress reduction # Higher theta over alpha is supposedly associated with reduced anxiety theta_metric = smooth_band_powers[Band.Theta] / \ smooth_band_powers[Band.Alpha] thetaReadings.append(theta_metric) dt = datetime.datetime.now().strftime("%x %X") if dt in timeReadings.keys(): tempDt = timeReadings[dt] aph = tempDt['alpha'] bth = tempDt['beta'] tth = tempDt['theta'] aph.append(alpha_metric) bth.append(beta_metric) tth.append(theta_metric) else: timeReadings = { dt: { "alpha": [alpha_metric], "beta": [beta_metric], "theta": [theta_metric] } } result.append(timeReadings) i = i + 1 except KeyboardInterrupt: print('Closing!')
while True: # Get the time since the start of the script. timeSinceStart = (datetime.datetime.now() - startTime).total_seconds() """ 3.1 ACQUIRE DATA """ # Obtain EEG data from the LSL stream eeg_data, timestamp = inlet.pull_chunk(timeout=1, max_samples=int( SHIFT_LENGTH * fs)) # Only keep the channel we're interested in ch_data = np.array(eeg_data)[:, INDEX_CHANNEL] # Update EEG buffer with the new data eeg_buffer, filter_state = utils.update_buffer( eeg_buffer, ch_data, notch=True, filter_state=filter_state) """ 3.2 COMPUTE BAND POWERS """ # Get newest samples from the buffer data_epoch = utils.get_last_data(eeg_buffer, EPOCH_LENGTH * fs) # Compute band powers band_powers = utils.compute_band_powers(data_epoch, fs) band_buffer, _ = utils.update_buffer(band_buffer, np.asarray([band_powers])) # Compute the average band powers for all epochs in buffer # This helps to smooth out noise smooth_band_powers = np.mean(band_buffer, axis=0) print('Delta: ', band_powers[Band.Delta], ' Theta: ', band_powers[Band.Theta], ' Alpha: ', band_powers[Band.Alpha], ' Beta: ', band_powers[Band.Beta])
# The following loop acquires data, computes band powers, and calculates neurofeedback metrics based on those band powers while True: new_cursor.check_direction() """ 3.1 ACQUIRE DATA """ # Obtain EEG data from the LSL stream eeg_data, timestamp = inlet.pull_chunk(timeout=1, max_samples=int( SHIFT_LENGTH * fs)) # Only keep the channel we're interested in ch_data = np.array(eeg_data)[:, INDEX_CHANNEL_LEFT] # Update EEG buffer with the new data eeg_buffer_left, filter_state_left = utils.update_buffer( eeg_buffer_left, ch_data, notch=True, filter_state=filter_state_left) """ 3.2 COMPUTE BAND POWERS """ # Get newest samples from the buffer data_epoch = utils.get_last_data(eeg_buffer_left, int(EPOCH_LENGTH * fs)) #print(data_epoch.shape) matchFilt = signal.hilbert(filt) matches = signal.correlate(matchFilt, data_epoch[:, 0]) matchesAbs = np.abs(matches[:]) maxMatch = np.max(matchesAbs) / 1e5