Using an if statement to create counter balancing online

URL of experiment: showCoin [PsychoPy]

Description of the problem:
What do I want to happen?

I am looking to add a counter balancing code component for an online experiment with 2 possible orders (task 1 and 2 first or task 3 and 4 first)

What have I tried?

I have conditions files called conditionsAB.xlsx and conditionsBA.xlsx in my experiment folder which define the number of nReps for each condition (as shown in the documentation here: Blocks of trials and counterbalancing — PsychoPy v2021.2
and see screen shot of my loop structure below following the counter balancing different subtasks). They have not yet been imported to my html folder.

The inner loops are the subtasks (1 and 2) and (3 and 4)
The outer loop is control Task Order as shown on documentation. The references to trial 1 and trial 2 is unrelated to the counter balancing as its purely because the time line of trial 1 and trial 2 onwards is different.

These conditions files are called to the outer most loop by typing
$“conditions”+”group”+”.xlsx” into the box for conditions files (see screen shot).

The conditions files look like this (with conditionsBA.xlsx being set up as nRepsTask 1 as 0 on the first row 1 on the second row etc.)

Then my if statement to decide which group it will be based on if the participant number is odd or even (in a code component in my instructions Instr3 routine) looks like this…

What happened when I did this?
I got an error saying unknown source (see screen shot). I’m guessing that this is because my if statement isn’t working but I’m not sure why.

The above isn’t variable – it will always give “conditionsgroup.xlsx”. Do you mean $conditions+group+“.xlsx”

1 Like

Dear Lucy,

there is an error in your code-component code_3. expInfo[‘participant’] is a string, not a number. Convert it to int:

if int(expInfo['participant'])%2 == 0:
    group = 'AB'
else:
    group = 'BA'

Best wishes Jens

2 Likes

Ah that makes sense thank you!

Hi that is great thank you.

When I try to commit the changes I receive a traceback error saying that group is not defined (I have put the code component at the start of the experiment and the if statement for assigning groups is in begin routine). Even if I move it to before experiment, I still get the error saying that group is not defined.

Traceback (most recent call last):
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/experiment/_experiment.py”, line 808, in findPathsInFile
File “”, line 1, in
NameError: name ‘group’ is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/app/builder/builder.py”, line 1221, in onPavloviaSync
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/app/builder/builder.py”, line 719, in fileExport
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/scripts/psyexpCompile.py”, line 73, in generateScript
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/scripts/psyexpCompile.py”, line 242, in compileScript
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/scripts/psyexpCompile.py”, line 214, in _makeTarget
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/experiment/_experiment.py”, line 214, in writeScript
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/experiment/components/settings/init.py”, line 527, in writeInitCodeJS
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/experiment/components/settings/init.py”, line 515, in prepareResourcesJS
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/experiment/_experiment.py”, line 856, in getResourceFiles
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/experiment/_experiment.py”, line 822, in findPathsInFile
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/experiment/_experiment.py”, line 839, in findPathsInFile
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/psychopy/data/utils.py”, line 447, in importConditions
UnboundLocalError: local variable ‘trialList’ referenced before assignment

Hello

add a group = 'CD' before your **if-**statement. Put everything in the Begin Experiment tab instead of the Begin Routine tab.

Best wishes Jens

1 Like

Thank you I will try that. I think I was also setting up a group in experiment info and then defining it again in the code which might be confusing Pavlovia

Upon further inspection I think the ‘group’ that is being referred to in this error is from experiment info as I was setting up the groups there in addition to defining them in the code.
Although even as I removed the ‘group’ field from experiment info I still get the error. So I’m wondering if my changes are being committed correctly.

Are you using Ctrl-Shift-R to refresh to use the latest version?

Yes but when I am in Psychopy and select the commit changes icon, its not allowing me to commit these new changes without this error.

So it looks like the .js script is still using ‘group’ as a field in exp info. I don’t want it to do that here as I am now defining group as a variable in the code instead.
I’m not sure what to do but I’ve heard that directly editing the .js script is not advised?

Hello Lucy

well, you can edit your PsychoJS-script but these edits are not reflected in you local experiment instance. So, it is often best to get the experiment running locally, set auto->JS for the code-components, and to edit manually the instances that are not properly auto-translated.

So, if your experiment is not yet finished, try to find the error that prevents syncing.

You mean you still get the error message

Best wishes Jens

1 Like

I think the issue is your default options for group. I would use a default of 1 and then assign the order in code.

1 Like

Yes that is what I meant by still getting the error.

I will try to find the error that prevents syncing

Sorry – I’ve just seen that [‘AB’,‘BA’] does work online.

Yes that does work but I would like to assign the participants to each group according to whether their participant number is odd or even. So at the moment I get the traceback error when I try to commit that change from local to online.

So what error do you get when you delete group from expInfo and whatever code you used to use when participants selected it, and then try to sync?

I still get the same traceback error as above. I thought if it was an issue with syncing that I should update to the latest version- I have done that now and receive the same error.

In the experiment runner in psychopy I also see:
Command ‘[‘git’, ‘branch’, ‘–show-current’]’ returned non-zero exit status 129

I’ve found some info on the forum for windows (subprocess.CalledProcessError: Command '['git', 'tag']' returned non-zero exit status 128) and was wondering if there is an analogous solution for Mac.

At what points does trialList appear in your experiment (either in code components or as the name of a component or loop)?