Begining docs and marking updates for metadata fetching

This commit is contained in:
vzhang03 2024-07-08 15:06:06 -04:00
parent 631150e8fe
commit 8e9835f285
3 changed files with 72 additions and 11 deletions

View File

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

View File

@ -15,10 +15,10 @@ export class PluginCache {
* @returns {Promise<string|null>} The description of the plugin variable if found, otherwise null. * @returns {Promise<string|null>} The description of the plugin variable if found, otherwise null.
* @throws Will throw an error if the fetch operation fails. * @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 // fetches if it doesn't exist
if (!(pluginType in this.pluginFields)) { if (!(pluginType in this.pluginFields)) {
const fields = await this.generatePluginFields(pluginType, version); const fields = await this.generatePluginFields(pluginType, version, extension);
this.pluginFields[pluginType] = fields; this.pluginFields[pluginType] = fields;
} }
@ -31,8 +31,8 @@ export class PluginCache {
}; };
} }
private async generatePluginFields(pluginType: string, version?) { private async generatePluginFields(pluginType: string, version, extension?) {
const script = await this.fetchScript(pluginType, version); const script = await this.fetchScript(pluginType, version, extension);
// parses if they exist // parses if they exist
if (script !== undefined && script !== null && script !== "") if (script !== undefined && script !== null && script !== "")
@ -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 // 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`; // 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`; const unpkgUrl = `http://localhost:3000/plugin/${pluginType}/index.ts`;
try { try {
@ -57,7 +63,7 @@ export class PluginCache {
pluginType, pluginType,
"with error", "with error",
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; return undefined;
} }

View File

@ -300,8 +300,10 @@ export default class JsPsychMetadata {
// variables can be thought of mapping of one column in a row // variables can be thought of mapping of one column in a row
const version = observation["version"] ? observation["version"] : null; // changed const version = observation["version"] ? observation["version"] : null; // changed
const pluginType = observation["trial_type"]; const pluginType = observation["trial_type"];
const extensionType = observation["extension_type"]; // fix for non-list (single item extension) const extensionType = observation["extension_type"]; // fix for non-list (single item extension)
const extensionVersion = observation["extension_version"]; const extensionVersion = observation["extension_version"];
const ignored_fields = new Set(["trial_type", "trial_index", "time_elapsed"]); const ignored_fields = new Set(["trial_type", "trial_index", "time_elapsed"]);
for (const variable in observation) { for (const variable in observation) {
@ -314,16 +316,16 @@ export default class JsPsychMetadata {
await this.generateMetadata(variable, value, pluginType, version); await this.generateMetadata(variable, value, pluginType, version);
extensionType extensionType
? extensionType.forEach((ext, index) => ? extensionType.forEach((ext, index) =>
this.generateMetadata(variable, value, ext, extensionVersion[index]) this.generateMetadata(variable, value, ext, extensionVersion[index], true)
) // verify ) // verify
: console.log("No extensionType"); : 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 // 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 description = pluginInfo["description"];
const new_description = description const new_description = description
? { [pluginType]: description } ? { [pluginType]: description }
@ -423,7 +425,7 @@ export default class JsPsychMetadata {
* @returns {Promise<string|null>} The description of the plugin variable if found, otherwise null. * @returns {Promise<string|null>} The description of the plugin variable if found, otherwise null.
* @throws Will throw an error if the fetch operation fails. * @throws Will throw an error if the fetch operation fails.
*/ */
private async getPluginInfo(pluginType: string, variableName: string, version?) { private async getPluginInfo(pluginType: string, variableName: string, version, extension?) {
return this.pluginCache.getPluginInfo(pluginType, variableName, version); return this.pluginCache.getPluginInfo(pluginType, variableName, version, extension);
} }
} }