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

Method test_loading_array

tests/data/test_patch_dataset.py:47–88  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

45 self.assertEqual(output, expected)
46
47 def test_loading_array(self):
48 set_determinism(seed=1234)
49 # image dataset
50 images = [np.arange(16, dtype=float).reshape(1, 4, 4), np.arange(16, dtype=float).reshape(1, 4, 4)]
51 # image patch sampler
52 n_samples = 8
53 sampler = RandSpatialCropSamples(roi_size=(3, 3), num_samples=n_samples, random_center=True, random_size=False)
54
55 # image level
56 patch_intensity = RandShiftIntensity(offsets=1.0, prob=1.0)
57 image_ds = Dataset(images, transform=patch_intensity)
58 # patch level
59 ds = PatchDataset(data=image_ds, patch_func=sampler, samples_per_image=n_samples, transform=patch_intensity)
60
61 np.testing.assert_equal(len(ds), n_samples * len(images))
62 # use the patch dataset, length: len(images) x samplers_per_image
63 for item in DataLoader(ds, batch_size=2, shuffle=False, num_workers=0):
64 np.testing.assert_equal(tuple(item.shape), (2, 1, 3, 3))
65 np.testing.assert_allclose(
66 item[0],
67 np.array(
68 [[[4.970372, 5.970372, 6.970372], [8.970372, 9.970372, 10.970372], [12.970372, 13.970372, 14.970372]]]
69 ),
70 rtol=1e-5,
71 )
72 if sys.platform != "win32":
73 for item in DataLoader(ds, batch_size=2, shuffle=False, num_workers=2):
74 np.testing.assert_equal(tuple(item.shape), (2, 1, 3, 3))
75 np.testing.assert_allclose(
76 item[0],
77 np.array(
78 [
79 [
80 [5.028125, 6.028125, 7.028125],
81 [9.028125, 10.028125, 11.028125],
82 [13.028125, 14.028125, 15.028125],
83 ]
84 ]
85 ),
86 rtol=1e-5,
87 )
88 set_determinism(seed=None)
89
90
91if __name__ == "__main__":

Callers

nothing calls this directly

Calls 7

set_determinismFunction · 0.90
RandShiftIntensityClass · 0.90
DatasetClass · 0.90
PatchDatasetClass · 0.90
DataLoaderClass · 0.90
arrayMethod · 0.80

Tested by

no test coverage detected