Code Component not working when running in Pavlovia

Hi @jonathan.kominsky ,
Thanks for the response!
I translated the first part to check it, and it appears in the code but doesn’t do what I expected it to do:

if (sit_trials.thisN === 0){
    stimulusList = [];
    for (var i = 0, i < 66, i++){
        stimulusList[i] = sit_trials.trialList[i]
        }
    Util.shuffle(stimulusList);
    Stimulus = stimulusList[0]['Audio']; 
    condition = stimulusList[0]['condition'];
    sound.Sound(Stimulus);
    sit_trials.addData('Audio', Stimulus);
    sit_trials.addData('condition', condition);
    sit_trials.trialList.pop(Stimulus);
}

As for the other parts, I couldn’t figure out how to translate them properly yet.
Thanks!

I have tried it with

if (ImageConditions.thisTrialN !==67 && ImageConditions.thisTrialN !==105 && ImageConditions.thisTrialN !==203){
continueRoutine=false;
}

and

if (ImageConditions.thisN !==67 && ImageConditions.thisN !==105 && ImageConditions.thisN !==203){
continueRoutine=false;
}

it does not show the the pause routine after 67 (68) trials, but it also doesn’t show it every trial loop either so it is doing something…

@N_T When you say it’s not doing what you expected, what exactly do you mean? I have seen people run into trouble with creating sound.Sound objects via code components, but that typically throws a bunch of errors.

@Stuart_Furnell I assume it’s not showing any of the breaks then? What’s happening seems to be an issue of how the conditional is being evaluated. Before, when it was showing on every trial, the condition was always false. Now the condition is always true, even when it’s not supposed to be. So, at the very least, we are making some progress. I would suggest replacing !== with != and seeing if that makes a difference to start with. If it doesn’t, try using trials.thisTrialN instead of ImageConditions.thisTrialN. If neither of those work I’ll take a closer look at the code and see what I can figure out.

@jonathan.kominsky thank you i will try those suggestions out now.

It is indeed not showing any of the breaks now.

really appreciate the help, thank you.

@jonathan.kominsky You’re right, I should have been more clear. I’m not having trouble with creating sound objects (yet), and I do not get any error.

The full Python code above is meant to prevent trials from the same conditions to appear more than three times in a row, and also to add attention checks (catch trials) every 10th trial. To avoid repetitions of the same item, every item is popped of trialList after it appears.
The first part is there because I set presentation method to be sequential and not random, as was suggested in another post when using code to randomize.
The second part is supposed to create a shuffled trials list, and then run sequentially on it. The second part checks if the current item is from a condition which repeated three consecutive times, and if it is - reshuffling occurs.
The third part is supposed to insert catch trials every 10th trial.
Does it make any sense?

When I sync the study from the builder, this code is not compiled in Pavlovia, and items are presented sequentially.

Thanks a lot for your time, much appreciated!

I tried it with != and also with trials.thisTrialN and still nothing :frowning: it isn’t showing any breaks again. It worked perfectly in builder.

Any more help will be appreciated.

Do you mind sharing your experiment so we could have a look?

the whole code base in PY or JS (what’s in Pavlovia)?

@jonathan.kominsky I got Pavlovia to acknowledge my added code, which is now:
##Begin routine:

if (sit_trials.thisTrialN === 0){
    while (sit_trials.trialList[0]['condition'] === 'Catch'){
        Util.shuffle(sit_trials.trialList)
     }
    Stimulus = sit_trials.trialList[0]['Audio'];
    condition = sit_trials.trialList[0]['condition'];
    sound.Sound(Stimulus);
    sit_trials.addData('Audio', Stimulus);
    sit_trials.addData('condition', condition);
    sit_trials.trialList.pop(Stimulus);
}

But now I get this error:
ReferenceError: sound is not defined

What did I do wrong?

Thanks again!

You did it right, this is the error I mentioned earlier when I said that sound.Sound doesn’t really work in a code component in JavaScript. See here: Type Error: Cannot read property '_psychoJS' of undefined

Sound objects really need to be created in the builder, and while you can start or stop them or change their order in a code component, it is sadly not possible to create a new one from a code component.

Oh, OK, so do you have any suggestion how to randomize by the conditions I mentioned above, given that the sound object is already created in the builder? I just want the shuffling to be conditional, and to insert items of a certain condition every 10th trial. So to look at the first part as an example, should it be something like:

