-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscaffold.py
401 lines (360 loc) · 17.6 KB
/
scaffold.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# coding:utf-8
import pyglet.window.key as key_
from expy import shared
from .colors import *
from .stim.draw import *
from .stim.sound import *
from .stim.display import *
from .io import *
from .response import *
np = shared.np
def timing(name):
'''
Get a value of timing parameter:
If the setting gave a int, return it;
If the setting gave a range, return a random int in that range.
Parameters
----------
name: str
The name of timing parameter.
Return
---------
value: int
'''
val = shared.setting['timing'][name]
if type(val) == float:
return val
else:
return np.random.randint(int(val[0]*1000), int(val[1]*1000))/1000
def button(text='Click', w=0.1, h=0.1, x=0.0, y=0.0, color=C_maroon, font_size='stim_font_size', font_color=C_white):
'''
Draw a clicked rectangle button with text.
Parameters
----------
text: str
The text on the button.
w: float or int (default: 0.1)
The width of rectangle.
If w is float, it represents the width scale on screen,
if int, it represents the width in pixel.
h: float or int (default: 0.1)
The height of rectangle.
Similar with x.
x: int, or float (default: 0.0)
The x coordinate of text. If x is int, the coordinate would be pixel number to the left margin of screen; If x is float (-1~1), the coordinate would be percentage of half screen to the screen center.
y: int, or float (default: 0.0)
The y coordinate of text. If y is int, the coordinate would be pixel number to the upper margin of screen; If y is float (-1~1), the coordinate would be percentage of half screen to the screen center.
color: RGB tuple, or pre-defined variable (default:'C_maroon')
The font color of text, you can either use an RGB value or a pre-defined color name.
The pre-defined colors include C_black, C_white, C_red, C_lime, C_blue, C_yellow, C_aqua, C_fuchsia, C_silver, C_gray, C_maroon, C_olive, C_green, C_purple, C_teal, C_navy.
font_size: int, or str (default: 'stim_font_size')
The font size of text, you can either use a number or a pre-defined number name.
font_color: RGB tuple, or pre-defined variable (default:'C_white')
The font color of text, you can either use an RGB value or a pre-defined color name.
The pre-defined colors include C_black, C_white, C_red, C_lime, C_blue, C_yellow, C_aqua, C_fuchsia, C_silver, C_gray, C_maroon, C_olive, C_green, C_purple, C_teal, C_navy.
Return
---------
x: int
X value of clicked position
y: int
Y value of clicked position
rt: int
The second count since the function starts.
button: int
The id of clicked mouse button
'''
drawRect(w, h, x, y, color=color, show_now=False) # Draw a button
# Draw text on the canvas and display it
drawText(text, size=font_size, color=font_color)
(button, (x, y)), rt = waitForResponse(allowed_clicks=ClickRange(
(x-w/2, x+w/2), (y-h/2, y+h/2), shared.mouse_.LEFT)) # Waiting for mouse click and get the click
return x, y, rt, button
def textSlide(text, font=shared.default_font, size='normal_font_size', color=C_white, rotation=0, x=0.0, y=0.0, anchor_x='center', anchor_y='center', background_image=None):
'''
Display a new text slide right now.
Parameters
----------
text: str
The text on the screen.
font: str (default:'shared.default_font')
The fontname of the text.
size:int, or str (default: 'normal_font_size')
The font size of text, you can either use a number or a pre-defined number name.
color: RGB tuple, or pre-defined variable (default:'C_white')
The font color of text, you can either use an RGB value or a pre-defined color name.
The pre-defined colors include C_black, C_white, C_red, C_lime, C_blue, C_yellow, C_aqua, C_fuchsia, C_silver, C_gray, C_maroon, C_olive, C_green, C_purple, C_teal, C_navy.
rotation: int (default: 0)
The rotation angle of text.
x: int, or float (default: 0.0)
The x coordinate of text. If x is int, the coordinate would be pixel number to the left margin of screen; If x is float (-1~1), the coordinate would be percentage of half screen to the screen center.
y: int, or float (default: 0.0)
The y coordinate of text. If y is int, the coordinate would be pixel number to the upper margin of screen; If y is float (-1~1), the coordinate would be percentage of half screen to the screen center.
anchor_x: str (default: 'center')
The position benchmark on this object to the given x.
Options: 'center', 'left', or 'right'.
anchor_y: str (default: 'center')
The position benchmark on this object to the given y.
Options: 'center', 'top', or 'bottom'.
background_image: str, or None(default)
The path of background picture.
Return
---------
None
'''
clear()
if background_image:
drawPic(background_image, show_now=False)
drawText(text, font, size, color, rotation, x, y, anchor_x, anchor_y)
def getInput(pre_text, out_time=0, font=shared.default_font, size='normal_font_size', color=C_white, rotation=0, x=0.0, y=0.0, anchor_x='center', anchor_y='center', background_image=None):
'''
Get user input until "ENTER" pressed, then give it to a variable
Parameters
----------
pre_text: str
The text that will be displayed before user's input.
out_time: num(>0) or 0(default)
The time limitation of this function.
font: str (default:'shared.default_font')
The fontname of the text.
size:int, or str (default: 'normal_font_size')
The font size of text, you can either use a number or a pre-defined number name.
color: RGB tuple, or pre-defined variable (default:'C_white')
The font color of text, you can either use an RGB value or a pre-defined color name.
The pre-defined colors include C_black, C_white, C_red, C_lime, C_blue, C_yellow, C_aqua, C_fuchsia, C_silver, C_gray, C_maroon, C_olive, C_green, C_purple, C_teal, C_navy.
rotation: int (default: 0)
The rotation angle of text.
x: int, or float (default: 0.0)
The x coordinate of text. If x is int, the coordinate would be pixel number to the left margin of screen; If x is float (-1~1), the coordinate would be percentage of half screen to the screen center.
y: int, or float (default: 0.0)
The y coordinate of text. If y is int, the coordinate would be pixel number to the upper margin of screen; If y is float (-1~1), the coordinate would be percentage of half screen to the screen center.
anchor_x: str (default: 'center')
The position benchmark on this object to the given x.
Options: 'center', 'left', or 'right'.
anchor_y: str (default: 'center')
The position benchmark on this object to the given y.
Options: 'center', 'top', or 'bottom'.
background_image: str, or None(default)
The path of background picture.
Return
---------
input_text: str
The content of user's input.
'''
textSlide(pre_text, font, size, color, rotation, x, y, anchor_x, anchor_y, background_image)
text = pre_text
if not shared.start_tp:
shared.start_tp = shared.time.time()
while 1:
inkey = waitForResponse(has_RT=False, out_time=out_time)
if inkey in [key_.RETURN, 'None']:
break
elif inkey == key_.BACKSPACE:
text = text[0:-1]
elif inkey <= 127:
text += (chr(inkey))
elif 65456 <= inkey <= 65465:
text += (chr(inkey-65408))
textSlide(text, font, size, color, rotation, x, y, anchor_x, anchor_y, background_image)
input_text = text[len(pre_text):]
clear()
return input_text
def getSubjectID(pre_text='Please enter the subject ID:'):
'''
Get subject's ID.
Parameters
----------
pre_text: str
The text that will be displayed before user's input.
'''
shared.subject = getInput(pre_text)
def instruction(instruction_text=None, has_practice=False):
'''
Show the instruction of experiment
(press 'left' to back, 'right' to continue)
Parameters
----------
instruction_text: None (default), list of str
The text that will be displayed as instruction. If None, the instruction text in the setting file will be loaded.
Return
---------
resp: Keyname/int
The last pressed key name.
'''
if instruction_text is None:
instruction_text = shared.setting['instruction']
intro = '\n'.join(instruction_text).split('>\n')
i = 0
while True:
if intro[i] == '[demo]':
demo()
i += 1
continue
if i == 0:
textSlide(
intro[i] + '\n\n\n(按“→”进入下一页)\n\n(Press "→" to the next page)')
elif i == len(intro) - 1:
textSlide(intro[
i] + '\n\n\n(按“←”返回上一页,按 [空格] 开始实验. )\n\n(Press "←" to the previous page, or Press "SPACE" to start the experiment)')
else:
textSlide(intro[
i] + '\n\n\n(按“←”返回上一页,按“→”进入下一页)\n\n(Press "←" to the previous page, or Press "→" to the next page)')
resp = waitForResponse(has_RT=False)
if resp == key_.LEFT and i > 0:
i -= 1
elif resp == key_.RIGHT and i < len(intro) - 1:
i += 1
elif resp in [key_.SPACE, key_.RETURN] and i == len(intro) - 1:
clear()
return resp
def alert(text, out_time=0, allowed_keys=[key_.RETURN], font=shared.default_font, size='normal_font_size', color=C_white, rotation=0, x=0.0, y=0.0, anchor_x='center', anchor_y='center', background_image=None):
'''
Display a new text slide right now, and keep the screen until user's response.
Parameters
----------
text: str
The text on the screen.
allowed_keys: Keyname, or list of Keyname (default:[key_.RETURN])
The allowed user's response.
out_time: num(>0) or 0(default)
The display time limitation of this function.
font: str (default:'shared.default_font')
The fontname of the text.
size:int, or str (default: 'normal_font_size')
The font size of text, you can either use a number or a pre-defined number name.
color: RGB tuple, or pre-defined variable (default:'C_white')
The font color of text, you can either use an RGB value or a pre-defined color name.
The pre-defined colors include C_black, C_white, C_red, C_lime, C_blue, C_yellow, C_aqua, C_fuchsia, C_silver, C_gray, C_maroon, C_olive, C_green, C_purple, C_teal, C_navy.
rotation: int (default: 0)
The rotation angle of text.
x: int, or float (default: 0.0)
The x coordinate of text. If x is int, the coordinate would be pixel number to the left margin of screen; If x is float (-1~1), the coordinate would be percentage of half screen to the screen center.
y: int, or float (default: 0.0)
The y coordinate of text. If y is int, the coordinate would be pixel number to the upper margin of screen; If y is float (-1~1), the coordinate would be percentage of half screen to the screen center.
anchor_x: str (default: 'center')
The position benchmark on this object to the given x.
Options: 'center', 'left', or 'right'.
anchor_y: str (default: 'center')
The position benchmark on this object to the given y.
Options: 'center', 'top', or 'bottom'.
background_image: str, or None(default)
The path of background picture.
Return
---------
resp: Keyname/int
The last pressed key name.
'''
textSlide(text, font, size, color, rotation, x, y, anchor_x, anchor_y, background_image)
resp = waitForResponse(allowed_keys, out_time, has_RT=False)
clear()
return resp
def alertAndGo(text, out_time=3, allowed_keys=[key_.RETURN], font=shared.default_font, size='normal_font_size', color=C_white, rotation=0, x=0.0, y=0.0, anchor_x='center', anchor_y='center', background_image=None):
'''
Display a new text slide right now,
and keep the screen in a given period of time, or until user pressed SPACE or key_.RETURN
Parameters
----------
text: str
The text on the screen.
allowed_keys: Keyname, or list of Keyname (default:[key_.RETURN])
The allowed user's response.
out_time: out_time: num(>0) (default: 3)
The display time limitation of this function.
font: str (default:'shared.default_font')
The fontname of the text.
size:int, or str (default: 'normal_font_size')
The font size of text, you can either use a number or a pre-defined number name.
color: RGB tuple, or pre-defined variable (default:'C_white')
The font color of text, you can either use an RGB value or a pre-defined color name.
The pre-defined colors include C_black, C_white, C_red, C_lime, C_blue, C_yellow, C_aqua, C_fuchsia, C_silver, C_gray, C_maroon, C_olive, C_green, C_purple, C_teal, C_navy.
rotation: int (default: 0)
The rotation angle of text.
x: int, or float (default: 0.0)
The x coordinate of text. If x is int, the coordinate would be pixel number to the left margin of screen; If x is float (-1~1), the coordinate would be percentage of half screen to the screen center.
y: int, or float (default: 0.0)
The y coordinate of text. If y is int, the coordinate would be pixel number to the upper margin of screen; If y is float (-1~1), the coordinate would be percentage of half screen to the screen center.
anchor_x: str (default: 'center')
The position benchmark on this object to the given x.
Options: 'center', 'left', or 'right'.
anchor_y: str (default: 'center')
The position benchmark on this object to the given y.
Options: 'center', 'top', or 'bottom'.
background_image: str, or None(default)
The path of background picture.
Return
---------
None
'''
alert(text, out_time, allowed_keys, font, size, color, rotation, x, y, anchor_x, anchor_y, background_image)
def alertAndQuit(text, out_time=3, allowed_keys=[key_.RETURN], font=shared.default_font, size='normal_font_size', color=C_white, rotation=0, x=0.0, y=0.0, anchor_x='center', anchor_y='center', background_image=None):
'''
Display a new text slide right now,
and keep the screen in a given period of time, or until user pressed SPACE or key_.RETURN,
then quit the program.
Parameters
----------
text: str
The text on the screen.
allowed_keys: Keyname, or list of Keyname (default:[key_.RETURN])
The allowed user's response.
out_time: out_time: num(>0) (default: 3)
The display time limitation of this function.
font: str (default:'shared.default_font')
The fontname of the text.
size:int, or str (default: 'normal_font_size')
The font size of text, you can either use a number or a pre-defined number name.
color: RGB tuple, or pre-defined variable (default:'C_white')
The font color of text, you can either use an RGB value or a pre-defined color name.
The pre-defined colors include C_black, C_white, C_red, C_lime, C_blue, C_yellow, C_aqua, C_fuchsia, C_silver, C_gray, C_maroon, C_olive, C_green, C_purple, C_teal, C_navy.
rotation: int (default: 0)
The rotation angle of text.
x: int, or float (default: 0.0)
The x coordinate of text. If x is int, the coordinate would be pixel number to the left margin of screen; If x is float (-1~1), the coordinate would be percentage of half screen to the screen center.
y: int, or float (default: 0.0)
The y coordinate of text. If y is int, the coordinate would be pixel number to the upper margin of screen; If y is float (-1~1), the coordinate would be percentage of half screen to the screen center.
anchor_x: str (default: 'center')
The position benchmark on this object to the given x.
Options: 'center', 'left', or 'right'.
anchor_y: str (default: 'center')
The position benchmark on this object to the given y.
Options: 'center', 'top', or 'bottom'.
background_image: str, or None(default)
The path of background picture.
Return
---------
None
'''
alert(text, out_time, allowed_keys, font, size, color, rotation, x, y, anchor_x, anchor_y, background_image)
shared.pa.terminate()
shared.pyglet.app.exit()
shared.win.close()
rest_text = '实验暂停,您可以休息一会\n\
Now you can have a rest.\n\
如果休息结束请按 [空格] 继续实验。\n\
Please press [SPACE] key when you want to continue.\n'
def restTime(text=rest_text, background_image=None, background_music=None):
'''
Suspend the experiment and ask participant to rest:
1. Display a blank screen in 3s,
2. Display a new text slide which tells user to rest,
3. keep the screen until user pressed SPACE.
Parameters
----------
text: str
The text on the screen.
background_image: str, or None(default)
The path of background picture.
Return
---------
None
'''
if background_music:
sound = loadSound(background_music)
playing_track = playFreeSound(sound)
textSlide(text, background_image=background_image)
shared.time.sleep(3)
shared.win.dispatch_events()
shared.events = []
text2 = text + '>>>'
alert(text2, 0, key_.SPACE, background_image=background_image)
if background_music:
shared.states[playing_track] = False