Skip to content

nabu.processing.utils

[docs] module nabu.processing.utils

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import numpy as np

try:
    from numba import njit

    __have_numba__ = True
except ImportError:
    __have_numba__ = False


def _range_counting(array, tol=1e-7):
    n = len(array)
    order = np.argsort(array)
    arr_sorted = array[order]

    w_sorted = np.zeros(n, dtype=np.intp)

    left = 0
    right = 0
    for i in range(n):
        # expand right boundary
        while right < n and arr_sorted[right] - arr_sorted[i] < tol:
            right += 1
        # shrink left boundary
        while arr_sorted[i] - arr_sorted[left] >= tol:
            left += 1

        w_sorted[i] = right - left

    # unsort back to original order
    weights = np.zeros_like(w_sorted)
    weights[order] = w_sorted
    return weights


if __have_numba__:
    range_counting = njit(parallel=False)(_range_counting)
else:
    range_counting = _range_counting