def exclude(self, other): """ Remove coordinates derived from another Source or an array. If other is an array, will remove coordinates of all non-zero elements from this source. If other is a source, will remove any matching coordinates. Parameters ---------- other : ndarray or Source Source to remove """ if isinstance(other, ndarray): coordsOther = asarray(where(other)).T else: coordsOther = aslist(other.coordinates) coordsSelf = aslist(self.coordinates) newid = self.id if hasattr(self, 'id') else None if hasattr(self, 'values') and self.values is not None: valuesSelf = self.values complement = [(c, v) for c, v in zip(coordsSelf, valuesSelf) if c not in coordsOther] newcoords, newvalues = zip(*complement) return Source(coordinates=newcoords, values=newvalues, id=newid) else: complement = [a for a in coordsSelf if a not in coordsOther] return Source(coordinates=complement, id=newid)
def exclude(self, other): """ Remove coordinates derived from another Source or an array. If other is an array, will remove coordinates of all non-zero elements from this source. If other is a source, will remove any matching coordinates. Parameters ---------- other : ndarray or Source Source to remove """ if isinstance(other, ndarray): coordsOther = asarray(where(other)).T else: coordsOther = aslist(other.coordinates) coordsSelf = aslist(self.coordinates) newid = self.id if hasattr(self, "id") else None if hasattr(self, "values") and self.values is not None: valuesSelf = self.values complement = [(c, v) for c, v in zip(coordsSelf, valuesSelf) if c not in coordsOther] newcoords, newvalues = zip(*complement) return Source(coordinates=newcoords, values=newvalues, id=newid) else: complement = [a for a in coordsSelf if a not in coordsOther] return Source(coordinates=complement, id=newid)
def overlap(self, other, method='support', counts=False, symmetric=True): """ Compute the overlap between this source and other, in terms of either support or similarity of coefficients. Support computes the number of overlapping pixels relative to the union of both sources. Correlation computes the similarity of the weights (not defined for binary masks). Parameters ---------- other : Source The source to compute overlap with. method : str Compare either support of source coefficients ('support'), or the source spatial filters (not yet implemented). counts : boolean, optional, default = True Whether to return raw counts when computing support, otherwise return a fraction. """ checkParams(method, ['support', 'corr']) coordsSelf = aslist(self.coordinates) coordsOther = aslist(other.coordinates) intersection = [a for a in coordsSelf if a in coordsOther] complementLeft = [a for a in coordsSelf if a not in intersection] complementRight = [a for a in coordsOther if a not in intersection] hits = len(intersection) if symmetric is True: misses = len(complementLeft + complementRight) else: misses = len(complementLeft) if method == 'support': if counts: return hits, misses else: return hits/float(hits+misses) if method == 'corr': from scipy.stats import spearmanr if not (hasattr(self, 'values') and hasattr(other, 'values')): raise Exception('Sources must have values to compute correlation') else: valuesSelf = aslist(self.values) valuesOther = aslist(other.values) if len(intersection) > 0: rho, _ = spearmanr(valuesSelf[intersection], valuesOther[intersection]) else: rho = 0.0 return (rho * hits)/float(hits + misses)
def overlap(self, other, method="fraction"): """ Compute the overlap between this source and other. Options are a symmetric measure of overlap based on the fraction of intersecting pixels relative to the union ('fraction'), an assymmetric measure of overlap that expresses detected intersecting pixels (relative to this source) using precision and recall rates ('rates'), or a correlation coefficient of the weights within the intersection (not defined for binary weights) ('correlation') Parameters ---------- other : Source The source to compute overlap with. method : str Which estimate of overlap to compute, options are 'fraction' (symmetric) 'rates' (asymmetric) or 'correlation' """ checkParams(method, ["fraction", "rates", "correlation"]) coordsSelf = aslist(self.coordinates) coordsOther = aslist(other.coordinates) intersection = [a for a in coordsSelf if a in coordsOther] nhit = float(len(intersection)) ntotal = float(len(set([tuple(x) for x in coordsSelf] + [tuple(x) for x in coordsOther]))) if method == "rates": recall = nhit / len(coordsSelf) precision = nhit / len(coordsOther) return recall, precision if method == "fraction": return nhit / float(ntotal) if method == "correlation": from scipy.stats import spearmanr if not (hasattr(self, "values") and hasattr(other, "values")): raise ValueError("Sources must have values to compute correlation") else: valuesSelf = aslist(self.values) valuesOther = aslist(other.values) if len(intersection) > 0: left = [v for v, c in zip(valuesSelf, coordsSelf) if c in coordsOther] right = [v for v, c in zip(valuesOther, coordsOther) if c in coordsSelf] rho, _ = spearmanr(left, right) else: rho = 0.0 return rho
def overlap(self, other, method='fraction'): """ Compute the overlap between this source and other. Options are a symmetric measure of overlap based on the fraction of intersecting pixels relative to the union ('fraction'), an assymmetric measure of overlap that expresses detected intersecting pixels (relative to this source) using precision and recall rates ('rates'), or a correlation coefficient of the weights within the intersection (not defined for binary weights) ('correlation') Parameters ---------- other : Source The source to compute overlap with. method : str Which estimate of overlap to compute, options are 'fraction' (symmetric) 'rates' (asymmetric) or 'correlation' """ checkParams(method, ['fraction', 'rates', 'correlation']) coordsSelf = aslist(self.coordinates) coordsOther = aslist(other.coordinates) intersection = [a for a in coordsSelf if a in coordsOther] nhit = float(len(intersection)) ntotal = float( len( set([tuple(x) for x in coordsSelf] + [tuple(x) for x in coordsOther]))) if method == 'rates': recall = nhit / len(coordsSelf) precision = nhit / len(coordsOther) return recall, precision if method == 'fraction': return nhit / float(ntotal) if method == 'correlation': from scipy.stats import spearmanr if not (hasattr(self, 'values') and hasattr(other, 'values')): raise ValueError( 'Sources must have values to compute correlation') else: valuesSelf = aslist(self.values) valuesOther = aslist(other.values) if len(intersection) > 0: left = [ v for v, c in zip(valuesSelf, coordsSelf) if c in coordsOther ] right = [ v for v, c in zip(valuesOther, coordsOther) if c in coordsSelf ] rho, _ = spearmanr(left, right) else: rho = 0.0 return rho