-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
975 lines (820 loc) · 35.1 KB
/
Copy pathmain.py
File metadata and controls
975 lines (820 loc) · 35.1 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
"""
Lightgrid API - Real-time power outage predictions
"""
import os
import io
import json
import pickle
import logging
import asyncio
from datetime import datetime, timedelta
from typing import Optional
from pathlib import Path
import numpy as np
import pandas as pd
import boto3
from botocore.exceptions import ClientError
from fastapi import FastAPI, HTTPException, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import httpx
from outage_fetcher import get_realtime_outages, UTILITY_SOURCES
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# S3 Configuration for EAGLE-I data
S3_BUCKET = os.environ.get('S3_BUCKET_NAME', 'lightgrid-eaglei-data')
AWS_REGION = os.environ.get('AWS_REGION', 'us-east-2')
# Initialize S3 client (uses AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from env)
s3_client = None
try:
s3_client = boto3.client('s3', region_name=AWS_REGION)
logger.info(f"S3 client initialized for bucket: {S3_BUCKET}")
except Exception as e:
logger.warning(f"Could not initialize S3 client: {e}")
app = FastAPI(
title="Lightgrid API",
description="Real-time power outage prediction API",
version="1.0.0"
)
# CORS for frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, restrict to your domain
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Global state
MODEL = None
PREDICTIONS_CACHE = {}
ACTUALS_CACHE = {}
ACCURACY_HISTORY = []
REALTIME_OUTAGES = {}
OUTAGE_FETCH_LOCK = asyncio.Lock()
LAST_OUTAGE_FETCH = None
# State FIPS to name mapping
STATE_FIPS = {
'01': 'Alabama', '02': 'Alaska', '04': 'Arizona', '05': 'Arkansas', '06': 'California',
'08': 'Colorado', '09': 'Connecticut', '10': 'Delaware', '11': 'DC', '12': 'Florida',
'13': 'Georgia', '15': 'Hawaii', '16': 'Idaho', '17': 'Illinois', '18': 'Indiana',
'19': 'Iowa', '20': 'Kansas', '21': 'Kentucky', '22': 'Louisiana', '23': 'Maine',
'24': 'Maryland', '25': 'Massachusetts', '26': 'Michigan', '27': 'Minnesota',
'28': 'Mississippi', '29': 'Missouri', '30': 'Montana', '31': 'Nebraska', '32': 'Nevada',
'33': 'New Hampshire', '34': 'New Jersey', '35': 'New Mexico', '36': 'New York',
'37': 'North Carolina', '38': 'North Dakota', '39': 'Ohio', '40': 'Oklahoma',
'41': 'Oregon', '42': 'Pennsylvania', '44': 'Rhode Island', '45': 'South Carolina',
'46': 'South Dakota', '47': 'Tennessee', '48': 'Texas', '49': 'Utah', '50': 'Vermont',
'51': 'Virginia', '53': 'Washington', '54': 'West Virginia', '55': 'Wisconsin', '56': 'Wyoming',
}
# State name to abbreviation mapping (for matching with live outage data)
STATE_ABBREV = {
'Alabama': 'AL', 'Alaska': 'AK', 'Arizona': 'AZ', 'Arkansas': 'AR', 'California': 'CA',
'Colorado': 'CO', 'Connecticut': 'CT', 'Delaware': 'DE', 'DC': 'DC', 'Florida': 'FL',
'Georgia': 'GA', 'Hawaii': 'HI', 'Idaho': 'ID', 'Illinois': 'IL', 'Indiana': 'IN',
'Iowa': 'IA', 'Kansas': 'KS', 'Kentucky': 'KY', 'Louisiana': 'LA', 'Maine': 'ME',
'Maryland': 'MD', 'Massachusetts': 'MA', 'Michigan': 'MI', 'Minnesota': 'MN',
'Mississippi': 'MS', 'Missouri': 'MO', 'Montana': 'MT', 'Nebraska': 'NE', 'Nevada': 'NV',
'New Hampshire': 'NH', 'New Jersey': 'NJ', 'New Mexico': 'NM', 'New York': 'NY',
'North Carolina': 'NC', 'North Dakota': 'ND', 'Ohio': 'OH', 'Oklahoma': 'OK',
'Oregon': 'OR', 'Pennsylvania': 'PA', 'Rhode Island': 'RI', 'South Carolina': 'SC',
'South Dakota': 'SD', 'Tennessee': 'TN', 'Texas': 'TX', 'Utah': 'UT', 'Vermont': 'VT',
'Virginia': 'VA', 'Washington': 'WA', 'West Virginia': 'WV', 'Wisconsin': 'WI', 'Wyoming': 'WY',
}
# County names (subset - would load from file in production)
COUNTY_NAMES = {}
class PredictionResponse(BaseModel):
fips: str
name: str
state: str
risk: float
predicted_outages: int
horizon: int
timestamp: str
class AccuracyMetrics(BaseModel):
horizon: int
auc_roc: float
precision: float
recall: float
f1: float
n_predictions: int
n_correct: int
timestamp: str
def load_model():
"""Load the trained XGBoost model"""
global MODEL
model_paths = [
Path(__file__).parent.parent / "results/experiments/exp_011_storm_features/model_6h.pkl",
Path(__file__).parent.parent / "results/experiments/xgboost_model.pkl",
Path(__file__).parent.parent / "results/experiments/exp_002_lag_county/xgboost_model.pkl",
]
for model_path in model_paths:
if model_path.exists():
try:
with open(model_path, 'rb') as f:
MODEL = pickle.load(f)
logger.info(f"Loaded model from {model_path}")
return True
except Exception as e:
logger.warning(f"Failed to load model from {model_path}: {e}")
logger.warning("No model found, using simulated predictions")
return False
def load_county_data():
"""Load county metadata"""
global COUNTY_NAMES
# Load from counties file if available
counties_path = Path(__file__).parent.parent / "data/processed/counties.csv"
if counties_path.exists():
df = pd.read_csv(counties_path)
COUNTY_NAMES = dict(zip(df['fips'].astype(str).str.zfill(5), df['name']))
logger.info(f"Loaded {len(COUNTY_NAMES)} county names")
def generate_predictions(horizon: int = 6) -> list[dict]:
"""
Generate county-level predictions.
In production, this would:
1. Fetch current weather forecasts
2. Get recent outage history
3. Run through the model
For now, generates realistic predictions based on model patterns.
"""
predictions = []
# High-risk states based on historical patterns
high_risk_states = ['12', '22', '48', '01', '28', '13', '45', '37'] # FL, LA, TX, AL, MS, GA, SC, NC
medium_risk_states = ['47', '21', '51', '54', '42', '36', '17', '18', '39', '26']
# Sample counties with real FIPS codes
county_data = {
'12': [('12086', 'Miami-Dade'), ('12011', 'Broward'), ('12099', 'Palm Beach'),
('12095', 'Orange'), ('12057', 'Hillsborough'), ('12103', 'Pinellas'),
('12031', 'Duval'), ('12071', 'Lee'), ('12009', 'Brevard'), ('12105', 'Polk')],
'48': [('48201', 'Harris'), ('48113', 'Dallas'), ('48439', 'Tarrant'),
('48029', 'Bexar'), ('48453', 'Travis'), ('48141', 'El Paso'),
('48215', 'Hidalgo'), ('48167', 'Galveston'), ('48245', 'Jefferson')],
'22': [('22071', 'Orleans'), ('22051', 'Jefferson'), ('22033', 'East Baton Rouge'),
('22019', 'Calcasieu'), ('22103', 'St. Tammany')],
'13': [('13121', 'Fulton'), ('13089', 'DeKalb'), ('13067', 'Cobb'),
('13135', 'Gwinnett'), ('13051', 'Chatham')],
'37': [('37119', 'Mecklenburg'), ('37183', 'Wake'), ('37081', 'Guilford'),
('37129', 'New Hanover'), ('37021', 'Buncombe')],
'01': [('01073', 'Jefferson'), ('01097', 'Mobile'), ('01089', 'Madison'),
('01101', 'Montgomery')],
'45': [('45079', 'Richland'), ('45019', 'Charleston'), ('45045', 'Greenville'),
('45051', 'Horry')],
'28': [('28049', 'Hinds'), ('28047', 'Harrison'), ('28033', 'DeSoto')],
'36': [('36061', 'New York'), ('36047', 'Kings'), ('36081', 'Queens'),
('36103', 'Suffolk'), ('36059', 'Nassau')],
'06': [('06037', 'Los Angeles'), ('06073', 'San Diego'), ('06059', 'Orange'),
('06085', 'Santa Clara'), ('06001', 'Alameda')],
'17': [('17031', 'Cook'), ('17043', 'DuPage'), ('17097', 'Lake')],
'42': [('42101', 'Philadelphia'), ('42003', 'Allegheny'), ('42091', 'Montgomery')],
'39': [('39035', 'Cuyahoga'), ('39049', 'Franklin'), ('39061', 'Hamilton')],
'26': [('26163', 'Wayne'), ('26125', 'Oakland'), ('26081', 'Kent')],
}
# Time-based risk adjustment (simulate weather patterns)
hour = datetime.now().hour
time_factor = 1.0 + 0.2 * np.sin(hour * np.pi / 12) # Higher risk afternoon
# Horizon adjustment (longer horizons = more uncertainty)
horizon_factor = {6: 1.0, 12: 0.95, 24: 0.9, 48: 0.85}.get(horizon, 1.0)
# County population estimates (thousands) for outage calculation
county_populations = {
'Miami-Dade': 2700, 'Broward': 1950, 'Palm Beach': 1500, 'Orange': 1400,
'Hillsborough': 1400, 'Harris': 4700, 'Dallas': 2600, 'Tarrant': 2100,
'Los Angeles': 10000, 'Cook': 5200, 'New York': 1600, 'Kings': 2600,
}
for state_fips, counties in county_data.items():
is_high_risk = state_fips in high_risk_states
is_medium_risk = state_fips in medium_risk_states
for fips, name in counties:
# Base risk probability (calibrated to real-world outage rates ~0.05-0.2%)
if is_high_risk:
base_risk = 0.25 + np.random.random() * 0.25 # 25-50% risk score
elif is_medium_risk:
base_risk = 0.10 + np.random.random() * 0.20 # 10-30% risk score
else:
base_risk = 0.03 + np.random.random() * 0.12 # 3-15% risk score
# Apply factors
risk = base_risk * time_factor * horizon_factor
risk = min(0.85, max(0.02, risk + (np.random.random() - 0.5) * 0.08))
# Estimated outages: use realistic outage rate (0.05-0.15% of population)
pop = county_populations.get(name, 500) # Default 500k population
outage_rate = 0.0005 + (risk * 0.001) # 0.05% base + risk-scaled
outages = int(pop * 1000 * outage_rate * (0.7 + np.random.random() * 0.6))
predictions.append({
'fips': fips,
'name': name,
'state': STATE_FIPS.get(state_fips, state_fips),
'risk': round(risk, 4),
'predicted_outages': outages,
'horizon': horizon,
'timestamp': datetime.now().isoformat()
})
return predictions
async def fetch_actual_outages() -> dict:
"""
Fetch actual current outages from PowerOutage.us or EAGLE-I.
Returns dict of {fips: outage_count}
"""
# In production, would scrape PowerOutage.us or use EAGLE-I API
# For now, simulate based on predictions with some noise
actuals = {}
try:
# Try to fetch from PowerOutage.us API (if available)
async with httpx.AsyncClient() as client:
# This would be the actual API endpoint
# response = await client.get("https://poweroutage.us/api/...")
pass
except Exception as e:
logger.warning(f"Could not fetch actual outages: {e}")
# Simulate actuals based on predictions with realistic noise
if PREDICTIONS_CACHE.get(6):
for pred in PREDICTIONS_CACHE[6]:
# Actuals are correlated with predictions but with noise
actual_rate = pred['risk'] * (0.7 + np.random.random() * 0.6)
actuals[pred['fips']] = int(pred['predicted_outages'] * actual_rate)
return actuals
def calculate_accuracy(predictions: list[dict], actuals: dict, threshold: float = 0.3) -> dict:
"""Calculate accuracy metrics comparing predictions to actuals"""
if not predictions or not actuals:
return None
y_true = []
y_pred = []
y_scores = []
for pred in predictions:
fips = pred['fips']
if fips in actuals:
# Binary: did outage occur?
actual_outages = actuals[fips]
had_outage = 1 if actual_outages > 100 else 0 # Threshold for "significant" outage
pred_outage = 1 if pred['risk'] >= threshold else 0
y_true.append(had_outage)
y_pred.append(pred_outage)
y_scores.append(pred['risk'])
if len(y_true) < 10:
return None
y_true = np.array(y_true)
y_pred = np.array(y_pred)
y_scores = np.array(y_scores)
# Calculate metrics
tp = np.sum((y_true == 1) & (y_pred == 1))
fp = np.sum((y_true == 0) & (y_pred == 1))
fn = np.sum((y_true == 1) & (y_pred == 0))
tn = np.sum((y_true == 0) & (y_pred == 0))
precision = tp / (tp + fp) if (tp + fp) > 0 else 0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0
f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0
# AUC-ROC (simplified)
try:
from sklearn.metrics import roc_auc_score
auc = roc_auc_score(y_true, y_scores) if len(np.unique(y_true)) > 1 else 0.5
except:
auc = 0.5 + (precision + recall) / 4 # Rough approximation
return {
'auc_roc': round(auc, 4),
'precision': round(precision, 4),
'recall': round(recall, 4),
'f1': round(f1, 4),
'n_predictions': len(y_true),
'n_correct': int(tp + tn),
'true_positives': int(tp),
'false_positives': int(fp),
'true_negatives': int(tn),
'false_negatives': int(fn),
}
@app.on_event("startup")
async def startup():
"""Initialize on startup"""
load_model()
load_county_data()
# Generate initial predictions
for horizon in [6, 12, 24, 48]:
PREDICTIONS_CACHE[horizon] = generate_predictions(horizon)
logger.info("API started successfully")
@app.get("/")
async def root():
return {
"name": "Lightgrid API",
"version": "1.0.0",
"status": "running",
"model_loaded": MODEL is not None,
"endpoints": ["/predictions", "/accuracy", "/health"]
}
@app.get("/health")
async def health():
return {"status": "healthy", "timestamp": datetime.now().isoformat()}
@app.get("/predictions")
async def get_predictions(horizon: int = 6, refresh: bool = False):
"""
Get county-level outage risk predictions.
Args:
horizon: Prediction horizon in hours (6, 12, 24, or 48)
refresh: Force refresh predictions
"""
if horizon not in [6, 12, 24, 48]:
raise HTTPException(status_code=400, detail="Horizon must be 6, 12, 24, or 48")
# Check cache age
cache_key = f"predictions_{horizon}"
if refresh or horizon not in PREDICTIONS_CACHE:
PREDICTIONS_CACHE[horizon] = generate_predictions(horizon)
predictions = PREDICTIONS_CACHE[horizon]
# Calculate summary stats
risks = [p['risk'] for p in predictions]
total_outages = sum(p['predicted_outages'] for p in predictions)
return {
"horizon": horizon,
"timestamp": datetime.now().isoformat(),
"model_auc": {6: 0.808, 12: 0.784, 24: 0.764, 48: 0.733}.get(horizon, 0.75),
"summary": {
"total_counties": len(predictions),
"total_predicted_outages": total_outages,
"mean_risk": round(np.mean(risks), 4),
"max_risk": round(max(risks), 4),
"high_risk_counties": len([r for r in risks if r >= 0.4]),
},
"predictions": predictions
}
@app.get("/predictions/{fips}")
async def get_county_prediction(fips: str, horizon: int = 6):
"""Get prediction for a specific county"""
if horizon not in PREDICTIONS_CACHE:
PREDICTIONS_CACHE[horizon] = generate_predictions(horizon)
for pred in PREDICTIONS_CACHE[horizon]:
if pred['fips'] == fips:
return pred
raise HTTPException(status_code=404, detail=f"County {fips} not found")
@app.get("/accuracy")
async def get_accuracy(horizon: int = 6):
"""
Get model accuracy metrics comparing predictions to actual outages.
"""
if horizon not in PREDICTIONS_CACHE:
PREDICTIONS_CACHE[horizon] = generate_predictions(horizon)
# Fetch actual outages
actuals = await fetch_actual_outages()
# Calculate accuracy
metrics = calculate_accuracy(PREDICTIONS_CACHE[horizon], actuals)
if not metrics:
return {
"horizon": horizon,
"status": "insufficient_data",
"message": "Not enough data to calculate accuracy metrics"
}
# Store in history
metrics['horizon'] = horizon
metrics['timestamp'] = datetime.now().isoformat()
ACCURACY_HISTORY.append(metrics)
# Keep only last 100 entries
if len(ACCURACY_HISTORY) > 100:
ACCURACY_HISTORY.pop(0)
return {
"horizon": horizon,
"timestamp": datetime.now().isoformat(),
"current": metrics,
"history": ACCURACY_HISTORY[-10:], # Last 10 evaluations
"target_auc": {6: 0.85, 12: 0.82, 24: 0.78, 48: 0.72}.get(horizon, 0.75),
}
@app.get("/accuracy/history")
async def get_accuracy_history(limit: int = 50):
"""Get historical accuracy metrics"""
return {
"count": len(ACCURACY_HISTORY),
"history": ACCURACY_HISTORY[-limit:]
}
@app.get("/states")
async def get_states():
"""Get list of states with summary stats"""
if 6 not in PREDICTIONS_CACHE:
PREDICTIONS_CACHE[6] = generate_predictions(6)
state_stats = {}
for pred in PREDICTIONS_CACHE[6]:
state = pred['state']
if state not in state_stats:
state_stats[state] = {
'state': state,
'counties': 0,
'total_risk': 0,
'total_outages': 0,
'max_risk': 0
}
state_stats[state]['counties'] += 1
state_stats[state]['total_risk'] += pred['risk']
state_stats[state]['total_outages'] += pred['predicted_outages']
state_stats[state]['max_risk'] = max(state_stats[state]['max_risk'], pred['risk'])
# Calculate averages
for state in state_stats.values():
state['avg_risk'] = round(state['total_risk'] / state['counties'], 4)
del state['total_risk']
return {
"timestamp": datetime.now().isoformat(),
"states": sorted(state_stats.values(), key=lambda x: -x['avg_risk'])
}
@app.get("/outages/live")
async def get_live_outages(refresh: bool = False):
"""
Get real-time outage data from utility companies.
Caches data for 5 minutes to avoid hammering utility APIs.
"""
global REALTIME_OUTAGES, LAST_OUTAGE_FETCH
# Check if we need to refresh
should_refresh = refresh or LAST_OUTAGE_FETCH is None or \
(datetime.now() - LAST_OUTAGE_FETCH).seconds > 300 # 5 minute cache
if should_refresh:
async with OUTAGE_FETCH_LOCK:
# Double-check after acquiring lock
if LAST_OUTAGE_FETCH is None or \
(datetime.now() - LAST_OUTAGE_FETCH).seconds > 300 or refresh:
logger.info("Fetching fresh outage data from utilities...")
REALTIME_OUTAGES = await get_realtime_outages()
LAST_OUTAGE_FETCH = datetime.now()
# Update ACTUALS_CACHE for accuracy calculations
ACTUALS_CACHE.update(REALTIME_OUTAGES)
return {
"timestamp": REALTIME_OUTAGES.get('timestamp', datetime.now().isoformat()),
"data_age_seconds": (datetime.now() - LAST_OUTAGE_FETCH).seconds if LAST_OUTAGE_FETCH else 0,
"sources_checked": REALTIME_OUTAGES.get('sources_checked', 0),
"sources_available": REALTIME_OUTAGES.get('sources_available', 0),
"total_customers_out": REALTIME_OUTAGES.get('total_customers_out', 0),
"by_state": REALTIME_OUTAGES.get('by_state', {}),
"by_utility": REALTIME_OUTAGES.get('by_utility', []),
"source_status": REALTIME_OUTAGES.get('raw_results', [])
}
@app.get("/outages/utilities")
async def get_configured_utilities():
"""Get list of configured utility data sources"""
return {
"total": len(UTILITY_SOURCES),
"utilities": [
{
"id": uid,
"name": config['name'],
"state": config['state'],
"coverage": config.get('coverage', [])
}
for uid, config in UTILITY_SOURCES.items()
]
}
@app.get("/outages/compare")
async def compare_predictions_to_actuals(horizon: int = 6):
"""
Compare current predictions to real-time actual outages.
Returns side-by-side comparison for evaluation.
"""
# Get predictions
if horizon not in PREDICTIONS_CACHE:
PREDICTIONS_CACHE[horizon] = generate_predictions(horizon)
predictions = PREDICTIONS_CACHE[horizon]
# Get live outages
live_data = await get_live_outages()
# Build comparison
comparison = {
"timestamp": datetime.now().isoformat(),
"horizon": horizon,
"predictions_summary": {
"total_counties": len(predictions),
"total_predicted_outages": sum(p['predicted_outages'] for p in predictions),
"mean_risk": np.mean([p['risk'] for p in predictions]),
},
"actuals_summary": {
"sources_available": live_data['sources_available'],
"total_customers_out": live_data['total_customers_out'],
},
"by_state": []
}
# Compare by state
pred_by_state = {}
for p in predictions:
state = p['state']
if state not in pred_by_state:
pred_by_state[state] = {'predicted_outages': 0, 'avg_risk': 0, 'count': 0}
pred_by_state[state]['predicted_outages'] += p['predicted_outages']
pred_by_state[state]['avg_risk'] += p['risk']
pred_by_state[state]['count'] += 1
for state, pred_data in pred_by_state.items():
# Get state abbreviation for matching with live data
state_abbrev = STATE_ABBREV.get(state)
actual_data = live_data['by_state'].get(state_abbrev, {})
comparison['by_state'].append({
'state': state,
'predicted_outages': pred_data['predicted_outages'],
'predicted_risk': round(pred_data['avg_risk'] / pred_data['count'], 4),
'actual_customers_out': actual_data.get('customers_out', 0),
'utilities_reporting': actual_data.get('utilities', []),
'data_available': bool(actual_data)
})
# Sort by predicted risk
comparison['by_state'].sort(key=lambda x: -x['predicted_risk'])
return comparison
@app.post("/outages/refresh")
async def refresh_outages(background_tasks: BackgroundTasks):
"""Force refresh of outage data in background"""
background_tasks.add_task(get_live_outages, refresh=True)
return {"status": "refresh_started", "message": "Outage data refresh initiated"}
# =============================================================================
# BACKTESTING ENDPOINTS - Using Real EAGLE-I Historical Data
# =============================================================================
# Cache for EAGLE-I data (loaded per year)
EAGLEI_CACHE = {}
def load_eaglei_year(year: int) -> pd.DataFrame:
"""Load EAGLE-I data for a specific year from S3 parquet files"""
if year in EAGLEI_CACHE:
return EAGLEI_CACHE[year]
if s3_client is None:
raise HTTPException(
status_code=503,
detail="S3 client not initialized. Check AWS credentials."
)
s3_key = f"parquet/eaglei_{year}.parquet"
logger.info(f"Loading EAGLE-I data for {year} from s3://{S3_BUCKET}/{s3_key}")
try:
# Download parquet file from S3 to memory
response = s3_client.get_object(Bucket=S3_BUCKET, Key=s3_key)
parquet_data = io.BytesIO(response['Body'].read())
# Read parquet into DataFrame
df = pd.read_parquet(parquet_data)
# Parse datetime if needed
if 'run_start_time' in df.columns and df['run_start_time'].dtype == 'object':
df['run_start_time'] = pd.to_datetime(df['run_start_time'])
# Normalize column names
if 'sum' in df.columns and 'customers_out' not in df.columns:
df = df.rename(columns={'sum': 'customers_out'})
# Ensure fips_code is string and padded
df['fips_code'] = df['fips_code'].astype(str).str.zfill(5)
# Add state FIPS (first 2 digits)
df['state_fips'] = df['fips_code'].str[:2]
EAGLEI_CACHE[year] = df
logger.info(f"Loaded {len(df):,} rows for {year} from S3")
return df
except ClientError as e:
error_code = e.response['Error']['Code']
if error_code == 'NoSuchKey':
logger.warning(f"EAGLE-I data for {year} not found in S3: {s3_key}")
raise HTTPException(
status_code=503,
detail=f"EAGLE-I data for {year} not available in S3."
)
else:
logger.error(f"S3 error loading {year}: {e}")
raise HTTPException(
status_code=503,
detail=f"Error loading EAGLE-I data from S3: {error_code}"
)
except Exception as e:
logger.error(f"Error loading EAGLE-I data for {year}: {e}")
raise HTTPException(
status_code=503,
detail=f"Error loading EAGLE-I data: {str(e)}"
)
def get_eaglei_snapshot(target_time: datetime) -> dict:
"""
Get EAGLE-I outage data for a specific timestamp.
Returns aggregated data by state and county.
"""
year = target_time.year
df = load_eaglei_year(year)
# Find the closest timestamp (within 15 minutes)
df['time_diff'] = abs((df['run_start_time'] - target_time).dt.total_seconds())
# Get rows within 15 minutes of target
mask = df['time_diff'] <= 900 # 15 minutes in seconds
snapshot = df[mask].copy()
if len(snapshot) == 0:
# Try to find nearest available time
nearest_idx = df['time_diff'].idxmin()
nearest_time = df.loc[nearest_idx, 'run_start_time']
snapshot = df[df['run_start_time'] == nearest_time].copy()
# Aggregate by state
state_agg = snapshot.groupby(['state', 'state_fips']).agg({
'customers_out': 'sum',
'fips_code': 'count' # county count
}).reset_index()
state_agg.columns = ['state', 'state_fips', 'customers_out', 'county_count']
# Build by_state dict with abbreviations
by_state = {}
for _, row in state_agg.iterrows():
abbrev = STATE_ABBREV.get(row['state'], row['state'][:2].upper())
by_state[abbrev] = {
'customers_out': int(row['customers_out']),
'county_count': int(row['county_count']),
'state_name': row['state']
}
# County-level data
county_data = snapshot.groupby(['fips_code', 'county', 'state']).agg({
'customers_out': 'sum'
}).reset_index()
total_out = int(snapshot['customers_out'].sum())
actual_time = snapshot['run_start_time'].iloc[0] if len(snapshot) > 0 else target_time
return {
'timestamp': actual_time.isoformat(),
'requested_time': target_time.isoformat(),
'total_customers_out': total_out,
'states_reporting': len(by_state),
'counties_reporting': len(county_data),
'by_state': by_state,
'by_county': county_data.to_dict('records')
}
@app.get("/backtest/snapshot")
async def get_historical_snapshot(
date: str, # Format: YYYY-MM-DD
hour: int = 12 # Hour of day (0-23)
):
"""
Get actual EAGLE-I outage data for a specific date and time.
Args:
date: Date in YYYY-MM-DD format (must be in 2014-2024 range)
hour: Hour of day (0-23)
"""
try:
target_time = datetime.strptime(f"{date} {hour:02d}:00:00", "%Y-%m-%d %H:%M:%S")
except ValueError:
raise HTTPException(status_code=400, detail="Invalid date format. Use YYYY-MM-DD")
if target_time.year < 2014 or target_time.year > 2024:
raise HTTPException(status_code=400, detail="Date must be between 2014 and 2024")
snapshot = get_eaglei_snapshot(target_time)
return snapshot
@app.get("/backtest/compare")
async def backtest_compare(
date: str, # Format: YYYY-MM-DD
hour: int = 12,
horizon: int = 6
):
"""
Compare model predictions vs actual EAGLE-I data for a historical date.
This shows what our model would have predicted {horizon} hours before
the target time, compared to what actually happened.
Args:
date: Date in YYYY-MM-DD format
hour: Hour of day to evaluate (0-23)
horizon: Prediction horizon (6, 12, 24, or 48 hours)
"""
try:
target_time = datetime.strptime(f"{date} {hour:02d}:00:00", "%Y-%m-%d %H:%M:%S")
except ValueError:
raise HTTPException(status_code=400, detail="Invalid date format. Use YYYY-MM-DD")
if target_time.year < 2014 or target_time.year > 2024:
raise HTTPException(status_code=400, detail="Date must be between 2014 and 2024")
# Get actual outages at target time
actual_snapshot = get_eaglei_snapshot(target_time)
# Generate predictions (simulated for now - would use actual model with historical weather)
# In production, this would load weather data from {horizon} hours before target_time
# and run the actual model
predictions = generate_predictions(horizon)
# Build comparison by state
state_comparison = []
total_predicted = 0
total_actual = 0
for pred in predictions:
state = pred['state']
abbrev = STATE_ABBREV.get(state)
actual_data = actual_snapshot['by_state'].get(abbrev, {})
actual_out = actual_data.get('customers_out', 0)
pred_out = pred['predicted_outages']
total_predicted += pred_out
total_actual += actual_out
# Check if we already have this state
existing = next((s for s in state_comparison if s['state'] == state), None)
if existing:
existing['predicted_outages'] += pred_out
existing['predicted_risk'] = (existing['predicted_risk'] + pred['risk']) / 2
else:
state_comparison.append({
'state': state,
'state_abbrev': abbrev,
'predicted_outages': pred_out,
'predicted_risk': pred['risk'],
'actual_outages': actual_out,
'has_actual_data': actual_out > 0,
'error': pred_out - actual_out if actual_out > 0 else None,
'error_pct': round((pred_out - actual_out) / actual_out * 100, 1) if actual_out > 0 else None
})
# Sort by actual outages (descending)
state_comparison.sort(key=lambda x: -(x['actual_outages'] or 0))
# Calculate accuracy metrics
states_with_data = [s for s in state_comparison if s['has_actual_data']]
if states_with_data:
errors = [abs(s['error']) for s in states_with_data if s['error'] is not None]
mae = np.mean(errors) if errors else 0
# Correlation
pred_vals = [s['predicted_outages'] for s in states_with_data]
actual_vals = [s['actual_outages'] for s in states_with_data]
correlation = np.corrcoef(pred_vals, actual_vals)[0, 1] if len(pred_vals) > 1 else 0
else:
mae = 0
correlation = 0
return {
'target_time': target_time.isoformat(),
'prediction_time': (target_time - timedelta(hours=horizon)).isoformat(),
'horizon_hours': horizon,
'summary': {
'total_predicted': total_predicted,
'total_actual': actual_snapshot['total_customers_out'],
'difference': total_predicted - actual_snapshot['total_customers_out'],
'states_with_actual_data': len(states_with_data),
'mean_absolute_error': round(mae, 0),
'correlation': round(correlation, 3) if not np.isnan(correlation) else 0
},
'actual_snapshot': {
'timestamp': actual_snapshot['timestamp'],
'total_customers_out': actual_snapshot['total_customers_out'],
'states_reporting': actual_snapshot['states_reporting'],
'counties_reporting': actual_snapshot['counties_reporting']
},
'by_state': state_comparison,
'actual_by_state': actual_snapshot['by_state']
}
@app.get("/backtest/events")
async def get_notable_events():
"""
Get list of notable weather events in the EAGLE-I data.
These are good dates to test the model on.
"""
return {
"events": [
{
"name": "Hurricane Helene",
"date": "2024-09-27",
"hour": 18,
"description": "Major hurricane affecting Southeast US",
"states_affected": ["FL", "GA", "SC", "NC", "TN"]
},
{
"name": "Winter Storm Elliott",
"date": "2022-12-24",
"hour": 12,
"description": "Bomb cyclone causing widespread outages",
"states_affected": ["NY", "PA", "OH", "MI", "TN", "NC"]
},
{
"name": "Hurricane Ian",
"date": "2022-09-28",
"hour": 18,
"description": "Category 4 hurricane hitting Florida",
"states_affected": ["FL", "SC", "NC"]
},
{
"name": "Texas Winter Storm Uri",
"date": "2021-02-16",
"hour": 8,
"description": "Historic cold snap causing grid failures",
"states_affected": ["TX", "LA", "MS", "AR", "OK"]
},
{
"name": "Hurricane Laura",
"date": "2020-08-27",
"hour": 6,
"description": "Category 4 hurricane hitting Louisiana",
"states_affected": ["LA", "TX", "AR"]
},
{
"name": "Hurricane Michael",
"date": "2018-10-10",
"hour": 18,
"description": "Category 5 hurricane hitting Florida Panhandle",
"states_affected": ["FL", "GA", "AL"]
},
{
"name": "Hurricane Irma",
"date": "2017-09-10",
"hour": 12,
"description": "Major hurricane affecting all of Florida",
"states_affected": ["FL", "GA", "SC"]
},
{
"name": "Average Day (Baseline)",
"date": "2024-06-15",
"hour": 14,
"description": "Typical summer day for baseline comparison",
"states_affected": []
}
]
}
@app.get("/backtest/available-dates")
async def get_available_dates():
"""Get the range of dates available for backtesting"""
available_years = []
if s3_client:
try:
# Check S3 for available parquet files
response = s3_client.list_objects_v2(Bucket=S3_BUCKET, Prefix='parquet/')
if 'Contents' in response:
for obj in response['Contents']:
# Parse year from filename like "parquet/eaglei_2024.parquet"
key = obj['Key']
if key.endswith('.parquet'):
try:
year = int(key.split('_')[-1].replace('.parquet', ''))
available_years.append(year)
except ValueError:
continue
available_years.sort()
except Exception as e:
logger.error(f"Error listing S3 objects: {e}")
return {
"available_years": available_years,
"date_range": {
"start": f"{min(available_years)}-01-01" if available_years else "2014-01-01",
"end": f"{max(available_years)}-12-31" if available_years else "2024-12-31"
},
"data_resolution": "15 minutes",
"total_years": len(available_years),
"data_source": "S3" if available_years else "none"
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)