From 8e9835f285e9cda4f0af365a6b47addb5ef277f3 Mon Sep 17 00:00:00 2001 From: vzhang03 Date: Mon, 8 Jul 2024 15:06:06 -0400 Subject: [PATCH] Begining docs and marking updates for metadata fetching --- docs/reference/jspsych-metadata.md | 53 ++++++++++++++++++++++++++++ packages/metadata/src/PluginCache.ts | 18 ++++++---- packages/metadata/src/index.ts | 12 ++++--- 3 files changed, 72 insertions(+), 11 deletions(-) diff --git a/docs/reference/jspsych-metadata.md b/docs/reference/jspsych-metadata.md index e69de29b..c318bde3 100644 --- a/docs/reference/jspsych-metadata.md +++ b/docs/reference/jspsych-metadata.md @@ -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 + + + diff --git a/packages/metadata/src/PluginCache.ts b/packages/metadata/src/PluginCache.ts index 8d889710..a365f8df 100644 --- a/packages/metadata/src/PluginCache.ts +++ b/packages/metadata/src/PluginCache.ts @@ -15,10 +15,10 @@ export class PluginCache { * @returns {Promise} 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; } @@ -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 !== "") @@ -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 { @@ -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; } diff --git a/packages/metadata/src/index.ts b/packages/metadata/src/index.ts index 915ef741..2ed3ad38 100644 --- a/packages/metadata/src/index.ts +++ b/packages/metadata/src/index.ts @@ -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) { @@ -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 } @@ -423,7 +425,7 @@ export default class JsPsychMetadata { * @returns {Promise} 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); } }