-
Notifications
You must be signed in to change notification settings - Fork 6
/
RealTimePunisherDisplay_CLOUD.m
executable file
·895 lines (773 loc) · 40.6 KB
/
RealTimePunisherDisplay_CLOUD.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
function [blockData] = RealTimePunisherDisplay_CLOUD(toml_file, runNum)
%%%%%%% LOAD CONFIGURATIONS FROM TOML FILE
%Display_Cfg;
addpath(genpath('matlab-toml'));
raw_text = fileread(toml_file);
Cfg = toml.decode(raw_text);
subjectNum = Cfg.session.subjectNum;
subjectDay = Cfg.session.subjectDay;
subjectDayInt = floor(subjectDay); get the actual day number
useButtonBox = Cfg.session.useButtonBox;
rtData = Cfg.session.rtData;
debugMode = Cfg.session.debugMode;
usePyOutput = Cfg.session.usePyOutput;
%%%%%%% LOAD CONFIGURATIONS FROM TOML FILE
Screen('Preference', 'SkipSyncTests', 1);
fprintf('Running for subject %i, run% i, day %i\n', subjectNum,runNum,subjectDay);
fprintf('usebuttonbox: %i\nrealtimedata: %i\ndebug: %i\nusePyOutput: %i\n', useButtonBox,rtData,debugMode, usePyOutput);
seed = sum(100*clock); %get random seed
RandStream.setGlobalStream(RandStream('mt19937ar','seed',seed));%set seed
%%%%%%%
% function [blockData] = RealTimePunisherDisplay(dataDirHeader,subjectNum,subjectName,runNum,useButtonBox,fMRI,rtData,debug)
%
% Face/house attention experiment with real-time classifier feedback
%
%
% REQUIRED INPUTS:
% - dataDirHeader: where the volume files are being sent to
% - subjectNum: participant number [any integer]
% if subjectNum = 0, no information will be saved
% - subjecsubjectNametName: ntblab subject naming convention [MMDDYY#_REALTIME02]
% - matchNum: if subject is yoked to a subject & which subject
% - runNum: run number [any integer]
% - useButtonBox:whether using skyra realtime computer [1/0]
% - fMRI: whether collecting fMRI data [scannumber if yes/0 if not]q5
% - rtData: whether to give feedback in realtime [1/0]
% - debug: whether debugging [1/0]
%
% OUTPUTS
% - testTiming: elapsed time for each iteration of SVM testing
% - blockData:
%
% Written by: Nick Turk-Browne
% Editied by: Megan deBettencourt
% Version: 2.0
% Last modified: 10/14/11
%% check inputs
KbName('UnifyKeyNames');
addpath(genpath('/opt/psychtoolbox/'))
Screen('Preference', 'SkipSyncTests', 1);
%check that there is a sufficient number of inputs
if ~isnumeric(subjectNum)
error('subjectNum must be a number');
end
if ~isnumeric(runNum)
error('runNum must be a number');
end
if (useButtonBox~=1) && (useButtonBox~=0)
error('useButtonBox must be either 1 (if at scanner) or 0 (if not)')
end
if (rtData~=1) && (rtData~=0)
error('rtData must be either 1 (if returning feedback) or 0 (if not)')
end
if (debugMode~=1) && (debugMode~=0)
error('debugMode must be either 1 (if debugging) or 0 (if not)')
end
%% Boilerplate
if (~debugMode) %so that when debugging you can do other things
%Screen('Preference', 'SkipSyncTests', 1);
ListenChar(2); %prevent command window output
HideCursor; %hide mouse cursor
else
Screen('Preference', 'SkipSyncTests', 2);
end
%initialize system time calls
seed = sum(100*clock); %get random seed
GetSecs;
%% Experimental Parameters
%stimulus number parameters
categStr = {'sc','fa'};
typeStr = {'stab','rtfd'};
% block timing
instructDur = 1; % secs
instructTRnum = 1; % TRs
%fixationOn = TR-.3; % secs
% trial timing
stimDur = 1; % secs
respWindow = .85; % secs
deltat = .1; % secs
allowance = .05; % secs
% display parameters
textColor = 0;
textFont = 'Arial';
textSize = 35;
textSpacing = 25;
fixColor = 0;
respColor = 255;
backColor = 127;
imageSize = 256; % assumed square %MdB check image size
destSize = 518; % size that we want image
fixationSize = 4;% pixels
progWidth = 400; % image loading progress bar
progHeight = 20;
minimumDisplay = 0.25;
% how to average the faces and scenes
attImgPropPhase1 = .5;
attImgPropPhase2 = .5;
% new: to use during the stable trials
faceProp = 0.6;
sceneProp = 1-faceProp;
% function mapping classifier output to attended image proportion
% this has the constraints built in
%gain = 2.75;
gain = 2.3;
x_shift = .2;
%y_shift = .05;
y_shift = 0.12;
steepness = .9;
ScreenResX = 1280;
ScreenResY = 720;
%% Response Mapping and Counterbalancing
% skyra: use current design button box (keys 1,2,3,4)
KbName('UnifyKeyNames');
LEFT = KbName('1!');
subj_keycode = LEFT;
DEVICENAME = 'Current Designs, Inc. 932';
if useButtonBox && (~debugMode)
[index devName] = GetKeyboardIndices;
for device = 1:length(index)
if strcmp(devName(device),DEVICENAME)
DEVICE = index(device);
end
end
else
% let's set it to look for the Dell keyboard instead
%DEVICENAME = 'Dell KB216 Wired Keyboard';
DEVICENAME = 'DELL Dell QuietKey Keyboard';
[index devName] = GetKeyboardIndices;
for device = 1:length(index)
if strcmp(devName(device),DEVICENAME)
DEVICE = index(device);
end
end
%DEVICE = -1;
end
TRIGGER = '5%';
%TRIGGER ='=+' put in for Princeton scanner
TRIGGER_keycode = getKeys(TRIGGER);
% counterbalancing response mapping based on subject assignment
% correctResp spells out the responses for {INDOOR,OUTDOOR,MALE,FEMALE}
respMap = mod(subjectNum-1,4)+1;
switch (respMap)
case 1
sceneInstruct = 'Places: respond if it is an indoor place. DO NOT respond if it is an outdoor place';
faceInstruct = 'Faces: respond if it is a male face. DO NOT respond if it is a female face';
sceneShorterInstruct = 'indoor places';
faceShorterInstruct = 'male faces';
case 2
sceneInstruct = 'Places: respond if it is an outdoor place. DO NOT respond if it is an indoor place';
faceInstruct = 'Faces: respond if it is a female face. DO NOT respond if it is a male face';
sceneShorterInstruct = 'outdoor places';
faceShorterInstruct = 'female faces';
case 3
sceneInstruct = 'Places: respond if it is an outdoor place. DO NOT respond if it is an indoor place';
faceInstruct = 'Faces: respond if it is a male face. DO NOT respond if it is a female face';
sceneShorterInstruct = 'outdoor places';
faceShorterInstruct = 'male faces';
case 4
sceneInstruct = 'Places: respond if it is an indoor place. DO NOT respond if it is an outdoor place';
faceInstruct = 'Faces: respond if it is a female face. DO NOT respond if it is a male face';
sceneShorterInstruct = 'indoor places';
faceShorterInstruct = 'female faces';
otherwise
error('Impossible response mapping!');
end
%% Initialize Screens
screenNumbers = Screen('Screens');
% show full screen if real, otherwise part of screen
if debugMode
screenNum = 0;
else
screenNum = screenNumbers(end);
end
%retrieve the size of the display screen
if debugMode
screenX = 800;
screenY = 800;
else
% first just make the screen tiny
[screenX screenY] = Screen('WindowSize',screenNum);
% put this back in!!!
windowSize.degrees = [51 30];
resolution = Screen('Resolution', screenNum);
%resolution = Screen('Resolution', 0); % REMOVE THIS AFTERWARDS!!
%windowSize.pixels = [resolution.width/2 resolution.height];
%screenX = windowSize.pixels(1);
%screenY = windowSize.pixels(2);
% new: setting resolution manually
% for Penn
screenX = 1920;
screenY = 1080;
% for Princeton
%screenX = 1280;
%screenY = 720;
% %to ensure that the images are standardized (they take up the same degrees of the visual field) for all subjects
% if (screenX ~= ScreenResX) || (screenY ~= ScreenResY)
% fprintf('The screen dimensions may be incorrect. For screenNum = %d,screenX = %d (not 1152) and screenY = %d (not 864)',screenNum, screenX, screenY);
% end
end
%create main window
% ACM: took out if statement because specifying top doesn't work on penn
% comp
%if (useButtonBox)%scanner display monitor has error with inputs of screen size
% mainWindow = Screen(screenNum,'OpenWindow',backColor);
%else
mainWindow = Screen(screenNum,'OpenWindow',backColor,[0 0 screenX screenY]);
%end
ifi = Screen('GetFlipInterval', mainWindow);
slack = ifi/2;
% details of main window
centerX = screenX/2; centerY = screenY/2;
Screen(mainWindow,'TextFont',textFont);
Screen(mainWindow,'TextSize',textSize);
% placeholder for images
imageRect = [0,0,imageSize,imageSize];
% position of images
%centerRect = [centerX-imageSize/2,centerY-imageSize/2,centerX+imageSize/2,centerY+imageSize/2];
centerRect = [centerX-destSize/2,centerY-destSize/2,centerX+destSize/2,centerY+destSize/2];
% position of fixation dot
fixDotRect = [centerX-fixationSize,centerY-fixationSize,centerX+fixationSize,centerY+fixationSize];
% image loading progress bar
progRect = [centerX-progWidth/2,centerY-progHeight/2,centerX+progWidth/2,centerY+progHeight/2];
%% Load or Initialize Real-Time Data & Staircasing Parameters
dataDirHeader = pwd;
dataHeader = fullfile(dataDirHeader,[ 'data/subject' num2str(subjectNum)]);
dayHeader = [dataHeader '/day' num2str(subjectDay)];
runHeader = [dayHeader '/run' num2str(runNum)];
classOutputDir = [runHeader '/classoutput'];
fname = findNewestFile(runHeader, fullfile(runHeader, ['blockdatadesign_' num2str(runNum) '*.mat']));
%fn = ls([runHeader '/blockdatadesign_' num2str(runNum) '_*']);
load(fname);
if any([blockData.type]==2) %#ok<NODEF>
restInstruct = 'The feedback blocks will start soon';
else
restInstruct = 'The next blocks will start soon';
end
restDur = 3; % seconds to keep that display on
stimFix = 6; % seconds to keep the middle on before starting the block 5
%% Load Images
cd images;
% 1/25: updating to _new image categories where luminance isn't made to
% have the same mean--instead the luminance is scaled by the maximum
% luminance that it is to see if that makes things more equal in luminance
for categ=1:nSubCategs
% move into the right folder
if (categ == INDOOR)
cd indoor_NEW;
elseif (categ == OUTDOOR)
cd outdoor_NEW;
elseif (categ == MALE)
cd male_neut_NEW;
elseif (categ == FEMALE)
cd female_neut_NEW;
elseif (categ == MALESAD)
cd male_sad_NEW;
elseif (categ == FEMALESAD)
cd female_sad_NEW;
elseif (categ == MALEHAPPY)
cd male_happy_NEW;
elseif (categ == FEMALEHAPPY)
cd female_happy_NEW;
else
error('Impossible category!');
end
% get filenames
dirList{categ} = dir; %#ok<AGROW>
dirList{categ} = dirList{categ}(3:end); %#ok<AGROW> skip . & ..
if (~isempty(dirList{categ}))
if (strcmp(dirList{categ}(1).name,'.DS_Store')==1)
dirList{categ} = dirList{categ}(2:end); %#ok<AGROW>
end
if (strcmp(dirList{categ}(end).name,'Thumbs.db')==1)
dirList{categ} = dirList{categ}(1:(end-1)); %#ok<AGROW>
end
numImages(categ) = length(dirList{categ}); %#ok<AGROW>
if (numImages(categ)>0)
% get images
for img=1:numImages(categ)
% update progress bar
Screen('FrameRect',mainWindow,0,progRect,10);
Screen('FillRect',mainWindow,0,progRect);
Screen('FillRect',mainWindow,[255 0 0],progRect-[0 0 round((1-img/numImages(categ))*progWidth) 0]);
Screen('Flip',mainWindow);
% read images
images{categ,img} = imread(dirList{categ}(img).name); %#ok<AGROW>
tempFFT = fft2(images{categ,img});
imagePower{categ,img} = abs(tempFFT); %#ok<AGROW>
imagePhase{categ,img} = angle(tempFFT); %#ok<AGROW>
end
cd ..;
end
else
error('Need at least one image per directory!');
end
end
cd ..;
Screen('Flip',mainWindow);
%% Output Files Setup
% open and set-up output file
dataFile = fopen([runHeader '/behavior.txt'],'a');
fprintf(dataFile,'\n*********************************************\n');
fprintf(dataFile,'* rtAttenPenn v.1.0\n');
fprintf(dataFile,['* Date/Time: ' datestr(now,0) '\n']);
fprintf(dataFile,['* Seed: ' num2str(seed) '\n']);
fprintf(dataFile,['* Subject Number: ' num2str(subjectNum) '\n']);
fprintf(dataFile,['* Run Number: ' num2str(runNum) '\n']);
fprintf(dataFile,['* Use Button Box: ' num2str(useButtonBox) '\n']);
fprintf(dataFile,['* rtData: ' num2str(rtData) '\n']);
fprintf(dataFile,['* debugMode: ' num2str(debugMode) '\n']);
fprintf(dataFile,'*********************************************\n\n');
% print header to command window
fprintf('\n*********************************************\n');
fprintf('* rtAttenPenn v.1.0\n');
fprintf(['* Date/Time: ' datestr(now,0) '\n']);
fprintf(['* Seed: ' num2str(seed) '\n']);
fprintf(['* Subject Number: ' num2str(subjectNum) '\n']);
fprintf(['* Run Number: ' num2str(runNum) '\n']);
fprintf(['* Use Button Box: ' num2str(useButtonBox) '\n']);
fprintf(['* rtData: ' num2str(rtData) '\n']);
fprintf(['* debugMode: ' num2str(debugMode) '\n']);
fprintf('*********************************************\n\n');
%% Show Instructions
% clear screen
Screen(mainWindow,'FillRect',backColor);
Screen('Flip',mainWindow);
FlushEvents('keyDown');
% show instructions
if (blockData(1).type == 1)
runInstruct{1} = sceneInstruct;
runInstruct{2} = faceInstruct;
else
runInstruct{1} = faceInstruct;
runInstruct{2} = sceneInstruct;
end
runInstruct{3} = '-- Please press to begin once you understand these instructions. --';
for instruct=1:length(runInstruct)
tempBounds = Screen('TextBounds',mainWindow,runInstruct{instruct});
if instruct==3
textSpacing = textSpacing*3;
end
Screen('drawtext',mainWindow,runInstruct{instruct},centerX-tempBounds(3)/2,centerY-tempBounds(4)/5+textSpacing*(instruct-1),textColor);
clear tempBounds;
end
Screen('Flip',mainWindow);
waitForKeyboard(subj_keycode,DEVICE);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% now here we're adding to say waiting for scanner, hold tight!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
waitMessage = 'Waiting for scanner start, hold tight!';
tempBounds = Screen('TextBounds', mainWindow, waitMessage);
Screen('drawtext',mainWindow,waitMessage,centerX-tempBounds(3)/2,centerY-tempBounds(4)/2,textColor);
Screen('Flip', mainWindow);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% now here we're going to say to stay still once the triggers start coming
% in
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Start Experiment
STILLREMINDER = ['The scan is now starting.\n\nMoving your head even a little blurs the image, so '...
'please try to keep your head totally still until the scanning noise stops.\n\n Do it for science!'];
STILLDURATION = 6;
% wait for initial trigger
Priority(MaxPriority(screenNum));
if (rtData && ~debugMode )
% if strcmp(computer,'MACI') % taking out because we're running on a linux!
%runStart = WaitTRPulsePTB3_skyra(1);
timing.trig.wait = WaitTRPulse(TRIGGER_keycode,DEVICE);
runStart = timing.trig.wait;
DrawFormattedText(mainWindow,STILLREMINDER,'center','center',textColor,70)
%tempBounds = Screen('TextBounds', mainWindow, STILLREMINDER);
%Screen('drawtext',mainWindow,STILLREMINDER,centerX-tempBounds(3)/2,centerY-tempBounds(4)/2,textColor);
startTime = Screen('Flip',mainWindow);
elapsedTime = 0;
while (elapsedTime < STILLDURATION)
pause(0.005)
elapsedTime = GetSecs()-startTime;
end
% else
% WaitSecs(.5);
% runStart = KbWait;
% end
else
runStart = GetSecs;
%timing = -1;
end
Screen(mainWindow,'FillRect',backColor);
Screen(mainWindow,'FillOval',fixColor,fixDotRect);
Screen('Flip',mainWindow);
Priority(0);
% prepare for trial sequence
fprintf(dataFile,'run\tblock\tbltyp\tblcat\ttrial\tonsdif\tpulse\tscat\tfcat\tsimg\tfimg\tcorresp\tresp\tacc\trt\tfile\tload\tcatsep\tattProp\tsmoothProp\n');
fprintf('run\tblock\tbltyp\tblcat\ttrial\tonsdif\tpulse\tscat\tfcat\tsimg\tfimg\tcorresp\tresp\tacc\trt\tfile\tload\tcatsep\tattProp\tsmoothProp\n');
%% set up
for iBlock = 1:numel(blockData)
%block instructions
if (blockData(iBlock).attCateg==SCENE)
blockInstruct{1} = sceneShorterInstruct; %#ok<AGROW>
elseif (blockData(iBlock).attCateg==FACE)
blockInstruct{2} = faceShorterInstruct; %#ok<AGROW>
end
%timing
blockDur(iBlock+1) = TR*(instructLen+blockData(iBlock).trialsPerBlock/nTrialsPerTR+IBI); %#ok<AGROW>
blockOnsets(iBlock) = disdaqs + sum(blockDur(1:iBlock)); %#ok<AGROW>
end
blockOnsets((numel(blockOnsets)/2+1):end) = blockOnsets((numel(blockOnsets)/2+1):end)+stimFix; % account for the rest in between
typeOrder = [blockData.type];
indBlocksPhase1 = 1:(numel(typeOrder)/2);
indBlocksPhase2 = (numel(typeOrder)/2+1):numel(typeOrder);
%% Block Sequence - Phase 1
trialCounter = 0;
volCounter = 1+disdaqs/TR-1;
for iBlock=1:numel(indBlocksPhase1)
% timing
blockData(iBlock).actualblockonset = GetSecs; %#ok<AGROW>
blockData(iBlock).plannedinstructonset = blockOnsets(iBlock)+runStart; %#ok<AGROW>
blockData(iBlock).plannedinstructoffset = blockData(iBlock).plannedinstructonset + instructDur;
blockData(iBlock).plannedtrialonsets = blockData(iBlock).plannedinstructonset + TR*instructTRnum + [0 cumsum(repmat(TR/nTrialsPerTR,1,blockData(iBlock).trialsPerBlock))]; % so this says start on the next TR
blockData(iBlock).plannedIBI = blockData(iBlock).plannedtrialonsets(blockData(iBlock).trialsPerBlock) + stimDur;
% show instructions
tempBounds = Screen('TextBounds',mainWindow,blockInstruct{blockData(iBlock).attCateg});
Screen('drawtext',mainWindow,blockInstruct{blockData(iBlock).attCateg},centerX-tempBounds(3)/2,centerY-tempBounds(4)/5,textColor);
clear tempBounds;
timespec = blockData(iBlock).plannedinstructonset - slack;
if rtData
[timing.trig.instructpulses(iBlock),blockData(iBlock).instructpulses] = WaitTRPulse(TRIGGER_keycode,DEVICE,blockData(iBlock).plannedinstructonset);
end
blockData(iBlock).actualinstructonset = Screen('Flip',mainWindow,timespec); %#ok<AGROW> % turn on
fprintf('Flip time error = %.4f\n', blockData(iBlock).actualinstructonset-blockData(iBlock).plannedinstructonset)
% show fixation
timespec = blockData(iBlock).plannedinstructoffset - slack;
Screen(mainWindow,'FillOval',fixColor,fixDotRect);
blockData(iBlock).actualinstructoffset = Screen('Flip',mainWindow,timespec); %turn off
% start trial sequence
for iTrial=1:(blockData(iBlock).trialsPerBlock)
trialCounter = trialCounter+1;
% prep images
for half=[SCENE FACE]
% get current images
tempPower{half} = imagePower{blockData(iBlock).categs{half}(iTrial),blockData(iBlock).images{half}(iTrial)}; %#ok<AGROW>
tempImagePhase{half} = imagePhase{blockData(iBlock).categs{half}(iTrial),blockData(iBlock).images{half}(iTrial)}; %#ok<AGROW>
tempImage{half} = real(ifft2(tempPower{half}.*exp(sqrt(-1)*tempImagePhase{half}))); %#ok<AGROW>
end
%update imgProp value
blockData(iBlock).attImgProp(iTrial)=attImgPropPhase1;
% generate image
%fullImage = uint8((1-blockData(iBlock).attImgProp(iTrial))*tempImage{SCENE}+blockData(iBlock).attImgProp(iTrial)*tempImage{FACE});
% new: have it always be 60% face, 40% scene
fullImage = uint8(tempImage{SCENE}*sceneProp+tempImage{FACE}*faceProp);
% make textures
imageTex = Screen('MakeTexture',mainWindow,fullImage);
Screen('PreloadTextures',mainWindow,imageTex);
% wait for trigger and show image
FlushEvents('keyDown');
Priority(MaxPriority(screenNum));
Screen('FillRect',mainWindow,backColor);
Screen('DrawTexture',mainWindow,imageTex,imageRect,centerRect);
Screen(mainWindow,'FillOval',fixColor,fixDotRect);
tRespTimeout = blockData(iBlock).plannedtrialonsets(iTrial)+respWindow; %response timeout
%wait for pulse
if (rtData) && mod(iTrial,nTrialsPerTR)==1
%[~,blockData(iBlock).pulses(iTrial)] = WaitTRPulsePTB3_skyra(1,blockData(iBlock).plannedtrialonsets(iTrial)+allowance); %#ok<AGROW>
[timing.trig.pulses(iBlock,iTrial),blockData(iBlock).pulses(iTrial)] = WaitTRPulse(TRIGGER_keycode,DEVICE,blockData(iBlock).plannedtrialonsets(iTrial));
timespec = blockData(iBlock).plannedtrialonsets(iTrial) - slack;
blockData(iBlock).actualtrialonsets(iTrial) = Screen('Flip',mainWindow,timespec); %#ok<AGROW> % turn on
else
blockData(iBlock).pulses(iTrial) = 0; %#ok<AGROW>
timespec = blockData(iBlock).plannedtrialonsets(iTrial) - slack;
blockData(iBlock).actualtrialonsets(iTrial) = Screen('Flip',mainWindow,timespec); %#ok<AGROW>
end
stimOn = 1;
FlushEvents('keyDown');
while(GetSecs < tRespTimeout)
% check for responses if none received yet
if isnan(blockData(iBlock).rts(iTrial))
[keyIsDown, secs, keyCode] = KbCheck(DEVICE); % -1 checks all keyboards
if keyIsDown
if (keyCode(LEFT))
blockData(iBlock).rts(iTrial) = secs-blockData(iBlock).actualtrialonsets(iTrial); %#ok<AGROW> NTB: deltasecs is timed to last KbCheck call
blockData(iBlock).resps(iTrial) = find(keyCode,1); %#ok<AGROW>
Screen('FillRect',mainWindow,backColor);
if (stimOn) % leave image up if response before image duration
Screen('DrawTexture',mainWindow,imageTex,imageRect,centerRect);
end
Screen(mainWindow,'FillOval',respColor,fixDotRect);
Screen('Flip',mainWindow);
end
end
end
end
%accuracy
if ~isnan(blockData(iBlock).corrresps(iTrial)) %go trial
if (blockData(iBlock).resps(iTrial)==blockData(iBlock).corrresps(iTrial)) %made correct response
blockData(iBlock).accs(iTrial) = 1; %#ok<AGROW>
else
blockData(iBlock).accs(iTrial) = 0; %#ok<AGROW>
end
else %nogo trial
if isnan(blockData(iBlock).resps(iTrial)) %correctly did NOT make a response
blockData(iBlock).accs(iTrial) = 2; %#ok<AGROW>
else %made a response
blockData(iBlock).accs(iTrial) = 0; %#ok<AGROW>
end
end
% print trial results
fprintf(dataFile,'%d\t%d\t%s\t%s\t%d\t%.3f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n',runNum,iBlock,typeStr{blockData(iBlock).type},categStr{blockData(iBlock).attCateg},iTrial,blockData(iBlock).actualtrialonsets(iTrial)-blockData(iBlock).plannedtrialonsets(iTrial),blockData(iBlock).pulses(iTrial),blockData(iBlock).categs{SCENE}(iTrial),blockData(iBlock).categs{FACE}(iTrial),blockData(iBlock).images{SCENE}(iTrial),blockData(iBlock).images{FACE}(iTrial),blockData(iBlock).corrresps(iTrial),blockData(iBlock).resps(iTrial),blockData(iBlock).accs(iTrial),blockData(iBlock).rts(iTrial),NaN,NaN,NaN,blockData(iBlock).attImgProp(iTrial),NaN);
fprintf('%d\t%d\t%s\t%s\t%d\t%.3f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n',runNum,iBlock,typeStr{blockData(iBlock).type},categStr{blockData(iBlock).attCateg},iTrial,blockData(iBlock).actualtrialonsets(iTrial)-blockData(iBlock).plannedtrialonsets(iTrial),blockData(iBlock).pulses(iTrial),blockData(iBlock).categs{SCENE}(iTrial),blockData(iBlock).categs{FACE}(iTrial),blockData(iBlock).images{SCENE}(iTrial),blockData(iBlock).images{FACE}(iTrial),blockData(iBlock).corrresps(iTrial),blockData(iBlock).resps(iTrial),blockData(iBlock).accs(iTrial),blockData(iBlock).rts(iTrial),NaN,NaN,NaN,blockData(iBlock).attImgProp(iTrial),NaN);
end % trial loop
Screen('FillRect',mainWindow,backColor);
Screen(mainWindow,'FillOval',fixColor,fixDotRect);
% the IBI goes here!
if rtData
[timing.trig.IBIpulses(iBlock),blockData(iBlock).IBIpulses] = WaitTRPulse(TRIGGER_keycode,DEVICE,blockData(iBlock).plannedIBI);
end
timespec = blockData(iBlock).plannedIBI -slack;
blockData(iBlock).actualIBI = Screen('Flip',mainWindow,timespec);
fprintf('Flip time error = %.4f\n', blockData(iBlock).actualIBI-blockData(iBlock).plannedIBI)
end % end phase 1 block loop
%% pause & wait for model to be trained
timing.plannedreston = blockData(iBlock).plannedIBI + TR*IBI;
timing.plannedrestoff = timing.plannedreston + restDur;
% show instructions-wait for first trigger
if rtData
[timing.trig.pulserest,timing.pulsereston] = WaitTRPulse(TRIGGER_keycode,DEVICE,timing.plannedreston);
end
tempBounds = Screen('TextBounds',mainWindow,restInstruct);
Screen('drawtext',mainWindow,restInstruct,centerX-tempBounds(3)/2,centerY-tempBounds(4)/5,textColor);
clear tempBounds;
timespec = timing.plannedreston - slack;
timing.actualreston = Screen('Flip',mainWindow,timespec); % show instructions
fprintf('Flip time error = %.4f\n', timing.actualreston-timing.plannedreston)
% show fixation
Screen(mainWindow,'FillRect',backColor);
Screen(mainWindow,'FillOval',fixColor,fixDotRect);
timespec = timing.plannedrestoff - slack;
timing.actualrestoff = Screen('Flip',mainWindow,timespec); %turn off
%% Block Sequence - Phase2
% prepare for trial sequence
fprintf(dataFile,'run\tblock\tloadsep\tblcat\ttrial\tonsdif\tscat\tfcat\tsimg\tfimg\tcorresp\tresp\tacc\trt\tfile\tload\tcatsep\tattProp\tsmoothProp\n');
fprintf('run\tblock\tloadsep\tblcat\ttrial\tonsdif\tscat\tfcat\tsimg\tfimg\tcorresp\tresp\tacc\trt\tfile\tload\tcatsep\tattProp\tsmoothProp\n');
trialCounter = 0;
volCounter = firstVolPhase2+disdaqs/TR-1;
for iBlock=indBlocksPhase2
if ~isfield(blockData,'categsep')
blockData(iBlock).categsep = NaN(1,blockData(iBlock).trialsPerBlock); %#ok<AGROW>
end
volCounter = volCounter+1;
% timing
blockData(iBlock).actualblockonset = GetSecs; %#ok<AGROW>
blockData(iBlock).plannedinstructonset = blockOnsets(iBlock)+runStart; %#ok<AGROW>
blockData(iBlock).plannedinstructoffset = blockData(iBlock).plannedinstructonset + instructDur;
blockData(iBlock).plannedtrialonsets = blockData(iBlock).plannedinstructonset + TR*instructTRnum + [0 cumsum(repmat(TR/nTrialsPerTR,1,blockData(iBlock).trialsPerBlock))]; % so this says start on the next TR
blockData(iBlock).plannedIBI = blockData(iBlock).plannedtrialonsets(blockData(iBlock).trialsPerBlock) + stimDur;
% show instructions
tempBounds = Screen('TextBounds',mainWindow,blockInstruct{blockData(iBlock).attCateg});
Screen('drawtext',mainWindow,blockInstruct{blockData(iBlock).attCateg},centerX-tempBounds(3)/2,centerY-tempBounds(4)/5,textColor);
clear tempBounds;
timespec = blockData(iBlock).plannedinstructonset - slack;
if rtData
[timing.trig.instructpulses(iBlock),blockData(iBlock).instructpulses] = WaitTRPulse(TRIGGER_keycode,DEVICE,blockData(iBlock).plannedinstructonset);
end
blockData(iBlock).actualinstructonset = Screen('Flip',mainWindow,timespec); %#ok<AGROW> % turn on
fprintf('Flip time error = %.4f\n', blockData(iBlock).actualinstructonset-blockData(iBlock).plannedinstructonset)
% show fixation
timespec = blockData(iBlock).plannedinstructoffset - slack;
Screen(mainWindow,'FillOval',fixColor,fixDotRect);
blockData(iBlock).actualinstructoffset = Screen('Flip',mainWindow,timespec); %turn off
% start trial sequence
for iTrial=1:(blockData(iBlock).trialsPerBlock)
blockData(iBlock).tr1(iTrial) = GetSecs;
trialCounter = trialCounter+1;
if (mod(iTrial,nTrialsPerTR)==1)
volCounter = volCounter+1;
end
blockData(iBlock).volCounter(iTrial) = volCounter; %#ok<AGROW>
% prep images
for half=[SCENE FACE]
% get current images
tempPower{half} = imagePower{blockData(iBlock).categs{half}(iTrial),blockData(iBlock).images{half}(iTrial)}; %#ok<AGROW>
tempImagePhase{half} = imagePhase{blockData(iBlock).categs{half}(iTrial),blockData(iBlock).images{half}(iTrial)}; %#ok<AGROW>
tempImage{half} = real(ifft2(tempPower{half}.*exp(sqrt(-1)*tempImagePhase{half}))); %#ok<AGROW>
end
if iTrial <= nTrialsPerTR %initialize attImgProp for first 2 trials
if iTrial == 1
if blockData(iBlock).attCateg == FACE
attImgPropPhase2 = faceProp;
else % attend to scene
attImgPropPhase2 = sceneProp;
end
blockData(iBlock).attImgProp(iTrial) = attImgPropPhase2; %#ok<AGROW>
blockData(iBlock).smoothAttImgProp(iTrial) = attImgPropPhase2; %#ok<AGROW>
end
blockData(iBlock).attImgProp(iTrial+1) = attImgPropPhase2; %#ok<AGROW>
blockData(iBlock).smoothAttImgProp(iTrial+1) = attImgPropPhase2; %#ok<AGROW>
end
% generate image
fullImage = uint8((1-blockData(iBlock).smoothAttImgProp(iTrial))*tempImage{blockData(iBlock).inattCateg}+blockData(iBlock).smoothAttImgProp(iTrial)*tempImage{blockData(iBlock).attCateg});
% make textures
imageTex = Screen('MakeTexture',mainWindow,fullImage);
Screen('PreloadTextures',mainWindow,imageTex);
% wait for trigger and show image
FlushEvents('keyDown');
Priority(MaxPriority(screenNum));
Screen('FillRect',mainWindow,backColor);
Screen('DrawTexture',mainWindow,imageTex,imageRect,centerRect);
Screen(mainWindow,'FillOval',fixColor,fixDotRect);
blockData(iBlock).tprep(iTrial) = GetSecs;
tRespTimeout = blockData(iBlock).plannedtrialonsets(iTrial)+respWindow;
%wait for pulse
if (rtData) && mod(iTrial,nTrialsPerTR) % this will be true for every other then
%[~,blockData(iBlock).pulses(iTrial)] = WaitTRPulsePTB3_skyra(1,blockData(iBlock).plannedtrialonsets(iTrial)+allowance); %#ok<AGROW>
[timing.trig.pulses(iBlock,iTrial),blockData(iBlock).pulses(iTrial)] = WaitTRPulse(TRIGGER_keycode,DEVICE,blockData(iBlock).plannedtrialonsets(iTrial));
timespec = blockData(iBlock).plannedtrialonsets(iTrial) - slack;
blockData(iBlock).actualtrialonsets(iTrial) = Screen('Flip',mainWindow,timespec); %#ok<AGROW> % turn on
else
blockData(iBlock).pulses(iTrial) = 0; %#ok<AGROW>
timespec = blockData(iBlock).plannedtrialonsets(iTrial) - slack;
blockData(iBlock).actualtrialonsets(iTrial) = Screen('Flip',mainWindow,timespec); %#ok<AGROW>
end
stimOn = 1;
FlushEvents('keyDown');
while(GetSecs < tRespTimeout)
% check for responses if none received yet
if isnan(blockData(iBlock).rts(iTrial))
[keyIsDown, secs, keyCode] = KbCheck(DEVICE); % -1 checks all keyboards
if keyIsDown
if (keyCode(LEFT))
blockData(iBlock).rts(iTrial) = secs-blockData(iBlock).actualtrialonsets(iTrial); %#ok<AGROW> NTB: deltasecs is timed to last KbCheck call
blockData(iBlock).resps(iTrial) = find(keyCode,1); %#ok<AGROW>
Screen('FillRect',mainWindow,backColor);
if (stimOn) % leave image up if response before image duration
Screen('DrawTexture',mainWindow,imageTex,imageRect,centerRect);
end
Screen(mainWindow,'FillOval',respColor,fixDotRect);
Screen('Flip',mainWindow);
end
end
end
end
%accuracy
if ~isnan(blockData(iBlock).corrresps(iTrial)) %go trial
if (blockData(iBlock).resps(iTrial)==blockData(iBlock).corrresps(iTrial)) %made correct response
blockData(iBlock).accs(iTrial) = 1; %#ok<AGROW>
else
blockData(iBlock).accs(iTrial) = 0; %#ok<AGROW>
end
else %nogo trial
if isnan(blockData(iBlock).resps(iTrial)) %correctly did NOT make a response
blockData(iBlock).accs(iTrial) = 2; %#ok<AGROW>
else %made a response
blockData(iBlock).accs(iTrial) = 0; %#ok<AGROW>
end
end
blockData(iBlock).tr2(iTrial) = GetSecs;
%load rtfeedback values once per TR
blockData(iBlock).RT1(iTrial) = GetSecs;
FILEFOUND = 'NOSEP';
if rtfeedback
% will start looking at the odd TR numbers on
if (mod(iTrial,nTrialsPerTR)==1) && (iTrial>nTrialsPerTR)
%number of odd trials - paired with TRs
iTrialOdd = ceil(iTrial/2);
%preset the file load to 0 and the timeout
blockData(iBlock).classOutputFileLoad(iTrial) = 0; %#ok<AGROW>
tClassOutputFileTimeout = GetSecs + deltat;
%check for classifier output file
timing.classifierLoadStart(iBlock,iTrial)=GetSecs;
while (~blockData(iBlock).classOutputFileLoad(iTrial) && (GetSecs < tClassOutputFileTimeout))
[blockData(iBlock).classOutputFileLoad(iTrial) blockData(iBlock).classOutputFile{iTrial}] = GetSpecificClassOutputFile(classOutputDir,volCounter-2,usePyOutput); %#ok<AGROW>
end
timing.classifierLoadEnd(iBlock,iTrial)=GetSecs;
% get newest file in that TR
blockData(iBlock).fileList{iTrial} = ls(classOutputDir);
if ~usePyOutput
allFn = dir([classOutputDir '/vol' '*.mat']);
else
allFn = dir([classOutputDir '/vol' '*_py.txt']);
end
dates = [allFn.datenum];
names = {allFn.name};
[~,newestIndex] = max(dates);
if ~isempty(ls(classOutputDir))
blockData(iBlock).newestFile{iTrial} = names{newestIndex};
end
%load classifier output file
if blockData(iBlock).classOutputFileLoad(iTrial)
tempStruct = load([classOutputDir '/' blockData(iBlock).classOutputFile{iTrial}]);
if ~usePyOutput
blockData(iBlock).categsep(iTrial) = tempStruct.classOutput; %#ok<AGROW>
else
blockData(iBlock).categsep(iTrial) = tempStruct;
end
FILEFOUND = 'FSEP';
else
blockData(iBlock).classOutputFile{iTrial} = 'notload'; %#ok<AGROW>
end
%constrain the proportion of attended image
if isnan(blockData(iBlock).categsep(iTrial)) %attImgProp for that trial for some reason was NaN
tempLastClassOutput = find(~isnan(blockData(iBlock).categsep),1,'last');
if ~isempty(tempLastClassOutput)
blockData(iBlock).attImgProp(iTrial+1) = blockData(iBlock).attImgProp(tempLastClassOutput); %#ok<AGROW>
else
blockData(iBlock).attImgProp(iTrial+1) = attImgPropPhase2; %#ok<AGROW>
end
% %******* SET DEMO AMOUNTS--DELETE THIS AFTERWARDS!!! ****
% vals = linspace(0,.8,25);
% blockData(iBlock).attImgProp(iTrial+1) = vals(iTrialOdd);
% %******* SET DEMO AMOUNTS--DELETE THIS AFTERWARDS!!! ****
else
blockData(iBlock).attImgProp(iTrial+1) = steepness./(1+exp(-gain*(blockData(iBlock).categsep(iTrial)-x_shift)))+y_shift; %#ok<AGROW>
end
%set for next trial
if ((iTrial+2)<blockData(iBlock).trialsPerBlock)
blockData(iBlock).attImgProp(iTrial+2) = blockData(iBlock).attImgProp(iTrial+1); %#ok<AGROW>
end
%smooth the trials
if iTrialOdd == 2
blockData(iBlock).smoothAttImgProp(2,iTrialOdd) = .5*blockData(iBlock).attImgProp(2,iTrialOdd-1)+.5*blockData(iBlock).attImgProp(2,iTrialOdd); %#ok<AGROW>
else
blockData(iBlock).smoothAttImgProp(2,iTrialOdd) = (1/3)*blockData(iBlock).attImgProp(2,iTrialOdd-2)+(1/3)*blockData(iBlock).attImgProp(2,iTrialOdd-1)+(1/3)*blockData(iBlock).attImgProp(2,iTrialOdd); %#ok<AGROW>
end
%set for next trial
if ((iTrial+2)<=blockData(iBlock).trialsPerBlock)
blockData(iBlock).smoothAttImgProp(iTrial+2) = blockData(iBlock).smoothAttImgProp(iTrial+1); %#ok<AGROW>
end
else
blockData(iBlock).classOutputFile{iTrial} = '12orev'; %#ok<AGROW>
end
else
if ((iTrial+1)<=blockData(iBlock).trialsPerBlock)
blockData(iBlock).attImgProp(iTrial+1) = attImgPropPhase2; %#ok<AGROW>
blockData(iBlock).smoothAttImgProp(iTrial+1)= attImgPropPhase2; %#ok<AGROW>
end
if ((iTrial+2)<=blockData(iBlock).trialsPerBlock-2)
blockData(iBlock).attImgProp(iTrial+2) = attImgPropPhase2; %#ok<AGROW>
blockData(iBlock).smoothAttImgProp(iTrial+2)= attImgPropPhase2; %#ok<AGROW>
end
blockData(iBlock).classOutputFile{iTrial} = 'notrt'; %#ok<AGROW>
blockData(iBlock).classOutputFile{iTrial} = NaN; %#ok<AGROW>
end
blockData(iBlock).RT2(iTrial) = GetSecs;
% print trial results
fprintf(dataFile,'%d\t%d\t%s\t%s\t%d\t%.3f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%.3f\t%d\t%d\t%.3f\t%.3f\t%.3f\n',runNum,iBlock,FILEFOUND,categStr{blockData(iBlock).attCateg},iTrial,blockData(iBlock).actualtrialonsets(iTrial)-blockData(iBlock).plannedtrialonsets(iTrial),blockData(iBlock).categs{SCENE}(iTrial),blockData(iBlock).categs{FACE}(iTrial),blockData(iBlock).images{SCENE}(iTrial),blockData(iBlock).images{FACE}(iTrial),blockData(iBlock).corrresps(iTrial),blockData(iBlock).resps(iTrial),blockData(iBlock).accs(iTrial),blockData(iBlock).rts(iTrial),blockData(iBlock).volCounter(iTrial),blockData(iBlock).classOutputFileLoad(iTrial),blockData(iBlock).categsep(iTrial),blockData(iBlock).attImgProp(iTrial),blockData(iBlock).smoothAttImgProp(iTrial));
fprintf('%d\t%d\t%s\t%s\t%d\t%.3f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%.3f\t%d\t%d\t%.3f\t%.3f\t%.3f\n',runNum,iBlock,FILEFOUND,categStr{blockData(iBlock).attCateg},iTrial,blockData(iBlock).actualtrialonsets(iTrial)-blockData(iBlock).plannedtrialonsets(iTrial),blockData(iBlock).categs{SCENE}(iTrial),blockData(iBlock).categs{FACE}(iTrial),blockData(iBlock).images{SCENE}(iTrial),blockData(iBlock).images{FACE}(iTrial),blockData(iBlock).corrresps(iTrial),blockData(iBlock).resps(iTrial),blockData(iBlock).accs(iTrial),blockData(iBlock).rts(iTrial),blockData(iBlock).volCounter(iTrial),blockData(iBlock).classOutputFileLoad(iTrial),blockData(iBlock).categsep(iTrial),blockData(iBlock).attImgProp(iTrial),blockData(iBlock).smoothAttImgProp(iTrial));
end % trial loop
volCounter = volCounter+2;
Screen('FillRect',mainWindow,backColor);
Screen(mainWindow,'FillOval',fixColor,fixDotRect);
if rtData
[timing.trig.IBIpulses(iBlock),blockData(iBlock).IBIpulses] = WaitTRPulse(TRIGGER_keycode,DEVICE,blockData(iBlock).plannedIBI);
end
timespec = blockData(iBlock).plannedIBI -slack;
blockData(iBlock).actualIBI = Screen('Flip',mainWindow,timespec);
fprintf('Flip time error = %.4f\n', blockData(iBlock).actualIBI-blockData(iBlock).plannedIBI)
end % phase 2 block loop
% want to wait 7 TRS--2 IBIs and then 5 more TRs
WaitSecs(14);
%% save
% question: do you want to save it to this computer's data or where you
% save the data folder???
save([runHeader '/blockdata_' num2str(runNum) '_' datestr(now,30)],'blockData','runStart', 'timing');
% clean up and go home
sca;
ListenChar(1);
fclose('all');
end