Skip to content

Commit 9531122

Browse files
committed
Correcting the implementation of the ascii() method.
1 parent 31ddc7f commit 9531122

2 files changed

Lines changed: 22 additions & 41 deletions

File tree

histogrammar/primitives/bin.py

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -144,30 +144,25 @@ def __init__(self, num, low, high, quantity, value=Count(), underflow=Count(), o
144144

145145
def ascii(self):
146146
"""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-
147+
underflow = self.underflow.entries
148+
overflow = self.overflow.entries
149+
nanflow = self.nanflow.entries
150+
values = [underflow] + [x.entries for x in self.values] + [overflow, nanflow]
151+
minimum = min(values)
152+
maximum = max(values)
153+
163154
# Map values to number of dots representing them (maximum is 63)
164-
range = max - min
165-
prop = 63 / range
155+
mintomax = maximum - minimum
156+
if mintomax == 0.0:
157+
mintomax = 1.0
158+
159+
prop = 62.0 / mintomax
166160

161+
length = len(self.values)
167162
dots = [None] * length
168163
i = 0
169164
while i < length:
170-
dots[i] = int((values[i] - min)*prop)
165+
dots[i] = int(round((values[i] - minimum)*prop))
171166
i += 1
172167

173168
# Get range of values corresponding to each bin
@@ -177,15 +172,19 @@ def ascii(self):
177172
ranges[i] = "[" + str(self.range(i))[1:]
178173
i += 1
179174

180-
print("{:>19}{:>65}".format(min, max))
181-
print(" " * 18 + "+" + "-" * 63 + "+")
175+
printedValues = ["{0:<.2g}".format(v) for v in values]
176+
printedValuesWidth = max(len(x) for x in printedValues)
177+
formatter = "{0:<14} {1:<%s} {2:<65}" % printedValuesWidth
178+
179+
print(" " * printedValuesWidth + "{0:>16}{1:>65}".format(minimum, maximum))
180+
print(" " * (16 + printedValuesWidth) + "+" + "-" * 62 + "+")
182181

183182
i = 0
184183
while i < length:
185-
print("{:<14}{:<4}{:<65}".format(ranges[i], int(values[i]), "|" + "*" * dots[i] + " " * (63 - dots[i]) + "|"))
184+
print(formatter.format(ranges[i], printedValues[i], "|" + "*" * dots[i] + " " * (62 - dots[i]) + "|"))
186185
i += 1
187186

188-
print(" " * 18 + "+" + "-" * 63 + "+")
187+
print(" " * (16 + printedValuesWidth) + "+" + "-" * 62 + "+")
189188

190189
def histogram(self):
191190
"""Return a plain histogram by converting all sub-aggregator values into :doc:`Counts <histogrammar.primitives.count.Count>`."""
@@ -517,17 +516,6 @@ def _numpy(self, data, weights, shape):
517516
def _sparksql(self, jvm, converter):
518517
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))
519518

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-
531519
@property
532520
def children(self):
533521
"""List of sub-aggregators, to make it possible to walk the tree."""

histogrammar/primitives/count.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,6 @@ 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-
204197
@property
205198
def children(self):
206199
"""List of sub-aggregators, to make it possible to walk the tree."""

0 commit comments

Comments
 (0)