Skip to content

Handle constant B-spline mutual information inputs#9016

Closed
yinkev wants to merge 1 commit into
Project-MONAI:devfrom
yinkev:fix/global-mi-constant-bspline
Closed

Handle constant B-spline mutual information inputs#9016
yinkev wants to merge 1 commit into
Project-MONAI:devfrom
yinkev:fix/global-mi-constant-bspline

Conversation

@yinkev

@yinkev yinkev commented Jul 25, 2026

Copy link
Copy Markdown

Closes #9018.

Description

B-spline global mutual information now remains finite for constant prediction or target tensors, rejects bin counts that cannot accommodate the required spline padding, and promotes half-precision spline distances before the cubic basis calculation.

Constant tensors have zero intensity range, so the previous bin width was zero and normalization produced NaNs. Separately, the cubic polynomial could overflow for float16 distances, while num_bins <= 4 made the padded interior denominator zero or negative.

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.

Validation

  • float32 and float16 constant-input regressions fail on upstream/dev and pass with the fix;
  • QUICKTEST=true python -m pytest tests/losses/image_dissimilarity/test_global_mutual_information_loss.py -q: 15 passed, 2 skipped;
  • ruff check on both touched files;
  • git diff --check.

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

GlobalMutualInformationLoss now requires more than four bins for the b-spline kernel. Its Parzen windowing path handles zero intensity ranges and casts float16 intermediate distances to float32. Tests cover invalid bin counts, constant full-precision inputs, constant half-precision inputs, finite losses, and finite gradients.

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

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title is concise and clearly reflects the main fix for constant B-spline mutual information inputs.
Description check ✅ Passed The description matches the template well, with description, change types, and validation details included.
✨ 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)
monai/losses/image_dissimilarity.py (1)

217-217: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Complete Google-style documentation for the added definitions.

  • monai/losses/image_dissimilarity.py#L217-L217: add a Raises section for invalid bin counts.
  • monai/losses/image_dissimilarity.py#L234-L235: document the b-spline-specific validation.
  • tests/losses/image_dissimilarity/test_global_mutual_information_loss.py#L171-L196: add docstrings to the new test class and methods.
🤖 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 `@monai/losses/image_dissimilarity.py` at line 217, The documentation for
GlobalMutualInformationLoss is incomplete: add a Google-style Raises section at
monai/losses/image_dissimilarity.py lines 217-217 covering invalid bin counts,
and document the b-spline-specific validation at lines 234-235. Add Google-style
docstrings to the new test class and each method in
tests/losses/image_dissimilarity/test_global_mutual_information_loss.py lines
171-196, describing the behavior each test verifies.

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 `@monai/losses/image_dissimilarity.py`:
- Line 217: The documentation for GlobalMutualInformationLoss is incomplete: add
a Google-style Raises section at monai/losses/image_dissimilarity.py lines
217-217 covering invalid bin counts, and document the b-spline-specific
validation at lines 234-235. Add Google-style docstrings to the new test class
and each method in
tests/losses/image_dissimilarity/test_global_mutual_information_loss.py lines
171-196, describing the behavior each test verifies.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ff583b21-a14a-43a8-85b2-78f57eb84094

📥 Commits

Reviewing files that changed from the base of the PR and between 3ee058b and dc3b7f0.

📒 Files selected for processing (2)
  • monai/losses/image_dissimilarity.py
  • tests/losses/image_dissimilarity/test_global_mutual_information_loss.py

@yinkev
yinkev force-pushed the fix/global-mi-constant-bspline branch from dc3b7f0 to e5a119c Compare July 25, 2026 18:34
Signed-off-by: kyinhub <kevinpyin@gmail.com>
@yinkev
yinkev force-pushed the fix/global-mi-constant-bspline branch from e5a119c to 11ee2f4 Compare July 25, 2026 18:44
@yinkev

yinkev commented Jul 25, 2026

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@yinkev

yinkev commented Jul 25, 2026

Copy link
Copy Markdown
Author

Replaced by #9022 to conform to this repository's documented contributor workflow and branch-naming convention. The replacement uses the same signed commit and identical code diff: #9022

@yinkev yinkev closed this Jul 25, 2026
@yinkev
yinkev deleted the fix/global-mi-constant-bspline branch July 25, 2026 19:48
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.

GlobalMutualInformationLoss returns NaN for constant B-spline inputs

1 participant