apply_operator

function of dascore.proc.basic source

apply_operator(
    patch: Patch | ndarray ,
    other: Patch | ndarray ,
    operator: Callable ,
    *args ,
    attrs_to_ignore = (‘history’, ‘dims’),
    **kwargs ,
)-> ‘PatchType’

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

import numpy as np
import dascore as dc
from dascore.proc.basic import apply_ufunc
patch = dc.get_example_patch()
# multiply the patch by 10
new = apply_ufunc(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_ufunc(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_ufunc(patch, patch, np.subtract)
assert np.allclose(new.data, 0)