import numpy as np
import dascore as dc
from dascore.utils.array import PatchUFunc
# Create a patch ufunc from np.add
add_ufunc = PatchUFunc(np.add)
patch = dc.get_example_patch()
# Use it to operate on patches
result = add_ufunc(patch, patch)
# Use accumulate and reduce methods
cumsum = add_ufunc.accumulate(patch, dim="time")
total = add_ufunc.reduce(patch, dim="distance")
# Bind to patch instance for cleaner syntax
bound_ufunc = add_ufunc.__get__(patch, type(patch))
result2 = bound_ufunc.reduce(dim="time") # No patch argument neededPatchUFunc
PatchUFunc(
np_ufunc ,
)
A ufunc wrapper that can be applied to patches with dimension support.
This class wraps numpy ufuncs to work seamlessly with DASCore patches, providing support for dimension-aware operations, coordinate preservation, and method binding.
Parameters
| Parameter | Description |
|---|---|
| np_ufunc : np.ufunc | The numpy ufunc to wrap. |
Examples
Note
- Automatically handles dimension-to-axis conversion when
dimparameter is used - Preserves patch coordinates and attributes appropriately
- Supports method binding via the descriptor protocol
- Methods call
dascore.utils.array.apply_ufuncunder the hood
Methods
| Name | Description |
|---|---|
| accumulate | Apply ufunc accumulation along specified dimensions. |
| reduce | Apply ufunc reduction along specified dimensions. |