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

Method test_loading_dict

tests/data/test_grid_dataset.py:138–178  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

136 )
137
138 def test_loading_dict(self):
139 set_determinism(seed=1234)
140 # test sequence input data with dict
141 data = [
142 {
143 "image": np.arange(16, dtype=float).reshape(1, 4, 4),
144 "label": np.arange(16, dtype=float).reshape(1, 4, 4),
145 "metadata": "test string",
146 },
147 {
148 "image": np.arange(16, dtype=float).reshape(1, 4, 4),
149 "label": np.arange(16, dtype=float).reshape(1, 4, 4),
150 "metadata": "test string",
151 },
152 ]
153 # image level
154 patch_intensity = RandShiftIntensityd(keys="image", offsets=1.0, prob=1.0)
155 patch_iter = PatchIterd(keys=["image", "label"], patch_size=(2, 2), start_pos=(0, 0))
156 ds = GridPatchDataset(data=data, patch_iter=patch_iter, transform=patch_intensity, with_coordinates=True)
157 # use the grid patch dataset
158 for item in DataLoader(ds, batch_size=2, shuffle=False, num_workers=0):
159 np.testing.assert_equal(item[0]["image"].shape, (2, 1, 2, 2))
160 np.testing.assert_equal(item[0]["label"].shape, (2, 1, 2, 2))
161 self.assertListEqual(item[0]["metadata"], ["test string", "test string"])
162 np.testing.assert_allclose(
163 item[0]["image"],
164 np.array([[[[8.708934, 9.708934], [12.708934, 13.708934]]], [[[10.8683, 11.8683], [14.8683, 15.8683]]]]),
165 rtol=1e-4,
166 )
167 np.testing.assert_allclose(item[1], np.array([[[0, 1], [2, 4], [0, 2]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5)
168 if sys.platform != "win32":
169 for item in DataLoader(ds, batch_size=2, shuffle=False, num_workers=2):
170 np.testing.assert_equal(item[0]["image"].shape, (2, 1, 2, 2))
171 np.testing.assert_allclose(
172 item[0]["image"],
173 np.array([[[[7.27427, 8.27427], [11.27427, 12.27427]]], [[[9.4353, 10.4353], [13.4353, 14.4353]]]]),
174 rtol=1e-3,
175 )
176 np.testing.assert_allclose(
177 item[1], np.array([[[0, 1], [2, 4], [0, 2]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5
178 )
179
180 def test_set_data(self):
181 from monai.transforms import Compose, Lambda, RandLambda

Callers

nothing calls this directly

Calls 6

set_determinismFunction · 0.90
RandShiftIntensitydClass · 0.90
PatchIterdClass · 0.90
GridPatchDatasetClass · 0.90
DataLoaderClass · 0.90
arrayMethod · 0.80

Tested by

no test coverage detected