supervision icon indicating copy to clipboard operation
supervision copied to clipboard

[Bug]: Fix RuntimeWarning division by zero in F1Score computation

Open Shehzad3684 opened this issue 4 months ago • 1 comments

Search before asking

  • [x] I have searched the issues and discussions and found no similar bug report.

Bug

While running the test suite with default warning flags (pytest -W default), I noticed a RuntimeWarning: invalid value encountered in divide coming from src/supervision/metrics/f1_score.py:509. Root Cause: f1_score = np.where(denominator == 0, 0, 2 * true_positives / denominator) Because np.where evaluates both branches eagerly before masking, division by zero still occurs during intermediate evaluation when denominator contains zeros.

Proposed Fix: Replace eager division with np.divide using explicit out= and where= masking so division by zero is never calculated in memory. I am submitting a PR to fix this now.

Environment

  • Supervision: 0.30.0.dev0 (latest develop branch)
  • Python: 3.11.9
  • OS: Windows 11

Minimal Reproducible Example

# 1. You can reproduce this instantly on the latest develop branch by running the existing test suite with warnings enabled:
# pytest tests/metrics/test_f1_score.py -k test_medium_bucket_scores_target_matched_small_prediction -W default

# 2. Minimal standalone code reproducing the eager evaluation bug at line 509:
import numpy as np

true_positives = np.array([0.0, 1.0])
denominator = np.array([0.0, 2.0])

# This throws: RuntimeWarning: invalid value encountered in divide
# Because np.where evaluates the division across the entire array BEFORE applying the mask:
f1_score = np.where(denominator == 0, 0, 2 * true_positives / denominator)

Are you willing to submit a PR?

  • [x] Yes I'd like to help by submitting a PR!

Shehzad3684 avatar Jul 16 '26 11:07 Shehzad3684

@Shehzad3684 @vikassaini77, it seems you accidentally duplicated the same work. Regardless how I love how much you want to contribute, pls next time wait for the issue assignment... 🦝

Borda avatar Jul 17 '26 08:07 Borda