-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.py
More file actions
315 lines (262 loc) · 10.9 KB
/
Copy pathfrontend.py
File metadata and controls
315 lines (262 loc) · 10.9 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
304
305
306
307
308
309
310
311
312
313
314
315
import os
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from simulator import Simulator
from modeling import Modeling
import pandas as pd
import plotly.graph_objects as go
settings = {
'inputs': [
'u1',
'u2'
],
'unknown': [
'y1',
'y3',
'y4'
],
'output': 'y2',
'time_var': 'dt',
'feat_lags': {'y2': [6], 'u1': [6, 7, 8], 'u2': [6, 7, 8]},
'features': [
'y2l6',
'u1',
'u2',
'u1l6',
'u2l6'
]
}
time_var = settings['time_var']
output = settings['output']
output_model = output+'_prediction'
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
colors = {
'grey': '#797878',
'yellow': '#FFDB00',
'light-green': '#90ee90',
'light-yellow': '#ffffed',
'light-red': '#ffcccb'
}
path_project = os.path.abspath(
os.path.join(os.path.realpath(__file__), '..')
)
path_stream_data = os.path.join(path_project, 'database', 'raw_data.csv')
path_database = os.path.join(path_project, 'database', 'database.csv')
path_model = os.path.join(path_project, 'models', 'lr.pkl')
sim = Simulator(path_stream_data, path_database)
model = Modeling(settings)
model.load(path_model)
app.layout = html.Div(children=[
# Storage in application for database
dcc.Store(id='store-database'),
dcc.Store(id='store-clock'),
# Div for controls
html.Div([
html.Div([
html.H6('Digital Twin'),
], className='four columns', style={'padding-left': '10px'}),
html.Div([
html.Button('Start', id='button-start-simulation',
n_clicks_timestamp=0,
style={'backgroundColor': colors['light-green'], 'borderColor': 'yellow'}),
html.Button('Pause', id='button-pause-simulation',
n_clicks_timestamp=0, style={'margin-left': '10px', 'backgroundColor': colors['light-yellow'],
'borderColor': 'yellow'}),
html.Button('Reset', id='button-reset-simulation',
n_clicks_timestamp=0, style={'margin-left': '10px', 'backgroundColor': colors['light-red'],
'borderColor': 'yellow'}),
], className='four columns', style={'margin-top': '5px'})
],
className='row',
style={'backgroundColor': colors['yellow'],
'padding': '10px'}
),
# Div for row 2 (spaces 1 and 2)
html.Div([
# Div for space 1
html.Div([
html.H3('Sensors'),
dcc.Graph(id='graph-sensors')
], className="six columns"),
# Div for space 2
html.Div([
html.H3('Output'),
dcc.Graph(id='graph-interest')
], className="six columns"),
], className="row"),
# Div for row 3 (spaces 3 and 4)
html.Div([
# Div for space 3
html.Div([
html.H3('Model training'),
html.Div([
dcc.Input(
placeholder='Parameter 1...', id='input-1',
type='text', value=''
),
dcc.Input(
placeholder='Parameter 2...', id='input-2',
type='text', value='', style={'margin-left': '10px'}
),
], className='row'),
html.Div([
html.Button('Train model (base struct)', id='button-train-model', n_clicks_timestamp=0,
style={'margin-top': '10px', 'background-color': colors['yellow'],
'margin-right': '10px'}),
html.Button('Train model (change struct)', id='button-train-model-struct', n_clicks_timestamp=0,
style={'margin-top': '10px', 'background-color': colors['yellow'],
'margin-bottom': '10px'}),
], className='row'),
html.H6('Modeling messages:'),
html.P(id='modeling-messages')
], className="six columns"),
# Div for space 4
html.Div([
html.H3('Monitoring'),
dcc.Graph(id='graph-monitoring')
], className="six columns"),
], className="row"),
dcc.Interval(
id='interval-component',
interval=60*60*1000, # in milliseconds
n_intervals=0
)
])
def pivot_sensors(df):
"""
Returns the pivoted data of sensors
"""
df_sensors = pd.pivot_table(df, index=[time_var], columns=['sensor'], values='value').reset_index()
df_sensors[time_var] = pd.to_datetime(df_sensors[time_var])
return df_sensors
@app.callback([Output('graph-sensors', 'figure'),
Output('graph-interest', 'figure'),
Output('graph-monitoring', 'figure')],
[Input('store-database', 'data')])
def update_graphs(database):
if database is not None:
df_database = pd.DataFrame.from_records(database)
df_sensors = pivot_sensors(df_database)
print(len(df_sensors))
fig_sensors = go.Figure()
for input in settings['inputs']:
fig_sensors.add_trace(go.Scatter(x=df_sensors[time_var], y=df_sensors[input],
mode='lines',
name=input))
for input in settings['unknown']:
fig_sensors.add_trace(go.Scatter(x=df_sensors[time_var], y=df_sensors[input],
mode='lines',
name=input))
# Run model here to present output (if it exists)
fig_interest = go.Figure()
fig_interest.add_trace(go.Scatter(x=df_sensors[time_var], y=df_sensors[output],
mode='lines',
name=output))
df_preds = model.run_model(df_sensors)
fig_interest.add_trace(go.Scatter(x=df_preds[time_var], y=df_preds[output_model],
mode='lines',
name=output_model))
# Evaluate measures here if, at least show the upper and lower limits
df_performance = df_sensors.copy()
performance_measures = model.monitor_measures
performance_lines = []
fig_performance = go.Figure()
for measure in performance_measures:
min_col = 'Min. {}'.format(measure)
max_col = 'Max. {}'.format(measure)
df_performance[min_col] = performance_measures[measure]['min']
df_performance[max_col] = performance_measures[measure]['max']
performance_lines.append(min_col)
performance_lines.append(max_col)
color = performance_measures[measure]['color']
fig_performance.add_trace(go.Scatter(x=df_performance[time_var], y=df_performance[min_col],
mode='lines',
name=min_col,
line=dict(dash='dot', color=color)))
fig_performance.add_trace(go.Scatter(x=df_performance[time_var], y=df_performance[max_col],
mode='lines',
name=max_col,
line=dict(dash='dot', color=color)))
df_performance_values = model.monitor_model(df_sensors, df_preds)
if df_performance_values is not None:
for measure in performance_measures:
color = performance_measures[measure]['color']
fig_performance.add_trace(go.Scatter(x=df_performance_values[time_var], y=df_performance_values[measure],
mode='lines',
name=measure,
line=dict(color=color)))
if len(df_sensors) > 200:
fig_sensors.layout.xaxis.range = [df_sensors.iloc[-200][time_var], df_sensors.iloc[-1][time_var]]
fig_interest.layout.xaxis.range = [df_sensors.iloc[-200][time_var], df_sensors.iloc[-1][time_var]]
fig_performance.layout.xaxis.range = [df_sensors.iloc[-200][time_var], df_sensors.iloc[-1][time_var]]
return fig_sensors, fig_interest, fig_performance
else:
return {}, {}, {}
@app.callback(Output('interval-component', 'interval'),
[Input('button-start-simulation', 'n_clicks_timestamp'),
Input('button-pause-simulation', 'n_clicks_timestamp'),
Input('button-reset-simulation', 'n_clicks_timestamp')])
def start_simulation(n_start, n_pause, n_reset):
if int(n_start) > int(n_pause) and int(n_start) > int(n_reset):
return 5 * 1000
elif int(n_pause) > int(n_start) and int(n_pause) > int(n_reset):
return 60 * 60 * 1000
elif int(n_reset) > int(n_start) and int(n_reset) > int(n_pause):
sim.start()
model.reset()
model.load(path_model)
return 5 * 1000
else:
return 60 * 60 * 1000
@app.callback([Output('store-database', 'data'),
Output('store-clock', 'data')],
[Input('interval-component', 'n_intervals')],
[State('store-clock', 'data')])
def update_database(n, clock):
if n != 0:
sim.step(60*10)
else:
sim.start()
database = sim.read_db()
if clock is None:
clock = 0
print(clock)
else:
clock += 1
print('clock {}'.format(clock))
return database.to_dict('records'), clock
@app.callback(Output('modeling-messages', 'children'),
[Input('button-train-model', 'n_clicks_timestamp'),
Input('button-train-model-struct', 'n_clicks_timestamp')],
[State('store-database', 'data'),
State('input-1', 'value'),
State('input-2', 'value')])
def start_simulation(n_train, n_train_struct, database, param1, param2):
params = {
'param1': param1,
'param2': param2
}
if int(n_train) > int(n_train_struct):
df_database = pd.DataFrame.from_records(database)
df_sensors = pivot_sensors(df_database)
train_log = model.train_model(df_sensors, params)
return train_log
elif int(n_train_struct) > int(n_train):
df_database = pd.DataFrame.from_records(database)
df_sensors = pivot_sensors(df_database)
settings['features'] = [
'y2l6',
'u1',
'u1l6'
]
model.settings = settings
train_log = model.train_model(df_sensors, params)
return train_log
else:
train_log = 'No model currently trained.'
return train_log
if __name__ == '__main__':
app.run_server(debug=True)