Skip to content

Commit

Permalink
Merge pull request #1 from bjoluc/main
Browse files Browse the repository at this point in the history
Add .prettierrc and update testing examples
  • Loading branch information
jodeleeuw authored Mar 13, 2022
2 parents 6383cd3 + fb47cd6 commit d2ce4c8
Show file tree
Hide file tree
Showing 6 changed files with 438 additions and 312 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"printWidth": 100
}
270 changes: 129 additions & 141 deletions basic-testing/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,163 +10,151 @@
}
</style>
</head>
<body>
</body>
<body></body>
<script>
const jsPsych = initJsPsych({
on_finish: function () {
test();
},
});

const jsPsych = initJsPsych({
on_finish: function(){
test();
}
});

const instructions = {
type: jsPsychHtmlButtonResponse,
stimulus: `
const instructions = {
type: jsPsychHtmlButtonResponse,
stimulus: `
<p>This experiment measures how fast you can identify whether a number is odd or even.</p>
<p>You'll see a number appear on the screen. As quickly as you can:</p>
<p>Press the A key if the number is odd.</p>
<p>Press the L key if the number is even.</p>
`,
choices: ['I understand the instructions']
}

const instruction_check = {
timeline: [
{
type: jsPsychHtmlButtonResponse,
stimulus: ()=>{ return `
<p class="stimulus">${jsPsych.timelineVariable('number')}</p>
`},
choices: ['A', 'L'],
prompt: '<p>To see if you understood the instructions, which key should you press if you see this number?</p>',
data: {
task: 'instructions-check'
choices: ["I understand the instructions"],
};

const instruction_check = {
timeline: [
{
type: jsPsychHtmlButtonResponse,
stimulus: () => {
return `
<p class="stimulus">${jsPsych.timelineVariable("number")}</p>
`;
},
choices: ["A", "L"],
prompt:
"<p>To see if you understood the instructions, which key should you press if you see this number?</p>",
data: {
task: "instructions-check",
},
on_finish: (data) => {
data.correct = data.response === jsPsych.timelineVariable("answer");
},
},
on_finish: (data) => {
data.correct = data.response == jsPsych.timelineVariable('answer');
}
}
],
timeline_variables: [
{number: 3, answer: 0},
{number: 6, answer: 1}
],
randomize_order: true
}

const instructions_check_fail = {
timeline: [
{
type: jsPsychHtmlButtonResponse,
stimulus: `<p>You did not answer the questions correctly.</p>
],
timeline_variables: [
{ number: 3, answer: 0 },
{ number: 6, answer: 1 },
],
randomize_order: true,
};

const instructions_check_fail = {
timeline: [
{
type: jsPsychHtmlButtonResponse,
stimulus: `<p>You did not answer the questions correctly.</p>
<p>We'll show you the instructions again.</p>
`,
choices: ['Got it']
}
],
conditional_function: ()=>{
const ic_data = jsPsych.data.get().filter({task: 'instructions-check'}).last(2);
return !ic_data.select('correct').all((value)=>{return value==true});
}
}
choices: ["Got it"],
},
],
conditional_function: () => {
const ic_data = jsPsych.data.get().filter({ task: "instructions-check" }).last(2);
return !ic_data.select("correct").all(Boolean);
},
};

const instructions_loop = {
timeline: [instructions, instruction_check, instructions_check_fail],
loop_function: (data)=>{
return !data.filter({task: 'instructions-check'}).select('correct').all((value)=>{return value==true});
}
}
const instructions_loop = {
timeline: [instructions, instruction_check, instructions_check_fail],
loop_function: (data) =>
!data.filter({ task: "instructions-check" }).select("correct").all(Boolean),
};

