1111import numpy as np
1212from numpy .typing import ArrayLike , NDArray
1313
14- from khisto .core import HistogramResult , compute_histogram
14+ from khisto .core import HistogramResult , compute_histograms
1515
1616
1717def _select_histogram (
18- results : list [HistogramResult ],
18+ histogram_results : list [HistogramResult ],
1919 max_bins : Optional [int ] = None ,
2020) -> HistogramResult :
2121 """Select the appropriate histogram from the list of results.
@@ -35,38 +35,20 @@ def _select_histogram(
3535 if max_bins is not None :
3636 # Find the finest granularity that respects max_bins
3737 selected = None
38- for r in results :
38+ for r in histogram_results :
3939 if len (r ) <= max_bins :
4040 selected = r
4141 else :
4242 break
4343 # If no histogram respects the constraint, use the coarsest one
44- return selected if selected is not None else results [0 ]
44+ return selected if selected is not None else histogram_results [0 ]
4545 else :
4646 # Return the best (optimal) histogram
47- for r in results :
47+ for r in reversed ( histogram_results ) :
4848 if r .is_best :
4949 return r
5050 # Fallback to finest granularity if no best is marked
51- return results [- 1 ]
52-
53-
54- def _apply_cumulative (
55- hist_values : NDArray [np .float64 ],
56- bin_edges : NDArray [np .float64 ],
57- * ,
58- density : bool ,
59- reverse : bool = False ,
60- ) -> NDArray [np .float64 ]:
61- """Accumulate histogram values using matplotlib-compatible semantics."""
62- if density :
63- source_values = hist_values * np .diff (bin_edges )
64- else :
65- source_values = hist_values
66-
67- if reverse :
68- return np .cumsum (source_values [::- 1 ])[::- 1 ]
69- return np .cumsum (source_values )
51+ return histogram_results [- 1 ]
7052
7153
7254def histogram (
@@ -125,15 +107,20 @@ def histogram(
125107 """
126108 arr = np .asarray (a , dtype = np .float64 ).flatten ()
127109
110+ if max_bins is not None and max_bins <= 0 :
111+ raise ValueError ("max_bins must be a positive integer or None." )
112+
128113 # Filter values by range if specified
129114 if range is not None :
130115 min_val , max_val = range
131116 arr = arr [(arr >= min_val ) & (arr <= max_val )]
132117
133- results = compute_histogram (arr )
134- result = _select_histogram (results , max_bins = max_bins )
118+ histogram_results = compute_histograms (arr )
119+ histogram_result = _select_histogram (histogram_results , max_bins = max_bins )
135120
136121 if density :
137- return result . density .copy (), result .bin_edges .copy ()
122+ return histogram_result . densities .copy (), histogram_result .bin_edges .copy ()
138123 else :
139- return result .frequency .astype (np .float64 ), result .bin_edges .copy ()
124+ return histogram_result .frequencies .astype (
125+ np .float64
126+ ), histogram_result .bin_edges .copy ()
0 commit comments