-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpages.py
251 lines (180 loc) · 8.12 KB
/
pages.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
import streamlit as st
import joblib
from visualizations import *
import datetime
from datetime import date
import holidays
from weather_api import get_weather_pred, weather_info
import numpy as np
def home_page():
st.title("⚡ Energytics")
st.subheader("Analytics and Insights for everything energy-related")
st.image("media/energy.jpg", width=1000, use_column_width=True)
st.markdown(
"""
As it stands, fossil fuels are significantly cheaper at producing energy than renewable technologies.
However, this is not a sustainable solution, and we are still years away from reaching parity in efficiency between renewable sources and conventional fossil fuel sources.
Moreover, businesses would not only profit from minimizing their energy consumption but also help them achieve lower CO2 emissions.
Our goal is to identify, through machine learning, which features contribute the most to energy consumption so that they can be tackled to minimize the energy consumption for businesses.
"""
)
def energy_prod():
st.subheader("Energy Production cost by State and Energy Source")
st.caption(
"We are using energy cost and production datasets in order to help energy companies discern what energy source would be most benficial for them to invest in."
)
chosen_year = st.slider("Choose a year", 2010, 2020, 2010)
map_plt = plot_map(chosen_year)
st.plotly_chart(map_plt, use_container_width=True)
st.markdown(
"""
Form the plot, we can see that there are notably 3 main states in the US that are generating the most revenue year on year, with Texas ranking first, followed by California then Washington.
With Texas being the 2nd largest state in the US in terms of total land area, the state is able to yield much crude oil and natural gas since the land is abundant of the fields. The same reasoning resonates with California being the 2nd highest revenue generating state.
Looking closer to the map, we can also see that states in the middle of the US focuses more on generating revenue from wind energy. This is evident because the annual average wind speed is highest and consistent at the middle of the US, reference [Annual average wind speed of the US at 80 m 5. | Download Scientific Diagram](https://www.researchgate.net/figure/Annual-average-wind-speed-of-the-US-at-80-m-5_fig1_325422763)
---
"""
)
col1, col2 = st.columns(2)
chosen_source = col1.selectbox(
"Choose resource", list(energy_df["source"].unique())
)
chosen_year = col2.slider("Choose a year", 2010, 2020, 2010, key="map")
st.plotly_chart(plot_src_map(chosen_year, chosen_source), use_container_width=True)
st.markdown(
"""
---
"""
)
st.subheader("Getting a closer look at each state")
col1, col2 = st.columns(2)
chosen_state = col1.selectbox("Choose state", list(energy_df["state"].unique()))
chosen_year = col2.slider("Choose a year", 2010, 2020, 2010, key="piechart")
scttr_plt, pie_plt = plot_scatter_pie(chosen_state, chosen_year)
st.plotly_chart(pie_plt, use_container_width=True)
st.plotly_chart(scttr_plt, use_container_width=True)
st.markdown(
"""
---
### Data source
1. [U.S. Energy Information Administration](https://www.eia.gov/)
1. [International Renewable Energy Agency (IRENA)](https://www.irena.org/)
"""
)
def energy_cons():
country_list = pd.read_pickle("data/country_list.pkl")
prim_use = pd.read_pickle("data/primary_use.pkl")
us_holidays = holidays.US()
lgb1 = joblib.load("models/lgb1.pkl")
lgb2 = joblib.load("models/lgb2.pkl")
le = joblib.load("models/encoder.pkl")
models = [lgb1, lgb2]
meter_types = {"electricity": 0, "chilledwater": 1, "steam": 2, "hotwater": 3}
st.markdown(
"""
## Predicting your Building's energy consumption
We need some information from you to make an accurate prediction!
First provide us a date you want to predict energy for, then provide us your location
---
"""
)
if "load_state" not in st.session_state:
st.session_state.load_state = False
with st.form("location"):
col1, col2 = st.columns(2)
d = col1.date_input(
"Choose any date 30 days from today",
date.today(),
max_value=date.today() + datetime.timedelta(days=30),
)
hour = col2.number_input("Choose an hour too", min_value=0, max_value=23)
col1, col2 = st.columns(2)
zip_code = col1.text_input("Enter your ZIP Code")
cnt_code = col2.selectbox("Enter your country", country_list)
weather_input = {"zip_code": zip_code, "cnt_code": cnt_code}
submitted = st.form_submit_button("Submit")
if submitted or st.session_state.load_state is True:
delta = d - date.today()
day_from = delta.days # get difference in days
weather_pred = get_weather_pred(weather_input, local=True)
st.session_state.load_state = True
if st.session_state.load_state is False:
return
weather_dict = weather_info(weather_pred, day_from, hour)
if weather_dict is None:
return
map = pd.DataFrame(
[[weather_dict["lat"], weather_dict["lon"]]], columns=["lat", "lon"]
)
# plot map
st.map(map)
st.write(
f"For the zip code {zip_code} in {cnt_code}, here's what we know about the weather on {d.strftime('%m/%d/%Y')} at hour {hour}"
)
st.markdown(
f"""
- It will be a day with {weather_dict['weather']['main']}, more specifically, {weather_dict['weather']['description']}
- The low is {weather_dict['temp']['min'] } degrees and the high is {weather_dict['temp']['max']} degrees
"""
)
# save for model prediction
wind_direction = weather_dict["wind_dir"]
wind_speed = weather_dict["wind_speed"]
air_temperature = weather_dict["air_temp"]
st.subheader("This app isn't your weather channel, time to make predictions!")
st.subheader("But first, some info about your building")
submit = False
with st.form("company_info"):
col1, col2 = st.columns(2)
primary_use = col1.selectbox("What is the use of the building?", prim_use)
meter = col2.selectbox("What meter are you predicting for?", meter_types.keys())
col1, col2 = st.columns(2)
year_built = col1.number_input(
"What year was the building built?",
min_value=1968,
)
square_feet = col2.number_input("Square feet", min_value=0)
floor_count = st.number_input("Floor count", min_value=0)
is_holiday = 0
if d in us_holidays:
is_holiday = 1
weekday = d.weekday()
month = d.month
day = d.day
user_input = [
[
int(le.transform([primary_use])[0]),
meter_types[meter],
wind_direction,
is_holiday,
square_feet,
year_built,
air_temperature,
floor_count,
weekday,
day,
hour,
month,
wind_speed,
]
]
submitted = st.form_submit_button("Submit")
def get_energy_pred(features):
res = np.expm1(sum(model.predict(features) for model in models) / 2)
return round(res[0], 3)
if submitted:
# st.write(user_input)
pred = get_energy_pred(user_input)
st.success("Thanks for the information!")
submit = True
if submit is False:
return
st.subheader(f"The energy consumption of your building is {pred} kWh!")
st.markdown(
"""
---
### Data Source
- [ASHRAE - Great Energy Predictor III](https://www.kaggle.com/c/ashrae-energy-prediction/data)
### Notebook for training the model
- https://colab.research.google.com/drive/1b8RsxVAfBuSFnlijExt6Oef2ayF4hyFV?usp=sharing
"""
)