Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
test_data_association_metrics.py
Go to the documentation of this file.
1import unittest
2import numpy.testing as npt
3import numpy as np
4from evaluator.metrics import (
5 get_average_difference,
6 get_mean_squared_difference,
7 get_inter_cones_distance,
8 get_false_positives,
9 build_adjacency_matrix,
10 get_duplicates,
11)
12
13
14class TestDataAssociationMetrics(unittest.TestCase):
15 """
16 Test case for the data association methods of the Evaluator class.
17 """
18
19 def setUp(self):
20 """
21 Set up the test environment by initializing output and ground truth arrays.
22 """
23 self.output = np.array([
24 [1, 2, 0],
25 [2, 1, 0],
26 [2, 3.5, 0]
27 ])
28
29 self.ground_truth = np.array([
30 np.array([1, 2, 0]),
31 np.array([2.4, 0.8, 0]),
32 np.array([4, 3, 0]),
33 np.array([5, 5, 0]),
34 np.array([3.5, 4.3, 0]),
35 np.array([0, 0, 0]),
36 np.array([4.3, 3.4, 0])
37 ])
38
40 """
41 Test the get_average_difference function when both arrays are non-empty.
42 Expects a correct average difference based on manually calculated values.
43 """
44 average_difference = get_average_difference(self.output, self.ground_truth)
45 self.assertAlmostEqual(average_difference, 0.7157378651666527, delta=1e-10)
46
48 """
49 Test the get_average_difference function when the output array is empty.
50 Expects infinity as the return value.
51 """
52 average_difference = get_average_difference([], self.ground_truth)
53 self.assertEqual(average_difference, float("inf"))
54
56 """
57 Test the get_average_difference function when the ground truth array is empty.
58 Expects a ValueError due to incompatible data.
59 """
60 with self.assertRaises(ValueError):
61 get_average_difference(self.output, [])
62
64 """
65 Test the get_mean_squared_difference function with valid output and ground truth arrays.
66 Expects a mean squared difference close to the manually calculated value.
67 """
68 mean_squared_error = get_mean_squared_difference(self.output, self.ground_truth)
69 self.assertAlmostEqual(mean_squared_error, 1.03, delta=0.00001)
70
72 """
73 Test the get_inter_cones_distance function for a non-empty output array.
74 Expects a correct average inter-cone distance based on the output array.
75 """
76 inter_cones_distance = get_inter_cones_distance(self.output)
77 self.assertAlmostEqual(inter_cones_distance, 1.6071067811865476, delta=0.005)
78
80 """
81 Test the get_inter_cones_distance function for an empty output array.
82 Expects a zero return value since there are no cones.
83 """
84 inter_cones_distance = get_inter_cones_distance(np.array([]))
85 self.assertEqual(inter_cones_distance, 0)
86
88 """
89 Test the get_false_positives function with a threshold of 1.0.
90 Expects the number of false positives based on the given threshold and arrays.
91 """
92 threshold = 1.0
93 false_positives = get_false_positives(self.output, self.ground_truth, threshold)
94 self.assertEqual(false_positives, 1)
95
97 """
98 Test the get_false_positives function with arrays swapped and a threshold of 1.0.
99 Expects a different number of false positives when the arrays are swapped.
100 """
101 threshold = 1.0
102 output = self.ground_truth
103 ground_truth = self.output
104 false_positives = get_false_positives(output, ground_truth, threshold)
105 self.assertEqual(false_positives, 5)
106
108 """
109 Test the get_duplicates function with a threshold of 0.01.
110 Expects no duplicates since the output array does not contain any within this threshold.
111 """
112 threshold = 0.01
113 count = get_duplicates(self.output, threshold)
114 self.assertEqual(count, 0)
115
117 """
118 Test the get_duplicates function with a threshold of 1.0.
119 Expects 1 duplicate since the ground truth array contains a close match within this threshold.
120 """
121 threshold = 1.0
122 output = self.ground_truth
123 count = get_duplicates(output, threshold)
124 self.assertEqual(count, 1)
125
127 """
128 Test the build_adjacency_matrix function with a subarray of the ground truth array.
129 Expects an adjacency matrix calculated based on pairwise distances.
130 """
131 subarray = self.ground_truth[:2, :]
132 adjacency_matrix = build_adjacency_matrix(subarray)
133 expected = np.array([
134 np.array([0, 1.8439088914585773]), # Manually computed distance
135 np.array([1.8439088914585773, 0])
136 ])
137 npt.assert_array_almost_equal(adjacency_matrix, expected, decimal=6)
138
140 """
141 Test the build_adjacency_matrix function with a different subarray of the ground truth array.
142 Expects an adjacency matrix calculated for a different subset of cones.
143 """
144 subarray = self.ground_truth[1:3, :]
145 adjacency_matrix = build_adjacency_matrix(subarray)
146 expected = np.array([
147 np.array([0, 2.720294101747089]), # Manually computed distance
148 np.array([2.720294101747089, 0])
149 ])
150 npt.assert_array_almost_equal(adjacency_matrix, expected, decimal=6)