create_ordering

sofia_redux.toolkit.splines.spline_utils.create_ordering(indices, size=-1)[source]

Given a list of indices, create an ordering structure for fast access.

The purpose of this function is to create two arrays than can be used to quickly retrieve all reverse indices for a given index using numba. For example, consider the array x:

>>> x = np.array([0, 0, 2, 1, 4, 2, 1, 2, 2, 1])
>>> si, ni = create_ordering(x)

We can now extract the indices on x for a given value. E.g.,

>>> ri = []
>>> for value in [0, 1, 2, 3, 4]:
...     inds = []
...     ri.append(inds)
...     reverse_index = si[value]
...     while reverse_index != -1:
...         inds.append(reverse_index)
...         reverse_index = ni[reverse_index]
>>> print(ri)
[[1, 0], [9, 6, 3], [8, 7, 5, 2], [], [4]]

i.e., the indices on x where x is equal to 0 are [1, 0], and [8, 7, 5, 2] where x is equal to 2.

This is a very useful feature for N-D numba functions as reverse indices can be precalculated and represented as 1-dimensional arrays and we avoid functions such as np.nonzero, argwhere etc, that introduce additional overhead associated with array creation.

Parameters:
indicesnumpy.ndarray (int)

A list of input indices.

sizeint

The size of the array to return.

Returns:
start_indices, next_indicesnumpy.ndarray (int), numpy.ndarray (int)