glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if __name__ == '__main__': from experiment import Experiment from state import UntilDone, Wait, Parallel, Serial from keyboard import KeyPress from video import Label exp = Experiment(background_color="#4F33FF") g = Grating(width=250, height=250, contrast=0.1) with UntilDone(): KeyPress() g.update(bottom=exp.screen.center) KeyPress() g = Grating(width=500, height=500, envelope='Gaussian', frequency=75, phase=11.0, color_one='blue', color_two='red', contrast=0.25) with UntilDone(): KeyPress() g.update(bottom=exp.screen.center) KeyPress()
def __init__(self, txt=None, duration=10.0, base_time=None, max_resp=0, parent=None, save_log=True): super(FreeKey, self).__init__(parent=parent, duration=duration, save_log=save_log) # show the initial text if txt is None: txt = Text('??????', parent=self) else: # claim that text self.claim_child(txt) # set self as parent # like using with, but without the indentation self.__enter__() # save the starting text Set('fk_start_text',txt['shown'].text) # set base_time if base_time is None: base_time = txt['first_flip']['time'] # Loop until out of time or press enter Set('fk_end_time', base_time+duration) Set('fk_cur_text','') with Loop(conditional=Get('fk_end_time')>Ref(gfunc=now)) as loop: # accept keyboard response (for remaining duration) kp = KeyPress(keys=asciiplus, base_time=base_time, duration=Get('fk_end_time')-Ref(gfunc=now)) # process the key # if Backspace remove the end if_backspace = If((kp['pressed'] == 'BACKSPACE')) with if_backspace.true_state: # if there's text with If(Get('fk_cur_text')!='').true_state: # delete last char Set('fk_cur_text',Get('fk_cur_text')[:-1]) # update the text Update(txt, 'text', Get('fk_cur_text')) with if_backspace.false_state: # see if Enter if_enter = If((kp['pressed'] == 'ENTER')|(kp['pressed']=='RETURN')) with if_enter.true_state: # is Enter, so log Log(first_key_time=Get('fk_first_time'), enter_key_time=kp['press_time'], response=Get('fk_cur_text')) # start over Set('fk_cur_text','') Update(txt, 'text', Get('fk_start_text')) with if_enter.false_state: # is a new letter, so process it # if first letter, then save the time If(Get('fk_cur_text')=='', Set('fk_first_time',kp['press_time'])) # else normal letter, append it (processing SPACE) If(kp['pressed']=='SPACE', Set('fk_cur_text', Get('fk_cur_text')+' '), Set('fk_cur_text', Get('fk_cur_text')+kp['pressed'])) # update the text Update(txt, 'text', Get('fk_cur_text')) # log if anything was there and not complete If(Get('fk_cur_text')!='', Log(first_key_time=Get('fk_first_time'), enter_key_time=0.0, response=Get('fk_cur_text'))) # remove the text cause we're done Unshow(txt) # pop off self as parent self.__exit__(None,None,None)
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(): g.slide(color='red', duration=4.0) Wait(.25) with Loop(4): MovingDots() with UntilDone(): KeyPress() Debug(rate=g.widget.refresh_rate) exp.run(trace=False)
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) ]