-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticeDetection.m
More file actions
270 lines (218 loc) · 10.1 KB
/
practiceDetection.m
File metadata and controls
270 lines (218 loc) · 10.1 KB
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
function [PD,PA,V] = practiceDetection(subID,orientations)
%% practice detection (PD) script
% Opens window -> defines trials -> makes gabors -> adds noise ->
% runs through trials.
% =========================================================================
% Setup
% =========================================================================
% output
output = fullfile(cd,'results',subID);
if ~exist(output,'dir'); mkdir(output); end
saveName = sprintf('PD_%s.mat',subID);
[w, rect] = setWindow(0);
HideCursor;
% Trial number and P/A
nOri = length(orientations);
nTrials = 8; % per orientation
PA = [ones(nTrials/2,1); zeros(nTrials/2,1)];
PA = repmat(PA,1,2);
PA(:,1) = PA(randperm(nTrials),1); PA(:,2) = PA(randperm(nTrials),2);
PD = nan(nOri,nTrials,2);
% Visibility settings
threshold = 0.8; % acc to continue
vis_scale = [0 logspace(log10(0.005),log10(0.2),299)]; % steps in log space
visibility = 0.12; % Start visibility
[~,visibility] = min(abs(vis_scale-visibility)); % scale idx
% responses
trialResponse = 1;
trialRT = 2;
yesKey = 'h';
noKey = 'j';
% timings
fixTime = 0.2;
mITI = 2; % mean ITI - randomly sample from norm
sITI = 1; % SD for sampling
ITIs = normrnd(mITI,sITI,nOri,nTrials);
% =========================================================================
% Stimuli
% =========================================================================
% Makes the gabors to show for instruction
gaborPatch = cell(nOri,1);
gaborTexture = cell(nOri,1);
for iOri = 1:nOri
% stimulus
gaborPatch{iOri} = make_stimulus(orientations(iOri),0.3);
% texture
gaborTexture{iOri} = Screen('MakeTexture',w,gaborPatch{iOri});
end
% Noise stimulus info
displayDuration = 2; % Duration of the stimulus in seconds
hz = Screen('NominalFrameRate', w);
ifi = 1/hz; % Refresh rate
nStepSize = 2; % 2 frames per step
nSteps = (displayDuration/ifi)/nStepSize;
cues = {'A','B'};
%% Instructon screen
[xCenter, yCenter] = RectCenter(rect);
[x_pix, ~] = Screen('WindowSize', w);
% explain finger placement
text = ['During this experiment, you will have to use all your fingers'...
' except your thumbs to respond. \n \n Please place your left hand over the '...
'[a],[s],[d],[f] keys, \n with your left pinky (LP) on the [a] and ' ...
'left index (LI) on the [f]. \n \n Place your right hand over the [h],[j],[k],[l] '...
' \n with your right index (RI) on the [h] and your right pinky (RP) on the [l]. \n'...
'\n [Press any key to continue] \n '];
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', yCenter*0.65, [255 255 255]);
vbl=Screen('Flip', w);
WaitSecs(1);
KbWait;
% show instructions
text = ['In this experiment you will be shown gratings in noise (see below) \n ',...
'Your task is to indicate whether a grating is presented. \n ',...
'Your right index finger (RI) indicates "Yes" and your right middle finger (RM) indicates "No" \n ',...
'Please keep your eyes fixated on the fixation cross in the middle of the screen. \n ',...
'A grating will be present in 50% of the trials. \n ',...
'We will first practice this a few times. \n \n ',...
'[Press any key to continue] \n '];
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', yCenter*0.65, [255 255 255]);
% show gratings
[xCenter, yCenter] = RectCenter(rect);
Xpos = [x_pix*(1/3) x_pix*(2/3)];
baseRect = [0 0 size(gaborPatch{1},1) size(gaborPatch{1},1)];
allRects = nan(4, 3);
for i = 1:2
allRects(:, i) = CenterRectOnPointd(baseRect, Xpos(i), yCenter*1.4);
end
Screen('DrawTextures', w, gaborTexture{1}, [], allRects(:,1), [],[], 0.5);
DrawFormattedText(w, 'Grating A', xCenter*(1.75/3), yCenter*1.125, [255 255 255]);
Screen('DrawTextures', w, gaborTexture{2}, [], allRects(:,2), [],[], 0.5);
DrawFormattedText(w, 'Grating B', xCenter*(3.75/3), yCenter*1.125, [255 255 255]);
vbl=Screen('Flip', w);
WaitSecs(1)
KbWait;
%% Trials start
for iOri = 1:nOri
notYet = 1;
while notYet % until enough correct
WaitSecs(fixTime); % otherwise it doesn't work
% instruction screen with gratings
text = sprintf('During this block, you will be detecting Grating %s (see below). \n After each trial, please indicate whether this grating was presented or not. \n \n [Press any key to start] \n ',cues{iOri});
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', yCenter*0.75, [255 255 255]);
Screen('DrawTextures', w, gaborTexture{1}, [], allRects(:,1), [],[], 0.5);
DrawFormattedText(w, 'Grating A', xCenter*(1.75/3), yCenter*1.125, [255 255 255]);
Screen('DrawTextures', w, gaborTexture{2}, [], allRects(:,2), [],[], 0.5);
DrawFormattedText(w, 'Grating B', xCenter*(3.75/3), yCenter*1.125, [255 255 255]);
vbl=Screen('Flip', w);
KbWait;
for iTrial = 1:nTrials
% Fixation
Screen('DrawLines', w, [0 0 -10 10; -10 10 0 0],...
4, [0,0,0], [rect(3)/2, rect(4)/2], 1);
vbl=Screen('Flip', w);
WaitSecs(0.2);
if PA(iTrial,iOri) == 1 % present trial
% schedule of visibility gradient (i.e how visible at each frame)
% Increases till most visible at the end
schedule = vis_scale(round(linspace(1,visibility,nSteps)));
else % Pure noise trial
% 0 for entire schedule
schedule = zeros(1,nSteps);
end
% Make the texture for each frame by combining the gabor with noise.
% Rotates the annulus mask to hide the rotated boundary box around the
% grating.
target = {};
for i_frame = 1:nSteps
idx = ((i_frame-1)*nStepSize)+1:(i_frame*nStepSize);
tmp = Screen('MakeTexture',w, ...
make_stimulus(orientations(iOri),schedule(i_frame)));
for i = 1:length(idx); target{idx(i)} = tmp; end
end
% =========================================================================
% Presentation
% =========================================================================
% Present stimulus
tini = GetSecs;
for i_frame = 1:length(target)
while GetSecs-tini < ifi*i_frame
Screen('DrawTextures',w,target{i_frame});
Screen('DrawLines', w, [0 0 -10 10; -10 10 0 0],...
4, [0,0,0], [rect(3)/2, rect(4)/2], 1); % fixation
vbl=Screen('Flip', w);
end
end
% Decision
text = 'Was there a grating on the screen? \n Yes [RI] or no [RM]';
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', 'center', 255);
vbl = Screen('Flip', w);
% Log response
keyPressed = 0; % clear previous response
while ~keyPressed
[~, keyTime, keyCode] = KbCheck(-3);
key = KbName(keyCode);
if ~iscell(key) % only start a keypress if there is only one key being pressed
if any(strcmp(key, {yesKey,noKey}))
% fill in B
PD(iOri,iTrial,trialResponse) = strcmp(key,yesKey); % 1 yes 0 no
PD(iOri,iTrial,trialRT) = keyTime-vbl;
keyPressed = true;
elseif strcmp(key, 'ESCAPE')
Screen('TextSize',w, 28);
DrawFormattedText(w, 'Experiment was aborted!', 'center', 'center', [255 255 255]);
Screen('Flip',w);
WaitSecs(0.5);
ShowCursor;
disp(' ');
disp('Experiment aborted by user!');
disp(' ');
Screen('CloseAll');
save(fullfile(output,saveName)); % save everything
return;
end
end
end
% Inter trial interval
Screen('DrawLines', w, [0 0 -10 10; -10 10 0 0],...
4, [255,255,255], [rect(3)/2, rect(4)/2], 1);
Screen('Flip', w);
WaitSecs(ITIs(iOri,iTrial));
% Close all textures to free memory
tmp = unique(cell2mat(target));
for i_tex = 1:length(tmp)
Screen('Close', tmp(i_tex));
end
end
% check if the accuracy is high enough, otherwise go again
acc = mean(squeeze(PD(iOri,:,trialResponse))'==PA(:,iOri));
WaitSecs(0.2);
if acc < threshold % too low
visibility = visibility - round((acc-threshold)*100); % add visibility
if visibility > length(vis_scale); visibility = length(vis_scale); end % cap at end
text = sprintf('You were only correct on %d out of %d trials. \n So, let"s try this again! \n [Press any key to continue] \n ',sum(PD(iOri,:,trialResponse)'==PA(:,iOri)),nTrials);
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', 'center', 255);
vbl = Screen('Flip', w);
KbWait;
else % good
notYet = 0; % we can continue
text = sprintf('Well done! You were correct on %d out of %d trials. \n [Press any key to continue]',sum(PD(iOri,:,trialResponse)'==PA(:,iOri)),nTrials);
%text = 'poop';
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', 'center', 255);
vbl = Screen('Flip', w);
KbWait;
end
end
end
V = vis_scale(visibility); % use this for staircase and next practice
save(fullfile(output,saveName)); % save everything
Screen('TextSize',w, 28);
DrawFormattedText(w, 'This is the end of the first practice task!', 'center', 'center', [255 255 255]);
vbl = Screen('Flip', w);
WaitSecs(2);
Screen('CloseAll')
sca;