test_prevs bug fixed, basedir added to TestReport, added load_results

This commit is contained in:
Lorenzo Volpi 2024-04-05 15:53:33 +02:00
parent 5ee04a2a19
commit dcbbaba361
1 changed files with 38 additions and 19 deletions

View File

@ -1,25 +1,33 @@
import os
from quacc.experiments.util import getpath
from quacc.utils.commons import load_json_file, save_json_file
from genericpath import isfile
from quacc.utils.commons import get_results_path, load_json_file, save_json_file
class TestReport:
def __init__(
self,
basedir,
cls_name,
acc_name,
dataset_name,
method_name,
):
self.basedir = basedir
self.cls_name = cls_name
self.acc_name = acc_name
self.dataset_name = dataset_name
self.method_name = method_name
def path(self, basedir):
return getpath(
basedir, self.cls_name, self.acc_name, self.dataset_name, self.method_name
@property
def path(self):
return get_results_path(
self.basedir,
self.cls_name,
self.acc_name,
self.dataset_name,
self.method_name,
)
def add_result(self, test_prevs, true_accs, estim_accs, t_train, t_test_ave):
@ -35,28 +43,31 @@ class TestReport:
raise AttributeError("Incomplete report cannot be dumped")
result = {
"basedir": self.basedir,
"cls_name": self.cls_name,
"acc_name": self.acc_name,
"dataset_name": self.dataset_name,
"method_name": self.method_name,
"t_train": self.t_train,
"t_test_ave": self.t_test,
"test_prevs": self.test_prevs,
"true_accs": self.true_accs,
"estim_accs": self.estim_accs,
"t_train": self.t_train,
"t_test_ave": self.t_test_ave,
}
result_path = self.path(basedir)
save_json_file(result_path, result)
save_json_file(self.path, result)
@classmethod
def load_json(cls, path) -> "TestReport":
def _test_report_hook(_dict):
return TestReport(
basedir=_dict["basedir"],
cls_name=_dict["cls_name"],
acc_name=_dict["acc_name"],
dataset_name=_dict["dataset_name"],
method_name=_dict["method_name"],
).add_result(
test_prevs=_dict["test_prevs"],
true_accs=_dict["true_accs"],
estim_accs=_dict["estim_accs"],
t_train=_dict["t_train"],
@ -67,20 +78,28 @@ class TestReport:
class Report:
def __init__(self, tests: list[TestReport]):
self.tests = tests
def __init__(self, results: list[TestReport]):
self.results = results
@classmethod
def load_tests(cls, path):
if not os.path.isdir(path):
raise ValueError("Cannot load test results: invalid directory")
def load_results(cls, basedir):
def walk_results(path):
results = []
if not os.path.exists(path):
return results
_tests = []
for f in os.listdir(path):
if f.endswith(".json"):
_tests.append(TestReport.load_json(f))
for f in os.listdir(path):
n_path = os.path.join(path, f)
if os.path.isdir(n_path):
results += walk_results(n_path)
if os.path.isfile(n_path) and n_path.endswith(".json"):
results.append(TestReport.load_json(n_path))
return Report(_tests)
return results
_path = os.path.join("results", basedir)
_results = walk_results(_path)
return Report(results=_results)
def _filter_by_dataset(self):
pass