Duplicating random visual stimuli in Python/Psychopy

妖精的绣舞 提交于 2019-12-11 12:46:42

问题


Using Python/Psychopy. I am presenting 3 random visual stimuli to the center of the screen for 1 second (imgList1). I am then presenting 3 other random visual stimuli to the upper right of the screen (imgList). On 50% of occasions I need the second group of stimuli (imgList) to be the same as the first (imgList1). How do i access which stimuli were randomly selected at part 1 so i can then use that information to display those same ones thereafter? I am not sure how to keep track of what results from my initial randomly selected images.

Here is my code:

#make initial stimuli
imgList1 = glob.glob(os.path.join('stim','*.png'))
random.shuffle(imgList1)
targetset = [visual.ImageStim(window, img) for img in imgList1]

setlocation = [(-2,0),(0,0),(2,0)]
random.shuffle(setlocation)

#make second group of stimuli
imgList = glob.glob(os.path.join('stim', '*.png'))
random.shuffle(imgList)
pics = [visual.ImageStim(window, img) for img in imgList[:3]]

location = [(1,2),(3,3),(5,5)]
random.shuffle(location)

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

window.flip()
core.wait(1)

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

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

回答1:


targetset is the list that has your chosen images from imgList1. They don't go anywhere when you draw them. You can still access that list later (as long as you don't delete or overwrite). Just flip a coin (select a random number between 0 and 1 and check if less than 0.5. If so use pics in the secondary stimuli, if not use targetset (with the second location list). You might find this worth abstracting to a function.

def drawSet (imgs,locs):
    for(i,l) in (imgs,locs):
        i.pos = l
        i.draw()
        window.flip()
        core.wait(0.25)

Then you would use this for drawSet(targetset,setlocation) and drawSet(pics,location) or drawSet(targetset,location) with random (assuming you import random as r somewhere)

if r.random() < 0.5:
    drawSet(pics,location)
else:
    drawSet(targetset,location)


来源:https://stackoverflow.com/questions/34771064/duplicating-random-visual-stimuli-in-python-psychopy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!