def _get(self, url): logging.debug('--> GET: %s' % url) # fire it off res = requests.get(url) if not res.ok: logging.error("xx> %s when getting %s: %s" % (res.status_code, url, res.error)) return [] # decode JSON data = {} try: data = json.loads(res.content) except Exception as e: logging.error("-----\n%s\n-----\n%s\n-----" % (e, res.content)) return [] self.previousPageURI = data.get('previousPageURI') self.nextPageURI = data.get('nextPageURI') if self.nextPageURI: self.nextPageURI = self.nextPageURI.replace( ' ', '+') # some queries come back with a space! self.totalCount = int(data.get('totalCount', 1)) # instantiate Trial objects trials = [] for tr in data.get('results', []): trial = Trial() trial.update_from_lilly(tr) trials.append(trial) return trials
def make_trial_set(self): self.trialSet = [] all_pairs = list( itertools.product(self.sPlusOrientations, self.sMinusOrientations)) for numSMinus in self.sMinusPresentations: for p in all_pairs: (sPlusOrientation, sMinusOrientation) = p if abs(sPlusOrientation - sMinusOrientation) < 0.001: #make sure SPLUS and SMINUS are different continue t = Trial() t.numSMinus = numSMinus t.sPlusOrientation = sPlusOrientation t.sMinusOrientation = sMinusOrientation self.trialSet.append(t) print str(len(self.trialSet)) + " different trial conditions." self.sequencer = Sequencer(self.trialSet, self.sequenceType) if self.sequenceType == Sequences.INTERVAL or self.sequenceType == Sequences.INTERVAL_RETRY: self.sequencer.sequencer.easyOris = self.easyOris self.sequencer.sequencer.hardOris = self.hardOris self.sequencer.sequencer.numEasy = self.numEasy self.sequencer.sequencer.numHard = self.numHard
def _get(self, url): logging.debug('--> GET: %s' % url) # fire it off res = requests.get(url) if not res.ok: logging.error("xx> %s when getting %s: %s" % (res.status_code, url, res.error)) return [] # decode JSON data = {} try: data = json.loads(res.content) except Exception as e: logging.error("-----\n%s\n-----\n%s\n-----" % (e, res.content)) return [] self.previousPageURI = data.get('previousPageURI') self.nextPageURI = data.get('nextPageURI') if self.nextPageURI: self.nextPageURI = self.nextPageURI.replace(' ', '+') # some queries come back with a space! self.totalCount = int(data.get('totalCount', 1)) # instantiate Trial objects trials = [] for tr in data.get('results', []): trial = Trial() trial.update_from_lilly(tr) trials.append(trial) return trials
def move_to_next_trial(self): if self.current_trial_index is None: self.current_trial_index = 0 rospy.logwarn(self.current_trial_index) else: del self.current_trial self.current_trial_index += 1 rospy.logwarn(self.current_trial_index) trial_param = self.get_trial_params(self.current_trial_index) self.current_trial = Trial(self.elapsed_time, self.mean_angle, trial_param, self.devices, self.current_trial_index)
def extract_trials(self): trials = [] all_poke_idx, poke_times, all_withdraw_idx, withdraw_times, all_odor_idx, odor_times = self.get_trial_times( ) # set up parameters self.total_bins_per_trial = int( (self.config.test.end_sec + self.config.train.window_size - self.config.test.start_sec) / self.config.original_bin_width) self.num_bins_per_spike_data = int(self.config.bin_width / self.config.original_bin_width) test_data_start_idx = range( self.total_bins_per_trial)[0::self.num_bins_per_spike_data] # for each trial, create a Trial instance for i in range(len(odor_times)): # get behaviorial data for current trial odor, position, in_seq, performance = self.get_behavior( all_odor_idx[i]) # create instance of class Trial cur_trial = Trial(i + 1, poke_times[i], withdraw_times[i], odor_times[i], odor, position, in_seq, performance) # get start index and end index for a trial if self.config.onset_time: start_idx, end_idx = self.get_start_end_idx( all_odor_idx[i], all_withdraw_idx[i], self.config.original_bin_width, self.config.test.start_sec, self.config.test.end_sec + self.config.train.window_size) else: start_idx, end_idx = self.get_start_end_idx( all_poke_idx[i], all_withdraw_idx[i], self.config.original_bin_width, self.config.test.start_sec, self.config.test.end_sec + self.config.train.window_size) # get neural activity data (spike train and lfp) for current trial all_spikes_data, lfp_data = self.get_spikes_lfp_data( start_idx, end_idx) # save neural spike train data as a nested list cur_trial.spikes['all'] = self.get_test_data( test_data_start_idx, all_spikes_data, self.config.compress_data) trials.append(cur_trial) # save the trials data in a pickle file with open(self.config.test.trials_file, 'wb') as f: pickle.dump(trials, f) return trials
def run_test(upper_len, incr, num_trials, func, is_c, safety_time=2): '''Returns multidimensional array of [[array_size, time],...] upper_len - @type - int - @param - the longest array size that should be tested on incr - @type - int - @param - increment the length of array for every set of trials num_trials - @type - int - @param - how many trials for every array length? func - @type - function - @param - the function to run the trials on ''' trials = [] cur_len = 1 int_bound = int(math.log(sys.maxint, 2)) #is C's longint, actually do_more = True while (cur_len <= upper_len and do_more): for i in range(num_trials): t = Trial() t.set_test_data(True, [], int, cur_len, int_bound) t.run_test(func, is_c) trials.append([cur_len, t.get_elapsed_time()]) e_t = t.get_elapsed_time() # if it took more than safety_time, finish this set and stop if (e_t > safety_time): do_more = False cur_len += incr return trials
def next_task(self): if self.trial.is_end(): if self.sentence_count == 6: self.sentence_count = 3 chunk = self.stimuli.get_chunk(self.sentence_count) if chunk is None: self.current_handler = ExperimentEndHandler(self.update, self.end_callback) return else: self.current_handler = BlockEndHandler(self.update, self.start_sentence_task) self.trial = Trial(chunk, self.stimuli.get_shuffled_groups()) return else: self.sentence_count += 1 self.trial = Trial(self.stimuli.get_chunk(self.sentence_count), self.stimuli.get_shuffled_groups()) self.start_sentence_task()
def rank_image_cb(self, msg): logger.info( 'Received message with {} compressed_imgs, {} imgs, {} strs'. format(len(msg.compressed_imgs), len(msg.imgs), len(msg.strs))) if self.trial and not self.trial.mode in (Trial.State.ABORTED, Trial.State.COMPLETED): print('Aborting due to ongoing trial') self.action_server.set_aborted() return self.reset() if len(msg.compressed_imgs) > 0: self.screen.fill((255, 255, 255)) self.screen.blit( self.font.render( 'Received {} images'.format(len(msg.compressed_imgs)), 1, (0, 0, 0)), (40, self.size[1] / 2)) pygame.display.flip() scaled_imgs = [ ImageConverter.from_ros(img) for img in msg.compressed_imgs ] self.start_trial( Trial(zip(msg.option_ids, scaled_imgs), size=self.size, preview_time=4000, image_time=self.PRESENTATION_DELAY)) while self.ranking: pygame.time.wait(100) else: self.action_server.set_aborted()
def main(): gridSize = 15 bounds = [1, 1, gridSize - 2, gridSize - 2] minDistanceBetweenGrids = 5 condition = [-5, -3, -1, 0, 1, 3, 5] counter = [0] * len(condition) initialWorld = InitialWorld(bounds) updateWorld = UpdateWorld(bounds, condition, counter) pg.init() screenWidth = 680 screenHeight = 680 screen = pg.display.set_mode((screenWidth, screenHeight)) leaveEdgeSpace = 2 lineWidth = 1 backgroundColor = [205, 255, 204] lineColor = [0, 0, 0] targetColor = [255, 50, 50] playerColor = [50, 50, 255] targetRadius = 10 playerRadius = 10 stopwatchUnit = 100 finishTime = 1000 * 30 block = 1 textColorTuple = (255, 50, 50) stopwatchEvent = pg.USEREVENT + 1 pg.time.set_timer(stopwatchEvent, stopwatchUnit) pg.event.set_allowed([pg.KEYDOWN, pg.QUIT, stopwatchEvent]) picturePath = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) + '/Pictures/' resultsPath = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) + '/Results/' experimentValues = co.OrderedDict() experimentValues["name"] = input("Please enter your name:").capitalize() experimentValues["condition"] = 'None' writerPath = resultsPath + experimentValues["name"] + '.csv' writer = WriteDataFrameToCSV(writerPath) introductionImage = pg.image.load(picturePath + 'introduction.png') restImage = pg.image.load(picturePath + 'rest.png') finishImage = pg.image.load(picturePath + 'finish.png') introductionImage = pg.transform.scale(introductionImage, (screenWidth, screenHeight)) finishImage = pg.transform.scale(finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4))) drawBackground = DrawBackground(screen, gridSize, leaveEdgeSpace, backgroundColor, lineColor, lineWidth, textColorTuple) drawNewState = DrawNewState(screen, drawBackground, targetColor, playerColor, targetRadius, playerRadius) drawImage = DrawImage(screen) humanController = HumanController(gridSize, stopwatchEvent, stopwatchUnit, drawNewState, finishTime) trial = Trial(humanController, drawNewState, stopwatchEvent, finishTime) experiment = Experiment(trial, writer, experimentValues, initialWorld, updateWorld, drawImage, resultsPath, minDistanceBetweenGrids) giveExperimentFeedback = GiveExperimentFeedback(screen, textColorTuple, screenWidth, screenHeight) drawImage(introductionImage) score = [0] * block for i in range(block): score[i] = experiment(finishTime) giveExperimentFeedback(i, score) if i == block - 1: drawImage(finishImage) else: drawImage(restImage) participantsScore = np.sum(np.array(score)) print(participantsScore)
def __init__(self, db, update, end_callback, groups=None): self.db = db self.stimuli = Stimuli(groups) self.update = update self.sentence_count = 3 self.trial = Trial(self.stimuli.get_chunk(self.sentence_count), self.stimuli.get_shuffled_groups()) self.start_sentence_task() self.end_callback = end_callback
def all_trials_main(): # roughly 1 day of trails machines = [ # "mlcnetA.cs.wpi.edu", # "mlcnetB.cs.wpi.edu", # "mlcnetC.cs.wpi.edu", "mlcnetD.cs.wpi.edu" ] protocols = [ # "cubic", # "bbr", # "hybla", "pcc" ] dir = 'data/' + date.today().strftime("%Y-%m-%d") if not os.path.exists(dir): os.makedirs(dir) # roughly 24 hours for i in range(80): for machine, protocol in zip(machines, protocols): print(machine, protocol) title = f"{dir}/{machine}_{protocol}_{i}" trial = Trial(name=title, data="1G", remote=machine) trial.remote_tc(cc=protocol) trial.start()
def load_trials(mat, session): if session is None: return [] trials = [] data = mat['dv']['exp'][0, 0] keys = ['dir', 'coh', 'durIdx', 'dur', 'tr', 'response', 'correct'] data = [data[key][0, 0] for key in keys] data = [d.transpose()[0] if d.shape[0] > 1 else d[0] for d in data] for dirc, coh, durIdx, dur, tr, resp, crct in zip(*data): # for tr, coh, dirc, durIdx, dur, resp, crct in mat['D']: trial = Trial(session, tr, coh, dur, durIdx, dirc, resp, True if crct == 1 else False) trials.append(trial) return trials
def parse_trial(trial_path): with open(trial_path,'rb') as input_file: trial_result = json.load(input_file) generation_list = list() for gen in trial_result['generationalData']: individual_list = list() for indi in gen['individualList']: individual_list.append( Individual(indi['UUID'], indi['parentUUID'], indi['switch'], indi['lightFirst'], indi['fitness'])) generation_list.append(Generation(gen['generationNumber'], individual_list)) trial = Trial(trial_result['config'], generation_list) return trial
class TrialHandler(): def __init__(self, db, update, end_callback, groups=None): self.db = db self.stimuli = Stimuli(groups) self.update = update self.sentence_count = 3 self.trial = Trial(self.stimuli.get_chunk(self.sentence_count), self.stimuli.get_shuffled_groups()) self.start_sentence_task() self.end_callback = end_callback def handle_keyboard(self, keycode): self.current_handler.handle_keyboard(keycode) def start_sentence_task(self): self.sentences, self.words = self.trial.get_one_group_data_from_chunk() self.current_handler = SentenceHandler(self.sentences, self.update, self.start_word_task) def start_word_task(self): self.current_handler = WordHandler(self.words, self.update, self.next_task, self.db) def next_task(self): if self.trial.is_end(): if self.sentence_count == 6: self.sentence_count = 3 chunk = self.stimuli.get_chunk(self.sentence_count) if chunk is None: self.current_handler = ExperimentEndHandler(self.update, self.end_callback) return else: self.current_handler = BlockEndHandler(self.update, self.start_sentence_task) self.trial = Trial(chunk, self.stimuli.get_shuffled_groups()) return else: self.sentence_count += 1 self.trial = Trial(self.stimuli.get_chunk(self.sentence_count), self.stimuli.get_shuffled_groups()) self.start_sentence_task()
def get_best_fit(self, n_try, test_size): best_trials = [] for a in self.dh.actions: trials = [] for i in range(n_try): a.split_train_test(test_size) a.scale_dataset() a.offline_train() print a.get_regression_score(), a.gmm.score(a.y_test) trials.append( Trial(a, a.get_regression_score(), a.gmm.score(a.y_test))) # print trials trials.sort(key=lambda t: t.cluster_score, reverse=True) best_trials.append(trials[0]) return best_trials
def main(): dir = 'pcc' if not os.path.exists(dir): os.makedirs(dir) machine = "mlcnetD.cs.wpi.edu" protocol = "pcc" title = f"{dir}/{machine}_{protocol}" trial = Trial(name=title, data="1G", remote=machine) trial.remote_tc(cc=protocol) dir = trial.data_dir() trial.start()
def window_sizes(): dir = 'data/WINDOW_SIZES/' + date.today().strftime("%Y-%m-%d") if not os.path.exists(dir): os.makedirs(dir) # roughly 24 hours wmems = [ int(BDP * 0.5), BDP * 1, BDP * 2, BDP * 4, BDP * 8, BDP * 16, 65625000, ] for wmem in wmems: title = f"{dir}/{wmem}" trial = Trial(name=title, data="1G", remote='mlc1') trial.remote_tc(cc='cubic', win=wmem) trial.local_tc(cc='cubic', win=wmem) trial.start()
def main(batch_size: int = Option(32, "-b"), G_lr: float = Option(0.01, help="Learning rate for generator training"), D_lr: float = Option(0.01, help="Learning rate for discriminator training"), GAN_G_lr: float = Option( 0.00008, help="Learning rate for GAN generator training"), GAN_D_lr: float = Option( 0.00016, help="Learning rate for GAN discriminator training"), G_epoch: int = Option(10, help="Iteration of generator training"), D_epoch: int = Option(3, help="Iteration of discriminator training"), GAN_epoch: int = Option(1, help="Iteration of GAN training"), itr: int = Option(1, help="Iteration of whole NOGAN training"), optim_type: str = Option("ADAB", help="Options of Optimizers for Generator"), level: str = Option("O1", help="FP16 Training Flags, Refer to Apex Docs"), adv_weight: float = Option(1.0, help="Weight for discriminator loss"), load_dir: Optional[Path] = Option(None)): """ NOGAN training Trial. """ torch.backends.cudnn.benchmark = True assert (itr > 0), "Number must be bigger than 0" trial = Trial(batch_size=batch_size, G_lr=G_lr, D_lr=D_lr, optim_type=optim_type, level=level) decreased_lr = False if load_dir is not None: trial.load_trial(load_dir) for _ in range(itr): trial.Generator_NOGAN( epochs=G_epoch, content_weight=3.0, recon_weight=10., loss=['content_loss', 'recon_loss'], ) trial.Discriminator_NOGAN(epochs=D_epoch, adv_weight=adv_weight) trial.GAN_NOGAN(GAN_epoch, GAN_G_lr=GAN_G_lr, GAN_D_lr=GAN_D_lr, adv_weight=adv_weight)
def read_input_file(modo_inputs): input_list = [] if modo_inputs == "prueba": input_file = open("pairAndResInputs.txt", "r") if modo_inputs == "biblia": input_file = open("pairAndResInputsBIBLIA.txt", "r") try: for line in input_file: # rstrip() para evitar que algun \n moleste prime, pairString, res = line.rstrip().split(" ") left, right = pairString.split(",") trial = Trial(prime, int(left), int(right), res) #res es una string porque a veces es una latra input_list.append(trial) input_file.close() except UnboundLocalError as error: print(error) print("\nRecordar debes: es prueba o biblia. Escribir bien debes.\n") sys.exit() return input_list
def __init__(self): self.current_trial = DummyTrial() self.current_trial_index = None rospy.init_node('virtual_desert') self.get_param() self.rate = rospy.Rate(self.param['update_rate']) self.lock = threading.Lock() self.start_time = rospy.get_time() self.angle_lowpass_filter = lowpass_filter.LowpassFilter( self.param['angle_lowpass_fcut']) self.angle_accumulator = angle_utils.AngleAccumulator() self.angle_fixer = angle_utils.AngleFixer() self.devices = {} self.devices['panels_controller'] = display_ctrl.LedController() self.devices['alicat_proxy'] = AlicatProxy() self.devices['autostep_proxy'] = AutostepProxy() self.devices['autostep_tracking_data_pub'] = rospy.Publisher( '/autostep/tracking_data', TrackingData, queue_size=10) self.devices['sunled_proxy'] = BasicLedStripProxy() self.initialize_panels_controller() self.initialize_autostep() self.rolling_circ_mean = RollingCircularMean( self.param['rolling_mean_size']) self.angle_data_sub = rospy.Subscriber('/angle_data', MsgAngleData, self.on_angle_data_callback) self.data_pub = rospy.Publisher('/virtual_desert_data', VirtualDesertData, queue_size=10) self.param_pub = rospy.Publisher('/virtual_desert_param', std_msgs.msg.String, queue_size=10)
def all_trials_main(): # roughly 1 day of trails machines = [ #"mlcnetA.cs.wpi.edu", "mlcnetB.cs.wpi.edu" #"mlcnetC.cs.wpi.edu", # "mlcnetD.cs.wpi.edu" ] protocols = [ #"cubic", "bbr" #"hybla", # "pcc" ] initcwnd = [ # 3, # 5, #10, # 20, # 40, 100, 250 ] dir = 'data/' + date.today().strftime("%Y-%m-%d") if not os.path.exists(dir): os.makedirs(dir) # roughly 24 hours for i in range(5): for j in initcwnd: for machine, protocol in zip(machines, protocols): print(machine, protocol) title = f"{dir}/{j}/{machine}_{protocol}_{i}" # time.sleep(15) trial = Trial(name=title, data="128MB", remote=machine) # trial.mock() trial.remote_tc(cc=protocol, initcwnd=j) trial.start()
# The same for positions positions = [] try: while True: p = input('Enter number of shares to buy (Ctrl+D to end) ') try: positions.append(int(p)) except ValueError as e: print(str(e)) except KeyboardInterrupt: print() sys.exit(1) except EOFError: print() # Continue execution if len(positions) == 0: print('No number of shares specified, exiting') sys.exit(2) output_file = open('result.txt', 'w') for p in positions: trial = Trial(p, num_trial) trial.plot('histogram_%04d_pos.pdf' % p) output_file.write( 'Position %d: Mean %f, STD %f\n' % (p, trial.mean, trial.std) ) output_file.close()
class VirtualDesert(object): Default_Param_File = 'virtual_desert_param.yaml' def __init__(self): self.current_trial = DummyTrial() self.current_trial_index = None rospy.init_node('virtual_desert') self.get_param() self.rate = rospy.Rate(self.param['update_rate']) self.lock = threading.Lock() self.start_time = rospy.get_time() self.angle_lowpass_filter = lowpass_filter.LowpassFilter( self.param['angle_lowpass_fcut']) self.angle_accumulator = angle_utils.AngleAccumulator() self.angle_fixer = angle_utils.AngleFixer() self.devices = {} self.devices['panels_controller'] = display_ctrl.LedController() self.devices['alicat_proxy'] = AlicatProxy() self.devices['autostep_proxy'] = AutostepProxy() self.devices['autostep_tracking_data_pub'] = rospy.Publisher( '/autostep/tracking_data', TrackingData, queue_size=10) self.devices['sunled_proxy'] = BasicLedStripProxy() self.initialize_panels_controller() self.initialize_autostep() self.rolling_circ_mean = RollingCircularMean( self.param['rolling_mean_size']) self.angle_data_sub = rospy.Subscriber('/angle_data', MsgAngleData, self.on_angle_data_callback) self.data_pub = rospy.Publisher('/virtual_desert_data', VirtualDesertData, queue_size=10) self.param_pub = rospy.Publisher('/virtual_desert_param', std_msgs.msg.String, queue_size=10) def initialize_panels_controller(self): # Wait until we have a subscriber connected or else message may be thrown away # It would be better if the panels controller used a service. while self.devices['panels_controller'].pub.get_num_connections() < 1: # May want to put a try counter on this and error out after some number of attempts rospy.sleep(0.1) self.devices['panels_controller'].set_config_id( self.param['panels_config_id']) self.devices['panels_controller'].stop() def shutdown_panels(self): self.devices['panels_controller'].stop() self.devices['panels_controller'].all_off() def initialize_autostep(self): # Set up known starting angle=0 self.devices['autostep_proxy'].disable_tracking_mode() self.devices['autostep_proxy'].set_move_mode('jog') self.devices['autostep_proxy'].move_to(0.0) self.devices['autostep_proxy'].busy_wait() while self.devices['autostep_tracking_data_pub'].get_num_connections( ) < 1: # May want to put a try counter on this and error out after some number of attempts rospy.sleep(0.1) def shutdown_autostep(self): self.devices['autostep_proxy'].disable_tracking_mode() self.devices['autostep_proxy'].soft_stop() def get_param(self): self.param = rospy.get_param('/virtual_desert', None) if self.param is None: param_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), self.Default_Param_File) with open(param_file_path, 'r') as f: self.param = yaml.load(f) @property def angle_dt(self): return 1.0 / self.param['angle_framerate'] @property def elapsed_time(self): return rospy.get_time() - self.start_time @property def mean_angle(self): with self.lock: mean_angle = self.rolling_circ_mean.value() return mean_angle @property def angle(self): with self.lock: angle = self.angle_lowpass_filter.value return angle def on_angle_data_callback(self, data): with self.lock: self.rolling_circ_mean.insert_data(data.angle) angle_unwrapped = self.angle_accumulator.update(data.angle) angle_fixed = self.angle_fixer.fix_data(angle_unwrapped) self.angle_lowpass_filter.update(angle_fixed, self.angle_dt) def move_to_next_trial(self): if self.current_trial_index is None: self.current_trial_index = 0 rospy.logwarn(self.current_trial_index) else: del self.current_trial self.current_trial_index += 1 rospy.logwarn(self.current_trial_index) trial_param = self.get_trial_params(self.current_trial_index) self.current_trial = Trial(self.elapsed_time, self.mean_angle, trial_param, self.devices, self.current_trial_index) def get_trial_params(self, index): """ Get parameters for trial set flow parameters to default and override where specified in trial """ trial_param = copy.deepcopy(self.param['trials'][index]) param_name_list = ['flow', 'panels', 'sunled', 'autostep'] for param_name in param_name_list: try: param = copy.deepcopy( self.param['{}_default'.format(param_name)]) except KeyError: param = {} if param is None: param = {} param.update(trial_param[param_name]) trial_param[param_name] = param return trial_param def run(self): # Set to true to make sure parameters are in bag file if self.param['wait_for_param_sub']: while self.param_pub.get_num_connections() < 1: rospy.sleep(0.1) self.param_pub.publish(json.dumps(self.param)) while not rospy.is_shutdown(): elapsed_time = self.elapsed_time if elapsed_time > self.param['startup_delay']: if self.current_trial.is_done(elapsed_time): try: self.move_to_next_trial() except IndexError: break # Done with trials -> exit loop trial_msg = self.current_trial.update(elapsed_time, self.angle) #print('t: {:0.2f}'.format(self.elapsed_time)) else: print('pretrial delay, t={:0.2f}'.format(elapsed_time)) trial_msg = TrialData() msg = VirtualDesertData() msg.header.stamp = rospy.Time.now() msg.angle = self.angle msg.elapsed_time = elapsed_time msg.current_trial_index = self.current_trial_index msg.trial_data = trial_msg self.data_pub.publish(msg) self.rate.sleep() self.shutdown_panels() self.shutdown_autostep()
def _get_trials_data(self, session_nr, raw_spike_timestamps): """ This method use the original experiment stimuli data set to extract the order and categories of the stimuli shown to each subjects. The label of the stimulus for the new old recognition task is also obtained using this method. :param session_nr: session number :param raw_spike_timestamps: raw_spike_timestamps from cell :return: a list of all trial objects """ # Get data from variant stimuli data set block_id_recog = self.sessions[session_nr]['block_id_recog'] block_id_learn = self.sessions[session_nr]['block_id_learn'] variant = self.sessions[session_nr]['variant'] filename = '' if variant == 1: filename = 'NewOldDelay_v3.mat' filename2 = 'NewOldDelayStimuli.mat' elif variant == 2: filename = 'NewOldDelay2_v3.mat' filename2 = 'NewOldDelayStimuli2.mat' elif variant == 3: filename = 'NewOldDelay3_v3.mat' filename2 = 'NewOldDelayStimuli3.mat' path_to_labels = os.path.join(self.path, 'stimFiles', filename) experiment_stimuli = loadmat(path_to_labels)['experimentStimuli'] stimuli_recog_list = experiment_stimuli[0, block_id_recog - 1][3] new_old_recog_list = experiment_stimuli[0, block_id_recog - 1][4] stimuli_learn_list = experiment_stimuli[0, block_id_learn - 1][2] path_to_categories = os.path.join(self.path, 'stimFiles', filename2) category_mat = loadmat(path_to_categories) category_names = category_mat['categories'] category_mapping = pd.DataFrame(category_mat['categoryMapping']).set_index(0) file_mapping = category_mat['fileMapping'] # Get events and raw cell data events_recog = self._get_event_data(session_nr, experiment_type='recog') recog_events_time_points = self._extract_event_periods(events_recog) events_learn = self._get_event_data(session_nr, experiment_type='learn') learn_events_time_points = self._extract_event_periods(events_learn) # Get recog response from log file experiment_id_recog = self.sessions[session_nr]['experiment_id_recog'] log_file_path = os.path.join(self._construct_data_path(session_nr, 'events'), 'newold'+str(experiment_id_recog)+'.txt') log_file = pd.read_csv(log_file_path, sep=';', header=None) log_events = np.asarray(log_file[1]) recog_responses = log_events[(log_events >= 31)*(log_events <= 36)] - 30 # Get learn response from log file experiment_id_learn = self.sessions[session_nr]['experiment_id_learn'] log_file_path = os.path.join(self._construct_data_path(session_nr, 'events'), 'newold'+str(experiment_id_learn)+'.txt') log_file = pd.read_csv(log_file_path, sep=';', header=None) log_events = np.asarray(log_file[1]) learn_responses = log_events[(log_events >= 20) * (log_events <= 21)] - 20 # Create the trial list trials = [] for i in range(0, 100): # Processing the recog phase baseline_offset = 1000000 trial_start = recog_events_time_points['stimulus_on'][i] - baseline_offset trial_end = recog_events_time_points['trial_end'][i] trial_duration = trial_end - trial_start trial_timestamps_recog = raw_spike_timestamps[(raw_spike_timestamps > trial_start) * (raw_spike_timestamps <= trial_end)] - trial_start stimuli_recog_id = stimuli_recog_list[0][i] category_recog = category_mapping.loc[stimuli_recog_id, 1] category_name_recog = category_names[0, category_recog - 1][0] file_path = file_mapping[0][i][0].replace('C:\code\\', '') new_old_recog = new_old_recog_list[0][i] response_recog = recog_responses[i] # Processing the learning phase trial_start = learn_events_time_points['stimulus_on'][i] - baseline_offset trial_end = learn_events_time_points['trial_end'][i] trial_duration = trial_end - trial_start trial_timestamps_learn = raw_spike_timestamps[(raw_spike_timestamps > trial_start) * (raw_spike_timestamps <= trial_end)] - trial_start stimuli_learn_id = stimuli_learn_list[0][i] category_learn = category_mapping.loc[stimuli_learn_id, 1] category_name_learn = category_names[0, category_learn - 1][0] response_learn = learn_responses[i] # Creating this trial object trial = Trial(category_recog, category_name_recog, new_old_recog, response_recog, category_learn, category_name_learn, response_learn, file_path, stimuli_recog_id, trial_timestamps_recog, trial_timestamps_learn) trials.append(trial) return trials
# The same for positions positions = [] try: while True: p = input('Enter number of shares to buy (Ctrl+D to end) ') try: positions.append(int(p)) except ValueError as e: print(str(e)) except KeyboardInterrupt: print() sys.exit(1) except EOFError: print() # Continue execution if len(positions) == 0: print('No number of shares specified, exiting') sys.exit(2) output_file = open('result.txt', 'w') for p in positions: trial = Trial(p, num_trial) trial.plot('histogram_%04d_pos.pdf' % p) output_file.write('Position %d: Mean %f, STD %f\n' % (p, trial.mean, trial.std)) output_file.close()
def Instruct(self, config, lang="E"): inst = Func(get_instructions, config).result # with If(practice==True): with Parallel(): if not config.TOUCH: MouseCursor(blocking=False) with Serial(blocking=False): with Parallel(): toplbl = Label(text=inst['top_text'], markup=True, halign='center', center_x=self.exp.screen.center_x, center_y=self.exp.screen.center_y, font_size=s(config.INST_FONT_SIZE)) # upper left Flanker(center_x=self.exp.screen.center_x - s(400), center_y=toplbl.top + s(125), sep=s(config.CONFIG_SEP), df=s(config.CONFIG_DF), line_width=s(config.LW), stim="__<__\n_<<<_\n<<<<<\n_<<<_\n__<__\n") # upper middle Flanker(center_x=self.exp.screen.center_x, center_y=toplbl.top + s(125), sep=s(config.CONFIG_SEP), df=s(config.CONFIG_DF), line_width=s(config.LW), stim="__>__\n_><>_\n><<<>\n_><>_\n__>__\n") # upper right Flanker(center_x=self.exp.screen.center_x + s(400), center_y=toplbl.top + s(125), sep=s(config.CONFIG_SEP), df=s(config.CONFIG_DF), line_width=s(config.LW), stim="__<__\n_<><_\n<><><\n_<><_\n__<__\n") # lower left Flanker(center_x=self.exp.screen.center_x - s(400), center_y=toplbl.bottom - s(125), sep=s(config.CONFIG_SEP), df=s(config.CONFIG_DF), line_width=s(config.LW), stim="__<__\n_<><_\n<><><\n_<><_\n__<__\n") # lower middle Flanker(center_x=self.exp.screen.center_x, center_y=toplbl.bottom - s(125), sep=s(config.CONFIG_SEP), df=s(config.CONFIG_DF), line_width=s(config.LW), stim="__>__\n_><>_\n><><>\n_><>_\n__>__\n") # lower right Flanker(center_x=self.exp.screen.center_x + s(400), center_y=toplbl.bottom - s(125), sep=s(config.CONFIG_SEP), df=s(config.CONFIG_DF), line_width=s(config.LW), stim="__<__\n_<><_\n<>>><\n_<><_\n__<__\n") with UntilDone(): Wait(until=toplbl.appear_time) GetResponse(keys=config.CONT_KEY) Wait(1.0) with Loop( [["__<__\n_<<<_\n<<<<<\n_<<<_\n__<__\n", config.RESP_KEYS[0]], ["__>__\n_><>_\n><<<>\n_><>_\n__>__\n", config.RESP_KEYS[0]], ["__<__\n_<><_\n<>>><\n_<><_\n__<__\n", config.RESP_KEYS[-1]], ["__>__\n_><>_\n><><>\n_><>_\n__>__\n", config.RESP_KEYS[-1]]]) as prac_ev: Wait(1.0) p2 = Trial(config, stim=prac_ev.current[0], center_x=self.exp.screen.center_x, center_y=self.exp.screen.center_y, correct_resp=prac_ev.current[1], condition='+') with If(p2.correct): # They got it right Label(text=u"\u2713", color='green', duration=config.FEEDBACK_TIME, font_size=s(config.FEEDBACK_FONT_SIZE), font_name='DejaVuSans.ttf') with Else(): # they got it wrong Label(text=u"\u2717", color='red', font_size=s(config.FEEDBACK_FONT_SIZE), duration=config.FEEDBACK_TIME, font_name='DejaVuSans.ttf') with Meanwhile(): with Parallel(): Ellipse(color='red', size=(s(55), s(55))) Ellipse(color=config.INNER_CIRCLE_COLOR, size=(s(45), s(45))) Label(text=inst['inst_1'], markup=True, halign='center', center_x=self.exp.screen.center_x, center_y=self.exp.screen.center_y + s(300), font_size=s(config.INST_FONT_SIZE)) Wait(1.0) Label(text=inst['inst_2'], markup=True, halign='center', center_x=self.exp.screen.center_x, center_y=self.exp.screen.center_y + s(300), font_size=s(config.INST_FONT_SIZE)) with UntilDone(): GetResponse(keys=config.CONT_KEY) with Loop([[ "__<__\n_<><_\n<>>><\n_<><_\n__<__\n", config.RESP_KEYS[1], cos(radians((360. / config.NUM_LOCS) * 5)), sin(radians((360. / config.NUM_LOCS) * 5)) ], [ "__<__\n_<><_\n<><><\n_<><_\n__<__\n", config.RESP_KEYS[0], cos(radians((360. / config.NUM_LOCS) * 1)), sin(radians((360. / config.NUM_LOCS) * 1)) ], [ "__>__\n_><>_\n><><>\n_><>_\n__>__\n", config.RESP_KEYS[1], cos(radians((360. / config.NUM_LOCS) * 3.)), sin(radians((360. / config.NUM_LOCS) * 3.)) ], [ "__>__\n_><>_\n><><>\n_><>_\n__>__\n", config.RESP_KEYS[1], cos(radians((360. / config.NUM_LOCS) * 4.)), sin(radians((360. / config.NUM_LOCS) * 4.)) ], [ "__>__\n_>>>_\n>>>>>\n_>>>_\n__>__\n", config.RESP_KEYS[1], cos(radians((360. / config.NUM_LOCS) * 0)), sin(radians((360. / config.NUM_LOCS) * 0)) ], [ "__>__\n_><>_\n><<<>\n_><>_\n__>__\n", config.RESP_KEYS[0], cos(radians((360. / config.NUM_LOCS) * 2)), sin(radians((360. / config.NUM_LOCS) * 2)) ]]) as prac_ev: Wait(1.0) with Parallel(): Ellipse(color='red', size=(s(55), s(55)), center_x=self.exp.screen.center_x + prac_ev.current[2] * s(config.FROM_CENTER), center_y=self.exp.screen.center_y + prac_ev.current[3] * s(config.FROM_CENTER), blocking=False) Ellipse(color=config.INNER_CIRCLE_COLOR, size=(s(45), s(45)), center_x=self.exp.screen.center_x + prac_ev.current[2] * s(config.FROM_CENTER), center_y=self.exp.screen.center_y + prac_ev.current[3] * s(config.FROM_CENTER), blocking=False) p4 = Trial(config, stim=prac_ev.current[0], center_x=self.exp.screen.center_x + prac_ev.current[2] * s(config.FROM_CENTER), center_y=self.exp.screen.center_y + prac_ev.current[3] * s(config.FROM_CENTER), correct_resp=prac_ev.current[1], condition='+') with If(p4.correct): # They got it right Label(text=u"\u2713", color='green', duration=config.FEEDBACK_TIME, font_size=s(config.FEEDBACK_FONT_SIZE), font_name='DejaVuSans.ttf') with Else(): # they got it wrong Label(text=u"\u2717", color='red', font_size=s(config.FEEDBACK_FONT_SIZE), duration=config.FEEDBACK_TIME, font_name='DejaVuSans.ttf') Label(text=inst['to_cont'], halign='center', center_x=self.exp.screen.center_x, center_y=self.exp.screen.center_y, font_size=s(config.INST_FONT_SIZE)) with UntilDone(): GetResponse(keys=config.CONT_KEY) Wait(1.0) Label(text=inst['inst_3'], markup=True, halign='center', center_x=self.exp.screen.center_x, center_y=self.exp.screen.center_y + s(200), font_size=s(config.INST_FONT_SIZE)) with UntilDone(): GetResponse(keys=config.CONT_KEY) with Loop([[ "__<__\n_<><_\n<>>><\n_<><_\n__<__\n", config.RESP_KEYS[1], cos(radians((360. / config.NUM_LOCS) * 5)), sin(radians((360. / config.NUM_LOCS) * 5)) ], [ "__<__\n_<><_\n<><><\n_<><_\n__<__\n", config.RESP_KEYS[0], cos(radians((360. / config.NUM_LOCS) * 1)), sin(radians((360. / config.NUM_LOCS) * 1)) ], [ "__>__\n_><>_\n><><>\n_><>_\n__>__", config.RESP_KEYS[1], cos(radians((360. / config.NUM_LOCS) * 3)), sin(radians((360. / config.NUM_LOCS) * 3)) ], [ "__>__\n_>>>_\n>>>>>\n_>>>_\n__>__\n", config.RESP_KEYS[1], cos(radians((360. / config.NUM_LOCS) * 6)), sin(radians((360. / config.NUM_LOCS) * 6)) ], [ "__>__\n_><>_\n><<<>\n_><>_\n__>__\n", config.RESP_KEYS[0], cos(radians((360. / config.NUM_LOCS) * 2)), sin(radians((360. / config.NUM_LOCS) * 2)) ]]) as prac_ev: Wait(1.0) p5 = Trial(config, stim=prac_ev.current[0], center_x=self.exp.screen.center_x + prac_ev.current[2] * s(config.FROM_CENTER), center_y=self.exp.screen.center_y + prac_ev.current[3] * s(config.FROM_CENTER), correct_resp=prac_ev.current[1], condition='+') with If(p5.correct): # They got it right Label(text=u"\u2713", color='green', duration=config.FEEDBACK_TIME, font_size=s(config.FEEDBACK_FONT_SIZE), center_y=self.exp.screen.center_y + s(50), font_name='DejaVuSans.ttf') with Else(): # they got it wrong Label(text=u"\u2717", color='red', font_size=s(config.FEEDBACK_FONT_SIZE), center_y=self.exp.screen.center_y + s(50), duration=config.FEEDBACK_TIME, font_name='DejaVuSans.ttf') with Meanwhile(): Label(text="+", font_size=s(config.ORIENT_FONT_SIZE)) with Serial(blocking=False): with ButtonPress(): Button(text=inst['skip_text'], right=self.exp.screen.width, bottom=0, width=s(config.SKIP_SIZE[0]), height=s(config.SKIP_SIZE[1]), blocking=False, font_size=s(config.SKIP_FONT_SIZE)) Wait(1.0) Label(text=inst['done'], halign='center', center_x=self.exp.screen.center_x, center_y=self.exp.screen.center_y, font_size=s(config.INST_FONT_SIZE)) with UntilDone(): GetResponse(keys=config.CONT_KEY)
def __init__(self, expparadigm, config, phrases, args): # TODO: add a 'question' argument debug("__init__") Trial.__init__(self, config, args['experiment'], args['item'], args['condition'], args['mode'], phrases, len(phrases)+1, question=None) if expparadigm == "MR-SAT": self.paradigm = "MR" elif expparadigm == "SAT": self.paradigm = "SR" else: raise("Invalid experiment paradigm.") mode = args['mode'] if not args.has_key('speededAcceptability'): args['speededAcceptability'] = False #if not args.has_key('lastFeedbackOffset'): # args['lastFeedbackOffset'] = 0 self.args = args self.displayPhrase = None self.lastPhraseRead = None self.lastResponseCorrect = None # create phrase chunks masked_phrases = [] text_phrases = [] space = Text(text=" ", color=(0.0,0.0,0.0), position=(0,0), font_size=self.config.sprStimulusSize, anchor='left', on=0, font_name=self.config.font) space_size = space.parameters.size[0] # set initial word coordinates phrase_positions = [] if mode == CPresentationMode.MovingWindow: x_pos = self.config.xPositionStart y_pos = globalScreen.size[1]/2 anchor = "left" elif mode == CPresentationMode.Centered: x_pos = globalScreen.size[0]/2 y_pos = globalScreen.size[1]/2 anchor = "center" # create phrases self.sentence = "" for i in range(0, len(phrases)): if phrases[i] == "$": self.phraseSignalReference = i continue self.sentence = self.sentence + " " + phrases[i] text = Text(text = phrases[i], color = (0.0,0.0,0.0), position = (x_pos, y_pos), font_size = self.config.sprStimulusSize, anchor=anchor, on=0, font_name=self.config.font) if mode == CPresentationMode.MovingWindow: phrase_positions.append( (x_pos,y_pos) ) x_pos = x_pos + text.parameters.size[0] + space_size text_phrases.append(text) # TODO: Fix y coordinates too. Necessary once the stimulus # becomes longer than one line. assert( hasattr(self, "phraseSignalReference")) self.sentence.strip() # create masks if the mode is moving window if mode == CPresentationMode.MovingWindow: for i in range(0, len(phrases)): text_size = text_phrases[i].parameters.size mask_size = (text_size[0], config.sprMaskHeight ) mask_position = [ phrase_positions[i][0], phrase_positions[i][1] ] mask_position[1] = mask_position[1]-(text_size[1]/2-mask_size[1]/2) phrase_mask = Target2D(color=(0.0,0.0,0.0), position=mask_position, anchor='left', on=1, size=mask_size) masked_phrases.append(phrase_mask) self.screen = TrialScreenSAT(config, text_phrases, masked_phrases, mode, self.args['signalsCnt'], self.args['feedbackIndicators'], self.args['mappingIndicators']) if not self.args['speededAcceptability']: self.responses = [False]*self.args['signalsCnt'] else: self.responses = [False] self._directionBeforeSignal = None self._directionAfterSignal = None
def do_experiment(self, targets, nontargets, slug): t = Trial(list(enumerate(targets + nontargets)), size=self.size, preview_time=4000, image_time=self.PRESENTATION_DELAY) self.start_trial(t) accumulated_raw_results = [] while self.running: self.clock.tick(self.FRAMERATE) for event in pygame.event.get(): if event.type == pygame.QUIT: self.reset() return elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.reset() return elif event.key == pygame.K_SPACE: self.reset() t = Trial(list(enumerate(targets + nontargets)), size=self.size, preview_time=4000, image_time=self.PRESENTATION_DELAY) self.start_trial(t) elif event.type == self.EVENT_ID: if self.trial: pygame.time.set_timer( self.EVENT_ID, self.trial.show_next_image(self.screen, self.bci)) pygame.display.flip() if self.trial.mode == Trial.State.COMPLETED: self.bci and self.bci.end_block() results = None print('Trial completed') logger.info('completed trial') results = self.trial.process_results( self.screen, self.bci) if results: self.action_server.set_succeeded(results) else: print('Trial insufficient') pygame.display.flip() logger.info('raw results:') raw_results = sorted(self.trial.option_results, key=lambda x: x.idx) accumulated_raw_results.append(raw_results) with open('data/{}.pickle'.format(slug), 'w') as f: pickle.dump(accumulated_raw_results, f) logger.info('\n'.join(map(str, raw_results))) logger.info('processed results:') logger.info(results) self.ranking = False self.trial = None print('press space to run again') elif self.trial.mode == Trial.State.ABORTED: print('Trial aborted') self.action_server.set_aborted() self.ranking = False self.trial = None self.bci and self.bci.end_block() self.reset()
from trial import Trial, MyException import json trials = [] with open('../data.json') as json_data: d = json.load(json_data) for obj in d["data"]: try: t = Trial(obj) except MyException: continue t.clean() trials.append(t) done = [] done_obj = [] for t in trials: if t.participant_id not in done: done_obj.append(t) done.append(t.participant_id)
def __init__(self, expparadigm, config, phrases, args): # TODO: add a 'question' argument debug("__init__") Trial.__init__(self, config, args['experiment'], args['item'], args['condition'], args['mode'], phrases, len(phrases) + 1, question=None) if expparadigm == "MR-SAT": self.paradigm = "MR" elif expparadigm == "SAT": self.paradigm = "SR" else: raise ("Invalid experiment paradigm.") mode = args['mode'] if not args.has_key('speededAcceptability'): args['speededAcceptability'] = False #if not args.has_key('lastFeedbackOffset'): # args['lastFeedbackOffset'] = 0 self.args = args self.displayPhrase = None self.lastPhraseRead = None self.lastResponseCorrect = None # create phrase chunks masked_phrases = [] text_phrases = [] space = Text(text=" ", color=(0.0, 0.0, 0.0), position=(0, 0), font_size=self.config.sprStimulusSize, anchor='left', on=0, font_name=self.config.font) space_size = space.parameters.size[0] # set initial word coordinates phrase_positions = [] if mode == CPresentationMode.MovingWindow: x_pos = self.config.xPositionStart y_pos = globalScreen.size[1] / 2 anchor = "left" elif mode == CPresentationMode.Centered: x_pos = globalScreen.size[0] / 2 y_pos = globalScreen.size[1] / 2 anchor = "center" # create phrases self.sentence = "" for i in range(0, len(phrases)): if phrases[i] == "$": self.phraseSignalReference = i continue self.sentence = self.sentence + " " + phrases[i] text = Text(text=phrases[i], color=(0.0, 0.0, 0.0), position=(x_pos, y_pos), font_size=self.config.sprStimulusSize, anchor=anchor, on=0, font_name=self.config.font) if mode == CPresentationMode.MovingWindow: phrase_positions.append((x_pos, y_pos)) x_pos = x_pos + text.parameters.size[0] + space_size text_phrases.append(text) # TODO: Fix y coordinates too. Necessary once the stimulus # becomes longer than one line. assert (hasattr(self, "phraseSignalReference")) self.sentence.strip() # create masks if the mode is moving window if mode == CPresentationMode.MovingWindow: for i in range(0, len(phrases)): text_size = text_phrases[i].parameters.size mask_size = (text_size[0], config.sprMaskHeight) mask_position = [ phrase_positions[i][0], phrase_positions[i][1] ] mask_position[1] = mask_position[1] - (text_size[1] / 2 - mask_size[1] / 2) phrase_mask = Target2D(color=(0.0, 0.0, 0.0), position=mask_position, anchor='left', on=1, size=mask_size) masked_phrases.append(phrase_mask) self.screen = TrialScreenSAT(config, text_phrases, masked_phrases, mode, self.args['signalsCnt'], self.args['feedbackIndicators'], self.args['mappingIndicators']) if not self.args['speededAcceptability']: self.responses = [False] * self.args['signalsCnt'] else: self.responses = [False] self._directionBeforeSignal = None self._directionAfterSignal = None
path = args.restore_path full_path = gen_full_path(subdir, path, unique=False) init_logger(full_path, args.debug) vae, agent, environment, params = restore_experiment( full_path, env_class, args) trigger = params['env_params'].get('trigger') if (trigger == 'None') or (trigger == ''): trigger = None if trigger is not None: trigger_class = importlib.import_module(subdir + ".triggers") trigger = trigger_class.__dict__[trigger]( params['env_params']['trigger_step']) train = not params['test'] trial = Trial(full_path, vae, agent, environment, trigger) try: update_step = params['concurrent_batches'] * params['train_step'] trial.run(params['num_steps'], params['summary_step'], update_step, train, params['record']) except Exception as e: logging.exception("Exception in trial.") finally: try: trial.agent.lookup.table.shutdown() trial.model.exit_signal.set() except AttributeError: pass try: trial.vae.sess.close() except: