根据两个列表长度修改序列的长度

时间:2015-02-25 23:17:53

标签: python list psychopy

这是我的代码:

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:列表索引超出范围。"基本上我需要一些方法来从1-64索引一个列表,而另一个65-128,然后只是随机生成顺序,同时确保列表a和b的索引是相同的。

1 个答案:

答案 0 :(得分: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()