Datasets:
File size: 3,050 Bytes
4bd5225 | 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 | from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import numpy as np
import pandas as pd
DEFAULT_CITIES = ["Beijing", "Shanghai", "Chengdu", "Shenzhen", "Hangzhou"]
@dataclass(frozen=True)
class BenchmarkData:
nodes: pd.DataFrame
generation: pd.DataFrame
market: pd.DataFrame
trades: pd.DataFrame
city_hour: pd.DataFrame
cities: list[str]
hour_count: int
def load_benchmark_data(data_dir: str | Path) -> BenchmarkData:
root = Path(data_dir)
nodes = pd.read_csv(root / "urban_energy_nodes.csv")
generation = pd.read_csv(root / "spatiotemporal_generation.csv")
market = pd.read_csv(root / "market_liquidity.csv")
trades = pd.read_csv(root / "p2p_trades.csv")
generation["timestamp"] = pd.to_datetime(generation["timestamp"])
market["timestamp"] = pd.to_datetime(market["timestamp"])
trades["timestamp"] = pd.to_datetime(trades["timestamp"])
_add_absolute_hour_columns(generation, market, trades)
generation["fdia_detected"] = generation["fdia_detected"].astype(bool)
cities = [city for city in DEFAULT_CITIES if city in set(generation["city"])]
generation["verified_W"] = np.where(generation["verification_status"].eq("verified"), generation["P_reported_W"], 0.0)
generation["physics_excess_W"] = np.maximum(generation["P_reported_W"] - generation["P_max_W"], 0.0)
generation["physics_violation"] = generation["physics_excess_W"].gt(1e-6) | generation["fdia_detected"]
generation["rejected_reported_W"] = np.where(generation["physics_violation"], generation["P_reported_W"], 0.0)
city_hour = (
generation.groupby(["city", "absolute_hour", "hour"], as_index=False)
.agg(
verified_W=("verified_W", "sum"),
reported_W=("P_reported_W", "sum"),
pmax_W=("P_max_W", "sum"),
physics_excess_W=("physics_excess_W", "sum"),
rejected_reported_W=("rejected_reported_W", "sum"),
violation_count=("physics_violation", "sum"),
record_count=("node_id", "count"),
)
.sort_values(["absolute_hour", "city"])
.reset_index(drop=True)
)
city_hour["violation_rate"] = city_hour["violation_count"] / city_hour["record_count"].clip(lower=1)
hour_count = int(generation["absolute_hour"].max()) + 1 if not generation.empty else 0
return BenchmarkData(nodes, generation, market, trades, city_hour, cities, hour_count)
def _add_absolute_hour_columns(*frames: pd.DataFrame) -> None:
timestamp_frames = [frame for frame in frames if "timestamp" in frame and not frame.empty]
if not timestamp_frames:
return
canonical = timestamp_frames[0]
start = canonical["timestamp"].min()
for frame in frames:
if "timestamp" not in frame or frame.empty:
continue
elapsed = frame["timestamp"] - start
frame["absolute_hour"] = (elapsed / pd.Timedelta(hours=1)).round().astype(int)
frame["hour"] = frame["timestamp"].dt.hour.astype(int)
|