Cache¶
- class acoular.process.Cache¶
Bases:
InOut
Cache the output of a source in a file to avoid redundant computations.
The
Cache
class stores the output of a source (derived fromGenerator
) in a cache file within the Acoular cache directory. This enables faster reuse of precomputed data by avoiding time-consuming recalculations. The cache behavior is managed through theConfig
class by setting theglobal_caching
attribute.The class intelligently determines whether to use the cached data, update it, or bypass caching based on the global caching configuration and the state of the cache file. The caching mechanism supports scenarios such as:
Reading from a complete or incomplete cache.
Overwriting an existing cache.
Operating in a read-only or no-cache mode.
See also
acoular.base.InOut
Receive data from any source domain and return signals in the same domain.
Examples
Caching the output of an FFT computation:
>>> import acoular as ac >>> import numpy as np >>> >>> ac.config.h5library = 'tables' >>> data = np.random.rand(1024, 1) >>> ts = ac.TimeSamples(data=data, sample_freq=51200) >>> fft = ac.RFFT(source=ts, block_size=1024) >>> cache = ac.Cache(source=fft) # cache the output of the FFT in cache file >>> for block in cache.result(num=1): # read the cached data block-wise ... print(block.shape) [('void_cache.h5', 1)] (1, 513)
Disabling caching globally:
>>> ac.config.global_caching = 'none'
Changing the cache directory:
>>> ac.config.cache_dir = '/path/to/cache_dir'
- digest = Property(depends_on=['source.digest'])¶
A unique identifier based on the cache properties.
- result(num)¶
Generate data blocks from the source, using cache when available.
This method acts as a Python generator that yields blocks of output data from the source, reading from the cache file when possible. The size of the data blocks is determined by the
num
parameter. The caching mechanism helps prevent redundant calculations by storing and reusing the source’s output.- Parameters:
- num
int
The number of time samples or frequency snapshots per block to yield. The final block may be smaller if there is insufficient data.
- num
- Yields:
numpy.ndarray
A 2D NumPy array of shape
(num, num_channels)
representing the output data. Each block is either retrieved from the cache file or generated by the source and cached dynamically during processing.
Notes
The behavior of the caching mechanism depends on the
global_caching
setting:'none'
: Bypasses caching and directly retrieves data from the source.'readonly'
: Reads data from the cache if available; otherwise, retrieves data from the source without caching.'overwrite'
: Replaces any existing cache with newly computed data.
If the cache file is incomplete or corrupted, the method may generate new data from the source to update the cache unless the caching mode is
'readonly'
.The cache node name is based on the source’s
digest
attribute.