Skip to content

Commit

Permalink
ref: change window.jsPsych to use util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
YUUU23 committed Aug 14, 2024
1 parent fae7f47 commit 25b93e9
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/App/components/JsPsychExperiment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React from "react";
import { ENV } from "../../config/";
import { buildTimeline, jsPsychOptions } from "../../experiment";
import { initParticipant } from "../deployments/firebase";
import { getJsPsych } from "../../lib/utils";

// ID used to identify the DOM element that holds the experiment.
const EXPERIMENT_ID = "experiment-window";
Expand Down Expand Up @@ -70,7 +71,7 @@ export default function JsPsychExperiment({
// set up jsPsych object as global variable
window.jsPsych = jsPsych;
const timeline = buildTimeline(studyID, participantID);
window.jsPsych.run(timeline);
getJsPsych().run(timeline);
}
}, [jsPsych]);

Expand Down
1 change: 0 additions & 1 deletion src/experiment/procedures/endProcedure.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { exitFullscreenTrial } from "../trials/fullscreen";
* 1) Trial used to complete the user's camera recording is displayed
* 2) The experiment exits fullscreen
*
* @param {Object} jsPsych The jsPsych instance being used to run the task
* @returns {Object} A jsPsych (nested) timeline object
*/
export function buildEndProcedure() {
Expand Down
8 changes: 4 additions & 4 deletions src/experiment/procedures/honeycombProcedure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ENV, SETTINGS } from "../../config/";
import { eventCodes } from "../../config/trigger";
import { pdSpotEncode, photodiodeGhostBox } from "../../lib/markup/photodiode";
import { buildFixationTrial } from "../trials/fixation";
import { getJsPsych } from "../../lib/utils";

/**
* Builds the block of trials that form the core of the Honeycomb experiment
Expand All @@ -12,7 +13,6 @@ import { buildFixationTrial } from "../trials/fixation";
*
* Note that the block is conditionally rendered and repeated based on the task settings
*
* @param {Object} jsPsych The jsPsych instance being used to run the task
* @returns {Object} A jsPsych (nested) timeline object
*/
export function buildHoneycombProcedure() {
Expand All @@ -29,7 +29,7 @@ export function buildHoneycombProcedure() {
const taskTrial = {
type: imageKeyboardResponse,
// Display the image passed as a timeline variable
stimulus: window.jsPsych.timelineVariable("stimulus"),
stimulus: getJsPsych().timelineVariable("stimulus"),
prompt: function () {
// Conditionally displays the photodiodeGhostBox
if (ENV.USE_PHOTODIODE) return photodiodeGhostBox;
Expand All @@ -40,15 +40,15 @@ export function buildHoneycombProcedure() {
data: {
// Record the correct_response passed as a timeline variable
code: eventCodes.honeycomb,
correct_response: window.jsPsych.timelineVariable("correct_response"),
correct_response: getJsPsych().timelineVariable("correct_response"),
},
on_load: function () {
// Conditionally flashes the photodiode when the trial first loads
if (ENV.USE_PHOTODIODE) pdSpotEncode(eventCodes.honeycomb);
},
// Add a boolean value ("correct") to the data - if the user responded with the correct key or not
on_finish: function (data) {
data.correct = window.jsPsych.pluginAPI.compareKeys(data.response, data.correct_response);
data.correct = getJsPsych().pluginAPI.compareKeys(data.response, data.correct_response);
},
};

Expand Down
1 change: 0 additions & 1 deletion src/experiment/procedures/startProcedure.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { introductionTrial } from "../trials/introduction";
* 4) Trials used to set up a photodiode and trigger box are displayed (if applicable)
* 5) Trials used to set up the user's camera are displayed (if applicable)
*
* @param {Object} jsPsych The jsPsych instance being used to run the task
* @returns {Object} A jsPsych (nested) timeline object
*/
export function buildStartProcedure() {
Expand Down
9 changes: 5 additions & 4 deletions src/experiment/trials/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import initializeCamera from "@jspsych/plugin-initialize-camera";

import { LANGUAGE, ENV } from "../../config/";
import { div, h1, p, tag } from "../../lib/markup/tags";
import { getJsPsych } from "../../lib/utils";

const WEBCAM_ID = "webcam";

Expand Down Expand Up @@ -48,7 +49,7 @@ export function buildCameraStartTrial() {
throw new Error("video recording is only available when running inside Electron");
}

const cameraRecorder = window.jsPsych.pluginAPI.getCameraRecorder();
const cameraRecorder = getJsPsych().pluginAPI.getCameraRecorder();
if (!cameraRecorder) {
console.error("Camera is not initialized, no data will be recorded.");
return;
Expand Down Expand Up @@ -76,11 +77,11 @@ export function buildCameraStartTrial() {
// Assign camera feed to the <video> element
const camera = document.getElementById(WEBCAM_ID);

camera.srcObject = window.jsPsych.pluginAPI.getCameraRecorder().stream;
camera.srcObject = getJsPsych().pluginAPI.getCameraRecorder().stream;
},
on_finish: function () {
// Begin video recording
window.jsPsych.pluginAPI.getCameraRecorder().start();
getJsPsych().pluginAPI.getCameraRecorder().start();
},
},
],
Expand All @@ -107,7 +108,7 @@ export function buildCameraEndTrial() {
throw new Error("video recording is only available when running inside Electron");
}

const cameraRecorder = window.jsPsych.pluginAPI.getCameraRecorder();
const cameraRecorder = getJsPsych().pluginAPI.getCameraRecorder();
if (!cameraRecorder) {
console.error("Camera is not initialized, no data will be recorded.");
return;
Expand Down
3 changes: 2 additions & 1 deletion src/experiment/trials/fixation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SETTINGS, ENV } from "../../config/";
import { eventCodes } from "../../config/trigger";
import { pdSpotEncode, photodiodeGhostBox } from "../../lib/markup/photodiode";
import { div } from "../../lib/markup/tags";
import { getJsPsych } from "../../lib/utils";

/**
* Builds a trial with a fixation dot and optional photodiode box.
Expand All @@ -27,7 +28,7 @@ export function buildFixationTrial() {
trial_duration: function () {
if (fixationSettings.randomize_duration) {
// Select a random duration from the durations array to show the fixation dot for
return window.jsPsych.randomization.sampleWithoutReplacement(
return getJsPsych().randomization.sampleWithoutReplacement(
fixationSettings.durations,
1
)[0];
Expand Down
3 changes: 2 additions & 1 deletion src/experiment/trials/honeycombTrials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import preloadResponse from "@jspsych/plugin-preload";
import { LANGUAGE, SETTINGS } from "../../config/";
import { eventCodes } from "../../config/trigger";
import { b, div, image, p } from "../../lib/markup/tags";
import { getJsPsych } from "../../lib/utils";

const honeycombLanguage = LANGUAGE.trials.honeycomb;

Expand Down Expand Up @@ -61,7 +62,7 @@ export function buildDebriefTrial() {
* By accessing jsPsych inside the "stimulus" callback we have access to all of the data when this trial is run
* Calling jsPsych outside of the trial object would be executed to soon (when the experiment first starts) and would therefore have no data
*/
const responseTrials = window.jsPsych.data.get().filter({ code: eventCodes.honeycomb });
const responseTrials = getJsPsych().data.get().filter({ code: eventCodes.honeycomb });
const correct_trials = responseTrials.filter({ correct: true });
const accuracy = Math.round((correct_trials.count() / responseTrials.count()) * 100);
const reactionTime = Math.round(correct_trials.select("rt").mean());
Expand Down

0 comments on commit 25b93e9

Please sign in to comment.