| Reference | Downloads | Github

Building a 10x10 array with elements placed in random positions

I’d like to build a numerosity discrimination task. In each trial, subjects are presented with an imaginary 10x10 array with 31 to 70 asterisks placed in random positions. I could prepare my stimuli outside of PsychoPy, but I think I should be able to build them using psychopy.visual.elementarray without dropping frames. Could you give some advice about the most efficient way to build these stimuli?

Hi @Servant_Mathieu, here is an example of using ElementArrayStim to generate your stim. It works by generating a list of colors to determine whether elements should be presented. The color list created is a list of binary values conforming to your constraint (i.e., 31 to 100 inclusive visible asterisks) which set the rgb value of elements to white(1) or grey (0), where grey matches the color of the background. This is the code, but can easily be inserted into a code component in Builder.

from psychopy import visual
from numpy.random import shuffle, randint

#Create window 
win = visual.Window([1024, 768], units='pix', monitor='testMonitor')

N = 100  # 100 elements
fieldSize = 500  # Field size in pixels
elemSize = 20 

# Generate element array stim
globForm = visual.ElementArrayStim(win,
    nElements=N, sizes=elemSize, sfs=0,
    xys=None, colors=[1,1,1], colorSpace='rgb')

# Generate list of colors based on displayN
for trial in range(5):
    cols = []
    xys = []
    # Generate random number between 31 and 101 (not including 101)
    displayN =  randint(31,101)  # Number of visible elements
    cols = [1] * displayN + ([0] * (100-displayN))
    cols = [[i,i,i] for i in cols]

    # Generate positions
    for i in range(-250, 250, 50):
        for j in range(-250, 250, 50):
            xys.append([i, j])
    # Set pos and color
    globForm.xys = xys
    globForm.colors = cols

    # Present stim
    for frame in range(100):
1 Like