@@ -14,8 +14,6 @@ def compute_rocauc(
1414 n_labels : int ,
1515 accumulated_tp : NDArray [np .uint64 ],
1616 accumulated_fp : NDArray [np .uint64 ],
17- prev_fpr : NDArray [np .float64 ],
18- prev_tpr : NDArray [np .float64 ],
1917) -> tuple [NDArray [np .float64 ], NDArray [np .uint64 ], NDArray [np .uint64 ]]:
2018 """
2119 Compute ROCAUC.
@@ -54,98 +52,81 @@ def compute_rocauc(
5452 positive_count = gt_count_per_label
5553 negative_count = pd_count_per_label - gt_count_per_label
5654
55+ print ()
5756 for label_idx in range (n_labels ):
5857 mask_pds = pd_labels == label_idx
5958 n_masked_pds = mask_pds .sum ()
6059 if pd_count_per_label [label_idx ] == 0 or n_masked_pds == 0 :
6160 continue
6261
6362 true_positives = mask_matching_labels [mask_pds ]
64- false_positives = ~ mask_matching_labels [mask_pds ]
6563 tp_scores = scores [mask_pds ]
6664
67- cumulative_fp = np .cumsum (false_positives ) + accumulated_fp [label_idx ]
68- cumulative_tp = np .cumsum (true_positives ) + accumulated_tp [label_idx ]
65+ distinct_score_indices = np .where (np .diff (tp_scores ))[0 ]
66+ indices = np .r_ [distinct_score_indices , n_masked_pds - 1 ]
67+ cumulative_tp = np .cumsum (true_positives , dtype = np .uint64 )[indices ]
68+ cumulative_fp = indices + 1 - cumulative_tp
69+
70+ cumulative_tp += accumulated_tp [label_idx ]
71+ cumulative_fp += accumulated_fp [label_idx ]
72+
73+ cumulative_tp = np .concatenate ([accumulated_tp [label_idx :label_idx + 1 ], cumulative_tp ])
74+ cumulative_fp = np .concatenate ([accumulated_fp [label_idx :label_idx + 1 ], cumulative_fp ])
6975
70- accumulated_fp [label_idx ] = cumulative_fp [- 1 ]
7176 accumulated_tp [label_idx ] = cumulative_tp [- 1 ]
77+ accumulated_fp [label_idx ] = cumulative_fp [- 1 ]
7278
73- fpr = np .zeros ( n_masked_pds , dtype = np .float64 )
79+ fpr = np .zeros_like ( cumulative_fp , dtype = np .float64 )
7480 np .divide (
7581 cumulative_fp ,
7682 negative_count [label_idx ],
7783 where = negative_count [label_idx ] > 0 ,
7884 out = fpr ,
7985 )
80- tpr = np .zeros ( n_masked_pds , dtype = np .float64 )
86+ tpr = np .zeros_like ( cumulative_tp , dtype = np .float64 )
8187 np .divide (
8288 cumulative_tp ,
8389 positive_count [label_idx ],
8490 where = positive_count [label_idx ] > 0 ,
8591 out = tpr ,
8692 )
8793
88- if prev_fpr [label_idx ] > - 0.5 and prev_tpr [label_idx ] > - 0.5 :
89- fpr = np .concatenate ([prev_fpr [label_idx :label_idx + 1 ], fpr ])
90- tpr = np .concatenate ([prev_tpr [label_idx :label_idx + 1 ], tpr ])
91-
92- # sort by -tpr, -score
93- indices = np .lexsort ((- tpr , - tp_scores ))
94- fpr = fpr [indices ]
95- tpr = tpr [indices ]
96-
97- sfpr = fpr .copy ()
98- stpr = tpr .copy ()
94+ # # sort by -tpr, -score
95+ # indices = np.lexsort((-tpr, -tp_scores))
96+ # fpr = fpr[indices]
97+ # tpr = tpr[indices]
9998
10099 # running max of tpr
101100 np .maximum .accumulate (tpr , out = tpr )
102101
103-
104- prev_fpr [label_idx ] = fpr [- 1 ]
105- prev_tpr [label_idx ] = tpr [- 1 ]
106-
107102 # compute rocauc
108103 rocauc [label_idx ] += npc .trapezoid (x = fpr , y = tpr , axis = 0 )
109104
110- print ()
111- print (label_idx , rocauc [label_idx ])
112- print ("====" )
113- print (
114- f"{ 'FP' :4} " ,
115- f"{ 'TP' :4} " ,
116- f"{ 'CFP' :4} " ,
117- f"{ 'CTP' :4} " ,
118- f"{ 'FPR' :4} " ,
119- f"{ 'TPR' :4} " ,
120- f"{ 'SFPR' :4} " ,
121- f"{ 'STPR' :4} " ,
122- f"{ 'SCO' :4} " ,
123- )
124- for f , t , af , at , fr , tr , sf , st , s in zip (
125- false_positives ,
126- true_positives ,
127- cumulative_fp ,
128- cumulative_tp ,
129- fpr ,
130- tpr ,
131- sfpr ,
132- stpr ,
133- tp_scores ,
134- ):
105+ if label_idx == 3 :
106+ print (rocauc [label_idx ])
135107 print (
136- f"{ f :.2f} " ,
137- f"{ t :.2f} " ,
138- f"{ af :.2f} " ,
139- f"{ at :.2f} " ,
140- f"{ fr :.2f} " ,
141- f"{ tr :.2f} " ,
142- f"{ sf :.2f} " ,
143- f"{ st :.2f} " ,
144- f"{ s :.2f} " ,
108+ f"{ 'CFP' :4} " ,
109+ f"{ 'CTP' :4} " ,
110+ f"{ 'FPR' :4} " ,
111+ f"{ 'TPR' :4} " ,
112+ # f"{'SCO':4}",
145113 )
114+ for af , at , fr , tr in zip (
115+ cumulative_fp ,
116+ cumulative_tp ,
117+ fpr ,
118+ tpr ,
119+ # tp_scores,
120+ ):
121+ print (
122+ f"{ af :.2f} " ,
123+ f"{ at :.2f} " ,
124+ f"{ fr :.2f} " ,
125+ f"{ tr :.2f} " ,
126+ # f"{s:.2f}",
127+ )
146128
147-
148- return rocauc , accumulated_fp , accumulated_tp , prev_fpr , prev_tpr
129+ return rocauc , accumulated_fp , accumulated_tp
149130
150131
151132def compute_counts (
0 commit comments