From d07a2805741f0db102aa02f76979d925a0874df0 Mon Sep 17 00:00:00 2001 From: Gabriel Price Date: Fri, 7 Jul 2023 19:53:49 -0400 Subject: [PATCH] Generalized jspsych configurator --- .../lab/jspsych/psychometrics/Stroop.js | 145 ++++++++++++------ 1 file changed, 98 insertions(+), 47 deletions(-) diff --git a/neurofusion/next-client/src/components/lab/jspsych/psychometrics/Stroop.js b/neurofusion/next-client/src/components/lab/jspsych/psychometrics/Stroop.js index 70d7ec33..fd7ebb60 100644 --- a/neurofusion/next-client/src/components/lab/jspsych/psychometrics/Stroop.js +++ b/neurofusion/next-client/src/components/lab/jspsych/psychometrics/Stroop.js @@ -13,6 +13,71 @@ function mapElapsedToUnix(data){ } +fixation = { + type: jsPsychHtmlKeyboardResponse, + stimulus: '+', + trial_duration: 500, + response_ends_trial: false +}; + +// blank (ITI stands for "inter trial interval") +iti = { + type: jsPsychHtmlKeyboardResponse, + stimulus: '', + trial_duration: 250, + response_ends_trial: false +} +init = { + type: jsPsychHtmlKeyboardResponse, + stimulus: '', + trial_duration: 0, + response_ends_trial: false, + on_finish: function(data){ + data.unixTimestamp = Date.now(); + } +} + +class experimentGenerator{ + constructor(jsPsych,trialGenerator,instructions = null,feedBack = false, timeLimit=0,trialCountLimit=20){ + this.jsPsych = jsPsych; + this.trials = [] + instructions = null + if(instructions!=null) this.trials.push(instructions); + console.log(this.trials) + this.trials.push(init) + + let fixation_ = fixation + + if(timeLimit!=0){ + init.on_finish = function(data){ + data.unixTimestamp = Date.now(); + setTimeout(jsPsych.endExperiment,timeLimit*1000) + } + + } + + if(feedBack){ + fixation_.trial_duration = 750; + fixation_.stimulus = function () { + return jsPsych.data.get().last(1).trials[0].correct ? "Correct" : "Incorrect" + }; + + } + + for(let i = 0; i < trialCountLimit; ++i){ + this.trials.push(...trialGenerator.next().value) + if(!feedBack) this.trials.push(iti) + this.trials.push(fixation_) + } + } + + run(){ + jsPsych.run(this.trials) + } + +} + + var jsPsych = initJsPsych({on_finish: function() { window.parent.postMessage(mapElapsedToUnix(jsPsych.data.get()), '*'); @@ -54,22 +119,14 @@ var instructions = { show_clickable_nav: true } -var fixation = { - type: jsPsychHtmlKeyboardResponse, - stimulus: '+', - trial_duration: 500, - response_ends_trial: false -}; -// blank (ITI stands for "inter trial interval") -var iti = { - type: jsPsychHtmlKeyboardResponse, - stimulus: '', - trial_duration: 250, - response_ends_trial: false -} var keymap = {'r':0,'g':1,'b':2,'y':3} +let keymapInv = Object.entries(keymap).reduce((obj, [key, value]) => { + obj[value] = key; + return obj; + }, {}); + // keymap.map((x,y) => {y:x}) document.addEventListener('keypress', function(event) { // Allows dual inputs if(document.getElementsByClassName("keyboardToBtn").length==0) return @@ -79,44 +136,37 @@ document.addEventListener('keypress', function(event) { // Allows dual inputs } }); -var init = { - type: jsPsychHtmlKeyboardResponse, - stimulus: '', - trial_duration: 0, - response_ends_trial: false, - on_finish: function(data){ - data.unixTimestamp = Date.now(); - } - } + var trials = [instructions,init];//instructions // repeat this code n_trials times -for (var i=0; i'+values.text+'

', - // 'choices' restricts the available responses for the participant - choices: ['r','g','b','y'], - data: values, - on_finish: function(data){ - console.log(data) - // data.correct = jsPsych.pluginAPI.compareKeys(data.response, data.correct_response); + function* stroopGen() { + while(true){ + var values; + // Math.random returns a random number between 0 and 1. Use this to decide + // whether the current trial is congruent or incongruent. + if (Math.random() < 0.5) { + values = congruent(); + } else { + values = incongruent(); } - }; - trials.push(iti); - trials.push(fixation); - trials.push(trial); + var trial = { + type: jsPsychHtmlButtonResponse, + stimulus: + + '

'+values.text+'

', + // 'choices' restricts the available responses for the participant + choices: ['r','g','b','y'], + data: values, + on_finish: function(data){ + console.log(data) + data.response = keymapInv[data.response] + data.correct = data.colour[0] == data.response + } + }; + yield [trial] + } } @@ -132,6 +182,7 @@ for (var i=0; i