Create visual array of pictures with presented and new objects

I am building an object memory task whereby 10 pictures are presented consecutively followed by a 30 second video. For responding, the participant will see the 10 original pictures plus 10 distractor pictures and click all of the pictures that were apart of the originally viewed 10. All pictures have the opportunity to be test and distractor images.

To create this array I will need to call upon the 10 pictures presented and then 10 more from the original list of pictures. Can someone point me in the right direction for solving this?

Hi @sarahnpope, how do you choose which images are the targets for the first presentation? From which list and how are they sampled?

It is randomly chosen from a list

Ok, then this could work for you. This code samples 20 elements from your full list and then divides them into targets and distractors evenly.

ListOfStimuli = range(100) 
Selection = random.sample(ListOfStimuli , 20)
Targets = Selection[0:11]
Distractors = Selection[10:21]

Let me know if that works!

Thank you!!! I will add that this week and see how it goes. I’ll let you know!

Will I need to adjust some of these variables in the code? Like, my list of stimuli is in an excel sheet, should I replace "ListOfStimuli with that excel file name?

Ok, then you need to do it a bit differently:

For both loops, use the same xlsx. Before the first loop, run

TargetsAndDistractors = random.sample(range(n_rows), 20)
Targets = TargetsAndDistractors[0:11]
  • n_rows needs to be replaced with the number of rows in your xlsx file

Then, use $Targets in the “selected rows” field of your first loop, and $TargetsAndDistractors for the second loop. Set both loops to “random”.

If you want to mark the trials of your second loop as being target or distractor trials you can do

if trials.thisIndex in Targets:
    thisExp.addData("trialtype", "target")
elif trials.thisIndex in TargetsAndDistractors:
    thisExp.addData("trialtype", "distractor")
  • “trials” needs to be replaced with the name of your second loop

in “End routine” of each trial.

Would $Targets and $TargetsAndDistractors be an image component? Or would I need to have this as a condition in the excel file?

No, these are the indices of the rows that are chosen to be targets and distractors. They determine, which rows of your xlsx are used as targets and distractors. This is why they have to be used in “selected rows”.

1 Like


I deleted my previous posts because I made some mistakes that made them no longer relevant. I am getting the following error message:

TargetsAndDistractors = random.sample(range(7), 6)
AttributeError: ‘builtin_function_or_method’ object has no attribute ‘sample’

Ah yes, sorry, you have to preface the code by

import random

It is now saying “Targets” is not defined.

Did you change any part of the code? I see that you changed the number in the first line. This can lead to errors if you don’t also adopt the second line.

Yes, I’m starting with a practice block first, which will only have 6 total images (3 targets and 3 distractors). The code looks like this:

import random
TargetsAndDistractors = random.sample((range(7), 6))
Targets = TargetsAndDistractors[0:5]

In this case, your code should look like this

import random
TargetsAndDistractors = random.sample(range(6), 6)
Targets = TargetsAndDistractors[0:3]

But this does not explain, why Targets should be undefined… Maybe it was because of the brackets you added (compare the codes).

1 Like

Removing the extra brackets and switching the numbers did not change anything:

File “C:\Users\sarah\Desktop\OMT\”, line 141, in

trialList=data.importConditions(‘omtprac.xlsx’, selection=Targets),

NameError: name ‘Targets’ is not defined

################ Experiment ended with exit code 1 [pid:11320] #################

2837.7881 INFO Loaded monitor calibration from [‘2022_09_14 11:58’]

I really appreciate all your help with this

Ah, one important detail: You have to run the code BEFORE the loop gets started, not within the loop.

Okay!! It worked! Well, sort of. The program is starting but it only shows one picture and then immediately goes to the response array and shows just the same picture.

Could you upload a screenshot of what your flow looks like?