我正在尝试创建两个同时显示的圆形光栅(一个水平的,一个垂直的)。在每次试验中,两个光栅的位置应该随机改变。
这是我到目前为止的代码,但我不知道如何添加“随机化”。
import psychopy.visual
import psychopy.event
win = psychopy.visual.Window(
size=[1200, 600],
units="pix",
fullscr=False
)
grating = psychopy.visual.GratingStim(
win=win,
units="pix",
size=[400,400]
)
grating.sf = 5.0 / 150.0
grating.mask = "circle"
orientations = [0.0, 90.0]
grating_hpos = [-300, 300]
for i_grating in range(2):
grating.ori = orientations[i_grating]
grating.pos = [grating_hpos[i_grating], 0]
grating.draw()
win.flip()
psychopy.event.waitKeys()
win.close()
import psychopy.visual
import psychopy.event
import random
win = psychopy.visual.Window(
size=[1200, 600],
units="pix",
fullscr=False
)
grating = psychopy.visual.GratingStim(
win=win,
units="pix",
size=[400,400]
)
grating.sf = 5.0 / 150.0
grating.mask = "circle"
orientations = [0.0, 90.0]
# Define the range within which you want the gratings to appear
# For example, if you want them within the central 800 pixels:
x_range = [-400, 400]
y_range = [-300, 300]
for _ in range(10): # This will show the gratings 10 times
# Randomize the position of each grating
grating_hpos = [random.uniform(*x_range), random.uniform(*y_range)]
grating_vpos = [random.uniform(*x_range), random.uniform(*y_range)]
for i_grating in range(2):
if i_grating == 0:
grating.ori = orientations[i_grating]
grating.pos = grating_hpos
else:
grating.ori = orientations[i_grating]
grating.pos = grating_vpos
grating.draw()
win.flip()
psychopy.event.waitKeys()
win.close()
This code does the following:
-
Imports
random
: This module is needed for generating random numbers. -
Defines
x_range
andy_range
: These variables control the area where the gratings can appear. Adjust the values to change the area. - Loop for multiple trials: The code now includes a loop that runs for 10 trials. In each trial:
-
Randomizes
grating_hpos
andgrating_vpos
: For each grating (horizontal and vertical), it generates random x and y coordinates within the specified ranges usingrandom.uniform()
. - Draws the gratings: It sets the orientation and position of each grating based on the randomized values and draws them.
- Waits for a key press: After each presentation, it waits for the user to press a key before proceeding to the next trial.
Now, each time you run the code, the two gratings will appear at different, random positions within the defined area.
标签:python,psychopy From: 78789886