-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrun_algorithm.py
More file actions
executable file
·303 lines (273 loc) · 11.2 KB
/
run_algorithm.py
File metadata and controls
executable file
·303 lines (273 loc) · 11.2 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import sys
import time
from functools import reduce
from pathlib import Path
from typing import Dict, List
import click
import numpy as np
import pandas as pd
# from run_apriori import run
from joblib import Parallel, delayed
from loguru import logger
# noinspection PyProtectedMember
from loguru._defaults import LOGURU_FORMAT
from Adtributor import Adtributor
from Apriori import AprioriRCA
from ImpAPTr import ImpAPTr
from MID import MID
from hotspotpy import HotSpot
from post_process import post_process
from squeeze import Squeeze, SqueezeOption
from utility import AC
@click.command('Runner')
@click.option("--name", default="", help="name of this setting")
@click.option("--input-path", "-i", help="will read data from {input_path}/{name}")
@click.option("--output-path", "-o", help="if {output_path} is a dir, save to {output_path}/{name}.json; \
otherwise save to {output_path}")
@click.option("--num-workers", "-j", default=1, help="num of processes")
@click.option(
"--injection_info", default="",
help="path to injection info, if empty, {input_path}/{name}/injection_info.csv will be used"
)
@click.option(
"--algorithm", default="psqueeze", help="algorithm name",
)
@click.option("--derived", is_flag=True, help="means we should read {timestamp}.a.csv and {timestamp}.b.csv")
@click.option("--toint", is_flag=True, help="round measure values to integer")
@click.option("--n-ele", default=1, help="for ImpAPTr")
def main(name, input_path, output_path, num_workers, **kwargs):
"""
:param name:
:param input_path:
:param output_path:
:param num_workers:
:param kwargs:
:return:
"""
logger.remove()
logger.add(
sys.stdout, level="DEBUG",
format="[<green>{time}</green>, <blue>{level}</blue>] <white>{message}</white>"
)
input_path = Path(input_path)
assert input_path.exists(), f"{input_path} does not exist"
logger.info(f"read data from {input_path / name}")
output_path = Path(output_path)
if output_path.is_dir():
output_path = output_path / f"{name}.json"
elif not output_path.exists():
logger.info(f"create {output_path}")
output_path.mkdir(parents=True)
output_path = output_path / f"{name}.json"
logger.info(f"save to {output_path}")
injection_info: str = kwargs.pop('injection_info')
if not injection_info:
injection_info = str(input_path / name / "injection_info.csv")
injection_info: pd.DataFrame = pd.read_csv(injection_info, engine='c')
timestamps = sorted(injection_info['timestamp'])
# timestamps = ['1451188800'] # NOTE: for debug
injection_info: pd.DataFrame = injection_info.set_index(['timestamp'])
derived: bool = kwargs.pop('derived')
if not derived:
results = Parallel(n_jobs=num_workers, backend="multiprocessing", verbose=100)(
delayed(executor)(file_path, output_path.parent, injection_info, **kwargs)
for file_path in map(lambda x: input_path / name / f'{x}.csv', timestamps))
else:
results = Parallel(n_jobs=num_workers, backend="multiprocessing", verbose=100)(
delayed(executor_derived)(file_path_list, output_path.parent, injection_info, **kwargs)
for file_path_list in map(
lambda x: [input_path / name / f'{x}.a.csv', input_path / name / f'{x}.b.csv'],
timestamps
)
)
if kwargs['algorithm'] in {'psq', 'psqueeze'}:
post_process(results)
with open(str(output_path.resolve()), "w+") as f:
json.dump(results, f, indent=4)
logger.info(json.dumps(results, indent=4))
def load_data(file_path: Path, injection_info, toint=False):
df = pd.read_csv(file_path.resolve(), dtype='str', delimiter=r",")
if "ex_rc_dim" in injection_info.columns:
ex_rc_dim = str(injection_info.loc[int(file_path.stem), "ex_rc_dim"])
if not ex_rc_dim == "nan":
# TODO? wrong. need groupby
df = df.drop(ex_rc_dim.split(";"), axis=1)
df['real'] = df['real'].astype(float)
df['predict'] = df['predict'].astype(float)
if toint:
df['real'] = df['real'].astype(int)
df['predict'] = df['predict'].astype(int)
return df
def executor(file_path: Path, output_path: Path, injection_info: pd.DataFrame, **kwargs) -> Dict:
from loguru import logger
debug = kwargs.pop('debug', False)
toint = kwargs.pop('toint', False)
algorithm = kwargs.pop("algorithm", "psqueeze")
logger.remove()
logger.add(
sys.stdout, level='INFO',
format=f"<yellow>{file_path.name}</yellow> - {LOGURU_FORMAT}",
backtrace=True
)
logger.info(f"running {algorithm} for {file_path}")
df = load_data(file_path, injection_info, toint)
try:
timestamp = int(file_path.name.rstrip('.csv'))
except ValueError:
timestamp = file_path.name.rstrip('.csv')
logger.warning(f"Unresolved timestamp: {timestamp}")
tic = time.time()
if algorithm.lower() in {'psqueeze', 'psq'}:
model = get_model_psq(
[df], op=lambda x: x,
debug=debug, output_path=output_path, timestamp=timestamp, derived=False,
**kwargs
)
elif algorithm.lower() in {'squeeze', 'sq'}:
model = get_model_squeeze(
[df], op=lambda x: x,
debug=debug, output_path=output_path, timestamp=timestamp, derived=False,
**kwargs
)
elif algorithm.lower() in {'mid'}:
model = MID(data_list=[df], **kwargs)
elif algorithm.lower() in {"impaptr", 'iap'}:
model = ImpAPTr(data_list=[df], **kwargs)
elif algorithm.lower() in {"apriori", 'apr'}:
model = AprioriRCA(data_list=[df], **kwargs)
elif algorithm.lower() in {"adt", "adtributor", 'rad', 'r-adtributor', 'recursive_adtributor'}:
model = Adtributor([df], algorithm, derived=False)
elif algorithm.lower() in {"hs", "hotspot"}:
model = HotSpot(data_list=[df], max_steps=100,
ps_upper_threshold=0.95,
ps_lower_threshold=0.05,
)
else:
raise RuntimeError(f"unknown algorithm name: {algorithm=}")
model.run()
logger.info("\n" + model.report)
try:
root_cause = AC.batch_to_string(
frozenset(reduce(lambda x, y: x.union(y), model.root_cause, set())))
except IndexError:
root_cause = ""
toc = time.time()
elapsed_time = toc - tic
try:
ground_truth = injection_info.loc[int(file_path.stem), 'set'] if int(file_path.stem) in injection_info.index else None
except ValueError:
ground_truth = None
result = {
'timestamp': timestamp,
'elapsed_time': elapsed_time,
'root_cause': root_cause,
# 'ep': explanatory_power(model.derived_data, root_cause) if algorithm in {'psq', 'psqueeze'} else None,
'ground_truth': ground_truth,
'info_collect': model.info_collect,
}
return result
def executor_derived(file_path_list: List[Path], output_path: Path, injection_info: pd.DataFrame, **kwargs) -> Dict:
from loguru import logger
toint = kwargs.pop('toint', False)
debug = kwargs.pop('debug', False)
logger.remove()
ts = file_path_list[0].name.rstrip('.a.csv')
logger.add(
sys.stdout, level='INFO',
format=f"<yellow>{ts}</yellow> - {LOGURU_FORMAT}",
backtrace=True
)
algorithm = kwargs.pop("algorithm", "psqueeze")
logger.info(f"running {algorithm} for {ts}")
dfa = load_data(file_path_list[0].resolve(), injection_info, toint=toint)
dfb = load_data(file_path_list[1].resolve(), injection_info, toint=toint)
zero_index = (dfa.real == 0) & (dfa.predict == 0) & (dfb.real == 0) & (dfb.predict == 0)
dfa = dfa[~zero_index]
dfb = dfb[~zero_index]
try:
timestamp = int(ts)
except ValueError:
timestamp = ts
logger.warning(f"Unresolved timestamp: {timestamp}")
tic = time.time()
divide = lambda x, y: np.divide(x, y, out=np.zeros_like(x), where=y != 0)
if algorithm.lower() in {'psqueeze', 'psq'}:
model = get_model_psq(
[dfa, dfb], op=divide,
debug=debug, output_path=output_path, timestamp=timestamp, derived=True,
**kwargs
)
elif algorithm.lower() in {'squeeze', 'sq'}:
model = get_model_squeeze(
[dfa, dfb], op=divide,
debug=debug, output_path=output_path, timestamp=timestamp, derived=True,
**kwargs
)
elif algorithm.lower() in {'mid'}:
model = MID(data_list=[dfa, dfb], op=divide, **kwargs)
elif algorithm.lower() in {"impaptr", 'iap'}:
model = ImpAPTr(data_list=[dfa, dfb], op=divide, **kwargs)
elif algorithm.lower() in {"apriori", 'apr'}:
model = AprioriRCA(data_list=[dfa, dfb], op=divide, **kwargs)
elif algorithm.lower() in {"adt", "adtributor", 'rad', 'r-adtributor', 'recursive_adtributor'}:
model = Adtributor([dfa, dfb], algorithm, derived=True)
elif algorithm.lower() in {"hs", "hotspot"}:
model = HotSpot(data_list=[dfa, dfb], op=divide, max_steps=100,
ps_upper_threshold=0.95,
ps_lower_threshold=0.05, )
else:
raise RuntimeError(f"unknown algorithm name: {algorithm=}")
model.run()
logger.info("\n" + model.report)
try:
root_cause = AC.batch_to_string(
frozenset(reduce(lambda x, y: x.union(y), model.root_cause, set()))) # type:
except IndexError:
root_cause = ""
toc = time.time()
elapsed_time = toc - tic
try:
ground_truth = injection_info.loc[int(timestamp), 'set'] if int(timestamp) in injection_info.index else None
except ValueError:
ground_truth = None
result = {
'timestamp': timestamp,
'elapsed_time': elapsed_time,
'root_cause': root_cause,
# 'ep': ep,
'ground_truth': ground_truth,
'info_collect': model.info_collect,
}
return result
def get_model_squeeze(data_list, op, debug: bool, output_path: Path, timestamp: int, derived: bool, **kwargs):
option = SqueezeOption(
psqueeze=False,
debug=debug,
fig_save_path=f"{output_path.resolve()}/{timestamp}" + "{suffix}" + ".pdf",
**kwargs,
)
return Squeeze(data_list=data_list, op=op, option=option)
def get_model_psq(data_list, op, debug: bool, output_path: Path, timestamp: int, derived: bool, **kwargs):
option = SqueezeOption(
psqueeze=True,
debug=debug,
fig_save_path=f"{output_path.resolve()}/{timestamp}" + "{suffix}" + ".pdf",
density_estimation_method="histogram_prob",
bias=1 if not derived else 0,
**kwargs,
)
return Squeeze(data_list=data_list, op=op, option=option)
def explanatory_power(df, rc_str):
if not rc_str: return 0.0
delta = df["real"].values.sum() - df["predict"].values.sum()
rc_list = [dict(map(lambda x: x.split("="), i.split("&"))) for i in rc_str.split(";")]
cover = df.loc[np.logical_or.reduce([
np.logical_and.reduce([df[k] == v for k, v in i.items()])
for i in rc_list
])]
return (cover["real"].values.sum() - cover["predict"].values.sum()) / delta
if __name__ == '__main__':
main()