-
Notifications
You must be signed in to change notification settings - Fork 0
/
canonical_correlation_analysis.m
148 lines (124 loc) · 4.17 KB
/
canonical_correlation_analysis.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
function canonical_correlation_analysis(peri_event_signals_ii, peri_event_signals_jj, before_event,latent_dim)
% canonical correlation analysis for two sets of peri-event signals implemented based
% on the article "Long-term stability of cortical population dynamics underlying
% consistent behavior", NN, 2020
% Inputs:
% peri_event_signals_ii: neuron by time by trials (events)
% peri_event_signals_jj: neuron by time by trials (events)
% before_event: the period before and after events for calculation of
% canonical components
% srate: sampling rate
% latent_dim: number of canonical components to be considered
after_event = before_event;
midpoint = round(size(peri_event_signals_ii,2)/2);
peri_event_signals_ii = peri_event_signals_ii(:,midpoint+(-round(before_event*camera_srate):round(after_event*camera_srate)),:);
peri_event_signals_ii = bsxfun(@minus, peri_event_signals_ii, mean(peri_event_signals_ii,2));
peri_event_signals_ii = reshape(peri_event_signals_ii, size(peri_event_signals_ii,1), []);
[U_ii,S_ii,V_ii] = svd(peri_event_signals_ii,'econ');
midpoint = round(size(peri_event_signals_jj,2)/2);
peri_event_signals_jj = peri_event_signals_jj(:,midpoint+(-round(before_event*camera_srate):round(after_event*camera_srate)),:);
peri_event_signals_jj = bsxfun(@minus, peri_event_signals_jj, mean(peri_event_signals_jj,2));
peri_event_signals_jj = reshape(peri_event_signals_jj, size(peri_event_signals_jj,1), []);
[U_jj,S_jj,V_jj] = svd(peri_event_signals_jj,'econ');
L_ii = S_ii(1:latent_dim,1:latent_dim)*V_ii(:,1:latent_dim)';
L_jj = S_jj(1:latent_dim,1:latent_dim)*V_jj(:,1:latent_dim)';
[Q_ii,R_ii] = qr(L_ii',0);
Q_ii = single(Q_ii);
R_ii = single(R_ii);
[Q_jj,R_jj] = qr(L_jj',0);
Q_jj = single(Q_jj);
R_jj = single(R_jj);
[U,~,V] = svd(Q_ii(:,1:m)' * Q_jj(:,1:m), 'econ');
M_ii = inv(R_ii(1:m,1:m)) * U;
M_jj = inv(R_jj(1:m,1:m)) * V;
L_ii_aligned = M_ii' * L_ii;
L_jj_aligned = M_jj' * L_jj;
L_ii_to_jj = inv(M_jj)' * M_ii' * L_ii;
L_jj_to_ii = inv(M_ii)' * M_jj' * L_jj;
PC_num = 1;
figure;
yyaxis left
plot(L_ii(PC_num,:));
yyaxis right
plot(L_jj(PC_num,:));
corrcoef(L_ii(PC_num,:), L_jj(PC_num,:))
%%
PC_num = 1;
figure;
yyaxis left
plot(L_ii_aligned(PC_num,:));
yyaxis right
plot(L_jj_aligned(PC_num,:));
corrcoef(L_ii_aligned(PC_num,:), L_jj_aligned(PC_num,:))
%%
PC_num = 1;
figure;
yyaxis left
plot(L_ii(PC_num,:));
yyaxis right
plot(L_jj_to_ii(PC_num,:));
corrcoef(L_ii(PC_num,:), L_jj_to_ii(PC_num,:))
%%
PC_num = 1;
figure;
yyaxis left
plot(L_jj(PC_num,:));
yyaxis right
plot(L_ii_to_jj(PC_num,:));
corrcoef(L_jj(PC_num,:), L_ii_to_jj(PC_num,:))
%%
figure;
plot(mean(U_ii(:,1:m)*L_ii,1));
hold on;
plot(mean(U_ii(:,1:m)*L_jj_to_ii,1),'r');
corrcoef(mean(U_ii(:,1:m)*L_ii,1), mean(U_ii(:,1:m)*L_jj_to_ii,1))
%%
figure;
plot(mean(U_jj(:,1:m)*L_jj,1));
hold on;
plot(mean(U_jj(:,1:m)*L_ii_to_jj,1),'r');
corrcoef(mean(U_jj(:,1:m)*L_jj,1), mean(U_jj(:,1:m)*L_ii_to_jj,1))
%%
aaa = U_ii(:,1:m)*L_ii;
bbb = U_ii(:,1:m)*L_jj_to_ii;
neuron_num = 300;
figure;
plot(aaa(neuron_num,:));
hold on;
plot(bbb(neuron_num,:));
corrcoef(aaa(neuron_num,:), bbb(neuron_num,:))
%%
corr_mat = zscore(aaa,1,2) * zscore(bbb,1,2)' / size(aaa,2);
figure; plot(diag(corr_mat));
figure; histogram(diag(corr_mat));
%%
aaa = U_jj(:,1:m)*L_jj;
bbb = U_jj(:,1:m)*L_ii_to_jj;
neuron_num = 300;
figure;
plot(aaa(neuron_num,:));
hold on;
plot(bbb(neuron_num,:));
corrcoef(aaa(neuron_num,:), bbb(neuron_num,:))
%%
corr_mat = zscore(aaa,1,2) * zscore(bbb,1,2)' / size(aaa,2);
figure; plot(diag(corr_mat));
figure; histogram(diag(corr_mat));
%%
aaa = U_jj(:,1:m)*L_jj;
bbb = U_ii(:,1:m)*L_ii;
neuron_num = 300;
figure;
plot(aaa(neuron_num,:));
hold on;
plot(bbb(neuron_num,:));
corrcoef(aaa(neuron_num,:), bbb(neuron_num,:))
%%
corr_mat = zscore(aaa,1,2) * zscore(bbb,1,2)' / size(aaa,2);
figure; plot(diag(corr_mat));
figure; histogram(diag(corr_mat),'BinWidth',0.1);
%%
corr_mat = zscore(peri_event_signals_ii,1,2) * zscore(peri_event_signals_jj,1,2)' / size(peri_event_signals_ii,2);
figure; plot(diag(corr_mat));
figure; histogram(diag(corr_mat),'BinWidth',0.1);
end