Skip to content

Commit cecfd30

Browse files
committed
fixing batch counting and finished frontend
1 parent bf6822a commit cecfd30

File tree

6 files changed

+159
-181
lines changed

6 files changed

+159
-181
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ descriptions/
77
.DS_Store
88
launch.json
99
generate_trials.log
10-
batchCounts*
10+
batchesCounts*
1111
word_to_rate/

Diff for: dev/dev.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ $(document).ready(function(){
1212
// DEFINE workerId, hitId, assignmentId HERE
1313
//////////////////////////////////////////
1414
let subjCode = $("#subjCode").val().slice();
15-
let numTrials = $("#numTrials").val();
1615
let reset = $("#reset").val();
1716
let workerId = 'null';
1817
let assignmentId = 'null';
@@ -27,22 +26,11 @@ $(document).ready(function(){
2726
url: 'http://'+document.domain+':'+PORT+'/trials',
2827
type: 'POST',
2928
contentType: 'application/json',
30-
data: JSON.stringify({subjCode, numTrials, reset, dev: true }),
29+
data: JSON.stringify({subjCode, reset, dev: true }),
3130
success: function (data) {
3231
console.log(data);
33-
34-
let images = [];
35-
let categories = data.trials.categories;
36-
let stimuli = data.trials.images;
37-
38-
for (let category of categories)
39-
for (let file of stimuli[category])
40-
images.push(file);
4132

42-
jsPsych.pluginAPI.preloadImages(images, function(){});
43-
44-
// $("#loading").remove();
45-
runExperiment({categories, images: stimuli, questions: data.trials.questions, debriefing: data.trials.debriefing, categoryNamesMap: data.trials.categoryNamesMap}, subjCode, workerId, assignmentId, hitId, FULLSCREEN, PORT);
33+
runExperiment(data.trials, subjCode, workerId, assignmentId, hitId, FULLSCREEN, PORT);
4634

4735
}
4836
})

Diff for: dev/experiment.js

+115-127
Original file line numberDiff line numberDiff line change
@@ -65,132 +65,127 @@ export function runExperiment(
6565
};
6666

6767
timeline.push(instructions);
68-
let num_trials = trials.categories.reduce(
69-
(a, category) => a + trials.images[category].length,
70-
0
71-
);
72-
document.trials = trials;
68+
const num_trials = trials.length;
7369

7470
let trial_number = 1;
7571
let progress_number = 1;
7672

77-
const { categoryNamesMap } = trials;
7873
// Pushes each audio trial to timeline
79-
trials.categories.forEach(category => {
80-
trials.images[category].forEach(image => {
81-
let response = {
82-
subjCode: subjCode,
83-
participantID: participantID,
84-
category: category,
85-
image: image,
86-
expTimer: -1,
87-
response: -1,
88-
trial_number: trial_number,
89-
rt: -1
90-
};
91-
92-
let stimulus = /*html*/ `
93-
<h4 style="text-align:center;margin-top:0;">Trial ${trial_number} of ${num_trials}</h4>
94-
<h1>How typical is this ${categoryNamesMap[category]}?</div>
95-
<div style="width:100%;">
96-
<div style="width: 100%;;text-align:center;margin: auto;padding: 0em;">
97-
<img src="${image}" alt="${image}" height="200px" align="middle" style="max-width:400px;width=50%;" />
98-
</div>
99-
</div>
100-
`;
101-
102-
const choices = ["1 (Very typical)", "2", "3", "4", "5 (Very atypical)"];
103-
104-
let circles = choices.map(choice => {
105-
return /*html*/ `
106-
<div class="choice">
107-
<div class="choice-circle empty-circle"></div>
108-
<div class="text">${choice}</div>
109-
</div>
110-
`;
111-
});
74+
trials.forEach(trial => {
75+
let response = {
76+
subjCode: subjCode,
77+
word: trial.word,
78+
question_prompt_pre: trial.question_prompt_pre,
79+
question_prompt_post: trial.question_prompt_post,
80+
question_type: trial.question_type,
81+
bin: trial.bin,
82+
choice1: trial.choice1,
83+
choice2: trial.choice2,
84+
choice3: trial.choice3,
85+
choice4: trial.choice4,
86+
choice5: trial.choice5,
87+
expTimer: -1,
88+
response: -1,
89+
trial_number: trial_number,
90+
rt: -1
91+
};
92+
93+
let stimulus = /*html*/ `
94+
<h4 style="text-align:center;margin-top:0;">Trial ${trial_number} of ${num_trials}</h4>
95+
<div style="padding:10%;"><h1>${trial.question_prompt_pre}${trial.word}${trial.question_prompt_post}</h1></div>
96+
`;
97+
98+
const choices = [trial.choice1, trial.choice2, trial.choice3, trial.choice4, trial.choice5];
99+
100+
let circles = choices.map(choice => {
101+
return /*html*/ `
102+
<div class="choice">
103+
<div class="choice-circle empty-circle"></div>
104+
<div class="text">${choice}</div>
105+
</div>
106+
`;
107+
});
112108

113-
let prompt = /*html*/ `
114-
<div class="bar">
115-
${circles.join("")}
116-
</div>
117-
`;
118-
119-
// Picture Trial
120-
let pictureTrial = {
121-
type: "html-keyboard-response",
122-
choices: choices.map((choice, index) => {
123-
return `${index + 1}`;
124-
}),
125-
126-
stimulus: stimulus,
127-
128-
prompt: function() {
129-
return prompt;
130-
},
131-
132-
on_finish: function(data) {
133-
response.response = String.fromCharCode(data.key_press);
134-
response.choice = choices[Number(response.response) - 1];
135-
response.rt = data.rt;
136-
response.expTimer = data.time_elapsed / 1000;
137-
138-
// POST response data to server
139-
$.ajax({
140-
url: "http://" + document.domain + ":" + PORT + "/data",
141-
type: "POST",
142-
contentType: "application/json",
143-
data: JSON.stringify(response),
144-
success: function() {
145-
console.log(response);
146-
}
147-
});
148-
}
149-
};
150-
timeline.push(pictureTrial);
151-
152-
// let subject view their choice
153-
let breakTrial = {
154-
type: "html-keyboard-response",
155-
trial_duration: 500,
156-
response_ends_trial: false,
157-
158-
stimulus: stimulus,
159-
160-
prompt: function() {
161-
const circles = choices.map((choice, index) => {
162-
if (choice == response.choice) {
163-
return /*html*/ `
164-
<div class="choice">
165-
<div class="choice-circle filled-circle"></div>
166-
<div class="text">${choice}</div>
167-
</div>
168-
`;
169-
}
109+
let prompt = /*html*/ `
110+
<div class="bar">
111+
${circles.join("")}
112+
</div>
113+
`;
114+
115+
// Picture Trial
116+
let pictureTrial = {
117+
type: "html-keyboard-response",
118+
choices: choices.map((choice, index) => {
119+
return `${index + 1}`;
120+
}),
121+
122+
stimulus: stimulus,
123+
124+
prompt: function() {
125+
return prompt;
126+
},
127+
128+
on_finish: function(data) {
129+
response.response = String.fromCharCode(data.key_press);
130+
response.choice = choices[Number(response.response) - 1];
131+
response.rt = data.rt;
132+
response.expTimer = data.time_elapsed / 1000;
133+
134+
// POST response data to server
135+
$.ajax({
136+
url: "http://" + document.domain + ":" + PORT + "/data",
137+
type: "POST",
138+
contentType: "application/json",
139+
data: JSON.stringify(response),
140+
success: function() {
141+
console.log(response);
142+
}
143+
});
144+
}
145+
};
146+
timeline.push(pictureTrial);
147+
148+
// let subject view their choice
149+
let breakTrial = {
150+
type: "html-keyboard-response",
151+
trial_duration: 500,
152+
response_ends_trial: false,
153+
154+
stimulus: stimulus,
155+
156+
prompt: function() {
157+
const circles = choices.map((choice, index) => {
158+
if (choice == response.choice) {
170159
return /*html*/ `
171-
<div class="choice">
172-
<div class="choice-circle empty-circle"></div>
173-
<div class="text">${choice}</div>
174-
</div>
175-
`;
176-
});
177-
178-
const prompt = /*html*/ `
179-
<div class="bar">
180-
${circles.join("")}
181-
</div>
182-
`;
183-
return prompt;
184-
},
185-
186-
on_finish: function() {
187-
jsPsych.setProgressBar((progress_number - 1) / num_trials);
188-
progress_number++;
189-
}
190-
};
191-
timeline.push(breakTrial);
192-
trial_number++;
193-
});
160+
<div class="choice">
161+
<div class="choice-circle filled-circle"></div>
162+
<div class="text">${choice}</div>
163+
</div>
164+
`;
165+
}
166+
return /*html*/ `
167+
<div class="choice">
168+
<div class="choice-circle empty-circle"></div>
169+
<div class="text">${choice}</div>
170+
</div>
171+
`;
172+
});
173+
174+
const prompt = /*html*/ `
175+
<div class="bar">
176+
${circles.join("")}
177+
</div>
178+
`;
179+
return prompt;
180+
},
181+
182+
on_finish: function() {
183+
jsPsych.setProgressBar((progress_number - 1) / num_trials);
184+
progress_number++;
185+
}
186+
};
187+
timeline.push(breakTrial);
188+
trial_number++;
194189
});
195190

196191
let questionsInstructions = {
@@ -234,14 +229,7 @@ export function runExperiment(
234229
};
235230
timeline.push(demographicsTrial);
236231

237-
let images = [];
238-
// add scale pic paths to images that need to be loaded
239-
images.push("img/scale.png");
240-
for (let i = 1; i <= 7; i++) images.push("img/scale" + i + ".jpg");
241-
242-
jsPsych.pluginAPI.preloadImages(images, function() {
243-
startExperiment();
244-
});
232+
startExperiment();
245233
document.timeline = timeline;
246234
function startExperiment() {
247235
jsPsych.init({

Diff for: dev/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
<form action="" class="form-horizontal">
3838
Subject Code: <input id="subjCode" type="text" name="FirstName" value="subjCode" autofocus><br>
39-
Number of Categories: <input id="numTrials" type="text" name="trials" value="4"> <br>
4039
<label for="select" class="control-label">Start over (Reset):</label>
4140
<select name="new" id="reset" style="width:20%">
4241
<option value="false">false</option>

Diff for: dev/prod.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ $(document).ready(function(){
1515
// DEFINE workerId, hitId, assignmentId HERE
1616
//////////////////////////////////////////
1717
let subjCode = $.urlParam('workerId') || 'unknown';
18-
let numTrials = $.urlParam("numTrials") || 15;
1918
let reset = $.urlParam("newSet") || 'false';
2019
let workerId = 'workerId';
2120
let assignmentId = 'assignmentId';
@@ -29,22 +28,12 @@ $(document).ready(function(){
2928
url: 'http://'+document.domain+':'+PORT+'/trials',
3029
type: 'POST',
3130
contentType: 'application/json',
32-
data: JSON.stringify({subjCode, numTrials, reset, dev: false }),
31+
data: JSON.stringify({subjCode, reset, dev: false }),
3332
success: function (data) {
3433
console.log(data);
3534

36-
let images = [];
37-
let categories = data.trials.categories;
38-
let stimuli = data.trials.images;
39-
40-
for (let category of categories) {
41-
for (let file of stimuli[category]) {
42-
images.push(file);
43-
}
44-
}
4535
$("#loading").remove();
4636
runExperiment(data.trials, subjCode, workerId, assignmentId, hitId, FULLSCREEN, PORT);
47-
jsPsych.pluginAPI.preloadImages(images, function(){});
4837
}
4938
})
5039

0 commit comments

Comments
 (0)