psychopy.org | Reference | Downloads | Github

Problem with JavaScript conversion - Pavlovia stuck on "intializing experiment" screen

URL of experiment: https://run.pavlovia.org/t_s/24drb_erw/html

Description of the problem: Pavlovia is stuck on the “intializing experiment” screen.
I have read on other forum posts that one reason could be the python to js conversion, and I think this might be my problem.

I have built the experiment with the PsychoPy Builder, and have a few custom code elements in it.
The code element that I think might be causing the problem is one I used to set the interval between the fixation cross and the first stimulus (the other code elements have been used before). I tried this out in the offline version of the experiment, and it works as I want it to. Basically, it uses numpy’s random.exponential function to set the interval between two stimuli. If the interval that is drawn is too long then it is rejected with a if statement.

Below is the python code I am using in the Begin Routine tab:

from numpy import random
sc = 0.2
maxi = sc * 12.5

#generate number
isi = random.exponential(scale = 0.2, size = None)
#if isi is larger than maxi, keep drawing until it is smaller
while isi > maxi:
    #print("larger")
    isi = random.exponential(scale = 0.2, size = None)

thisExp.addData("isi", isi)

In the Begin Experiment tab I have:
isi = 0

In the Duration field of the respective Routine, I use “$isi” to set the duration.

Since this works offline but not online, I guess this has something to do with the conversion to javascript? Or can I not import python libraries online? I don’t really know javascript, so I am not sure where the problem lies.

Any help would be appreciated.

Cheers,
tasi

Please look at my crib sheet for how to define random variables.

Would this work for randexp if defined in code_JS or am I misreading the function?

randexp = function(scale) {
  return Math.exp(-Math.random()/scale)/scale;
}

Hello,

You can’t import Python libraries online. PsychoPy imports a set of libraries and their equivalent JavaScript-counterparts.

Best wishes Jens

thanks so much for the function in js code and for pointing me towards the crib sheet.

However after taking a look at the implementation of numpys random.exponential function (python - How to view the source code of numpy.random.exponential? - Stack Overflow) and translating it to js, it looks like this might be a closer approximation of the function

randexp = function(scale) {
  return -scale*Math.log(1.0-Math.random());
} 

I would be very happy for any feedback on this.

thanks again for your help :slight_smile:

Hey,

thanks for the reply. I just found it in the crib sheet mentioned in @wakecarter 's comment (which I should have read before starting!)

cheers,
tasi