@@ -29,6 +29,59 @@ def compute_label_metadata(
2929 return label_metadata
3030
3131
32+ def filter_cache (
33+ confusion_matrices : NDArray [np .int64 ],
34+ datum_mask : NDArray [np .bool_ ],
35+ label_mask : NDArray [np .bool_ ],
36+ number_of_labels : int ,
37+ ) -> tuple [NDArray [np .int64 ], NDArray [np .int64 ]]:
38+ """
39+ Performs the filter operation over the internal cache.
40+
41+ Parameters
42+ ----------
43+ confusion_matrices : NDArray[int64]
44+ The internal evaluator cache.
45+ datum_mask : NDArray[bool]
46+ A mask that filters out datums.
47+ datum_mask : NDArray[bool]
48+ A mask that filters out labels.
49+
50+ Returns
51+ -------
52+ NDArray[int64]
53+ Filtered confusion matrices.
54+ NDArray[int64]
55+ Filtered label metadata.
56+ """
57+ if label_mask .any ():
58+ # add filtered labels to background
59+ null_predictions = confusion_matrices [:, label_mask , :].sum (
60+ axis = (1 , 2 )
61+ )
62+ null_groundtruths = confusion_matrices [:, :, label_mask ].sum (
63+ axis = (1 , 2 )
64+ )
65+ null_intersection = (
66+ confusion_matrices [:, label_mask , label_mask ]
67+ .reshape (confusion_matrices .shape [0 ], - 1 )
68+ .sum (axis = 1 )
69+ )
70+ confusion_matrices [:, 0 , 0 ] += (
71+ null_groundtruths + null_predictions - null_intersection
72+ )
73+ confusion_matrices [:, label_mask , :] = 0
74+ confusion_matrices [:, :, label_mask ] = 0
75+
76+ confusion_matrices = confusion_matrices [datum_mask ]
77+
78+ label_metadata = compute_label_metadata (
79+ confusion_matrices = confusion_matrices ,
80+ n_labels = number_of_labels ,
81+ )
82+ return confusion_matrices , label_metadata
83+
84+
3285def compute_intermediate_confusion_matrices (
3386 groundtruths : NDArray [np .bool_ ],
3487 predictions : NDArray [np .bool_ ],
0 commit comments