ClarusC64's picture
Create scorer.py
602174a verified
Raw History Blame Contribute Delete
1.6 kB
__version__ = "1.0.0"
__scorer_id__ = "clarus-f1-thermal-load-v1"
import csv, hashlib, json, sys
from datetime import datetime, timezone
def _find_label_column(fields):
for f in fields:
if f.startswith("label_"):
return f
raise ValueError("No label column")
def _norm(v):
v = str(v).strip().lower()
return 1 if v in {"1","true","yes"} else 0
def _safe(n,d): return n/d if d else 0.0
def _read(p):
with open(p,"r") as f:
return list(csv.DictReader(f))
def _hash(p):
h=hashlib.sha256()
with open(p,"rb") as f:
h.update(f.read())
return h.hexdigest()
def score(ref, pred):
r = _read(ref)
p = _read(pred)
label = _find_label_column(r[0].keys())
y_true = [_norm(x[label]) for x in r]
y_pred = [_norm(x["prediction"]) for x in p]
tp = sum(1 for a,b in zip(y_true,y_pred) if a==1 and b==1)
tn = sum(1 for a,b in zip(y_true,y_pred) if a==0 and b==0)
fp = sum(1 for a,b in zip(y_true,y_pred) if a==0 and b==1)
fn = sum(1 for a,b in zip(y_true,y_pred) if a==1 and b==0)
precision = _safe(tp,tp+fp)
recall = _safe(tp,tp+fn)
f1 = _safe(2*precision*recall, precision+recall)
return {
"accuracy": _safe(tp+tn,len(y_true)),
"precision": precision,
"recall": recall,
"f1": f1,
"false_activation_rate": _safe(fp,fp+tn),
"missed_latent_activation_rate": _safe(fn,fn+tp),
"hash_ref": _hash(ref),
"hash_pred": _hash(pred)
}
if __name__ == "__main__":
print(json.dumps(score(sys.argv[1], sys.argv[2]), indent=2))