Shuffling image locations with conditions

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

OS (e.g. ): macOS Catalina 10.15.7
PsychoPy version (e.g. 1.84.x): 2020.2.5
Standard Standalone? (y/n) If not then what?: Yes
What are you trying to achieve?:
I want to present 4 images (a target, a competitor, 2 filler images) in 4 quadrants and randomly assign each image a location. However, I need to implement certain restrictions on which images can appear adjacent to each other. Specifically, the target image and the competitor image cannot be diagonal from each other. I am not sure how to get about doing this simply.

Sample Within-language
For example in this image, the comb (target) and corn (competitor) cannot appear diagonal from each other. But apart from that condition, everything else should be randomized.

What did you try to make it work?:
I have added a code compoment (see below) that successfully shuffles all the image locations. However, I am not sure how to implement the rule specified above. I have 4 image components (target, competitor, filler1, filler2) that use $imglocation to determine the position of the image.

Q1 = (-.5, .33)
Q2 = (.5, .33)
Q3 = (-.5, -.33)
Q4 = (0.5, -.33)

imglocation = [Q1,Q2,Q3,Q4]

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.

You could implement this using if statements, for example:

# Choose location of one stimulus
imglocation = [Q1,Q2,Q3,Q4]
targetStim.pos = randchoice(imglocation)
# Define possible locations for second stm
if targetStim.pos == Q1:
    img2location = [Q2, Q3]
elif targetStim.pos == Q2:
# Choose location of second stim
competitorStim.pos = randchoice(img2location)

(this requires import numpy.random.choice as randchoice in the Begin Experiment or Before Experiment tab)

However, with only 4 options, it’s probably cleaner to pre-define the possble combintions in an excel file and use that as the conditions file for a loop. So have one column which is the possible position of a target stim, and another which is a matching possible position of a competitor stim. So:

targetPos competitorPos
Q1 Q2
Q1 Q3
Q2 Q1
Q2 Q4
Q3 Q1
Q3 Q4
Q4 Q2
Q4 Q3

Thank you so much for this! I was getting stuck on actually generating this into code. I will give this a try.

If I’m going to be running this online (Pavlovia), do you recommend avoiding the import functions and using a file to randomize position?

Hello, thank you for your help on this. I’ve tried implementing this, but I am having trouble adding the excel file. I already have an excel file for the actual images (N=60) that is used in a loop. How would I add another file to select the 8 options above randomly?

In other words, where do I put the 2nd excel file to loop image positions and select one random row (i.e., a random position) of the 8 fixed options?

Have a look at my Brookes template experiment which contains an example of preloading stimuli into a list. You can have the positions loaded fromExcel into a list which you then read during your main trials loop.

1 Like