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", value=0).data[5:8] == 0).all()fill_gaps
fill_gaps(
patch: Patch ,
*args ,
value: Any = nan,
samples: bool = False,
**kwargs ,
)-> ‘PatchType’
Fill the holes along a dimension with a constant value.
Places runs of samples on one evenly sampled grid and writes 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").
|
| 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.