-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolt_practice_localizer.m
executable file
·298 lines (236 loc) · 9.34 KB
/
volt_practice_localizer.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
function data = volt_practice_localizer(header)
%% Setup
par = header.parameters;
expName = 'volt';
data = struct('subNr', header.subNr);
data.header = header;
data.stim = header.loc.practice;
data.onset = header.loc.practiceonset;
nTrials = size(data.onset,1);
data.resp = nan(nTrials,1);
data.rt = nan(nTrials,1);
data.baseIsCorrect = nan(nTrials,1);
% Set response options
oneKey = KbName('1!'); % button to output 1
twoKey = KbName('2@'); % button to output 2
threeKey = KbName('3#'); % button to output 3
fourKey = KbName('4$'); % button to output 4
posskeys = [oneKey twoKey threeKey fourKey];
% repKey = KbName(par.repKey);
% leftKey = KbName(par.leftKey);
% midKey = KbName(par.midKey);
% rightKey = KbName(par.rightKey);
%% Check for button box
intake=PsychHID('Devices');
extKeys=0;
for n=1:length(intake)
if (intake(n).productID == 8 && strcmp(intake(n).usageName,'Keyboard')) % UT
extKeys=n;
elseif strcmp(intake(n).usageName,'Keyboard')
intKeys=n;
end
end
if extKeys==0, disp('No Buttonbox Detected.'); end
% dev = extKeys; % change this line to intKeys for running in the lab
dev = intKeys; % change this line to extKeys for running in the scanner
%% Load base stim
% Uncommnent these if you need baseline (only use the baseline in the
% localizer if it's the same baseline as used in your task)
% baseStim = cell(1,3);
% baseNamepattern = strcat({header.path.localizer},{'baseline_%d.jpg'}); % change to scene for real thing
%
% for b = 1:3
%
% basestimfname = sprintf(baseNamepattern{1},b);
% [tmp, ~, ~] = imread(basestimfname);
% baseStim{1,b} = imresize(tmp,[NaN 500]);
%
% end
%% Create output text file
outfname = sprintf('%s/%s_practice_localizer_%d_%s',header.path.practicedata_subNr,expName,header.subNr,header.subInit);
if exist([outfname '.txt'],'file') == 2
error('The data file for this scan already exists. Please check your input parameters.');
end
formatString = '%d\t%d\t%d\t%d\t%d\t%d\t%0.3f\t%d\n';
fid=fopen([outfname '.txt'], 'w'); % open the file
fprintf(fid,'%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n',...
'blockNmbr','stimType','stimNmbr','isTarget','RespKey','Resp','RT','Accuracy'); %Create header
%% Open up the screen
oldEnableFlag = Screen('Preference','SuppressAllWarnings');
Screen('Preference', 'VisualDebugLevel', 1);
Screen('Preference', 'SkipSyncTests', 2); % will not show warning screen
[par.window, par.screenRect] = Screen(0, 'OpenWindow', par.backColor); % change depending on screen output (either 0 or 1)
[monitorFlipInterval] = Screen(par.window,'GetFlipInterval'); % gets the refresh time / monitor flip interval, to be used in calculating onsets below
par.xc = par.screenRect(3)/2;
par.yc = par.screenRect(4)/2;
par.xct = par.xc - par.txtSize/3; % center of screen for text
par.yct = par.yc - par.txtSize/3; % center of screen for text
Screen(par.window, 'TextSize', par.txtSize);
Screen(par.window, 'TextFont', 'Arial');
HideCursor;
% Display run number -- PRESS 5 MANUALLY TO GET FIXATION FOR REFERENCE LINES
repText = sprintf('Scene Repeat Task: Practice Run');
Screen('TextSize',par.window,par.txtSize);
[normBoundsRect_repText,offsetBoundsRects_repText] = Screen('TextBounds',par.window,repText);
xcreptext = par.xc - normBoundsRect_repText(3)/2;
Screen(par.window, 'DrawText', repText, xcreptext, par.yc, par.txtColor);
Screen(par.window, 'Flip');
clear keyCode;
clear keyIsDown;
pa.TRIGGERED = 0;
while ~pa.TRIGGERED
[keyIsDown, t, keyCode] = KbCheck(-1);
if strcmp(KbName(keyCode), '7&')
pa.TRIGGERED = 1;
end
end
while KbCheck; end
% Fixation for reference lines, trigger happens by scanner
Screen(par.window, 'TextSize', par.numSize);
Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
Screen(par.window, 'Flip');
clear keyCode;
clear keyIsDown;
clear t;
pa.TRIGGERED = 0;
while ~pa.TRIGGERED
[keyIsDown, t, keyCode] = KbCheck(-1);
if strcmp(KbName(keyCode), '5%')
pa.TRIGGERED = 1;
end
end
% to here
scannerStart = GetSecs;
startTime = scannerStart+par.bookendFixTime; %add par.initTime for scanner!;
data.beginTime = fix(clock);
% Flip fixation on screen
Screen(par.window, 'TextSize', par.numSize);
Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
Screen(par.window, 'Flip');
%% Loop through all trials
for t=1:nTrials
% get trial onset
ontime = data.onset(t,1);
% if it's a baseline trial
if data.stim(t,2)==7
% draw fixation
Screen(par.window, 'TextSize', par.numSize);
Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
% calculate presentation onset
stimtime = startTime + ontime;
% flip presentation of image to screen at correct onset
on = Screen(par.window,'Flip',stimtime);
data.onset(t) = on - startTime;
% draw fixation to buffer
Screen(par.window, 'TextSize', par.numSize);
Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
% calculate fixation onset
fixtime = stimtime + par.loc.stimTime;
% flip fixation to screen
Screen(par.window,'Flip',fixtime);
% set accuracy to a NaN because there is no response
acc(t) = NaN;
% save trial info to text file - shouldn't be anything to save
% during baseline; it's passive rest
fprintf(fid, formatString, data.stim(t,:), data.resp(t), data.rt(t), acc(t));
% show a scene block
else
type = data.stim(t,2);
switch type
case 1, prefix = 'volcano_';
case 2, prefix = 'volcano_';
case 3, prefix = 'volcano_';
case 4, prefix = 'volcano_';
case 5, prefix = 'volcano_';
case 6, prefix = 'volcano_';
case 7, prefix = 'baseline_';
end
fname = sprintf('%s%s%03d.jpg',header.path.localizer,prefix,data.stim(t,3));
tmp = imread(fname);
img = imresize(tmp,[400 NaN]);
% make texture for current trial presentation
stim = Screen(par.window,'MakeTexture',img);
% draw current trial image to buffer
Screen(par.window, 'DrawTexture',stim);
% to superimpose fixation, add these two lines
% Screen(par.window, 'TextSize', par.numSize);
% Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
% calculate presentation onset
stimtime = startTime + ontime;
% flip presentation of image to screen at correct onset
on = Screen(par.window,'Flip',stimtime);
data.onset(t) = on - startTime;
% No fixation + if not using fixation over stimuli
% Draw fixation to buffer
% Screen(par.window, 'TextSize', par.numSize);
% Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
% calculate fixation onset
fixtime = stimtime + par.loc.stimTime;
% get response and RTs
resp = [];
while GetSecs < fixtime
[keyIsDown, ~, keyCode] = KbCheck; %check for key response
if keyIsDown
if keyCode(oneKey)
resp = 1;
respstop = GetSecs;
break;
elseif keyCode(twoKey)
resp = 2;
respstop = GetSecs;
break;
end %end of keycode check
end %end of kbcheck
end %end of while
% flip fixation to screen
Screen(par.window,'Flip',fixtime);
%% save responses
% assign RT and fixation response if participant didn't respond in time
if isempty(resp)
resp = nan;
rt = nan;
else
rt = respstop - stimtime; % trial RT
end
% data.resp(t) = str2double(resp(1));
data.resp(t) = resp(1);
data.rt(t) = rt(1);
% calculate accuracy
if resp == header.loc.practice(t,5) %correct response
acc = 1;
else
acc = 0;
end
acc(t) = acc;
% save trial info to text file
fprintf(fid, formatString, data.stim(t,:), data.resp(t), data.rt(t), acc(t));
end
end
clear t
% while (GetSecs-startTime) <= par.rep_supp.nTotalTrials*par.rep_supp.trialTime
% end
WaitSecs(par.bookendFixTime)
% Put relevant info into data structure
data.duration = GetSecs - startTime; % includes end bookend fix time but not beginning
data.endTime = fix(clock);
clear startTime;
%% End experiment
ShowCursor;
Screen('CloseAll');
Screen('Preference','SuppressAllWarnings',oldEnableFlag);
clear screen;
data.parameters = par;
save(outfname,'data');
% % save scan data to file
% data.nTarget = sum(data.stim(:,4)==1);
% data.nMissedTarget = sum(data.stim(:,4)==1 & data.resp~=1);
% data.pctBaseNan = sum(isnan(data.resp(isnan(data.stim(:,4)))))/sum(isnan(data.stim(:,4)));
% data.baseacc = nanmean(data.baseIsCorrect(data.stim(:,2)==7));
disp('---------------');
disp('Practice scene repeat task completed!');
disp('---------------');
disp(sprintf('# of Repeats: %d',sum(data.stim(:,4)==1)));
disp(sprintf('# of Missed Repeats: %d',sum(data.stim(:,4)==1 & data.resp~=1)));
% disp(sprintf('Base Accuracy: %0.2f', nanmean(data.baseIsCorrect(data.stim(:,2)==7))));
disp('---------------');
disp(' ');