state_space_bounds.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import yaml
  2. import pandas as pd
  3. from pathlib import Path
  4. from loader import load_all_units_cycles, resolve_path
  5. from state_construction import build_chem_cycle_state, STATE_COLUMNS
  6. def load_config(path: Path) -> dict:
  7. with open(path, "r", encoding="utf-8") as f:
  8. return yaml.safe_load(f)
  9. def extract_state_space_bounds(config_path: Path):
  10. cfg = load_config(config_path)
  11. project_root = Path(cfg["Paths"]["project_root"])
  12. output_dir = resolve_path(
  13. project_root,
  14. cfg["Paths"]["data_to_rl"]["output_dir"]
  15. )
  16. output_dir.mkdir(parents=True, exist_ok=True)
  17. df = load_all_units_cycles(cfg)
  18. df = df[df["chem_cycle_valid"] == True]
  19. state_df = build_chem_cycle_state(df)
  20. percentiles = cfg["StateSpaceExtraction"]["statistics"]["percentiles"]
  21. stats = {}
  22. for col in STATE_COLUMNS:
  23. desc = state_df[col].describe(
  24. percentiles=[p / 100 for p in percentiles]
  25. )
  26. stats[col] = {
  27. "min": desc["min"],
  28. "max": desc["max"],
  29. "mean": desc["mean"],
  30. "std": desc["std"],
  31. **{f"p{p}": desc[f"{p}%"] for p in percentiles}
  32. }
  33. pd.DataFrame(stats).T.to_csv(
  34. output_dir / "state_space_bounds.csv"
  35. )