-
Notifications
You must be signed in to change notification settings - Fork 0
/
d181211_single_cell_anova_spk.m
547 lines (499 loc) · 30.2 KB
/
d181211_single_cell_anova_spk.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
% # of trials, # of touches in each angle in 2 different volumes
% Considering just unimodal (or monotonic) tuning for now 2018/06/27 JK
% load u and, if it exists, ANOVA tuning file
clear
baseDir = 'Y:\Whiskernas\JK\suite2p\';
mice = [25,27,30,36,37,38,39,41,52,53,54,56,70,74,75,76];
sessions = {[4,19],[3,16],[3,21],[1,17],[7],[2],[1,22],[3],[3,21],[3],[3],[3],[6],[4],[4],[4]};
% mice = [38,41];
% sessions = {[2],[3]};
% settings
angles = 45:15:135;
% baseFrameNum = 3;
baseFrameDuration = 0.5; % in sec
afterFrameDuration = 0.5; % in sec
thresholdAnovaP = 0.05;
thresholdTtestSharpness = 0.05;
thresholdTtestResponse = 0.05;
thresholdCategory = 0.05;
thresholdResponseTotal = 0.05;
% thresholdResponsePositive = 1 * afterFrameDuration/frameRate; % spk - base spk
% thresholdResponseNegative = -1 * afterFrameDuration/frameRate; % spk - base spk
anovactype = 'hsd';
excludeDrinkingTime = 0;
onlyBeforeDecision = 0;
% if onlyBeforeDecision
onlyAfterDecision = 0;
% else
% onlyAfterDecision = 1;
% end
onlyFirstTouch = 0;
allowOverlap = 0;
for mi = 1 : length(mice)
% for mi = 3
mouse = mice(mi);
cd(sprintf('%s%03d',baseDir,mouse))
for si = 1 : length(sessions{mi})
% for si = 1
session = sessions{mi}(si);
ufn = sprintf('UberJK%03dS%02d',mouse, session);
load(ufn)
% u = Uber.buildUberArray(mice(mi), sessions{mi}(si));
% still some settings
frameRate = u.frameRate;
thresholdResponsePositive = 1 * afterFrameDuration/frameRate; % spk - base spk
thresholdResponseNegative = -1 * afterFrameDuration/frameRate; % spk - base spk
baseFrameNum = round(frameRate*baseFrameDuration);
afterFrameNum = round(frameRate*afterFrameDuration);
savefn = [u.mouseName,u.sessionName,'singleCell_anova_spk.mat']; %
% if exist(savefn, 'file')
% fprintf('JK%03dS%02d file already exists. Abort.\n', mice(mi), sessions{mi}(si))
% continue
% end
% initialization
cellsTuned = [];
tuneAngle = []; % Max response angle, in case of multimodal response
tuneDirection = []; % 1 up, 2 down, 3 bipolar
tuneAmplitude = []; % response to the tuned angle.
tuneModulationMaxmin = []; % -2~2. abs > 2 means bipolar. sign shows which direction is stronger, and value shows how strong it's bipolarity (both large increase and large reduction).
tuneSharpness = []; % based on posthoc analysis. How many neighboring bins are significantly indifferent from the max tuned bin.
tuneSharpnessFWHM = []; % # of neighbors with response larger than half of the modulation. For now it will be useful for only within-angle comparison
tuneSharpnessTtest = []; % # of neighbors with same tune direction. For now it will be useful for only within-angle comparison
tuneResponseProb = []; % proportion of response to the tuned angle
tuneReliability = []; % To the max response angle. Average of Pearson's correlation to the average time series
tuneSingle = [];
tuneBroad = [];
tuneLOO = []; % leave-one-out
tuneMM = []; % multimodal. Including bipolar.
tuneCateg = [] ; % categorical (>= 90 or <= 90)
tuneRamp = []; % ramping up or down. One may be off.
tuneRampStrict = []; % from all means
cellsNTResponse = [];
cellsNTNR = [];
NTRAmplitude = [];
NTRdirection = []; % 1 up, 2 down
NTRresponseProb = []; % proportion of response to the touch
NTRreliability = []; % average of Pearson's correlation to the average time series (from all the angles)
tuneMaxResponseTimepoint = [];
NTRMaxResponseTimepoint = [];
% for confirmation
spkTotal = cell(length(u.cellNums),1); % for all cells. ordered by cell number
cellsTotal = u.cellNums;
cellDepths = u.cellDepths;
cellMaps = u.cellmap;
cellisC2 = u.isC2;
cellThresholds = zeros(length(u.cellNums),2); % 1st for positive, 2nd for negative
oneSampleH = zeros(length(u.cellNums), length(angles));
anovaP = zeros(length(u.cellNums),1);
responseP = zeros(length(u.cellNums),1);
meanTotal = zeros(length(u.cellNums),1);
for cellid = 1:length(u.cellNums)
fprintf('Processing mouse #%03d session #%02d cell id %04d / %d\n', mice(mi), sessions{mi}(si), cellid, length(u.cellNums))
cellNum = u.cellNums(cellid);
touchNumTrial = cell(length(angles),1);
touchNumChunk = cell(length(angles),1);
countedTouchs = cell(length(angles),1);
spk = cell(length(angles),1);
for ai = 1 : length(angles)
angle = angles(ai);
plane = floor(cellNum/1000);
trialPlaneInd = find(cellfun(@(x) ~isempty(find(x.planes == plane, 1)), u.trials));
trialAngleInd = find(cellfun(@(x) x.angle == angle, u.trials));
trialInd = intersect(trialPlaneInd, trialAngleInd);
touchNum = 0;
touchNumTrial{ai} = [];
touchNumChunk{ai} = [];
for i = 1 : length(trialInd)
if ~isempty(u.trials{trialInd(i)}.protractionTouchChunks)
if onlyFirstTouch
if excludeDrinkingTime && ~isempty(u.trials{trialInds(i)}.drinkingTime)
if u.trials{trialInds(i)}.protractionTouchChunks{1}(1) + afterFrameNum / frameRate > u.trials{trialInds(i)}.drinkingTime
continue
else
touchChunks = {u.trials{trialInd(i)}.protractionTouchChunks{1}};
chunkIndSave = 1;
end
else
touchChunks = {u.trials{trialInd(i)}.protractionTouchChunks{1}};
chunkIndSave = 1;
end
else
if excludeDrinkingTime && ~isempty(u.trials{trialInd(i)}.drinkingTime)
tempChunksDrinking = u.trials{trialInd(i)}.protractionTouchChunks;
chunkIndDrinking = 1:length(tempChunksDrinking);
drinkInd = [];
for j = 1 : length(tempChunksDrinking)
if tempChunksDrinking{j}(1) > u.trials{trialInd(i)}.drinkingTime(1) - afterFrameDuration || tempChunksDrinking{j}(1) < u.trials{trialInd(i)}.drinkingTime(2) + afterFrameDuration
drinkInd = [drinkInd, j];
end
end
chunkIndDrinking = setdiff(chunkIndDrinking, drinkInd);
touchChunks = cell(length(chunkIndDrinking),1);
for j = 1 : length(touchChunks)
touchChunks{j} = tempChunksDrinking{chunkIndDrinking(j)};
end
chunkIndSave = chunkIndDrinking;
else
touchChunks = u.trials{trialInd(i)}.protractionTouchChunks;
chunkIndSave = 1 : length(u.trials{trialInd(i)}.protractionTouchChunks);
end
if onlyBeforeDecision && ~isempty(u.trials{trialInd(i)}.answerLickTime)
decisionChunkInd = length(touchChunks)+1;
for j = 1 : length(touchChunks)
if touchChunks{j}(1) > u.trials{trialInd(i)}.answerLickTime
decisionChunkInd = j;
break
end
end
if decisionChunkInd == 1
touchChunks = {};
chunkIndSave = [];
else
decisionInd = 1 : decisionChunkInd - 1;
tempTouchChunks = touchChunks;
touchChunks = cell(length(decisionInd),1);
for j = 1 : length(decisionInd)
touchChunks{j} = tempTouchChunks{decisionInd(j)};
end
chunkIndSave = chunkIndSave(decisionInd);
end
end
if onlyAfterDecision && ~isempty(u.trials{trialInd(i)}.answerLickTime)
decisionChunkInd = length(touchChunks)+1;
for j = 1 : length(touchChunks)
if touchChunks{j}(1) > u.trials{trialInd(i)}.answerLickTime
decisionChunkInd = j;
break
end
end
if decisionChunkInd == length(touchChunks)
touchChunks = {};
chunkIndSave = [];
else
decisionInd = decisionChunkInd : length(touchChunks);
tempTouchChunks = touchChunks;
touchChunks = cell(length(decisionInd),1);
for j = 1 : length(decisionInd)
touchChunks{j} = tempTouchChunks{decisionInd(j)};
end
chunkIndSave = chunkIndSave(decisionInd);
end
end
end
if ~isempty(touchChunks)
touchNum = touchNum + length(touchChunks);
touchNumTrial{ai} = [touchNumTrial{ai}; ones(length(touchChunks),1) * trialInd(i)];
touchNumChunk{ai} = [touchNumChunk{ai}; chunkIndSave'];
end
end
end
%%
countedTouchs{ai} = 0;
spk{ai} = nan(touchNum, baseFrameNum + afterFrameNum);
for i = 1 : touchNum
trial = touchNumTrial{ai}(i);
chunk = touchNumChunk{ai}(i);
trialSpk = u.trials{trial}.spk;
time = u.trials{trial}.protractionTouchChunks{chunk}(1); % for now, just consider the first touch point
frameInd = find(u.trials{trial}.tpmTime{mod(plane-1,4)+1} >= time, 1, 'first') - 1; % -1 to adjust 0 s point 2018/12/11 JK
if i > 1 && ~allowOverlap
if trial == trialOld
if frameInd - frameOld < afterFrameNum
continue
end
end
end
countedTouchs{ai} = countedTouchs{ai} + 1;
spkInd = find(u.trials{trial}.neuindSession == cellNum);
if frameInd < baseFrameNum
tempBaseNum = frameInd;
spk{ai}(i,baseFrameNum-frameInd+1:baseFrameNum) = trialSpk(spkInd, 1:frameInd);
else
spk{ai}(i,1:baseFrameNum) = trialSpk(spkInd, frameInd-baseFrameNum + 1 : frameInd);
end
maxafternum = max(size(trialSpk,2)-frameInd, afterFrameNum);
if size(trialSpk,2)-frameInd < afterFrameNum
tempAfter = size(trialSpk,2)-frameInd;
spk{ai}( i, baseFrameNum + 1 : baseFrameNum + tempAfter ) = trialSpk(spkInd, frameInd + 1 : frameInd + tempAfter);
else
spk{ai}( i, baseFrameNum + 1 : end) = trialSpk(spkInd, frameInd + 1 : frameInd + afterFrameNum);
end
tempBase = nanmean(spk{ai}(i, 1 : baseFrameNum));
spk{ai}(i,:) = (spk{ai}(i,:) - tempBase);
frameOld = frameInd;
trialOld = trial;
end
spk{ai}(isnan(nanmean(spk{ai},2)),:) = []; % removing all the NaN rows
end
spkTotal{cellid} = spk;
% ANOVA
touchNumGroups = cellfun(@(x) size(x,1), spk);
timeAverageSpk = zeros(sum(touchNumGroups),1);
anovaGroups = zeros(sum(cellfun(@(x) size(x,1), spk)),1);
frames = 1 : afterFrameNum;
groupAverageSpk = zeros(length(angles),length(frames));
for ai = 1 : length(angles)
timeAverageSpk( sum(touchNumGroups(1:(ai-1))) + 1 : sum(touchNumGroups(1:ai)) ) = nanmean(spk{ai}(:,baseFrameNum+frames),2);
anovaGroups( sum(touchNumGroups(1:(ai-1))) + 1 : sum(touchNumGroups(1:ai)) ) = deal(ai);
groupAverageSpk(ai,:) = nanmean(spk{ai}(:,baseFrameNum+frames),1);
end
[anovaP(cellid), ~, anovaStat] = anova1(timeAverageSpk, anovaGroups, 'off');
pairComp = multcompare(anovaStat, 'Ctype', anovactype, 'Display', 'off');
statMeans = cellfun(@(x) mean(nanmean(x(:,baseFrameNum+frames),2)), spk);
tempH = cellfun(@(x) ttest(nanmean(x(:,baseFrameNum+frames),2)), spk);
if ~isempty(find(isnan(tempH)))
tempH(find(isnan(tempH))) = deal(0);
end
oneSampleInd = find(tempH);
[~, responseP(cellid)] = ttest(timeAverageSpk); % regardless of the angles.
meanTotal(cellid) = mean(timeAverageSpk);
tempThreshold = [thresholdResponsePositive, thresholdResponseNegative];
cellThresholds(cellid,:) = tempThreshold;
if anovaP(cellid) <= thresholdAnovaP && ~isempty(oneSampleInd)
if max(statMeans(oneSampleInd)) > tempThreshold(1) || min(statMeans(oneSampleInd)) < tempThreshold(2)
cellsTuned = [cellsTuned; cellNum];
tuneModulationMaxmin = [tuneModulationMaxmin; (max(statMeans) - min(statMeans))];
if max(statMeans(oneSampleInd)) > tempThreshold(1) % have to breakdown because the absolute threshold is different 2018/10/19 JK
if min(statMeans(oneSampleInd)) < tempThreshold(2) % bipolar % Response to major tuned angle is positive
tuneDirection = [tuneDirection; 3]; % bipolar
threshinds = find(statMeans(oneSampleInd) > tempThreshold(1) | statMeans(oneSampleInd) < tempThreshold(2));
[maxAmp, maxInd] = max(abs(statMeans(oneSampleInd(threshinds))));
tuneAmplitude = [tuneAmplitude; maxAmp];
tunedAngleInd = oneSampleInd(threshinds(maxInd)); % for readability
tuneAngle = [tuneAngle; angles(tunedAngleInd)];
tempTimeseries = groupAverageSpk(tunedAngleInd,:);
if statMeans(tunedAngleInd) > tempThreshold(1) % major response positive
mt = find(tempTimeseries > max(tempTimeseries)*0.9,1);
elseif statMeans(tunedAngleInd) < tempThreshold(2) % majore response negative
mt = find(tempTimeseries < min(tempTimeseries)*0.9,1);
else % error
mt = -1;
end
else % unidirectional
tuneDirection = [tuneDirection; 1]; % positive
[maxAmp, maxInd] = max(statMeans(oneSampleInd));
tuneAmplitude = [tuneAmplitude; maxAmp];
tunedAngleInd = oneSampleInd(maxInd); % for readability
tuneAngle = [tuneAngle; angles(tunedAngleInd)];
tempTimeseries = groupAverageSpk(tunedAngleInd,:);
mt = find(tempTimeseries > max(tempTimeseries)*0.9,1);
end
elseif min(statMeans(oneSampleInd)) < tempThreshold(2) % No positive response present. Purely negative response
tuneDirection = [tuneDirection; 2]; % decrease
[maxAmp, maxInd] = min(statMeans(oneSampleInd));
tuneAmplitude = [tuneAmplitude; maxAmp];
tunedAngleInd = oneSampleInd(maxInd); % for readability
tuneAngle = [tuneAngle; angles(tunedAngleInd)];
tempTimeseries = groupAverageSpk(tunedAngleInd,:);
mt = find(tempTimeseries < min(tempTimeseries)*0.9,1);
else % error
tuneDirection = [tuneDirection; 0]; % error
tuneAmplitude = [tuneAmplitude; 0];
tuneAngle = [tuneAngle; 0];
mt = -1;
end
tuneMaxResponseTimepoint = [tuneMaxResponseTimepoint; mt / frameRate];
% Response probability
spktuned = spk{tunedAngleInd}(:,baseFrameNum+1 : end); % for readability
responseNum = 0;
for ri = 1:size(spktuned,1)
tempTS = spktuned(ri,:);
tempTS = tempTS(~isnan(tempTS));
[~, p] = ttest(tempTS');
if p < thresholdTtestResponse
if (statMeans(tunedAngleInd) > tempThreshold(1) && mean(tempTS) > tempThreshold(1)) || ...
(statMeans(tunedAngleInd) < tempThreshold(2) && mean(tempTS) < tempThreshold(2))
responseNum = responseNum + 1;
end
end
end
tuneResponseProb = [tuneResponseProb; responseNum/ri*100];
% Response reliability
template = nanmean(spk{tunedAngleInd}(:,baseFrameNum:end)); % include the last baseline frames
rho = zeros(size(spktuned,1),1);
for ri = 1 : length(rho)
tempTS = spk{tunedAngleInd}(ri,baseFrameNum:end);
inds = find(~isnan(tempTS));
rho(ri) = corr(template(inds)',tempTS(inds)');
end
tuneReliability = [tuneReliability; mean(rho)];
% Categorization
ind__1 = find(pairComp(:,1) == tunedAngleInd);
ind__2 = find(pairComp(:,2) == tunedAngleInd);
testInd = union(ind__1, ind__2);
insigInd = find(pairComp(testInd,6) >= thresholdCategory);
sigInd = find(pairComp(testInd,6) < thresholdCategory);
temp = pairComp(testInd(insigInd),1:2);
insigIndGroup = unique(temp(:)); % sorted. Include tunedAngleInd, except when there's nothing
if isempty(insigIndGroup)
tuneSharpness = [tuneSharpness; 1];
tuneSingle = [tuneSingle; cellNum];
else
broadInd = intersect(oneSampleInd,insigIndGroup);
if length(broadInd) < 2
tuneSharpness = [tuneSharpness; 1];
tuneSingle = [tuneSingle; cellNum];
else
tuneSharpness = [tuneSharpness; length(broadInd)];
broadNum = 1;
for tunei = tunedAngleInd-1:-1:1
if ismember(tunei, broadInd)
broadNum = broadNum + 1;
else
break
end
end
for tunei = tunedAngleInd+1:length(angles)
if ismember(tunei, broadInd)
broadNum = broadNum + 1;
end
end
if broadNum > 2
tuneBroad = [tuneBroad; cellNum];
end
end
end
temp = pairComp(testInd(sigInd),1:2);
sigIndGroup = setdiff(temp(:), tunedAngleInd); % exclude tunedAngleInd. Any index that is significantly different from the tuned angle index.
if ~isempty(find(diff(insigIndGroup)>1,1))
if sum(tempH(sigIndGroup))
tuneMM = [tuneMM; cellNum]; % multimodal. Including bipolar.
end
if length(sigIndGroup) == 1 && ... % only one bin is significantly different from the tuned bin. (can't be larger in response because of the way tuned bin is defined)
all(tempH(insigIndGroup)) % and all insignicant indices are different from 0
tuneLOO = [tuneLOO; cellNum]; % leave-one-out. Part of multimodal in definition.
end
end
center = (length(angles)+1) / 2;
compInd = union(find(pairComp(:,1) == tunedAngleInd), find(pairComp(:,2) == tunedAngleInd));
indMat = pairComp(compInd,1:2);
if tunedAngleInd < center
withinInd = unique(mod( setdiff( find(indMat < center), find(indMat == tunedAngleInd) ) , size(indMat,1)));
withinInd(withinInd==0) = size(indMat,1);
betweenInd = unique(mod( find(indMat > center) , size(indMat,1) ));
betweenInd(betweenInd==0) = size(indMat,1);
else
withinInd = unique(mod( setdiff( find(indMat > center), find(indMat == tunedAngleInd) ) , size(indMat,1)));
withinInd(withinInd==0) = size(indMat,1);
betweenInd = unique(mod( find(indMat < center) , size(indMat,1) ));
betweenInd(betweenInd==0) = size(indMat,1);
end
if isempty(find(pairComp(compInd(withinInd),6) < thresholdCategory, 1)) && ... % nothing within the same half is different from the max ind
isempty(find(pairComp(compInd(betweenInd),6) >= thresholdCategory, 1)) % nothing between different half is same with the max ind
tuneCateg = [tuneCateg; cellNum] ; % categorical (>= 90 or <= 90)
end
if isempty(find(diff(sign(diff(statMeans))),1)) % everything is going up or down
tuneRampStrict = [tuneRampStrict; cellNum]; % ramping up or down
end
elseif responseP(cellid) < thresholdResponseTotal
if meanTotal(cellid) > tempThreshold(1) || meanTotal(cellid) < tempThreshold(2)
spkAll = cell2mat(cellfun(@(x) x, spk, 'uniformoutput', false));
cellsNTResponse = [cellsNTResponse; cellNum];
tempTimeseries = nanmean(spkAll(:,baseFrameNum+frames));
NTRAmplitude = [NTRAmplitude; abs(mean(timeAverageSpk))];
if meanTotal(cellid) > tempThreshold(1)
NTRdirection = [NTRdirection; 1]; % increase
mt = find(tempTimeseries > max(tempTimeseries)*0.9,1);
NTRMaxResponseTimepoint = [NTRMaxResponseTimepoint; mt / frameRate];
elseif meanTotal(cellid) < tempThreshold(2)
NTRdirection = [NTRdirection; 2]; % decrease
mt = find(tempTimeseries < min(tempTimeseries)*0.9,1);
NTRMaxResponseTimepoint = [NTRMaxResponseTimepoint; mt / frameRate];
else
NTRdirection = [NTRdirection; 0]; % error
NTRMaxResponseTimepoint = [NTRMaxResponseTimepoint; NaN];
end
% Response probability
responseNum = 0;
for ri = 1 : size(spkAll,1)
tempTS = spkAll(ri,baseFrameNum + frames);
tempTS = tempTS(~isnan(tempTS));
[~, p] = ttest(tempTS');
if p < thresholdTtestResponse
if (mean(timeAverageSpk) > tempThreshold(1) && mean(tempTS) > tempThreshold(1)) || ...
(mean(timeAverageSpk) < tempThreshold(2) && mean(tempTS) < tempThreshold(2))
responseNum = responseNum + 1;
end
end
end
NTRresponseProb = [NTRresponseProb; responseNum/ri*100];
% Response reliability
template = nanmean(spkAll(:,baseFrameNum:end));
rho = zeros(size(spkAll,1),1);
for ri = 1 : size(spkAll,1)
tempTS = spkAll(ri,baseFrameNum:end);
inds = find(~isnan(tempTS));
rho(ri) = corr(template(inds)', tempTS(inds)');
end
NTRreliability = [NTRreliability; mean(rho)];
else
cellsNTNR = [cellsNTNR; cellNum];
end
else
cellsNTNR = [cellsNTNR; cellNum];
end
else
if responseP(cellid) < thresholdResponseTotal
if meanTotal(cellid) > tempThreshold(1) || meanTotal(cellid) < tempThreshold(2)
spkAll = cell2mat(cellfun(@(x) x, spk, 'uniformoutput', false));
cellsNTResponse = [cellsNTResponse; cellNum];
tempTimeseries = nanmean(spkAll(:,baseFrameNum+frames));
NTRAmplitude = [NTRAmplitude; abs(mean(timeAverageSpk))];
if meanTotal(cellid) > tempThreshold(1)
NTRdirection = [NTRdirection; 1]; % increase
mt = find(tempTimeseries > max(tempTimeseries)*0.9,1);
NTRMaxResponseTimepoint = [NTRMaxResponseTimepoint; mt / frameRate];
elseif meanTotal(cellid) < tempThreshold(2)
NTRdirection = [NTRdirection; 2]; % decrease
mt = find(tempTimeseries < min(tempTimeseries)*0.9,1);
NTRMaxResponseTimepoint = [NTRMaxResponseTimepoint; mt / frameRate];
else
NTRdirection = [NTRdirection; -1]; % another kind of error
NTRMaxResponseTimepoint = [NTRMaxResponseTimepoint; NaN];
end
% Response probability
responseNum = 0;
for ri = 1 : size(spkAll,1)
tempTS = spkAll(ri,baseFrameNum + frames);
tempTS = tempTS(~isnan(tempTS));
[~, p] = ttest(tempTS');
if p < thresholdTtestResponse
if (mean(timeAverageSpk) > tempThreshold(1) && mean(tempTS) > tempThreshold(1)) || ...
(mean(timeAverageSpk) < tempThreshold(2) && mean(tempTS) < tempThreshold(2))
responseNum = responseNum + 1;
end
end
end
NTRresponseProb = [NTRresponseProb; responseNum/ri*100];
% Response reliability
template = nanmean(spkAll(:,baseFrameNum:end));
rho = zeros(size(spkAll,1),1);
for ri = 1 : size(spkAll,1)
tempTS = spkAll(ri,baseFrameNum:end);
inds = find(~isnan(tempTS));
rho(ri) = corr(template(inds)', tempTS(inds)');
end
NTRreliability = [NTRreliability; mean(rho)];
else
cellsNTNR = [cellsNTNR; cellNum];
end
else
cellsNTNR = [cellsNTNR; cellNum];
end
end
oneSampleH(cellid,:) = tempH;
end
noise = u.noise;
celly = u.celly;
cellx = u.cellx;
c2ypoints = u.c2ypoints;
c2xpoints = u.c2xpoints;
fovsize = u.fovsize;
fovxrange = u.fovxrange;
fovyrange = u.fovyrange;
fovdepth = u.fovdepth;
save(savefn, 'cell*','tune*','NTR*', 'spkTotal', '*ctype', 'angles', 'baseFrameNum', 'afterFrameNum', 'threshold*', ...
'excludeDrinkingTime', 'onlyBeforeDecision', 'onlyAfterDecision', 'allowOverlap', 'onlyFirstTouch','frameRate', 'oneSampleH', '*P', 'noise', 'c2*points', 'fov*')
% end
end
end