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 # .

@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
Commits

Reviewing files that changed from the base of the PR and between ba59ee7 and 67a70ae.

Files selected for processing: 2
  • monai/metrics/confusion_matrix.py
  • tests/metrics/test_compute_confusion_matrix.py

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

Walkthrough

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

Priority: Low

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

Severity of issue fixed: Low

Merge Risk: Minimal · up to 67a70

Boolean metric inputs now follow the same counting behavior as equivalent float inputs, with no actionable merge-blocking risk identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 66.67% 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 summarizes the main fix for Boolean true-positive counting in the confusion matrix and F-beta score.
Description check Passed The description explains the defect, impact, cause, fix, and added tests. It is mostly complete, although the issue number is missing and several template checklist items are omitted.
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 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.

…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>
@tritsystem
tritsystem force-pushed the fix/confusion-matrix-bool-inputs branch from ba59ee7 to 67a70ae Compare October 25, 2026 11:42

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