import numpy as np
import dascore as dc
from dascore.proc.basic import apply_operator
= dc.get_example_patch()
patch # multiply the patch by 10
= apply_operator(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_operator(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_operator(patch, patch, np.subtract)
new assert np.allclose(new.data, 0)
apply_operator
apply_operator(
patch: Patch ,
other ,
operator ,
)-> ‘PatchType’
Apply a ufunc-type operator to a patch.
This is used to implement a patch’s operator overload.
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. |