Skip to content

fix(metrics): count true positives for bool inputs in confusion matrix and F-beta score - #9189

Open
tritsystem wants to merge 2 commits into
Project-MONAI:devfrom
tritsystem:fix/confusion-matrix-bool-inputs
Open

tritsystem wants to merge 2 commits into
Project-MONAI:devfrom
tritsystem:fix/confusion-matrix-bool-inputs

Conversation

@tritsystem

Copy link
Copy Markdown
Contributor

Files: monai/metrics/confusion_matrix.py (get_confusion_matrix) and monai/metrics/f_beta_score.py (get_f_beta_score). Both count true positives with (y_pred + y) == 2, which works for float/int tensors. If both y_pred and y are bool tensors, PyTorch's + does logical OR, not addition, so True + True gives True (1), never 2 -- the true-positive count comes out as zero every time for bool inputs.

Impact: a perfect bool prediction (identical bool masks) scores sensitivity 0, precision NaN, F1 0, F-beta 0 -- a perfect match graded as a total failure. The same perfect prediction in float form correctly scores 1.0. This only triggers when both inputs are bool; if either is float, PyTorch promotes the other and it works. Checked every other metric in MONAI (Dice, MeanIoU, GeneralizedDice, Hausdorff, surface distance) -- none have this pattern, so it's isolated to these two functions.

This is a regression: #7187 (merged Nov 2023) removed the .float() casts from both functions as "unnecessary".

Fix: cast bool inputs to float at the top of both functions; float/int inputs are unchanged.

Types of changes

  • Non-breaking change
  • New tests added to cover the changes
  • In-line docstrings updated
  • Documentation updated

Fixes # .

…atrix

get_confusion_matrix finds true positives with (y_pred + y) == 2. For bool
tensors that sum is a logical OR, which is never 2, so bool masks such as
pred > 0.5 lost every true positive: a perfect prediction scored
sensitivity 0 and F1 0. Cast bool inputs to float before counting; float
and integer inputs are unchanged.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: tritsystem <gbranaa4@gmail.com>
get_f_beta_score has the same (y_pred + y) == 2 true-positive test as
get_confusion_matrix; both lost their float casts in Project-MONAI#7187. For bool masks
the sum is a logical OR, so every true positive was dropped and a perfect
prediction scored F-beta 0. Cast bool inputs to float before counting, as
in the previous commit; float and integer inputs are unchanged.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: tritsystem <gbranaa4@gmail.com>
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: Project-MONAI/MONAI/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b5ccfb1d-6eef-4488-a18b-256478bef57e
📥 Commits

Reviewing files that changed from the base of the PR and between 6aa4abf and ba59ee7.

📒 Files selected for processing: 4
  • monai/metrics/confusion_matrix.py
  • monai/metrics/f_beta_score.py
  • tests/metrics/test_compute_confusion_matrix.py
  • tests/metrics/test_compute_f_beta.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

📝 Walkthrough

get_confusion_matrix and get_f_beta_score now convert Boolean inputs to floats before calculating counts. Tests compare results for Boolean and float inputs, with and without background. The F-beta tests also check identical Boolean predictions and targets.

Priority: ➖ Normal

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

Merge Risk: ⚪ Minimal · up to ba59e

Boolean inputs are handled consistently by both metrics, with tests covering their results against float inputs. No material merge risk is identified; the change is ready for normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the fix for true-positive counting with Boolean inputs in the confusion matrix and F-beta score metrics.
Description check Passed The description explains the defect, impact, root cause, scope, fix, and added tests. It omits the issue number and does not report integration or quick test results, but it is otherwise sufficiently …
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.
Full details: Description check

Explanation

The description explains the defect, impact, root cause, scope, fix, and added tests. It omits the issue number and does not report integration or quick test results, but it is otherwise sufficiently complete.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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.

This branch has not been deployed

No deployments
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.

1 participant