Presentation of picture based on previous response

Hi! So far, I’ve only built experiments in the Builder, but I think what I need now is the coder. So, it’s a simple picture selection task. 2 pictures are displayed on the screen. If the participant chooses one of them, the chosen picture needs to be displayed in the next screen together with a new picture, etc.

I think you can’t design it in the builder. As for the Coder, the way I see it is that in the beginning of the routine I would need to create some variable ‘chosen_pic’, and at the end of the routine I would have to assign the key response that represents a certain picture to this variable.Then I need to somehow tell PsychoPy to show that chosen picture in the next trial together with a new picture from some sort of a list.

Although I have experience in Python, I’m not sure how to build it technically. I’d be glad if you could provide some hints.

In almost all cases I would recommend you use Builder and then add code components to customise, rather than writing in Coder.

Have a look at some of my demos to see how variables can be used in code components and other builder components

You could have ‘left’ for left picture and ‘right’ for right picture as keyboard responses. Then use a code component to set chosen_pic as image1 or image2. Then in Begin Routine, set image1 (or image2) as chosen_pic and the other one as newImage (if this is the name of your column of image names in your spreadsheet).

Hi! Thanks for the answer! Just to clarify, are all images supposed to be listed in one column in the Excel spreadsheet (no matter whether left or right)? And in the builder, would you still have 2 visual components (one for left and one for right)?

My understanding is that on each trial there is only one new image. If so, then all the images should be in one column in your spreadsheet.

Since you want two images on screen simultaneously there should be two image components.

Is the new image always on the left?

Thanks a lot once again! At the moment, I have 2 visual components: image1 and image2. Both are set to take pictures from the column newImage ($newImage). Then I added a code component as you said: in the Begin Routine, I have written:

image1 = chosen_pic
image2 = newImage

Finally, in the End Routine, I have:

if key_resp.keys is 'left':
    chosen_pic = image1
if key_resp.keys is 'right':
    chosen_pic = image2

Unfortunately, at the moment I see the same pictures displayed both on the left and on the right. When I press either ‘left’ or ‘right’ arrow keys, I proceed to the next trial where identical pictures are shown on both sides. What am I missing?

If your image components are called image1 and image 2 then set them to display $pic1 and $pic2 and in code put:

pic1 = chosen_pic
pic2 = newImage

then in End Routine

if key_resp.keys is 'left':
    chosen_pic = pic1
if key_resp.keys is 'right':
    chosen_pic = pic2

Now it says:

Traceback (most recent call last):
File “/Users/arminegaribyan/Desktop/Image_test_conditional/”, line 131, in
NameError: name ‘pic1’ is not defined

Is that because the column name is actually called newImage? If it sounds too abstract, I’m attaching the experiment file with pictures and the Excel sheet (archived). (252.4 KB)

I managed to make it work, although I had to change a few things. First of all, I was constantly running into wrong path issues for the images, so I had to play around with text. So, that’s what I’ve changed:

  1. Moved the code component to the top of the routine.
  2. In the Begin Experiment tab of the code component, I added chosen_pic = None because the first routine will not have any chosen picture and I was running into errors like chosen_pic is not defined.
  3. Then in the Begin Experiment tab of the code component I added and if statement to account for the absence of the chosen_pic:
if chosen_pic:
    pic1 = chosen_pic
    pic2 = newImage
    pic1 = ''
    pic2 = newImage
  1. Finally, the End Routine tab stays as earlier suggested.

Ideally, there are still a few drawbacks: 1, I start with one of the sides empty, but I’m sure I can fix this by introducing a random picture for one-time use; second, 2. the chosen pictures appear always on the left, so this could be also probably counter-balanced in the future.

I then tested this with visual components and it worked too. Just instead of pic1 = ' ', you need to write pic1 = None, otherwise PsychoPy can’t recognise a picture, it’s just text.

Thanks for the tips @wakecarter , I’m so glad it has been resolved. :smiling_face_with_tear: