Skip to content

Commit

Permalink
Merge pull request #130 from jspsych/fix-video-multi-test
Browse files Browse the repository at this point in the history
updates to data structure and simulation, remove test that cannot run without mock video element
  • Loading branch information
jodeleeuw authored Aug 9, 2024
2 parents 3f9f953 + 382f075 commit 13fbdf2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-pumpkins-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@jspsych-contrib/plugin-video-several-keyboard-responses": major
---

Make data `rt` `key` and `video_time` fields always be an array, even when multiple responses are not allowed. Improve compatibility of simulation mode, though there are still some likely inconsistencies.
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,24 @@ describe("video-several-keyboard-responses simulation", () => {

await expectFinished();

expect(getData().values()[0].rt).toBeGreaterThan(0);
expect(typeof getData().values()[0].response).toBe("string");
const data = getData().values()[0];

expect(data.rt.every((value) => value > 0)).toBe(true);
expect(data.response.length).toEqual(data.rt.length);
expect(data.video_time.length).toEqual(data.rt.length);
});

// can't run this until we mock video elements.
test("visual mode works", async () => {
test.skip("visual mode works", async () => {
const jsPsych = initJsPsych();

const timeline = [
{
type: videoSeveralKeyboardResponses,
stimulus: ["foo.mp4"],
prompt: "foo",
trial_duration: 1000,
trial_ends_after_video: true,
response_ends_trial: false,
},
];

Expand All @@ -78,5 +82,6 @@ describe("video-several-keyboard-responses simulation", () => {
expect(rt.every((value) => value > 0)).toBe(true);
expect(response.every((value) => typeof value === "string")).toBe(true);
expect(video_time.every((value) => typeof value === "number")).toBe(true);
expect(response.length).toEqual(rt.length);
});
});
35 changes: 23 additions & 12 deletions packages/plugin-video-several-keyboard-responses/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,8 @@ class VideoSeveralKeyboardResponsesPlugin implements JsPsychPlugin<Info> {
"#jspsych-video-several-keyboard-responses-stimulus"
).className += " responded";

// by default only record the first response
if (response.key == null) {
if (!trial.multiple_responses_allowed) {
// Would make sense to add it to a list, but then it would not be backwards compatible?
response = { rt: info.rt, key: info.key, video_time: video_element.currentTime };
} else {
response = { rt: [info.rt], key: [info.key], video_time: [video_element.currentTime] };
}
response = { rt: [info.rt], key: [info.key], video_time: [video_element.currentTime] };
} else if (trial.multiple_responses_allowed) {
response.rt.push(info.rt);
response.key.push(info.key);
Expand Down Expand Up @@ -366,7 +360,9 @@ class VideoSeveralKeyboardResponsesPlugin implements JsPsychPlugin<Info> {

const respond = () => {
if (data.rt !== null) {
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
for (let i = 0; i < data.rt.length; i++) {
this.jsPsych.pluginAPI.pressKey(data.response[i], data.rt[i]);
}
}
};

Expand All @@ -378,16 +374,31 @@ class VideoSeveralKeyboardResponsesPlugin implements JsPsychPlugin<Info> {
}

private create_simulation_data(trial: TrialType<Info>, simulation_options) {
let n_responses = this.jsPsych.randomization.randomInt(1, 5);
if (!trial.multiple_responses_allowed) {
n_responses = 1;
}

const rts = [];
const responses = [];
let last_rt = 0;
for (let i = 0; i < n_responses; i++) {
const rt = Math.round(this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true));
rts.push(rt + last_rt);
last_rt = rt;
responses.push(this.jsPsych.pluginAPI.getValidKey(trial.choices));
}

const default_data = {
stimulus: trial.stimulus,
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
video_time: 0,
response: responses,
rt: rts,
video_time: rts,
};

const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);

this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
//this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);

return data;
}
Expand Down

0 comments on commit 13fbdf2

Please sign in to comment.