-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat: print packages to install #8240
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
from __future__ import annotations | ||
|
||
import re | ||
from collections.abc import Mapping, Sequence | ||
from typing import TYPE_CHECKING, Any, cast | ||
|
||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please also include the test case in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @KumoLiu, should it be added as a separate class? |
||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest you modify
OptionalImportError
to have a member recording the name of the package that isn't present so you don't have to do a re search. This would be used here like thisraise OptionalImportError(err_msg, pkg_name)
in place of the original line. The other place this is raised is here where the second argument could just beNone
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ericspod, If I understand you correctly:
raise OptionalImportError(err_msg, pkg_name)
raise OptionalImportError(f"No ImageWriter backend found for {fmt}.", None)
OptionalImportError
module in monai/utils/module.py by adding a functionality to track package names that aren't present?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's it, that'll allow you to get the name of the package without assumptions about the exception's error string, it'll be a more robust way of doing this going forward. Thanks!