find_nearest in py_tools_ds can be much faster

Several issues:

  • The array sorting is slow and only required if the input array is not already sorted. In actual usages, the arrays are mostly created using np.arange. Way faster with an is_sorted flag in those cases.
if isinstance(array, list):
    array = np.array(array)
if array.ndim > 1:
    array = array.flatten()
if not is_sorted:
    array = np.sort(array)
minimum, maximum = array[0], array[-1]
  • The array is already sorted, yet the code uses min(array) and max(array). That's unnecessary, those are just the first and last elements
  • The array is already sorted, the collections.Counter use can be replaced by something like
neighbours = diffs[max(minIdx-1,0):min(minIdx+2, len(diffs)-1)]
isMiddleVal = len(neighbours) > len(np.unique(neighbours))
Edited by Roelof van Dijk