def __add__(self, other): if isinstance(other, SparselyBin): if self.binWidth != other.binWidth: raise ContainerException( "cannot add SparselyBins because binWidth differs ({0} vs {1})" .format(self.binWidth, other.binWidth)) if self.origin != other.origin: raise ContainerException( "cannot add SparselyBins because origin differs ({0} vs {1})" .format(self.origin, other.origin)) out = SparselyBin( self.binWidth, self.quantity, self.value.copy() if self.value is not None else None, self.nanflow + other.nanflow, self.origin) out.entries = self.entries + other.entries out.bins = self.bins.copy() for i, v in other.bins.items(): if i in out.bins: out.bins[i] = out.bins[i] + v else: out.bins[i] = v return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __iadd__(self, other): if isinstance(other, Index): if self.size != other.size: raise ContainerException( "cannot add Indexes because they have different sizes: ({0} vs {1})" .format(self.size, other.size)) self.entries += other.entries for x, y in zip(self.values, other.values): x += y return self else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __iadd__(self, other): if isinstance(other, Stack): if self.thresholds != other.thresholds: raise ContainerException( "cannot add Stack because cut thresholds differ") self.entries += other.entries for ((k1, v1), (k2, v2)) in zip(self.bins, other.bins): v1 += v2 self.nanflow += other.nanflow return self else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __add__(self, other): if isinstance(other, Index): if self.size != other.size: raise ContainerException( "cannot add Indexes because they have different sizes: ({0} vs {1})" .format(self.size, other.size)) out = Index(*[x + y for x, y in zip(self.values, other.values)]) out.entries = self.entries + other.entries return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __add__(self, other): if isinstance(other, Label): if self.keySet != other.keySet: raise ContainerException( "cannot add Labels because keys differ:\n {0}\n {1}". format(", ".join(sorted(self.keys)), ", ".join(sorted(other.keys)))) out = Label(**dict((k, self(k) + other(k)) for k in self.keys)) out.entries = self.entries + other.entries return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __iadd__(self, other): if isinstance(other, UntypedLabel): if self.keySet != other.keySet: raise ContainerException( "cannot add UntypedLabels because keys differ:\n {0}\n {1}" .format(", ".join(sorted(self.keys)), ", ".join(sorted(other.keys)))) self.entries += other.entries for k in self.keys: v = self(k) v += other(k) return self else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __add__(self, other): if isinstance(other, Count): out = Count(self.transform) out.entries = self.entries + other.entries return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format(self.name, other.name))
def __add__(self, other): if isinstance(other, Stack): if self.thresholds != other.thresholds: raise ContainerException( "cannot add Stack because cut thresholds differ") out = Stack([(k1, v1 + v2) for ((k1, v1), (k2, v2)) in zip(self.bins, other.bins)], self.quantity, None, self.nanflow + other.nanflow) out.entries = self.entries + other.entries return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __init__(self, *values): """Create an Index that is capable of being filled and added. Parameters: values (list of :doc:`Container <histogrammar.defs.Container>`): the collection of aggregators to fill. Other parameters: entries (float): the number of entries, initially 0.0. """ if not all(isinstance(v, Container) for v in values): raise TypeError( "values ({0}) must be a list of Containers".format(values)) if len(values) < 1: raise ContainerException("at least one value required") contentType = values[0].name if any(x.name != contentType for x in values): raise ValueError("all Index values must have the same type") if contentType == "Bag": rangeType = values[0].range if any(x.range != rangeType for x in values): raise ValueError("all Index values must have the same type") self.entries = 0.0 self.values = values super(Index, self).__init__() self.specialize()
def __iadd__(self, other): if isinstance(other, Fraction): self.entries += other.entries self.numerator += other.numerator self.denominator += other.denominator return self else: raise ContainerException("cannot add {0} and {1}".format(self.name, other.name))
def __add__(self, other): if isinstance(other, Sum): out = Sum(self.quantity) out.entries = self.entries + other.entries out.sum = self.sum + other.sum return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format(self.name, other.name))
def __add__(self, other): if isinstance(other, Minimize): out = Minimize(self.quantity) out.entries = self.entries + other.entries out.min = minplus(self.min, other.min) return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __add__(self, other): if isinstance(other, Fraction): out = Fraction(self.quantity, None) out.entries = self.entries + other.entries out.numerator = self.numerator + other.numerator out.denominator = self.denominator + other.denominator return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format(self.name, other.name))
def __iadd__(self, other): if self.centers != other.centers: raise ContainerException( "cannot add CentrallyBin because centers are different:\n {0}\nvs\n {1}" .format(self.centers, other.centers)) self.entries += other.entries for (c1, v1), (_, v2) in zip(self.bins, other.bins): v1 += v2 self.nanflow += other.nanflow return self
def __iadd__(self, other): if isinstance(other, SparselyBin): if self.binWidth != other.binWidth: raise ContainerException( "cannot add SparselyBins because binWidth differs ({0} vs {1})" .format(self.binWidth, other.binWidth)) if self.origin != other.origin: raise ContainerException( "cannot add SparselyBins because origin differs ({0} vs {1})" .format(self.origin, other.origin)) self.entries += other.entries for i, v in other.bins.items(): if i in self.bins: self.bins[i] += v else: self.bins[i] = v.copy() return self else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __iadd__(self, other): if isinstance(other, Categorize): self.entries += other.entries for k in self.keySet.union(other.keySet): if k in self.bins and k in other.bins: self.bins[k] += other.bins[k] elif k not in self.bins and k in other.bins: self.bins[k] = other.bins[k].copy() return self else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __add__(self, other): if isinstance(other, Bag): if self.range != other.range: raise ContainerException( "cannot add Bag because range differs ({0} vs {1})".format( self.range, other.range)) out = Bag(self.quantity, self.range) out.entries = self.entries + other.entries out.values = dict(self.values) for value, count in other.values.items(): if value in out.values: out.values[value] += count else: out.values[value] = count return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format(self.name, other.name))
def __add__(self, other): if self.centers != other.centers: raise ContainerException( "cannot add CentrallyBin because centers are different:\n {0}\nvs\n {1}" .format(self.centers, other.centers)) newbins = [(c1, v1 + v2) for (c1, v1), (_, v2) in zip(self.bins, other.bins)] out = CentrallyBin([c for c, v in self.bins], self.quantity, self.value, self.nanflow + other.nanflow) out.entries = self.entries + other.entries out.bins = newbins return out.specialize()
def __mul__(self, factor): if self.transform != identity or \ not callable(self.transform.expr) or \ (hasattr(self.transform.expr, "func_code") and self.transform.expr.func_code.co_code != identity.expr.func_code.co_code) or \ (hasattr(self.transform.expr, "__code__") and self.transform.expr.__code__.co_code != identity.expr.__code__.co_code): raise ContainerException("Cannot scalar-multiply Count with a non-identity transform.") elif math.isnan(factor) or factor <= 0.0: return self.zero() else: out = self.zero() out.entries = factor * self.entries return out.specialize()
def __add__(self, other): if isinstance(other, Average): out = Average(self.quantity) out.entries = self.entries + other.entries if self.entries == 0.0: out.mean = other.mean elif other.entries == 0.0: out.mean = self.mean else: out.mean = (self.entries * self.mean + other.entries * other.mean) / (self.entries + other.entries) return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __init__(self, **pairs): """Create a Label that is capable of being filled and added. Parameters: pairs (list of str, :doc:`Container <histogrammar.defs.Container>` pairs): the collection of aggregators to fill. Other Parameters: entries (float): the number of entries, initially 0.0. """ if not all( isinstance(k, basestring) and isinstance(v, Container) for k, v in pairs.items()): raise TypeError( "pairs ({0}) must be a dict from strings to Containers".format( pairs)) if any(not isinstance(x, basestring) for x in pairs.keys()): raise ValueError("all Label keys must be strings") if len(pairs) < 1: raise ValueError("at least one pair required") contentType = list(pairs.values())[0].name if any(x.name != contentType for x in pairs.values()): raise ContainerException( "all Label values must have the same type") if contentType == "Bag": rangeType = list(pairs.values())[0].range if any(x.range != rangeType for x in pairs.values()): raise ContainerException( "all Label values must have the same type") self.entries = 0.0 self.pairs = pairs super(Label, self).__init__() self.specialize()
def __add__(self, other): if isinstance(other, Categorize): out = Categorize(self.quantity, self.value) out.entries = self.entries + other.entries out.bins = {} for k in self.keySet.union(other.keySet): if k in self.bins and k in other.bins: out.bins[k] = self.bins[k] + other.bins[k] elif k in self.bins: out.bins[k] = self.bins[k].copy() else: out.bins[k] = other.bins[k].copy() return out.specialize() else: raise ContainerException("cannot add {0} and {1}".format( self.name, other.name))
def __iadd__(self, other): if isinstance(other, Count): self.entries += other.entries return self else: raise ContainerException("cannot add {0} and {1}".format(self.name, other.name))