Mega counterbalancing

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

OS (e.g. Win10): OS 10.13.6
PsychoPy version (e.g. 1.84.x): 3.1.5
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:
Hello everyone, I am making an experiment in Builder that I eventually plan to put online with Pavlovia, but I am running into the issue of having trouble counterbalancing my experiment. I am relatively new to PsychoPy and unsure of how to proceed.
My experiment is a word learning experiment. The only instructions to the participant are to observe and remember the word pairs that appear to them on the screen. Let me describe it:
I will be presenting word pairs on the screen (a known word, and a foreign word). For half of the participants, the foreign word should appear on the right during the whole experiment, and for the other half, on the left. For this I have put a field in the “Experiment Info” in order to select an “A” or “B” set, which reflects which side the stimuli appear on, and I then have 2 different excel sheets, A and B for my stim which in turn have $word1 or $word2 linked in the text component part, and it is working well. However, the next part is not so straightforward

I have 60 word pairs, which I want to present in 3 learning blocks: In one block, a monochromatic tone will play with the words, in another a natural sound, and in the third there will be silence when the words are presented. These blocks should be shuffled before starting the experiment. Also, I want to program the experiment in a way that the known and foreign word pairs are matched together in a random way for each participant. However, I need to write a part of the code saying that if the known word and the foreign word start with the same phoneme, they are NOT to be paired together.

What I want to ideally do is to shuffle my list of stimuli and my blocks at the beginning of the experiment. After shuffling, if the first block selected is a tone block, then the following 20 stimuli will be presented along with a monochromatic tone, and the second block will present the next 20 stimuli with a sound, and the final without sound (or vice versa).

While my natural sounds are linked explicitly to my known words (i.e., bird – bird sounds), I have 20 monochromatic tones which should be shuffled and linked to word pairs in a random way during the tone block.

Additionally, each block must contain an equal number of “natural” (i.e. rain) versus “artificial” stim words (i.e. a car horn). I am wondering if I can do all these counterbalancing measures in 1 excel sheet or if I need multiple…I’ve been looking at the following thread Random selection of image stimuli without repetition across different blocks as I work on my answer, but I find myself fumbling quite a bit. Hopefully my explanation makes sense. If anyone has advice or has done something similar, please let me know, and I am very grateful for the advice!!

What did you try to make it work?:
This is some code I have tried to put into the “Begin Experiment” code component.

# create different lists
spanWordsNA  = ['spanStimNA.xlsx']
spanWordsAR = ['spanStimAR.xlsx']
vimmiWords = ['vimStim.xlsx']
monochromTones = ['tonesStim.xlsx']

# randomize list orders

# get now-randomised selections of each for each block:
soundBlock_files = spanWordsNA[0:10] + spanWordsAR[0:10] + vimmiWords[0:20]
toneBlock_files = spanWordsNA[10:20] + spanWordsAR[10:20] + vimmiWords[20:40] + monochromTones[0:20]
silenceBlock_files = spanWordsNA[40:60] + spanWordsAR[40:60] + vimmiWords[40:60]

# randomly intersperse files in blocks:

What specifically went wrong when you tried that?:
I have having trouble specifically linking the different excel sheets I made to one routine (maybe that isn’t possible?). I have one routine, surrounded by 2 loops. In the inner loop I have the condition linked to $Excels and on the outside loop I have the conditions linked to my excel sheet called all.xlsx, which contains the other excel sheets spanStimAR.xlsx, spanStimNA.xlsx, vimStim.xlsx,
NameError: name ‘Word1’ is not defined
This is the error I get, so obviously my excel sheets are not being read correctly.

This just creates a list with one entry, a filename:

spanWordsNA  = ['spanStimNA.xlsx']

So this does nothing, as the list has only one entry:


And this can’t give you ten entries, as the list still only has one:


You really need to test your code to see if it is giving you what you intend. Try running it in the shell window in the Coder view, for example, so you can inspect the variables and see what they contain.

Were you intending to read in values from those files, or are you actually just wanting to generate combinations of filenames?

The way I deal with counterbalancing presentation side is I multiply the x coordinate by a variable (side) which takes the value 1 or -1.

@Michael thank you for your reply, I didn’t realize exactly what the code was doing. I am trying to import my excel files and randomize the variables contained within them so they can be selected for different blocks of the experiment. I had read that in Pavlovia, you can’t use any of the import functions (i.e. pandas and xlrd command) so I am trying to find a way around that.

@wakecarter thanks for the suggestion, I will try it out. For now I have added a code component to randomize the side of presentation that seems to be working well. This way I don’t have to have separate excel files based on the side of presentation.

#Randomizing which side words are presented on
if random()>0.5:
    spanPos=[0.4,0.0] #spanWord on the right
    vimPos=[-0.4,0.0] #vimWord on the left
    spanPos=[-0.4,0.0] #spanWord on the left
    vimPos=[0.4,0.0] #vimWord on the right

Hi There,

I just wanted to contribute to this discussion with some relevant links that could be helpful in the long run (since you are new to psychopy, and counterbalancing is always difficult!)

  1. We recently updated the docs on this (including counterbalancing online - see link at end)
  2. This nice youtube tutorial on another approach to counterbalancing which I see has an accompanying pavlovia project
  3. A script to generate .csv files for fully counterbalanced designs without manually making a million excel sheets.

Hope these are helpful,


Hello Becca, thank you so much for those links they have been very helpful.
Regarding second link, I have followed the code for it however every time I try to run my study it comes up with the error code ‘nRepsA’ is not defined for the trial properties. I was just wondering why that is. I have copied what he’s done down to a t and checked it over so I don’t know what I’m missing.
I think the code is not linking up to the nReps$ value on trial properties but I don’t know how to change it.
Best Victoria.

Hi Victoria,

nRepsA was set using a conditions file with a column header nRepsA in this example :slight_smile: