From f9f560fbf74d45ee2dc11ea92491584beb8979b7 Mon Sep 17 00:00:00 2001 From: Jiaqi-Lv <60471431+Jiaqi-Lv@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:12:39 +0000 Subject: [PATCH 1/2] Refine error messages --- tests/test_patch_extraction.py | 18 ++++++++++++++++++ tiatoolbox/tools/patchextraction.py | 8 ++++++-- tiatoolbox/utils/misc.py | 2 +- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/tests/test_patch_extraction.py b/tests/test_patch_extraction.py index 5f7441f20..8b6d8e047 100644 --- a/tests/test_patch_extraction.py +++ b/tests/test_patch_extraction.py @@ -663,3 +663,21 @@ def test_mask_based_patch_extractor_ndpi( len_region1 = len(patches) assert len_all > len_region2 > len_region1 + + +def test_invalid_points_type() -> None: + """Test invalid locations_list type for PointsPatchExtractor.""" + img = np.zeros((256, 256, 3)) + coords = [[10, 10]] + with pytest.raises( + TypeError, + match="Please input correct locations_list", + ): + patchextraction.get_patch_extractor( + "point", input_img=img, locations_list=coords, patch_size=38 + ) + + patches = patchextraction.get_patch_extractor( + "point", input_img=img, locations_list=np.array(coords), patch_size=38 + ) + assert len(patches) > 0 diff --git a/tiatoolbox/tools/patchextraction.py b/tiatoolbox/tools/patchextraction.py index 704e70e40..f8a144f9e 100644 --- a/tiatoolbox/tools/patchextraction.py +++ b/tiatoolbox/tools/patchextraction.py @@ -10,7 +10,7 @@ from tiatoolbox import logger from tiatoolbox.utils import misc -from tiatoolbox.utils.exceptions import MethodNotSupportedError +from tiatoolbox.utils.exceptions import FileNotSupportedError, MethodNotSupportedError from tiatoolbox.utils.visualization import AnnotationRenderer from tiatoolbox.wsicore import wsireader @@ -772,7 +772,11 @@ def __init__( pad_constant_values=pad_constant_values, within_bound=within_bound, ) - self.locations_df = misc.read_locations(input_table=locations_list) + try: + self.locations_df = misc.read_locations(input_table=locations_list) + except (TypeError, FileNotSupportedError) as exc: + msg = "Please input correct locations_list" + raise TypeError(msg) from exc self.locations_df["x"] = self.locations_df["x"] - int( (self.patch_size[1] - 1) / 2, ) diff --git a/tiatoolbox/utils/misc.py b/tiatoolbox/utils/misc.py index 7c1c349e7..f8703673b 100644 --- a/tiatoolbox/utils/misc.py +++ b/tiatoolbox/utils/misc.py @@ -540,7 +540,7 @@ def read_locations( if isinstance(input_table, pd.DataFrame): return __assign_unknown_class(input_table) - msg = "Please input correct image path or an ndarray image." + msg = "File type not supported." raise TypeError(msg) From 3f8b70e3a42d4bf58ccda829ce896a4cdeb35b41 Mon Sep 17 00:00:00 2001 From: Jiaqi-Lv <60471431+Jiaqi-Lv@users.noreply.github.com> Date: Thu, 28 Nov 2024 16:09:26 +0000 Subject: [PATCH 2/2] Update error message --- tests/test_patch_extraction.py | 4 +++- tiatoolbox/tools/patchextraction.py | 3 ++- tiatoolbox/utils/misc.py | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/test_patch_extraction.py b/tests/test_patch_extraction.py index 8b6d8e047..2609b9a48 100644 --- a/tests/test_patch_extraction.py +++ b/tests/test_patch_extraction.py @@ -669,9 +669,11 @@ def test_invalid_points_type() -> None: """Test invalid locations_list type for PointsPatchExtractor.""" img = np.zeros((256, 256, 3)) coords = [[10, 10]] + msg = "Please input correct locations_list. " + msg += "Supported types: np.ndarray, DataFrame, str, Path." with pytest.raises( TypeError, - match="Please input correct locations_list", + match=msg, ): patchextraction.get_patch_extractor( "point", input_img=img, locations_list=coords, patch_size=38 diff --git a/tiatoolbox/tools/patchextraction.py b/tiatoolbox/tools/patchextraction.py index f8a144f9e..21fcb4eb0 100644 --- a/tiatoolbox/tools/patchextraction.py +++ b/tiatoolbox/tools/patchextraction.py @@ -775,7 +775,8 @@ def __init__( try: self.locations_df = misc.read_locations(input_table=locations_list) except (TypeError, FileNotSupportedError) as exc: - msg = "Please input correct locations_list" + msg = "Please input correct locations_list. " + msg += "Supported types: np.ndarray, DataFrame, str, Path." raise TypeError(msg) from exc self.locations_df["x"] = self.locations_df["x"] - int( (self.patch_size[1] - 1) / 2, diff --git a/tiatoolbox/utils/misc.py b/tiatoolbox/utils/misc.py index f8703673b..1e02a3ce0 100644 --- a/tiatoolbox/utils/misc.py +++ b/tiatoolbox/utils/misc.py @@ -531,7 +531,7 @@ def read_locations( out_table = pd.read_json(input_table) return __assign_unknown_class(out_table) - msg = "File type not supported." + msg = "File type not supported. Supported types: .npy, .csv, .json" raise FileNotSupportedError(msg) if isinstance(input_table, np.ndarray): @@ -540,7 +540,9 @@ def read_locations( if isinstance(input_table, pd.DataFrame): return __assign_unknown_class(input_table) - msg = "File type not supported." + msg = "File type not supported. " + msg += "Supported types: str, Path, PathLike, np.ndarray, pd.DataFrame" + raise TypeError(msg)