Randomization of SOA and block order

Dear Jens
sure, I appreciate the time you take.
these are the toy excel files for the first and second blocks. and also the choose block condition file.

Hi farbia that’s for this! Please could you are share a screen shot of your “flow” section on the builder so that we can see the trial structure?

On trials where it doesn’t run, do you get an error message in your runner view?

If so please can you share that?


Hi Becca
this is the flow

No I don’t get any error message in the runner view.

If Eblock1 and Eblock2 contain the same components (but the attributes of those component are diff for each block e.g. PrimeStim) then you only need a single block routine rather than 2. The first loop will go around that block and have the variable $CondsFile in the “conditions” field. The second loop will go around that loop (as you have already)


The blocks contain totally different components.


Hello Fariba

small comments on your toy Excel files. Use “/” instead of “” in your file paths. Your pictures are missing file-extensions. Do you add those with a code element? All columns should have column-name or simply deleted the column containing 1-4.

So, I gues that the loop chooseBlock1 and chooseBlock2 are set to random so that they cycle randdomly through EBlock1 and Eblock2 (again file extensions are missing; Do you add them?) which are also set to random. That does not work?

Regarding SOA, either you add a SOA column to your excel file or you add some code at the beginning of the experiment. This code element sets the soa for each block depending on the participant number. Something like the following:

if int(expInfo['participant'])%2 == 0:
    soaBlock1 = 100
    soaBlock2 = 1000
    soaBlock1 = 1000
    soaBlock2 = 100

Cheers Jens

Dear Becca
Thanks for your response. This is actually the appropriate way to make the presentation of the two blocks randomly possible.

Hi Fariba,

Ah yes, your question is much clearer now. You will be please to hear that we now have a demo of how to do this and have updated our documentation to discuss counterbalancing a several routines with different components :slight_smile:

Here is a demo that should do what you need (download the files and save in the same folder):

randomBlock.psyexp (12.3 KB) conditions.xlsx (8.5 KB) conditionsA.xlsx (8.5 KB) conditionsB.xlsx (8.5 KB)

Here are some other useful threads:

  1. The demo made for you above is actually adapted from this question here Sequence of routines based on slider response - #6 by Becca

  2. If you want to fully counterbalance several blocks/routines with different components aee here for a thread on how to counterbalance several blocks containing different components:
    Using variables in completed URL to direct to next PsychoPy expt - #8 by Becca

This documentation will soon be available at Running and sharing studies online — PsychoPy v2021.2

Hope this helps,

My dear @Becca , thanks for your response but what seems to be relevant in my case is your first suggestion to link all the conditions files to only one block. since they are not reflecting different tasks or components as you said. But I need to make an experiment for each language to be performed on different occasions. However, each of them should be randomized. what I did was that I defined 2 conditions files for two different conditions (group A and B) then another conditions file including both groups and entered ‘$“Group”+expInfo[‘Group’]+".xlsx"’ into the conditions field (with a new field in the experiment GUI setting). But the ‘ok’ button gets inactive and then the experiment doesn’t run. with this error:
File “C:\Users\Novin\Desktop\psychpractice\last_lastrun.py”, line 49, in
filename = thisDir + os.sep + u’data/%s%s_%s’ % (expInfo[‘participant’], expName, expInfo[‘date’])
KeyError: ‘participant’
Even doing so, I couldn’t make SOA randomization.
I also tried a new way. I defined an excel table consisting of group A and B conditions and the two SOA conditions, say 50 and 100 msec.(left at last with four condition rows). But I don’t know how to set the ‘duration’ box for my stimuli and the ‘start’ box for the next related target stimuli!
Maybe I’m going all the solutions halfway, but I don’t know how to fix it…
thanks for your time.

Hello Jens

Thanks for the help.
My images are not missing files and the experiment could find them if I don’t add new conditions to it.
but how can I mix this condition (SOA: if int(expInfo['participant…) with the counterbalancing of blocks?

Aha I see, my apologies, the participant key error means that you are missing the ‘participant’ field from your experiment settings - are you able to make sure you keep that field?


Hello Fariba,

in your Excel toy-example you write, for instance


This pointer to a file is missing a file extension such as *.jpg, *.png or whatever file-format you are using for your pictures. PsychoPy makes a best guess to find the file. I suggest to change the point to your file to, for instance


if this is the file-format you are using. But that seems to be a minor problem at the moment.

It would help if you uploaded a toy-vesion of your experiment or granted access to a copy on Pavlovia.

CU Jens

Oh sorry. It was actually corrected at once, when I first ran the exp… since it was just a toy version.
But unfortunately I haven’t yet granted getting Pavlovia.
Thanks anyway.

Hi @JensBoelte,
How can I share it with you here? Does changing it to a .rar file help? Can I upload it this way?

Simply upload your *.psyexp, your *.xlsx files and the relevant material files using the upload-button in this forum.

Best Jens

Sure. Thanks a bunch

toyexp.psyexp (18.0 KB)
conditions.xlsx (7.9 KB)
EnglishBlock1.xlsx (8.2 KB)
EnglishBlock2.xlsx (8.2 KB)

These make the first draft of the experiment without adding any components yet.
What I actually need to create is: 1) counterbalancing the presentation of EnglishBlock1 and EnglishBlock2, and 2) change the presentation of time of the PrimeStim from block to block and from participant to participant. It is now set to 0.05 sec, but it is just a fixed number for the two blocks, while, it should be actually 0.05 for EnglishBlock1 and 0.1 for EnglishBlock2 for participant 1 and 0.1 for EnglishBlock1 and 0.05 for EnglishBlock2 for participant 2.

I also tried this type of conditions file:
conditions1.xlsx (9.1 KB)
But it seems to be wrong. since only the first block is actually included in the column Blocks or SOA. and as I said when entered ‘$“Group”+expInfo[‘Group’]+".xlsx"’ into the conditions field (with a new field in the experiment GUI setting for group info), the ‘ok’ button gets inactive.

That’s ok dear @Becca .
Thanks for your point. I seriously didn’t know this field is essential, so I included a participant field. The error didn’t appear anymore. But the same thing was with the conditions field.

There is another thing too. When the SOA time is set to a random number, the start time for the ‘noise’ item and the ‘target’ item should be random too!!!

Hello Farbia,

I am sorry but probably nobody will programm your experiment for you. Everybody will help you if you describe your requests as accurately as possibly. For instance,

When the SOA time is set to a random number, the start time for the ‘noise’ item and the ‘target’ item should be random too!!!


SOA =  0
SOA = random,randint(1,4)

The randint() method generates a integer between a given range of numbers, so in ths case between 1 and 4.

SOA = 0
SOA = random.random()

generates a float number between 0 and 1.

Your pick. I don’t know what suits your needs.

Best Jens