Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print what package should be installed when suitable writer is missing #8001

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions monai/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@
"pytorch_after",
]

WRITER_PACKAGE_MAP: dict[str, list[str]] = {
"png": ["pillow"],
"jpg": ["pillow"],
"jpeg": ["pillow"],
"nii": ["nibabel"],
"nii.gz": ["nibabel"],
"dcm": ["pydicom"],
# Add more mappings as needed
Copy link
Contributor

Choose a reason for hiding this comment

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

Who is supposed to add the rest of the entries on this list? ITK supports quite a few, including all of the ones you listed above.

Copy link
Member

Choose a reason for hiding this comment

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

We can add ITK for a number of these types though the listed libraries are the primary ones used. If you can identify what other libraries load each of these types (and other types that can be loaded) please add them here as well.

}


def look_up_option(
opt_str: Hashable,
Expand Down Expand Up @@ -335,6 +345,20 @@ class OptionalImportError(ImportError):
Could not import APIs from an optional dependency.
"""

def __init__(self, msg: str = "") -> None:
super().__init__(msg)
self.msg = msg

def __str__(self) -> str:
original_msg = super().__str__()
if "ImageWriter" in original_msg and "backend found for" in original_msg:
ext = original_msg.split("for ")[-1].strip(".")
suggested_packages = WRITER_PACKAGE_MAP.get(ext, [])
if suggested_packages:
package_list = ", ".join(suggested_packages)
return f"{original_msg} Please install one of the following packages: {package_list}"
return original_msg


def optional_import(
module: str,
Expand Down
Loading