psychopy.org | Reference | Downloads | Github

Randomise four images in one screen

Hi there,

I am programming a psychology experiment. In the first step, I need to put four images into one screen for 1s. One of the pictures needs to be selected randomly from the list and the other three pictures are 72, 144 and 216 away from the target. For example, if the target picture is 1.jpg, then the second picture is 73.jpg and the next two are 145.jpg and 217.jpg. Can I know how can I set it? Also, the second step will be one of the four conditions, can I know how can I pair the images show in the first step to the conditions in the second step. Thanks in advance!

In your .csv file, set it up like this:

image_number
1
2
3

and set the loop to be “random”. Ensure that the largest number doesn’t exceed the highest image file name number minus 216.

In the image file field of the first image stimulus, put something like this (using the magic of Python’s f-strings Python 3's f-Strings: An Improved String Formatting Syntax (Guide) – Real Python):

f'{image_number}.jpg'

and then in the others, do something like this:

f'{image_number + 73}.jpg'

and so on. Ensure that the image file field is set to update “every repeat”.

If the images are in a subdirectory called, say, images, amend to:

f'images/{image_number}.jpg'

etc

Sorry, I don’t really understand the second part of your question.

Brilliant! Thanks, Michael. I would like to ask a follow-up question, I have 360 images, if the image number of third and fourth images are exceed 360 (for example, the image number of the third image is 404), and then I would like the programme pick the image which is the total number is minus 360, that is image number 44 in this case (i.e.404-360=44). How can I do?

I am sorry I have not explained my situation clearly in the second part of my previous question. There are two steps in my experiment. The in the first step, I need to show four images and ask the participant to remember them. After that, I will show any one of that four images I have presented in the first step. Therefore, I would like to know how can I pair the images I have presented in the first step and second step. Also, if I need to implement four different conditions into the images I will present in the second step, how can I do that? Thanks a lot!

To do this requires a few more steps of logic than can be achieved with simple expressions in the filename field of a stimulus. So instead, insert a code component (from the “custom” component panel). This allows you to insert multiple lines of code that will run at a specified time. In this case, put the code in the “begin routine” tab of the code component, and ensure the code component is above any of the image stimuli (so the latter will get to to refer to the latest-calculated values). You can change the order of components by right-clicking their icons. Something like this:

image1_file = f'{image_number}.jpg'
image2_file = f'{image_number + 72}.jpg'

image3_number = image_number + 144
if image3_number > 360:
    image3_number = image3_number - 360
image3_file = f'{image3_number}.jpg'

image4_number = image_number + 216
if image4_number > 360:
    image4_number = image4_number - 360
image4_file = f'{image4_number}.jpg'

# randomly select one to show again:
repeat_image_file = choice([image1_file, image2_file, image3_file, image4_file])
# record it in the data:
thisExp.addData('repeat_image', repeat_image_file)

# NB you might also want to record all four image names.

To get access to the random choice() function, you will need to insert this line in the “begin experiment” tab:

from numpy.random import choice

Now in your four image stimuli, just put $image1_file or $image2_file etc in the respective image fields, again set to update on every repeat.

On the follow-up routine, put $repeat_image_file in its image field.