python - 每次试验的 Psychopy 重置变量

标签 python timeline psychopy

我制定了自己的评分标准,时间线是从 0 点到 28:18。它根据人们每次试验按下“t”或“b”键的速度而移动。屏幕上显示的内容如下所示:

experiment

我希望每次试用时顶部的时间都重置为 14:09。每次试验后有 0.5 秒的试验间隔,期间屏幕上会显示“+”。我遇到的问题是,如果您在 ITI 期间按“t”或“b”,则下一次试验不会在 14:09 开始。相反,它将按按“t”或“b”键移动时间线的方向移动。这是我当前尝试纠正此问题的代码:

prevPos = 0
trialNum=0
b_list=[]
t_list=[]
key=[]

# loop through pictures
for eachPic in catPictures:
    b_list=[]
    t_list=[]
    timer = core.CountdownTimer(TrialDuration)
    while timer.getTime() > 0:
        for key in event.getKeys():
            if key in ['escape']:
                core.quit() # quit if they press escape
            if key in ['b']:
                # add keypress to list for each keypress. then move cursor proportionally to length of this list
                b_list.append(key)
                prevPos+=len(b_list)
            if key in ['t']:
                t_list.append(key)
                prevPos-=len(t_list)

    # set upper and lower limits to where cursor can go (which will later be halved to restrict range of cursor on the screen)
        if prevPos <= -849:
            prevPos = -849
        elif prevPos >=849:
            prevPos = 849
        # make absolute position so pos_absolute becomes a range from 0 to 300 (based on 28:18 min movie)
        pos_absolute = prevPos + 849
        # need to have range of 1698 (# of seconds in 28:18)
        # need to have range of 1698 (# of seconds in 28:18)
        # current range is 0 to 849 (which is 50% of 1698)
        seconds =  pos_absolute

        Image2 = visual.ImageStim(window)
        #curbImage2.setSize = ((0.5,0.5), units = 'norm')
        # make a little higher than the absolute middle
        Image2.setPos([0,100])
        # use each image (i in curbImages)
        Image2.setImage(catPictures[trialNum])

        # define cursor that moves along timeline
        cursorImage = visual.ImageStim(window)
        cursorImage.setImage(directoryStim+'cursor.png')
        # make cursor move by however big prevPos is
        cursorImage.setPos([int(prevPos)*.5,int(400)])
        # make the line
        timeline = visual.SimpleImageStim(win=window, image=directoryStim+'line.png', units='pix', pos=[0, 400])
    event.clearEvents() # get rid of other, unprocessed events

        # print min and max values next to timeline
        min = visual.TextStim(window, '0:00', color='Black', pos=[-500, 400])
        max = visual.TextStim(window, '28:18', color='Black', pos=[500, 400])
        # print constantly updating time value
        timeText = visual.TextStim(window,'%d:%02d' % (seconds/60, seconds % 60),color='Black',wrapWidth=1080,font='Verdana', pos=[0,465], height=50)

        ## now put everything on the screen
        Image2.draw(window)
        min.draw(window)
        max.draw(window)
        timeText.draw(window)
        timeline.draw(window)
        cursorImage.draw(window)
    ## flip so it actually appears
        window.flip()

    ITI = visual.TextStim(window, '+', pos=[0,0], height=50, color='Black')
    ITI.draw(window)
    window.flip()
    core.wait(.5,.5)
    trialNum+=1
    prevPos = 0
    b_list =[]
    t_list=[]
    key=[] 

如何在每次试用开始时将时间线重置为 14:09(又名 prevPos = 0),即使人们在试用结束时按“t”或“b”或在试验间隔期间?

最佳答案

  1. 在内部 for 循环下缩进内容。
  2. event.clearEvents() 移至 while 循环之前 或在core.wait之后。这就是您开始聆听新内容的地方 按键。等待期间的按下将返回 在下一个周期中调用 event.getKeys() while 循环。这就是它立即移动光标的原因。没有 拥有 event.clearEvents() 的真正原因 因为您只监听 while 循环中的事件。这就是为什么我 建议您移动它而不是插入新的。
  3. 启动心理刺激需要大量资源 有时可能需要几百毫秒。没有 创建多个新的 ImageStimsTextStims 的原因 每次审判。
  4. pos_absoluteseconds 是多余的。只需几秒钟即可完成。
  5. 一种风格:您现在可以对所有其他属性执行 stim.pos = x 等操作,而不是 stim.setPos(x) 。这是从现在开始设置刺激属性的首选方法(更干净的代码并允许对属性进行更多操作)。

