forked from NBCLab/arithmetic-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
342 lines (302 loc) · 10.2 KB
/
training.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from __future__ import absolute_import, division, print_function
import os.path as op
import sys
import time
from psychopy import core, event, gui, visual
from psychopy.constants import STARTED, STOPPED
def set_word_size(img):
# det from orig height 2row / orig height 1row
const = 1.764505119453925
# desired 1row height
height_1row = 0.225
height_2rows = height_1row * const
width, height = img.size
if height > 1: # det by stim gen procedure
new_height = height_2rows
else:
new_height = height_1row
new_shape = (new_height * (width / height), new_height)
return new_shape
def close_on_esc(win):
"""
Closes window if escape is pressed
"""
if "escape" in event.getKeys():
win.close()
core.quit()
def draw(win, stim, keyList=["1", "2", "3"]):
"""
Draw stimulus for a given duration.
Parameters
----------
win : (visual.Window)
stim : object with `.draw()` method or list of such objects
duration : (numeric)
duration in seconds to display the stimulus
"""
# Use a busy loop instead of sleeping so we can exit early if need be.
start_time = time.time()
response = event.BuilderKeyResponse()
response.tStart = start_time
response.frameNStart = 0
response.status = STARTED
window.callOnFlip(response.clock.reset)
event.clearEvents(eventType="keyboard")
while True:
if isinstance(stim, list):
for s in stim:
s.draw()
else:
stim.draw()
keys = event.getKeys(keyList=keyList)
if keys:
response.keys.extend(keys)
break
close_on_esc(win)
win.flip()
response.status = STOPPED
return response.keys[0]
if __name__ == "__main__":
# Ensure that relative paths start from the same directory as this script
try:
script_dir = op.dirname(op.abspath(__file__)).decode(
sys.getfilesystemencoding()
)
except AttributeError:
script_dir = op.dirname(op.abspath(__file__))
exp_info = {}
dlg = gui.DlgFromDict(exp_info, title="Math task training")
window = visual.Window(
fullscr=True,
size=(800, 600),
monitor="testMonitor",
units="norm",
allowStencil=False,
allowGUI=False,
color="black",
colorSpace="rgb",
blendMode="avg",
useFBO=True,
)
if not dlg.OK:
core.quit() # user pressed cancel
instruction_text_box = visual.TextStim(
win=window,
name="instruction_text_box",
text="""\
You will be shown a series of formulae and individual numbers,
you must determine if the result is less than, equal to, or greater than
the value that follows:
1 - Less Than
2 - Equal to
3 - Greater Than""",
font="Arial",
height=0.1,
pos=(0, 0),
wrapWidth=None,
ori=0,
color="white",
colorSpace="rgb",
opacity=1,
depth=-1.0,
)
term1_image = visual.ImageStim(
win=window,
name="equation_first_term",
image=None,
ori=0,
color=[1, 1, 1],
colorSpace="rgb",
opacity=1,
depth=-1.0,
interpolate=True,
)
op_image = visual.ImageStim(
win=window,
name="equation_operator",
image=None,
ori=0,
pos=(0, 0),
color=[1, 1, 1],
colorSpace="rgb",
opacity=1,
depth=-1.0,
interpolate=True,
)
term2_image = visual.ImageStim(
win=window,
name="equation_second_term",
image=None,
ori=0,
color=[1, 1, 1],
colorSpace="rgb",
opacity=1,
depth=-1.0,
interpolate=True,
)
eq_image = visual.ImageStim(
win=window,
name="equation",
image=None,
ori=0,
pos=(0, 0),
color=[1, 1, 1],
colorSpace="rgb",
opacity=1,
depth=-1.0,
interpolate=True,
)
comparison_image = visual.ImageStim(
win=window,
name="comparison",
image=None,
ori=0,
pos=(0, 0),
color=[1, 1, 1],
colorSpace="rgb",
opacity=1,
depth=-1.0,
interpolate=True,
)
feedback_image = visual.ImageStim(
win=window,
name="feedback",
image=None,
size=None,
ori=0,
pos=(0, 0),
color=[1, 1, 1],
colorSpace="rgb",
opacity=1,
depth=-1.0,
interpolate=True,
)
instruction_text_box.draw()
window.flip()
event.waitKeys(keyList=["space"])
instruction_text_box.setText(
"Each trial consists of three stages: an equation, a comparison value, and feedback."
)
instruction_text_box.draw()
window.flip()
event.waitKeys(keyList=["space"])
instruction_text_box.setText(
"Your job is to solve the equation, and then compare the result to the value in the "
"next stage."
)
instruction_text_box.draw()
window.flip()
event.waitKeys(keyList=["space"])
instruction_text_box.setText(
"""If the solution is less than the comparison value, press 1 (index).
If it is equal to the comparison value, press 2 (middle).
If it is greater than the comparison value, press 3 (ring)"""
)
instruction_text_box.draw()
window.flip()
event.waitKeys(keyList=["space"])
instruction_text_box.setText(
"Try to answer as quickly as you can, even if the number has gone away."
)
instruction_text_box.draw()
window.flip()
event.waitKeys(keyList=["space"])
instruction_text_box.setText("Let's try practicing.")
instruction_text_box.draw()
window.flip()
event.waitKeys(keyList=["space"])
# First example- equation with numbers
term1_image.setImage(op.join(script_dir, "stimuli/numerals/10_n.png"))
term2_image.setImage(op.join(script_dir, "stimuli/numerals/01_n.png"))
op_image.setImage(op.join(script_dir, "stimuli/numerals/add_n.png"))
comparison_image.setImage(op.join(script_dir, "stimuli/numerals/10_n.png"))
feedback_image.setImage(op.join(script_dir, "stimuli/feedback/positive.png"))
term1_image.setSize(set_word_size(term1_image))
term2_image.setSize(set_word_size(term2_image))
op_image.setSize(set_word_size(op_image))
comparison_image.setSize(set_word_size(comparison_image))
width, height = feedback_image.size
new_height = 0.6
new_shape = (new_height * (width / height), new_height)
feedback_image.setSize(new_shape)
term1_pos = (term1_image.size[0] / 2.0) + (op_image.size[0] / 2.0)
term2_pos = -1 * ((term2_image.size[0] / 2.0) + (op_image.size[0] / 2.0))
term1_image.pos = (term1_pos, 0.0)
term2_image.pos = (term2_pos, 0.0)
draw(win=window, stim=[term1_image, op_image, term2_image], keyList=["space"])
draw(win=window, stim=comparison_image, keyList=["3"])
draw(win=window, stim=feedback_image, keyList=["space"])
# Unset stim sizes so they don't pass on to the next trial
term1_image.size = None
op_image.size = None
term2_image.size = None
eq_image.size = None
comparison_image.size = None
# Instructions
instruction_text_box.setText(
"""\
Great job!
Now let's try an equation with words."""
)
draw(win=window, stim=instruction_text_box, keyList=["space"])
# Next example- equation with words
term1_image.setImage(op.join(script_dir, "stimuli/numerals/05_w.png"))
term2_image.setImage(op.join(script_dir, "stimuli/numerals/07_w.png"))
op_image.setImage(op.join(script_dir, "stimuli/numerals/subtract_w.png"))
comparison_image.setImage(op.join(script_dir, "stimuli/numerals/-2_w.png"))
feedback_image.setImage(op.join(script_dir, "stimuli/feedback/positive.png"))
term1_image.setSize(set_word_size(term1_image))
term2_image.setSize(set_word_size(term2_image))
op_image.setSize(set_word_size(op_image))
term1_pos = (term1_image.size[1] / 2.0) + (op_image.size[1] / 2.0)
term2_pos = -1 * ((term2_image.size[1] / 2.0) + (op_image.size[1] / 2.0))
term1_image.pos = (0.0, term1_pos)
term2_image.pos = (0.0, term2_pos)
comparison_image.setSize(set_word_size(comparison_image))
width, height = feedback_image.size
new_height = 0.6
new_shape = (new_height * (width / height), new_height)
feedback_image.setSize(new_shape)
draw(win=window, stim=[term1_image, op_image, term2_image], keyList=["space"])
draw(win=window, stim=comparison_image, keyList=["2"])
draw(win=window, stim=feedback_image, keyList=["space"])
# Unset stim sizes so they don't pass on to the next trial
term1_image.size = None
op_image.size = None
term2_image.size = None
eq_image.size = None
comparison_image.size = None
# Instructions
instruction_text_box.setText(
"""\
Great job!
I think you're ready. Just one last thing to remember."""
)
draw(win=window, stim=instruction_text_box, keyList=["space"])
# Let's talk about feedback
instruction_text_box.setText(
"""\
The feedback image indicates if you got the answer right or wrong.
But sometimes the feedback will be "uninformative" (i.e., neutral)."""
)
draw(win=window, stim=instruction_text_box, keyList=["space"])
feedback_image.setImage(op.join(script_dir, "stimuli/feedback/positive.png"))
width, height = feedback_image.size
new_height = 0.6
new_shape = (new_height * (width / height), new_height)
feedback_image.setSize(new_shape)
draw(win=window, stim=feedback_image, keyList=["space"])
feedback_image.setImage(op.join(script_dir, "stimuli/feedback/negative.png"))
width, height = feedback_image.size
new_height = 0.6
new_shape = (new_height * (width / height), new_height)
feedback_image.setSize(new_shape)
draw(win=window, stim=feedback_image, keyList=["space"])
feedback_image.setImage(op.join(script_dir, "stimuli/feedback/noninformative.png"))
width, height = feedback_image.size
new_height = 0.6
new_shape = (new_height * (width / height), new_height)
feedback_image.setSize(new_shape)
draw(win=window, stim=feedback_image, keyList=["space"])
instruction_text_box.setText("And now we're done.")
draw(win=window, stim=instruction_text_box, keyList=["space"])