Tracy André commited on
Commit
e84dbef
·
1 Parent(s): 1000669
Files changed (1) hide show
  1. app.py +58 -35
app.py CHANGED
@@ -158,43 +158,66 @@ class AgricultureAnalyzer:
158
 
159
  # Groupement des données par parcelle
160
  risk_analysis = self.df.groupby(group_cols).agg(agg_dict).round(2)
161
-
162
- # Quantités d'herbicides spécifiques
163
- herbicide_quantities = self.df[self.df['familleprod'] == 'Herbicides'].groupby(
164
- ['numparcell', 'nomparc', 'libelleusag', 'surfparc'])['quantitetot'].sum().fillna(0)
165
-
166
- risk_analysis['Quantite_herbicides'] = herbicide_quantities.reindex(risk_analysis.index, fill_value=0)
167
-
168
- risk_analysis.columns = ['Nb_herbicides', 'Diversite_evenements', 'Diversite_produits',
169
- 'Quantite_totale', 'Quantite_herbicides']
170
-
171
- # Calcul de l'IFT approximatif
172
- risk_analysis['IFT_herbicide_approx'] = (risk_analysis['Quantite_herbicides'] /
173
- risk_analysis.index.get_level_values('surfparc')).round(2)
174
-
175
- # Classification du risque
176
- def classify_risk(row):
177
- ift = row['IFT_herbicide_approx']
178
- nb_herb = row['Nb_herbicides']
179
 
180
- if ift == 0 and nb_herb == 0:
181
- return 'TRÈS FAIBLE'
182
- elif ift < 1 and nb_herb <= 1:
183
- return 'FAIBLE'
184
- elif ift < 3 and nb_herb <= 3:
185
- return 'MODÉRÉ'
186
- elif ift < 5 and nb_herb <= 5:
187
- return 'ÉLEVÉ'
188
  else:
189
- return 'TRÈS ÉLEVÉ'
190
-
191
- risk_analysis['Risque_adventice'] = risk_analysis.apply(classify_risk, axis=1)
192
-
193
- # Tri par risque
194
- risk_order = ['TRÈS FAIBLE', 'FAIBLE', 'MODÉRÉ', 'ÉLEVÉ', 'TRÈS ÉLEVÉ']
195
- risk_analysis['Risk_Score'] = risk_analysis['Risque_adventice'].map({r: i for i, r in enumerate(risk_order)})
196
-
197
- self.risk_analysis = risk_analysis.sort_values(['Risk_Score', 'IFT_herbicide_approx'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
  def get_summary_stats(self):
200
  """Retourne les statistiques de résumé"""
 
158
 
159
  # Groupement des données par parcelle
160
  risk_analysis = self.df.groupby(group_cols).agg(agg_dict).round(2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
 
162
+ # Quantités d'herbicides spécifiques (seulement si les colonnes existent)
163
+ if 'familleprod' in self.df.columns and 'quantitetot' in self.df.columns:
164
+ herbicides_df = self.df[self.df['familleprod'] == 'Herbicides']
165
+ if len(herbicides_df) > 0:
166
+ herbicide_quantities = herbicides_df.groupby(group_cols)['quantitetot'].sum().fillna(0)
167
+ risk_analysis['Quantite_herbicides'] = herbicide_quantities.reindex(risk_analysis.index, fill_value=0)
168
+ else:
169
+ risk_analysis['Quantite_herbicides'] = 0
170
  else:
171
+ risk_analysis['Quantite_herbicides'] = 0
172
+
173
+ # Renommer les colonnes de façon sécurisée
174
+ new_column_names = {}
175
+ if 'familleprod' in agg_dict:
176
+ new_column_names['familleprod'] = 'Nb_herbicides'
177
+ if 'libevenem' in agg_dict:
178
+ new_column_names['libevenem'] = 'Diversite_evenements'
179
+ if 'produit' in agg_dict:
180
+ new_column_names['produit'] = 'Diversite_produits'
181
+ if 'quantitetot' in agg_dict:
182
+ new_column_names['quantitetot'] = 'Quantite_totale'
183
+
184
+ risk_analysis = risk_analysis.rename(columns=new_column_names)
185
+
186
+ # Calcul de l'IFT approximatif
187
+ if 'surfparc' in group_cols:
188
+ risk_analysis['IFT_herbicide_approx'] = (risk_analysis['Quantite_herbicides'] /
189
+ risk_analysis.index.get_level_values('surfparc')).round(2)
190
+ else:
191
+ risk_analysis['IFT_herbicide_approx'] = 0
192
+
193
+ # Classification du risque
194
+ def classify_risk(row):
195
+ ift = row.get('IFT_herbicide_approx', 0)
196
+ nb_herb = row.get('Nb_herbicides', 0)
197
+
198
+ if ift == 0 and nb_herb == 0:
199
+ return 'TRÈS FAIBLE'
200
+ elif ift < 1 and nb_herb <= 1:
201
+ return 'FAIBLE'
202
+ elif ift < 3 and nb_herb <= 3:
203
+ return 'MODÉRÉ'
204
+ elif ift < 5 and nb_herb <= 5:
205
+ return 'ÉLEVÉ'
206
+ else:
207
+ return 'TRÈS ÉLEVÉ'
208
+
209
+ risk_analysis['Risque_adventice'] = risk_analysis.apply(classify_risk, axis=1)
210
+
211
+ # Tri par risque
212
+ risk_order = ['TRÈS FAIBLE', 'FAIBLE', 'MODÉRÉ', 'ÉLEVÉ', 'TRÈS ÉLEVÉ']
213
+ risk_analysis['Risk_Score'] = risk_analysis['Risque_adventice'].map({r: i for i, r in enumerate(risk_order)})
214
+
215
+ self.risk_analysis = risk_analysis.sort_values(['Risk_Score', 'IFT_herbicide_approx'])
216
+ print(f"✅ Analyse des risques terminée: {len(self.risk_analysis)} parcelles analysées")
217
+
218
+ except Exception as e:
219
+ print(f"❌ Erreur lors du calcul des risques: {str(e)}")
220
+ self.risk_analysis = pd.DataFrame()
221
 
222
  def get_summary_stats(self):
223
  """Retourne les statistiques de résumé"""