Skip to content

Commit 3e99878

Browse files
committed
feat: Enhance histogram functionality and CLI integration
- Introduced function to choose the best histogram based on max_bins or optimal selection criteria. - Updated function to filter input data by range and utilize the new selection logic. - Refactored to always return a list of histogram results, removing the and parameters. - Modified to always return all granularities of histogram results. - Improved CSV reading logic in for better type handling. - Updated function to align with new histogram computation logic. - Enhanced tests to verify histogram filtering by range and validate the structure of returned results.
1 parent bad4883 commit 3e99878

13 files changed

Lines changed: 345 additions & 32336 deletions

File tree

docs/API.md

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ khisto.histogram(
2929

3030
Compute an optimal histogram using the Khiops binning algorithm.
3131

32-
This function is designed as a drop-in replacement for `numpy.histogram`, providing automatic optimal binning instead of fixed-width bins.
32+
Drop-in replacement for [`numpy.histogram`](https://numpy.org/doc/stable/reference/generated/numpy.histogram.html), using optimal binning instead of fixed-width bins.
3333

3434
#### Parameters
3535

3636
| Parameter | Type | Default | Description |
3737
|-----------|------|---------|-------------|
3838
| `a` | `ArrayLike` | required | Input data. The histogram is computed over the flattened array. |
39-
| `range` | `tuple[float, float]` | `None` | The lower and upper range of the bins. If not provided, range is `(a.min(), a.max())`. Values outside the range are ignored. |
40-
| `max_bins` | `int` | `None` | Maximum number of bins. If not provided, the algorithm determines the optimal number. |
41-
| `density` | `bool` | `False` | If `False`, the result contains the number of samples in each bin. If `True`, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1. |
39+
| `range` | `tuple[float, float]` | `None` | Lower and upper range of the bins. Values outside are ignored. |
40+
| `max_bins` | `int` | `None` | Maximum number of bins. If not provided, the optimal number is determined automatically. |
41+
| `density` | `bool` | `False` | If `True`, return probability density values; otherwise return counts. |
4242

4343
#### Returns
4444

@@ -47,6 +47,10 @@ This function is designed as a drop-in replacement for `numpy.histogram`, provid
4747
| `hist` | `NDArray[np.floating]` | The values of the histogram. |
4848
| `bin_edges` | `NDArray[np.floating]` | Array of length `len(hist) + 1` containing the bin edges. |
4949

50+
#### See Also
51+
52+
- [`numpy.histogram`](https://numpy.org/doc/stable/reference/generated/numpy.histogram.html) — NumPy's standard histogram function.
53+
5054
#### Examples
5155

5256
Basic usage:
@@ -90,28 +94,26 @@ The core API provides direct access to the Khiops histogram computation with det
9094
```python
9195
khisto.core.compute_histogram(
9296
x: ArrayLike,
93-
max_bins: Optional[int] = None,
94-
range: Optional[tuple[float, float]] = None,
95-
return_all: bool = False,
96-
) -> Union[HistogramResult, list[HistogramResult]]
97+
) -> list[HistogramResult]
9798
```
9899

99-
Compute an optimal histogram using the Khiops binning algorithm.
100+
Compute optimal histograms at all granularity levels using the Khiops binning algorithm.
100101

101102
#### Parameters
102103

103104
| Parameter | Type | Default | Description |
104105
|-----------|------|---------|-------------|
105106
| `x` | `ArrayLike` | required | Input data array. |
106-
| `max_bins` | `int` | `None` | Maximum number of bins for the histogram. |
107-
| `range` | `tuple[float, float]` | `None` | The range `(min, max)` over which to compute the histogram. Values outside this range are ignored. |
108-
| `return_all` | `bool` | `False` | If `True`, return all granularity levels computed by the algorithm. If `False`, return only the optimal histogram. |
109107

110108
#### Returns
111109

112110
| Return | Type | Description |
113111
|--------|------|-------------|
114-
| `result` | `HistogramResult` or `list[HistogramResult]` | If `return_all=False`, returns a single `HistogramResult` for the optimal histogram. If `return_all=True`, returns a list of `HistogramResult` objects for all granularity levels. |
112+
| `results` | `list[HistogramResult]` | List of `HistogramResult` objects for all granularity levels, from coarsest to finest. |
113+
114+
#### See Also
115+
116+
- [`khisto.histogram`](#histogram) — Simplified interface returning `(hist, bin_edges)`.
115117

116118
#### Examples
117119

@@ -122,20 +124,13 @@ import numpy as np
122124
from khisto.core import compute_histogram
123125

124126
data = np.random.normal(0, 1, 1000)
125-
result = compute_histogram(data)
127+
results = compute_histogram(data)
126128

127-
print(f"Number of bins: {len(result.frequency)}")
128-
print(f"Bin edges: {result.bin_edges}")
129-
print(f"Is optimal: {result.is_best}")
130-
```
131-
132-
Get all granularity levels:
133-
134-
```python
135-
results = compute_histogram(data, return_all=True)
129+
# Find the optimal histogram
136130
for r in results:
137-
marker = " (best)" if r.is_best else ""
138-
print(f"Granularity {r.granularity}: {len(r.frequency)} bins{marker}")
131+
if r.is_best:
132+
print(f"Optimal: {len(r.frequency)} bins")
133+
print(f"Bin edges: {r.bin_edges}")
139134
```
140135

141136
---
@@ -212,35 +207,24 @@ khisto.matplotlib.hist(
212207
range: Optional[tuple[float, float]] = None,
213208
max_bins: Optional[int] = None,
214209
density: bool = False,
215-
histtype: str = "bar",
216-
orientation: Literal["vertical", "horizontal"] = "vertical",
217-
log: bool = False,
218-
color: Optional[str] = None,
219-
label: Optional[str] = None,
220210
ax: Optional[Axes] = None,
221211
**kwargs,
222212
) -> tuple[NDArray[np.floating], NDArray[np.floating], Any]
223213
```
224214

225-
Plot an optimal histogram using matplotlib.
215+
Compute and plot an optimal histogram.
226216

227-
This function is designed to work similarly to `matplotlib.pyplot.hist`, but uses Khiops optimal binning.
217+
Drop-in replacement for [`matplotlib.pyplot.hist`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html) using Khisto's optimal binning algorithm.
228218

229219
#### Parameters
230220

231221
| Parameter | Type | Default | Description |
232222
|-----------|------|---------|-------------|
233223
| `x` | `ArrayLike` | required | Input data. |
234-
| `range` | `tuple[float, float]` | `None` | The lower and upper range of the bins. |
235-
| `max_bins` | `int` | `None` | Maximum number of bins. |
236-
| `density` | `bool` | `False` | If `True`, plot probability density instead of counts. |
237-
| `histtype` | `str` | `"bar"` | Type of histogram: `"bar"`, `"step"`, or `"stepfilled"`. |
238-
| `orientation` | `str` | `"vertical"` | `"vertical"` or `"horizontal"`. |
239-
| `log` | `bool` | `False` | If `True`, set log scale on the value axis. |
240-
| `color` | `str` | `None` | Color of the histogram. |
241-
| `label` | `str` | `None` | Label for legend. |
242-
| `ax` | `Axes` | `None` | Matplotlib axes to plot on. If `None`, uses current axes. |
243-
| `**kwargs` | | | Additional arguments passed to matplotlib's `bar` or `stairs`. |
224+
| `range` | `tuple[float, float]` | `None` | Lower and upper range of the bins. Values outside are ignored. |
225+
| `max_bins` | `int` | `None` | Maximum number of bins. If `None`, uses optimal binning. |
226+
| `ax` | `Axes` | `None` | Axes to plot on. If `None`, uses current axes. |
227+
| `**kwargs` | | | Other parameters passed to matplotlib. See [`matplotlib.pyplot.hist`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html). |
244228

245229
#### Returns
246230

@@ -250,6 +234,11 @@ This function is designed to work similarly to `matplotlib.pyplot.hist`, but use
250234
| `bins` | `NDArray[np.floating]` | The bin edges. |
251235
| `patches` | `Any` | Container of individual artists (bars or StepPatch). |
252236

237+
#### See Also
238+
239+
- [`matplotlib.pyplot.hist`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html) — Full documentation of supported parameters.
240+
- [`khisto.histogram`](#histogram) — Underlying histogram computation.
241+
253242
#### Examples
254243

255244
Basic plot:

0 commit comments

Comments
 (0)