def timerEvent(self, event): if not self.training.run(): self.killTimer(self.timer_id) #training BCI model bci = BCI() bci.train(self.training.name) bci.saveModel("bci.m") bci.plotPSD()
def __init__(self, model_file_name=None): super(Server, self).__init__() #init UI self.initUI() #init server self.initServer('0.0.0.0',45451) #init Mindwave Mobile self.mindwave = MindwaveMobile(self.macaddress,self.srate*self.len_buf) self.timer_id=self.startTimer(33) self.mu = QMutex() #init BCI module if model_file_name: self.bci=BCI() self.bci.loadModel(model_file_name) else: self.bci=None
f"secondary_volume_filter: {secondary_volume_filter}, " f"max_allocation: {max_allocation}, " f"running_avg_volume_period: {running_avg_volume_period}, " f"candidates: {candidates}" f"primary_candidate: {primary_candidate}, " f"secondary_candidate: {secondary_candidate}, " f"offset: {offset}") try: bci = BCI(index_size=index, rebalancing_period=rebalancing, primary_usd_filtering=primary_volume_filter, secondary_usd_filtering=secondary_volume_filter, max_asset_allocation=max_allocation, fee=0.02, running_avg_volume_period=running_avg_volume_period, index_candidate_size=candidates, primary_candidate_size=primary_candidate, secondary_candidate_size=secondary_candidate, initial_funds=1000, offset=offset, start_dt=start_dt, end_dt=end_dt) # use previously calculated data to save initialization time if data_by_coin is None: bci.set_input_data(input_data) else: bci.data = data bci.dates = dates bci.data_by_coin = data_by_coin
f"max_allocation: {max_allocation}, " f"running_avg_volume_period: {running_avg_volume_period}, " f"primary_candidate: {primary_candidate}, " f"offset: {offset}") try: bci = BCI( index_size=index, rebalancing_period=rebalancing, primary_usd_filtering= primary_volume_filter, secondary_usd_filtering= secondary_volume_filter, max_asset_allocation=max_allocation, fee=FEE, running_avg_volume_period= running_avg_volume_period, index_candidate_size=index * 2, primary_candidate_size=min( primary_candidate, index), secondary_candidate_size=min( primary_candidate, index) + 5, initial_funds=1000, offset=offset, start_dt=start_dt, end_dt=end_dt) # use previously calculated data to save initialization time if data_by_coin is None: bci.set_input_data(input_data) else: bci.data = data
PygameButton(game_display, left_arm_g, (0, 0, 255), font, " C", (255, 255, 255)) PygameButton(game_display, right_arm_g, (0, 0, 255), font, " O", (255, 255, 255)) finish_button = PygameButton(game_display, finish_button_position, (255, 0, 0), font, "Finish", (255, 255, 255)) pygame.display.update() crashed = False # directory = max(glob.glob('*'), key=os.path.getctime) # path to last modified folder directory = 'Your model directory name' # hard coded path to trained model with open(directory + '/Data.pickle', 'rb') as handle: rows_per_epoch = pickle.load(handle)['X'].shape[0] with open(directory + '/Model.pickle', 'rb') as handle: clf = pickle.load(handle) # clf.zero_thresh = 0.5 # this classifier parameter can be changed during control stage bci = BCI() prp = BatyaGGPreprocessor(rows_per_epoch) recent_data = prp.fit_test(bci.get_recent_data(rows_per_epoch)) current_class = clf.predict(recent_data) control_updater = ControlUpdater(['x', 'y', 'z', 'g']) current_control = control_updater.get_next() ur = UR() ur.go_home() mixer.music.load('x_axis.mp3') mixer.music.play() while not crashed: if current_class == 3: current_control = control_updater.get_next() if current_control == 'x': mixer.music.load('x_axis.mp3') mixer.music.play()
class Server(QWidget): len_buf=100# The length of buffer in seconds srate=512# The sample rate of Mindwave Mobile macaddress = '20:68:9D:B8:C9:28'# MAC address of Mindwave Mobile def __init__(self, model_file_name=None): super(Server, self).__init__() #init UI self.initUI() #init server self.initServer('0.0.0.0',45451) #init Mindwave Mobile self.mindwave = MindwaveMobile(self.macaddress,self.srate*self.len_buf) self.timer_id=self.startTimer(33) self.mu = QMutex() #init BCI module if model_file_name: self.bci=BCI() self.bci.loadModel(model_file_name) else: self.bci=None def closeEvent(self, event): print 'Server is closed...' def initServer(self, ip, port): self.clients=[] self.server=QTcpServer(self) self.server.listen(QHostAddress(ip),port) self.server.newConnection.connect(self.acceptConnection) def initUI(self): self.connect_state=QLabel('Disconnected: 0', self) self.connect_state.setStyleSheet("QLabel { color: red; font-size: 14pt; font-family: 'Times New Roman';}"); hbox = QHBoxLayout() hbox.setAlignment(Qt.AlignCenter) hbox.addWidget(self.connect_state) self.setLayout(hbox) self.setGeometry(100,100,100,20) self.setWindowTitle('Server') self.show() def acceptConnection(self): new_client = self.server.nextPendingConnection() print new_client," is connected..." self.clients.append(new_client) new_client.disconnected.connect(self.closeClient) new_client.readyRead.connect(self.readRequest) self.connect_state.setText('Connected: %d'%(len(self.clients))) self.connect_state.setStyleSheet("QLabel { color: green; font-size: 14pt; font-family: 'Times New Roman';}"); def readRequest(self): client=self.sender() request=client.readLine()[:-1].split(',') if request[0] == 'raw': n_sample = int(request[1]) data = self.formatData(n_sample) client.write(data) elif request[0] == 'command': if self.bci: self.mu.lock() try: poor_signal = self.mindwave.poor_signal except: poor_signal = 255 if len(self.mindwave.raw_values)>=self.bci.window*self.bci.downsample: #windowing recent EEG eeg = self.mindwave.raw_values[-int(self.bci.window*self.bci.downsample)::self.bci.downsample] self.mu.unlock() c=self.bci.predict(eeg) client.write("{0}, {1}\n".format(int(c[0]),poor_signal)) else: self.mu.unlock() client.write("0, {0}\n".format(poor_signal)) else: client.write("-1, 255\n") else: pass def closeClient(self): closed=self.sender() print closed," client is closed..." self.clients.remove(closed) if len(self.clients)!=0: self.connect_state.setText('Connected: %d'%(len(self.clients))) else: self.connect_state.setText('Disconnected: 0') self.connect_state.setStyleSheet("QLabel { color: red; font-size: 14pt; font-family: 'Times New Roman';}"); def timerEvent(self,event): self.mu.lock() self.mindwave.update() self.mu.unlock() def formatData(self, n_samples): # make datastream from the buffer # n_samples: the number of samples requested by clients self.mu.lock() data=list(self.mindwave.raw_values[-n_samples:]) self.mu.unlock() format=str(data)[1:-1]+'\n' return format
legs_position = (0.45*width, 0.6*height, 0.1*width, 0.1*height) pause_button_position = (0.05*width, 0.9*height, 0.1*width, 0.05*height) finish_button_position = (0.85*width, 0.9*height, 0.1*width, 0.05*height) font = pygame.font.SysFont('Comic Sans MS', 30) PygameButton(game_display, left_arm_position, (0, 0, 255), font, "Left Hand", (255, 255, 255)) PygameButton(game_display, right_arm_position, (0, 0, 255), font, "Right Hand", (255, 255, 255)) PygameButton(game_display, legs_position, (0, 0, 255), font, "Legs", (255, 255, 255)) pause_button = PygameButton(game_display, pause_button_position, (255, 0, 0), font, "Pause", (255, 255, 255)) finish_button = PygameButton(game_display, finish_button_position, (255, 0, 0), font, "Finish", (255, 255, 255)) crashed = False # home = False class_updater = ClassUpdater(4) current_class = class_updater.get_next() window_duration = 1000 bci = BCI() start_time = time.time() while not crashed: if current_class == 0: PygameButton(game_display, left_arm_position, (0, 0, 255), font, "Left Hand", (255, 255, 255)) PygameButton(game_display, right_arm_position, (0, 0, 255), font, "Right Hand", (255, 255, 255)) PygameButton(game_display, legs_position, (0, 0, 255), font, "Legs", (255, 255, 255)) elif current_class == 1: PygameButton(game_display, left_arm_position, (0, 255, 0), font, "Left Hand", (255, 255, 255)) PygameButton(game_display, right_arm_position, (0, 0, 255), font, "Right Hand", (255, 255, 255)) PygameButton(game_display, legs_position, (0, 0, 255), font, "Legs", (255, 255, 255)) elif current_class == 2: PygameButton(game_display, left_arm_position, (0, 0, 255), font, "Left Hand", (255, 255, 255)) PygameButton(game_display, right_arm_position, (0, 255, 0), font, "Right Hand", (255, 255, 255)) PygameButton(game_display, legs_position, (0, 0, 255), font, "Legs", (255, 255, 255))
return vars(parser.parse_args()) if __name__ == "__main__": LOG.info("Bitpanda Crypto Index Simulator") args = parse_args() bci = BCI( index_size = args['index'], rebalancing_period = args['rebalancing'], primary_usd_filtering = args['primary_volume_filter'], secondary_usd_filtering = args['secondary_volume_filter'], max_asset_allocation = args['max_allocation'], fee = args['fee'], running_avg_volume_period = args['volume_period'], index_candidate_size = args['candidates'], primary_candidate_size = args['primary_candidates'], secondary_candidate_size = args['secondary_candidates'], initial_funds = args['funds'], offset = args['offset'], bypass_validation = args['bypass_validation'], input_file_name = args['input_file'], start_dt = args['start_date'], end_dt = args['end_date'], show_graph = args['show_graph'], save_graph = args['save_graph'] ) bci.run()