Exemplo n.º 1
0
def addTriggerImage(win, all_stim, image_file):
    img_path = getImageFilePath(image_file)
    if img_path:
        trig_img_key = image_file[:-4]
        if trig_img_key not in all_stim["triggers"]:
            all_stim["triggers"][trig_img_key] = visual.ImageStim(win, image=img_path, units="norm", pos=(0.8, -0.8))
            print("Created TRIG: {} for image file: {}".format(trig_img_key, img_path))
    else:
        print("ERROR: Trigger Image File not found: ", img_path)
Exemplo n.º 2
0
def addTriggerImage(win, all_stim, image_file):
    img_path = getImageFilePath(image_file)
    if img_path:
        trig_img_key = image_file[:-4]
        if trig_img_key not in all_stim['triggers']:
            all_stim['triggers'][trig_img_key] = visual.ImageStim(
                win, image=img_path, units='norm', pos=(0.8, -0.8))
            print("Created TRIG: {} for image file: {}".format(
                trig_img_key, img_path))
    else:
        print("ERROR: Trigger Image File not found: ", img_path)
Exemplo n.º 3
0
def runTrialState(state_code, trial, myWin, io, all_stim):
    # Init time related DVs for trial to 0
    trial['DV_%s_START'%(state_code)] = 0
    trial['DV_%s_VIS_ONSET'%(state_code)] = 0.0
    trial['DV_%s_SND_ONSET'%(state_code)] = 0
    trial['DV_%s_END'%(state_code)] = 0.0

    state_screen = trial['%s_VIS'%(state_code)]
    if state_screen:
        state_screen=u''+str(state_screen).lower().strip()
        if state_screen == '.':
            state_screen = None

    graphics_pos = trial['%s_VPOS'%(state_code)]
    if graphics_pos:
        if graphics_pos == '.':
            graphics_pos = [0,0]
        if isinstance(graphics_pos, (list, tuple)):
            # reset it to a str for storage in hdf5 file
            trial['%s_VPOS'%(state_code)]=str(graphics_pos)
        else:
            graphics_pos = [0,0]

    state_audio = trial['%s_SND'%(state_code)]
    if state_audio:
        state_audio=u''+str(state_audio).strip()
        if state_audio == '.':
            state_audio = None

    state_end_trig = StateTrigger.create(state_code,trial, all_stim['triggers'])

    show_pen = draw_pen_pos = draw_pen_trace = trial['%s_SHOWPEN'%(state_code)]
    if show_pen:
        draw_pen_pos = show_pen.upper().find('P')>=0
        draw_pen_trace = show_pen.upper().find('T')>=0
        clear_existing_traces =  show_pen.upper().find('C')>=0

    possible_state_stim = all_stim['trial']['%s'%(state_code)]
    actual_state_stim = []

    if state_screen:

        if isImageFileCandidate(state_screen):
            img_stim = possible_state_stim['image']
            img_stim.image = getImageFilePath(state_screen)
            img_stim.pos = graphics_pos
            actual_state_stim.append(img_stim)
        else:
            txt_stim = possible_state_stim['text']
            txt_stim.text = state_screen
            txt_stim.pos = graphics_pos
            actual_state_stim.append(txt_stim)

    if state_audio:
        if state_audio.lower().endswith('.wav'):
            possible_state_stim['sound'].setFile(getAudioFilePath(state_audio))
        else:
            # TODO: Write warning to log file that .wav audio is only supported
            state_audio = None

    if clear_existing_traces:
        all_stim['pen']['traces'].clear()

    # Display any state graphics and play any associated audio file
    if actual_state_stim:
        for stim in actual_state_stim:
            stim.draw()

    if state_audio:
        possible_state_stim['sound'].start()

    # If state has a visual stim, use it's flip time to equal the states
    # start time.
    if actual_state_stim:
        if draw_pen_trace:
            all_stim['pen']['traces'].draw()
        trial['DV_%s_START'%(state_code)] = trial['DV_%s_VIS_ONSET'%(state_code)] = myWin.flip()

    if state_audio:
        while not possible_state_stim['sound'].starttime:
            time.sleep(0.001)
        trial['DV_%s_SND_ONSET'%(state_code)] = possible_state_stim['sound'].starttime
        if trial['DV_%s_START'%(state_code)] == 0:
            # If state has an audio stim, but did not have a visual stim,
            # use the audio stim onset time to equal the states start time.
            trial['DV_%s_START'%(state_code)] = trial['DV_%s_SND_ONSET'%(state_code)]

    # If state had no audio and no visual stim, then use
    # current time as the states start time.
    if trial['DV_%s_START'%(state_code)] == 0:
        trial['DV_%s_START'%(state_code)] = core.getTime()

    io.clearEvents()

    state_end_trig.reset()

    # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    # Maintain current state for [state_code]_DUR seconds, or until
    # audio_stim has completed playing, whichever comes last.
    sample_evts=[]
    while (state_audio and possible_state_stim['sound'].status == PLAYING) or \
            state_end_trig.fired(sample_evts) is False:

        # Redraw display and flip
        for stim in actual_state_stim:
            stim.draw()

        if draw_pen_pos or draw_pen_trace:
            sample_evts = tablet.getSamples()

            if draw_pen_trace:
                # Redraw stim based on tablet events
                all_stim['pen']['traces'].updateFromEvents(sample_evts)
                all_stim['pen']['traces'].draw()

            if draw_pen_pos and sample_evts:
                last_evt = sample_evts[-1]
                all_stim['pen']['pos'].updateFromEvent(last_evt)
                all_stim['pen']['pos'].draw()

        flip_time=myWin.flip()

        if terminateExperimentKeyPressed(keyboard):
            if state_audio:
                possible_state_stim['sound'].closeFile()
            return False

    if state_audio:
        possible_state_stim['sound'].closeFile()

    trial['DV_%s_END'%(state_code)] = core.getTime()
