-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
496 lines (449 loc) · 16.5 KB
/
app.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# -*- coding: utf-8 -*-
import dash
import dash_auth
import json
import dash_core_components as dcc
import dash_daq as daq
import dash_bootstrap_components as dbc
import dash_html_components as html
from dash.dependencies import Input, Output
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
import numpy as np
import datetime
VALID_USERNAME_PASSWORD_PAIRS = {
'kraton': 'energy'
}
data = pd.read_excel('data/energy.xlsx', sheet_name='Data for Dashboard', header=0)
data_cto = pd.read_excel('data/energy.xlsx', sheet_name='Data for Dashboard CTO', header=0)
data = data.set_index(['Item', 'Units', 'Plant'])
data_cto = data_cto.set_index(['Item', 'Units', 'Plant'])
data.columns = pd.to_datetime(data.columns)
data_cto.columns = pd.to_datetime(data_cto.columns)
axis_options = data.index.get_level_values(0).unique()
metrics = ["CO2", "Production", "Energy Intensity", "CO2/production"]
app = dash.Dash(
__name__, meta_tags=[{"name": "viewport", "content": "width=device-width"}]
)
auth = dash_auth.BasicAuth(
app,
VALID_USERNAME_PASSWORD_PAIRS
)
server = app.server
initial_range = [
'2019-07-01', '2019-10-01'
]
def make_time_plot(clickData=None,
selectedData=None,
dropdown=None,
sample='Month',
plot_type='Scatter'):
if dropdown != None:
items = dropdown
else:
items = ['Electricity', 'Nat. Gas', 'Steam usage', 'Energy Intensity']
if selectedData != None:
line = pd.Series(pd.DataFrame.from_dict(selectedData['points'])['x'].unique())
elif clickData != None:
line = clickData['points'][0]['label']
else:
line = data.index.get_level_values(2).unique()
if type(line) == str:
line = pd.Series(line)
df = data.loc[(data.index.get_level_values(2).isin(line)) & # grab the right lines
(data.index.get_level_values(0).isin(items))]
units = df.index.get_level_values(1).unique() # detect number of different unit types; compute axes
fig = go.Figure()
fig.update_layout(xaxis_rangeslider_visible=True)
titles = []
for index2, unit in enumerate(units):
df2 = df.loc[(df.index.get_level_values(1) == unit)]
titles.append("{}".format(df2.index.get_level_values(1).unique()[0]))
for index in df2.index.get_level_values(0).unique():
if '/' not in unit: # sum Item values if no '/' otherwise take weight average
y = df2.loc[index].sum()
else:
y = df2.loc[index].mean()
fig.add_trace(
go.Scatter(
name="{} ({})".format(index,unit),
y=df2.loc[index].sum(),
x=df2.columns,
yaxis="y{}".format(index2+1)
),
)
################
# LAYOUT
################
r_domain = min(1.25 - (len(units)*.125), .95)
fig.update_layout(
xaxis=dict(
domain=[0, r_domain]
),
yaxis=dict(
title="{}".format(titles[0]),
),
)
positionl = .3
positionr = r_domain
for i in range(2,len(units)+1):
if i == 1:
positionl = 0.3
side = 'left'
anchor = 'x'
position = None
elif i == 2:
side = 'right'
positionl = positionl - .15
position = positionl
anchor = 'x'
else:
side= 'right'
positionr = positionr + .1
position = positionr
anchor = 'free'
fig.update_layout(
{'yaxis{}'.format(i): {'anchor': anchor,
'overlaying': 'y',
'position': position,
'side': side,
'title': {'text': '{}'.format(titles[i-1])}}
}
)
if len(items) == 1:
title="{}; Lines: {}".format(items[0], ", ".join(line.values))
else:
title="Lines: {}".format(", ".join(line.values))
fig.update_layout(dict(
title=title,
barmode="stack",
xaxis={'rangeselector': {'buttons': list([{'count': 1, 'label': '1M', 'step': 'month', 'stepmode': 'backward'},
{'count': 3, 'label': '3M', 'step': 'month', 'stepmode': 'backward'},
{'count': 6, 'label': '6M', 'step': 'month', 'stepmode': 'backward'},
{'count': 1, 'label': '1Y', 'step': 'year', 'stepmode': 'backward'},
{'step': 'all'}])}},
),
)
fig.update_layout({
"plot_bgcolor": "#F9F9F9",
"paper_bgcolor": "#F9F9F9",
})
fig['layout']['xaxis'].update(range=initial_range)
return fig
def make_bar_plot_delta(relayoutData=None,
dropdown=None,):
data_filt = data
start_str = end_str = start_obj = end_obj = None
if relayoutData != None:
if ("xaxis.autorange" not in relayoutData.keys()) &\
("autosize" not in relayoutData.keys()):
if 'xaxis.range' in relayoutData.keys():
start_str = relayoutData['xaxis.range'][0]
end_str = relayoutData['xaxis.range'][1]
elif "xaxis.range[0]" in relayoutData.keys():
start_str = relayoutData["xaxis.range[0]"]
end_str = relayoutData["xaxis.range[1]"]
start_str = start_str.split(" ")[0]
end_str = end_str.split(" ")[0]
start_obj = datetime.datetime.strptime(start_str, '%Y-%m-%d')
end_obj = datetime.datetime.strptime(end_str, '%Y-%m-%d')
else:
start_obj = datetime.datetime.strptime(initial_range[0], '%Y-%m-%d')
end_obj = datetime.datetime.strptime(initial_range[1], '%Y-%m-%d')
else:
start_obj = datetime.datetime.strptime(initial_range[0], '%Y-%m-%d')
end_obj = datetime.datetime.strptime(initial_range[1], '%Y-%m-%d')
cols = [col for col in data.columns if (col >= start_obj) &
(col <= end_obj)]
data_filt = data[cols]
date_title = "{}, {} - {}, {}".format(start_obj.month,
start_obj.year, end_obj.month, end_obj.year)
#calculate totals for CO2, Energy, Production
months = np.round((end_obj - start_obj).days/31)
timedelta = (end_obj - start_obj)
prev_period_start = start_obj - timedelta
cols = [col for col in data.columns if (col <= start_obj) &
(col >= prev_period_start)]
data_filt_prev = data[cols]
# item filter
if dropdown != None:
items = [dropdown[-1]]
else:
items = ['Electricity', 'Nat. Gas', 'Steam usage']
items = ['Energy Intensity']
df = data_filt.loc[(data_filt.index.get_level_values(0).isin(items))]
df_prev = data_filt_prev.loc[(data_filt_prev.\
index.get_level_values(0).isin(items))]
units = df.index.get_level_values(1).unique() # detect number of different unit types; compute axes
fig = go.Figure()
titles = ["{}".format(df.index.get_level_values(1).unique()[0])]
df2 = df.reorder_levels([0,2,1])
df2_prev = df_prev.reorder_levels([0,2,1])
for index in df2.index.get_level_values(0).unique():
if '/' not in units[0]:
y=df2.loc[index].sum(axis=1) - df2_prev.loc[index].sum(axis=1)
else:
y=df2.loc[index].mean(axis=1) - df2_prev.loc[index].mean(axis=1)
fig.add_trace(
go.Bar(
name='Change From Previous Time Period',
y=y,
x=df2.loc[index].index.get_level_values(0),
),
)
if '/' not in units[0]: # sum Item values if no '/' otherwise take weight average
y=df2.loc[index].sum(axis=1)
else:
y=df2.loc[index].mean(axis=1)
fig.add_trace(
go.Bar(
name='Aggregate Over Time Period',
y=y,
x=df2.loc[index].index.get_level_values(0),
),
)
################
# LAYOUT
################
r_domain = min(1.25 - (len(units)*.125), .95)
fig.update_layout(
xaxis=dict(
domain=[0, r_domain]
),
yaxis=dict(
title="{}".format(titles[0]),
),
)
positionl = .3
positionr = r_domain
for i in range(2,len(units)+1):
if i == 1:
positionl = 0.3
side = 'left'
anchor = 'x'
position = None
elif i == 2:
side = 'right'
positionl = positionl - .15
position = positionl
anchor = 'x'
else:
side= 'right'
positionr = positionr + .1
position = positionr
anchor = 'free'
if position > 1:
position = 1
fig.update_layout(
{'yaxis{}'.format(i): {'anchor': anchor,
'overlaying': 'y',
'position': position,
'side': side,
'title': {'text': '{}'.format(titles[i-1])}
}
}
)
if len(items) == 1:
title="{} By Site {}".format(items[0], date_title)
else:
title="By Site: {}".format(date_title)
fig.update_layout(dict(
title=title,
barmode="group",
),
)
fig.update_layout({
"plot_bgcolor": "#F9F9F9",
"paper_bgcolor": "#F9F9F9",
})
return fig
def calc_key_takeaways(relayoutData):
# time filter
data_filt = data
start_str = end_str = start_obj = end_obj = None
if relayoutData != None:
if ("xaxis.autorange" not in relayoutData.keys()) &\
("autosize" not in relayoutData.keys()):
if 'xaxis.range' in relayoutData.keys():
start_str = relayoutData['xaxis.range'][0]
end_str = relayoutData['xaxis.range'][1]
elif "xaxis.range[0]" in relayoutData.keys():
start_str = relayoutData["xaxis.range[0]"]
end_str = relayoutData["xaxis.range[1]"]
start_str = start_str.split(" ")[0]
end_str = end_str.split(" ")[0]
start_obj = datetime.datetime.strptime(start_str, '%Y-%m-%d')
end_obj = datetime.datetime.strptime(end_str, '%Y-%m-%d')
else:
start_obj = datetime.datetime.strptime(initial_range[0], '%Y-%m-%d')
end_obj = datetime.datetime.strptime(initial_range[1], '%Y-%m-%d')
else:
start_obj = datetime.datetime.strptime(initial_range[0], '%Y-%m-%d')
end_obj = datetime.datetime.strptime(initial_range[1], '%Y-%m-%d')
cols = [col for col in data.columns if (col >= start_obj) &
(col <= end_obj)]
data_filt = data[cols]
#calculate totals for CO2, Energy, Production
months = np.round((end_obj - start_obj).days/31)
timedelta = (end_obj - start_obj)
prev_period_start = start_obj - timedelta
cols = [col for col in data.columns if (col <= start_obj) &
(col >= prev_period_start)]
data_filt_prev = data[cols]
prevs = []
totals = []
for index, met in enumerate(metrics):
if ("/" not in met) and (" " not in met):
total = data_filt.loc[met].sum(axis=1).sum()
prev = data_filt_prev.loc[met].sum(axis=1).sum()
else:
total = data_filt.loc[met].mean(axis=1).mean()
prev = data_filt_prev.loc[met].mean(axis=1).mean()
unit = data_filt.loc[met].index.get_level_values(0).unique()[0]
percent = (total - prev) / prev * 100
prevs.append("{:.2f}% since last {:.0f}mo".format(percent, months))
totals.append("{:.2f} {}".format(total, unit))
df = pd.DataFrame([totals,prevs])
df.columns = metrics
return df
app.layout = html.Div([
html.H4(["Energy Dashboard"]),
html.Div([
html.Div(
[html.H6(id="co2_text"), html.P("CO2"),
html.P(id="co2_percent")],
id="co2",
className="mini_container",
),
html.Div(
[html.H6(id="production_text"), html.P("Production"),
html.P(id="production_percent")],
id="production",
className="mini_container",
),
html.Div(
[html.H6(id="energy_text"), html.P("Energy Intensity"),
html.P(id="energy_percent")],
id="energy",
className="mini_container",
),
html.Div(
[html.H6(id="ratio_text"), html.P("CO2/Production"),
html.P(id="ratio_percent")],
id="ratio",
className="mini_container",
),
],
id="info-container",
className="row container-display",
),
html.Div([
html.P("Metrics:"),
dcc.Dropdown(
options=[
{"label": str(county), "value": str(county)} for county in axis_options
],
value=['Electricity', 'Nat. Gas', 'Steam usage', 'Energy Intensity'],
multi=True,
id='dropdown'),
],
className='mini_container',
),
html.Div([
dcc.Graph(id='time_plot',
figure=make_time_plot()),
],
className="pretty_container"
),
html.Div([
html.Div([
html.Div([
dcc.Graph(id='bar_plot_delta',
figure=make_bar_plot_delta()),
],
className="pretty_container",
),
],
className='twelve columns',
),
],
className="row flex-display"
),
html.Div([
html.H6(["Background"]),
dcc.Markdown('''
* Corporate level KPIs:
* Energy intensity – unit GJ/mt of production
* GHG intensity – unit mtCO2/ton of production
* Reporting frequency: quarterly – display monthly actuals and cumulative year
* Reporting/Visualizing method: Dash/Flask + python
* Plant level KPIs:
* Plant energy intensity – unit GJ/mt of production
* Plant GHG intensity – unit GJ/mt of production
* Plant energy consumption total – GJ + local unit
* Plant GHG total – mtCO2
* Reporting frequency: monthly, cumulative year
* Reporting/Visualizing method: Dash/Flask + python
''')
],
className='mini_container'
),
],
className='container',
id="mainContainer",
style={"display": "flex", "flex-direction": "column"},
)
app.config.suppress_callback_exceptions = False
@app.callback(
[Output('co2_text', 'children'),
Output('co2_percent', 'children')],
[Input('time_plot', 'relayoutData')])
def update_met1(relayoutData=None):
df = calc_key_takeaways(relayoutData)
value1 = df[df.columns[0]].values[0]
value2 = df[df.columns[0]].values[1]
return value1, value2
@app.callback(
[Output('production_text', 'children'),
Output('production_percent', 'children')],
[Input('time_plot', 'relayoutData')])
def update_met1(relayoutData):
df = calc_key_takeaways(relayoutData)
value1 = df[df.columns[1]].values[0]
value2 = df[df.columns[1]].values[1]
return value1, value2
@app.callback(
[Output('energy_text', 'children'),
Output('energy_percent', 'children')],
[Input('time_plot', 'relayoutData')])
def update_met1(relayoutData):
df = calc_key_takeaways(relayoutData)
value1 = df[df.columns[2]].values[0]
value2 = df[df.columns[2]].values[1]
return value1, value2
@app.callback(
[Output('ratio_text', 'children'),
Output('ratio_percent', 'children')],
[Input('time_plot', 'relayoutData')])
def update_met1(relayoutData):
df = calc_key_takeaways(relayoutData)
value1 = df[df.columns[3]].values[0]
value2 = df[df.columns[3]].values[1]
return value1, value2
@app.callback(
Output('bar_plot_delta', 'figure'),
[Input('time_plot', 'relayoutData'),
Input('dropdown', 'value')])
def display_bar_delta(relayoutData, dropdown):
return make_bar_plot_delta(relayoutData, dropdown)
@app.callback(
Output('time_plot', 'figure'),
[Input('bar_plot_delta', 'clickData'),
Input('bar_plot_delta', 'selectedData'),
Input('dropdown', 'value')]
)
def display_time_plot(barClickData, barSelectedData, dropdown):
return make_time_plot(barClickData, barSelectedData, dropdown)
if __name__ == "__main__":
app.run_server(debug=True)