split_into_contig_seqs

mdtools.numpy_helper_functions.split_into_contig_seqs(a, step=1, step_tol=1e-08, sort=False, return_ix=False)[source]

Split an array into subarrays of contiguous sequences.

Parameters:
  • a (array_like) – 1-dimensional array which to split into subarrays of contiguous sequences with step size step.

  • step (scalar, optional) – The step size defining the contiguous sequence.

  • step_tol (scalar, optional) – A tolerance value for the step size. Values in a are considered to form a contiguous sequence if their difference lies within step +/- step_tol.

  • sort (bool, optional) – If True, sort a before searching for contiguous sequences.

  • return_ix (bool, optional) – If True, return the indices where a was split to create the subarrays. If sort is True, these are the indices where the sorted input array was split.

Returns:

  • contig_seqs (list) – List of subarrays, one for each contiguous sequence of numbers with step size step in a.

  • ix_split (numpy.ndarray) – Array of indices at which a was split. If sort is True, these are the indices at which the sorted input array was split.

See also

get_const_seqs()

Get all sequences of constant values in an array

References

Adapted from https://stackoverflow.com/a/7353335.

Examples

>>> a = np.array([-3, -2,  0,  3,  2,  4])
>>> contig_seqs, ix_split = mdt.nph.split_into_contig_seqs(
...     a, return_ix=True
... )
>>> contig_seqs
[array([-3, -2]), array([0]), array([3]), array([2]), array([4])]
>>> ix_split
array([2, 3, 4, 5])
>>> contig_seqs, ix_split = mdt.nph.split_into_contig_seqs(
...     a, sort=True, return_ix=True
... )
>>> contig_seqs
[array([-3, -2]), array([0]), array([2, 3, 4])]
>>> ix_split
array([2, 3])
>>> a = np.array([0, 1, 2, 5, 7, 9])
>>> mdt.nph.split_into_contig_seqs(a, step=2)
[array([0]), array([1]), array([2]), array([5, 7, 9])]
>>> a = np.array([0, 1, 2, 2, 1, 0])
>>> mdt.nph.split_into_contig_seqs(a, step=-1)
[array([0]), array([1]), array([2]), array([2, 1, 0])]
>>> a = np.array([1, 2, 2, 3, 3, 3])
>>> mdt.nph.split_into_contig_seqs(a, step=0)
[array([1]), array([2, 2]), array([3, 3, 3])]

Edge cases:

>>> a = np.array([])
>>> mdt.nph.split_into_contig_seqs(a, sort=True, return_ix=True)
([array([], dtype=float64)], array([], dtype=int64))
>>> a = np.arange(3)
>>> mdt.nph.split_into_contig_seqs(a, sort=True, return_ix=True)
([array([0, 1, 2])], array([], dtype=int64))