-
Notifications
You must be signed in to change notification settings - Fork 1
/
expt_trial.m
459 lines (342 loc) · 17.8 KB
/
expt_trial.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
classdef expt_trial < handle
%
%
%
properties
% Experimentor Defined Variables
save_filename = '';
date = '';
start_time = '';
expt_id = 'sz_sequential_simultaneous_attn';
subj_id = 'Z99';
trial_order_num = NaN;
trial_type = 'sequential';
catch_type = 'no_catch';
ITI = NaN;
char_stim = {};
font_rgb_color = NaN
response01_key = 'noResponse';
response01_time = NaN;
response01_acc = NaN;
response02_key = 'noResponse';
response02_time = NaN;
response02_acc = NaN;
luminance_contrast = NaN;
end % properties
properties (Hidden = true)
save_filedir = './data';
% Removed letters: 'O','I'
letter_array = upper(...
{ 'A','B','C','D','E','F','G','H','J','K'...
, 'L','M','N','P','Q','R','S','T','U','V'...
, 'W','X','Y','Z'});
number_array = {'1','2','3','4','5','6','7','8','9'};
ITI_range = [.500 .750];
end
methods
function obj = expt_trial(varargin)
switch nargin
case 0
case 6
obj.trial_type = varargin{1};
obj.catch_type = varargin{2};
obj.expt_id = varargin{3};
obj.subj_id = varargin{4};
obj.date = varargin{5};
obj.start_time = varargin{6};
otherwise
error('Wrong number of input arguments');
end
obj.save_filename = fullfile(...
obj.save_filedir, ...
[obj.expt_id '-' obj.subj_id '.csv']);
%% Randomly calculate inter-trial interval
% based min & max ITI-range
obj.ITI = min(obj.ITI_range) ...
+ (rand * (max(obj.ITI_range)-min(obj.ITI_range)));
%% Set up stimulus objects
% Choose character stimuli
obj.char_stim = [...
randsample(obj.letter_array, 2), ...
randsample(obj.number_array, 2)];
if strcmpi(obj.trial_type, 'simultaneous')
obj.char_stim = Shuffle(obj.char_stim);
else
obj.char_stim = [ ...
Shuffle({obj.char_stim{1} obj.char_stim{3}}) ...
Shuffle({obj.char_stim{2} obj.char_stim{4}}) ];
end
end % constructor method
function run(obj, trialNum, winPtr, background, fixation, luminanceContrast)
% -------------------------
% Execute Single Trial
% -------------------------
%
% ITI_processing_time = (GetSecs-ITI_start_time);
% leftover_ITI_dur = ITI_dur -ITI_processing_time;
% WaitSecs(leftover_ITI_dur);
% fprintf('Nominal ITI Duration:\t%-8.4f\tms\n' , ITI_dur *1000);
% fprintf('Actual ITI Duration:\t%-8.4f\tms\n' , (GetSecs-ITI_start_time)*1000);
fprintf('=================================================\n');
fprintf('Current trial Num:\t\t%03d\n' , trialNum);
fprintf('Block type: \t\t\t%s\n', obj.trial_type);
fprintf('Catch type: \t\t\t%s\n', obj.catch_type);
fprintf('Luminance contrast: \t\t%3.3f\n', luminanceContrast(1));
fprintf('\n');
obj.trial_order_num = trialNum;
obj.luminance_contrast = luminanceContrast(1);
obj.font_rgb_color = abs(background.color_rgb - luminanceContrast);
keyboard = seKeyboard();
%% Calculate character locations
X = 1; Y = 2;
[winSizeX, winSizeY] = Screen('WindowSize', winPtr);
screenCenter_pt = [winSizeX winSizeY]/2;
charRect = Screen('TextBounds', winPtr, 'A');
charCenterOffset = 200;
charHeightOffset = RectHeight(charRect)/2;
charWidthOffset = RectWidth(charRect)/2;
sy_center = screenCenter_pt(Y) - charHeightOffset;
sx_center = screenCenter_pt(X) - charWidthOffset;
sy_bottomChar = sy_center + charCenterOffset;
sy_topChar = sy_center - charCenterOffset;
sx_rightChar = sx_center + charCenterOffset;
sx_leftChar = sx_center - charCenterOffset;
%% Present: Pre-trial frame
background.draw(winPtr);
Screen('Flip', winPtr);
WaitSecs(0.500);
background.draw(winPtr);
fixation.draw(winPtr);
Screen('Flip', winPtr);
WaitSecs(0.500);
% fprintf('Pre-trial fixation dur: %1.4f\t\t ms\n' , (GetSecs-start)/1000);
%% Present character stim
% ifi = Screen('GetFlipInterval', winPtr);
% trueStimOnsetTimestamp = Sc
%
background.draw(winPtr);
fixation.draw(winPtr);
switch obj.trial_type
case 'simultaneous'
background.draw(winPtr);
fixation.draw(winPtr);
% TOP character
DrawFormattedText(winPtr, obj.char_stim{1} ...
, sx_center ...
, sy_topChar ...
, obj.font_rgb_color,5,0,0,2);
% BOTTOM character
DrawFormattedText(winPtr, obj.char_stim{2} ...
, sx_center ...
, sy_bottomChar ...
, obj.font_rgb_color,5,0,0,2);
% RIGHT character
DrawFormattedText(winPtr, obj.char_stim{3} ...
, sx_rightChar, sy_center ...
, obj.font_rgb_color,5,0,0,2);
% LEFT character
DrawFormattedText(winPtr, obj.char_stim{4} ...
, sx_leftChar, sy_center ...
, obj.font_rgb_color,5,0,0,2);
Screen('Flip', winPtr);
WaitSecs(.100);
case 'sequential'
% Draw 1st Character Stim Pair
background.draw(winPtr);
fixation.draw(winPtr);
% TOP character
DrawFormattedText(winPtr, obj.char_stim{1} ...
, sx_center ...
, sy_topChar ...
, obj.font_rgb_color,5,0,0,2);
% BOTTOM character
DrawFormattedText(winPtr, obj.char_stim{2} ...
, sx_center ...
, sy_bottomChar ...
, obj.font_rgb_color,5,0,0,2);
Screen('Flip', winPtr);
WaitSecs(0.050);
% Draw Blank ISI
background.draw(winPtr);
fixation.draw(winPtr);
Screen('Flip', winPtr);
WaitSecs(0.800);
% Draw 2nd Character Stim Pair
background.draw(winPtr);
fixation.draw(winPtr);
% RIGHT character
DrawFormattedText(winPtr, obj.char_stim{3} ...
, sx_rightChar, sy_center ...
, obj.font_rgb_color,5,0,0,2);
% LEFT character
DrawFormattedText(winPtr, obj.char_stim{4} ...
, sx_leftChar, sy_center ...
, obj.font_rgb_color,5,0,0,2);
Screen('Flip', winPtr);
WaitSecs(0.050);
otherwise
error('trial type');
% do nothing
end % switch
% ----------------------------------
% Subj Response
% ----------------------------------
% start = GetSecs;
background.draw(winPtr);
DrawFormattedText(winPtr, '?', 'center', 'center', seColor2RGB('white'), 5,0,0,2);
Screen('Flip', winPtr);
responseNum = 1;
subjectResponse_01 = keyboard.waitForResponse();
obj.saveResponse(subjectResponse_01, responseNum); % save the response to the expt class structure
WaitSecs(0.300);
background.draw(winPtr);
DrawFormattedText(winPtr, '??', 'center', 'center', seColor2RGB('white'), 5,0,0,2);
Screen('Flip', winPtr);
responseNum = 2;
subjectResponse_02 = keyboard.waitForResponse();
obj.saveResponse(subjectResponse_02, responseNum); % save the response to the expt class structure
% ------------------------------
% Present: Post-response frame
% ------------------------------
% ITI_start_time = GetSecs;
background.draw(winPtr);
Screen('Flip', winPtr); % flip/draw buffer to display monitor
WaitSecs(obj.ITI);
% -------------------------
% Post-Trial Processing
% -------------------------
% Save trial information to file
% Save header information to file before first trial
if(obj.trial_order_num == 1); obj.save_to_file(true); end;
% Save trial information to file
obj.save_to_file();
% curr_mean_accuracy = nanmean([ obj.block.trials.accuracy ]) * 100; % calculate current mean accuracy
% curr_mean_RT = nanmean([ obj.block.trials.RT ]) * 1000; % calculate current mean response time
% ITI_dur = obj.block.trials(currTrialNum).ITI; % figure out the ITI duration at the end of the trial
%
fprintf('Accuracy - 1st Response: \t%-3f %% - %s\n' ...
, obj.response01_acc*100 ...
, obj.response01_key);
fprintf('Accuracy - 2nd Response: \t%-3f %% - %s\n' ...
, obj.response02_acc*100 ...
, obj.response02_key);
% fprintf('Trial Response Time:\t%-8.4f\tms\n' , obj.block.trials(currTrialNum).RT * 1000);
% fprintf('\n');
% fprintf('Mean Accuracy: \t%-8.4f\t%%\n' , curr_mean_accuracy );
% fprintf('Mean Response Time: \t%-8.4f\tms\n' , curr_mean_RT );
% fprintf('\n');
% fprintf('Target Display SOA:\t %1.4f\t ms\n' , targDisplaySOA * 1000);
% fprintf('Target Display Duration: %1.4f\t ms\n\n' , targetDisplayDur * 1000);
fprintf('-------------------------------------------------\n');
end % function
function testRun(obj)
try
bgColorWd = 'black';
clc; HideCursor;
[ winPtr, ~, screenCenter_pt ] = seSetupScreen(seColor2RGB(bgColorWd)); % setup screen
background = stimBackground(bgColorWd); % setup background stim
fixation = stimFixationPt(screenCenter_pt); % setup fixation stim
% Font
fontName = 'Monaco'; % font name for instructions
fontSize = 50; % font size for all instructions
Screen('TextFont', winPtr, fontName); % setup text font
Screen('TextSize', winPtr, fontSize); % setup text size
trialNum = 1;
luminanceContrast = 50;
obj.run(trialNum, winPtr, background, fixation, luminanceContrast);
ShowCursor;
Screen('CloseAll'); % close psychtoolbox screen
catch err
ShowCursor;
Screen('CloseAll'); % close psychtoolbox screen
rethrow(err);
end
end % function
function trialObj = saveResponse(trialObj, subjectResponse, responseNum)
% Save response info into trial object
responseNum = num2str(responseNum);
eval(['trialObj.response0' responseNum '_key = upper(subjectResponse.keycode{1});']);
eval(['trialObj.response0' responseNum '_time = subjectResponse.responseTime;']);
% Calculate accuracy
if( ismember( ...
subjectResponse.keycode, ...
lower(trialObj.char_stim)))
eval(['trialObj.response0' responseNum '_acc = true;']);
else
eval(['trialObj.response0' responseNum '_acc = false;']);
end
end % method
function save_to_file(obj, printHeader, separator, decimal)
% Writes cell array content into a *.csv file.
%
% CELL2CSV(obj.save_filename, cellArray, separator, excelYear, decimal)
%
% obj.save_filename = Name of the file to save. [ i.e. 'text.csv' ]
% cellArray = Name of the Cell Array where the data is in
% separator = sign separating the values (default = ';')
% decimal = defines the decimal separator (default = '.')
%
% by Sylvain Fiedler, KA, 2004
% updated by Sylvain Fiedler, Metz, 06
% fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler
% added the choice of decimal separator, 11/2010, S.Fiedler
% Check for optional Variables
if ~exist('separator', 'var')
separator = ',';
end
if ~exist('printHeader', 'var')
printHeader = false;
end
if ~exist('decimal', 'var')
decimal = '.';
end
% Write file
outputFileID = fopen(obj.save_filename, 'a+'); % open/create file; append data
% Print all variables in trial data object
var_names = transpose(fieldnames(obj));
for var_name = var_names
if (printHeader)
% Print variable names
print_value = var_name{1};
else
% Print variable values
print_value = eval(['obj.' var_name{1}]);
% If zero, then empty cell
if size(print_value, 1) == 0
print_value = '';
end
% If numeric -> String
if isnumeric(print_value)
print_value = num2str(print_value);
% Conversion of decimal separator (4 Europe & South America)
% http://commons.wikimedia.org/wiki/File:DecimalSeparator.svg
if decimal ~= '.'
print_value = strrep(print_value, '.', decimal);
end
end
% If logical -> 'true' or 'false'
if islogical(print_value)
if print_value == 1
print_value = 'TRUE';
else
print_value = 'FALSE';
end
end
% If cell array -> String
if iscell(print_value)
print_value = cell2mat(print_value);
end
end
% OUTPUT value
fprintf(outputFileID, '%s', print_value);
% OUTPUT separator
% if s ~= size(cellArray, 2)
fprintf(outputFileID, separator);
% end
end
fprintf(outputFileID, '\n'); % print new line at end of every line
% Closing file
fclose(outputFileID);
end
end % methods
end % classdef