librosa.util.index_to_slice

librosa.util.index_to_slice(idx, *, idx_min=None, idx_max=None, step=None, pad=True)[source]

Generate a slice array from an index array.

Parameters:
idxlist-like

Array of index boundaries

idx_min, idx_maxNone or int

Minimum and maximum allowed indices

stepNone or int

Step size for each slice. If None, then the default step of 1 is used.

padboolean

If True, pad idx to span the range idx_min:idx_max.

Returns:
sliceslist of slice

slices[i] = slice(idx[i], idx[i+1], step) Additional slice objects may be added at the beginning or end, depending on whether pad==True and the supplied values for idx_min and idx_max.

See also

fix_frames

Examples

>>> # Generate slices from spaced indices
>>> librosa.util.index_to_slice(np.arange(20, 100, 15))
[slice(20, 35, None), slice(35, 50, None), slice(50, 65, None), slice(65, 80, None),
 slice(80, 95, None)]
>>> # Pad to span the range (0, 100)
>>> librosa.util.index_to_slice(np.arange(20, 100, 15),
...                             idx_min=0, idx_max=100)
[slice(0, 20, None), slice(20, 35, None), slice(35, 50, None), slice(50, 65, None),
 slice(65, 80, None), slice(80, 95, None), slice(95, 100, None)]
>>> # Use a step of 5 for each slice
>>> librosa.util.index_to_slice(np.arange(20, 100, 15),
...                             idx_min=0, idx_max=100, step=5)
[slice(0, 20, 5), slice(20, 35, 5), slice(35, 50, 5), slice(50, 65, 5), slice(65, 80, 5),
 slice(80, 95, 5), slice(95, 100, 5)]