From a894adc89c88780044be4cfad050dd34f0256cee Mon Sep 17 00:00:00 2001 From: Richard Brown <33289025+rijobro@users.noreply.github.com> Date: Tue, 23 Feb 2021 18:20:58 +0000 Subject: [PATCH] make random inverse affine matrix (#1621) 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 1adc20d280..4597a18fbd 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -155,6 +155,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.