| Reference | Downloads | Github

Counterbalancing experiment?

Hello everybody,
I’m a novice in psychopy and i’m trying to counterbalance between participants two trials containing two sets of stimuli (my stimuli are words) separated by a break. I want to do that to avoid order effect. Here you can see my experiment structure:
To do that, i’m a little bit lost because I saw topic from this subject but only when there is just counterbalancing in one trial… I want to know if it’s possible to created two versions of the experiment (with two differents orders) and tkanks to an empty thirst one, call one of the two versions. I try to write a code on the section “begin experiment” on the empty version like that but I Don’t think it’s right :

if (participantNumber % 2) == 1:
Experiment1.psyexp = True
elif (participantNumber % 2) == 0:
Experiment2.psyexp = True

Do you have an idea?

Thanks a lot for you help

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

OS (e.g. Win10):
PsychoPy version (e.g. 1.84.x):
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:

What did you try to make it work?:

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.

Hi @DocPsy, please use backticks around your code, it makes it easier to read.

What you could do is have your trials in a conditions file, and have two separate conditions files for each condition to satisfy your counterbalancing requirement. Your conditions files could be called ‘orderA.xlsx’ and ‘orderB.xlsx’. Add a field called “condition” to the experiment info in your experiment settings, and when you start each experiment, enter A or B depending on the condition you are running. In your loops, you would define the conditions file as:

$'order' + expInfo['condition'] + '.xlsx'  # this will evaluate to orderA.xlsx, if A was entered as your condition.

Hello @dvdridges,
thanks for your answer
Where could we put the two files “ordre À and B”, in which loop?

So you want Order A and Order B to alternate, so that trials loop might be order A and trials_2 would be order B? If so, then slight adjustment to be made. Add “condition” to the experiment info, but instead of a single letter i.e., ‘A’, you enter the order ‘A,B’ or ‘B,A’ separated by commas, no spaces. Then for trials loop, add:

$'order' + expInfo['condition'].split(',')[0] + '.xlsx'

and for trials_2, add:

$'order' + expInfo['condition'].split(',')[1] + '.xlsx'

What this will do is take the value passed to “condition”, and split the value at the comma, and use those values (either A or B) to create the file name.

Yes but I want the participant will see the both trials but sometimes I will start by bloc À then bloc B and other participant start by B then À. Is it still good ?

Yes, see the last post. Give that a go and let us know how you get on.

So, I’d tried to do it.
I created two order of my stimuli like that:

I insered two to order A and B in the condition space in psychopy like that (exemple for loop “trial”)

But the programm doesn’t run because “Mots” is not defined… However, I have defined it in text properties and I choose “set after repeat”


Right, just for info, it would be useful if you could make the method more precise in your initial post, as you would in a methods section, then we provide more useful help.

What you want can be achieved using nested loops, see image:


In the blocks loop, you will want to define your order condtions file, with sequential order, for nReps of 1:

$expInfo['condition'][0] + '.xlsx'  # will be A.xlsx, if you enter A in the GUI

In the inner loop called “trials”, use $condition, the variable defined in your order conditions file which links to the sheets with the actual trials. Use random order, with nReps of 1 ( if you only want 1 block of trials).

Yes, I’m doing a lexical decision task with a feedback after a wrong answer. I created to trial to propose a break in the middle of the task. I want to contrebalance these trials
So, I realised an other loop but the programme doesn’t run. A black screen stay in the display…
Maybe I have doing Something wrong… I send the file.psyexp that I programme, I would be simplier maybe (I deleted the feedback, it’s not important for the counterbalancing)

The version you have sent me does not include the codes I have sent you. Here is my version which works, with excel files for you inspect. If you can get one of your blocks to match my version, hopefully you can get something running.

nestedLoops.psyexp (10.8 KB)
OrderA.xlsx (9.2 KB)
A.xlsx (7.8 KB)
B.xlsx (7.8 KB)

Ok my mistake, I wrote the code in the bad place. So I tried to do the modifications but this dark screen still appear. Here the modified file. I created a section “condition” in experimental settings and tried to run the experiment with; “B,A” for the purposes that the participants sees Bloc B and then Bloc A
I’m sorry for desagrementsTDLtest.psyexp (20.4 KB)

You see a blank screen because you have set the text opacity of “consigne” to zero, meaning the text is invisible. Set the opacity to 1 to see the text, see the advanced settings of text component.

The problem I can see with your loops is that you have not actually copied the code. In your example, you have used the following in the “Block” loop:

$'order' + expInfo['conditionA'][0] + '.xlsx'

This problem is two-fold - 1) expInfo[‘conditionA’] does not exist as a field in your gui, you created a field called “condition”, not “conditionA”. 2) You are not splitting the text. If you are going to enter “A,B” for “condition” in the GUI, use:

# For your first loops i.e., block
$'order' + expInfo['condition'].split(',')[0] + '.xlsx'  # This is will index the first of A,B e.g, A
# For your second loops i.e., blockB
$'order' + expInfo['condition'].split(',')[1] + '.xlsx'  # This is will index the second of A,B e.g, B
# and make sure you have a spreadsheet called orderA.xlsx and orderB.xlsx

The next problem relates to your inner loops e.g., trials. This loop will use the variables in the spreadsheet from the outer loop (e.g., block). You need to make sure that your orderA and orderB.xlsx files have a column called “condition”, and this lists your stimuli excel files (those that have Mots variable etc). In your inner loops, e.g., “trials”, use the $condition variable to use your stimuli excel files.

So it’s work! but not exactly How I was thinking.
It’s doing (B.xlsx and A.xlsx) - BREAK- and then (A.xlsx and B .xlsx)
I would like A.xlsx -BREAK- B.xlsx for the first participant
B.xlsx-BREAK-A.xlsx for the second participant;

Is it the same design? Sorry I was maybe unclear.

Ok it’s working. Just write “B” or “A” to launch the experiment.
A big thank-you to @dvbridges for his usefull help!!

Ok then my original suggestion was what you wanted, but images you posted suggested you wanted to present A then B per participant, and that A had stim list 1 then 2, and B had stim list 2 then 1. Just to be clear, you want two spreadsheets, orderA.xlsx, and orderB.xlsx. In those spreadsheets, you have columns (Mots, Couleur, and RepCorrecte). No more nested loops needed. You just need one loop, surrounding each trial block, loops trials and trials_2. Use the following in the respective loop to define conditions files.

# trials loop 
$'order' + expInfo['condition'].split(',')[0] + '.xlsx'
# trials_2 loop
$'order' + expInfo['condition'].split(',')[1] + '.xlsx'

Now when the experiment starts enter “A,B” or “B,A” in the condition field. “A,B” exists in the example by default just to show you. Also, you may want to consider starting your keyboard component from when the stimuli being, and give the keyboard infinite duration (as in example) because if the participant misses the response time window, the trial gets stuck.

TDLtest.psyexp (20.8 KB)
OrderA.xlsx (9.3 KB)
OrderB.xlsx (9.3 KB)

Hello @dvbridges
I haven’t seen you answer!
I tried to do that and it’s working very well. At least, form now on I could use the both methods! Thanks a lot for your help!!

A post was split to a new topic: Counterbalancing factors