get_coord

function of dascore.core.coords source

get_coord(
    data: ndarray | BaseCoord | Sequence | int | None = None,
    values: ndarray | None = None,
    start = None,
    min = None,
    stop = None,
    max = None,
    step = None,
    units: pint.registry.Unit | pint.registry.Quantity | str | None[Unit, Quantity, str, None] = None,
    shape: int | tuple[int, tuple[int, …], None] = None,
    dtype: str | numpy.dtype | None[str, dtype, None] = None,
    segments: tuple[tuple[BaseCoord, …], list[BaseCoord], None] = None,
    runs: tuple[tuple[dascore.core.coords.Grid | dascore.core.coords.Labels[Grid, Labels], …], None] = None,
    sources: collections.abc.Mapping[collections.abc.Mapping[str, ndarray], None] = None,
    snap: bool | None[bool, None] = None,
)-> ‘BaseCoord’

Return a coordinate from provided inputs.

Parameters

Parameter Description
data An array indicating the values or an integer to specify the length
of a partial coordinate.
values Alias for data.
start The start value of the array, inclusive.
min The minimum value, same as start.
stop The stopping value of an array, exclusive.
max Alias for stop; exclusive, like stop.
step The sampling spacing of an array. With start, a Fraction or a
(numerator, denominator) tuple states an exact fractional step
for time (in seconds) or integer coordinates.
units Indication of units.
shape If an int or tuple, the output should be a partial coord of with
this shape. Otherwise, leave unset.
dtype Data type for coord. Often can be inferred from other arguments.
segments A sequence of monotonic coordinates to concatenate into one
coordinate (see concat_coords).
Cannot be combined with other value inputs.
runs The runs (see NumericCoord)
the coordinate holds, normally from a dumped coordinate. A blank
run is rebuilt as it was, unless its start, stop and step state an
evenly sampled range of its own length, which it becomes.
sources The arrays the stored runs window, keyed by id; they accompany
runs in a dumped coordinate. An id is taken on trust rather
than recomputed, so a dump is a contract: pass back the arrays it
named, not arrays of your own under its keys.
snap How labels are read. None (default) or False reads them exactly:
labels a fitted grid restates at consecutive positions are that grid,
float ones moving by up to twice the float allowance (a millionth of
a step or 4 ulp in their stored dtype, whichever is larger); anything
else is labels with no step. False differs only in making a
zero-dimensional array one label. DASCore 0.1.24 instead snapped
labels within 0.1% of one spacing onto a grid, which True still does.
A declared step places labels on its grid likewise, a segment per
stretch of consecutive positions, up to max_index_runs segments.
Note

See ‘Coordinate Internals’ for dispatch and coord-family design notes.

The following combinations of input parameters are typical: (start, stop, step) (data) (data, step) - useful for length 1 arrays. (values) (values, step) - useful for length 1 arrays.

Examples

import numpy as np
from dascore.core import get_coord

# Create a coordinate from a start, stop, and range value.
range_coord = get_coord(start=1, stop=12, step=1)

# Create an identical coordinate from an array.
array_coord = get_coord(data=np.arange(1, 12, 1))
# This array coord should return an identical coordinate
assert range_coord == array_coord

# Coordinate from an array that is sorted, but not evenly sampled
array = np.sort(np.random.rand(20))
array_coord2 = get_coord(data=array)

# Coordinate from random array
array = np.random.rand(20)
array_coord3 = get_coord(data=array)

# Create a partial coordinate of a given shape
partial_coord = get_coord(shape=(10,))

# A time coordinate sampled at exactly 1024 Hz (a fraction of a
# nanosecond) never drifts from its grid.
time = get_coord(
    start=np.datetime64("2020-01-01"), step=(1, 1024), shape=(2048,)
)
assert time.step_exact == 1 / 1024 and time[::2].step_exact == 1 / 512