Presenting in pairs 3 sets of images

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2022.2.5
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:
I am developing an incentivized facial emotion recognition task, and I want the subjects to see a pair of photos of people portraying different emotional expressions. For each trial, the face & the emotion will be the same, but in a different moment (gradient 50, 70 or 90). As you can see, I have 3 sets of images, that are disposed as 3 different columns in an .xlsx file. I want one of the image components to always be the gradient 50, and the other one of the pair has to be randomized between gradient 70 and gradient 90. Also, I want the images components to randomize in their position in the screen, that is, image component from gradient 50 to be sometimes in the right, and others in the left - the same goes for gradient 70/90.

What did you try to make it work?:
At the moment, I have an experiment that works as I described, but only presenting images from the columns corresponding to only 2 gradients, i.e., 50 & 70 (or 90, if I change it), as I do not know how to make the second image component to vary between the 2 columns (70 & 90) so that the subjects are sometimes presented with an image from gradient 70 (+ gradient 50), and others with an image from gradient 90 (+ gradient 50) - and doing so from the Builder.

The following is a screenshot of the Builder:

And this is one of the image components:

Here I show some of the Excel file so you can see the columns corresponding to each gradient:

Do let me kwow whether something is unclear.

Thank you in advance

This is kind of dirty, but should work:

Add a code component at the bottom and insert the following into the “Begin Routine” tab:

gradients_70_90 = [Stimuli_2, Stimuli_3] # the two alternatives
rnd_gradient_70_90 = np.random.choice(gradients_70_90, 1) # pick one of the alternatives at random
rnd_gradients = np.append(rnd_gradient_70_90 , Stimuli_1) # add the fixed stimulus
np.random.shuffle(rnd_gradients) # get random order of fixed and picked alternative

# set images with random stimuli

I’m sure theres a more elegant way.


I don’t know whether my solution is more elegant.

Insert the following code to a code-component at the top of your routine.

#determine stimulus to add
stim = [Stim70,Stim90]
stimulus = stim[0]

#determine variable stimuli positions
xpos = [0.25, -0.25]
xpos50 = xpos[0]
xpos7090 = xpos[1]

#save to data file
thisExp.addData("stimulus", stimulus)
thisExp.addData("xpos50", xpos50)
thisExp.addData("xpos7090", xpos7090)

Use stimulus as variable when setting the Image (see below)

Add the random location xpos7090 to the Position parameter of the Image7090. Do the same for the Position parameter of Image50, use xpos50 instead of xpos7090.

I have used the component names Image50 and Image7090 simply for the sake of clarity. IMHO this makes more sense if you want random positions for your stimuli.

Best wishes Jens

Thank you, Jens, for your answer (and your tip on the clarity).

I did just what you said, but I am getting the following error: NameError: name ‘xpos50’ is not defined

Here is the code:

Here is the image component for gradient 50:

I tried setting different position coordinates, as I would want them to be at either (0.4,0) or (-0.4,0), but nothing changed.


You need to set the x and y positions in your image properties, surrounded by “( …)”, e.g. (xpos50, 0). For the program to run, the values don’t really matter.

Best wishes Jens

Thank you umb for your answer.

I tried copying your code to a code component, but I realized I did not know what should I be putting in the image field of the image component…Which is the variable?



It does not matter, you can even leave it blank. The image is set in the code snippet.

Although I’d suggest you use Jens’ approach as it tells you in the data which stimulus was used and where it was placed.
The error you encounter (NameError: name ‘xpos50’ is not defined) is because for Jens’ approach you have to put the code component at the very top of the component list (right click on component → “move to top”).

Thank you again, umb

I am using Jens approach, and have done what you recommended. But I am still encountering problems: NameError: name ‘Stim70’ is not defined

I am running the experiment correctly until this routine has to appear, when it crashes and I get the above mentioned error.

I see. Jens solution assumes, you rename the variables in the xlsx as well:
Go into your Excel file and rename the colums “Stimulus1”, “Stimulus2” and “Stimulus3” to “Stim50”, “Stim70” and “Stim90”.

For the Image_50 component, set the image to Stim50. Leave everything else as is.


Sorry for not using your variable names, but umb’s explanation is correct. It works both ways. Changing the names I used to the ones you use also works.

Best wishes Jens

It worked! Many thanks to both of you, umb and Jens!