Files
supervision/tests/dataset/test_core.py
T
Jirka Borovecandpre-commit-ci[bot] 2d0404e67c Extend docstrings to test cases for enhanced readability (#2130)
* Extend docstrings to test cases for enhanced readability and consistency across testing modules
* Extend test case docstrings across multiple modules for improved clarity and consistency
* Refactor test case docstrings for clarity and consistency in `test_annotators.py`

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-02-03 17:28:33 +01:00

190 lines
7.1 KiB
Python

from __future__ import annotations
from contextlib import ExitStack as DoesNotRaise
import pytest
from supervision import DetectionDataset
from tests.helpers import _create_detections
@pytest.mark.parametrize(
("dataset_list", "expected_result", "exception"),
[
(
[],
DetectionDataset(classes=[], images=[], annotations={}),
DoesNotRaise(),
), # empty dataset list
(
[DetectionDataset(classes=[], images=[], annotations={})],
DetectionDataset(classes=[], images=[], annotations={}),
DoesNotRaise(),
), # single empty dataset
(
[
DetectionDataset(classes=["dog", "person"], images=[], annotations={}),
DetectionDataset(classes=["dog", "person"], images=[], annotations={}),
],
DetectionDataset(classes=["dog", "person"], images=[], annotations={}),
DoesNotRaise(),
), # two datasets; no images and annotations, the same classes
(
[
DetectionDataset(classes=["dog", "person"], images=[], annotations={}),
DetectionDataset(classes=["cat"], images=[], annotations={}),
],
DetectionDataset(
classes=["cat", "dog", "person"], images=[], annotations={}
),
DoesNotRaise(),
), # two datasets; no images and annotations, different classes
(
[
DetectionDataset(
classes=["dog", "person"],
images=["image-1.png", "image-2.png"],
annotations={
"image-1.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[0]
),
"image-2.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[1]
),
},
),
DetectionDataset(classes=[], images=[], annotations={}),
],
DetectionDataset(
classes=["dog", "person"],
images=["image-1.png", "image-2.png"],
annotations={
"image-1.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[0]
),
"image-2.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[1]
),
},
),
DoesNotRaise(),
), # two datasets; images and annotations, the same classes
(
[
DetectionDataset(
classes=["dog", "person"],
images=["image-1.png", "image-2.png"],
annotations={
"image-1.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[0]
),
"image-2.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[1]
),
},
),
DetectionDataset(classes=["cat"], images=[], annotations={}),
],
DetectionDataset(
classes=["cat", "dog", "person"],
images=["image-1.png", "image-2.png"],
annotations={
"image-1.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[1]
),
"image-2.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[2]
),
},
),
DoesNotRaise(),
), # two datasets; images and annotations, different classes
(
[
DetectionDataset(
classes=["dog", "person"],
images=["image-1.png", "image-2.png"],
annotations={
"image-1.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[0]
),
"image-2.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[1]
),
},
),
DetectionDataset(
classes=["cat"],
images=["image-3.png"],
annotations={
"image-3.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[0]
),
},
),
],
DetectionDataset(
classes=["cat", "dog", "person"],
images=["image-1.png", "image-2.png", "image-3.png"],
annotations={
"image-1.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[1]
),
"image-2.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[2]
),
"image-3.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[0]
),
},
),
DoesNotRaise(),
), # two datasets; images and annotations, different classes
(
[
DetectionDataset(
classes=["dog", "person"],
images=["image-1.png", "image-2.png"],
annotations={
"image-1.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[0]
),
"image-2.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[1]
),
},
),
DetectionDataset(
classes=["dog", "person"],
images=["image-2.png", "image-3.png"],
annotations={
"image-2.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[0]
),
"image-3.png": _create_detections(
xyxy=[[0, 0, 10, 10]], class_id=[1]
),
},
),
],
None,
pytest.raises(ValueError),
),
],
)
def test_dataset_merge(
dataset_list: list[DetectionDataset],
expected_result: DetectionDataset | None,
exception: Exception,
) -> None:
"""
Verify that multiple DetectionDataset objects can be successfully merged.
Ensures that multiple `DetectionDataset` objects can be merged into single dataset.
This is vital for users who need to combine data from different sources or
augment their datasets with additional labeled examples.
"""
with exception:
result = DetectionDataset.merge(dataset_list=dataset_list)
assert result == expected_result