-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis.py
345 lines (257 loc) · 10.7 KB
/
analysis.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python3
"""
Generate the figures and results for the paper: "Sintel: A Machine
Learning Framework to Extract Insights from Signals."
"""
import os
import json
import logging
import pickle
import warnings
from functools import partial
from pathlib import Path
import pandas as pd
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
from orion.benchmark import benchmark
from orion.evaluation import CONTEXTUAL_METRICS as METRICS
from orion.evaluation import contextual_confusion_matrix
from sintel.benchmark import tune_benchmark, BENCHMARK_DATA
warnings.simplefilter('ignore')
LOGGER = logging.getLogger(__name__)
plt.style.use('default')
mpl.rcParams['hatch.linewidth'] = 0.2
DATA_PATH = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'data'
)
RESULT_PATH = os.path.join(DATA_PATH, '{}.csv')
OUTPUT_PATH = Path('output')
os.makedirs(OUTPUT_PATH, exist_ok=True)
PIPELINES = [
'arima',
'lstm_dynamic_threshold',
'lstm_autoencoder',
'dense_autoencoder',
'tadgan'
]
with open(os.path.join(DATA_PATH, 'data.json'), 'r') as fp:
DATASETS = json.load(fp)
_ORDER = ['arima', 'lstm_autoencoder', 'lstm_dynamic_threshold', 'dense_autoencoder', 'tadgan']
_LABELS = ['ARIMA', 'LSTM AE', 'LSTM DT', 'Dense AE', 'TadGAN']
_COLORS = ["#ED553B", "#F6D55C", "#3CAEA3", "#20639B", "#173F5F"]
_HATCHES = ['-', '//', '|', '\\', 'x'] * 2
_PALETTE = sns.color_palette(_COLORS)
# ------------------------------------------------------------------------------
# Saving results
# ------------------------------------------------------------------------------
def _savefig(fig, name, figdir=OUTPUT_PATH):
figdir = Path(figdir)
for ext in ['.png', '.pdf', '.eps']:
fig.savefig(figdir.joinpath(name+ext),
bbox_inches='tight')
# ------------------------------------------------------------------------------
# Plotting results
# ------------------------------------------------------------------------------
def _get_summary(result):
order = [
'lstm_dynamic_threshold',
'tadgan',
'lstm_autoencoder',
'arima',
'dense_autoencoder',
'azure'
]
family = {
"MSL": "NASA",
"SMAP": "NASA",
"YAHOOA1": "YAHOO",
"YAHOOA2": "YAHOO",
"YAHOOA3": "YAHOO",
"YAHOOA4": "YAHOO",
"artificialWithAnomaly": "NAB",
"realAWSCloudwatch": "NAB",
"realAdExchange": "NAB",
"realTraffic": "NAB",
"realTweets": "NAB"
}
result['group'] = result['dataset'].apply(family.get)
df = result.groupby(['group', 'dataset', 'pipeline'])[['fp', 'fn', 'tp']].sum().reset_index()
df['precision'] = df.eval('tp / (tp + fp)')
df['recall'] = df.eval('tp / (tp + fn)')
df['f1'] = df.eval('2 * (precision * recall) / (precision + recall)')
df = df.reset_index()
df = df.groupby(['group', 'pipeline']).mean().reset_index()
df = df.set_index(['group', 'pipeline'])[['f1', 'precision', 'recall']].unstack().T.reset_index(level=0)
df = df.pivot(columns='level_0')
df.columns = df.columns.rename('dataset', level=0)
df.columns = df.columns.rename('metric', level=1)
return df.loc[order]
def make_table_2():
result = pd.read_csv(RESULT_PATH.format('results'))
return _get_summary(result)
def make_figure_7a():
profiles = pd.read_csv(RESULT_PATH.format('comp_performance'))
profiles['pipeline'] = pd.Categorical(profiles['pipeline'], _ORDER)
profiles = profiles.sort_values('pipeline')
fig, axes = plt.subplots(
nrows=1, ncols=2, figsize=(6.5, 2),
gridspec_kw={'width_ratios':[1,2]}
)
memory = profiles[profiles['source'] == 'memory']
g = sns.barplot(x="source", y='value', hue="pipeline", ax=axes[0], palette=_PALETTE,
data=memory, saturation=0.7, linewidth=0.5, edgecolor='k')
for i,thisbar in enumerate(g.patches):
thisbar.set_hatch(_HATCHES[i] * 3)
time = pd.concat([
profiles[profiles['source'] == 'fit_time'],
profiles[profiles['source'] == 'predict_time']
])
g = sns.barplot(x="source", y='value', hue="pipeline", ax=axes[1], palette=_PALETTE,
data=time, saturation=0.7, linewidth=0.5, edgecolor='k')
for bars, hatch in zip(g.containers, _HATCHES):
for bar in bars:
bar.set_hatch(hatch * 3)
xlabels = [['Memory'], ['Training Time', 'Pipeline Latency']]
for i in range(2):
axes[i].set_yscale('log')
axes[i].grid(True, linestyle='--')
axes[i].set_xticklabels(xlabels[i])
axes[i].get_legend().remove()
axes[i].set_xlabel('')
axes[0].set_ylim([0.2e6, 0.2e9])
axes[1].set_ylim([0.2e1, 0.2e5])
axes[0].set_ylabel('memory in KB (log)')
axes[1].set_ylabel('time in seconds (log)')
handles = [mpatches.Patch(facecolor=_COLORS[i], label=_LABELS[i], hatch=_HATCHES[i] * 3, ec='k', lw=0.5)
for i in range(len(_LABELS))]
fig.legend(handles=handles, edgecolor='k', loc='upper center', bbox_to_anchor=(0.53, 1.12), ncol=len(_LABELS))
plt.tight_layout()
_savefig(fig, 'figure7a', figdir=OUTPUT_PATH)
def make_figure_7b():
time = pd.read_csv(RESULT_PATH.format('delta'))
end = time.groupby('pipeline')['end-to-end'].mean()
alone = time.groupby('pipeline')['stand-alone'].mean()
avg_inc = (end - alone) / alone * 100
avg_inc = avg_inc.reset_index()
avg_inc.columns = ['pipeline', 'inc']
fig = plt.figure(figsize=(3.6, 3))
ax = plt.gca()
g = sns.barplot(x='pipeline', y='inc', data=avg_inc, palette=_PALETTE, order=_ORDER,
linewidth=0.5, edgecolor='k', ax=ax)
for i,thisbar in enumerate(g.patches):
thisbar.set_hatch(_HATCHES[i] * 3)
labels = ['\n'.join(label.split(' ')) for label in _LABELS]
ax.set_xticks(range(len(labels)))
ax.set_xticklabels(labels)
plt.title('Increase of Pipeline Runtime')
plt.xlabel('')
plt.ylabel('Average Increase (%)')
plt.ylim([0, 4])
plt.grid(True, linestyle='--')
plt.tight_layout()
_savefig(fig, 'figure7b', figdir=OUTPUT_PATH)
def _get_f1_score(df):
df = df.groupby(['dataset', 'pipeline'])[['fp', 'fn', 'tp']].sum().reset_index()
df['precision'] = df.eval('tp / (tp + fp)')
df['recall'] = df.eval('tp / (tp + fn)')
df['f1'] = df.eval('2 * (precision * recall) / (precision + recall)')
df = df.set_index(['dataset', 'pipeline'])[['f1']].unstack().T.droplevel(0)
df = df.mean(axis=1).reset_index()
df.columns = ['pipeline', 'f1']
return df
def make_figure_7c():
untuned = pd.read_csv(RESULT_PATH.format('untuned_results'))
tuned = pd.read_csv(RESULT_PATH.format('tuned_results'))
untuned = _get_f1_score(untuned)
untuned['source'] = ['non-tuned'] * len(untuned)
tuned = _get_f1_score(tuned)
tuned['source'] = ['tuned'] * len(tuned)
df = pd.concat([untuned, tuned])
colors = ["#8d96a3", "#2e4057"]
palette = sns.color_palette(colors)
labels = _LABELS[1:]
orders = _ORDER[1:]
fig = plt.figure(figsize=(3.7, 3))
sns.barplot(data=df, x='pipeline', y='f1', hue='source', palette=palette, edgecolor='k',
order=orders)
ax = plt.gca()
for p in ax.patches:
ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
ha='center', va='center', fontsize=11, color='k', xytext=(0, 20),
textcoords='offset points', rotation=90)
plt.ylim([0.5, 0.8])
plt.xticks(range(len(labels)), labels)
plt.ylabel('F1 Score')
plt.xlabel('')
plt.title('Auto-Tuning F1 Score on NAB', size=13)
plt.legend(edgecolor='k')
plt.tight_layout()
_savefig(fig, 'figure7c', figdir=OUTPUT_PATH)
def make_figure_8a():
result = pd.read_csv(RESULT_PATH.format('results'))
result = _get_summary(result)
with open(os.path.join(DATA_PATH, 'semi-model.pkl'), 'rb') as f:
scores = pickle.load(f)
fig = plt.figure(figsize=(3.7, 3.5))
for j, score in enumerate(scores.values()):
plt.plot([i*2 for i in range(20)], score[:20], label=_LABELS[j], color=_COLORS[j])
plt.axhline(result.loc['lstm_autoencoder']['NAB']['f1'], ls='--', c='r')
plt.text(-1, 0.7, "best\nunsupervised", c='r', fontsize=8)
plt.ylabel('F1 Score')
plt.xlabel('Iteration')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.3), frameon=False, ncol=3)
plt.tight_layout()
_savefig(fig, 'figure8a', figdir=OUTPUT_PATH)
# ------------------------------------------------------------------------------
# Running benchmark
# ------------------------------------------------------------------------------
def run_benchmark():
workers = 1 # int or "dask"
# path of results
result_path = os.path.join(OUTPUT_PATH, 'benchmark.csv')
# path to save pipelines
pipeline_dir = os.path.join(OUTPUT_PATH, 'save_pipelines')
# path to save output on the fly
cache_dir = os.path.join(OUTPUT_PATH, 'cache')
# metrics
del METRICS['accuracy']
METRICS['confusion_matrix'] = contextual_confusion_matrix
metrics = {k: partial(fun, weighted=False) for k, fun in METRICS.items()}
results = benchmark(
pipelines=PIPELINES, datasets=DATASETS, metrics=metrics, output_path=result_path,
workers=workers, show_progress=True, pipeline_dir=pipeline_dir, cache_dir=cache_dir)
return results
def run_tune_benchmark():
workers = 1 # int or "dask"
# path of results
result_path = os.path.join(OUTPUT_PATH, 'tune_benchmark.csv')
# path to save pipelines
pipeline_dir = os.path.join(OUTPUT_PATH, 'tune_save_pipelines')
# path to save output on the fly
cache_dir = os.path.join(OUTPUT_PATH, 'tune_cache')
# metrics
del METRICS['accuracy']
METRICS['confusion_matrix'] = contextual_confusion_matrix
metrics = {k: partial(fun, weighted=False) for k, fun in METRICS.items()}
# pipelines
pipelines = ['lstm_dynamic_threshold', 'tadgan', 'lstm_autoencoder', 'dense_autoencoder']
# datasets
datasets = {
"artificialWithAnomaly": BENCHMARK_DATA["artificialWithAnomaly"],
"realAWSCloudwatch": BENCHMARK_DATA["realAWSCloudwatch"],
"realAdExchange": BENCHMARK_DATA["realAdExchange"],
"realTraffic": BENCHMARK_DATA["realTraffic"],
"realTweets": BENCHMARK_DATA["realTweets"]
}
results = tune_benchmark(datasets=datasets,
pipelines=pipelines, metrics=metrics, output_path=result_path, workers=workers,
show_progress=True, pipeline_dir=pipeline_dir, cache_dir=cache_dir)
return results
if __name__ == '__main__':
print("Running benchmark.. ")
run_benchmark()
print("Running tuning benchmark.. ")
run_tune_benchmark()