Skip to content

Commit e35f6fd

Browse files
yashnaiduuyashnaiduu
authored andcommitted
Refactor filetype hints for ImageWriter backendsrefactor: move install hints to module-level FILETYPE_HINT constant
Signed-off-by: YASH NAIDU <152394598+yashnaiduu@users.noreply.github.com> Signed-off-by: yashnaiduu <yashnaidu@example.com>
1 parent 6a5238f commit e35f6fd

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

monai/data/image_writer.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@
6363

6464
SUPPORTED_WRITERS: dict = {}
6565

66+
# Maps common file extensions to the package needed by ImageWriter backends.
67+
FILETYPE_HINT: dict[str, str] = {
68+
"nii": "nibabel",
69+
"nii.gz": "nibabel",
70+
"mha": "itk",
71+
"mhd": "itk",
72+
"nrrd": "itk",
73+
"png": "pillow",
74+
"jpg": "pillow",
75+
"jpeg": "pillow",
76+
"tif": "pillow",
77+
"tiff": "pillow",
78+
"bmp": "pillow",
79+
}
80+
6681

6782
def register_writer(ext_name, *im_writers):
6883
"""
@@ -116,21 +131,8 @@ def resolve_writer(ext_name, error_if_not_found=True) -> Sequence:
116131
except Exception: # other writer init errors indicating it exists
117132
avail_writers.append(_writer)
118133
if not avail_writers and error_if_not_found:
119-
install_hints: dict = {
120-
"nii": "nibabel",
121-
"nii.gz": "nibabel",
122-
"mha": "itk",
123-
"mhd": "itk",
124-
"nrrd": "itk",
125-
"png": "pillow",
126-
"jpg": "pillow",
127-
"jpeg": "pillow",
128-
"tif": "pillow",
129-
"tiff": "pillow",
130-
"bmp": "pillow",
131-
}
132-
hint = install_hints.get(fmt)
133-
extra = f" Try installing the required package: pip install {hint}" if hint else ""
134+
hint = FILETYPE_HINT.get(fmt)
135+
extra = f" Try: pip install {hint}" if hint else ""
134136
raise OptionalImportError(f"No ImageWriter backend found for '{fmt}'.{extra}")
135137
writer_tuple = ensure_tuple(avail_writers)
136138
SUPPORTED_WRITERS[fmt] = writer_tuple

0 commit comments

Comments
 (0)