projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
More type annotations.
[python_utils.git]
/
histogram.py
diff --git
a/histogram.py
b/histogram.py
index 7be643f3a038365f58c2522910a7d769ce398991..d45e93f328185f869c2b3af4cc832db280d14428 100644
(file)
--- a/
histogram.py
+++ b/
histogram.py
@@
-2,46
+2,49
@@
import math
from numbers import Number
import math
from numbers import Number
-from typing import Generic, Iterable, List, Optional, Tuple, TypeVar
+from typing import
Dict,
Generic, Iterable, List, Optional, Tuple, TypeVar
-T = TypeVar("T", bound=Number)
+T = TypeVar("T", int, float)
+Bound = int
+Count = int
class SimpleHistogram(Generic[T]):
class SimpleHistogram(Generic[T]):
-
# Useful in defining wide open bottom/top bucket bounds:
POSITIVE_INFINITY = math.inf
NEGATIVE_INFINITY = -math.inf
# Useful in defining wide open bottom/top bucket bounds:
POSITIVE_INFINITY = math.inf
NEGATIVE_INFINITY = -math.inf
- def __init__(self, buckets: List[Tuple[
T, T
]]):
+ def __init__(self, buckets: List[Tuple[
Bound, Bound
]]):
from math_utils import RunningMedian
from math_utils import RunningMedian
- self.buckets = {}
+ self.buckets
: Dict[Tuple[Bound, Bound], Count]
= {}
for start_end in buckets:
if self._get_bucket(start_end[0]) is not None:
raise Exception("Buckets overlap?!")
self.buckets[start_end] = 0
for start_end in buckets:
if self._get_bucket(start_end[0]) is not None:
raise Exception("Buckets overlap?!")
self.buckets[start_end] = 0
- self.sigma
=
0
- self.median = RunningMedian()
- self.maximum = None
- self.minimum = None
- self.count = 0
+ self.sigma
: float = 0.
0
+ self.median
: RunningMedian
= RunningMedian()
+ self.maximum
: Optional[T]
= None
+ self.minimum
: Optional[T]
= None
+ self.count
: Count
= 0
@staticmethod
def n_evenly_spaced_buckets(
min_bound: T,
max_bound: T,
n: int,
@staticmethod
def n_evenly_spaced_buckets(
min_bound: T,
max_bound: T,
n: int,
- ) -> List[Tuple[
T, T
]]:
- ret = []
+ ) -> List[Tuple[
int, int
]]:
+ ret
: List[Tuple[int, int]]
= []
stride = int((max_bound - min_bound) / n)
if stride <= 0:
raise Exception("Min must be < Max")
stride = int((max_bound - min_bound) / n)
if stride <= 0:
raise Exception("Min must be < Max")
- for bucket_start in range(min_bound, max_bound, stride):
+ imax = math.ceil(max_bound)
+ imin = math.floor(min_bound)
+ for bucket_start in range(imin, imax, stride):
ret.append((bucket_start, bucket_start + stride))
return ret
ret.append((bucket_start, bucket_start + stride))
return ret
- def _get_bucket(self, item: T) -> Optional[Tuple[
T, T
]]:
+ def _get_bucket(self, item: T) -> Optional[Tuple[
int, int
]]:
for start_end in self.buckets:
if start_end[0] <= item < start_end[1]:
return start_end
for start_end in self.buckets:
if start_end[0] <= item < start_end[1]:
return start_end
@@
-67,9
+70,10
@@
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,
width: int = 80, *, label_formatter: str = '%5s
') -> str:
+ def __repr__(self,
*, width: int = 80, label_formatter: str = '%d
') -> str:
from text_utils import bar_graph
from text_utils import bar_graph
+ txt = ""
max_population: Optional[int] = None
for bucket in self.buckets:
pop = self.buckets[bucket]
max_population: Optional[int] = None
for bucket in self.buckets:
pop = self.buckets[bucket]
@@
-77,14
+81,12
@@
class SimpleHistogram(Generic[T]):
last_bucket_start = bucket[0] # beginning of range
if max_population is None or pop > max_population:
max_population = pop # bucket with max items
last_bucket_start = bucket[0] # beginning of range
if max_population is None or pop > max_population:
max_population = pop # bucket with max items
-
- txt = ""
if max_population is None:
return txt
if max_population is None:
return txt
- max_label_width = None
- lowest_start = None
- highest_end = None
+ max_label_width
: Optional[int]
= None
+ lowest_start
: Optional[int]
= None
+ highest_end
: Optional[int]
= None
for bucket in sorted(self.buckets, key=lambda x: x[0]):
start = bucket[0]
if lowest_start is None:
for bucket in sorted(self.buckets, key=lambda x: x[0]):
start = bucket[0]
if lowest_start is None:
@@
-98,6
+100,10
@@
class SimpleHistogram(Generic[T]):
max_label_width = label_width
if start == last_bucket_start:
break
max_label_width = label_width
if start == last_bucket_start:
break
+ assert max_label_width
+ assert lowest_start
+ assert highest_end
+
sigma_label = f'[{label_formatter}..{label_formatter}): ' % (
lowest_start,
highest_end,
sigma_label = f'[{label_formatter}..{label_formatter}): ' % (
lowest_start,
highest_end,