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

Method test_set_data

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

Source from the content-addressed store, hash-verified

178 )
179
180 def test_set_data(self):
181 from monai.transforms import Compose, Lambda, RandLambda
182
183 images = [np.arange(2, 18, dtype=float).reshape(1, 4, 4), np.arange(16, dtype=float).reshape(1, 4, 4)]
184
185 transform = Compose(
186 [Lambda(func=lambda x: np.array(x * 10)), RandLambda(func=lambda x: x + 1)], map_items=False
187 )
188 patch_iter = PatchIter(patch_size=(2, 2), start_pos=(0, 0))
189 dataset = GridPatchDataset(
190 data=images,
191 patch_iter=patch_iter,
192 transform=transform,
193 cache=True,
194 cache_rate=1.0,
195 copy_cache=not sys.platform == "linux",
196 )
197
198 num_workers = 2 if sys.platform == "linux" else 0
199 for item in DataLoader(dataset, batch_size=2, shuffle=False, num_workers=num_workers):
200 np.testing.assert_equal(tuple(item[0].shape), (2, 1, 2, 2))
201 np.testing.assert_allclose(item[0], np.array([[[[81, 91], [121, 131]]], [[[101, 111], [141, 151]]]]), rtol=1e-4)
202 np.testing.assert_allclose(item[1], np.array([[[0, 1], [2, 4], [0, 2]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5)
203 # simulate another epoch, the cache content should not be modified
204 for item in DataLoader(dataset, batch_size=2, shuffle=False, num_workers=num_workers):
205 np.testing.assert_equal(tuple(item[0].shape), (2, 1, 2, 2))
206 np.testing.assert_allclose(item[0], np.array([[[[81, 91], [121, 131]]], [[[101, 111], [141, 151]]]]), rtol=1e-4)
207 np.testing.assert_allclose(item[1], np.array([[[0, 1], [2, 4], [0, 2]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5)
208
209 # update the datalist and fill the cache content
210 data_list2 = [np.arange(1, 17, dtype=float).reshape(1, 4, 4)]
211 dataset.set_data(data=data_list2)
212 # rerun with updated cache content
213 for item in DataLoader(dataset, batch_size=2, shuffle=False, num_workers=num_workers):
214 np.testing.assert_equal(tuple(item[0].shape), (2, 1, 2, 2))
215 np.testing.assert_allclose(
216 item[0], np.array([[[[91, 101], [131, 141]]], [[[111, 121], [151, 161]]]]), rtol=1e-4
217 )
218 np.testing.assert_allclose(item[1], np.array([[[0, 1], [2, 4], [0, 2]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5)
219
220
221if __name__ == "__main__":

Callers

nothing calls this directly

Calls 8

set_dataMethod · 0.95
ComposeClass · 0.90
LambdaClass · 0.90
RandLambdaClass · 0.90
PatchIterClass · 0.90
GridPatchDatasetClass · 0.90
DataLoaderClass · 0.90
arrayMethod · 0.80

Tested by

no test coverage detected