Accessing Experiment Data Via Code Component for PsychoJS

URL of experiment: https://gitlab.pavlovia.org/Lrivera/gui-based-round-2

Description of the problem:
In this version of the IAT, I’ve built most of it in builder and have succeeded in writing a few parts of custom code for both the JS and Py side of PsychoJS/Py. In particular, I am struggling with accessing data after running a trial (e.g. accessing Block 1 data while in Block 7).

Ideally, I’d be able to access participants’ responses after the final block of the IAT so that I can calculate a D score (via Greenwald, Nosek, & Banaji’s 2003 paper) in order to provide feedback to participants at the end of the experiment (The IAT is just a small part of a much larger experiment).

I’m trying to do something like this (the use of Block 3 is arbitrary):

eachResp = iterator going through the length of Block 3
Block3_RTs_Array = empty array hoping to collect RTs of correct responses

for (eachResp=0; eachResp<=psychoJS.experiment._trialsData.length; eachResp++) {
if (psychoJS.experiment._trialsData[eachResp][“Block3_Resp.corr”] ==1)
{Block3_RTs_Array.push(psychoJS.experiment._trialsData[eachResp][‘Block3_Resp.rt’])

I believe there is something wrong with using this (psychoJS.experiment._trialsData[eachResp][“Block3_Resp.corr”]) notation as I am accessing undefined variables.

psychoJS.experiment.addData works perfectly!

@dvbridges , Thank you for offering to show me ways of accessing data via the code component.

Please let me know if there’s anything else I can provide with regards to an explanation - after working on this for so long, I can imagine that there might be a detail or two missing from this post.

1 Like

Hi @Laur_R, for online tasks, you can access your response data using the following code. This example assumes you have a keyboard called key_resp and it has the correct answers defined in the keyboard component:

// Get JS array of trial objects (i.e., a list of python dicts with response data)
dat = psychoJS.experiment._trialsData

// Filter data to get correct trials
corr = dat.filter((trial) => trial['key_resp.corr'] === 1)

// Get RTs only as an array
rts = corr.map((trial) => trial['key_resp.rt'])

// Reduce RTs to a single number : the mean
meanRT = rts.reduce((a, b) => a + b) / rts.length

Thanks @dvbridges!

One thing I am running into is a problem with the reduce function. It seems that the reaction times are not viewed as numbers (I am getting NaN for the mean). I am trying to track things via the developer console.

Thank you for the code, I am starting to get a better grasp of PsychoJS (next I’ll need to calculate the standard deviation, but I guess that’ll be intuitive after calculating a mean).

I’ve tried using parseInt, as well as using .map notation, but this does not seem to work.

Ok, would you be able to share the URL?

I’m a bit unsure of which url to share, here is the link to my pavlovia site, and to my github respectively

Thanks, not sure what is happening right now, will know more when I get my laptop, but in the code below you are missing the map methods described above, which are required to turn your response objects into an array which is in turn reduced to a single number (the mean):

corr = dat.filter((trial) => (Block1['block1_rts']< 10))
console.log("end of exp filtering: ")
  
console.log(corr)

console.log("mean for block 1 accurate rts:")
  
meanRT = corr.reduce((a, b) => a + b) / corr.length
console.log(meanRT)

Thank you @dvbridges!

I was able to work through the code and finish the IAT.

For any folks following this discussion who might want to look at a PsychoJS version of the IAT, here is a link to the project: