psychopy.org | Reference | Downloads | Github

Python code conversion of JS in online experiment (Pavlovia)

pavlovia

#1

Dear all,

We are looking to publish our psychopy experiment on pavlovia, and we understand that we need to manually translate our python code to Javascript code in order for custom code components to work. Our experiment works offline in psychopy, but not on pavlovia.

After some unfruitful searches, we still don’t understand why our simple code block does not work I’m posting the code component here.

The error message is : ReferenceError: Group is not defined

Python:

import numpy as np

result = np.random.randint(2)


if result == 0:

   Group = "BlockRandomizationFile_1.xlsx"

else:

   Group = "BlockRandomizationFile_2.xlsx"

JS:

var Group = "BlockRandomizationFile_1.xlsx";

var test = Math.floor(Math.random() * 2);

switch (test){

    case 0:

        Group = "BlockRandomizationFile_1.xlsx";

        break;

    case 1:

        Group = "BlockRandomizationFile_2.xlsx";

what we are doing here is a two step thing:

  1. generate a random number between 0 and 1.

  2. according to the number generated, we create a variable “Group” and assign it with one of our block order files.

Thanks in advance!


#2

@katsuragiai, there are missing closing curly braces in the example given, although probably a copy and paste error. Try removing the var statement from your code component variables, because this is done for you when the code is compiled. Psychopy defines your variables created in your code component outside of your functions to give them a global scope. As you are defining your variables within functions using var, you are limiting your variables to a local scope, so they cannot be found outside of the function they are defined in. So, that may solve your problem. So try:

Group = "BlockRandomizationFile_1.xlsx";

test = Math.floor(Math.random() * 2);

switch (test){

    case 0:

        Group = "BlockRandomizationFile_1.xlsx";

        break;

    case 1:

        Group = "BlockRandomizationFile_2.xlsx";
}

#3

@dvbridges, Thank you so much for your reply. And yes, you pointed out our problem. After removing the “var” before variable definition, this code block works and we can randomize between different block orders. Within this code component, all the variables are defined by ourselves.

In a different part of the experiment, we have an additional question related to a variable that seems to be “intrinsically” created by psychopy. In this section, we want to make sure participants understand the instructions of the experiment, so we ask them 5 questions about the experiment. If they get all 5 correct, they move on in the experiment; if they get <5 correct, they have to take the test over again. This repeats until they get all 5 correct.

In order to do that, we have a loop called Test, where we repeated the routine of presenting subjects questions. Every time the subject answers correctly, we increase a counter by 1. If the counter reaches 5, we let them bypass a loop called FailedLoop. Otherwise they need to go through that FailedLoop, which presents them with a screen telling them they have to re-do the test. The participants are then looped back to the beginning of the test.

Here, the counter we defined is number_correct, and in order to make this counter change values, we used variables that are automatically and “intrinsically” created by psychopy , like Test.thisN, which stands for the number of occurrences of the current Test loop, and Testkey.corr, which is a Boolean variable indicating whether a response is correct or not.

It seems these variables work in psychopy but not in JS, because we encounter some problems with them:

In our Test loop, under the “Begin Routine” tab, we have

Python:

if Test.thisN == 0:
   number_correct = 0

JS:

number_correct = 0

if (Test.thisN == 0) {
    
    number_correct = 0;
    
    }

Under the “End Routine” tab, we have

python:

if TestKey.corr:
   number_correct = number_correct + 1

if Test.thisN + 1 == Test.nTotal:
   if number_correct == 5:
       FailedLoop_reps = 0
       TestLoop.finished = True
   else:
       FailedLoop_reps = 1

JS:


if (TestKey.corr == 1) {
    
    number_correct = number_correct + 1;
    
    }

if (Test.thisN + 1 == Test.nTotal) {
    
    if (number_correct == 5) {
       FailedLoop_reps = 0;
       TestLoop.finished = true;
       }
   else {
       FailedLoop_reps = 1;
      }
    }

The offline version works perfectly. But when we run the online version, we get the error :

TypeError: Cannot read property ‘0’ of undefined

We suspect this has something to do with the psychopy-generated variables, as mentioned above. I was wondering if you could take a look into this too? Thanks again.