psychopy.org | Reference | Downloads | Github

Presenting Randomly Selected Stimuli in Different Loops Involving Conditional Branching

URL of experiment:

Description of the problem:
Participants are first presented with a photograph on one initial routine and then depending on the participant’s response made on this first routine (1,2,3,4,5,6 or x), they are presented with one of seven loops where the same photograph is presented again alongside some rating scales. I have included a snippet from the flow (this only shows the first photo routine then one of the seven loops):

I currently have the conditional branching included in an End Routine code component in initial photo routine (“SetLineup” routine). This works fine. Participants are branched to the appropriate loop depending on their key press.

if (key_resp.keys == '1'):
            DoTradConfID1=1
            DoTradConfID2=0
            DoTradConfID3=0
            DoTradConfID4=0
            DoTradConfID5=0
            DoTradConfID6=0
            DoTradConfReject=0
elif (key_resp.keys == '2'):
            DoTradConfID1=0
            DoTradConfID2=1
            DoTradConfID3=0
            DoTradConfID4=0
            DoTradConfID5=0
            DoTradConfID6=0
            DoTradConfReject=0
elif (key_resp.keys == '3'):
            DoTradConfID1=0
            DoTradConfID2=0
            DoTradConfID3=1
            DoTradConfID4=0
            DoTradConfID5=0
            DoTradConfID6=0
            DoTradConfReject=0
elif (key_resp.keys == '4'):
            DoTradConfID1=0
            DoTradConfID2=0
            DoTradConfID3=0
            DoTradConfID4=1
            DoTradConfID5=0
            DoTradConfID6=0
            DoTradConfReject=0
elif (key_resp.keys == '5'):
            DoTradConfID1=0
            DoTradConfID2=0
            DoTradConfID3=0
            DoTradConfID4=0
            DoTradConfID5=1
            DoTradConfID6=0
            DoTradConfReject=0
elif (key_resp.keys == '6'):
            DoTradConfID1=0
            DoTradConfID2=0
            DoTradConfID3=0
            DoTradConfID4=0
            DoTradConfID5=0
            DoTradConfID6=1
            DoTradConfReject=0
elif (key_resp.keys == 'x'):
            DoTradConfID1=0
            DoTradConfID2=0
            DoTradConfID3=0
            DoTradConfID4=0
            DoTradConfID5=0
            DoTradConfID6=0
            DoTradConfReject=1
elif (key_resp.keys == 'X'):
            DoTradConfID1=0
            DoTradConfID2=0
            DoTradConfID3=0
            DoTradConfID4=0
            DoTradConfID5=0
            DoTradConfID6=0
            DoTradConfReject=1
else:
            DoTradConfID1=0
            DoTradConfID2=0
            DoTradConfID3=0
            DoTradConfID4=0
            DoTradConfID5=0
            DoTradConfID6=0
            DoTradConfReject=1

This code works fine when I have a pre-selected png photo inserted into the image component on the set lineup and then again in each loop. The problem with this is that I have to create many experimental files because I have different photographs I need to use. I then use Qualtrics to randomize participants to an experiment file. Ideally, I would like one image to be randomly selected from a list of photographs. I successfully programmed this in a separate tester experimental file, but I am unsure of how I should implement this in the current study that involves conditional branching and different loops. How do I code this study so that a randomly selected photo is presented on one initial routine then again in different loops?

Below is information on how I programmed the randomizer. Again this works on it’s own, but I do not know how to incorporate it into the main project (e.g. where to put the loops, if I am missing any additional code, how to code so that the randomly selected photo is presented again on a subsequent loop). I tried incorporating this randomizer into the main study and have tinkered around but I cannot get the code to work. I the photographs do not load and I am presented with multiple trials rather than just one trial, for example.








Thanks so much!

Hi There,

To randomly select an image I believe you need something like this:

imageList = ['image1.png', 'image2.png', 'image3.png']
shuffle(imageList)

then in your image component, in the path field use:
$imageList[0]

Does that achieve what you want?

Which routine is it that you are presenting the randomly selected image in?

Thanks,
Becca

Yes, this worked for me! Thank you.

1 Like

Hi Becca,

Your solution worked fine offline, but when I uploaded the experiment to Pavlovia, I received the following error message every time I tried to pilot the experiment:


Currently, I have all of the png image files in the same folder as the experiment itself.
Would you happen to know what I need to do to fix this?

Thanks so much!

For resources (e.g. images) set in code you need to “attach” them via Experiment Settings / Online.

1 Like

This worked! Thank you! I appreciate your help.

Hi again, one last thing. It looks like the shuffle function uses traditional randomization. I ran through the study to test the randomization and found that the randomization is not exactly equal. Some photos show up more than other photos. Is there a way to make the randomization more equal? Perhaps through block randomization like Qualtrics uses? Thanks so much!

If you want equal distribution then shuffle once and then use the trials.thisN value instead of the 0th value, or .pop() a value from the list.

Hi, I have tried to piece together some code for this based off of what I have found in other threads. I am receiving some errors. Could you please advise? I have the following in the ‘Begin Routine’ section

imageList = ['Slide1.png', 'Slide2.png', 'Slide3.png', 'Slide4.png', 'Slide5.png', 'Slide6.png']
if trials.thisN == 0:
    shuffle(imageList)
# get a value for this trial:
imageList.pop()
# store it in the data:
thisExp.addData('imageList', imageList)

my image component path is:

$imageList[trials.thisN]

I have tinkered around with variations of the code I included above but I still received errors of different types each time I modified the code. Thanks for your help! I am inexperienced with Python, so I appreciate your help and patience.

Try the following

Begin Experiment

imageList = ['Slide1.png', 'Slide2.png', 'Slide3.png', 'Slide4.png', 'Slide5.png', 'Slide6.png']
shuffle(imageList)

Begin Routine

# get a value for this trial:
thisImage = imageList.pop()
# store it in the data:
thisExp.addData('thisImage', thisImage)

Then use $thisImage