From f005ea1d17b141733f8a5fe040cb13542ea91923 Mon Sep 17 00:00:00 2001 From: jirka <6035284+Borda@users.noreply.github.com> Date: Mon, 2 Feb 2026 09:11:24 +0100 Subject: [PATCH] Convert `supervision/metrics/detection.py` docstrings to doctests --- Co-authored-by: copilot-swe-agent[bot] <198982749+copilot@users.noreply.github.com> --- supervision/metrics/detection.py | 227 +++++++++++++------------------ 1 file changed, 95 insertions(+), 132 deletions(-) diff --git a/supervision/metrics/detection.py b/supervision/metrics/detection.py index bc393469..67c3a798 100644 --- a/supervision/metrics/detection.py +++ b/supervision/metrics/detection.py @@ -115,34 +115,30 @@ class ConfusionMatrix: Returns: ConfusionMatrix: New instance of ConfusionMatrix. - Example: - ```python - import supervision as sv - - targets = [ - sv.Detections(...), - sv.Detections(...) - ] - - predictions = [ - sv.Detections(...), - sv.Detections(...) - ] - - confusion_matrix = sv.ConfusionMatrix.from_detections( - predictions=predictions, - targets=target, - classes=['person', ...] - ) - - print(confusion_matrix.matrix) - # np.array([ - # [0., 0., 0., 0.], - # [0., 1., 0., 1.], - # [0., 1., 1., 0.], - # [1., 1., 0., 0.] - # ]) - ``` + Examples: + >>> import numpy as np + >>> import supervision as sv + >>> targets = [ + ... sv.Detections( + ... xyxy=np.array([[0, 0, 10, 10]]), + ... class_id=np.array([0]) + ... ) + ... ] + >>> predictions = [ + ... sv.Detections( + ... xyxy=np.array([[0, 0, 10, 10]]), + ... class_id=np.array([0]), + ... confidence=np.array([0.9]) + ... ) + ... ] + >>> confusion_matrix = sv.ConfusionMatrix.from_detections( + ... predictions=predictions, + ... targets=targets, + ... classes=['person'] + ... ) + >>> confusion_matrix.matrix + array([[1., 0.], + [0., 0.]]) """ prediction_tensors = [] @@ -190,50 +186,32 @@ class ConfusionMatrix: Returns: ConfusionMatrix: New instance of ConfusionMatrix. - Example: - ```python - import supervision as sv - import numpy as np - - targets = ( - [ - np.array( - [ - [0.0, 0.0, 3.0, 3.0, 1], - [2.0, 2.0, 5.0, 5.0, 1], - [6.0, 1.0, 8.0, 3.0, 2], - ] - ), - np.array([1.0, 1.0, 2.0, 2.0, 2]), - ] - ) - - predictions = [ - np.array( - [ - [0.0, 0.0, 3.0, 3.0, 1, 0.9], - [0.1, 0.1, 3.0, 3.0, 0, 0.9], - [6.0, 1.0, 8.0, 3.0, 1, 0.8], - [1.0, 6.0, 2.0, 7.0, 1, 0.8], - ] - ), - np.array([[1.0, 1.0, 2.0, 2.0, 2, 0.8]]) - ] - - confusion_matrix = sv.ConfusionMatrix.from_tensors( - predictions=predictions, - targets=targets, - classes=['person', ...] - ) - - print(confusion_matrix.matrix) - # np.array([ - # [0., 0., 0., 0.], - # [0., 1., 0., 1.], - # [0., 1., 1., 0.], - # [1., 1., 0., 0.] - # ]) - ``` + Examples: + >>> import supervision as sv + >>> import numpy as np + >>> targets = [ + ... np.array([ + ... [0.0, 0.0, 3.0, 3.0, 0], + ... [2.0, 2.0, 5.0, 5.0, 0], + ... [6.0, 1.0, 8.0, 3.0, 1], + ... ]) + ... ] + >>> predictions = [ + ... np.array([ + ... [0.0, 0.0, 3.0, 3.0, 0, 0.9], + ... [0.1, 0.1, 3.0, 3.0, 0, 0.9], + ... [6.0, 1.0, 8.0, 3.0, 1, 0.8], + ... ]) + ... ] + >>> confusion_matrix = sv.ConfusionMatrix.from_tensors( + ... predictions=predictions, + ... targets=targets, + ... classes=['person', 'dog'] + ... ) + >>> confusion_matrix.matrix + array([[1., 0., 1.], + [0., 1., 0.], + [1., 0., 0.]]) """ validate_input_tensors(predictions, targets) @@ -546,28 +524,28 @@ class MeanAveragePrecision: Returns: MeanAveragePrecision: New instance of ConfusionMatrix. - Example: - ```python - import supervision as sv - - targets = [ - sv.Detections(...), - sv.Detections(...) - ] - - predictions = [ - sv.Detections(...), - sv.Detections(...) - ] - - mean_average_precision = sv.MeanAveragePrecision.from_detections( - predictions=predictions, - targets=target, - ) - - print(mean_average_precison.map50_95) - # 0.2899 - ``` + Examples: + >>> import numpy as np + >>> import supervision as sv + >>> targets = [ + ... sv.Detections( + ... xyxy=np.array([[0, 0, 10, 10]]), + ... class_id=np.array([0]) + ... ) + ... ] + >>> predictions = [ + ... sv.Detections( + ... xyxy=np.array([[0, 0, 10, 10]]), + ... class_id=np.array([0]), + ... confidence=np.array([0.9]) + ... ) + ... ] + >>> mAP = sv.MeanAveragePrecision.from_detections( + ... predictions=predictions, + ... targets=targets, + ... ) + >>> round(float(mAP.map50), 2) + 0.99 """ prediction_tensors = [] target_tensors = [] @@ -650,44 +628,29 @@ class MeanAveragePrecision: Returns: MeanAveragePrecision: New instance of MeanAveragePrecision. - Example: - ```python - import supervision as sv - import numpy as np - - targets = ( - [ - np.array( - [ - [0.0, 0.0, 3.0, 3.0, 1], - [2.0, 2.0, 5.0, 5.0, 1], - [6.0, 1.0, 8.0, 3.0, 2], - ] - ), - np.array([[1.0, 1.0, 2.0, 2.0, 2]]), - ] - ) - - predictions = [ - np.array( - [ - [0.0, 0.0, 3.0, 3.0, 1, 0.9], - [0.1, 0.1, 3.0, 3.0, 0, 0.9], - [6.0, 1.0, 8.0, 3.0, 1, 0.8], - [1.0, 6.0, 2.0, 7.0, 1, 0.8], - ] - ), - np.array([[1.0, 1.0, 2.0, 2.0, 2, 0.8]]) - ] - - mean_average_precision = sv.MeanAveragePrecision.from_tensors( - predictions=predictions, - targets=targets, - ) - - print(mean_average_precision.map50_95) - # 0.6649 - ``` + Examples: + >>> import supervision as sv + >>> import numpy as np + >>> targets = [ + ... np.array([ + ... [0.0, 0.0, 3.0, 3.0, 0], + ... [2.0, 2.0, 5.0, 5.0, 0], + ... [6.0, 1.0, 8.0, 3.0, 1], + ... ]) + ... ] + >>> predictions = [ + ... np.array([ + ... [0.0, 0.0, 3.0, 3.0, 0, 0.9], + ... [0.1, 0.1, 3.0, 3.0, 0, 0.9], + ... [6.0, 1.0, 8.0, 3.0, 1, 0.8], + ... ]) + ... ] + >>> mAP = sv.MeanAveragePrecision.from_tensors( + ... predictions=predictions, + ... targets=targets, + ... ) + >>> round(float(mAP.map50), 2) + 0.81 """ validate_input_tensors(predictions, targets) iou_thresholds = np.linspace(0.5, 0.95, 10)