python - 根据列表更改文本和图像刺激
问题描述
我正在尝试做的是向参与者展示动物的词和动物的图像。有时单词和图像会匹配,有时它们不会匹配,参与者必须对动物的名字做出反应。我正在努力的是呈现文本和图像。
所以...
我有一份动物清单:
animal_words =[ 'gorilla', 'Ostrich', 'Snake', 'Panda']
然后我有一个动物的图像,与上面的颜色相同
animal_image=[ 'gorilla', 'Ostrich', 'Snake', 'Panda']
我有两个条件:相同和不同。我已将上述列表分为相同和不同。相同的条件将呈现给参与者 10 次,不同的条件将呈现 5。
same=[]
different[]
conditions=[]
for animal in animals:
for image in animal_images:
if animal == image:
same.extend([[animal,image]]* 10)
else:
different.extend ([[animal,image]] * 5)
shuffle (same)
shuffle (different)
#combine conditions into trial list
conditions=[same,different]
相同条件的一个示例是:
[大猩猩(文字),大猩猩(图片)]
然后我创建我的窗口和刺激:
from psychopy import visual, event, core, gui
win=visual.Window([1024,768], fullscr=False,allowGUI=True, units='pix',\
color= (-1,-1,-1))
tstim=visual.TextStim(win, text='', pos=(0,0), color= ((0,0,0)))
imstim=visual.ImageStim(win, image='', pos=(0,0)
我需要做的是将动物文本分配给 tstim 并将 animal_image 分配给 imstim 并设置一个循环,以便它们根据我制作的列表进行更改。我无法成功地做到这一点,因为列表粘在一起。我也不知道如何设置循环下面的代码是我对循环外观的最佳猜测:
for a in conditions:
tstim.setText(animal_name)
imstim.setImage(animal_image)
tstim.draw()
imstim.draw()
win.flip()
core.wait()
但是,我认为循环不正确,但我想不出其他任何东西。任何帮助,将不胜感激
解决方案
你在正确的轨道上。拥有一个嵌套列表是个好主意,其中每个子列表都包含用于试验的配对。我建议创建一个包含图像和文本刺激的嵌套列表,然后遍历该刺激列表。我在下面展示了这一点(未经测试)。
import random
from psychopy import visual, event, core, gui
animal_words = ['Gorilla', 'Ostrich', 'Snake', 'Panda']
animal_images = ['Gorilla', 'Ostrich', 'Snake', 'Panda']
win = visual.Window(
[1024, 768], fullscr=False, allowGUI=True, units='pix',
color=(-1, -1, -1))
# Create list of trials
trials = []
for animal in animal_words:
for image in animal_images:
# Get the filepath to the animal image.
image_file = "{}.png".format(image)
# Instantiate the stimulus objects.
stim_pair = [
visual.TextStim(win, text=animal, pos=(0, 0), color=(0, 0, 0)),
visual.ImageStim(win, image=image_file, pos=(0, 0))
]
# Add stimulus objects to growing list of trials.
if animal == image:
trials.extend([stim_pair] * 10)
else:
trials.extend([stim_pair] * 5)
random.shuffle(trials)
# Run trials
for text, image in trials:
text.draw()
image.draw()
win.flip()
core.wait()
如果所有试验都相同(只是顺序不同),那么您可以将文本和图像文件路径保存在电子表格中,将电子表格读入列表,创建刺激对象,然后打乱该列表。这将不是在每次运行时创建列表。
推荐阅读
- spring - 使用 @RefreshScope 更改运行时的日志记录级别
- php - URL 中的尾随 %20 空格在 Codeigniter 中产生 404 错误
- javascript - 在 JavaScript 中将数组转换为 json 的问题
- java - 多个存储库(Maven / Pip)和 protobuffers - 最佳项目设置?
- android - 安卓深度链接
- python - 旋转三角形的最佳方法是在完全旋转后顶部和底部三角形之间没有间隙?
- tensorflow - 如何找到使用 Tensorflow 预测股票市场下一个价值的标准方法?
- javascript - Uncaught (in promise): TypeError: data.data is not iterable
- nestjs - 如何构建 NestJS monorepo 并使用相关资产构建应用程序?
- python - 通过 CheckBox 删除 QTableView/QAbstractTableModel 中的行