File size: 14,590 Bytes
dc128e4
7ca901a
dc128e4
7ca901a
dc128e4
 
7ca901a
dc128e4
 
7ca901a
dc128e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ca901a
dc128e4
 
7ca901a
5ddad7c
dc128e4
 
5ddad7c
 
 
 
 
 
 
 
 
dc128e4
 
 
 
8b09855
dc128e4
8b09855
 
dc128e4
 
8b09855
 
 
 
dc128e4
 
 
 
8b09855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc128e4
 
8b09855
7ca901a
8b09855
 
 
 
 
 
 
 
 
 
 
 
 
 
7ca901a
dc128e4
8b09855
 
 
dc128e4
 
 
 
 
8b09855
 
 
 
7ca901a
dc128e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ca901a
dc128e4
 
 
 
 
 
 
 
 
 
7ca901a
dc128e4
 
7ca901a
dc128e4
 
 
 
 
 
 
 
 
 
7ca901a
dc128e4
7ca901a
dc128e4
 
 
 
 
 
 
 
7ca901a
dc128e4
 
 
 
 
 
7ca901a
dc128e4
7ca901a
dc128e4
 
7ca901a
dc128e4
 
 
 
 
 
7ca901a
dc128e4
 
 
 
7ca901a
dc128e4
 
 
 
7ca901a
dc128e4
 
 
 
7ca901a
dc128e4
 
 
 
 
 
 
 
 
 
 
7ca901a
8b09855
 
dc128e4
8b09855
 
 
dc128e4
 
 
 
 
 
 
 
 
 
 
 
8b09855
dc128e4
8b09855
 
5ddad7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b09855
 
 
 
 
 
 
dc128e4
 
8b09855
 
 
 
dc128e4
8b09855
 
5ddad7c
8b09855
 
7ca901a
dc128e4
 
 
 
 
 
 
 
7ca901a
dc128e4
 
 
 
 
 
 
 
7ca901a
dc128e4
 
 
 
 
 
7ca901a
dc128e4
7ca901a
 
