-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound-bites.py
453 lines (370 loc) · 15.2 KB
/
sound-bites.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# -*- coding: utf-8 -*-
import os, csv, time, sys, random, copy
from os.path import join, getsize
from psychopy import core, event, gui, sound
from psychopy.visual import Window, TextStim, ImageStim, Rect, GratingStim, BaseVisualStim, RatingScale
from psychopy.constants import FINISHED, STARTED, NOT_STARTED
import config
from utils import from_csv
class InstructionScreen:
"""
This is a class for instructions only. Text stays up
until a key has been pressed.
"""
def __init__(self, window, mouse, image):
self.window = window
self.mouse = mouse
self.instruction = ImageStim(win=window, image=image, interpolate=True) #, size=(800, 800))
# self.instruction = TextStim(self.window, text=self.text, color='black',
# font='Helvetica', wrapWidth=.9 * self.window.size[0])
self.next = Rect(self.window, width=75, height=50, pos=(0, -self.window.size[1] / 2 + 50),
fillColor='blue')
self.nextText = TextStim(self.window, text='Weiter', height=15, pos=(0, -self.window.size[1] / 2 + 50),
color='white')
def display(self):
self.mouse.clickReset(buttons=(0,1,2))
instr_timer = core.Clock()
while True:
self.instruction.draw()
self.next.draw()
self.nextText.draw()
self.window.flip()
if self.mouse.isPressedIn(self.next) and instr_timer.getTime() > 2:
break
class SearchStim(BaseVisualStim):
"""
Implement basic functionality to display a stim (images) and accept arrow keys (left, right) as response.
Logs response and response time.
"""
def __init__(self, window, name, image):
super(SearchStim, self).__init__(win=window)
self.name = name
self.image = image
self.stim = ImageStim(win=window, image=image, interpolate=True)
self.clock = core.Clock()
self.status = NOT_STARTED
self.firstDraw = True
self.respKeys = {'y': 'left', 'period': 'right'}
self.response = None # will be 'left' or 'right'
self.rt = None
self.too_slow_box = Rect(win=window, width=200, height=120, fillColor=LIGHTGREY)
self.too_slow_text = TextStim(win=window, text="Zu langsam", color='black')
self.reset()
def __str__(self):
return "SearchStim: " + self.name
def reset(self):
self.status = NOT_STARTED
self.firstDraw = True
self.clock.reset()
self.response = None # will be 'left' or 'right'
self.rt = None
def draw(self):
if self.firstDraw:
self.firstDraw = False
self.status = STARTED
event.clearEvents('keyboard')
self.clock.reset()
# enforce timeout of 3.5s
elif self.clock.getTime() > 3.5 and self.status != TIMEOUT:
self.status = TIMEOUT
# stop prematurely
elif self.clock.getTime() > 5:
self.status = FINISHED
# check for keys
if self.status == STARTED:
for key in event.getKeys():
if key in self.respKeys.keys():
self.response = self.respKeys[key]
self.rt = self.clock.getTime()
self.status = FINISHED
self.stim.draw()
if self.status == TIMEOUT:
self.too_slow_box.draw()
self.too_slow_text.draw()
def getResponse(self):
if not self.status == FINISHED:
raise "SearchStim not completed yet"
else:
return self.response
def getRT(self):
if not self.status == FINISHED:
raise "SearchStim not completed yet"
else:
return self.rt
class InstructionScreen:
"""
This is a class for instructions only. Text stays up
until a key has been pressed.
"""
def __init__(self, window, mouse, image):
self.window = window
self.mouse = mouse
self.instruction = ImageStim(win=window, image=image, interpolate=True) # , size=(800, 800))
# self.instruction = TextStim(self.window, text=self.text, color='black',
# font='Helvetica', wrapWidth=.9 * self.window.size[0])
self.next = Rect(self.window, width=75, height=50, pos=(0, -self.window.size[1] / 2 + 50),
fillColor='blue')
self.nextText = TextStim(self.window, text='Weiter', height=15, pos=(0, -self.window.size[1] / 2 + 50),
color='white')
def display(self):
self.mouse.clickReset(buttons=(0, 1, 2))
instr_timer = core.Clock()
while True:
self.instruction.draw()
self.next.draw()
self.nextText.draw()
self.window.flip()
if self.mouse.isPressedIn(self.next) and instr_timer.getTime() > 2:
break
# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__)).decode(sys.getfilesystemencoding())
os.chdir(_thisDir)
### Screen config
XRES = 900
YRES = 900
BOX_SIZE = (150, 150)
BOX_SPACING = 50
FONT_SIZE = 30
LIGHTGREY = [0.5, 0.5, 0.5]
RES_PATH = 'resources/'
OUTPUT_PATH = 'output/'
SOUNDS_PATH = 'resources/sounds/exp1/'
INSTR_PATH = 'resources/instruction/'
VISUALS_PATH = 'resources/search_arrays/exp1/'
SOUNDS = ['cluckinghen.wav',
'firealarm.wav',
'growlingdog.wav',
'microwaveoven.wav']
TIMEOUT = 3 # stimulus state constant
expInfo = {'VPN': '', 'Geschlecht': ''}
# some global objects
window = None
mouse = None
searchStims = dict()
va_sounds = list()
va_stimuli = None
vo_stimuli = None
fixation = None
def create_sound_stimuli():
config.sound_stimuli = {s: sound.Sound(value=SOUNDS_PATH + s, name=s) for s in SOUNDS}
def create_ratingscales():
config.ratingscales = [RatingScale(win=window, name='valence_rating', scale=u'Wie fühlen Sie sich gerade?',
labels=('negativ', 'positiv'),
stretch=2, pos=[0.0, -0.4], low=0, high=1, precision=100, showValue=False,
markerExpansion=0, acceptText='Weiter', textColor='white'),
RatingScale(win=window, name='arousal_rating', scale=u'Wie fühlen Sie sich gerade?',
labels=('niedrige Aktivierung', 'hohe Aktivierung'),
stretch=2, pos=[0.0, -0.4], low=0, high=1, precision=100, showValue=False,
markerExpansion=0, acceptText='Weiter', textColor='white')]
def create_visual_stimuli():
# keys:
# - salience: 'L1' = low, 'H2' = high
# - position: 'cen' = central, 'per' = peripheral
# - orientation: 'l' = left, 'r' = right
for sal in ['L1', 'L2']:
config.visual_stimuli[sal] = dict()
for pos in ['cen', 'per']:
config.visual_stimuli[sal][pos] = dict()
for ori in ['l', 'r']:
# read folder, fill dict with list of images
config.visual_stimuli[sal][pos][ori] = list()
for _, _, files in os.walk(VISUALS_PATH):
# filename has format L1_cen1_l.jpg
for f in files:
sal, pos, ori_misc = f.split('_')
pos = pos[:3] # leave out number
ori, _ = ori_misc.split('.') # remove file extension
ori = ori[0].lower() # only look at orientation: L3 -> l, R4 -> r
config.visual_stimuli[sal][pos][ori].append(
SearchStim(window=window, name=f, image=VISUALS_PATH + f))
def block_sound_ratings():
config.increase_block("Ratings")
config.log_append("StartBlockRatings", "", "")
for s in va_sounds:
config.increase_trial(s.name)
config.log_append("DisplayFixation", "", "")
sound_played = False
# fixation
while config.trial_clock.getTime() < 0.5 + s.getDuration():
if config.trial_clock.getTime() >= 0.5 and not sound_played:
sound_played = True
s.play()
fixation.draw()
window.flip()
# ratings
for r in config.ratingscales:
r.reset()
# draw scale
while r.status != FINISHED:
r.draw()
window.flip()
config.log_append(r.name, r.getRating(), r.getRT())
def block_va_search():
config.increase_block("VA")
config.log_append("StartBlockVA", "", "")
for va in va_stimuli:
va_sound = va.keys()[0]
va_stim = va.values()[0]
config.increase_trial("%s-%s" % (va_sound, va_stim.name))
config.log_append("DisplayFixation", "", "")
va_stim.reset()
sound_played = False
# fixation
while config.trial_clock.getTime() < 0.5 + config.sound_stimuli[va_sound].getDuration():
if config.trial_clock.getTime() >= 0.5 and not sound_played:
print("trial clock: %f, duration: %f" %
(config.trial_clock.getTime(), 0.5 + config.sound_stimuli[va_sound].getDuration()))
sound_played = True
config.sound_stimuli[va_sound].play()
fixation.draw()
window.flip()
# search task
while va_stim.status != FINISHED:
va_stim.draw()
window.flip()
config.log_append("SearchResponse", va_stim.getResponse(), va_stim.getRT())
# 3s empty screen
emptyscreen_clock = core.Clock()
while emptyscreen_clock.getTime() < 3:
window.flip()
def block_vo_search():
config.increase_block("VO")
config.log_append("StartBlockVO", "", "")
for vo in vo_stimuli:
config.increase_trial(vo.name)
config.log_append("DisplayFixation", "", "")
vo.reset()
# fixation
while config.trial_clock.getTime() < 1:
fixation.draw()
window.flip()
# search task
while vo.status != FINISHED:
vo.draw()
window.flip()
config.log_append("SearchResponse", vo.getResponse(), vo.getRT())
# 3s empty screen
emptyscreen_clock = core.Clock()
while emptyscreen_clock.getTime() < 3:
window.flip()
def save():
# add experiment info to log entries
[config.resp_data[i].update({'respondent': expInfo['VPN'], 'sex': expInfo['Geschlecht']})
for i in range(len(config.resp_data))]
# encode everything as unicode
config.resp_data = [
{(k.encode('utf-8') if isinstance(k, unicode) else k): (v.encode('utf-8') if isinstance(v, unicode) else v)
for (k, v) in row.iteritems()} for row in config.resp_data]
with open(fileName, 'wb') as csvfile:
fieldnames = (['respondent', 'sex', 'condition'] + config.log_fields)
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(config.resp_data)
def count_other_sounds(snd, dic):
assert(isinstance(dic, dict))
other_snds = set(SOUNDS) - set([snd])
count = 0
for s in other_snds:
count += len(dic[s])
return count
def copy_dict_list(input):
ret = {}
for k in input.keys():
ret[k] = input[k][:]
return ret
### Construct experiment
if __name__ == '__main__':
# get respondent info
expInfoDlg = gui.Dlg(title="Experiment Log")
expInfoDlg.addField('VPN')
expInfoDlg.addField('Geschlecht', choices=["m", "f"])
expInfoDlg.show() # show dialog and wait for OK or Cancel
if expInfoDlg.OK: # then the user pressed OK
expInfo = dict(zip(["VPN", "Geschlecht"], expInfoDlg.data))
print(expInfo)
elif expInfoDlg.OK or not expInfo['VPN'] or not expInfo['Geschlecht']:
core.quit()
# Create output file
fileName = OUTPUT_PATH + expInfo['VPN'] + '_' + time.strftime("%Y%m%d-%H%M%S") + '_sound-bites.csv'
# create window and mouse objects
window = Window((XRES, YRES), allowGUI=False, color='black', fullscr=True,
monitor='testMonitor', winType='pyglet', units='pix')
mouse = event.Mouse(win=window)
# create instruction
instruction = InstructionScreen(window=window, mouse=mouse, image=INSTR_PATH + "instruction.png")
# first create all stimuli once
fixation = GratingStim(win=window, tex=None, mask='gauss', sf=0, size=15,
name='fixation', autoLog=False, units='pix', pos=(0.0, 0.0), color="white")
create_sound_stimuli()
create_visual_stimuli()
create_ratingscales()
### create random blocks
# ratings: random sound order
random.shuffle(SOUNDS)
va_sounds = [config.sound_stimuli[k] for k in SOUNDS[:]]
# va: for each sound - pick one of each combination
# -> 8 combinations
va_stimuli = []
sound_stimuli = {}
for sal in ['L1', 'L2']:
for pos in ['cen', 'per']:
for ori in ['l', 'r']:
for snd in SOUNDS:
# get random stimuli of given category
if not snd in sound_stimuli.keys():
sound_stimuli[snd] = list()
sound_stimuli[snd].append({snd: random.choice(config.visual_stimuli[sal][pos][ori])})
# randomize order, same sounds never in succession
backup = copy_dict_list(sound_stimuli)
for snd in SOUNDS:
random.shuffle(sound_stimuli[snd])
last_snd = None
restart_count = 0
print "Start randomizing, this can take a while..."
start_time = time.time()
while len(va_stimuli) < 32:
snd = random.choice(SOUNDS)
# store stim if there are any left of this sound
if not snd == last_snd and len(sound_stimuli[snd]) > 0:
last_snd = snd
va_stimuli.append(sound_stimuli[snd].pop(0))
# restart if only one sound is left and it's the same as last_sound
elif snd == last_snd and count_other_sounds(snd, sound_stimuli) == 0:
last_snd = None
va_stimuli = []
restart_count += 1
sound_stimuli = copy_dict_list(backup)
# one run takes ~1ms on this system, so allow at most 1s = 1000 runs
if restart_count > 1000:
sys.exit("Randomization did not succeed")
print "Finished randomizing, taking %i restarts in %s seconds" % (restart_count, time.time() - start_time)
# for i in va_stimuli:
# print("sound: %s,\tstim: %s" % (i.keys()[0], i.values()[0].name))
# vo: pick four of each combination
# -> 8 combinations
vo_stimuli = []
for sal in ['L1', 'L2']:
for pos in ['cen', 'per']:
for ori in ['l', 'r']:
for _ in xrange(4):
vo_stimuli.append(random.choice(config.visual_stimuli[sal][pos][ori]))
# randomize visuals order
random.shuffle(vo_stimuli)
config.exp_clock = core.Clock()
config.trial_clock = core.Clock()
# get sound ratings
block_sound_ratings()
# display instruction
instruction.display()
# flip a coin with which task to start...
startVA = random.choice([True, False])
if startVA:
block_va_search()
block_va_search()
block_vo_search()
else:
block_vo_search()
block_va_search()
block_va_search()
# save logged results
save()