sel_valid#

ethograph.sel_valid(da, sel_kwargs)[source]#

Select a slice of a DataArray, silently ignoring dimensions it doesn’t have.

Useful when the GUI holds a single sel_kwargs dict (e.g. {"keypoints": "nose", "space": "x"}) but different features have different subsets of those dimensions. Dimensions with labelled coordinates are selected with .sel(); dimensions without coordinates fall back to .isel(). The result is squeezed and transposed so time is always the first axis.

Parameters:
  • da (xarray.DataArray) – Source array. Must contain at least one dimension whose name includes "time".

  • sel_kwargs (dict) – Candidate selections. Keys that don’t match any dimension in da are silently dropped.

Returns:

  • data (numpy.ndarray) – Selected values with shape (n_time,) or (n_time, n_other).

  • used_kwargs (dict) – The subset of sel_kwargs that were actually applied via .sel() (i.e. only label-based selections, not integer-based ones). Handy for building plot titles.

Raises:

ValueError – If da has no dimension containing "time".

Examples

>>> import xarray as xr, numpy as np
>>> da = xr.DataArray(
...     np.random.randn(100, 3),
...     dims=["time", "space"],
...     coords={"time": np.linspace(0, 10, 100), "space": ["x", "y", "z"]},
... )
>>> data, used = eto.sel_valid(da, {"space": "x", "individuals": "mouse1"})
>>> data.shape
(100,)
>>> used
{'space': 'x'}