Skip to content

Commit

Permalink
fix #2462
Browse files Browse the repository at this point in the history
  • Loading branch information
jodeleeuw committed May 17, 2023
1 parent 612d9e1 commit 347bbb5
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-mails-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@jspsych/plugin-instructions": patch
---

Fix simulation mode behavior so that setting RT and/or view_history correctly sets the other parameter
59 changes: 59 additions & 0 deletions packages/plugin-instructions/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,63 @@ describe("instructions plugin simulation", () => {
expect(data.view_history.length).toBeGreaterThanOrEqual(6);
expect(data.view_history[data.view_history.length - 1].page_index).toBe(5);
});

test("Setting RT correctly sets the total length of the trial, #2462", async () => {
const timeline = [
{
type: instructions,
pages: ["page 1", "page 2", "page 3"],
simulation_options: {
data: {
rt: 4000,
},
},
},
];

const { getData, expectFinished } = await simulateTimeline(timeline);

await expectFinished();

const data = getData().values()[0];

console.log(data.view_history);

expect(data.rt).toBe(4000);

let sum_view_history_rt = 0;
for (const view of data.view_history) {
sum_view_history_rt += view.viewing_time;
}

// this may not be exactly 4000 due to rounding errors

expect(Math.abs(sum_view_history_rt - 4000)).toBeLessThan(10);
});

test("Setting view history correctly sets the total RT, #2462", async () => {
const timeline = [
{
type: instructions,
pages: ["page 1", "page 2", "page 3"],
simulation_options: {
data: {
view_history: [
{ page_index: 0, viewing_time: 1000 },
{ page_index: 1, viewing_time: 1000 },
{ page_index: 2, viewing_time: 1000 },
],
},
},
},
];

const { getData, expectFinished } = await simulateTimeline(timeline);

await expectFinished();

const data = getData().values()[0];

expect(data.rt).toBe(3000);
});
});
73 changes: 62 additions & 11 deletions packages/plugin-instructions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,23 +258,74 @@ class InstructionsPlugin implements JsPsychPlugin<Info> {
private create_simulation_data(trial: TrialType<Info>, simulation_options) {
let curr_page = 0;
let rt = 0;
const view_history = [];

while (curr_page !== trial.pages.length) {
const view_time = this.jsPsych.randomization.sampleExGaussian(3000, 300, 1 / 300);
view_history.push({ page_index: curr_page, viewing_time: view_time });
rt += view_time;
if (curr_page == 0 || !trial.allow_backward) {
curr_page++;
} else {
if (this.jsPsych.randomization.sampleBernoulli(0.9) == 1) {
let view_history = [];

// if there is no view history and no RT, simulate a random walk through the pages
if (!simulation_options.data?.view_history && !simulation_options.data?.rt) {
while (curr_page !== trial.pages.length) {
const view_time = Math.round(
this.jsPsych.randomization.sampleExGaussian(3000, 300, 1 / 300)
);
view_history.push({ page_index: curr_page, viewing_time: view_time });
rt += view_time;
if (curr_page == 0 || !trial.allow_backward) {
curr_page++;
} else {
curr_page--;
if (this.jsPsych.randomization.sampleBernoulli(0.9) == 1) {
curr_page++;
} else {
curr_page--;
}
}
}
}

// if there is an RT but no view history, simulate a random walk through the pages
// that ends on the final page when the RT is reached
if (!simulation_options.data?.view_history && simulation_options.data?.rt) {
rt = simulation_options.data.rt;
while (curr_page !== trial.pages.length) {
view_history.push({ page_index: curr_page, viewing_time: null });
if (curr_page == 0 || !trial.allow_backward) {
curr_page++;
} else {
if (this.jsPsych.randomization.sampleBernoulli(0.9) == 1) {
curr_page++;
} else {
curr_page--;
}
}
}
const avg_rt_per_page = simulation_options.data.rt / view_history.length;
let total_time = 0;
for (const page of view_history) {
const t = Math.round(
this.jsPsych.randomization.sampleExGaussian(
avg_rt_per_page,
avg_rt_per_page / 10,
1 / (avg_rt_per_page / 10)
)
);
page.viewing_time = t;
total_time += t;
}
const diff = simulation_options.data.rt - total_time;
// remove equal diff from each page
const diff_per_page = Math.round(diff / view_history.length);
for (const page of view_history) {
page.viewing_time += diff_per_page;
}
}

// if there is a view history but no RT, make the RT equal the sum of the view history
if (simulation_options.data?.view_history && !simulation_options.data?.rt) {
view_history = simulation_options.data.view_history;
rt = 0;
for (const page of simulation_options.data.view_history) {
rt += page.viewing_time;
}
}

const default_data = {
view_history: view_history,
rt: rt,
Expand Down

0 comments on commit 347bbb5

Please sign in to comment.