Coverage for aether_quant/samples.py: 100%
24 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
1"""The sample market data that ships with the library (V6.6.5): the shipped book's 11 coins, daily open and close from
2Coinbase's public candles, 2021-06-01 to 2026-10-31. Offline, deterministic, and enough for `aq hello` and the notebooks.
4 from aether_quant import samples
5 closes = samples.load_sample_prices() # dates x tickers
6 opens = samples.load_sample_prices("open")
7"""
9from __future__ import annotations
11import gzip
12import hashlib
13import io
14from functools import lru_cache
15from pathlib import Path
16from typing import Literal
18import pandas as pd
20SAMPLE_PATH = Path(__file__).resolve().parent / "_sample" / "coinbase_daily.csv.gz"
21SAMPLE_README = SAMPLE_PATH.parent / "README.md"
23__all__ = ["SAMPLE_PATH", "load_sample_bars", "load_sample_prices", "sample_info"]
26@lru_cache(maxsize=1)
27def _long_frame() -> pd.DataFrame:
28 with gzip.open(SAMPLE_PATH, "rb") as handle:
29 return pd.read_csv(io.BytesIO(handle.read()), dtype={"date": str, "ticker": str})
32def load_sample_bars() -> pd.DataFrame:
33 """The sample in long format: columns `date, ticker, open, close`, sorted by date then ticker. A copy, safe to edit."""
34 return _long_frame().copy()
37def load_sample_prices(field: Literal["open", "close"] = "close") -> pd.DataFrame:
38 """One field as a dates x tickers frame indexed by `YYYY-MM-DD` strings; NaN before a coin's first bar."""
39 if field not in ("open", "close"):
40 raise ValueError("field must be 'open' or 'close'")
41 return _long_frame().pivot(index="date", columns="ticker", values=field).sort_index().astype(float)
44def sample_info() -> dict[str, str]:
45 """Where the sample came from: path, window, coins, row count and sha256."""
46 frame = _long_frame()
47 return {
48 "path": str(SAMPLE_PATH),
49 "source": "Coinbase Exchange public candles (no key)",
50 "start": str(frame["date"].min()),
51 "end": str(frame["date"].max()),
52 "tickers": ",".join(sorted(frame["ticker"].unique())),
53 "rows": str(len(frame)),
54 "sha256": hashlib.sha256(SAMPLE_PATH.read_bytes()).hexdigest(),
55 }