Skip to content
Open
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
17 changes: 16 additions & 1 deletion monai/data/image_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,22 @@ def resolve_writer(ext_name, error_if_not_found=True) -> Sequence:
except Exception: # other writer init errors indicating it exists
avail_writers.append(_writer)
if not avail_writers and error_if_not_found:
raise OptionalImportError(f"No ImageWriter backend found for {fmt}.")
install_hints: dict = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this dictionary into a global variable at the top of this file. A name like FILETYPE_HINT or something else sticking to the all-caps convention would work. This keeps the dictionary easily found and modifiable at runtime.

"nii": "nibabel",
"nii.gz": "nibabel",
"mha": "itk",
"mhd": "itk",
"nrrd": "itk",
"png": "pillow",
"jpg": "pillow",
"jpeg": "pillow",
"tif": "pillow",
"tiff": "pillow",
"bmp": "pillow",
}
hint = install_hints.get(fmt)
extra = f" Try installing the required package: pip install {hint}" if hint else ""
raise OptionalImportError(f"No ImageWriter backend found for '{fmt}'.{extra}")
Comment on lines +119 to +134
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add regression tests for the new install-hint behavior.

Line 119 through Line 134 changes user-facing error output and format mapping, but no tests are included here. Please add cases for nii.gz -> nibabel, png -> pillow, nrrd -> itk, and unknown extensions (no hint).

As per coding guidelines, "Ensure new or modified definitions will be covered by existing or new unit tests."

🧰 Tools
🪛 Ruff (0.15.2)

[warning] 134-134: Avoid specifying long messages outside the exception class

(TRY003)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@monai/data/image_writer.py` around lines 119 - 134, Add unit tests for the
new install-hint behavior in monai.data.image_writer: exercise the mapping
stored in install_hints by calling the ImageWriter error path (the code that
raises OptionalImportError) with formats "nii.gz", "png", "nrrd" and an unknown
extension (e.g., "abc"); for each of the first three assert the raised
OptionalImportError message contains the expected package string ("nibabel",
"pillow", "itk" respectively) and for the unknown extension assert the message
does not contain a "pip install" hint; ensure tests reference the image_writer
module and OptionalImportError so they fail if the mapping or message changes.

writer_tuple = ensure_tuple(avail_writers)
SUPPORTED_WRITERS[fmt] = writer_tuple
return writer_tuple
Expand Down
Loading