Convert supervision/metrics/detection.py docstrings to doctests

---

Co-authored-by: copilot-swe-agent[bot] <198982749+copilot@users.noreply.github.com>
This commit is contained in:
jirkaandcopilot-swe-agent[bot] authored and Jirka Borovec committed 2026-02-02 18:10:37 +09:00
1 parent f6326d5590
commit f005ea1d17
1 file changed
+95 -132
+95 -132
View File
@@ -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)