[d6904d]: / app / utils / perflog.py

Download this file

150 lines (137 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import json
import time
from numpyencoder import NumpyEncoder
from omegaconf import OmegaConf
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, relationship, sessionmaker
Base = declarative_base()
class Perflog(Base):
__tablename__ = "perflog"
id = Column(Integer, primary_key=True, index=True)
dataset = Column(String)
task = Column(String)
model_type = Column(String)
model_name = Column(String)
hidden_dim = Column(Integer)
performance = Column(String)
config = Column(String)
record_time = Column(Integer)
def process_performance_raw_info(
cfg,
mae=None,
mse=None,
rmse=None,
mape=None,
acc=None,
auroc=None,
auprc=None,
early_prediction_score=None,
multitask_prediction_score=None,
verbose=0,
):
result = []
if mae is not None:
result.extend(
[
{"name": "mae", "mean": mae.mean(), "std": mae.std()},
{"name": "mse", "mean": mse.mean(), "std": mse.std()},
{"name": "rmse", "mean": rmse.mean(), "std": rmse.std()},
{"name": "mape", "mean": mape.mean(), "std": mape.std()},
]
)
if acc is not None:
result.extend(
[
{"name": "acc", "mean": acc.mean(), "std": acc.std()},
{"name": "auroc", "mean": auroc.mean(), "std": auroc.std()},
{"name": "auprc", "mean": auprc.mean(), "std": auprc.std()},
]
)
thresholds = cfg.thresholds
if early_prediction_score is not None:
for i in range(len(thresholds)):
result.append(
{
"name": "early_prediction_score",
"mean": early_prediction_score.mean(axis=0)[i],
"std": early_prediction_score.std(axis=0)[i],
"threshold": thresholds[i],
}
)
if multitask_prediction_score is not None:
for i in range(len(thresholds)):
result.append(
{
"name": "multitask_prediction_score",
"mean": multitask_prediction_score.mean(axis=0)[i],
"std": multitask_prediction_score.std(axis=0)[i],
"threshold": thresholds[i],
}
)
if verbose == 1:
print(result)
return result
def create_perflog(db: Session, cfg, perf=None):
hid_dim = 0
if "hidden_dim" in cfg:
hid_dim = cfg.hidden_dim
db_perflog = Perflog(
task=cfg.task,
dataset=cfg.dataset,
model_type=cfg.model_type,
model_name=cfg.model,
hidden_dim=hid_dim,
performance=json.dumps(perf, cls=NumpyEncoder),
config=OmegaConf.to_yaml(cfg),
record_time=int(time.time()),
)
db.add(db_perflog)
db.commit()
db.refresh(db_perflog)
return db_perflog
def process_and_upload_performance(
cfg,
mae=None,
mse=None,
rmse=None,
mape=None,
acc=None,
auroc=None,
auprc=None,
early_prediction_score=None,
multitask_prediction_score=None,
verbose=0,
upload=False,
):
perf = process_performance_raw_info(
cfg,
mae=mae,
mse=mse,
rmse=rmse,
mape=mape,
acc=acc,
auroc=auroc,
auprc=auprc,
early_prediction_score=early_prediction_score,
multitask_prediction_score=multitask_prediction_score,
verbose=verbose,
)
if upload:
db_cfg = OmegaConf.load("configs/_base_/db.yaml")
engine, username, password, host, port, database = (
db_cfg.engine,
db_cfg.username,
db_cfg.password,
db_cfg.host,
db_cfg.port,
db_cfg.database,
)
SQLALCHEMY_DATABASE_URL = (
f"{engine}://{username}:{password}@{host}:{port}/{database}"
)
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
db = SessionLocal()
create_perflog(db=db, cfg=cfg, perf=perf)
db.close()