forked from phamquiluan/RCAEval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrq1.py
304 lines (249 loc) · 9.61 KB
/
rq1.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import argparse
import glob
import math
import itertools
import json
import os
import pickle
import warnings
from datetime import datetime, timedelta
from tempfile import TemporaryDirectory
from os.path import abspath, basename, dirname, exists, join
warnings.filterwarnings("ignore")
import numpy as np
import pandas as pd
from causallearn.graph.GeneralGraph import GeneralGraph
from causallearn.score.LocalScoreFunction import local_score_BIC
from tqdm import tqdm
from RCAEval.benchmark.evaluation import Evaluator
from RCAEval.benchmark.metrics import F1, SHD, F1_Skeleton
from RCAEval.classes.graph import MemoryGraph, Node
from RCAEval.graph_heads import finalize_directed_adj
from RCAEval.io.time_series import drop_constant, drop_extra, drop_time
from RCAEval.utility import (
dump_json,
download_syn_rcd_dataset,
download_syn_circa_dataset,
download_syn_causil_dataset,
is_py310,
load_json,
)
if is_py310():
from causallearn.search.ConstraintBased.FCI import fci
from causallearn.search.ConstraintBased.PC import pc
from causallearn.search.FCMBased.lingam import DirectLiNGAM, ICALiNGAM, VARLiNGAM
from causallearn.search.ScoreBased.GES import ges
from causallearn.utils.cit import chisq, fisherz, gsq, kci, mv_fisherz
from RCAEval.graph_construction.granger import granger
from RCAEval.graph_construction.pcmci import pcmci
from RCAEval.graph_construction.cmlp import cmlp
try:
from RCAEval.graph_construction.dag_gnn import dag_gnn
from RCAEval.graph_construction.dag_gnn import notears_low_rank as ntlr
from RCAEval.graph_construction.notears import notears
except Exception as e:
print(e)
else:
from RCAEval.graph_construction.fges import fges
AVAILABLE_METHODS = sorted(
[
"pc",
"ppc",
"pcmci",
"fci",
"fges",
"notears",
"ntlr",
"DirectLiNGAM",
"VARLiNGAM",
"ICALiNGAM",
"ges",
"granger",
]
)
def parse_args():
parser = argparse.ArgumentParser(description="RCAEval evaluation")
# for data
parser.add_argument("--dataset", type=str, default="data", help="Choose a dataset.",
choices=["circa10", "circa50", "rcd10", "rcd50", "causil10", "causil50"])
parser.add_argument("--method", type=str, help="Method name")
parser.add_argument("--length", type=int, default=None, help="length of time series")
parser.add_argument("--test", action="store_true", help="Perform smoke test on certain methods without fully run")
args = parser.parse_args()
if args.method not in globals():
raise ValueError(f"{args.method=} not defined. Available: {AVAILABLE_METHODS}")
if args.dataset not in ["circa10", "circa50", "rcd10", "rcd50", "causil10", "causil50"]:
print(f"{args.dataset=} not defined. Available: circa10, circa50, rcd10, rcd50, causil10, causil50")
exit()
return args
args = parse_args()
# download dataset
if "circa" in args.dataset:
download_syn_circa_dataset()
elif "rcd" in args.dataset:
download_syn_rcd_dataset()
elif "causil" in args.dataset:
download_syn_causil_dataset()
DATASET_MAP = {
"circa10": "data/syn_circa/10",
"circa50": "data/syn_circa/50",
"causil10": "data/syn_causil/10",
"causil50": "data/syn_causil/50",
"rcd10": "data/syn_rcd/10",
"rcd50": "data/syn_rcd/50"
}
dataset = DATASET_MAP[args.dataset]
output_path = TemporaryDirectory().name
report_path = join(output_path, "report.xlsx")
result_path = join(output_path, "results")
os.makedirs(result_path, exist_ok=True)
# ==== PROCESS TO GENERATE JSON ====
data_paths = list(glob.glob(os.path.join(dataset, "**/data.csv"), recursive=True))
if args.test is True:
data_paths = data_paths[:2]
def evaluate():
eval_data = {
"Case": [],
"Precision": [],
"Recall": [],
"F1-Score": [],
"Precision-Skel": [],
"Recall-Skel": [],
"F1-Skel": [],
"SHD": [],
}
for data_path in data_paths:
if "circa" in data_path or "rcd" in data_path:
num_node = int(basename(dirname(dirname(dirname(dirname(data_path))))))
graph_idx = int(basename(dirname(dirname(dirname(data_path)))))
case_idx = int(basename(dirname(data_path)))
if "causil" in data_path:
graph_idx = int(basename(dirname(data_path))[-1:])
case_idx = 0
# ===== READ RESULT =====
est_graph_name = f"{graph_idx}_{case_idx}_est_graph.json"
est_graph_path = join(result_path, est_graph_name)
if not exists(est_graph_path):
continue
est_graph = MemoryGraph.load(est_graph_path)
# ====== READ TRUE GRAPH =====
if "circa" in data_path:
true_graph_path = join(dirname(dirname(dirname(data_path))), "graph.json")
true_graph = MemoryGraph.load(true_graph_path)
if "causil" in data_path:
dag_gt = pickle.load(open(join(dirname(data_path), "DAG.gpickle"), "rb"))
true_graph = MemoryGraph(dag_gt)
if "rcd" in data_path:
dag_gt = pickle.load(
open(join(dirname(dirname(dirname(data_path))), "g_graph.pkl"), "rb")
)
true_graph = MemoryGraph(dag_gt)
true_graph = MemoryGraph.load(
join(dirname(dirname(dirname(data_path))), "true_graph.json")
)
e = F1(true_graph, est_graph)
e_skel = F1_Skeleton(true_graph, est_graph)
shd = SHD(true_graph, est_graph)
eval_data["Case"].append(est_graph_name)
eval_data["Precision"].append(e["precision"])
eval_data["Recall"].append(e["recall"])
eval_data["F1-Score"].append(e["f1"])
eval_data["Precision-Skel"].append(e_skel["precision"])
eval_data["Recall-Skel"].append(e_skel["recall"])
eval_data["F1-Skel"].append(e_skel["f1"])
eval_data["SHD"].append(shd)
avg_precision = np.mean(eval_data["Precision"])
avg_recall = np.mean(eval_data["Recall"])
avg_f1 = np.mean(eval_data["F1-Score"])
avg_precision_skel = np.mean(eval_data["Precision-Skel"])
avg_recall_skel = np.mean(eval_data["Recall-Skel"])
avg_f1_skel = np.mean(eval_data["F1-Skel"])
avg_shd = np.mean(eval_data["SHD"])
print(f"F1: {avg_f1:.2f}")
print(f"F1-S: {avg_f1_skel:.2f}")
print(f"SHD: {math.floor(avg_shd)}")
def process(data_path):
if "circa" in data_path:
num_node = int(basename(dirname(dirname(dirname(dirname(data_path))))))
graph_idx = int(basename(dirname(dirname(dirname(data_path)))))
case_idx = int(basename(dirname(data_path)))
if "causil" in data_path:
num_node = int(basename(dirname(dirname(dirname(data_path)))).split("_")[0])
graph_idx = int(basename(dirname(data_path))[-1:])
case_idx = 0
if "rcd" in data_path:
num_node = int(basename(dirname(dirname(dirname(dirname(data_path))))))
graph_idx = int(basename(dirname(dirname(dirname(data_path)))))
case_idx = int(basename(dirname(data_path)))
if "circa" in data_path:
data = pd.read_csv(data_path, header=None)
data.header = list(map(str, range(0, data.shape[1])))
else:
data = pd.read_csv(data_path)
# == PROCESS ==
data = data.fillna(method="ffill")
data = data.fillna(value=0)
np_data = np.absolute(data.to_numpy().astype(float))
if args.length is not None:
np_data = np_data[: args.length, :]
adj = []
G = None
st = datetime.now()
try:
if args.method == "pc":
adj = pc(
np_data,
stable=False,
show_progress=False,
).G.graph
elif args.method == "fci":
adj = fci(
np_data,
show_progress=False,
verbose=False,
)[0].graph
elif args.method == "fges":
adj = fges(pd.DataFrame(np_data))
elif args.method == "ICALiNGAM":
model = ICALiNGAM()
model.fit(np_data)
adj = model.adjacency_matrix_
adj = adj.astype(bool).astype(int)
elif args.method == "VARLiNGAM":
raise NotImplementedError
elif args.method == "DirectLiNGAM":
model = DirectLiNGAM()
model.fit(np_data)
adj = model.adjacency_matrix_
adj = adj.astype(bool).astype(int)
elif args.method == "ges":
record = ges(np_data)
adj = record["G"].graph
elif args.method == "granger":
adj = granger(data)
elif args.method == "pcmci":
adj = pcmci(pd.DataFrame(np_data))
elif args.method == "ntlr":
adj = ntlr(pd.DataFrame(np_data))
else:
raise ValueError(f"{args.method=} not defined. Available: {AVAILABLE_METHODS}")
if "circa" in data_path:
est_graph = MemoryGraph.from_adj(
adj, nodes=[Node("SIM", str(i)) for i in range(len(adj))]
)
else:
est_graph = MemoryGraph.from_adj(adj, nodes=data.columns.to_list())
est_graph.dump(join(result_path, f"{graph_idx}_{case_idx}_est_graph.json"))
except Exception as e:
raise e
print(f"{args.method=} failed on {data_path=}")
est_graph = MemoryGraph.from_adj([], nodes=[])
est_graph.dump(join(result_path, f"{graph_idx}_{case_idx}_failed.json"))
start_time = datetime.now()
for data_path in tqdm(data_paths):
output = process(data_path)
end_time = datetime.now()
time_taken = end_time - start_time
avg_speed = round(time_taken.total_seconds() / len(data_paths), 2)
evaluate()
print("Avg speed:", avg_speed)