(self)
| 82 | self.assertTupleEqual(img_dataset[0][1].shape, (1, 14, 14, 7)) |
| 83 | |
| 84 | def test_dataset(self): |
| 85 | with tempfile.TemporaryDirectory() as tempdir: |
| 86 | full_names, ref_data = [], [] |
| 87 | for filename in FILENAMES: |
| 88 | test_image = np.random.randint(0, 2, size=(4, 4, 4)).astype(float) |
| 89 | ref_data.append(test_image) |
| 90 | save_path = os.path.join(tempdir, filename) |
| 91 | full_names.append(save_path) |
| 92 | nib.save(nib.Nifti1Image(test_image, np.eye(4)), save_path) |
| 93 | |
| 94 | # default loading no meta |
| 95 | dataset = ImageDataset(full_names) |
| 96 | for d, ref in zip(dataset, ref_data): |
| 97 | np.testing.assert_allclose(d, ref, atol=1e-3) |
| 98 | |
| 99 | # loading no meta, int |
| 100 | dataset = ImageDataset(full_names, dtype=np.float16) |
| 101 | for d, _ in zip(dataset, ref_data): |
| 102 | self.assertEqual(d.dtype, torch.float16) |
| 103 | |
| 104 | # loading with meta, no transform |
| 105 | dataset = ImageDataset(full_names, image_only=False) |
| 106 | for d_tuple, ref in zip(dataset, ref_data): |
| 107 | d, meta = d_tuple |
| 108 | np.testing.assert_allclose(d, ref, atol=1e-3) |
| 109 | np.testing.assert_allclose(meta["original_affine"], np.eye(4)) |
| 110 | |
| 111 | # loading image/label, no meta |
| 112 | dataset = ImageDataset(full_names, seg_files=full_names, image_only=True) |
| 113 | for d_tuple, ref in zip(dataset, ref_data): |
| 114 | img, seg = d_tuple |
| 115 | np.testing.assert_allclose(img, ref, atol=1e-3) |
| 116 | np.testing.assert_allclose(seg, ref, atol=1e-3) |
| 117 | |
| 118 | # loading image/label, no meta |
| 119 | dataset = ImageDataset(full_names, transform=lambda x: x + 1, image_only=True) |
| 120 | for d, ref in zip(dataset, ref_data): |
| 121 | np.testing.assert_allclose(d, ref + 1, atol=1e-3) |
| 122 | |
| 123 | # loading image/label, with meta |
| 124 | dataset = ImageDataset( |
| 125 | full_names, |
| 126 | transform=lambda x: x + 1, |
| 127 | seg_files=full_names, |
| 128 | seg_transform=lambda x: x + 2, |
| 129 | image_only=False, |
| 130 | ) |
| 131 | for d_tuple, ref in zip(dataset, ref_data): |
| 132 | img, seg, meta, seg_meta = d_tuple |
| 133 | np.testing.assert_allclose(img, ref + 1, atol=1e-3) |
| 134 | np.testing.assert_allclose(seg, ref + 2, atol=1e-3) |
| 135 | np.testing.assert_allclose(meta["original_affine"], np.eye(4), atol=1e-3) |
| 136 | np.testing.assert_allclose(seg_meta["original_affine"], np.eye(4), atol=1e-3) |
| 137 | |
| 138 | # loading image/label, with meta |
| 139 | dataset = ImageDataset( |
| 140 | image_files=full_names, |
| 141 | seg_files=full_names, |
nothing calls this directly
no test coverage detected