import numpy as np
import dascore as dc
from dascore.proc.basic import apply_ufunc
= dc.get_example_patch()
patch # multiply the patch by 10
= apply_ufunc(patch, 10, np.multiply)
new assert np.allclose(patch.data * 10, new.data)
# add a random value to each element of patch data
= np.random.random(patch.shape)
noise = apply_ufunc(patch, noise, np.add)
new assert np.allclose(new.data, patch.data + noise)
# subtract one patch from another. Coords and attrs must be compatible
= apply_ufunc(patch, patch, np.subtract)
new assert np.allclose(new.data, 0)
apply_operator
Apply a ufunc-type operator to a patch.
This is used to implement a patch’s operator overloading.
Parameters
Parameter | Description |
---|---|
patch | The patch instance. |
other |
The other object to apply the operator element-wise. Must be either a non-patch which is broadcastable to the shape of the patch’s data, or a patch which has compatible coordinates. If units are provided they must be compatible. |
operator | The operator. Must be numpy ufunc-like. |
*args | Arguments to pass to the operator. |
attrs_to_ignore | Attributes to ignore when considering if patches are compatible. |
**kwargs | Keyword arguments to pass to the operator. |
Examples
Note