Spaces:
Sleeping
Sleeping
adjoint-bass
commited on
Commit
•
b1a1a2f
1
Parent(s):
36e720f
update app and functions
Browse files- app.py +6 -6
- functions.py +9 -3
app.py
CHANGED
@@ -3,7 +3,7 @@ import hopsworks
|
|
3 |
import joblib
|
4 |
import pandas as pd
|
5 |
import datetime
|
6 |
-
from functions import get_weather_data_weekly, data_encoder, get_aplevel
|
7 |
from PIL import Image
|
8 |
|
9 |
|
@@ -45,19 +45,19 @@ model = joblib.load(model_dir + "/aqi_model.pkl")
|
|
45 |
st.write("Succesfully loaded!✔️")
|
46 |
st.sidebar.write("-" * 36)
|
47 |
|
48 |
-
fancy_header("Making AQI
|
49 |
|
50 |
preds = model.predict(data_encoder(weekly_data)).astype(int)
|
51 |
|
52 |
-
|
|
|
53 |
|
54 |
next_week_datetime = [today + datetime.timedelta(days=d) for d in range(7)]
|
55 |
-
|
56 |
-
next_week_str = [f"{days.strftime('%Y-%m-%d')}, {days.strftime('%A')}" for days in next_week_datetime]
|
57 |
|
58 |
df = pd.DataFrame(data=[preds, poll_level], index=["AQI", "Air pollution level"], columns=next_week_str)
|
59 |
|
60 |
st.write("Here they are!")
|
61 |
-
st.dataframe(df.style.apply
|
62 |
|
63 |
st.button("Re-run")
|
|
|
3 |
import joblib
|
4 |
import pandas as pd
|
5 |
import datetime
|
6 |
+
from functions import get_weather_data_weekly, data_encoder, get_aplevel, get_color
|
7 |
from PIL import Image
|
8 |
|
9 |
|
|
|
45 |
st.write("Succesfully loaded!✔️")
|
46 |
st.sidebar.write("-" * 36)
|
47 |
|
48 |
+
fancy_header("Making AQI predictions for the next 7 days")
|
49 |
|
50 |
preds = model.predict(data_encoder(weekly_data)).astype(int)
|
51 |
|
52 |
+
air_pollution_level = ['Good', 'Moderate', 'Unhealthy for sensitive Groups','Unhealthy' ,'Very Unhealthy', 'Hazardous']
|
53 |
+
poll_level = get_aplevel(preds.T.reshape(-1, 1), air_pollution_level)
|
54 |
|
55 |
next_week_datetime = [today + datetime.timedelta(days=d) for d in range(7)]
|
56 |
+
next_week_str = [f"{days.strftime('%A')}, {days.strftime('%Y-%m-%d')}" for days in next_week_datetime]
|
|
|
57 |
|
58 |
df = pd.DataFrame(data=[preds, poll_level], index=["AQI", "Air pollution level"], columns=next_week_str)
|
59 |
|
60 |
st.write("Here they are!")
|
61 |
+
st.dataframe(df.style.apply(get_color, subset=(["Air pollution level"], slice(None))))
|
62 |
|
63 |
st.button("Re-run")
|
functions.py
CHANGED
@@ -163,12 +163,18 @@ def data_encoder(X):
|
|
163 |
X['conditions'] = OrdinalEncoder().fit_transform(X[['conditions']])
|
164 |
return X
|
165 |
|
166 |
-
def get_aplevel(temps:np.ndarray
|
167 |
boundary_list = np.array([0, 50, 100, 150, 200, 300]) # assert temps.shape == [x, 1]
|
168 |
redf = np.logical_not(temps<=boundary_list) # temps.shape[0] x boundary_list.shape[0] ndarray
|
169 |
hift = np.concatenate((np.roll(redf, -1)[:, :-1], np.full((temps.shape[0], 1), False)), axis = 1)
|
170 |
cat = np.nonzero(np.not_equal(redf,hift))
|
171 |
|
|
|
|
|
|
|
|
|
172 |
air_pollution_level = ['Good', 'Moderate', 'Unhealthy for sensitive Groups','Unhealthy' ,'Very Unhealthy', 'Hazardous']
|
173 |
-
|
174 |
-
|
|
|
|
|
|
163 |
X['conditions'] = OrdinalEncoder().fit_transform(X[['conditions']])
|
164 |
return X
|
165 |
|
166 |
+
def get_aplevel(temps:np.ndarray, table:list):
|
167 |
boundary_list = np.array([0, 50, 100, 150, 200, 300]) # assert temps.shape == [x, 1]
|
168 |
redf = np.logical_not(temps<=boundary_list) # temps.shape[0] x boundary_list.shape[0] ndarray
|
169 |
hift = np.concatenate((np.roll(redf, -1)[:, :-1], np.full((temps.shape[0], 1), False)), axis = 1)
|
170 |
cat = np.nonzero(np.not_equal(redf,hift))
|
171 |
|
172 |
+
level = [table[el] for el in cat[1]]
|
173 |
+
return level
|
174 |
+
|
175 |
+
def get_color(level:list):
|
176 |
air_pollution_level = ['Good', 'Moderate', 'Unhealthy for sensitive Groups','Unhealthy' ,'Very Unhealthy', 'Hazardous']
|
177 |
+
color_list = ["Green", "Yellow", "DarkOrange", "Red", "Purple", "DarkRed"]
|
178 |
+
ind = [air_pollution_level.index(lel) for lel in level]
|
179 |
+
text = [f"color:{color_list[idex]};" for idex in ind]
|
180 |
+
return text
|