ethograph.labels.ml.fix_endings#

ethograph.labels.ml.fix_endings(labels, changepoints)[source]#

Extend label endings by one sample at changepoint boundaries.

When a labelled segment ends and the very next sample is a changepoint, the segment is extended by one sample. This accounts for the common off-by-one between predicted segment boundaries and detected changepoints.

Parameters:
  • labels (np.ndarray) – 1-D dense label array (int).

  • changepoints (array-like) – Either a boolean mask of the same length (True = changepoint), or an array of integer changepoint indices.

Returns:

Copy of labels with qualifying endings extended by one sample.

Return type:

np.ndarray

Examples

>>> import numpy as np
>>> from ethograph.labels.ml import fix_endings
>>> labels = np.array([0, 1, 1, 0, 0, 2, 2, 0])
>>> cps     = np.array([0, 0, 0, 1, 0, 0, 0, 1], dtype=bool)
>>> fix_endings(labels, cps).tolist()
[0, 1, 1, 1, 0, 2, 2, 2]

The segment of label 1 ended at index 2, and index 3 is a changepoint, so label 1 is extended to index 3. Same for label 2 at index 7.