这是我的代码:
from psychopy import visual, event, gui
import random, os
from random import shuffle
from PIL import Image
import glob
# import images sequences and randomize in the same order
a = glob.glob("DDtest/targetimagelist1/*")
b = glob.glob("DDtest/distractorimagelist1/*")
c = glob.glob("DDtest/targetimagelist2/*")
d = glob.glob("DDtest/distractorimagelist3/*")
indices = random.sample(range(len(a)), len(a))
a = map(a.__getitem__, indices)
b = map(b.__getitem__, indices)
c = map(c.__getitem__, indices)
d = map(d.__getitem__, indices)
def loop():
# randomizes the location of the stimuli
loc = [1, 2]
location = random.choice(loc)
if location == 1:
pos1 = [-.05,-.05]
pos2 = [.05, .05]
else:
pos1 = [.05, .05]
pos2 = [-.05, -.05]
# randomizes the image lists
type = [1,2]
trialtype = random.choice(type)
if trialtype == 1:
target = a
distractor = b
else:
target = c
distractor = d
# Create window and stimuli.
win = visual.Window(size=(1280, 800), fullscr=True, screen=0, monitor='testMonitor', color=[-1,-1,-1]) # removed a default value
targetstim = visual.ImageStim(win=win, pos=pos2, size=[0.5,0.5])
targetstim.autoDraw = True
distractorstim = visual.ImageStim(win=win, pos=pos1, size=[0.5,0.5])
distractorstim.autoDraw = True
distractorstim.image = distractor[i]
targetstim.image = target[i]
# Display and wait for answer
win.flip()
event.waitKeys(keyList = ['space'])
# loop
for i in range(len(a)):
loop()
所以这是我的问题:每个文件中有 64 张图像。当前程序在显示 64 张图像时终止(长度基于“a”中的图像数量)。我想要的是在显示所有图像时终止文件(128 次试验)。有没有办法做到这一点?我将不胜感激任何帮助或指导。:)
编辑:
我尝试对循环执行此操作:
# loop
for i in range(len(a)*2):
loop()
当我这样做时会发生什么,图像会像以前一样循环,除非当我超过 64 (65-67) 时,它会尝试调用超出范围的图像,这会导致“IndexError:list index out of范围。” 基本上我需要一些方法从 1-64 和另一个 65-128 索引一个列表,然后随机生成顺序,同时确保列表 a 和 b 的索引相同。