-
Notifications
You must be signed in to change notification settings - Fork 0
/
maskedAIContrast_lum.m
executable file
·1268 lines (1158 loc) · 43.7 KB
/
maskedAIContrast_lum.m
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function maskedAIContrast(ana)
%----------compatibility for windows
%if ispc; PsychJavaTrouble(); end
KbName('UnifyKeyNames');
%===================Initiate out metadata===================
ana.date = datestr(datetime);
ana.version = Screen('Version');
ana.computer = Screen('Computer');
%===================experiment parameters===================
if ana.debug
ana.screenID = 0;
else
ana.screenID = max(Screen('Screens'));%-1;
end
%===================Make a name for this run===================
cd(ana.ResultDir)
if ~isempty(ana.subject)
if ana.useStaircase; type = 'AISTAIR'; else; type = 'AIMOC'; end %#ok<*UNRCH>
nameExp = [type '_' ana.subject];
c = sprintf(' %i',fix(clock()));
nameExp = [nameExp c];
ana.nameExp = regexprep(nameExp,' ','_');
else
ana.nameExp = 'debug';
end
cla(ana.plotAxis1);
cla(ana.plotAxis2);
cla(ana.plotAxis3);
useEyeLink = ana.useEyelink;
nBlocks = ana.nBlocks;
nBlocksOverall = nBlocks * length(ana.pedestalRange);
if ana.useStaircase
pedestalBlack = ana.pedestalRange;
pedestalBlackLinear = pedestalBlack;
pedestalWhite = ana.pedestalRange;
pedestalWhiteLinear = pedestalWhite;
else
pedestalBlack = 0.5 - fliplr(ana.pedestalRange);
pedestalBlackLinear = pedestalBlack;
pedestalWhite = 0.5 + ana.pedestalRange;
pedestalWhiteLinear = pedestalWhite;
end
%-------------------response values, linked to left, up, down
NOSEE = 1; YESBRIGHT = 2; YESDARK = 3; UNSURE = 4; BREAKFIX = -1;
%-----------------------Positions to move stimuli
XPos = [3 1.5 -1.5 -1.5 1.5 -3] * 4 / 3;
YPos = [0 2.598 2.598 -2.598 -2.598 0] * 4 / 3;
saveMetaData();
%======================================================stimulus objects
%---------------------main disc (stimulus and pedestal).
st = discStimulus();
st.name = ['STIM_' ana.nameExp];
st.xPosition = XPos(1);
st.colour = [1 1 1 1];
st.size = ana.discSize;
st.sigma = ana.sigma;
%-----mask stimulus
m = dotsStimulus();
m.mask = true;
m.density = 1000;
m.coherence = 0;
m.size = st.size+1;
m.speed=0.5;
m.name = ['MASK_' ana.nameExp];
m.xPosition = st.xPosition;
m.size = st.size;
%----------combine them into a single meta stimulus------------------
stimuli = metaStimulus();
stimuli.name = ana.nameExp;
sidx = 1;
maskidx = 1;
stimuli{sidx} = st;
stimuli.maskStimuli{maskidx} = m;
stimuli.showMask = false;
%======================================================stimulus objects
%-----------------------open the PTB screens------------------------
PsychDefaultSetup(2);
Screen('Preference', 'SkipSyncTests', 0);
%===================open our screen====================
sM = screenManager();
sM.screen = ana.screenID;
sM.windowed = ana.windowed;
sM.pixelsPerCm = ana.pixelsPerCm;
sM.distance = ana.distance;
sM.debug = ana.debug;
sM.blend = true;
sM.bitDepth = 'FloatingPoint32Bit';
if exist(ana.gammaTable, 'file')
load(ana.gammaTable);
if isa(c,'calibrateLuminance')
sM.gammaTable = c;
end
clear c;
if ana.debug
sM.gammaTable.plot
end
end
sM.backgroundColour = ana.backgroundColor;
screenVals = sM.open; % OPEN THE SCREEN
fprintf('\n--->>> AIContrast Opened Screen %i : %s\n', sM.win, sM.fullName);
setup(stimuli,sM); %setup our stimulus object
%==============================setup eyelink==========================
if useEyeLink == true
ana.strictFixation = true;
eL = eyelinkManager('IP',[]);
fprintf('--->>> eL setup starting: %s\n', eL.fullName);
eL.isDummy = ana.isDummy; %use dummy or real eyelink?
eL.name = ana.nameExp;
eL.saveFile = [ana.nameExp '.edf'];
eL.recordData = true; %save EDF file
eL.sampleRate = ana.sampleRate;
eL.remoteCalibration = false; % manual calibration?
eL.calibrationStyle = ana.calibrationStyle; % calibration style
eL.modify.calibrationtargetcolour = [1 1 1];
eL.modify.calibrationtargetsize = 1;
eL.modify.calibrationtargetwidth = 0.05;
eL.modify.waitformodereadytime = 500;
eL.modify.devicenumber = -1; % -1 = use any keyboard
% X, Y, FixInitTime, FixTime, Radius, StrictFix
updateFixationValues(eL, ana.fixX, ana.fixY, ana.firstFixInit,...
ana.firstFixTime, ana.firstFixDiameter, ana.strictFixation);
%sM.verbose = true; eL.verbose = true; sM.verbosityLevel = 10; eL.verbosityLevel = 4; %force lots of log output
initialise(eL, sM); %use sM to pass screen values to eyelink
setup(eL); % do setup and calibration
fprintf('--->>> eL setup complete: %s\n', eL.fullName);
WaitSecs('YieldSecs',0.5);
getSample(eL); %make sure everything is in memory etc.
end
%---------------------------Set up task variables----------------------
task = stimulusSequence();
task.name = ana.nameExp;
task.nBlocks = nBlocksOverall;
task.nVar(1).name = 'colour';
task.nVar(1).stimulus = 1;
task.nVar(1).values = [0 1];
randomiseStimuli(task);
initialiseTask(task);
if ana.useStaircase == false
staircaseB = []; staircaseW = [];
taskW = stimulusSequence();
taskW.name = ana.nameExp;
taskW.nBlocks = nBlocks;
taskW.nVar(1).name = 'pedestalWhite';
taskW.nVar(1).stimulus = 1;
taskW.nVar(1).values = pedestalWhite;
randomiseStimuli(taskW);
initialiseTask(taskW);
taskB = stimulusSequence();
taskB.name = ana.nameExp;
taskB.nBlocks = nBlocks;
taskB.nVar(1).name = 'pedestalBlack';
taskB.nVar(1).stimulus = 1;
taskB.nVar(1).values = pedestalBlack;
randomiseStimuli(taskB);
initialiseTask(taskB);
else
taskB.thisRun = 0; taskW.thisRun = 0;
stopRule = 40;
usePriors = ana.usePriors;
grain = 100;
setupStairCase();
end
%=====================================================================
try %our main experimental try catch loop
%=====================================================================
loop = 1;
posloop = 1;
breakloop = false;
fixated = 'no';
response = NaN;
responseRedo = 0; %number of trials the subject was unsure and redid (left arrow)
while ~breakloop && task.thisRun <= task.nRuns
%-----setup our values and print some info for the trial
hide(stimuli);
response = NaN;
stimuli.showMask = false;
colourOut = task.outValues{task.thisRun,1};
stimuli{1}.colourOut = colourOut;
if ana.useStaircase == true
if colourOut == 0
pedestal = staircaseB.xCurrent;
else
pedestal = staircaseW.xCurrent;
end
else
if colourOut == 0
pedestal = taskB.outValues{taskB.thisRun,1};
else
pedestal = taskW.outValues{taskW.thisRun,1};
end
end
if posloop > 6; posloop = 1; end
stimuli{1}.xPositionOut = XPos(posloop);
stimuli{1}.yPositionOut = YPos(posloop);
stimuli.maskStimuli{1}.xPositionOut = XPos(posloop);
stimuli.maskStimuli{1}.yPositionOut = YPos(posloop);
ts.x = XPos(posloop);
ts.y = YPos(posloop);
ts.size = stimuli{1}.size;
ts.selected = true;
%save([tempdir filesep nameExp '.mat'],'task','taskB','taskW');
fprintf('\n==>># %i: Pedestal = %.3g | Colour = %.3g | ',task.thisRun,pedestal,colourOut);
Priority(MaxPriority(sM.win));
posloop = posloop + 1;
stimuli.update();
stimuli.maskStimuli{1}.update();
%-----initialise eyelink and draw fix spot
if useEyeLink
resetFixation(eL);
trackerClearScreen(eL);
trackerDrawStimuli(eL,ts);
trackerDrawFixation(eL); %draw fixation window on eyelink computer
edfMessage(eL,'V_RT MESSAGE END_FIX END_RT'); ... %this 3 lines set the trial info for the eyelink
edfMessage(eL,['TRIALID ' num2str(task.thisRun)]); ... %obj.getTaskIndex gives us which trial we're at
edfMessage(eL,['MSG:PEDESTAL ' num2str(pedestal)]); ... %add in the pedestal of the current state for good measure
edfMessage(eL,['MSG:CONTRAST ' num2str(colourOut)]); ... %add in the pedestal of the current state for good measure
startRecording(eL);
statusMessage(eL,'INITIATE FIXATION...');
fixated = '';
syncTime(eL);
while ~strcmpi(fixated,'fix') && ~strcmpi(fixated,'breakfix')
drawCross(sM,0.4,[1 1 1 1],ana.fixX,ana.fixY);
Screen('DrawingFinished', sM.win); %tell PTB/GPU to draw
tFix = Screen('Flip',sM.win); %flip the buffer
getSample(eL);
fixated=testSearchHoldFixation(eL,'fix','breakfix');
end
if strcmpi(fixated,'breakfix')
fprintf(' BREAK INIT FIXATION');
response = BREAKFIX;
end
else
drawCross(sM,0.4,[1 1 1 1],ana.fixX,ana.fixY);try
n = 1;
while n <= trials
if exitLoop; break; end
%%%%%%% initialise eyelink and draw fix spot %%%%%%%
if useEyeLink
resetFixation(eL);
trackerClearScreen(eL);
% trackerDrawStimuli(eL,ts);
trackerDrawFixation(eL); %draw fixation window on eyelink computer
edfMessage(eL,'V_RT MESSAGE END_FIX END_RT'); ... %this 3 lines set the trial info for the eyelink
edfMessage(eL,['TRIALID ' num2str(n)]); ... %obj.getTaskIndex gives us which trial we're at
startRecording(eL);
statusMessage(eL,'INITIATE FIXATION...');
fixated = '';
syncTime(eL);
while ~strcmpi(fixated,'fix') && ~strcmpi(fixated,'breakfix')
drawCross(sM,0.4,[1 1 1 1],ana.fixX,ana.fixY);
Screen('DrawingFinished', sM.win); %tell PTB/GPU to draw
tFix = Screen('Flip',sM.win); %flip the buffer
getSample(eL);
fixated=testSearchHoldFixation(eL,'fix','breakfix');
end
if strcmpi(fixated,'breakfix')
fprintf(' BREAK INIT FIXATION');
response = BREAKFIX;
end
else
drawCross(sM,0.4,[1 1 1 1],ana.fixX,ana.fixY);
tFix = Screen('Flip',sM.win); %flip the buffer
% WaitSecs(0.5);
fixated = 'fix';
end
%%%%%%% draw circle to show the location of stimulus %%%%%%%
Screen('FrameOval', win, 1, [maskDstRectsRand(1, n)+(masktexrect(3)-maskRadiusPix*2)/2-circelSizeWeight1, maskDstRectsRand(2, n)+(masktexrect(4)-maskRadiusPix*2)/2-circelSizeWeight1, ...
maskDstRectsRand(3, n)-(masktexrect(3)-maskRadiusPix*2)/2+circelSizeWeight1, maskDstRectsRand(4, n)-(masktexrect(4)-maskRadiusPix*2)/2+circelSizeWeight1]);
sM.drawCross();
sM.flip();
WaitSecs('YieldSecs', 0.5);
Screen('FillRect', win, 0.5);
sM.drawCross();
sM.flip;
WaitSecs('YieldSecs', 0.5);
fprintf('TRIAL:%i: staircase value = %.2f\n', n, staircase.xCurrent);
%%%%%%% draw fixed phase %%%%%%%
finishLoop = false;
while strcmp(fixated, 'fix') && finishLoop == false
if useEyeLink; edfMessage(eL,'END_FIX'); statusMessage(eL,'Show Stimulus...'); end
currentTime = GetSecs;
nextTime = currentTime + ana.part1_duration;
vbl = currentTime;
while vbl < nextTime
Screen('DrawTextures', win, dotstex, [], dotDstRects, [], [], 1, dotColorMatrix, [], [], mydots);
if ana.foregroundMask
Screen('DrawTextures', win, masktex, [], maskDstRectsRand(:, n), [], [], 1, [0.5, 0.5, 0.5, 1]', [], [], mymask);
end
sM.drawCross();
[x1, y1] = RectCenterd(dotDstRects);
index_kill1 = find(((x1+initialSpeed(1)*cosd(initialDirection))-w)>0);
x1 = mod(x1+initialSpeed(1)*cosd(initialDirection), w);
y1 = mod(y1-initialSpeed(1)*sind(initialDirection), h);
y1(index_kill1) = h*rand(1,length(index_kill1)); %rand y of out_dots
dotDstRects = CenterRectOnPointd(inrect, x1, y1);
if useEyeLink
getSample(eL); %drawEyePosition(eL);
isfix = isFixated(eL);
if ~isfix
fixated = 'breakfix';
break
end
end
vbl = Screen('Flip', win, vbl + halfisi);
end
if useEyeLink && ~strcmpi(fixated,'fix')
response = BREAKFIX; finishLoop = true;
statusMessage(eL,'Subject Broke Fixation!');
edfMessage(eL,'MSG:BreakFix')
break
end
%%%%%%% draw test phase %%%%%%%
dotSpeedPerFrame = ((dotSpeedWeight*staircase.xCurrent)+dotSpeedMean)*pixelPerDeg*ifi; % pixel/frame
%mae_dir(n)= ana.dotDirection(dotDirectionMatrix(n));
%=== Negative is left == 1 | Positive is right == 2
MAEDirection(n) = (dotSpeedPerFrame >= 0) + 1;
MAESpeed(n) = dotSpeedPerFrame/(pixelPerDeg*ifi);
fprintf('==Dot Speed is %.3f (%.3f), direction is %.2f\n',dotSpeedPerFrame,MAESpeed(n),MAEDirection(n));
currentTime = GetSecs;
nextTime = currentTime + ana.stimulusDuration;
vbl = currentTime;
while vbl < nextTime
Screen('DrawTextures', win, dotstex, [], dotDstRects, [], [], 1, dotColorMatrix, [], [], mydots);
if ana.foregroundMask
Screen('DrawTextures', win, masktex, [], maskDstRectsRand(:, n), [], [], 1, [0.5, 0.5, 0.5, 1]', [], [], mymask);
end
sM.drawCross();
[x, y] = RectCenterd(dotDstRects);
index_kill2 = find((x+dotSpeedPerFrame*cosd(0)-w)>0);
x = mod(x+dotSpeedPerFrame*cosd(0),w);
y = mod(y-dotSpeedPerFrame*sind(0),h);
y(index_kill2) = h*rand(1,length(index_kill2)); %rand y of out_dots
dotDstRects = CenterRectOnPointd(inrect, x, y);
if useEyeLink
getSample(eL); %drawEyePosition(eL);
isfix = isFixated(eL);
if ~isfix
fixated = 'breakfix';
break
endtry
n = 1;
while n <= trials
if exitLoop; break; end
%%%%%%% initialise eyelink and draw fix spot %%%%%%%
if useEyeLink
resetFixation(eL);
trackerClearScreen(eL);
% trackerDrawStimuli(eL,ts);
trackerDrawFixation(eL); %draw fixation window on eyelink computer
edfMessage(eL,'V_RT MESSAGE END_FIX END_RT'); ... %this 3 lines set the trial info for the eyelink
edfMessage(eL,['TRIALID ' num2str(n)]); ... %obj.getTaskIndex gives us which trial we're at
startRecording(eL);
statusMessage(eL,'INITIATE FIXATION...');
fixated = '';
syncTime(eL);
while ~strcmpi(fixated,'fix') && ~strcmpi(fixated,'breakfix')
drawCross(sM,0.4,[1 1 1 1],ana.fixX,ana.fixY);
Screen('DrawingFinished', sM.win); %tell PTB/GPU to draw
tFix = Screen('Flip',sM.win); %flip the buffer
getSample(eL);
fixated=testSearchHoldFixation(eL,'fix','breakfix');
end
if strcmpi(fixated,'breakfix')
fprintf(' BREAK INIT FIXATION');
response = BREAKFIX;
end
else
drawCross(sM,0.4,[1 1 1 1],ana.fixX,ana.fixY);
tFix = Screen('Flip',sM.win); %flip the buffer
% WaitSecs(0.5);
fixated = 'fix';
end
%%%%%%% draw circle to show the location of stimulus %%%%%%%
Screen('FrameOval', win, 1, [maskDstRectsRand(1, n)+(masktexrect(3)-maskRadiusPix*2)/2-circelSizeWeight1, maskDstRectsRand(2, n)+(masktexrect(4)-maskRadiusPix*2)/2-circelSizeWeight1, ...
maskDstRectsRand(3, n)-(masktexrect(3)-maskRadiusPix*2)/2+circelSizeWeight1, maskDstRectsRand(4, n)-(masktexrect(4)-maskRadiusPix*2)/2+circelSizeWeight1]);
sM.drawCross();
sM.flip();
WaitSecs('YieldSecs', 0.5);
Screen('FillRect', win, 0.5);
sM.drawCross();
sM.flip;
WaitSecs('YieldSecs', 0.5);
fprintf('TRIAL:%i: staircase value = %.2f\n', n, staircase.xCurrent);
%%%%%%% draw fixed phase %%%%%%%
finishLoop = false;
while strcmp(fixated, 'fix') && finishLoop == false
if useEyeLink; edfMessage(eL,'END_FIX'); statusMessage(eL,'Show Stimulus...'); end
currentTime = GetSecs;
nextTime = currentTime + ana.part1_duration;
vbl = currentTime;
while vbl < nextTime
Screen('DrawTextures', win, dotstex, [], dotDstRects, [], [], 1, dotColorMatrix, [], [], mydots);
if ana.foregroundMask
Screen('DrawTextures', win, masktex, [], maskDstRectsRand(:, n), [], [], 1, [0.5, 0.5, 0.5, 1]', [], [], mymask);
end
sM.drawCross();
[x1, y1] = RectCenterd(dotDstRects);
index_kill1 = find(((x1+initialSpeed(1)*cosd(initialDirection))-w)>0);
x1 = mod(x1+initialSpeed(1)*cosd(initialDirection), w);
y1 = mod(y1-initialSpeed(1)*sind(initialDirection), h);
y1(index_kill1) = h*rand(1,length(index_kill1)); %rand y of out_dots
dotDstRects = CenterRectOnPointd(inrect, x1, y1);
if useEyeLink
getSample(eL); %drawEyePosition(eL);
isfix = isFixated(eL);
if ~isfix
fixated = 'breakfix';
break
end
end
vbl = Screen('Flip', win, vbl + halfisi);
end
if useEyeLink && ~strcmpi(fixated,'fix')
response = BREAKFIX; finishLoop = true;
statusMessage(eL,'Subject Broke Fixation!');
edfMessage(eL,'MSG:BreakFix')
break
end
%%%%%%% draw test phase %%%%%%%
dotSpeedPerFrame = ((dotSpeedWeight*staircase.xCurrent)+dotSpeedMean)*pixelPerDeg*ifi; % pixel/frame
%mae_dir(n)= ana.dotDirection(dotDirectionMatrix(n));
%=== Negative is left == 1 | Positive is right == 2
MAEDirection(n) = (dotSpeedPerFrame >= 0) + 1;
MAESpeed(n) = dotSpeedPerFrame/(pixelPerDeg*ifi);
fprintf('==Dot Speed is %.3f (%.3f), direction is %.2f\n',dotSpeedPerFrame,MAESpeed(n),MAEDirection(n));
currentTime = GetSecs;
nextTime = currentTime + ana.stimulusDuration;
vbl = currentTime;
while vbl < nextTime
Screen('DrawTextures', win, dotstex, [], dotDstRects, [], [], 1, dotColorMatrix, [], [], mydots);
if ana.foregroundMask
Screen('DrawTextures', win, masktex, [], maskDstRectsRand(:, n), [], [], 1, [0.5, 0.5, 0.5, 1]', [], [], mymask);
end
sM.drawCross();
[x, y] = RectCenterd(dotDstRects);
index_kill2 = find((x+dotSpeedPerFrame*cosd(0)-w)>0);
x = mod(x+dotSpeedPerFrame*cosd(0),w);
y = mod(y-dotSpeedPerFrame*sind(0),h);
y(index_kill2) = h*rand(1,length(index_kill2)); %rand y of out_dots
dotDstRects = CenterRectOnPointd(inrect, x, y);
if useEyeLink
getSample(eL); %drawEyePosition(eL);
isfix = isFixated(eL);
if ~isfix
fixated = 'breakfix';
break
end
end
vbl = Screen('Flip', win, vbl + halfisi);
end
if useEyeLink && ~strcmpi(fixated,'fix')
response = BREAKFIX; finishLoop = true;
statusMessage(eL,'Subject Broke Fixation!');
edfMessage(eL,'MSG:BreakFix')
break
end
%%%%%%%%% GET Result %%%%%%%%%%%
Screen('FillRect', win, 0.5);
sM.drawCross();
sM.flip();
WaitSecs('YieldSecs',0.5);
if ana.dotDirection(dotDirectionMatrix(n)) == 0 || ana.dotDirection(dotDirectionMatrix(n)) == 180
imageTexture = imread('choice_plane_LR.jpg');
elseif ana.dotDirection(dotDirectionMatrix(n)) == 90 || ana.dotDirection(dotDirectionMatrix(n)) == 270
imageTexture = imread('choice_plane_UD.jpg');
end
imageID = Screen('MakeTexture', win, imageTexture);
Screen('DrawTexture', win, imageID, [], sM.winRect);
Screen('Flip', win);
if useEyeLink
statusMessage(eL,'Waiting for Subject Response!');
edfMessage(eL,'Subject Responding')
edfMessage(eL,'END_RT'); ...
end
finishLoop = true;
end
if response ~= BREAKFIX
[~, keyCode, ~] = KbWait;
if keyCode(leftKey) == 1
Response(n) = 1;
elseif keyCode(rightKey) == 1
Response(n) = 2;
elseif keyCode(upKey) == 1
Response(n) = 1;
elseif keyCode(downKey) == 1
Response(n) = 2;
elseif keyCode(escKey) == 1
exitLoop = true;
break
end
end
Screen('FillRect', win, 0.5);
sM.flip();
WaitSecs('YieldSecs', ana.ITI);
% thisResponse = Response(n) == MAEDirection(n);
thisResponse = Response(n) == 2;
fprintf('====Subject response %i, means this trial response was: %i\n\n',Response(n), thisResponse);
staircase = PAL_AMPM_updatePM(staircase, thisResponse);
plotResults();
if useEyeLink
resetFixation(eL); trackerClearScreen(eL);
stopRecording(eL);
edfMessage(eL,['TRIAL_RESULT ' num2str(Response(n))]);
setOffline(eL);
end
% drawBackground(sM);
% Screen('Flip',sM.win); %flip the buffer
% WaitSecs(0.5);
if response ~= BREAKFIX;n = n + 1;end %
response = 1;
end
%-----Cleanup
Screen('Flip',sM.win);
Priority(0); ListenChar(0); ShowCursor;
close(sM); %close screen
p=uigetdir(pwd,'Select Directory to Save Data, CANCEL to not save.');
if ischar(p)
cd(p);
if ~useEyeLink; eL = []; end
save([ana.nameExp '.mat'], 'ana', 'Response','staircase', 'sM','eL','MAEDirection','MAESpeed');
disp(['=====SAVE, saved current data to: ' pwd]);
else
if useEyeLink; eL.saveFile = ''; end %blank save file so it doesn't save
end
if useEyeLink == true; close(eL); end
% reset(stimuli); %reset our stimulus ready for use again
% if ~exitLoop
% fileName = strcat('MAE_tasks', datestr(now, 'yyyy-mm-dd-HH-MM-SS'), '.mat');
% save(fileName, 'Response', 'ana', 'staircase','MAEDirection','MAESpeed');
% end
% sM.close();
% sca;
% Screen('CloseAll');
catch ME
ple(ME)
close(sM); %close screen
Priority(0); ListenChar(0); ShowCursor;
disp(['!!!!!!!!=====CRASH, save current data to: ' pwd]);
save([ana.nameExp 'CRASH.mat'], 'ana', 'Response','staircase', 'sM','eL','MAEDirection','MAESpeed')
if useEyeLink == true; eL.saveFile = [ana.nameExp 'CRASH.edf']; close(eL); end
% reset(stimuli);
% clear stimuli task taskB taskW md eL s
clear eL s
rethrow(ME);
end
end
vbl = Screen('Flip', win, vbl + halfisi);
end
if useEyeLink && ~strcmpi(fixated,'fix')
response = BREAKFIX; finishLoop = true;
statusMessage(eL,'Subject Broke Fixation!');
edfMessage(eL,'MSG:BreakFix')
break
end
%%%%%%%%% GET Result %%%%%%%%%%%
Screen('FillRect', win, 0.5);
sM.drawCross();
sM.flip();
WaitSecs('YieldSecs',0.5);
if ana.dotDirection(dotDirectionMatrix(n)) == 0 || ana.dotDirection(dotDirectionMatrix(n)) == 180
imageTexture = imread('choice_plane_LR.jpg');
elseif ana.dotDirection(dotDirectionMatrix(n)) == 90 || ana.dotDirection(dotDirectionMatrix(n)) == 270
imageTexture = imread('choice_plane_UD.jpg');
end
imageID = Screen('MakeTexture', win, imageTexture);
Screen('DrawTexture', win, imageID, [], sM.winRect);
Screen('Flip', win);
if useEyeLink
statusMessage(eL,'Waiting for Subject Response!');
edfMessage(eL,'Subject Responding')
edfMessage(eL,'END_RT'); ...
end
finishLoop = true;
end
if response ~= BREAKFIX
[~, keyCode, ~] = KbWait;
if keyCode(leftKey) == 1
Response(n) = 1;
elseif keyCode(rightKey) == 1
Response(n) = 2;
elseif keyCode(upKey) == 1
Response(n) = 1;
elseif keyCode(downKey) == 1
Response(n) = 2;
elseif keyCode(escKey) == 1
exitLoop = true;
break
end
end
Screen('FillRect', win, 0.5);
sM.flip();
WaitSecs('YieldSecs', ana.ITI);
% thisResponse = Response(n) == MAEDirection(n);
thisResponse = Response(n) == 2;
fprintf('====Subject response %i, means this trial response was: %i\n\n',Response(n), thisResponse);
staircase = PAL_AMPM_updatePM(staircase, thisResponse);
plotResults();
if useEyeLink
resetFixation(eL); trackerClearScreen(eL);
stopRecording(eL);
edfMessage(eL,['TRIAL_RESULT ' num2str(Response(n))]);
setOffline(eL);
end
% drawBackground(sM);
% Screen('Flip',sM.win); %flip the buffer
% WaitSecs(0.5);
if response ~= BREAKFIX;n = n + 1;end %
response = 1;
end
%-----Cleanup
Screen('Flip',sM.win);
Priority(0); ListenChar(0); ShowCursor;
close(sM); %close screen
p=uigetdir(pwd,'Select Directory to Save Data, CANCEL to not save.');
if ischar(p)
cd(p);
if ~useEyeLink; eL = []; end
save([ana.nameExp '.mat'], 'ana', 'Response','staircase', 'sM','eL','MAEDirection','MAESpeed');
disp(['=====SAVE, saved current data to: ' pwd]);
else
if useEyeLink; eL.saveFile = ''; end %blank save file so it doesn't save
end
if useEyeLink == true; close(eL); end
% reset(stimuli); %reset our stimulus ready for use again
% if ~exitLoop
% fileName = strcat('MAE_tasks', datestr(now, 'yyyy-mm-dd-HH-MM-SS'), '.mat');
% save(fileName, 'Response', 'ana', 'staircase','MAEDirection','MAESpeed');
% end
% sM.close();
% sca;
% Screen('CloseAll');
catch ME
ple(ME)
close(sM); %close screen
Priority(0); ListenChar(0); ShowCursor;
disp(['!!!!!!!!=====CRASH, save current data to: ' pwd]);
save([ana.nameExp 'CRASH.mat'], 'ana', 'Response','staircase', 'sM','eL','MAEDirection','MAESpeed')
if useEyeLink == true; eL.saveFile = [ana.nameExp 'CRASH.edf']; close(eL); end
% reset(stimuli);
% clear stimuli task taskB taskW md eL s
clear eL s
rethrow(ME);
end
tFix = Screen('Flip',sM.win); %flip the buffer
WaitSecs(0.5);
fixated = 'fix';
end
%------Our main stimulus drawing loop
finishLoop = false;
while strcmp(fixated, 'fix') && finishLoop == false
if useEyeLink; edfMessage(eL,'END_FIX'); statusMessage(eL,'Show Stimulus...'); end
%=====================STIMULUS
stimuli.show();
tStim = GetSecs; vbl = tStim;
while vbl <= tStim + ana.stimulusTime
draw(stimuli); %draw stimulus
drawCross(sM,0.4,[1 1 1 1],ana.fixX,ana.fixY);
Screen('DrawingFinished', sM.win); %tell PTB/GPU to draw
if useEyeLink
getSample(eL); %drawEyePosition(eL);
isfix = isFixated(eL);
if ~isfix
fixated = 'breakfix';
break
end
end
animate(stimuli); %animate stimulus, will be seen on next draw
vbl = Screen('Flip',sM.win, vbl + screenVals.halfisi); %flip the buffer
end
if useEyeLink && ~strcmpi(fixated,'fix')
response = BREAKFIX; finishLoop = true;
statusMessage(eL,'Subject Broke Fixation!');
edfMessage(eL,'MSG:BreakFix')
break
end
%=====================PEDESTAL
stimuli{1}.colourOut = pedestal;
tPedestal=GetSecs;
while GetSecs <= tPedestal + ana.pedestalTime
draw(stimuli); %draw stimulus
drawCross(sM,0.4,[1 1 1 1],ana.fixX,ana.fixY);
Screen('DrawingFinished', sM.win); %tell PTB/GPU to draw
if useEyeLink
getSample(eL);
isfix = isFixated(eL);
if ~isfix
fixated = 'breakfix';
break
end
end
%animate(stimuli); %animate stimulus, will be seen on next draw
vbl = Screen('Flip',sM.win, vbl + screenVals.halfisi); %flip the buffer
end
if ~strcmpi(fixated,'fix')
response = BREAKFIX; finishLoop = true;
statusMessage(eL,'Subject Broke Fixation!');
edfMessage(eL,'MSG:BreakFix')
break
end
%=====================MASK
stimuli.showMask = true; %metaStimulus can trigger a mask
tMask=GetSecs;
while GetSecs <= tMask + ana.maskTime
draw(stimuli); %draw stimulus
drawCross(sM,0.4,[1 1 1 1],ana.fixX,ana.fixY);
Screen('DrawingFinished', sM.win); %tell PTB/GPU to draw
animate(stimuli); %animate stimulus, will be seen on next draw
vbl = Screen('Flip',sM.win, vbl + screenVals.halfisi); %flip the buffer
end
%=====================RESPONSE
drawBackground(sM);
Screen('DrawText',sM.win,['See anything AFTER stimulus: [LEFT]=NO [UP]=BRIGHTER [DOWN]=DARKER [RIGHT]=SHOW AGAIN'],0,0);
tMaskOff = Screen('Flip',sM.win);
if useEyeLink
statusMessage(eL,'Waiting for Subject Response!');
edfMessage(eL,'Subject Responding')
edfMessage(eL,'END_RT'); ...
end
finishLoop = true;
end
%-----check keyboard
if response ~= BREAKFIX
ListenChar(2);
[secs, keyCode] = KbWait(-1);
rchar = KbName(keyCode);
if iscell(rchar);rchar=rchar{1};end
switch lower(rchar)
case {'leftarrow','left'}
response = NOSEE;
updateResponse();
if useEyeLink
trackerDrawText(eL,'Subject Pressed LEFT!');
edfMessage(eL,'Subject Pressed LEFT')
end
doPlot();
case {'uparrow','up'} %brighter than
response = YESBRIGHT;
updateResponse();
if useEyeLink
trackerDrawText(eL,'Subject Pressed RIGHT!');
edfMessage(eL,'Subject Pressed RIGHT')
end
doPlot();
case {'downarrow','down'} %darker than
response = YESDARK;
updateResponse();
if useEyeLink
trackerDrawText(eL,'Subject Pressed RIGHT!');
edfMessage(eL,'Subject Pressed RIGHT')
end
doPlot();
case {'righttarrow','right'}
response = UNSURE;
updateResponse();
if useEyeLink
trackerDrawText(eL,'Subject UNSURE!');
edfMessage(eL,'Subject UNSURE')
end
doPlot();
case {'backspace','delete'}
response = -10;
updateResponse();
if useEyeLink
trackerDrawText(eL,'Subject UNDO!');
edfMessage(eL,'Subject UNDO')
end
doPlot();
case {'c'} %calibrate
response = BREAKFIX;
stopRecording(eL);
setOffline(eL);
trackerSetup(eL);
WaitSecs(2);
case {'d'}
response = BREAKFIX;
stopRecording(eL);
setOffline(eL);
success = driftCorrection(eL);
WaitSecs(2);
case {'q'} %quit
response = BREAKFIX;
fprintf('\n!!!QUIT!!!\n');
breakloop = true;
otherwise
response = UNSURE;
updateResponse();
if useEyeLink
statusMessage(eL,'Subject UNSURE!');
edfMessage(eL,'Subject UNSURE')
end
end
end
tEnd = GetSecs;
ListenChar(0);
if useEyeLink
resetFixation(eL); trackerClearScreen(eL);
stopRecording(eL);
edfMessage(eL,['TRIAL_RESULT ' num2str(response)]);
setOffline(eL);
end
drawBackground(sM);
Screen('Flip',sM.win); %flip the buffer
WaitSecs(0.5);
end
%-----Cleanup
Screen('Flip',sM.win);
Priority(0); ListenChar(0); ShowCursor;
close(sM); %close screen
p=uigetdir(pwd,'Select Directory to Save Data, CANCEL to not save.');
if ischar(p)
cd(p);
response = task.response;
responseInfo = task.responseInfo;
if ~useEyeLink; eL = []; end
save([ana.nameExp '.mat'], 'ana', 'response', 'responseInfo', 'task',...
'taskB', 'taskW', 'staircaseB', 'staircaseW', 'sM',...
'stimuli', 'eL');
disp(['=====SAVE, saved current data to: ' pwd]);
else
if useEyeLink; eL.saveFile = ''; end %blank save file so it doesn't save
end
if useEyeLink == true; close(eL); end
reset(stimuli); %reset our stimulus ready for use again
catch ME
ple(ME)
close(sM); %close screen
Priority(0); ListenChar(0); ShowCursor;
disp(['!!!!!!!!=====CRASH, save current data to: ' pwd]);
save([ana.nameExp 'CRASH.mat'], 'task', 'taskB', 'taskW',...
'staircaseB', 'staircaseW', 'ana', 'sM', 'stimuli', 'eL', 'ME')
if useEyeLink == true; eL.saveFile = [ana.nameExp 'CRASH.edf']; close(eL); end
reset(stimuli);
clear stimuli task taskB taskW md eL s
rethrow(ME);
end
function updateResponse()
tEnd = GetSecs;
ListenChar(0);
if response == NOSEE || response == YESBRIGHT || response == YESDARK %subject responded
responseInfo.response = response;
responseInfo.N = task.thisRun;
responseInfo.times = [tFix tStim tPedestal tMask tMaskOff tEnd];
responseInfo.contrastOut = colourOut;
responseInfo.pedestal = pedestal;
responseInfo.pedestalGamma = pedestal;
responseInfo.blackN = taskB.thisRun;
responseInfo.whiteN = taskW.thisRun;
responseInfo.redo = responseRedo;
updateTask(task,response,tEnd,responseInfo)
if ana.useStaircase == true
if colourOut == 0
if response == NOSEE || response == YESDARK
yesnoresponse = 0;
else
yesnoresponse = 1;
end
staircaseB = PAL_AMPM_updatePM(staircaseB, yesnoresponse);
elseif colourOut == 1
if response == NOSEE || response == YESDARK
yesnoresponse = 0;
else
yesnoresponse = 1;
end
staircaseW = PAL_AMPM_updatePM(staircaseW, yesnoresponse);
end
fprintf('subject response: %i | ', yesnoresponse)
else
if colourOut == 0
taskB.thisRun = taskB.thisRun + 1;
else
taskW.thisRun = taskW.thisRun + 1;
end
end
elseif response == -10
if task.totalRuns > 1
if ana.useStaircase == true
warning('Not Implemented yet!!!')
else
if task.responseInfo(end) == 0
taskB.rewindRun;
else
taskW.rewindRun;
end
task.rewindRun
fprintf('new trial = %i\n', task.thisRun);
end
end
elseif response == UNSURE
responseRedo = responseRedo + 1;
fprintf('Subject is trying stimulus again, overall = %.2g %\n',responseRedo);
end
end
function doPlot()
ListenChar(0);
x = 1:length(task.response);
info = cell2mat(task.responseInfo);
ped = [info.pedestal];
idxW = [info.contrastOut] == 1;
idxB = [info.contrastOut] == 0;
idxNO = task.response == NOSEE;
idxYESBRIGHT = task.response == YESBRIGHT;
idxYESDARK = task.response == YESDARK;
cla(ana.plotAxis1); line(ana.plotAxis1,[0 max(x)+1],[0.5 0.5],'LineStyle','--','LineWidth',2); hold(ana.plotAxis1,'on')