-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticeMainTask.m
More file actions
451 lines (367 loc) · 17.2 KB
/
practiceMainTask.m
File metadata and controls
451 lines (367 loc) · 17.2 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
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
%% practice the main task
% contains all components and set at a relatively high visibility level,
% determined by the detection practice to lead to > 0.7 accuracy
function [R,C] = practiceMainTask(subID,orientations,vivDet,V)
% =========================================================================
% Setup
% =========================================================================
[w, rect] = setWindow(0);
HideCursor;
% output
output = fullfile(cd,'results',subID);
if ~exist(output,'dir'); mkdir(output); end
saveName = sprintf('PMT_%s.mat',subID);
% Trial numbers and order
nOri = length(orientations);
nRep = 1; % how often to repeat unique block combinations
blocks = blockStructure(nOri,nOri,nRep);
nBlocks = size(blocks,1);
nTrials = 10; % per block
trials = trialStructure(blocks,nTrials);
R = nan(nBlocks,nTrials,4);
C = nan(nBlocks,1);
% Visibility settings
vis_scale = [0 logspace(log10(0.005),log10(0.2),299)]; % steps in log space
visibility = V;
% responses
vivRating = 1;
vivRT = 2;
detResponse = 3;
detRT = 4;
responseMappings = repmat(1:2,1,nBlocks/2);
responseMappings = responseMappings(randperm(nBlocks));
yesKey = {'h','f'};
noKey = {'j','d'};
vividnessKeys = {'a','s','d','f';'l','k','j','h'};
checkKeys = {'k','l';'a','s'};
% timing
mITI = 2; % mean ITI - randomly sample from norm
sITI = 1; % SD for sampling
ITIs = normrnd(mITI,sITI,nBlocks,nTrials);
fixTime = 0.2;
% =========================================================================
% 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),1); % full visibility
% 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);
% show instructions
text = ['Now for the main task, we will combine these two things together. \n ',...
'During each block, you will imagine a grating of a specific orientation \n ',...
'and indicate your vividness, like you did just now. \n ',...
'At the same time, you will be detecting a grating of a certain orientation, \n ',...
'either the same as the one you are imagining, or the other one.\n \n ',...
'We will also switch each block which hand you should use for each response type! \n ',...
'Your index finger will always indicate yes and high vividness. \n \n '];
if vivDet == 1 % first vividness
text = [text 'After each trial, you will first indicate how vivid your imagery was \n and then whether the to-be-detected grating was presented. \n \n'];
elseif vivDet == 2 % first detection
text = [text 'After each trial, you will first indicate whether the to-be-detected grating was presented \n and then how vivid your imagery was. \n \n'];
end
text = [text 'Let"s practice this a few times. \n \n ',...
'[Press any key to continue] \n '];
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', yCenter*0.75, [255 255 255]);
% show gratings info
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
vbl=Screen('Flip', w);
KbWait;
%% Trials start
for iBlock = 1:length(blocks)
WaitSecs(fixTime);
% instruction screen with gratings
text = sprintf('During this block, you will be imagining Grating %s and detecting Grating %s. \n \n',cues{blocks(iBlock,2)},cues{blocks(iBlock,1)});
if responseMappings(iBlock) == 1
text = [text 'You will use your left hand to indicate vividness \n and your right to indicate whether a stimulus was presented.'];
RM = 1;
elseif responseMappings(iBlock) == 2
text = [text 'You will use your right hand to indicate vividness \n and your left to indicate whether a stimulus was presented.'];
RM = 2;
end
text = [text '\n \n [Press any key to start] \n '];
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.8/3), yCenter*1.2, [255 255 255]);
Screen('DrawTextures', w, gaborTexture{2}, [], allRects(:,2), [],[], 0.5);
DrawFormattedText(w, 'Grating B', xCenter*(3.8/3), yCenter*1.2, [255 255 255]);
vbl=Screen('Flip', w);
KbWait;
% loop over trials
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(fixTime);
if trials(iBlock,iTrial) == 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(blocks(iBlock,1)),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
if vivDet == 1
% Vividness rating first
text = 'How vivid was your imagery? \n';
if RM == 2 % right hand
text = [text '\n 4[RI] - 1[RL]'];
else
text = [text '\n 1[LL] - 4[LI]'];
end
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', 'center', 255);
vbl = Screen('Flip', w);
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, vividnessKeys(RM,:)))
% fill in B
R(iBlock,iTrial,vivRating) = find(strcmp(key,vividnessKeys(RM,:))); % 1 to 4
R(iBlock,iTrial,vivRT) = keyTime-vbl; % RT
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
% Detection
text = 'Was there a grating on the screen? \n';
if RM == 1 % right hand
text = [text 'Yes [RI] or no [RM]'];
else
text = [text 'No [LM] or yes [LI]'];
end
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{RM},noKey{RM}}))
% fill in B
R(iBlock,iTrial,detResponse) = strcmp(key,yesKey{RM}); % 1 yes 0 no
R(iBlock,iTrial,detRT) = 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
elseif vivDet == 2
% Detection first
text = 'Was there a grating on the screen? \n';
if RM == 1 % right hand
text = [text 'Yes [RI] or no [RM]'];
else
text = [text 'No [LM] or yes [LI]'];
end
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{RM},noKey{RM}}))
% fill in B
R(iBlock,iTrial,detResponse) = strcmp(key,yesKey{RM}); % 1 yes 0 no
R(iBlock,iTrial,detRT) = 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
% Vividness rating
text = 'How vivid was your imagery? \n';
if RM == 2 % right hand
text = [text '\n 4[RI] - 1[RL]'];
else
text = [text '\n 1[LL] - 4[LI]'];
end
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', 'center', 255);
vbl = Screen('Flip', w);
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, vividnessKeys(RM,:)))
% fill in B
R(iBlock,iTrial,vivRating) = find(strcmp(key,vividnessKeys(RM,:))); % 1 to 4
R(iBlock,iTrial,vivRT) = keyTime-vbl; % RT
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
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(iBlock,iTrial));
% Close all textures to free memory
tmp = unique(cell2mat(target));
for i_tex = 1:length(tmp)
Screen('Close', tmp(i_tex));
end
end
% Imagery check
text = 'CHECK! Which grating did you imagine this block? \n';
if RM == 1 % Right hand
text = [text 'Grating A [RR] or Grating B [RL]'];
else
text = [text 'Grating A [LL] or Grating B [LR]'];
end
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', yCenter, [255 255 255]);
Screen('DrawTextures', w, gaborTexture{1}, [], allRects(:,1), [],[], 0.5);
DrawFormattedText(w, 'Grating A', xCenter*(1.8/3), yCenter*1.2, [255 255 255]);
Screen('DrawTextures', w, gaborTexture{2}, [], allRects(:,2), [],[], 0.5);
DrawFormattedText(w, 'Grating B', xCenter*(3.8/3), yCenter*1.2, [255 255 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, checkKeys(RM,:)))
% fill in response
checkResponse = find(strcmp(key,checkKeys(RM,:))); % 1 to 2
if checkResponse == blocks(iBlock,2) % correct
C(iBlock) = 1;
else
C(iBlock) = 0;
end
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
% Feedback
WaitSecs(0.2);
if C(iBlock) == 1
text = 'Correct! \n \n [Press any key to continue]';
elseif C(iBlock) == 0
text = 'That is incorrect, please read the block instructions carefully! \n \n [Press any key to continue]';
end
Screen('TextSize',w, 28);
DrawFormattedText(w, text, 'center', 'center', 255);
vbl = Screen('Flip', w);
KbWait;
end
save(fullfile(output,saveName)); % save everything
Screen('TextSize',w, 28);
DrawFormattedText(w, 'This is the end of the final practice task!', 'center', 'center', [255 255 255]);
vbl = Screen('Flip', w);
WaitSecs(2);
Screen('CloseAll')
sca;