Exemplo n.º 4
0
def createPsychopyGraphics(display):
    #
    # Initialize Graphics
    #

    display_resolution = display.getPixelResolution()
    psychopy_monitor = display.getPsychopyMonitorName()
    unit_type = display.getCoordinateType()
    screen_index = display.getIndex()

    # Create a psychopy window, full screen resolution, full screen mode.
    myWin = visual.Window(
        display_resolution,
        monitor=psychopy_monitor,
        units=unit_type,
        color=DEFAULT_SCREEN_COLOR,
        colorSpace="rgb255",
        fullscr=True,
        allowGUI=False,
        screen=screen_index,
    )

    all_stim = dict()

    all_stim["pen"] = {
        "pos": psychopy.iohub.client.wintabtablet.PenPositionStim(myWin),
        "traces": psychopy.iohub.client.wintabtablet.PenTracesStim(myWin),
    }

    all_stim["triggers"] = dict()
    all_stim["triggers"]["naechster"] = visual.ImageStim(
        myWin, image=getImageFilePath("naechster.bmp"), units="norm", pos=(0.8, -0.8)
    )

    all_stim["practice"] = dict()
    prac_text_stim = visual.TextStim(
        myWin,
        units="norm",
        pos=(0, 0.9),
        height=DEFAULT_TEXT_STIM_HEIGHT,
        text="Practice using the tablet pen on this screen.\n" "Tap the 'Continue...' button when done.",
    )
    all_stim["practice"]["text"] = prac_text_stim
    all_stim["practice"]["naechster"] = all_stim["triggers"]["naechster"]

    all_stim["trial"] = dict()
    all_stim["trial"]["INST"] = dict()
    all_stim["trial"]["INST"]["text"] = visual.TextStim(
        myWin,
        units="norm",
        pos=(0, 0),
        height=DEFAULT_TEXT_STIM_HEIGHT,
        text="Default Trial Instruction" " State Text.",
    )
    all_stim["trial"]["INST"]["image"] = visual.ImageStim(
        myWin, image=getImageFilePath("button_cont_large.png"), units="norm", pos=(0.0, 0.0)
    )

    all_stim["trial"]["GO"] = dict()
    all_stim["trial"]["GO"]["text"] = visual.TextStim(
        myWin, units="norm", pos=(0, 0), height=DEFAULT_TEXT_STIM_HEIGHT, text="Default Trial Go" " State Text."
    )
    all_stim["trial"]["GO"]["image"] = visual.ImageStim(
        myWin, image=getImageFilePath("button_cont_large.png"), units="norm", pos=(0.0, 0.0)
    )

    all_stim["trial"]["STOP"] = dict()
    all_stim["trial"]["STOP"]["text"] = visual.TextStim(
        myWin,
        units="norm",
        pos=(0, 0),
        height=DEFAULT_TEXT_STIM_HEIGHT,
        text="Default Trial Instruction" " State Text.",
    )
    all_stim["trial"]["STOP"]["image"] = visual.ImageStim(
        myWin, image=getImageFilePath("button_cont_large.png"), units="norm", pos=(0.0, 0.0)
    )

    all_stim["exp_end"] = dict()
    all_stim["exp_end"]["txt"] = visual.TextStim(
        myWin, units="norm", pos=(0, -0.9), height=DEFAULT_TEXT_STIM_HEIGHT, text="Press ESCAPE or 'q' to exit."
    )

    all_stim["exp_end"]["img"] = visual.ImageStim(
        myWin, image=getImageFilePath("ende.bmp"), units="norm", pos=(0.0, 0.0)
    )

    return myWin, all_stim