const pre_task_start = {
type: jsPsychHtmlKeyboardResponse,
stimulus: `<p>Great! You are ready to begin the task.</p>
const pre_task_start = {
type: jsPsychHtmlKeyboardResponse,
stimulus: `<p>Great! You are ready to begin the task.</p>
<p>Please place your hands over the A and L keys.</p>
<p>Press either A or L to begin.</p>
`,
choices: ['a','l']
}

const snarc = {
timeline: [
{
type: jsPsychHtmlKeyboardResponse,
stimulus: `<p class="stimulus">+</p>`,
trial_duration: 500,
choices: "NO_KEYS"
},
{
type: jsPsychHtmlKeyboardResponse,
stimulus: ()=>{
return `<p class="stimulus">${jsPsych.timelineVariable('number')}</p>`
choices: ["a", "l"],
};

const snarc = {
timeline: [
{
type: jsPsychHtmlKeyboardResponse,
stimulus: `<p class="stimulus">+</p>`,
trial_duration: 500,
choices: "NO_KEYS",
},
choices: ['a','l'],
data: {
digit: jsPsych.timelineVariable('number'),
parity: jsPsych.timelineVariable('parity'),
magnitude: jsPsych.timelineVariable('magnitude')
}
}
],
timeline_variables: [
{number: 1, parity: 'odd', magnitude: 'small'},
{number: 2, parity: 'even', magnitude: 'small'},
{number: 3, parity: 'odd', magnitude: 'small'},
{number: 4, parity: 'even', magnitude: 'small'},
{number: 6, parity: 'even', magnitude: 'large'},
{number: 7, parity: 'odd', magnitude: 'large'},
{number: 8, parity: 'even', magnitude: 'large'},
{number: 9, parity: 'odd', magnitude: 'large'}
],
sample: {
type: 'fixed-repetitions',
size: 5
}
}

jsPsych.simulate([instructions_loop, pre_task_start, snarc]);

function test(){
const data = jsPsych.data.get();

let test_results = '';

// check that there are an equal number of all trial types
const all_equal = [
data.filter({parity: 'odd', magnitude: 'small'}).count(),
data.filter({parity: 'odd', magnitude: 'large'}).count(),
data.filter({parity: 'even', magnitude: 'small'}).count(),
data.filter({parity: 'even', magnitude: 'large'}).count()
].every((value, i, array)=>{ return value === array[0] });

if(all_equal){
test_results += '<p>✔️ Equal number of all trial types</p>'
} else {
test_results += '<p>❌ Unequal number of all trial types</p>'
}

// check that answering the instructions incorrectly causes a loop
const looped = data.filter({task:'instructions-check'}).count() > 2;
if(!data.filter({task: 'instructions-check'}).first(2).select('correct').all((x)=>{return x==true})){
if(looped){
test_results += '<p>✔️ Instructions looped on check fail</p>'
} else {
test_results += '<p>❌ Instructions did not loop on check fail</p>'
}
} else {
if(!looped){
test_results += '<p>✔️ Instructions did not loop on check pass</p>'
{
type: jsPsychHtmlKeyboardResponse,
stimulus: () => `<p class="stimulus">${jsPsych.timelineVariable("number")}</p>`,
choices: ["a", "l"],
data: {
digit: jsPsych.timelineVariable("number"),
parity: jsPsych.timelineVariable("parity"),
magnitude: jsPsych.timelineVariable("magnitude"),
},
},
],
timeline_variables: [
{ number: 1, parity: "odd", magnitude: "small" },
{ number: 2, parity: "even", magnitude: "small" },
{ number: 3, parity: "odd", magnitude: "small" },
{ number: 4, parity: "even", magnitude: "small" },
{ number: 6, parity: "even", magnitude: "large" },
{ number: 7, parity: "odd", magnitude: "large" },
{ number: 8, parity: "even", magnitude: "large" },
{ number: 9, parity: "odd", magnitude: "large" },
],
sample: {
type: "fixed-repetitions",
size: 5,
},
};

jsPsych.simulate([instructions_loop, pre_task_start, snarc]);

function test() {
const data = jsPsych.data.get();

let test_results = "";

// check that there are an equal number of all trial types
const all_equal = [
{ parity: "odd", magnitude: "small" },
{ parity: "odd", magnitude: "large" },
{ parity: "even", magnitude: "small" },
{ parity: "even", magnitude: "large" },
]
.map((properties) => data.filter(properties).count())
.every((value, i, array) => value === array[0]);

test_results += all_equal
? "<p>✔️ Equal number of all trial types</p>"
: "<p>❌ Unequal number of all trial types</p>";

// check whether answering the instructions incorrectly causes a loop
const looped = data.filter({ task: "instructions-check" }).count() > 2;
if (!data.filter({ task: "instructions-check" }).first(2).select("correct").all(Boolean)) {
test_results += looped
? "<p>✔️ Instructions looped on check fail</p>"
: "<p>❌ Instructions did not loop on check fail</p>";
} else {
test_results += '<p>❌ Instructions looped on check pass</p>'
test_results += looped
? "<p>❌ Instructions looped on check pass</p>"
: "<p>✔️ Instructions did not loop on check pass</p>";
}
}

jsPsych.getDisplayElement().innerHTML = test_results;

}





</script>
jsPsych.getDisplayElement().innerHTML = test_results;
}
</script>
Loading

0 comments on commit d2ce4c8

Please sign in to comment.