-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_learning_800_Day2_4.m
313 lines (273 loc) · 13.9 KB
/
object_learning_800_Day2_4.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
% Day2-4 20190712 For full group
% note1:
% each staircase had 50 trials with each object in the set presented twice
% 800 trials in total
% here we will use 4 reversals
%% uncomment these parameters and comment 'function'-line if you want to run it like a script
clear all
subjID = 108;
condition = 2; % determine stimulus folder
This_day = 4;% 2,3,4
acq = 4;%%1,2,3,4
warning off;
% set general parameters of experiment
Dir.s = 'C:\code_learning_stimulus_presentation\code_CYC\6sets_20190710\';
Dir.m = 'C:\code_learning_stimulus_presentation\code_CYC\';
n_mask = 20; % number of mask; assumption that there are also that many masks
n_stimuli=5;
n_staircasePerBlock= 100;%100;%100
fmt = 'tif';
% pixRect = [0 0 450 450]; %% this is going to be the desired rectangle -- images will be scaled appropriately
%background_color=[144 144 144]; %% not > 110 to avoid colored background
background_color=[203 203 203]; %% not > 110 to avoid colored background
itemDur_sec = 0.12;
maskDur_sec = 0.25;
feedbackDur_sec = 1.00;
test_sec = 0.01;
n_t_stimuli = n_stimuli*2;
numBlocks = 1 ; %number of stimuli set
% set number and codition here
num_block=2;
% put the complete startup-sequence in a try-catch block to prevent
% freezing of the computer while the Screen function is on
% try
% This script calls Psychtoolbox commands available only in OpenGL-based
% versions of the Psychtoolbox. The Psychtoolbox command AssertPsychOpenGL
% will issue an error message if someone tries to execute this script on a
% computer without an OpenGL Psychtoolbox
AssertOpenGL;
%determine stimulus folder
if condition==1
thedir = char([Dir.s 'stimuli_5s_cond2'], ...
[Dir.m 'ruisstimuli_corrected']);
elseif condition==2
thedir = char([Dir.s 'stimuli_5s_cond5'], ...
[Dir.m 'ruisstimuli_corrected']);
end
[numConds junk] = size(thedir);
%% make sure that the output file does not exist yet
fid = fopen([num2str(subjID) '_object_800_Day' num2str(This_day) '_' num2str(acq) '.mat']);
if fid~=-1
disp('!!!!! OUTPUT FILE EXISTS ALREADY !!!!!')
disp(' Program stopped to avoid deletion of these previous data')
return;
end;
% Decide which screen is used to display the stimuli
screens=Screen('Screens');
screenNumber=max(screens);
% Open screen
window=Screen('OpenWindow', screenNumber,background_color(1));
% window=Screen('OpenWindow', 1,background_color(1));
% properties of window
[screenWidth screenHeight]=WindowSize(window);
frameRate=Screen('NominalFrameRate',window);
screenCenter= [screenWidth/2 screenHeight/2];
black=BlackIndex(window);
% dit gaat niet mogelijk zijn bij het programmeren, wel opnieuw uit
% commentaar zetten bij eigenlijke testen
if frameRate ~= 100
disp('!!!!! FRAMERATE SHOULD BE 100 !!!!!')
disp(' Program stopped ')
Screen('CloseAll')
return;
end;
% perform extra calibration pass to estimate monitor refresh interval
[ifi , ~, stddev] = Screen('GetFlipInterval', window, 100, 0.005, 20);
% define textsize for onScreen messages
Screen(window,'TextSize',25);
% Use realtime priority for better timing precision
priorityLevel=MaxPriority(window);
Priority(priorityLevel);
% load the images
% con1=stimuli 5
% con2= mask 20
for cond = 1:numConds
cd(deblank(thedir(cond,:)));
d = dir;
[numItems junk] = size(d);
[itemlist{1:numItems}] = deal(d.name);
% filename
for theitem = 1:(numItems-2)
filename{((cond-1)*(numItems-2))+theitem} = itemlist{2+theitem}; % 2+ to get rid of . and ..
imgArray = imread(filename{((cond-1)*(numItems-2))+theitem}, fmt);
imgArray = double(imgArray);
% load the images into textures
tex(((cond-1)*(numItems-2))+theitem)=Screen('MakeTexture', window, imgArray(:,:,1));
% masks
if cond==1
else
filename{n_t_stimuli+theitem} = itemlist{2+theitem}; % 2+ to get rid of . and ..
imgArray = imread(filename{n_t_stimuli+theitem}, fmt);
imgArray = double(imgArray);
tex(n_t_stimuli+theitem)=Screen('MakeTexture', window, imgArray(:,:,1));
end
%for the objects we will read in their filename, and use that to
%determine the correct response
if cond ==1
correctResponse{(cond-1)*(numItems-2)+theitem} = filename{((cond-1)*(numItems-2))+theitem}(1:13);
end
end
end
cd(Dir.s);
%% decide the order in which stimuli will appear in each block
theorder = zeros(n_staircasePerBlock, num_block);
for block = 1:num_block
X=[];
for ii=1:n_staircasePerBlock/n_stimuli
r=randperm(5);
X=[X,r];
end
theorder(:, block) = X';
end
theorderMasks = zeros(n_staircasePerBlock, num_block,3);
for i = 1:num_block
thelist1 = randi(n_mask,1,n_staircasePerBlock)';
theorderMasks(:,i, 1) = n_t_stimuli+thelist1;
thelist2 = randi(n_mask,1,n_staircasePerBlock)';
theorderMasks(:,i, 2) = n_t_stimuli+thelist2;
thelist3 = randi(n_mask,1,n_staircasePerBlock)';
theorderMasks(:,i, 3) = n_t_stimuli +thelist3;
end
key = zeros(num_block, n_staircasePerBlock); % to store performance
opeenvolgend_juist_1=0;
opeenvolgend_juist_2=0;
itemDur1 = round(itemDur_sec * frameRate);
itemDur2 = round(itemDur_sec * frameRate);
maskDur=round(maskDur_sec * frameRate);
% maskDur=round(maskDur_sec * frameRate)/3 ;%because three masks shown
% preStimDur=round(preStimDur_sec * frameRate);
feedbackDur=round(feedbackDur_sec * frameRate);
testDur = round(test_sec * frameRate);
% hide mouse and lock keyboard to ruin
HideCursor;
ListenChar(2);
% Show task instructions until key is pressed
Screen('DrawText', window, 'Benoem de figuur', [screenCenter(1)-170], [screenCenter(2)], black);
Screen('Flip', window);
KbWait;
vbl=Screen('Flip', window); % initial sync is needed to sync us to the VBL
%start experiment
timing.experimentStart = GetSecs;
for theBlock = 1:num_block
% day5_con = condition(theBlock);
timing.block(theBlock).start = GetSecs;
itemDur1 = round(itemDur_sec * frameRate);
itemDur2 = round(itemDur_sec * frameRate);
for i = 1: n_staircasePerBlock % itemsPerBlock
while KbCheck, end
responded = 0;
responseChar=zeros(1,2);
thepic = theorder(i, theBlock);
themask1 = theorderMasks(i, theBlock,1);
themask2 = theorderMasks(i, theBlock,2);
themask3 = theorderMasks(i, theBlock,3);
%% determine the screen position of the next stimulus + mask
raposy = round(50 - 100*rand);
raposx = round(50 - 100*rand);
% pixRect need to be random
Ima_Loc(theBlock).x(i).pixel =raposx ;
Ima_Loc(theBlock).y(i).pixel =raposy ;
imagesize = round(450 - 200*rand);
Ima_Size(theBlock).imagesize(i).pixel =imagesize;
pixRect = [0 0 imagesize imagesize];
%% pre-stimulus period ; i-1 and i is for feedback
[vbl timing.block(theBlock).stimulus(i).blank]=Screen('Flip', window, [vbl+(feedbackDur-0.5)*ifi]); %blank screen
% enlarge a circle
% Screen('FillOval', window, black, [screenCenter(1)-4 screenCenter(2)-4 screenCenter(1)+4 screenCenter(2)+4]);
Screen('FillOval', window, black, [screenCenter(1)-8 screenCenter(2)-8 screenCenter(1)+8 screenCenter(2)+8]);
% this time is for blank
[vbl timing.block(theBlock).stimulus(i).base]=Screen('Flip', window, [vbl+(testDur-0.5)*ifi]);
% Show the image
Screen('DrawTexture', window, tex(thepic), [], [screenCenter(1)-(pixRect(3)/2) screenCenter(2)-(pixRect(4)/2) screenCenter(1)+(pixRect(3)/2) screenCenter(2)+(pixRect(4)/2)]+[raposx raposy raposx raposy]);
% this time is for fixation
[vbl timing.block(theBlock).stimulus(i).fix]=Screen('Flip', window, [vbl+(maskDur-0.5)*ifi]);
if mod(i,2)
[vbl timing.block(theBlock).stimulus(i).stimulus]=Screen('Flip', window, [vbl+(itemDur1-0.5)*ifi]);
trialItemDur(theBlock, i) = itemDur1;
else
[vbl timing.block(theBlock).stimulus(i).stimulus]=Screen('Flip', window, [vbl+(itemDur2-0.5)*ifi]);
trialItemDur(theBlock, i) = itemDur2;
end
% show mask
Screen('DrawTexture', window, tex(themask1), [], [screenCenter(1)-(pixRect(3)/2) screenCenter(2)-(pixRect(4)/2) screenCenter(1)+(pixRect(3)/2) screenCenter(2)+(pixRect(4)/2)]+[raposx raposy raposx raposy]);
% buffer
[vbl timing.block(theBlock).stimulus(i).answer1]=Screen('Flip', window, [vbl+(testDur -0.5)*ifi]);
Screen('DrawTexture', window, tex(themask2), [], [screenCenter(1)-(pixRect(3)/2) screenCenter(2)-(pixRect(4)/2) screenCenter(1)+(pixRect(3)/2) screenCenter(2)+(pixRect(4)/2)]+[raposx raposy raposx raposy]);
% mask1
[vbl timing.block(theBlock).stimulus(i).answer2]=Screen('Flip', window, [vbl+(maskDur-0.5)*ifi]);
Screen('DrawTexture', window, tex(themask3), [], [screenCenter(1)-(pixRect(3)/2) screenCenter(2)-(pixRect(4)/2) screenCenter(1)+(pixRect(3)/2) screenCenter(2)+(pixRect(4)/2)]+[raposx raposy raposx raposy]);
% mask2
[vbl timing.block(theBlock).stimulus(i).answer3]=Screen('Flip', window, [vbl+(maskDur-0.5)*ifi]);
% mask3
[vbl timing.block(theBlock).stimulus(i).endstim]=Screen('Flip', window, [vbl+(maskDur-0.5)*ifi]);
%% collect a key response
FlushEvents;
while responded < 2
responded = responded + 1;
[responseChar(responded) junk] = GetChar;
if responseChar(responded)==27 % 'esc'
ListenChar(0);
Screen('CloseAll')
return;
end
while KbCheck, end %% to avoid problems if subjects press too long
end
responseCharPerTrial{(theBlock-1)*n_staircasePerBlock + i} = responseChar; %save response
% check correctness of response
if responseChar(1)==correctResponse{thepic}(1) && responseChar(2)==correctResponse{thepic}(2)
key(theBlock, i) = 1; % correct trial
Screen('DrawText', window, 'CORRECT!', [screenCenter(1)-170], [screenCenter(2)], black);
if mod(i,2) %first staircase
opeenvolgend_juist_1= opeenvolgend_juist_1 + 1;
if opeenvolgend_juist_1 == 2 %wanneer 2 opeenvolgende trials juist beantwoord worden, wordt de aanbiedingsduur bij volgende trial verminderd
itemDur1= itemDur1-1;
if itemDur1<1
itemDur1 = 1;
end
opeenvolgend_juist_1 = 0;
end
else %second staircase
opeenvolgend_juist_2= opeenvolgend_juist_2 + 1;
if opeenvolgend_juist_2 == 2 %wanneer 2 opeenvolgende trials juist beantwoord worden, wordt de aanbiedingsduur bij volgende trial verminderd
itemDur2= itemDur2-1;
if itemDur2<1
itemDur2 = 1;
end
opeenvolgend_juist_2 = 0;
end
end
else % incorrect trials % reverse, keep recording it
key(theBlock, i) = 0; % incorrect trial naam{thepic}
stimulusname=correctResponse{thepic};
Screen('DrawText', window, 'FOUT! Het was ', [screenCenter(1)-270], [screenCenter(2)], black);
Screen('DrawText', window, stimulusname, [screenCenter(1)+80], [screenCenter(2)], black);
if mod(i,2)
opeenvolgend_juist_1=0;
itemDur1= itemDur1+1;
else
opeenvolgend_juist_2=0;
itemDur2= itemDur2+1;
end
end % checking correction of typing
[vbl timing.block(theBlock).stimulus(i).feedback]=Screen('Flip', window,[vbl+(feedbackDur-0.5)*ifi]); %blank screen);
end
end
timing.itIsOver = GetSecs;
ShowCursor;
Screen('CloseAll');
% '_object_800_Day' num2str(acq) '.mat']);
save([num2str(subjID) '_object_800_Day' num2str(This_day) '_' num2str(acq) '.mat'], 'key', 'theorder', 'theorderMasks', 'subjID', 'acq', 'frameRate', ...
'Ima_Size','Ima_Loc', 'trialItemDur', 'responseCharPerTrial','timing', 'screenWidth', 'screenHeight', 'condition');
warning on;
%the "catch" section executes in case of an error in the "try" section
%most importantly, it will close the onscreen window
% catch
ListenChar(0);
% Close display windows
Screen('CloseAll');
ShowCursor;
%restore priority
Priority(0);
% tell user what caused the crash
rethrow(lasterror);
% end