Source code for HALF.Hooks.WriteAccuracyLogsHook
from HALF.Interfaces.IHook import IHook, ActiveLearner
from typing import List
from HALF.Utils.Registry import REGISTRY
from HALF.Commons.data import DataLogAccuracy
import pandas as pd
import os
[docs]@REGISTRY.register()
class WriteAccuracyLogsHook(IHook):
[docs] def __init__(self, saving_dir: str, name: str):
"""Writes accuracy logs to disk
Args:
saving_dir (str): output directory
name (str): output name for the accuracy logs
"""
IHook.__init__(self)
self.saving_dir = saving_dir
self.name = name
[docs] def apply(self, al: ActiveLearner):
"""Saves accuracy logs to csv file
Args:
al (ActiveLearner) : The ActiveLearner whose accuracy logs should be written to disk
"""
if "test_logs" not in al.ds_hook:
al.ds_hook["test_logs"] : List[DataLogAccuracy]= list()
list_dataLogAccuracy : List[DataLogAccuracy] = al.ds_hook["test_logs"]
list_values = [[e.lb_dataset_size, e.test_acc] for e in list_dataLogAccuracy]
df = pd.DataFrame(data=list_values, columns=["dataset length", "test accuracy"])
path_file = os.path.join(self.saving_dir, self.name)
df.to_csv(index=False, path_or_buf=path_file)
print(df)