def main (): BoardShim.enable_board_logger () DataFilter.enable_data_logger () MLModel.enable_ml_logger () parser = argparse.ArgumentParser () # use docs to check which parameters are required for specific board, e.g. for Cyton - set serial port parser.add_argument ('--timeout', type = int, help = 'timeout for device discovery or connection', required = False, default = 0) parser.add_argument ('--ip-port', type = int, help = 'ip port', required = False, default = 0) parser.add_argument ('--ip-protocol', type = int, help = 'ip protocol, check IpProtocolType enum', required = False, default = 0) parser.add_argument ('--ip-address', type = str, help = 'ip address', required = False, default = '') parser.add_argument ('--serial-port', type = str, help = 'serial port', required = False, default = '') parser.add_argument ('--mac-address', type = str, help = 'mac address', required = False, default = '') parser.add_argument ('--other-info', type = str, help = 'other info', required = False, default = '') parser.add_argument ('--streamer-params', type = str, help = 'streamer params', required = False, default = '') parser.add_argument ('--serial-number', type = str, help = 'serial number', required = False, default = '') parser.add_argument ('--board-id', type = int, help = 'board id, check docs to get a list of supported boards', required = True) parser.add_argument ('--file', type = str, help = 'file', required = False, default = '') args = parser.parse_args () params = BrainFlowInputParams () params.ip_port = args.ip_port params.serial_port = args.serial_port params.mac_address = args.mac_address params.other_info = args.other_info params.serial_number = args.serial_number params.ip_address = args.ip_address params.ip_protocol = args.ip_protocol params.timeout = args.timeout params.file = args.file board = BoardShim (args.board_id, params) master_board_id = board.get_board_id () sampling_rate = BoardShim.get_sampling_rate (master_board_id) board.prepare_session () board.start_stream (45000, args.streamer_params) BoardShim.log_message (LogLevels.LEVEL_INFO.value, 'start sleeping in the main thread') time.sleep (5) # recommended window size for eeg metric calculation is at least 4 seconds, bigger is better data = board.get_board_data () board.stop_stream () board.release_session () eeg_channels = BoardShim.get_eeg_channels (int (master_board_id)) bands = DataFilter.get_avg_band_powers (data, eeg_channels, sampling_rate, True) feature_vector = np.concatenate ((bands[0], bands[1])) print(feature_vector) # calc concentration concentration_params = BrainFlowModelParams (BrainFlowMetrics.CONCENTRATION.value, BrainFlowClassifiers.KNN.value) concentration = MLModel (concentration_params) concentration.prepare () print ('Concentration: %f' % concentration.predict (feature_vector)) concentration.release () # calc relaxation relaxation_params = BrainFlowModelParams (BrainFlowMetrics.RELAXATION.value, BrainFlowClassifiers.REGRESSION.value) relaxation = MLModel (relaxation_params) relaxation.prepare () print ('Relaxation: %f' % relaxation.predict (feature_vector)) relaxation.release ()
def test_brainflow_knn (data): print ('Test BrainFlow KNN') params = BrainFlowModelParams (BrainFlowMetrics.CONCENTRATION.value, BrainFlowClassifiers.KNN.value) model = MLModel (params) start_time = time.time () model.prepare () predicted = [model.predict (x) >= 0.5 for x in data[0]] model.release () stop_time = time.time () print ('Total time %f' % (stop_time - start_time)) print (metrics.classification_report (data[1], predicted))
def main(): BoardShim.enable_board_logger() DataFilter.enable_data_logger() MLModel.enable_ml_logger() params = BrainFlowInputParams() board = BoardShim(BoardIds.BRAINBIT_BOARD.value, params) master_board_id = board.get_board_id() sampling_rate = BoardShim.get_sampling_rate(master_board_id) board.prepare_session() board.start_stream(45000, '') eeg_channels = BoardShim.get_eeg_channels(int(master_board_id)) while True: BoardShim.log_message(LogLevels.LEVEL_INFO.value, 'start sleeping in the main thread') time.sleep( 5 ) # recommended window size for eeg metric calculation is at least 4 seconds, bigger is better data = board.get_board_data() bands = DataFilter.get_avg_band_powers(data, eeg_channels, sampling_rate, True) feature_vector = np.concatenate((bands[0], bands[1])) print(feature_vector) # calc concentration concentration_params = BrainFlowModelParams( BrainFlowMetrics.CONCENTRATION.value, BrainFlowClassifiers.KNN.value) concentration = MLModel(concentration_params) concentration.prepare() print('Concentration: %f' % concentration.predict(feature_vector)) concentration.release() # calc relaxation relaxation_params = BrainFlowModelParams( BrainFlowMetrics.RELAXATION.value, BrainFlowClassifiers.REGRESSION.value) relaxation = MLModel(relaxation_params) relaxation.prepare() print('Relaxation: %f' % relaxation.predict(feature_vector)) relaxation.release() board.stop_stream() board.release_session()
def liveStream(): BoardShim.enable_dev_board_logger() # use synthetic board for demo params = BrainFlowInputParams() board_id = BoardIds.SYNTHETIC_BOARD.value board = BoardShim(board_id, params) eeg_channels = BoardShim.get_eeg_channels(board_id) sampling_rate = BoardShim.get_sampling_rate(board_id) timestamp = BoardShim.get_timestamp_channel(board_id) board.prepare_session() board.start_stream() while True: #get board data removes data from the buffer while board.get_board_data_count() < 250: time.sleep(0.005) data = board.get_board_data() #datadf = pd.DataFrame(np.transpose(data)) #creating a dataframe of the eeg data to extract eeg values later """for count, channel in enumerate(eeg_channels): # filters work in-place #Check Brainflow docs for more filters if count == 0: DataFilter.perform_bandstop(data[channel], sampling_rate, 60.0, 4.0, 4, FilterTypes.BUTTERWORTH.value, 0) # bandstop 58 - 62 DataFilter.perform_bandpass(data[channel], sampling_rate, 21.0, 20.0, 4, FilterTypes.BESSEL.value, 0) # bandpass 11 - 31 if count == 1: DataFilter.perform_bandstop(data[channel], sampling_rate, 60.0, 4.0, 4, FilterTypes.BUTTERWORTH.value, 0) # bandstop 58 - 62 DataFilter.perform_bandpass(data[channel], sampling_rate, 21.0, 20.0, 4, FilterTypes.BESSEL.value, 0) # bandpass 11 - 31 if count == 2: DataFilter.perform_bandstop(data[channel], sampling_rate, 60.0, 4.0, 4, FilterTypes.BUTTERWORTH.value, 0) # bandstop 58 - 62 DataFilter.perform_bandpass(data[channel], sampling_rate, 21.0, 20.0, 4, FilterTypes.BESSEL.value, 0) # bandpass 11 - 31 if count == 3: DataFilter.perform_bandstop(data[channel], sampling_rate, 60.0, 4.0, 4, FilterTypes.BUTTERWORTH.value, 0) # bandstop 58 - 62 DataFilter.perform_bandpass(data[channel], sampling_rate, 21.0, 20.0, 4, FilterTypes.BESSEL.value, 0) # bandpass 11 - 31""" #Brainflow ML Model bands = DataFilter.get_avg_band_powers(data, eeg_channels, sampling_rate, True) feature_vector = np.concatenate((bands[0], bands[1])) print(feature_vector) # calc concentration concentration_params = BrainFlowModelParams( BrainFlowMetrics.CONCENTRATION.value, BrainFlowClassifiers.KNN.value) concentration = MLModel(concentration_params) concentration.prepare() print('Concentration: %f' % concentration.predict(feature_vector)) concentration.release() # calc relaxation relaxation_params = BrainFlowModelParams( BrainFlowMetrics.RELAXATION.value, BrainFlowClassifiers.REGRESSION.value) relaxation = MLModel(relaxation_params) relaxation.prepare() print('Relaxation: %f' % relaxation.predict(feature_vector)) relaxation.release() DataFilter.write_file(data, 'data.csv', 'w') #writing data to csv file board.stop_stream() board.release_session()
def main(i): BoardShim.enable_dev_board_logger() BoardShim.disable_board_logger( ) #optional. take this out for initial setup for your board. # use synthetic board for demo params = BrainFlowInputParams() board_id = BoardIds.SYNTHETIC_BOARD.value board = BoardShim(board_id, params) eeg_channels = BoardShim.get_eeg_channels(board_id) sampling_rate = BoardShim.get_sampling_rate(board_id) timestamp = BoardShim.get_timestamp_channel(board_id) board.prepare_session() board.start_stream() style.use('fivethirtyeight') plt.title("Live EEG stream from Brainflow", fontsize=15) plt.ylabel("Data in millivolts", fontsize=15) plt.xlabel("\nTime", fontsize=10) keep_alive = True eeg1 = [] #lists to store eeg data eeg2 = [] eeg3 = [] eeg4 = [] timex = [] #list to store timestamp while keep_alive == True: while board.get_board_data_count( ) < 250: #ensures that all data shape is the same time.sleep(0.005) data = board.get_current_board_data(250) # creating a dataframe of the eeg data to extract eeg values later eegdf = pd.DataFrame(np.transpose(data[eeg_channels])) eegdf_col_names = [ "ch1", "ch2", "ch3", "ch4", "ch5", "ch6", "ch7", "ch8", "ch9", "ch10", "ch11", "ch12", "ch13", "ch14", "ch15", "ch16" ] eegdf.columns = eegdf_col_names # to keep it simple, making another dataframe for the timestamps to access later timedf = pd.DataFrame(np.transpose(data[timestamp])) print( "EEG Dataframe" ) #easy way to check what data is being streamed and if program is working print(eegdf) #isn't neccesary. for count, channel in enumerate(eeg_channels): # filters work in-place # Check Brainflow docs for more filters if count == 0: DataFilter.perform_bandstop(data[channel], sampling_rate, 60.0, 4.0, 4, FilterTypes.BUTTERWORTH.value, 0) # bandstop 58 - 62 DataFilter.perform_bandpass(data[channel], sampling_rate, 21.0, 20.0, 4, FilterTypes.BESSEL.value, 0) # bandpass 11 - 31 if count == 1: DataFilter.perform_bandstop(data[channel], sampling_rate, 60.0, 4.0, 4, FilterTypes.BUTTERWORTH.value, 0) # bandstop 58 - 62 DataFilter.perform_bandpass(data[channel], sampling_rate, 21.0, 20.0, 4, FilterTypes.BESSEL.value, 0) # bandpass 11 - 31 if count == 2: DataFilter.perform_bandstop(data[channel], sampling_rate, 60.0, 4.0, 4, FilterTypes.BUTTERWORTH.value, 0) # bandstop 58 - 62 DataFilter.perform_bandpass(data[channel], sampling_rate, 21.0, 20.0, 4, FilterTypes.BESSEL.value, 0) # bandpass 11 - 31 if count == 3: DataFilter.perform_bandstop(data[channel], sampling_rate, 60.0, 4.0, 4, FilterTypes.BUTTERWORTH.value, 0) # bandstop 58 - 62 DataFilter.perform_bandpass(data[channel], sampling_rate, 21.0, 20.0, 4, FilterTypes.BESSEL.value, 0) # bandpass 11 - 31 # Brainflow ML Model bands = DataFilter.get_avg_band_powers(data, eeg_channels, sampling_rate, True) feature_vector = np.concatenate((bands[0], bands[1])) # calc concentration concentration_params = BrainFlowModelParams( BrainFlowMetrics.CONCENTRATION.value, BrainFlowClassifiers.KNN.value) concentration = MLModel(concentration_params) concentration.prepare() print('Concentration: %f' % concentration.predict(feature_vector)) concentrated_measure = concentration.predict(feature_vector) concentration.release() # calc relaxation relaxation_params = BrainFlowModelParams( BrainFlowMetrics.RELAXATION.value, BrainFlowClassifiers.KNN.value) relaxation = MLModel(relaxation_params) relaxation.prepare() print('Relaxation: %f' % relaxation.predict(feature_vector)) relaxed_measure = relaxation.predict(feature_vector) relaxation.release() #appending eeg data to lists eeg1.extend( eegdf.iloc[:, 0].values ) # I am using OpenBCI Ganglion board, so I only have four channels. eeg2.extend( eegdf.iloc[:, 1].values ) # If you have a different board, you should be able to copy paste eeg3.extend(eegdf.iloc[:, 2].values) # these commands for more channels. eeg4.extend(eegdf.iloc[:, 3].values) timex.extend(timedf.iloc[:, 0].values) # timestamps plt.cla() #plotting eeg data plt.plot(timex, eeg1, label="Channel 1", color="red") plt.plot(timex, eeg2, label="Channel 2", color="blue") plt.plot(timex, eeg3, label="Channel 3", color="orange") plt.plot(timex, eeg4, label="Channel 4", color="purple") plt.tight_layout() keep_alive = False #resetting stream so that matplotlib can plot data if concentrated_measure >= 0.5: print( "GOOD KEEP CONCENTRATING" ) #a program screaming at you to concentrate should do the trick :) else: print("WHERE IS THE CONCENTRATION??") if relaxed_measure >= 0.5: print("YES RELAX MORE") else: print("NO, START RELAXING") board.stop_stream() board.release_session()