if (sit_trials.thisTrialN === 0){
    while (sit_trials.trialList[0]['condition'] === 'Catch'){
    Util.shuffle(sit_trials.trialList)
    }
    Stimulus = sit_trials.trialList[0]['Audio'];
    condition = sit_trials.trialList[0]['condition'];
    Stimulus.play();
    sit_trials.addData('Audio', Stimulus);
    sit_trials.addData('condition', condition);
    sit_trials.trialList.pop(Stimulus);
}

I’m new to Pavlovia and PsychoPy, I’m very sorry for the trouble but I just can’t figure this out completely.
Thanks again!

When you go to “view code” for your experiment in Pavlovia, it should take you to a complete gitlab repository that includes both the python and the JS code, as well as the psyexp file and any stimulus files and data files. Basically the whole experiment folder. You will need to make that repository public, go to “settings” then “general” and “permissions”. You can then link the repository, which will have a URL like No repository · Jonathan Kominsky / TestMouseContains · GitLab but with your username and project name after the slashes.

@N_T IS the basic idea that you are shuffling a larger list of trials and want it to be mostly random, but also to have certain non-random trials? In that case I would suggest an approach like the one I proposed in this thread (read to end): Randomising types of trials so the same stimuli do not repeat one after the other - #4 by jonathan.kominsky

Long story short you will have a file with all of your trials in it, you will have some code which manually shuffles arrays that represent the contents of those trials, and you will then use that array to reference the relevant stimuli. You are basically making your whole order as a separate list and then using it as a key to your actual stimuli, but trying to let PsychoPy do all of the loading and presentation with its built-in systems.

@jonathan.kominsky That is pretty much the basic idea. I want trials to be randomized but to prevent trials from the same condition to appear more than 3 consecutive times, and also to have some trials in fixed locations in the loop. Does your proposal allow this?
I will read this thread anyway, thank you very much!

Thanks, i am not sure i can share the whole experiment folder due to peoples personal images being used etc, the pavlovia experiment wasn’t going to be made public. I can certainly get the code minus the stimuli etc though.

i am just rebuilding the builder and putting the elements you previously mentioned in there again from afresh and see if that helps, as i synced so many changed versions it may not have synced correctly.

@Stuart_Furnell I also assume you have read through this whole thread: "Take a break" works locally but not online

Your case is actually slightly more difficult because you are targeting specific trials that don’t have an obvious regular structure. That said, the logging I suggested there is also worth doing here, it will let you see the values you are working with and when the conditions are or are not being met.

Thank you, i had read through that and other posts regarding JS and PY in pavlovia, feels like i am banging my head against the wall. I will keep trying and utilise the logging also.

The trials are set to random, but the structure is pretty regular, each loop is a new random line from the cond file and when it gets to the 68th loop show the pause routine, and then another 68 and show the pause routine and finally another 68 and then the pause routine and then 68 and then end (thank you) routine. I could do it by breaking the cond file into 4 parts but it will lose the full randomisation

Right now it looks like the JS doesn’t have the code component at all. Make sure the code component is set to auto->JS or both, so that the code actually appears in the file Pavlovia will ultimately run. When you sync your study to pavlovia, the running window in PsychoPy should tell you it’s compiling the JS script. Is it throwing any errors when it does this?

Ah, sorry, found it. Interesting. So the current behavior is that it always skips the trial, meaning that this condition is always true, even on trials 68 etc.

Let’s try the following JS code:

consoel.log(ImageConditions.thisN);
if (ImageConditions.thisN != 67){
    continueRoutine=false;
} 

This is basically just an attempt to isolate, one piece at a time, all the things that could be going wrong, and create a little paper trail while we’re at it. If you open the browser’s JS console (in Chrome, it’s under view -> developer), the console.log statement should display the trial number on each trial. If it does not, that means ImageConditions.thisN isn’t what you should use, so you can try it with trials.thisTrialN and such as well. Having it only focus on trial 67 is just to make sure the basic conditional logic works at all. Once we get that working, we worry about trials 105 and 203.

thank you, that is strange behaviour, i will try as you have suggested and get back to you.

Again, your help is greatly appreciated.

forgive me, where will i see the trial numbers in the console log view? I have a lot of what is attached…I amended the code to 3 rather than 67 for the purposes of testing, make it quicker