0

这是我的代码:

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 的索引相同。

4

1 回答 1

2

以下是我建议解决此问题的方法 - 为每种试验类型创建一个单独的计数器。每次使用其中一种试验类型的图像集时,使用该试验类型的计数器作为索引,然后增加该计数器。当您选择试用类型时,请随机选择它,除非其中一个试用类型图像集已用尽,在这种情况下选择另一个。

这是代码 - 有三个地方需要修改:

    type = [1,2]
    if trialImageCounters[1] == len(a):
        trialtype = 2  # If the maximum number of type-1 images have been used the trial is automatically type-2.
    elif trialImageCounters[2] == len(a):
        trialtype = 1  # If the maximum number of type-2 images have been used the trial is automatically type-1.
    else:
        trialtype = random.choice(type)  # If neither type-1 or type-2 images are all used up, pick a random type.
    if trialtype == 1:
        target = a
        distractor = b
    else:
        target = c
        distractor = d

...

# The image index is taken from the counter for the selected type of trial
distractorstim.image = distractor[trialImageCounters[trialtype]]  
targetstim.image = target[trialImageCounters[trialtype]]

trialImageCounters[trialtype] += 1

...

# loop
trialImageCounters = {1:0, 2:0}  # Create a different image counter for each trial type
for i in range(len(a)*2):
    loop()
于 2015-02-25T23:25:37.980 回答