-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathafmap_grid.m
1185 lines (1012 loc) · 43 KB
/
afmap_grid.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 [ myscreen ] = afmap_grid( varargin )
%
% TODO
% * fix rotation order
% * fix fixation cross task (way too hard at 5,5)
%
%
%ATTENTIONFIELDMAPPING
%
% Map the attention field in the scanner. This function works by having a
% participant perform an asynchronous attention task at fixation or in a
% quarterfield region. A pre-determined poisson process generates random
% flashes of rotating gratings throughout the visual field at low or high
% contrast.
%
% The probe stimuli have three sizes 0.5x0.5, 1x1 or 2x2 deg, to
% help estimate different RF sizes and are placed at 2 degree
% increments. The probability of a probe stimulus turning on is 2.5% per
% TR and the dead time is five seconds. Using poisson processes in this
% way ensures a random distribution at every location that is
% uncorrelated to all other locations. The code starts with a 10 s blank
% and has another 10 s blank every three minutes. Probes are at 20% and
% 80% contrast, each probe lasts two TRs.
%
% The attention task involves performing orientation judgments on gabors
% at a location cued continuously by a circular aperture. Fixation is
% maintained at the center and monitored within a 1.5 deg window. Gabor
% is at full contrast to differentiate from the probe stimuli (and to
% minimize the effect of the probes on performance)
%
% The goal of the task is to provide an independent pRF dataset that can
% be used to build channel encoding models that cover space, contrast,
% and attention. The end goal is to link frontal areas that control
% attention with visual areas.
%
% Project goals:
% (1) Show that we can map pRFs hierarchically throughout all of
% cortex
% (2) Build a visuospatial encoding model that incorporates attention
% (3) Show that our model predicts tuning shifts for non-visuospatial
% features due to spatial attention
%%
global stimulus
stimulus = struct;
%% Initialize Variables
% add arguments later
scan = 0;
plots = 0;
noeye = 0;
debug = 0;
replay = 0;
attend = 0; run = 0; build = 0;
getArgs(varargin,{'scan=1','plots=0','noeye=0','debug=0','replay=0','attend=1','run=0','build=0'});
stimulus.scan = scan;
stimulus.plots = plots;
stimulus.noeye = noeye;
stimulus.debug = debug;
stimulus.replay = replay;
stimulus.overrideRun = run;
stimulus.attend = attend; % controls whether attention mode runs
stimulus.buildOverride = build;
if ~stimulus.attend
warning('*****ATTENTION MODE IS DISABLED*****');
end
clear localizer invisible scan noeye task test2 attend build
%% Replay mode
if any(replay>0)
if ischar(replay)
% a file was called for, load it
loaded = load(replay);
stimulus = loaded.stimulus;
% check that this is actually an afmap file
if isempty(strfind(loaded.task{1}{1}.taskFilename,'afmap'))
disp(sprintf('File %s is not an afmap run.',replay));
return
end
% get the task parameters so that you can sync the replay correctly
disp('GET TASK PARAMETERS');
e = getTaskParameters(loaded.myscreen,loaded.task);
e1 = e{1};
% pull out the trial volumes
stimulus.tVolumes = e{1}.trialVolume;
disp('Stimulus volumes were found at:');
disp(stimulus.tVolumes);
stimulus.replayFile = strcat(replay(1:(strfind(replay,'.mat')-1)),'_replay.mat');
stimulus.replay = true;
else
% do an entire subject
disp('Replay mode initiated');
folder = input('What folder would you like to replay? [Folder]: ');
files = dir(fullfile(folder,'*.mat'));
for fi = 1:length(files)
if isempty(strfind(files(fi).name,'replay')) && isempty(strfind(files(fi).name,'original'))
afmap(sprintf('replay=%s/%s',folder,files(fi).name));
end
end
return;
end
end
%% Open Old Stimfile
if ~stimulus.replay
stimulus.counter = 1;
stimulus.curRun = 1;
if ~isempty(mglGetSID) && isdir(sprintf('~/data/afmap/%s',mglGetSID))
% Directory exists, check for a stimefile
files = dir(sprintf('~/data/afmap/%s/1*mat',mglGetSID));
if length(files) >= 1
fname = files(end).name;
s = load(sprintf('~/data/afmap/%s/%s',mglGetSID,fname));
% copy staircases and run numbers
stimulus.counter = s.stimulus.counter + 1;
stimulus.curRun = s.stimulus.curRun + 1;
stimulus.build = s.stimulus.build;
stimulus.builds = s.stimulus.builds;
stimulus.attention = s.stimulus.attention;
stimulus.staircases = s.stimulus.staircases;
stimulus.order = s.stimulus.order;
stimulus.live.attend = mod(s.stimulus.live.attend+1,3);
if s.stimulus.attend ~= stimulus.attend
error('Cannot continue: stimfile parameters were generated with a different attention mode than you requested. You need to save the existing stimfiles into a backup folder');
end
clear s;
disp(sprintf('(afmap) Data file: %s loaded.',fname));
end
end
end
%% Override run
if ~stimulus.replay && stimulus.overrideRun>0
stimulus.curRun = stimulus.overrideRun;
end
%% Display run info
if ~stimulus.replay
disp('*************************');
disp(sprintf('(afmap) This is scan #%i',stimulus.counter));
disp(sprintf('(afmap) This is run #%i',stimulus.curRun));
disp('*************************');
end
%% Stimulus parameters
if ~stimulus.replay
stimulus.stimX = 25; % max ecc in any direction
stimulus.stimY = 13;
stimulus.stimR = 2; % deg between each stimulus
if mod(stimulus.stimX,stimulus.stimR)==0 || mod(stimulus.stimY,stimulus.stimR)==0
warning('Your stimulus size is not correctly setup');
end
stimulus.stimx = -stimulus.stimX:stimulus.stimR:stimulus.stimX;
stimulus.stimy = -stimulus.stimY:stimulus.stimR:stimulus.stimY;
if any(stimulus.stimx==0) || any(stimulus.stimy==0)
warning('Your stimulus overlaps the fixation cross and the attention task!! You totally fd up!');
end
% how many times each probe location turns on per contrast and size
% condition
stimulus.probeOn = 2;
% how long a probe stays up for (in TR, 4 = 2.0s)
stimulus.probeUp = 4; % this must be EVEN!!
% how long a probe is guaranteed to stay down (in TR, 12 = 6.0s)
stimulus.probeDown = 12;
% stimulus.live will hold what actually gets displayed on the screen
stimulus.live.con = zeros(length(stimulus.stimx),length(stimulus.stimy));
stimulus.live.sz = zeros(length(stimulus.stimx),length(stimulus.stimy));
stimulus.live.ph = zeros(length(stimulus.stimx),length(stimulus.stimy));
stimulus.live.theta = zeros(length(stimulus.stimx),length(stimulus.stimy));
% gratingContrasts and gratingsizes control the possible sizes
stimulus.gratingContrasts = [0.1 1.0];
stimulus.gratingSizes = [0.5 1 2];
% when we are doing the attention task
stimulus.live.attend = 0;
% blank options
stimulus.blanks.none.range = [0 0 0 0];
stimulus.blanks.all.range = [-inf inf -inf inf];
stimulus.blanks.NW.range = [-inf 0 0 inf];
stimulus.blanks.NE.range = [0 inf 0 inf];
stimulus.blanks.SE.range = [0 inf -inf 0];
stimulus.blanks.SW.range = [-inf 0 -inf 0];
stimulus.blanks.opts = {'NW','NE','SE','SW'};
end
%% Attention stimulus
if ~stimulus.replay && ~isfield(stimulus,'attention')
stimulus.attention = struct;
stimulus.attention.attendX = [0 4 4];
stimulus.attention.attendY = [0 4 -4];
for ai = 1:length(stimulus.attention.attendX)
if any(stimulus.attention.attendX(ai)==stimulus.stimx)
warning('Stimulus and attention task are overlapping on the x-axis');
end
end
for ai = 1:length(stimulus.attention.attendY)
if any(stimulus.attention.attendY(ai)==stimulus.stimy)
warning('Stimulus and attention task are overlapping on the y-axis');
end
end
if stimulus.attend
stimulus.attention.rotate = length(stimulus.attention.attendX);
else
stimulus.attention.rotate = 1;
end
stimulus.attention.curAttend = 1;
end
%% Build stimulus
if ~stimulus.replay && ~isfield(stimulus,'build')
stimulus.build = struct;
stimulus.build.curBuild = 0; % will be incremented later
stimulus.build.uniques = 3; % how many unique patterns to generate
stimulus.build.rotate = 3; % how many patterns to rotate through (set to 4 or 5 for 2x repeat runs)
stimulus.build.cycles = 6;
stimulus.build.cycleLength = 120;
stimulus.build.availableTRs = stimulus.build.cycles*stimulus.build.cycleLength; % how long the task should run for
stimulus.build.conditions = length(stimulus.gratingContrasts)*length(stimulus.gratingSizes);
stimulus.build.conditionsRep = stimulus.build.conditions * stimulus.probeOn; % total # of displays per location
if stimulus.build.conditionsRep*(stimulus.probeUp+stimulus.probeDown) > (stimulus.build.availableTRs*2/3) % ~1/3 of the time the screen will be blank
warning('The code has insufficient TRs available for the stimulus program you requested');
keyboard
end
disp(sprintf('Building %i unique probe sequences. Each sequence consists of %i cycles of %i TRs each.',stimulus.build.uniques,stimulus.build.cycles,stimulus.build.cycleLength));
for bi = 1:stimulus.build.uniques
build = struct; % initialize
build.con = zeros(stimulus.build.availableTRs,length(stimulus.stimx),length(stimulus.stimy));
build.sz = zeros(stimulus.build.availableTRs,length(stimulus.stimx),length(stimulus.stimy));
build.ph = zeros(stimulus.build.availableTRs,length(stimulus.stimx),length(stimulus.stimy));
build.theta = zeros(stimulus.build.availableTRs,length(stimulus.stimx),length(stimulus.stimy));
% build the blackout positions for this run
% each cycle goes ALL, {NW/NE/SE/SW}, NONE in 10 s increments (60 s
% total)
% get the rotations for each blackout and put them in place
blackout = cell(stimulus.build.cycles,6);
for c = 1:stimulus.build.cycles
order = randperm(4);
blackout{c,1} = 'none';
for r = 1:4
blackout{c,r+1} = stimulus.blanks.opts{order(r)};
end
blackout{c,6} = 'all';
end
build.blackout = blackout;
% now compute the X/Y ranges that are subject to the blackout
bminx = zeros(stimulus.build.cycles,6);
bmaxx = zeros(stimulus.build.cycles,6);
bminy = zeros(stimulus.build.cycles,6);
bmaxy = zeros(stimulus.build.cycles,6);
for c = 1:stimulus.build.cycles
for d = 1:6
bminx(c,d) = stimulus.blanks.(blackout{c,d}).range(1);
bmaxx(c,d) = stimulus.blanks.(blackout{c,d}).range(2);
bminy(c,d) = stimulus.blanks.(blackout{c,d}).range(3);
bmaxy(c,d) = stimulus.blanks.(blackout{c,d}).range(4);
end
end
disppercent(-1/length(stimulus.stimx));
for x = 1:length(stimulus.stimx)
for y = 1:length(stimulus.stimy)
% determine which cycles will get which of the conditions
% on which of their repeats
% 1 2 3 4
% cycle con size repeat
redo = true;
while redo
conditionTiming = zeros(stimulus.build.conditionsRep,4);
count = 1;
for i = 1:length(stimulus.gratingContrasts)
for j = 1:length(stimulus.gratingSizes)
for k = 1:stimulus.probeOn
conditionTiming(count,:) = [randi(stimulus.build.cycles) i j k];
count = count + 1;
end
end
end
redo = false;
for c = 1:stimulus.build.cycles
cycleCount = sum(conditionTiming(:,1)==c);
if (cycleCount==0) || (cycleCount >= (2 / stimulus.build.cycles * size(conditionTiming,1)))
% if we accidentally generated a set of cycles
% where there is a poor distribution of probes,
% we just repeat it and do it again.
redo = true;
end
end
end
% pre-compute the blackout positions
sx = stimulus.stimx(x);
sy = stimulus.stimy(y);
bpos = logical((sx>bminx).*(sx<bmaxx).*(sy>bminy).*(sy<bmaxy));
for c = 1:stimulus.build.cycles
% get all the indexes
cStart = (c-1) * stimulus.build.cycleLength + 1;
cEnd = c * stimulus.build.cycleLength;
indexes = cStart:cEnd;
% get the blackout indexes for this cycle
for d = 1:6
if bpos(c,d)
% blackout
rmindexes = cStart-1 + (((d-1)*20+1):(d*20));
indexes = setdiff(indexes,rmindexes);
end
end
% for each cycle, build a stimulus display timeseries,
% this means picking the actual timing of the various
% events within the time block
events = conditionTiming(conditionTiming(:,1)==c,:);
eventTimes = randsample(indexes,size(events,1));
attempts = 0;
while any(diff(eventTimes)<(stimulus.probeUp+stimulus.probeDown))
attempts = attempts + 1;
eventTimes = randsample(indexes,size(events,1));
% if attempts>10000
% disp(sprintf('(afmap) Attempted %i times to create a functional cycle and failed--stimulus properties are fd up',attempts));
% keyboard
% end
end
if attempts>100000
warning(sprintf('It took a whole lot of attempts: %i, to build that cycle',attempts));
end
for ei = 1:size(events,1)
eidxs = eventTimes(ei):(eventTimes(ei)+stimulus.probeUp-1);
build.con(eidxs,x,y) = events(ei,2);
build.sz(eidxs,x,y) = events(ei,3);
build.ph(eidxs,x,y) = repmat([1 2],1,length(eidxs)/2);
build.theta(eidxs,x,y) = rand*2*pi;
end
end
end
disppercent(x/length(stimulus.stimx));
end
disppercent(inf/length(stimulus.stimx));
% test code
% % keyboard
% % figure
% % colormap('gray');
% % caxis([0 1]);
% % % build.con = build.con>0;
% % tb = build.con/max(build.con(:));
% % for i = 1:720
% % imagesc(squeeze(tb(i,:,:)));
% % pause(.01);
% % end
% test code end
disp(sprintf('(afmap) Pre-build of build %i has finished (will be saved with stimfile).',bi));
stimulus.builds{bi} = build;
end
disp(sprintf('(afmap) Pre-build complete. Created %i unique builds which will rotate every %i runs.',stimulus.build.uniques,stimulus.build.rotate));
end
%% Build the order
if ~stimulus.replay && ~isfield(stimulus,'order')
stimulus.order = struct;
stimulus.order.BaseAtt = zeros(1,stimulus.build.rotate*stimulus.attention.rotate);
for ai = 1:stimulus.attention.rotate
stimulus.order.BaseAtt((ai-1)*stimulus.build.rotate+1:ai*stimulus.build.rotate) = ai*ones(1,stimulus.build.rotate);
end
stimulus.order.BaseBui = repmat(1:stimulus.build.rotate,1,stimulus.attention.rotate);
stimulus.order.n = length(stimulus.order.BaseAtt);
stimulus.order.curOrder = [];
stimulus.order.doneMat = zeros(stimulus.attention.rotate,stimulus.build.rotate);
end
%% Staircase
if ~stimulus.replay
if ~isfield(stimulus,'staircases')
disp('(afmap) WARNING: New staircase');
initStair();
else
resetStair();
end
end
%% Get the current build number
if ~stimulus.replay
% need to set: stimulus.build.curBuild and stimulus.attention.curAttend
if stimulus.curRun > length(stimulus.order.curOrder)
nOrder = randperm(stimulus.order.n);
stimulus.order.curOrder = [stimulus.order.curOrder nOrder];
end
orderIdx = stimulus.order.curOrder(stimulus.curRun);
stimulus.build.curBuild = stimulus.order.BaseBui(orderIdx);
if stimulus.buildOverride
stimulus.build.curBuild = stimulus.buildOverride;
end
stimulus.attention.curAttend = stimulus.order.BaseAtt(orderIdx);
stimulus.attention.curAttendX = stimulus.attention.attendX(stimulus.attention.curAttend);
stimulus.attention.curAttendY = stimulus.attention.attendY(stimulus.attention.curAttend);
stimulus.staircase = stimulus.staircases{stimulus.attention.curAttend};
end
%% Display completion information
if ~stimulus.replay
strs = {};
initStrs = {'Build\t','#\t'};
for bi = 1:stimulus.build.rotate
if bi==1
strs{end+1} = sprintf('\t');
elseif bi>(length(initStrs)+1)
strs{end+1} = sprintf('\t');
else
strs{end+1} = sprintf(initStrs{bi-1});
end
for ai = 1:stimulus.attention.rotate
strs{end} = sprintf('%s%i\t',strs{end},stimulus.order.doneMat(ai,bi));
end
end
if stimulus.attend
disp('******************************');
disp(sprintf('\tAttention condition'));
% disp(aconds);
for bi = 1:stimulus.build.rotate
disp(sprintf('%s',strs{bi}));
end
disp('******************************');
end
end
%% Display build info
if ~stimulus.replay
disp('******************************');
disp(sprintf('(afmap) Build %i selected',stimulus.build.curBuild));
disp(sprintf('(afmap) Attending X: %i Y: %i selected',stimulus.attention.curAttendX,stimulus.attention.curAttendY));
disp('******************************');
end
%% If tVolumes are not % 120 = 1, then we need to adjust the build
if stimulus.replay
m120 = mod(stimulus.tVolumes,120);
if any(m120>1)
disp('WARNING: Volume timing failed. Build output must be adjusted to contain additional blanks.');
% get the indexes for each build (i.e. 1:120, 121:240, etc)
def = [1:120:720];
bIndexes = zeros(6,120); nIndexes = zeros(6,120);
for bi = 1:length(def)
bIndexes(bi,:) = def(bi):(def(bi)+119);
nIndexes(bi,:) = stimulus.tVolumes(bi):(stimulus.tVolumes(bi)+119);
end
% save orig
stimulus.grid.preTRfix = stimulus.grid;
nMax = max(nIndexes(:));
orig_sz = size(stimulus.grid.con);
ncon = zeros(nMax,orig_sz(2),orig_sz(3));
nsz = ncon;
nph = ncon;
ntheta = ncon;
for bi = 1:size(bIndexes,1)
disp(sprintf('Build %i needs to be offset by %i from %i to %i',bi,m120(bi),def(bi),stimulus.tVolumes(bi)));
ncon(nIndexes(bi,:),:,:) = stimulus.grid.con(bIndexes(bi,:),:,:);
nsz(nIndexes(bi,:),:,:) = stimulus.grid.sz(bIndexes(bi,:),:,:);
nph(nIndexes(bi,:),:,:) = stimulus.grid.ph(bIndexes(bi,:),:,:);
ntheta(nIndexes(bi,:),:,:) = stimulus.grid.theta(bIndexes(bi,:),:,:);
end
% replace
stimulus.grid.con = ncon(1:orig_sz(1),:,:);
stimulus.grid.sz = nsz(1:orig_sz(1),:,:);
stimulus.grid.ph = nph(1:orig_sz(1),:,:);
stimulus.grid.theta = ntheta(1:orig_sz(1),:,:);
end
end
%% Load the current build
if ~stimulus.replay
% stimulus.grid is used to track the grid for this run -- we will
% update this every time the screen changes and save the time it
% occurred
stimulus.grid.buildNumber = stimulus.build.curBuild;
stimulus.grid.t = zeros(1,stimulus.build.availableTRs);
stimulus.grid.con = stimulus.builds{stimulus.build.curBuild}.con;
stimulus.grid.sz = stimulus.builds{stimulus.build.curBuild}.sz;
stimulus.grid.ph = stimulus.builds{stimulus.build.curBuild}.ph;
stimulus.grid.theta = stimulus.builds{stimulus.build.curBuild}.theta;
disp(sprintf('(afmap) Build %i loaded from pre-build',stimulus.build.curBuild));
end
%% Setup attention
if ~stimulus.replay
stimulus.attendX = [0 5 5];
stimulus.attendY = [0 5 -5];
stimulus.live.aX = stimulus.attendX(stimulus.live.attend+1);
stimulus.live.aY = stimulus.attendY(stimulus.live.attend+1);
end
%% Setup Screen
if stimulus.replay
myscreen = initScreen('replayScreen');
else
myscreen = initScreen('VPixx');
end
% set background to grey
myscreen.background = 0.5;
%% Plot and return
if stimulus.plots==2
dispInfo(stimulus);
return
end
%% Initialize Stimulus
if ~stimulus.replay
myscreen.stimulusNames{1} = 'stimulus';
if ~isfield(stimulus.live,'grating')
localInitStimulus();
end
stimulus.responseKeys = [1 2]; % left right
else
localInitStimulus();
end
%% Colors
stimulus.colors.white = [1 1 1]; stimulus.colors.red = [1 0 0];
stimulus.colors.green = [0 1 0]; stimulus.colors.black = [0 0 0];
% % initGammaTable(myscreen);
% 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 = [1 0 0; 0 0 0]; % fixation cross colors
% stimulus.colors.reservedTop = [1 1 1; 0 1 0]; % correct/incorrect colors
% stimulus.colors.black = 1/255; stimulus.colors.white = 254/255;
% stimulus.colors.red = 0/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;
%% Setup Probe Task
task{1}{1} = struct;
% task waits for fixation on first segment
if stimulus.replay
task{1}{1}.waitForBacktick = 0;
task{1}{1}.seglen = repmat(0.050,1,120);
else
task{1}{1}.waitForBacktick = 1;
task{1}{1}.seglen = repmat(0.500,1,120);
if stimulus.scan
task{1}{1}.seglen(end) = 0.050; % make the last segment short so that it will synchtovol and hopefully align the runs
end
end
stimulus.seg.stim = 1;
task{1}{1}.getResponse = 0;
task{1}{1}.numTrials = stimulus.build.cycles;
task{1}{1}.random = 0;
if ~stimulus.replay && stimulus.scan
task{1}{1}.synchToVol = zeros(1,length(task{1}{1}.seglen));
task{1}{1}.synchToVol(end) = 1;
end
task{1}{1}.randVars.calculated.probesOn = nan;
%% Setup Attention Task
stimulus.curTrial = 0;
if ~stimulus.replay
global fixStimulus %#ok<TLEV>
fixStimulus.diskSize = 0.75;
fixStimulus.fixWidth = 1;
fixStimulus.fixLineWidth = 3;
fixStimulus.stimTime = 0.35;
fixStimulus.interTime = 1.4;
fixStimulus.stairUsePest = 1;
fixStimulus.responseTime = 2;
fixStimulus.staircase = stimulus.staircase;
fixStimulus.pos = [stimulus.attention.curAttendX stimulus.attention.curAttendY];
[task{2}, myscreen] = gruFixStairInitTask(myscreen);
% task{2}{1} = struct;
% task{2}{1}.waitForBacktick = 0;
% % task waits for fixation on first segment
% task{2}{1}.segmin = [0.500 1 0.200 1];
% task{2}{1}.segmax = [2.500 1 0.200 1];
%
% stimulus.seg.ITI = 1;
% stimulus.seg.delay1 = 2;
% stimulus.seg.stim = 3;
% stimulus.seg.resp = 4;
%
% task{2}{1}.synchToVol = [0 0 0 0];
% task{2}{1}.getResponse = [0 0 0 1];
%
% task{2}{1}.numTrials = Inf;
%
% task{2}{1}.parameter.rotation = [-1 1];
%
% task{2}{1}.random = 1;
%
% if stimulus.scan
% task{2}{1}.synchToVol = 1;
% end
%
% task{2}{1}.randVars.calculated.resp = nan;
% task{2}{1}.randVars.calculated.correct = nan;
end
%% Full Setup
% Initialize task (note phase == 1)
for phaseNum = 1:length(task{1})
[task{1}{phaseNum}, myscreen] = initTask(task{1}{phaseNum},myscreen,@startSegmentCallback1,@screenUpdateCallback1,[],@startTrialCallback1,[],[]);
% [task{2}{phaseNum}, myscreen] = initTask(task{2}{phaseNum},myscreen,@startSegmentCallback2,@screenUpdateCallback2,@getResponseCallback2,@startTrialCallback2,[],[]);
end
%% EYE CALIB
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% run the eye calibration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~stimulus.replay
myscreen = eyeCalibDisp(myscreen);
% let the user know
disp(sprintf('(afmap) Starting run number: %i.',stimulus.counter));
end
%% Main Task Loop
% setGammaTable(1);
if stimulus.replay
mglClearScreen(0);
mglFlush;
mglClearScreen(0);
else
mglClearScreen(0.5); %mglFixationCross(1,1,stimulus.colors.white);
if stimulus.attention.curAttendX>0 || stimulus.attention.curAttendY > 0
mglFixationCross(1,3,stimulus.colors.black);
end
mglFlush
mglClearScreen(0.5); %mglFixationCross(1,1,stimulus.colors.white);
if stimulus.attention.curAttendX>0 || stimulus.attention.curAttendY > 0
mglFixationCross(1,3,stimulus.colors.black);
end
end
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);
if length(task)>1
[task{2}, myscreen, phaseNum] = updateTask(task{2},myscreen,phaseNum);
end
% flip screen
myscreen = tickScreen(myscreen,task);
end
% task ended
mglClearScreen(0.5);
mglTextSet([],32,stimulus.colors.white);
% get count
mglTextDraw('Please wait',[0 0]);
mglFlush
myscreen.flushMode = 1;
% save stimulus
if stimulus.replay
s = load(replay);
screenWidth = myscreen.screenWidth; screenHeight = myscreen.screenHeight;
imageWidth = myscreen.imageWidth; imageHeight = myscreen.imageHeight;
[pRFstim.x, pRFstim.y] = ndgrid(-imageWidth/2:imageWidth/(screenWidth-1):imageWidth/2, -imageHeight/2:imageHeight/(screenHeight-1):imageHeight/2);
pRFstim.t = 1:size(stimulus.frames,3);
if size(stimulus.frames,3)~=720
warning('Number of frames does not match the expected length (720)--padding end with blank screen');
input('Press [enter] to confirm: ');
stimulus.frames(:,:,end:720) = 0;
end
pRFstim.im = stimulus.frames;
s.pRFStimImage = pRFstim;
save(replay,'-struct','s');
end
% save info
if ~stimulus.replay
% save which run we just finished
stimulus.order.doneMat(stimulus.attention.curAttend,stimulus.build.curBuild) = stimulus.order.doneMat(stimulus.attention.curAttend,stimulus.build.curBuild)+1;
% save staircase
stimulus.staircase = fixStimulus.staircase;
stimulus.staircases{stimulus.attention.curAttend} = stimulus.staircase;
end
% if we got here, we are at the end of the experiment
myscreen = endTask(myscreen,task);
if ~stimulus.replay && stimulus.plots
disp('(afmap) Displaying plots');
dispInfo(stimulus);
end
%%%%%%%%%%%%%%%%%%%%%%%%% EXPERIMENT OVER: HELPER FUNCTIONS FOLLOW %%%%%%%%
function [task, myscreen] = startTrialCallback1(task,myscreen)
global stimulus
disp(sprintf('(afmap) Starting cycle %01.0f',(stimulus.curTrial/120)+1));
% disppercent(-1/120,'(afmap) Running: ');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Runs at the start of each Trial %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [task, myscreen] = startSegmentCallback1(task,myscreen)
%%
global stimulus
stimulus.curTrial = stimulus.curTrial + 1;
stimulus.live.con = squeeze(stimulus.grid.con(stimulus.curTrial,:,:));
stimulus.live.sz = squeeze(stimulus.grid.sz(stimulus.curTrial,:,:));
stimulus.live.ph = squeeze(stimulus.grid.ph(stimulus.curTrial,:,:));
stimulus.live.theta = squeeze(stimulus.grid.theta(stimulus.curTrial,:,:));
stimulus.grid.t(stimulus.curTrial) = mglGetSecs;
if stimulus.replay
mglClearScreen(0);
drawGratings();
mglFlush
mglClearScreen(0);
drawGratings();
myscreen.flushMode = 1;
else
mglClearScreen();
drawGratings();
if stimulus.attention.curAttendX>0 || stimulus.attention.curAttendY > 0
mglFixationCross(1,3,stimulus.colors.black);
end
drawFix(myscreen);
mglFlush;
mglClearScreen();
drawGratings();
if stimulus.attention.curAttendX>0 || stimulus.attention.curAttendY > 0
mglFixationCross(1,3,stimulus.colors.black);
end
drawFix(myscreen);
end
% draw gratings for probe task
if stimulus.replay
mglFlush % the screen will blank after the frame, but whatever
frame = mglFrameGrab;
if ~isfield(stimulus,'frames')
stimulus.frames = zeros(myscreen.screenWidth,myscreen.screenHeight,stimulus.build.availableTRs);
end
stimulus.frames(:,:,stimulus.curTrial) = frame(:,:,1);
end
% disp(sprintf('(afmap) Starting trial %01.0f',stimulus.curTrial));
% disppercent(stimulus.curTrial/120,'(afmap) Running: ');
function drawFix(myscreen)
global fixStimulus;
if fixStimulus.trainingMode,mglClearScreen;end
if ~isempty(fixStimulus.displayText)
mglBltTexture(fixStimulus.displayText,fixStimulus.displayTextLoc);
end
mglGluDisk(fixStimulus.pos(1),fixStimulus.pos(2),fixStimulus.diskSize*[1 1],myscreen.background,60);
mglFixationCross(fixStimulus.fixWidth,fixStimulus.fixLineWidth,fixStimulus.thisColor,fixStimulus.pos);
function drawGratings
global stimulus
for xi = 1:length(stimulus.stimx)
for yi = 1:length(stimulus.stimy)
if stimulus.live.con(xi,yi)>0
x = stimulus.stimx(xi);
y = stimulus.stimy(yi);
con = stimulus.live.con(xi,yi);
sz = stimulus.live.sz(xi,yi);
ph = stimulus.live.ph(xi,yi);
theta = stimulus.live.theta(xi,yi);
if stimulus.replay
% just draw a circle
% /2 because the FWHM defines a diameter of 1/2/3 degree
mglBltTexture(stimulus.gaussian(con,sz,ph),[x y],0,0,0);
% mglFillOval(x,y,repmat(stimulus.gratingSizes(sz)/(2*sqrt(2*log(2)))*2,1,2),stimulus.gratingContrasts(con)*[1 1 1]);
else
mglBltTexture(stimulus.grating(con,sz,ph),[x y],0,0,theta*180/pi);
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Refreshes the Screen %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [task, myscreen] = screenUpdateCallback1(task, myscreen)
%%
% global stimulus
function [task, myscreen] = startTrialCallback2(task,myscreen)
%%
global stimulus
stimulus.curTrial = stimulus.curTrial + 1;
[rotation, stimulus.staircase] = doStaircase('testValue',stimulus.staircase);
task.thistrial.rotation = rotation*task.thistrial.rotation;
rotText = {'-','+'};
disp(sprintf('Trial %i: %s%01.2f',stimulus.curTrial,rotText{1+(task.thistrial.rotation>0)},abs(task.thistrial.rotation)*180/pi));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Refreshes the Screen %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Runs at the start of each Segment %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [task, myscreen] = startSegmentCallback2(task, myscreen)
%%
global stimulus
stimulus.live.fix = 1;
stimulus.live.stim = 0;
stimulus.live.fixColor = stimulus.colors.white;
if task.thistrial.thisseg==stimulus.seg.ITI
stimulus.live.fixColor = stimulus.colors.black;
elseif task.thistrial.thisseg == stimulus.seg.stim
stimulus.live.rotation = task.thistrial.rotation*180/pi;
stimulus.live.stim = 1;
% elseif task.thistrial.thisseg == stimulus.seg.stim2
% stimulus.live.rotation = task.thistrial.rotation*180/pi;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Refreshes the Screen %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [task, myscreen] = screenUpdateCallback2(task, myscreen)
%%
global stimulus
% mglClearScreen();
if stimulus.live.fix
upFix(stimulus);
end
if stimulus.live.stim
upStim(stimulus);
end
upAttend(stimulus);
function upAttend(stimulus)
%%
for i = 1:8
mglGluPartialDisk(stimulus.live.aX,stimulus.live.aY,0.99,1.01,(i-1)*360/8-11.25,360/16,stimulus.colors.white);
end
function upFix(stimulus)
%%
% mglGluAnnulus(0,0,1.5,1.55,stimulus.live.fixColor,64);
mglFixationCross(1,1,stimulus.live.fixColor);
function upStim(stimulus)
mglBltTexture(stimulus.grating(3,1),[stimulus.live.aX,stimulus.live.aY],0,0,stimulus.live.rotation+90);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Called When a Response Occurs %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
function [task, myscreen] = getResponseCallback2(task, myscreen)
global stimulus
colors = [stimulus.colors.red;stimulus.colors.green];
text = {'Incorrect','Correct'};
stext = {'Left','Right'};
if any(task.thistrial.whichButton==stimulus.responseKeys)
if task.thistrial.gotResponse==0
task.thistrial.correct = (task.thistrial.whichButton==1 && task.thistrial.rotation>0) || (task.thistrial.whichButton==2 && task.thistrial.rotation<0);
stimulus.staircase = doStaircase('update',stimulus.staircase,task.thistrial.correct);
stimulus.live.fixColor = colors(task.thistrial.correct+1,:);
disp(sprintf('Subject responded %s: %s',stext{task.thistrial.whichButton},text{task.thistrial.correct+1}));
else
disp(sprintf('Subject responded multiple times: %i',task.thistrial.gotResponse));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% HELPER FUNCTIONS %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function initStair()
global stimulus
for ai = 1:stimulus.attention.rotate
stimulus.staircases{ai} = doStaircase('init','upDown',...
'initialThreshold',0.40,...
'initialStepsize',0.03,...
'minThreshold=0.0001','maxThreshold=0.4','stepRule','pest',...
'nTrials=80','maxStepsize=0.2','minStepsize=0.0001');
end
function resetStair()
global stimulus
for ai = 1:stimulus.attention.rotate
s = stimulus.staircases{ai};
if doStaircase('stop',s)
disp('(afmap) Staircase is being reset');
s(end+1) = doStaircase('init',s(end));
if s(end).s.threshold>1
disp('(afmap) Bad staircase threshold: setting to 1');
s(end).s.threshold=1;
elseif s(end).s.threshold<0
disp('(afmap) Bad staircase threshold: setting to 0.05');
s(end).s.threshold=0.05;
end
stimulus.staircases{ai} = s;
end
end
function [trials] = totalTrials()
%%
% Counts trials + estimates the threshold based on the last 500 trials
% get the files list
files = dir(fullfile(sprintf('~/data/afmap/%s/17*stim*.mat',mglGetSID)));