Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Refine PatchExtractor Error Message (TissueImageAnalytics#883)
Browse files Browse the repository at this point in the history
- Fix Misleading error message TissueImageAnalytics#881
  • Loading branch information
Jiaqi-Lv authored Nov 29, 2024
1 parent ca13e7f commit 5f1cecb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
20 changes: 20 additions & 0 deletions tests/test_patch_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,23 @@ 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]]
msg = "Please input correct locations_list. "
msg += "Supported types: np.ndarray, DataFrame, str, Path."
with pytest.raises(
TypeError,
match=msg,
):
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
9 changes: 7 additions & 2 deletions tiatoolbox/tools/patchextraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -772,7 +772,12 @@ 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. "
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,
)
Expand Down
6 changes: 4 additions & 2 deletions tiatoolbox/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -540,7 +540,9 @@ 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. "
msg += "Supported types: str, Path, PathLike, np.ndarray, pd.DataFrame"

raise TypeError(msg)


Expand Down

0 comments on commit 5f1cecb

Please sign in to comment.