More work on plugin ambiguity #422

This commit is contained in:
Josh de Leeuw 2017-07-07 12:17:09 -04:00
parent 35827773a2
commit a764ba75a4
11 changed files with 293 additions and 315 deletions

View File

@ -38,6 +38,6 @@ var trial = {
type: 'button-response',
stimulus: 'img/happy_face_1.jpg',
choices: ['Happy', 'Sad'],
prompt: "<p class='center-content'>What emotion is this person showing?</p>"
prompt: "<p>What emotion is this person showing?</p>"
};
```

View File

@ -46,7 +46,7 @@ key_press_stim1 | numeric | Indicates which key the subject pressed to continue.
var block = {
type: 'same-different',
stimuli: ['img/happy_face_1.jpg', 'img/sad_face_3.jpg'],
prompt: "<p class='center-content'>Press S if the faces had the same emotional expression. Press D if the faces had different emotional expressions.</p>",
prompt: "<p>Press S if the faces had the same emotional expression. Press D if the faces had different emotional expressions.</p>",
same_key: 'S',
different_key: 'D',
answer: 'different'
@ -66,7 +66,7 @@ key_press_stim1 | numeric | Indicates which key the subject pressed to continue.
var block = {
type: 'same-different',
stimuli: ['img/happy_face_1.jpg', 'img/happy_face_3.jpg'],
prompt: "<p class='center-content'>Press S if the faces had the same emotional expression. Press D if the faces had different emotional expressions.</p>",
prompt: "<p>Press S if the faces had the same emotional expression. Press D if the faces had different emotional expressions.</p>",
same_key: 'S',
different_key: 'D',
answer: 'same'

View File

@ -20,9 +20,9 @@
key_answer: 68,
choices: [68, 83],
text_answer: 'different',
correct_text: "<p class='center-content'>Correct. The faces had %ANS% expressions.</p>",
incorrect_text: "<p class='center-content'>Incorrect. The faces had %ANS% expressions.</p>",
prompt: "<p class='center-content'>Press D if the faces had different emotional expressions. Press S if the faces had the same emotional expression.</p>",
correct_text: "<p>Correct. The faces had %ANS% expressions.</p>",
incorrect_text: "<p>Incorrect. The faces had %ANS% expressions.</p>",
prompt: "<p>Press D if the faces had different emotional expressions. Press S if the faces had the same emotional expression.</p>",
};
jsPsych.init({

View File

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<script src="../jspsych.js"></script>
<script src="../plugins/jspsych-categorize-html.js"></script>
<link rel="stylesheet" href="../css/jspsych.css"></link>
<style>
img {
width: 300px;
}
</style>
</head>
<body></body>
<script>
var trial = {
type: 'categorize-html',
stimulus: '<div style="margin: auto; width: 200px; height: 200px; background-color: blue;></div>"',
key_answer: 66,
choices: [66, 80],
text_answer: 'happy',
correct_text: "<p>Correct. This color is %ANS%.</p>",
incorrect_text: "<p>Incorrect. This color is %ANS%. Please press the correct key to continue.</p>",
prompt: "<p>Press B if the square is blue. Press P if the square is purple.</p>",
timing_response: 1500,
show_feedback_on_timeout: false,
force_correct_button_press: true
};
jsPsych.init({
timeline: [trial],
on_finish: function() {
jsPsych.data.displayData();
}
});
</script>
</html>

View File

@ -3,7 +3,7 @@
<head>
<script src="../jspsych.js"></script>
<script src="../plugins/jspsych-categorize.js"></script>
<script src="../plugins/jspsych-categorize-image.js"></script>
<link rel="stylesheet" href="../css/jspsych.css"></link>
<style>
img {
@ -14,14 +14,14 @@
<body></body>
<script>
var trials = {
type: 'categorize',
type: 'categorize-image',
stimulus: 'img/happy_face_1.jpg',
key_answer: 72,
choices: [72, 83],
text_answer: 'happy',
correct_text: "<p class='center-content'>Correct. This face is %ANS%.</p>",
incorrect_text: "<p class='center-content'>Incorrect. This face is %ANS%. Please press the correct key to continue.</p>",
prompt: "<p class='center-content'>Press H if the face is happy. Press S if the face is sad.</p>",
correct_text: "<p>Correct. This face is %ANS%.</p>",
incorrect_text: "<p>Incorrect. This face is %ANS%. Please press the correct key to continue.</p>",
prompt: "<p>Press H if the face is happy. Press S if the face is sad.</p>",
timing_response: 1500,
show_feedback_on_timeout: false,
force_correct_button_press: true

View File

@ -16,7 +16,7 @@
var trial = {
type: 'same-different',
stimuli: ['img/happy_face_1.jpg', 'img/sad_face_3.jpg'],
prompt: "<p class='center-content'>Press S if the faces had the same emotional expression. Press D if the faces had different emotional expressions.</p>",
prompt: "<p>Press S if the faces had the same emotional expression. Press D if the faces had different emotional expressions.</p>",
same_key: 'S',
different_key: 'D',
answer: 'different'

View File

@ -17,7 +17,7 @@
var trial = {
type: 'xab',
stimuli: ['img/happy_face_1.jpg', 'img/happy_face_2.jpg'],
prompt: "<p class='center-content'>Press Q if the face you just saw is on the left. Press P if the face you just saw is on the right.</p>",
prompt: "<p>Press Q if the face you just saw is on the left. Press P if the face you just saw is on the right.</p>",
timing_response: 2000
}

View File

@ -0,0 +1,235 @@
/**
* jspsych plugin for categorization trials with feedback
* Josh de Leeuw
*
* documentation: docs.jspsych.org
**/
jsPsych.plugins['categorize-html'] = (function() {
var plugin = {};
plugin.info = {
name: 'categorize-html',
description: '',
parameters: {
stimulus: {
type: [jsPsych.plugins.parameterType.HTML_STRING],
default: undefined,
no_function: false,
description: ''
},
key_answer: {
type: [jsPsych.plugins.parameterType.KEYCODE],
default: undefined,
no_function: false,
description: ''
},
choices: {
type: [jsPsych.plugins.parameterType.KEYCODE],
default: jsPsych.ALL_KEYS,
array: true,
no_function: false,
description: ''
},
text_answer: {
type: [jsPsych.plugins.parameterType.STRING],
default: '',
no_function: false,
description: ''
},
correct_text: {
type: [jsPsych.plugins.parameterType.STRING],
default: 'Correct.',
no_function: false,
description: ''
},
incorrect_text: {
type: [jsPsych.plugins.parameterType.STRING],
default: 'Wrong.',
no_function: false,
description: ''
},
prompt: {
type: [jsPsych.plugins.parameterType.STRING],
default: '',
no_function: false,
description: ''
},
force_correct_button_press: {
type: [jsPsych.plugins.parameterType.BOOL],
default: false,
no_function: false,
description: ''
},
show_stim_with_feedback: {
type: [jsPsych.plugins.parameterType.BOOL],
default: true,
no_function: false,
description: ''
},
show_feedback_on_timeout: {
type: [jsPsych.plugins.parameterType.BOOL],
default: false,
no_function: false,
description: ''
},
timeout_message: {
type: [jsPsych.plugins.parameterType.STRING],
default: 'Please respond faster.',
no_function: false,
description: ''
},
timing_stim: {
type: [jsPsych.plugins.parameterType.INT],
default: -1,
no_function: false,
description: ''
},
timing_response: {
type: [jsPsych.plugins.parameterType.INT],
default: -1,
no_function: false,
description: ''
},
timing_feedback_duration: {
type: [jsPsych.plugins.parameterType.INT],
default: 2000,
no_function: false,
description: ''
}
}
}
plugin.trial = function(display_element, trial) {
// default parameters
trial.choices = trial.choices || jsPsych.ALL_KEYS;
trial.text_answer = (typeof trial.text_answer === 'undefined') ? "" : trial.text_answer;
trial.correct_text = (typeof trial.correct_text === 'undefined') ? "<p class='feedback'>Correct</p>" : trial.correct_text;
trial.incorrect_text = (typeof trial.incorrect_text === 'undefined') ? "<p class='feedback'>Incorrect</p>" : trial.incorrect_text;
trial.show_stim_with_feedback = (typeof trial.show_stim_with_feedback === 'undefined') ? true : trial.show_stim_with_feedback;
trial.force_correct_button_press = (typeof trial.force_correct_button_press === 'undefined') ? false : trial.force_correct_button_press;
trial.prompt = (typeof trial.prompt === 'undefined') ? '' : trial.prompt;
trial.show_feedback_on_timeout = (typeof trial.show_feedback_on_timeout === 'undefined') ? false : trial.show_feedback_on_timeout;
trial.timeout_message = trial.timeout_message || "<p>Please respond faster.</p>";
// timing params
trial.timing_stim = trial.timing_stim || -1; // default is to show image until response
trial.timing_response = trial.timing_response || -1; // default is no max response time
trial.timing_feedback_duration = trial.timing_feedback_duration || 2000;
display_element.innerHTML = '<div id="jspsych-categorize-html-stimulus" class="jspsych-categorize-html-stimulus">'+trial.stimulus+'</div>';
// hide image after time if the timing parameter is set
if (trial.timing_stim > 0) {
jsPsych.pluginAPI.setTimeout(function() {
display_element.querySelector('#jspsych-categorize-html-stimulus').style.visibility = 'hidden';
}, trial.timing_stim);
}
// if prompt is set, show prompt
if (trial.prompt !== "") {
display_element.innerHTML += trial.prompt;
}
var trial_data = {};
// create response function
var after_response = function(info) {
// kill any remaining setTimeout handlers
jsPsych.pluginAPI.clearAllTimeouts();
// clear keyboard listener
jsPsych.pluginAPI.cancelAllKeyboardResponses();
var correct = false;
if (trial.key_answer == info.key) {
correct = true;
}
// save data
trial_data = {
"rt": info.rt,
"correct": correct,
"stimulus": trial.stimulus,
"key_press": info.key
};
display_element.innerHTML = '';
var timeout = info.rt == -1;
doFeedback(correct, timeout);
}
jsPsych.pluginAPI.getKeyboardResponse({
callback_function: after_response,
valid_responses: trial.choices,
rt_method: 'date',
persist: false,
allow_held_key: false
});
if (trial.timing_response > 0) {
jsPsych.pluginAPI.setTimeout(function() {
after_response({
key: -1,
rt: -1
});
}, trial.timing_response);
}
function doFeedback(correct, timeout) {
if (timeout && !trial.show_feedback_on_timeout) {
display_element.innerHTML += trial.timeout_message;
} else {
// show image during feedback if flag is set
if (trial.show_stim_with_feedback) {
display_element.innerHTML = '<div id="jspsych-categorize-html-stimulus" class="jspsych-categorize-html-stimulus">'+trial.stimulus+'</div>';
}
// substitute answer in feedback string.
var atext = "";
if (correct) {
atext = trial.correct_text.replace("%ANS%", trial.text_answer);
} else {
atext = trial.incorrect_text.replace("%ANS%", trial.text_answer);
}
// show the feedback
display_element.innerHTML += atext;
}
// check if force correct button press is set
if (trial.force_correct_button_press && correct === false && ((timeout && trial.show_feedback_on_timeout) || !timeout)) {
var after_forced_response = function(info) {
endTrial();
}
jsPsych.pluginAPI.getKeyboardResponse({
callback_function: after_forced_response,
valid_responses: [trial.key_answer],
rt_method: 'date',
persist: false,
allow_held_key: false
});
} else {
jsPsych.pluginAPI.setTimeout(function() {
endTrial();
}, trial.timing_feedback_duration);
}
}
function endTrial() {
display_element.innerHTML = '';
jsPsych.finishTrial(trial_data);
}
};
return plugin;
})();

View File

@ -6,24 +6,18 @@
**/
jsPsych.plugins.categorize = (function() {
jsPsych.plugins['categorize-image'] = (function() {
var plugin = {};
jsPsych.pluginAPI.registerPreload('categorize', 'stimulus', 'image',function(t){ return !t.is_html || t.is_html == 'undefined'});
jsPsych.pluginAPI.registerPreload('categorize-image', 'stimulus', 'image');
plugin.info = {
name: 'categorize',
name: 'categorize-image',
description: '',
parameters: {
stimulus: {
type: [jsPsych.plugins.parameterType.STRING],
default: undefined,
no_function: false,
description: ''
},
is_html: {
type: [jsPsych.plugins.parameterType.BOOL],
type: [jsPsych.plugins.parameterType.IMAGE],
default: undefined,
no_function: false,
description: ''
@ -118,7 +112,6 @@ jsPsych.plugins.categorize = (function() {
trial.correct_text = (typeof trial.correct_text === 'undefined') ? "<p class='feedback'>Correct</p>" : trial.correct_text;
trial.incorrect_text = (typeof trial.incorrect_text === 'undefined') ? "<p class='feedback'>Incorrect</p>" : trial.incorrect_text;
trial.show_stim_with_feedback = (typeof trial.show_stim_with_feedback === 'undefined') ? true : trial.show_stim_with_feedback;
trial.is_html = (typeof trial.is_html === 'undefined') ? false : trial.is_html;
trial.force_correct_button_press = (typeof trial.force_correct_button_press === 'undefined') ? false : trial.force_correct_button_press;
trial.prompt = (typeof trial.prompt === 'undefined') ? '' : trial.prompt;
trial.show_feedback_on_timeout = (typeof trial.show_feedback_on_timeout === 'undefined') ? false : trial.show_feedback_on_timeout;
@ -128,21 +121,12 @@ jsPsych.plugins.categorize = (function() {
trial.timing_response = trial.timing_response || -1; // default is no max response time
trial.timing_feedback_duration = trial.timing_feedback_duration || 2000;
// if any trial variables are functions
// this evaluates the function and replaces
// it with the output of the function
trial = jsPsych.pluginAPI.evaluateFunctionParameters(trial);
if (!trial.is_html) {
display_element.innerHTML = '<img id="jspsych-categorize-stimulus" class="jspsych-categorize-stimulus" src="'+trial.stimulus+'"></img>';
} else {
display_element.innerHTML = '<div id="jspsych-categorize-stimulus" class="jspsych-categorize-stimulus">'+trial.stimulus+'</div>';
}
display_element.innerHTML = '<img id="jspsych-categorize-image-stimulus" class="jspsych-categorize-image-stimulus" src="'+trial.stimulus+'"></img>';
// hide image after time if the timing parameter is set
if (trial.timing_stim > 0) {
jsPsych.pluginAPI.setTimeout(function() {
display_element.querySelector('#jspsych-categorize-stimulus').style.visibility = 'hidden';
display_element.querySelector('#jspsych-categorize-image-stimulus').style.visibility = 'hidden';
}, trial.timing_stim);
}
@ -205,11 +189,7 @@ jsPsych.plugins.categorize = (function() {
} else {
// show image during feedback if flag is set
if (trial.show_stim_with_feedback) {
if (!trial.is_html) {
display_element.innerHTML = '<img id="jspsych-categorize-stimulus" class="jspsych-categorize-stimulus" src="'+trial.stimulus+'"></img>';
} else {
display_element.innerHTML = '<div id="jspsych-categorize-stimulus" class="jspsych-categorize-stimulus">'+trial.stimulus+'</div>';
}
display_element.innerHTML = '<img id="jspsych-categorize-image-stimulus" class="jspsych-categorize-image-stimulus" src="'+trial.stimulus+'"></img>';
}
// substitute answer in feedback string.

View File

@ -1,194 +0,0 @@
/**
* jspsych-single-audio
* Josh de Leeuw
*
* plugin for playing an audio file and getting a keyboard response
*
* documentation: docs.jspsych.org
*
**/
jsPsych.plugins["single-audio"] = (function() {
var plugin = {};
jsPsych.pluginAPI.registerPreload('single-audio', 'stimulus', 'audio');
plugin.info = {
name: 'single-audio',
description: '',
parameters: {
stimulus: {
type: [jsPsych.plugins.parameterType.STRING],
default: undefined,
no_function: false,
description: ''
},
choices: {
type: [jsPsych.plugins.parameterType.KEYCODE],
array: true,
default: jsPsych.ALL_KEYS,
no_function: false,
description: ''
},
prompt: {
type: [jsPsych.plugins.parameterType.STRING],
default: '',
no_function: false,
description: ''
},
timing_response: {
type: [jsPsych.plugins.parameterType.INT],
default: -1,
no_function: false,
description: ''
},
response_ends_trial: {
type: [jsPsych.plugins.parameterType.BOOL],
default: true,
no_function: false,
description: ''
},
trial_ends_after_audio: {
type: [jsPsych.plugins.parameterType.BOOL],
default: false,
no_function: false,
description: ''
},
}
}
plugin.trial = function(display_element, trial) {
// default parameters
trial.choices = trial.choices || jsPsych.ALL_KEYS;
trial.response_ends_trial = (typeof trial.response_ends_trial === 'undefined') ? true : trial.response_ends_trial;
trial.trial_ends_after_audio = (typeof trial.trial_ends_after_audio === 'undefined') ? false : trial.trial_ends_after_audio;
trial.timing_response = trial.timing_response || -1; // if -1, then wait for response forever
trial.prompt = (typeof trial.prompt === 'undefined') ? "" : trial.prompt;
// if any trial variables are functions
// this evaluates the function and replaces
// it with the output of the function
trial = jsPsych.pluginAPI.evaluateFunctionParameters(trial);
// setup stimulus
var context = jsPsych.pluginAPI.audioContext();
if(context !== null){
var source = context.createBufferSource();
source.buffer = jsPsych.pluginAPI.getAudioBuffer(trial.stimulus);
source.connect(context.destination);
} else {
var audio = jsPsych.pluginAPI.getAudioBuffer(trial.stimulus);
audio.currentTime = 0;
}
// set up end event if trial needs it
if(trial.trial_ends_after_audio){
if(context !== null){
source.onended = function() {
end_trial();
}
} else {
audio.addEventListener('ended', end_trial);
}
}
// show prompt if there is one
if (trial.prompt !== "") {
display_element.innerHTML = trial.prompt;
}
// store response
var response = {
rt: -1,
key: -1
};
// function to end trial when it is time
function end_trial() {
// kill any remaining setTimeout handlers
jsPsych.pluginAPI.clearAllTimeouts();
// stop the audio file if it is playing
// remove end event listeners if they exist
if(context !== null){
source.stop();
source.onended = function() { }
} else {
audio.pause();
audio.removeEventListener('ended', end_trial);
}
// kill keyboard listeners
jsPsych.pluginAPI.cancelAllKeyboardResponses();
// gather the data to store for the trial
var trial_data = {
"rt": context !== null ? response.rt * 1000 : response.rt,
"stimulus": trial.stimulus,
"key_press": response.key
};
// clear the display
display_element.innerHTML = '';
// move on to the next trial
jsPsych.finishTrial(trial_data);
};
// function to handle responses by the subject
var after_response = function(info) {
// only record the first response
if (response.key == -1) {
response = info;
}
if (trial.response_ends_trial) {
end_trial();
}
};
// start audio
if(context !== null){
startTime = context.currentTime + 0.1;
source.start(startTime);
} else {
audio.play();
}
// start the response listener
if(context !== null) {
var keyboardListener = jsPsych.pluginAPI.getKeyboardResponse({
callback_function: after_response,
valid_responses: trial.choices,
rt_method: 'audio',
persist: false,
allow_held_key: false,
audio_context: context,
audio_context_start_time: startTime
});
} else {
var keyboardListener = jsPsych.pluginAPI.getKeyboardResponse({
callback_function: after_response,
valid_responses: trial.choices,
rt_method: 'date',
persist: false,
allow_held_key: false
});
}
// end trial if time limit is set
if (trial.timing_response > 0) {
jsPsych.pluginAPI.setTimeout(function() {
end_trial();
}, trial.timing_response);
}
};
return plugin;
})();

View File

@ -1,81 +0,0 @@
/**
* jspsych-survey-slider
* a jspsych plugin for free response survey questions
*
* Josh de Leeuw
*
* documentation: docs.jspsych.org
*
*/
jsPsych.plugins['survey-slider'] = (function() {
var plugin = {};
plugin.info = {
name: 'survey-slider',
description: '',
parameters: {
}
}
plugin.trial = function(display_element, trial) {
trial.preamble = typeof trial.preamble == 'undefined' ? "" : trial.preamble;
trial.button_label = typeof trial.button_label === 'undefined' ? 'Next' : trial.button_label;
// if any trial variables are functions
// this evaluates the function and replaces
// it with the output of the function
trial = jsPsych.pluginAPI.evaluateFunctionParameters(trial);
// show preamble text
var html = '<div id="jspsych-survey-slider-preamble" class="jspsych-survey-slider-preamble">'+trial.preamble+'</div>';
// add questions
for (var i = 0; i < trial.questions.length; i++) {
html += '<div id="jspsych-survey-slider-"'+i+'" class="jspsych-survey-slider-question" style="margin: 2em 0em;">';
html += '<div class="jspsych-survey-slider-question-text">' + trial.questions[i] + '</div>';
html += '<input type="range" name="#jspsych-survey-slider-response-' + i + '"></input>';
html += '</div>';
}
// add submit button
html += '<button id="jspsych-survey-slider-next" class="jspsych-btn">'+trial.button_label+'</button>';
display_element.innerHTML = html;
display_element.querySelector('#jspsych-survey-slider-next').addEventListener('click', function() {
// measure response time
var endTime = (new Date()).getTime();
var response_time = endTime - startTime;
// create object to hold responses
var question_data = {};
var matches = display_element.querySelectorAll('div.jspsych-survey-slider-question');
for(var index=0; index<matches.length; index++){
var id = "Q" + index;
var val = matches[index].querySelector('input').value;
var obje = {};
obje[id] = val;
Object.assign(question_data, obje);
}
// save data
var trialdata = {
"rt": response_time,
"responses": JSON.stringify(question_data)
};
display_element.innerHTML = '';
// next trial
jsPsych.finishTrial(trialdata);
});
var startTime = (new Date()).getTime();
};
return plugin;
})();