Issues with counterbalancing online

Hi all,

URL of experiment: Face-Project/OtherRaceEffect_Program.psyexp at main · PaigeKemp/Face-Project · GitHub

Extra information: All the extra files (I.e., jpg’s, and xlsx’s) to run the experiment are in located here:

Description of the problem: I am trying to convert my local experiment to an online format, and I have been informed by many other forum topics that the code import random does not work online because it is Python and you have to convert everything to Javascript. I have attempted to use the crib sheet which has been promoted by @wakecarter many times and I believe I have changed my code accurately. For example, my code has now been changed from (Code: Auto → JS):

import * as random from 'random';
var format, formats;
formats = [1, 2, 3];
format = random.choice(formats);

to (Code: JS):

function random_character() {
    var chars = "ABC";
    return chars.substr( Math.floor(Math.random() * 3), 1);
}
condition = random_character();

But now I am uncertain on how to access the three different xlsx files (B1_A.xlsx, B1_B.xlsx, B1_C.xlsx) I have per loop based on whether the format is A, B, C. I have tried changing the condition of the loop to:

$“B1_” + condition + “.xlsx” however the “Ok” button always seems to be disabled and so I cannot move forward.

Any help on resolving this issue would be fantastic, I appreciate all the help I can receive.

Best,
Paige

I figured it out, here is my solution:

I changed my Begin Experiment code to:

chars = ["A","B","C"]
shuffle(chars)
condition = chars[0]

and then I had too many quotation marks in my condition of the loop, so now it is:

$'B1_' + condition + '.xlsx'

Have a good day!

1 Like

Hi there,

I’m trying to do something similar and came across a similar problem to you (I also have 3 blocks, but can’t get it to run more than 2 blocks before crashing.

I’ve tried several different versions:

function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
switch (getRandomInt(3)) {
case 0:
condition = “A”;
break;
case 1:
condition = “B”;
break;
case 2:
condition = “C”;
break;
}

function random_character() {
var chars = [“A”,“B”,“C”];
return chars.substr( Math.floor(Math.random() * 3), 1);
}
condition = random_character(); // the condition variable will be used in your loop, as with the Python code

You said you found a solution but I can’t figure out how to implement that. It doesn’t seem to work for me on its own i.e.

chars = [“A”,“B”,“C”]
shuffle(chars)
condition = chars[0]

I’m using: $‘Group’ + condition + ‘.xlsx’ in the condition field of my loop - it worked before when it was Python code. This bit I get, it’s the JS that I’m struggling with as a newbie.

Thanks,

Ben

Are you getting an unknown resource error?

It appears to be an issue with defining the variable “conditions” even though I’ve defined it at the top of the experiment (see above). I’m using word stimuli in an Excel file so I don’t think there are any resources needed for this particular instance?

This is what I get when I try to compile the JS:

Traceback (most recent call last):
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\experiment_experiment.py”, line 829, in findPathsInFile
filePath = eval(filePath)
File “”, line 1, in
NameError: name ‘condition’ is not defined

It won’t run locally properly or online now (I get a ReferenceError to do with colors online). Locally, it only runs 2 out of 3 blocks before ending.

Ben

The Excel files are resources.

Try putting condition = “” in Begin Experiment.

HI there,

Thanks for the response. I’m still getting the same issue regardless. The code above is already in the begin experiment tab, so maybe it’s in the wrong place? I assumed because it was something that needed to happen at the start of the experiment it should go there?

This is my full error report when I try to compile JS.

Traceback (most recent call last):
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\experiment_experiment.py”, line 829, in findPathsInFile
filePath = eval(filePath)
File “”, line 1, in
NameError: name ‘condition’ is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\app\builder\builder.py”, line 793, in fileExport
target=“PsychoJS”)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\scripts\psyexpCompile.py”, line 75, in generateScript
compileScript(infile=exp, version=None, outfile=filename)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\scripts\psyexpCompile.py”, line 248, in compileScript
_makeTarget(thisExp, outfile, targetOutput)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\scripts\psyexpCompile.py”, line 220, in _makeTarget
script = thisExp.writeScript(outfile, target=targetOutput, modular=True)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\experiment_experiment.py”, line 254, in writeScript
self_copy.flow.writeFlowSchedulerJS(script)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\experiment\flow.py”, line 307, in writeFlowSchedulerJS
resourceFiles = set([resource[‘rel’].replace("\", “/”) for resource in self.exp.getResourceFiles()])
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\experiment_experiment.py”, line 877, in getResourceFiles
condsPaths = findPathsInFile(params[‘conditionsFile’].val)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\experiment_experiment.py”, line 843, in findPathsInFile
files.extend(findPathsInFile(str(condFile)))
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\experiment_experiment.py”, line 860, in findPathsInFile
conds = data.importConditions(thisFile[‘abs’]) # load the abs path
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\data\utils.py”, line 420, in importConditions
fieldName = fieldNames[colN]
IndexError: list index out of range