-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_zkc_infer_vs_tmax.py
119 lines (91 loc) · 3.19 KB
/
collect_zkc_infer_vs_tmax.py
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
import json
import os
import numpy as np
from joblib import Parallel, delayed
from lcs import *
data_dir = "Data/zkc_infer_vs_tmax/"
def collect_parameters(dir):
clist = set()
tlist = set()
rlist = set()
for f in os.listdir(dir):
d = f.split(".json")[0].split("_")
c = int(d[0])
t = float(d[1])
r = float(d[2])
clist.add(c)
tlist.add(t)
rlist.add(r)
c_dict = {c: i for i, c in enumerate(sorted(clist))}
t_dict = {t: i for i, t in enumerate(sorted(tlist))}
r_dict = {r: i for i, r in enumerate(sorted(rlist))}
return c_dict, t_dict, r_dict
def get_metrics(f, dir, c_dict, t_dict, r_dict):
fname = os.path.join(dir, f)
d = f.split(".json")[0].split("_")
c = int(d[0])
t = float(d[1])
r = float(d[2])
i = c_dict[c]
j = t_dict[t]
k = r_dict[r]
with open(fname, "r") as file:
data = json.loads(file.read())
A = np.array(data["A"])
samples = np.array(data["samples"])
m = dict()
m["rho"] = density(A)
m["rho-samples"] = density(samples.mean(axis=0))
m["ps"] = posterior_similarity(samples, A)
m["sps"] = samplewise_posterior_similarity(samples, A)
m["fs"] = f_score(samples, A)
m["fs-norm-random"] = f_score(samples, A, normalize=True, rho_guess=0.5)
m["fs-norm-density"] = f_score(samples, A, normalize=True, rho_guess=m["rho"])
m["fce"] = fraction_of_correct_entries(samples, A)
m["fce-norm-random"] = fraction_of_correct_entries(
samples, A, normalize=True, rho_guess=0.5
)
m["fce-norm-density"] = fraction_of_correct_entries(
samples, A, normalize=True, rho_guess=m["rho"]
)
m["precision"] = precision(samples, A)
m["recall"] = recall(samples, A)
m["auroc"] = auroc(samples, A)
m["auprc"] = auprc(samples, A)
print((i, j, k), flush=True)
return i, j, k, m
# get number of available cores
n_processes = len(os.sched_getaffinity(0))
c_dict, t_dict, r_dict = collect_parameters(data_dir)
n_c = len(c_dict)
n_t = len(t_dict)
n_r = len(r_dict)
data = {}
data["tmax"] = list(t_dict)
data["rho"] = np.zeros((n_c, n_t, n_r))
data["rho-samples"] = np.zeros((n_c, n_t, n_r))
data["ps"] = np.zeros((n_c, n_t, n_r))
data["sps"] = np.zeros((n_c, n_t, n_r))
data["fs"] = np.zeros((n_c, n_t, n_r))
data["fs-norm-random"] = np.zeros((n_c, n_t, n_r))
data["fs-norm-density"] = np.zeros((n_c, n_t, n_r))
data["fce"] = np.zeros((n_c, n_t, n_r))
data["fce-norm-random"] = np.zeros((n_c, n_t, n_r))
data["fce-norm-density"] = np.zeros((n_c, n_t, n_r))
data["precision"] = np.zeros((n_c, n_t, n_r))
data["recall"] = np.zeros((n_c, n_t, n_r))
data["auroc"] = np.zeros((n_c, n_t, n_r))
data["auprc"] = np.zeros((n_c, n_t, n_r))
arglist = []
for f in os.listdir(data_dir):
arglist.append((f, data_dir, c_dict, t_dict, r_dict))
results = Parallel(n_jobs=n_processes)(delayed(get_metrics)(*arg) for arg in arglist)
for i, j, k, m in results:
for key, val in m.items():
data[key][i, j, k] = val
for key, val in data.items():
if not isinstance(val, list):
data[key] = val.tolist()
datastring = json.dumps(data)
with open("Data/zkc_infer_vs_tmax.json", "w") as output_file:
output_file.write(datastring)