Last active
March 17, 2022 22:29
-
-
Save migbash/1e38c05bd7631f702c36a34e09672089 to your computer and use it in GitHub Desktop.
Honors Project Data Parsing and Analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... import necessary modules; | |
const fs = require('fs') | |
// import { data } from './participants' | |
// console.log('data', data) | |
// ... ############################## | |
// ... [INITIALIZE]; | |
// ... ############################## | |
// ... ℹ select TARGET TEST; | |
const testSel = 'test_3'; // ... 'test_1' | 'test_2' | 'test_3' | |
const timerSel = 10; // ... deemed acceptable reading-time spent on the website; | |
const messagesExSel = 2; // ... deemed acceptable num. of messages exchanged between the AI and the participant; | |
const quizCorrectAns = [ | |
// ... TEST-1 | |
'Titan is larger than the planet Mercury and is the second largest moon in our solar system.', | |
'15.8 days', | |
'45 percent greater than on Earth', | |
'-290 degrees Fahrenheit or -179 degrees Celsius', | |
'nitrogen (about 95 percent) and methane (about 5 percent)', | |
// ... + TEST 2 & 3 | |
'Yes', | |
'15 days', | |
'50 percent higher than Earth’s', | |
'nitrogen', | |
] | |
// ... read in the DATA from the participants; | |
fs.readFile('./participants.json', 'utf8' , (err, data) => { | |
if (err) { | |
console.error(err) | |
return | |
} | |
// ... convert to JSON format; | |
data = JSON.parse(data); | |
// ... analyze the data; | |
analyzeData(data); | |
}) | |
// ################################## | |
// ... ℹ MAIN - data-analysis-METHOD; | |
// ################################## | |
async function analyzeData(data) { | |
// ... ℹ declare global run variables; | |
let participantCount = 0; | |
let participantArrObs = []; | |
let questionnaireAggr = []; | |
let quizAggr = []; | |
let timerAggr = []; | |
let messagesExTotal = 0; | |
let responseAvgTime = 0; | |
let correctResponseCounter = 0; | |
// ... ℹ iterate over each participant; | |
// data.forEach(async participant => { | |
for await (const participant of data) { | |
// ... ℹ identify the necessary data for each INDIVIDUAL PARTICIPANT; | |
const questionnaire = participant.test_data[testSel].questionnaire; | |
const quiz = participant.test_data[testSel].quiz; | |
const timerData = participant.test_data[testSel].timer_data; | |
const modelTimer = participant.test_data[testSel].model_view_timer; | |
const textTimer = participant.test_data[testSel].text_bot_timer; | |
const conversation_h = participant.test_data[testSel].conversation_history; | |
// ... ℹ individual variables; | |
let individualResponseTime = 0; | |
let individualResponseTimeCount = 0; | |
// ... ℹ appropiate-completion-project-validation: | |
if (quiz != undefined && | |
questionnaire != undefined && | |
timerData.reading > timerSel) { | |
// ... extra conversational-agent (2 & 3) validation: | |
if ((testSel === 'test_2' || testSel === 'test_3') && | |
(conversation_h.total_ex_messages < messagesExSel)) { | |
// ... DEBUGGING; | |
// console.log('Message exchange threshold reached!', conversation_h.total_ex_messages); | |
// ... | |
continue; | |
} | |
const quiz_i2 = JSON.parse(JSON.stringify(quiz)); | |
// ... increment-participant-for-THIS-test; | |
participantCount++; | |
// ... ####################### | |
// ... ℹ AVERAGE VALUES; | |
// ... ####################### | |
// ... ℹ QUESTIONNAIRE; | |
questionnaireAggr.push(questionnaire); | |
// ... ℹ QUIZ; | |
quizAggr.push(quiz) | |
// ... ℹ TIMER-DATA; | |
timerData['modelTimerHalf'] = modelTimer | |
timerData['textTimerHalf'] = textTimer | |
timerAggr.push(timerData); | |
// ... ℹ CONVERSATIONAL BOT DATA; | |
if (conversation_h != undefined && | |
conversation_h != null && | |
conversation_h != NaN && | |
conversation_h) { | |
// ... | |
messagesExTotal = messagesExTotal + conversation_h.total_ex_messages; | |
// ... response-time-avg-aggr; | |
conversation_h.history.forEach(res => { | |
// ... further-validation; | |
if (res.response_time) { | |
// ... DEBUGGING; | |
// console.log(res.response_time) | |
// ... | |
responseAvgTime = responseAvgTime + res.response_time | |
correctResponseCounter++; | |
} | |
}); | |
} | |
// ... ####################### | |
// ... ℹ INDIVIDUAL STREAMLINE; | |
// ... ####################### | |
// ... ℹ individual-particiapnt-observations; | |
let averageQuizScore = await quizAnswersCount_v2([quiz_i2]); | |
averageQuizScore = participantQuizScore(averageQuizScore[0]) | |
// ... | |
let participantStreamLineObj = { | |
participant_num: participantCount, | |
scoreTestAvg: averageQuizScore, | |
learning_time: timerData.reading, | |
modelFocus_time: timerData.modelTimerHalf, | |
textFocus_time: timerData.textTimerHalf, | |
q: questionnaire | |
} | |
if (conversation_h) { | |
// ... | |
participantStreamLineObj.messagesEx = conversation_h.total_ex_messages | |
// ... | |
for (const res of conversation_h.history) { | |
// ... further-validation; | |
if (res.response_time) { | |
// ... | |
individualResponseTime = individualResponseTime + res.response_time; | |
individualResponseTimeCount++; | |
} | |
} | |
// ... | |
participantStreamLineObj.responseAvgTime = (individualResponseTime / individualResponseTimeCount) || 0; | |
} | |
// ... | |
participantArrObs.push(participantStreamLineObj) | |
} | |
}; | |
// ... ℹ final-data-output-data; | |
console.log('\n' + testSel + '\n'); | |
console.log('participants total: \t' + (participantCount)); | |
console.log('parameters -> \t timerSel: ' + (timerSel) + ' messagesExSel: ' + (messagesExSel) + '\n'); | |
// ... | |
if (testSel === 'test_2' || testSel === 'test_3') { | |
console.log('responseAvgTime: \t' + (responseAvgTime / correctResponseCounter)); | |
console.log('messagesExAvg: \t\t' + (messagesExTotal / participantCount) + '\n'); | |
} | |
// ... DEBUGGING; | |
// participantArrObs.forEach(elem => { | |
// console.log('q', elem.q) | |
// }); | |
// console.log('quizAggr', quizAggr); | |
// ... write data to JSON file; | |
fs.writeFile("./participants_" + testSel + ".json", JSON.stringify(participantArrObs), 'utf8', (err) => { | |
if (err) { console.error(err); return; }; | |
console.log("✅ file has been created!"); | |
}); | |
// ... ℹ INVESTIGATING GLOBAL AVERAGE DATA; | |
console.log('ℹ quiz average data:', calcAverage(combineArray(quizAnswersCount(quizAggr)), participantCount)); | |
console.log('ℹ questionnaire average data:', calcAverage(combineArray(questionnaireAggr), participantCount)); | |
console.log('ℹ timer average data:', calcAverage(combineArray(timerAggr), participantCount)); | |
console.log('\n'); | |
} | |
// ... ############################## | |
// ... AVERAGING OUT RESULTS METHODS; | |
// ... ############################## | |
// ... ℹ combining-JSON-parameters-into-one-AVERAGE; | |
function combineArray(array) { | |
// ... https://stackoverflow.com/questions/53517874/how-to-sum-two-or-more-json-object-into-one | |
// ... | |
return array.reduce((a, obj) => { | |
// ... | |
Object.entries(obj).forEach(([key, val]) => { | |
// ... DEBUGGING; | |
// console.log(a[key], key, val) | |
// ... | |
a[key] = (parseInt(a[key].toString()) || 0) + parseInt(val.toString()); | |
}); | |
return a; | |
}); | |
} | |
// ... ℹ average-out-result-of-object-DATA; | |
function calcAverage(obj, num) { | |
// ... | |
let newObj = {} | |
// ... | |
Object.keys(obj).forEach(key => { | |
// ... | |
newObj[key.toString()] = (parseInt(obj[key]) / num) | |
}); | |
// ... | |
return newObj; | |
} | |
// ... ℹ convert quiz answers to binary (1 / 0); | |
function quizAnswersCount(array) { | |
// ... DEBUGGING; | |
// console.log('incoming-array', array) | |
// ... https://simplernerd.com/js-iterate-json/ | |
// ... iterate over each answer; | |
return array.map(obj => { | |
// ... strip-down-keys-and-values; | |
Object.keys(obj).forEach(key => { | |
// ... isolate the value (answer); | |
let answer = obj[key].toString() | |
// ... and identify if the answer is correct; | |
// ... by checking if it matches array of answers; | |
if (quizCorrectAns.some(v => answer.includes(v))) { | |
// ... correct = 1; | |
obj[key] = 1 | |
} else { | |
// ... wrong = 0; | |
obj[key] = 0 | |
} | |
}); | |
// ... DEBUGGING; | |
// console.log('obj', obj) | |
// ... | |
return obj; | |
}); | |
} | |
// ... ℹ convert quiz answers to binary (1 / 0); | |
async function quizAnswersCount_v2(array) { | |
// ... | |
let finalArry = [] | |
// ... iterate over each answer; | |
for await (const obj of array) { | |
// ... strip-down-keys-and-values; | |
Object.keys(obj).forEach(key => { | |
// ... isolate the value (answer); | |
let answer = obj[key].toString() | |
// ... and identify if the answer is correct; | |
// ... by checking if it matches array of answers; | |
if (quizCorrectAns.some(v => answer.includes(v))) { | |
// ... correct = 1; | |
obj[key] = 1 | |
} else { | |
// ... wrong = 0; | |
obj[key] = 0 | |
} | |
}); | |
// ... DEBUGGING; | |
// console.log('obj', obj) | |
finalArry.push(obj) | |
// ... | |
// return obj; | |
}; | |
// ... | |
return finalArry | |
} | |
// ... ℹ obtain participants average score for test; | |
function participantQuizScore(obj) { | |
// ... | |
let quizScore = 0; | |
let counter = 0; | |
// ... | |
Object.keys(obj).forEach(key => { | |
// ... and identify if the answer is correct; | |
let answer = parseInt(obj[key].toString()) | |
quizScore = quizScore + answer; | |
counter++; | |
}); | |
// ... | |
return (quizScore / counter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment