|
10 | 10 | from ..confounds import CompCor, TCompCor, ACompCor
|
11 | 11 |
|
12 | 12 |
|
| 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 | + |
13 | 48 | class TestCompCor:
|
14 | 49 | """Note: Tests currently do a poor job of testing functionality"""
|
15 | 50 |
|
@@ -238,7 +273,7 @@ def run_cc(
|
238 | 273 | ]
|
239 | 274 |
|
240 | 275 | 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) |
242 | 277 |
|
243 | 278 | if ccinterface.inputs.save_metadata:
|
244 | 279 | expected_metadata_file = ccinterface._list_outputs()["metadata_file"]
|
|
0 commit comments