Translating PsychoPy code to Javascript & general documentation

HI David,

so I thought I can solve the problems I have been having (see last post) by hard-coding the lists content into the code == see the updated version https://gitlab.pavlovia.org/MJB/immanent-justice-v1-3
However, I still get get same problem: That trials that are supposed to show stimuli based on the text in these lists only show NaN and that the slider that is supposed to collect ratings is not visible (and the experiment consequently gets stuck there).
Do you have any idea what is going on or should I start a new thread with this problem?
Many thanks

Marc

Hi @Marc_Buehner, this looks like an issue with how you are formatting your strings in JavaScript. Try the following instead:

story.setText("Early morning one day, {0} {1}...".format(thisPerp, thisDeed)) // etc

You can also format your strings in JavaScript in a similar way to Pythons new f"" string formatting. Note the use of backticks to surround strings below, rather than single or double quotation marks:

story.setText(`Early morning one day, ${thisPerp} ${thisDeed}`)

This method is not compatible with Python, so the first method (using .format) may be best for compatibility.

HI David,

Thank you SO MUCH!

That did the trick.

I did not know about this other way of string formatting ā€” Iā€™ve only learnt the other way!

Thanks again!

Marc

Hi @dvbridges. I have a similar issue: I need to translate some of my code components to Javascript so that I can run my study online. In particular I have a code component at the very beginning of my experiment which uses pandas pd.DataFrame to create a list of 32 names, and then adds these 32 names to a csv file. This csv file is then used to determine the trials for each participant. The code component is the following:

    conditions2 = pd.DataFrame ([{'conditions':f1} for f1 in conditions2])
    conditions2.to_csv("myStim.csv")

My question: is there a way of translating this piece of code into Javascript, so that I can create an csv file at the beginning of my online experiment?

Thanks!

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?