maad.util.rle

maad.util.rle(x)[source]

Compute the Run-Length encoding (RLE) of a vector.

Run-length encoding is a lossless data compression in which runs of data (sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count. This is useful on data that contains many repeated values, for example, simple graphic images such as icons, line drawings, and animations. It is not useful with files that don’t have many runs as it could greatly increase the file size [1].

Parameters:
x1D array

vector with numbers (ndarray or list)

Returns:
lengths, values1D array

2 vectors that stores values (in values) and the number of times each value is repeated (in lengths).

References

Examples

RLE compression of the vector [1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 5]

>>> from maad.util import rle
>>> length, values = rle([1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 5])
>>> print('length:', length, '- values:', values)
length: [3 2 4 1 2] - values: [1 2 3 4 5]