Skip to content

Commit

Permalink
added record_data parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jodeleeuw committed Oct 30, 2023
1 parent 9926a79 commit 6f9d01b
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/lucky-glasses-crash.md
Original file line number Diff line number Diff line change
@@ -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.

12 changes: 12 additions & 0 deletions docs/overview/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 1 addition & 0 deletions docs/overview/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions packages/jspsych/src/modules/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions packages/jspsych/src/timeline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export interface TrialDescription extends Record<string, any> {
/** https://www.jspsych.org/latest/overview/extensions/ */
extensions?: Parameter<TrialExtensionsConfiguration>;

/**
* Whether to record the data of this trial. Defaults to `true`.
*/
record_data?: Parameter<boolean>;

// Events

/** https://www.jspsych.org/latest/overview/events/#on_start-trial */
Expand Down
75 changes: 75 additions & 0 deletions packages/jspsych/tests/data/recorddataparameter.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 6f9d01b

Please sign in to comment.