psychopy.org | Reference | Downloads | Github

Experiments stucks at initializing - problem adapting to javascript?

URL of experiment: https://run.pavlovia.org/takishl625/inter-temporal-choice_searchstrategies/html/

Description of the problem: I cannot initialize the experiment the page stucks and it doesn’t open the Exp that I want to pilot test.

Please help me. I read in the forum that usually the problem is that the code is not adapted correctly from python to javascript in some points. However how can I solve this and still the Exp run as it it’s supposed to ?

Thank you a lot.

Do you have any imports?

Best wishes,

Wakefield

1 Like

I read previous posts and tried to run it by commenting out the imports in the js code @wakecarter @JKugel , sorry for not mentioning that from the very start.

But that didn’t make a difference, still the Exp lagged initialising. It’s a bit confusing for me what’s the point of translating into js the imports such as
/*import { PsychoJS }
if they are useless. Sorry, if I am unaware of how js works.

@ wakecarter So the work around is to change every part of the code with equivalents as mentioned in this doc. These have to be done directly in the js file created I guess ? (so one has to learn some js syntax to do so?)

Thank you a lot for your help.

I have never learned to code in JavaScript and thanks to the auto translator I still don’t have to. I feel like it’s important to recognise that having the auto translate at all it a huge bonus and since it has only been there since January, it’s not surprising that it’s a bit buggy and can’t get everything right.

The solution is to be creative with your Python code and either replicate the functions you wanted to import or find a different way to achieve the same goal.

What functions do you need?

1 Like

I had used a function in the builder in the section within each frame (I saw you suggest to avoid this maybe that could be the problem). I use this function as I want to change opacity of stimuli depending on where the mouse is. I check the js code to understand which part is this translated but it’s a mess. For example, I have this function in js:

function trialRoutineEachFrame(trials) {
  return function () {
    //------Loop for each frame of Routine 'trial'-------
    let continueRoutine = true; // until we're told otherwise
    // get current time
    t = trialClock.getTime();
    frameN = frameN + 1;// number of completed frames (so 0 is the first frame)
    // update/draw components on each frame
    if (polygon_RA.contains(mouse)) {
        polygon_RA.opacity = 0;
    } else {
        polygon_RA.opacity = 1; 

In my python compiled script it starts like:

        # get current time
        t = trialClock.getTime()
        tThisFlip = win.getFutureFlipTime(clock=trialClock)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)  

I don’t get any feedback what gets wrong when the exp stucks initializing so I can’t understand what’s wrong. :confused:

Thank you once more!!

Dear @wakecarter,

Sorry if I overwhelmed you.

One important thing is to clarify if the solution can be essentially given by just changing the code elements that I inserted in the builder (and so I should focus changing the builder before exporting the html etc).

I realize that messing with the code files doesn’t help, so I have to modify my code elements in the builder ? given that the other text stimuli etc. defined at the builder in “clicks way” are arguably simple enough to be successfully translated

You are a life savior keep up the great work!.

Cheers,
Peter.

I never use the coder, even when it’s more appropriate. Adding code snippets in builder makes experiments easier to edit while keeping a handle on what’s going on.

1 Like

I’m not clear on the other issues in this thread, but the specific error you’re getting at the moment is actually much simpler than it looks. The JS console provides the following error code:

Inter-temporal%20choice_SearchStrategies.js:430 Uncaught SyntaxError: missing ) after argument list

You might wonder why it thinks you’re missing a close-paren, and the answer is JS is being cranky about some strings in your text items. In particular, the lines in question are these:

    RA_item.setText(f'''€{rewardA}''');
    DA_item.setText(f'''{delayA}''');
    RB_item.setText(f'''€{rewardB}''');
    DB_item.setText(f'''{delayB}''');

I think this is builder-generated code, and from the look of things, it’s a set of text components that are set from a loop. Could you share what is in the text fields of the components RA_item etc. in the routine “trial” in the builder?

1 Like

Dear @jonathan.kominsky thank you very much for your reply.

I wrote the code which is in the js parenethesis, in the text field in the builder. I coded it with Python string formatting to show the text corresponding to my conditions in every trial (e.g 50, 70 etc euros according to the values in my excel file’s column/variable “rewards” etc)

However, now that you mention it I don’t know how that gets translated. Maybe this string formating practice is not advisable ? How can I solve the problem but keep the functionality ?

My original code was like this and I wrote it in the text field of the text stimuli in the builder:

f'€{rewardA}'   #a string is shown with the euro sign according to what is included in my "rewardA" excel column 
f'€{rewardB}'  #similarly with above
f'{delayA}'   # the delay period is shown from the excel column "delayA", e.g 15 days etc.
#etc etc

Your best bet is to put the € sign in the excel file, and in the text component of the builder just have it be $rewardA (which is how Python identifies something that should be filled in by a variable, and it will auto-translate to JS relatively smoothly). Since these are being displayed as strings rather than numeric values anyways, there’s no downside to just incorporating the euro symbol into the excel file unless you are trying to do computations that modify rewardA during the experiment.

1 Like

Dear @jonathan.kominsky thank you once more.

URL: https://run.pavlovia.org/takishl625/inter-temporal-choice_searchstrategies/html/

I tried it and commented out the import(s) as well but I get the same error:

Uncaught SyntaxError: missing ) after argument list

I googled that and it seems that it means args in a function or something similar. But does that mean that one parenthesis is not closed properly ?! that seems stupid error to make for the translation tool to js really. The annoying thing is that it doesn’t show the line where the problem is.

It’s not that it’s actually missing a ), it’s that there’s a string it’s misreading and so it thinks that an argument is supposed to end earlier than it does. It’s not an intuitive error. I’ll try to explain:

if you had this code:

var a = "lorem ipsum";
var b = "dolor si amet";

textComponent.setText(a b);

You would get the same error because the function expects only one string as an argument, and the strings in this example aren’t concatenated properly. So, it’s looking for a “)” after a, failing to find it, and throwing the error, rather than telling you why it’s not a valid argument.

EDIT: As for the actual error I’m seeing now, you commented out too many import statements! You should only comment out the ones that you added in code components. The import statements at the start of the file work because they are referring to JS libraries that actually exist (and that are needed), so you should put those back in.

The reason import doesn’t work in a code component isn’t that you can’t import anything into JS, it’s that the things you try to import from Python don’t exist in JS, because they’re just totally different languages. NumPy is Python, there is no JS equivalent, for example. However, PsychoJS is a JS library (and a rather important one) so you should leave that import statement alone.

1 Like

Thank you for your great explanation once more!

Unfortunately, I did both ways with the imports and with them commented out as well and I tried again now (see URL).

The imports that are made by the JS are the following:

import { PsychoJS } from './lib/core-2020.1.js';
import * as core from './lib/core-2020.1.js';
import { TrialHandler } from './lib/data-2020.1.js';
import { Scheduler } from './lib/util-2020.1.js';
import * as util from './lib/util-2020.1.js';
import * as visual from './lib/visual-2020.1.js';
import * as sound from './lib/sound-2020.1.js';

I understand your point and is valuable insight what you say thank you!! So it has to do with a string the problem but that’s strange. Maybe should I try to rename the text objects I created in the builder (can it be such a stupid thing idk what else to think…)

Cheers,
Peter.

Dear psychopy mates,

Thank you once more for your help.

Suddenly today (after I made the changes with the text stimuli discussed here yesterday),
my exp is loaded and doesn’t stuck at initializing, but I started by getting an error "mouse.setPos(…) is not a function.

Then, after pain because I have the impression the system lags and doesn’t read the changes, I removed this part of the code (it’s purpose was to reset the mouse in the start of every trial. Anyway I try to get it work even without that).

At the moment I get the same error:
Cannot read property ‘map’ of undefined

Did you solve this before or know what does that mean ? I have 4 polygon stimuli in my Exp (and also 4 text stimuli) is it related ?

Thank you a lot. :slight_smile:

I believe this thread may be helpful: Randomization not working online (problem with "map"?)

To sum up, you can’t set the mouse’s position regardless. For the “map” issue, it’s probably something like this: Rotate a line when mouse is clicked on a stimuli

Basically check if you are trying to read off something about a polygon before it is actually being drawn, if you have code in a begin routine tab or something.

1 Like