| |
| from __future__ import annotations |
|
|
| import argparse |
| import json |
| from pathlib import Path |
|
|
| import pyarrow.parquet as pq |
|
|
| from build_dataset import MlbApiClient, default_end_date, default_start_date, load_schedule |
|
|
|
|
| def parse_args() -> argparse.Namespace: |
| parser = argparse.ArgumentParser(description="Verify the generated MLB ChatML parquet dataset.") |
| parser.add_argument( |
| "--parquet", |
| type=Path, |
| default=None, |
| ) |
| parser.add_argument("--season", type=int, default=2025) |
| parser.add_argument("--start-date", default=None) |
| parser.add_argument("--end-date", default=None) |
| parser.add_argument("--cache-dir", type=Path, default=Path(".cache/mlb_api")) |
| parser.add_argument("--max-rps", type=float, default=4.0) |
| return parser.parse_args() |
|
|
|
|
| def main() -> None: |
| args = parse_args() |
| parquet_path = args.parquet or Path( |
| f"data/processed/mlb_{args.season}_chatml_matchups.parquet" |
| ) |
| if not parquet_path.exists(): |
| raise SystemExit(f"missing parquet file: {parquet_path}") |
|
|
| table = pq.read_table(parquet_path) |
| rows = table.to_pylist() |
| client = MlbApiClient(cache_dir=args.cache_dir, max_rps=args.max_rps) |
| start_date = args.start_date or default_start_date(args.season) |
| end_date = args.end_date or default_end_date(args.season) |
| games = load_schedule(client, args.season, start_date, end_date) |
| expected_rows = len(games) * 2 |
|
|
| row_ids = [row["row_id"] for row in rows] |
| game_pks = {row["game_pk"] for row in rows} |
| team_ids = {row["team_id"] for row in rows} |
| seasons = {row["season"] for row in rows} |
| bad_messages = [ |
| row["row_id"] |
| for row in rows |
| if [message["role"] for message in row["messages"]] != ["system", "user", "assistant"] |
| ] |
| missing_pitching = [ |
| row["row_id"] |
| for row in rows |
| if not json.loads(row["team_game_pitching_json"]) |
| or not json.loads(row["team_season_pitching_json"]) |
| or not json.loads(row["opponent_game_pitching_json"]) |
| ] |
|
|
| checks = { |
| "rows_match_schedule_x2": len(rows) == expected_rows, |
| "unique_row_ids": len(row_ids) == len(set(row_ids)), |
| "all_games_present": len(game_pks) == len(games), |
| "all_30_teams_present": len(team_ids) == 30, |
| "season_matches": seasons == {args.season}, |
| "chatml_roles_valid": not bad_messages, |
| "pitching_fields_present": not missing_pitching, |
| } |
| print( |
| json.dumps( |
| { |
| "parquet": str(parquet_path), |
| "season": args.season, |
| "rows": len(rows), |
| "expected_rows": expected_rows, |
| "games": len(games), |
| "teams": len(team_ids), |
| "checks": checks, |
| "api_metrics": client.metrics.__dict__, |
| }, |
| indent=2, |
| sort_keys=True, |
| ) |
| ) |
| failed = [name for name, passed in checks.items() if not passed] |
| if failed: |
| raise SystemExit(f"verification failed: {failed}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|