projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Make rate_limited work and add doctest.
[python_utils.git]
/
histogram.py
diff --git
a/histogram.py
b/histogram.py
index ca67839da4cb7a7031df835690c30dc9dde15c73..3391b0b3c51d33164b6d173d59a7b4a1b3c9788b 100644
(file)
--- a/
histogram.py
+++ b/
histogram.py
@@
-4,10
+4,6
@@
import math
from numbers import Number
from typing import Generic, Iterable, List, Optional, Tuple, TypeVar
from numbers import Number
from typing import Generic, Iterable, List, Optional, Tuple, TypeVar
-from math_utils import RunningMedian
-from text_utils import bar_graph
-
-
T = TypeVar("T", bound=Number)
T = TypeVar("T", bound=Number)
@@
-18,6
+14,7
@@
class SimpleHistogram(Generic[T]):
NEGATIVE_INFINITY = -math.inf
def __init__(self, buckets: List[Tuple[T, T]]):
NEGATIVE_INFINITY = -math.inf
def __init__(self, buckets: List[Tuple[T, T]]):
+ from math_utils import RunningMedian
self.buckets = {}
for start_end in buckets:
if self._get_bucket(start_end[0]) is not None:
self.buckets = {}
for start_end in buckets:
if self._get_bucket(start_end[0]) is not None:
@@
-69,14
+66,18
@@
class SimpleHistogram(Generic[T]):
all_true = all_true and self.add_item(item)
return all_true
all_true = all_true and self.add_item(item)
return all_true
- def __repr__(self) -> str:
+ def __repr__(self,
+ label_formatter='%10s') -> str:
+ from text_utils import bar_graph
+
max_population: Optional[int] = None
for bucket in self.buckets:
pop = self.buckets[bucket]
if pop > 0:
max_population: Optional[int] = None
for bucket in self.buckets:
pop = self.buckets[bucket]
if pop > 0:
- last_bucket_start = bucket[0]
+ last_bucket_start = bucket[0]
# beginning of range
if max_population is None or pop > max_population:
if max_population is None or pop > max_population:
- max_population = pop
+ max_population = pop # bucket with max items
+
txt = ""
if max_population is None:
return txt
txt = ""
if max_population is None:
return txt
@@
-88,17
+89,11
@@
class SimpleHistogram(Generic[T]):
bar = bar_graph(
(pop / max_population),
include_text = False,
bar = bar_graph(
(pop / max_population),
include_text = False,
- width =
70
,
+ width =
58
,
left_end = "",
right_end = "")
left_end = "",
right_end = "")
- label = f'{
start}..{end}'
- txt += f'{label:
12}: ' + bar + f"({pop}) ({len(bar)
})\n"
+ label = f'{
label_formatter}..{label_formatter}' % (start, end)
+ txt += f'{label:
20}: ' + bar + f"({pop/self.count*100.0:5.2f}% n={pop
})\n"
if start == last_bucket_start:
break
if start == last_bucket_start:
break
-
- txt = txt + f'''{self.count} item(s)
-{self.maximum} max
-{self.minimum} min
-{self.sigma/self.count:.3f} mean
-{self.median.get_median()} median'''
return txt
return txt