dc128e4
bb831a1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""MCP Server for Agricultural Weed Pressure Analysis"""

import gradio as gr
import pandas as pd
import numpy as np
import plotly.express as px
from data_loader import AgriculturalDataLoader
import warnings
warnings.filterwarnings('ignore')

class WeedPressureAnalyzer:
    """Analyze weed pressure and recommend plots for sensitive crops."""
    
    def __init__(self):
        self.data_loader = AgriculturalDataLoader()
        self.data_cache = None
        
    def load_data(self):
        if self.data_cache is None:
            self.data_cache = self.data_loader.load_all_files()
        return self.data_cache
    
    def calculate_herbicide_ift(self, years=None):
        """Calculate IFT for herbicides by plot and year."""
        df = self.load_data()
        
        if years:
            df = df[df['year'].isin(years)]
        
        herbicide_df = df[df['is_herbicide'] == True].copy()
        
        if len(herbicide_df) == 0:
            return pd.DataFrame()
        
        ift_summary = herbicide_df.groupby(['plot_name', 'year', 'crop_type']).agg({
            'produit': 'count',
            'plot_surface': 'first',
            'quantitetot': 'sum'
        }).reset_index()
        
        ift_summary['ift_herbicide'] = ift_summary['produit'] / ift_summary['plot_surface']
        
        return ift_summary
    
    def predict_weed_pressure(self, target_years=[2025, 2026, 2027]):
        """Predict weed pressure for future years."""
        ift_data = self.calculate_herbicide_ift()
        
        if len(ift_data) == 0:
            return pd.DataFrame()
        
        predictions = []
        
        for plot in ift_data['plot_name'].unique():
            plot_data = ift_data[ift_data['plot_name'] == plot].sort_values('year')
            
            if len(plot_data) < 2:
                continue
                
            years = plot_data['year'].values
            ift_values = plot_data['ift_herbicide'].values
            
            if len(years) > 1:
                slope = np.polyfit(years, ift_values, 1)[0]
                intercept = np.polyfit(years, ift_values, 1)[1]
                
                for target_year in target_years:
                    predicted_ift = slope * target_year + intercept
                    predicted_ift = max(0, predicted_ift)
                    
                    if predicted_ift < 1.0:
                        risk_level = "Faible"
                    elif predicted_ift < 2.0:
                        risk_level = "Modéré"
                    else:
                        risk_level = "Élevé"
                    
                    predictions.append({
                        'plot_name': plot,
                        'year': target_year,
                        'predicted_ift': predicted_ift,
                        'risk_level': risk_level,
                        'recent_crops': ', '.join(plot_data['crop_type'].tail(3).unique()),
                        'historical_avg_ift': plot_data['ift_herbicide'].mean()
                    })
        
        return pd.DataFrame(predictions)

# Initialize analyzer
analyzer = WeedPressureAnalyzer()

def analyze_herbicide_trends(year_start, year_end, plot_filter):
    """Analyze herbicide usage trends over time."""
    try:
        # Créer la liste des années à partir des deux sliders
        start_year = int(year_start)
        end_year = int(year_end)
        
        # S'assurer que start <= end
        if start_year > end_year:
            start_year, end_year = end_year, start_year
            
        years = list(range(start_year, end_year + 1))
        
        ift_data = analyzer.calculate_herbicide_ift(years=years)
        
        if len(ift_data) == 0:
            return None, "Aucune donnée d'herbicides trouvée pour la période sélectionnée."
        
        # Filtrage par parcelle si nécessaire
        if plot_filter and plot_filter != "Toutes":
            ift_data = ift_data[ift_data['plot_name'] == plot_filter]
        
        if len(ift_data) == 0:
            return None, f"Aucune donnée trouvée pour la parcelle '{plot_filter}' sur la période {years[0]}-{years[-1]}."
        
        # Création du graphique
        fig = px.line(ift_data, 
                     x='year', 
                     y='ift_herbicide',
                     color='plot_name',
                     title=f'Évolution de l\'IFT Herbicides ({years[0]}-{years[-1]})',
                     labels={'ift_herbicide': 'IFT Herbicides', 'year': 'Année'},
                     markers=True)
        
        fig.update_layout(
            height=500,
            xaxis_title="Année",
            yaxis_title="IFT Herbicides",
            legend_title="Parcelle"
        )
        
        # Ajout d'une ligne de référence IFT = 2.0
        fig.add_hline(y=2.0, line_dash="dash", line_color="red", 
                     annotation_text="Seuil IFT élevé (2.0)", annotation_position="top right")
        fig.add_hline(y=1.0, line_dash="dash", line_color="orange", 
                     annotation_text="Seuil IFT modéré (1.0)", annotation_position="bottom right")
        
        # Calcul des statistiques
        ift_mean = ift_data['ift_herbicide'].mean()
        ift_max = ift_data['ift_herbicide'].max()
        ift_min = ift_data['ift_herbicide'].min()
        n_plots = ift_data['plot_name'].nunique()
        n_records = len(ift_data)
        
        # Classification des niveaux de risque
        low_risk = len(ift_data[ift_data['ift_herbicide'] < 1.0])
        moderate_risk = len(ift_data[(ift_data['ift_herbicide'] >= 1.0) & (ift_data['ift_herbicide'] < 2.0)])
        high_risk = len(ift_data[ift_data['ift_herbicide'] >= 2.0])
        
        summary = f"""
📊 **Analyse de l'IFT Herbicides ({years[0]}-{years[-1]})**

**Période analysée:** {years[0]} à {years[-1]}
**Parcelle(s):** {plot_filter if plot_filter != "Toutes" else "Toutes les parcelles"}

**Statistiques globales:**
- IFT moyen: {ift_mean:.2f}
- IFT minimum: {ift_min:.2f}
- IFT maximum: {ift_max:.2f}
- Nombre de parcelles: {n_plots}
- Nombre d'observations: {n_records}

