-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask03.m
More file actions
458 lines (379 loc) · 16.6 KB
/
task03.m
File metadata and controls
458 lines (379 loc) · 16.6 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
452
453
454
455
456
457
458
function task03( logpars, PTB, EThndl, LPT )
% Pavlovian
ver = '2.0';
fprintf('Pavlovian expression task version %s\n', ver);
fprintf('\n\n');
%% init experiment structure
EXP = {};
EXP.stimduration = 6;
EXP.shockduration = 0.200;
EXP.keyCodeYes = KbName('LeftArrow');
EXP.keyCodeNo = KbName('RightArrow');
EXP.stimsdir = fullfile('textures-03');
EXP.UPenndir = fullfile('UPennNID');
EXP.assetsdir = fullfile('assets');
EXP.baseImg = [0 0 800 600];
EXP.dotColor = [0 1; 0 1; 0 1];
EXP.dotSize = [20 14];
EXP.UPennDuration = 5;
EXP.UPennMarker = 94;
EXP.markerStartTask = 130;
EXP.markerEndTask = 136;
EXP.pauseOn = 132;
EXP.pauseOff = 134;
EXP.responsePrompt = 1; % seconds
EXP.promptMarker = 96;
%% get randomization
if str2double(logpars.subjid) == 1000
EXP.id = 1;
else
EXP.id = str2double(logpars.subjid);
EXP.id = EXP.id - 1000;
end
% get texture randomization
SI = load(fullfile(EXP.assetsdir, 'imgs_randomization.mat'));
EXP.imgs = SI.XX(EXP.id, :);
% get prompt randomization
S = load(fullfile(EXP.assetsdir, 'task_test_prompt.mat'));
EXP.XP = S.XX(EXP.id).XP;
S = load(fullfile(EXP.assetsdir, 'task_test_randomization.mat'));
% get design matrix
EXP.X = S.X(:,EXP.id);
% update design matrix to 2 columns
f_element = nan(size(EXP.X));
f_element(ismember(EXP.X, 2, 'rows')) = 0;
f_element(ismember(EXP.X, 4, 'rows')) = 0;
f_element(ismember(EXP.X, 6, 'rows')) = 0;
f_element(ismember(EXP.X, 10, 'rows')) = 0;
indx = find(EXP.X == 1 | EXP.X == 3 | EXP.X == 5);
f_element(indx) = EXP.X(indx);
s_element = zeros(size(EXP.X));
indx = find(EXP.X == 2 | EXP.X == 4 | EXP.X == 10);
s_element(indx) = EXP.X(indx);
% concatenate
EXP.XX = [f_element s_element];
% replace shock marker
EXP.XX(EXP.XX == 10)= 4;
% add condition 6
EXP.XX(EXP.XX(:,1) == 5,2) = 6;
% replace zeros with 7 in the second element
EXP.XX(EXP.XX(:,2) == 0, 2) = 7;
% itis
S = load(fullfile(EXP.assetsdir, 'task_test_iti.mat'));
EXP.iti = S.X(:,EXP.id);
% get UPenn images
S = load(fullfile(EXP.assetsdir, 'UPenn_task_test_randomization.mat'));
EXP.UPennImgs = S.X2(:, EXP.id);
%% draw textures and shapes
% prepare image size
if logpars.smallscreen == 1
EXP.baseImg = round(EXP.baseImg*.66);
elseif logpars.smallscreen == 2
EXP.baseImg = round(EXP.baseImg*.33);
end
% load images and rescale them
EXP.textures = cell(length(EXP.imgs), 1);
for i=1:length(EXP.imgs)
img = imread(fullfile(EXP.stimsdir, EXP.imgs{i}));
img = imresize(img, flip(EXP.baseImg(3:4)));
EXP.textures{i} = Screen('MakeTexture', PTB.window, img);
% Show init screen
text = sprintf('%s %d%%', 'Loading resources...', round(i/length(EXP.imgs)*100));
drawInstructions(PTB.window, text);
Screen('Flip', PTB.window);
end
clear img;
% load UPennNID images and rescale them
EXP.UPenntextures = cell(length(EXP.UPennImgs), 1);
fx = @(x) sprintf('1%03d.jpg', x);
EXP.UPennImgs = cellfun(fx, num2cell(EXP.UPennImgs), 'UniformOutput', false);
for i=1:length(EXP.UPenntextures)
img = imread(fullfile(EXP.UPenndir, EXP.UPennImgs{i}));
img = imresize(img, flip(EXP.baseImg(3:4)));
EXP.UPenntextures{i}= Screen('MakeTexture', PTB.window, img);
% Show init screen
text = sprintf('%s %d%%', 'Loading resources...', round(i/length(EXP.UPennImgs)*100));
drawInstructions(PTB.window, text);
Screen('Flip', PTB.window);
end
clear img;
%% init logfile
fprintf('\n\n');
fprintf('Initialising log files...\n')
logfile = fopen(logpars.logfilename,'w+');
fprintf(logfile, '%s', ...
strcat(...
'trial,', ...
'shock,', ...
'condition1,', ...
'condition2,', ...
'condition1_texture,', ...
'condition2_texture,', ...
'condition1_timestamp,', ...
'condition2_timestamp,', ...
'prompt1_timestamp,', ...
'prompt2_timestamp,', ...
'image1,', ...
'image2,', ...
'image3,', ...
'image4,', ...
'image5') ...
);
fprintf(logfile, '\n');
fclose(logfile);
%% init keyboard input
fprintf('\n\n');
fprintf('Initialising keyboard logger...\n')
% [keyboardIndices, productNames, allInfos] = GetKeyboardIndices;
kblog = Keylogger();
%% Welcome screen
fprintf('\n\n');
fprintf('Showing instructions...\n')
text = {
'Ihnen werden nacheinander Bilder gezeigt.\n'
'Dabei erhalten Sie möglicherweise leichte elektrische Schocks.\n'
'Es gibt verschiedene Arten von Bildern:\n'
'Szenen im Freien (z. B. eine Landschaft, Bäume, eine Savanne) und verschiedene Farben.\n'
'Bei Szenen im Freien können Sie sicher sein, dass Sie KEINEN Schock erhalten werden.\n'
'Zwischen den Szenen im Freien sehen Sie Farbbilder und Sie müssen raten,\n'
'ob Sie irgendwann vor der nächsten Szene im Freien einen Schock erhalten werden.\n'
'Immer wenn der Kreis in der Mitte zu einem Quadrat wird,\n'
'benutzen Sie die Tastatur um vorherzusagen,\n'
'ob irgendwann ein Schock vor der nächsten Szene im Freien kommen könnte.\n'
'Drücken Sie PFEIL LINKS, um anzugeben, dass gleich ein Schock kommen könnte.\n'
'Drücken Sie PFEIL RECHTS, um anzugeben, dass gleich KEIN Schock kommen könnte.\n\n'
' <- ->'
'\n\n'
'SCHOCK KEIN SCHOCK'
'\n\n'
'Ihre Antwort entscheidet nicht darüber, ob Sie einen Schock erhalten werden oder nicht.\n\n'
'Bevor die Aufgabe beginnt, müssen wir den Eyetracker kalibrieren.\n'
'Bitte halten Sie Ihren Kopf während und nach der Kalibrierung still.\n'
'Versuchen Sie, Ihren Blick nahe der Mitte des Bildschirms zu halten.\n\n'
'Legen Sie Ihre Hand auf die Pfeile (links und rechts)\n'
'und drücken Sie den Pfeil nach links, um die Kalibrierung zu beginnen...'
};
drawInstructions(PTB.window, strjoin(text));
Screen('Flip', PTB.window);
RestrictKeysForKbCheck(KbName('LeftArrow'));
KbStrokeWait;
Screen('Flip', PTB.window);
RestrictKeysForKbCheck([]);
%% run calibration
fprintf('\n\n');
fprintf('Running tracker calibration...\n')
calValInfo = EThndl.calibrate(PTB.window,true);
% save calibration data
save(logpars.calibfilename, getname(calValInfo));
text = {
'Die Aufgabe dauert etwa 15 Minuten.\n\n'
'Bitte halten Sie während der gesamten Aufgabe den Kopf still.\n'
'Versuchen Sie, Ihren Blick nahe der Mitte des Bildschirms zu halten.\n\n'
'Immer wenn der Kreis in der Mitte zu einem Quadrat wird,\n'
'benutzen Sie die Tastatur um vorherzusagen,\n'
'ob irgendwann ein Schock vor der nächsten Szene im Freien kommen könnte.\n\n'
' <- ->'
'\n\n'
'SCHOCK KEIN SCHOCK'
'\n\n'
'Drücken Sie den Pfeil nach links, um die Aufgabe zu beginnen...'
};
drawInstructions(PTB.window, strjoin(text));
Screen('Flip', PTB.window);
RestrictKeysForKbCheck(KbName('LeftArrow'));
KbStrokeWait;
Screen('Flip', PTB.window);
RestrictKeysForKbCheck([]);
%% start data collection
fprintf('\n\n');
fprintf('Starting data collection...\n')
if logpars.debugging
nTrials = 4;
else
nTrials = length(EXP.X);
end
% clear the screen
Screen('Flip', PTB.window);
% start recording
EThndl.startRecording();
EThndl.startBuffer();
WaitSecs(1);
EThndl.setBegazeTrialImage('interval.png');
LPT.sendMarker(EXP.markerStartTask);
% init buffer data
bufferdata = EThndl.consumeBufferData();
% reshape to rowise
bufferdata = reshapeBufferData(bufferdata);
% init eyetracker event data
eteventdata = {};
eteventdata.timestamp = [];
eteventdata.event = {};
%% trial loop
UPennCounter = 1;
for trial = 1:nTrials
% ITI before the first trial
if trial == 1
vbl = Screen('Flip', PTB.window);
% start recording keyboard responses
kblog.startRecording(vbl);
for i = 1:2
Screen('DrawTexture', PTB.window, EXP.UPenntextures{UPennCounter});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 2);
vbl = Screen('Flip', PTB.window, vbl + EXP.UPennDuration + PTB.ifi/2);
eteventdata = updateETeventData(eteventdata, EThndl, string(EXP.UPennMarker));
EThndl.setBegazeTrialImage(EXP.UPennImgs{UPennCounter});
LPT.sendMarker(EXP.UPennMarker);
UPennCounter = UPennCounter + 1;
end
vbl = vbl + EXP.UPennDuration + PTB.ifi/2;
end
shocking = EXP.X(trial)==10;
IMGS = repmat({'N/A'}, 5, 1);
t_info = {};
t_info{1} = string(trial);
t_info{2} = string(shocking);
t_info{3} = string(EXP.XX(trial, 1));
t_info{4} = string(EXP.XX(trial, 2));
t_info{5} = 'N/A';
t_info{6} = 'N/A';
t_info{7} = 'N/A';
t_info{8} = 'N/A';
t_info{9} = 'N/A';
t_info{10} = 'N/A';
if ismember(EXP.XX(trial, 1), [1 3 5])
t_info{5} = EXP.imgs{EXP.XX(trial, 1)};
t_info{6} = EXP.imgs{EXP.XX(trial, 2)};
% 1
Screen('DrawTexture', PTB.window, EXP.textures{EXP.XX(trial,1)});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 2);
vbl = Screen('Flip', PTB.window, vbl + PTB.ifi/2);
t_info{7} = string(vbl);
eteventdata = updateETeventData(eteventdata, EThndl, string(EXP.XX(trial, 1) * 10));
EThndl.setBegazeTrialImage(EXP.imgs{EXP.XX(trial, 1)});
LPT.sendMarker(EXP.XX(trial, 1) * 10);
% prompt
Screen('DrawTexture', PTB.window, EXP.textures{EXP.XX(trial,1)});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 0);
Screen('Flip', PTB.window, vbl + EXP.XP(trial, 1) + PTB.ifi/2);
EThndl.sendMessage('prompt');
eteventdata = updateETeventData(eteventdata, EThndl, string(EXP.promptMarker));
LPT.sendMarker(EXP.promptMarker);
Screen('DrawTexture', PTB.window, EXP.textures{EXP.XX(trial,1)});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 2);
Screen('Flip', PTB.window, vbl + EXP.XP(trial, 1) + EXP.responsePrompt + PTB.ifi/2);
t_info{9} = string(vbl + EXP.XP(trial, 1));
% 2
Screen('DrawTexture', PTB.window, EXP.textures{EXP.XX(trial, 2)});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 2);
vbl = Screen('Flip', PTB.window, vbl + EXP.stimduration + PTB.ifi/2);
t_info{8} = string(vbl);
eteventdata = updateETeventData(eteventdata, EThndl, string(EXP.XX(trial, 2)*10));
EThndl.setBegazeTrialImage(EXP.imgs{EXP.XX(trial, 2)});
LPT.sendMarker(EXP.XX(trial, 2) * 10);
% prompt
Screen('DrawTexture', PTB.window, EXP.textures{EXP.XX(trial,2)});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 0);
Screen('Flip', PTB.window, vbl + EXP.XP(trial, 2) + PTB.ifi/2);
EThndl.sendMessage('prompt');
eteventdata = updateETeventData(eteventdata, EThndl, string(EXP.promptMarker));
LPT.sendMarker(EXP.promptMarker);
Screen('DrawTexture', PTB.window, EXP.textures{EXP.XX(trial,2)});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 2);
Screen('Flip', PTB.window, vbl + EXP.XP(trial, 2) + EXP.responsePrompt + PTB.ifi/2);
t_info{10} = string(vbl + EXP.XP(trial, 2));
vbl = WaitSecs('UntilTime', vbl + EXP.stimduration + PTB.ifi/2);
% only the second associate present
elseif ismember(EXP.XX(trial, 2), [2 4])
t_info{6} = EXP.imgs{EXP.XX(trial, 2)};
% 2
Screen('DrawTexture', PTB.window, EXP.textures{EXP.XX(trial,2)});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 2);
vbl = Screen('Flip', PTB.window, vbl + PTB.ifi/2);
t_info{8} = string(vbl);
eteventdata = updateETeventData(eteventdata, EThndl, string(EXP.XX(trial, 2) * 10));
EThndl.setBegazeTrialImage(EXP.imgs{EXP.XX(trial, 2)});
LPT.sendMarker(EXP.XX(trial, 2) * 10);
% prompt
Screen('DrawTexture', PTB.window, EXP.textures{EXP.XX(trial,2)});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 0);
Screen('Flip', PTB.window, vbl + EXP.XP(trial, 2) + PTB.ifi/2);
EThndl.sendMessage('prompt');
eteventdata = updateETeventData(eteventdata, EThndl, string(EXP.promptMarker));
LPT.sendMarker(EXP.promptMarker);
Screen('DrawTexture', PTB.window, EXP.textures{EXP.XX(trial,2)});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 2);
Screen('Flip', PTB.window, vbl + EXP.XP(trial, 2) + EXP.responsePrompt + PTB.ifi/2);
t_info{10} = string(vbl + EXP.XP(trial, 2));
if shocking
vbl = WaitSecs('UntilTime', vbl + EXP.stimduration - EXP.shockduration - PTB.ifi/2);
LPT.sendShock;
else
vbl = WaitSecs('UntilTime', vbl + EXP.stimduration + PTB.ifi/2);
end
end
for i = 1:EXP.iti(trial)
Screen('DrawTexture', PTB.window, EXP.UPenntextures{UPennCounter});
Screen('DrawDots', PTB.window, [PTB.xCenter PTB.xCenter; PTB.yCenter PTB.yCenter], EXP.dotSize, EXP.dotColor, [], 2);
if i == 1
vbl = Screen('Flip', PTB.window, vbl + PTB.ifi/2);
else
vbl = Screen('Flip', PTB.window, vbl + EXP.UPennDuration + PTB.ifi/2);
end
eteventdata = updateETeventData(eteventdata, EThndl, string(EXP.UPennMarker));
EThndl.setBegazeTrialImage(EXP.UPennImgs{UPennCounter});
LPT.sendMarker(EXP.UPennMarker);
IMGS{i} = EXP.UPennImgs{UPennCounter};
UPennCounter = UPennCounter + 1;
end
bufferdata = catBufferData(bufferdata, EThndl);
% save buffer data
save(logpars.bufferfilename, getname(bufferdata));
% save event data
save(logpars.eteventfilename, getname(eteventdata));
% log responses
logfile = fopen(logpars.logfilename,'a+');
fprintf(logfile, '%s', ...
strcat(t_info{1}, ',', ...
t_info{2}, ',', ...
t_info{3}, ',', ...
t_info{4}, ',', ...
t_info{5}, ',', ...
t_info{6}, ',', ...
t_info{7}, ',', ...
t_info{8}, ',', ...
t_info{9}, ',', ...
t_info{10}, ',', ...
IMGS{1}, ',', ...
IMGS{2}, ',', ...
IMGS{3}, ',', ...
IMGS{4}, ',', ...
IMGS{5}));
fprintf(logfile, '\n');
fclose(logfile);
vbl = WaitSecs('UntilTime', vbl + EXP.UPennDuration + PTB.ifi/2);
end
% clear the screen
Screen('Flip', PTB.window);
text = {'Saving data...'};
drawInstructions(PTB.window, strjoin(text));
Screen('Flip', PTB.window);
%% save data
fprintf('\n\n');
fprintf('Saving data...\n');
keypress = kblog.dumpLog();
% save responses
filename = fullfile(logpars.subjdir, ...
sprintf('%s_responses.csv', logpars.taskname));
writetable(struct2table(keypress), filename);
bufferdata = catBufferData(bufferdata, EThndl);
% save buffer data
save(logpars.bufferfilename, getname(bufferdata));
% save event data
save(logpars.eteventfilename, getname(eteventdata));
EThndl.stopBuffer();
EThndl.stopRecording();
LPT.sendMarker(EXP.markerEndTask);
EThndl.saveData(fullfile(cd,logpars.idffilename), logpars.subjid, logpars.taskname, true);
WaitSecs(3);
Screen('Flip', PTB.window);
end