diff --git a/histogrammar/primitives/bin.py b/histogrammar/primitives/bin.py index 3e47eec..ab2a93f 100644 --- a/histogrammar/primitives/bin.py +++ b/histogrammar/primitives/bin.py @@ -142,6 +142,51 @@ def __init__(self, num, low, high, quantity, value=Count(), underflow=Count(), o super(Bin, self).__init__() self.specialize() + def ascii(self): + """Prints ascii histogram, for debuging on headless machines""" + underflow = self.underflow.toInt() + overflow = self.overflow.toInt() + nanflow = self.nanflow.toInt() + values = [underflow] + self._toArray() + [overflow, nanflow] + min = values[0] + max = values[0] + + length = len(values) + i = 1 + while i < length: + if values[i] > max: + max = values[i] + elif values[i] < min: + min = values[i] + i += 1 + + # Map values to number of dots representing them (maximum is 63) + range = max - min + prop = 63 / range + + dots = [None] * length + i = 0 + while i < length: + dots[i] = int((values[i] - min)*prop) + i += 1 + + # Get range of values corresponding to each bin + ranges = ["underflow"] + [None] * (length - 3) + ["overflow", "nanflow"] + i = 1 + while i < (length - 2): + ranges[i] = "[" + str(self.range(i))[1:] + i += 1 + + print("{:>19}{:>65}".format(min, max)) + print(" " * 18 + "+" + "-" * 63 + "+") + + i = 0 + while i < length: + print("{:<14}{:<4}{:<65}".format(ranges[i], int(values[i]), "|" + "*" * dots[i] + " " * (63 - dots[i]) + "|")) + i += 1 + + print(" " * 18 + "+" + "-" * 63 + "+") + def histogram(self): """Return a plain histogram by converting all sub-aggregator values into :doc:`Counts `.""" out = Bin(len(self.values), self.low, self.high, self.quantity, None, self.underflow.copy(), self.overflow.copy(), self.nanflow.copy()) @@ -472,6 +517,17 @@ def _numpy(self, data, weights, shape): def _sparksql(self, jvm, converter): return converter.Bin(len(self.values), self.low, self.high, self.quantity.asSparkSQL(), self.values[0]._sparksql(jvm, converter), self.underflow._sparksql(jvm, converter), self.overflow._sparksql(jvm, converter), self.nanflow._sparksql(jvm, converter)) + def _toArray(self): + """Converts Bin to array of frequencies""" + values = [None] * int(len(self.values)) + i = 0 + for value in self.values: + value = str(value) + end = len(value) - 1 + values[i] = float(value[7:end]) + i += 1 + return values + @property def children(self): """List of sub-aggregators, to make it possible to walk the tree.""" @@ -579,3 +635,4 @@ def __hash__(self): return hash((self.low, self.high, self.quantity, self.entries, tuple(self.values), self.underflow, self.overflow, self.nanflow)) Factory.register(Bin) + diff --git a/histogrammar/primitives/count.py b/histogrammar/primitives/count.py index 229d7ad..7525d7c 100644 --- a/histogrammar/primitives/count.py +++ b/histogrammar/primitives/count.py @@ -194,6 +194,13 @@ def _numpy(self, data, weights, shape): def _sparksql(self, jvm, converter): return converter.Count() # TODO: handle transform + def toInt(self): + """Return intiger value of count""" + value = str(self) + end = len(value) - 1 + value = float(value[7:end]) + return int(value) + @property def children(self): """List of sub-aggregators, to make it possible to walk the tree."""