Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion monai/apps/pathology/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def __getitem__(self, index):
grid_shape=self.grid_shape,
patch_size=self.patch_size,
)
labels = np.array(sample["label"], dtype=np.float32)[:, np.newaxis, np.newaxis]
labels = np.array(sample["label"], dtype=np.float32)
# expand dimensions to have 4 dimension as batch, class, height, and width.
for _ in range(4 - labels.ndim):
labels = np.expand_dims(labels, 1)
patches = [{"image": images[i], "label": labels[i]} for i in range(len(sample["label"]))]
if self.transform:
patches = self.transform(patches)
Expand Down
53 changes: 40 additions & 13 deletions tests/test_patch_wsi_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"image_reader_name": "cuCIM",
},
[
{"image": np.array([[[239]], [[239]], [[239]]], dtype=np.uint8), "label": np.array([[1]])},
{"image": np.array([[[239]], [[239]], [[239]]], dtype=np.uint8), "label": np.array([[[1]]])},
],
]

Expand All @@ -41,10 +41,10 @@
"image_reader_name": "cuCIM",
},
[
{"image": np.array([[[247]], [[245]], [[248]]], dtype=np.uint8), "label": np.array([[0]])},
{"image": np.array([[[245]], [[247]], [[244]]], dtype=np.uint8), "label": np.array([[0]])},
{"image": np.array([[[246]], [[246]], [[246]]], dtype=np.uint8), "label": np.array([[0]])},
{"image": np.array([[[246]], [[246]], [[246]]], dtype=np.uint8), "label": np.array([[1]])},
{"image": np.array([[[247]], [[245]], [[248]]], dtype=np.uint8), "label": np.array([[[0]]])},
{"image": np.array([[[245]], [[247]], [[244]]], dtype=np.uint8), "label": np.array([[[0]]])},
{"image": np.array([[[246]], [[246]], [[246]]], dtype=np.uint8), "label": np.array([[[0]]])},
{"image": np.array([[[246]], [[246]], [[246]]], dtype=np.uint8), "label": np.array([[[1]]])},
],
]

Expand All @@ -60,10 +60,25 @@
"image_reader_name": "cuCIM",
},
[
{"image": np.array([[[239]], [[239]], [[239]]], dtype=np.uint8), "label": np.array([[1]])},
{"image": np.array([[[239]], [[239]], [[239]]], dtype=np.uint8), "label": np.array([[[1]]])},
],
]

TEST_CASE_3 = [
FILE_URL,
{
"data": [
{"image": "./CMU-1.tiff", "location": [0, 0], "label": [[[0, 1], [1, 0]]]},
],
"region_size": 1,
"grid_shape": 1,
"patch_size": 1,
"image_reader_name": "cuCIM",
},
[
{"image": np.array([[[239]], [[239]], [[239]]], dtype=np.uint8), "label": np.array([[[0, 1], [1, 0]]])},
],
]

TEST_CASE_OPENSLIDE_0 = [
FILE_URL,
Expand All @@ -77,7 +92,7 @@
"image_reader_name": "OpenSlide",
},
[
{"image": np.array([[[239]], [[239]], [[239]]], dtype=np.uint8), "label": np.array([[1]])},
{"image": np.array([[[239]], [[239]], [[239]]], dtype=np.uint8), "label": np.array([[[1]]])},
],
]

Expand All @@ -91,16 +106,23 @@
"image_reader_name": "OpenSlide",
},
[
{"image": np.array([[[247]], [[245]], [[248]]], dtype=np.uint8), "label": np.array([[0]])},
{"image": np.array([[[245]], [[247]], [[244]]], dtype=np.uint8), "label": np.array([[0]])},
{"image": np.array([[[246]], [[246]], [[246]]], dtype=np.uint8), "label": np.array([[0]])},
{"image": np.array([[[246]], [[246]], [[246]]], dtype=np.uint8), "label": np.array([[1]])},
{"image": np.array([[[247]], [[245]], [[248]]], dtype=np.uint8), "label": np.array([[[0]]])},
{"image": np.array([[[245]], [[247]], [[244]]], dtype=np.uint8), "label": np.array([[[0]]])},
{"image": np.array([[[246]], [[246]], [[246]]], dtype=np.uint8), "label": np.array([[[0]]])},
{"image": np.array([[[246]], [[246]], [[246]]], dtype=np.uint8), "label": np.array([[[1]]])},
],
]


class TestPatchWSIDataset(unittest.TestCase):
@parameterized.expand([TEST_CASE_0, TEST_CASE_1, TEST_CASE_2])
@parameterized.expand(
[
TEST_CASE_0,
TEST_CASE_1,
TEST_CASE_2,
TEST_CASE_3,
]
)
@skipUnless(has_cim, "Requires CuCIM")
def test_read_patches_cucim(self, file_url, input_parameters, expected):
self.camelyon_data_download(file_url)
Expand All @@ -112,7 +134,12 @@ def test_read_patches_cucim(self, file_url, input_parameters, expected):
self.assertIsNone(assert_array_equal(samples[i]["label"], expected[i]["label"]))
self.assertIsNone(assert_array_equal(samples[i]["image"], expected[i]["image"]))

@parameterized.expand([TEST_CASE_OPENSLIDE_0, TEST_CASE_OPENSLIDE_1])
@parameterized.expand(
[
TEST_CASE_OPENSLIDE_0,
TEST_CASE_OPENSLIDE_1,
]
)
@skipUnless(has_osl, "Requires OpenSlide")
def test_read_patches_openslide(self, file_url, input_parameters, expected):
self.camelyon_data_download(file_url)
Expand Down