apply_operator

function of dascore.proc.basic source

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.

Examples

import numpy as np
import dascore as dc
from dascore.proc.basic import apply_operator
patch = dc.get_example_patch()
# multiply the patch by 10
new = apply_operator(patch, 10, np.multiply)
assert np.allclose(patch.data * 10, new.data)
# add a random value to each element of patch data
noise = np.random.random(patch.shape)
new = apply_operator(patch, noise, np.add)
assert np.allclose(new.data, patch.data + noise)
# subtract one patch from another. Coords and attrs must be compatible
new = apply_operator(patch, patch, np.subtract)
assert np.allclose(new.data, 0)