psychopy.org | Reference | Downloads | Github

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

Hello,
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.


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:
$randchoice(categoryAlist.split(","))

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:

$randchoice(categoryAlist.split(","))

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!