Update part2_visualization.py
Browse files- part2_visualization.py +147 -74
part2_visualization.py
CHANGED
@@ -4,6 +4,7 @@ import folium
|
|
4 |
from folium import plugins
|
5 |
import numpy as np
|
6 |
import branca.colormap as cm
|
|
|
7 |
|
8 |
class VisualizationHandler:
|
9 |
def __init__(self, optimal_conditions):
|
@@ -20,33 +21,33 @@ class VisualizationHandler:
|
|
20 |
]
|
21 |
|
22 |
def create_interactive_plots(self, df):
|
23 |
-
"""Create enhanced interactive Plotly visualizations"""
|
24 |
fig = make_subplots(
|
25 |
-
rows=5, cols=1,
|
26 |
subplot_titles=(
|
27 |
'<b>Temperature (°C)</b>',
|
28 |
'<b>Humidity (%)</b>',
|
29 |
'<b>Rainfall (mm/day)</b>',
|
30 |
'<b>Vegetation Index (NDVI)</b>',
|
31 |
-
'<b>
|
32 |
),
|
33 |
-
vertical_spacing=0.
|
34 |
-
row_heights=[0.
|
35 |
)
|
36 |
-
|
37 |
-
#
|
38 |
self.add_temperature_plot(fig, df)
|
39 |
|
40 |
-
#
|
41 |
self.add_humidity_plot(fig, df)
|
42 |
|
43 |
-
#
|
44 |
self.add_rainfall_plot(fig, df)
|
45 |
|
46 |
-
# NDVI
|
47 |
self.add_ndvi_plot(fig, df)
|
48 |
|
49 |
-
#
|
50 |
self.add_suitability_plot(fig, df)
|
51 |
|
52 |
# Update layout
|
@@ -54,7 +55,7 @@ class VisualizationHandler:
|
|
54 |
height=1200,
|
55 |
showlegend=True,
|
56 |
title={
|
57 |
-
'text': "
|
58 |
'y':0.95,
|
59 |
'x':0.5,
|
60 |
'xanchor': 'center',
|
@@ -74,23 +75,25 @@ class VisualizationHandler:
|
|
74 |
margin=dict(l=60, r=30, t=100, b=60)
|
75 |
)
|
76 |
|
|
|
|
|
|
|
|
|
77 |
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,0,0.1)')
|
78 |
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,0,0.1)')
|
79 |
|
80 |
return fig
|
81 |
|
82 |
def add_temperature_plot(self, fig, df):
|
83 |
-
"""Add temperature visualization"""
|
84 |
-
#
|
85 |
fig.add_trace(
|
86 |
go.Scatter(
|
87 |
x=df['date'],
|
88 |
y=df['temp_max'],
|
89 |
name='Max Temperature',
|
90 |
-
line=dict(color='
|
91 |
-
|
92 |
-
fillcolor='rgba(255,0,0,0.1)',
|
93 |
-
fill='tonexty'
|
94 |
),
|
95 |
row=1, col=1
|
96 |
)
|
@@ -99,27 +102,46 @@ class VisualizationHandler:
|
|
99 |
go.Scatter(
|
100 |
x=df['date'],
|
101 |
y=df['temp_min'],
|
102 |
-
name='
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
),
|
108 |
row=1, col=1
|
109 |
)
|
110 |
|
111 |
# Add optimal range
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
def add_humidity_plot(self, fig, df):
|
122 |
-
"""Add humidity visualization"""
|
|
|
123 |
fig.add_trace(
|
124 |
go.Scatter(
|
125 |
x=df['date'],
|
@@ -131,42 +153,77 @@ class VisualizationHandler:
|
|
131 |
row=2, col=1
|
132 |
)
|
133 |
|
|
|
134 |
fig.add_trace(
|
135 |
go.Scatter(
|
136 |
x=df['date'],
|
137 |
y=df['humidity_7day_avg'],
|
138 |
-
name='7-day Humidity
|
139 |
-
line=dict(color='
|
140 |
mode='lines'
|
141 |
),
|
142 |
row=2, col=1
|
143 |
)
|
144 |
|
145 |
def add_rainfall_plot(self, fig, df):
|
146 |
-
"""Add rainfall visualization"""
|
|
|
147 |
fig.add_trace(
|
148 |
go.Bar(
|
149 |
x=df['date'],
|
150 |
y=df['rainfall'],
|
151 |
name='Rainfall',
|
152 |
-
marker_color='blue'
|
|
|
153 |
),
|
154 |
row=3, col=1
|
155 |
)
|
156 |
|
|
|
|
|
157 |
fig.add_trace(
|
158 |
go.Scatter(
|
159 |
x=df['date'],
|
160 |
-
y=
|
161 |
-
name='
|
162 |
-
line=dict(color='
|
163 |
-
|
164 |
),
|
165 |
row=3, col=1
|
166 |
)
|
167 |
|
168 |
def add_ndvi_plot(self, fig, df):
|
169 |
-
"""Add NDVI visualization"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
fig.add_trace(
|
171 |
go.Scatter(
|
172 |
x=df['date'],
|
@@ -177,33 +234,47 @@ class VisualizationHandler:
|
|
177 |
),
|
178 |
row=4, col=1
|
179 |
)
|
180 |
-
|
181 |
-
# Add optimal NDVI range
|
182 |
-
for limit in ['min', 'max']:
|
183 |
-
fig.add_hline(
|
184 |
-
y=self.optimal_conditions['ndvi'][limit],
|
185 |
-
line_dash="dash",
|
186 |
-
line_color="green",
|
187 |
-
annotation_text=f"Optimal {limit}",
|
188 |
-
row=4, col=1
|
189 |
-
)
|
190 |
|
191 |
def add_suitability_plot(self, fig, df):
|
192 |
-
"""Add
|
193 |
fig.add_trace(
|
194 |
go.Scatter(
|
195 |
x=df['date'],
|
196 |
y=df['daily_suitability'],
|
197 |
name='Growing Suitability',
|
198 |
-
line=dict(color='
|
199 |
-
|
200 |
-
|
201 |
),
|
202 |
row=5, col=1
|
203 |
)
|
204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
def create_enhanced_map(self, lat, lon, score, ndvi_value):
|
206 |
-
"""Create an interactive map with
|
207 |
m = folium.Map(location=[lat, lon], zoom_start=13)
|
208 |
|
209 |
# Add measurement tools
|
@@ -222,7 +293,7 @@ class VisualizationHandler:
|
|
222 |
colors=self.ndvi_colors,
|
223 |
vmin=-1,
|
224 |
vmax=1,
|
225 |
-
caption='NDVI
|
226 |
)
|
227 |
|
228 |
# Add NDVI circle
|
@@ -235,46 +306,38 @@ class VisualizationHandler:
|
|
235 |
fillOpacity=0.4
|
236 |
).add_to(m)
|
237 |
|
238 |
-
# Add
|
239 |
score_color = self.get_score_color(score)
|
240 |
for radius in [500, 1000, 1500]:
|
241 |
folium.Circle(
|
242 |
radius=radius,
|
243 |
location=[lat, lon],
|
244 |
-
popup=f'Suitability
|
245 |
color=score_color,
|
246 |
fill=False,
|
247 |
weight=2
|
248 |
).add_to(m)
|
249 |
|
|
|
|
|
|
|
250 |
# Add mini map
|
251 |
minimap = plugins.MiniMap()
|
252 |
m.add_child(minimap)
|
253 |
|
254 |
-
# Add
|
255 |
-
folium.LayerControl().add_to(m)
|
256 |
m.add_child(ndvi_colormap)
|
257 |
|
258 |
return m._repr_html_()
|
259 |
|
260 |
-
def
|
261 |
-
"""Get color based on score"""
|
262 |
-
if score >= 0.8:
|
263 |
-
return 'green'
|
264 |
-
elif score >= 0.6:
|
265 |
-
return 'yellow'
|
266 |
-
elif score >= 0.4:
|
267 |
-
return 'orange'
|
268 |
-
return 'red'
|
269 |
-
|
270 |
-
def create_gauge_chart(self, score, title="Growing Conditions Score"):
|
271 |
"""Create an enhanced gauge chart for the overall score"""
|
272 |
fig = go.Figure(go.Indicator(
|
273 |
mode="gauge+number+delta",
|
274 |
value=score,
|
275 |
domain={'x': [0, 1], 'y': [0, 1]},
|
276 |
title={
|
277 |
-
'text':
|
278 |
'font': {'size': 24}
|
279 |
},
|
280 |
delta={
|
@@ -309,4 +372,14 @@ class VisualizationHandler:
|
|
309 |
font={'color': "darkblue", 'family': "Arial"}
|
310 |
)
|
311 |
|
312 |
-
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from folium import plugins
|
5 |
import numpy as np
|
6 |
import branca.colormap as cm
|
7 |
+
from datetime import datetime, timedelta
|
8 |
|
9 |
class VisualizationHandler:
|
10 |
def __init__(self, optimal_conditions):
|
|
|
21 |
]
|
22 |
|
23 |
def create_interactive_plots(self, df):
|
24 |
+
"""Create enhanced interactive Plotly visualizations with pattern emphasis"""
|
25 |
fig = make_subplots(
|
26 |
+
rows=5, cols=1,
|
27 |
subplot_titles=(
|
28 |
'<b>Temperature (°C)</b>',
|
29 |
'<b>Humidity (%)</b>',
|
30 |
'<b>Rainfall (mm/day)</b>',
|
31 |
'<b>Vegetation Index (NDVI)</b>',
|
32 |
+
'<b>Growing Suitability</b>'
|
33 |
),
|
34 |
+
vertical_spacing=0.08,
|
35 |
+
row_heights=[0.23, 0.19, 0.19, 0.19, 0.20]
|
36 |
)
|
37 |
+
|
38 |
+
# Add temperature visualization with range
|
39 |
self.add_temperature_plot(fig, df)
|
40 |
|
41 |
+
# Add humidity visualization
|
42 |
self.add_humidity_plot(fig, df)
|
43 |
|
44 |
+
# Add rainfall visualization
|
45 |
self.add_rainfall_plot(fig, df)
|
46 |
|
47 |
+
# Add NDVI visualization
|
48 |
self.add_ndvi_plot(fig, df)
|
49 |
|
50 |
+
# Add suitability visualization
|
51 |
self.add_suitability_plot(fig, df)
|
52 |
|
53 |
# Update layout
|
|
|
55 |
height=1200,
|
56 |
showlegend=True,
|
57 |
title={
|
58 |
+
'text': "Weather Patterns and Agricultural Conditions Analysis",
|
59 |
'y':0.95,
|
60 |
'x':0.5,
|
61 |
'xanchor': 'center',
|
|
|
75 |
margin=dict(l=60, r=30, t=100, b=60)
|
76 |
)
|
77 |
|
78 |
+
# Add season shading for all plots
|
79 |
+
self.add_season_shading(fig, df)
|
80 |
+
|
81 |
+
# Update axes
|
82 |
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,0,0.1)')
|
83 |
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,0,0.1)')
|
84 |
|
85 |
return fig
|
86 |
|
87 |
def add_temperature_plot(self, fig, df):
|
88 |
+
"""Add detailed temperature visualization"""
|
89 |
+
# Temperature range area
|
90 |
fig.add_trace(
|
91 |
go.Scatter(
|
92 |
x=df['date'],
|
93 |
y=df['temp_max'],
|
94 |
name='Max Temperature',
|
95 |
+
line=dict(color='rgba(255,0,0,0.0)'),
|
96 |
+
showlegend=False
|
|
|
|
|
97 |
),
|
98 |
row=1, col=1
|
99 |
)
|
|
|
102 |
go.Scatter(
|
103 |
x=df['date'],
|
104 |
y=df['temp_min'],
|
105 |
+
name='Temperature Range',
|
106 |
+
fill='tonexty',
|
107 |
+
fillcolor='rgba(255,0,0,0.1)',
|
108 |
+
line=dict(color='rgba(255,0,0,0.0)'),
|
109 |
+
showlegend=True
|
110 |
+
),
|
111 |
+
row=1, col=1
|
112 |
+
)
|
113 |
+
|
114 |
+
# Main temperature line
|
115 |
+
fig.add_trace(
|
116 |
+
go.Scatter(
|
117 |
+
x=df['date'],
|
118 |
+
y=df['temperature'],
|
119 |
+
name='Temperature',
|
120 |
+
line=dict(color='red', width=2),
|
121 |
+
mode='lines'
|
122 |
),
|
123 |
row=1, col=1
|
124 |
)
|
125 |
|
126 |
# Add optimal range
|
127 |
+
fig.add_hline(
|
128 |
+
y=self.optimal_conditions['temperature']['min'],
|
129 |
+
line_dash="dash",
|
130 |
+
line_color="green",
|
131 |
+
annotation_text="Min Optimal",
|
132 |
+
row=1, col=1
|
133 |
+
)
|
134 |
+
fig.add_hline(
|
135 |
+
y=self.optimal_conditions['temperature']['max'],
|
136 |
+
line_dash="dash",
|
137 |
+
line_color="green",
|
138 |
+
annotation_text="Max Optimal",
|
139 |
+
row=1, col=1
|
140 |
+
)
|
141 |
|
142 |
def add_humidity_plot(self, fig, df):
|
143 |
+
"""Add humidity visualization with pattern emphasis"""
|
144 |
+
# Add main humidity line
|
145 |
fig.add_trace(
|
146 |
go.Scatter(
|
147 |
x=df['date'],
|
|
|
153 |
row=2, col=1
|
154 |
)
|
155 |
|
156 |
+
# Add rolling average
|
157 |
fig.add_trace(
|
158 |
go.Scatter(
|
159 |
x=df['date'],
|
160 |
y=df['humidity_7day_avg'],
|
161 |
+
name='7-day Humidity Trend',
|
162 |
+
line=dict(color='darkblue', width=1, dash='dot'),
|
163 |
mode='lines'
|
164 |
),
|
165 |
row=2, col=1
|
166 |
)
|
167 |
|
168 |
def add_rainfall_plot(self, fig, df):
|
169 |
+
"""Add enhanced rainfall visualization"""
|
170 |
+
# Add rainfall bars
|
171 |
fig.add_trace(
|
172 |
go.Bar(
|
173 |
x=df['date'],
|
174 |
y=df['rainfall'],
|
175 |
name='Rainfall',
|
176 |
+
marker_color='blue',
|
177 |
+
opacity=0.6
|
178 |
),
|
179 |
row=3, col=1
|
180 |
)
|
181 |
|
182 |
+
# Add cumulative rainfall line
|
183 |
+
cumulative_rainfall = df['rainfall'].cumsum()
|
184 |
fig.add_trace(
|
185 |
go.Scatter(
|
186 |
x=df['date'],
|
187 |
+
y=cumulative_rainfall,
|
188 |
+
name='Cumulative Rainfall',
|
189 |
+
line=dict(color='darkblue', width=1, dash='dot'),
|
190 |
+
yaxis='y2'
|
191 |
),
|
192 |
row=3, col=1
|
193 |
)
|
194 |
|
195 |
def add_ndvi_plot(self, fig, df):
|
196 |
+
"""Add NDVI visualization with confidence bands"""
|
197 |
+
# Calculate confidence bands
|
198 |
+
ndvi_std = df['estimated_ndvi'].std()
|
199 |
+
upper_band = df['estimated_ndvi'] + ndvi_std
|
200 |
+
lower_band = df['estimated_ndvi'] - ndvi_std
|
201 |
+
|
202 |
+
# Add NDVI line with confidence band
|
203 |
+
fig.add_trace(
|
204 |
+
go.Scatter(
|
205 |
+
x=df['date'],
|
206 |
+
y=upper_band,
|
207 |
+
name='NDVI Upper Band',
|
208 |
+
line=dict(color='rgba(0,100,0,0)'),
|
209 |
+
showlegend=False
|
210 |
+
),
|
211 |
+
row=4, col=1
|
212 |
+
)
|
213 |
+
|
214 |
+
fig.add_trace(
|
215 |
+
go.Scatter(
|
216 |
+
x=df['date'],
|
217 |
+
y=lower_band,
|
218 |
+
name='NDVI Confidence',
|
219 |
+
fill='tonexty',
|
220 |
+
fillcolor='rgba(0,100,0,0.1)',
|
221 |
+
line=dict(color='rgba(0,100,0,0)'),
|
222 |
+
showlegend=True
|
223 |
+
),
|
224 |
+
row=4, col=1
|
225 |
+
)
|
226 |
+
|
227 |
fig.add_trace(
|
228 |
go.Scatter(
|
229 |
x=df['date'],
|
|
|
234 |
),
|
235 |
row=4, col=1
|
236 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
|
238 |
def add_suitability_plot(self, fig, df):
|
239 |
+
"""Add growing suitability visualization"""
|
240 |
fig.add_trace(
|
241 |
go.Scatter(
|
242 |
x=df['date'],
|
243 |
y=df['daily_suitability'],
|
244 |
name='Growing Suitability',
|
245 |
+
line=dict(color='purple', width=2),
|
246 |
+
fill='tozeroy',
|
247 |
+
fillcolor='rgba(128,0,128,0.1)'
|
248 |
),
|
249 |
row=5, col=1
|
250 |
)
|
251 |
|
252 |
+
def add_season_shading(self, fig, df):
|
253 |
+
"""Add season shading to all plots"""
|
254 |
+
seasons = df['season'].unique()
|
255 |
+
season_colors = {
|
256 |
+
'Main': 'rgba(0,255,0,0.1)', # Green
|
257 |
+
'Early': 'rgba(255,255,0,0.1)', # Yellow
|
258 |
+
'Late': 'rgba(255,165,0,0.1)', # Orange
|
259 |
+
'Dry': 'rgba(255,0,0,0.1)' # Red
|
260 |
+
}
|
261 |
+
|
262 |
+
for season in seasons:
|
263 |
+
season_data = df[df['season'] == season]
|
264 |
+
if not season_data.empty:
|
265 |
+
fig.add_vrect(
|
266 |
+
x0=season_data['date'].iloc[0],
|
267 |
+
x1=season_data['date'].iloc[-1],
|
268 |
+
fillcolor=season_colors[season],
|
269 |
+
layer="below",
|
270 |
+
line_width=0,
|
271 |
+
annotation_text=season,
|
272 |
+
annotation_position="top left",
|
273 |
+
row="all"
|
274 |
+
)
|
275 |
+
|
276 |
def create_enhanced_map(self, lat, lon, score, ndvi_value):
|
277 |
+
"""Create an interactive map with enhanced visualizations"""
|
278 |
m = folium.Map(location=[lat, lon], zoom_start=13)
|
279 |
|
280 |
# Add measurement tools
|
|
|
293 |
colors=self.ndvi_colors,
|
294 |
vmin=-1,
|
295 |
vmax=1,
|
296 |
+
caption='Vegetation Index (NDVI)'
|
297 |
)
|
298 |
|
299 |
# Add NDVI circle
|
|
|
306 |
fillOpacity=0.4
|
307 |
).add_to(m)
|
308 |
|
309 |
+
# Add suitability circles
|
310 |
score_color = self.get_score_color(score)
|
311 |
for radius in [500, 1000, 1500]:
|
312 |
folium.Circle(
|
313 |
radius=radius,
|
314 |
location=[lat, lon],
|
315 |
+
popup=f'Growing Suitability: {score:.2f}',
|
316 |
color=score_color,
|
317 |
fill=False,
|
318 |
weight=2
|
319 |
).add_to(m)
|
320 |
|
321 |
+
# Add heat map layer control
|
322 |
+
folium.LayerControl().add_to(m)
|
323 |
+
|
324 |
# Add mini map
|
325 |
minimap = plugins.MiniMap()
|
326 |
m.add_child(minimap)
|
327 |
|
328 |
+
# Add colormap to map
|
|
|
329 |
m.add_child(ndvi_colormap)
|
330 |
|
331 |
return m._repr_html_()
|
332 |
|
333 |
+
def create_gauge_chart(self, score):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
334 |
"""Create an enhanced gauge chart for the overall score"""
|
335 |
fig = go.Figure(go.Indicator(
|
336 |
mode="gauge+number+delta",
|
337 |
value=score,
|
338 |
domain={'x': [0, 1], 'y': [0, 1]},
|
339 |
title={
|
340 |
+
'text': "Growing Conditions Score",
|
341 |
'font': {'size': 24}
|
342 |
},
|
343 |
delta={
|
|
|
372 |
font={'color': "darkblue", 'family': "Arial"}
|
373 |
)
|
374 |
|
375 |
+
return fig
|
376 |
+
|
377 |
+
def get_score_color(self, score):
|
378 |
+
"""Get color based on score"""
|
379 |
+
if score >= 0.8:
|
380 |
+
return 'green'
|
381 |
+
elif score >= 0.6:
|
382 |
+
return 'yellow'
|
383 |
+
elif score >= 0.4:
|
384 |
+
return 'orange'
|
385 |
+
return 'red'
|