-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocal_template.py
More file actions
268 lines (238 loc) · 8.51 KB
/
Copy pathlocal_template.py
File metadata and controls
268 lines (238 loc) · 8.51 KB
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
import numpy as np
from scipy.linalg import LinAlgError
from scipy.stats import zscore
from sklearn.decomposition import PCA
from sklearn.utils.extmath import randomized_svd
from hyperalignment.linalg import safe_svd
from hyperalignment.procrustes import procrustes
def PCA_decomposition(
dss, max_npc=None, flavor="sklearn", adjust_ns=False, demean=True
):
"""Decompose concatenated data matrices using PCA/SVD.
Parameters
----------
dss : ndarray of shape (ns, nt, nv)
max_npc : integer or None
flavor : {'sklearn', 'svd'}
adjust_ns : bool
Whether to adjust the variance of the output so that it doesn't increase with the number of subjects.
demean : bool
Whether to remove the mean of the columns prior to SVD.
Returns
-------
XX : ndarray of shape (nt, npc)
cc : ndarray of shape (npc, ns, nv)
"""
ns, nt, nv = dss.shape
X = dss.transpose(1, 0, 2).reshape(nt, ns * nv)
if max_npc is not None:
max_npc = min(max_npc, min(X.shape[0], X.shape[1]))
if flavor == "sklearn":
try:
if demean:
pca = PCA(n_components=max_npc, random_state=0)
XX = pca.fit_transform(X)
cc = pca.components_.reshape(-1, ns, nv)
if adjust_ns:
XX /= np.sqrt(ns)
return XX, cc
else:
U, s, Vt = randomized_svd(
X,
(max_npc if max_npc is not None else min(X.shape)),
random_state=0,
)
if adjust_ns:
XX = U[:, :max_npc] * (s[np.newaxis, :max_npc] / np.sqrt(ns))
else:
XX = U[:, :max_npc] * (s[np.newaxis, :max_npc])
cc = Vt[:max_npc].reshape(-1, ns, nv)
return XX, cc
except LinAlgError as e:
return PCA_decomposition(
dss, max_npc=max_npc, flavor="svd", adjust_ns=adjust_ns, demean=demean
)
elif flavor == "svd":
U, s, Vt = safe_svd(X, demean=demean)
if adjust_ns:
XX = U[:, :max_npc] * (s[np.newaxis, :max_npc] / np.sqrt(ns))
else:
XX = U[:, :max_npc] * (s[np.newaxis, :max_npc])
cc = Vt[:max_npc].reshape(-1, ns, nv)
return XX, cc
else:
raise NotImplementedError
def compute_PCA_template(dss, sl=None, max_npc=None, flavor="sklearn", demean=True):
if sl is not None:
dss = dss[:, :, sl]
XX, cc = PCA_decomposition(
dss, max_npc=max_npc, flavor=flavor, adjust_ns=True, demean=demean
)
return XX
def compute_PCA_var1_template(
dss, sl=None, max_npc=None, flavor="sklearn", demean=True
):
if sl is not None:
dss = dss[:, :, sl]
XX, cc = PCA_decomposition(
dss, max_npc=max_npc, flavor=flavor, adjust_ns=False, demean=demean
)
w = np.sqrt(np.sum(cc**2, axis=2)).mean(axis=1)
XX *= w[np.newaxis]
return XX
def compute_PCA_var2_template(
dss, sl=None, max_npc=None, flavor="sklearn", demean=True
):
if sl is not None:
dss = dss[:, :, sl]
XX, cc = PCA_decomposition(
dss, max_npc=max_npc, flavor=flavor, adjust_ns=False, demean=demean
)
# w = np.sqrt(np.sum(cc**2, axis=2)).mean(axis=1)
w = np.exp(0.5 * np.log(np.sum(cc**2, axis=2)).mean(axis=1))
XX *= w[np.newaxis]
return XX
def compute_GPA_template(
dss,
sl=None,
reflection=True,
scaling=False,
zscore_common=True,
level2_iter=1,
dss2=None,
**kwargs
):
if sl is not None:
dss = dss[:, :, sl]
common_space = np.mean(dss, axis=0)
if zscore_common:
common_space = np.nan_to_num(zscore(common_space, axis=0))
aligned_dss = []
for ds in dss:
T = procrustes(ds, common_space, reflection=reflection, scaling=scaling)
aligned_ds = ds.dot(T)
if zscore_common:
aligned_ds = np.nan_to_num(zscore(aligned_ds, axis=0))
aligned_dss.append(aligned_ds)
aligned_dss2 = []
for level2 in range(level2_iter):
common_space = np.zeros_like(dss[0])
for ds in aligned_dss:
common_space += ds
for i, ds in enumerate(dss):
reference = (common_space - aligned_dss[i]) / float(len(dss) - 1)
if zscore_common:
reference = np.nan_to_num(zscore(reference, axis=0))
T = procrustes(ds, reference, reflection=reflection, scaling=scaling)
if level2 == level2_iter - 1 and dss2 is not None:
aligned_dss2.append(dss2[i].dot(T))
aligned_dss[i] = ds.dot(T)
common_space = np.sum(aligned_dss, axis=0)
if zscore_common:
common_space = np.nan_to_num(zscore(common_space, axis=0))
else:
common_space /= float(len(dss))
if dss2 is not None:
common_space2 = np.zeros_like(dss2[0])
for ds in aligned_dss2:
common_space2 += ds
if zscore_common:
common_space2 = np.nan_to_num(zscore(common_space2, axis=0))
else:
common_space2 /= float(len(dss))
return common_space, common_space2
return common_space
def compute_procrustes_template(
dss,
sl=None,
reflection=True,
scaling=False,
zscore_common=True,
level2_iter=1,
dss2=None,
**kwargs
):
if sl is not None:
dss = dss[:, :, sl]
common_space = np.copy(dss[0])
aligned_dss = [dss[0]]
for ds in dss[1:]:
T = procrustes(ds, common_space, reflection=reflection, scaling=scaling)
aligned_ds = ds.dot(T)
if zscore_common:
aligned_ds = np.nan_to_num(zscore(aligned_ds, axis=0))
aligned_dss.append(aligned_ds)
common_space = (common_space + aligned_ds) * 0.5
if zscore_common:
common_space = np.nan_to_num(zscore(common_space, axis=0))
aligned_dss2 = []
for level2 in range(level2_iter):
common_space = np.zeros_like(dss[0])
for ds in aligned_dss:
common_space += ds
for i, ds in enumerate(dss):
reference = (common_space - aligned_dss[i]) / float(len(dss) - 1)
if zscore_common:
reference = np.nan_to_num(zscore(reference, axis=0))
T = procrustes(ds, reference, reflection=reflection, scaling=scaling)
if level2 == level2_iter - 1 and dss2 is not None:
aligned_dss2.append(dss2[i].dot(T))
aligned_dss[i] = ds.dot(T)
# common_space = np.zeros_like(dss[0])
# for ds in aligned_dss:
# common_space += ds
common_space = np.sum(aligned_dss, axis=0)
if zscore_common:
common_space = np.nan_to_num(zscore(common_space, axis=0))
else:
common_space /= float(len(dss))
if dss2 is not None:
common_space2 = np.zeros_like(dss2[0])
for ds in aligned_dss2:
common_space2 += ds
if zscore_common:
common_space2 = np.nan_to_num(zscore(common_space2, axis=0))
else:
common_space2 /= float(len(dss))
return common_space, common_space2
return common_space
def compute_template(
dss, sl=None, kind="procrustes", max_npc=None, common_topography=False, demean=False
):
mapping = {
"pca": compute_PCA_template,
"pcav1": compute_PCA_var1_template,
"pcav2": compute_PCA_var2_template,
"cls": compute_procrustes_template,
}
if kind == "procrustes":
tmpl = compute_procrustes_template(
dss=dss, sl=sl, reflection=True, scaling=False, zscore_common=True
)
elif kind in mapping:
tmpl = mapping[kind](dss=dss, sl=sl, max_npc=max_npc, demean=demean)
else:
raise ValueError
if common_topography:
if sl is not None:
dss = dss[:, :, sl]
ns, nt, nv = dss.shape
T = procrustes(np.tile(tmpl, (ns, 1)), dss.reshape(ns * nt, nv))
tmpl = tmpl @ T
return tmpl
if __name__ == "__main__":
from scipy.stats import zscore
from hyperalignment.linalg import svd_pca
rng = np.random.default_rng(0)
dss = rng.standard_normal((1, 200, 100))
dss = zscore(dss, axis=1)
dss = np.tile(dss, (10, 1, 1))
XX0 = compute_PCA_template(dss)
XX1 = compute_PCA_var1_template(dss)
XX2 = compute_PCA_var2_template(dss)
np.testing.assert_allclose(XX0, XX1, atol=1e-7)
np.testing.assert_allclose(XX0, XX2, atol=1e-7)
XX3 = compute_procrustes_template(dss)
np.testing.assert_allclose(dss[0], XX3, atol=1e-7)
XX3 = svd_pca(XX3)
np.testing.assert_allclose(np.abs(XX0[:, :100]), np.abs(XX3), atol=1e-7)