Exemplo n.º 5
0
def createPsychopyGraphics(display):
    #
    # Initialize Graphics
    #

    display_resolution=display.getPixelResolution()
    psychopy_monitor=display.getPsychopyMonitorName()
    unit_type=display.getCoordinateType()
    screen_index=display.getIndex()

    # Create a psychopy window, full screen resolution, full screen mode.
    myWin=visual.Window(display_resolution,
                        monitor=psychopy_monitor,
                        units=unit_type,
                        color=DEFAULT_SCREEN_COLOR,
                        colorSpace='rgb255',
                        fullscr=True,
                        allowGUI=False,
                        screen=screen_index)

    all_stim = dict()

    all_stim['pen'] = {
                    'pos': wintabgraphics.PenPositionStim(myWin,
                                              PEN_POS_GFX_MIN_OPACITY,
                                              PEN_POS_HOVER_COLOR,
                                              PEN_POS_TOUCHING_COLOR,
                                              PEN_POS_ANGLE_COLOR,
                                              PEN_POS_ANGLE_WIDTH,
                                              PEN_POS_GFX_MIN_SIZE,
                                              PEN_POS_GFX_SIZE_RANGE,
                                              PEN_POS_TILTLINE_SCALAR),
                    'traces': wintabgraphics.PenTracesStim(myWin,
                                             PEN_TRACE_LINE_WIDTH,
                                             PEN_TRACE_LINE_COLOR,
                                             PEN_TRACE_LINE_OPACITY)
                    }

    all_stim['triggers']=dict()
    all_stim['triggers']['naechster'] = visual.ImageStim(myWin,
                                image=getImageFilePath('naechster.bmp'),
                                units='norm',
                                pos=(0.8, -0.8))

    all_stim['practice']=dict()
    prac_text_stim = visual.TextStim(myWin, units='norm', pos=(0, .9),
                        height = DEFAULT_TEXT_STIM_HEIGHT,
                        text="Practice using the tablet pen on this screen.\n"
                               "Tap the 'Continue...' button when done.")
    all_stim['practice']['text'] = prac_text_stim
    all_stim['practice']['naechster'] = all_stim['triggers']['naechster']

    all_stim['trial']=dict()
    all_stim['trial']['INST']=dict()
    all_stim['trial']['INST']['text'] = visual.TextStim(myWin,
                                        units='norm',
                                        pos=(0, 0),
                                        height = DEFAULT_TEXT_STIM_HEIGHT,
                                        text="Default Trial Instruction"
                                             " State Text.")
    all_stim['trial']['INST']['image'] = visual.ImageStim(myWin,
                                image=getImageFilePath('button_cont_large.png'),
                                units='norm',
                                pos=(0.0, 0.0))

    all_stim['trial']['GO']=dict()
    all_stim['trial']['GO']['text'] = visual.TextStim(myWin,
                                        units='norm',
                                        pos=(0, 0),
                                        height = DEFAULT_TEXT_STIM_HEIGHT,
                                        text="Default Trial Go"
                                             " State Text.")
    all_stim['trial']['GO']['image'] = visual.ImageStim(myWin,
                                image=getImageFilePath('button_cont_large.png'),
                                units='norm',
                                pos=(0.0, 0.0))

    all_stim['trial']['STOP']=dict()
    all_stim['trial']['STOP']['text'] = visual.TextStim(myWin,
                                        units='norm',
                                        pos=(0, 0),
                                        height = DEFAULT_TEXT_STIM_HEIGHT,
                                        text="Default Trial Instruction"
                                             " State Text.")
    all_stim['trial']['STOP']['image'] = visual.ImageStim(myWin,
                                image=getImageFilePath('button_cont_large.png'),
                                units='norm',
                                pos=(0.0, 0.0))

    all_stim['exp_end'] = dict()
    all_stim['exp_end']['txt'] = visual.TextStim(myWin,
                                        units='norm',
                                        pos=(0, -0.9),
                                        height = DEFAULT_TEXT_STIM_HEIGHT,
                                        text="Press ESCAPE or 'q' to exit.")

    all_stim['exp_end']['img'] = visual.ImageStim(myWin,
                                image=getImageFilePath('ende.bmp'),
                                units='norm',
                                pos=(0.0, 0.0))


    return myWin, all_stim
