-
Notifications
You must be signed in to change notification settings - Fork 0
/
anomaly_detection_dash.py
162 lines (132 loc) · 4.19 KB
/
anomaly_detection_dash.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 18:03:19 2020
@author: nutchapoldendumrongsup
"""
import dash
import dash_table
import pandas as pd
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import numpy as np
from function import *
df = pd.read_csv('../data/nab/realKnownCause/realKnownCause/ambient_temperature_system_failure.csv')
data=preprocess_data(df)
a_isf=isolation_forest(data,df)
a_svm=one_class_svm(data,df)
app = dash.Dash(__name__)
app.layout =dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.iloc[:1000].to_dict('records'),
style_table={
'maxHeight': '300px',
'overflowY': 'scroll'
},
fixed_rows={ 'headers': True, 'data': 0 }
)
app.layout = html.Div(children=[
html.H1(children='Predictive Maintenance'),
dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'),
style_table={
'maxHeight': '300px',
'overflowY': 'scroll'
},
fixed_rows={ 'headers': True, 'data': 0 }
),
dcc.Dropdown(
id='dropdown-1',
options=[
{'label': 'Isolation Forest', 'value': 'ISF'},
{'label': 'One-Class SVM', 'value': 'SVM'},
],
value='ISF'
),
dcc.Graph(id='graph-with-drowdown-1'),
dcc.Dropdown(
id='dropdown-2',
options=[
{'label': 'Isolation Forest', 'value': 'ISF'},
{'label': 'One-Class SVM', 'value': 'SVM'},
],
value='ISF'
),
dcc.Graph(id='graph-with-drowdown-2')
])
@app.callback(
Output('graph-with-drowdown-1', 'figure'),
[Input('dropdown-1', 'value')])
def update_figure_1(model):
if model=='ISF':
a=a_isf
elif model=='SVM':
a=a_svm
return {
'data': [
dict(
x=df['timestamp'],
y=df['value'],
name='measurement'
),
dict(
x=a['timestamp'],
y=a['value'],
mode='markers',
opacity=0.5,
marker={
'size': 8,
'line': {'width': 0.5, 'color': 'red'}
},
name='anomaly'
)
],
'layout': dict(
xaxis={'title': 'time'},
yaxis={'title': 'value'},
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1},
hovermode='closest'
)
}
@app.callback(
Output('graph-with-drowdown-2', 'figure'),
[Input('dropdown-2', 'value')])
def update_figure_2(model):
if model=='ISF':
a=a_isf
elif model=='SVM':
a=a_svm
return {
'data': [
dict(
x=df['timestamp'],
y=df['value'],
name='measurement'
),
dict(
x=a['timestamp'],
y=a['value'],
mode='markers',
opacity=0.5,
marker={
'size': 8,
'line': {'width': 0.5, 'color': 'red'}
},
name='anomaly'
)
],
'layout': dict(
xaxis={'title': 'time'},
yaxis={'title': 'value'},
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1},
hovermode='closest'
)
}
if __name__ == '__main__':
app.run_server(debug=True)