Utilities

This page’s code cells are executed live by Quarto every time the docs are built. q.utils holds the small shared helpers the rest of the library (and your research code) leans on.

Reproducibility: q.utils.set_seed

Seeds Python’s random, NumPy, and PyTorch (CPU + CUDA) in one call — also re-exported as q.set_seed. Pass strict_determinism=True to additionally force deterministic cuDNN kernels (slower, and some ops may raise if they have no deterministic implementation):

import qrt as q

q.set_seed(42)

Caching: q.utils.cache_dir and q.utils.memory

cache_dir resolves (and creates) qrt’s OS-standard user cache directory — e.g. ~/.cache/qrt on Linux — used by q.data.sources for downloaded data so caches don’t depend on the working directory. q.utils.memory is a joblib Memory rooted there, for disk-backed memoization of expensive, pure functions. q.utils.clear_cache() wipes either a named subdirectory or everything:

q.utils.cache_dir()
PosixPath('/home/hi/.cache/qrt')
@q.utils.memory.cache
def slow_feature(df): ...

q.utils.clear_cache("joblib")  # or clear_cache() for everything

Environment and console

  • q.utils.load_env() — thin wrapper around dotenv.load_dotenv, for API keys/secrets needed by data sources (not executed here).
  • q.utils.console — a shared rich Console for pretty CLI/notebook output.
  • q.utils.log — element-wise natural logarithm that preserves pandas labels; for simple returns r, q.utils.log(1 + r) gives log returns:
returns = q.data.datasets.load("spy")["close"].pct_change().dropna()
q.utils.log(1 + returns).tail(3)
datetime
2026-07-15    0.003956
2026-07-16   -0.005433
2026-07-17   -0.009946
Name: close, dtype: float64
Back to top