Skip to content

Commit f38d366

Browse files
committed
TEST: Allow SVD columns to flip sign, validate comparator
1 parent 450998d commit f38d366

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

nipype/algorithms/tests/test_CompCor.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,40 @@
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: break
23+
24+
return ret
25+
26+
27+
@pytest.mark.parametrize(
28+
"a, b, close",
29+
[
30+
([[0.1, 0.2], [0.3, 0.4]], [[-0.1, 0.2], [-0.3, 0.4]], True),
31+
([[0.1, 0.2], [0.3, 0.4]], [[-0.1, 0.2], [0.3, -0.4]], False),
32+
]
33+
)
34+
def test_close_up_to_column_sign(a, b, close):
35+
a = np.asanyarray(a)
36+
b = np.asanyarray(b)
37+
assert close_up_to_column_sign(a, b) == close
38+
# Sign flips of all columns never changes result
39+
assert close_up_to_column_sign(a, -b) == close
40+
assert close_up_to_column_sign(-a, b) == close
41+
assert close_up_to_column_sign(-a, -b) == close
42+
# Trivial case
43+
assert close_up_to_column_sign(a, a)
44+
assert close_up_to_column_sign(b, b)
45+
46+
1347
class TestCompCor:
1448
"""Note: Tests currently do a poor job of testing functionality"""
1549

@@ -236,7 +270,7 @@ def run_cc(
236270
assert header == [f"{expected_header}{i:02d}" for i in range(expected_n_components)]
237271

238272
assert components_data.shape == (self.fake_data.shape[3], expected_n_components)
239-
assert np.allclose(components_data[:, :2], expected_components)
273+
assert close_up_to_column_sign(components_data[:, :2], expected_components)
240274

241275
if ccinterface.inputs.save_metadata:
242276
expected_metadata_file = ccinterface._list_outputs()["metadata_file"]

0 commit comments

Comments
 (0)