我在Matlab有一些经验,但对PsychoPy来说还是新手。
现在我想在两个图像之间连续切换,直到有键盘响应。 每个图像应该在屏幕上保持100毫秒,我希望能够验证是否是这种情况(例如在日志文件中)。
我在win.flip()之后使用core.wait(.084)在60Hz屏幕上使用大约100ms。 我通过使用win.logOnFlip()
将每个翻转的帧写入日志文件来验证它但我相信我可以更加精确,我只知道如何根据帧定义图像的持续时间。
函数core.wait()只需要几秒钟的时间,而不是帧,是正确的吗?
如果你能给我一些关于如何实现(和验证)每帧图像6帧的提示,我将非常感激。
提前感谢
最佳
塞巴斯蒂安
这是我的代码:
import os # for file/folder operations
from psychopy import visual, event, core, gui, data, logging
# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(_thisDir)
# screen size in pixels
scrsize = (600,400)
# gather info participant
exp_name = 'MyFirstPsychoPy'
exp_info = {
'participant': '',
}
dlg = gui.DlgFromDict(dictionary=exp_info, title=exp_name)
# if user pressed cancel quit
if dlg.OK == False:
core.quit()
# Get date and time
exp_info['date'] = data.getDateStr()
exp_info['exp_name'] = exp_name
#save a log file for detail verbose info
filename = _thisDir + os.sep + 'data/%s_%s_%s' %(exp_info['participant'], exp_name, exp_info['date'])
# print filename #to see if path correct
logFile = logging.LogFile(filename+'.log', level=logging.DEBUG)
logging.console.setLevel(logging.WARNING) # outputs to the screen, not a file
# Create a window small window
win = visual.Window(size=scrsize, color='white', units='pix', fullscr=False)
# or go full screen
#win = visual.Window([1280,1024], fullscr=True, allowGUI=False, waitBlanking=True)
# this is supposed to record all frames
win.setRecordFrameIntervals(True)
# show instructions until spacebar
start_message = visual.TextStim(win,
text="hello. you will see mondrians. press space to respond.",
color='red', height=20)
event.clearEvents()
keys = event.getKeys(keyList=['space', 'escape']) #allow only space and escape keys
while len(keys) == 0:
start_message.draw()
win.flip()
keys = event.getKeys(keyList=['space', 'escape'])
if len(keys)>0:
break
print keys #show on output screen
keys = event.clearEvents() # empty keys
keys = event.getKeys(keyList=['space', 'escape'])
# define 2 pictures
bitmap1 = visual.ImageStim(win, 'Mondrians/Mask_1.bmp', size=scrsize)
bitmap2 = visual.ImageStim(win, 'Mondrians/Mask_2.bmp', size=scrsize)
bitmap = bitmap1
# Initialize clock to register response time
rt_clock = core.Clock()
rt_clock.reset() # set rt clock to 0
# show alternating pics until response
frameN = 0
while len(keys) == 0:
if bitmap == bitmap1:
bitmap = bitmap2
else:
bitmap = bitmap1
bitmap.draw()
win.logOnFlip(msg='frame=%i' %frameN, level=logging.DEBUG) #record the time of win.flip() in the log file
win.flip() # show image
frameN = frameN + 1
core.wait(.084) # wait 100 ms
keys = event.getKeys(keyList=['space', 'escape']) #record resp
# if response stop
if len(keys)>0:
rt = rt_clock.getTime()
break
print keys, rt #show resp and rt on screen
win.saveFrameIntervals(filename+'.log', clear=True)
win.close()
core.quit()
答案 0 :(得分:1)
是的,还有更好的方法!标准解决方案利用win.flip()
暂停代码执行直到下一次监视器更新的事实。因此,循环win.flip()
将为每个循环提供一个帧。因此,要在两个imagesStims(bitmap1
和bitmap2
)之间切换,直到有响应:
clock = core.Clock() # to assess timing
keepLooping = True
while keepLooping: # continue until break
for thisBitmap in [bitmap1, bitmap2]: # alternate between images
if keepLooping: # do not show bitmap2 if a key was pressed on bitmap1
for frameN in range(6): # 100 ms
thisBitmap.draw()
print clock.getTime()
win.callOnFlip(clock.reset) # ... or use win.logOnFlip
win.flip() # inner loop is timed to this, as long as the rest of the code in here doesn't take longer than a frame.
keys = event.getKeys(keyList=['space', 'escape'])
if keys: # notice simplification. [] evaluates to False.
rt = rt_clock.getTime()
keepLooping = False
break
......然后是剩下的。我已使用core.Clock()
来评估此处的时间,但您的win.logOnFlip()
同样出色。取决于您想要的输出类型。
请注意event.getKeys()
记录此行执行的时间,而不是按下该键的时间。因此它增加了一点延迟。在这种“帧锁定循环”中,密钥响应因此被离散化为帧间隔。如果要获得键盘状态的实际异步轮询(即,如果RT记录中的错误最多+ 16ms),请使用iohub
模块。许多键盘无论如何都具有固有的10-30毫秒延迟,因此您无法摆脱所有延迟。