Spaces:
Sleeping
Sleeping
File size: 8,301 Bytes
4202f60 |
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 |
"""Data loading utilities for FBMC forecasting project.
Provides convenient functions to load and filter FBMC data files.
"""
import polars as pl
from pathlib import Path
from typing import Optional, List
from datetime import datetime, timedelta
class FBMCDataLoader:
"""Load and filter FBMC data with convenient methods."""
def __init__(self, data_dir: Path = Path("data/raw")):
"""Initialize data loader.
Args:
data_dir: Directory containing Parquet files (default: data/raw)
"""
self.data_dir = Path(data_dir)
if not self.data_dir.exists():
raise FileNotFoundError(f"Data directory not found: {data_dir}")
def load_cnecs(
self,
start_date: Optional[str] = None,
end_date: Optional[str] = None,
borders: Optional[List[str]] = None
) -> pl.DataFrame:
"""Load CNEC data with optional filtering.
Args:
start_date: Start date (ISO format: 'YYYY-MM-DD')
end_date: End date (ISO format: 'YYYY-MM-DD')
borders: List of border codes to filter (e.g., ['DE_NL', 'DE_FR'])
Returns:
Polars DataFrame with CNEC data
"""
file_path = self.data_dir / "cnecs_2024_2025.parquet"
if not file_path.exists():
raise FileNotFoundError(f"CNECs file not found: {file_path}")
cnecs = pl.read_parquet(file_path)
# Apply date filters
if start_date:
cnecs = cnecs.filter(pl.col("timestamp") >= start_date)
if end_date:
cnecs = cnecs.filter(pl.col("timestamp") <= end_date)
# Apply border filter
if borders:
cnecs = cnecs.filter(pl.col("border").is_in(borders))
return cnecs
def load_weather(
self,
start_date: Optional[str] = None,
end_date: Optional[str] = None,
grid_points: Optional[List[str]] = None
) -> pl.DataFrame:
"""Load weather data with optional filtering.
Args:
start_date: Start date (ISO format: 'YYYY-MM-DD')
end_date: End date (ISO format: 'YYYY-MM-DD')
grid_points: List of grid point IDs to filter
Returns:
Polars DataFrame with weather data
"""
file_path = self.data_dir / "weather_2024_2025.parquet"
if not file_path.exists():
raise FileNotFoundError(f"Weather file not found: {file_path}")
weather = pl.read_parquet(file_path)
# Apply date filters
if start_date:
weather = weather.filter(pl.col("timestamp") >= start_date)
if end_date:
weather = weather.filter(pl.col("timestamp") <= end_date)
# Apply grid point filter
if grid_points:
weather = weather.filter(pl.col("grid_point").is_in(grid_points))
return weather
def load_entsoe(
self,
start_date: Optional[str] = None,
end_date: Optional[str] = None,
zones: Optional[List[str]] = None
) -> pl.DataFrame:
"""Load ENTSO-E data with optional filtering.
Args:
start_date: Start date (ISO format: 'YYYY-MM-DD')
end_date: End date (ISO format: 'YYYY-MM-DD')
zones: List of bidding zone codes (e.g., ['DE_LU', 'FR', 'NL'])
Returns:
Polars DataFrame with ENTSO-E data
"""
file_path = self.data_dir / "entsoe_2024_2025.parquet"
if not file_path.exists():
raise FileNotFoundError(f"ENTSO-E file not found: {file_path}")
entsoe = pl.read_parquet(file_path)
# Apply date filters
if start_date:
entsoe = entsoe.filter(pl.col("timestamp") >= start_date)
if end_date:
entsoe = entsoe.filter(pl.col("timestamp") <= end_date)
# Apply zone filter
if zones:
entsoe = entsoe.filter(pl.col("zone").is_in(zones))
return entsoe
def get_date_range(self) -> dict:
"""Get available date range from all datasets.
Returns:
Dictionary with min/max dates for each dataset
"""
date_ranges = {}
try:
cnecs = pl.read_parquet(self.data_dir / "cnecs_2024_2025.parquet")
date_ranges['cnecs'] = {
'min': cnecs['timestamp'].min(),
'max': cnecs['timestamp'].max()
}
except Exception:
date_ranges['cnecs'] = None
try:
weather = pl.read_parquet(self.data_dir / "weather_2024_2025.parquet")
date_ranges['weather'] = {
'min': weather['timestamp'].min(),
'max': weather['timestamp'].max()
}
except Exception:
date_ranges['weather'] = None
try:
entsoe = pl.read_parquet(self.data_dir / "entsoe_2024_2025.parquet")
date_ranges['entsoe'] = {
'min': entsoe['timestamp'].min(),
'max': entsoe['timestamp'].max()
}
except Exception:
date_ranges['entsoe'] = None
return date_ranges
def validate_data_completeness(
self,
start_date: str,
end_date: str,
max_missing_pct: float = 5.0
) -> dict:
"""Validate data completeness for a given date range.
Args:
start_date: Start date (ISO format)
end_date: End date (ISO format)
max_missing_pct: Maximum acceptable missing data percentage
Returns:
Dictionary with validation results for each dataset
"""
results = {}
# Calculate expected number of hours
start_dt = datetime.fromisoformat(start_date)
end_dt = datetime.fromisoformat(end_date)
expected_hours = int((end_dt - start_dt).total_seconds() / 3600)
# Validate CNECs
try:
cnecs = self.load_cnecs(start_date, end_date)
actual_hours = cnecs.select(pl.col("timestamp").n_unique()).item()
missing_pct = (1 - actual_hours / expected_hours) * 100
results['cnecs'] = {
'expected_hours': expected_hours,
'actual_hours': actual_hours,
'missing_pct': missing_pct,
'valid': missing_pct <= max_missing_pct
}
except Exception as e:
results['cnecs'] = {'error': str(e), 'valid': False}
# Validate weather
try:
weather = self.load_weather(start_date, end_date)
actual_hours = weather.select(pl.col("timestamp").n_unique()).item()
missing_pct = (1 - actual_hours / expected_hours) * 100
results['weather'] = {
'expected_hours': expected_hours,
'actual_hours': actual_hours,
'missing_pct': missing_pct,
'valid': missing_pct <= max_missing_pct
}
except Exception as e:
results['weather'] = {'error': str(e), 'valid': False}
# Validate ENTSO-E
try:
entsoe = self.load_entsoe(start_date, end_date)
actual_hours = entsoe.select(pl.col("timestamp").n_unique()).item()
missing_pct = (1 - actual_hours / expected_hours) * 100
results['entsoe'] = {
'expected_hours': expected_hours,
'actual_hours': actual_hours,
'missing_pct': missing_pct,
'valid': missing_pct <= max_missing_pct
}
except Exception as e:
results['entsoe'] = {'error': str(e), 'valid': False}
return results
# Example usage
if __name__ == "__main__":
# Initialize loader
loader = FBMCDataLoader(data_dir=Path("data/raw"))
# Check available date ranges
print("Available date ranges:")
date_ranges = loader.get_date_range()
for dataset, ranges in date_ranges.items():
if ranges:
print(f" {dataset}: {ranges['min']} to {ranges['max']}")
else:
print(f" {dataset}: Not available")
# Load specific data
# cnecs = loader.load_cnecs(start_date="2024-10-01", end_date="2024-10-31")
# weather = loader.load_weather(start_date="2024-10-01", end_date="2024-10-31")
|