Make histogram auto-format labels.
[python_utils.git] / histogram.py
index 4aa47490122481852ae03e264795509a8794e54e..87ef77340fd036b8ea29d5ddf3def5d902db3688 100644 (file)
@@ -67,7 +67,7 @@ class SimpleHistogram(Generic[T]):
             all_true = all_true and self.add_item(item)
         return all_true
 
-    def __repr__(self, label_formatter='%10s') -> str:
+    def __repr__(self, *, width: int = 80, label_formatter: str = None) -> str:
         from text_utils import bar_graph
 
         max_population: Optional[int] = None
@@ -82,23 +82,56 @@ class SimpleHistogram(Generic[T]):
         if max_population is None:
             return txt
 
+        max_label_width = None
+        lowest_start = None
+        highest_end = None
         for bucket in sorted(self.buckets, key=lambda x: x[0]):
-            pop = self.buckets[bucket]
             start = bucket[0]
+            if lowest_start is None:
+                lowest_start = start
             end = bucket[1]
+            if highest_end is None or end > highest_end:
+                highest_end = end
+            if label_formatter is None:
+                if type(start) == int and type(end) == int:
+                    label_formatter = '%d'
+                elif type(start) == float and type(end) == float:
+                    label_formatter = '%.2f'
+                else:
+                    label_formatter = '%s'
+            label = f'[{label_formatter}..{label_formatter}): ' % (start, end)
+            label_width = len(label)
+            if max_label_width is None or label_width > max_label_width:
+                max_label_width = label_width
+            if start == last_bucket_start:
+                break
+        sigma_label = f'[{label_formatter}..{label_formatter}): ' % (
+            lowest_start,
+            highest_end,
+        )
+        if len(sigma_label) > max_label_width:
+            max_label_width = len(sigma_label)
+        bar_width = width - (max_label_width + 16)
+
+        for bucket in sorted(self.buckets, key=lambda x: x[0]):
+            start = bucket[0]
+            end = bucket[1]
+            label = f'[{label_formatter}..{label_formatter}): ' % (start, end)
+            pop = self.buckets[bucket]
             bar = bar_graph(
                 (pop / max_population),
                 include_text=False,
-                width=58,
+                width=bar_width,
                 left_end="",
                 right_end="",
             )
-            label = f'{label_formatter}..{label_formatter}' % (start, end)
-            txt += (
-                f'{label:20}: '
-                + bar
-                + f"({pop/self.count*100.0:5.2f}% n={pop})\n"
-            )
+            txt += label.rjust(max_label_width)
+            txt += bar
+            txt += f"({pop/self.count*100.0:5.2f}% n={pop})\n"
             if start == last_bucket_start:
                 break
+        txt += '-' * width + '\n'
+        txt += sigma_label.rjust(max_label_width)
+        txt += ' ' * (bar_width - 2)
+        txt += f'Σ=(100.00% n={self.count})\n'
         return txt