Beispiel #1
0
def _find_local_maxima(ordered_RD, k=1):
    """
    Find local maxima to the k neighbor of left and k neighbors of right.\
    It equivalent to a moving windows of size 2*k+1. RD is an 1D numpy array.\

    Since we are looking for max, a max heap would be most beneficial.\
    For the sake of simplicity, we just take the negtive of RD, and use \
    a min heap already implemented a while ago.    
    """
    local_maxima = heapdict()
    moving_window = BinHeap()
    window_size = 2 * k + 1
    poi_idx = -k  # index for point of interest start from -k from convinience.
    RD_size = len(ordered_RD)

    assert window_size < RD_size, 'Invalid k or RD, window size can not be smaller than RD size'

    while poi_idx < RD_size:
        w_s_idx = poi_idx - k  # window start idx
        w_e_idx = poi_idx + k  # window end idx
        if w_e_idx < RD_size:
            moving_window.insert(
                (-ordered_RD[w_e_idx], w_e_idx)
            )  # negtive is to accomadate use of min heap, since RD is always non-negtive.

        if (moving_window.heap_size > window_size) or (w_e_idx >= RD_size):
            heap_el_idx = moving_window.pos[w_s_idx - 1]
            moving_window.extract_element(
                heap_el_idx)  # delete elements outside of the window.

        if poi_idx >= 0:
            current_RD = -ordered_RD[poi_idx]
            if current_RD <= moving_window.heap[0][0]:
                local_maxima[
                    poi_idx] = current_RD  # a negative RD is also used for local maxima since heapdict is a min priority queue
        poi_idx += 1

    return local_maxima