-
Notifications
You must be signed in to change notification settings - Fork 0
/
nStreamBufferImageStim.py
945 lines (745 loc) · 38.1 KB
/
nStreamBufferImageStim.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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
#Alex Holcombe alex.holcombe@sydney.edu.au
#See the README.md for more information: https://github.com/alexholcombe/attentional-blink/blob/master/README.md
#git remote add origin https://github.com/alexholcombe/nStream
from __future__ import print_function, division
from psychopy import monitors, visual, event, data, logging, core, sound, gui
import psychopy.info
import numpy as np
from math import atan, log, ceil, cos, sin, pi, atan2
from copy import deepcopy
import time, sys, os#, pylab
import string, random
eyetrackingOption = True #Include this so can turn it off, because Psychopy v1.83.01 mistakenly included an old version of pylink which prevents EyelinkEyetrackerForPsychopySUPA3 stuff from importing
#Eyetracking stuff
if eyetrackingOption:
from EyelinkEyetrackerForPsychopySUPA3 import Tracker_EyeLink #Chris Fajou integration
eyetracking = False
getEyeTrackingFileFromEyetrackingMachineAtEndOfExperiment = False #If True, can take up to 1.5 hrs in certain conditions
#End eyetracking stuff
try:
import stringResponse
except ImportError:
print('Could not import strongResponse.py (you need that file to be in the same directory)')
try:
import letterLineupResponse
except ImportError:
print('Could not import letterLineupResponse.py (you need that file to be in the same directory)')
try: #LetterToNumber NumberToLetter
import alphabetHelpers
except ImportError:
print('Could not import alphabetHelpers.py (you need that file to be in the same directory)')
try:
import corticalMagnification
except ImportError:
print('Could not import corticalMagnification.py (you need that file to be in the same directory)')
try:
import setupHelpers
except ImportError:
print('Could not import setupHelpers.py (you need that file to be in the same directory)')
#THINGS THAT COULD PREVENT SUCCESS ON A STRANGE MACHINE
#same screen or external screen? Set scrn=0 if one screen. scrn=1 means display stimulus on second screen.
#widthPix, heightPix
quitFinder = False #if checkRefreshEtc, quitFinder becomes True
autopilot=False
demo=False #False
exportImages= False #quits after one trial
subject='Hubert' #user is prompted to enter true subject name
if autopilot: subject='auto'
if os.path.isdir('.'+os.sep+'rawData/18Streams'):
dataDir='rawData/18Streams'
else:
print('"rawData" directory does not exist, so saving data in present working directory')
dataDir='.'
timeAndDateStr = time.strftime("%d%b%Y_%H-%M", time.localtime())
####################################
####################################
## Display and stimuli parameters ##
####################################
####################################
demo = False
showRefreshMisses=True #flicker fixation at refresh rate, to visualize if frames missed
feedback=False
autoLogging=False
refreshRate = 100
if demo:
refreshRate = 60.; #100 LN: refresh rate for previous AB and RSVP task for gamers was 60
font = 'sloan'
threshCriterion = 0.58
bgColor = [-.7,-.7,-.7] # [-1,-1,-1]
cueColor = [1.,1.,1.]
cueType = 'exogenousRing' #'lowerCase' #'exogenousRing' #'endogenous':
if cueType == 'endogenous':
cueColor = [1,-1,-1]
letterColor = [1.,0.,0.]
cueRadius = 2.5 #6 deg, as in Martini E2 Letters should have height of 2.5 deg
#There's no lowercase in sloan
if cueType is 'lowerCase':
font = 'Arial'
viewdist = 59. #cm
monitorname = 'testmonitor'
waitBlank = False
widthPix = 1024 #monitor width in pixels of Agosta
heightPix = 768 #800 #monitor height in pixels
monitorwidth = 40.5
mon = monitors.Monitor(monitorname,width=monitorwidth, distance=viewdist)#relying on monitorwidth cm (39 for Mitsubishi to do deg calculations) and gamma info in calibratn
mon.setSizePix( (widthPix,heightPix) )
units='deg' #'cm'
scrn = 1
doStaircase = False
screenValues = {
'widthPix': 1024, #monitor width in pixels of Agosta
'heightPix': 768, #800 #monitor height in pixels
'monitorwidth' :40.5, #monitor width in cm
'scrn':0, #0 to use main screen, 1 to use external screen connected to computer
'fullscr':True, #True to use fullscreen, False to not. Timing probably won't be quite right if fullscreen = False
'allowGUI' : False,
'bgColor' : bgColor,
'screen' : scrn,
'units' : units,
'waitBlank':waitBlank
}
if demo: monitorwidth = 23#18.0
if exportImages:
widthPix = 400; heightPix = 400
monitorwidth = 13.0
fullscr=False; scrn=0
if demo:
scrn=0; fullscr=False
widthPix = 800; heightPix = 600
monitorname='testMonitor'
allowGUI = True
pixelperdegree = widthPix/ (atan(monitorwidth/viewdist) /np.pi*180)
msg= 'pixelperdegree=' + str( round(pixelperdegree,2) )
logging.info(pixelperdegree)
#letter size 2.5 deg
numLettersToPresent = 24
#For AB, minimum SOAms should be 84 because any shorter, I can't always notice the second ring when lag1. 71 in Martini E2 and E1b (actually he used 66.6 but that's because he had a crazy refresh rate of 90 Hz)
SOAms = 83.25 #82.35 Battelli, Agosta, Goodbourn, Holcombe mostly using 133
letterDurMs = 60 #60
ISIms = SOAms - letterDurMs
letterDurFrames = int( np.floor(letterDurMs / (1000./refreshRate)) )
cueDurFrames = letterDurFrames
print('global cueDurFrames is ' + str(cueDurFrames))
ISIframes = int( np.floor(ISIms / (1000./refreshRate)) )
SOAFrames = int( np.floor(SOAms / (1000./refreshRate)) )
#have set ISIframes and letterDurFrames to integer that corresponds as close as possible to originally intended ms
rateInfo = 'total SOA=' + str(round( (ISIframes + letterDurFrames)*1000./refreshRate, 2)) + ' or ' + str(ISIframes + letterDurFrames) + ' frames, comprising\n'
rateInfo+= 'ISIframes ='+str(ISIframes)+' or '+str(ISIframes*(1000./refreshRate))+' ms and letterDurFrames ='+str(letterDurFrames)+' or '+str(round( letterDurFrames*(1000./refreshRate), 2))+'ms'
logging.info(rateInfo); print(rateInfo)
trialDurFrames = int( numLettersToPresent*(ISIframes+letterDurFrames) ) #trial duration in frames
#############################################
#############################################
###### Create a dialog from dictionary ######
#############################################
#############################################
fullscr, subject = setupHelpers.setupDialogue(mon, screenValues, refreshRate, quitFinder, demo)
screenValues['fullscr'] = fullscr
if not fullscr:
screenValues['widthPix'] = 800
screenValues['heightPix'] = 600
myWin = setupHelpers.openMyStimWindow(mon, screenValues)
#set up output data file, log file, copy of program code, and logging
infix = ''
fileName = os.path.join(dataDir, subject + '_' + infix+ timeAndDateStr)
if not demo and not exportImages:
dataFile = open(fileName+'.txt', 'w')
saveCodeCmd = 'cp \'' + sys.argv[0] + '\' '+ fileName + '.py'
os.system(saveCodeCmd) #save a copy of the code as it was when that subject was run
logFname = fileName+'.log'
ppLogF = logging.LogFile(logFname,
filemode='w',#if you set this to 'a' it will append instead of overwriting
level=logging.INFO)#errors, data and warnings will be sent to this logfile
if demo or exportImages:
dataFile = sys.stdout; logF = sys.stdout
logging.console.setLevel(logging.ERROR) #only show this level messages and higher
logging.console.setLevel(logging.ERROR) #DEBUG means set console to receive nearly all messges, INFO next level, EXP, DATA, WARNING and ERROR
if fullscr and not demo and not exportImages:
runInfo = psychopy.info.RunTimeInfo(
# if you specify author and version here, it overrides the automatic detection of __author__ and __version__ in your script
#author='<your name goes here, plus whatever you like, e.g., your lab or contact info>',
#version="<your experiment version info>",
win=myWin, ## a psychopy.visual.Window() instance; None = default temp window used; False = no win, no win.flips()
refreshTest='grating', ## None, True, or 'grating' (eye-candy to avoid a blank screen)
verbose=False, ## True means report on everything
userProcsDetailed=True, ## if verbose and userProcsDetailed, return (command, process-ID) of the user's processes
#randomSeed='set:42', ## a way to record, and optionally set, a random seed of type str for making reproducible random sequences
## None -> default
## 'time' will use experimentRuntime.epoch as the value for the seed, different value each time the script is run
##'set:time' --> seed value is set to experimentRuntime.epoch, and initialized: random.seed(info['randomSeed'])
##'set:42' --> set & initialize to str('42'), and will give the same sequence of random.random() for all runs of the script
)
logging.info(runInfo)
logging.flush()
#create click sound for keyboard
try:
click=sound.Sound('406__tictacshutup__click-1-d.wav')
except: #in case file missing, create inferiro click manually
logging.warn('Could not load the desired click sound file, instead using manually created inferior click')
click=sound.Sound('D',octave=4, sampleRate=22050, secs=0.015, bits=8)
if showRefreshMisses:
fixSizePix = 18 #2.6 #make fixation bigger so flicker more conspicuous
else: fixSizePix = 6
########################################
####Fixation and instruction stimuli####
########################################
fixColor = [1,1,1]
fixatnPtSize = 4
if exportImages: fixColor= [0,0,0]
fixatnTextureWidth = np.round(fixSizePix/4).astype(int)
fixatnNoiseTexture = np.round( np.random.rand(fixatnTextureWidth,fixatnTextureWidth) ,0 ) *2.0-1 #Can counterphase flicker noise texture to create salient flicker if you break fixation
fixatn= visual.PatchStim(myWin, tex=fixatnNoiseTexture, size=(fixSizePix,fixSizePix), units='pix', mask='circle', interpolate=False, autoLog=False)
fixatnCounterphase= visual.PatchStim(myWin, tex= -1*fixatnNoiseTexture, size=(fixSizePix,fixSizePix), units='pix', mask='circle', interpolate=False, autoLog=False) #reverse contrast
fixatnPoint= visual.Circle(myWin,fillColorSpace='rgb',fillColor=(1,1,1),radius=fixatnPtSize,pos=[0,0],units='pix',autoLog=autoLogging)
respPromptStim = visual.TextStim(myWin,pos=(0, -.8),colorSpace='rgb',color=(1,1,1),alignHoriz='center', alignVert='center',height=.1,units='norm',autoLog=autoLogging)
acceptTextStim = visual.TextStim(myWin,pos=(0, -.7),colorSpace='rgb',color=(1,1,1),alignHoriz='center', alignVert='center',height=.1,units='norm',autoLog=autoLogging)
acceptTextStim.setText('Hit ENTER to accept. Backspace to edit')
respStim = visual.TextStim(myWin,pos=(0,0),colorSpace='rgb',color=(1,1,0),alignHoriz='center', alignVert='center',height=.16,units='norm',autoLog=autoLogging)
clickSound, badKeySound = stringResponse.setupSoundsForResponse()
requireAcceptance = False
nextText = visual.TextStim(myWin,pos=(0, .1),colorSpace='rgb',color = (1,1,1),alignHoriz='center', alignVert='center',height=.1,units='norm',autoLog=autoLogging)
NextRemindCountText = visual.TextStim(myWin,pos=(0,.2),colorSpace='rgb',color= (1,1,1),alignHoriz='center', alignVert='center',height=.1,units='norm',autoLog=autoLogging)
screenshot= False; screenshotDone = False
instructions1 = visual.TextStim(myWin,pos=(0,0),colorSpace='rgb',color= (1,1,1),alignHoriz='center', alignVert='center',height=.5,units='deg',autoLog=autoLogging)
instructions2 = visual.TextStim(myWin,pos=(0,0),colorSpace='rgb',color= (1,1,1),alignHoriz='center', alignVert='center',height=.5,units='deg',autoLog=autoLogging)
instructionText1 = """
This experiment is made up of several trials. On each trial you will fixate your eyes on a central point on the screen. Then several rapid, randomly-ordered sequences of letters will appear at two or 18 locations on the screen. You must not move your eyes from the fixation point while these sequences are playing.
One of the letters will appear with a white ring around it. Your job is to tell us which of the letters appeared within the white ring. Again, you must not move your eyes from the fixation point in the centre of the screen while the letter streams are shown.
Press space to read more instructions
"""
instructionText2 ="""
At the end of each trial you will see a screen in which the whole alphabet is displayed. You should click on the letter that you saw within the circle.
Please tell the experimenter you have read the instructions. Be sure to ask him if you have any questions.
"""
instructions1.text = instructionText1
instructions2.text = instructionText2
### Near the rest of the instruction stimuli ###
startTrialStimuli = visual.TextStim(myWin,pos=(0,0),colorSpace='rgb',color= (1,1,1),alignHoriz='center', alignVert='center',height=.5,units='deg',autoLog=autoLogging)
startTrialStimuli.text = 'Click here to start the trial'
startTrialBox = visual.Rect(myWin, height = .75, width = 6, units = 'deg', lineColor = bgColor, lineColorSpace = 'rgb')
######################################
### Buffered image size dimensions ###
######################################
'''
Element array stim textures must be a power of two. I'm doing this here to keep it out of the doRSVPstim loop
'''
new_size = max( [
int(np.power(2, np.ceil(np.log(dim_size) / np.log(2))))
for dim_size in (screenValues['heightPix'], screenValues['widthPix'])
]
)
pad_amounts = []
for dim in ['heightPix','widthPix']:
first_offset = int((new_size - screenValues[dim]) / 2.0)
second_offset = new_size - screenValues[dim] - first_offset
pad_amounts.append([first_offset, second_offset])
###########################################################
### Mask covering only fixation so fixation can flicker ###
###########################################################
mask = np.full((screenValues['widthPix'],screenValues['heightPix']), 1, int)
mask = np.pad(
array = mask,
pad_width=pad_amounts,
mode="constant",
constant_values= 1
)
centreX, centreY = [int(dimension/2) for dimension in mask.shape]
mask[centreX-fixSizePix/2:centreX+fixSizePix/2, centreY-fixSizePix/2:centreY+fixSizePix/2] = -(np.round( np.random.rand(fixSizePix,fixSizePix) ,0 ) *2.0-1)
######################################
####### SETTING THE CONDITIONS #######
######################################
#For the optional attentional blink
#For the dual-stream simultaneous target
stimList=[]
possibleCueTemporalPositions = np.array([6,7,8,9,10]) #debugAH np.array([6,7,8,9,10])
tasks=['T1','T1T2','allCued','oneCued','nStreams']
numResponsesWanted=1; maxNumRespsWanted=1
streamsPerRing = 6
pairAngles = range(0,180,int(360/streamsPerRing)) #angles of possible cued streams
nStreamsPossibilities = [2,6,18] #np.arange(2,21,3) #this needs to be listed here so when print header can work out the maximum value
rings = range(int(np.ceil(float(max(nStreamsPossibilities))/streamsPerRing)))
for nStreams in nStreamsPossibilities:
for cueTemporalPos in possibleCueTemporalPositions: #5 or 6 temporal serial positions for the cue
for ring in rings:
for pairAngle in pairAngles:
for whichInPair in [0,1]:
halfAngle = 360/float(streamsPerRing)/2.0
thisRingAngleOffset = (ring % 2) * halfAngle #offset odd-numbered rings by half the angle
whichStreamCuedAngle = (pairAngle + whichInPair * 180) + thisRingAngleOffset #either the stream at pairangle or the one opposite
print(whichStreamCuedAngle)
stimList.append(
{'nStreams':nStreams,
'cue0temporalPos':cueTemporalPos,
'pairAngle':pairAngle, 'ring':ring, 'whichStreamCuedAngle' : whichStreamCuedAngle, 'whichInPair':whichInPair}
)
trialsPerCondition = 2
trials = data.TrialHandler(stimList,trialsPerCondition) #constant stimuli method
print('There are ' + str(trials.nTotal) + ' trials.')
#################################################################
###Print cued stream to check calculations ###
###This iterates the trials object, so the main loop won't run###
#################################################################
testCuedStreams = False
testCuedStreamsResults = {
'2' : [0]*2,
'6' : [0]*6,
'18' : [0]*18
}
if testCuedStreams:
n = 0
while n < trials.nTotal:
trial = trials.next()
nStreams = trial['nStreams']
#print(nStreams)
ring = trial['ring']
thisRingAngleOffset = (ring % 2) * halfAngle
whichStreamCuedAngle = trial['whichStreamCuedAngle']
#print('whichStreamCuedAngle = ' + str(whichStreamCuedAngle))
cuedFrame = trial['cue0temporalPos']
if nStreams > streamsPerRing:
#print(ring*streamsPerRing)
cuedStream = (ring*streamsPerRing) + ((whichStreamCuedAngle-thisRingAngleOffset) / (360/streamsPerRing))
cuedStream = int(cuedStream)
elif nStreams == streamsPerRing:
cuedStream= ((whichStreamCuedAngle-thisRingAngleOffset) / (360/streamsPerRing))
cuedStream = int(cuedStream)
elif nStreams == 2:
cuedStream = trial['whichInPair']
#print('nStreams =' + str(nStreams))
#print('cuedStream =' + str(cuedStream))
testCuedStreamsResults[str(nStreams)][cuedStream] += 1
n+=1
myWin.close()
print(testCuedStreamsResults)
logging.info( ' each trialDurFrames='+str(trialDurFrames)+' or '+str(trialDurFrames*(1000./refreshRate))+ \
' ms' )
print('Trials computed')
####################################
#### Print header for data file ####
####################################
print('experimentPhase\ttrialnum\tsubject\t',file=dataFile,end='')
print('nStreams\t',end='',file=dataFile)
print('ring\t',end='',file=dataFile)
printInOrderOfResponses = True
assert (printInOrderOfResponses==True), "Sorry, feature not supported"
if printInOrderOfResponses:
for i in range(maxNumRespsWanted):
dataFile.write('resp'+str(i)+'\t') #have to use write to avoid ' ' between successive text, at least until Python 3
dataFile.write('button'+str(i)+'\t') #have to use write to avoid ' ' between successive text, at least until Python 3
dataFile.write('cuePos'+str(i)+'\t')
dataFile.write('answer'+str(i)+'\t') #have to use write to avoid ' ' between successive text, at least until Python 3
dataFile.write('correct'+str(i)+'\t') #have to use write to avoid ' ' between successive text, at least until Python 3
dataFile.write('pairAngle'+str(i)+'\t')
dataFile.write('whichStreamCuedAngle'+str(i)+'\t')
dataFile.write('whichRespCue'+str(i)+'\t') #have to use write to avoid ' ' between successive text, at least until Python 3
dataFile.write('whichInPair'+str(i)+'\t')
dataFile.write('cuedStream'+str(i)+'\t')
dataFile.write('responsePosRelative'+str(i)+'\t')
for i in xrange(max(nStreamsPossibilities)):
dataFile.write('streamLtrSequence'+str(i)+'\t')#have to use write to avoid ' ' between successive text, at least until Python 3
print('timingBlips',file=dataFile)
#end of header
################################
#### Cue and Stream Stimuli ####
################################
for cueN in xrange(maxNumRespsWanted):
if cueType == 'exogenousRing':
cue = visual.Circle(myWin,
radius=cueRadius,#Martini used circles with diameter of 12 deg
lineColorSpace = 'rgb',
lineColor=letterColor,
lineWidth=2.0, #in pixels
units = 'deg',
fillColorSpace = 'rgb',
fillColor=None, #beware, with convex shapes fill colors don't work
pos= [-5,-5], #the anchor (rotation and vertices are position with respect to this)
interpolate=True,
autoLog=False)#this stim changes too much for autologging to be useful
elif cueType =='endogenous': #tiny dot at fixation point
cue = visual.Circle(myWin,
units='pix',
radius=1, #4
fillColorSpace='rgb',
fillColor=bgColor,
lineWidth=0,
interpolate=False,
autoLog = False)
#In each stream, predraw all 26 letters
ltrHeight = .9 #This is the cortically-scaled height at 3 degrees of eccentricity. This is what we used in 2vs8
cueOffsets = [3,7,11.5]
maxStreams = max(nStreamsPossibilities)
lettersToExclude = ['C','W'] #lets exclude these
potentialLetters = [letter for letter in string.ascii_uppercase if letter not in lettersToExclude]
streamTextObjects = list() #A text object for every stream. I'll update the text for each frame
nextLargestMultiple = int(np.ceil(float(maxStreams)/streamsPerRing)*streamsPerRing) #The next largest multiple of streamsPerRing after maxStreams
print('nextLargestMultiple is ' + str(nextLargestMultiple))
for stream in xrange(nextLargestMultiple):
thisStream = list()
for letter in potentialLetters:
streamText = visual.TextStim(
myWin,
pos=(0,0),
colorSpace='rgb',
font = font,
color=letterColor,
alignHoriz='center',
alignVert='center',
units='deg',
text = letter,
autoLog=autoLogging)
'''
Because each stream has a different angular offset from the x axis, I can't use set pos here.
In order to scale the object according to magnification - which I'm doing here to save doing it on every trial -
I need to fake the position of the object so that it's the same eccentricity as it will be later on
Objects will never been drawn in the position I'm about to calculate. It's just here for the cortical magnification function
'''
ringOfThisStim = int(stream/streamsPerRing)
x = 0
y = cueOffsets[ringOfThisStim] #We're using cue offsets to set the position of the rings
streamText.pos = (x,y)
streamText = corticalMagnification.corticalMagnification( #Scale the height (and thus the width for a monospaced font like Sloan) based on cortical magnification estimate. This function returns the stimulus, not its size
stimulus = streamText,
ltrHeight = ltrHeight,
cue = False)
#print('For stream %(streamN)d and letter %(letter)s the height is %(height)s' % {'streamN':stream, 'letter':letter, 'height':streamText.height})
thisStream.append(streamText)
streamTextObjects.append( thisStream )
streamTextObjects = np.array(streamTextObjects)
print('streamTextObjects shape')
print(streamTextObjects.shape)
#All noise dot coordinates ultimately in pixels, so can specify each dot is one pixel
noiseFieldWidthDeg=ltrHeight *0.9 #1.0 makes noise sometimes intrude into circle
noiseFieldWidthPix = int( round( noiseFieldWidthDeg*pixelperdegree ) )
###############
#### Mouse ####
###############
myMouse = event.Mouse()
##############
### Sounds ###
##############
clickSound, badKeySound = stringResponse.setupSoundsForResponse()
###########
###Clock###
###########
trialClock = core.Clock()
################
###Eyetracker###
################
if eyetracking:
if getEyeTrackingFileFromEyetrackingMachineAtEndOfExperiment:
eyeMoveFile=('EyeTrack_'+subject+'_'+timeAndDateStr+'.EDF')
tracker=Tracker_EyeLink(myWin,trialClock,subject,1, 'HV5',(255,255,255),(0,0,0),False,(screenValues['widthPix'],screenValues['heightPix']))
#################
### Functions ###
#################
def calcStreamPos(trial,cueOffsets,streami,streamOrNoise):
'''
Calculates the xy position of the stream in degrees of visual angle relative to the centre of the screen
'''
#streamOrNoise because noise coordinates have to be in deg, stream in pix
#cueOffsets are in deg, for instance indicating the eccentricity of the streams/cues
nStreams = trial['nStreams']
pairAngle = trial['pairAngle']
ring = trial['ring']
noiseOffsetKludge = 0.9 #Because the noise coords were drawn in pixels but the cue position is specified in deg, I must convert pix to deg for noise case
thisRingNum = int(streami / streamsPerRing)
ringStreami = streami % streamsPerRing
if nStreams - streamsPerRing * thisRingNum >= streamsPerRing:
streamsThisRing = streamsPerRing
else:
streamsThisRing = nStreams - streamsPerRing * thisRingNum
if nStreams <= streamsPerRing:
thisRingNum = ring #innermost ring is now the outermost ring
else:
pairAngle = 0 #if 18 streams, no need for angular offset
#print('streams this ring: ',streamsThisRing)
if nStreams ==0:
pos = np.array([0,0])
else:
#assume want them evenly spaced, counterclockwise starting from directly east
halfAngle = 360/float(streamsThisRing)/2.0
thisRingAngleOffset = (thisRingNum % 2) * halfAngle #offset odd-numbered rings by half the angle
thisAngle = pairAngle + ringStreami/streamsThisRing * 360 + thisRingAngleOffset
x = cueOffsets[thisRingNum]*cos(thisAngle/180*pi)
y = cueOffsets[thisRingNum]*sin(thisAngle/180*pi)
pos = np.array([x,y])
if streamOrNoise: #Because the noise coords were drawn in pixels but the cue position is specified in deg, I must convert pix to deg
pos *= noiseOffsetKludge*pixelperdegree
#pos = np.round(pos) #rounding or integer is a bad idea. Then for small radii, not equally spaced
#pos = pos.astype(int)
return pos
def checkTiming(ts):
interframeIntervals = np.diff(ts) * 1000
#print(interframeIntervals)
frameTimeTolerance=.3 #proportion longer than refreshRate that will not count as a miss
longFrameLimit = np.round(1000/refreshRate*(1.0+frameTimeTolerance),2)
idxsInterframeLong = np.where( interframeIntervals > longFrameLimit ) [0] #frames that exceeded 150% of expected duration
numCasesInterframeLong = len( idxsInterframeLong )
if numCasesInterframeLong > 0:
print(numCasesInterframeLong,'frames of', trialDurFrames,'were longer than',str(1000/refreshRate*(1.0+frameTimeTolerance)))
return numCasesInterframeLong
def oneFrameOfStim(n, frameStimuli):
thisFrameN = int(n/SOAFrames) #Which object in frameStimuli do we select?
drawFrame = (n % SOAFrames) < letterDurFrames
thisFrame = frameStimuli[thisFrameN]
if drawFrame:
if n % 2 != 0:
thisFrame[1].draw() #dimmed fixation
else:
thisFrame[0].draw() #Normal fixation
else:
if n % 2 == 0:
fixatn.draw()
else:
fixatnCounterphase.draw()
return True
def doRSVPStim(trial):
'''
ONLY DOES ONE CUE AT THE MOMENT
This function generates the stimuli for each trial. The word "frame" here refers to a set of simultaneous RSVP stimuli. I'll use "refresh" to refer to monitor refreshes.
Using the parameters for the current trial:
- Work out the temporal position of the cue(s)
- Shuffle the order of the letters
- Calculate the position and cortically-magnified size for each stream
- Capture each stream's pixels on each frame using bufferImageStim
- Collate each frame's pixels so that all stimuli are represented by the same matrix of pixels. Put the cue in there if it's the cued frame
- pass the matrix of pixels to elementarraystim
'''
global cue
global tracker
nStreams = trial['nStreams']
print(nStreams)
ring = trial['ring']
thisRingAngleOffset = (ring % 2) * halfAngle
whichStreamCuedAngle = trial['whichStreamCuedAngle']
cuedFrame = trial['cue0temporalPos']
if nStreams > streamsPerRing:
#print(ring*streamsPerRing)
cuedStream = (ring*streamsPerRing) + ((whichStreamCuedAngle-thisRingAngleOffset) / (360/streamsPerRing))
cuedStream = int(cuedStream)
elif nStreams == streamsPerRing:
cuedStream= ((whichStreamCuedAngle-thisRingAngleOffset) / (360/streamsPerRing))
cuedStream = int(cuedStream)
elif nStreams == 2:
cuedStream = trial['whichInPair']
print('cueFrame = ' + str(cuedFrame))
print('cuedStream = ' + str(cuedStream))
print('whichinPair = ' + str(trial['whichInPair']))
streamPositions = list() #Might need to pass this to elementArrayStim as xys. Might not though
streamLetterIdxs = np.empty( #use an array so I can select columns (frames) for buffering. This is an empty array that will eventually have the
#letter indexes for each stream. Selecting a column gives us all streams at a particular frame. Selecting a row gives us
#all frames for a particular stream
shape = (nStreams,numLettersToPresent),
dtype = int
)
streamLetterIdentities = np.empty( #Letter identities for these streams
shape = (nStreams,numLettersToPresent),
dtype = str
)
for thisStream in xrange(nStreams):
thisSequence = np.arange(24)
np.random.shuffle(thisSequence)
theseIdentities = [potentialLetters[idx] for idx in thisSequence]
streamLetterIdxs[thisStream,:] = thisSequence
streamLetterIdentities[thisStream,:] = theseIdentities
#print('For stream %(streamN)d the letters are: %(theseLetters)s' % {'streamN':thisStream, 'theseLetters':''.join(theseIdentities)})
#print(streamLetterIdentities)
correctIdx = streamLetterIdxs[cuedStream,cuedFrame]
print('correctIdx')
print(correctIdx)
correctLetter = alphabetHelpers.numberToLetter(correctIdx, potentialLetters) #potentialLetters is global
frameStimuli = list() #A list of elementArrayStim objects, each represents a frame. Drawing one of these objects will draw the letters and the cue for that frame
for thisFrame in xrange(numLettersToPresent):
theseStimuli = streamLetterIdxs[:,thisFrame] #The alphabetical indexes of stimuli to be shown on this frame
### IN DO RSVP STIM ###
stimuliToDraw = list() #Can pass a list to bufferimageStim!
stimuliToDraw.append(fixatn)
stimuliToDrawCounterPhase = list()
stimuliToDrawCounterPhase.append(fixatnCounterphase)
for thisStream in xrange(nStreams):
thisPos = calcStreamPos(
trial = trial,
cueOffsets = cueOffsets,
streami = thisStream,
streamOrNoise = False
)
cueThisFrame = thisStream == cuedStream and thisFrame == cuedFrame #If true, draw the cue and capture that too
thisLetterIdx = theseStimuli[thisStream] #The letter index for this particular stream on this particular frame
if nStreams <= streamsPerRing and max(nStreamsPossibilities)>2:
#print('Stream was' + str(thisStream) +', but is now' + str(trial['ring']*streamsPerRing+thisStream))
thisStreamStimulus = streamTextObjects[trial['ring']*streamsPerRing+thisStream, thisLetterIdx]
else:
thisStreamStimulus = streamTextObjects[thisStream, thisLetterIdx]
thisStreamStimulus.pos = thisPos
#print('For stream %(thisStream)d the height is: %(letterHeight)s' % {'thisStream':thisStream, 'letterHeight':thisStreamStimulus.height})
stimuliToDraw.append(thisStreamStimulus)
stimuliToDrawCounterPhase.append(thisStreamStimulus)
if cueThisFrame and cueType == 'exogenousRing':
cue.setPos( thisPos )
cue = corticalMagnification.corticalMagnification(cue, 0.9810000000000002, cue = True) #this is the cuesize from the original experiment
stimuliToDraw.append(cue)
stimuliToDrawCounterPhase.append(cue)
buff = visual.BufferImageStim( #Buffer these stimuli
win = myWin,
stim = stimuliToDraw
)
buff = np.flipud(np.array(buff.image)[..., 0]) / 255.0 * 2.0 - 1.0 #Via djmannion. This converts the pixel values from [0,255] to [-1,1]. I think 0 is middle grey. I'll need to change this to match the background colour eventually
buff = np.pad(
array=buff,
pad_width=pad_amounts, #See 'Buffered image size dimensions' section
mode="constant",
constant_values=0.0
)
thisFrameStimuli = visual.ElementArrayStim( #A stimulus representing this frame with the fixation at full luminance
win = myWin,
units = 'pix',
nElements=1,
xys = [[0,0]],
sizes=buff.shape,
elementTex=buff,
elementMask = 'none'
)
buff = visual.BufferImageStim( #Buffer these stimuli
win = myWin,
stim = stimuliToDrawCounterPhase
)
buff = np.flipud(np.array(buff.image)[..., 0]) / 255.0 * 2.0 - 1.0 #Via djmannion. This converts the pixel values from [0,255] to [-1,1]. I think 0 is middle grey. I'll need to change this to match the background colour eventually
buff = np.pad(
array=buff,
pad_width=pad_amounts, #See 'Buffered image size dimensions' section
mode="constant",
constant_values=0.0
)
thisFrameStimuliCounterPhase = visual.ElementArrayStim( #A stimulus representing this frame with the fixation phase reversed
win = myWin,
units = 'pix',
nElements=1,
xys = [[0,0]],
sizes=buff.shape,
elementTex=buff,
elementMask = 'none'
)
frameStimuli.append([thisFrameStimuli, thisFrameStimuliCounterPhase])
ts = []
waiting = True
myMouse.setVisible(waiting)
if nDone is not 0:
if trials.nTotal/float(nDone) in [1.,1.5,2.,3.]:
startTrialStimuli.text = startTrialStimuli.text + '\n\rThis is trial ' + str(nDone) + ' of ' + str(trials.nTotal)
else:
startTrialStimuli.text = 'Click here to start the trial'
while waiting and not autopilot:
startTrialStimuli.draw()
startTrialBox.draw()
myWin.flip()
if myMouse.isPressedIn(startTrialBox):
waiting = False
myMouse.setVisible(waiting)
if eyetracking:
tracker.startEyeTracking(nDone,True,widthPix,heightPix) #start recording with eyetracker
ts = []
myWin.flip(); myWin.flip()#Make sure raster at top of screen (unless not in blocking mode), and give CPU a chance to finish other tasks
fixatn.draw()
myWin.flip()
core.wait(1)
t0 = trialClock.getTime()
for n in xrange(trialDurFrames):
oneFrameOfStim(n, frameStimuli)
myWin.flip()
ts.append(trialClock.getTime() - t0)
if eyetracking:
tracker.stopEyeTracking()
print('stopped tracking')
return streamLetterIdxs, streamLetterIdentities, correctLetter, ts, cuedStream, cuedFrame
instructions1.draw()
myWin.flip()
waiting = True
while waiting:
for key in event.getKeys(): #check if pressed abort-type key
if key in ['space','ESCAPE']:
waiting=False
if key in ['ESCAPE']:
expStop = True
instructions2.draw()
myWin.flip()
waiting = True
while waiting:
for key in event.getKeys():
if key in ['m', 'ESCAPE']:
waiting = False
if key in ['Escape']:
expStop = True
myWin.flip()
allBlips = list()
expStop = False #If True, end experiment
nDone = 0 #Which trial?
while nDone < trials.nTotal and not expStop:
trial = trials.next()
showBothSides = False #Need to modify this if doing 2 streams only, that way we can replicate the G&H lineups
sideFirstLeftRightCentral=2 #default , respond to central. Charlie: I guess we need this to replicate other experiments
streamLetterIdxs, streamLetterIdentities, correctLetter, ts, cuedStream, cuePos = doRSVPStim(trial)
myMouse.setVisible(True)
if not autopilot:
expStop,passThisTrial,responses,buttons,responsesAutopilot = \
letterLineupResponse.doLineup( #doLineup(myWin,bgColor,myMouse,clickSound,badClickSound,possibleResps,bothSides,leftRightCentral,autopilot):
myWin,
bgColor,
myMouse,
clickSound,
badKeySound,
potentialLetters,
showBothSides,
sideFirstLeftRightCentral,
autopilot
)
myWin.flip()
else:
responses = ['Z']
buttons = [0]
accuracy = responses[0] == correctLetter
#print('printing accuracy vars')
#print(streamLetterIdentities[cuedStream,:])
#print(responses[0])
#print(np.where(streamLetterIdentities[cuedStream,:]==responses[0])[0][0])
responseLetterIdx = np.where(streamLetterIdentities[cuedStream,:]==responses[0]) #need index on where because it treats sliced streamLetterIdentities as a ndarray
if len(responseLetterIdx) is not 0:
responseLetterIdx = responseLetterIdx[0]
SPE = responseLetterIdx[0] - cuePos
print('SPE ' + str(SPE))
else:
responseLetterIdx = 999
SPE = 999
cuedStreamPos = calcStreamPos(trial, cueOffsets, cuedStream, False)
print(cuedStreamPos)
cuedStreamAngle = atan2(cuedStreamPos[1],cuedStreamPos[0])*(180/np.pi)
print(cuedStreamAngle)
timingBlips = checkTiming(ts)
allBlips.append(timingBlips)
dataFile.write(
'main\t' +
str(nDone) + '\t' +
subject + '\t' +
str(trial['nStreams']) + '\t' +
str(trial['ring']) + '\t' +
responses[0] + '\t' +
str(buttons[0]) + '\t' +
str(cuePos) + '\t' +
correctLetter + '\t' +
str(accuracy) + '\t' +
str(trial['pairAngle']) + '\t' +
str(trial['whichStreamCuedAngle']) + '\t' +
'0' + '\t' +
str(trial['whichInPair'])+'\t'+
str(cuedStream)+'\t'+
str(SPE) + '\t'
)
for stream in xrange(max(nStreamsPossibilities)):
if stream < trial['nStreams']:
dataFile.write(''.join(streamLetterIdentities[stream,:]) + '\t')
else:
dataFile.write('999\t')
dataFile.write(str(timingBlips) + '\n')
dataFile.flush()
nDone += 1
dataFile.flush()
dataFile.close()
myWin.close()