Report a missing nnunetv2 as an optional dependency in nnUNetV2Runner - #9054
Report a missing nnunetv2 as an optional dependency in nnUNetV2Runner#9054Kirscher wants to merge 1 commit into
Conversation
`nnUNetV2Runner.__init__` imports `nnunetv2.configuration` directly, so
constructing the runner without nnunetv2 installed raises a bare
ModuleNotFoundError rather than MONAI's OptionalImportError. The dataset
lookup just above it is wrapped in a broad `except Exception`, which
swallows that same ImportError first and logs
Dataset with name/ID: 123 cannot be found in the record. ...
please check your input_config.
so the reported cause is the user's configuration, not the missing
package.
Decorate the class with `@require_pkg(pkg_name="nnunetv2")`, as is done
for other optional dependencies (ITKReader, NibabelReader, ...). The
failure now names the package and links the installation docs, and the
misleading dataset warning is no longer emitted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Signed-off-by: Kirscher <tristan.kirscher@gmail.com>
📝 WalkthroughWalkthroughThe change imports Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/apps/nnunet/test_nnunetv2_runner_optional_import.py (1)
26-37: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueAdd fixture method docstrings.
tests/apps/nnunet/test_nnunetv2_runner_optional_import.py#L26-L37: Add a Google-style docstring forsetUp.tests/apps/nnunet/test_nnunetv2_runner_optional_import.py#L53-L54: Add a Google-style docstring fortearDown.As per path instructions, “Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings.”
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/apps/nnunet/test_nnunetv2_runner_optional_import.py` around lines 26 - 37, The setUp method in tests/apps/nnunet/test_nnunetv2_runner_optional_import.py:26-37 needs a Google-style docstring describing its setup variables and return behavior; add that documentation without changing the fixture logic. The tearDown method at tests/apps/nnunet/test_nnunetv2_runner_optional_import.py:53-54 also requires a Google-style docstring describing its cleanup behavior and any relevant return or raised-exception details.Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/apps/nnunet/test_nnunetv2_runner_optional_import.py`:
- Around line 26-37: The setUp method in
tests/apps/nnunet/test_nnunetv2_runner_optional_import.py:26-37 needs a
Google-style docstring describing its setup variables and return behavior; add
that documentation without changing the fixture logic. The tearDown method at
tests/apps/nnunet/test_nnunetv2_runner_optional_import.py:53-54 also requires a
Google-style docstring describing its cleanup behavior and any relevant return
or raised-exception details.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 2da07832-1c38-4fbc-a207-a8018b0a6f4a
📒 Files selected for processing (2)
monai/apps/nnunet/nnunetv2_runner.pytests/apps/nnunet/test_nnunetv2_runner_optional_import.py
There was a problem hiding this comment.
Pull request overview
This PR improves MONAI’s nnUNetV2Runner optional-dependency handling by ensuring missing nnunetv2 fails fast with MONAI’s standard optional-import error, avoiding a misleading dataset lookup warning when the package isn’t installed.
Changes:
- Decorates
nnUNetV2Runnerwith@require_pkg(pkg_name="nnunetv2")so construction reports a missing dependency asOptionalImportError. - Adds regression tests to validate the exception type and ensure no misleading dataset warning is emitted when
nnunetv2is absent.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
monai/apps/nnunet/nnunetv2_runner.py |
Adds require_pkg decorator to standardize missing-nnunetv2 failure behavior. |
tests/apps/nnunet/test_nnunetv2_runner_optional_import.py |
Adds tests covering the missing-package exception and warning behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| DATASET_ID_FORMAT = r"Dataset[0-9]{3}|[0-9]+" # regex format for a valid nnUnet dataset name | ||
|
|
||
|
|
||
| @require_pkg(pkg_name="nnunetv2") |
Found while investigating #7694.
Description
nnUNetV2Runner.__init__importsnnunetv2.configurationdirectly:https://github.com/Project-MONAI/MONAI/blob/dev/monai/apps/nnunet/nnunetv2_runner.py#L220
so constructing the runner without
nnunetv2installed raises a bareModuleNotFoundError, rather than theOptionalImportErrorMONAI raises for its other optional dependencies. Every other third-party import in that module goes throughoptional_import.It is also actively misleading. The dataset lookup immediately above is wrapped in a broad
except Exception, so it swallows that sameImportErrorfirst and logs:A user whose only problem is that
nnunetv2is not installed is told to check their configuration, and only then gets an unrelatedModuleNotFoundError.This decorates the class with
@require_pkg(pkg_name="nnunetv2"), the same pattern used forITKReader,NibabelReader,PILReaderand others. Construction now fails immediately with anOptionalImportErrornaming the package and pointing at the installation docs, and the misleading dataset warning is no longer emitted. Behaviour withnnunetv2installed is unchanged.Two regression tests are added in
tests/apps/nnunet/test_nnunetv2_runner_optional_import.py, one per symptom (wrong exception type, misleading warning). Both fail ondevand pass with the change. They simulate the missing package viamock.patch, so they run whether or notnnunetv2is installed — which matters here, sincennunetv2is not currently a declared test dependency.Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.