psychopy.org | Reference | Downloads | Github

Presenting/repeating multiple components (objects) using a loop within the same routine

Hi PsychoPy users/experts,

I am intending to present multiple (similar*) objects/components on the screen at the same time (i.e. within the same Routine). For example, in the attached picture I present the numbers (1 to 12) using multiple ‘text’ objects and the corresponding tick-marks next to them on the y-axis using multiple ‘polygon’ objects (with 4-vertices, like a small rectangle).

*The ‘numbers’ (1-12) are only different in their values and y-coordinate position.

Certainly I can repeatedly create 12 ‘text’ objects for the numbers and 12’polygons’ for the tick-markers. But, this would not be very efficient as I will have to do the same for a number of routines throughout the experiment.
As a result I am trying to use a for-loop to create a list when presenting them.

This is what I have done so far.


#at the very beginning of the script, I created a list of variable names, values, y-coordinate for the 'text' objects
ynum_name= ["ynum1_name", "ynum2_name", "ynum3_name" ... "ynum12_name"]

ynum=range(1,13)
yco=range(-1,11)


#Then, in the  parts of the script that 'Initialize components for a Rountine', 
#I wrote a for-loop intending to 'initialise' each of the number 1-12 'text' objects one after the other. 

for xyz in ynum_name:
    xyz = visual.TextStim(win=win, name=xyz,
    text='default text',
    font=u'Arial',
    units='cm', pos=[0,0], height=0.75, wrapWidth=None, ori=0, 
    color=u'blue', colorSpace='rgb', opacity=1,
    depth=-1.0);


#Correspondingly, I do the same when 'Prepare to Start the Routine' 
#a for-loop to set 'Text' and 'Position' of each of the number 'text' objects
for i, ynumpoly in enumerate(ynum_clim):
         ynumpoly.setText(ynum[i])
         ynumpoly.setPos([-7, yco[i]])

#Similarly, to 'Start the Routine'
for i, ynumpoly in enumerate(ynum_clim):
       if t >= 0.0 and ynumpoly.status == NOT_STARTED:
               # keep track of start time/frame for later
               ynumpoly.tStart = t
               ynumpoly.frameNStart = frameN  # exact frame index
               ynumpoly.setAutoDraw(True)

This time I have got a error as fellows:
value = numpy.array(value, float)
ValueError: setting an array element with a sequence.

I think my question is how we can actually run a list of psychopy.visual.TextStim in a loop like a list of strings and numbers. Can it be done at all?

Hopefully some of you may have some ideas on it. Many thanks.

1 Like

Here’s how I’d approach this for drawing the labels

tick_label = visual.TextStim(win=win, name='tick_label', text='default text', 
                             font=u'Arial', units='cm', pos=[0,0], height=0.75,
                             color=u'blue', depth=-1.0);

for i in enumerate(ynum):
    tick_label.text = str(ynum[i]))
    tick_label.pos = [-7, yco[i]]
    tick_label.draw()

win.flip()

You can do the same procedure for the tick mark polygon(s) before flipping. Your mileage may vary though, as it looks like you’re using the Builder interface

I am getting the same error message “setting an array element with a sequence.”

I am trying to use independent randomization in a dot probe task. Specifically, trying to randomly present paired word stimuli on the left and right and randomly present the dot probe on the left or right.

Here is my code:
#begin experiment
positions = [[-0.3, 0], [0.3,0]]
orientation = [0,360]

#begin routine
randomIdx=randint(0,2)
posrel=positions[randomIdx]
posirrel=positions[1-randomIdx]

randomIdx=randint(0,2)
orientrel=orientation[randomIdx]
orientirrel=orientation[1-randomIdx]

if orientrel==360:
corrAns=‘left’
else:
corrAns=‘right’

I am a novice to PsychopY and an even bigger novice to using code.
Any advice would be greatly appreciated!