diff --git a/.changeset/lucky-glasses-crash.md b/.changeset/lucky-glasses-crash.md new file mode 100644 index 00000000..54522df1 --- /dev/null +++ b/.changeset/lucky-glasses-crash.md @@ -0,0 +1,6 @@ +--- +"jspsych": minor +--- + +Added `record_data` as a parameter available for any trial. Setting `record_data: false` will prevent data from being stored in the jsPsych data object for that trial. + diff --git a/docs/overview/data.md b/docs/overview/data.md index 4e2b0e36..e4676b31 100644 --- a/docs/overview/data.md +++ b/docs/overview/data.md @@ -70,6 +70,18 @@ var trial = { } ``` +### Skipping data collection for a particular trial + +Sometimes you may want to skip data collection for a particular trial. This can be done by setting the `record_data` parameter to `false`. This is useful if you want your data output to only contain the trials that collect relevant responses from the participant. For example, you might want to skip data collection for trials that just present a fixation cross for a fixed period of time. + +```js +var trial = { + type: jsPsychImageKeyboardResponse, + stimulus: 'imgA.jpg', + record_data: false +} +``` + ## Aggregating and manipulating jsPsych data When accessing the data with `jsPsych.data.get()` the returned object is a special data collection object that exposes a number of methods for aggregating and manipulating the data. The full list of methods is detailed in the [data module documentation](../reference/jspsych-data.md). diff --git a/docs/overview/plugins.md b/docs/overview/plugins.md index 5d79dd1c..534ee7cb 100644 --- a/docs/overview/plugins.md +++ b/docs/overview/plugins.md @@ -62,6 +62,7 @@ There is also a set of parameters that can be specified for any plugin: | css_classes | string | null | A list of CSS classes to add to the jsPsych display element for the duration of this trial. This allows you to create custom formatting rules (CSS classes) that are only applied to specific trials. For more information and examples, see the [Controlling Visual Appearance page](../overview/style.md) and the "css-classes-parameter.html" file in the jsPsych examples folder. | | save_trial_parameters | object | `{}` | An object containing any trial parameters that should or should not be saved to the trial data. Each key is the name of a trial parameter, and its value should be `true` or `false`, depending on whether or not its value should be saved to the data. If the parameter is a function that returns the parameter value, then the value that is returned will be saved to the data. If the parameter is always expected to be a function (e.g., an event-related callback function), then the function itself will be saved as a string. For more examples, see the "save-trial-parameters.html" file in the jsPsych examples folder. | | save_timeline_variables | boolean or array | `false` | If set to `true`, then all timeline variables will have their current value recorded to the data for this trial. If set to an array, then any variables listed in the array will be saved. +| record_data | boolean | `true` | If set to `false`, then the data for this trial will not be recorded. | ### The data parameter diff --git a/packages/jspsych/src/modules/data/index.ts b/packages/jspsych/src/modules/data/index.ts index 34247a88..cc9ede61 100644 --- a/packages/jspsych/src/modules/data/index.ts +++ b/packages/jspsych/src/modules/data/index.ts @@ -59,8 +59,10 @@ export class JsPsychData { write(trial: Trial) { const result = trial.getResult(); Object.assign(result, this.dataProperties); - this.results.push(result); - this.resultToTrialMap.set(result, trial); + if (trial.getParameterValue("record_data") ?? true) { + this.results.push(result); + this.resultToTrialMap.set(result, trial); + } } addProperties(properties) { diff --git a/packages/jspsych/src/timeline/index.ts b/packages/jspsych/src/timeline/index.ts index 19a3ced7..7f445d11 100644 --- a/packages/jspsych/src/timeline/index.ts +++ b/packages/jspsych/src/timeline/index.ts @@ -55,6 +55,11 @@ export interface TrialDescription extends Record { /** https://www.jspsych.org/latest/overview/extensions/ */ extensions?: Parameter; + /** + * Whether to record the data of this trial. Defaults to `true`. + */ + record_data?: Parameter; + // Events /** https://www.jspsych.org/latest/overview/events/#on_start-trial */ diff --git a/packages/jspsych/tests/data/recorddataparameter.test.ts b/packages/jspsych/tests/data/recorddataparameter.test.ts new file mode 100644 index 00000000..f266b6b1 --- /dev/null +++ b/packages/jspsych/tests/data/recorddataparameter.test.ts @@ -0,0 +1,75 @@ +import htmlKeyboardResponse from "@jspsych/plugin-html-keyboard-response"; +import { pressKey, startTimeline } from "@jspsych/test-utils"; + +import { initJsPsych } from "../../src"; + +describe("The record_data parameter", () => { + it("Defaults to true", async () => { + const { getData } = await startTimeline([ + { + type: htmlKeyboardResponse, + stimulus: "

foo

", + }, + ]); + + await pressKey(" "); + + expect(getData().count()).toBe(1); + }); + + it("Can be set to false to prevent the data from being recorded", async () => { + const { getData } = await startTimeline([ + { + type: htmlKeyboardResponse, + stimulus: "

foo

", + record_data: false, + }, + ]); + + await pressKey(" "); + + expect(getData().count()).toBe(0); + }); + + it("Can be set as a timeline variable", async () => { + const jsPsych = initJsPsych(); + const { getData } = await startTimeline( + [ + { + timeline: [ + { + type: htmlKeyboardResponse, + stimulus: "

foo

", + record_data: jsPsych.timelineVariable("record_data"), + }, + ], + timeline_variables: [{ record_data: true }, { record_data: false }], + }, + ], + jsPsych + ); + + await pressKey(" "); + await pressKey(" "); + + expect(getData().count()).toBe(1); + }); + + it("Can be set as a function", async () => { + const jsPsych = initJsPsych(); + const { getData } = await startTimeline( + [ + { + type: htmlKeyboardResponse, + stimulus: "

foo

", + record_data: () => false, + }, + ], + jsPsych + ); + + await pressKey(" "); + + expect(getData().count()).toBe(0); + }); +});