import numpy as np
import dascore as dc
from dascore.core.coords import concat_coords, get_coord
# A patch whose distance coordinate misses three samples.
dist = concat_coords(
get_coord(start=0.0, stop=5.0, step=1.0),
get_coord(start=8.0, stop=10.0, step=1.0),
)
patch = dc.Patch(
data=np.ones((len(dist), 3)),
coords={"distance": dist, "time": dc.to_datetime64(np.arange(3))},
dims=("distance", "time"),
)
filled = patch.fill_gaps("distance")
assert filled.shape == (10, 3)
assert np.isnan(filled.data[5:8]).all()
# Fill only holes of at most two missing samples: this one stays.
assert patch.fill_gaps(distance=2, samples=True).shape == patch.shape
# Fill with zeros instead of NaN.
assert (patch.fill_gaps("distance", fill_value=0).data[5:8] == 0).all()FillGaps
FillGaps(
*args: Any ,
fill_value: Any = nan,
samples: Any = False,
**kwargs ,
)-> None
Fill the holes along a dimension with a constant value.
Places runs of samples on one evenly sampled grid and writes fill_value where no sample sits, so a segmented coordinate (for example from Spool.chunk with snap_coords=False across a gap) becomes a plain range, unless a limit leaves wider holes as seams.
Parameters
| Parameter | Description |
|---|---|
| patch | The patch to fill. |
| *args |
The dimension to fill, eg patch.fill_gaps("time").
|
| fill_value |
The value written at filled positions. It must fit the data’s dtype: NaN cannot fill integer data, so pass an integer or cast the data to float first. |
| samples | If True, the limit given with the dimension counts missing samples. |
| **kwargs |
The dimension and the widest hole to fill, eg time=10 fills holesmissing up to ten seconds of samples and leaves wider ones as seams. A hole’s width is its missing samples times the step, one step less than the jump between the labels either side. Give the limit in the coordinate’s units (seconds for time), or as a quantity or timedelta; None fills every hole. |
The coordinate needs a declared step: a segmented coordinate whose runs share one step (different steps raise; resample first), or an array declared with a step. A sample or run off the grid moves to the nearest position, by at most half a step.
Non-dimensional coordinates along the dimension are dropped with a warning, since their values at the filled positions are unknown.
Examples
Methods
| Name | Description |
|---|---|
| check | Refuse a patch which does not carry what the operation needs. |
| get_metadata | Return the filled grid, and where each run of samples lands on it. |
| model_copy | Copy the model, dropping cached values the update invalidates. |
| new | Create new instance with some attributed updated. |
| numpy_kernel | Return the runs placed on the grid with the fill value between them. |
| fill_gaps | Fill the holes along a dimension with a constant value. |
| reconcile | Return metadata or a patch holding the final data; default as is. |
| run | Run the operation: check, get_metadata, kernel, reconcile, record. |