Skip to content

Commit e1dd3ae

Browse files
authored
Merge pull request #22 from ab-10/master
Nice!
2 parents 0626b05 + 1bcac10 commit e1dd3ae

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

histogrammar/primitives/bin.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,51 @@ def __init__(self, num, low, high, quantity, value=Count(), underflow=Count(), o
142142
super(Bin, self).__init__()
143143
self.specialize()
144144

145+
def ascii(self):
146+
"""Prints ascii histogram, for debuging on headless machines"""
147+
underflow = self.underflow.toInt()
148+
overflow = self.overflow.toInt()
149+
nanflow = self.nanflow.toInt()
150+
values = [underflow] + self._toArray() + [overflow, nanflow]
151+
min = values[0]
152+
max = values[0]
153+
154+
length = len(values)
155+
i = 1
156+
while i < length:
157+
if values[i] > max:
158+
max = values[i]
159+
elif values[i] < min:
160+
min = values[i]
161+
i += 1
162+
163+
# Map values to number of dots representing them (maximum is 63)
164+
range = max - min
165+
prop = 63 / range
166+
167+
dots = [None] * length
168+
i = 0
169+
while i < length:
170+
dots[i] = int((values[i] - min)*prop)
171+
i += 1
172+
173+
# Get range of values corresponding to each bin
174+
ranges = ["underflow"] + [None] * (length - 3) + ["overflow", "nanflow"]
175+
i = 1
176+
while i < (length - 2):
177+
ranges[i] = "[" + str(self.range(i))[1:]
178+
i += 1
179+
180+
print("{:>19}{:>65}".format(min, max))
181+
print(" " * 18 + "+" + "-" * 63 + "+")
182+
183+
i = 0
184+
while i < length:
185+
print("{:<14}{:<4}{:<65}".format(ranges[i], int(values[i]), "|" + "*" * dots[i] + " " * (63 - dots[i]) + "|"))
186+
i += 1
187+
188+
print(" " * 18 + "+" + "-" * 63 + "+")
189+
145190
def histogram(self):
146191
"""Return a plain histogram by converting all sub-aggregator values into :doc:`Counts <histogrammar.primitives.count.Count>`."""
147192
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):
472517
def _sparksql(self, jvm, converter):
473518
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))
474519

520+
def _toArray(self):
521+
"""Converts Bin to array of frequencies"""
522+
values = [None] * int(len(self.values))
523+
i = 0
524+
for value in self.values:
525+
value = str(value)
526+
end = len(value) - 1
527+
values[i] = float(value[7:end])
528+
i += 1
529+
return values
530+
475531
@property
476532
def children(self):
477533
"""List of sub-aggregators, to make it possible to walk the tree."""
@@ -579,3 +635,4 @@ def __hash__(self):
579635
return hash((self.low, self.high, self.quantity, self.entries, tuple(self.values), self.underflow, self.overflow, self.nanflow))
580636

581637
Factory.register(Bin)
638+

histogrammar/primitives/count.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ def _numpy(self, data, weights, shape):
194194
def _sparksql(self, jvm, converter):
195195
return converter.Count() # TODO: handle transform
196196

197+
def toInt(self):
198+
"""Return intiger value of count"""
199+
value = str(self)
200+
end = len(value) - 1
201+
value = float(value[7:end])
202+
return int(value)
203+
197204
@property
198205
def children(self):
199206
"""List of sub-aggregators, to make it possible to walk the tree."""

0 commit comments

Comments
 (0)