Reuse#

class deeptrack.optical.augmentations.Reuse(feature: Feature, uses: int | Callable[[...], int] = 2, storage: int | Callable[[...], int] = 1, **kwargs)#

Bases: Feature

Cache and reuse the output of another feature.

Reuse wraps a feature and avoids recomputing it at every evaluation. Instead, it stores up to storage previously computed outputs and reuses them multiple times.

The cache is filled until it contains storage outputs. Afterwards, each cached output is reused uses times before a new evaluation cycle begins.

This is useful when an expensive feature should only be evaluated occasionally while still producing varying outputs through reuse.

Parameters#

feature: Feature

Feature whose output should be cached.

uses: PropertyLike[int], optional

Number of times each cached output is reused. Defaults to 2.

storage: PropertyLike[int], optional

Maximum number of cached outputs stored. Defaults to 1.

Methods#

get(data, uses, storage, **kwargs) -> np.ndarray | torch.Tensor

Implements the caching and reuse logic. Evaluates the wrapped feature only when necessary and otherwise returns cached outputs.

Examples#

>>> import deeptrack as dt
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> particle = dt.PointParticle(
...     intensity=1,
...     position=lambda: np.random.rand(2) * 64
... )
>>> optics = dt.Fluorescence()
>>> base = optics(particle)
>>> pipeline = dt.Reuse(base, uses=2, storage=2)
>>> fig, ax = plt.subplots(1, 8, figsize=(12, 3))
>>> for i in range(8):
...     ax[i].imshow(pipeline.new(), cmap="gray")
...     ax[i].axis("off")
>>> plt.show();

Methods Summary

get(data, uses, storage, **kwargs)

Return a cached output or recompute the wrapped feature.

Methods Documentation

get(data: ndarray | Tensor, uses: int, storage: int, **kwargs) ndarray | Tensor#

Return a cached output or recompute the wrapped feature.

The cache stores up to storage outputs from the wrapped feature. Each cached output is reused uses times before a new evaluation cycle begins. Cached outputs are returned in cyclic order.

Parameters#

data: np.ndarray | torch.Tensor

Input passed to the wrapped feature.

uses: int

Number of times each cached output is reused.

storage: int

Maximum number of outputs stored in the cache.

**kwargs: Any

Additional keyword arguments passed to the wrapped feature when recomputation is necessary.

Returns#

np.ndarray | torch.Tensor

Cached output if reuse is possible, otherwise a newly computed output from the wrapped feature.