@@ -174,54 +174,7 @@ def compute_polygon_iou(
174174 return ious
175175
176176
177- def compute_label_metadata (
178- ids : NDArray [np .int32 ],
179- n_labels : int ,
180- ) -> NDArray [np .uint32 ]:
181- """
182- Computes label metadata returning a count of annotations per label.
183-
184- Parameters
185- ----------
186- detailed_pairs : NDArray[np.int32]
187- Detailed annotation pairings with shape (N, 7).
188- Index 0 - Datum Index
189- Index 1 - GroundTruth Index
190- Index 2 - Prediction Index
191- Index 3 - GroundTruth Label Index
192- Index 4 - Prediction Label Index
193- n_labels : int
194- The total number of unique labels.
195-
196- Returns
197- -------
198- NDArray[np.int32]
199- The label metadata array with shape (n_labels, 2).
200- Index 0 - Ground truth label count
201- Index 1 - Prediction label count
202- """
203- label_metadata = np .zeros ((n_labels , 2 ), dtype = np .uint32 )
204-
205- ground_truth_pairs = ids [:, (0 , 1 , 3 )]
206- ground_truth_pairs = ground_truth_pairs [ground_truth_pairs [:, 1 ] >= 0 ]
207- unique_pairs = np .unique (ground_truth_pairs , axis = 0 )
208- label_indices , unique_counts = np .unique (
209- unique_pairs [:, 2 ], return_counts = True
210- )
211- label_metadata [label_indices .astype (np .int32 ), 0 ] = unique_counts
212-
213- prediction_pairs = ids [:, (0 , 2 , 4 )]
214- prediction_pairs = prediction_pairs [prediction_pairs [:, 1 ] >= 0 ]
215- unique_pairs = np .unique (prediction_pairs , axis = 0 )
216- label_indices , unique_counts = np .unique (
217- unique_pairs [:, 2 ], return_counts = True
218- )
219- label_metadata [label_indices .astype (np .int32 ), 1 ] = unique_counts
220-
221- return label_metadata
222-
223-
224- def rank_pairs_returning_indices (sorted_pairs : NDArray [np .float64 ]):
177+ def rank_pairs (sorted_pairs : NDArray [np .float64 ]):
225178 """
226179 Prunes and ranks prediction pairs.
227180
@@ -327,7 +280,7 @@ def rank_table(tbl: pa.Table, number_of_labels: int) -> pa.Table:
327280 pairs = np .column_stack (
328281 [sorted_tbl [col ].to_numpy () for col in numeric_columns ]
329282 )
330- pairs , indices = rank_pairs_returning_indices (pairs )
283+ pairs , indices = rank_pairs (pairs )
331284 ranked_tbl = sorted_tbl .take (indices )
332285 lower_iou_bound , winning_predictions = calculate_ranking_boundaries (
333286 pairs , number_of_labels = number_of_labels
@@ -344,57 +297,6 @@ def rank_table(tbl: pa.Table, number_of_labels: int) -> pa.Table:
344297 return ranked_tbl
345298
346299
347- def rank_pairs (
348- detailed_pairs : NDArray [np .float64 ],
349- ) -> NDArray [np .float64 ]:
350- """
351- Highly optimized pair ranking for computing precision and recall based metrics.
352-
353- Only ground truths and predictions that provide unique information are kept. The unkept
354- pairs are represented via the label metadata array.
355-
356- Parameters
357- ----------
358- detailed_pairs : NDArray[np.float64]
359- Detailed annotation pairs with shape (n_pairs, 7).
360- Index 0 - Datum Index
361- Index 1 - GroundTruth Index
362- Index 2 - Prediction Index
363- Index 3 - GroundTruth Label Index
364- Index 4 - Prediction Label Index
365- Index 5 - IOU
366- Index 6 - Score
367-
368- Returns
369- -------
370- NDArray[np.float64]
371- Array of ranked pairs for precision-recall metric computation.
372- """
373- # remove unmatched ground truths
374- pairs = detailed_pairs [detailed_pairs [:, 2 ] >= 0.0 ]
375-
376- # find best fits for prediction
377- mask_label_match = np .isclose (pairs [:, 3 ], pairs [:, 4 ])
378- matched_predictions = np .unique (pairs [mask_label_match , 2 ])
379- mask_unmatched_predictions = ~ np .isin (pairs [:, 2 ], matched_predictions )
380- pairs = pairs [mask_label_match | mask_unmatched_predictions ]
381-
382- # only keep the highest ranked pair
383- _ , indices = np .unique (pairs [:, [0 , 2 , 4 ]], axis = 0 , return_index = True )
384- pairs = pairs [indices ]
385-
386- # np.unique orders its results by value, we need to sort the indices to maintain the results of the lexsort
387- indices = np .lexsort (
388- (
389- - pairs [:, 5 ], # iou
390- - pairs [:, 6 ], # score
391- )
392- )
393- pairs = pairs [indices ]
394-
395- return pairs
396-
397-
398300def compute_counts (
399301 ranked_pairs : NDArray [np .float64 ],
400302 iou_thresholds : NDArray [np .float64 ],
0 commit comments