对于go-NoGo任务,我想用心理学中的data.TrialHandler类来组织图片:
trials = data.TrialHandler(ImageList, nReps=1, method='random')
现在我想编写一个循环,其中心理进入字典,呈现第一组图片(例如A_n),然后进入第二组直到第六组。我尝试了以下方法:
import glob, os, random, sys, time
import numpy.random as rnd
from psychopy import visual, core, event, monitors, gui, logging, data
im_a = glob.glob('./a*') # upload pictures of the a-type, gives out a List of .jpg-files
im_n = glob.glob('./n*') # n-type
im_e = glob.glob('./e*') # e-type
# combining Lists of Pictures
A_n = im_a + im_n
N_a = im_n + im_a
A_e = im_a + im_e
E_a = im_e + im_a
E_n = im_e + im_n
N_e = im_n + im_e
# making a Dictionary of Pictures and Conditions
PicList = [A_n, N_a, A_e, E_a, E_n, N_e] # just the six Combinations
CondList = [im_a,im_n,im_a,im_e,im_e,im_n] # images that are in the GO-Condition
ImageList = []
for imagelist, condition in zip(PicList, CondList):
ImageList.append({'imagelist':imagelist,'condition':condition}) # to associate the picturelist with the GO Conditionlist
标题我问了一个额外的问题:Combining and associating multiple dictionaries
# Set Experiment
win = visual.Window(color='white',units='pix', fullscr=False)
fixCross=visual.TextStim(win,text='+',color='black',pos=(0.0,0.0), height=40)
corrFb = visual.TextStim(win,text='O',height=40,color='green',pos=[0,0])
incorrFb = visual.TextStim(win,text='X',height=40, color='red',pos=[0,0])
# Start Experiement
trials = data.TrialHandler(ImageList, nReps=1, method='random')
rt_clock = core.Clock()
bitmap = visual.ImageStim(win)
for liste in ImageList[0:5]: # to loop through all 6 conditions
keys = []
for i,Pictures in enumerate(liste): # to loop through all pictures in each condition
bitmap.setImage(Pictures) # attribute error occurs, not if I use Pictures[0][0], even though in this case every pictures is the same
bitmap.draw()
win.flip()
rt_clock.reset()
resp = False
while rt_clock.getTime() < 2.0: # timelimit is defined 2 s
if not resp:
resp = event.getKeys(keyList=['space'])
rt = rt_clock.getTime()
if bool(resp) is (Pictures in CondList): # at this point python should have access to the Dictionary in which the associated GO Pictures are saved
corrFb.draw()
accu=1 # doesn't work yet
else:
incorrFb.draw()
accu=0
win.flip()
core.wait(0.5)
trials.addData('rt_'+str(i), rt) # is working well when loop: if trial in trials: ...; in this case trialHAndler is not used, therefor trials.addData is not working
trials.addData('accu_'+str(i), accu)
trials.saveAsExcel(datanames)
core.quit()
此代码中存在一些问题:首先它只呈现一个图像六次,但不是六个不同的图片[1]
其次一个完全不同的问题[2]是时间测量和试验操作者正在进行的准确度的保存,但对于每个试验。因此,它为每个试验添加了所有RT。我想为每张图片获取RT。我试了几个东西,比如额外的刺激。刺激和最后一个额外的循环,它给了我最后的RT但不是每个。 - &GT;在下面回答!!!
for stimuli in stimulus: stimulus.addData('rt', rt)
我知道这四个问题对于一个问题来说很重要,但也许有人可以给我一些很好的想法,我可以解决这些问题......谢谢大家!
答案 0 :(得分:2)
标记为[1]的问题的原因是您将图像设置为PicList [0] [0],它永远不会改变。正如迈克建议的那样,你需要::
for i,thisPic in enumerate(PicList):
bitmap.setImage(thisPic) #not PicList[0][0]
但也许您需要回到基础,以便您实际使用试用处理程序来处理您的试验; - )
创建单个词典列表,其中一个词典代表一个试验,然后按顺序运行(告诉TrialHandler使用列表'sequential'而不是'random')。因此,您当前使用的循环应该只是创建条件序列列表,而不是运行试验。然后将一个列表传递给试用处理程序::
trials = TrialHandler(trialTypes = myCondsListInOrder, nReps=1, method='sequential')
for thisTrial in trials:
pic = thisTrial['pic']
stim.setImage(pic)
...
trials.addData('rt', rt)
trials.addData('acc',acc)
另外,我不会使用excel格式输出您的数据,而是使用'long wide'格式::
trials.saveAsWideText('mydataFile.csv')
最好的祝福,
乔恩
答案 1 :(得分:0)
(A)这与您的问题无关,但会提高效果。 这一行:
bitmap = visual.ImageStim(win)
不应该在循环内发生。即你应该只对每个刺激进行一次初始化,然后在一个循环中你只需更新该刺激的属性,例如使用bitmap.setImage(...)。因此,将此初始化行转移到顶部,您可以在其中创建TextStims。
(B)[已删除:我没有注意到第一个代码块。]
(C)
bitmap.draw(pictures)
这一行没有任何论据。它应该只是bitmap.draw()。无论如何,它并不清楚什么&#39;图片&#39;是指。请记住,Python区分大小写。这与&#39;图片&#39;不同。在上面的循环中定义。我猜你想要更新正在显示的图片?在这种情况下,你需要在这个循环中进行bitmap.setImage(...)行,而不是在上面,你将始终绘制一个固定的图片,因为这是唯一一个在每个试验中设置的图片。
(D)重新启动RT,每次试验只保存一次(检查缩进)。如果要为每个图像保存一个,则需要再次缩进这些行。此外,您在数据输出中每次试用只能获得一行。如果要在每次试用中记录多个RT,则需要为它们指定唯一的名称,例如: rt_1,rt_2,...,rt_6因此它们各自出现在单独的列中。例如你可以在这个循环中使用枚举器:
for i, picture in enumerate(Piclist)
# lots of code
# then save data:
trials.addData('rt_'+str(i), rt)