Using Experiment Data Via Code Component (PsychoJS) for online adjustment of task difficulty

URL of experiment: dual-d-detection [PsychoPy]

code: https://gitlab.pavlovia.org/matteo_lab/dual-d-detection

Description of the problem:
Dear all

I am new to psychopy, javascript (and online experiments in general) but, thanks to all the tutorials and useful advice on this forum, I have managed to set up my experiment, which has a relatively complex structure: participants have to make 2 decisions sequentially each trial, with the stimulus for decision 2 being determined by the correctness of the response to decision 1 (similar to this https://www.nature.com/articles/s41562-020-00953-1 but with detection task rather than discrimination).

Overall the experiment works as intended except for for the online adjustment of difficulty.
What it should do is keeping track of the accuracy of responses to the first decision, then every N trials (currently set to N=10) it should increase/decrease the contrast of the stimulus depending on whether the accuracy is below/above a certain range (currently set to 0.65, 0.75).

To achieve that, what I have done is create a routine that every N trials check the accuracy of the last N responses and adjust the contrast parameter (also offers the chance to take a break). In particular the routine has this javascript code:

nTrialsSoFar = psychoJS.experiment._trialsData.length;
nCorr = 0;
eachResp = 0;
for (eachResp=nTrialsSoFar-9; eachResp<nTrialsSoFar; eachResp++) 
{
  nCorr += psychoJS.experiment._trialsData[eachResp]['key_resp.corr'];
}
acc_current = nCorr/10;


if ((acc_current > 0.75)) {
    contrast_lvl = (contrast_lvl - 1);
}
if ((acc_current < 0.65)) {
    contrast_lvl = (contrast_lvl + 1);
}
if ((contrast_lvl < 1)) {
    contrast_lvl = 1;
}
if ((contrast_lvl > 10)) {
    contrast_lvl = 10;
}

where key_resp is the name of the keyboard component that I want to use to monitor accuracy

While on the one hand it seems to work (I can reach the point in the experiment where I can take a break and continue without problems) however the contrast of the stimulus does not seem to change, regardless of how many errors/correct responses I make.

I am not sure of what I am doing wrong here, any help/advice would be greatly appreciated.
Thanks!

Using the approach described here I managed to do some debugging and understand a little bit more what is going on.

The issue was due to some values of psychoJS.experiment._trialsData[eachResp]['key_resp.corr'] that were undefined. Apparently every 10 trials my code generate 20 _trialsData, and half of them was undefined.

Amending the code as below seems to work as intended:

nTrialsSoFar = psychoJS.experiment._trialsData.length;
nCorr = 0;
eachResp = 0;
for (eachResp=nTrialsSoFar-19; eachResp<nTrialsSoFar; eachResp++) 
{
  if (typeof psychoJS.experiment._trialsData[eachResp]['key_resp.corr'] !== 'undefined')
    {
      nCorr += psychoJS.experiment._trialsData[eachResp]['key_resp.corr'];
    }
}
acc_current = nCorr/10;

However, I still do not understand why I get 20 “trialsData” after having run only 10 trials.
It seems that I have an alternation of “good” trialsData, which looks like this:

psychoJS.experiment._trialsData[0]
{…}
OS: "Linux x86_64"
Tpath_D1: "stimuli/N/48.bmp"
Tpath_D2: "stimuli/S/SCS6/1.bmp"
"X Scale": 61.5
"Y Scale": 64
contrast_lvl_D1: 6
contrast_lvl_D2: 6
"correct_D1.ran": 1
"correct_D1.thisIndex": 0
"correct_D1.thisN": 0
"correct_D1.thisRepN": 0
"correct_D1.thisTrialN": 0
cresp: undefined
date: "2021-01-25_17h53.09.184"
decision_D1: 1
decision_D2: 2
expName: "dual-d-detection"
frameRate: 60
"key_resp.corr": 1
"key_resp.keys": "n"
"key_resp.rt": 2.0950000000000273
"key_resp_corrD1.corr": 1
"key_resp_corrD1.keys": "s"
"key_resp_corrD1.rt": 0.5600000000000023
participant: "E2"
psychopyVersion: "2020.1.2"
"ready.keys": "N/A"
"ready.rt": 4.423999999999978
"ready_2.keys": "3"
"ready_2.rt": 1.2930000000000064
session: "001"
trialtype: "S"

with other trialsData that looks like this

psychoJS.experiment._trialsData[1]
{…}
OS: "Linux x86_64"
cresp: "n"
date: "2021-01-25_17h53.09.184"
expName: "dual-d-detection"
frameRate: 60
participant: "E2"
psychopyVersion: "2020.1.2"
session: "001"
"trials.ran": 1
"trials.thisIndex": 1
"trials.thisN": 0
"trials.thisRepN": 0
"trials.thisTrialN": 0
trialtype: "N"