added record_data parameter

This commit is contained in:
Josh de Leeuw 2023-10-30 09:48:07 -04:00
parent 9926a797b9
commit 6f9d01b2ae
6 changed files with 103 additions and 2 deletions

View File

@ -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.

View File

@ -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 ## 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). 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).

View File

@ -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. | | 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_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. | 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 ### The data parameter

View File

@ -59,9 +59,11 @@ export class JsPsychData {
write(trial: Trial) { write(trial: Trial) {
const result = trial.getResult(); const result = trial.getResult();
Object.assign(result, this.dataProperties); Object.assign(result, this.dataProperties);
if (trial.getParameterValue("record_data") ?? true) {
this.results.push(result); this.results.push(result);
this.resultToTrialMap.set(result, trial); this.resultToTrialMap.set(result, trial);
} }
}
addProperties(properties) { addProperties(properties) {
// first, add the properties to all data that's already stored // first, add the properties to all data that's already stored

View File

@ -55,6 +55,11 @@ export interface TrialDescription extends Record<string, any> {
/** https://www.jspsych.org/latest/overview/extensions/ */ /** https://www.jspsych.org/latest/overview/extensions/ */
extensions?: Parameter<TrialExtensionsConfiguration>; extensions?: Parameter<TrialExtensionsConfiguration>;
/**
* Whether to record the data of this trial. Defaults to `true`.
*/
record_data?: Parameter<boolean>;
// Events // Events
/** https://www.jspsych.org/latest/overview/events/#on_start-trial */ /** https://www.jspsych.org/latest/overview/events/#on_start-trial */

View File

@ -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: "<p>foo</p>",
},
]);
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: "<p>foo</p>",
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: "<p>foo</p>",
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: "<p>foo</p>",
record_data: () => false,
},
],
jsPsych
);
await pressKey(" ");
expect(getData().count()).toBe(0);
});
});