WrightTools.kit.closest_pair

WrightTools.kit.closest_pair(arr, give='indicies')[source]

Find the pair of indices corresponding to the closest elements in an array.

If multiple pairs are equally close, both pairs of indicies are returned. Optionally returns the closest distance itself.

I am sure that this could be written as a cheaper operation. I wrote this as a quick and dirty method because I need it now to use on some relatively small arrays. Feel free to refactor if you need this operation done as fast as possible. - Blaise 2016-02-07

Parameters:
  • arr (numpy.ndarray) – The array to search.

  • give ({'indicies', 'distance'} (optional)) – Toggle return behavior. If ‘distance’, returns a single float - the closest distance itself. Default is indicies.

Returns:

List containing lists of two tuples: indicies the nearest pair in the array.

>>> arr = np.array([0, 1, 2, 3, 3, 4, 5, 6, 1])
>>> closest_pair(arr)
[[(1,), (8,)], [(3,), (4,)]]

Return type:

list of lists of two tuples