def merge_segments()

in voxpopuli/segmentation/run_pyannote_sd.py [0:0]


def merge_segments(path_list_pkl: List[Path], size_overlap: float):

    out = []
    shift = 0
    last_start = None
    for i_pkl, pkl_path in enumerate(path_list_pkl):
        with open(pkl_path, "rb") as f:
            annotation = pkl.load(f)
        segments = [
            (
                shift + round(segment.start, 3),
                shift + round(segment.end, 3),
                f"{i_pkl}_{label}",
            )
            for segment, track, label in annotation.itertracks(yield_label=True)
        ]
        if len(segments) == 0:
            continue

        start_index = 0
        if last_start is not None:
            min_diff = size_overlap + 1
            for i, pack in enumerate(segments):
                s = pack[0]
                d = abs(s - last_start)
                if d < min_diff:
                    min_diff = d
                    start_index = i

        if len(out) > 0:
            s, e, l = segments[start_index]
            out[-1] = last_start, e, l
            start_index += 1

        if start_index < len(segments):
            out += segments[start_index:]

        if len(out) > 0:
            last_start = out[-1][0]

        shift += size_overlap
    return out