Skip to content

Commit de0d541

Browse files
committed
TEST: Allow SVD columns to flip sign, validate comparator
1 parent 921c583 commit de0d541

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

nipype/algorithms/tests/test_CompCor.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,41 @@
1010
from ..confounds import CompCor, TCompCor, ACompCor
1111

1212

13+
def close_up_to_column_sign(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
14+
"""SVD can produce sign flips on a per-column basis."""
15+
kwargs = dict(rtol=rtol, atol=atol, equal_nan=equal_nan)
16+
if np.allclose(a, b, **kwargs):
17+
return True
18+
19+
ret = True
20+
for acol, bcol in zip(a.T, b.T):
21+
ret &= np.allclose(acol, bcol, **kwargs) or np.allclose(acol, -bcol, **kwargs)
22+
if not ret:
23+
break
24+
25+
return ret
26+
27+
28+
@pytest.mark.parametrize(
29+
"a, b, close",
30+
[
31+
([[0.1, 0.2], [0.3, 0.4]], [[-0.1, 0.2], [-0.3, 0.4]], True),
32+
([[0.1, 0.2], [0.3, 0.4]], [[-0.1, 0.2], [0.3, -0.4]], False),
33+
],
34+
)
35+
def test_close_up_to_column_sign(a, b, close):
36+
a = np.asanyarray(a)
37+
b = np.asanyarray(b)
38+
assert close_up_to_column_sign(a, b) == close
39+
# Sign flips of all columns never changes result
40+
assert close_up_to_column_sign(a, -b) == close
41+
assert close_up_to_column_sign(-a, b) == close
42+
assert close_up_to_column_sign(-a, -b) == close
43+
# Trivial case
44+
assert close_up_to_column_sign(a, a)
45+
assert close_up_to_column_sign(b, b)
46+
47+
1348
class TestCompCor:
1449
"""Note: Tests currently do a poor job of testing functionality"""
1550

@@ -238,7 +273,7 @@ def run_cc(
238273
]
239274

240275
assert components_data.shape == (self.fake_data.shape[3], expected_n_components)
241-
assert np.allclose(components_data[:, :2], expected_components)
276+
assert close_up_to_column_sign(components_data[:, :2], expected_components)
242277

243278
if ccinterface.inputs.save_metadata:
244279
expected_metadata_file = ccinterface._list_outputs()["metadata_file"]

0 commit comments

Comments
 (0)