Skip to content

Report a missing nnunetv2 as an optional dependency in nnUNetV2Runner - #9054

Open
Kirscher wants to merge 1 commit into
Project-MONAI:devfrom
Kirscher:fix/nnunetv2-runner-require-pkg
Open

Report a missing nnunetv2 as an optional dependency in nnUNetV2Runner#9054
Kirscher wants to merge 1 commit into
Project-MONAI:devfrom
Kirscher:fix/nnunetv2-runner-require-pkg

Conversation

@Kirscher

Copy link
Copy Markdown
Contributor

Found while investigating #7694.

Description

nnUNetV2Runner.__init__ imports nnunetv2.configuration directly:

https://github.com/Project-MONAI/MONAI/blob/dev/monai/apps/nnunet/nnunetv2_runner.py#L220

so constructing the runner without nnunetv2 installed raises a bare ModuleNotFoundError, rather than the OptionalImportError MONAI raises for its other optional dependencies. Every other third-party import in that module goes through optional_import.

It is also actively misleading. The dataset lookup immediately above is wrapped in a broad except Exception, so it swallows that same ImportError first and logs:

WARNING - Dataset with name/ID: 123 cannot be found in the record. Please ignore the
message above if you are running the pipeline from a fresh start. But if the dataset is
expected to be found, please check your input_config.

A user whose only problem is that nnunetv2 is not installed is told to check their configuration, and only then gets an unrelated ModuleNotFoundError.

This decorates the class with @require_pkg(pkg_name="nnunetv2"), the same pattern used for ITKReader, NibabelReader, PILReader and others. Construction now fails immediately with an OptionalImportError naming the package and pointing at the installation docs, and the misleading dataset warning is no longer emitted. Behaviour with nnunetv2 installed 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 on dev and pass with the change. They simulate the missing package via mock.patch, so they run whether or not nnunetv2 is installed — which matters here, since nnunetv2 is not currently a declared test dependency.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

`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>
Copilot AI lite review requested due to automatic review settings August 10, 2026 20:07
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The change imports require_pkg and applies it to nnUNetV2Runner for nnunetv2 availability checks. A test covers the unavailable-dependency case, verifies OptionalImportError, checks that no dataset warning is logged, and cleans up temporary resources.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: reporting missing nnunetv2 as an optional dependency in nnUNetV2Runner.
Description check ✅ Passed The description explains the problem, solution, impact, and regression tests, and includes the applicable change type.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
tests/apps/nnunet/test_nnunetv2_runner_optional_import.py (1)

26-37: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Add fixture method docstrings.

  • tests/apps/nnunet/test_nnunetv2_runner_optional_import.py#L26-L37: Add a Google-style docstring for setUp.
  • tests/apps/nnunet/test_nnunetv2_runner_optional_import.py#L53-L54: Add a Google-style docstring for tearDown.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 87060c4 and 494c2dc.

📒 Files selected for processing (2)
  • monai/apps/nnunet/nnunetv2_runner.py
  • tests/apps/nnunet/test_nnunetv2_runner_optional_import.py

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 nnUNetV2Runner with @require_pkg(pkg_name="nnunetv2") so construction reports a missing dependency as OptionalImportError.
  • Adds regression tests to validate the exception type and ensure no misleading dataset warning is emitted when nnunetv2 is 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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants