Repository navigation
Expand file tree
/
Copy pathtest_srm.py
More file actions
304 lines (249 loc) · 10.7 KB
/
Copy pathtest_srm.py
File metadata and controls
304 lines (249 loc) · 10.7 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
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
# Copyright 2016 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from sklearn.exceptions import NotFittedError
import pytest
def test_can_instantiate():
import brainiak.funcalign.srm
s = brainiak.funcalign.srm.SRM()
assert s, "Invalid SRM instance!"
import numpy as np
np.random.seed(0)
voxels = 100
samples = 500
subjects = 2
features = 3
s = brainiak.funcalign.srm.SRM(n_iter=5, features=features)
assert s, "Invalid SRM instance!"
# Create a Shared response S with K = 3
theta = np.linspace(-4 * np.pi, 4 * np.pi, samples)
z = np.linspace(-2, 2, samples)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
S = np.vstack((x, y, z))
X = []
W = []
Q, R = np.linalg.qr(np.random.random((voxels, features)))
W.append(Q)
X.append(Q.dot(S) + 0.1*np.random.random((voxels, samples)))
# Check that transform does NOT run before fitting the model
with pytest.raises(NotFittedError):
s.transform(X)
print("Test: transforming before fitting the model")
# Check that it does NOT run with 1 subject
with pytest.raises(ValueError):
s.fit(X)
print("Test: running SRM with 1 subject")
for subject in range(1, subjects):
Q, R = np.linalg.qr(np.random.random((voxels, features)))
W.append(Q)
X.append(Q.dot(S) + 0.1*np.random.random((voxels, samples)))
# Check that runs with 2 subject
s.fit(X)
from pathlib import Path
sr_v0_4 = np.load(Path(__file__).parent / "sr_v0_4.npz")['sr']
assert(np.allclose(sr_v0_4, s.s_))
assert len(s.w_) == subjects, (
"Invalid computation of SRM! (wrong # subjects in W)")
for subject in range(subjects):
assert s.w_[subject].shape[0] == voxels, (
"Invalid computation of SRM! (wrong # voxels in W)")
assert s.w_[subject].shape[1] == features, (
"Invalid computation of SRM! (wrong # features in W)")
ortho = np.linalg.norm(s.w_[subject].T.dot(s.w_[subject])
- np.eye(s.w_[subject].shape[1]),
'fro')
assert ortho < 1e-7, "A Wi mapping is not orthonormal in SRM."
difference = np.linalg.norm(X[subject] - s.w_[subject].dot(s.s_),
'fro')
datanorm = np.linalg.norm(X[subject], 'fro')
assert difference/datanorm < 1.0, "Model seems incorrectly computed."
assert s.s_.shape[0] == features, (
"Invalid computation of SRM! (wrong # features in S)")
assert s.s_.shape[1] == samples, (
"Invalid computation of SRM! (wrong # samples in S)")
# Check that it does run to compute the shared response after the model
# computation
new_s = s.transform(X)
assert len(new_s) == subjects, (
"Invalid computation of SRM! (wrong # subjects after transform)")
for subject in range(subjects):
assert new_s[subject].shape[0] == features, (
"Invalid computation of SRM! (wrong # features after transform)")
assert new_s[subject].shape[1] == samples, (
"Invalid computation of SRM! (wrong # samples after transform)")
# Check that it does NOT run with non-matching number of subjects
with pytest.raises(ValueError):
s.transform(X[1])
print("Test: transforming with non-matching number of subjects")
# Check that it does not run without enough samples (TRs).
with pytest.raises(ValueError):
s.set_params(features=(samples+1))
s.fit(X)
print("Test: not enough samples")
# Check that it does not run with different number of samples (TRs)
S2 = S[:, :-2]
X.append(Q.dot(S2))
with pytest.raises(ValueError):
s.fit(X)
print("Test: different number of samples per subject")
def test_new_subject():
import brainiak.funcalign.srm
s = brainiak.funcalign.srm.SRM()
assert s, "Invalid SRM instance!"
import numpy as np
np.random.seed(0)
voxels = 100
samples = 500
subjects = 3
features = 3
s = brainiak.funcalign.srm.SRM(n_iter=5, features=features)
assert s, "Invalid SRM instance!"
# Create a Shared response S with K = 3
theta = np.linspace(-4 * np.pi, 4 * np.pi, samples)
z = np.linspace(-2, 2, samples)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
S = np.vstack((x, y, z))
X = []
W = []
Q, R = np.linalg.qr(np.random.random((voxels, features)))
W.append(Q)
X.append(Q.dot(S) + 0.1*np.random.random((voxels, samples)))
for subject in range(1, subjects):
Q, R = np.linalg.qr(np.random.random((voxels, features)))
W.append(Q)
X.append(Q.dot(S) + 0.1*np.random.random((voxels, samples)))
# Check that transform does NOT run before fitting the model
with pytest.raises(NotFittedError):
s.transform_subject(X)
print("Test: transforming before fitting the model")
# Check that runs with 3 subject
s.fit(X)
# Check that you get an error when the data is the wrong shape
with pytest.raises(ValueError):
s.transform_subject(X[0].T)
# Check that it does run to compute a new subject
new_w = s.transform_subject(X[0])
assert new_w.shape[1] == features, (
"Invalid computation of SRM! (wrong # features for new subject)")
assert new_w.shape[0] == voxels, (
"Invalid computation of SRM! (wrong # voxels for new subject)")
# Check that these analyses work with the deterministic SRM too
ds = brainiak.funcalign.srm.DetSRM(n_iter=5, features=features)
# Check that transform does NOT run before fitting the model
with pytest.raises(NotFittedError):
ds.transform_subject(X)
print("Test: transforming before fitting the model")
# Check that runs with 3 subject
ds.fit(X)
# Check that you get an error when the data is the wrong shape
with pytest.raises(ValueError):
ds.transform_subject(X[0].T)
# Check that it does run to compute a new subject
new_w = ds.transform_subject(X[0])
assert new_w.shape[1] == features, (
"Invalid computation of SRM! (wrong # features for new subject)")
assert new_w.shape[0] == voxels, (
"Invalid computation of SRM! (wrong # voxels for new subject)")
def test_det_srm():
import brainiak.funcalign.srm
model = brainiak.funcalign.srm.DetSRM()
assert model, "Invalid DetSRM instance!"
import numpy as np
voxels = 100
samples = 500
subjects = 2
features = 3
model = brainiak.funcalign.srm.DetSRM(n_iter=5, features=features)
assert model, "Invalid DetSRM instance!"
# Create a Shared response S with K = 3
theta = np.linspace(-4 * np.pi, 4 * np.pi, samples)
z = np.linspace(-2, 2, samples)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
S = np.vstack((x, y, z))
X = []
W = []
Q, R = np.linalg.qr(np.random.random((voxels, features)))
W.append(Q)
X.append(Q.dot(S) + 0.1*np.random.random((voxels, samples)))
# Check that transform does NOT run before fitting the model
with pytest.raises(NotFittedError):
model.transform(X)
print("Test: transforming before fitting the model")
# Check that it does NOT run with 1 subject
with pytest.raises(ValueError):
model.fit(X)
print("Test: running DetSRM with 1 subject")
for subject in range(1, subjects):
Q, R = np.linalg.qr(np.random.random((voxels, features)))
W.append(Q)
X.append(Q.dot(S) + 0.1*np.random.random((voxels, samples)))
# Check that runs with 2 subject
model.fit(X)
assert len(model.w_) == subjects, (
"Invalid computation of DetSRM! (wrong # subjects in W)")
for subject in range(subjects):
assert model.w_[subject].shape[0] == voxels, (
"Invalid computation of DetSRM! (wrong # voxels in W)")
assert model.w_[subject].shape[1] == features, (
"Invalid computation of DetSRM! (wrong # features in W)")
ortho = np.linalg.norm(model.w_[subject].T.dot(model.w_[subject])
- np.eye(model.w_[subject].shape[1]),
'fro')
assert ortho < 1e-7, "A Wi mapping is not orthonormal in DetSRM."
difference = np.linalg.norm(X[subject]
- model.w_[subject].dot(model.s_),
'fro')
datanorm = np.linalg.norm(X[subject], 'fro')
assert difference/datanorm < 1.0, "Model seems incorrectly computed."
assert model.s_.shape[0] == features, (
"Invalid computation of DetSRM! (wrong # features in S)")
assert model.s_.shape[1] == samples, (
"Invalid computation of DetSRM! (wrong # samples in S)")
# Check that it does run to compute the shared response after the model
# computation
new_s = model.transform(X)
assert len(new_s) == subjects, (
"Invalid computation of DetSRM! (wrong # subjects after transform)")
for subject in range(subjects):
assert new_s[subject].shape[0] == features, (
"Invalid computation of DetSRM! (wrong # features after "
"transform)")
assert new_s[subject].shape[1] == samples, (
"Invalid computation of DetSRM! (wrong # samples after transform)")
# Check that it does run to compute a new subject
new_w = model.transform_subject(X[0])
assert new_w.shape[1] == features, (
"Invalid computation of SRM! (wrong # features for new subject)")
assert new_w.shape[0] == voxels, (
"Invalid computation of SRM! (wrong # voxels for new subject)")
# Check that it does NOT run with non-matching number of subjects
with pytest.raises(ValueError):
model.transform(X[1])
print("Test: transforming with non-matching number of subjects")
# Check that it does not run without enough samples (TRs).
with pytest.raises(ValueError):
model.set_params(features=(samples+1))
model.fit(X)
print("Test: not enough samples")
# Check that it does not run with different number of samples (TRs)
S2 = S[:, :-2]
X.append(Q.dot(S2))
with pytest.raises(ValueError):
model.fit(X)
print("Test: different number of samples per subject")