Psychopy to Pavlovia code error: stuck on initialising experiment code error

URL of experiment: https://run.pavlovia.org/ellen_howard/1a/html/

Description of the problem:
I have used builder view to put together an experiment and used a couple of code components. The experiment runs fine in Psychopy, but I am having a problem getting it to pilot on Pavlovia. I used the following code to have an accuracy threshold that the participant must meet before their training ends:
respcorrs.append(resp.corr);

while len(respcorrs) >= minNrPractice:
respcorrsRecent = respcorrs[-considerNrTrials:];
respcorrsSum = sum(respcorrsRecent);
accuracy = float(respcorrsSum)/considerNrTrials;
if accuracy > minAccuracy:
break;
if len(respcorrs)>maxNrPractice:
break;
However, when I’m trying to link my experiment to Pavlovia, the experiment gets stuck on the initialising experiment screen. When I look at the developer tools on chrome it says the following error: Uncaught SyntaxError: Illegal break statement
So I tried to fix this in the code by changing it to a while statement. However, if I change the bottom two ifs to whiles, then the code no longer works in Psychopy. I was wondering if anyone can help me know what the solution might be for this code so that it will translate to Pavlovia?

Many thanks,
Ellen

Hi Ellen!

OK so assuming “minAccuracy” corresponds to your threshold and “minNrPractice” is the minimum number of practice trials? try something like this should work to end your training trials loop based on accuracy.

if accuracy > minAccuracy:
    continueRoutine = False
    trainingTrials.finished = True

and I think the JS should read

if ((accuracy > minAccuracy)) {
    continueRoutine = false;
    trials.finished = true;
}

In this replace “trainingTrials” with whatever the name of your loop is for the training routine and make sure the JS equivilent of this is edited to just be “trials” (since JS doesn’t like the loop specific names).

Let us know how you get on with that.
Becca

PS. it is also worth saying that whilst use of “break” would be the right way to go about ending a loop in the coder, in the builder we explicitly say a routine or loop has finished, which is where it is subtly different from your solution there.

Hi Becca,

Thanks for this!
I have subbed in your bit of code to the last line of the EndRoutine tab as follows:

respcorrs.apend(resp.corr)

while len(respcorrs) >= minNrPractice:

respcorrsRecent = respcorrs[-considerNrTrials:]

respcorrsSum = sum(respcorrsRecent)  
accuracy = float(respcorrsSum)/considerNrTrials

if accuracy>minAccuracy:
continueRoutine = False
trials.finished = True

This is what I have in the begin experiment:
respcorrs = []
minNrPractice = 30; # min number of trials to practice
considerNrTrials = 30; # number of trials to consider for accuracy calculation
minAccuracy = 0.75;

After trying to run it, the program ends after the first trial as it says accuracy is not defined, even though it did not have this error previously. I’ve tried defining accuracy in the BeginExperiment and BeginRoutine tab, but that didn’t work. I’m sure I’m missing something really obvious here!

Thanks,
Ellen

I managed to fix the above code and now it runs in Pavlovia, thank you Becca!

One thing is that the code I have in my training phase is meant to cut out when participants reach 80%, after completing a minimum of 30 trials. I’ve currently got it at 3 repetitions so that if the accuracy isn’t reached, the routine ends after 36 trials anyway. In Psychopy it works perfectly and will stop after 30 if accuracy is above 80. But in Pavlovia, it doesn’t the stop routine isn’t working, as it will just run the full number of trials regardless of accuracy. Here is the code I have:

Begin experiment
respcorrs = []
minNrPractice = 30 # min number of trials to practice
considerNrTrials = 30 # number of trials to consider for accuracy calculation
minAccuracy = 0.8

End routine
respcorrs.append(resp.corr);

if len(respcorrs) >= minNrPractice:

respcorrsRecent = respcorrs[-considerNrTrials:]

respcorrsSum = sum(respcorrsRecent)  # only works if error=0 and correct=1

accuracy = float(respcorrsSum)/considerNrTrials

if accuracy > minAccuracy:
    continueRoutine = False
    trials.finished = True

Do you know if there is a reason that this code wouldn’t work on Pavlovia?
Thanks,
Ellen

I think I may have fixed the problem I put in my latest reply, but I can’t test it because of another error I’m getting.

I added a loop to randomise the order of two blocks, I used the following code component in the begin experiment tab:

orders = [[1, 0], [0, 1]]

shuffle(orders)

Then set the reps of the block loops to: orders[0][outer_loop_name.thisN] and orders[1][outer_loop_name.thisN]

It works perfectly offline, but I get this error when running on Pavlovia TypeError: Cannot read property ‘0’ of undefined

Any idea of a JS solution to this?

Thanks!

Hello,

any solution yet? Try to define two different orders (order1, order2) and randomly select one.

Cheers Jens

Use an index variable instead of .thisN