torch_ecg.utils.mask_to_intervals

torch_ecg.utils.mask_to_intervals(mask: ndarray, vals: int | Sequence[int] | None = None, right_inclusive: bool = False) list | dict[source]

Convert a mask into a list of intervals, or a dict of lists of intervals.

Parameters:
  • mask (numpy.ndarray) – The (1D) mask to convert.

  • vals (int or Sequence[int], optional) – Values in mask to obtain the intervals.

  • right_inclusive (bool, default False) – If True, the intervals will be right inclusive, otherwise, right exclusive.

Returns:

intervals – The intervals corr. to each value in vals if vals is None or of sequence type; or the intervals corr. to vals if vals is int. each interval is of the form [a, b].

Return type:

dict or list

Examples

>>> mask = np.zeros(100, dtype=int)
>>> mask[10: 20] = 1
>>> mask[80: 95] = 1
>>> mask[50: 60] = 2
>>> mask_to_intervals(mask, vals=1)
[[10, 20], [80, 95]]
>>> mask_to_intervals(mask, vals=[0, 2])
{0: [[0, 10], [20, 50], [60, 80], [95, 100]], 2: [[50, 60]]}
>>> mask_to_intervals(mask)
{0: [[0, 10], [20, 50], [60, 80], [95, 100]], 1: [[10, 20], [80, 95]], 2: [[50, 60]]}
>>> mask_to_intervals(mask, vals=[1, 2], right_inclusive=True)
{1: [[10, 19], [80, 94]], 2: [[50, 59]]}