def overlap(self, other, method='fraction'): """ Compute the overlap between this region and another. Optional methods are a symmetric measure of overlap based on the fraction of intersecting pixels relative to the union ('fraction'), or an assymmetric measure of overlap using precision and recall rates ('rates'). Parameters ---------- other : one region The region to compute overlap with. method : str Which estimate of overlap to compute, options are 'fraction' (symmetric) or 'rates' (asymmetric) """ checkist.opts(method, ['fraction', 'rates']) coords_self = self.coordinates.tolist() coords_other = other.coordinates.tolist() intersection = [a for a in coords_self if a in coords_other] nhit = float(len(intersection)) ntotal = float(len(set([tuple(x) for x in coords_self] + [tuple(x) for x in coords_other]))) if method == 'rates': recall = nhit / len(coords_self) precision = nhit / len(coords_other) return recall, precision if method == 'fraction': return nhit / float(ntotal)
def test_opts_one_error(): with pytest.raises(ValueError): checkist.opts('cow', ['bear'])
def test_opts_one(): checkist.opts('cow', ['cow'])
def test_opts_many_error(): with pytest.raises(ValueError): checkist.opts('cow', ['bear', 'cat', 'owl'])
def test_opts_many(): checkist.opts('cow', ['cow', 'bear', 'cat'])