Skip to content

Commit

Permalink
Finished reading paths to directory names and paths to data
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhang03 committed Jul 11, 2024
1 parent f7530bc commit 2d400d4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
1 change: 1 addition & 0 deletions backend/mockdata/Timeline Variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"success":true,"timeout":false,"failed_images":[],"failed_audio":[],"failed_video":[],"trial_type":"preload","trial_index":0,"plugin_version":"1.1.3","time_elapsed":18},{"rt":null,"stimulus":"<p style='text-align:center; font-size:80px;'>+</p>","response":null,"trial_type":"html-keyboard-response","trial_index":1,"plugin_version":"1.1.3","time_elapsed":773},{"rt":10540,"stimulus":"img/happy_face_1.jpg","response":"y","trial_type":"image-keyboard-response","trial_index":2,"plugin_version":"1.1.3","time_elapsed":11576},{"rt":null,"stimulus":"<p style='text-align:center; font-size:80px;'>+</p>","response":null,"trial_type":"html-keyboard-response","trial_index":3,"plugin_version":"1.1.3","time_elapsed":12344},{"rt":117,"stimulus":"img/happy_face_2.jpg","response":"y","trial_type":"image-keyboard-response","trial_index":4,"plugin_version":"1.1.3","time_elapsed":12715},{"rt":null,"stimulus":"<p style='text-align:center; font-size:80px;'>+</p>","response":null,"trial_type":"html-keyboard-response","trial_index":5,"plugin_version":"1.1.3","time_elapsed":13470},{"rt":184,"stimulus":"img/happy_face_3.jpg","response":"y","trial_type":"image-keyboard-response","trial_index":6,"plugin_version":"1.1.3","time_elapsed":13908},{"rt":null,"stimulus":"<p style='text-align:center; font-size:80px;'>+</p>","response":null,"trial_type":"html-keyboard-response","trial_index":7,"plugin_version":"1.1.3","time_elapsed":14662},{"rt":135,"stimulus":"img/happy_face_3.jpg","response":"y","trial_type":"image-keyboard-response","trial_index":8,"plugin_version":"1.1.3","time_elapsed":15051},{"rt":null,"stimulus":"<p style='text-align:center; font-size:80px;'>+</p>","response":null,"trial_type":"html-keyboard-response","trial_index":9,"plugin_version":"1.1.3","time_elapsed":15805},{"rt":134,"stimulus":"img/happy_face_1.jpg","response":"y","trial_type":"image-keyboard-response","trial_index":10,"plugin_version":"1.1.3","time_elapsed":16193},{"rt":null,"stimulus":"<p style='text-align:center; font-size:80px;'>+</p>","response":null,"trial_type":"html-keyboard-response","trial_index":11,"plugin_version":"1.1.3","time_elapsed":16949},{"rt":134,"stimulus":"img/happy_face_2.jpg","response":"y","trial_type":"image-keyboard-response","trial_index":12,"plugin_version":"1.1.3","time_elapsed":17337}]
19 changes: 19 additions & 0 deletions backend/mockoptions/metadata-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"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"
}
}
}
2 changes: 2 additions & 0 deletions examples/metadata-timeline-variables.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
await metadata.generate(jsPsych.data.get().json(), metadata_options);
jsPsych.data.displayData();
metadata.displayMetadata(jsPsych.getDisplayElement());

// jsPsych.data.get().localSave('json','timeline-variables.json');
},
default_iti: 250
});
Expand Down
58 changes: 40 additions & 18 deletions packages/metadata/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,50 @@ import path from "path";
import JsPsychMetadata from "../dist/index.js";

const metadata = new JsPsychMetadata();
const directoryPath = process.cwd() + "/test"; // can make this the path that is given (relative)
console.log(directoryPath);
// console.log("arg 0", process.argv[0]);
// console.log("arg 1", process.argv[1]);
// console.log("arg 2", process.argv[2], "typeof", typeof process.argv[2]);

fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error("Error reading directory:", err);
return;
const relativePath = process.argv[2] || "../../../backend/mockdata/"; // process.argv[2] |
const directoryPath = path.resolve(process.cwd(), relativePath);

var metadata_options;

if (process.argv[3]) {
const metadata_options_path = path.resolve(process.cwd(), process.argv[3]);

try {
const data = fs.readFileSync(metadata_options_path, "utf8"); // synchronous read
console.log("metadata options:", data); // log the raw data
metadata_options = JSON.parse(data); // parse the JSON data
} catch (error) {
console.error("Error reading or parsing metadata options:", error);
}
}

const processData = async () => {
try {
const files = await fs.promises.readdir(directoryPath);

files.forEach((file) => {
const filePath = path.join(directoryPath, file);
for (const file of files) {
const filePath = path.join(directoryPath, file);

// Read each file asynchronously
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
try {
const data = await fs.promises.readFile(filePath, "utf8");

console.log(`Reading file: ${file}:`);

await metadata.generate(data, metadata_options);
} catch (err) {
console.error(`Error reading file ${file}:`, err);
return;
}
console.log(`Contents of ${file}:`);
console.log(data); // Output the contents of the file
});
});
});
}
} catch (err) {
console.error("Error reading directory:", err);
}

await console.log(metadata.getMetadata());
};

// console.log("metadata:", metadata.getMetadata());
// Call the processFiles function
processData();

0 comments on commit 2d400d4

Please sign in to comment.