make random inverse affine matrix
@@ -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.
