import numpy as np
import dascore as dc
from dascore.utils.array import PatchUFunc
# Create a patch ufunc from np.add
= PatchUFunc(np.add)
add_ufunc = dc.get_example_patch()
patch
# Use it to operate on patches
= add_ufunc(patch, patch)
result
# Use accumulate and reduce methods
= add_ufunc.accumulate(patch, dim="time")
cumsum = add_ufunc.reduce(patch, dim="distance")
total
# Bind to patch instance for cleaner syntax
= add_ufunc.__get__(patch, type(patch))
bound_ufunc = bound_ufunc.reduce(dim="time") # No patch argument needed result2
PatchUFunc
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
dim
parameter is used - Preserves patch coordinates and attributes appropriately
- Supports method binding via the descriptor protocol
- Methods call
dascore.utils.array.apply_ufunc
under the hood
Methods
Name | Description |
---|---|
accumulate | Apply ufunc accumulation along specified dimensions. |
reduce | Apply ufunc reduction along specified dimensions. |