find_const_seq_n

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

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

Parameters:
  • x (array_like) – 1-dimensional array in which to find the first sequence of at least n 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:

  • start_ix (int) – Index indicating the start of the first sequence of at least n constant values in x.

  • length (int) – The actual length of this sequence.

  • val (scalar) – The value of this sequence. Because consecutive values can differ by tol, this is the mean value of the sequence.

See also

get_const_seqs()

Get all sequences of constant values in an array

find_const_seq_long()

Find the longest sequence of constant values in an array

Notes

If the array does not contain a sequence of at least n constant values, (array([]), 0, array([]))` is returned.

Examples

>>> a = np.array([0, 4, 4, 4, 2, 2])
>>> mdt.nph.find_const_seq_n(a, n=2)
(1, 3, 4.0)
>>> mdt.nph.find_const_seq_n(a, n=2, sort=True)
(1, 2, 2.0)
>>> np.sort(a)
array([0, 2, 2, 4, 4, 4])
>>> a = np.array([0, 2, 2, 4, 4, 4, 2, 2, 0])
>>> mdt.nph.find_const_seq_n(a, n=2)
(1, 2, 2.0)
>>> mdt.nph.find_const_seq_n(a, n=3)
(3, 3, 4.0)
>>> mdt.nph.find_const_seq_n(a, n=2, sort=True)
(0, 2, 0.0)
>>> np.sort(a)
array([0, 0, 2, 2, 2, 2, 4, 4, 4])

Edge cases:

>>> a = np.array([1, 2, 2, 3, 3, 3])
>>> mdt.nph.find_const_seq_n(a, n=4)
(array([], dtype=int64), 0, array([], dtype=float64))
>>> mdt.nph.find_const_seq_n(a, n=0)
(0, 1, 1.0)
>>> mdt.nph.find_const_seq_n(a, n=-1)
(0, 1, 1.0)
>>> a = np.array([])
>>> mdt.nph.find_const_seq_n(a, n=1, sort=True)
(array([], dtype=int64), 0, array([], dtype=float64))
>>> a = np.ones(3)
>>> mdt.nph.find_const_seq_n(a, n=1, sort=True)
(0, 3, 1.0)