create_elementwise_class#
- deeptrack.elementwise.create_elementwise_class(name: str, function: Callable[[Any], Any], docstring: str = '', function_name: str | None = None) type[ElementwiseFeature]#
Factory function to create subclasses of ElementwiseFeature.
This function generates a new subclass of ElementwiseFeature that applies the given function elementwise to a NumPy array or PyTorch tensor. It dynamically sets the class name, qualified name, module, and docstring to make the generated class fully compatible with IDEs and documentation tools such as Sphinx.
Parameters#
- name: str
Name of the new class to be created (e.g., “Sin”, “Exp”).
- function: Callable[[array], array] | Callable[[tensor], tensor]
The elementwise function to apply, such as np.sin, torch.exp, or xp.abs. The arrays can be NumPy arrays or PyTorch tensors.
- docstring: str, optional
The docstring for the generated class. This string will be visible in IDE tooltips and Sphinx documentation.
- function_name: str | None, optional
The name of the function, used for error messages.
Returns#
- type[ElementwiseFeature]
A dynamically generated subclass of ElementwiseFeature that wraps the given function.
Examples#
>>> import deeptrack as dt
Import the backend-agnostic functionality from DeepTrack2:
>>> from deeptrack.backend import xp
Create an elementwise feature to execute a backend-agnostic function:
>>> from deeptrack.elementwise import create_elementwise_class >>> >>> Abs = create_elementwise_class( ... name="Abs", ... function=xp.abs, ... docstring="Elementwise abs function." ... )
NumPy backend with direct resolved input
>>> import numpy as np >>> >>> array = np.array([-1.0, 0.0, 2.5]) >>> result = Abs()(array) >>> result array([1. , 0. , 2.5])
PyTorch backend with direct resolved input
>>> import torch >>> >>> tensor = torch.tensor([-1.0, 0.0, 2.5]) >>> result = Abs()(tensor) >>> result tensor([1.0000, 0.0000, 2.5000])
NumPy pipeline
>>> value = dt.Value(value=np.array([-3.0, 0.0, 3.0])) >>> pipeline = value >> Abs() >>> result = pipeline() >>> result array([3., 0., 3.])
This is equivalent to:
>>> pipeline = Abs(value)
PyTorch pipeline
>>> value = dt.Value(value=torch.tensor([-3.0, 0.0, 3.0])) >>> pipeline = value >> Abs() >>> result = pipeline() >>> result tensor([3., 0., 3.])
This is equivalent to:
>>> pipeline = Abs(value)