Skip to content

Commit

Permalink
Begining docs and marking updates for metadata fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhang03 committed Jul 8, 2024
1 parent 631150e commit 8e9835f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
53 changes: 53 additions & 0 deletions docs/reference/jspsych-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Metadata Module

The metadata module contains functions for interacting metadata according to [Psych-DS standards](https://psych-ds.github.io/). To interact with the metadata, we strongly recommend you call the generate method on the experiment data then adjust fields accordingly. The generate method uses documentation for plugins and extensions created in the main JsPsych repo to create default descriptions. This works best for experiments run in v8+, but will also work for old experimental data.

---

## metadata.generate





### Examples



#### Calling metadata after running an experiment

```javascript
var metadata = new jsPsychMetadata();

const metadata_options = {
randomField: "this is a field",
author: {
"John": {
name: "John",
givenName: "Johnathan",
},
},
variables: {
"trial_type" : {
description: {
"chat-plugin": "this chat plugin allows you to talk to gpt!",
}
},
"trial_index": {
name: "index",
},
},
}

var jsPsych = initJsPsych({
on_finish: async function() {
await metadata.generate(jsPsych.data.get().json());
},
default_iti: 250
});
```

#### Calling metadata on data loaded locally



18 changes: 12 additions & 6 deletions packages/metadata/src/PluginCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export class PluginCache {
* @returns {Promise<string|null>} The description of the plugin variable if found, otherwise null.
* @throws Will throw an error if the fetch operation fails.
*/
async getPluginInfo(pluginType: string, variableName: string, version?) {
async getPluginInfo(pluginType: string, variableName: string, version, extension?) {
// fetches if it doesn't exist
if (!(pluginType in this.pluginFields)) {
const fields = await this.generatePluginFields(pluginType, version);
const fields = await this.generatePluginFields(pluginType, version, extension);
this.pluginFields[pluginType] = fields;
}

Expand All @@ -31,8 +31,8 @@ export class PluginCache {
};
}

private async generatePluginFields(pluginType: string, version?) {
const script = await this.fetchScript(pluginType, version);
private async generatePluginFields(pluginType: string, version, extension?) {
const script = await this.fetchScript(pluginType, version, extension);

// parses if they exist
if (script !== undefined && script !== null && script !== "")
Expand All @@ -42,9 +42,15 @@ export class PluginCache {
}
}

private async fetchScript(pluginType: string, version?) {
private async fetchScript(pluginType: string, version: string, extension?: boolean) {
// implement logic here how to use version field
// match upon name (extension version) and name ->
// const unpkgUrl = `https://unpkg.com/@jspsych/plugin-${pluginType}/src/index.ts`;
console.log(
"fetchScript parameters: pluginType (" + pluginType + "), version (",
version,
"), extension(" + extension + ")"
); // console logging each fetch
const unpkgUrl = `http://localhost:3000/plugin/${pluginType}/index.ts`;

try {
Expand All @@ -57,7 +63,7 @@ export class PluginCache {
pluginType,
"with error",
error,
"Note: if you are using a plugin not in the main JsPsych branch this will always fail."
"Note: if you are using a plugin not supported the main JsPsych branch this will always fail."
);
return undefined;
}
Expand Down
12 changes: 7 additions & 5 deletions packages/metadata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ export default class JsPsychMetadata {
// variables can be thought of mapping of one column in a row
const version = observation["version"] ? observation["version"] : null; // changed
const pluginType = observation["trial_type"];

const extensionType = observation["extension_type"]; // fix for non-list (single item extension)
const extensionVersion = observation["extension_version"];

const ignored_fields = new Set(["trial_type", "trial_index", "time_elapsed"]);

for (const variable in observation) {
Expand All @@ -314,16 +316,16 @@ export default class JsPsychMetadata {
await this.generateMetadata(variable, value, pluginType, version);
extensionType
? extensionType.forEach((ext, index) =>
this.generateMetadata(variable, value, ext, extensionVersion[index])
this.generateMetadata(variable, value, ext, extensionVersion[index], true)
) // verify
: console.log("No extensionType");
}
}
}

private async generateMetadata(variable, value, pluginType, version?) {
private async generateMetadata(variable, value, pluginType, version, extension?) {
// probably should work in a call to the plugin here
const pluginInfo = await this.getPluginInfo(pluginType, variable, version);
const pluginInfo = await this.getPluginInfo(pluginType, variable, version, extension);
const description = pluginInfo["description"];
const new_description = description
? { [pluginType]: description }
Expand Down Expand Up @@ -423,7 +425,7 @@ export default class JsPsychMetadata {
* @returns {Promise<string|null>} The description of the plugin variable if found, otherwise null.
* @throws Will throw an error if the fetch operation fails.
*/
private async getPluginInfo(pluginType: string, variableName: string, version?) {
return this.pluginCache.getPluginInfo(pluginType, variableName, version);
private async getPluginInfo(pluginType: string, variableName: string, version, extension?) {
return this.pluginCache.getPluginInfo(pluginType, variableName, version, extension);
}
}

0 comments on commit 8e9835f

Please sign in to comment.