src.core.mixins.caching

Caching utilities for expensive MMM calculations.

This module provides caching decorators and utilities for response curves, contributions, and other expensive calculations. Implements LRU-style caching with automatic invalidation.

Module Contents

src.core.mixins.caching.cache_response_curve(maxsize: int = 128)

Decorator to cache response curve calculations.

Uses data hash + parameters as cache key. Automatically handles numpy arrays and nested parameters. Implements FIFO eviction when cache reaches maxsize.

Parameters:

maxsize – Maximum cache entries (FIFO eviction when exceeded).

Returns:

Decorated function with caching capabilities.

Example

>>> @cache_response_curve(maxsize=256)
... def calculate_curves(channel_data, alpha, L, k):
...     # Expensive calculation
...     return curves
>>>
>>> # First call - cache miss
>>> result1 = calculate_curves(data, 0.5, 1.0, 0.3)
>>>
>>> # Second call with same args - cache hit
>>> result2 = calculate_curves(data, 0.5, 1.0, 0.3)
>>>
>>> # Check cache statistics
>>> stats = calculate_curves.cache_info()
>>> print(f"Hits: {stats['hits']}, Misses: {stats['misses']}")