BaryonForge.utils.Cache module

class BaryonForge.utils.Cache.SimpleArrayCache(maxsize=32)[source]

Bases: object

A lightweight LRU-style cache designed for functions whose inputs include NumPy arrays. Unlike functools.lru_cache, this cache supports unhashable arguments (e.g. numpy.ndarray) by constructing a stable byte-based key from the array contents.

The cache stores results keyed by a tuple that is dynamically generated according to the function being cached:

  • floats, ints, and strings are saved as single values

  • For each array argument, we store:
    • its shape,

    • its dtype,

    • its raw byte buffer from .tobytes().

  • Any lists and tuples are converted to arrays and follow the above

  • Other objects (custom classes) are converted to string representations

When used as a decorator, the cache wraps a function of the form func(*args) and automatically caches its return value based on these arguments. Repeated calls with identical inputs return the cached result without re-evaluating the function.

Parameters:

maxsize (int, optional) – Maximum number of cached entries to store. The cache evicts the least recently used (LRU) entry when the limit is exceeded. Default is 64

Notes

  • This cache treats the contents of arrays as part of the key, so even small differences in floating-point values produce distinct cache entries.

  • Custom classes are converted to a string and used verbatim. Two custom objects that print identically will collide.

  • The cache is implemented using collections.OrderedDict and maintains LRU behavior manually.

Examples

>>> cached_func = SimpleArrayCache(maxsize=64)(func)
get(*args)[source]
set(value, *args)[source]
class BaryonForge.utils.Cache.CachedProfile(self, Profile, maxsize=64, methods=['real', 'projected', 'fourier'])[source]

Bases: BaseBFGProfiles

A class that will cache the profile evaluations for the real, projected, and fourier methods.

This class will take in a BaryonForge (BFG) class and cache its results. It is useful for halo model P(k) calculations, where the same masses, redshifts, wavenumbers/radii are evaluated many times. See also TabulatedProfile if you want to only store a sparser grid.

Parameters:

Profile (object) – A profile that we want to cache. Can either be a vanilla CCL profile or a BaryonForge Profile.