-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoContactAnalyzerSi.m
406 lines (296 loc) · 15.1 KB
/
autoContactAnalyzerSi.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
function [contacts, params]=autoContactAnalyzerSi(array, params, contacts, varargin)
% Version 0.5.0 SAH 110827
%% Trial Primary Contact Type Determination
%
% Estimates contact times of the whisker into the pole. Good results
% requires a very accurate .bar file and accurate touch thresholds. Four
% touch thresholds are used, for go (protraction, retraction), no-go
% (protraction,retraction). These should be determined by visual
% inspection of the distanceToPoleCenter plots in several example trials,
% in concert with the video of the trial. Use Parameter Estimation cell to
% help.
%
% In general, k represents a trial number, i represents some event number
% in the trial (contact, spike).
%
% First use some generous limits for the touchThresh values. Then, use the
% plot output of the Contact Segmenter cell to refine the touchThresh
% values and rerun the Trial Primary Contact Type Determination
%
% Use Parameter Estimation cell to check how well the contacts are being
% scored (currently broken?)
%
%
% Currently designed to work only with 1 whisker, but could be modified to
% analyze a specific tid.
%
% Designed to be used with the Whisker.WhiskerTrialLiteI subclass, which
% has two additional fields, M0I: Holds the acceleration calculated moment
% contactInds: Index of contact times
%
% Pulls data from T.trials{k}.whiskerTrial.M0I
% Stores data to a subclassed field : T.trials{k}.whiskerTrial.contactInds
% Also writes a contacts{k} structure to the workspace that contains all
% the analysis output.
%
% Version 0.1.0 SAH 06/07/10
%
%
%
if nargin==1
[contacts, params] = buildNewContactArray(array);
elseif nargin == 2
[contacts, params] = buildNewContactArray(array,params);
else
end
whiskerTIN = find(array.whiskerTrialInds);
% goTrialNums = cat(1,array.missTrialNums(:),array.hitTrialNums(:));
trialNums = array.trialNums;
pinDescentOnsetTime(whiskerTIN) = cellfun(@(x)x.behavTrial.pinDescentOnsetTime,array.trials(whiskerTIN),'UniformOutput',0);
pinAscentOnsetTime(whiskerTIN) = cellfun(@(x)x.behavTrial.pinAscentOnsetTime,array.trials(whiskerTIN),'UniformOutput',0);
time(whiskerTIN) = cellfun(@(x)x.whiskerTrial.time{1}, array.trials(whiskerTIN),'UniformOutput',0);
distanceToPoleCenter(whiskerTIN) = cellfun(@(x)x.whiskerTrial.distanceToPoleCenter{1}, array.trials(whiskerTIN),'UniformOutput',0);
kappa(whiskerTIN) = cellfun(@(x)x.whiskerTrial.deltaKappa{1}, array.trials(whiskerTIN),'UniformOutput',0);
% thetaAtBase(whiskerTIN) = cellfun(@(x)x.whiskerTrial.thetaAtBase{1}, array.trials(whiskerTIN),'UniformOutput',0);
% thetaAtContact(whiskerTIN) = cellfun(@(x)x.whiskerTrial.thetaAtContact{1}, array.trials(whiskerTIN),'UniformOutput',0);
M0(whiskerTIN) = cellfun(@(x)x.whiskerTrial.M0{1}, array.trials(whiskerTIN),'UniformOutput',0);
M0I(whiskerTIN) = cellfun(@(x)x.whiskerTrial.M0I{1}, array.trials(whiskerTIN),'UniformOutput',0);
Faxial(whiskerTIN) = cellfun(@(x)x.whiskerTrial.Faxial{1}, array.trials(whiskerTIN),'UniformOutput',0);
trialClass(whiskerTIN) = cellfun(@(x)x.trialType*2 + x.trialCorrect + 1, array.trials(whiskerTIN),'UniformOutput',0);
answerLickTime = cellfun(@(x)x.behavTrial.answerLickTime, array.trials ,'UniformOutput',0);
meanContactCurve = zeros(max(whiskerTIN),1);
trialContactType = zeros(max(whiskerTIN),1);
% use new parameters but retain manually scored contacts
if nargin == 3
addFieldIdx = find(cellfun(@(x)isfield(x,'manualAdd'),contacts));
delFieldIdx = find(cellfun(@(x)isfield(x,'manualAdd'),contacts));
addContactInds(addFieldIdx) = cellfun(@(x)x.manualAdd{1},contacts(addFieldIdx),'UniformOutput',0);
delContactInds(delFieldIdx) = cellfun(@(x)x.manualAdd{1},contacts(delFieldIdx),'UniformOutput',0);
contacts = buildNewContactArray(array,params);
for i = addFieldIdx
contacts{i}.manualAdd{1} = addContactInds{i};
contacts{i}.contactInds{1} = unique(cat(2,contacts{i}.manualAdd{1}(:)',contacts{i}.contactInds{1}(:)'));
end
for i = delFieldIdx
contacts{i}.manualDel{1} = delContactInds{i};
contacts{i}.contactInds{1} = unique(cat(2,contacts{i}.manualDel{1}(:)',contacts{i}.contactInds{1}(:)'));
end
end
%% Contact Segmenter
%
% Segmentation of contacts into an ordered list. Each trial gets its own
% cell within the contacts structure. Analysis of each contact resides in
% within fields of contacts{k}, where k is the trial index.
% (k ~= overall trial number)
%
% This cell also plots the distance to pole of the first trial of each
% class (go pro/ret, nogo pro/ret)
if nargin <= 3;
for k=whiskerTIN;
if isempty(contacts{k}.contactInds{1})==0;
contacts{k}.contactInds{1} = contacts{k}.contactInds{1}(contacts{k}.contactInds{1}>array.trials{k}.pinDescentOnsetTime*1000);
contacts{k}.segmentInds{1}(:,1)=contacts{k}.contactInds{1}([1 find(diff(contacts{k}.contactInds{1})>4)+1]); % don't switch back to intertial if the tracking disappears for 1-3 frames
contacts{k}.segmentInds{1}(:,2)=contacts{k}.contactInds{1}([find(diff(contacts{k}.contactInds{1})>4) end]);
ind=[];
for i=1:length(contacts{k}.segmentInds{1}(:,1))
ind=cat(2,ind,contacts{k}.segmentInds{1}(i,1):contacts{k}.segmentInds{1}(i,2));
end
contacts{k}.inferredInds{1} = setdiff(ind,contacts{k}.contactInds{1});
contacts{k}.contactInds{1} = ind;
else
contacts{k}.segmentInds={[]};
trialContactType(k)=0;
end
end
end
%cellfun(@(x)x.trialContactType,contacts)
if nargin ==4
argString = varargin{1};
end
% just recalculate dependent fields
%% Contact Characterizer
% Find timelength for each contact
disp('Finding timelength for each contact')
for k = whiskerTIN
if isempty(contacts{k}.segmentInds{1})==0
contacts{k}.contactLength{1}=time{k}(contacts{k}.segmentInds{1}(:,2))-time{k}(contacts{k}.segmentInds{1}(:,1));
else
contacts{k}.contactLength={[]};
end
end
% Find precontact curvature to adjust force calculations
for k = whiskerTIN
cLookBack = 3; % in frames
if isempty(contacts{k}.segmentInds{1})==0
for i =1:size(contacts{k}.segmentInds{1},1);
contacts{k}.preConCurve{1}(i) = ...
mean(array.trials{k}.whiskerTrial.deltaKappa{1}(contacts{k}.segmentInds{1}(i,1)-(1:cLookBack)));
end
else
contacts{k}.preConCurve{1} = [];
end
end
disp('Merging contact/curvature-derived moment (M0) and axial force (FaxialAdj) with acceleration based moment (M0I)')
M0combo=cell(1,length(array.trials)); % Combined moment calculated from acceleration for non-contact periods and curvature from contact periods.
for k = whiskerTIN ;
contacts{k}.M0combo{1}=M0I{k};
contacts{k}.M0combo{1}(abs(contacts{k}.M0combo{1})>1e-7)=NaN;
contacts{k}.Faxial{1}=zeros(1,length(Faxial{k}));
if isnan(contacts{k}.contactInds{1})==0
ind=[];
for i=1:length(contacts{k}.segmentInds{1}(:,1))
ind=cat(2,ind,contacts{k}.segmentInds{1}(i,1):contacts{k}.segmentInds{1}(i,2));
end
contacts{k}.M0combo{1}(ind)=M0{k}(ind); % build M0Combo from the Segment Inds that have had the 1-2 frame drops filtered out
contacts{k}.Faxial{1}(ind)=Faxial{k}(ind);
else
end
end
% Generate M0comboAdj FaxialAdj to shift baseline curvature for each contact period
for k=whiskerTIN
contacts{k}.M0comboAdj = contacts{k}.M0combo;
contacts{k}.FaxialAdj = contacts{k}.Faxial;
for j=1:size(contacts{k}.segmentInds{1},1)
adjind=contacts{k}.segmentInds{1}(j,1):contacts{k}.segmentInds{1}(j,2);
contacts{k}.M0comboAdj{1}(adjind) = contacts{k}.M0combo{1}(adjind)...
.* (1-contacts{k}.preConCurve{1}(j) ./ array.trials{k}.whiskerTrial.deltaKappa{1}(adjind));
contacts{k}.FaxialAdj{1}(adjind) = contacts{k}.Faxial{1}(adjind)...
.* (1-contacts{k}.preConCurve{1}(j) ./ array.trials{k}.whiskerTrial.deltaKappa{1}(adjind));
end
end
% Find mean M0 for each contact
disp('Finding mean M0 for each contact')
contacts{1}.meanM0={[]};
for k = whiskerTIN
meanContactCurve(k)=nanmean(kappa{k}(contacts{k}.contactInds{1}));
if isempty(contacts{k}.segmentInds{1})==0
for i=1:length(contacts{k}.segmentInds{1}(:,1));
contacts{k}.meanM0{1}(i)=nanmean(M0{k}(contacts{k}.segmentInds{1}(i,1):contacts{k}.segmentInds{1}(i,2)));
contacts{k}.meanM0adj{1}(i)=nanmean(contacts{k}.M0comboAdj{1}(contacts{k}.segmentInds{1}(i,1):contacts{k}.segmentInds{1}(i,2)));
end
else
contacts{k}.meanM0={[]};
contacts{k}.meanM0adj={[]};
end
end
% Find peak M0 for each contact
disp('Find peak M0 for each contact')
contacts{1}.peakM0={[]};
for k = whiskerTIN
if isempty(contacts{k}.segmentInds{1})==0
for i=1:length(contacts{k}.segmentInds{1}(:,1));
contacts{k}.peakM0{1}(i)=max(abs(M0{k}(contacts{k}.segmentInds{1}(i,1):contacts{k}.segmentInds{1}(i,2))))*...
sign(contacts{k}.meanM0{1}(i));
contacts{k}.peakM0adj{1}(i)=max(abs(contacts{k}.M0comboAdj{1}(contacts{k}.segmentInds{1}(i,1):contacts{k}.segmentInds{1}(i,2))))*...
sign(contacts{k}.meanM0adj{1}(i));
end
else
contacts{k}.peakM0={[]};
contacts{k}.peakM0adj={[]};
end
end
% Find mean Faxial for each contact
disp('Finding mean Faxial for each contact')
contacts{1}.meanFaxial={[]};
for k = whiskerTIN
if isempty(contacts{k}.segmentInds{1})==0
for i=1:length(contacts{k}.segmentInds{1}(:,1));
contacts{k}.meanFaxial{1}(i)=nanmean(contacts{k}.Faxial{1}(contacts{k}.segmentInds{1}(i,1):contacts{k}.segmentInds{1}(i,2)));
contacts{k}.meanFaxialAdj{1}(i)=nanmean(contacts{k}.FaxialAdj{1}(contacts{k}.segmentInds{1}(i,1):contacts{k}.segmentInds{1}(i,2)));
end
else
contacts{k}.meanFaxial={[]};
contacts{k}.meanFaxialAdj={[]};
end
end
% Find peak M0 for each contact
disp('Find peak Faxial for each contact')
contacts{1}.peakFaxial={[]};
for k = whiskerTIN
if isempty(contacts{k}.segmentInds{1})==0
for i=1:length(contacts{k}.segmentInds{1}(:,1));
contacts{k}.peakFaxial{1}(i)=min(contacts{k}.Faxial{1}(contacts{k}.segmentInds{1}(i,1):contacts{k}.segmentInds{1}(i,2)));
contacts{k}.peakFaxialAdj{1}(i)=min(contacts{k}.FaxialAdj{1}(contacts{k}.segmentInds{1}(i,1):contacts{k}.segmentInds{1}(i,2)));
end
else
contacts{k}.peakFaxial={[]};
contacts{k}.peakFaxialAdj={[]};
end
end
% Find answertime for each contact
contacts{1}.answerLickTime={[]};
for k = whiskerTIN
contacts{k}.answerLickTime=answerLickTime{k};
end
assignin('base','contacts',contacts);
% %% Plotting Output
%
% Plot the estimated primary contact type by trial number
h_analyzer=figure(12);
set(gcf,'DefaultLineMarkerSize',12)
subplot(2,3,[2 3]);cla;hold on;
for k=whiskerTIN
plot(trialNums(k),meanContactCurve(k), [params.trialcolors{trialClass{k}} '.']);
end
plot([trialNums(1) trialNums(end)],[params.goProThresh params.goProThresh],'k:');
plot([trialNums(1) trialNums(end)],[params.nogoProThresh params.nogoProThresh],'k--');
xlabel('Trial Number');
ylabel('Mean Contact Curvature (\kappa)');
grid off
subplot(2,3,[5 6]);cla;hold on
for k=whiskerTIN
plot(trialNums(k),contacts{k}.trialContactType, [params.trialcolors{trialClass{k}} '.']);
end
axis([trialNums(1) trialNums(end) -.1 4.1])
xlabel('Trial Number')
grid on
set(gca,'YTick',[0 1 2 3 4],'YTickLabel','No Contact | Go Protract | Go Retract | Nogo Protract | Nogo Retract')
title('estimated primary contact type')
subplot(2,3,1);cla;
% plot([0 1],[0 1],'.');
set(gca,'Visible','off');
text(-.3,.9, ['\fontsize{8}' 'Trajectory ID :\bf ' num2str(params.tid)]);
text(-.3,.8, ['\fontsize{8}' 'Pole Delay Offset On :\bf ' num2str(params.poleOffset) '\rm Off : \bf' num2str(params.poleEndOffset) '(s)']);
text(-.3,.7, ['\fontsize{8}' 'Pro/Ret Threshold Go :\bf ' num2str(params.goProThresh) '\rm NoGo : \bf' num2str(params.nogoProThresh)]);
text(-.3,.6, ['\fontsize{8}' 'Contact Threshold :\bf ' num2str(params.touchThresh(1)) ' / ' num2str(params.touchThresh(2)) ' / ' num2str(params.touchThresh(3)) ' / ' num2str(params.touchThresh(4))]);
text(-.3,.5, ['\fontsize{8}' 'Curvature Multiplier :\bf ' num2str(params.curveMultiplier)]);
% text(-.3,.4, ['\fontsize{8}' 'Mean Spike Rate :\bf ' num2str(array.meanSpikeRateInHz) ' (Hz)']);
text(-.3,.3, ['\fontsize{8}' 'Mouse :\bf ' array.mouseName]);
% text(-.3,.2, ['\fontsize{8}' 'Cell :\bf ' array.cellNum '' array.cellCode '' array.mouseName]) ;
% text(-.3,.1, ['\fontsize{8}' 'Location :\bf ' num2str([min(array.depth) ) ' \mum' ' ' num2str(array.recordingLocation)]) ;
set(h_analyzer,'PaperOrientation','landscape','PaperPosition',[.25 .25 10.75 7.75])
%%
hitTIN = intersect(whiskerTIN,find(array.hitTrialInds));
CRTIN = intersect(whiskerTIN,find(array.correctRejectionTrialInds));
edges = -.2:.001:.5;
onTrim = mean(cellfun(@(x)x,pinDescentOnsetTime(whiskerTIN))) + params.poleOffset;
offTrim = min(cellfun(@(x)x,pinAscentOnsetTime(whiskerTIN)));
n = zeros(length(CRTIN),length(edges));
n2 = zeros(length(CRTIN),length(edges));
hfig_contactThresh = figure(5);
for k=hitTIN
y = distanceToPoleCenter{k}(time{k} > onTrim & time{k} < pinAscentOnsetTime{k} + params.poleEndOffset);
y2 = kappa{k}(time{k} > onTrim & time{k} < pinAscentOnsetTime{k} + params.poleEndOffset);
n(k,:) = histc(y,edges);
n2(k,:) = histc(y-params.curveMultiplier*abs(y2)-(5*params.curveMultiplier*y2).^2,edges);
end
subplot(2,1,1);cla;hold on
plot(edges+.0005,mean(n))
plot(edges+.0005,mean(n2),'r')
n = zeros(length(CRTIN),length(edges));
n2 = zeros(length(CRTIN),length(edges));
for k=CRTIN
y = distanceToPoleCenter{k}(time{k} > onTrim & time{k} < pinAscentOnsetTime{k} + params.poleEndOffset);
y2 = kappa{k}(time{k} > onTrim & time{k} < pinAscentOnsetTime{k} + params.poleEndOffset);
edges = -.2:.001:.5;
edges2 = -.2:.001:.5;
n(k,:) = histc(y,edges);
n2(k,:) = histc(y-params.curveMultiplier*abs(y2)-5*(params.curveMultiplier*y2).^2,edges);
end
subplot(2,1,2);cla;
hold on;
plot(edges+.0005,mean(n))
plot(edges2+.0005,mean(n2),'r')
%display('saving contacts');
%save(['Z:\users\Andrew\Whisker Project\Silicon\ConTA\recalc\ConTA_' array.mouseName '_' array.sessionName], 'contacts', 'params');
%print(h_analyzer, '-depsc',[array.mouseName '-' array.cellNum '-' 'contactParams.eps']);