-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFigure4.m
1790 lines (1392 loc) · 63.6 KB
/
Figure4.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
baseDir = 'D:\TPM\JK\Pub_S1AngleCode\'; % The folder containing folders of data ('\Behavior', '\Calcium', '\Whisker') and dependent codes ('\MATLAB codes')
%% basic settings
calciumDir = [baseDir, 'Calcium\'];
mouse = 39;
session = 1;
mice = [25,27,30,36,37,38,39,41,52,53,54,56];
sessions = {[4,19],[3,10],[3,21],[1,17],[7],[2],[1,23],[3],[3,21],[3],[3],[3]};
naiveInd = find(mice == mouse);
repeat = 10;
% %% dependent settings
ufn = sprintf('UberJK%03dS%02d_NC',mouse, session);
glmfnBase = sprintf('glmResponseType_JK%03dS%02d_lasso_NC_R', mouse, session);
angletuningFn = sprintf('JK%03dS%02dangle_tuning_lasso_predecision_NC_permTestCorrected', mouse, session);
% %% load files
load([calciumDir, 'glmResults_devExp_touch_NC'], 'naive', 'expert')
% cd(sprintf('%s%03d',baseDir, mouse))
load(sprintf('%s%03d\\%s',calciumDir, mouse, ufn), 'u') % loading u
load(sprintf('%s%03d\\%s01',calciumDir, mouse, glmfnBase), 'allPredictors', 'posShift');
load(sprintf('%s%03d\\%s',calciumDir, mouse, angletuningFn))
touch = load([calciumDir, 'glmResults_devExp_touch_NC']);
wkv = load([calciumDir, 'glmResults_devExp_WKV_touchCell_NC']);
%%
%% Fig 5A
%%
%% Figure of Whisker GLM
cID = 5087;
wkvglmfnBase = sprintf('%s%03d\\glmWhisker_lasso_touchCell_NC_JK%03dS%02d_R', calciumDir, mouse, mouse, session);
load([wkvglmfnBase, '01'], 'allPredictors', 'posShift', 'cIDAll', 'fitCoeffs', 'indPartial')
ci = find(cIDAll == cID);
wkv.naive(7).allDE(ci)
testCoeff = zeros(size(fitCoeffs{ci},1),10);
for i = 1 : 10
load([wkvglmfnBase,sprintf('%02d',i)], 'fitCoeffs')
testCoeff(:,i) = fitCoeffs{ci};
end
traces = get_traces_per_cell(u, cID, allPredictors, testCoeff, posShift);
figure('units', 'normalized', 'outerposition', [0.3 0.3 0.4 0.4]), hold on
plot(min_max_normalization(traces.calcium)*4 + 8 , 'color', [0.1 0.8 0.1], 'linewidth', 1)
plot(min_max_normalization(traces.spikes)*2.5 + 6.2, 'color', [0.8 0.1 0.1], 'linewidth', 1)
plot(min_max_normalization(traces.model)*2.5 + 6.2, 'k-', 'linewidth', 1)
plot(min_max_normalization(traces.predictors(:,10)) + 4.4, 'color', 'g', 'linewidth', 1) % dKv
plot(min_max_normalization(traces.predictors(:,4)) + 3.4, 'color', 'r', 'linewidth', 1) % dPhi
plot(min_max_normalization(traces.predictors(:,13)) + 3.6, 'color', 'b', 'linewidth', 1) % slide distance
plot(min_max_normalization(traces.predictors(:,51)) + 2.5, 'color', [0.7 0.7 0.7], 'linewidth', 1)
plot(min_max_normalization(traces.predictors(:,58)) + 1.8, 'color', [0.7 0.7 0.7], 'linewidth', 1)
plot(min_max_normalization(traces.predictors(:,65)) + 1.1, 'color', [0.7 0.7 0.7], 'linewidth', 1)
lickL = min_max_normalization(traces.predictors(:,72));
lickR = min_max_normalization(traces.predictors(:,76));
plot(lickL, 'm', 'linewidth', 1)
plot(lickR, 'c', 'linewidth', 1)
rewardL = min_max_normalization(traces.predictors(:,41));
rewardL(rewardL<max(rewardL)) = 0;
rewardL(isnan(rewardL)) = 0;
rewardR = min_max_normalization(traces.predictors(:,46));
rewardR(rewardR<max(rewardR)) = 0;
rewardR(isnan(rewardR)) = 0;
reward = rewardL + rewardR;
rewarded = find(reward);
rdiffOne = find(diff(find(reward))==1);
reward(rewarded(rdiffOne+1)) = nan;
reward(reward==0) = nan;
sound = min_max_normalization(traces.predictors(:,37));
sound(sound<max(sound)) = nan;
plot(reward-1.5, '^', 'markersize', 4, 'markeredgecolor', 'none', 'markerfacecolor', 'b')
plot(sound-1.5, '^', 'markersize', 4, 'markeredgecolor', 'none', 'markerfacecolor', 'k')
onedF = 4 / max(traces.calcium);
plot([930 930], [10 10+onedF], 'color', [0.1 0.8 0.1], 'linewidth', 1) % 1 dF/F0 scale bar
plot([900 900 + u.frameRate*5], [10 10], 'k-', 'linewidth', 1) % 5 s scale bar
xlim([240 940]), ylim([-0.5 12])
axis off
set(gcf, 'InvertHardCopy', 'off', 'color', 'w');
%%
%%
%%
%%
%% S4A
%%
%% object model GOF - sensorimotor model GOF
touch = load([calciumDir, 'glmResults_devExp_touch_NC']);
wkv = load([calciumDir, 'glmResults_devExp_WKV_touchCell_NC']);
expertInd = [1:4,7,9];
%% all naive
diffDE = cell(length(wkv.naive),1);
for i = 1 : length(wkv.naive)
tempCID = wkv.naive(i).cellID;
pfTemp = zeros(length(tempCID),1); % parfor temp
for ci = 1 : length(tempCID)
wkvInd = find(wkv.naive(i).cellID == tempCID(ci));
touchInd = find(touch.naive(i).cellID == tempCID(ci)); % this way is safer than assuming sorted
pfTemp(ci) = touch.naive(i).allDE(touchInd) - wkv.naive(i).allDE(wkvInd);
end
diffDE{i} = pfTemp;
end
histRange = [-0.15:0.01:0.25];
pdfDiff = zeros(length(diffDE), length(histRange)-1);
for i = 1 : length(diffDE)
pdfDiff(i,:) = histcounts(diffDE{i}, histRange, 'normalization', 'probability');
end
figure, hold on
boundedline(histRange(1:end-1), mean(pdfDiff), std(pdfDiff), 'cmap', [0 0 0])
graphMaxVal = max(mean(pdfDiff) + std(pdfDiff));
graphMean = mean(cell2mat(diffDE))
graphStd = std(cell2mat(diffDE))
ylim([0 0.2])
yval = ylim();
errorbar(graphMean, graphMaxVal + (yval(2)-graphMaxVal)/2, graphStd, 'ro-','horizontal')
plot([0.1 0.1], yval, '--', 'color', [0.7 0.7 0.7])
plot([-0.1 -0.1], yval, '--', 'color', [0.7 0.7 0.7])
xlabel('Difference between models')
ylabel('Proportion')
set(gca,'fontsize',12,'fontname','Arial')
%%
%% S4B
%%
%% compare angle-tuning correlation in angle-tuned neurons
% between full whisker model and whisker-only model
%% naive all
%
% featureNames = {'maxDq', 'maxDf', 'maxDkH', 'maxDkV', 'max(slide distance)', 'max(duration)', ...
% 'q', 'f', 'kH', 'kV', 'arc length', 'touch count'};
loadFn = 'wkv_angle_tuning_model_v9';
load(sprintf('%s%s',calciumDir, loadFn));
angles = 45:15:135;
learnerInd = [1:4,7,9];
% correct for JK027 when comparing between naive and expert (n = 6)
naiveLearner = naive(learnerInd);
jk027inds = find(naiveLearner(2).cIDAll > 5000);
jk027AllLength = length(naiveLearner(2).cIDAll);
fn = fieldnames(naiveLearner);
for i = 1 : length(fn)
if length(naiveLearner(2).(fn{i})) == jk027AllLength
naiveLearner(2).(fn{i}) = naiveLearner(2).(fn{i})(jk027inds);
end
end
atCorr = cell(length(naive),2); %(:,1) full model, (:,2) whisker-only model
histRange = 0:0.02:1;
distCorrFull = zeros(length(naive),length(histRange)-1);
distCorrWhisker = zeros(length(naive),length(histRange)-1);
for mi = 1 : length(naive)
ind = find(naive(mi).tuned);
fullCorrMat = cell2mat(naive(mi).atCorrFull(ind));
atCorr{mi,1} = fullCorrMat(:,1);
whiskerCorrMat = cell2mat(naive(mi).atCorrWhisker(ind));
atCorr{mi,2} = whiskerCorrMat(:,1);
distCorrFull(mi,:) = histcounts(atCorr{mi,1}, histRange, 'norm', 'prob');
distCorrWhisker(mi,:) = histcounts(atCorr{mi,2}, histRange, 'norm', 'prob');
end
%
figure, hold on
plot(histRange(2:end), mean(distCorrFull), 'k')
plot(histRange(2:end), mean(distCorrWhisker), 'c')
legend({'Full model', 'Whisker-only model'}, 'autoupdate', false, 'location', 'northwest')
boundedline(histRange(2:end), mean(distCorrFull), sem(distCorrFull), 'k')
boundedline(histRange(2:end), mean(distCorrWhisker), sem(distCorrFull), 'c')
xlabel('Correlation')
ylabel('Proportion')
title({'Naive all (n = 12)'; sprintf('Full model mean %.2f \\pm %.2f', ...
mean(cellfun(@mean, atCorr(:,1))), sem(cellfun(@mean, atCorr(:,1)))) ;
sprintf('Whisker-only model mean %.2f \\pm %.2f', ...
mean(cellfun(@mean, atCorr(:,2))), sem(cellfun(@mean, atCorr(:,2))))})
%%
%% 4B - example plots for impact on angle tuning
%%
load([calciumDir, 'wkv_angle_tuning_model_v9'], 'naive');
mouse = naive(7);
%%
angles = 45:15:135;
tempInd = find(mouse.cIDAll == 5104); % 5104
% cID = cellIDs(tempInd);
spikesTemp = (cellfun(@mean, mouse.spikeAngleAll{tempInd,1}));
whiskerTemp = (cellfun(@mean, mouse.whiskerOnlyAngleAll{tempInd}(1,:)));
woDkVTemp = (cellfun(@mean, mouse.whiskerOnlyAngleAll{tempInd}(5,:)));
woSDTemp = (cellfun(@mean, mouse.whiskerOnlyAngleAll{tempInd}(6,:)));
spikes = (spikesTemp-mean(spikesTemp)) / std(spikesTemp);
whiskerModel = (whiskerTemp-mean(whiskerTemp)) / std(whiskerTemp);
woDkV = (woDkVTemp-mean(woDkVTemp)) / std(woDkVTemp);
woSD = (woSDTemp-mean(woSDTemp)) / std(woSDTemp);
figure, hold on
plot(angles, spikes, 'ro-', 'MarkerFaceColor','r', 'linewidth', 1);
ylabel('Response (standardized)')
plot(angles, whiskerModel, 'o-', 'color',[0.6 0.6 0.6], 'linewidth', 3);
plot(angles, woDkV, 'go--', 'linewidth', 1)
plot(angles, woSD, 'bo--', 'linewidth', 1)
xticks(angles)
xlabel('Object angle (\circ)')
legend({'Inferred spikes', 'Full model', '-maxDkV', '-max(slide distance)'}, 'location', 'northwest', 'box', 'off')
%%
%% 4B (top right)
%%
%%
corrVal = zeros(1,3);
corrVal(1) = mouse.atCorrWhisker{tempInd}(1);
corrVal(2) = mouse.atCorrWhisker{tempInd}(5);
corrVal(3) = mouse.atCorrWhisker{tempInd}(6);
figure, hold on
bar(1, corrVal(1), 'k', 'baseValue', -1)
bar(2, corrVal(2), 'g', 'baseValue', -1)
bar(3, corrVal(3), 'b', 'baseValue', -1)
yticks([-1:0.5:1])
ylabel('Correlation')
xticks([1:3])
xticklabels({'Full model', '-vertical distance', '-slide distance'})
xtickangle(45)
%% confirmation
corrVal = zeros(1,3);
corrVal(1) = corr(spikesTemp', whiskerTemp');
corrVal(2) = corr(spikesTemp', woDkVTemp');
corrVal(3) = corr(spikesTemp', woSDTemp');
figure, hold on
bar(1, corrVal(1), 'k', 'baseValue', -1)
bar(2, corrVal(2), 'g', 'baseValue', -1)
bar(3, corrVal(3), 'b', 'baseValue', -1)
ylabel('Correlation')
xticks([1:3])
xticklabels({'Full model', '-maxDkV', '-max(slide distance)'})
xtickangle(45)
%%
%% 4B (bottome left)
%%
%% listening to slide distance, but not to dKv
tempInd = find(mouse.cIDAll == 5326);
spikesTemp = (cellfun(@mean, mouse.spikeAngleAll{tempInd,1}));
whiskerTemp = (cellfun(@mean, mouse.whiskerOnlyAngleAll{tempInd}(1,:)));
woDkVTemp = (cellfun(@mean, mouse.whiskerOnlyAngleAll{tempInd}(5,:)));
woSDTemp = (cellfun(@mean, mouse.whiskerOnlyAngleAll{tempInd}(6,:)));
spikes = (spikesTemp-mean(spikesTemp)) / std(spikesTemp);
whiskerModel = (whiskerTemp-mean(whiskerTemp)) / std(whiskerTemp);
woDkV = (woDkVTemp-mean(woDkVTemp)) / std(woDkVTemp);
woSD = (woSDTemp-mean(woSDTemp)) / std(woSDTemp);
figure, hold on
plot(angles, spikes, 'ro-', 'MarkerFaceColor','r', 'linewidth', 1);
ylabel('Response (standardized)')
plot(angles, whiskerModel, 'o-', 'color',[0.6 0.6 0.6], 'linewidth', 3);
plot(angles, woDkV, 'go--', 'linewidth', 1)
plot(angles, woSD, 'bo--', 'linewidth', 1)
xticks(angles)
xlabel('Object angle (\circ)')
legend({'Inferred spikes', 'Full model', '-maxDkV', '-max(slide distance)'}, 'location', 'northwest', 'box', 'off')
%%
%% 4B (bottom right)
%%
%%
corrVal = zeros(1,3);
corrVal(1) = corr(spikesTemp', whiskerTemp');
corrVal(2) = corr(spikesTemp', woDkVTemp');
corrVal(3) = corr(spikesTemp', woSDTemp');
figure, hold on
bar(1, corrVal(1), 'k', 'baseValue', -1)
bar(2, corrVal(2), 'g', 'baseValue', -1)
bar(3, corrVal(3), 'b', 'baseValue', -1)
yticks([-1:0.5:1])
ylabel('Correlation')
xticks([1:3])
xticklabels({'Full model', '-vertical bending', '-slide distance'})
xtickangle(45)
%%
%% 4C - Impact on angle tuning by each features from whisker-only models
%%
impactWhiskerNaiveAll = cell(length(naive),1);
for mi = 1 : length(naive)
tunedInd = find(naive(mi).tuned);
tempMat = cell2mat(naive(mi).atCorrWhisker(tunedInd));
impactWhiskerNaiveAll{mi} = tempMat(:,1) - tempMat(:,2:end);
end
featureXpos = [8,9,10,11,13,12,1,2,3,4,5,6];
figure, hold on
tempMatWhisker = cell2mat(cellfun(@nanmean, impactWhiskerNaiveAll, 'un', 0));
bar(featureXpos, mean(tempMatWhisker), 'facecolor', 'k')
errorbar(featureXpos, mean(tempMatWhisker), sem(tempMatWhisker), 'k', 'lines', 'no')
ylabel('Impact on tuning')
ylim([-0.02 0.2]), yticks([0:0.05:0.2])
title('Naive all (n = 12)')
xticks([1:6, 8:13])
xticklabels({'Azimuthal angle', 'Vertical angle', 'Horizontal curvature', 'Vertical curvature', 'Arc length', 'Touch count', ...
'Azimuthal push angle', 'Vertical displacement', 'Horizontal bending', 'Vertical bending', 'Touch duration', 'Slide distance'})
xtickangle(45)
%% (Impact on angle tuning by each features from whisker-only models)
%% (expert mice)
impactWhiskerNaiveAll = cell(length(expert),1);
for mi = 1 : length(expert)
tunedInd = find(expert(mi).tuned);
tempMat = cell2mat(expert(mi).atCorrWhisker(tunedInd));
impactWhiskerNaiveAll{mi} = tempMat(:,1) - tempMat(:,2:end);
end
featureXpos = [8,9,10,11,13,12,1,2,3,4,5,6];
figure, hold on
tempMatWhisker = cell2mat(cellfun(@nanmean, impactWhiskerNaiveAll, 'un', 0));
bar(featureXpos, mean(tempMatWhisker), 'facecolor', 'k')
errorbar(featureXpos, mean(tempMatWhisker), sem(tempMatWhisker), 'k', 'lines', 'no')
ylabel('Impact on tuning')
ylim([-0.02 0.2]), yticks([0:0.05:0.2])
title('Expert (n = 6)')
xticks([1:6, 8:13])
xticklabels({'Azimuthal angle', 'Vertical angle', 'Horizontal curvature', 'Vertical curvature', 'Arc length', 'Touch count', ...
'Azimuthal push angle', 'Vertical displacement', 'Horizontal bending', 'Vertical bending', 'Touch duration', 'Slide distance'})
xtickangle(45)
%% Fig 4D
%% Impact of dynamics whisker features on angle tuning
%% across tuned angles (defined from inferred spikes)
atCorrThreshold = 0.1;
sortedFeatureImportance = cell(length(naive), 2); %(:,1) from full model, (:,2) from whisker-only model
sortedFeatureInd = cell(length(naive),2);
cellDepths = cell(length(naive),1);
cellTunedAngle = cell(length(naive),2); %(:,1) from spikes, (:,2) from whisker-only model
for mi = 1 : length(naive)
tempInd = find(naive(mi).tuned);
cellDepths{mi} = naive(mi).depth(tempInd);
tempSpikeAll = cell2mat(naive(mi).atSpikeAll(tempInd));
[~, tempAngleInd] = max(tempSpikeAll, [], 2);
cellTunedAngle{mi,1} = 30 + tempAngleInd * 15;
tempWhiskerAll = cell2mat(cellfun(@(x) x(1,:), naive(mi).atWhiskerAll(tempInd), 'un', 0));
[~, tempAngleInd] = max(tempWhiskerAll, [], 2);
cellTunedAngle{mi,2} = 30 + tempAngleInd * 15;
tempMat = cell2mat(naive(mi).atCorrFull(tempInd));
[sortedFeatureImportance{mi,1}, sortedFeatureInd{mi,1}] = sort(tempMat(:,1) - tempMat(:,2:end),2, 'descend');
tempMat = cell2mat(naive(mi).atCorrWhisker(tempInd));
[sortedFeatureImportance{mi,2}, sortedFeatureInd{mi,2}] = sort(tempMat(:,1) - tempMat(:,2:end),2, 'descend');
end
impactFeature = zeros(length(naive), 12, length(angles));
propFeature = zeros(length(naive), 12, length(angles));
for mi = 1 : length(naive)
tempInd = find(naive(mi).tuned);
tunedAngleInd = cellfun(@(x) find(x == max(x)), naive(mi).atSpikeAll(tempInd));
for ai = 1 : length(angles)
angleInd = find(tunedAngleInd == ai);
% proportion (Whisker-only)
tempMat = sortedFeatureImportance{mi,2}(angleInd,:);
tempMatInd = find(tempMat(:) > atCorrThreshold);
featureIndexMat = sortedFeatureInd{mi,2}(angleInd,:);
allIndices = featureIndexMat(tempMatInd);
for fi = 1 : 12
propFeature(mi,fi,ai) = length(find(allIndices == fi)) / length(angleInd);
end
% mean impact of each feature from multi feature neurons (Whisker-only)
for fi = 1 : 12
tempImpact = zeros(length(angleInd),1);
for i = 1 : length(angleInd)
tempImpactVec = sortedFeatureImportance{mi,2}(angleInd(i),:);
tempIndVec = sortedFeatureInd{mi,2}(angleInd(i),:);
tempImpact(i) = tempImpactVec(find(tempIndVec == fi));
end
impactFeature(mi,fi,ai) = mean(tempImpact);
end
end
end
figure('units','inch','pos',[2 1 3 6]),
for ai = 1 : length(angles)
subplot(7,1,ai), hold on
tempMat = squeeze(impactFeature(:,[1,2,3,4,6,5],ai));
bar(nanmean(tempMat), 'facecolor', 'k')
errorbar(nanmean(tempMat), sem(tempMat), 'k', 'lines', 'no')
if ai == 4
ylabel('Mean impact on tuning')
end
if ai < length(angles)
xticks([])
else
xticks(1:6)
xticklabels({'Push angle', 'Vertical displacement', 'Horizontal bending', ...
'Vertical bending', 'Touch duration', 'Slide distance'})
xtickangle(45)
end
ylim([0 0.35])
xlim([0.5 6.5])
end
sgtitle('Naive all (n = 12)')
%%
%% Fig 5C (top left)
%%
%% Show which features affect the correlation the most.
loadFn1 = 'modelAngleTuning_NC';
loadFn2 = 'modelAngleTuning_NC_combinations';
data1 = load(sprintf('%s%s',calciumDir, loadFn1), 'naive', 'expert'); %
data2 = load(sprintf('%s%s',calciumDir, loadFn2), 'naive', 'expert'); %
numMiceNaive = length(data1.naive);
numMiceExpert = length(data1.expert);
% whiskerGLM = load('Y:\Whiskernas\JK\suite2p\glmResults_devExp_WKV_touchCell_NC.mat', 'naive');
%% listening to dKv, but not to slide distance
%%
%% Fig 5D
%%
%% map position
% load u first
%% Whisker feature-angle tuning impact map
baseDir = 'D:\TPM\JK\suite2p\';
% baseDir = 'Y:\Whiskernas\JK\suite2p\';
mouse = 39;
session = 1;
plane = 5;
load(sprintf('%s%03d\\angle_tuning_model_lasso_NC_preAnswer_perTouch_JK%03dS%02d.mat', baseDir, mouse, mouse, session), 'spkValAllCell', 'tuneAngleAllCell')
ufn = sprintf('%s%03d\\UberJK%03dS%02d_NC', baseDir, mouse, mouse, session);
load(ufn, 'u') % loading u
load(sprintf('%s%03d\\JK%03dS%02dangle_tuning_lasso_preAnswer_perTouch_spkOnly_NC', baseDir, mouse, mouse,session), 'spk', 'info')
%%
numFeature = 12;
corrFeaturesOutNaive = cell(1,length(spkValAllCell));
for ci = 1 : length(spkValAllCell)
corrFeaturesOutNaive{ci} = zeros(1,numFeature+1);
if isfinite(tuneAngleAllCell(ci,1)) % only in tuned neurons, because not-tuned neurons have NaN angle
tempVal = corr(cellfun(@mean, spkValAllCell{(ci),1}), cellfun(@mean, spkValAllCell{(ci),3}));
if isnan(tempVal) || tempVal < 0
tempVal = 0;
end
corrFeaturesOutNaive{ci}(1) = tempVal;
for fi = 1 : numFeature
tempVal = corr(cellfun(@mean, spkValAllCell{(ci),1}), cellfun(@mean, spkValAllCell{(ci),3+fi}));
if isnan(tempVal) || tempVal < 0
tempVal = 0;
end
corrFeaturesOutNaive{ci}(fi+1) = corrFeaturesOutNaive{ci}(1) - tempVal;
end
end
end
maxCorrVal = cellfun(@(x) max(x(2:end)), corrFeaturesOutNaive, 'un', 0);
maxCorrInd = cellfun(@(x,y) find(ismember(x(2:end),y),1), corrFeaturesOutNaive, maxCorrVal);
colors = jet(7); % because we know there is only up to 7 different features (differ between examples)
mimg = (mat2gray(u.mimg{plane}));
h = figure;
imshow((mimg))
hold on,
plot([u.c2xpoints, u.c2xpoints(1)], [u.c2ypoints, u.c2ypoints(1)], 'w--', 'linewidth', 2)
cIDlist = spk.touchID(find(spk.touchID > plane * 1000 & spk.touchID < (plane+1) * 1000));
ciList = find(spk.touchID > plane * 1000 & spk.touchID < (plane+1) * 1000);
tunedAngles = spk.tunedAngle(find(spk.touchID > plane * 1000 & spk.touchID < (plane+1) * 1000));
indList = find(ismember(u.cellNums, cIDlist));
cellmap = u.cellmap{plane};
maxCorrIndsMap = zeros(length(cIDlist),1);
for ci = 1 : length(cIDlist)
cID = cIDlist(ci);
[ypix,xpix] = ind2sub(size(cellmap),find(cellmap == cID));
k = boundary(ypix, xpix);
if tunedAngles(ci)
if maxCorrVal{ciList(ci)} < 0.1
patch(xpix(k), ypix(k), 'k', 'edgecolor', 'none')
else
tempFeatureInd = maxCorrInd(ciList(ci)); % from 1 to 12
patch(xpix(k), ypix(k), colors(tempFeatureInd,:), 'edgecolor', 'none')
maxCorrIndsMap(ci) = tempFeatureInd;
end
end
end
% scale bar edge. scale bar length 100 um
scaleBarPix = 100 / u.pixResolution;
margins = 20; % 20 pixels each away from right bottom corner
sizes = size(u.mimg{plane});
plot([sizes(2)-margins-scaleBarPix, sizes(2)-margins], [sizes(1) - margins, sizes(1) - margins], 'w-', 'linewidth', 4)
expectedTextLength = 120;
text(sizes(2)-expectedTextLength, margins, [num2str(-round(mean(mean(u.fovdepth{plane})))), ' \mum'], 'fontsize', 18, 'fontweight', 'bold', 'color', 'w')
set(gcf, 'InvertHardCopy', 'off', 'color', 'w');
set(gca, 'fontsize', 12, 'fontname', 'Arial')
imcontrast(h)
%%
% saveDir = 'C:\Users\jinho\Dropbox\Works\Manuscripts\Object Angle Coding in vS1\Figures\Fig5 Sensory input for angle tuning\';
% fn = sprintf('example_FOV(JK%03dS%02dp%d)_whiskerImpactMap.eps', mouse, session, plane);
% export_fig([saveDir, fn], '-depsc', '-painters', '-r600', '-transparent')
% fix_eps_fonts([saveDir, fn])
% and then...
cmtemp = zeros(size(u.cellmap{5}));
cmtemp(find(u.cellmap{5} == 5104)) = 1;
cmtemp(find(u.cellmap{5} == 5326)) = 2;
figure, imagesc(cmtemp)
%%
%% Fig 5E
%%
%% maxDkV and max(slide distance) affects the angle tuning the most.
%% but first off, how many of whisker models are angle-tuned?
tunedWhiskerModel = zeros(numMiceNaive,1);
for i = 1 : numMiceNaive
indTunedSpikes = find(data1.naive(i).tunedAllCell(:,1));
tunedWhiskerModel(i) = sum(data1.naive(i).tunedAllCell(indTunedSpikes,3)) / length(indTunedSpikes);
end
mean(tunedWhiskerModel)
sem(tunedWhiskerModel)
%%
tunedWhiskerModel = zeros(numMiceExpert,1);
for i = 1 : numMiceExpert
indTunedSpikes = find(data1.expert(i).tunedAllCell(:,1));
tunedWhiskerModel(i) = sum(data1.expert(i).tunedAllCell(indTunedSpikes,3)) / length(indTunedSpikes);
end
mean(tunedWhiskerModel)
sem(tunedWhiskerModel)
%%
%% touch, full, 12 drop-outs, others only (16 total)
corrTouchNaive = cell(numMiceNaive,1);
corrWhiskerNaive = cell(numMiceNaive,1);
corrIndsNaive = cell(numMiceNaive,1);
for i = 1 : numMiceNaive
indTuned = find(data1.naive(i).tunedAllCell(:,1));
indTemp = find(data1.naive(i).tunedAllCell(indTuned,2));
indTouch = indTuned(indTemp);
indTemp = find(data1.naive(i).tunedAllCell(indTuned,3));
indWhisker = indTuned(indTemp);
corrIndsNaive{i} = indWhisker;
corrTouchNaive{i} = zeros(length(indTouch),1);
corrWhiskerNaive{i} = zeros(length(indWhisker),1);
for j = 1 : length(indTouch)
tempVal = corr(cellfun(@mean, data1.naive(i).spkValAllCell{indTouch(j),1}), cellfun(@mean, data1.naive(i).spkValAllCell{indTouch(j),2}));
if isnan(tempVal)
tempVal = 0;
end
corrTouchNaive{i}(j) = tempVal;
end
for j = 1 : length(indWhisker)
tempVal = corr(cellfun(@mean, data1.naive(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data1.naive(i).spkValAllCell{indWhisker(j),3}));
if isnan(tempVal)
tempVal = 0;
end
corrWhiskerNaive{i}(j) = tempVal;
end
end
corrFeaturesOutNaive = cell(numMiceNaive,12);
for i = 1 : numMiceNaive
indTuned = find(data1.naive(i).tunedAllCell(:,1));
indTemp = find(data1.naive(i).tunedAllCell(indTuned,3));
indWhisker = indTuned(indTemp);
for fi = 1 : 12
corrFeaturesOutNaive{i,fi} = zeros(length(indWhisker),1);
for j = 1 : length(indWhisker)
tempVal = corr(cellfun(@mean, data1.naive(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data1.naive(i).spkValAllCell{indWhisker(j),3+fi}));
if isnan(tempVal)
tempVal = 0;
end
corrFeaturesOutNaive{i,fi}(j) = tempVal;
end
end
end
corrOtherNaive = cell(numMiceNaive,1);
for i = 1 : numMiceNaive
indTuned = find(data1.naive(i).tunedAllCell(:,1));
indTemp = find(data1.naive(i).tunedAllCell(indTuned,3));
indWhisker = indTuned(indTemp);
for j = 1 : length(indWhisker)
tempVal = corr(cellfun(@mean, data1.naive(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data2.naive(i).spkValAllCell{indWhisker(j), 1}));
if isnan(tempVal)
tempVal = 0;
end
corrOtherNaive{i}(j) = tempVal;
end
end
corrFeaturesCombOutNaive = cell(numMiceNaive,4);
corrFeaturesCombInNaive = cell(numMiceNaive,4);
for i = 1 : numMiceNaive
indTuned = find(data1.naive(i).tunedAllCell(:,1));
indTemp = find(data1.naive(i).tunedAllCell(indTuned,3));
indWhisker = indTuned(indTemp);
for fi = 1 : 4
corrFeaturesCombOutNaive{i,fi} = zeros(length(indWhisker),1);
corrFeaturesCombInNaive{i,fi} = zeros(length(indWhisker),1);
for j = 1 : length(indWhisker)
tempVal = corr(cellfun(@mean, data1.naive(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data2.naive(i).spkValAllCell{indWhisker(j),13+fi}));
if isnan(tempVal)
tempVal = 0;
end
corrFeaturesCombOutNaive{i,fi}(j) = tempVal;
tempVal = corr(cellfun(@mean, data1.naive(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data2.naive(i).spkValAllCell{indWhisker(j),21+fi}));
if isnan(tempVal)
tempVal = 0;
end
corrFeaturesCombInNaive{i,fi}(j) = tempVal;
end
end
end
corrFeaturesInNaive = cell(numMiceNaive,12);
for i = 1 : numMiceNaive
indTuned = find(data1.naive(i).tunedAllCell(:,1));
indTemp = find(data1.naive(i).tunedAllCell(indTuned,3));
indWhisker = indTuned(indTemp);
for fi = 1 : 12
corrFeaturesInNaive{i,fi} = zeros(length(indWhisker),1);
for j = 1 : length(indWhisker)
tempVal = corr(cellfun(@mean, data1.naive(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data2.naive(i).spkValAllCell{indWhisker(j),1+fi}));
if isnan(tempVal)
tempVal = 0;
end
corrFeaturesInNaive{i,fi}(j) = tempVal;
end
end
end
%% touch, full, 12 drop-outs, others only (16 total) - Expert
corrTouchExpert = cell(numMiceExpert,1);
corrWhiskerExpert = cell(numMiceExpert,1);
corrIndsExpert = cell(numMiceExpert,1);
for i = 1 : numMiceExpert
indTuned = find(data1.expert(i).tunedAllCell(:,1));
indTemp = find(data1.expert(i).tunedAllCell(indTuned,2));
indTouch = indTuned(indTemp);
indTemp = find(data1.expert(i).tunedAllCell(indTuned,3));
indWhisker = indTuned(indTemp);
corrIndsExpert{i} = indWhisker;
corrTouchExpert{i} = zeros(length(indTouch),1);
corrWhiskerExpert{i} = zeros(length(indWhisker),1);
for j = 1 : length(indTouch)
tempVal = corr(cellfun(@mean, data1.expert(i).spkValAllCell{indTouch(j),1}), cellfun(@mean, data1.expert(i).spkValAllCell{indTouch(j),2}));
if isnan(tempVal)
tempVal = 0;
end
corrTouchExpert{i}(j) = tempVal;
end
for j = 1 : length(indWhisker)
tempVal = corr(cellfun(@mean, data1.expert(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data1.expert(i).spkValAllCell{indWhisker(j),3}));
if isnan(tempVal)
tempVal = 0;
end
corrWhiskerExpert{i}(j) = tempVal;
end
end
corrFeaturesOutExpert = cell(numMiceExpert,12);
for i = 1 : numMiceExpert
indTuned = find(data1.expert(i).tunedAllCell(:,1));
indTemp = find(data1.expert(i).tunedAllCell(indTuned,3));
indWhisker = indTuned(indTemp);
for fi = 1 : 12
corrFeaturesOutExpert{i,fi} = zeros(length(indWhisker),1);
for j = 1 : length(indWhisker)
tempVal = corr(cellfun(@mean, data1.expert(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data1.expert(i).spkValAllCell{indWhisker(j),3+fi}));
if isnan(tempVal)
tempVal = 0;
end
corrFeaturesOutExpert{i,fi}(j) = tempVal;
end
end
end
corrOtherExpert = cell(numMiceExpert,1);
for i = 1 : numMiceExpert
indTuned = find(data1.expert(i).tunedAllCell(:,1));
indTemp = find(data1.expert(i).tunedAllCell(indTuned,3));
indWhisker = indTuned(indTemp);
for j = 1 : length(indWhisker)
tempVal = corr(cellfun(@mean, data1.expert(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data2.expert(i).spkValAllCell{indWhisker(j), 1}));
if isnan(tempVal)
tempVal = 0;
end
corrOtherExpert{i}(j) = tempVal;
end
end
corrFeaturesCombOutExpert = cell(numMiceExpert,4);
corrFeaturesCombInExpert = cell(numMiceExpert,4);
for i = 1 : numMiceExpert
indTuned = find(data1.expert(i).tunedAllCell(:,1));
indTemp = find(data1.expert(i).tunedAllCell(indTuned,3));
indWhisker = indTuned(indTemp);
for fi = 1 : 4
corrFeaturesCombOutExpert{i,fi} = zeros(length(indWhisker),1);
corrFeaturesCombInExpert{i,fi} = zeros(length(indWhisker),1);
for j = 1 : length(indWhisker)
tempVal = corr(cellfun(@mean, data1.expert(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data2.expert(i).spkValAllCell{indWhisker(j),13+fi}));
if isnan(tempVal)
tempVal = 0;
end
corrFeaturesCombOutExpert{i,fi}(j) = tempVal;
tempVal = corr(cellfun(@mean, data1.expert(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data2.expert(i).spkValAllCell{indWhisker(j),21+fi}));
if isnan(tempVal)
tempVal = 0;
end
corrFeaturesCombInExpert{i,fi}(j) = tempVal;
end
end
end
corrFeaturesInExpert = cell(numMiceExpert,12);
for i = 1 : numMiceExpert
indTuned = find(data1.expert(i).tunedAllCell(:,1));
indTemp = find(data1.expert(i).tunedAllCell(indTuned,3));
indWhisker = indTuned(indTemp);
for fi = 1 : 12
corrFeaturesInExpert{i,fi} = zeros(length(indWhisker),1);
for j = 1 : length(indWhisker)
tempVal = corr(cellfun(@mean, data1.expert(i).spkValAllCell{indWhisker(j),1}), cellfun(@mean, data2.expert(i).spkValAllCell{indWhisker(j),1+fi}));
if isnan(tempVal)
tempVal = 0;
end
corrFeaturesInExpert{i,fi}(j) = tempVal;
end
end
end
%%
%% Fig S8G
%%
%% Distribution of whisker correlation
histRange = 0:0.1:1;
corrWhiskerDist = cell2mat(cellfun(@(x) histcounts(x, histRange, 'norm', 'prob'), corrWhiskerNaive, 'un', 0));
figure, hold on
% boundedline(histRange(2:end)-0.05, mean(corrWhiskerDist), sem(corrWhiskerDist), 'k')
bar(histRange(2:end)-0.05, mean(corrWhiskerDist), 'k'), hold on
errorbar(histRange(2:end)-0.05, mean(corrWhiskerDist), sem(corrWhiskerDist), 'k', 'lines', 'no')
xlabel('Correlation with inferred spike')
ylabel('Proportion')
title('Naive')
set(gca, 'fontsize', 12, 'fontname','Arial', 'box', 'off');
%% Expert
histRange = 0:0.1:1;
corrWhiskerDist = cell2mat(cellfun(@(x) histcounts(x, histRange, 'norm', 'prob'), corrWhiskerExpert, 'un', 0));
figure, hold on
% boundedline(histRange(2:end)-0.05, mean(corrWhiskerDist), sem(corrWhiskerDist), 'k')
bar(histRange(2:end)-0.05, mean(corrWhiskerDist), 'k'), hold on
errorbar(histRange(2:end)-0.05, mean(corrWhiskerDist), sem(corrWhiskerDist), 'k', 'lines', 'no')
xlabel('Correlation with inferred spike')
ylabel('Proportion')
title('Expert')
set(gca, 'fontsize', 12, 'fontname','Arial', 'box', 'off');
%%
%% Fig S8H
%%
% Impacts of each features
eachFeatureOut = cellfun(@mean, corrWhiskerNaive) - cellfun(@mean, corrFeaturesOutNaive);
figure, hold on
bar(1:12, mean(eachFeatureOut), 'k')
errorbar(1:12, mean(eachFeatureOut), sem(eachFeatureOut), 'k', 'lines', 'no')
ylim([-0.001 0.2])
yticks(0:.05:0.2)
xticks(1:12)
xticklabels({'maxDq', 'maxDf', 'maxDkH', 'maxDkV', 'Slide distance', 'Touch duration', ...
'q', 'f', 'kH', 'kV', 'Arc length', 'Touch count'})
xtickangle(45)
ylabel('Impact on tuning curve')
title('Naive')
set(gca, 'fontsize', 12, 'fontname', 'Arial')
%% Expert
eachFeatureOut = cellfun(@mean, corrWhiskerExpert) - cellfun(@mean, corrFeaturesOutExpert);
figure, hold on
bar(1:12, mean(eachFeatureOut), 'k')
errorbar(1:12, mean(eachFeatureOut), sem(eachFeatureOut), 'k', 'lines', 'no')
ylim([-0.001 0.2])
yticks(0:.05:0.2)
xticks(1:12)
xticklabels({'maxDq', 'maxDf', 'maxDkH', 'maxDkV', 'Slide distance', 'Touch duration', ...
'q', 'f', 'kH', 'kV', 'Arc length', 'Touch count'})
xtickangle(45)
ylabel('Impact on tuning curve')
title('Expert')
set(gca, 'fontsize', 12, 'fontname', 'Arial')
%%
% saveDir = 'C:\Users\jinho\Dropbox\Works\Manuscripts\Object Angle Coding in vS1\Figures\Fig5-S2 Variable importance and impact on angle tuning\';
% fn = 'each_feature_impacts.eps';
% export_fig([saveDir, fn], '-depsc', '-painters', '-r600', '-transparent')
% fix_eps_fonts([saveDir, fn])
%%
%% Fig 5E
%%
% Distribution of maximum single feature impact
maxImpact = cell(numMiceNaive,1);
for mi = 1 : numMiceNaive
maxImpact{mi} = max(corrWhiskerNaive{mi} - cell2mat(corrFeaturesOutNaive(mi,:)),[],2);
end
maxImpactDist = cell2mat(cellfun(@(x) histcounts(x, histRange, 'norm', 'prob'), maxImpact, 'un', 0));
% figure,
%boundedline(histRange(2:end)-0.05, mean(maxImpactDist), sem(maxImpactDist), 'b')
%% Normalized by whisker correlation
maxReductionProportion = cellfun(@(x,y) y./x, corrWhiskerNaive, maxImpact, 'un', 0);
maxRPdist = cell2mat(cellfun(@(x) histcounts(x,histRange, 'norm', 'prob'), maxReductionProportion, 'un', 0));
%figure,
%boundedline(histRange(2:end)-0.05, mean(maxRPdist), sem(maxRPdist))
%% Distribution of all feature impact
allImpact = cell(numMiceNaive,1);
for mi = 1 : numMiceNaive
allImpact{mi} = max(corrWhiskerNaive{mi} - corrOtherNaive{mi}',[],2);
end
allImpactDist = cell2mat(cellfun(@(x) histcounts(x, histRange, 'norm', 'prob'), allImpact, 'un', 0));
% figure,
% boundedline(histRange(2:end)-0.05, mean(allImpactDist), sem(allImpactDist), 'r')
%% Compare mean impacts of
% mean whisker feature, each candidate features, max single feature, all whisker features
tempFeatureOut = cellfun(@mean, corrFeaturesOutNaive(:, [10,2,5,4]));
allImpactMean = cellfun(@mean, allImpact);
maxImpactMean = cellfun(@mean, maxImpact);
temptempFeatureOutMean = cellfun(@mean, corrFeaturesOutNaive);
tempFeatureOutMean = mean(temptempFeatureOutMean,2);
meanFeatureImpact = cellfun(@mean, corrWhiskerNaive) - tempFeatureOutMean;
eachFeatureImpact = cellfun(@mean, corrWhiskerNaive) - tempFeatureOut;
bothFeatureImpact = cellfun(@mean, corrWhiskerNaive) - cellfun(@mean, corrFeaturesCombOutNaive(:,3));
allBut2FeatureImpact = cellfun(@mean, corrWhiskerNaive) - cellfun(@mean, corrFeaturesCombInNaive(:,3));
figure, hold on