import dascore as dc
from dascore.core.source import ArraySource
from dascore.lazy.contract import Pointwise
from dascore.lazy.recipe import Recipe
class Negate(dc.PatchProcessor):
'''Negate every sample.'''
name = None # so this example claims no name of its own
def chunk_contract(self, dtype, plan):
return Pointwise()
def kernel(self, data):
return -data
recipe = Recipe.from_source(ArraySource.full((3, 2), -1.0))
patch = dc.Patch(data=recipe, coords={"x": range(3), "y": range(2)},
dims=("x", "y"))
out = Negate()(patch)
assert out.backend == "lazy" and len(out.data.chain) == 1
assert out.data.chain.last.data_id == out.attrs.data_id
assert (out.load().data == 1).all()kernels
The kernel every operation runs on a lazy array, unless it registers its own.
It computes nothing. It asks the processor for its chunk contract for the input’s dtype and the plan – a contract written above the class which owns the kernel describes some other kernel, so it does not count – then:
- no contract, or a
reconcileof the processor’s own, which would be handed the lazy array: refuses, before anything is read; Unchanged: hands the data back, so the operation is the same no-op it is on data in memory;Pointwise: appends the operation to the chain, pending until the operation settles it with the shape, dtype anddata_idof what it made.
PatchProcessor.kernel_for answers with it for the lazy backend.
Examples
Functions
| Name | Description |
|---|---|
| lazy_kernel | Answer for an operation on a lazy array by its chunk contract. |