python - 从先前图像集中绘制 1 个图像 + 从列表中随机绘制 2 个图像 (Python/Psychopy)

标签 python psychopy

在下面的代码中,我从列表(目标集)中绘制 3 个图像,然后将它们显示到屏幕上。接下来,我将另外 3 张图像(图片)显示到屏幕的不同部分。对于第二部分;在 50% 的情况下,我希望“图片”图像与最初显示的图像相同(目标集)。我对这部分没问题,当 x == 0 时我设置 pics = targetset (基本上是抛硬币)。

我的问题是,在其他 50% 的情况下,我希望其中一张“图片”集与原始显示的集(目标集)之一相同,即三者中的任何一个。我希望从图片列表中随机选择剩余的 2 张。就目前情况而言,根据我的逻辑,我只能使“pics”图像与“targetset”中最初显示的图像完全相同或完全不同。

增加问题:当x==0(使pics=targetset)时,它们都显示正常,但是当x==1(意味着不进行更改)时,我收到以下错误消息:

图片[i].pos = 位置[i] UnboundLocalError:分配之前引用的局部变量“pics”

这是我的代码:

#create initial target set
imgList1 = glob.glob(os.path.join('stim','*.png'))
random.shuffle(imgList1)
targetset = [visual.ImageStim(window, img) for img in imgList1[:3]]

#initial target set location
setlocation = [(-2,0),(0,0),(2,0)]
random.shuffle(setlocation)

#create target list
imgList = glob.glob(os.path.join('stim', '*.png'))
random.shuffle(imgList)
pics = [visual.ImageStim(window, img) for img in imgList[:3]]

#set target locations
location = [(1,2),(3,3),(5,5)]
random.shuffle(location)

'''define sequential presentation function'''
def seq():
    x = random.randint(0,1)
    if x == 0:
        pics = targetset
    print x

    #display initial target set
    for i in range(3):
        targetset[i].pos = setlocation[i]
        targetset[i].draw()

    window.flip()
    core.wait(3)

    #display targets
    for i in range(3):
        pics[i].pos = location[i]
        pics[i].draw()
        window.flip()
        core.wait(1)

seq()
core.wait(3)
window.close()
quit()

希望有人能帮忙 干杯

最佳答案

我发现您的代码存在一些可能的陷阱。一种是您从同一组图像创建两个单独的列表 imgList1imgList,然后分别随机化每个列表并从每个列表中提取前三个元素分别列出到 targetsetpics 中。这些子集在图像中可能会有一些重叠。从你的任务描述来看,我认为这不是故意的。我可能建议仅创建 1 个 imgList,然后使用 imgList.pop() 函数从列表中提取图像。 Pop 返回列表中的最后一个元素,然后从列表中删除该元素。我认为这就像从一副牌中抽一张牌,你不能再次抽这张牌,因为它不再在牌堆中。您当前正在做的事情就像从两个不同的牌组中抽牌。您可能会获得同一张卡两次。

我在这里看到的第二个问题是,你定义了当你的硬币翻转 x 为 0 时要做什么的条件,但你没有为当你的硬币翻转为 1 时定义一个条件。目前,它只会显示imgList 中的前 3 个图像,因为这就是您定义 pics 的方式。我可能建议在 x 抛硬币的 if/else block 中定义 pics。如果为 0,则使用 targetset,如果为 1,则从目标列表中随机选择一个元素,然后将该元素复制到 pics 并从 中再弹出两张图像>imgList。与所有编码一样,解决这个问题的方法不止一种,但我将在这里提供一个解决方案。

#Outside the trial loop
imgList = glob.glob(os.path.join('stim', '*.png'))
random.shuffle(imgList)

#Inside the trial loop
targetset = []
for i in range(0,3):
    targetset.append(imgList.pop())
#Do your display stuff with targetset
if random.randint(0,1) == 0:
   pics = targetset
else:
   pics = []
   pics.append(targetset[random.randint(0,2)])
   pics.append(imgList.pop())
   pics.append(imgList.pop())
random.shuffle(pics)
#Do your display stuff with pics 

关于python - 从先前图像集中绘制 1 个图像 + 从列表中随机绘制 2 个图像 (Python/Psychopy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34857898/

相关文章:

python - 神经网络 MNIST

Python - 在循环中使用预定义变量列表(psychopy)

python - 如何使用编码器在 PsychoPy 上呈现 jpg 图像?

python - 将眼动追踪 .edf 文件转换为 ASC/CSV 格式

python - 使用 Jinja 遍历项目,在每第 5 个项目后添加 div

python - Flask - 让 `abort` 使用纯文本

python - 如何根据双方最近的可用日期制作重叠的几周窗口?

Python - Pygame - 获取特定声音是否正在播放

python - 如何使 PsychoPy 对象保持 Visual.Window 成为 Windows 7 上最顶层(事件)窗口?