-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmain.py
More file actions
431 lines (339 loc) · 15.9 KB
/
Copy pathmain.py
File metadata and controls
431 lines (339 loc) · 15.9 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"""
AgriQuant AI - Main Orchestration System
Coordinates weather monitoring, AI analysis, and prediction generation
"""
import time
import schedule
from datetime import datetime, timedelta
from typing import Dict, List
import logging
import json
import uuid
from config import *
from weather_collector import NOAAWeatherCollector
from claude_engine import ClaudeAnalysisEngine
from database import AgriQuant AIDatabase
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class AgriQuant AIOrchestrator:
"""
Main system coordinator
Runs continuous monitoring and generates predictions
"""
def __init__(self):
self.weather_collector = NOAAWeatherCollector()
self.ai_engine = ClaudeAnalysisEngine()
self.database = AgriQuant AIDatabase()
self.deployment_phase = DEPLOYMENT_PHASE
self.last_forecast_time = {} # Track last forecast for each county
self.active_predictions = [] # Currently active predictions
logger.info(f"AgriQuant AI Orchestrator initialized - {self.deployment_phase} mode")
def initialize(self):
"""Initialize system components"""
logger.info("Initializing AgriQuant AI system...")
# Connect to database
self.database.connect()
# Create tables if they don't exist
self.database.create_tables()
# Seed with historical data if empty
logger.info("System initialization complete")
def shutdown(self):
"""Graceful shutdown"""
logger.info("Shutting down AgriQuant AI system...")
self.database.disconnect()
logger.info("Shutdown complete")
def monitor_weather_all_counties(self) -> Dict:
"""
Main monitoring loop - check all counties for weather threats
"""
logger.info("="*80)
logger.info(f"WEATHER MONITORING CYCLE - {datetime.utcnow().isoformat()}")
logger.info("="*80)
results = {
'timestamp': datetime.utcnow().isoformat(),
'counties_monitored': [],
'predictions_generated': [],
'alerts': []
}
for county_name in CITRUS_COUNTIES.keys():
try:
logger.info(f"\n--- Monitoring {county_name} County ---")
# Get comprehensive forecast
forecast = self.weather_collector.get_ensemble_forecast(county_name)
if not forecast:
logger.warning(f"Failed to get forecast for {county_name}")
continue
# Store forecast in database
forecast_id = self.database.store_weather_forecast(forecast['primary_forecast'])
results['counties_monitored'].append({
'county': county_name,
'forecast_id': forecast_id,
'freeze_risk': forecast['freeze_risk']['risk']
})
# Check if action needed
freeze_risk = forecast['freeze_risk']
if freeze_risk['risk'] != 'none':
logger.info(f"⚠️ FREEZE RISK DETECTED: {freeze_risk['risk']}")
# Check confidence threshold
model_agreement = forecast.get('model_agreement', {}).get('agreement_score', 0)
if model_agreement >= MIN_MODEL_AGREEMENT:
logger.info(f"✓ Model agreement sufficient: {model_agreement*100:.0f}%")
# Generate AI prediction
prediction = self._generate_and_store_prediction(forecast, county_name)
if prediction:
results['predictions_generated'].append(prediction)
# Check if high confidence - send alert
if prediction['confidence_score'] >= ALERT_THRESHOLDS['prediction_confidence']:
alert = self._create_alert(prediction)
results['alerts'].append(alert)
else:
logger.info(f"✗ Model agreement too low: {model_agreement*100:.0f}% < {MIN_MODEL_AGREEMENT*100:.0f}%")
# Hurricane check
hurricane_threat = self.weather_collector.detect_hurricane_threat(county_name)
if hurricane_threat:
logger.info(f"🌀 HURRICANE THREAT: {len(hurricane_threat)} active storms")
# Would trigger hurricane analysis here
# Small delay to be respectful to APIs
time.sleep(2)
except Exception as e:
logger.error(f"Error monitoring {county_name}: {e}")
continue
# Summary
logger.info("\n" + "="*80)
logger.info("MONITORING CYCLE COMPLETE")
logger.info(f"Counties monitored: {len(results['counties_monitored'])}")
logger.info(f"Predictions generated: {len(results['predictions_generated'])}")
logger.info(f"Alerts triggered: {len(results['alerts'])}")
logger.info("="*80 + "\n")
return results
def _generate_and_store_prediction(self, forecast: Dict, county_name: str) -> Dict:
"""
Generate AI prediction and store in database
"""
logger.info(f"Generating AI prediction for {county_name}...")
# Get historical analogs
freeze_details = forecast['freeze_risk'].get('details', {})
min_temp = freeze_details.get('temperature')
historical_analogs = self.database.get_historical_analogs(
event_type='freeze',
temperature=min_temp,
limit=10
)
# Format for Claude
historical_formatted = [
{
'date': str(h['event_date']),
'min_temp': h['min_temperature'],
'duration_hours': h['duration_hours'],
'crop_damage_actual': h['crop_damage_actual'],
'similarity_score': 0.85 # Would calculate actual similarity
}
for h in historical_analogs if h.get('min_temperature')
]
# Generate prediction with Claude
prediction = self.ai_engine.analyze_freeze_event(
forecast['primary_forecast'],
historical_formatted
)
if not prediction:
logger.error("Failed to generate prediction")
return None
# Add unique ID
prediction['prediction_id'] = f"PRED_{county_name}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}_{str(uuid.uuid4())[:8]}"
prediction['county'] = county_name
# Store in database
pred_id = self.database.store_ai_prediction(prediction)
prediction['database_id'] = pred_id
# Add to active predictions
self.active_predictions.append(prediction)
logger.info(f"✓ Prediction stored: {prediction['prediction_id']}")
logger.info(f" Probability: {prediction['probability']*100:.0f}%")
logger.info(f" Expected damage: {prediction['expected_crop_damage_pct']*100:.1f}%")
logger.info(f" Confidence: {prediction['confidence_score']*100:.0f}%")
return prediction
def _create_alert(self, prediction: Dict) -> Dict:
"""
Create high-priority alert for significant predictions
"""
alert = {
'timestamp': datetime.utcnow().isoformat(),
'alert_type': 'HIGH_CONFIDENCE_PREDICTION',
'severity': 'HIGH' if prediction['confidence_score'] >= 0.90 else 'MODERATE',
'prediction_id': prediction['prediction_id'],
'county': prediction['county'],
'event_type': prediction['event_type'],
'expected_damage': prediction['expected_crop_damage_pct'],
'confidence': prediction['confidence_score'],
'message': self._format_alert_message(prediction)
}
logger.info(f"🚨 ALERT GENERATED: {alert['severity']}")
logger.info(f" {alert['message']}")
# In production, would send email/SMS/webhook
self._send_alert(alert)
return alert
def _format_alert_message(self, prediction: Dict) -> str:
"""Format human-readable alert message"""
county = prediction['county']
damage = prediction['expected_crop_damage_pct'] * 100
confidence = prediction['confidence_score'] * 100
timing = prediction.get('timing', {}).get('onset', 'TBD')
message = f"""
HIGH CONFIDENCE WEATHER EVENT PREDICTED
County: {county} County, Florida
Event: {prediction['event_type'].upper()}
Expected Impact: {damage:.1f}% crop damage
Confidence: {confidence:.0f}%
Timing: {timing}
This prediction meets AgriQuant AI's threshold for significant agricultural impact.
Review full analysis and prepare for potential supply disruption.
""".strip()
return message
def _send_alert(self, alert: Dict):
"""
Send alert via configured channels
In production: email, SMS, Slack, webhook
"""
# For now, just log and save to file
alert_filename = f"/tmp/agriquant_alert_{alert['timestamp'].replace(':', '-')}.json"
with open(alert_filename, 'w') as f:
json.dump(alert, f, indent=2)
logger.info(f"Alert saved to: {alert_filename}")
def verify_predictions(self):
"""
Check if any predictions can now be verified against USDA data
This would run daily to check for new USDA reports
"""
logger.info("Checking for predictions ready for verification...")
# Get unverified predictions older than typical USDA lag
cutoff_date = datetime.utcnow() - timedelta(days=14)
unverified = [p for p in self.active_predictions
if not p.get('verified') and
datetime.fromisoformat(p['timestamp']) < cutoff_date]
logger.info(f"Found {len(unverified)} predictions ready for verification")
# In production, would fetch latest USDA reports and match
# For now, simulate verification
for prediction in unverified[:3]: # Limit for demo
# Simulate USDA report data
simulated_actual_damage = prediction['expected_crop_damage_pct'] * (0.9 + 0.2 * (2 * 0.5 - 1)) # ±10% random
success = self.database.verify_prediction(
prediction['prediction_id'],
simulated_actual_damage,
'USDA Simulated Report'
)
if success:
logger.info(f"✓ Verified prediction {prediction['prediction_id']}")
def generate_performance_report(self) -> Dict:
"""
Generate comprehensive performance metrics
"""
logger.info("Generating performance report...")
# Get metrics from database
metrics = self.database.get_prediction_performance()
# Calculate additional metrics
recent_predictions = self.database.get_recent_predictions(days=30)
report = {
'generated_at': datetime.utcnow().isoformat(),
'period': 'Last 30 days',
'overall_metrics': metrics,
'recent_predictions': len(recent_predictions),
'deployment_phase': self.deployment_phase,
'system_status': 'OPERATIONAL'
}
logger.info("Performance Report:")
logger.info(f" Total predictions: {metrics.get('total_predictions', 0)}")
logger.info(f" Verified: {metrics.get('verified_count', 0)}")
logger.info(f" Average accuracy: {metrics.get('avg_accuracy', 0)*100:.1f}%")
logger.info(f" Success rate: {metrics.get('success_rate', 0)*100:.1f}%")
return report
def run_backtest(self, start_date: str, end_date: str):
"""
Run historical backtest
Test prediction accuracy on past data
"""
logger.info(f"Running backtest: {start_date} to {end_date}")
# This would:
# 1. Load historical weather forecasts
# 2. Generate predictions as if in real-time
# 3. Compare to actual USDA-verified outcomes
# 4. Calculate accuracy metrics
logger.info("Backtest functionality - would run comprehensive historical validation")
logger.info("See backtest.py for full implementation")
def run_continuous_monitoring(self):
"""
Run continuous monitoring on schedule
"""
logger.info("Starting continuous monitoring mode...")
# Schedule monitoring every 15 minutes
schedule.every(15).minutes.do(self.monitor_weather_all_counties)
# Verify predictions daily
schedule.every().day.at("09:00").do(self.verify_predictions)
# Generate performance report weekly
schedule.every().monday.at("08:00").do(self.generate_performance_report)
logger.info("Scheduler configured:")
logger.info(" - Weather monitoring: Every 15 minutes")
logger.info(" - Prediction verification: Daily at 9:00 AM")
logger.info(" - Performance report: Weekly on Monday 8:00 AM")
# Run initial monitoring cycle
self.monitor_weather_all_counties()
# Main loop
try:
while True:
schedule.run_pending()
time.sleep(60) # Check every minute
except KeyboardInterrupt:
logger.info("Received shutdown signal")
self.shutdown()
def main():
"""
Main entry point
"""
print("="*80)
print("AgriQuant AI - Weather Prediction System")
print(f"Version: {VERSION}")
print(f"Deployment Phase: {DEPLOYMENT_PHASE}")
print("="*80)
print()
# Initialize orchestrator
orchestrator = AgriQuant AIOrchestrator()
orchestrator.initialize()
# Run mode selection
import sys
if len(sys.argv) > 1:
mode = sys.argv[1]
if mode == "once":
# Run single monitoring cycle
print("Running single monitoring cycle...")
results = orchestrator.monitor_weather_all_counties()
print(f"\nResults: {json.dumps(results, indent=2)}")
orchestrator.shutdown()
elif mode == "continuous":
# Run continuous monitoring
print("Starting continuous monitoring...")
orchestrator.run_continuous_monitoring()
elif mode == "report":
# Generate performance report
print("Generating performance report...")
report = orchestrator.generate_performance_report()
print(json.dumps(report, indent=2))
orchestrator.shutdown()
elif mode == "verify":
# Verify predictions
print("Running prediction verification...")
orchestrator.verify_predictions()
orchestrator.shutdown()
else:
print(f"Unknown mode: {mode}")
print("Available modes: once, continuous, report, verify")
else:
print("Usage: python main.py [mode]")
print("\nModes:")
print(" once - Run single monitoring cycle")
print(" continuous - Run continuous monitoring (production)")
print(" report - Generate performance report")
print(" verify - Verify pending predictions")
print("\nExample: python main.py once")
if __name__ == "__main__":
main()