utils

utils

General-purpose helper functions.

Add new concerns (config loading, caching, retry helpers, …) as additional modules in this package as they land.

Functions

Name Description
cache_dir Return (creating if needed) qrt’s OS-standard user cache directory.
clear_cache Delete qrt’s cached files, e.g. for a clean re-fetch or freeing disk space.
log Return the element-wise natural logarithm of scalar or array-like values.
set_seed Set random seed for reproducibility across various libraries. If none is given, default is 42.

cache_dir

utils.cache_dir(name='')

Return (creating if needed) qrt’s OS-standard user cache directory.

Resolved via :mod:platformdirs, e.g. ~/.cache/qrt on Linux, ~/Library/Caches/qrt on macOS. Used as the default location for downloaded/cached data (see q.data.sources) so caches don’t depend on the current working directory.

Parameters

Name Type Description Default
name str Optional subdirectory under the qrt cache dir (e.g. "joblib"). ''

clear_cache

utils.clear_cache(name='')

Delete qrt’s cached files, e.g. for a clean re-fetch or freeing disk space.

Safe to call anytime: caches (downloaded OHLC/trades parquet, joblib memoization, …) are rebuilt lazily on next use.

Parameters

Name Type Description Default
name str Optional subdirectory to clear (e.g. "data", "joblib"). If omitted, clears everything under :func:cache_dir. ''

log

utils.log(values)

Return the element-wise natural logarithm of scalar or array-like values.

This delegates to :func:numpy.log, preserving pandas Series and DataFrame labels. For simple returns r, use log(1 + r) to obtain log returns.

set_seed

utils.set_seed(seed=42, strict_determinism=False)

Set random seed for reproducibility across various libraries. If none is given, default is 42.

Parameters

Name Type Description Default
seed int Random seed to use. 42
strict_determinism bool If True, also forces deterministic cuDNN kernels, disables cuDNN auto-tuning, and enables torch.use_deterministic_algorithms(True) for stronger reproducibility guarantees, at the cost of speed and possible errors (see Caveats). Defaults to False. False

Caveats

  • Enabling torch.use_deterministic_algorithms(True) can raise a RuntimeError if an op has no deterministic implementation. The CUBLAS_WORKSPACE_CONFIG environment variable is set to :4096:8 automatically (unless already set) as required by CUDA.
  • torch.backends.cudnn.deterministic = True and torch.backends.cudnn.benchmark = False can noticeably slow down training since cuDNN can no longer auto-tune kernels.
  • Determinism is not guaranteed across different PyTorch versions, hardware, or number of threads/devices.
Back to top