From 17b7334fb19b6cadb5426dbf1a15e18b4567d604 Mon Sep 17 00:00:00 2001 From: Rich <33289025+rijobro@users.noreply.github.com> Date: Tue, 23 Feb 2021 15:52:02 +0000 Subject: [PATCH] make random inverse affine matrix Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com> --- tests/utils.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/utils.py b/tests/utils.py index 8de82bee82..07927cd62f 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -151,6 +151,19 @@ def make_nifti_image(array, affine=None): return image_name +def make_rand_affine(ndim: int = 3, random_state: Optional[np.random.RandomState] = None): + """Create random affine transformation (with values == -1, 0 or 1).""" + rs = np.random if random_state is None else random_state + + vals = rs.choice([-1, 1], size=ndim) + positions = rs.choice(range(ndim), size=ndim, replace=False) + af = np.zeros([ndim + 1, ndim + 1]) + af[ndim, ndim] = 1 + for i, (v, p) in enumerate(zip(vals, positions)): + af[i, p] = v + return af + + class DistTestCase(unittest.TestCase): """ testcase without _outcome, so that it's picklable.