chunk

method of dascore.core.spool.Spool source

chunk(
    self ,
    overlap: int | float | str | numpy.datetime64 | pandas.Timestamp | None[int, float, str, datetime64, Timestamp, None] = None,
    keep_partial: bool = False,
    snap_coords: Any = <object object at 0x75eb88f01e30>,
    tolerance: float | pint.registry.Quantity | numpy.timedelta64 | None[float, Quantity, timedelta64, None] = None,
    conflict: Literal[‘drop’, ‘raise’, ‘keep_first’] = raise,
    group: str | collections.abc.Sequence[str, collections.abc.Sequence[str], None] = None,
    on_missing_dim: Literal[‘raise’, ‘drop’] = raise,
    max_shift: float | None[float, None] = None,
    **kwargs ,
)-> ‘Self’

Chunk the data in the spool along specified dimension.

Parameters

Parameter Description
overlap The amount of overlap between each segment, starting with the end of
first patch. Negative values can be used to create gaps.
keep_partial If True, keep the segments which are smaller than chunk size.
This often occurs because of data gaps or at end of chunks.
snap_coords Deprecated and ignored from DASCore 1.0, with a warning: chunk
always fits one grid to each stretch of joined patches. Use
Spool.concatenate to join
patches keeping their recorded labels, and Patch.snap_coords
to regularize labels.
tolerance Off by default. A number joins seams up to that many steps wide
as well as those max_shift joins, closing each gap by
refitting the stretch’s grid as v0.1.24 did (a seam wider than
1.5 steps joined this way warns). A quantity or timedelta (eg
tolerance=1 * s) is the widest seam joined, in the
coordinate’s own units; patches with no step join where they
overlap or touch, and across a seam only under this form. A
dascore.utils.gaps.GapTolerance keeps its own meaning: its
excess is past one step.
conflict Indicates how to handle attributes which hold conflicting values across
the patches being combined (eg data_type, data_units, custom attrs). A
missing value (None, NaN, ““) is a value like any other: it equals
another missing one and nothing else, so a patch which never stated an
attribute conflicts with one which did. History, the SEG-Y text header
and the ids are never compared. If”raise” (default) raise an
AttributeMergeError for
conflicting values. If “drop”, omit the conflicting attributes from the
output. If “keep_first”, keep the first patch’s value of each. The same
policy decides a non-dimensional coordinate on a dimension other than the
combined one which the patches hold differently, or which some patch
lacks: “keep_first” keeps the first patch’s coordinate and “drop” leaves it
out, each with a warning naming it.
group Attributes which partition patches into separate outputs:
conflicting values are never an error, the patches simply land
in different outputs. A missing value (null or ““) is a value
like any other, so patches which never stated an attribute are
grouped together and apart from those which did. Defaults to
the config option
patch_kind_attrs; unlike the default, explicitly passed names
must exist on at least one patch. Dimensions and coordinate
identities always partition implicitly.
on_missing_dim What to do when patches lack the chunked dimension: “raise”
(default) or “drop” (exclude them from the output).
max_shift How far, as a fraction of a step, a seam may be from one step
wide and still join: two patches join when the later starts one
step (the earlier patch’s) after the earlier ends, give or take
this and the precision labels are stored in. A plain number,
0 <= max_shift < 0.5; None means dascore.config.max_shift
(0.2). It bounds each seam, not how far labels move in total.
kwargs kwargs are used to specify the dimension along which to chunk, eg:
time=10 chunks along the time axis in 10 second increments.
Windows start at start + k * (length - overlap) from each
stretch’s first label and are half-open, [start, start +<br>length), holding the samples whose labels on the stretch’s
grid fall inside, so windows without overlap share no sample;
only a final partial window is dropped, unless
keep_partial=True.
The value may also be a quantity: one of the coordinate’s own
units (time=10 * s) or a data size (time=25 * megabytes),
which chunks so each patch’s data array is about that large.
overlap accepts the same forms. An (n, 2) array of
explicit windows is refused: use
Spool.cut to join each window, or
Spool.select for each source’s
matching part unjoined.

Examples

import numpy as np

import dascore as dc
from dascore.units import s, megabytes

spool = dc.get_example_spool("random_das")
# get spools with time duration of 10 seconds
time_chunked = spool.chunk(time=10, overlap=1)
# the same, with the units stated explicitly
unit_chunked = spool.chunk(time=10 * s)
# get patches whose data arrays are at most ~1 MB
size_chunked = spool.chunk(time=1 * megabytes)
# merge along time axis
time_merged = spool.chunk(time=...)
# also join seams up to 1.5 steps wide, as v0.1.24 did by default
released = spool.chunk(time=..., tolerance=1.5)
# fill gaps of up to 10 seconds first, then chunk across them
gapless = spool.fill_gaps(time=10).chunk(time=...)
Note

Patches join into stretches across seams the seam test passes (see max_shift and tolerance). Where patches overlap, each sample is kept once and the earlier patch wins: a later patch continues from its first sample at least one step less max_shift past the earlier one’s end, if that sample passes the seam test, and starts a new stretch otherwise. Each stretch gets one grid before windows are cut: it starts at the stretch’s first label, and its step is the roundest one its patches state which keeps every patch’s first and last label within max_shift of its numbered position, or else the stretch’s span spread evenly over its samples. So every output is one evenly sampled run, and chunk outputs rejoin exactly, except from a stretch holding a patch with no step, which keeps its labels as recorded. So does a stretch needing a fractional step under 100 ticks, the only one kept for want of a grid: time stretches of any practical length get a grid.

Data-size chunks measure only the data array and round sample counts down. Mixed dtypes are sized after promotion.

Spool.concatenate performs a similar operation but disregards the coordinate values.

To inspect what a chunk call will do before running it — which output patches it produces and which slice of which source patch feeds each one — use Spool.chunk_plan, which takes the same arguments and returns the plan without touching any data.

See Also

Spool.cut joins each of a set of explicit windows, as chunk joins.

Spool.fill_gaps fills gaps, which chunk then joins across; chunk itself never fills.

Spool.select returns each source’s part of explicit windows, unjoined.

Keywords

spool, chunking, overlapping chunks, gaps, archive