-
Notifications
You must be signed in to change notification settings - Fork 2
/
space_exposure.m
1183 lines (1030 loc) · 43.2 KB
/
space_exposure.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 [cfg,expParam] = space_exposure(w,cfg,expParam,logFile,sesName,phaseName,phaseCount)
% function [cfg,expParam] = space_exposure(w,cfg,expParam,logFile,sesName,phaseName,phaseCount)
%
% Description:
% This function runs the exposure task. There are no blocks, only
% short (blink) breaks.
% TODO: Maybe add a longer break in the middle and tell subjects that this
% is the middle of the experiment.
%
% The stimuli for the exposure task must already be in presentation order.
% They are stored in expParam.session.(sesName).(phaseName).expoStims_img
% as a struct.
%
%
% Inputs:
%
%
% Outputs:
%
%
%
% NB:
% Once agian, stimuli must already be sorted in presentation order!
%
%
% NB:
% presNum is the presentation number for this stimulus pair (1 or 2)
% pairNum is to keep the image and word together (both stims are the same)
% pairOrd is the order in which the image and word are shown (one is 1 and
% the other is 2)
% NB:
% When same and diff stimuli are combined, to find the corresponding pair
% search for a matching familyNum (basic or subordinate), a matching or
% different speciesNum field (same or diff condition), a matching or
% different trained field, the same matchPairNum, and the opposite
% matchStimNum (1 or 2).
% % durations, in seconds
% % keys
fprintf('Running %s %s (expo) (%d)...\n',sesName,phaseName,phaseCount);
phaseNameForParticipant = 'judgment';
%% set the starting date and time for this phase
thisDate = date;
startTime = fix(clock);
startTime = sprintf('%.2d:%.2d:%.2d',startTime(4),startTime(5),startTime(6));
%% determine the starting trial, useful for resuming
% set up progress file, to resume this phase in case of a crash, etc.
phaseProgressFile = fullfile(cfg.files.sesSaveDir,sprintf('phaseProgress_%s_%s_expo_%d.mat',sesName,phaseName,phaseCount));
if exist(phaseProgressFile,'file')
load(phaseProgressFile);
else
trialComplete = false(1,length(expParam.session.(sesName).(phaseName)(phaseCount).expoStims_img));
phaseComplete = false; %#ok<NASGU>
save(phaseProgressFile,'thisDate','startTime','trialComplete','phaseComplete');
end
% find the starting trial
incompleteTrials = find(~trialComplete);
if ~isempty(incompleteTrials)
trialNum = incompleteTrials(1);
else
fprintf('All trials for %s %s (expo) (%d) have been completed. Moving on to next phase...\n',sesName,phaseName,phaseCount);
% release any remaining textures
Screen('Close');
return
end
%% start the log file for this phase
phaseLogFile = fullfile(cfg.files.sesSaveDir,sprintf('phaseLog_%s_%s_expo_%d.txt',sesName,phaseName,phaseCount));
phLFile = fopen(phaseLogFile,'at');
%% record the starting date and time for this phase
expParam.session.(sesName).(phaseName)(phaseCount).date = thisDate;
expParam.session.(sesName).(phaseName)(phaseCount).startTime = startTime;
% put it in the log file
fprintf(logFile,'!!! Start of %s %s (%d) (%s) %s %s\n',sesName,phaseName,phaseCount,mfilename,thisDate,startTime);
fprintf(phLFile,'!!! Start of %s %s (%d) (%s) %s %s\n',sesName,phaseName,phaseCount,mfilename,thisDate,startTime);
thisGetSecs = GetSecs;
fprintf(logFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',...
thisGetSecs,...
expParam.subject,...
sesName,...
phaseName,...
phaseCount,...
cfg.stim.(sesName).(phaseName)(phaseCount).isExp,...
'PHASE_START');
fprintf(phLFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',...
thisGetSecs,...
expParam.subject,...
sesName,...
phaseName,...
phaseCount,...
cfg.stim.(sesName).(phaseName)(phaseCount).isExp,...
'PHASE_START');
%% preparation
phaseCfg = cfg.stim.(sesName).(phaseName)(phaseCount);
expoStims_img = expParam.session.(sesName).(phaseName)(phaseCount).expoStims_img;
if phaseCfg.isExp
imgStimDir = cfg.files.imgStimDir;
else
imgStimDir = cfg.files.imgStimDir_prac;
end
% default is to preload the images
if ~isfield(cfg.stim,'preloadImages')
cfg.stim.preloadImages = false;
end
% read the proper exposure ranking key image
expoKeyImg = imread(cfg.files.exposureRankRespKeyImg);
expoKeyImgHeight = size(expoKeyImg,1) * cfg.files.respKeyImgScale;
expoKeyImgWidth = size(expoKeyImg,2) * cfg.files.respKeyImgScale;
expoKeyImg = Screen('MakeTexture',w,expoKeyImg);
% set the expo ranking response key image rectangle
expoKeyImgRect = SetRect(0, 0, expoKeyImgWidth, expoKeyImgHeight);
expoKeyImgRect = CenterRect(expoKeyImgRect, cfg.screen.wRect);
expoKeyImgRect = AlignRect(expoKeyImgRect, cfg.screen.wRect, 'bottom', 'bottom');
% default is to not print out trial details
if ~isfield(cfg.text,'printTrialInfo') || isempty(cfg.text.printTrialInfo)
cfg.text.printTrialInfo = false;
end
% default is to not play sounds
if ~isfield(phaseCfg,'playSound') || isempty(phaseCfg.playSound)
phaseCfg.playSound = false;
end
% initialize beep player if needed
if phaseCfg.playSound
Beeper(1,0);
if ~isfield(phaseCfg,'correctSound')
phaseCfg.correctSound = 1000;
end
if ~isfield(phaseCfg,'incorrectSound')
phaseCfg.incorrectSound = 300;
end
if ~isfield(phaseCfg,'correctVol')
phaseCfg.correctVol = 0.4;
end
if ~isfield(phaseCfg,'incorrectVol')
phaseCfg.incorrectVol = 0.6;
end
end
% are they allowed to respond while the stimulus is on the screen?
if ~isfield(phaseCfg,'respDuringStim')
phaseCfg.respDuringStim = true;
end
% default is to show fixation during ISI
if ~isfield(phaseCfg,'fixDuringISI')
phaseCfg.fixDuringISI = true;
end
% default is to show fixation during preStim
if ~isfield(phaseCfg,'fixDuringPreStim')
phaseCfg.fixDuringPreStim = true;
end
% default is to show fixation with the stimulus
if ~isfield(phaseCfg,'fixDuringStim')
phaseCfg.fixDuringStim = true;
end
if ~isfield(phaseCfg,'impedanceAfter_nTrials')
phaseCfg.impedanceAfter_nTrials = 0;
end
if ~isfield(phaseCfg,'showRespInBreak')
phaseCfg.showRespInBreak = true;
end
if ~isfield(phaseCfg,'showRespBtStim')
phaseCfg.showRespBtStim = true;
end
% whether to ask the participant if they have any questions; only continues
% with experimenter's secret key
if ~isfield(phaseCfg.instruct,'questions')
phaseCfg.instruct.questions = true;
end
% whether to present a white square during the stimulus and a black square
% at all other times
if ~isfield(cfg.stim,'photoCell')
cfg.stim.photoCell = false;
end
%% set up text rectangles
% create a rectangle for placing fixation symbol using Screen('DrawText')
Screen('TextSize', w, cfg.text.fixSize);
fixRect = Screen('TextBounds', w, cfg.text.fixSymbol);
% center it in the middle of the screen
fixRect = CenterRect(fixRect, cfg.screen.wRect);
% get the X and Y coordinates
fixRectX = fixRect(1);
fixRectY = fixRect(2);
% create a rectangle for placing response symbol using Screen('DrawText')
Screen('TextSize', w, cfg.text.fixSize);
respRect = Screen('TextBounds', w, cfg.text.respSymbol);
% center it in the middle of the screen
respRect = CenterRect(respRect, cfg.screen.wRect);
% get the X and Y coordinates
respRectX = respRect(1);
respRectY = respRect(2);
%% preload all stimuli for presentation
expoImgTex = nan(1,length(expoStims_img));
message = sprintf('Preparing images, please wait...');
Screen('TextSize', w, cfg.text.basicTextSize);
% put the "preparing" message on the screen
DrawFormattedText(w, message, 'center', 'center', cfg.text.instructColor, cfg.text.instructCharWidth);
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
% Update the display to show the message:
Screen('Flip', w);
% initialize to store image stimulus parameters
stimImgRect_all = nan(length(expoStims_img),4);
errorTextY_all = nan(length(expoStims_img),1);
%wordStimY_all = nan(length(expoStims_img),1);
%responsePromptY_all = nan(length(expoStims_img),1);
[~, screenCenterY] = RectCenter(cfg.screen.wRect);
for i = 1:length(expoStims_img)
% make sure image stimulus exists
stimImgFile = fullfile(imgStimDir,expoStims_img(i).categoryStr,expoStims_img(i).fileName);
if exist(stimImgFile,'file')
% load up stim's texture
stimImg = imread(stimImgFile);
% resize the image, if necessary
if size(stimImg,1) > cfg.stim.nRows
stimImg = imresize(stimImg,[cfg.stim.nRows, NaN]);
end
% crop the image, if necessary
if size(stimImg,2) > cfg.stim.cropWidth
widthCenter = round(size(stimImg,2) / 2);
stimImg = stimImg(:,(widthCenter - round(cfg.stim.cropWidth / 2)):(widthCenter + round(cfg.stim.cropWidth / 2)),:);
end
% set the coordinates that we will use later
stimImgHeight = size(stimImg,1) * cfg.stim.stimScale;
stimImgWidth = size(stimImg,2) * cfg.stim.stimScale;
% set the stimulus image rectangle
stimImgRect_all(i,:) = [0 0 stimImgWidth stimImgHeight];
stimImgRect_all(i,:) = CenterRect(stimImgRect_all(i,:), cfg.screen.wRect);
% text location for error (e.g., "too fast") text
errorTextY_all(i) = screenCenterY + (stimImgHeight / 2);
% text location for word stimulus
%wordStimY_all(i) = screenCenterY;
%wordStimY_all(i) = screenCenterY + (stimImgHeight / 2);
% text location for response prompt
%responsePromptY_all(i) = screenCenterY + (stimImgHeight / 2) + (screenCenterY * 0.05);
if cfg.stim.preloadImages
expoImgTex(i) = Screen('MakeTexture',w,stimImg);
% TODO: optimized?
%studyImgTex(i) = Screen('MakeTexture',w,stimImg,[],1);
%elseif ~cfg.stim.preloadImages && i == length(expoStims_img)
% % still need to load the last image to set the rectangle
% stimImg = imread(stimImgFile);
end
else
error('Study stimulus %s does not exist!',stimImgFile);
end
end
%% do an impedance check before the phase begins, if desired
if ~isfield(phaseCfg,'impedanceBeforePhase')
phaseCfg.impedanceBeforePhase = false;
end
if ~expParam.photoCellTest && expParam.useNS && phaseCfg.impedanceBeforePhase
% run the impedance break
thisGetSecs = GetSecs;
fprintf(logFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'IMPEDANCE_START');
fprintf(phLFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'IMPEDANCE_START');
thisGetSecs = et_impedanceCheck(w, cfg, false, phaseName);
fprintf(logFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'IMPEDANCE_END');
fprintf(phLFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'IMPEDANCE_END');
end
%% start NS recording, if desired
% put a message on the screen as experiment phase begins
message = sprintf('Starting %s phase...',phaseNameForParticipant);
if expParam.useNS
% start recording
[NSStopStatus, NSStopError] = NetStation('StartRecording'); %#ok<NASGU,ASGLU>
% synchronize
[NSSyncStatus, NSSyncError] = NetStation('Synchronize'); %#ok<NASGU,ASGLU>
message = sprintf('Starting data acquisition for %s phase...',phaseNameForParticipant);
thisGetSecs = GetSecs;
fprintf(logFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'NS_REC_START');
fprintf(phLFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'NS_REC_START');
end
Screen('TextSize', w, cfg.text.basicTextSize);
% draw message to screen
DrawFormattedText(w, message, 'center', 'center', cfg.text.basicTextColor, cfg.text.instructCharWidth);
% put it on
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
Screen('Flip', w);
% Wait before starting trial
WaitSecs(5.000);
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
% Clear screen to background color (our 'bgColor' as set at the beginning):
Screen('Flip', w);
%% show the instructions
if ~expParam.photoCellTest
for i = 1:length(phaseCfg.instruct.expo)
WaitSecs(1.000);
et_showTextInstruct(w,cfg,phaseCfg.instruct.expo(i),cfg.keys.instructContKey,...
cfg.text.instructColor,cfg.text.instructTextSize,cfg.text.instructCharWidth);
end
% Wait a second before starting trial
WaitSecs(1.000);
end
%% questions? only during practice. continues with experimenter's key.
if ~expParam.photoCellTest && ~phaseCfg.isExp && phaseCfg.instruct.questions
questionsMsg.text = sprintf('If you have any questions about the %s phase,\nplease ask the experimenter now.\n\nPlease tell the experimenter when you are ready to begin the task.',phaseNameForParticipant);
et_showTextInstruct(w,cfg,questionsMsg,cfg.keys.expContinue,...
cfg.text.instructColor,cfg.text.instructTextSize,cfg.text.instructCharWidth);
% Wait a second before continuing
WaitSecs(1.000);
end
%% let them start when they're ready
if ~expParam.photoCellTest
if phaseCfg.isExp
expStr = '';
else
expStr = ' practice';
end
readyMsg.text = sprintf('Ready to begin%s %s phase.\nPress "%s" to start.',expStr,phaseNameForParticipant,cfg.keys.instructContKey);
et_showTextInstruct(w,cfg,readyMsg,cfg.keys.instructContKey,...
cfg.text.instructColor,cfg.text.instructTextSize,cfg.text.instructCharWidth);
% Wait a second before starting trial
WaitSecs(1.000);
end
%% run the exposure task
% only check these keys
RestrictKeysForKbCheck([cfg.keys.expoVA, cfg.keys.expoSA, cfg.keys.expoSU, cfg.keys.expoVU]);
% start the blink break timer
if phaseCfg.isExp && phaseCfg.secUntilBlinkBreak > 0
blinkTimerStart = GetSecs;
end
for i = trialNum:length(expoStims_img)
% do an impedance check after a certain number of trials
if ~expParam.photoCellTest && expParam.useNS && phaseCfg.isExp && i > 1 && i < length(expoStims_img) && mod((i - 1),phaseCfg.impedanceAfter_nTrials) == 0
% run the impedance break
thisGetSecs = GetSecs;
fprintf(logFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'IMPEDANCE_START');
fprintf(phLFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'IMPEDANCE_START');
thisGetSecs = et_impedanceCheck(w, cfg, true, phaseName);
fprintf(logFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'IMPEDANCE_END');
fprintf(phLFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'IMPEDANCE_END');
RestrictKeysForKbCheck([cfg.keys.expoVA, cfg.keys.expoSA, cfg.keys.expoSU, cfg.keys.expoVU]);
if phaseCfg.showRespInBreak
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
end
% show preparation text
DrawFormattedText(w, 'Get ready...', 'center', 'center', cfg.text.fixationColor, cfg.text.instructCharWidth);
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
Screen('Flip', w);
WaitSecs(2.0);
if phaseCfg.showRespInBreak
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
end
if (phaseCfg.expo_isi > 0 && phaseCfg.fixDuringISI) || (phaseCfg.expo_isi == 0 && phaseCfg.fixDuringPreStim)
Screen('TextSize', w, cfg.text.fixSize);
Screen('DrawText', w, cfg.text.fixSymbol, fixRectX, fixRectY, cfg.text.fixationColor);
%DrawFormattedText(w,cfg.text.fixSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
end
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
Screen('Flip',w);
WaitSecs(1.0);
% reset the blink timer
if phaseCfg.secUntilBlinkBreak > 0
blinkTimerStart = GetSecs;
end
end
% Do a blink break if specified time has passed
if ~expParam.photoCellTest && phaseCfg.isExp && phaseCfg.secUntilBlinkBreak > 0 && (GetSecs - blinkTimerStart) >= phaseCfg.secUntilBlinkBreak && i > 3 && i < (length(expoStims_img) - 3)
thisGetSecs = GetSecs;
fprintf(logFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'BLINK_START');
fprintf(phLFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'BLINK_START');
Screen('TextSize', w, cfg.text.basicTextSize);
if expParam.useNS
pauseMsg = 'Blink now.\n\n';
else
pauseMsg = '';
end
pauseMsg = sprintf('%sReady for trial %d of %d.\nPress any key to continue.', pauseMsg, i, length(expoStims_img));
if phaseCfg.showRespInBreak
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
end
% just draw straight into the main window since we don't need speed here
DrawFormattedText(w, pauseMsg, 'center', 'center', cfg.text.instructColor, cfg.text.instructCharWidth);
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
Screen('Flip', w);
% listen for any keypress on any keyboard
RestrictKeysForKbCheck([]);
thisGetSecs = KbWait(-1,2);
%thisGetSecs = GetSecs;
fprintf(logFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'BLINK_END');
fprintf(phLFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\n',thisGetSecs,expParam.subject,sesName,phaseName,phaseCount,phaseCfg.isExp,'BLINK_END');
RestrictKeysForKbCheck([cfg.keys.expoVA, cfg.keys.expoSA, cfg.keys.expoSU, cfg.keys.expoVU]);
if phaseCfg.showRespInBreak
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
end
% show preparation text
DrawFormattedText(w, 'Get ready...', 'center', 'center', cfg.text.fixationColor, cfg.text.instructCharWidth);
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
Screen('Flip', w);
WaitSecs(2.0);
if phaseCfg.showRespInBreak
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
end
if (phaseCfg.expo_isi > 0 && phaseCfg.fixDuringISI) || (phaseCfg.expo_isi == 0 && phaseCfg.fixDuringPreStim)
Screen('TextSize', w, cfg.text.fixSize);
%DrawFormattedText(w,cfg.text.fixSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
Screen('DrawText', w, cfg.text.fixSymbol, fixRectX, fixRectY, cfg.text.fixationColor);
end
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
Screen('Flip',w);
WaitSecs(1.0);
% reset the timer
blinkTimerStart = GetSecs;
end
% load the image stimulus now if we didn't load it earlier
if ~cfg.stim.preloadImages
stimImg = imread(fullfile(imgStimDir,expoStims_img(i).categoryStr,expoStims_img(i).fileName));
% resize the image, if necessary
if size(stimImg,1) > cfg.stim.nRows
stimImg = imresize(stimImg,[cfg.stim.nRows, NaN]);
end
% crop the image, if necessary
if size(stimImg,2) > cfg.stim.cropWidth
widthCenter = round(size(stimImg,2) / 2);
stimImg = stimImg(:,(widthCenter - round(cfg.stim.cropWidth / 2)):(widthCenter + round(cfg.stim.cropWidth / 2)),:);
end
% create the texture
expoImgTex(i) = Screen('MakeTexture',w,stimImg);
end
% pull out the coordinates we need
stimImgRect = stimImgRect_all(i,:);
errorTextY = errorTextY_all(i);
% resynchronize netstation before the start of drawing
if expParam.useNS
[NSSyncStatus, NSSyncError] = NetStation('Synchronize'); %#ok<NASGU,ASGLU>
end
% ISI between trials
if phaseCfg.expo_isi > 0
if phaseCfg.showRespBtStim
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
end
if phaseCfg.fixDuringISI
Screen('TextSize', w, cfg.text.fixSize);
%DrawFormattedText(w,cfg.text.fixSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
Screen('DrawText', w, cfg.text.fixSymbol, fixRectX, fixRectY, cfg.text.fixationColor);
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
Screen('Flip',w);
end
WaitSecs(phaseCfg.expo_isi);
end
% % preStimulus period, with fixation if desired
% if length(phaseCfg.expo_preStim) == 1
% if phaseCfg.expo_preStim > 0
% if phaseCfg.fixDuringPreStim
% if phaseCfg.showRespBtStim
% % draw response prompt
% Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
% end
% % draw fixation
% Screen('TextSize', w, cfg.text.fixSize);
% %DrawFormattedText(w,cfg.text.fixSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
% Screen('DrawText', w, cfg.text.fixSymbol, fixRectX, fixRectY, cfg.text.fixationColor);
% if cfg.stim.photoCell
% Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
% end
% [preStimFixOn] = Screen('Flip',w);
% else
% if phaseCfg.showRespBtStim
% % draw response prompt
% Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
% end
% preStimFixOn = NaN;
% if cfg.stim.photoCell
% Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
% end
% Screen('Flip',w);
% end
% WaitSecs(phaseCfg.expo_preStim);
% end
% elseif length(phaseCfg.expo_preStim) == 2
% if ~all(phaseCfg.expo_preStim == 0)
% if phaseCfg.fixDuringPreStim
% if phaseCfg.showRespBtStim
% % draw response prompt
% Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
% end
% % draw fixation
% Screen('TextSize', w, cfg.text.fixSize);
% %DrawFormattedText(w,cfg.text.fixSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
% Screen('DrawText', w, cfg.text.fixSymbol, fixRectX, fixRectY, cfg.text.fixationColor);
% if cfg.stim.photoCell
% Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
% end
% [preStimFixOn] = Screen('Flip',w);
% else
% if phaseCfg.showRespBtStim
% % draw response prompt
% Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
% end
% preStimFixOn = NaN;
% if cfg.stim.photoCell
% Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
% end
% Screen('Flip',w);
% end
% % fixation on screen before stim for a random amount of time
% WaitSecs(phaseCfg.expo_preStim(1) + ((phaseCfg.expo_preStim(2) - phaseCfg.expo_preStim(1)).*rand(1,1)));
% end
% end
% preStimulus period, with fixation if desired
if length(phaseCfg.expo_preStim) == 1 && phaseCfg.expo_preStim > 0
preStimTime = phaseCfg.expo_preStim;
elseif length(phaseCfg.expo_preStim) == 2 && ~all(phaseCfg.expo_preStim == 0)
preStimTime = phaseCfg.expo_preStim(1) + ((phaseCfg.expo_preStim(2) - phaseCfg.expo_preStim(1)).*rand(1,1));
else
preStimTime = 0;
end
if preStimTime > 0
if phaseCfg.fixDuringPreStim
if phaseCfg.showRespBtStim
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
end
% draw fixation
Screen('TextSize', w, cfg.text.fixSize);
%DrawFormattedText(w,cfg.text.fixSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
Screen('DrawText', w, cfg.text.fixSymbol, fixRectX, fixRectY, cfg.text.fixationColor);
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
[preStimFixOn] = Screen('Flip',w);
else
if phaseCfg.showRespBtStim
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
end
preStimFixOn = NaN;
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
Screen('Flip',w);
end
% fixation on screen before stim for a random amount of time
WaitSecs(preStimTime);
end
% draw the image stimulus
Screen('DrawTexture', w, expoImgTex(i), [], stimImgRect);
if phaseCfg.respDuringStim
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
end
if phaseCfg.fixDuringStim
% and fixation on top of it
Screen('TextSize', w, cfg.text.fixSize);
%DrawFormattedText(w,cfg.text.fixSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
Screen('DrawText', w, cfg.text.fixSymbol, fixRectX, fixRectY, cfg.text.fixationColor);
end
% photocell rect with stim
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellRectColor, cfg.stim.photoCellRect);
end
% put the stimuli on the screen
[imgOn, stimOnset] = Screen('Flip', w);
if cfg.text.printTrialInfo
fprintf('Trial %d of %d: stim (%s): category %d (%s).\n',i,length(expoStims_img),expoStims_img(i).fileName,expoStims_img(i).categoryNum,expoStims_img(i).categoryStr);
end
% while loop to show stimulus until subject response or until
% "study_stim2" seconds elapse.
while (GetSecs - stimOnset) <= phaseCfg.expo_stim
% check for too-fast response
if ~phaseCfg.respDuringStim
[keyIsDown] = KbCheck;
% if they press a key too early, tell them they responded too fast
if keyIsDown
% draw the image stimulus
Screen('DrawTexture', w, expoImgTex(i), [], stimImgRect);
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
if phaseCfg.fixDuringStim
% and fixation on top of it
Screen('TextSize', w, cfg.text.fixSize);
%DrawFormattedText(w,cfg.text.fixSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
Screen('DrawText', w, cfg.text.fixSymbol, fixRectX, fixRectY, cfg.text.fixationColor);
end
% and the "too fast" text
Screen('TextSize', w, cfg.text.instructTextSize);
DrawFormattedText(w,cfg.text.tooFastText,'center',errorTextY,cfg.text.errorTextColor, cfg.text.instructCharWidth);
% photocell rect with stim
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellRectColor, cfg.stim.photoCellRect);
end
Screen('Flip', w);
keyIsDown = 0;
break
end
else
[keyIsDown, endRT, keyCode] = KbCheck;
% if they push more than one key, don't accept it
if keyIsDown && sum(keyCode) == 1
% wait for key to be released
while KbCheck(-1)
WaitSecs(0.0001);
% % proceed if time is up, regardless of whether key is held
% if (GetSecs - startRT) > phaseCfg.expo_response
% break
% end
end
% if cfg.text.printTrialInfo
% fprintf('"%s" typed at time %.3f seconds\n', KbName(keyCode), endRT - startRT);
% end
if (keyCode(cfg.keys.expoVA) == 1 && all(keyCode(~cfg.keys.expoVA) == 0)) ||...
(keyCode(cfg.keys.expoSA) == 1 && all(keyCode(~cfg.keys.expoSA) == 0)) ||...
(keyCode(cfg.keys.expoSU) == 1 && all(keyCode(~cfg.keys.expoSU) == 0)) ||...
(keyCode(cfg.keys.expoVU) == 1 && all(keyCode(~cfg.keys.expoVU) == 0))
break
end
elseif keyIsDown && sum(keyCode) > 1
% draw the image stimulus
Screen('DrawTexture', w, expoImgTex(i), [], stimImgRect);
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
if phaseCfg.fixDuringStim
% and fixation on top of it
Screen('TextSize', w, cfg.text.fixSize);
%DrawFormattedText(w,cfg.text.fixSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
Screen('DrawText', w, cfg.text.fixSymbol, fixRectX, fixRectY, cfg.text.fixationColor);
end
% don't push multiple keys
Screen('TextSize', w, cfg.text.instructTextSize);
DrawFormattedText(w,cfg.text.multiKeyText,'center',errorTextY,cfg.text.errorTextColor, cfg.text.instructCharWidth);
% photocell rect with stim
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellRectColor, cfg.stim.photoCellRect);
end
% put them on the screen
Screen('Flip', w);
keyIsDown = 0;
end
end
% Wait <1 ms before checking the keyboard again to prevent
% overload of the machine at elevated Priority():
WaitSecs(0.0001);
end
% wait out any remaining time
while (GetSecs - stimOnset) <= phaseCfg.expo_stim
% Wait <1 ms before checking the keyboard again to prevent
% overload of the machine at elevated Priority():
WaitSecs(0.0001);
end
keyIsDown = logical(keyIsDown);
if keyIsDown
% if they hit a key while the stimulus was on the screen (the only way
% keyIsDown==1)
% code that follows this if statement block will take the stimulus off
% screen and give feedback if this is a practice phase
respPromptOn = NaN;
else
% draw the image stimulus
Screen('DrawTexture', w, expoImgTex(i), [], stimImgRect);
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
if phaseCfg.fixDuringStim
% and fixation on top of it
Screen('TextSize', w, cfg.text.fixSize);
%DrawFormattedText(w,cfg.text.respSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
Screen('DrawText', w, cfg.text.respSymbol, respRectX, respRectY, cfg.text.fixationColor);
end
% photocell rect with stim
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellRectColor, cfg.stim.photoCellRect);
end
[respPromptOn, startRT] = Screen('Flip',w);
% poll for a resp
while (GetSecs - startRT) <= phaseCfg.expo_response
[keyIsDown, endRT, keyCode] = KbCheck;
% if they push more than one key, don't accept it
if keyIsDown && sum(keyCode) == 1
% wait for key to be released
while KbCheck(-1)
WaitSecs(0.0001);
% % proceed if time is up, regardless of whether key is held
% if (GetSecs - startRT) > phaseCfg.expo_response
% break
% end
end
% if cfg.text.printTrialInfo
% fprintf('"%s" typed at time %.3f seconds\n', KbName(keyCode), endRT - startRT);
% end
if (keyCode(cfg.keys.expoVA) == 1 && all(keyCode(~cfg.keys.expoVA) == 0)) ||...
(keyCode(cfg.keys.expoSA) == 1 && all(keyCode(~cfg.keys.expoSA) == 0)) ||...
(keyCode(cfg.keys.expoSU) == 1 && all(keyCode(~cfg.keys.expoSU) == 0)) ||...
(keyCode(cfg.keys.expoVU) == 1 && all(keyCode(~cfg.keys.expoVU) == 0))
break
end
elseif keyIsDown && sum(keyCode) > 1
% draw the image stimulus
Screen('DrawTexture', w, expoImgTex(i), [], stimImgRect);
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
if phaseCfg.fixDuringStim
% and fixation on top of it
Screen('TextSize', w, cfg.text.fixSize);
%DrawFormattedText(w,cfg.text.respSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
Screen('DrawText', w, cfg.text.respSymbol, respRectX, respRectY, cfg.text.fixationColor);
end
% don't push multiple keys
Screen('TextSize', w, cfg.text.instructTextSize);
DrawFormattedText(w,cfg.text.multiKeyText,'center',errorTextY,cfg.text.errorTextColor, cfg.text.instructCharWidth);
% photocell rect with stim
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellRectColor, cfg.stim.photoCellRect);
end
% put them on the screen
Screen('Flip', w);
keyIsDown = 0;
end
% wait so we don't overload the system
WaitSecs(0.0001);
end
keyIsDown = logical(keyIsDown);
end
% determine response and compute accuracy
if keyIsDown
if (keyCode(cfg.keys.expoVA) == 1 && all(keyCode(~cfg.keys.expoVA) == 0))
resp = cfg.text.expoVAresp;
message = '';
elseif (keyCode(cfg.keys.expoSA) == 1 && all(keyCode(~cfg.keys.expoSA) == 0))
resp = cfg.text.expoSAresp;
message = '';
elseif (keyCode(cfg.keys.expoSU) == 1 && all(keyCode(~cfg.keys.expoSU) == 0))
resp = cfg.text.expoSUresp;
message = '';
elseif (keyCode(cfg.keys.expoVU) == 1 && all(keyCode(~cfg.keys.expoVU) == 0))
resp = cfg.text.expoVUresp;
message = '';
elseif sum(keyCode) > 1
warning('Multiple keys were pressed.\n');
resp = 'ERROR_MULTIKEY';
elseif sum(~ismember(find(keyCode == 1),[cfg.keys.expoVA, cfg.keys.expoSA, cfg.keys.expoSU, cfg.keys.expoVU])) > 0
warning('Key other than expo VA/SA/SU/VU was pressed. This should not happen.\n');
resp = 'ERROR_OTHERKEY';
else
warning('Some other error occurred.\n');
resp = 'ERROR_OTHER';
end
% if ~phaseCfg.isExp
% % only give feedback during practice
% feedbackTime = cfg.text.respondFasterFeedbackTime;
% else
% message = '';
% feedbackTime = 0.01;
% end
else
resp = 'NO_RESPONSE';
% % did not push a key
% acc = false;
% need a new endRT
endRT = GetSecs;
% "need to respond faster"
message = cfg.text.respondFaster;
feedbackColor = cfg.text.respondFasterColor;
feedbackTime = cfg.text.respondFasterFeedbackTime;
if phaseCfg.playSound
respSound = phaseCfg.incorrectSound;
respVol = phaseCfg.incorrectVol;
end
end
if ~isempty(message)
if phaseCfg.playSound && (~phaseCfg.isExp || (phaseCfg.isExp && ~keyIsDown))
Beeper(respSound,respVol);
end
Screen('TextSize', w, cfg.text.instructTextSize);
DrawFormattedText(w,message,'center','center',feedbackColor, cfg.text.instructCharWidth);
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
Screen('Flip', w);
% wait to let them view the feedback
WaitSecs(feedbackTime);
end
% get key pressed by subject
if keyIsDown
if sum(keyCode) == 1
respKey = KbName(keyCode);
elseif sum(keyCode) > 1
thisResp = KbName(keyCode);
respKey = sprintf('multikey%s',sprintf(repmat(' %s',1,numel(thisResp)),thisResp{:}));
end
else
respKey = 'NO_RESPONSE_KEY';
end
if (phaseCfg.expo_isi > 0 && phaseCfg.fixDuringISI) || (phaseCfg.expo_isi == 0 && phaseCfg.fixDuringPreStim)
if phaseCfg.showRespBtStim
% draw response prompt
Screen('DrawTexture', w, expoKeyImg, [], expoKeyImgRect);
end
% draw fixation after response
Screen('TextSize', w, cfg.text.fixSize);
%DrawFormattedText(w,cfg.text.fixSymbol,'center','center',cfg.text.fixationColor, cfg.text.instructCharWidth);
Screen('DrawText', w, cfg.text.fixSymbol, fixRectX, fixRectY, cfg.text.fixationColor);
end
if cfg.stim.photoCell
Screen('FillRect', w, cfg.stim.photoCellAntiRectColor, cfg.stim.photoCellRect);
end
% clear screen
Screen('Flip', w);
% Close the image stimulus before next trial
Screen('Close', expoImgTex(i));
% compute response time
if phaseCfg.respDuringStim
measureRTfromHere = stimOnset;
else
measureRTfromHere = startRT;
end
rt = int32(round(1000 * (endRT - measureRTfromHere)));
if cfg.text.printTrialInfo
fprintf('Trial %d of %d: response: %s (key: %s; rt = %d)\n',i,length(expoStims_img),resp,respKey,rt);
end
% img stimulus properties
i_catNum = int32(expoStims_img(i).categoryNum);
i_stimNum = int32(expoStims_img(i).stimNum);
% both stimuli
targStatus = expoStims_img(i).targ;
spacStatus = expoStims_img(i).spaced;
studyLag = int32(expoStims_img(i).lag);
%% session log file
% TODO: put image and word in correct order
% Write image stimulus presentation to file:
fprintf(logFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t%d\n',...
imgOn,...
expParam.subject,...
sesName,...
phaseName,...
phaseCount,...
phaseCfg.isExp,...
'EXPO_IMAGE',...
i,...
expoStims_img(i).fileName,...
i_stimNum,...
targStatus,...
spacStatus,...
studyLag,...
int32(expoStims_img(i).presNum),...
int32(expoStims_img(i).pairNum),...
int32(expoStims_img(i).pairOrd),...
expoStims_img(i).categoryStr,...
i_catNum);
% Write trial result to file:
fprintf(logFile,'%f\t%s\t%s\t%s\t%d\t%d\t%s\t%d\t%s\t%s\t%d\n',...
endRT,...
expParam.subject,...
sesName,...
phaseName,...
phaseCount,...
phaseCfg.isExp,...
'EXPO_RESP',...
i,...
resp,...
respKey,...
rt);
%% phase log file