-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdataclinic.m
269 lines (252 loc) · 13 KB
/
dataclinic.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
% Data clinic
function allcodes=dataclinic(varargin)
cure=varargin{1};
%% check, and if necessary, fix wrong directions ecodes in countermanding task
if strcmp(cure,'fixdircs') || strcmp(cure,'fixdirol')
allcodes=varargin{2};
saccadeInfo=varargin{3};
allbad=varargin{4};
if strcmp(cure,'fixdircs')
% (the issue appears when using negative x values for tab3)
% treatedfile='S121L4A5_13091.mat';
% load(treatedfile);
basecode=6040;
elseif strcmp(cure,'fixdirol')
% two problems:
% - use same dir code for multiple amplitude
% - use same dir code for left and right
% In addition to that, since I coded the task like an idiot, target
% locations for FP1 are flipped horizontally AND vertically with
% respect to FP0.
%load(firstarg);
% allcodes=firstarg;
% saccadeInfo=secarg;
basecode=6010;
% optilocstruct=[
% 28 28 6011
% 40 0 6012
% 28 -28 6013
% 84 84 6011
% 120 0 6012
% 84 -84 6013
% 140 140 6011
% 200 0 6012
% 140 -140 6013]; %6011=TR; 6012=SR; 6013=BR
end
alldirs=reshape({saccadeInfo.direction},size(saccadeInfo)); % all directions found in saccadeInfo
allamps=reshape({saccadeInfo.amplitude},size(saccadeInfo));
alldirs=alldirs'; %needs to be transposed because the logical indexing below will be done column by column, not row by row
allamps=allamps';
allgoodsacs=~cellfun('isempty',reshape({saccadeInfo.latency},size(saccadeInfo)));
%removing bad trials
allgoodsacs(logical(allbad),:)=0;
%removing stop trials that may be included
allgoodsacs(floor(allcodes(:,2)./1000)~=6,:)=0;
%removing "2222" trials, although they may be correct
if find(allcodes==2222,1)
[~,wecol]=ind2sub(size(allcodes),find(allcodes==2222,1));
allgoodsacs(allcodes(:,wecol)==2222,:)=0;
end
%indexing good sac trials
% if saccade detection corrected, there may two 'good' saccades
if max(sum(allgoodsacs,2))>1
twogoods=find(sum(allgoodsacs,2)>1);
for dblsac=1:length(twogoods)
allgoodsacs(twogoods(dblsac),find(allgoodsacs(twogoods(dblsac),:),1))=0;
end
end
goodsacindex=logical(sum(allgoodsacs,2));
%translate ecodes into directions
goodsactrialecodes=allcodes(goodsacindex,:);
%getting basecode
gsacbasecode=goodsactrialecodes(:,2);
%getting all good directions
allgooddirs=cell2mat(alldirs(allgoodsacs'));
allgoodamps=cell2mat(allamps(allgoodsacs'));
% convert codes to directions (going anticlockwise because codes are
% inverted left-right)
gsacbcodeangle=nan(size(gsacbasecode));
if strcmp(cure,'fixdircs')
gsacbcodeangle(gsacbasecode-basecode==0)=0;
gsacbcodeangle(gsacbasecode-basecode==7)=45;
gsacbcodeangle(gsacbasecode-basecode==6)=90;
gsacbcodeangle(gsacbasecode-basecode==5)=135;
gsacbcodeangle(gsacbasecode-basecode==4)=180;
gsacbcodeangle(gsacbasecode-basecode==3)=225;
gsacbcodeangle(gsacbasecode-basecode==2)=270;
gsacbcodeangle(gsacbasecode-basecode==1)=315;
elseif strcmp(cure,'fixdirol')
%use allgoodamps as predictor of direction reversal for optiloc
gsacbcodeanglefp0=gsacbcodeangle(allgoodamps<0);
gsacbcodeanglefp1=gsacbcodeangle(allgoodamps>0);
gsacbcodeanglefp0(gsacbasecode(allgoodamps<0)-basecode==1)=45;
gsacbcodeanglefp1(gsacbasecode(allgoodamps>0)-basecode==1)=135;
gsacbcodeanglefp0(gsacbasecode(allgoodamps<0)-basecode==2)=90;
gsacbcodeanglefp1(gsacbasecode(allgoodamps>0)-basecode==2)=90;
gsacbcodeanglefp0(gsacbasecode(allgoodamps<0)-basecode==3)=135;
gsacbcodeanglefp1(gsacbasecode(allgoodamps>0)-basecode==3)=45;
gsacbcodeangle=[gsacbcodeanglefp0;gsacbcodeanglefp1];
end
% compare code angle with actual saccade direction
anglediff=allgooddirs-gsacbcodeangle;
% find out range of wrong trials
trialq=find(goodsacindex);
wrongdircode=abs(anglediff)>45; %indexing in good saccade trials
%wrongconditions=max(bwlabel(wrongdircode)); %should hopefully be one at most. Although, if bad conditions are contiguous, it will not be detected here
wrongdircode=trialq(find(wrongdircode,1)); % first wrong trial in all trials
% wrong direction digit
if strcmp(cure,'fixdirol')
if ~isempty(wrongdircode)
if find(allgoodamps<0,1)==1
wrongdircode=trialq(find(abs(anglediff)>45,1,'last'))+1;
rangewtrials=wrongdircode:size(allcodes,1);
else
wrongdircode=1;
rangewtrials=wrongdircode:trialq(find(abs(anglediff)<45,1)-1);
end
for wtr=1:length(rangewtrials)
trbasecd=floor(allcodes(rangewtrials(wtr),2)/10)*10;
wtrdir=allcodes(rangewtrials(wtr),2)-trbasecd;
replacedir=wtrdir+4;
findwrcd=trbasecd+wtrdir;
while logical(sum(find(allcodes(rangewtrials(wtr),:)==findwrcd)))
allcodes(rangewtrials(wtr),allcodes(rangewtrials(wtr),:)==findwrcd)=...
floor(findwrcd/10)*10+replacedir;
findwrcd=findwrcd+200; % next ecode in line
end
end
end
elseif strcmp(cure,'fixdircs')
if allcodes(wrongdircode,2)-basecode>4
wrgdirdg=[allcodes(wrongdircode,2)-basecode allcodes(wrongdircode,2)-6044];
else
wrgdirdg=[allcodes(wrongdircode,2)-basecode allcodes(wrongdircode,2)-6036];
end
%find if stop trials before, and if yes, if wrong direction too
% foo=allcodes;
%foo(wrongdircode-4:wrongdircode-1,2)=4075;
if ~isempty(wrongdircode)
firstwrongcode=wrongdircode;
while floor(allcodes(firstwrongcode-1,2)/10)==407 && firstwrongcode~=1
firstwrongcode=firstwrongcode-1;
end
if firstwrongcode~=1 || (firstwrongcode==1 && wrongdircode==firstwrongcode) %otherwise it means there's no way to differentiate previous condition
% testing direction of potential wrong dir stop trials
while ~(allcodes(firstwrongcode,2)-4070==wrgdirdg(1) || allcodes(firstwrongcode,2)-4070==wrgdirdg(2)) && firstwrongcode~=wrongdircode
firstwrongcode=firstwrongcode+1;
end
end
if floor(allcodes(firstwrongcode,2)/10)==407 && max(allcodes(firstwrongcode-1,2)-basecode==wrgdirdg)
firstwrongcode=firstwrongcode+1; %just to be conservative
end
% same story for last wrong trial
wrongdircode=abs(anglediff)>45; %re-indexing
%wrongconditions=max(bwlabel(wrongdircode)); %should hopefully be one at most. Although, if bad conditions are contiguous, it will not be detected here
wrongdircode=trialq(find(wrongdircode,1,'last'));
lastwrongcode=wrongdircode;
%foo(wrongdircode+1:wrongdircode+4,2)=4072;
while floor(allcodes(lastwrongcode+1,2)/10)==407 && lastwrongcode~=size(allcodes,1)
lastwrongcode=lastwrongcode+1;
end
if (lastwrongcode~=size(allcodes,1) || (lastwrongcode==size(allcodes,1) && wrongdircode==lastwrongcode))
% testing direction of potential wrong dir stop trials
while ~(allcodes(lastwrongcode,2)-4070==wrgdirdg(1) || allcodes(lastwrongcode,2)-4070==wrgdirdg(2)) && lastwrongcode~=wrongdircode
lastwrongcode=lastwrongcode+1;
end
end
if floor(allcodes(lastwrongcode,2)/10)==407 && max(allcodes(lastwrongcode+1,2)-basecode==wrgdirdg)
lastwrongcode=lastwrongcode-1; % same conservative principle
end
% now find the correct code direction
wrongdircode=abs(anglediff)>45; %re-re-indexing
gooddirs=allgooddirs(wrongdircode);
[~,gooddirlim]=hist(gooddirs,2);
gooddirlowval=ceil(median(gooddirs(gooddirs<gooddirlim(1))));
gooddirupval=ceil(median(gooddirs(gooddirs>gooddirlim(2))));
allanglecodes=[0 45 90 135 180 225 270 315];
alldirscode=[0 7 6 5 4 3 2 1]; % counterclockwise to replicate leftward inversion
% "absolute" correct directions
% gooddirlowval=allanglecodes(abs(gooddirlowval-allanglecodes)==min(abs(gooddirlowval-allanglecodes)));
% gooddirupval=allanglecodes(abs(gooddirupval-allanglecodes)==min(abs(gooddirupval-allanglecodes)));
% corresponding ecode direction
gooddirfirstvalcode=alldirscode(abs(gooddirlowval-allanglecodes)==min(abs(gooddirlowval-allanglecodes)));
gooddirsecvalcode=alldirscode(abs(gooddirupval-allanglecodes)==min(abs(gooddirupval-allanglecodes)));
%find the corresponding wrong dir
firstwrongdirvalcode=goodsactrialecodes(find(wrongdircode,1),2)-floor(goodsactrialecodes(find(wrongdircode,1),2)/10)*10;
if allgooddirs(find(wrongdircode,1))<gooddirlim(1);
wrongdirfirstvalcode=firstwrongdirvalcode;
if firstwrongdirvalcode<4
wrongdirsecvalcode=firstwrongdirvalcode+4;
else
wrongdirsecvalcode=firstwrongdirvalcode-4;
end
else
if firstwrongdirvalcode<4
wrongdirfirstvalcode=firstwrongdirvalcode+4;
else
wrongdirfirstvalcode=firstwrongdirvalcode-4;
end
wrongdirsecvalcode=firstwrongdirvalcode;
end
% then replace values in allcodes
rangewtrials=firstwrongcode:lastwrongcode;
for wtr=1:length(rangewtrials)
wtrdir=allcodes(rangewtrials(wtr),2)-floor(allcodes(rangewtrials(wtr),2)/10)*10;
if wtrdir==wrongdirfirstvalcode
replacedir=gooddirfirstvalcode;
else
replacedir=gooddirsecvalcode;
end
trbasecd=floor((allcodes(rangewtrials(wtr),2))/10)*10;
findwrcd=trbasecd+wtrdir;
while logical(sum(find(allcodes(rangewtrials(wtr),:)==findwrcd)))
allcodes(rangewtrials(wtr),find(allcodes(rangewtrials(wtr),:)==findwrcd))=...
floor(findwrcd/10)*10+replacedir;
findwrcd=findwrcd+200; % next ecode in line
end
end
%save allcodes in file
% corecdata=matfile(treatedfile);
% corecdata.Properties.Writable = true;
% corecdata.allcodes=allcodes;
end
%% individual trial tests
% trialw=233;
% trialq=find(goodsacindex);
% trialq=trialq(trialw);
% %trialq=104;
% trialgsac(1)=saccadeInfo(trialq,find(allgoodsacs(trialq,:))).starttime;
% trialgsac(2)=saccadeInfo(trialq,find(allgoodsacs(trialq,:))).endtime;
% % double check what was done in find_saccade_3
% % getting amplitudes
% checkamp=saccadeInfo(trialq,find(allgoodsacs(trialq,:))).amplitude;
% sacamp=sqrt((allh(trialq,trialgsac(2))-allh(trialq,trialgsac(1)))^2 + ...
% (allv(trialq,trialgsac(2))-allv(trialq,trialgsac(1)))^2);
% % getting angle
% sacdeg=abs(atand((allh(trialq,trialgsac(2))-allh(trialq,trialgsac(1)))/(allv(trialq,trialgsac(2))-allv(trialq,trialgsac(1)))));
%
% % sign adjustements
% if allv(trialq,trialgsac(2))<allv(trialq,trialgsac(1)) % vertical flip
% sacdeg=180-sacdeg;
% end
%
% if allh(trialq,trialgsac(2))>allh(trialq,trialgsac(1))% inverted signal: leftward is in postive range.
% % Correcting to negative.
% sacamp=-sacamp;
% sacdeg=360-sacdeg; % mirror image;
% end
%
%
% %comparisons
% ampdiff=sacamp-checkamp
% angldiff=sacdeg-gsacbcodeangle(trialw)
%
% figure(21)
% plot(allh(trialq,trialgsac(1):trialgsac(2)));
% hold on;
% plot(allv(trialq,trialgsac(1):trialgsac(2)),'r');
% hold off
end
end
end
%% access outliers and mismatch