**Répartition des niveaux de pression:**
- 🟢 Faible (IFT < 1.0): {low_risk} observations ({low_risk/n_records*100:.1f}%)
- 🟡 Modérée (1.0 ≤ IFT < 2.0): {moderate_risk} observations ({moderate_risk/n_records*100:.1f}%)
- 🔴 Élevée (IFT ≥ 2.0): {high_risk} observations ({high_risk/n_records*100:.1f}%)

**Interprétation:**
- IFT < 1.0: Pression adventices faible ✅
- 1.0 ≤ IFT < 2.0: Pression adventices modérée ⚠️
- IFT ≥ 2.0: Pression adventices élevée ❌
        """
        
        return fig, summary
        
    except Exception as e:
        import traceback
        error_msg = f"Erreur dans l'analyse: {str(e)}\n{traceback.format_exc()}"
        print(error_msg)
        return None, error_msg

def predict_future_weed_pressure():
    """Predict weed pressure for the next 3 years."""
    try:
        predictions = analyzer.predict_weed_pressure()
        
        if len(predictions) == 0:
            return None, "Impossible de générer des prédictions."
        
        fig = px.bar(predictions, 
                    x='plot_name', 
                    y='predicted_ift',
                    color='risk_level',
                    facet_col='year',
                    title='Prédiction Pression Adventices (2025-2027)',
                    color_discrete_map={'Faible': 'green', 'Modéré': 'orange', 'Élevé': 'red'})
        
        low_risk = len(predictions[predictions['risk_level'] == 'Faible'])
        moderate_risk = len(predictions[predictions['risk_level'] == 'Modéré'])
        high_risk = len(predictions[predictions['risk_level'] == 'Élevé'])
        
        summary = f"""
🔮 **Prédictions 2025-2027**

**Répartition des risques:**
- ✅ Risque faible: {low_risk} prédictions
- ⚠️ Risque modéré: {moderate_risk} prédictions  
- ❌ Risque élevé: {high_risk} prédictions
        """
        
        return fig, summary
        
    except Exception as e:
        return None, f"Erreur: {str(e)}"

def recommend_sensitive_crop_plots():
    """Recommend plots for sensitive crops."""
    try:
        predictions = analyzer.predict_weed_pressure()
        
        if len(predictions) == 0:
            return None, "Aucune recommandation disponible."
        
        suitable_plots = predictions[predictions['risk_level'] == "Faible"].copy()
        
        if len(suitable_plots) > 0:
            suitable_plots['recommendation_score'] = 100 - (suitable_plots['predicted_ift'] * 30)
            suitable_plots = suitable_plots.sort_values('recommendation_score', ascending=False)
            
            top_recommendations = suitable_plots.head(10)[['plot_name', 'year', 'predicted_ift', 'recommendation_score']]
            
            summary = f"""
🌱 **Recommandations Cultures Sensibles**

**Top parcelles recommandées:**
{top_recommendations.to_string(index=False)}

**Critères:** IFT prédit < 1.0 (faible pression adventices)
            """
            
            fig = px.scatter(suitable_plots, 
                           x='predicted_ift', 
                           y='recommendation_score',
                           color='year',
                           hover_data=['plot_name'],
                           title='Parcelles Recommandées pour Cultures Sensibles')
            
            return fig, summary
        else:
            return None, "Aucune parcelle à faible risque identifiée."
        
    except Exception as e:
        return None, f"Erreur: {str(e)}"

def generate_technical_alternatives(herbicide_family):
    """Generate technical alternatives."""
    summary = f"""
🔄 **Alternatives aux {herbicide_family}**

**🚜 Alternatives Mécaniques:**
• Faux-semis répétés avant implantation
• Binage mécanique en inter-rang
• Herse étrille en post-levée précoce

