def merge_intervals(elems, overlap_thres=2.0):
    """
    Project in x axis
    Sort by start
    Go through segments and keep max x1

    Return a list of non-overlapping intervals
    """
    overlap_thres = max(0.0, overlap_thres)
    ordered = sorted(elems, key=lambda e: e.x0)

    intervals = []
    cur = [-overlap_thres, -overlap_thres]
    for e in ordered:
        if e.x0 - cur[1] > overlap_thres:
            # Check interval validity
            if cur[1] > 0.0:
                intervals.append(cur)
            cur = [e.x0, e.x1]
            continue
        cur[1] = max(cur[1], e.x1)
    intervals.append(cur)
    # Freeze the interval to tuples
    return map(tuple, intervals)
def merge_intervals ( elems , overlap_thres = 2.0 ) : overlap_thres = max ( 0.0 , overlap_thres ) ordered = sorted ( elems , key = lambda e : e . x0 ) intervals = [ ] cur = [ - overlap_thres , - overlap_thres ] for e in ordered : if e . x0 - cur [ 1 ] > overlap_thres : if cur [ 1 ] > 0.0 : intervals . append ( cur ) cur = [ e . x0 , e . x1 ] continue cur [ 1 ] = max ( cur [ 1 ] , e . x1 ) intervals . append ( cur ) return map ( tuple , intervals )
Project in x axis
    Sort by start
    Go through segments and keep max x1

    Return a list of non-overlapping intervals