ClarusC64 commited on
Commit
602174a
·
verified ·
1 Parent(s): aeee00c

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +58 -0
scorer.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __version__ = "1.0.0"
2
+ __scorer_id__ = "clarus-f1-thermal-load-v1"
3
+
4
+ import csv, hashlib, json, sys
5
+ from datetime import datetime, timezone
6
+
7
+ def _find_label_column(fields):
8
+ for f in fields:
9
+ if f.startswith("label_"):
10
+ return f
11
+ raise ValueError("No label column")
12
+
13
+ def _norm(v):
14
+ v = str(v).strip().lower()
15
+ return 1 if v in {"1","true","yes"} else 0
16
+
17
+ def _safe(n,d): return n/d if d else 0.0
18
+
19
+ def _read(p):
20
+ with open(p,"r") as f:
21
+ return list(csv.DictReader(f))
22
+
23
+ def _hash(p):
24
+ h=hashlib.sha256()
25
+ with open(p,"rb") as f:
26
+ h.update(f.read())
27
+ return h.hexdigest()
28
+
29
+ def score(ref, pred):
30
+ r = _read(ref)
31
+ p = _read(pred)
32
+
33
+ label = _find_label_column(r[0].keys())
34
+ y_true = [_norm(x[label]) for x in r]
35
+ y_pred = [_norm(x["prediction"]) for x in p]
36
+
37
+ tp = sum(1 for a,b in zip(y_true,y_pred) if a==1 and b==1)
38
+ tn = sum(1 for a,b in zip(y_true,y_pred) if a==0 and b==0)
39
+ fp = sum(1 for a,b in zip(y_true,y_pred) if a==0 and b==1)
40
+ fn = sum(1 for a,b in zip(y_true,y_pred) if a==1 and b==0)
41
+
42
+ precision = _safe(tp,tp+fp)
43
+ recall = _safe(tp,tp+fn)
44
+ f1 = _safe(2*precision*recall, precision+recall)
45
+
46
+ return {
47
+ "accuracy": _safe(tp+tn,len(y_true)),
48
+ "precision": precision,
49
+ "recall": recall,
50
+ "f1": f1,
51
+ "false_activation_rate": _safe(fp,fp+tn),
52
+ "missed_latent_activation_rate": _safe(fn,fn+tp),
53
+ "hash_ref": _hash(ref),
54
+ "hash_pred": _hash(pred)
55
+ }
56
+
57
+ if __name__ == "__main__":
58
+ print(json.dumps(score(sys.argv[1], sys.argv[2]), indent=2))