@@ -37,15 +37,15 @@ class Evaluator(Base):
3737 def __init__ (
3838 self ,
3939 reader : MemoryCacheReader | FileCacheReader ,
40- sorted_reader : MemoryCacheReader | FileCacheReader ,
40+ rocauc_reader : MemoryCacheReader | FileCacheReader ,
4141 info : EvaluatorInfo ,
4242 label_counts : NDArray [np .uint64 ],
4343 index_to_label : dict [int , str ],
4444 path : str | Path | None ,
4545 ):
4646 self ._path = Path (path ) if path else None
4747 self ._reader = reader
48- self ._sorted_reader = sorted_reader
48+ self ._rocauc_reader = rocauc_reader
4949 self ._info = info
5050 self ._label_counts = label_counts
5151 self ._index_to_label = index_to_label
@@ -82,8 +82,8 @@ def load(
8282
8383 # load cache
8484 reader = FileCacheReader .load (cls ._generate_cache_path (path ))
85- sorted_reader = FileCacheReader .load (
86- cls ._generate_sorted_cache_path (path )
85+ rocauc_reader = FileCacheReader .load (
86+ cls ._generate_rocauc_cache_path (path )
8787 )
8888
8989 # build evaluator meta
@@ -101,7 +101,7 @@ def load(
101101 return cls (
102102 path = path ,
103103 reader = reader ,
104- sorted_reader = sorted_reader ,
104+ rocauc_reader = rocauc_reader ,
105105 info = info ,
106106 label_counts = label_counts ,
107107 index_to_label = index_to_label ,
@@ -216,8 +216,7 @@ def delete(self):
216216 if self ._path and self ._path .exists ():
217217 self .delete_at_path (self ._path )
218218
219- @staticmethod
220- def iterate_values (reader : MemoryCacheReader | FileCacheReader ):
219+ def iterate_values (self ):
221220 columns = [
222221 "datum_id" ,
223222 "gt_label_id" ,
@@ -226,7 +225,7 @@ def iterate_values(reader: MemoryCacheReader | FileCacheReader):
226225 "winner" ,
227226 "match" ,
228227 ]
229- for tbl in reader .iterate_tables (columns = columns ):
228+ for tbl in self . _reader .iterate_tables (columns = columns ):
230229 ids = np .column_stack (
231230 [
232231 tbl [col ].to_numpy ()
@@ -242,11 +241,8 @@ def iterate_values(reader: MemoryCacheReader | FileCacheReader):
242241 matches = tbl ["match" ].to_numpy ()
243242 yield ids , scores , winners , matches
244243
245- @staticmethod
246- def iterate_values_with_tables (
247- reader : MemoryCacheReader | FileCacheReader ,
248- ):
249- for tbl in reader .iterate_tables ():
244+ def iterate_values_with_tables (self ):
245+ for tbl in self ._reader .iterate_tables ():
250246 ids = np .column_stack (
251247 [
252248 tbl [col ].to_numpy ()
@@ -266,13 +262,6 @@ def compute_rocauc(self) -> dict[MetricType, list[Metric]]:
266262 """
267263 Compute ROCAUC.
268264
269- Parameters
270- ----------
271- rows_per_chunk : int, default=10_000
272- The number of sorted rows to return in each chunk.
273- read_batch_size : int, default=1_000
274- The maximum number of rows to load in-memory per file.
275-
276265 Returns
277266 -------
278267 dict[MetricType, list[Metric]]
@@ -281,42 +270,42 @@ def compute_rocauc(self) -> dict[MetricType, list[Metric]]:
281270 n_labels = self .info .number_of_labels
282271
283272 rocauc = np .zeros (n_labels , dtype = np .float64 )
284- cumulative_fp = np .zeros (n_labels , dtype = np .uint64 )
285- cumulative_tp = np .zeros (n_labels , dtype = np .uint64 )
286-
287- tpr = np .zeros (n_labels , dtype = np .float64 )
288- fpr = np .zeros (n_labels , dtype = np .float64 )
273+ accumulated_fp = np .zeros (n_labels , dtype = np .uint64 )
274+ accumulated_tp = np .zeros (n_labels , dtype = np .uint64 )
289275
290- positive_count = self . _label_counts [:, 0 ]
291- negative_count = self . _label_counts [:, 1 ] - self . _label_counts [:, 0 ]
276+ prev_tpr = np . ones ( n_labels , dtype = np . float64 ) * - 1
277+ prev_fpr = np . ones ( n_labels , dtype = np . float64 ) * - 1
292278
293- for ids , scores , winners , matches in self .iterate_values (
294- self ._sorted_reader
295- ):
296- for id_row , score in zip (ids , scores ):
297- glabel = id_row [1 ]
298- plabel = id_row [2 ]
299- if glabel < 0 or plabel < 0 :
300- continue
301- elif glabel == plabel :
302- cumulative_tp [plabel ] += 1
303- tpr_new = cumulative_tp [plabel ] / positive_count [plabel ]
304- tpr [plabel ]
305- else :
306- cumulative_fp [plabel ] += 1
307-
308- batch_rocauc , cumulative_fp , cumulative_tp = compute_rocauc (
309- ids = ids ,
310- scores = scores ,
279+ for loopid , array in enumerate (self ._rocauc_reader .iterate_arrays (
280+ numeric_columns = [
281+ "pd_label_id" ,
282+ "score" ,
283+ "match" ,
284+ ]
285+ )):
286+ # for id_row, score in zip(ids, scores):
287+ # glabel = id_row[1]
288+ # plabel = id_row[2]
289+ # if glabel < 0 or plabel < 0:
290+ # continue
291+ # elif glabel == plabel:
292+ # cumulative_tp[plabel] += 1
293+ # tpr_new = cumulative_tp[plabel] / positive_count[plabel]
294+ # tpr[plabel]
295+ # else:
296+ # cumulative_fp[plabel] += 1
297+ print ("loop" , loopid )
298+ rocauc , accumulated_fp , accumulated_tp , prev_fpr , prev_tpr = compute_rocauc (
311299 rocauc = rocauc ,
312- cumulative_fp = cumulative_fp ,
313- cumulative_tp = cumulative_tp ,
300+ array = array ,
314301 gt_count_per_label = self ._label_counts [:, 0 ],
315302 pd_count_per_label = self ._label_counts [:, 1 ],
316- n_datums = self .info .number_of_datums ,
317303 n_labels = self .info .number_of_labels ,
304+ accumulated_fp = accumulated_fp ,
305+ accumulated_tp = accumulated_tp ,
306+ prev_fpr = prev_fpr ,
307+ prev_tpr = prev_tpr ,
318308 )
319- rocauc += batch_rocauc
320309
321310 mean_rocauc = rocauc .mean ()
322311
0 commit comments