[Python] How to get start indices of regions of empty intervals?

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I have sorted start indices (included) and end indices (excluded) of intervals (obtained by using seachsorted), for instance:

    import numpy as np

    # Both arrays are of same size, and sorted.
    # Size of arrays is number of intervals.
    # Intervals do not overlap.

    # interval indices: 0 1 2 3 4 5
    interval_start_idxs = np.array([0, 3, 3, 3, 6, 7])
    interval_end_excl_idxs = np.array([2, 4, 4, 4, 7, 9])


    An empty interval is identified when

    interval_start_idxs[interval_idx] == interval_end_excl_idxs[interval_idx]-1


    I would like to identify the starts and ends of each region where intervals are empty. A region is made with one or several intervals sharing the same start indices and end excluded indices.

    With previous data, expected result would then be:

    empty_interval_starts = [1, 4] # start is included
    empty_intervals_ends_excl = [4, 5] # end is excluded


    This result is to be understood as:

    • intervals from index 1 to 3, these intervals are a same region of empty intervals
    • and interval at index 4 is a separate region on its own

    Continue reading...

Compartilhe esta Página