Exemplo n.º 6
0
def runTrialState(state_code, trial, myWin, io, all_stim):
    # Init time related DVs for trial to 0
    trial['DV_%s_START'%(state_code)] = 0
    trial['DV_%s_VIS_ONSET'%(state_code)] = 0.0
    trial['DV_%s_SND_ONSET'%(state_code)] = 0
    trial['DV_%s_END'%(state_code)] = 0.0

    state_screen = trial['%s_VIS'%(state_code)]
    if state_screen:
        state_screen=u''+str(state_screen).lower().strip()
        if state_screen == '.':
            state_screen = None

    graphics_pos = trial['%s_VPOS'%(state_code)]
    if graphics_pos:
        if graphics_pos == '.':
            graphics_pos = [0,0]
        if isinstance(graphics_pos, (list, tuple)):
            # reset it to a str for storage in hdf5 file
            trial['%s_VPOS'%(state_code)]=str(graphics_pos)
        else:
            graphics_pos = [0,0]

    state_audio = trial['%s_SND'%(state_code)]
    if state_audio:
        state_audio=u''+str(state_audio).strip()
        if state_audio == '.':
            state_audio = None

    state_end_trig = StateTrigger.create(state_code,trial, all_stim['triggers'])

    show_pen = draw_pen_pos = draw_pen_trace = trial['%s_SHOWPEN'%(state_code)]
    if show_pen:
        draw_pen_pos = show_pen.upper().find('P')>=0
        draw_pen_trace = show_pen.upper().find('T')>=0
        clear_existing_traces =  show_pen.upper().find('C')>=0

    possible_state_stim = all_stim['trial']['%s'%(state_code)]
    actual_state_stim = []

    if state_screen:

        if isImageFileCandidate(state_screen):
            img_stim = possible_state_stim['image']
            img_stim.image = getImageFilePath(state_screen)
            img_stim.pos = graphics_pos
            actual_state_stim.append(img_stim)
        else:
            txt_stim = possible_state_stim['text']
            txt_stim.text = state_screen
            txt_stim.pos = graphics_pos
            actual_state_stim.append(txt_stim)

    if state_audio:
        if state_audio.lower().endswith('.wav'):
            possible_state_stim['sound'].setFile(getAudioFilePath(state_audio))
        else:
            # TODO: Write warning to log file that .wav audio is only supported
            state_audio = None

    if clear_existing_traces:
        all_stim['pen']['traces'].clear()

    # Display any state graphics and play any associated audio file
    if actual_state_stim:
        for stim in actual_state_stim:
            stim.draw()

    if state_audio:
        possible_state_stim['sound'].start()

    # If state has a visual stim, use it's flip time to equal the states
    # start time.
    if actual_state_stim:
        if draw_pen_trace:
            all_stim['pen']['traces'].draw()
        trial['DV_%s_START'%(state_code)] = trial['DV_%s_VIS_ONSET'%(state_code)] = myWin.flip()

    if state_audio:
        while not possible_state_stim['sound'].starttime:
            time.sleep(0.001)
        trial['DV_%s_SND_ONSET'%(state_code)] = possible_state_stim['sound'].starttime
        if trial['DV_%s_START'%(state_code)] == 0:
            # If state has an audio stim, but did not have a visual stim,
            # use the audio stim onset time to equal the states start time.
            trial['DV_%s_START'%(state_code)] = trial['DV_%s_SND_ONSET'%(state_code)]

    # If state had no audio and no visual stim, then use
    # current time as the states start time.
    if trial['DV_%s_START'%(state_code)] == 0:
        trial['DV_%s_START'%(state_code)] = core.getTime()

    io.clearEvents()

    state_end_trig.reset()

    # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    # Maintain current state for [state_code]_DUR seconds, or until
    # audio_stim has completed playing, whichever comes last.
    sample_evts=[]
    key_evts_keys=[]
    while (state_audio and possible_state_stim['sound'].status == PLAYING) or \
            state_end_trig.fired(samples = sample_evts, keys = key_evts_keys) is False:

        # Redraw display and flip
        for stim in actual_state_stim:
            stim.draw()

        if draw_pen_pos or draw_pen_trace:
            sample_evts = tablet.getSamples()

            if draw_pen_trace:
                # Redraw stim based on tablet events
                all_stim['pen']['traces'].updateFromEvents(sample_evts)
                all_stim['pen']['traces'].draw()

            if draw_pen_pos and sample_evts:
                last_evt = sample_evts[-1]
                all_stim['pen']['pos'].updateFromEvent(last_evt)
                all_stim['pen']['pos'].draw()

        flip_time=myWin.flip()        
        
        if terminateExperimentKeyPressed(keyboard):
            if state_audio:
                possible_state_stim['sound'].closeFile()
            return False

        key_evts_keys = [k.key for k in keyboard.getPresses()]

    if state_audio:
        possible_state_stim['sound'].closeFile()

    trial['DV_%s_END'%(state_code)] = core.getTime()
