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

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. 

3 

4 from aether_quant import samples 

5 closes = samples.load_sample_prices() # dates x tickers 

6 opens = samples.load_sample_prices("open") 

7""" 

8 

9from __future__ import annotations 

10 

11import gzip 

12import hashlib 

13import io 

14from functools import lru_cache 

15from pathlib import Path 

16from typing import Literal 

17 

18import pandas as pd 

19 

20SAMPLE_PATH = Path(__file__).resolve().parent / "_sample" / "coinbase_daily.csv.gz" 

21SAMPLE_README = SAMPLE_PATH.parent / "README.md" 

22 

23__all__ = ["SAMPLE_PATH", "load_sample_bars", "load_sample_prices", "sample_info"] 

24 

25 

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}) 

30 

31 

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() 

35 

36 

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) 

42 

43 

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 }