Skip to content

Commit

Permalink
Implement error handling for unsupported modes
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanklut committed Dec 13, 2023
1 parent 6041aa1 commit 84ef480
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion page_xml/output_pageXML.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def generate_single_page(
with AtomicFileName(file_path=sem_seg_output_path) as path:
save_image_array_to_path(str(path), (sem_seg * 128).clip(0, 255).astype(np.uint8))
else:
raise NotImplementedError
raise NotImplementedError(f"Mode {self.mode} not implemented")

# TODO Overwrite when multiple image have the same name but different extension
page.save_xml()
Expand Down
2 changes: 1 addition & 1 deletion page_xml/xml_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,6 @@ def get_regions(self) -> list[str]:
elif self.mode == "top_bottom":
remaining_regions.extend(["top", "bottom"])
else:
raise NotImplementedError
raise NotImplementedError(f"Mode {self.mode} not implemented")

return remaining_regions
2 changes: 1 addition & 1 deletion utils/copy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ def copy_mode(path: str | Path, destination: str | Path, mode: str = "copy") ->
elif mode == "symlink":
symlink_force(path, destination)
else:
raise NotImplementedError
raise NotImplementedError(f"Mode {mode} not implemented")
4 changes: 2 additions & 2 deletions utils/input_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ def clean_input_paths(
elif isinstance(path, Path):
output.append(path)
else:
raise NotImplementedError
raise TypeError(f"Input path {path} is not a str or Path")
else:
raise NotImplementedError
raise TypeError(f"Input paths {input_paths} is not a str, Path or Sequence")

return output

Expand Down
2 changes: 1 addition & 1 deletion xml_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def reset(self):
TypeError: number of classes has not been set
"""
if self._num_classes is None:
raise TypeError
raise TypeError("Must set number of classes")

self._conf_matrix = np.zeros((self._num_classes + 1, self._num_classes + 1), dtype=np.int64)
self._b_conf_matrix = np.zeros((self._num_classes + 1, self._num_classes + 1), dtype=np.int64)
Expand Down
22 changes: 11 additions & 11 deletions xml_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(
elif self.output_type == "overlay":
self.save_function = self.save_overlay_image
else:
raise NotImplementedError
raise NotImplementedError(f"Output type {self.output_type} not implemented")

def save_gray_image(self, xml_path_i: Path):
"""
Expand All @@ -96,12 +96,12 @@ def save_gray_image(self, xml_path_i: Path):
xml_path_i (Path): single pageXML path
"""
output_image_path = self.output_dir.joinpath(xml_path_i.stem + ".png")
gray_image = self.xml_converter.to_sem_seg(xml_path_i)
sem_seg = self.xml_converter.to_sem_seg(xml_path_i)

if gray_image is None:
raise ValueError
if sem_seg is None:
raise ValueError(f"Could not convert {xml_path_i} to sem_seg image")

save_image_array_to_path(str(output_image_path), gray_image)
save_image_array_to_path(str(output_image_path), sem_seg)

def save_color_image(self, xml_path_i: Path):
"""
Expand All @@ -111,19 +111,19 @@ def save_color_image(self, xml_path_i: Path):
xml_path_i (Path): single pageXML path
"""
output_image_path = self.output_dir.joinpath(xml_path_i.stem + ".png")
gray_image = self.xml_converter.to_sem_seg(xml_path_i)
sem_seg = self.xml_converter.to_sem_seg(xml_path_i)

if gray_image is None:
raise ValueError
if sem_seg is None:
raise ValueError(f"Could not convert {xml_path_i} to sem_seg image")

color_image = np.empty((*gray_image.shape, 3), dtype=np.uint8)
color_image = np.empty((*sem_seg.shape, 3), dtype=np.uint8)

colors = self.metadata.get("stuff_colors")
assert colors is not None, "Can't make color images without colors"
assert np.max(gray_image) < len(colors), "Not enough colors, grayscale has too many classes"
assert np.max(sem_seg) < len(colors), "Not enough colors, grayscale has too many classes"

for i, color in enumerate(colors):
color_image[gray_image == i] = np.asarray(color).reshape((1, 1, 3))
color_image[sem_seg == i] = np.asarray(color).reshape((1, 1, 3))

save_image_array_to_path(str(output_image_path), color_image)

Expand Down

0 comments on commit 84ef480

Please sign in to comment.