Skip to content

Commit

Permalink
Completed inital metadata parameter to generate and allows for easy c…
Browse files Browse the repository at this point in the history
…ustomization of metadata output
  • Loading branch information
vzhang03 committed Jun 10, 2024
1 parent b369bc0 commit 1e267e3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
25 changes: 22 additions & 3 deletions examples/timeline-variables.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,29 @@
<body></body>
<script>

const metadata_options = {
randomField: "this is a field",
author: {
"John Cena": {
name: "John Cena",
givenName: "Sammy Salami",
},
"Darnell": {
givenName: "bob",
}
},
variables: {
"trial_type" : {
description: {
"chat-plugin": "this chat plugin allows you to talk to gpt!",
}
}
},
}

var jsPsych = initJsPsych({
on_finish: function() {

metadata.generate(jsPsych.data.get().json());
on_finish: function() {
metadata.generate(jsPsych.data.get().json(), metadata_options);

// metadata.saveAsJsonFile();
jsPsych.data.displayData();
Expand Down
8 changes: 5 additions & 3 deletions packages/metadata/src/AuthorsMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ export class AuthorsMap {
this.authors[fields.name] = fields.name;
return;
}
const new_author: { [key: string]: any } = {}; // Define an empty object to store the variables
new_author["name"] = fields["name"]; // to ensure that name is always first
delete fields["name"];

const new_variable: { [key: string]: any } = {}; // Define an empty object to store the variables
for (const key in fields) {
// Check if the property is defined and not null
if (fields[key] !== undefined && fields[key] !== null) {
new_variable[key] = fields[key];
new_author[key] = fields[key];
}
}

this.authors[new_variable.name] = new_variable;
this.authors[new_author.name] = new_author;
}

/**
Expand Down
42 changes: 35 additions & 7 deletions packages/metadata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,50 @@ export default class JsPsychMetadata {
}

// passing in authors mapping and variables mapping and then goes through each variable
generate(data) {
generate(data, metadata = {}) {
// have it so that can pass in a dict of object that the researcher wants to do
if (typeof data === "string") {
data = JSON.parse(data);
}

// observations can be thought of as rows
for (const observation of data) {
console.log("NEW OBSERVATION ----");
this.generateObservation(observation);
// console.log(this.getMetadata());
}

// iterate through this dict and figure out best way to handle variable updates and formatting the object

for (const key in metadata) {
const value = metadata[key];

if (key === "variables") {
if (typeof value !== "object" || value === null) {
console.error("Author object is not correct type");
continue;
}

// all of the variables must already exist because should have datapoints
for (const variable_key in value) {
const variable = value[variable_key];

for (const parameter in variable) {
const parameter_value = variable[parameter];
this.updateVariable(variable_key, parameter, parameter_value);
}
}
} else if (key === "author") {
if (typeof value !== "object" || value === null) {
console.error("Author object is not correct type");
continue;
}

for (const author_key in value) {
const author = value[author_key];
if (!("name" in author)) author["name"] = author_key;
this.setAuthor(author);
}
} else this.setMetadataField(key, value);
}

return this.getMetadata();
}

Expand All @@ -287,7 +316,6 @@ export default class JsPsychMetadata {
// probably should work in a call to the plugin here
const description = this.getPluginInfo(pluginType);
const type = typeof value;
console.log(type);

// probs should have update description called here
const new_var = {
Expand All @@ -299,8 +327,6 @@ export default class JsPsychMetadata {

this.setVariable(new_var);
this.updateFields(variable, value, type);

console.log(this.getVariable(variable));
}

// hardest part is updating the description
Expand Down Expand Up @@ -330,6 +356,8 @@ export default class JsPsychMetadata {
}
}

private createAuthor() {}

private getPluginInfo(pluginType) {
// fill in with logic on how to call plugin api and unpkg
}
Expand Down

0 comments on commit 1e267e3

Please sign in to comment.