get_const_seqs

mdtools.numpy_helper_functions.get_const_seqs(x, tol=1e-08, sort=False)[source]

Get all sequences of constant values in an array.

Parameters:
  • x (array_like) – 1-dimensional array for which to get all sequences of constant values.

  • tol (scalar, optional) – Tolerance value within which consecutive elements of x are considered to be equal.

  • sort (bool, optional) – If True, sort x before searching for sequences of constant values.

Returns:

  • seq_starts (numpy.ndarray) – Index array indicating the start of each constant sequence in x.

  • eq_lengths (numpy.ndarray) – The length (i.e. the number of values) of each constant sequence.

  • vals (numpy.ndarray) – The value of each constant sequence. Because consecutive values can differ by tol, this is the mean value of each constant sequence.

See also

split_into_contig_seqs()

Split an array into subarrays of contiguous sequences.

find_const_seq_n()

Find the first sequence of at least n constant values in an array

find_const_seq_long()

Find the longest sequence of constant values in an array

Examples

>>> a = np.array([0, 2, 2, 4, 4, 4, 3, 3, 1])
>>> mdt.nph.get_const_seqs(a)
(array([0, 1, 3, 6, 8]), array([1, 2, 3, 2, 1]), array([0., 2., 4., 3., 1.]))
>>> mdt.nph.get_const_seqs(a, sort=True)
(array([0, 1, 2, 4, 6]), array([1, 1, 2, 2, 3]), array([0., 1., 2., 3., 4.]))
>>> np.sort(a)
array([0, 1, 2, 2, 3, 3, 4, 4, 4])

Edge cases:

>>> a = np.array([])
>>> mdt.nph.get_const_seqs(a, sort=True)
(array([], dtype=int64), array([0]), array([], dtype=float64))
>>> a = np.ones(3)
>>> mdt.nph.get_const_seqs(a, sort=True)
(array([0]), array([3]), array([1.]))