我需要在 x 次试验中随机选择两个值 (300,-300),我需要这两个值在 50% 的时间内被选中。
作为替代方案,我尝试创建一个列表,其中两个值多次出现(5 x“300”,5 x“-300”),然后每次都打乱列表并循环遍历它。由于当前的代码结构和其他由此产生的问题,我无法这样做。我也是 python 新手。
def one_stim_testing():
facestims = choice(["-300", "300"])
punktestims = choice(["-300", "300"])
visual.ImageStim(win, "emo" + str(choice(range(23))) + ".jpg",
size=(300, 300), pos=(facestims,0)).draw()
if facestims is "300":
visual.ImageStim(win, "neutral" + str(choice(range(23))) + ".jpg",size=(300, 300), pos=(-300,0)).draw()
else:
visual.ImageStim(win, "neutral" + str(choice(range(23))) + ".jpg", size=(300, 300), pos=(300,0)).draw()
win.flip()
core.wait(.5)
win.flip()
visual.ImageStim(win, "vertical.jpg",
size=(300,300), pos=(punktestims,0)).draw()
if punktestims is "300":
visual.ImageStim(win, "horizontal.jpg",
size=(300, 300), pos=(-300,0)).draw()
else:
visual.ImageStim(win, "horizontal.jpg",
size=(300, 300), pos=(300,0)).draw()
最佳答案
做这样的事情:
# Set up stimulus container once. Saves memory and ressources.
# Do not generate new during runtime.
image1 = visual.ImageStim(win, size=(300, 300))
image2 = visual.ImageStim(win, size=(300, 300))
image_horizontal = visual.ImageStim(win, image='horizontal.jpg', size=(300, 300))
image_vertical = visual.ImageStim(win, image='vertical.jpg', size=(300, 300))
# Generate factorial sequences
# This is a list of dictionaries
# There are better ways to do it in this particular example,
# but this approach scales well to all sorts of trial structures.
REPETITIONS = 5
trials = []
for facestim in [-300, 300]: # For each of these coordinates
for punktestim in [-300, 300]: # ... and for each of these coordinates
for _ in range(REPETITIONS): # ... and for each repetition:
trials.append({'facestim': facestim,
'punktestim': punktestim}) #... add this combination
# Randomize order
random.shuffle(trials)
#print(trials) # If you want to inspect what happened
# Showtime! Now loop through the factorial trials:
for trial in trials:
# Set image 1
image1.image = 'emo%i.jpg' %choice(range(23))
image1.pos = (trial['facestim'], 0)
image1.draw()
# Draw image 2 (the opposite side)
image2.image = 'neutral%i.jpg' %choice(range(23))
image2.pos = (-trial['facestim'], 0) # The other coordinate
image2.draw()
# Show it
win.flip()
core.wait(0.5)
win.flip()
# Show horizontal and vertical in the correct positions
image_vertical.pos = (trial['punktestim'], 0)
image_vertical.draw()
image_horizontal.pos = (-trial['punktestim'], 0) # The other side
image_horizontal.draw()
# Perhaps a win.flip() here?
(代码未测试,因为我没有刺激)
关于python - 随机选择: how can I get a weighted presentation for x trials?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57427799/