8def get_average_difference(output: np.array, expected: np.array) -> float:
10 Computes the average difference between an output output and the expected values.
13 output (np.array): Empirical Output.
14 expected (np.array): Expected output.
17 float: Average difference between empirical and expected outputs.
23 if len(expected) == 0:
25 "No ground truth cones provided for computing average difference."
29 differences = np.linalg.norm(
30 output[:, np.newaxis, :] - expected[np.newaxis, :, :], axis=2
34 min_differences_sum: float = np.min(differences, axis=1).sum()
37 me: float = min_differences_sum / len(output)
42def get_false_positives(
43 output: np.ndarray, expected: np.ndarray, threshold: float
46 Computes the number of false positives in the output compared to the expected values.
49 output (np.ndarray): Empirical Output.
50 expected (np.ndarray): Expected output.
51 threshold (float): Distance threshold to consider values as matching.
54 int: Number of false positives.
59 if len(expected) == 0:
61 "No ground truth values provided for computing false positives."
64 differences = np.linalg.norm(
65 output[:, np.newaxis, :] - expected[np.newaxis, :, :], axis=-1
68 matched_output = np.full(len(output),
False)
69 matched_expected = np.full(len(expected),
False)
71 for i
in range(len(output)):
72 for j
in range(len(expected)):
73 if not matched_expected[j]
and differences[i, j] < threshold:
74 matched_output[i] =
True
75 matched_expected[j] =
True
78 true_positives = np.sum(matched_output)
80 return max(0, len(output) - true_positives)
83def get_mean_squared_difference(output: np.ndarray, expected: np.ndarray) -> float:
85 Computes the mean squared difference between an output output and the expected values.
88 output (list): Empirical Output.
89 expected (list): Expected output.
92 float: Mean squared difference.
95 raise ValueError(
"No perception output provided.")
97 raise ValueError(
"No ground truth cones provided.")
101 np.linalg.norm(output[:, np.newaxis, :] - expected[np.newaxis, :, :], axis=2)
106 min_differences_sum = np.min(differences, axis=1).sum()
109 mse = min_differences_sum / len(output)
127def build_adjacency_matrix(cones: np.array) -> np.array:
129 Build an adjacency matrix based on the distances between cones.
132 cones (np.array): An array containing the coordinates of cones.
135 np.array: The adjacency matrix representing the distances between cones.
138 num_cones = cones.shape[0]
143 differences = cones[:, np.newaxis] - cones[np.newaxis, :]
144 distances = np.linalg.norm(differences, axis=-1)
146 np.fill_diagonal(distances, 0.0)
170def get_inter_cones_distance(perception_output: np.array) -> float:
172 Computes the average distance between pairs of perceived cones using Minimum Spanning Tree Prim's algorithm.
175 perception_output (np.array): List of perceived cones, where each cone is represented as a numpy array.
178 float: Average distance between pairs of perceived cones.
181 adjacency_matrix: np.array = build_adjacency_matrix(perception_output)
183 adjacency_matrix: list[np.array] = [
184 adjacency_matrix[i]
for i
in range(len(adjacency_matrix))
187 if len(adjacency_matrix) == 0:
190 mst = minimum_spanning_tree(adjacency_matrix)
193 num_pairs = np.count_nonzero(mst.toarray().astype(float))
198 average_distance = mst_sum / num_pairs
199 return float(average_distance)
202def compute_closest_distances(arr1: np.ndarray, arr2: np.ndarray) -> np.ndarray:
204 Computes the distance between each element in arr2 and the closest element in arr1.
207 arr1 (np.ndarray): First array of positions.
208 arr2 (np.ndarray): Second array of positions.
211 np.ndarray: Array of distances between each element in arr2 and the closest element in arr1.
214 arr1_xy = arr1[:, :2]
215 arr2_xy = arr2[:, :2]
218 distances = np.linalg.norm(
219 arr2_xy[:, np.newaxis, :] - arr1_xy[np.newaxis, :, :], axis=2
223 closest_distances = np.min(distances, axis=1)
225 return closest_distances