Download recipe/manifest.py from ZipLime/us-treasury-events: direct link, hf CLI and curl.
- Browser
- Download file 5.37 kB
-
https://huggingface.co/datasets/ZipLime/us-treasury-events/resolve/main/recipe/manifest.py
- Command line
-
hf download hf://datasets/ZipLime/us-treasury-events/recipe/manifest.py
-
curl -L -o manifest.py https://huggingface.co/datasets/ZipLime/us-treasury-events/resolve/main/recipe/manifest.py
5.37 kB
| """The bundle descriptor, rebuilt from what is actually on disk.""" | |
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import os | |
| import tempfile | |
| from datetime import UTC, datetime | |
| from pathlib import Path | |
| from typing import Any | |
| import polars as pl | |
| from . import __version__ | |
| from .config import RELEASE_TIMEZONE | |
| from .pipeline import PIT_DELTA_NAME | |
| from .schema import BUILD_VERSION, CONFIG_SCHEMAS, PARSER_VERSION | |
| from .store import read_table | |
| RECIPE_ROOT = Path(__file__).resolve().parent.parent | |
| CONFIG_DESCRIPTIONS: dict[str, dict[str, str]] = { | |
| "auction_calendar": { | |
| "path": "data/auction_calendar/**/*.parquet", | |
| "grain": "one auctioned or announced security, dated from its announcement", | |
| }, | |
| "auction_announcements": { | |
| "path": "data/auction_announcements/**/*.parquet", | |
| "grain": "one official announcement of an auction, with its closing times", | |
| }, | |
| "auction_results": { | |
| "path": "data/auction_results/**/*.parquet", | |
| "grain": "one completed auction, at the minute its result was released", | |
| }, | |
| "nominal_yield_curve": { | |
| "path": "data/nominal_yield_curve/**/*.parquet", | |
| "grain": "one maturity of one day's official par yield curve", | |
| }, | |
| "real_yield_curve": { | |
| "path": "data/real_yield_curve/**/*.parquet", | |
| "grain": "one maturity of one day's official TIPS par real yield curve", | |
| }, | |
| "bill_rates": { | |
| "path": "data/bill_rates/**/*.parquet", | |
| "grain": "one bill maturity of one day, on both quote conventions", | |
| }, | |
| "curve_features": { | |
| "path": "data/curve_features/**/*.parquet", | |
| "grain": "derived slopes and one spread, from curves published for the same day", | |
| }, | |
| "pit": { | |
| "path": "data/pit/**/*.parquet", | |
| "delta_path": f"data/pit/{PIT_DELTA_NAME}", | |
| "grain": "one point-in-time Treasury number: an entity, a date, an instant, a value", | |
| }, | |
| } | |
| def recipe_hash(project_root: Path | None = None) -> str: | |
| """Hash of the code that produced a build.""" | |
| root = (project_root or RECIPE_ROOT).resolve() | |
| digest = hashlib.sha256() | |
| files = sorted( | |
| { | |
| path | |
| for pattern in ( | |
| "recipe/**/*.py", "jobs/**/*.py", "tests/**/*.py", | |
| "config/*.yaml", "pyproject.toml", | |
| ) | |
| for path in root.glob(pattern) | |
| if path.is_file() | |
| } | |
| ) | |
| if not files: | |
| raise FileNotFoundError(f"no recipe sources under {root}") | |
| for path in files: | |
| digest.update(str(path.relative_to(root)).encode()) | |
| digest.update(b"\0") | |
| digest.update(path.read_bytes()) | |
| digest.update(b"\0") | |
| return f"sha256:{digest.hexdigest()}" | |
| def _coverage(data_dir: Path) -> dict[str, str | None]: | |
| bounds = ( | |
| read_table(data_dir, "pit") | |
| .select( | |
| pl.col("event_date").min().alias("min_event"), | |
| pl.col("event_date").max().alias("max_event"), | |
| pl.col("knowledge_date").min().alias("min_knowledge"), | |
| pl.col("knowledge_date").max().alias("max_knowledge"), | |
| ) | |
| .collect() | |
| ) | |
| if bounds.is_empty(): | |
| return {} | |
| row = bounds.row(0, named=True) | |
| return {key: (value.isoformat() if value is not None else None) for key, value in row.items()} | |
| def build_manifest( | |
| *, data_dir: Path, row_counts: dict[str, int], quality_ok: bool | |
| ) -> dict[str, Any]: | |
| data_dir = Path(data_dir) | |
| project_root = data_dir.resolve().parent | |
| path = project_root / "manifest.json" | |
| manifest: dict[str, Any] = json.loads(path.read_text()) if path.exists() else {} | |
| manifest["generated_at"] = datetime.now(UTC).isoformat() | |
| manifest["rows"] = row_counts.get("pit", 0) | |
| manifest["config_rows"] = row_counts | |
| manifest["configs"] = CONFIG_DESCRIPTIONS | |
| manifest["coverage"] = _coverage(data_dir) | |
| manifest["quality_ok"] = quality_ok | |
| manifest["config_schemas"] = { | |
| name: {column: str(dtype) for column, dtype in schema.items()} | |
| for name, schema in CONFIG_SCHEMAS.items() | |
| } | |
| subsets = ( | |
| read_table(data_dir, "pit") | |
| .group_by("subset") | |
| .agg(pl.len().alias("rows"), pl.col("entity_id").n_unique().alias("entities")) | |
| .sort("subset") | |
| .collect() | |
| ) | |
| manifest["pit_subsets"] = [ | |
| {"subset": row["subset"], "rows": row["rows"], "entities": row["entities"]} | |
| for row in subsets.iter_rows(named=True) | |
| ] | |
| source = dict(manifest.get("source") or {}) | |
| source["recipe_hash"] = recipe_hash() | |
| source["build_version"] = BUILD_VERSION | |
| source["parser_version"] = PARSER_VERSION | |
| source["package_version"] = __version__ | |
| manifest["source"] = source | |
| manifest["release_timezone"] = RELEASE_TIMEZONE | |
| descriptor, temporary = tempfile.mkstemp(prefix=".manifest-", suffix=".json", dir=project_root) | |
| try: | |
| with os.fdopen(descriptor, "w", encoding="utf-8") as handle: | |
| json.dump(manifest, handle, indent=2, sort_keys=True, ensure_ascii=False) | |
| handle.write("\n") | |
| handle.flush() | |
| os.fsync(handle.fileno()) | |
| os.replace(temporary, path) | |
| finally: | |
| if os.path.exists(temporary): | |
| os.unlink(temporary) | |
| return manifest | |
| __all__ = ["CONFIG_DESCRIPTIONS", "build_manifest", "recipe_hash"] | |