def DynamicDotBox(self, duration=None, update_interval=jitter(1 / 20., (1 / 10.) - (1 / 20.)), **dotbox_args): """Display random dots that update at an interval. Parameters ---------- duration : float or None Duration to show the random dots. update_interval : float How often to update the random dots. Default is to jitter between 10 and 20 Hz. dotbox_args : kwargs See the DotBox for any kwargs options to control the DotBox Note: You can access the dotbox via the `db` attribute of the subroutine. Examples -------- Display a dynamic dot box with 40 dots for 3 seconds: :: DynamicDotBox(size=(500, 500), num_dots=40, duration=3.0) Display two dynamic dot boxes side-by-side until a key press: :: with Parallel(): ddb1 = DynamicDotBox(center_x=exp.screen.center_x-200, num_dots=40, size=(400, 400)) ddb2 = DynamicDotBox(center_x=exp.screen.center_x+200, num_dots=80, size=(400, 400)) with UntilDone(): kp = KeyPress() Log(appear_time=ddb1.db.appear_time) """ # show the dotbox with Parallel(): db = DotBox(duration=duration, **dotbox_args) self.db = db with Meanwhile(): # redraw the dots self.start_dots = db.num_dots with Loop() as l: Wait(duration=update_interval) # hack to make 1.8 work with If((l.i % 2) == 0): self.ndots = self.start_dots + .01 with Else(): self.ndots = self.start_dots #db.update(save_log=False, **dotbox_args) db.update(save_log=False, num_dots=self.ndots)
def __init__(self, vstate, duration=1.0, parent=None, save_log=True): super(Show, self).__init__(parent=parent, duration=duration, save_log=save_log) # remove vstate from parent if it exists self.claim_child(vstate) # add the wait and unshow states self._show_state = vstate self._wait_state = Wait(duration, parent=self) self._unshow_state = Unshow(vstate, parent=self) # expose the shown self.shown = vstate['shown'] # save the show and hide times self.show_time = Ref(self._show_state,'first_flip') self.unshow_time = Ref(self._unshow_state,'first_flip') # append times to log self.log_attrs.extend(['show_time','unshow_time'])
def JitteredPulses(self, code=1, width=0.010, port=0, pause_between=3.0, jitter_between=3.0, sync_style="parallel"): """Send pulses separated by a jittered wait. The typical use case for this subroutine is to send a random train of pulses during an EEG experiment to allow for subsequent synchronization of the EEG data with the behavioral data. This would be accomplished by calling JitteredPulses within a Meanwhile as the next state following the instantiation of the Experiment: exp = Experiment() with Meanwhile(): JitteredPulses() """ # loop indefinitely with Loop(): # send a pulse pulse = Pulse(code=code, port=port, width=width, sync_style=sync_style) Done(pulse) # do a jittered wait Wait(duration=pause_between, jitter=jitter_between) # Log the pulse to a pulse-specific log Log(name='pulse', pulse_on=pulse.pulse_on, pulse_code=pulse.code, pulse_off=pulse.pulse_off, pulse_port=pulse.port, pulse_width=pulse.width)
envelope='Circular', std_dev=7.5, contrast=0.75, color_one='green', color_two='orange') lbl = Label(text='Grating!', bottom=g.top) with UntilDone(): # kp = KeyPress() with Parallel(): g.slide(phase=-8 * math.pi, frequency=10., bottom=exp.screen.bottom, duration=6.) g.slide(rotate=90, duration=2.0) with Serial(): Wait(2.0) lbl.slide(top=g.bottom, duration=4.) with Parallel(): g = Grating(width=1000, height=1000, frequency=10, envelope='Linear', std_dev=20, contrast=0.4, color_one='blue', color_two='red') lbl = Label(text='Grating!', bottom=g.top) with UntilDone(): kp = KeyPress() with Parallel():
from experiment import Experiment, Get, Set, Log from state import Wait, Func, Loop def print_dt(state, *args): print args, state.dt exp = Experiment() Func(print_dt, args=['Mouse Press Test']) Set('last_pressed','') with Loop(conditional=(Get('last_pressed')!='RIGHT')): kp = MousePress(buttons=['LEFT','RIGHT'], correct_resp='RIGHT') Func(print_dt, args=[kp['pressed'],kp['rt'],kp['correct']]) Set('last_pressed',kp['pressed']) Log(pressed=kp['pressed'], rt=kp['rt']) kp = MousePress(buttons=['LEFT','RIGHT'], correct_resp='RIGHT') Func(print_dt, args=[kp['pressed'],kp['rt'],kp['correct']]) Wait(1.0) kp = MousePress() Func(print_dt, args=[kp['pressed'],kp['rt'],kp['correct']]) Wait(1.0) kp = MousePress(duration=2.0) Func(print_dt, args=[kp['pressed'],kp['rt'],kp['correct']]) Wait(1.0, stay_active=True) exp.run()
exp = Experiment() # Initialize the outlet OUTLET = init_lsl_outlet(server_name='MarkerStream', server_type='Markers', nchans=1, suggested_freq=500, channel_format='int32', unique_id='SMILE_LSL_OUT') # Signal the beginning of the experiment. LSLPush(server=OUTLET, val=55) # Wait for the experiment to start! Wait(2.) with Parallel(): Label(text="We will now push 10 markers.", blocking=False) with Loop(10, blocking=False): # Create the push state push_out = LSLPush(server=OUTLET, val=111) # Log like this if you want. #Log(name="MAKERS", # push_time=push_out.push_time) Wait(1.)
chan_path='Dev1/ao1', chan_des="mychan2") NIPulse( task1, push_vals=[1.0], width=0.10, ) NIPulse( task2, push_vals=[.5], width=0.10, ) # Wait for the experiment to start! Wait(2.) with Parallel(): Label(text="We will now push 10 markers.", blocking=False) with Loop(10, blocking=False): ni1 = NIPulse( task1, push_vals=[1.0], width=0.10, ) Wait(1.0) ni2 = NIPulse( task2, push_vals=[.5],
"key": keycode[1].upper(), "state": "up" }) if __name__ == '__main__': from experiment import Experiment from state import Wait, Debug, Loop, UntilDone, Log, Meanwhile exp = Experiment() with Meanwhile(): KeyRecord(name="record_all_key_presses") Debug(name='Press T+G+D or SHIFT+Q+R') Wait(until=((Key("T") & Key("G") & Key("D")) | (Key("SHIFT") & Key("Q") & Key("R")))) Debug(name='Key Press Test') exp.last_pressed = '' with Loop(conditional=(exp.last_pressed != 'K')): kp = KeyPress(keys=['J', 'K'], correct_resp='K') Debug(pressed=kp.pressed, rt=kp.rt, correct=kp.correct) exp.last_pressed = kp.pressed Log(pressed=kp.pressed, rt=kp.rt) KeyRecord() with UntilDone(): kp = KeyPress(keys=['J', 'K'], correct_resp='K') Debug(pressed=kp.pressed, rt=kp.rt, correct=kp.correct) Wait(1.0)
pulse_off=pulse.pulse_off, pulse_port=pulse.port, pulse_width=pulse.width) if __name__ == '__main__': from experiment import Experiment from state import Meanwhile, Debug # set up default experiment exp = Experiment() # test running pulses whilst the rest of the experiment is going with Meanwhile(): with Loop(): pulse = Pulse(code='S1') Wait(duration=1.0, jitter=1.0) Log(name='pulse', pulse_on=pulse.pulse_on, pulse_code=pulse.code, pulse_off=pulse.pulse_off) # First wait for a bit and send some pulses Wait(10) # print something Debug(width=exp.screen.width, height=exp.screen.height) # run the exp exp.run(trace=False)
def unshow(self): # custom unshow so that the widget doesn't run when not onscreen super(MovingDots, self).unshow() self._widget.stop() if __name__ == '__main__': from experiment import Experiment from state import UntilDone, Meanwhile, Wait, Loop, Debug from keyboard import KeyPress exp = Experiment(background_color=("purple", .3)) Wait(.5) g = MovingDots(radius=300, scale=10, num_dots=4, motion_props=[{"coherence": 0.25, "direction": 0, "direction_variance": 0}, {"coherence": 0.25, "direction": 90, "direction_variance": 0}, {"coherence": 0.25, "direction": 180, "direction_variance": 0}, {"coherence": 0.25, "direction": 270, "direction_variance": 0}]) with UntilDone(): KeyPress() with Meanwhile():
"direction_variance": 0, "speed": 50, "lifespan": .6, "lifespan_variance": .5 }, { "coherence": 0.1, "direction": 180, "direction_variance": 10, "speed": 400, "speed_variance": 200 }, ]] exp = Experiment(background_color=("purple", .3)) Wait(.5) g = MovingDots(radius=300, scale=10, num_dots=4, motion_props=[{ "coherence": 0.25, "direction": 0, "direction_variance": 0 }, { "coherence": 0.25, "direction": 90, "direction_variance": 0 }, { "coherence": 0.25, "direction": 180,
from experiment import Experiment from state import Wait, Debug, Loop, Meanwhile, Record, Log, Parallel def print_dt(state, *args): print(args) exp = Experiment() with Meanwhile(): #Record(pos=MousePos(), button=MouseButton()) with Parallel(): MouseRecord() MouseCursor() Wait(2.0) MouseCursor("face-smile.png", (125, 125), duration=5.0) Debug(name='Mouse Press Test') exp.last_pressed = '' with Loop(conditional=(exp.last_pressed != 'RIGHT')): kp = MousePress(buttons=['LEFT', 'RIGHT'], correct_resp='RIGHT') Debug(pressed=kp.pressed, rt=kp.rt, correct=kp.correct) exp.last_pressed = kp.pressed Log(pressed=kp.pressed, rt=kp.rt) kp = MousePress(buttons=['LEFT', 'RIGHT'], correct_resp='RIGHT') Debug(pressed=kp.pressed, rt=kp.rt, correct=kp.correct) Wait(1.0)
self.__rec = None def cancel(self, cancel_time): super(RecordSoundFile, self).cancel(cancel_time) clock.unschedule(self._stop_recording) clock.schedule(self._stop_recording, event_time=self._end_time) if __name__ == '__main__': from experiment import Experiment from state import Parallel, Wait, Serial, Meanwhile, UntilDone, Loop exp = Experiment() Wait(1.0) Beep(freq=[440, 500, 600], volume=0.1, duration=1.0) Beep(freq=880, volume=0.1, duration=1.0) with Parallel(): Beep(freq=440, volume=0.1, duration=2.0) with Serial(): Wait(1.0) Beep(freq=880, volume=0.1, duration=2.0) Wait(1.0) with Meanwhile(): Beep(freq=500, volume=0.1) Beep(freq=900, volume=0.1, duration=1.0) SoundFile("test_sound.wav") SoundFile("test_sound.wav", stop=1.0) Wait(1.0) SoundFile("test_sound.wav", loop=True, duration=3.0)
for child in self.__parallel._children[1:]: child._blocking = False self.__buttons.extend(iter_nested_buttons(self.__parallel)) self.__parallel = None return ret if __name__ == '__main__': from experiment import Experiment from state import Wait, Loop, Parallel, Meanwhile, UntilDone, Serial from math import sin, cos from contextlib import nested exp = Experiment(background_color="#330000") Wait(2.0) with Parallel(): slider = Slider(min=exp.screen.left, max=exp.screen.right, duration=5.0) rect = Rectangle(color="purple", width=50, height=50, center_top=exp.screen.left_top, duration=5.0) with Meanwhile(): rect.animate(center_x=lambda t, initial: slider.value) ti = TextInput(text="EDIT!", duration=5.0) Wait(until=ti.text)
def FreeKey(self, lbl, max_duration=10.0, max_resp=100, base_time=None): """ Perform free recall typed responses. Parameters ---------- lbl : Label state The text that will appear on the screen to indicate to the participant that they are to type a response. This text will disappear from the screen when a response is begun and return when ready for the next response. It's a good idea to use something like a label with '???????'. max_duration : {10.0, float} The amount of time in seconds that the participant is given to respond. max_resp : {100, int} Maximum number of responses that the participant is allowed to enter. base_time : float Manually set a time reference for the start of the state. This will be used to calculate reaction times. Example -------- FreeKey(Label('???????'), max_duration=15.0) The message '??????' will appear on the screen, and participants will be given 15 seconds to enter a response, replacing that text. They can enter as many responses as possible in the 15 second time period. Log Parameters --------------- All parameters above and below are available to be accessed and manipulated within the experiment code, and will be automatically recorded in the state.yaml and state.csv files. Refer to State class docstring for additional logged parameters. responses : list List of typed responses, each with the following information: first_key_time, first_key_rt, enter_key_time, enter_key_rt, response, response_num. The time is the actual time, the rt is the time since the base_time. """ # I'd like the lbl to be up until the below is done. How? # is it just that I would cancel it at the end here? #lbl = Label(text=txt, font_size=40) self.claim_child(lbl) with UntilDone(): # container for responses self.responses = [] # info for each response self.fk_num_resp = 0 self.fk_first_key_time = 0 self.fk_first_key_rt = 0 self.fk_cur_resp = '' # save the starting text and base time self.fk_start_text = lbl.text # handle starting values self.max_duration = max_duration self.max_resp = max_resp # handle the base time with If(base_time): # use the passed value self.base_time = base_time with Else(): # make sure it's available #Debug(fk_on_screen=lbl.on_screen) Wait(until=lbl.on_screen) #Debug(fk_on_screen=lbl.on_screen) # use the label's appear time self.base_time = lbl.appear_time['time'] # reset timing to the desired base_time ResetClock(self.base_time) # collect responses for the desired max_duration or max_resp with Loop(): # accept a key response, time is based on label's ontime kp = KeyPress(keys=asciiplus, base_time=self.base_time) # process the key with If(kp.pressed == 'BACKSPACE'): # if there is text, remove a char with If(self.fk_cur_resp != ''): self.fk_cur_resp = self.fk_cur_resp[:-1] lbl.text = self.fk_cur_resp with Elif(kp.pressed == 'ENTER'): # if there is text, log as a response # increment the response counter self.fk_num_resp += 1 # append the response to the list self.responses += [ Ref(dict, response=self.fk_cur_resp, response_num=self.fk_num_resp, first_key_time=self.fk_first_key_time, first_key_rt=self.fk_first_key_rt, enter_key_time=kp.press_time, enter_key_rt=kp.rt) ] # set starting text back and reset text self.fk_cur_resp = '' with If(self.fk_num_resp < self.max_resp): # gonna keep going lbl.text = self.fk_start_text with Else(): # new key, so append it # if it's first key, save the time with If(self.fk_cur_resp == ''): self.fk_first_key_rt = kp.rt self.fk_first_key_time = kp.press_time # append the text with If(kp.pressed == 'SPACEBAR'): # handle the space self.fk_cur_resp += ' ' with Else(): # just append the letter self.fk_cur_resp += kp.pressed # update the label lbl.text = self.fk_cur_resp with UntilDone(): Wait(max_duration, until=(self.fk_num_resp >= max_resp)) # ran out of time, see if there is an unfinished response with If(self.fk_cur_resp != ''): # there is something, so log it, too # increment the response counter self.fk_num_resp += 1 # append the response to the list, but with no Enter key time self.responses += [ Ref(dict, response=self.fk_cur_resp, response_num=self.fk_num_resp, first_key_time=self.fk_first_key_time, first_key_rt=self.fk_first_key_rt, enter_key_time=None, enter_key_rt=None) ]
response=self.fk_cur_resp, response_num=self.fk_num_resp, first_key_time=self.fk_first_key_time, first_key_rt=self.fk_first_key_rt, enter_key_time=None, enter_key_rt=None) ] if __name__ == '__main__': from experiment import Experiment from state import Wait, Debug from video import Label exp = Experiment() Wait(.5) fk = FreeKey(Label(text='XXXXXX', font_size=40), max_resp=1) Debug(responses=fk.responses) Label(text='Done', font_size=32, duration=2.0) fk2 = FreeKey(Label(text='??????', font_size=30)) Debug(responses=fk2.responses) Wait(1.0) exp.run()