Merge branch 'main' of https://huggingface.co/spaces/mihkelmj/utrecht-pollution-prediction
Browse files- app.py +50 -33
- pollution_data.csv +2 -2
- predictions_history.csv +0 -1
- weather_data.csv +3 -3
app.py
CHANGED
@@ -112,9 +112,8 @@ with col2:
|
|
112 |
)
|
113 |
|
114 |
# Create two columns for two separate graphs
|
115 |
-
# Plot O3 in the first subcolumn
|
116 |
st.subheader("O3 Forecast")
|
117 |
-
|
118 |
# Define the new color logic: green, orange, and red based on the threshold
|
119 |
def get_simple_color_scale(values, threshold):
|
120 |
"""Returns green for values below the threshold, orange for values between the threshold and 2x the threshold, and red for values above 2x the threshold."""
|
@@ -125,18 +124,35 @@ with col2:
|
|
125 |
]
|
126 |
|
127 |
# O3 Bar Plot (threshold: 40)
|
128 |
-
|
|
|
|
|
129 |
|
130 |
fig_o3 = go.Figure()
|
|
|
|
|
131 |
fig_o3.add_trace(
|
132 |
go.Bar(
|
133 |
-
x=df["Date"],
|
134 |
-
y=
|
135 |
-
name="O3",
|
136 |
marker=dict(color=o3_colors), # Apply the color scale
|
137 |
hovertemplate="%{x|%d-%b-%Y}<br>%{y} µg/m³<extra></extra>",
|
138 |
)
|
139 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
fig_o3.add_shape(
|
141 |
dict(
|
142 |
type="line",
|
@@ -147,6 +163,7 @@ with col2:
|
|
147 |
line=dict(color="White", width=3, dash="dash"),
|
148 |
)
|
149 |
)
|
|
|
150 |
fig_o3.update_layout(
|
151 |
plot_bgcolor="rgba(0, 0, 0, 0)",
|
152 |
paper_bgcolor="rgba(0, 0, 0, 0)",
|
@@ -162,22 +179,42 @@ with col2:
|
|
162 |
tickangle=-45,
|
163 |
tickcolor="gray",
|
164 |
),
|
|
|
165 |
)
|
|
|
166 |
st.plotly_chart(fig_o3, key="fig_o3")
|
167 |
|
168 |
# NO2 Bar Plot (threshold: 120)
|
169 |
-
no2_colors = get_simple_color_scale(no2_values, 120) # Green below 120, Orange above 120, Red above 240
|
170 |
st.subheader("NO2 Forecast")
|
|
|
|
|
|
|
|
|
171 |
fig_no2 = go.Figure()
|
|
|
|
|
172 |
fig_no2.add_trace(
|
173 |
go.Bar(
|
174 |
-
x=df["Date"],
|
175 |
-
y=
|
176 |
-
name="NO2",
|
177 |
marker=dict(color=no2_colors), # Apply the color scale
|
178 |
hovertemplate="%{x|%d-%b-%Y}<br>%{y} µg/m³<extra></extra>",
|
179 |
)
|
180 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
fig_no2.add_shape(
|
182 |
dict(
|
183 |
type="line",
|
@@ -188,6 +225,7 @@ with col2:
|
|
188 |
line=dict(color="White", width=3, dash="dash"),
|
189 |
)
|
190 |
)
|
|
|
191 |
fig_no2.update_layout(
|
192 |
plot_bgcolor="rgba(0, 0, 0, 0)",
|
193 |
paper_bgcolor="rgba(0, 0, 0, 0)",
|
@@ -203,28 +241,7 @@ with col2:
|
|
203 |
tickangle=-45,
|
204 |
tickcolor="gray",
|
205 |
),
|
|
|
206 |
)
|
207 |
-
st.plotly_chart(fig_no2, key="fig_no2")
|
208 |
-
|
209 |
-
import matplotlib.pyplot as plt
|
210 |
-
import numpy as np
|
211 |
-
|
212 |
-
fig, ax = plt.subplots()
|
213 |
-
|
214 |
-
bar = ax.bar([1,2,3,4,5,6],[4,5,6,3,7,5])
|
215 |
-
|
216 |
-
def gradientbars(bars):
|
217 |
-
grad = np.atleast_2d(np.linspace(0,1,256)).T
|
218 |
-
ax = bars[0].axes
|
219 |
-
lim = ax.get_xlim()+ax.get_ylim()
|
220 |
-
for bar in bars:
|
221 |
-
bar.set_zorder(1)
|
222 |
-
bar.set_facecolor("none")
|
223 |
-
x,y = bar.get_xy()
|
224 |
-
w, h = bar.get_width(), bar.get_height()
|
225 |
-
ax.imshow(grad, extent=[x,x+w,y,y+h], aspect="auto", zorder=0)
|
226 |
-
ax.axis(lim)
|
227 |
-
|
228 |
-
gradientbars(bar)
|
229 |
|
230 |
-
|
|
|
112 |
)
|
113 |
|
114 |
# Create two columns for two separate graphs
|
|
|
115 |
st.subheader("O3 Forecast")
|
116 |
+
|
117 |
# Define the new color logic: green, orange, and red based on the threshold
|
118 |
def get_simple_color_scale(values, threshold):
|
119 |
"""Returns green for values below the threshold, orange for values between the threshold and 2x the threshold, and red for values above 2x the threshold."""
|
|
|
124 |
]
|
125 |
|
126 |
# O3 Bar Plot (threshold: 40)
|
127 |
+
o3_past_values = o3_values[:-3] # Last 3 values are predictions
|
128 |
+
o3_future_values = o3_values[-3:] # Last 3 values are predictions
|
129 |
+
o3_colors = get_simple_color_scale(o3_past_values, 40) # Color for past values
|
130 |
|
131 |
fig_o3 = go.Figure()
|
132 |
+
|
133 |
+
# Add past values
|
134 |
fig_o3.add_trace(
|
135 |
go.Bar(
|
136 |
+
x=df["Date"][:-3], # Dates for past values
|
137 |
+
y=o3_past_values,
|
138 |
+
name="O3 Past",
|
139 |
marker=dict(color=o3_colors), # Apply the color scale
|
140 |
hovertemplate="%{x|%d-%b-%Y}<br>%{y} µg/m³<extra></extra>",
|
141 |
)
|
142 |
)
|
143 |
+
|
144 |
+
# Add predicted values with reduced opacity
|
145 |
+
predicted_o3_colors = get_simple_color_scale(o3_future_values, 40) # Color for future values
|
146 |
+
fig_o3.add_trace(
|
147 |
+
go.Bar(
|
148 |
+
x=df["Date"][-3:], # Dates for predicted values
|
149 |
+
y=o3_future_values,
|
150 |
+
name="O3 Predicted",
|
151 |
+
marker=dict(color=predicted_o3_colors, opacity=0.5), # Set opacity to 0.5 for predictions
|
152 |
+
hovertemplate="%{x|%d-%b-%Y}<br>%{y} µg/m³<extra></extra>",
|
153 |
+
)
|
154 |
+
)
|
155 |
+
|
156 |
fig_o3.add_shape(
|
157 |
dict(
|
158 |
type="line",
|
|
|
163 |
line=dict(color="White", width=3, dash="dash"),
|
164 |
)
|
165 |
)
|
166 |
+
|
167 |
fig_o3.update_layout(
|
168 |
plot_bgcolor="rgba(0, 0, 0, 0)",
|
169 |
paper_bgcolor="rgba(0, 0, 0, 0)",
|
|
|
179 |
tickangle=-45,
|
180 |
tickcolor="gray",
|
181 |
),
|
182 |
+
showlegend=False # Disable legend
|
183 |
)
|
184 |
+
|
185 |
st.plotly_chart(fig_o3, key="fig_o3")
|
186 |
|
187 |
# NO2 Bar Plot (threshold: 120)
|
|
|
188 |
st.subheader("NO2 Forecast")
|
189 |
+
no2_past_values = no2_values[:-3] # Last 3 values are predictions
|
190 |
+
no2_future_values = no2_values[-3:] # Last 3 values are predictions
|
191 |
+
no2_colors = get_simple_color_scale(no2_past_values, 120) # Color for past values
|
192 |
+
|
193 |
fig_no2 = go.Figure()
|
194 |
+
|
195 |
+
# Add past values
|
196 |
fig_no2.add_trace(
|
197 |
go.Bar(
|
198 |
+
x=df["Date"][:-3], # Dates for past values
|
199 |
+
y=no2_past_values,
|
200 |
+
name="NO2 Past",
|
201 |
marker=dict(color=no2_colors), # Apply the color scale
|
202 |
hovertemplate="%{x|%d-%b-%Y}<br>%{y} µg/m³<extra></extra>",
|
203 |
)
|
204 |
)
|
205 |
+
|
206 |
+
# Add predicted values with reduced opacity
|
207 |
+
predicted_no2_colors = get_simple_color_scale(no2_future_values, 120) # Color for future values
|
208 |
+
fig_no2.add_trace(
|
209 |
+
go.Bar(
|
210 |
+
x=df["Date"][-3:], # Dates for predicted values
|
211 |
+
y=no2_future_values,
|
212 |
+
name="NO2 Predicted",
|
213 |
+
marker=dict(color=predicted_no2_colors, opacity=0.5), # Set opacity to 0.5 for predictions
|
214 |
+
hovertemplate="%{x|%d-%b-%Y}<br>%{y} µg/m³<extra></extra>",
|
215 |
+
)
|
216 |
+
)
|
217 |
+
|
218 |
fig_no2.add_shape(
|
219 |
dict(
|
220 |
type="line",
|
|
|
225 |
line=dict(color="White", width=3, dash="dash"),
|
226 |
)
|
227 |
)
|
228 |
+
|
229 |
fig_no2.update_layout(
|
230 |
plot_bgcolor="rgba(0, 0, 0, 0)",
|
231 |
paper_bgcolor="rgba(0, 0, 0, 0)",
|
|
|
241 |
tickangle=-45,
|
242 |
tickcolor="gray",
|
243 |
),
|
244 |
+
showlegend=False # Disable legend
|
245 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
246 |
|
247 |
+
st.plotly_chart(fig_no2, key="fig_no2")
|
pollution_data.csv
CHANGED
@@ -11,5 +11,5 @@ date,NO2,O3
|
|
11 |
2024-10-26,24.46423484380123,18.70331123489324
|
12 |
2024-10-27,27.53722134983982,20.80809239842384
|
13 |
2024-10-28,23.337567567567568,26.82861788617886
|
14 |
-
2024-10-29,16.53533209586906,23.
|
15 |
-
2024-10-30,22.26162162162162,18.
|
|
|
11 |
2024-10-26,24.46423484380123,18.70331123489324
|
12 |
2024-10-27,27.53722134983982,20.80809239842384
|
13 |
2024-10-28,23.337567567567568,26.82861788617886
|
14 |
+
2024-10-29,16.53533209586906,23.28254887605004
|
15 |
+
2024-10-30,22.26162162162162,18.03443548387097
|
predictions_history.csv
CHANGED
@@ -70,4 +70,3 @@ NO2,2024-10-30,2024-10-31,29.77507241979599
|
|
70 |
O3,2024-10-30,2024-11-01,21.135906183680472
|
71 |
NO2,2024-10-30,2024-11-01,28.38872595850704
|
72 |
O3,2024-10-30,2024-11-02,19.67426015042635
|
73 |
-
NO2,2024-10-30,2024-11-02,25.564317135035058
|
|
|
70 |
O3,2024-10-30,2024-11-01,21.135906183680472
|
71 |
NO2,2024-10-30,2024-11-01,28.38872595850704
|
72 |
O3,2024-10-30,2024-11-02,19.67426015042635
|
|
weather_data.csv
CHANGED
@@ -9,7 +9,7 @@ date,temp,humidity,precip,windspeed,sealevelpressure,visibility,solarradiation
|
|
9 |
2024-10-24,10.4,94.0,0.0,20.5,1024.7,13.0,62.5
|
10 |
2024-10-25,13.6,92.2,0.5,11.9,1016.8,24.0,93.0
|
11 |
2024-10-26,13.7,91.5,0.0,11.9,1016.3,23.3,8.0
|
12 |
-
2024-10-27,
|
13 |
-
2024-10-28,
|
14 |
2024-10-29,13.8,95.9,0.2,20.5,1023.1,8.1,16.0
|
15 |
-
2024-10-30,12.7,92.
|
|
|
9 |
2024-10-24,10.4,94.0,0.0,20.5,1024.7,13.0,62.5
|
10 |
2024-10-25,13.6,92.2,0.5,11.9,1016.8,24.0,93.0
|
11 |
2024-10-26,13.7,91.5,0.0,11.9,1016.3,23.3,8.0
|
12 |
+
2024-10-27,13.2,87.1,0.1,20.5,1019.4,10.4,28.6
|
13 |
+
2024-10-28,12.4,91.8,1.1,31.7,1021.8,12.8,27.3
|
14 |
2024-10-29,13.8,95.9,0.2,20.5,1023.1,8.1,16.0
|
15 |
+
2024-10-30,12.7,92.9,0.6,9.4,1027.5,12.5,32.8
|