Creating interleaved test sequence in builder (randomizing two variables within a loop while keeping the sequence of presentation)

I am trying to create an interleaved task where participants will need to complete the task with stimuli from two categories (A or B), the order for them to complete it is going to be interleaved (e.g.ABABAB or BABABABA). Right now, my experiment setup looks like this. Screen Shot 2020-10-15 at 9.46.54 AM
I am trying to figure out how I can randomly select the stimuli such that a stimulus will be randomly selected from category A, followed by a random stimulus from category B, and repeat the process until all the stimuli were selected. I cannot use blocking because I don’t want the stimulus to appear in blocks, but I am not sure if it can be done.

Thank you in advance for your help

Set the stimuli for categories A and B to be laid out like this:

stim1.png, stim2.png, stim3.png

within a single cell of your conditions file, so that each filename is separated by a “delimiter” - a character which will not show up in the file name (a comma will probably be fine). Then you can split these into a list and choose one at random using code. I would first use a Code component with this in the Begin Experiment tab:
from numpy.random import choice as randchoice
to make sure you have the necessary function to hand. Then create a Variable component whose Begin Routine value is:

and set the value of your stimulus to be the value of this variable. That way, you’re splitting by a delimeter and then choosing one value at random from the resultant list.

1 Like

Thank you so much for your help! I am sorry this probably is a rookie question, but I am a bit confused about how to transform the cell in my file into a list? Thank you in advance for your help!

The point where it becomes a list is this line:


So categoryAlist is the text from the cell as a string - by calling the internal method split you’re saying “split this string at every instance of the caracter I specify”, which in this case is , . The output of this function is a list, just the same as if you’d defined it using:

["stim1.png", "stim2.png", "stim3.png"]

So provided you have that line in there, all you need to do for the cell in your Excel file to be a list is put commas between the different items

1 Like

Thank you so much for your help! I am sorry that I have so many question. I am now able to randomly select one element at a time. However, I am still having trouble to repeat this process until the items were all randomly selected from the list. I have tried to repeat the outer loop but it would sometimes select the same item, I am wondering if you happened to have any idea on how I can do that.

Ahhh I see, what you’ll want to do then is store categoryAlist as a list somewhere - then rather than putting the whole thing into randchoice, use randint to create a random index and pop it.

For example:

categoryAlist = categoryAlist.split(",")
i = randint(len(categoryAlist))
choice = categoryAlist.pop(i)

Because pop removes the specified item from the list and stores it in whatever is in front of the = sign, meaning when you then go back to the list in the next iteration, the items already chosen are gone

1 Like

Got it, thank you so much for your help!


I hope you are well. I am sorry but I am still encoutering issue with the iteration
I am not sure how do I repeat the iteration. By putting the stimuli into one cell, it seems like Psychopy is only showing one stimuli rather than running until the whole list is over. Would you mind to provide some insight into that? Thank you so much for your help!

When you create the loop, setting nReps says how many times each cell is repeated - if you have 5 values in each cell, then setting nReps to be 5 will repeat that cell 5 times, so if one is used & removed by pop each repeat then 5 repeats means each value will be used.

Somehow, it doesn’t seems to be working in my case. Sometimes the same value within the cell/list is still repeated. Below is how I set up my experiment. I am wondering if you can help me with identifying where I got it wrong. Thank you so much.

Screen Shot 2020-11-10 at 9.15.38 AM

This is how I set up the loop

Ah, I see the problem! The linecategoryAlist = categoryAlist.split(",") needs to be in Begin Experiment so that it runs once rather than every repetition

When I put it into the begin experiment tab, it runs into the following error, I am not sure how I get around this.
categoryAlist = categoryAlist.split(",")
NameError: name ‘categoryAlist’ is not defined

Experiment ended.

Ah, of course, because categoryAlist is an each routine thing…

Okay, I think a better solution is to take a totally different approach: I think you’d be better off putting these routines in a loop within a loop. So you have one loop using a conditions file with “categoryAlist” in it, another with “categoryBlist”, and these loops are within one another. This way, it will cycle through all values in the inner loop with a value in the outer loop, then move on to a different value in the outer loop and cycle through all values in the inner loop for this value, and so on. Is this closer to the behaviour you want?

1 Like


Thanks for this advice, I think it is till a bit far from what I want to achieve. I kinda want the order to be like A1B1A2B2A3B3 rather than A1B1A2B1A3B1. Additionally, I am hoping to have the stimuli to be randomly selected from the list without repeating itself.

Do you have any clue for how to do it?

Oh I see, so you want to present A and B in pairs! That should just be a single loop then, with columns for A and B. It will select a random row from the conditions file (without repetitions, if you set that in the Loop settings) and will use the A and B values from that row. What wasn’t working when you did that before? I thought the problem was that you wanted to randomise them independently of one another

I apologize for the confusion. Yes, the main issue is that I want to randomize them independently of one another. For example, say category A has A1,A2,A3 and category B has B1,B2,B3. I would like to randomly select one stimuli from catgeory A and randomly select another stimuli from category B.
Another goal is to be able to randomize across the two blocks such that some times we have the order ABABAB and sometimes we have the order BABABA, but that is secondary.

I think this is a situation where it’s easier to describe in code than in words, is this pretty much what you’re trying to do?
interleavedDemo.psyexp (13.4 KB) conditions.xlsx (11.4 KB)

So each text component is randomised independently of one another, and their order is switched randomly?

1 Like

Thank you so much for doing that. I think what you have is very close to what I want. Somehow when I running it, I do notice that the elements get repeated. Ideally the element will not be repeated. If that makes sense. Thank you so much!