Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests upgrade. #145

Merged
merged 7 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion depthai_nodes/ml/messages/creators/clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create_cluster_message(clusters: List[List[List[Union[float, int]]]]) -> Clu
if not isinstance(cluster, list):
raise TypeError(f"All clusters must be of type List, got {type(cluster)}")
for point in cluster:
if isinstance(point, list):
if not isinstance(point, list):
raise TypeError(
f"All points in clusters must be of type List, got {type(point)}"
)
Expand Down
8 changes: 4 additions & 4 deletions depthai_nodes/ml/parsers/utils/keypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ def transform_to_keypoints(
raise ValueError("Keypoints must be a numpy array.")
if len(keypoints.shape) != 2:
raise ValueError(
"Keypoints must be of shape (N, 2). Got shape {keypoints.shape}."
f"Keypoints must be of shape (N, 2). Got shape {keypoints.shape}."
)
if keypoints.shape[1] != 2 and keypoints.shape[1] != 3:
raise ValueError(
"Keypoints must be of shape (N, 2) or (N, 3). Got shape {keypoints.shape}."
f"Keypoints must be of shape (N, 2) or (N, 3). Got shape {keypoints.shape}."
)

if confidences is not None:
if not isinstance(confidences, np.ndarray):
raise ValueError("Confidences must be a numpy array.")
if len(confidences.shape) != 1:
raise ValueError(
"Confidences must be of shape (N,). Got shape {confidences.shape}."
f"Confidences must be of shape (N,). Got shape {confidences.shape}."
)
if len(confidences) != len(keypoints):
raise ValueError(
"Confidences should have same length as keypoints, got {len(confidences)} confidences and {len(keypoints)} keypoints."
f"Confidences should have same length as keypoints, got {len(confidences)} confidences and {len(keypoints)} keypoints."
)

dim = keypoints.shape[1]
Expand Down
135 changes: 47 additions & 88 deletions tests/unittests/test_creators/test_classification_sequence.py
Original file line number Diff line number Diff line change
@@ -1,102 +1,93 @@
import re

import numpy as np
import pytest

from depthai_nodes.ml.messages import Classifications
from depthai_nodes.ml.messages.creators import (
create_classification_sequence_message,
)


def test_valid_input():
classes = ["cat", "dog", "bird"]
scores = [[0.7, 0.2, 0.1], [0.1, 0.8, 0.1], [0.2, 0.3, 0.5]]
message = create_classification_sequence_message(classes, scores)

assert isinstance(message, Classifications)
assert message.classes == ["cat", "dog", "bird"]
assert np.array_equal(message.scores, np.array([0.7, 0.8, 0.5], dtype=np.float32))


def test_none_classes():
with pytest.raises(ValueError):
create_classification_sequence_message(None, [0.5, 0.2, 0.3])


def test_1D_scores():
with pytest.raises(
ValueError, match=re.escape("Scores should be a 2D array, got (3,).")
):
def test_empty_scores():
with pytest.raises(ValueError):
create_classification_sequence_message(["cat", "dog", "bird"], [[]])


def test_invalid_classes():
with pytest.raises(ValueError):
create_classification_sequence_message("not a list", [[0.7, 0.2, 0.1]])


def test_1d_scores():
with pytest.raises(ValueError):
create_classification_sequence_message(["cat", "dog", "bird"], [0.5, 0.2, 0.3])


def test_empty_scores():
with pytest.raises(
ValueError,
match=re.escape(
"Number of classes and scores mismatch. Provided 3 class names and 0 scores."
),
):
create_classification_sequence_message(["cat", "dog", "bird"], [[]])
def test_invalid_scores():
with pytest.raises(ValueError):
create_classification_sequence_message(["cat", "dog", "bird"], [0.7, 0.2, 0.1])


def test_scores():
with pytest.raises(
ValueError, match=re.escape("Scores should be in the range [0, 1].")
):
def test_mismatched_lengths():
with pytest.raises(ValueError):
create_classification_sequence_message(
["cat", "dog", "bird"], [[0.5, -0.2, 1.3]]
["cat", "dog", "bird"], [[0.7, 0.2], [0.1, 0.8]]
)


def test_probabilities():
with pytest.raises(
ValueError, match=re.escape("Each row of scores should sum to 1.")
):
def test_scores_out_of_range():
with pytest.raises(ValueError):
create_classification_sequence_message(
["cat", "dog", "bird"], [[0.5, 0.2, 0.3], [0.5, 0.2, 0.4]]
["cat", "dog", "bird"], [[1.2, 0.2, 0.1]]
)


def test_non_list_ignored_indexes():
with pytest.raises(
ValueError,
match=re.escape("Ignored indexes should be a list, got <class 'float'>."),
):
def test_scores_not_sum_to_1():
with pytest.raises(ValueError):
create_classification_sequence_message(
["cat", "dog", "bird"], [[0.5, 0.2, 0.3]], ignored_indexes=1.0
["cat", "dog", "bird"], [[0.7, 0.2, 0.2]]
)


def test_integer_ignored_indexes():
with pytest.raises(
ValueError, match=re.escape("Ignored indexes should be integers.")
):
def test_invalid_ignored_indexes():
with pytest.raises(ValueError):
create_classification_sequence_message(
["cat", "dog", "bird"], [[0.5, 0.2, 0.3]], ignored_indexes=[1.0]
["cat", "dog", "bird"], [[0.7, 0.2, 0.1]], ignored_indexes="not a list"
)


def test_2D_list_integers():
with pytest.raises(
ValueError, match=re.escape("Ignored indexes should be integers.")
):
def test_ignored_indexes_out_of_range():
with pytest.raises(ValueError):
create_classification_sequence_message(
["cat", "dog", "bird"], [[0.5, 0.2, 0.3]], ignored_indexes=[[3]]
["cat", "dog", "bird"], [[0.7, 0.2, 0.1]], ignored_indexes=[3]
)


def test_upper_limit_integers():
with pytest.raises(
ValueError,
match=re.escape(
"Ignored indexes should be integers in the range [0, num_classes -1]."
),
):
def test_integer_ignored_indexes():
with pytest.raises(ValueError):
create_classification_sequence_message(
["cat", "dog", "bird"], [[0.5, 0.2, 0.3]], ignored_indexes=[3]
["cat", "dog", "bird"], [[0.5, 0.2, 0.3]], ignored_indexes=[1.0]
)


def test_lower_limit_integers():
with pytest.raises(
ValueError,
match=re.escape(
"Ignored indexes should be integers in the range [0, num_classes -1]."
),
):
def test_2D_list_integers():
with pytest.raises(ValueError):
create_classification_sequence_message(
["cat", "dog", "bird"], [[0.5, 0.2, 0.3]], ignored_indexes=[-1]
["cat", "dog", "bird"], [[0.5, 0.2, 0.3]], ignored_indexes=[[3]]
)


Expand All @@ -110,34 +101,6 @@ def test_remove_duplicates():
assert np.all(res.scores == np.array([0.5], dtype=np.float32))


def test_ignored_indexes():
res = create_classification_sequence_message(
["cat", "dog", "bird"], [[0.5, 0.2, 0.3], [0.1, 0.6, 0.3]], ignored_indexes=[1]
)
assert res.classes == ["cat"]
assert np.all(res.scores == np.array([0.5], dtype=np.float32))


def test_all_ignored_indexes():
res = create_classification_sequence_message(
["cat", "dog", "bird"],
[[0.5, 0.2, 0.3], [0.1, 0.6, 0.3]],
ignored_indexes=[0, 1, 2],
)
assert len(res.classes) == 0
assert len(res.scores) == 0


def test_two_ignored_indexes():
res = create_classification_sequence_message(
["cat", "dog", "bird"],
[[0.5, 0.2, 0.3], [0.1, 0.6, 0.3]],
ignored_indexes=[0, 2],
)
assert res.classes == ["dog"]
assert np.all(res.scores == np.array([0.6], dtype=np.float32))


def test_concatenate_chars_nospace():
res = create_classification_sequence_message(
["c", "a", "t"],
Expand Down Expand Up @@ -229,7 +192,3 @@ def test_concatenate_mixed_words():
)
assert res.classes == ["Quick b fox jumps o"]
assert np.allclose(res.scores, [0.5])


if __name__ == "__main__":
pytest.main()
Loading
Loading