-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexp_feargen_PFfitting_YN.m
1008 lines (842 loc) · 40.4 KB
/
exp_feargen_PFfitting_YN.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 [p]=exp_feargen_PFfitting_YN(subject,phase,csp_degree)
% Diskrimination Task estimating the Threshold alpha and Slope beta of an
% observer's underlying Psychometric Function (PF).
% Enter the subject Number as well as the CS+ Face in Degrees (where 00 is
% 1st face, and so)
simulation_mode = 0;
ncircle=1;
ListenChar(2);%disable pressed keys to be spitted around
commandwindow;
%clear everything
clear mex global functions
cgshut;
global cogent;
%%%%%%%%%%%load the GETSECS mex files so call them at least once
GetSecs;
WaitSecs(0.001);
el = [];
p = [];
SetParams;
SetPTB;
InitEyeLink;
WaitSecs(2);
%calibrate if we are at the scanner computer.
if strcmp(p.hostname,'triostim1') || strcmp(p.hostname,'etpc');
CalibrateEL;
end
%save again the parameter file
save(p.path.path_param,'p');
% make a break every ....th Trial
breakpoint=50;
% counter for within chain trials (cc) and global trials (tt)
cc=zeros(1,tchain);
tt=0;
%trialID is counting every single face (2 per Trial_YN), need that for
%Eyelink
trialID=0;
for nc = 1:tchain
%set up procedure
PM{nc} = PAL_AMPM_setupPM('priorAlphaRange',p.psi.prioraaRange,'priorBetaRange',...
p.psi.priorBetaRange, 'priorLambdaRange', p.psi.priorLambdaRange,'priorGammaRange',...
p.psi.priorGammaRange,'numtrials',p.psi.numtrials, 'PF' , p.psi.PFfit, 'stimRange',p.psi.stimRange,...
'marginalize', [3 4]);
%
PM{nc}.reference_face = face_shift(nc);
PM{nc}.reference_circle = circle_shift(nc);
if p.psi.p0 ~= 0
shuffledummy=Shuffle(1:p.psi.numtrials);
p.psi.zerotrials(:,nc)=shuffledummy(1:ceil(p.psi.numtrials*p.psi.p0));
end
% set up Log Variable
SetupLog(nc);
end
ShowInstruction(1);
OK = 1;
while OK
current_chain = RandSample(1:tchain,[1 1]);
if PM{current_chain}.stop ~= 1
tt=tt+1;
% enter in break loop
if (tt~=1 && mod(tt,breakpoint)==1 && simulation_mode==0);
save(p.path.path_param,'p');
memory
ShowInstruction(4);
CalibrateEL;
end
cc(current_chain)=cc(current_chain)+1;
fprintf('Chain %4.2f , Trial %02d\n',current_chain,cc(current_chain))
fprintf('Original PM.x: %4.2f \n',PM{current_chain}.x(cc(current_chain)))
% manually force x=0 trial, if it fits the zerotrial condition
if p.psi.p0 ~= 0
if any(p.psi.zerotrials(:,current_chain)==cc(current_chain))
fprintf('Forcing x=0 Trial...\n')
PM{current_chain}.xCurrent=0;
PM{current_chain}.x(cc(current_chain))=0;
end
end
%Present trial here at stimulus intensity PM.xCurrent and collect
%response
direction = RandSample([-1 1],[1 1]);
fprintf('PM.x is now %4.2f \n',direction*PM{current_chain}.x(cc(current_chain)))
test = PM{current_chain}.xCurrent * direction + PM{current_chain}.reference_face + csp_degree + PM{current_chain}.reference_circle;
dummy = test;
% the computed degree has to stay in the same circle:
% whenever it goes left from the 00 degrees (360 at foreign),
% there's a problem
% for chain 1 and 2, values below 0 have to be shifted 360
% degrees,
% for chain 3 and 4, values below 360 have to be shifted 360 degrees
% e.g., -45 has to be 315 in chain 1; 315 has to be 675 in chain 3
% was done using mod... adding (0 0 360 360) (this is the last part)
test = mod(test,360)+ PM{current_chain}.reference_circle;
% the reference is one of the four faces
%(cs+ local, cs- local, cs+ foreign, cs- forein)
ref = PM{current_chain}.reference_face + csp_degree + PM{current_chain}.reference_circle;
ref = mod(ref,360)+ PM{current_chain}.reference_circle;
% fprintf('Chain: %03d\nxCurrent: %6.2f\nDirection:%6.2f\n %6.2f -> %6.2f vs. %6.2f\n',current_chain,PM{current_chain}.xCurrent,direction,dummy,test,ref);
% start Trial
fprintf('Starting Trial %03d/%03d.\n',tt,tchain*p.psi.numtrials)
[test_face, ref_face, signal,trialID] = Trial_YN(trialID,ref,test,circle_id(current_chain),tt);
fprintf('Rating.\n')
Screen('Textsize', p.ptb.w,p.text.fontsize);
%Rating Slider
%
message1 = 'Waren die Gesichter unterschiedlich oder gleich?\n';
message2 = 'Bewege den "Zeiger" mit der rechten und linken Pfeiltaste\n und bestätige deine Einschätzung mit der oberen Pfeiltaste.';
if ~simulation_mode
[response_subj] = RatingSlider(p.ptb.rect,2,Shuffle(1:2,1),p.keys.increase,p.keys.decrease,p.keys.confirm,{ 'unterschiedlich' 'gleich'},message1,message2,0);
% see if subject found the different pair of faces...
% buttonpress left (Yes) is response_subj=2, right alternative (No) outputs a 1.
% note that response=1 only means "yes", and not correct or anything
sdt=NaN;
if (response_subj == 2 && signal == 1)
response=1;
fprintf('...Hit. \n')
sdt=1;
elseif (response_subj==1 && signal == 1)
response=0;
fprintf('...Miss. \n')
sdt=3;
elseif (response_subj == 2 && signal==0)
response=1;
fprintf('...False Alarm. \n')
sdt=2;
elseif (response_subj == 1 && signal == 0)
response=0;
fprintf('...Correct Rejection. \n')
sdt=4;
else
fprintf('error in the answer algorithm! \n')
end
else
true_a = 45;
true_s = 10;
true_g = 0.2;
true_l = 0.02;
response = ObserverResponseFunction(p.psi.PFfit,true_a,1/true_s,true_g,true_l,PM{current_chain}.xCurrent);
end
% store everything in the Log
row = round(PM{current_chain}.xCurrent/p.stim.delta+1);
p.psi.log.trial_counter(row,current_chain) = p.psi.log.trial_counter(row,current_chain) + 1;
p.psi.log.xrounded(row,p.psi.log.trial_counter(row,current_chain),current_chain) = response;
%updating PM
PM{current_chain} = PAL_AMPM_updatePM(PM{current_chain},response);
SetLog;
%iteration control
dummy = cell2mat(PM);
OK = sum([dummy(:).stop]) ~= tchain;
end
%save Logfile here
%save([p.path.path_param 'Log' num2str(subject) '.mat'],'p.psi.log');
%save again the parameter file
save(p.path.path_param,'p');
end
%end of Experiment, shown to subject
ShowInstruction(2);
%Print summary of results to screen
for chain=1:tchain
fprintf('Chain %g: Estimated Threshold (alpha): %4.2f \n',chain,PM{chain}.threshold(end));
fprintf('Chain %g: Estimated Slope (beta): %4.2f \n',chain,PM{chain}.slope(end));
end
%get the eyelink file back to this computer
StopEyelink(p.path.edf);
%save([p.path.dropbox 'Log' num2str(subject) '.mat'],'p.psi.log')
% save PF Fit Plot
%feargenET_PFfitting_Fitplot(num2str(subject),p.log)
%clear the screen
%close everything down
cleanup;
%move the folder to appropriate location
movefile(p.path.subject,p.path.finalsubject);
function [test_face,ref_face,signal,trialID] = Trial_YN(trialID,ref_stim,test_stim,last_face_of_circle,tt)
Screen('Textsize', p.ptb.w,p.text.fixsize);
% computes the trial FACES, using the test/ref information input
% values (in Deg)
trial = [ref_stim test_stim]/p.stim.delta;
% correct face number within circle
trial = mod(round(trial),last_face_of_circle)+1;
ref_face = trial(1);
test_face = trial(2);
[trial,idx]= Shuffle(trial);
fprintf('Faces: %d %d \n',trial(1),trial(2));
%compute if trial had different faces or not
%if trial(1)=trial(2), they were the same and subject has to
%answer with 'no' (right option, is 1), else means correct hit
if trial(1)==trial(2)
signal = 0;
else
signal = 1;
end
trial_deg = [ref_stim test_stim];
isref=double(trial==ref_face) ;
delta_ref = MinimumAngleQuartile([trial_deg(idx(1)) trial_deg(idx(2))],ref);
delta_csp = MinimumAngleQuartile([trial_deg(idx(1)) trial_deg(idx(2))],csp_degree); % ...
abs_FGangle = [trial_deg(idx(1)) trial_deg(idx(2))];
%get fixation crosses and onsets from p parameter
fix = round(p.ptb.CrossPositions(tt,:));
trialID=trialID+1;
%GetSecs so that the onsets can be defined
onsets = 0.25+GetSecs;%fix1 onset
onsets = [onsets onsets(end)+p.duration.fix+rand(1)*.25];%stim1 onset
onsets = [onsets onsets(end)+p.duration.stim];%stim1 offset
fixdelta=p.duration.fix+rand(1)*.25;
onsets = [onsets onsets(end)+1.5-fixdelta];%fix2 onset
onsets = [onsets onsets(end)+fixdelta];%stim2 onset
onsets = [onsets onsets(end)+p.duration.stim];%stim2 offset
%fixation cross 1
FixCross = [fix(1)-1,fix(2)-20,fix(1)+1,fix(2)+20;fix(1)-20,fix(2)-1,fix(1)+20,fix(2)+1];
Screen('FillRect', p.ptb.w, [255,255,255], FixCross');
Eyelink('Message', 'FX Onset at %d %d',fix(1),fix(2));
Screen('Flip',p.ptb.w,onsets(1),0);
StartEyelinkRecording(trialID,phase,cc(current_chain),tt,current_chain,isref(1),trial(1),delta_ref(1),delta_csp(1),abs_FGangle(1),fix(1),fix(2));
%face trial(1)
Screen('DrawTexture',p.ptb.w,p.ptb.stim_sprites(trial(1)));
Screen('Flip',p.ptb.w,onsets(2),0);
Eyelink('Message', 'Stim Onset');
Eyelink('Message', 'SYNCTIME');
while GetSecs < onsets(3)
end
Screen('Flip',p.ptb.w,onsets(3),0);
StopEyelinkRecording;
%second face of the trial
trialID=trialID+1;
%fixation cross 2
FixCross = [fix(3)-1,fix(4)-20,fix(3)+1,fix(4)+20;fix(3)-20,fix(4)-1,fix(3)+20,fix(4)+1];
Screen('FillRect', p.ptb.w, [255,255,255], FixCross');
Eyelink('Message', 'FX Onset at %d %d',fix(3),fix(4));
Screen('Flip',p.ptb.w,onsets(4),0);
StartEyelinkRecording(trialID,phase,cc(current_chain),tt,current_chain,isref(2),trial(2),delta_ref(2),delta_csp(2),abs_FGangle(2),fix(3),fix(4));
%face trial(2)
Screen('DrawTexture', p.ptb.w, p.ptb.stim_sprites(trial(2)));
Screen('Flip',p.ptb.w,onsets(5),0);
Eyelink('Message', 'Stim Onset');
Eyelink('Message', 'SYNCTIME');
while GetSecs<onsets(6)
end
Screen('Flip',p.ptb.w,onsets(6),0);
StopEyelinkRecording;
end
function SetPTB
debug =0;
%Open a graphics window using PTB
screens = Screen('Screens');
[~, hostname] = system('hostname');
p.hostname = deblank(hostname);
if strcmp(p.hostname,'etpc')
screenNumber=1;
else
screenNumber=max(screens);
end
%make everything transparent for debuggin purposes.
if debug
commandwindow;
PsychDebugWindowConfiguration;
end
Screen('Preference', 'SkipSyncTests', 1);
Screen('Preference', 'DefaultFontSize', p.text.fontsize);
Screen('Preference', 'DefaultFontName', p.text.fontname);
%
[p.ptb.w ] = Screen('OpenWindow', screenNumber, p.stim.bg);
[p.ptb.width, p.ptb.height] = Screen('WindowSize', screenNumber);
%find the mid position.
p.ptb.midpoint = [ p.ptb.width./2 p.ptb.height./2];
p.ptb.imrect = [ p.ptb.midpoint(1)-p.stim.width/2 p.ptb.midpoint(2)-p.stim.height/2 p.stim.width p.stim.height];
%area of the slider
p.ptb.rect = [p.ptb.midpoint(1)*0.5 p.ptb.midpoint(2)*0.8 p.ptb.midpoint(1) p.ptb.midpoint(2)*0.2];
%compute the cross position.
[nx ny bb] = DrawFormattedText(p.ptb.w,'+','center','center');
Screen('FillRect',p.ptb.w,p.stim.bg);
% p.ptb.cross_shift = [45 50];%upper and lower cross positions
% p.ptb.CrossPosition_y = [ny-p.ptb.cross_shift(1) ny+p.ptb.cross_shift(2) ];
% p.ptb.CrossPosition_x = [bb(1) bb(1)];
% p.ptb.CrossPositionET_x = [p.ptb.midpoint(1) p.ptb.midpoint(1)];
% p.ptb.CrossPositionET_y = [p.ptb.midpoint(2)-p.ptb.cross_shift(2) p.ptb.midpoint(2)+p.ptb.cross_shift(2)];
p.ptb.CrossPositions = FixationCrossPool;
Priority(MaxPriority(p.ptb.w));
for nStim = 1:p.stim.tFile
filename = p.stim.files(nStim,:);
[im , ~, ~] = imread(filename);
%what is this good for?
if ndims(im) == 3
p.stim.stim(:,:,nStim) = rgb2gray(im);
else
p.stim.stim(:,:,nStim) = im;
end
p.ptb.stim_sprites(nStim) = Screen('MakeTexture', p.ptb.w, im );
end
p.stim.delta = ncircle*360/p.stim.tFile;
%create tukeywindow for the pink noise fadeout
w =.3;
p.ptb.tw = tukeywin(p.stim.height,w)*tukeywin(p.stim.height,w)';
function [cross_positions]=FixationCrossPool
radius = 590; %in px (around 14 degrees (37 px/deg))
center = [800 600];
%setting up fixation cross pool vector of size
% totaltrials x 4 (face_1_x face_1_y face_2_x face_2_y)
cross_directions = round(rand(tchain*p.psi.numtrials,2))*180;
dummy = cross_directions + rand(tchain*p.psi.numtrials,2)*30-15;
cross_positions = [cosd(dummy(:,1))*radius+center(1) sind(dummy(:,1))*radius+center(2)...
cosd(dummy(:,2))*radius+center(1) sind(dummy(:,2))*radius+center(2)];
end
end
function SetParams
%
p.var.timings = zeros(1,10);
p.var.event_count = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%relative path to stim and experiments
%Path Business.
[~, hostname] = system('hostname');
p.hostname = deblank(hostname);
if strcmp(p.hostname,'triostim1')
p.path.baselocation = 'C:\USER\onat\Experiments\';
elseif strcmp(p.hostname,'etpc')
p.path.baselocation = 'C:\Users\onat\Documents\Experiments\';
else
p.path.baselocation = 'C:\Users\onat\Documents\Experiments\';
end
p.path.experiment = [p.path.baselocation 'feargen_master\'];
p.path.stimfolder = 'stim\32discrimination';
p.path.stim = [p.path.experiment p.path.stimfolder '\'];
p.path.stim24 = [p.path.experiment p.path.stimfolder '\' '24bits' '\'];
%
p.subID = sprintf('sub%02d',subject);
p.path.edf = sprintf(['s%03dp%02d' ],subject,phase);
timestamp = datestr(now,30);
p.path.subject = [p.path.experiment 'data\tmp\' p.subID '_' timestamp '\'];
p.path.finalsubject = [p.path.experiment 'data\' p.subID '_' timestamp '\' ];
p.path.dropbox = 'C:\Users\onat\Dropbox\feargen_lea\EthnoMaster\DiscriminationTask\pilotedata\data';
%create folder hierarchy
mkdir(p.path.subject);
mkdir([p.path.subject 'stimulation']);
mkdir([p.path.subject 'pmf']);
p.path.path_param = sprintf([regexprep(p.path.subject,'\\','\\\') 'stimulation\\' 'p.mat']);
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%get stim files
dummy = dir([p.path.stim '*.bmp']);
p.stim.files = [repmat([fileparts(p.path.stim) filesep],length(dummy),1) vertcat(dummy(:).name)];
p.stim.label = {dummy(:).name};
p.stim.tFile = size(p.stim.files,1);%number of different files
p.stim.tFace = p.stim.tFile;%number of faces.
%
display([mat2str(p.stim.tFile) ' found in the destination.']);
%set the background gray according to the background of the stimuli
for i = 1:p.stim.tFile;
im = imread(p.stim.files(i,:));
bg(i) = im(1,1,1);
end
%is all the captured bg values the same?
if sum(diff(bg))==0;
%if so take it as the bg color
p.stim.bg = double([bg(1) bg(1) bg(1)]);
else
fprintf('background luminance was not successfully detected...\n')
keyboard;
end
%
%font size and background gray level
p.text.fontname = 'Times New Roman';
p.text.fontsize = 18;%30;
p.text.fixsize = 60;
%rating business
p.rating.division = 2;%number of divisions for the rating slider
%
p.stim.white = [255 255 255];
%get the actual stim size (assumes all the same)
info = imfinfo(p.stim.files(1,:));
p.stim.width = info.Width;
p.stim.height = info.Height;
if strcmp(p.hostname,'triostim1')
p.keys.confirm = KbName('7');
p.keys.increase = KbName('8');
p.keys.decrease = KbName('6');
p.keys.space = KbName('space');
p.keys.esc = KbName('esc');
else
%All settings for laptop computer.
p.keys.confirm = KbName('up');
p.keys.increase = KbName('right');
p.keys.decrease = KbName('left');
p.keys.space = KbName('space');
p.keys.esc = KbName('esc');
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%timing business
%these are the intervals of importance
%time2fixationcross->cross2onset->onset2shock->shock2offset
%these (duration.BLA) are average duration values:
% 1.5 0.5 0.5
p.duration.stim = 0.6;%s
%p.duration.pink = 0.7;%0.7
p.duration.fix = .85;
%p.duration.gray = 0;
if simulation_mode
p.duration.stim = .001;%s
p.duration.pink = .001;
p.duration.fix = .001;
%p.duration.gray = .001;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%create the randomized design
p.stim.cs_plus = csp_degree;%index of cs stimulus, this is the one paired to shock
%p.stim.cs_neg = csn;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%priors for the PSI fitting structure PM
%% Define prior, these are always the same so defining once is enough.
p.psi.prioraaRange = linspace(0,169,50); %values of aa to include in prior
%IS THIS RANGE OF BETA VALUES REASONABLE?
p.psi.priorBetaRange = linspace(-2,0,50); %values of log_10(beta) to include in prior
%Range of lapse rates for the marginalized estimation of lambda
%Prins(2013) uses 0:0.01:0.1;
p.psi.priorLambdaRange = 0:0.01:0.05;
%Range of guess rates (Prins: 0:0.03:0.3);
p.psi.priorGammaRange = 0:0.03:0.3;
% Stimulus values to select from (need not be equally spaced)
p.psi.stimRange = 0:11.25:169;
%Function to be fitted during procedure
p.psi.PFfit = @PAL_CumulativeNormal; %Shape to be assumed
%Termination after n Trials
p.psi.numtrials = 100;
% percentage of obligatory x=0 trials
p.psi.p0 = .2;
%set up procedure
PM = [];
face_shift = [0 180 0 180];
circle_shift = [0 0 360 360];
circle_id = [1 1]*p.stim.tFace;
%circle_id = [1 1 2 2]*p.stim.tFace/2 % for 2 circles;
tchain = 2;
%Save the stuff
save(p.path.path_param,'p');
%
end
function [rating]=RatingSlider(rect,tSection,position,up,down,confirm,labels,message1,message2,numbersOn)
%
%Detect the bounding boxes of the labels.
for nlab = 1:2
[nx, ny, bb(nlab,:)]=DrawFormattedText(p.ptb.w,labels{nlab}, 'center', 'center', p.stim.white,[],[],[],2);
Screen('FillRect',p.ptb.w,p.stim.bg);
end
bb = max(bb);
bb_size = bb(3)-bb(1);%vertical size of the bb.
%
DrawSkala;
ok = 1;
while ok == 1
[secs, keyCode, deltaSecs] = KbStrokeWait;
keyCode = find(keyCode);
if (keyCode == up) | (keyCode == down)
next = position + increment(keyCode);
if next < (tSection+1) & next > 0
position = position + increment(keyCode);
rating = tSection - position + 1;
end
DrawSkala;
elseif keyCode == confirm
WaitSecs(0.1);
ok = 0;
Screen('FillRect',p.ptb.w,p.stim.bg);
Screen('Flip',p.ptb.w);
end
end
function DrawSkala
rating = tSection - position + 1;
increment([up down]) = [1 -1];%delta
tick_x = linspace(rect(1),rect(1)+rect(3),tSection+1);%tick positions
tick_size = rect(3)./tSection;
ss = tick_size/5*0.9;%slider size.
%
for tick = 1:length(tick_x)%draw ticks
Screen('DrawLine', p.ptb.w, [255 0 0], tick_x(tick), rect(2), tick_x(tick), rect(2)+rect(4) , 3);
if tick <= tSection && numbersOn
Screen('TextSize', p.ptb.w,p.text.fontsize./2);
DrawFormattedText(p.ptb.w, mat2str(tick) , tick_x(tick)+ss/2, rect(2)+rect(4), p.stim.white);
Screen('TextSize', p.ptb.w,p.text.fontsize);
end
if tick == 1
DrawFormattedText(p.ptb.w, labels{1},tick_x(tick)-bb_size*1.6,rect(2), p.stim.white);
elseif tick == tSection+1
DrawFormattedText(p.ptb.w, labels{2},tick_x(tick)+bb_size*0.2,rect(2), p.stim.white);
end
end
%slider coordinates
slider = [ tick_x(position)+tick_size*0.1 rect(2) tick_x(position)+tick_size*0.9 rect(2)+rect(4)];
%draw the slider
Screen('FillRect',p.ptb.w, p.stim.white, round(slider));
Screen('TextSize', p.ptb.w,p.text.fontsize);
DrawFormattedText(p.ptb.w,message1, 'center', p.ptb.midpoint(2)*0.2, p.stim.white,[],[],[],2);
Screen('TextSize', p.ptb.w,p.text.fontsize./2);
DrawFormattedText(p.ptb.w,message2, 'center', p.ptb.midpoint(2)*0.4, p.stim.white,[],[],[],2);
Screen('TextSize', p.ptb.w,p.text.fontsize);
Screen('Flip',p.ptb.w);
end
end
function [shuffled,idx] = Shuffle(vector,N)
%takes first N from the SHUFFLED before outputting. This function
%could be used as a replacement for randsample
if nargin < 2;N = length(vector);end
[dummy, idx] = sort(rand([1 length(vector)]));
shuffled = vector(idx(1:N));
shuffled = shuffled(:);
end
function [a]=MinimumAngleQuartile(y,x)
%[a]=MinimumAngle(x,y);
%
%finds the minimum angle between two angles given in degrees, the answer is
%also in degrees. The clockwise distances from Y to X are considered as
%positive. Opposite angles are considered as positive 180.
x = deg2rad(x);
y = deg2rad(y);
a = atan2(sin(x-y), cos(x-y));
a = -round(((180/pi)*a)*4)/4;
if any(abs(a) == 180);
a(abs(a) == 180) = 180;
end
end
function ShowInstruction(nInstruct)
[text]=GetText(nInstruct);
ShowText(text);
%let subject read it and ask confirmation to proceed.
KbStrokeWait;
Screen('FillRect',p.ptb.w,p.stim.bg);
Screen('Flip',p.ptb.w);
end
function ShowText(text)
Screen('FillRect',p.ptb.w,p.stim.bg);
%DrawFormattedText(p.ptb.w, text, p.text.start_x, 'center',p.stim.white,[],[],[],2,[]);
DrawFormattedText(p.ptb.w, text, 'center', 'center',p.stim.white,[],[],[],2,[]);
Screen('Flip',p.ptb.w);
%show the messages at the experimenter screen
fprintf([repmat('=',1,50) '\n']);
fprintf('Subject''s monitor:\n');
fprintf(text);
fprintf([repmat('=',1,50) '\n']);
end
function [text]=GetText(nInstruct)
if nInstruct == 0%Eyetracking calibration
text = ['Um Ihre Augenbewegungen zu messen, \n' ...
'müssen wir jetzt den Eye-Tracker kalibrieren.\n' ...
'Dazu zeigen wir Ihnen einige Punkte auf dem Bildschirm, \n' ...
'bei denen Sie sich wie folgt verhalten:\n' ...
'Bitte fixieren Sie das Fixationskreuz und \n' ...
'bleiben Sie so lange darauf, wie es zu sehen ist.\n' ...
'Bitte drücken Sie jetzt den mittleren Knopf, \n' ...
'um mit der Kalibrierung weiterzumachen.\n' ...
];
elseif nInstruct == 1
text = ['Sie sehen nun nacheinander zwei Gesichter.\n'...
'\n'...
'Danach werden Sie gefragt, ob die Gesichter unterschiedlich oder gleich waren.\n'...
'\n'...
'Benutzen Sie dazu die Pfeiltasten (links, rechts) und die obere Taste zum Bestätigen.\n'...
'\n'...
'Wenn Sie noch Fragen haben, können Sie jetzt die Versuchsleiterin fragen.\n'...
'Wir können Sie jederzeit hören.\n'...
'\n'...
'Drücken Sie ansonsten die mittlere Taste,\n'...
' um das Experiment zu starten.\n' ...
];
elseif nInstruct == 3
text = ['You will now see two faces after each other.\n'...
'\n'...
'Please state, if they were different (left) or the same (right).\n'...
'\n'...
'Use the arrow keys to move the marker and confirm with the UP button.\n'...
'\n'...
'Please press UP\n'...
' to start the experiment!\n' ...
];
elseif nInstruct == 2%end
text = 'Experiment beendet!\n';
elseif nInstruct==4%break
text = [sprintf('Sie haben bereits %g von %g Durchgängen geschafft!\n',tt-1,p.psi.numtrials*tchain)...
'Machen Sie eine kurze Pause, lehnen Sie sich gern einen Moment zurück\n'...
'und schließen Sie die Augen, um diese zu entspannen.\n'...
'Drücken Sie anschließend die mittlere Taste, um weiterzumachen.\n'];
end
end
function SetupLog(nc)
p.psi.log.globaltrial= NaN(nc,p.psi.numtrials);
p.psi.log.signal = NaN(nc,p.psi.numtrials);
p.psi.log.x = NaN(nc,p.psi.numtrials);
p.psi.log.refface = NaN(nc,p.psi.numtrials);
p.psi.log.testface = NaN(nc,p.psi.numtrials);
p.psi.log.response = NaN(nc,p.psi.numtrials);
p.psi.log.alpha = NaN(nc,p.psi.numtrials);
p.psi.log.seAlpha = NaN(nc,p.psi.numtrials);
p.psi.log.beta = NaN(nc,p.psi.numtrials);
p.psi.log.seBeta = NaN(nc,p.psi.numtrials);
p.psi.log.gamma = NaN(nc,p.psi.numtrials);
p.psi.log.seGamma = NaN(nc,p.psi.numtrials);
p.psi.log.lambda = NaN(nc,p.psi.numtrials);
p.psi.log.seLambda = NaN(nc,p.psi.numtrials);
p.psi.log.xrounded = NaN(p.stim.tFace/tchain+1,p.psi.numtrials,nc);
p.psi.log.sdt = NaN(nc,p.psi.numtrials);
p.psi.log.trial_counter = zeros(p.stim.tFace/tchain+1,nc);
end
function SetLog
p.psi.log.globaltrial(current_chain,cc(current_chain))= tt;
p.psi.log.signal(current_chain,cc(current_chain)) = signal;
p.psi.log.x(current_chain,cc(current_chain)) = PM{current_chain}.x(cc(current_chain))*direction;
p.psi.log.refface(current_chain,cc(current_chain)) = ref_face;
p.psi.log.testface(current_chain,cc(current_chain)) = test_face;
p.psi.log.response(current_chain,cc(current_chain)) = response;
p.psi.log.alpha(current_chain,cc(current_chain)) = PM{current_chain}.threshold(end);
p.psi.log.seAlpha(current_chain,cc(current_chain)) = PM{current_chain}.seThreshold(end);
p.psi.log.beta(current_chain,cc(current_chain)) = PM{current_chain}.slope(end);
p.psi.log.seBeta(current_chain,cc(current_chain)) = PM{current_chain}.seSlope(end);
p.psi.log.gamma(current_chain,cc(current_chain)) = PM{current_chain}.guess(end);
p.psi.log.seGamma(current_chain,cc(current_chain)) = PM{current_chain}.seGuess(end);
p.psi.log.lambda(current_chain,cc(current_chain)) = PM{current_chain}.lapse(end);
p.psi.log.seLambda(current_chain,cc(current_chain)) = PM{current_chain}.seLapse(end);
p.psi.log.sdt(current_chain,cc(current_chain)) = sdt;%1=hit 2=FA 3=miss 4=CR
end
% function PlotProcedure
% plotproc=figure(1);
% % title(sprintf('Threshold Estimation for subject %02d',tchain),'FontSize',14)
% for sub=1:tchain;
% subplot(2,2,sub)
% t = 1:length(p.log.x(sub));
% hold on;
% plot(t,abs(p.log.x(sub)),'bo-');
% errorbar(t,p.log.alpha(sub),p.log.seAlpha(sub),'r--')
% plot(t(p.log.response(sub) == 1),p.log.x(sub)(p.log.response(sub) == 1),'ko', ...
% 'MarkerFaceColor','k');
% plot(t(p.log.response(sub) == 0),p.log.x(sub)(p.log.response(sub) == 0),'ko', ...
% 'MarkerFaceColor','w');
%
% set(gca,'FontSize',12);
% axis([0 max(t)+1 0 max(p.log{sub}.alpha)+max(p.log{sub}.seAlpha)+20])
% xlabel('Trial');
% ylabel('xCurrent (Deg)');
%
% end
% annotation('textbox', [0 0.9 1 0.1], 'String',...
% (sprintf('Threshold Estimation for subject %02d',tchain)), ...
% 'EdgeColor', 'none', ...
% 'HorizontalAlignment', 'center','FontSize',14)
% legend('xCurrent','estimated Threshold','Response = 1','Response = 0','Location','northeast');
%
% % end
% function Plot_Fit
% plotfit=figure(2);
% for sub=1:tchain;
% subplot(2,2,sub)
% t = 1:length(p.log{sub}.x);
% hold on;
% plot(t,abs(p.log{sub}.x),'bo-');
% errorbar(t,p.log{1}.alpha,p.log{1}.seAlpha,'r--')
% plot(t(p.log{sub}.response == 1),p.log{sub}.x(p.log{sub}.response == 1),'ko', ...
% 'MarkerFaceColor','k');
% plot(t(p.log{sub}.response == 0),p.log{sub}.x(p.log{sub}.response == 0),'ko', ...
% 'MarkerFaceColor','w');
%
% set(gca,'FontSize',12);
% axis([0 max(t)+1 0 max(p.log{sub}.alpha)+max(p.log{sub}.seAlpha)+20])
% xlabel('Trial');
% ylabel('xCurrent (Deg)');
%
% end
function [t]=StartEyelinkRecording(trialID,phase,cc,tt,current_chain,isref,file,delta_ref,delta_csp,abs_FGangle,fixx,fixy)
t = [];
Eyelink('Message', 'TRIALID:%04d, PHASE:%04d, CHAIN:%04d, CHAINTRIAL:%04d, TTRIAL:%04d, ISREF:%04d, FILE:%04d, DELTAREF:%04d, DELTACSP:%04d, FGDEG:%04d, FXX:%04d, FXY:%04d',trialID, phase,...
current_chain, cc, tt, isref, file, delta_ref*100, delta_csp*100, abs_FGangle*100,fixx,fixy);
% an integration message so that an image can be loaded as
% overlay background when performing Data Viewer analysis.
WaitSecs(0.01);
Eyelink('Message', '!V IMGLOAD CENTER %s %d %d', p.stim.files(file,:), p.ptb.midpoint(1), p.ptb.midpoint(2));
% This supplies the title at the bottom of the eyetracker display
Eyelink('Command', 'record_status_message "Stim: %d, Phase: %d"',file, phase);
%
%Put the tracker offline and draw the stimuli.
Eyelink('Command', 'set_idle_mode');
WaitSecs(0.01);
% clear tracker display and draw box at center
Eyelink('Command', 'clear_screen %d', 0);
%draw the image on the screen
Eyelink('ImageTransfer',p.stim.files24(file,:),p.ptb.imrect(1),p.ptb.imrect(2),p.ptb.imrect(3),p.ptb.imrect(4),p.ptb.imrect(1),p.ptb.imrect(2));
Eyelink('Command', 'draw_cross %d %d 15',fixx,fixy);
%
%drift correction
%EyelinkDoDriftCorrection(el,crosspositionx,crosspositiony,0,0);
%start recording following mode transition and a short pause.
Eyelink('Command', 'set_idle_mode');
WaitSecs(0.01);
Eyelink('StartRecording');
t = GetSecs;
Log(t,8,NaN);
end
function [t]=StopEyelinkRecording
Eyelink('Message', 'Stim Offset');
Eyelink('Message', 'BLANK_SCREEN');
Eyelink('StopRecording');
t = GetSecs;
%this is the end of the trial scope.
WaitSecs(0.01);
Eyelink('Message', 'TRIAL_RESULT 0');
%
WaitSecs(0.01);
Eyelink('Command', 'set_idle_mode');
WaitSecs(0.01);
Eyelink('Command', 'clear_screen %d', 0);
Log(t,-8,NaN);
end
function InitEyeLink
%
if EyelinkInit(0)%use 0 to init normaly
fprintf('=================\nEyelink initialized correctly...\n')
else
fprintf('=================\nThere is problem in Eyelink initialization\n')
keyboard;
end
%
WaitSecs(0.5);
[~, vs] = Eyelink('GetTrackerVersion');
fprintf('=================\nRunning experiment on a ''%s'' tracker.\n', vs );
%load 24bits pictures for eyelink...
dummy = dir([p.path.stim24 '*.bmp']);
p.stim.files24 = [repmat([fileparts(p.path.stim24) filesep],length(dummy),1) vertcat(dummy(:).name)];
% for i=1:32
% filename = p.stim.files24(nStim,:);
% [im , ~, ~] = imread(filename);
% end
%
%
el = EyelinkInitDefaults(p.ptb.w);
%update the defaults of the eyelink tracker
el.backgroundcolour = p.stim.bg;
el.msgfontcolour = WhiteIndex(el.window);
el.imgtitlecolour = WhiteIndex(el.window);
el.targetbeep = 0;
el.calibrationtargetcolour = WhiteIndex(el.window);
el.calibrationtargetsize = 1.5;
el.calibrationtargetwidth = 0.5;
el.displayCalResults = 1;
el.eyeimgsize = 50;
el.waitformodereadytime = 25;%ms
el.msgfont = 'Times New Roman';
el.cal_target_beep = [0 0 0];%[1250 0.6 0.05];
%shut all sounds off
el.drift_correction_target_beep = [0 0 0];
el.calibration_failed_beep = [0 0 0];
el.calibration_success_beep = [0 0 0];
el.drift_correction_failed_beep = [0 0 0];
el.drift_correction_success_beep= [0 0 0];
EyelinkUpdateDefaults(el);
%PsychEyelinkDispatchCallback(el)
% open file.
res = Eyelink('Openfile', p.path.edf);
if res == -3
fprintf('File cannot be created!!!!\n');
return;
end
%
Eyelink('command', 'add_file_preamble_text ''Recorded by EyelinkToolbox FearCloud Experiment''');
Eyelink('command', 'screen_pixel_coords = %ld %ld %ld %ld', 0, 0, p.ptb.width-1, p.ptb.height-1);
Eyelink('message', 'DISPLAY_COORDS %ld %ld %ld %ld', 0, 0, p.ptb.width-1, p.ptb.height-1);
% set calibration type.
Eyelink('command', 'calibration_type = HV13');
Eyelink('command','auto_calibration_messages = YES');
Eyelink('command', 'select_parser_configuration = 1');
%what do we want to record
Eyelink('command', 'file_sample_data = LEFT,RIGHT,GAZE,HREF,AREA,GAZERES,STATUS,INPUT,HTARGET');
Eyelink('command', 'file_event_filter = LEFT,RIGHT,FIXATION,SACCADE,BLINK,MESSAGE,BUTTON,INPUT');
Eyelink('command', 'use_ellipse_fitter = no');
% set sample rate in camera setup screen
Eyelink('command', 'sample_rate = %d',1000);
end
function StopEyelink(filename)
try
fprintf('Trying to stop the Eyelink system with StopEyelink\n');
Eyelink('StopRecording');
WaitSecs(0.5);
Eyelink('Closefile');
display('receiving the EDF file...');
Eyelink('ReceiveFile',filename,[p.path.subject '\eye\'],1);
display('...finished!')
% Shutdown Eyelink:
Eyelink('Shutdown');
catch
display('StopEyeLink routine didn''t really run well');
end
end
function CalibrateEL
fprintf('=================\n=================\nEntering Eyelink Calibration\n')
p.var.ExpPhase = 0;
ShowInstruction(0);
EyelinkDoTrackerSetup(el);
%Returns 'messageString' text associated with result of last calibration
[~, messageString] = Eyelink('CalMessage');
Eyelink('Message','%s',messageString);%
WaitSecs(0.05);
fprintf('=================\n=================\nNow we are done with the calibration\n')
end
function Log(ptb_time, event_type, event_info)
%Phases:
%Pre-Experiment : 1
%Post-Experiment : 2
% %Instruction : 1
% %Baseline : 2
% %Conditioning : 3
% %Test : 4
% %Rating : 5
% %Calibration : 0
%
%event types are as follows:
%
%Scan Detection : 0 info: NaN;
%Cross Onset : 1 info: position
%Stimulus Onset/Offset: 2/-2 info: stim_id
%Cross Movement : 3 info: NaN;
%Stimulus Offset : -2 info: NaN;
%UCS Delivery : 4 info: NaN;
%Key Presses : 7 info: NaN;
%Tracker Onset/Offset : 8 info: NaN;
%
%Text on the screen : 5 info: Which Text?
%RatingScreen Onset : 6 info: NaN;
p.var.event_count = p.var.event_count + 1;
p.out.log(p.var.event_count,:) = [ptb_time event_type event_info phase];
% % p_out_log(p.out.event_counter,:)
%logstring([ 'Logged: ' mat2str(p_out_log(p.out.event_counter,:)) ' - Type: ' p.verbose.eventtype{abs(event_type)} ' - Phase: ' p.verbose.eventphase{CurrentExperimentalPhase}])
%for i = 1:3;subplot(3,1,i);plot(p_out_log(1:p.out.event_counter ,i),'o-');drawnow;end
%
end
function cleanup
% Close window:
sca;
%set back the old resolution
if strcmp(p.hostname,'triostim1')
Screen('Resolution',p.ptb.screenNumber, p.ptb.oldres.width, p.ptb.oldres.height );
%show the cursor
ShowCursor(p.ptb.screenNumber);
end
%