-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_Adjustable.py
More file actions
564 lines (387 loc) · 18.9 KB
/
Count_Adjustable.py
File metadata and controls
564 lines (387 loc) · 18.9 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import viz
import numpy as np
import vizmat
import transform
import math
import viztask
import pandas as pd
import csv, io, os
import threading
import winsound
#import vizjoy
#Each trial should be an independent class.
class Distractor(viz.EventClass):
def __init__(self, filename, maxtargetnumber, ppid, triallength, ntrials = None, startscreentime = 2):
viz.EventClass.__init__(self)
#needs to be an eventclass for timer to work.
self.callback(viz.EXIT_EVENT,self.CloseConnections) #if exited, save the data.
##PARAMETERS THAT DO NOT VARY PER TRIAL
self.ppid = ppid
self.filename = filename
self.AudioList = [] #load once at start. List of audio-files
self.StartScreen_DisplayTime = startscreentime #amount of seconds that the targets for that trial are shown.
#letters = ['a','b','c','d','e','i','o']#,'f','g']#,'h','i','j','k','l']#,'m','n','o']
#letters = ['a','b','k','h','f','i','o']#,'f','g']#,'h','i','j','k','l']#,'m','n','o'] #don't rhyme.
self.letters = ['b','o','h','k','t','l','f','s','i','a','m','n','y','r','j'] #target letters are the front two. Need to be paired with three distractors.
l = len(self.letters)
#self.LetterLength
for i in range(l):
a = self.letters[i]
#self.AudioList.append(viz.addAudio('..\\textures\\audio-numbers\\' + a + '.wav'))
#self.AudioList.append(viz.addAudio('C:\\VENLAB data\\shared_modules\\textures\\Alphabet_Sounds\\' + a + '.wav'))
self.AudioList.append('C:\\VENLAB data\\shared_modules\\textures\\Alphabet_Sounds\\' + a + '.wav') #append file paths so you can use viz.SOUND_PRELOAD
#preload sounds
for af in self.AudioList:
viz.playsound(af, viz.SOUND_PRELOAD)
# self.SoundPlayer_threaded = SoundPlayer_threaded(self.AudioList) #load thread
# self.SoundPlayer_threaded.start() #start the threaed
self.Target_pool = self.letters[:maxtargetnumber] #returns list up to maxtargetnumber
self.Distractor_pool = self.letters[maxtargetnumber:]
self.nTrials = ntrials
self.Trial_length = triallength #length of trial. Is usually constant.
self.EndofTrial_Data, self.WithinTrial_Data_writer, self.WithinTrial_Data_file = self.BuildDataFrames(maxtargetnumber)
self.MaxTargetNumber = maxtargetnumber
#PARAMETERS THAT ARE SET AT THE START OF EACH TRIAL
self.Trial_targets = [] #targets for that particular trial.
self.Trial_audioindexes = [] #empty list that is populated with indexes corresponding to places on the audio list. Targets will be the first N of the letter array, depending on trial number.
self.Trial_targetoccurence_prob = 0 #trial parameters, target occurence
self.Trial_targetnumber = 0 #trial parameters, target number
self.Trial_targetcounts = [] #empty list with actual self.Trial_targetnumber counts.
self.Trial_EoTscores = [] #empty list with self.Trial_targetnumber user inputted counts.
self.Trial_N = 0
self.Trial_Timer = 0 #keeps track of trial length.
self.StartScreen_Timer = 0
#self.Trial_Index = 0 #count for number of Trails, to index Trial dataframe. Isn't needed as Trial_N gets passed on StartTrial.
# PARAMETERS THAT VARY WITHIN TRIALS
self.ON = 0 #flag denoting whether to enter the loop that records data
self.targetDelay = [] #randomly varies between 1-1.5s
self.currentaudio = None # currently heard stimuli
self.currentaudio_type = None # currently heard stimuli
self.ResponseStamp = 0 #time of response
self.Stimuli_PlayedStamp = 0 #time of stimuli presentation
self.delay = 1.25 #this is how quickly you want items repeated. Changes per stimuli.
self.Overall_Stimuli_Index = 0 #count for number of stimuli, to index response dataframe.
self.Trial_Stimuli_Index = 0 #count for number of stimuli, to index response dataframe.
self.callback(viz.TIMER_EVENT,self.onTimer, priority = 0)
self.Stimuli_Timer =0 #between - presentation timer.
self.interval = 0.1 #delay randomly varies between 1 - 1.5 at .1 increments.
self.targetTimer = 0
self.targetInterval = 0
self.starttimer(0,self.interval,viz.FOREVER)
self.QuitFlag = 0
self.ppresp = 0 #flag to say that participant has responded.
self.EoTFlag = False #set to True when it is the EoT screen.
self.PlaySoundFlag = False #set to true when start screen time elapses. #Flag that is called by the main experiment to know when to stop pausing.
self.EoT_NumberofResponses = 0 #count to say how many counts have been inputted.
### END OF TRIALS SCREEN ###
self.EoTScreen = viz.addTexQuad(viz.SCREEN)
self.EoTScreen.color(viz.BLACK)
self.EoTScreen.setPosition(.5,.6)
self.EoTScreen.setScale(100,100)
self.EoTScreen.visible(viz.OFF)
self.EndofTrial_Question = 'How many Xs did you hear? \n \n Press a single gear pad to register your count'
self.Question = viz.addText(self.EndofTrial_Question, viz.SCREEN)
self.Question.color(1,1,0)
self.Question.setPosition(0.5,0.75)
self.Question.fontSize(36)
self.Question.alignment(viz.TEXT_CENTER_TOP)
self.Question.visible(viz.OFF)
self.lblscore = viz.addText('-1', viz.SCREEN)
self.lblscore.setPosition(0.5,0.4)
self.lblscore.fontSize(50)
self.lblscore.alignment(viz.TEXT_CENTER_CENTER)
self.lblscore.visible(viz.OFF)
self.CurrentScore = -1 #for on-screen score to be submitted as user count.
### START OF TRIALS SCREEN ####
self.StartScreen = viz.addTexQuad(viz.SCREEN)
self.StartScreen.color(viz.BLACK)
self.StartScreen.setPosition(.5,.5)
self.StartScreen.setScale(100,100)
self.StartScreen.visible(viz.OFF)
self.Start_msg = 'Listen out for: \n \n'
self.Starttxt = viz.addText(self.Start_msg, viz.SCREEN)
self.Starttxt.color(1,1,0)
self.Starttxt.setPosition(0.5,0.5)
self.Starttxt.fontSize(36)
self.Starttxt.alignment(viz.TEXT_CENTER_TOP)
self.Starttxt.visible(viz.OFF)
def BuildDataFrames(self, maxtargetnumber):
"""Builds EoT_Dataframe and WithinTrial_DataFrame"""
#add info for individual trials
#EndofTrial_Data has columns: ['ppid', 'targetoccurence','targetnumber','trialn', 'EoTScore1','TargetCount1'...'EoTScoreN','TargetCountN']
#WithinTrial_Data has columns: ['ppid', 'targetoccurence','targetnumber','trialn', 'CurrentAudio','RT','ResponseCategory', 'Target1',...'TargetN']
trialinfo_columns = ['ppid', 'targetoccurence','targetnumber','trialn']
EoTcolumns = trialinfo_columns
WithinTrialcolumns = trialinfo_columns + ['timestamp_exp','item_delay','currentaudio_type','CurrentAudio','RT','ResponseCategory']
for i in range(1,maxtargetnumber +1):
EoTcolumn = 'EoTScore' + str(i)
EoTcolumns.append(EoTcolumn)
TargetCountcolumn = 'TargetCount' + str(i)
EoTcolumns.append(TargetCountcolumn) #columns for end of trial dataframe
Targetcolumn = 'Target' + str(i)
WithinTrialcolumns.append(Targetcolumn) #columns for within trial dataframe.
EndofTrial_Data = pd.DataFrame(columns=EoTcolumns) #make new empty EndofTrial data
#WithinTrial_Data = pd.DataFrame(columns=WithinTrialcolumns) #make new empty EndofTrial data
#WithinTrial_Data = pd.DataFrame(index = range(self.nTrials*(self.Trial_length*2)), columns=WithinTrialcolumns) #pre-allocate plenty of space. 2 per second will be plenty resposnes
#open stream as bytes object for quicker saving.
WithinTrialcolumns = tuple(WithinTrialcolumns) #needs to be tuple to write row.
WithinTrialFile = io.BytesIO()
WithinTrialWriter = csv.writer(WithinTrialFile)
WithinTrialWriter.writerow(WithinTrialcolumns)
#return (EndofTrial_Data, WithinTrial_Data)
return (EndofTrial_Data, WithinTrialWriter, WithinTrialFile)
def StartTrial(self, targetoccurence_prob, targetnumber, trialn, displayscreen = True):
"""Sets parameters at the start of each trial, based on targetoccurence_prob and targetnumber"""
print("Called StartTrial")
self.Trial_targetoccurence_prob = targetoccurence_prob #trial parameters, target occurence probability
self.Trial_targetnumber = targetnumber #trial parameters, target number
self.Trial_targetcounts = [0] * targetnumber #empty list with self.Trial_targetnumber counts
self.Trial_EoTscores = [-1] * targetnumber
self.Trial_N = trialn
#print ("TargetPool:", self.Target_pool)
self.Trial_targets = list(np.random.choice(self.Target_pool, size=targetnumber, replace=False))
print("Trial targets: ", self.Trial_targets)
#show start screen. Toggle whether you display the start screen or not.
if displayscreen:
self.ChangeStartMsg()
self.StartScreen_Visibility(viz.ON)
self.StartScreen_DisplayTime = 2
else:
self.StartScreen_DisplayTime = 0
self.EoT_NumberofResponses = 0 # not sure what this logic is for yet.
self.StartScreen_Timer = 0 #reset start screen timer
self.Stimuli_Timer = 0 #reset inter-presentation timer
self.Trial_Timer = 0 #reset trial length to zero
self.Trial_Stimuli_Index = 0
self.ON = 1
def onTimer(self,num):
if self.ON == 1:
#print "self.Stimuli_Timer: " + str(self.Stimuli_Timer)
#need to only play files sequentially.
#self.ON gets switched on at StartTrial().
#If it's the end of trial screen do nothing
if self.EoTFlag:
pass
else:
#check if we want to play a sound. If not, the start screen will be on.
if self.PlaySoundFlag:
if self.Stimuli_Timer > self.delay:
#change random choice
choice = np.random.randint(0,2)
if choice == 1:
if self.Trial_Stimuli_Index < 1:
self.SetNewStimuli()
else:
self.DetectAudioResponse() #function that sets target, with delay parameters
self.Stimuli_Timer = self.Stimuli_Timer+self.interval
self.Trial_Timer = self.Trial_Timer + self.interval #timer to keep track of overall trial length
if self.Trial_Timer > self.Trial_length:
#here start end of trial screens.
self.EndofTrial()
else:
# if the start screen is active, don't play a sound but increment the startscreen timer
if self.StartScreen_Timer > self.StartScreen_DisplayTime:
#remove startscreen and start recording.
self.PlaySoundFlag = True #flag for experiment class.
self.StartScreen_Visibility(viz.OFF)
else:
self.StartScreen_Timer += self.interval #increment StartScreenTimer by Timer interval
def getFlag(self):
#return whether end of trial screen is on.
"called get flag"
return(self.EoTFlag)
def getPlaySoundFlag(self):
#return whether end of trial screen is on.
"called start flag"
return(self.PlaySoundFlag)
def getEoT_NumberofResponses(self):
#return whether ppresponded.
"called get response"
return(self.EoT_NumberofResponses)
def ChangeQuestionText(self):
"""changes self.Question message based on targets in trial and number of responses"""
target = str(self.Trial_targets[self.EoT_NumberofResponses])
msg = str(self.EndofTrial_Question)
msg = msg.replace('X', target.upper())
self.lblscore.message('-1')
self.Question.message(msg)
def ChangeStartMsg(self):
"""changes self.Start_msg based on self.Trial_targets"""
#add targets msg string
msg = str(self.Start_msg)
for target in self.Trial_targets:
msg = msg + target.upper() + ' '
self.Starttxt.message(msg)
def EndofTrial(self):
"""Throws black screen and wait for gearpads to be pressed"""
self.ChangeQuestionText()
self.EoTScreen_Visibility(viz.ON)
#tell class it is end of trial.
self.EoTFlag = True
#self.ON = 0 #don't actually need to set this here because the EoTFlag does the same function.
self.PlaySoundFlag = False
def EoTScreen_Visibility(self, visible = viz.ON):
"""switches the EoTscreen visibility off or on"""
self.EoTScreen.visible(visible)
self.Question.visible(visible)
self.lblscore.visible(visible)
def StartScreen_Visibility(self, visible = viz.ON):
"""switches the Start Screen visibility off or on"""
self.StartScreen.visible(visible)
self.Starttxt.visible(visible)
def RecordCounts(self):
"""save End of Trial counts to dataframe"""
#do not record data
self.ON = 0
trialinfo = [self.ppid, self.Trial_targetoccurence_prob, self.Trial_targetnumber, self.Trial_N]
#Adds 'Nones' so that rows match with columns.
Trial_EoTscores_output = list(self.Trial_EoTscores)
while len(Trial_EoTscores_output) < self.MaxTargetNumber:
Trial_EoTscores_output.append(None)
Trial_targetcounts_output = list(self.Trial_targetcounts)
while len(Trial_targetcounts_output) < self.MaxTargetNumber:
Trial_targetcounts_output.append(None)
output = Trial_EoTscores_output + Trial_targetcounts_output #makes a list of the correct length
output[::2] = Trial_EoTscores_output
output[1::2] = Trial_targetcounts_output
output = list(trialinfo) + output
#Trial N gets incremented anyway.
self.EndofTrial_Data.loc[self.Trial_N,:] = output #this dataframe is actually just one line.
self.EoTScreen_Visibility(viz.OFF)
#tell class that the end of trial screen has finished
self.EoTFlag = False
print ("recorded counts")
def CloseConnections(self):
"""kills threaded functions and saves data"""
self.SaveData()
# self.SoundPlayer_threaded.thread_kill() #kill thread
def SaveData(self):
"""Call after all responses are recorded to save data to file"""
##save all data to file.
fileext = '.csv'
eot_file_path = 'Data//' + self.filename + '_EndofTrial'
eot_path = eot_file_path + fileext
if os.path.exists(eot_path):
rint = np.random.randint(1, 100)
eot_path = eot_file_path + '_copy_' + str(rint) + fileext
self.EndofTrial_Data.to_csv(eot_path)
#self.WithinTrial_Data = self.WithinTrial_Data.dropna() #drop trailing zeros
#self.WithinTrial_Data.to_csv('Data//' + str(self.filename) + '_WithinTrial.csv')
wit_file_path = 'Data//' + self.filename + '_WithinTrial'
wit_path = wit_file_path + fileext
if os.path.exists(wit_path):
rint = np.random.randint(1, 100)
wit_path = wit_file_path + '_copy_' + str(rint) + fileext
self.WithinTrial_Data_file.seek(0) #start from beginning
data = pd.read_csv(self.WithinTrial_Data_file) #read bytes stream
data.to_csv(wit_path) #save to csv
print ("Saved Data")
def DetectAudioResponse(self):
"""Function determines whether there has been an appropriate response"""
print ("DetectAudioResponse called")
print ("currentaudio_type", self.currentaudio_type)
t = viz.tick()
if self.currentaudio_type == 'T': #should have responded.
target_index = self.Trial_targets.index(self.currentaudio) #retrieve index of target within current trial
self.Trial_targetcounts[target_index] += 1 #increment target count.
if self.ppresp == 1:
RT = self.ResponseStamp-self.Stimuli_PlayedStamp
ResponseCategory = 1 #correctly responded.
elif self.ppresp == 0:
RT = -1
ResponseCategory = 2 #did not respond when should have.
elif self.currentaudio_type == 'D': #should NOT have responded.
if self.ppresp == 1:
RT = self.ResponseStamp-self.Stimuli_PlayedStamp
ResponseCategory = '3' #responded when shouldn't have.
elif self.ppresp == 0:
RT = -1
ResponseCategory = '4' #correct absence of response
#the row size will change depending on target number.
trialinfo = [self.ppid, self.Trial_targetoccurence_prob, self.Trial_targetnumber, self.Trial_N]
currentresponse = [viz.tick(), self.delay, self.currentaudio_type,self.currentaudio, RT, ResponseCategory]
Trial_targets_outputlist = list(self.Trial_targets)
while len(Trial_targets_outputlist) < self.MaxTargetNumber:
Trial_targets_outputlist.append(None)
output = list(trialinfo) + list(currentresponse) + Trial_targets_outputlist
# self.WithinTrial_Data.loc[self.Overall_Stimuli_Index-1,:] = output #this is takes about 3ms. Consider changing to csv writer.
output = tuple(output)
self.WithinTrial_Data_writer.writerow(output) #write row to output stream.
#https://stackoverflow.com/questions/41888080/python-efficient-way-to-add-rows-to-dataframe
print("DetectResposne: ", viz.tick() - t)
self.SetNewStimuli()
def SetNewStimuli(self):
"""Sets the delay and target for the next stimuli, based on the target occurence and current pool"""
print ("SetNewStimuli called")
t= viz.tick()
choices = ['T','D']
probabilities = [self.Trial_targetoccurence_prob, 1-self.Trial_targetoccurence_prob]
self.currentaudio_type = np.random.choice(choices, p = probabilities) #slow. ~ 1ms.
if self.currentaudio_type == 'T':
self.currentaudio = np.random.choice(self.Trial_targets)
elif self.currentaudio_type == 'D':
self.currentaudio = np.random.choice(self.Distractor_pool)
self.Stimuli_PlayedStamp=viz.tick()
print(self.currentaudio)
audioindex = self.letters.index(self.currentaudio)
af = self.AudioList[audioindex]
viz.playSound(af)
self.Stimuli_Timer=0
self.ppresp = 0
##set new delay. Random between 1-1.5
jitter = np.random.randint(0,49)
self.delay = 1.0 + (jitter/100.0)
self.Overall_Stimuli_Index += 1
self.Trial_Stimuli_Index += 1
print ("SetNewStimuli: ", viz.tick() - t)
#### THE FOLLOWING FUNCTIONS CONTROL THE DRIVER INTERACTING WITH THE WHEEL ######
def keydown(self,button):
"""records a button press response to stimuli"""
if self.ON == 1:
self.ResponseStamp = viz.tick()
#can press any button for response
self.ppresp = 1
#print "responded: " + str(viz.tick())
def gearpaddown(self,button=None):
"""saves on-screen count"""
print "Button: " + str(button) #6 is left. 5 is right. Participants can press any button for the response.
if self.EoTFlag:
self.Trial_EoTscores[self.EoT_NumberofResponses] = self.CurrentScore
self.EoT_NumberofResponses += 1
print("Recorded Count" + str(self.EoT_NumberofResponses))
if self.EoT_NumberofResponses < self.Trial_targetnumber:
self.ChangeQuestionText()
def joymove(self,pos):
"""translates steering wheel movement to movement of the label text"""
#if it is end of trial update score
if self.EoTFlag:
pos = pos[0]
pos = (pos * 10) + 5 #wheelpos is between -1,1
scale = 1
scalepos = pos * scale
roundpos = round(scalepos)
#print "round pos: " + str(roundpos)
clamppos = viz.clamp(roundpos, 0, 10)
self.CurrentScore = clamppos
self.lblscore.message(str(clamppos))
#print "score: " + str(self.EoTScore)
#### DO NOT NEED THREADED CLASS. FOR SOME REASONS THREADING IN VIZARD IS PROCESSING HEAVY
class SoundPlayer_threaded(threading.Thread):
def __init__(self, audiolist):
threading.Thread.__init__(self)
self.thread_init()
self.AudioFiles = audiolist
###preload sounds into vizard.
for af in self.AudioFiles:
viz.playsound(af, viz.SOUND_PRELOAD) #NEEDS TO PRE-LOAD THE SOUND
def thread_init(self):
"""Initialise the thread"""
self.__thread_live = True
def thread_kill(self):
"""Turn the thread loop off"""
self.__thread_live = False
def PlaySound(self,audioindex):
t = viz.tick()
myaudio = self.AudioFiles[audioindex]
viz.playSound(myaudio)
print ("PLAY SOUND: ", viz.tick() - t)