Exemplo n.º 7
0
def createPsychopyGraphics(display):
    #
    # Initialize Graphics
    #

    display_resolution = display.getPixelResolution()
    psychopy_monitor = display.getPsychopyMonitorName()
    unit_type = display.getCoordinateType()
    screen_index = display.getIndex()

    # Create a psychopy window, full screen resolution, full screen mode.
    myWin = visual.Window(display_resolution,
                          monitor=psychopy_monitor,
                          units=unit_type,
                          color=DEFAULT_SCREEN_COLOR,
                          colorSpace='rgb255',
                          fullscr=True,
                          allowGUI=False,
                          screen=screen_index)

    all_stim = dict()

    all_stim['pen'] = {
        'pos':
        wintabgraphics.PenPositionStim(
            myWin, PEN_POS_GFX_MIN_OPACITY, PEN_POS_HOVER_COLOR,
            PEN_POS_TOUCHING_COLOR, PEN_POS_ANGLE_COLOR, PEN_POS_ANGLE_WIDTH,
            PEN_POS_GFX_MIN_SIZE, PEN_POS_GFX_SIZE_RANGE,
            PEN_POS_TILTLINE_SCALAR),
        'traces':
        wintabgraphics.PenTracesStim(myWin, PEN_TRACE_LINE_WIDTH,
                                     PEN_TRACE_LINE_COLOR,
                                     PEN_TRACE_LINE_OPACITY)
    }

    all_stim['triggers'] = dict()
    all_stim['triggers']['naechster'] = visual.ImageStim(
        myWin,
        image=getImageFilePath('naechster.bmp'),
        units='norm',
        pos=(0.8, -0.8))

    all_stim['practice'] = dict()
    prac_text_stim = visual.TextStim(
        myWin,
        units='norm',
        pos=(0, .9),
        height=DEFAULT_TEXT_STIM_HEIGHT,
        text="Practice using the tablet pen on this screen.\n"
        "Tap the 'Continue...' button when done.")
    all_stim['practice']['text'] = prac_text_stim
    all_stim['practice']['naechster'] = all_stim['triggers']['naechster']

    all_stim['trial'] = dict()
    all_stim['trial']['INST'] = dict()
    all_stim['trial']['INST']['text'] = visual.TextStim(
        myWin,
        units='norm',
        pos=(0, 0),
        height=DEFAULT_TEXT_STIM_HEIGHT,
        text="Default Trial Instruction"
        " State Text.")
    all_stim['trial']['INST']['image'] = visual.ImageStim(
        myWin,
        image=getImageFilePath('button_cont_large.png'),
        units='norm',
        pos=(0.0, 0.0))

    all_stim['trial']['GO'] = dict()
    all_stim['trial']['GO']['text'] = visual.TextStim(
        myWin,
        units='norm',
        pos=(0, 0),
        height=DEFAULT_TEXT_STIM_HEIGHT,
        text="Default Trial Go"
        " State Text.")
    all_stim['trial']['GO']['image'] = visual.ImageStim(
        myWin,
        image=getImageFilePath('button_cont_large.png'),
        units='norm',
        pos=(0.0, 0.0))

    all_stim['trial']['STOP'] = dict()
    all_stim['trial']['STOP']['text'] = visual.TextStim(
        myWin,
        units='norm',
        pos=(0, 0),
        height=DEFAULT_TEXT_STIM_HEIGHT,
        text="Default Trial Instruction"
        " State Text.")
    all_stim['trial']['STOP']['image'] = visual.ImageStim(
        myWin,
        image=getImageFilePath('button_cont_large.png'),
        units='norm',
        pos=(0.0, 0.0))

    all_stim['exp_end'] = dict()
    all_stim['exp_end']['txt'] = visual.TextStim(
        myWin,
        units='norm',
        pos=(0, -0.9),
        height=DEFAULT_TEXT_STIM_HEIGHT,
        text="Press ESCAPE or 'q' to exit.")

    all_stim['exp_end']['img'] = visual.ImageStim(
        myWin,
        image=getImageFilePath('ende.bmp'),
        units='norm',
        pos=(0.0, 0.0))

    return myWin, all_stim