def get_subject_name_id(): """ Get the name (optional) and the initials of the subject """ name_input = xpy.io.TextInput("Subject name - optional:", length=40, message_colour=xpy.misc.constants.C_WHITE) subj_name = name_input.get() if subj_name == "": default_id = "" else: name_elems = subj_name.lower().split(" ") default_id = "".join([e[0] for e in name_elems if len(e) > 0]) id_input = xpy.io.TextInput("Subject ID (initials) - mandatory:", length=10, message_colour=xpy.misc.constants.C_WHITE) while True: subj_id = id_input.get(default_id) #-- Make sure that the subject ID is acceptable in file names and would make a valid #-- struct key in Matlab if re.match("^[A-Za-z_][A-Za-z0-9_]*$", subj_id): break ttrk.log_write("Invalid subject ID ({:})".format(subj_id), True) xpy.io.Keyboard.process_control_keys() return subj_id, subj_name
def trial_failed_common(err, exp_info, trial): """ Called when the trial failed for any reason (only when a strict error occurred; pointing at an incorrect location does not count as failure) :type err: ExperimentError :type exp_info: trajtracker.paradigms.common.BaseExperimentInfo :type trial: trajtracker.paradigms.common.BaseTrialInfo """ ttrk.log_write("ERROR in trial ({:}). Message shown to subject: {:}".format(err.err_code, err.message)) curr_time = u.get_time() trial.duration = curr_time - trial.start_time time_in_session = curr_time - exp_info.session_start_time if not trial.stopped_moving_event_dispatched: exp_info.event_manager.dispatch_event(FINGER_STOPPED_MOVING, trial.duration, time_in_session) trial.stopped_moving_event_dispatched = True exp_info.event_manager.dispatch_event(ttrk.events.TRIAL_FAILED, trial.duration, time_in_session) exp_info.errmsg_textbox.unload() exp_info.errmsg_textbox.text = err.message exp_info.errmsg_textbox.visible = True exp_info.sound_err.play()
def trial_succeeded_common(exp_info, trial): """ Called when the trial succeeded :type exp_info: trajtracker.paradigms.common.BaseExperimentInfo :type trial: trajtracker.paradigms.common.BaseTrialInfo """ ttrk.log_write("Trial ended successfully") curr_time = u.get_time() trial.duration = curr_time - trial.start_time time_in_session = curr_time - exp_info.session_start_time exp_info.event_manager.dispatch_event(ttrk.events.TRIAL_SUCCEEDED, trial.duration, time_in_session)
def run_trials(exp_info, run_one_trial_func, trial_info_class): """ Default implementation of the experiment :type exp_info: trajtrackerp.common.BaseExperimentInfo :param run_one_trial_func: The run_trial() function of the relevant paradigm :param trial_info_class: The TrialInfo class of the relevant paradigm """ init_experiment(exp_info) trial_num = 1 next_trial_already_initiated = False while len(exp_info.trials) > 0: trial_config = exp_info.trials.pop(0) ttrk.log_write("====================== Starting trial #{:} =====================".format(trial_num)) # noinspection PyUnresolvedReferences run_trial_rc = run_one_trial_func(exp_info, trial_info_class(trial_num, trial_config, exp_info.config), next_trial_already_initiated) next_trial_already_initiated = False if run_trial_rc == RunTrialResult.Aborted: print(" Trial aborted.") exp_info.return_unused_trial_to_pool(trial_config) continue trial_num += 1 exp_info.exp_data['nTrialsCompleted'] += 1 if run_trial_rc == RunTrialResult.Succeeded: exp_info.exp_data['nTrialsSucceeded'] += 1 elif run_trial_rc == RunTrialResult.SucceededAndProceed: exp_info.exp_data['nTrialsSucceeded'] += 1 next_trial_already_initiated = True elif run_trial_rc == RunTrialResult.Failed: exp_info.exp_data['nTrialsFailed'] += 1 exp_info.return_unused_trial_to_pool(trial_config) else: raise Exception("Invalid result from run_trial(): {:}".format(run_trial_rc))
def update_fixation_for_trial(exp_info, trial): """ Update the fixation when the trial is initialized :type exp_info: trajtracker.paradigms.common.BaseExperimentInfo :type trial: trajtracker.paradigms.common.BaseTrialInfo """ if exp_info.config.fixation_type == 'text': if 'fixation.text' in trial.csv_data: exp_info.config.fixation_text = trial.csv_data['fixation.text'] create_textbox_fixation(exp_info) print(3,exp_info.fixation.text) if exp_info.fixation.text == '': ttrk.log_write("WARNING: No fixation text was set for trial #{:}".format(trial.trial_num)) update_attr_by_csv_config(exp_info, trial, exp_info.fixation, 'fixation.position', 'position') update_obj_position(exp_info, trial, exp_info.fixation, 'fixation', 'x') update_obj_position(exp_info, trial, exp_info.fixation, 'fixation', 'y')
def _create_textbox_target_impl(exp_info, role): config = exp_info.config target = ttrk.stimuli.MultiTextBox() y, height = exp_info.get_default_target_y() target.position = (config.text_target_x_coord, y) target.text_font = config.text_target_font target.size = (config.text_target_width, int(height)) target.text_colour = config.text_target_colour target.text_justification = config.text_target_justification if not (0 < config.text_target_height <= 1): raise ttrk.ValueError("Invalid config.text_target_height ({:}): value must be between 0 and 1, check out the documentation".format(config.text_target_height)) hsr = u.get_font_height_to_size_ratio(target.text_font) font_size = int(height / hsr * config.text_target_height) target.text_size = font_size ttrk.log_write("{:} font size = {:}, height = {:.1f} pixels".format(role, font_size, font_size*hsr), print_to_console=True) return target
def create_textbox_fixation(exp_info): """ Create a textbox to serve as the fixation. :param exp_info: The experiment-level objects :type exp_info: trajtracker.paradigms.num2pos.ExperimentInfo """ config = exp_info.config y, height = exp_info.get_default_target_y() text = config.fixation_text print(config.text_target_width, int(height)) fixation = xpy.stimuli.TextBox( text='' if text is None else text, size=(config.text_target_width, int(height)), position=(config.text_target_x_coord, y), text_font=config.text_target_font, text_colour=config.text_target_colour, text_justification=config.text_target_justification ) hsr = u.get_font_height_to_size_ratio(fixation.text_font) #font_size = int(height / hsr * config.text_target_height) ####################################################### font_size=20 ###########Make sure this matches the .csv file####### fixation.text_size = font_size ttrk.log_write("Fixation font size = {:}, height = {:.1f} pixels".format(font_size, font_size*hsr), print_to_console=True) fixation.preload() exp_info.fixation = fixation
def _log_write(self, msg, prepend_self=False, print_to_console=False): if prepend_self: msg = type(self).__name__ + "," + msg ttrk.log_write(msg)