- Updated type hints across multiple files to replace `Optional[X]` with `X | None` for better readability and consistency. - Added `from __future__ import annotations` to files to enable postponed evaluation of type annotations. - Adjusted function signatures and variable types in various modules including `polygon_zone.py`, `smoother.py`, `transformers.py`, `vlm.py`, and others. - Ensured compatibility with Python 3.10+ type hinting features. Signed-off-by: Onuralp SEZER <thunderbirdtr@gmail.com>
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
from contextlib import ExitStack as DoesNotRaise
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from supervision.classification.core import Classifications
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"class_id, confidence, k, expected_result, exception",
|
|
[
|
|
(
|
|
np.array([0, 1, 2, 3, 4]),
|
|
np.array([0.1, 0.2, 0.9, 0.4, 0.5]),
|
|
5,
|
|
(np.array([2, 4, 3, 1, 0]), np.array([0.9, 0.5, 0.4, 0.2, 0.1])),
|
|
DoesNotRaise(),
|
|
), # class_id with 5 numbers and 5 confidences
|
|
(
|
|
np.array([5, 1, 2, 3, 4]),
|
|
np.array([0.1, 0.2, 0.9, 0.4, 0.5]),
|
|
1,
|
|
(np.array([2]), np.array([0.9])),
|
|
DoesNotRaise(),
|
|
), # class_id with 5 numbers and 5 confidences, retrieve where k = 1
|
|
(
|
|
np.array([4, 1, 2, 3, 6, 5]),
|
|
np.array([0.8, 0.2, 0.9, 0.4, 0.5, 0.1]),
|
|
2,
|
|
(np.array([2, 4]), np.array([0.9, 0.8])),
|
|
DoesNotRaise(),
|
|
), # class_id with 5 numbers and 5 confidences, retrieve where k = 3
|
|
(
|
|
np.array([0, 1, 2, 3, 4]),
|
|
np.array([]),
|
|
5,
|
|
None,
|
|
pytest.raises(ValueError),
|
|
), # class_id with 5 numbers and 0 confidences
|
|
(
|
|
[0, 1, 2, 3, 4],
|
|
[0.1, 0.2, 0.3, 0.4],
|
|
5,
|
|
None,
|
|
pytest.raises(ValueError),
|
|
), # class_id with 5 numbers and 4 confidences
|
|
],
|
|
)
|
|
def test_top_k(
|
|
class_id: np.ndarray,
|
|
confidence: np.ndarray | None,
|
|
k: int,
|
|
expected_result: tuple[np.ndarray, np.ndarray] | None,
|
|
exception: Exception,
|
|
) -> None:
|
|
with exception:
|
|
result = Classifications(
|
|
class_id=np.array(class_id), confidence=np.array(confidence)
|
|
).get_top_k(k)
|
|
|
|
assert np.array_equal(result[0], expected_result[0])
|
|
assert np.array_equal(result[1], expected_result[1])
|