-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
128 lines (100 loc) · 4.62 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
import pandas as pd
import json
from bokeh.models import ( ColumnDataSource, Circle, Legend)
from bokeh.plotting import figure, show,curdoc
from bokeh.layouts import widgetbox, column
from bokeh.models.widgets import Select
# load prediction result
pred_df = pd.read_csv('model/prediction.csv')
pred_df['Year-Month'] = pd.to_datetime(pred_df['Year-Month'])
# load steo
steo = pd.read_csv('clean-data/steo.csv')
steo['Year-Month'] = pd.to_datetime(steo['Year-Month'])
# group by Regions and merge with prediction data
region_df = pred_df.groupby(['Year-Month','Regions'],as_index=False).sum()
region_df = region_df.merge(steo, on = ['Year-Month','Regions'],how='right')
# dictionary to map state name to state abbreviation
with open('us_state_abbrev.json') as f:
us_state_abbrev = json.loads(f.read())
state_dict = us_state_abbrev
state_dict.pop('Hawaii',None)
state_names = [*state_dict]
# dictionary to map sector names to their abbreviations
sector_dict = {'Residential':'RES',
'Industrial':'IND', 'Commercial':'COM', 'Total':'ALL_no_OTH'}
def get_source(sector_name,state_name):
'''
return two bokeh columnar data sources for that sector, state and region
'''
sector = sector_dict[sector_name]
#column to plot
cols = ['Sale_'+sector,'Pred_'+sector, 'STEO_Sale_'+sector]
# pick a state abbreviation
state = state_dict[state_name]
# select the data dfs to plot
data1 = pred_df[pred_df['State']==state]
data1 = data1[cols[:2]+['Year-Month','Regions']]
data1.columns = ['col1','col2','Year-Month','Regions']
source1 = ColumnDataSource(data1)
region = data1['Regions'].unique()[0]
data2 = region_df[region_df['Regions']==region]
data2 = data2[cols+['Year-Month']]
data2.columns = ['col1','col2','col3','Year-Month']
source2 = ColumnDataSource(data2)
return ColumnDataSource(data1), ColumnDataSource(data2), region
# bokeh plot
# default sector and state
sector_name = 'Residential'
state_name = 'Alabama'
source1, source2, region = get_source(sector_name,state_name)
colors = ['#2c7fb8','#fec44f','#c51b8a']
#plot state
p1 = figure(plot_width=700, plot_height=250, x_axis_type="datetime",tools="pan,box_zoom,reset",toolbar_location='above')
r0 = p1.line(source=source1, x='Year-Month', y='col1', color=colors[0],line_width=2)
r1 = p1.line(source=source1, x='Year-Month', y='col2', color=colors[1],line_width=2)
p1.title.text = sector_name +' electricity consumption for state: '+ state_name
p1.yaxis.axis_label = "(million kWh)"
legend = Legend(items=[
("Actual" , [r0]),
("Predicted" , [r1]),], location="center")
p1.add_layout(legend,'right')
#plot region
p2 = figure(plot_width=700, plot_height=250, x_axis_type="datetime",x_range=p1.x_range,tools="pan,box_zoom,reset",toolbar_location='above')
r2 = p2.line(source=source2, x='Year-Month', y='col1', color=colors[0],line_width=2)
r3 = p2.line(source=source2, x='Year-Month', y='col2', color=colors[1],line_width=2)
r4 =p2.line(source=source2, x='Year-Month', y='col3', color=colors[2],line_width=2,line_dash=[4, 4])
p2.title.text = sector_name +' electricity consumption for Region: '+ region
p2.yaxis.axis_label = "(million kWh)"
legend = Legend(items=[
("Actual" , [r2]),
("Predicted" , [r3]),
("STEO Prediction",[r4])], location="center")
p2.add_layout(legend,'right')
def callback_sector(attr,old,new):
global state_name, sector_name, region
sector_name = new
temp1, temp2, region = get_source(sector_name,state_name)
# change data
source1.data = temp1.data
source2.data = temp2.data
# change title
p1.title.text = sector_name +' electricity consumption for state: '+ state_name
p2.title.text = sector_name +' electricity consumption for Region: '+ region
def callback_state(attr,old,new):
global state_name, sector_name, region
state_name = new
temp1, temp2, region = get_source(sector_name,state_name)
# change data
source1.data = temp1.data
source2.data = temp2.data
# change title
p1.title.text = sector_name +' electricity consumption for state: '+ state_name
p2.title.text = sector_name +' electricity consumption for Region: '+ region
# make drop down menu
select_sector = Select(title='Sector:', value=[*sector_dict][0], options=[*sector_dict])
select_sector.on_change('value', callback_sector)
select_state = Select(title='State:', value=[*state_dict][0], options=[*state_dict])
select_state.on_change('value', callback_state)
layout = column(widgetbox(select_sector,select_state),p1,p2)
# put the button and plot in a layout and add to the document
curdoc().add_root(layout)