built in function for computing overlap in Python

徘徊边缘 提交于 2019-11-27 02:11:04

问题


is there a built in function to compute the overlap between two discrete intervals, e.g. the overlap between [10, 15] and [20, 38]? In that case the overlap is 0. If it's [10, 20], [15, 20], the overlap is 5.


回答1:


You can use max and min:

>>> def getOverlap(a, b):
...     return max(0, min(a[1], b[1]) - max(a[0], b[0]))

>>> getOverlap([10, 25], [20, 38])
5
>>> getOverlap([10, 15], [20, 38])
0



回答2:


Check out pyinterval http://code.google.com/p/pyinterval/

import interval
x=interval.interval[10, 15]
y=interval.interval[20, 38]
z=interval.interval[12,18]

print(x & y)
# interval()
print(x & z)
# interval([12.0, 15.0])



回答3:


Here is a good function from Aaron Quinlan's chrom_sweep, modified for your interval representation. It returns the number of bp of overlap if they do overlap, otherwise it returns the distance as a negative int.

def overlaps(a, b):
    """
    Return the amount of overlap, in bp
    between a and b.
    If >0, the number of bp of overlap
    If 0,  they are book-ended.
    If <0, the distance in bp between them
    """

    return min(a[1], b[1]) - max(a[0], b[0])



回答4:


Just wrote this:

def overlap(interval1, interval2):
    """
    Given [0, 4] and [1, 10] returns [1, 4]
    """
    if interval2[0] <= interval1[0] <= interval2[1]:
        start = interval1[0]
    elif interval1[0] <= interval2[0] <= interval1[1]:
        start = interval2[0]
    else:
        raise Exception("Intervals are not overlapping")

    if interval2[0] <= interval1[1] <= interval2[1]:
        end = interval1[1]
    elif interval1[0] <= interval2[1] <= interval1[1]:
        end = interval2[1]
    else:
        raise Exception("Intervals are not overlapping")

    return (start, end)


def percentage_overlap(interval1, interval2):
    """
    Given [0, 4] and [1, 10] returns 0.75
    """
    try:
        overlap = _overlap(interval1, interval2)
    except Exception:
        return 0.0
    return (overlap[1] - overlap[0]) / (interval1[1] - interval1[0])


来源:https://stackoverflow.com/questions/2953967/built-in-function-for-computing-overlap-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!