Translating PsychoPy code to Javascript & general documentation

The newest version of PsychoPy (Download it here https://www.psychopy.org/download.html), has a great feature that will convert your python code to JS code automatically, and I would highly recommend updating your PsychoPy and trying that feature.

2 Likes

Hi Guys
I’ve used PsychoPy successfully for less than a year now and have been trying to create some online experiments through the coding platform. I was working through my python code to convert it to JS but saw some posts about using a codetype feature in the latest version of PsychoPy to autoconvert my script.

I’ve now updated my version but where do I find the codetype feature? I’ve attached a screenshot.

This feature exists only in the PsychoPy builder (it automatically translates any code components). Im unsure of the best way to try and translate a fully coded PsychoPy experiment but https://www.psychopy.org/online/psychojsCode.html is a good place to start.

We definitely won’t ever be able to auto-translate an entire script. The fact that this works for small chunks of code (where just syntax needs converting) is amazing already IMHO. Translating the logic/structure of an experiment between the two, and knowing about all possible python libs and their JS alternatives? It just isn’t possible.

I’d strongly recommend you learn to use Builder so you can export your study in either flavour.

best wishes,
Jon

1 Like

Hi,

I’m trying to use this method to import additional excel files, which contain some stimuli and additional “conditions” that need to be separately shuffled. We already ran one phase on 3.2, and it worked fine, but now on 2020.1 this seems to be broken. Example:

var dilemmaTexts = new TrialHandler({
      psychoJS: psychoJS,
      nReps: 1, method: TrialHandler.Method.SEQUENTIAL,
      extraInfo: expInfo, originPath: undefined,
      trialList: "dilemmas_text.xlsx",
      seed: undefined, name: 'dilemmaTexts'});

console.log(dilemmaTexts.trialList[1]['dilemma_id'])

I am (generally) able to print the console message, but when trying to use dilemmaTexts anywhere later on, it’s not defined. This is probably due to its being defined inside the experimentInit() function, and not globally. In 3.2 this behaviour seemed to be different. I am able to fix this by manually shifting the definition outside of the method, just to demonstrate that that is indeed the issue, but a more permanent solution would be preferrable.

If in addition to this I put an actual loop with the main conditions into the experiment, I all of a sudden get the “unknown resource” error. Adding the dilemmas_txt resource manually to the call of PsychoJS.start() does not solve the issue.

So what is the correct method of importing extra excel files in online experiments in version 2020.1? Or should we just roll back to 3.2?

@enniin, try removing the var declaration. Using var will limit the scope of that variable one particular function. Without var, PsychoPy will create a global variable for you, accessible anywhere in the experiment.

Thank you @dvbridges ! Removing var did solve the definition issue, and on 3.2.4 (from the Builder experiment settings) everything seems to run like it should. Great!

But still, if I try to change the project to 2020.1.2 I get the “unknown resource” error. The project was originally created as 2020.1.2., so that shouldn’t be the issue.

You can find my test experiment here: https://pavlovia.org/jennisaaristo/test_200528b
Code is here: https://gitlab.pavlovia.org/jennisaaristo/test_200528b
(Python side prob doesn’t work, I’m just debugging JS with this.)

Right now we’re rolling back to 3.2.3, but it would be great to know if this feat could also be accomplished on 2020.1.

Thanks for all the hard work!

I have a similar problem. The experiment runs locally but online it gives me a reference error. ReferenceError: win is not defined. I think this is because I am creating a visual.RatingScale and visual.Line component from the code block. I don’t understand why they are unable to access the win object created earlier(I have checked the lastrun.py file the object name is win). I think there must be an error in translation to JS, although I have changed the code type to both as mentioned. You can look at the experiment here.

@zenith092, I am not sure where the error is exactly, but looks like it will bein the Ml_StimuliRoutine routine, where you use a conditional statement in your “Each Frame” tab of a code component to create refline. The error is because you have used win instead of psychoJS.window., so change to:

 if ((rate !== rate1)) {
      refline = new visual.Line({win: psychoJS.window, //...
  }

1 Like

I changed it now. Besides I think the refline should be like this

refline = new visual.Line({
    win: psychoJS.window,
    name: 'polygon',

instead of

refline = new visual.Line({
    "win": "psychoJS.window",
    "name": "polygon",

Is there a tool I can use to run the JS thing locally. Opening index.htm in an browser doesn’t work. It would be a lot of help. Besides thanks for the Developer options bit, it helped a lot.

@zenith092, in the latest PsychoPy you should be able to run the experiment locally from the Runner window using the debug button (planet earth with bug icon).

1 Like

Got it! There is only one additional thing I noticed. The JS files on palvovia.org won’t update even after I pushed the changes to gitlab.pavlovia.org.

Great. Ok, could be you need to refresh your browser cache so the new changes are downloaded.

1 Like

I don’t know why the web version is unable to access the resources(trialList) even though its named correctly and just in the resources folder. Unfortunately we encountered the following error:

when importing condition: ml_exp.csv
when getting the value of resource: ml_exp.csv
unknown resource 

is it not pointing to the right address?

  // set up handler to look after randomisation of conditions etc
  trials = new TrialHandler({
    psychoJS: psychoJS,
    nReps: 1, method: TrialHandler.Method.RANDOM,
    extraInfo: expInfo, originPath: undefined,
    trialList: 'ml_exp.csv',
    seed: undefined, name: 'trials'});
  psychoJS.experiment.addLoop(trials); // add the loop to the experiment
  currentLoop = trials;  // we're now the current loop

Never mind @dvbridges this thread solved the issue already.

Cool, what was the issue exactly?

The problem as I mentioned earlier was this: I couldn’t run it on the local server because of the trialList file. As the thread mentions, it will only work on the psychopy server.

Now I am facing another issue :

  • TypeError: visual.Line is not a constructor

Which is quite similar to this. Unfortunately, the suggestions by @jonathan.kominsky don’t seem to work in this case.

PS: I feel I should really open new questions or at-least post the issues in a more specific question for better organization and searchability. Any opinions or say community guidelines?

I think that using the error message as the thread name is the most useful.

I think that “is not a constructor” may mean that it doesn’t work online. For example I think I found that polygon and rec work but circle doesn’t

1 Like

2 posts were merged into an existing topic: TypeError: visual.Line is not a constructor

Great Idea actually. So I opened a new topic. @jonathan.kominsky and @dvbridges you can post your suggestions there. Meanwhile, I will try to use shapeStim or rect as suggested.

Thanks.