Randomly Loop through Routines

OS: Win10
PsychoPy version: v2021.1.2

What are you trying to achieve?:


Hey, above you can see my flow. The loop ‘Trial’ is linked to an Excel file that supplies it with either pictures of words or of shapes. The loop is set to choose words at random. Each routine (‘frame’) in the loop presents the pictue (of word or shape) for a certain length of time - ex. ‘Frame_1’ does it for a duration of one frame, etc.

What I want to achieve is loop through each routine in a random manner with random pictures (from the Excel) until each picture has gone through each duration. Say if I have 30 pictures I want to start with a random picture (ex. pic number 7) presented at a random duration (ex. Frame_3), followed by another random picture in a random duration. Thus, until all pictures have gone through all durations randomly.

Currently the same random picture from the Excel goes through the given durations sequentially (say pic 7 runs throguh Frame_1, Frame_2, etc). How do I randomize this? In other words, how do I make the loop start at a random routine, go through this single routine (ex. Frame_3), change the picture it uses and start at a random routine again, until all pictures have gone through all routines?

I hope I was clear enough. I tried looking at coding solutions on the forum but could not figure anything out. Thanks!

So you want each picture to be presented 5 times consecutively, but with shuffled rather than sequential durations?

I would recommend having a single Frame routine and an inner loop pointing at a spreadsheet containing your durations. I think you might be able to do it without code components. The trial would take the image from the outer loop and the duration from the inner loop.

Hey Wakecarter,

Thanks for the quick reply!

I actually want each picture to be presented inconsecutive 5 times with shuffled durations. First time: picture 7 presented with duration A. Second time: picture 16 presented with duration C. And so on. Until each picture is presented in all 5 durations in a completely random manner. Each time different picture, different duration (it is not a problem if some repetition happens throughout the randomness, say we see pic7 at duration B followed by same pic at duration A - as long as this is a rare coinsidence).

Also, since I am quite new with Psychopy, could you specify how exactly I can link an Excel spreadsheet of frame (not time) durations with the builder menu. I think I can link it as conditions in the loop menu but how do I signify that frame durations are at stake?

Thank you so much!

That sounds like a single loop with a single Excel spreadsheet containing columns for picture and duration.

I tried to do that and in “duration” column set integers (supposed to indicate number of frames). Then in picture duration I let it start at 0 and for stop time I chose duration(frames) next to it I input $duration to indicate that it should take the value of the variable in the spreadsheet (I tried with different variable names but it kept not working). The error I get is just a syntax error with Psychopy being upset about the dollar sign ($).
How do I tell Psychopy to take the value from the excel spreadsheet variable called “duration” (the spreadsheet I have linked to the inner loop by listing its directory in the conditions tab from Trial Properties) and use it as indicator for the number of frames I want the picture to last?

Thank you!

It should just work (possibly without the dollar)

However, you are using a buggy version of PsychoPy so I’d recommend that you upgrade to at least 2021.1.3

God bless you! I spent days on this and it finally seems to work. I am not even using an inner loop. One loop set at random over one routine with an excel list that has both a “stimuli” column and a “time” column (along with “trigger” columns that I need for the EEG) seems to do the job. So simple!

Thank you so much. I hope no further obstacles come here on.