Reversing order of multiple routines each with its own loop

OS (e.g. Win10): Win7
PsychoPy version (e.g. 1.84.x): 1.83.04
What are you trying to achieve?:

Hi all,

I am building my experiment on builder view with little-to-no experience in programming.

For my experiment, I have three blocks (control1, violation, control2), with 16 trials each (calling an image and corresponding audio). I want to be able to (potentially) loop around these three blocks so that the order can be reversed half the time. For example, for a participant ending with an odd number (e.g., 01 or 105), the order would be control1-violation-control2 and for a participant ending with an even number (e.g., 02 or 208), the order would be the reversed, control2-violation-control1. I know the simple answer would to be build another experiment with the reversed order, but I would prefer a more systematic way to do this. As well, we may add another block in the future, then counterbalancing it via multiple experiment would be inefficient.

Any help is much appreciated

Exactly right, you should avoid duplication whenever possible. It becomes hard to maintain.

I think you want a setup like this:

  • Have three routines defined (control1, violation, control2).
  • Insert six copies routines of those routines in the flow panel in this order: control1, violation, control2, control2, violation, control1
  • Insert a loop around each individual routine, connected to their respective conditions files as required. (i.e. now you have six routines and six loops in the flow panel).
  • The magic step: insert a loop around the first three loops (i.e. the first three loops and their routines are nested within this outer loop).
  • Do the same around the last three loops.

Hopefully you can see the logic emerging here. We want to set the nReps value for one of the outer loops to be 1 (so the three blocks within it will run) and the other to be 0 (so that effectively all the blocks it contains will not run on this session). We can do this by putting an expression in the nReps field of each of these outer loops which will evaluate to either 0 or 1 as required.

e.g. we can check whether the subject number is odd or even by doing division modulo 2 (this gives us the remainder of a number when dividing by 2). The modulus operator in Python is % (nothing to do with percentages in this context). Let’s say you are using the ‘participant’ field in the info dialog for your subject ID code. Put this expression in the nReps field of each of the outer loops:

expInfo['participant'] % 2 == 1

and this in the nReps field of the second outer loop:

expInfo['participant'] % 2 == 0

i.e. if the subject number is even, the remainder will be 0, so the first expression will evaluate to False, which in Boolean logic terms, is numerically represented as 0. So the nReps field evaluates to 0 and hence this first set of blocks will not run at all. Conversely, as the remainder is 0, the second expression evaluates to True, which is numerically represented as 1. So the second set of blocks will run. Vice versa for an odd-numbered subject.

Lastly, it’s possible that the participant field is stored as a string of characters rather than a number, so if the above doesn’t work, you might need to force it to be an integer, e.g.

int(expInfo['participant']) % 2 == 1

Clear as mud?

Thank you! This works perfectly and it is stored as a string of characters so thanks for that

I was searching on this forum ways to randomize the order conditions are presented on the experiment and I finally found this, which is very similar to what I want to do. Instead of 3 conditions, I have only two but everything else is pretty similar. My question is: is there another way to accomplish this randomization actually, without using participants Id number?
While I was searching, I found many people recommending the use of blocks, and that we should avoid many routines but the conditions that I’m presenting are very different(pic and text) and I have specific questions question attached to them.

I would appreciate any insight,
Thank you!

I’m not exactly sure what you mean, but in Experiment Settings > Experiment Info dialog box, you can customize it so that you do not necessarily have to use participant IDs for this assignment. However, if you are sticking to the being divisible by 2 solution presented, then it would still have to be a numerical input.

Hi, thank you for your response.
Our problem is a little more complicated. Basically, we have a pic and text conditions, but we also have different sets of questions attached to these conditions. In the questions, participants are allowed to go back to see the conditions and then answer the questions. We have a outer loop controlling this part. We tried to add another loop around the pic/question(one conditions) and another one around text/question(second condition) just like explained here in your example(we tried using id number, without automatic randomization), however it only read the welcome screen(that is not around any loop) and the experiment ends.