MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / test_correct

Method test_correct

tests/transforms/utility/test_splitdimd.py:42–79  ·  view source on GitHub ↗
(self, keepdim, _, update_meta, list_output)

Source from the content-addressed store, hash-verified

40
41 @parameterized.expand(TESTS)
42 def test_correct(self, keepdim, _, update_meta, list_output):
43 data = deepcopy(self.data)
44 arr = data["i"]
45 for dim in range(arr.ndim):
46 out = SplitDimd("i", dim=dim, keepdim=keepdim, update_meta=update_meta, list_output=list_output)(data)
47 if list_output:
48 self.assertIsInstance(out, list)
49 self.assertEqual(len(out), arr.shape[dim])
50 else:
51 self.assertIsInstance(out, dict)
52 self.assertEqual(len(out.keys()), len(data.keys()) + arr.shape[dim])
53 # if updating metadata, pick some random points and
54 # check same world coordinates between input and output
55 if update_meta:
56 for _ in range(10):
57 idx = [np.random.choice(i) for i in arr.shape]
58 split_im_idx = idx[dim]
59 split_idx = deepcopy(idx)
60 split_idx[dim] = 0
61 if list_output:
62 split_im = out[split_im_idx]["i"]
63 else:
64 split_im = out[f"i_{split_im_idx}"]
65 if isinstance(data, MetaTensor) and isinstance(split_im, MetaTensor):
66 # idx[1:] to remove channel and then add 1 for 4th element
67 real_world = data.affine @ torch.tensor(idx[1:] + [1]).double()
68 real_world2 = split_im.affine @ torch.tensor(split_idx[1:] + [1]).double()
69 assert_allclose(real_world, real_world2)
70
71 if list_output:
72 out = out[0]["i"]
73 else:
74 out = out["i_0"]
75 expected_ndim = arr.ndim if keepdim else arr.ndim - 1
76 self.assertEqual(out.ndim, expected_ndim)
77 # assert is a shallow copy
78 arr[0, 0, 0, 0] *= 2
79 self.assertEqual(arr.flatten()[0], out.flatten()[0])
80
81 def test_singleton(self):
82 shape = (2, 1, 8, 7)

Callers

nothing calls this directly

Calls 4

SplitDimdClass · 0.90
assert_allcloseFunction · 0.90
doubleMethod · 0.80
flattenMethod · 0.45

Tested by

no test coverage detected