**🌾 Alternatives Culturales:**
• Rotation longue avec prairie temporaire
• Cultures intermédiaires piège à nitrates
• Densité de semis optimisée

**🧪 Alternatives Biologiques:**
• Stimulateurs de défenses naturelles
• Extraits végétaux (huiles essentielles)
• Bioherbicides à base de champignons

**📋 Plan d'Action:**
1. Tester sur petites surfaces
2. Former les équipes
3. Suivre l'efficacité
4. Documenter les résultats
    """
    
    return summary

def get_available_plots():
    """Get available plots."""
    try:
        df = analyzer.load_data()
        plots = sorted(df['plot_name'].dropna().unique().tolist())
        return ["Toutes"] + plots
    except Exception as e:
        print(f"Erreur lors du chargement des parcelles: {e}")
        return ["Toutes", "Champ ferme Bas", "Etang Milieu", "Lann Chebot"]

# Create Gradio Interface
def create_mcp_interface():
    with gr.Blocks(title="🚜 Analyse Pression Adventices", theme=gr.themes.Soft()) as demo:
        gr.Markdown("""
        # 🚜 Analyse Pression Adventices - CRA Bretagne
        
        Anticiper et réduire la pression des adventices pour optimiser les cultures sensibles (pois, haricot).
        """)
        
        with gr.Tabs():
            with gr.Tab("📈 Analyse Tendances"):
                gr.Markdown("### Analyser l'évolution de l'IFT herbicides par parcelle et période")
                
                with gr.Row():
                    with gr.Column():
                        with gr.Row():
                            year_start = gr.Slider(
                                minimum=2014, 
                                maximum=2025, 
                                value=2020, 
                                step=1, 
                                label="Année de début"
                            )
                            year_end = gr.Slider(
                                minimum=2014, 
                                maximum=2025, 
                                value=2025, 
                                step=1, 
                                label="Année de fin"
                            )
                        plot_dropdown = gr.Dropdown(
                            choices=get_available_plots(), 
                            value="Toutes", 
                            label="Filtrer par parcelle",
                            info="Choisissez une parcelle spécifique ou toutes"
                        )
                        analyze_btn = gr.Button("🔍 Analyser les Tendances", variant="primary", size="lg")
                
                with gr.Row():
                    with gr.Column(scale=2):
                        trends_plot = gr.Plot(label="Graphique d'évolution")
                    with gr.Column(scale=1):
                        trends_summary = gr.Markdown(label="Résumé statistique")
                
                analyze_btn.click(
                    analyze_herbicide_trends, 
                    inputs=[year_start, year_end, plot_dropdown], 
                    outputs=[trends_plot, trends_summary]
                )
            
            with gr.Tab("🔮 Prédictions"):
                predict_btn = gr.Button("🎯 Prédire 2025-2027", variant="primary")
                
                with gr.Row():
                    predictions_plot = gr.Plot()
                    predictions_summary = gr.Markdown()
                
                predict_btn.click(predict_future_weed_pressure, outputs=[predictions_plot, predictions_summary])
            
            with gr.Tab("🌱 Recommandations"):
                recommend_btn = gr.Button("🎯 Recommander Parcelles", variant="primary")
                
                with gr.Row():
                    recommendations_plot = gr.Plot()
                    recommendations_summary = gr.Markdown()
                
                recommend_btn.click(recommend_sensitive_crop_plots, outputs=[recommendations_plot, recommendations_summary])
            
            with gr.Tab("🔄 Alternatives"):
                herbicide_type = gr.Dropdown(["Herbicides", "Fongicides"], value="Herbicides", label="Type")
                alternatives_btn = gr.Button("💡 Générer Alternatives", variant="primary")
                alternatives_output = gr.Markdown()
                
                alternatives_btn.click(generate_technical_alternatives, [herbicide_type], [alternatives_output])
    
    return demo

if __name__ == "__main__":
    demo = create_mcp_interface()
    demo.launch(server_name="0.0.0.0", server_port=7860, share=True)