|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | +PROJECT_ROOT = Path(__file__).resolve().parents[1] |
| 12 | +if str(PROJECT_ROOT) not in sys.path: |
| 13 | + sys.path.insert(0, str(PROJECT_ROOT)) |
| 14 | + |
| 15 | +from src.config import load_config |
| 16 | +from src.pipeline import run_research_pipeline |
| 17 | + |
| 18 | +PANEL_COLUMNS = ("in_universe", "open", "final_score") |
| 19 | +COMBO_SYMBOLS = ("BTCUSDT", "ETHUSDT") |
| 20 | + |
| 21 | + |
| 22 | +def export_lifecycle_inputs(panel: pd.DataFrame, output_dir: Path) -> dict[str, object]: |
| 23 | + if list(panel.index.names) != ["date", "symbol"]: |
| 24 | + raise ValueError("research panel must use a date/symbol MultiIndex") |
| 25 | + missing = sorted(set((*PANEL_COLUMNS, "close")) - set(panel.columns)) |
| 26 | + if missing: |
| 27 | + raise ValueError(f"research panel is missing columns: {', '.join(missing)}") |
| 28 | + |
| 29 | + frame = panel.reset_index().copy() |
| 30 | + frame["date"] = pd.to_datetime(frame["date"], errors="coerce").dt.tz_localize(None).dt.normalize() |
| 31 | + frame["symbol"] = frame["symbol"].astype(str).str.strip().str.upper() |
| 32 | + lifecycle_panel = frame[["date", "symbol", *PANEL_COLUMNS]].dropna(subset=["date", "open", "final_score"]) |
| 33 | + if lifecycle_panel.empty: |
| 34 | + raise ValueError("research panel has no scored lifecycle rows") |
| 35 | + |
| 36 | + market_history = frame.loc[frame["symbol"].isin(COMBO_SYMBOLS), ["date", "symbol", "close"]].dropna() |
| 37 | + missing_combo = sorted(set(COMBO_SYMBOLS) - set(market_history["symbol"])) |
| 38 | + if missing_combo: |
| 39 | + raise ValueError(f"research panel is missing combo symbols: {', '.join(missing_combo)}") |
| 40 | + |
| 41 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 42 | + panel_path = output_dir / "research_panel.csv.gz" |
| 43 | + market_path = output_dir / "market_history.csv.gz" |
| 44 | + manifest_path = output_dir / "manifest.json" |
| 45 | + lifecycle_panel.to_csv(panel_path, index=False, compression="gzip") |
| 46 | + market_history.to_csv(market_path, index=False, compression="gzip") |
| 47 | + manifest = { |
| 48 | + "contract_version": "crypto.lifecycle_preflight.v1", |
| 49 | + "panel_rows": int(len(lifecycle_panel)), |
| 50 | + "panel_symbols": sorted(lifecycle_panel["symbol"].unique().tolist()), |
| 51 | + "market_rows": int(len(market_history)), |
| 52 | + "market_symbols": sorted(market_history["symbol"].unique().tolist()), |
| 53 | + "start_date": lifecycle_panel["date"].min().date().isoformat(), |
| 54 | + "end_date": lifecycle_panel["date"].max().date().isoformat(), |
| 55 | + } |
| 56 | + manifest_path.write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8") |
| 57 | + return manifest |
| 58 | + |
| 59 | + |
| 60 | +def main() -> int: |
| 61 | + parser = argparse.ArgumentParser(description="Export real production research inputs for lifecycle drift preflight.") |
| 62 | + parser.add_argument("--config", default="config/default.yaml") |
| 63 | + parser.add_argument("--universe-mode", default="broad_liquid") |
| 64 | + parser.add_argument("--output-dir", type=Path, required=True) |
| 65 | + args = parser.parse_args() |
| 66 | + |
| 67 | + config = load_config(args.config) |
| 68 | + result = run_research_pipeline(config, universe_mode=args.universe_mode) |
| 69 | + manifest = export_lifecycle_inputs(result["panel"], args.output_dir) |
| 70 | + print(manifest) |
| 71 | + return 0 |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + raise SystemExit(main()) |
0 commit comments