| Reference | Downloads | Github

How to dynamically present multiple stimul in builder mode?

OS Win10

PsychoPy version 2020 1.3

What are you trying to achieve?:
I am using the builder to create a working memory task where multiple triangles are simultaneously presented in random locations in a study display. I can do this in the coder, but am using the builder because I would like to eventually run this online through Pavlovia.

What did you try to make it work?:
At the start of my study display routine, I use a code component to create arrays containing stimuli proporties, such as location, colours, orientations etc. For example:

orientations = np.random.rand(1, 10)*360

One easy option is to create a triangle polygon component for every item, and refer to these arrays in the relevant field. For example, in the orientation field I can put:

orientations[0, 0]

I can change how many items are presented using this approach by defining the ‘set size’ as a condition, and making them either transparent or not.

alphas = np.zeros(15)
alphas[0:setSize] = 1

And in the polygon component’s opacity field:


The problem with this is that I need to create a new component for every possible item. If I decide to add more items in the future, I would need to add more components. I also find that after adding more than a few components, the program runs very slowly. I would prefer to be able to just change a variable some where (e.g., in an xlsx file containing the set size conditions) to dynamically change the number of items presented.

I can achieve this dynamically in the coder with:

# Define the shape of the polygon (a 0.8x2 triangle). 
verteces = ((-0.4, -1), (0, 1), (0.4, -1))

# Pick some random locations on a circle
locations = linspace(-pi+2*pi/15, pi, 15)
loc_indexes = np.random.permutation(15)
loc_rads = locations[loc_indexes]
X = (cos(loc_rads)* radius)
Y = (sin(loc_rads)* radius)

# Possible colours to choose from. 
colours = [[1, 0, 0], [1, 0.498039, 0], [1, 0.831373, 0], [1, 1, 0], [0.74902, 1, 0], 
[0.415686, 1, 0], [0, 0.917647, 1], [0, 0.584314, 1], [0, 0.25098, 1], 
[0.666667, 0, 1], [1, 0, 0.666667], [0.968627, 0.72549, 0.72549], [0.905882, 0.913725, 0.72549], 
[0.72549, 0.929412, 0.878431], [0.72549, 0.843137, 0.929412], [0.862745, 0.72549, 0.929412], 
[0.560784, 0.137255, 0.137255], [0.560784, 0.415686, 0.137255], [0.309804, 0.560784, 0.137255], 
[0.137255, 0.384314, 0.560784]]

# Randomise the indexes of these colours. 
col_indexes = np.random.permutation(15)

# Loop through each item you want to present. 
for N in range(setSize):
    stim = visual.ShapeStim(win, lineColor = None, 
    fillColor = colours[col_indexes[N]], fillColorSpace = 'rgb', 
    vertices = verteces, 
    pos = (X[N], Y[N]), 
    opacity = 1)

    # Set the orientation. 
core.wait(0.5) # Present the display for however long you want (half a second). 

What specifically went wrong when you tried that?:

This works in the coder. However, when I paste the above code into the ‘begin routine’ tab of a code component in the builder, nothing appears. I also receive this message in the runner:

Traceback (most recent call last):
  File "C:\Users\ ... \Experiments\", line 207, in <module>
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\", line 584, in draw
    self.verticesPix.shape[0] > 2 and
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\", line 539, in verticesPix
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\", line 520, in _updateVertices, units=self.units)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\tools\", line 95, in convertToPix
    return unit2pixFunc(vertices, pos, win)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\tools\", line 38, in _deg2pix
    return deg2pix(pos + vertices, win.monitor)
ValueError: operands could not be broadcast together with shapes (2,) (3,2,15)