def run_exp(): #control.run_test_suite() #control.set_develop_mode(True) # init control.defaults.initialize_delay = 0 exp = design.Experiment(background_colour=(0, 0, 0)) control.initialize(exp) # stimuli stimulus = expyriment.stimuli.Rectangle((1000, 1000)) blank = expyriment.stimuli.BlankScreen() # stimuli preload stimulus.preload() blank.preload() # main loop mean = 0 for k in range(1, 1000): t = stimulus.present() t += blank.present() mean += t print(t, mean / k) exp.clock.wait(500) control.end()
def close_display(experiment): """See openexp._canvas.legacy""" control.end()
exp.add_block(exp.blocks[0].copy()) # IO trigger = exp.keyboard # For USB (keyboad emulation) trigger #trigger = io.TriggerInput(io.ParallelPort(SCAN_TRIGGER_LTP_ADDRESS)) # For LTP trigger # RUN control.start() stimuli.TextLine("Waiting for trigger...").present() trigger.wait(SCAN_TRIGGER) # Initial scanner sync exp.clock.reset_stopwatch() for block in exp.blocks: start = exp.clock.stopwatch_time block.trials[0].stimuli[1].play() exp.clock.wait((BLOCK_LENGTH * TR - TR/2) * 1000 - \ block.trials[0].stimuli[0].present(), function=exp.keyboard.check) protocol.add_event(block.get_factor("Condition"), start, exp.clock.stopwatch_time) trigger.wait(SCAN_TRIGGER) # Sync to scanner for next block start if not os.path.isdir("protocols"): os.mkdir("protocols") protocol.export_to_brainvoyager( exp.name, "protocols"+os.path.sep+exp.name+"_"+"S"+repr(exp.subject).zfill(2)) control.end()
use_mouse = True) ### start ### control.start(exp) stimuli.TextScreen(heading= "Numerosity Production Task", text="Use mouse wheel or up/down keys to change numerosity.\n" + "Press mouse button or ENTER to accept estimation.").present() exp.keyboard.wait() exp.data_variable_names = ["estimation", "latency"] while True: # make numerosity production estimation estim, rt = npt.estimation(max_number = 100, start_value = 0, scroll_out_direction = "E") # save data exp.data.add([estim, rt]) stimuli.TextScreen(heading="The estimation was: {0}".format(estim), text = "Another trial (y/n)").present() key, _ = exp.keyboard.wait([ord('y'), ord('n')]) if key == ord('n'): break control.end(goodbye_text="bye bye", goodbye_delay=5)
ss, llamt, lldel = itc_stair(kval, trial) # collect behavior button, rt = response_device.wait_char(['f','j']) # present ITI screen fixcross.present() exp.clock.wait(random.randint(300,500)) # adjust the k estimate if 'f' in button: kval = kval + step ll = 0 elif 'j' in button: kval = kval - step ll = 1 # keep track of k values kvals = numpy.concatenate((kvals, numpy.array([kval]))) # decrease step size if a k is revisited within 5 consequetive trials if trial > 4 and len(numpy.unique(kvals[-5:]))<=4: step = step *.95 # add data to file exp.data.add([trial, round(kval,3), ss[0], ss[1], llamt, lldel, ll, rt]) trial = trial + 1 # End Experiment control.end(goodbye_text=None, goodbye_delay=None, fast_quit=None) #execfile('runFitK.py',)
''' Custom settings ''' # switch off opengl to avoid screen refesh sync control.defaults.open_gl = False # Fast open close and windowed; default: False #control.set_develop_mode(True) control.defaults.initialize_delay = 0 ''' Prepare experiment ''' exp = design.Experiment(name="MusicTraining") control.initialize(exp) musicsheet = MusicSheet(screen_size=exp.screen.size) control.start(subject_id=45) stimuli.BlankScreen().present(clear=True, update=False) musicsheet.Field.present(clear=False, update=True) # Wait for button press key, rt = exp.keyboard.wait(constants.K_ALL_LETTERS) control.end(goodbye_text='Thats it', goodbye_delay=1000)
""" A very short example experiment in 16 lines of pure code. Participants have to indicate the parity of digits by pressing the left arrow key for odd and the right arrow key for even numbers. """ from expyriment import control, stimuli, design, misc digit_list = [1, 2, 3, 4, 6, 7, 8, 9] * 12 design.randomize.shuffle_list(digit_list) exp = control.initialize() exp.data_variable_names = ["digit", "btn", "rt", "error"] control.start(exp) for digit in digit_list: target = stimuli.TextLine(text=str(digit), text_size=80) exp.clock.wait(500 - stimuli.FixCross().present() - target.preload()) target.present() button, rt = exp.keyboard.wait([misc.constants.K_LEFT, misc.constants.K_RIGHT]) error = (button == misc.constants.K_LEFT) == digit%2 if error: stimuli.Tone(duration=200, frequency=2000).play() exp.data.add([digit, button, rt, int(error)]) exp.clock.wait(1000 - stimuli.BlankScreen().present() - target.unload()) control.end(goodbye_text="Thank you very much...", goodbye_delay=2000)
exp.swap_blocks(0, 1) # Run the actual experiment for block in exp.blocks: # Show instruction screen if block.get_factor("mapping") == "left_odd": instruction = "Press LEFT arrow key for ODD\n" + \ "and RIGHT arrow key for EVEN numbers." else: instruction = "Press RIGHT arrow key for ODD\n" + \ "and LEFT arrow key for EVEN numbers." stimuli.TextScreen("Indicate the parity of the numbers", instruction + "\n\nPress space bar to start training.").present() exp.keyboard.wait(constants.K_SPACE) #training trials for cnt in range(0, no_training_trials): trial = block.get_random_trial() run_trial(-1 * (1 + cnt), trial) #training trails has negative trial numbers # Show instruction screen stimuli.TextScreen("Attention!", instruction + "\n\nThe experimental block starts now.").present() exp.keyboard.wait(constants.K_SPACE) # experimental trials for cnt, trial in enumerate(block.trials): run_trial(cnt, trial) ####### END EXPERIMENT ######## control.end(goodbye_text="Thank you very much for participating in our experiment", goodbye_delay=5000)
def close_display(experiment): control.end()
def runtimeerror(exp, text): stimuli.TextScreen("ERROR", text).present() exp.keyboard.wait() control.end() udp.send(rc.Command.QUIT) exit()
def start(remote_control, ask_filename, device_ids, sensor_names, calibration_folder, device_name_prefix="Dev", write_Fx = True, write_Fy = True, write_Fz = True, write_Tx = False, write_Ty = False, write_Tz = False, write_trigger1 = True, write_trigger2 = False, zip_data=False, reverse_scaling = None): """start gui remote_control should be None (ask) or True or False reverse scaling: dictionary with rescaling (see SensorSetting) key: device_id, value: list of parameter names (e.g., ["Fx"]) returns False only if quited by key while waiting for remote control """ if not isinstance(device_ids, (list, tuple)): device_ids = [device_ids] if not isinstance(sensor_names, (list, tuple)): sensor_names = [sensor_names] timer = Timer() sensors = [] for d_id, sn in zip(device_ids, sensor_names): try: reverse_parameter_names = reverse_scaling[d_id] except: reverse_parameter_names = [] sensors.append(SensorSettings(device_id = d_id, device_name_prefix=device_name_prefix, sensor_name = sn, sync_timer=timer, calibration_folder=calibration_folder, reverse_parameter_names=reverse_parameter_names, rate = config.sampling_rate)) # expyriment control.defaults.initialize_delay = 0 control.defaults.pause_key = None control.defaults.window_mode = True control.defaults.window_size = (1000, 700) control.defaults.fast_quit = True control.defaults.open_gl = False control.defaults.event_logging = 0 exp = design.Experiment(text_font=config.window_font) exp.set_log_level(0) filename = "output.csv" remote_control = initialize(exp, remote_control=remote_control) logo_text_line("Initializing Force Recording").present() recorder = DataRecorder(sensors, timer=timer, poll_udp_connection=True, write_deviceid = len(device_ids)>1, write_Fx = write_Fx, write_Fy = write_Fy, write_Fz = write_Fz, write_Tx = write_Tx, write_Ty = write_Ty, write_Tz = write_Tz, write_trigger1= write_trigger1, write_trigger2= write_trigger2) sleep(0.2) # wait for lib init recorder.determine_biases(n_samples=500) if remote_control: logo_text_line("Waiting to connect (my IP: {0})".format( recorder.udp.ip_address)).present() while not recorder.udp.event_is_connected.is_set(): key = exp.keyboard.check(check_for_control_keys=False) if key == misc.constants.K_q or key == misc.constants.K_ESCAPE: recorder.quit() control.end() return False sleep(0.01)# logo_text_line("Wait for filename").present() while True: try: x = recorder.udp.receive_queue.get_nowait() x = x.string except: x = None if x is not None and x.startswith(RcCmd.FILENAME): filename = x.replace(RcCmd.FILENAME, "") break exp.keyboard.check() sleep(0.01) else: if ask_filename: bkg = logo_text_line("") filename = io.TextInput("Filename", background_stimulus=bkg).get() filename = filename.replace(" ", "_") recorder.open_data_file(filename, directory="data", zipped=zip_data, time_stamp_filename=False, comment_line="") main_loop(exp, recorder=recorder, remote_control=remote_control) recorder.quit() control.end() return True
def end(): if trakstar is not None: #logo_text_line(text="Closing trakSTAR").present() trakstar.close_data_file() trakstar.close() control.end("Quitting Pytrak",goodbye_delay=0, fast_quit=True)
s = stimuli.TextLine(sentence,text_bold=True,text_size=25,\ text_colour=(255,255,255),position=(0, 100)) t.set_factor("sentence", sentence) t.add_stimulus(s) block.add_trial(t) exp.add_block(block) exp.add_data_variable_names(["sentence", "agreement"]) #Starting control.start() instructions_1.present() kb.wait(misc.constants.K_SPACE) instructions_2.present() kb.wait(misc.constants.K_SPACE) for trial in exp.blocks[0].trials: BB = io.TouchScreenButtonBox(list_img) BB.create() BB.add_stimulus(trial.stimuli[0]) BB.show() mouse.show_cursor() button, rt = BB.wait() agreement = get_resp(button) exp.data.add([trial.get_factor("sentence"), agreement]) exp.screen.clear() exp.screen.update() exp.clock.wait(1500) #Ending control.end()