-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcohcon.m
1334 lines (1146 loc) · 46.7 KB
/
cohcon.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
% cohCon
%
% usage: myscreen=cohcon()
% by: daniel birman
% date: 11/10/14
% purpose: contrast change detection with cued selective attention.
%
% use: call cohcon() to initialize. The first
% run takes significantly longer due to loading stimuli.
%
% flags: stimFileNum (-1/#) - Load a specific stimfile from a
% subject's folder. Defaults to the last stimfile. Warning:
% Only the first stimfile is saved with the image file data,
% subsequent stimfiles only contain run data.
% unattended (0/1) - If 1, runs a fixation task while the main
% task just runs on idle in the background (no inputs)
% plots (0/1) - Displays staircase plots (and estimated
% psychophysic functions)
% overrideTask (1/2) - Specifies the task to run: 1 =
% coherence, 2 = contrast
% projector (0/1) - Masks stimuli using the default projector
% mask.
% scan (0/1) - Scanner timing
%
%
% TR .75 = 560 volumes (7:00 total)
% TR 1.4 = 300 volumes (7:00 total)
function [myscreen] = cohcon(varargin)
global stimulus
clear fixStimulus
%% Initialize Variables
% add arguments later
stimFileNum = [];
plots = [];
overrideTask = 0;
scan = [];
nocatch = [];
stablecon = 0;
stablecoh = 0;
constant = [];
noeye = 0;
time = 0;
getArgs(varargin,{'stimFileNum=-1','nocatch=1','time=1',...
'plots=0','overrideTask=0','scan=0','constant=1','stablecon=0','stablecoh=0','noeye=1'});
stimulus.scan = scan;
stimulus.plots = plots;
stimulus.nocatch = nocatch;
stimulus.stablecon = stablecon;
stimulus.stablecoh = stablecoh;
stimulus.noeye = noeye;
stimulus.time = time;
stimulus.constant = constant; % new param, keeps stimulus on screen at all times with 0% coherence
if stimulus.scan && ~stimulus.nocatch
warning('You didn''t force nocatch during a scan, Auto-setting');
stimulus.nocatch = 1;
end
if stimulus.stablecon && ~overrideTask==1
warning('You didn''t specify task = motion, but you stabilized contrast. Auto-setting');
overrideTask = 1;
end
if stimulus.stablecoh && ~overrideTask==2
warning('You didn''t specify task = contrast, but you stabilized coherence. Auto-setting');
overrideTask = 2;
end
if stimulus.scan && ~mglGetParam('ignoreInitialVols')==16 && ~mglGetParam('ignoreInitialVols')==4
warning('ignoreInitialVols is set to %i.',mglGetParam('ignoreInitialVols'));
if ~strcmp('y',input('Is this correct? [y/n]'))
mglSetParam('ignoreInitialVols',input('Please input the correct value (mux8 = 16, mux2 = 4): '));
end
end
stimulus.counter = 1; % This keeps track of what "run" we are on.
%% Setup Screen
if stimulus.scan
myscreen = initScreen('fMRIprojFlex');
else
myscreen = initScreen('VPixx');
end
myscreen.background = 0.5;
%% Open Old Stimfile
stimulus.initStair = 1;
if ~isempty(mglGetSID) && isdir(sprintf('~/data/cohcon/%s',mglGetSID))
% Directory exists, check for a stimefile
files = dir(sprintf('~/data/cohcon/%s/1*mat',mglGetSID));
if length(files) >= 1
if stimFileNum == -1
if length(files) > 1
warning('Multiple stimfiles found, loading last one. If you wanted a different functionality use stimFileNum=#');
end
fname = files(end).name;
else
fname = files(stimFileNum).name;
end
s = load(sprintf('~/data/cohcon/%s/%s',mglGetSID,fname));
% copy staircases and run numbers
stimulus.staircases = s.stimulus.staircases;
stimulus.counter = s.stimulus.counter + 1;
% load blocks too
stimulus.runs = s.stimulus.runs;
stimulus.runs.loaded = 1;
clear s;
stimulus.initStair = 0;
disp(sprintf('(cohcon) Data file: %s loaded.',fname));
end
end
disp(sprintf('(cohcon) This is run #%i',stimulus.counter));
if stimulus.plots==2
dispInfoNum(stimulus);
dispInfo(stimulus);
return
end
%% Initialize Stimulus
myscreen = initStimulus('stimulus',myscreen);
if stimulus.scan
stimulus.responseKeys = [1 2]; % corresponds to LEFT - RIGHT
else
stimulus.responseKeys = [1 2]; % corresponds to LEFT - RIGHT
end
%% Block setup
if isfield(stimulus,'runs') && isfield(stimulus.runs,'loaded')
% We already have our blocks
stimulus.runs = rmfield(stimulus.runs,'loaded'); % remove the load field, otherwise it gets saved across runs
if stimulus.counter > length(stimulus.runs.taskList)
% double the length (maintains order)
stimulus.runs.taskList = [stimulus.runs.taskList stimulus.runs.taskBuilder]; %repmat(stimulus.runs.taskList,1,2);
end
else
% This is the first run, build up the blocks.
stimulus.runs = struct;
stimulus.runs.taskOpts = [1 2];
if stimulus.scan
stimulus.runs.taskBuild = {[1 1],[2 2]};
else
stimulus.runs.taskBuild = {[1 1 -1 1 -1] [2 2 -2 2 -2]};
end
stimulus.runs.taskOptsText = {'Motion','Contrast'};
stimulus.runs.taskBuilder = [stimulus.runs.taskBuild{stimulus.runs.taskOpts(randperm(2))}];
stimulus.runs.taskList = [stimulus.runs.taskBuilder];
end
%% Task Override
if overrideTask > 0
stimulus.runs.curTask = overrideTask;
if overrideTask>0
stimulus.nocatch = 1;
disp('(cohcon) Auto-setting nocatch!!');
end
% insert the override into the taskList
pre = stimulus.runs.taskList(1:stimulus.counter-1);
post = stimulus.runs.taskList(stimulus.counter:end);
stimulus.runs.taskList = [pre overrideTask post];
else
cT = stimulus.runs.taskList(stimulus.counter);
if cT>0
stimulus.nocatch = 1;
disp('(cohcon) Auto-run setup is doing a no-catch run...');
else
disp('(cohcon) Auto-run setup is doing a main+catch run...');
end
stimulus.runs.curTask = abs(stimulus.runs.taskList(stimulus.counter));
end
if stimulus.time && stimulus.runs.curTask<0
disp('(cohcon) Warning: you forgot to set a task!');
end
%% Useful stimulus stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% CONTROL BASE CONTRAST %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% what contrast/coherence should run constantly in the background?
stimulus.baseCon = 0.25;
stimulus.baseCoh = 0;
% for initStair to know how many staircases to make
stimulus.stairInfo.catchP = 1;
stimulus.stairInfo.mainP = 1;
% for the real psychophysics experiment test we will run a discrimination
% task, i.e. "which side goes higher". Contrast will jump to 40% and
% coherence to 25%, but one side will go up more than the other side.
stimulus.stairInfo.pedestals.contrast = 0.4;
stimulus.stairInfo.pedestals.coherence = 0.3;
% for our distractors we will use these random increments:
stimulus.stairInfo.increments.coherence = [0 exp(-3:.33:-.6)];
stimulus.stairInfo.increments.contrast = [0 exp(-4:.33:-1.6)];
% initial thresholds to use in staircases
stimulus.stairInfo.initThresh.contrast = 0.25;
stimulus.stairInfo.initThresh.coherence = 0.85;
stimulus.stairInfo.pedOpts = {'coherence','contrast'};
if stimulus.scan
stimulus.stairInfo.nocatchP = 4;
% we are scanning, add more pedestals so we get the full range (these are now the same as for nocatch runs)
stimulus.stairInfo.pedestals.contrast = [0.325 0.4 0.55 0.85];
stimulus.stairInfo.pedestals.coherence = [0.15 0.3 0.45 0.6];
if ~stimulus.nocatch
disp('(cohcon) Auto-setting nocatch for scan run.');
stimulus.nocatch = 1;
end
if stimulus.plots
disp('(cohcon) Auto-setting no plots for scan run.');
stimulus.plots =0 ;
end
elseif stimulus.nocatch
stimulus.stairInfo.nocatchP = 4;
% we aren't scanning, so we can still run staircases, but we are doing
% nocatch runs where there will be a LOT of runs. So let's add some
% more pedestals in so we get a better estimate of the psychometric
% function.
stimulus.stairInfo.pedestals.contrast = [0.325 0.4 0.55 0.85];
stimulus.stairInfo.pedestals.coherence = [0.15 0.3 0.45 0.6];
end
if stimulus.time
% use two contrast and two coherences (12 total staircases to estimate)
stimulus.stairInfo.pedestals.contrast = [0.325 0.85];
stimulus.stairInfo.pedestals.coherence = [0.15 0.6];
end
%% Colors
stimulus.colors.rmed = 127.5;
% We're going to add an equal number of reserved colors to the top and
% bottom, to try to keep the center of the gamma table stable.
stimulus.colors.reservedBottom = [0 0 0; .6 .6 .6]; % fixation cross colors
stimulus.colors.reservedTop = [.6 0 0; 0 .6 0]; % correct/incorrect colors
stimulus.colors.black = 0/255; stimulus.colors.white = 1/255;
stimulus.colors.red = 254/255; stimulus.colors.green = 255/255;
stimulus.colors.nReserved = 2; % this is /2 the true number, because it's duplicated
stimulus.colors.nUnreserved = 256-(2*stimulus.colors.nReserved);
stimulus.colors.mrmax = stimulus.colors.nReserved - 1 + stimulus.colors.nUnreserved;
stimulus.colors.mrmin = stimulus.colors.nReserved;
%% Everything else
stimulus.dots.xcenter = 0;
stimulus.dots.ycenter = 0;
stimulus.dots.dotsize = 3;
stimulus.dots.density = 21;
stimulus.dots.speed = 6;
stimulus.dots.centerOffset = 2;
stimulus.dotsR = stimulus.dots;
stimulus.dotsR.mult = 1;
stimulus.dotsL = stimulus.dots;
stimulus.dotsL.mult = -1;
stimulus = rmfield(stimulus,'dots');
stimulus.dotsR = initDotsRadial(stimulus.dotsR,myscreen);
stimulus.dotsL = initDotsRadial(stimulus.dotsL,myscreen);
if ~length(stimulus.baseCoh)==length(stimulus.baseCon)
disp('(cohcon) Contrast and coherence have different pedestal #, this can cause errors');
end
%% Gamma Table Initialization
% get gamma table
if ~isfield(myscreen,'gammaTable')
stimulus.linearizedGammaTable = mglGetGammaTable;
disp(sprintf('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'));
disp(sprintf('(cuecon:initGratings) No gamma table found in myscreen. Contrast displays like this'));
disp(sprintf(' should be run with a valid calibration made by moncalib for this monitor.'));
disp(sprintf('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'));
else
% check to make sure this matches the calibration file
% get each channel table that should have been set by mglGetGammaTable
redTable = myscreen.initScreenGammaTable.redTable(:);
greenTable = myscreen.initScreenGammaTable.greenTable(:);
blueTable = myscreen.initScreenGammaTable.blueTable(:);
% get what the calibration structure says it should have been set to
gammaTable = myscreen.gammaTable(:);
% table values are only good to 10 bits
redTable = round(redTable*1024)/1024;
greenTable = round(greenTable*1024)/1024;
blueTable = round(blueTable*1024)/1024;
gammaTable = round(gammaTable*1024)/1024;
% compare, ignoring nans
if ~isequaln(mglGetGammaTable,myscreen.initScreenGammaTable) || ~isequaln(redTable,gammaTable) || ~isequaln(greenTable,gammaTable) || ~isequaln(blueTable,gammaTable)
disp(sprintf('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'));
disp(sprintf('(curecon:initGrating) Gamma table does not match calibration'));
disp(sprintf('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'));
keyboard
end
end
stimulus.linearizedGammaTable = myscreen.initScreenGammaTable;
%% Character textures
mglTextSet('Helvetica',32,stimulus.colors.white,0,0,0,0,0,0,0);
stimulus.text.mTexW = mglText('M');
stimulus.text.cTexW = mglText('C');
mglTextSet('Helvetica',32,stimulus.colors.black,0,0,0,0,0,0,0);
stimulus.text.mTexK = mglText('M');
stimulus.text.cTexK = mglText('C');
%% Setup Task
% This is the contrast change detection task
task{1}{1}.waitForBacktick = 1;
stimulus.seg.stim = 1;
stimulus.seg.mask = 2;
stimulus.seg.ISI = 3;
stimulus.seg.resp = 4;
stimulus.seg.ITI = 5;
task{1}{1}.segmin = [0.5 0 .5 1 .2];
task{1}{1}.segmax = [0.5 0 1 1 .4];
if stimulus.scan
task{1}{1}.segmin(stimulus.seg.ITI) = 2;
task{1}{1}.segmax(stimulus.seg.ITI) = 11;
end
task{1}{1}.synchToVol = [0 0 0 0 0];
if stimulus.scan
task{1}{1}.synchToVol(stimulus.seg.ITI) = 1;
end
task{1}{1}.getResponse = [0 0 0 0 0]; task{1}{1}.getResponse(stimulus.seg.resp)=1;
task{1}{1}.parameter.conSide = [1 2]; % 1 = left, 2 = right, the side will be the one with con/flow + delta (From staircase)
task{1}{1}.parameter.cohSide = [1 2];
task{1}{1}.parameter.dir = [-1 1];
task{1}{1}.parameter.conPedestal = 1:length(stimulus.stairInfo.pedestals.contrast); % target contrast
task{1}{1}.parameter.cohPedestal = 1:length(stimulus.stairInfo.pedestals.coherence); % target flow coherence
task{1}{1}.parameter.catch = [1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; % 15% chance of being a catch trial
if stimulus.time
task{1}{1}.parameter.time = [250 500 1000];
end
task{1}{1}.random = 1;
task{1}{1}.numTrials = 65;
if stimulus.nocatch || stimulus.scan
task{1}{1}.parameter.catch = -1;
end
%% Run variables
task{1}{1}.randVars.calculated.coherence = nan;
task{1}{1}.randVars.calculated.contrast = nan;
task{1}{1}.randVars.calculated.cohDelta = nan;
task{1}{1}.randVars.calculated.conDelta = nan;
%% Tracking
% these are variables that we want to track for later analysis.
task{1}{1}.randVars.calculated.task = nan; % Current task (calc per run)
task{1}{1}.randVars.calculated.correct = nan;
task{1}{1}.randVars.calculated.trialNum = nan;
task{1}{1}.randVars.calculated.lCoh = nan;
task{1}{1}.randVars.calculated.rCoh = nan;
task{1}{1}.randVars.calculated.lCon = nan;
task{1}{1}.randVars.calculated.rCon = nan;
stimulus.curTrial = 0;
if stimulus.scan
% when scanning we add a
task{1}{1}.numTrials = inf;
task{1}{2} = task{1}{1};
task{1}{2}.waitForBacktick = 0;
task{1}{1}.parameter.conPedestal = 1;
task{1}{1}.parameter.cohPedestal = 1;
task{1}{1}.numTrials = 1;
task{1}{1}.getResponse = [0 0 0 0 0];
task{1}{1}.segmin = [0 0 0 0 30];
task{1}{1}.segmax = [0 0 0 0 30];
task{1}{1}.parameter.catch = 0;
else
% when scanning we add a
task{1}{2} = task{1}{1};
task{1}{2}.waitForBacktick = 0;
task{1}{1}.parameter.conPedestal = 1;
task{1}{1}.parameter.cohPedestal = 1;
task{1}{1}.numTrials = 1;
task{1}{1}.getResponse = [0 0 0 0 0];
task{1}{1}.segmin = [0 0 0 0 4];
task{1}{1}.segmax = [0 0 0 0 4];
task{1}{1}.parameter.catch = 0;
end
%% Full Setup
% Initialize task (note phase == 1)
for phaseNum = 1:length(task{1})
[task{1}{phaseNum}, myscreen] = initTask(task{1}{phaseNum},myscreen,@startSegmentCallback,@screenUpdateCallback,@getResponseCallback,@startTrialCallback,[],[]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% init staircase
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if stimulus.initStair
% We are starting our staircases from scratch
disp(sprintf('(cohcon) Initializing staircases'));
stimulus = initStaircase(stimulus);
else
disp('(cohcon) Re-using staircase from previous run...');
% Reset staircase if necessary
checkStaircaseStop();
end
%% EYE CALIB
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% run the eye calibration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~stimulus.scan
myscreen = eyeCalibDisp(myscreen);
end
% Put up text that should be invisible if everything
% with the gamma table is working.
setGammaTable_flowMax(0);
mglWaitSecs(1);
mglClearScreen(0.5);
mglTextSet([],32,0.9);
mglTextDraw('If you can see this STOP THE EXPERIMENT',[0 0]);
mglFlush;
mglWaitSecs(1);
%% Get Ready...
% clear screen
setGammaTable_flowMax(1);
mglClearScreen(0.5);
mglFlush
mglWaitSecs(1);
if stimulus.scan
mglTextDraw('DO NOT MOVE',[0 1.5]);
mglTextDraw(stimulus.runs.taskOptsText{stimulus.runs.curTask},[0 0]);
else
mglTextDraw(stimulus.runs.taskOptsText{stimulus.runs.curTask},[0 0]);
end
mglFlush
tasktypes = {'Main+Catch','Nocatch'};
% let the user know
disp(sprintf('(cohcon) Starting run number: %i. Current task: %s, Type: %s',stimulus.counter,stimulus.runs.taskOptsText{stimulus.runs.curTask},tasktypes{stimulus.nocatch+1}));
% if stimulus.unattended
myscreen.flushMode = 1;
% end
%% Main Task Loop
phaseNum = 1;
% Again, only one phase.
while (phaseNum <= length(task{1})) && ~myscreen.userHitEsc
% update the task
[task{1}, myscreen, phaseNum] = updateTask(task{1},myscreen,phaseNum);
% flip screen
myscreen = tickScreen(myscreen,task);
end
% task ended
mglClearScreen(0.5);
mglTextDraw('Run complete... please wait.',[0 0]);
mglFlush
myscreen.flushMode = 1;
mglWaitSecs(1);
% if we got here, we are at the end of the experiment
myscreen = endTask(myscreen,task);
if ~stimulus.scan
dispInfoNum(stimulus);
if stimulus.plots
disp('(cohcon) Displaying plots');
dispInfo(stimulus);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%% EXPERIMENT OVER: HELPER FUNCTIONS FOLLOW %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Runs at the start of each Trial %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [task, myscreen] = startTrialCallback(task,myscreen)
%%
global stimulus
if task.thistrial.thisphase==2 && stimulus.scan
task.thistrial.seglen(end) = 1.05^(rand*30+20);
end
if stimulus.time
task.thistrial.seglen(1) = task.thistrial.time/1000;
end
stimulus.curTrial = stimulus.curTrial + 1;
% Set the current task
if task.thistrial.catch > 0
switchTasks = [2 1];
task.thistrial.task = switchTasks(stimulus.runs.curTask);
% edit seglen
task.thistrial.seglen(stimulus.seg.mask) = .75;
task.thistrial.seglen(stimulus.seg.ISI) = .75;
task.thistrial.seglen(stimulus.seg.resp) = 2.5;
disp('(cohcon) Catch trial.');
else
task.thistrial.task = stimulus.runs.curTask;
end
% Set the missing thistrial vars
task.thistrial.coherence = stimulus.stairInfo.pedestals.coherence(task.thistrial.cohPedestal);
task.thistrial.contrast = stimulus.stairInfo.pedestals.contrast(task.thistrial.conPedestal);
task.thistrial.trialNum = stimulus.curTrial;
% Get the pedestals
[cohTh, conTh, stimulus] = getDeltaPed(task,stimulus);
% Reduce if pedestals are too large
if (task.thistrial.coherence + cohTh) > 1
cohTh = 1 - task.thistrial.coherence;
end
if (task.thistrial.contrast + conTh) > 1
conTh = 1 - task.thistrial.contrast;
end
if stimulus.stablecon
conTh = 0;
end
if stimulus.stablecoh
cohTh = 0;
end
% Save info
task.thistrial.conDelta = conTh;
task.thistrial.cohDelta = cohTh;
if task.thistrial.conSide==1
task.thistrial.lCon = task.thistrial.contrast+task.thistrial.conDelta;
task.thistrial.rCon = task.thistrial.contrast;
else
task.thistrial.rCon = task.thistrial.contrast+task.thistrial.conDelta;
task.thistrial.lCon = task.thistrial.contrast;
end
if task.thistrial.cohSide==1
task.thistrial.lCoh = task.thistrial.coherence+task.thistrial.cohDelta;
task.thistrial.rCoh = task.thistrial.coherence;
else
task.thistrial.rCoh = task.thistrial.coherence+task.thistrial.cohDelta;
task.thistrial.lCoh = task.thistrial.coherence;
end
if stimulus.time
disp(sprintf('(cohcon) Trial %i starting. Length: %i; Coherence: L %.02f; R %.02f Contrast: L %.02f; R %.02f',task.thistrial.trialNum,task.thistrial.time,...
task.thistrial.lCoh,task.thistrial.rCoh,...
task.thistrial.lCon,task.thistrial.rCon));
else
disp(sprintf('(cohcon) Trial %i starting. Coherence: L %.02f; R %.02f Contrast: L %.02f; R %.02f',task.thistrial.trialNum,...
task.thistrial.lCoh,task.thistrial.rCoh,...
task.thistrial.lCon,task.thistrial.rCon));
end
% set the gammaTable for this trial
setGammaTable_flowMax(task.thistrial.contrast + task.thistrial.conDelta);
% set directions
stimulus.dotsL.dir = task.thistrial.dir;
stimulus.dotsR.dir = task.thistrial.dir;
myscreen.flushMode = 0;
stimulus.live.eyeCount = 0;
stimulus.dead = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Runs at the start of each Segment %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [task, myscreen] = startSegmentCallback(task, myscreen)
%%
global stimulus
stimulus.live.mt = 0;
switch task.thistrial.thisseg
case stimulus.seg.ITI
stimulus.live.dots = 0;
stimulus.live.fixColor = stimulus.colors.black;
stimulus.live.catchFix = 0;
case stimulus.seg.stim
stimulus.live.dots = 1;
stimulus.live.fixColor = stimulus.colors.black;
stimulus.live.catchFix = 0;
case stimulus.seg.mask
stimulus.live.dots = 0;
stimulus.live.fixColor = stimulus.colors.black;
stimulus.live.catchFix = 0;
case stimulus.seg.ISI
stimulus.live.dots = 0;
stimulus.live.fixColor = stimulus.colors.black;
stimulus.live.catchFix = 1;
case stimulus.seg.resp
stimulus.live.dots = 0;
stimulus.live.fixColor = stimulus.colors.white;
stimulus.live.catchFix = 1;
end
if stimulus.constant
stimulus.live.dots=1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Refreshes the Screen %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [task, myscreen] = screenUpdateCallback(task, myscreen)
%%
global stimulus
mglClearScreen(0.5);
% check eye pos
if ~stimulus.noeye && task.thistrial.thisseg~=stimulus.seg.ITI
[pos,time] = mglEyelinkGetCurrentEyePos;
if ~any(isnan(pos))
dist = hypot(pos(1),pos(2));
if dist > 5 && stimulus.live.eyeCount > 30
mglTextSet([],32,[1 0 0]);
disp('Eye movement detected!!!!');
mglTextDraw('Eye Movement Detected',[0 0]);
mglFlush
myscreen.flushMode = 1;
stimulus.dead = 1;
return
elseif dist > 5
stimulus.live.eyeCount = stimulus.live.eyeCount + 1;
end
end
end
if stimulus.live.dots==1, stimulus = upDots(task,stimulus,myscreen); end
upFix(task,stimulus);
function upFix(task,stimulus)
%%
if ~(task.thistrial.catch > 0) || stimulus.live.catchFix == 0
mglFixationCross(1.5,1.5,stimulus.live.fixColor);
else
if stimulus.runs.curTask==2
if stimulus.live.fixColor==stimulus.colors.white
mglBltTexture(stimulus.text.mTexW,[0 0]);
else
mglBltTexture(stimulus.text.mTexK,[0 0]);
end
else
if stimulus.live.fixColor==stimulus.colors.white
mglBltTexture(stimulus.text.cTexW,[0 0]);
else
mglBltTexture(stimulus.text.cTexK,[0 0]);
end
end
end
function stimulus = upDots(task,stimulus,myscreen)
if task.thistrial.thisseg==stimulus.seg.stim
lcoh = task.thistrial.lCoh;
rcoh = task.thistrial.rCoh;
lcon = task.thistrial.lCon;
rcon = task.thistrial.rCon;
else
lcoh = stimulus.baseCoh;
rcoh = stimulus.baseCoh;
lcon = stimulus.baseCon;
rcon = stimulus.baseCon;
end
%% Old update code start here
stimulus.dotsL = updateDotsRadial(stimulus.dotsL,lcoh,myscreen,true);
stimulus.dotsR = updateDotsRadial(stimulus.dotsR,rcoh,myscreen,true);
% Correct values for gamma table adjustments
lcon = lcon / stimulus.curMaxContrast;
rcon = rcon / stimulus.curMaxContrast;
% dotsR
% update +contrast
rcon = adjustConToTable(rcon,stimulus)/2;
lcon = adjustConToTable(lcon,stimulus)/2;
mglPoints2(stimulus.dotsR.xdisp(stimulus.dotsR.con==1),stimulus.dotsR.ydisp(stimulus.dotsR.con==1),...
stimulus.dotsR.dotsize,[.5 .5 .5] - rcon);
% update - contrast
mglPoints2(stimulus.dotsR.xdisp(stimulus.dotsR.con==2),stimulus.dotsR.ydisp(stimulus.dotsR.con==2),...
stimulus.dotsR.dotsize,[.5 .5 .5] + rcon);
% dotsL
% update +contrast
mglPoints2(stimulus.dotsL.xdisp(stimulus.dotsL.con==1),stimulus.dotsL.ydisp(stimulus.dotsL.con==1),...
stimulus.dotsL.dotsize,[.5 .5 .5] - lcon);
% update - contrast
mglPoints2(stimulus.dotsL.xdisp(stimulus.dotsL.con==2),stimulus.dotsL.ydisp(stimulus.dotsL.con==2),...
stimulus.dotsL.dotsize,[.5 .5 .5] + lcon);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Adjust contrast to the gamma table %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function conValue = adjustConToTable(conValue,stimulus)
conValue = conValue * stimulus.colors.nUnreserved / 256;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Called When a Response Occurs %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
function [task, myscreen] = getResponseCallback(task, myscreen)
global stimulus
if stimulus.dead
return
end
responseText = {'Incorrect','Correct'};
responsePos = {'Left','Right'};
fixColors = {stimulus.colors.red,stimulus.colors.green};
if any(task.thistrial.whichButton == stimulus.responseKeys)
if task.thistrial.gotResponse == 0
if task.thistrial.task==1
cSide = task.thistrial.cohSide;
else
cSide = task.thistrial.conSide;
end
task.thistrial.correct = task.thistrial.whichButton == stimulus.responseKeys(cSide);
% Store whether this was correct
stimulus.live.fixColor = fixColors{task.thistrial.correct+1};
disp(sprintf('(cohcon) Response %s: %s',responseText{task.thistrial.correct+1},responsePos{find(stimulus.responseKeys==task.thistrial.whichButton)}));
if ~(task.thistrial.catch > 0)
if ~stimulus.nocatch
stimulus.staircases.main{task.thistrial.task,curPedValue(task,false)} = ...
doStaircase('update',stimulus.staircases.main{task.thistrial.task,curPedValue(task,false)},task.thistrial.correct);
else
if stimulus.time
timeOpts = [250 500 1000];
timeOpt = find(timeOpts==task.thistrial.time);
stimulus.staircases.nocatch{task.thistrial.task,curPedValue(task,false),timeOpt} = ...
doStaircase('update',stimulus.staircases.nocatch{task.thistrial.task,curPedValue(task,false),timeOpt},task.thistrial.correct);
else
stimulus.staircases.nocatch{task.thistrial.task,curPedValue(task,false)} = ...
doStaircase('update',stimulus.staircases.nocatch{task.thistrial.task,curPedValue(task,false)},task.thistrial.correct);
end
end
else
stimulus.live.fixColor = stimulus.colors.black; % we never show information about catch trials
stimulus.live.catchFix = 0;
stimulus.staircases.catch{task.thistrial.task,curPedValue(task,true)} = ...
doStaircase('update',stimulus.staircases.catch{task.thistrial.task,curPedValue(task,true)},task.thistrial.correct);
end
else
disp(sprintf('(cohcon) Subject responded multiple times: %i',task.thistrial.gotResponse+1));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% HELPER FUNCTIONS %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
% getDeltaPed %
%%%%%%%%%%%%%%%%%%%%%%%%
function [cohPed, conPed, stimulus] = getDeltaPed(task,stimulus)
%%
if stimulus.time
timeOpts = [250 500 1000];
timeOpt = find(timeOpts==task.thistrial.time);
[cohPed, stimulus.staircases.nocatch{1,curPedVal(task,1),timeOpt}] = doStaircase('testValue',stimulus.staircases.nocatch{1,curPedVal(task,1),timeOpt});
[conPed, stimulus.staircases.nocatch{2,curPedVal(task,2),timeOpt}] = doStaircase('testValue',stimulus.staircases.nocatch{2,curPedVal(task,2),timeOpt});
else
if stimulus.runs.curTask == 1
% COHERENCE MAIN TASK
if ~stimulus.nocatch
[cohPed, stimulus.staircases.main{1,curPedVal(task,1)}] = doStaircase('testValue',stimulus.staircases.main{1,curPedVal(task,1)});
else
[cohPed, stimulus.staircases.nocatch{1,curPedVal(task,1)}] = doStaircase('testValue',stimulus.staircases.nocatch{1,curPedVal(task,1)});
end
if task.thistrial.catch > 0
[conPed, stimulus.staircases.catch{2,curPedVal(task,2)}] = doStaircase('testValue',stimulus.staircases.catch{2,curPedVal(task,2)});
else
conPed = stimulus.stairInfo.increments.contrast(randi(length(stimulus.stairInfo.increments.contrast)));
end
else
% CONTRAST MAIN TASK
if ~stimulus.nocatch
[conPed, stimulus.staircases.main{2,curPedVal(task,2)}] = doStaircase('testValue',stimulus.staircases.main{2,curPedVal(task,2)});
else
[conPed, stimulus.staircases.nocatch{2,curPedVal(task,2)}] = doStaircase('testValue',stimulus.staircases.nocatch{2,curPedVal(task,2)});
end
if task.thistrial.catch > 0
[cohPed, stimulus.staircases.catch{1,curPedVal(task,1)}] = doStaircase('testValue',stimulus.staircases.catch{1,curPedVal(task,1)});
else
cohPed = stimulus.stairInfo.increments.coherence(randi(length(stimulus.stairInfo.increments.coherence)));
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%
% curPedValue %
%%%%%%%%%%%%%%%%%%%%%%%%
function ped = curPedValue(task,iscatch)
%%
switcher = [2 1];
if iscatch
usetask = switcher(task.thistrial.task);
else
usetask = task.thistrial.task;
end
ped = curPedVal(task,usetask);
function ped = curPedVal(task,taskNum)
%%
if taskNum==1
ped = task.thistrial.cohPedestal;
else
ped = task.thistrial.conPedestal;
end
%%%%%%%%%%%%%%%%%%%%%%%%
% initStaircase %
%%%%%%%%%%%%%%%%%%%%%%%%
function stimulus = initStaircase(stimulus)
% we're going to be fucking organized this time and put all the staircases
% in one place.... duh.
stimulus.staircases = struct;
if stimulus.time
% motion first then contrast
for task = 1:2
stimulus.staircases.nocatch{task,1,1} = doStaircase('init','upDown',...
'initialThreshold',stimulus.stairInfo.initThresh.(stimulus.stairInfo.pedOpts{task}),...
'initialStepsize',stimulus.stairInfo.initThresh.(stimulus.stairInfo.pedOpts{task})/3,...
'minThreshold=0.001','maxThreshold=2','stepRule','pest',...
'nTrials=50','maxStepsize=0.2','minStepsize=0.001');
stimulus.staircases.nocatch{task,2,1} = stimulus.staircases.nocatch{task,1,1};
for p = 2:3
stimulus.staircases.nocatch{task,1,p} = stimulus.staircases.nocatch{task,1,1};
stimulus.staircases.nocatch{task,2,p} = stimulus.staircases.nocatch{task,1,1};
end
end
else
%%
stimulus.staircases.catch = cell(2,stimulus.stairInfo.catchP); % task first, pedestal second
stimulus.staircases.main = cell(2,stimulus.stairInfo.mainP);
stimulus.staircases.nocatch = cell(2,stimulus.stairInfo.nocatchP);
if size(stimulus.staircases.catch,2)>1, error('Staircase Initialization Failure'); end
% Catch && Main staircases
for task = 1:2
stimulus.staircases.catch{task,1} = doStaircase('init','upDown',...
'initialThreshold',stimulus.stairInfo.initThresh.(stimulus.stairInfo.pedOpts{task}),...
'initialStepsize',stimulus.stairInfo.initThresh.(stimulus.stairInfo.pedOpts{task})/3,...
'minThreshold=0.001','maxThreshold=2','stepRule','pest',...
'nTrials=50','maxStepsize=0.2','minStepsize=0.001');
stimulus.staircases.main{task,1} = doStaircase('init','upDown',...
'initialThreshold',stimulus.stairInfo.initThresh.(stimulus.stairInfo.pedOpts{task}),...
'initialStepsize',stimulus.stairInfo.initThresh.(stimulus.stairInfo.pedOpts{task})/3,...
'minThreshold=0.001','maxThreshold=2','stepRule','pest',...
'nTrials=50','maxStepsize=0.2','minStepsize=0.001');
end
% NoCatch staircases: Warning, these have different sizes in scan sessions
% motion first then contrast
for task = 1:2
stimulus.staircases.nocatch{task,1} = doStaircase('init','upDown',...
'initialThreshold',stimulus.stairInfo.initThresh.(stimulus.stairInfo.pedOpts{task}),...
'initialStepsize',stimulus.stairInfo.initThresh.(stimulus.stairInfo.pedOpts{task})/3,...
'minThreshold=0.001','maxThreshold=2','stepRule','pest',...
'nTrials=50','maxStepsize=0.2','minStepsize=0.001');
for p = 2:size(stimulus.staircases.nocatch,2)
stimulus.staircases.nocatch{task,p} = stimulus.staircases.nocatch{task,1};
end
end
end
%%
%%%%%%%%%%%%%%%%%%%%%%%
% dispInfo %
%%%%%%%%%%%%%%%%%%%%%%%
function dispInfoNum(stimulus)
%%
trials = 0;
nmain = 0;
ncatch = 0;
ncontrol = 0;
if stimulus.time
for t = 1:2
for p = 1:2
for ti = 1:3
if length(stimulus.staircases.nocatch{t,p,ti})>1
for i = 1:length(stimulus.staircases.nocatch{t,p,ti})
trials = trials + stimulus.staircases.nocatch{t,p,ti}(i).trialNum;
end
else
trials = trials + stimulus.staircases.nocatch{t,p,ti}.trialNum;
end
end
end
end
else
for t = 1:2
cmain = stimulus.staircases.main{t};
for i = 1:length(cmain)
trials = trials + cmain(i).trialNum;
nmain = nmain + cmain(i).trialNum;
end
ccatch = stimulus.staircases.catch{t};
for i = 1:length(ccatch)
trials = trials + ccatch(i).trialNum;
ncatch = ncatch + ccatch(i).trialNum;
end
for n = 1:4
cno = stimulus.staircases.nocatch{t,n};
for i = 1:length(cno)
trials = trials + cno(i).trialNum;
ncontrol = ncontrol + cno(i).trialNum;
end
end
end
end
disp(sprintf('(dispInfo) Subject %s has completed %i trials so far. %i main, %i catch, %i control.',mglGetSID,trials,nmain,ncatch,ncontrol));
function dispInfo(stimulus)
%%
if stimulus.time
%%
data = zeros(2,2,3);
% datas = zeros(2,2,3);
tasks = {'coherence','contrast'};
h = figure; hold on
cmap = brewermap(7,'PuOr');
cols = [5 6 7;3 2 1];
for task = 1:2
for ped = 1:2
for time = 1:3
if stimulus.staircases.nocatch{task,ped,time}(1).trialNum>1
out = doStaircase('threshold',stimulus.staircases.nocatch{task,ped,time},'type','weibull','dispFig=0');
% out =
% doStaircase('threshold',stimulus.staircases.nocatch{task,ped,time}); % no weibull option
data(task,ped,time) = out.threshold;
pedOpts = stimulus.stairInfo.pedestals.(tasks{task});
plot(pedOpts(ped),out.threshold,'o','MarkerFaceColor',cmap(cols(task,time),:),'MarkerEdgeColor',[1 1 1],'MarkerSize',15);
end
% datas(task,ped,time) = out.thresholdSTE;
end
end
end
axis([0 1 0 0.5]);
drawPublishAxis;
return
end
try
%% No-Catch Performance
nocatch = zeros(2,4);
nocatchs = zeros(2,4);
for task = 1:size(nocatch,1)