Code component for additional data columns not working in Pavlovia (but does in Builder)

Hi all

I posted a couple of days ago about a code component for getting spacebar duration (Hold Time) and RT to key press from trial onset (Response Time). Thanks to Jonathan, these both work when run in the builder and the data file adds the variables correctly. See my last post here: Keyboard press response time from trial onset with coding component

However when uploading to Pavlovia, neither the Hold Time or Response Time (the two additional variables added to the data file) are not pulled through. Hold Time shows a , and response time does not even exist as a column in the data file.

The code I have used is below…experiment also attached if it helps…

Begin Routine
duration =
rt =
trialClock = core.Clock()
kb = keyboard.Keyboard(clock=trialClock)

Each Frame
keys2 = kb.getKeys()
for key in keys2:
if key.duration:
duration.append(key.duration)
if key.rt:
rt.append(key.rt)

End Routine
Practice_Trials_Loop.addData(“Hold Times”, duration)
Practice_Trials_Loop.addData(“Response Time”, rt)

Entropy Experiment FIlePractice.psyexp (41.9 KB)

Not sure about all the other auto-translated code, but the command to add data to the output in PsychoJS is psychoJS.experiment.addData('columnname', value).

1 Like

Thank you. I made this amendment (and a couple others which I have mentioned below) and unfortunately its not fully working, however both Hold Time and Response Time are both now showing in the data csv file but both with a instead of the data.

To clarify would it be

psychoJS.experiment.addData(“Hold Times”, duration);

OR

psychoJS.Practice_Trials_Loop.addData(“Hold Times”, duration);

Perhaps the JS code from the code component would help:

Begin Routine
duration = ;
rt = ;
trialClock = new util.Clock();
kb = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});

Each Frame
keys2 = kb.getKeys();
for (var key, _pj_c = 0, _pj_a = keys2, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
key = _pj_a[_pj_c];
if (key.duration) {
duration.push(key.duration);
}
if (key.rt) {
rt.push(key.rt);
}
}

End Routine
psychoJS.experiment.addData(“Hold Times”, duration);
psychoJS.experiment.addData(“Response Time”, rt)

Also posting the other changes I made - I hope this helps someone else!

I changed from trialClock = new core.Clock(); to trialClock = new util.Clock(); in Begin Routine in the JS code as pointed out on this post by wakecarter who says this needs to be manually changed as I got a core.Clock error when running in pavlovia - Thanks: Error: code1Clock = new util.Clock() - #2 by wakecarter

I also changed from kb = new keyboard.Keyboard({“clock”: trialClock}) to kb = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true}); in the JS code to remove a keyboard undefined error in pavlovia as mentioned in this post here: Failing to use Keyboard in Pavlovia - #2 by dvbridges

Hi @beckyttyler,

What exactly do you mean here? What does not work? Do you still need help?

Sorry that wasn’t cleat at all.

There is still no data showing in the csv files when downloading from pavlovia.

But with the amendments I made above, both Hold Times and Response Time are showing as columns in the data file, whereas before only Hold Times was. The issue is the missing data.

Screenshot 2022-10-26 at 14.26.00

Could you try replacing your each-frame-code with this?

keys2 = kb.getKeys();
for (var i in keys2) {
  key = keys2[i];
  if (key.duration) {
    duration.push(key.duration);
  }
  if (key.rt) {
    rt.push(key.rt);
  }
}

Thank you for your help and suggestions, but unfortunately there is still no data for Hold Times and Response Time showing in the data file, and instead a

I haven’t successfully measured keypress durations online yet.

Are you expecting simultaneous key presses or different keys?

I’ve previously used mouse button presses when I want press duration online.

Hi wakecarter

I am measuring just one key press duration RT per trial (using the space bar) as its a time reproduction experiment.

I have managed to successfully get key press duration RT of the space bar (which I have called Hold Times), and I currently have this working on pavlovia, with Hold Times showing in the csv. linked here Pavlovia

However I have needed to make some amendments to the code to also try to get the RT from trial onset to start of the key press to work on pavlovia but without much luck…now neither Hold Times or Response Times are showing in the data file. Note that the experiment I am enquiring about is the same as the linked one above, but I just have one ‘master’ copy (which is Pavlovia linked above), and then almost a ‘working experiment’ to mess around with to get RT from trial onset to start of the key press to work!

I’ve not very good at reading JS code, but this looks like the first iteration of this look would set

key = keys2[keys2[0]];

which isn’t going to work.

You could try replacing it with

for (key in keys2) {

However, when I Auto translate that I get

for (var key, _pj_c = 0, _pj_a = keys2, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
    key = _pj_a[_pj_c];

It’s much easier if you use Auto translate components whenever possible

https://psychopy.org/online/psychoJSCodingDebugging.html