sequenize

mdtools.numpy_helper_functions.sequenize(a, step=1, start=0)[source]

“Sequenize” the values of an array.

Assign to each different value in an array a a constant offset in such a way that subtracting the resulting offset array from a yields a new array whose unique sorted values yield a contiguous sequence with a given step width.

This might for example be useful if you have an array containing some indices and one or more intermediate indices are missing but you want the indices to form a contiguous sequence.

Parameters:
  • a (array_like) – The array whose values should yield a contiguous sequence with a given step width when computing the unique sorted values of the array.

  • step (scalar, optional) – The step width of this sequence. The data type of a and step must be castable.

  • start (scalar, optional) – The value at which this sequence should start. The data type of a and start must be castable.

Returns:

b (numpy.ndarray) – A ‘sequenized’ version of a. When calling numpy.unique on b, the returned array will be a contiguous sequence with step width step.

Examples

>>> a = np.array([-5, -3, -1,  2,  4,  6])
>>> mdt.nph.sequenize(a)
array([0, 1, 2, 3, 4, 5])
>>> mdt.nph.sequenize(a, step=3)
array([ 0,  3,  6,  9, 12, 15])
>>> mdt.nph.sequenize(a, step=-3)
array([  0,  -3,  -6,  -9, -12, -15])
>>> mdt.nph.sequenize(a, start=-(a.size - 1))
array([-5, -4, -3, -2, -1,  0])
>>> a = np.array([[-5, -3, -1],
...               [ 2,  4,  6]])
>>> mdt.nph.sequenize(a)
array([[0, 1, 2],
       [3, 4, 5]])
>>> mdt.nph.sequenize(a, step=3)
array([[ 0,  3,  6],
       [ 9, 12, 15]])
>>> mdt.nph.sequenize(a, step=-3)
array([[  0,  -3,  -6],
       [ -9, -12, -15]])
>>> mdt.nph.sequenize(a, start=-(a.size - 1))
array([[-5, -4, -3],
       [-2, -1,  0]])
>>> a = np.arange(6)
>>> np.all(mdt.nph.sequenize(a) == a)
True
>>> mdt.nph.sequenize(a, step=2)
array([ 0,  2,  4,  6,  8, 10])
>>> mdt.nph.sequenize(a, step=-2)
array([  0,  -2,  -4,  -6,  -8, -10])
>>> mdt.nph.sequenize(a, start=-(a.size - 1))
array([-5, -4, -3, -2, -1,  0])
>>> a = np.arange(6).reshape(2, 3)
>>> np.all(mdt.nph.sequenize(a) == a)
True
>>> mdt.nph.sequenize(a, step=2)
array([[ 0,  2,  4],
       [ 6,  8, 10]])
>>> mdt.nph.sequenize(a, step=-2)
array([[  0,  -2,  -4],
       [ -6,  -8, -10]])
>>> mdt.nph.sequenize(a, start=-(a.size - 1))
array([[-5, -4, -3],
       [-2, -1,  0]])