cheikhaidev-ui Claude Sonnet 4.6 commited on
Commit ·
9a0b5bd
1
Parent(s): b37f222
refactor(map): extract compute_map_risks() from tab2 inline logic
Browse filesMove vectorized batch prediction for all quartiers into a reusable
compute_map_risks() function in utils_simple.py and replace the
18-line inline block in tab2 with a single call.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- streamlit_app/app.py +3 -28
- streamlit_app/utils_simple.py +28 -0
streamlit_app/app.py
CHANGED
|
@@ -16,6 +16,7 @@ from streamlit_app.utils_simple import (
|
|
| 16 |
load_models_cached,
|
| 17 |
create_time_features,
|
| 18 |
make_prediction_single,
|
|
|
|
| 19 |
create_gauge_chart,
|
| 20 |
create_map,
|
| 21 |
create_bar_chart_quartiers,
|
|
@@ -169,37 +170,11 @@ with tab1:
|
|
| 169 |
with tab2:
|
| 170 |
st.header("🗺️ Carte Interactive")
|
| 171 |
|
| 172 |
-
# Calcul automatique basé sur les paramètres actuels
|
| 173 |
time_features_map = create_time_features(datetime.now(), hour_override=heure_pred)
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
scaler = models['scaler']
|
| 177 |
-
|
| 178 |
-
if lgb_model is None or scaler is None:
|
| 179 |
st.error("Modèles non chargés — impossible de calculer la carte.")
|
| 180 |
else:
|
| 181 |
-
feature_row = [
|
| 182 |
-
temperature, humidite, vitesse_vent, consommation,
|
| 183 |
-
time_features_map['hour'], time_features_map['day_of_week'],
|
| 184 |
-
time_features_map['month'], time_features_map['saison'],
|
| 185 |
-
time_features_map['is_peak_hour'],
|
| 186 |
-
]
|
| 187 |
-
X = np.tile(feature_row, (len(QUARTIERS_DAKAR), 1)) # (8, 9)
|
| 188 |
-
X_scaled = scaler.transform(X) # une seule passe
|
| 189 |
-
preds_lgb = lgb_model.predict(X_scaled) * 100 # (8,) en un appel
|
| 190 |
-
if lstm_model is not None:
|
| 191 |
-
X_lstm = X_scaled.reshape(len(QUARTIERS_DAKAR), 1, -1) # (8, 1, 9)
|
| 192 |
-
preds_lstm = lstm_model.predict(X_lstm, verbose=0).flatten() * 100
|
| 193 |
-
else:
|
| 194 |
-
preds_lstm = preds_lgb.copy()
|
| 195 |
-
risque_base = (preds_lgb + preds_lstm) / 2
|
| 196 |
-
results = [
|
| 197 |
-
{
|
| 198 |
-
'Quartier': q,
|
| 199 |
-
'Risque': float(np.clip(max(5.0, risque_base[i] * QUARTIER_ADJUSTMENT.get(q, 1.0)), 0, 100)),
|
| 200 |
-
}
|
| 201 |
-
for i, q in enumerate(QUARTIERS_DAKAR)
|
| 202 |
-
]
|
| 203 |
st.session_state['map_results'] = results
|
| 204 |
fig = create_map(results)
|
| 205 |
st.plotly_chart(fig, use_container_width=True, config={'scrollZoom': True})
|
|
|
|
| 16 |
load_models_cached,
|
| 17 |
create_time_features,
|
| 18 |
make_prediction_single,
|
| 19 |
+
compute_map_risks,
|
| 20 |
create_gauge_chart,
|
| 21 |
create_map,
|
| 22 |
create_bar_chart_quartiers,
|
|
|
|
| 170 |
with tab2:
|
| 171 |
st.header("🗺️ Carte Interactive")
|
| 172 |
|
|
|
|
| 173 |
time_features_map = create_time_features(datetime.now(), hour_override=heure_pred)
|
| 174 |
+
results = compute_map_risks(models, temperature, humidite, vitesse_vent, consommation, time_features_map)
|
| 175 |
+
if results is None:
|
|
|
|
|
|
|
|
|
|
| 176 |
st.error("Modèles non chargés — impossible de calculer la carte.")
|
| 177 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
st.session_state['map_results'] = results
|
| 179 |
fig = create_map(results)
|
| 180 |
st.plotly_chart(fig, use_container_width=True, config={'scrollZoom': True})
|
streamlit_app/utils_simple.py
CHANGED
|
@@ -102,6 +102,34 @@ def make_prediction_single(models, quartier, temp, humidite, vent, conso, time_f
|
|
| 102 |
st.error(f"❌ Erreur: {e}")
|
| 103 |
return None
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
def get_risk_color(risque_pct):
|
| 106 |
if risque_pct < 40:
|
| 107 |
return "#2ecc71"
|
|
|
|
| 102 |
st.error(f"❌ Erreur: {e}")
|
| 103 |
return None
|
| 104 |
|
| 105 |
+
def compute_map_risks(models, temperature, humidite, vitesse_vent, consommation, time_features):
|
| 106 |
+
lgb_model = models['lgb']
|
| 107 |
+
lstm_model = models['lstm']
|
| 108 |
+
scaler = models['scaler']
|
| 109 |
+
if lgb_model is None or scaler is None:
|
| 110 |
+
return None
|
| 111 |
+
feature_row = [
|
| 112 |
+
temperature, humidite, vitesse_vent, consommation,
|
| 113 |
+
time_features['hour'], time_features['day_of_week'],
|
| 114 |
+
time_features['month'], time_features['saison'],
|
| 115 |
+
time_features['is_peak_hour'],
|
| 116 |
+
]
|
| 117 |
+
X = np.tile(feature_row, (len(QUARTIERS_DAKAR), 1))
|
| 118 |
+
X_scaled = scaler.transform(X)
|
| 119 |
+
preds_lgb = lgb_model.predict(X_scaled) * 100
|
| 120 |
+
if lstm_model is not None:
|
| 121 |
+
preds_lstm = lstm_model.predict(X_scaled.reshape(len(QUARTIERS_DAKAR), 1, -1), verbose=0).flatten() * 100
|
| 122 |
+
else:
|
| 123 |
+
preds_lstm = preds_lgb.copy()
|
| 124 |
+
risque_base = (preds_lgb + preds_lstm) / 2
|
| 125 |
+
return [
|
| 126 |
+
{
|
| 127 |
+
'Quartier': q,
|
| 128 |
+
'Risque': float(np.clip(max(5.0, risque_base[i] * QUARTIER_ADJUSTMENT.get(q, 1.0)), 0, 100)),
|
| 129 |
+
}
|
| 130 |
+
for i, q in enumerate(QUARTIERS_DAKAR)
|
| 131 |
+
]
|
| 132 |
+
|
| 133 |
def get_risk_color(risque_pct):
|
| 134 |
if risque_pct < 40:
|
| 135 |
return "#2ecc71"
|