get_dim_axis_value

function of dascore.utils.patch source

get_dim_axis_value(
    patch: Patch ,
    args: <class ‘tuple = (),
    kwargs: <class ’dict = <FrozenDict {}>,
    arg_keys: tuple[str] = (’dim’, ‘coord’, ‘dims’, ‘coords’),
    allow_multiple: bool = False,
    allow_extra: bool = False,
)-> ‘tuple[_DimAxisValue, …]’

Get dimension nane, index, and values from args/kwargs for a patch.

This is helpful for implementing flexible fetching of dimension name, corresponding patch axis, and function specific values from args and kwargs as inputs.

Parameters

Parameter Description
patch The patch which contains desired dimensions.
args A tuple of possible dimension names.
kwargs A dict of dimension_name: value
arg_keys Keys in the dictionary that indicate
allow_multiple If True, allow multiple dimensions to be selected.
allow_extra If True, do not raise an error if extra args or kwargs are found.

Returns

Returns a tuple of: ((dim, axis, value), (dim, axis, value), …) To support retreiving multiple values from the same inputs. If dim name is found in args, its corresponding values is None.

Examples

import dascore as dc
from dascore.utils.patch import get_dim_axis_value

patch = dc.get_example_patch()

# Get tuple of dimension name, axis, and value from dict (eg kwargs)
(dim, ax, val) = get_dim_axis_value(patch, kwargs={"time": 10})[0]
assert dim == "time" and ax == patch.dims.index("time") and val == 10

# Get dim name and axis from tuple (eg args)
(dim, ax, val) = get_dim_axis_value(patch, args=("time",))[0]
assert dim == "time" and ax == patch.dims.index("time") and val is None

# Get list of dim, ax val from multiple kwargs and args
info = get_dim_axis_value(
    patch, args=("time", ), kwargs={"distance": 10}, allow_multiple=True,
)
assert len(info) == 2