Python 每次试验的精神病重置变量

Python 每次试验的精神病重置变量,python,timeline,psychopy,Python,Timeline,Psychopy,我制作了自己的评分表,时间从0到28:18。它的移动速度取决于人们在每次试验中按“t”或“b”键的速度。屏幕上显示的内容如下所示: 我希望每次试验的顶部时间重置为14:09。每次试验后,试验间隔为0.5秒,在此期间屏幕上显示“+”。我遇到的问题是,如果在ITI期间按“t”或“b”,下一次试验将不会在14:09开始。相反,它将按“t”或“b”键移动时间线的方向移动。以下是我试图更正的当前代码: prevPos = 0 trialNum=0 b_list=[] t_list=[] key=[]

我制作了自己的评分表,时间从0到28:18。它的移动速度取决于人们在每次试验中按“t”或“b”键的速度。屏幕上显示的内容如下所示:

我希望每次试验的顶部时间重置为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=[] 
prevPos=0
trialNum=0
b_列表=[]
t_list=[]
键=[]
#循环浏览图片
对于catPictures中的每个HPIC:
b_列表=[]
t_list=[]
计时器=核心。倒计时(TrialDuration)
当timer.getTime()大于0时:
对于事件中的键。getKeys():
如果输入['escape']:
core.quit()#按escape键退出
如果输入['b']:
#将按键添加到每个按键的列表中。然后按此列表的长度成比例移动光标
b_list.append(键)
prevPos+=len(b_列表)
如果输入['t']:
t_list.append(键)
prevPos-=len(t_列表)
#设置光标可以移动的上限和下限(稍后将减半以限制屏幕上光标的范围)
如果prevPos=849:
prevPos=849
#设置绝对位置,使绝对位置变为0到300的范围(基于28:18分钟的电影)
绝对位置=前置位置+849
#需要范围为1698(28:18秒)
#需要范围为1698(28:18秒)
#当前范围为0至849(为1698的50%)
秒=绝对位置
Image2=visual.ImageStim(窗口)
#路缘影像2.setSize=((0.5,0.5),单位='norm')
#使其略高于绝对中间值
图2.setPos([0100])
#使用每个图像(图像中的i)
图像2.设置图像(catPictures[trialNum])
#定义沿时间轴移动的光标
cursorImage=visual.ImageStim(窗口)
setImage(directoryStim+'cursor.png')
#无论prevPos有多大,都要使光标移动
光标图像设置位置([int(prevPos)*.5,int(400)])
#排队
timeline=visual.simpleMagestim(win=window,image=directorySim+'line.png',units='pix',pos=[0400])
event.clearEvents()#清除其他未处理的事件
#打印时间线旁边的最小值和最大值
min=visual.TextStim(窗口,'0:00',颜色='Black',位置=[-500400])
max=visual.textsim(窗口,'28:18',颜色='Black',位置=[500400])
#打印不断更新的时间值
timeText=visual.TextStim(窗口,“%d:%02d%”(秒/60,秒%60),颜色为“黑色”,宽度为1080,字体为“Verdana”,位置为[0465],高度为50)
##现在把所有的东西都放到屏幕上
图2.绘制(窗口)
最小绘制(窗口)
最大绘制(窗口)
timeText.draw(窗口)
时间线。绘制(窗口)
光标图像绘制(窗口)
##翻转使其实际出现
window.flip()
ITI=visual.TextStim(窗口“+”,位置=[0,0],高度=50,颜色=黑色)
ITI.draw(窗口)
window.flip()
等待(.5、.5)
三氮杂铀+=1
prevPos=0
b_列表=[]
t_list=[]
键=[]

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

在代码的最后一行,尝试以下操作:

event.clearEvents()

在ITI期间按下的键将进入缓冲区,因此下次调用
event.getKeys()
时将收集这些键。通过调用
clearEvents()
,可以刷新该缓冲区

  • 在内部for循环下缩进内容
  • 事件.clearEvents()
    移动到
    while
    循环之前 或在
    核心之后。等待
    。这就是你开始聆听新音乐的地方
    按键。等待期间的按压操作将在
    下一次调用
    event.getKeys()
    while循环。这就是为什么它会立即移动光标。没有 将
    event.clearEvents()
    放在哪里的真正原因 因为您只侦听while循环中的事件。这就是为什么我 建议您移动它,而不是插入新的
  • 启动精神变态刺激是相当重的资源 有时可能需要几百毫秒。没有 创建多个新的
    图像刺激
    文本刺激
    的原因 每次审判
  • 绝对位置
    是冗余的。您只需
  • 样式:现在可以对所有其他属性执行
    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
    
    刺激物 Image2=visual.ImageStim(窗口) cursorImage=visual.ImageStim(窗口) min=visual.TextStim(窗口,'0:00',颜色='Black',位置=[-500400]) max=visual.textsim(窗口,'28:18',颜色='Black',位置=[500400]) timeText=visual.TextStim(窗口,颜色为黑色,宽度为1080,字体为Verdana,位置为[0465],高度为50) ITI=visual.TextStim(窗口“+”,位置=[0,0],高度=50,颜色=黑色) timeline=visual.simpleMagestim(win=window,image=directorySim+'line.png',units='pix',pos=[0400]) 计时器=核心。倒计时(TrialDuration) #循环浏览图片 trialNum=0 对于catPictures中的每个HPIC: prevPos=0 键=[] b_列表=[] t_list=[] timer.reset() event.clearEvents()#摆脱