-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
556 lines (484 loc) · 19.1 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 16 16:09:33 2017
@author: saintlyvi
"""
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
from dash.dependencies import Input, Output#, State
import plotly.graph_objs as go
import plotly.offline as offline
offline.init_notebook_mode(connected=True)
import pandas as pd
import numpy as np
import os
import base64
import features
from support import appProfiles
# Load images
erc_logo = os.path.join('img', 'erc_logo.jpg')
erc_encoded = base64.b64encode(open(erc_logo, 'rb').read())
sanedi_logo = os.path.join('img', 'sanedi_logo.jpg')
sanedi_encoded = base64.b64encode(open(sanedi_logo, 'rb').read())
# Get mapbox token
mapbox_access_token = 'pk.eyJ1Ijoic2FpbnRseXZpIiwiYSI6ImNqZHZpNXkzcjFwejkyeHBkNnp3NTkzYnQifQ.Rj_C-fOaZXZTVhTlliofMA'
# Get load profile data from disk
print('...loading load profile data...')
profiles = appProfiles(1994,2014)
# Load datasets
print('...loading socio demographic data...')
ids = features.loadID()
#a little bit of data wrangling
loc_summary = pd.pivot_table(ids, values = ['AnswerID'], index = ['Year','LocName','Lat','Long','Municipality','Province'],aggfunc = np.count_nonzero)
loc_summary.reset_index(inplace=True)
loc_summary.rename(columns={'AnswerID':'# households'}, inplace=True)
#load socio-demographic feature frame
appliances = ['fridge freezer','geyser','heater','hotplate','iron','kettle','microwave','3 plate', '4 plate','tv','washing machine']
sd = features.socio_demographics(appliances)
print('Your app is starting now. Visit 127.0.0.1:8050 in your browser')
app = dash.Dash()
app.config['suppress_callback_exceptions']=True
external_css = ["https://fonts.googleapis.com/css?family=Overpass:300,300i",
"https://cdn.rawgit.com/plotly/dash-app-stylesheets/dab6f937fd5548cebf4c6dc7e93a10ac438f5efb/dash-technical-charting.css"]
for css in external_css:
app.css.append_css({"external_url": css})
app.layout = html.Div([
###############################
html.Div([
html.Div([
html.Img(src='data:image/png;base64,{}'.format(erc_encoded.decode()),
style={'width': '100%', 'paddingLeft':'5%', 'marginTop':'20%' })
],
className='three columns',
style={'margin_top':'20'}
),
html.Div([
html.H2('South African Domestic Load Research',
style={'textAlign': 'center'}
),
html.H1('Data Explorer',
style={'textAlign':'center'}
)
],
className='six columns'
),
html.Div([
html.Img(src='data:image/png;base64,{}'.format(sanedi_encoded.decode()),
style={'width':'100%','margin-left':'-5%','marginTop':'10%' })
],
className='three columns'
),
],
className='row',
style={'background':'white',
'margin-bottom':'40'}
),
################~Survey Locations~###############
html.Div([
html.Div([
html.H3('Survey Locations'
),
html.Div([
dcc.Graph(
animate=True,
style={'height': 450},
id='map'
),
],
className='columns',
style={'margin-left':'0'}
),
html.Div([
dcc.Slider(
id = 'input-years',
marks={i: i for i in range(1994, 2015, 2)},
min=1994,
max=2014,
step=1,
included=False,
value= 2010,#[1994, 2014],
updatemode='drag',
dots = True
)
],
className='eleven columns',
style={'margin-left':'25',
'margin-right':'15'}
),
],
className='columns',
style={'margin-bottom':'10',
'margin-left':'0',
'width':'50%',
'float':'left'}
),
#######~~~~~~~~Specify Socio-demographic Indicators~~~~~~~~~########
html.Div([
html.H5('Specify Socio-demographic Indicators'
),
#~~~~---Select number of years electrified---~~~~
html.Div([
html.P('Select number of years electrified'),
html.Div([
dcc.RangeSlider(
id = 'input-electrified',
marks= list(range(0, 15, 1)) + ['+15'],
min=0,
max=15,
included=True,
value= [0,15],
updatemode='drag'
)
],
style={'margin-bottom':'50',
'margin-left':'20'}
)
],
className='columns',
style={'margin-top':'25',
'margin-left':'0'}
),
#~~~~---Select household income range---~~~~
html.Div([
html.P('Select household income range'),
html.Div([
dcc.RangeSlider(
id = 'input-income',
marks={i: 'R {}k'.format(i/1000) for i in range(0, 26000, 2500)},
min=0,
max=25000,
included=True,
value= [0,25000],
updatemode='drag'
)
],
style={'margin-bottom':'50',
'margin-left':'20'}
)
],
className='columns',
style={'margin-left':'0'}
),
#~~~~---Select household appliances---~~~~
html.Div([
html.P('Select household appliances'),
html.Div([
dcc.Dropdown(
id = 'input-appliances',
options=[{'label': 'Fridge-Freezer', 'value': 'fridge_freezer'},
{'label': 'Geyser', 'value': 'geyser'},
{'label': 'Heater', 'value': 'heater'},
{'label': 'Hotplate', 'value': 'hotplate'},
{'label': 'Iron', 'value': 'iron'},
{'label': 'Kettle', 'value': 'kettle'},
{'label': 'Microwave', 'value': 'microwave'},
{'label': '3-plate Stove', 'value': '3_plate'},
{'label': '4-plate Stove', 'value': '4_plate'},
{'label': 'TV', 'value': 'tv'},
{'label': 'Washing machine', 'value': 'washing_machine'},
],
placeholder="Select appliances",
multi=True,
value = []
)
],
style={'margin-bottom':'50',
'margin-left':'20'}
),
],
className='columns',
style={'margin-left':'0'}
),
########~~~~~~~~section end~~~~~~~~########
],
className='columns',
style={'margin-top':'75',
'margin-bottom':'10',
'margin-right':'15',
'margin-left':'25',
'width':'45%',
'float':'right'}
),
################~section end~################
],
className='row',
),
html.Div(id='sd-features', style={'display': 'none'}),
html.Div(id='selected-ids', style={'display': 'none'}),
html.Div(id='map-select', style={'display': 'none'}),
#Uncomment to test input variables
# html.Div([
# html.Pre(id='test'),
# ], className='three columns'),
html.Hr(),
#################~View Load Profiles~###############
html.Div([
html.H3('View Load Profiles'),
dcc.RadioItems(
id = 'input-daytype',
options=[
{'label': 'Weekday', 'value': 'Weekday'},
{'label': 'Saturday', 'value': 'Saturday'},
{'label': 'Sunday', 'value': 'Sunday'}
],
value='Weekday'
),
dcc.Graph(
id='graph-profiles'
),
]),
html.Hr(),
###############~Explore Profile Meta-data~###############
html.Div([
html.H3('Explore Profile Meta-data'),
########~~~~~~~~Discover Survey Questions~~~~~~~~########
html.Div([
html.H5('Discover Survey Questions'
),
html.Div([
dcc.Input(
id='input-search-word',
placeholder='search term',
type='text',
value=''
)
],
className='container',
style={'margin': '10'}
),
dt.DataTable(
id='output-search-word-questions',
rows=[{}], # initialise the rows
row_selectable=True,
columns = ['Question','Survey','Datatype'],
filterable=False,
sortable=True,
selected_row_indices=[]
)
],
className='columns',
style={'margin-bottom':'10',
'margin-left':'0',
'width':'50%',
'float':'left'}
),
########~~~~~~~~Location Output Summary~~~~~~~~########
html.Div([
html.H5('Location Output Summary'),
html.Div([
dt.DataTable(
id='output-location-summary',
rows=[{}], # initialise the rows
row_selectable=False,
columns = ['Year','Province','Municipality','LocName','# households'],
filterable=False,
sortable=True,
column_widths=100,
min_height = 450,
resizable=True,
selected_row_indices=[]),
html.P('"# households" is the number of households for which socio-demographic survey data is available',
style={'font-style': 'italic'}
)
],
style={'margin-top':'57'}
)
],
className='columns',
style={'margin-bottom':'10',
'width':'45%',
'float':'right'}
),
#######~~~~~~~~section end~~~~~~~~########
],
className='row'
),
],
##############################
#Set the style for the overall dashboard
style={
'width': '100%',
'max-width': '1200',
'margin-left': 'auto',
'margin-right': 'auto',
'font-family': 'overpass',
'background-color': '#F3F3F3',
'padding': '40',
'padding-top': '20',
'padding-bottom': '20',
},
)
#Define outputs
#@app.callback(
# Output('test','children'),
# [Input('input-appliances','value')])
#def selected_ids(input):
#
# return json.dumps(input, indent=2)
@app.callback(
Output('sd-features','children'),
[Input('input-electrified', 'value'),
Input('input-electrified', 'max'),
Input('input-income', 'value'),
Input('input-income', 'max'),
Input('input-appliances', 'value')
])
def socio_demographics(electrified, electrified_max, income, income_max, appliances):
if electrified[1] == electrified_max:
electrified[1] = sd.years_electrified.max()
if income[1] == income_max:
income[1] = sd.monthly_income.max()
sd1 = sd[sd.monthly_income.isin(range(int(income[0]),int(income[1])+1))]
sd2 = sd1[sd1.years_electrified.isin(range(int(electrified[0]),int(electrified[1])+1))]
try:
sd_features = sd2.dropna(subset=appliances)
except:
sd_features = sd
return sd_features.to_json(date_format='iso', orient='split')
@app.callback(
Output('selected-ids','children'),
[Input('sd-features','children'),
Input('input-years','value')
])
def selected_ids(sd_features, input_years):
sd_df = pd.read_json(sd_features, orient='split')
id_select = sd_df.merge(ids, on='AnswerID', how='inner')
yrs = [input_years]
# yrs = list(range(input_years[0],input_years[1]+1))
output = id_select[id_select.Year.isin(yrs)].reset_index(drop=True)
return output.to_json(date_format='iso', orient='split')
@app.callback(
Output('map','figure'),
[Input('selected-ids','children')
])
def update_map(selected_ids):
ids_df = pd.read_json(selected_ids, orient='split')
georef = pd.pivot_table(ids_df, values = ['AnswerID'], index = ['Year','LocName','Lat','Long','Municipality','Province'],aggfunc = np.count_nonzero)
georef.reset_index(inplace=True)
georef.rename(columns={'AnswerID':'# households'}, inplace=True)
traces = []
for y in range(georef.Year.min(), georef.Year.max()+1):
lat = georef.loc[(georef.Year==y), 'Lat']
lon = georef.loc[(georef.Year==y), 'Long']
text = georef.loc[(georef.Year==y), '# households'].astype(str) + ' household surveys</br>'+ georef.loc[(georef.Year==y), 'LocName'] + ', ' + georef.loc[(georef.Year==y), 'Municipality']
marker_size = georef.loc[georef.Year==y,'# households']**(1/2.5)*2.7
marker_size.replace([0,1,2,3,4, 5], 6, inplace=True)
trace=go.Scattermapbox(
name=y,
lat=lat,
lon=lon,
mode='markers',
marker=go.Marker(
size=marker_size
),
text=text,
)
traces.append(trace)
figure=go.Figure(
data=go.Data(traces),
layout = go.Layout(
autosize=True,
hovermode='closest',
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=dict(
lat=-29.1,
lon=25
),
pitch=0,
zoom=4.32,
style='light'
),
margin = go.Margin(
l = 10,
r = 10,
t = 20,
b = 30
),
showlegend=False
)
)
return figure
@app.callback(
Output('map-select','children'),
[Input('map','selectedData'),
Input('selected-ids','children')
])
def map_data(selected_data, selected_ids):
ids_df = pd.read_json(selected_ids, orient='split')
try:
geos = pd.DataFrame(selected_data['points'])
geos['LocName'] = geos['text'].apply(lambda x: x.split(',')[0].split('>')[1])
geos.drop_duplicates('LocName',inplace=True)
output = ids_df[ids_df.LocName.isin(geos.LocName)].reset_index(drop=True)
except:
output = ids_df
return output.to_json(date_format='iso', orient='split')
@app.callback(
Output('graph-profiles','figure'),
[Input('input-daytype','value'),
Input('map-select','children'),
])
def graph_profiles(day_type, map_select):
map_df = pd.read_json(map_select, orient='split')
id_select = map_df[map_df.AnswerID!=0]
g = profiles[profiles.ProfileID_i.isin(id_select.ProfileID)]
gg = g.groupby(['daytype','season','hour'])['kw_mean'].describe().reset_index()
dt_mean = gg[(gg.daytype==day_type)]
traces = []
for s in ['high','low']:
trace = go.Scatter(
showlegend=True,
opacity=1,
x=dt_mean.loc[dt_mean['season']==s, 'hour'],
y=dt_mean.loc[dt_mean['season']==s, 'mean'],
mode='lines',
name=s+ ' season',
line=dict(
#color='red',
width=2.5),
hoverinfo = 'name+y+x'
)
traces.append(trace)
layout = go.Layout(showlegend=True,
title= day_type + ' Average Daily Demand for Selected Locations',
margin = dict(t=150,r=150,b=50,l=150),
height = 450,
yaxis = dict(
title = 'mean hourly demand (kW)',
ticksuffix=' kW'),
xaxis = dict(
title = 'time of day',
ticktext = dt_mean['hour'].unique(),
tickvals = dt_mean['hour'].unique(),
showgrid = True)
)
fig = go.Figure(data=traces, layout=layout)
return fig
@app.callback(
Output('output-location-summary','rows'),
[Input('map-select','children'),
])
def location_summary(map_select):
map_df = pd.read_json(map_select, orient='split')
output = pd.pivot_table(map_df, values = ['AnswerID'], index = ['Year','LocName','Lat','Long','Municipality','Province'],aggfunc = np.count_nonzero)
output.reset_index(inplace=True)
output.rename(columns={'AnswerID':'# households'}, inplace=True)
return output.to_dict('records')
@app.callback(
Output('output-search-word-questions','rows'),
[Input('input-search-word','value')
])
def update_questions(search_word):
df = features.searchQuestions(search_word)[['Question','QuestionaireID','Datatype']]
dff = df.loc[df['QuestionaireID'].isin([3,6])]
dff.loc[:,'Survey'] = dff.QuestionaireID.map({3:'2000-2014',6:'1994-1999'})
dff.drop(columns='QuestionaireID', inplace=True)
return dff.to_dict('records')
# Run app from script. Go to 127.0.0.1:8050 to view
if __name__ == '__main__':
app.run_server(debug=True)