-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_baselines.py
276 lines (242 loc) · 9.98 KB
/
calc_baselines.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import argparse
import pickle
import os
import pandas as pd
import torchmetrics
from data.generator import load_deterministic_ds
from helpers.tools import (
corr_calc_aerosol,
corr_ci,
corr_run_full_ds,
corr_run_full_ds_masked_diagonal,
load_river_data,
roc_curve,
)
from helpers.tools import (
load_river_data,
roc_curve,
var_calc_aerosol,
var_ci,
var_run_full_ds,
)
# Specify which baselines should be calculated.
# PLEASE use the second environment for the PCMCI results!!
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--pcmci", action="store_true")
parser.add_argument("--var", action="store_true")
parser.add_argument("--corr", action="store_true")
parser.add_argument("--synth", action="store_true")
parser.add_argument("--others", action="store_true")
parser.add_argument("--speed", action="store_true")
args = parser.parse_args()
synth1 = ["SL", "ML", "SNL", "MNL", "LNL", "XLNL", "joint"]
synth2 = ["scale_up", "scale_up_2", "scale_up_3", "scale_up_4", "scale_up_5"]
if not os.path.exists("scores"):
os.makedirs("scores")
if args.corr:
if args.synth:
save_paths = [
"scores/corr_test_auroc.p",
"scores/corr_test_scale_up_auroc.p",
]
# Te is full out of distribution while Te2 is in distribution data.
for n, exp in enumerate(
[
synth1,
synth2,
]
):
save = {}
print("Calc Corr Synth " + str(n))
for ds in exp:
print(ds)
tr, val, te = load_deterministic_ds(ds, corr_input=False)
a = corr_run_full_ds(tr)
b = corr_run_full_ds(val)
c = corr_run_full_ds(te)
_, _, te2 = load_deterministic_ds(
ds_name=ds + "_additional", corr_input=False
)
d = corr_run_full_ds(te2)
save[ds] = [a, b, c, d]
pickle.dump(save, open(save_paths[n], "wb"))
if args.others:
print("Calc Corr Others")
# Kuramoto
tr, val, te = load_deterministic_ds("kuramoto", corr_input=False)
a = corr_run_full_ds_masked_diagonal(tr, size=5)
b = corr_run_full_ds_masked_diagonal(val, size=5)
c = corr_run_full_ds_masked_diagonal(te, size=5)
pickle.dump([a, b, c], open("scores/kuramoto_corr_test_auroc.p", "wb"))
# River data
roc = torchmetrics.classification.BinaryROC()
auroc = torchmetrics.classification.BinaryAUROC()
data, lab = load_river_data()
corr = corr_ci(data, max_lags=1)[0]
result = roc_curve(corr.max(axis=3)[0], lab, roc, auroc)
pickle.dump(result, open("scores/river_corr_test_auroc.p", "wb"))
# Aerosol:
data = pickle.load(
open("data/deterministic_ds/aerosol/simple_test.p", "rb")
)
res = corr_calc_aerosol(data)
pickle.dump(res, open("scores/aerosol_corr_test_auroc.p", "wb"))
if args.speed:
print("Calc Corr Inference speed")
# inference speed
out = []
for _ in range(100):
stack = []
for ds in synth2:
tr, val, te = load_deterministic_ds(ds, corr_input=False)
te = next(iter(te))[0]
res, secs = corr_ci(te, max_lags=3)
stack.append(secs)
out.append(stack)
table = pd.concat(
[pd.DataFrame(out).mean(), pd.DataFrame(out).std()], axis=1
)
table.columns = ["M_C", "Std_C"]
table.to_csv("scores/compute_C.csv")
if args.var:
if args.synth:
save_paths = ["scores/var_test_auroc.p", "scores/var_test_scale_up_auroc.p"]
for n, exp in enumerate(
[
synth1,
synth2,
]
):
save = {}
print("Calc VAR Synth " + str(n))
for ds in exp:
tr, val, te = load_deterministic_ds(ds, corr_input=False)
a = var_run_full_ds(tr)
b = var_run_full_ds(val)
c = var_run_full_ds(te)
_, _, te2 = load_deterministic_ds(
ds_name=ds + "_additional", corr_input=False
)
d = var_run_full_ds(te2)
save[ds] = [a, b, c, d]
pickle.dump(save, open(save_paths[n], "wb"))
if args.others:
print("Calc Var Others")
# Kuramoto
for ds in ["kuramoto"]:
tr, val, te = load_deterministic_ds(ds, corr_input=False)
a = var_run_full_ds(tr)
b = var_run_full_ds(val)
c = var_run_full_ds(te)
pickle.dump([a, b, c], open("scores/var_test_kuramoto_auroc.p", "wb"))
# River data
roc = torchmetrics.classification.BinaryROC()
auroc = torchmetrics.classification.BinaryAUROC()
data, lab = load_river_data()
corr = var_ci(data, max_lags=1)[0]
result = roc_curve(corr.max(axis=3)[0], lab, roc, auroc)
pickle.dump(result, open("scores/river_var_test_auroc.p", "wb"))
# Aerosol:
data = pickle.load(
open("data/deterministic_ds/aerosol/simple_test.p", "rb")
)
res = var_calc_aerosol(data)
pickle.dump(res, open("scores/aerosol_var_test_auroc.p", "wb"))
if args.speed:
print("Calc Var Inference speed")
# inference speed
out = []
for _ in range(100):
stack = []
for ds in synth2:
tr, val, te = load_deterministic_ds(ds, corr_input=False)
te = next(iter(te))[0]
res, secs = var_ci(te, max_lags=3)
stack.append(secs)
out.append(stack)
table = pd.concat(
[pd.DataFrame(out).mean(), pd.DataFrame(out).std()], axis=1
)
table.columns = ["M_V", "Std_V"]
table.to_csv("scores/compute_V.csv")
if args.pcmci:
print("Please use second environment for pcmci computations.")
print("Also this takes a while...")
from helpers.tigramite_tools import (
pcmci_ci,
pcmci_calc_aerosol,
pcmci_run_full_ds,
)
from tigramite.independence_tests.parcorr_wls import ParCorr
roc = torchmetrics.classification.BinaryROC()
auroc = torchmetrics.classification.BinaryAUROC()
if args.synth:
print("Calc PCMCI Synthetic")
save_paths = [
"scores/pcmci_test_auroc.p",
"scores/pcmci_test_auroc_joint.p",
"scores/pcmci_test_scale_up_auroc.p",
]
for n, exp in enumerate(
[
synth1[:-1],
synth1[-1:],
synth2,
]
):
save = {}
for ds in exp:
tr, val, te = load_deterministic_ds(ds, corr_input=False)
out, lab = pcmci_run_full_ds(te)
d = roc_curve(1 - out, lab, roc, auroc)
tr, val, te = load_deterministic_ds(
ds + "_additional", corr_input=False
)
out, lab = pcmci_run_full_ds(te)
d2 = roc_curve(
1 - out, lab, roc, auroc
) # we reverse the p values to calculate the roc curve (only based on ordering)
save[ds] = [d, d2]
pickle.dump(save, open(save_paths[n], "wb"))
if args.others:
print("Calc PCMCI Others")
save = {}
for ds in ["kuramoto"]:
print(ds)
tr, val, te = load_deterministic_ds(ds,corr_input=False)
out, lab = pcmci_run_full_ds(te)
d = roc_curve(1-out,lab, roc, auroc)
save[ds] = [d]
pickle.dump(save, open("scores/pcmci_test_kuramoto_auroc.p", "wb"))
data,lab = load_river_data()
c_test = ParCorr()
roc = torchmetrics.classification.BinaryROC()
auroc = torchmetrics.classification.BinaryAUROC()
corr = pcmci_ci(data,c_test, max_lags=3)[0]
result = roc_curve(1- corr.min(axis=3),lab, roc,auroc)
pickle.dump(result, open("scores/river_pcmci_test_auroc.p", "wb"))
result
# Aerosol: (cut the ts at 100 to make this somehow runnable.)
data = pickle.load(open("data/deterministic_ds/aerosol/simple_test.p", "rb"))
res = pcmci_calc_aerosol([data[0][:,:100,:], data[1][:,:100,:]])
print(res)
pickle.dump(res, open("scores/aerosol_var_test_auroc.p", "wb"))
if args.speed:
print("Calc PCMCI Inference speed")
# inference speed
c_test = ParCorr()
out = []
for ds in synth2: # "big_base", "big_base_mirror", "poly", "big_poly", "nl", "big_nl"
stack = []
tr, val, te = load_deterministic_ds(ds,corr_input=False)
for _ in range(10):
sample = next(iter(te))[0][:2]
res,secs = pcmci_ci(sample, c_test, max_lags=3)
stack.append(secs)
out.append(stack)
table = pd.concat([pd.DataFrame(out).T.mean(), pd.DataFrame(out).T.std()],axis=1)
table.columns = ["M_P", "Std_P"]
table.to_csv("scores/compute_P.csv")
if __name__ == "__main__":
main()