fill_gaps

function of dascore.proc.coords source

fill_gaps(
    self ,
    *args ,
    fill_value: Any = nan,
    samples: bool = False,
    **kwargs ,
)-> ‘Self’

Fill the gaps between a dimension’s segments with a constant value.

Joins segments onto one grid, on the earlier segment’s step, and writes fill_value where no sample sits, so a segmented coordinate (from Spool.concatenate, or Patch.select with a mask removing samples) becomes one grid, unless a limit leaves wider gaps as segment boundaries.

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 holes
missing 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.
Note

The segment after each gap filled moves onto the grid of the one before it, to the positions nearest its labels, by under half a step; a move beyond the rounding of a float label warns, saying how far. A segment whose samples do not fit consecutive positions of that grid within half a step (another step, or labels with none) raises: resample or snap it first. So does a fill value the data’s dtype cannot hold.

Non-dimensional coordinates along the dimension take NaN (NaT for times) at the filled positions; those of other dtypes are dropped with a warning.

Examples

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()