Skip to content

Commit

Permalink
feat: print packages to install
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorvp committed Nov 26, 2024
1 parent 649c7c8 commit deff1e9
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions monai/data/image_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from __future__ import annotations

import re
from collections.abc import Mapping, Sequence
from typing import TYPE_CHECKING, Any, cast

Expand Down Expand Up @@ -106,17 +107,23 @@ def resolve_writer(ext_name, error_if_not_found=True) -> Sequence:
if fmt.startswith("."):
fmt = fmt[1:]
avail_writers = []
required_pkg = []
default_writers = SUPPORTED_WRITERS.get(EXT_WILDCARD, ())
for _writer in look_up_option(fmt, SUPPORTED_WRITERS, default=default_writers):
try:
_writer() # this triggers `monai.utils.module.require_pkg` to check the system availability
avail_writers.append(_writer)
except OptionalImportError:
except OptionalImportError as e:
error_match = re.search(r"`(.*?)`", str(e))
if error_match:
required_pkg.append(error_match.group(1))
continue
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}.")
raise OptionalImportError(
f"No ImageWriter backend found for {fmt}, install one of the required packages in {required_pkg}."
)
writer_tuple = ensure_tuple(avail_writers)
SUPPORTED_WRITERS[fmt] = writer_tuple
return writer_tuple
Expand Down

0 comments on commit deff1e9

Please sign in to comment.