这是经过上述更改后的清理代码:

# Stimuli
Image2 = visual.ImageStim(window)
cursorImage = visual.ImageStim(window)
min = visual.TextStim(window, '0:00', color='Black', pos=[-500, 400])
max = visual.TextStim(window, '28:18', color='Black', pos=[500, 400])
timeText = visual.TextStim(window,color='Black',wrapWidth=1080,font='Verdana', pos=[0,465], height=50)
ITI = visual.TextStim(window, '+', pos=[0,0], height=50, color='Black')
timeline = visual.SimpleImageStim(win=window, image=directoryStim+'line.png', units='pix', pos=[0, 400])
timer = core.CountdownTimer(TrialDuration)

# loop through pictures
trialNum=0
for eachPic in catPictures:
    prevPos = 0
    key=[]
    b_list=[]
    t_list=[]
    timer.reset()
    event.clearEvents() # get rid of other, unprocessed events
    while timer.getTime() > 0:
        for key in event.getKeys():
            if key in ['escape']:
                core.quit() # quit if they press escape
            if key in ['b']:
                # add keypress to list for each keypress. then move cursor proportionally to length of this list
                b_list.append(key)
                prevPos+=len(b_list)
            if key in ['t']:
                t_list.append(key)
                prevPos-=len(t_list)

        # set upper and lower limits to where cursor can go (which will later be halved to restrict range of cursor on the screen)
        if prevPos <= -849:
            prevPos = -849
        elif prevPos >=849:
            prevPos = 849
        # make absolute position so pos_absolute becomes a range from 0 to 300 (based on 28:18 min movie)
        # need to have range of 1698 (# of seconds in 28:18)
        # need to have range of 1698 (# of seconds in 28:18)
        # current range is 0 to 849 (which is 50% of 1698)
        seconds =  prevPos + 849

        #curbImage2.size = ((0.5,0.5), units = 'norm')
        # make a little higher than the absolute middle
        Image2.pos = [0,100]
        # use each image (i in curbImages)
        Image2.image = catPictures[trialNum]

        # define cursor that moves along timeline
        cursorImage.image = directoryStim+'cursor.png'
        # make cursor move by however big prevPos is
        cursorImage.pos = [int(prevPos)*.5,int(400)]
        timeText.text = '%d:%02d' % (seconds/60, seconds % 60))

        ## now put everything on the screen
        Image2.draw(window)
        min.draw(window)
        max.draw(window)
        timeText.draw(window)
        timeline.draw(window)
        cursorImage.draw(window)
        ## flip so it actually appears
        window.flip()

    ITI.draw(window)
    window.flip()
    core.wait(.5,.5)
    trialNum+=1

请注意,我还删除了一些看似不必要的 b_listkey 等重置。

关于python - 每次试验的 Psychopy 重置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32317483/

相关文章:

python - 从先前图像集中绘制 1 个图像 + 从列表中随机绘制 2 个图像 (Python/Psychopy)

python - 为什么当 fullscr=True 时我的对话框不显示?

python - 在crawlspider中配置规则时,follow参数似乎不起作用

python - Bokeh 和 Networkx - 悬停工具提示显示多个箭头

javascript - 时间线图表在 Google Apps 脚本中不起作用

apache-spark - 为什么 Spark Streaming 执行器在不同的时间启动?

python - 为什么我不能在我的 psychopy.visual.TextStim 中包含 "£"符号?

python - BeautifulSoup/Python 的 NoneType 错误

python - 如何使用 SQLAlchemy 测试无效记录插入?

javascript - 在 D3.js 中创建从 01JAN15 -> 31DEC15 的垂直时间线