API
scitex-decorators — decorator library extracted from scitex.decorators (standalone).
Includes type-conversion (numpy_fn, torch_fn, pandas_fn, xarray_fn, signal_fn), caching (cache_disk, cache_disk_async, cache_mem), batching, deprecation, timeout, and ordering helpers.
- class scitex_decorators.AutoOrderDecorator(name)[source]
Decorator that collects and applies decorators in predefined order.
- scitex_decorators.batch_numpy_fn(func)
Combined decorator: numpy_fn → batch_fn.
Converts inputs to numpy arrays, then processes in batches. This is the recommended order for NumPy operations.
- Return type:
Example
>>> @numpy_batch_fn ... def process_data(x, axis=None): ... return np.mean(x, axis=axis)
- scitex_decorators.batch_pandas_fn(func)
Combined decorator: pandas_fn → batch_fn.
Converts inputs to pandas DataFrames, then processes in batches. This is the recommended order for Pandas operations.
- Return type:
Example
>>> @pandas_batch_fn ... def process_data(df): ... return df.describe()
- scitex_decorators.batch_torch_fn(func)
Combined decorator: torch_fn → batch_fn.
Converts inputs to torch tensors, then processes in batches. This is the recommended order for PyTorch operations.
- Return type:
Example
>>> @torch_batch_fn ... def process_data(x, dim=None): ... return x.mean(dim=dim)
- scitex_decorators.cache_disk(func)[source]
Disk caching decorator that uses joblib.Memory.
joblib is lazy-imported to keep
import scitex.decorators(and the transitiveimport scitex.iochain) usable on venvs without joblib installed. Without lazy-import, the eagerfrom joblib import MemoryraisedModuleNotFoundError: No module named 'joblib'at import time and broke any caller of scitex.io that didn’t need caching at all (todo#442, same class as #441 / #279).Usage:
@cache_disk def expensive_function(x): return x ** 2
- scitex_decorators.cache_disk_async(func)[source]
Disk caching decorator for async functions.
joblib is lazy-imported inside the decorator body so that
import scitex.decoratorsdoes not fail on venvs without joblib (todo#442, same class as #441 / #279). Without this, the eager top-levelfrom joblib import MemorypropagatesModuleNotFoundErrorup thescitex.ioimport chain and breaks unrelated callers.Usage:
@cache_disk_async async def expensive_async_function(x): await asyncio.sleep(1) return x ** 2
- scitex_decorators.cache_mem(user_function)
- scitex_decorators.deprecated(reason=None, forward_to=None)[source]
A decorator to mark functions as deprecated. It will result in a warning being emitted when the function is used.
- scitex_decorators.disable_auto_order()[source]
Disable auto-ordering and restore original decorators.
- scitex_decorators.enable_auto_order()[source]
Enable auto-ordering for all decorators in the scitex_decorators module.
This replaces the standard decorators with auto-ordering versions.
Example
>>> import scitex_decorators >>> scitex_decorators.enable_auto_order() >>> >>> # Now decorators will auto-order regardless of how they're written >>> @scitex_decorators.batch_fn >>> @scitex_decorators.torch_fn >>> def my_func(x): ... return x.mean()
- scitex_decorators.is_cuda(*args, **kwargs)[source]
Check if any input is a CUDA tensor.
- Return type:
- scitex_decorators.is_torch(*args, **kwargs)[source]
Check if any input is a PyTorch tensor.
- Return type:
- scitex_decorators.not_implemented(func)[source]
Decorator to mark methods as not implemented, issue a warning, and prevent their execution.
- Parameters:
func (callable) – The function or method to decorate.
- Returns:
A wrapper function that issues a warning and raises NotImplementedError when called.
- Return type:
callable
- scitex_decorators.numpy_batch_fn(func)[source]
Combined decorator: numpy_fn → batch_fn.
Converts inputs to numpy arrays, then processes in batches. This is the recommended order for NumPy operations.
- Return type:
Example
>>> @numpy_batch_fn ... def process_data(x, axis=None): ... return np.mean(x, axis=axis)
- scitex_decorators.pandas_batch_fn(func)[source]
Combined decorator: pandas_fn → batch_fn.
Converts inputs to pandas DataFrames, then processes in batches. This is the recommended order for Pandas operations.
- Return type:
Example
>>> @pandas_batch_fn ... def process_data(df): ... return df.describe()
- scitex_decorators.preserve_doc(loader_func)[source]
Wrap the loader functions to preserve their docstrings
- scitex_decorators.signal_fn(func)[source]
Decorator for signal processing functions that converts only the first argument (signal) to torch tensor.
This decorator is designed for DSP functions where: - The first argument is the signal data that should be converted to torch tensor - Other arguments (like sampling frequency, bands, etc.) should remain as-is
- Return type:
- scitex_decorators.to_numpy(*args, return_fn=<function _return_if>, **kwargs)[source]
Convert various data types to NumPy arrays.
- Return type:
- scitex_decorators.to_torch(*args, return_fn=<function _return_if>, device=None, **kwargs)[source]
Convert various data types to PyTorch tensors.
- Return type:
- scitex_decorators.torch_batch_fn(func)[source]
Combined decorator: torch_fn → batch_fn.
Converts inputs to torch tensors, then processes in batches. This is the recommended order for PyTorch operations.
- Return type:
Example
>>> @torch_batch_fn ... def process_data(x, dim=None): ... return x.mean(dim=dim)
- scitex_decorators.torch_fn(func)[source]
Decorator for PyTorch function compatibility.
Automatically converts inputs to PyTorch tensors and handles various data types gracefully. Preserves the original input type in the output.
Features
Converts inputs to PyTorch tensors
Preserves scalar parameters (int, float, bool, str)
Preserves dimension tuples like dim=(0, 1)
Handles nested lists/tuples gracefully
Automatically converts axis to dim for torch functions
Applies device=”cuda” if available
Returns output in same type as input (numpy->numpy, pandas->pandas, etc.)
- type func:
- param func:
The function to decorate
- type func:
Callable
- returns:
The decorated function
- rtype:
Callable
Examples
>>> @torch_fn ... def mean_squared(x, dim=None): ... return (x ** 2).mean(dim=dim) >>> >>> # Works with numpy arrays >>> result = mean_squared(np.array([1, 2, 3])) >>> >>> # Works with nested lists >>> result = mean_squared([[1, 2], [3, 4]]) >>> >>> # Preserves dimension tuples >>> result = mean_squared(data, dim=(0, 1))
Notes
For optimal performance with batch processing, apply torch_fn before batch_fn: @batch_fn @torch_fn def my_function(x): …
Or use auto-ordering to handle this automatically.
- scitex_decorators.wrap(func)[source]
Basic function wrapper that preserves function metadata.
Usage:
@wrap def my_function(x): return x + 1 # Or manually: def my_function(x): return x + 1 wrapped_func = wrap(my_function)
This wrapper is useful as a template for creating more complex decorators or when you want to ensure function metadata is preserved.