Update part2_visualization.py
Browse files- part2_visualization.py +136 -128
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):
|
@@ -21,13 +22,16 @@ class VisualizationHandler:
|
|
21 |
|
22 |
def create_interactive_plots(self, df):
|
23 |
"""Create enhanced interactive Plotly visualizations"""
|
|
|
|
|
|
|
24 |
fig = make_subplots(
|
25 |
rows=4, cols=1,
|
26 |
subplot_titles=(
|
27 |
'<b>Temperature Pattern (°C)</b>',
|
28 |
'<b>Humidity Pattern (%)</b>',
|
29 |
'<b>Rainfall Pattern (mm/day)</b>',
|
30 |
-
'<b>Vegetation & Suitability
|
31 |
),
|
32 |
vertical_spacing=0.08,
|
33 |
row_heights=[0.28, 0.24, 0.24, 0.24]
|
@@ -42,15 +46,15 @@ class VisualizationHandler:
|
|
42 |
# Add rainfall visualization
|
43 |
self.add_rainfall_plot(fig, df)
|
44 |
|
45 |
-
# Add
|
46 |
-
self.
|
47 |
|
48 |
# Update layout
|
49 |
fig.update_layout(
|
50 |
height=1000,
|
51 |
showlegend=True,
|
52 |
title={
|
53 |
-
'text': "Agricultural Conditions Analysis
|
54 |
'y':0.95,
|
55 |
'x':0.5,
|
56 |
'xanchor': 'center',
|
@@ -73,38 +77,40 @@ class VisualizationHandler:
|
|
73 |
# Add season shading
|
74 |
self.add_season_shading(fig, df)
|
75 |
|
|
|
76 |
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,0,0.1)')
|
77 |
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,0,0.1)')
|
78 |
|
79 |
return fig
|
80 |
|
81 |
def add_temperature_plot(self, fig, df):
|
82 |
-
"""Add temperature visualization with range
|
83 |
# Temperature range area
|
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 |
fig.add_trace(
|
109 |
go.Scatter(
|
110 |
x=df['date'],
|
@@ -116,30 +122,31 @@ class VisualizationHandler:
|
|
116 |
row=1, col=1
|
117 |
)
|
118 |
|
119 |
-
# Add
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
130 |
|
131 |
# Add optimal range
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
|
141 |
def add_humidity_plot(self, fig, df):
|
142 |
-
"""Add humidity visualization
|
143 |
fig.add_trace(
|
144 |
go.Scatter(
|
145 |
x=df['date'],
|
@@ -150,31 +157,31 @@ class VisualizationHandler:
|
|
150 |
),
|
151 |
row=2, col=1
|
152 |
)
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
164 |
|
165 |
# Add optimal range
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
|
175 |
def add_rainfall_plot(self, fig, df):
|
176 |
-
"""Add rainfall visualization
|
177 |
-
# Daily rainfall bars
|
178 |
fig.add_trace(
|
179 |
go.Bar(
|
180 |
x=df['date'],
|
@@ -186,71 +193,72 @@ class VisualizationHandler:
|
|
186 |
row=3, col=1
|
187 |
)
|
188 |
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
|
201 |
-
def
|
202 |
-
"""Add
|
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 |
def add_season_shading(self, fig, df):
|
228 |
-
"""Add season
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
|
|
251 |
|
252 |
def create_enhanced_map(self, lat, lon, score, ndvi_value):
|
253 |
-
"""Create an interactive map with
|
254 |
m = folium.Map(location=[lat, lon], zoom_start=13)
|
255 |
|
256 |
# Add measurement tools
|
|
|
4 |
from folium import plugins
|
5 |
import numpy as np
|
6 |
import branca.colormap as cm
|
7 |
+
from datetime import datetime
|
8 |
|
9 |
class VisualizationHandler:
|
10 |
def __init__(self, optimal_conditions):
|
|
|
22 |
|
23 |
def create_interactive_plots(self, df):
|
24 |
"""Create enhanced interactive Plotly visualizations"""
|
25 |
+
if df is None or df.empty:
|
26 |
+
return go.Figure() # Return empty figure if no data
|
27 |
+
|
28 |
fig = make_subplots(
|
29 |
rows=4, cols=1,
|
30 |
subplot_titles=(
|
31 |
'<b>Temperature Pattern (°C)</b>',
|
32 |
'<b>Humidity Pattern (%)</b>',
|
33 |
'<b>Rainfall Pattern (mm/day)</b>',
|
34 |
+
'<b>Vegetation & Suitability Indices</b>'
|
35 |
),
|
36 |
vertical_spacing=0.08,
|
37 |
row_heights=[0.28, 0.24, 0.24, 0.24]
|
|
|
46 |
# Add rainfall visualization
|
47 |
self.add_rainfall_plot(fig, df)
|
48 |
|
49 |
+
# Add vegetation and suitability visualization
|
50 |
+
self.add_combined_indices_plot(fig, df)
|
51 |
|
52 |
# Update layout
|
53 |
fig.update_layout(
|
54 |
height=1000,
|
55 |
showlegend=True,
|
56 |
title={
|
57 |
+
'text': "Agricultural Conditions Analysis",
|
58 |
'y':0.95,
|
59 |
'x':0.5,
|
60 |
'xanchor': 'center',
|
|
|
77 |
# Add season shading
|
78 |
self.add_season_shading(fig, df)
|
79 |
|
80 |
+
# Update axes for all subplots
|
81 |
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,0,0.1)')
|
82 |
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,0,0.1)')
|
83 |
|
84 |
return fig
|
85 |
|
86 |
def add_temperature_plot(self, fig, df):
|
87 |
+
"""Add temperature visualization with range"""
|
88 |
# Temperature range area
|
89 |
+
if 'temp_max' in df.columns and 'temp_min' in df.columns:
|
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 |
+
)
|
100 |
+
|
101 |
+
fig.add_trace(
|
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 |
+
),
|
110 |
+
row=1, col=1
|
111 |
+
)
|
112 |
|
113 |
+
# Main temperature line
|
114 |
fig.add_trace(
|
115 |
go.Scatter(
|
116 |
x=df['date'],
|
|
|
122 |
row=1, col=1
|
123 |
)
|
124 |
|
125 |
+
# Add rolling average
|
126 |
+
if 'temp_7day_avg' in df.columns:
|
127 |
+
fig.add_trace(
|
128 |
+
go.Scatter(
|
129 |
+
x=df['date'],
|
130 |
+
y=df['temp_7day_avg'],
|
131 |
+
name='7-Day Average',
|
132 |
+
line=dict(color='darkred', width=1, dash='dot'),
|
133 |
+
mode='lines'
|
134 |
+
),
|
135 |
+
row=1, col=1
|
136 |
+
)
|
137 |
|
138 |
# Add optimal range
|
139 |
+
for limit_type, value in self.optimal_conditions['temperature'].items():
|
140 |
+
fig.add_hline(
|
141 |
+
y=value,
|
142 |
+
line_dash="dash",
|
143 |
+
line_color="green",
|
144 |
+
annotation_text=f"Optimal {limit_type}",
|
145 |
+
row=1, col=1
|
146 |
+
)
|
147 |
|
148 |
def add_humidity_plot(self, fig, df):
|
149 |
+
"""Add humidity visualization"""
|
150 |
fig.add_trace(
|
151 |
go.Scatter(
|
152 |
x=df['date'],
|
|
|
157 |
),
|
158 |
row=2, col=1
|
159 |
)
|
160 |
+
|
161 |
+
if 'humidity_7day_avg' in df.columns:
|
162 |
+
fig.add_trace(
|
163 |
+
go.Scatter(
|
164 |
+
x=df['date'],
|
165 |
+
y=df['humidity_7day_avg'],
|
166 |
+
name='7-Day Average',
|
167 |
+
line=dict(color='darkblue', width=1, dash='dot'),
|
168 |
+
mode='lines'
|
169 |
+
),
|
170 |
+
row=2, col=1
|
171 |
+
)
|
172 |
|
173 |
# Add optimal range
|
174 |
+
for limit_type, value in self.optimal_conditions['humidity'].items():
|
175 |
+
fig.add_hline(
|
176 |
+
y=value,
|
177 |
+
line_dash="dash",
|
178 |
+
line_color="green",
|
179 |
+
annotation_text=f"Optimal {limit_type}",
|
180 |
+
row=2, col=1
|
181 |
+
)
|
182 |
|
183 |
def add_rainfall_plot(self, fig, df):
|
184 |
+
"""Add rainfall visualization"""
|
|
|
185 |
fig.add_trace(
|
186 |
go.Bar(
|
187 |
x=df['date'],
|
|
|
193 |
row=3, col=1
|
194 |
)
|
195 |
|
196 |
+
if 'rainfall_7day_avg' in df.columns:
|
197 |
+
fig.add_trace(
|
198 |
+
go.Scatter(
|
199 |
+
x=df['date'],
|
200 |
+
y=df['rainfall_7day_avg'],
|
201 |
+
name='7-Day Average',
|
202 |
+
line=dict(color='blue', width=2),
|
203 |
+
mode='lines'
|
204 |
+
),
|
205 |
+
row=3, col=1
|
206 |
+
)
|
207 |
|
208 |
+
def add_combined_indices_plot(self, fig, df):
|
209 |
+
"""Add vegetation and suitability indices visualization"""
|
210 |
+
if 'estimated_ndvi' in df.columns:
|
211 |
+
fig.add_trace(
|
212 |
+
go.Scatter(
|
213 |
+
x=df['date'],
|
214 |
+
y=df['estimated_ndvi'],
|
215 |
+
name='Vegetation Index',
|
216 |
+
line=dict(color='green', width=2),
|
217 |
+
mode='lines'
|
218 |
+
),
|
219 |
+
row=4, col=1
|
220 |
+
)
|
221 |
|
222 |
+
if 'daily_suitability' in df.columns:
|
223 |
+
fig.add_trace(
|
224 |
+
go.Scatter(
|
225 |
+
x=df['date'],
|
226 |
+
y=df['daily_suitability'],
|
227 |
+
name='Growing Suitability',
|
228 |
+
line=dict(color='purple', width=2),
|
229 |
+
mode='lines'
|
230 |
+
),
|
231 |
+
row=4, col=1
|
232 |
+
)
|
233 |
|
234 |
def add_season_shading(self, fig, df):
|
235 |
+
"""Add season shading to all plots"""
|
236 |
+
if 'season' in df.columns:
|
237 |
+
seasons = df['season'].unique()
|
238 |
+
season_colors = {
|
239 |
+
'Main': 'rgba(0,255,0,0.1)', # Green
|
240 |
+
'Early': 'rgba(255,255,0,0.1)', # Yellow
|
241 |
+
'Late': 'rgba(255,165,0,0.1)', # Orange
|
242 |
+
'Dry': 'rgba(255,0,0,0.1)' # Red
|
243 |
+
}
|
244 |
+
|
245 |
+
for season in seasons:
|
246 |
+
season_data = df[df['season'] == season]
|
247 |
+
if not season_data.empty:
|
248 |
+
for row in range(1, 5):
|
249 |
+
fig.add_vrect(
|
250 |
+
x0=season_data['date'].iloc[0],
|
251 |
+
x1=season_data['date'].iloc[-1],
|
252 |
+
fillcolor=season_colors.get(season, 'rgba(128,128,128,0.1)'),
|
253 |
+
layer="below",
|
254 |
+
line_width=0,
|
255 |
+
annotation_text=season if row == 1 else None,
|
256 |
+
annotation_position="top left",
|
257 |
+
row=row, col=1
|
258 |
+
)
|
259 |
|
260 |
def create_enhanced_map(self, lat, lon, score, ndvi_value):
|
261 |
+
"""Create an interactive map with analysis overlays"""
|
262 |
m = folium.Map(location=[lat, lon], zoom_start=13)
|
263 |
|
264 |
# Add measurement tools
|