Skip to content

Commit c5cedcc

Browse files
committed
doc: get api comparison markdown back
1 parent 179391d commit c5cedcc

1 file changed

Lines changed: 246 additions & 0 deletions

File tree

docs/api_comparison.md

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
# API Comparison
2+
3+
This document compares the current Khisto APIs with NumPy and Matplotlib.
4+
5+
## NumPy Comparison
6+
7+
### `numpy.histogram` vs `khisto.histogram`
8+
9+
Khisto's `histogram` function is designed as a drop-in replacement for `numpy.histogram`.
10+
11+
#### Signature Comparison
12+
13+
```python
14+
# NumPy
15+
numpy.histogram(
16+
a,
17+
bins=10,
18+
range=None,
19+
density=None,
20+
weights=None,
21+
)
22+
23+
# Khisto
24+
khisto.histogram(
25+
a,
26+
range=None,
27+
max_bins=None,
28+
density=False,
29+
)
30+
```
31+
32+
#### Key Differences
33+
34+
| Feature | NumPy | Khisto |
35+
|---|---|---|
36+
| **Binning method** | Fixed-width bins | Optimal variable-width bins |
37+
| **Bins parameter** | `bins` (int or edges) | `max_bins` (optional limit) |
38+
| **Default bins** | 10 fixed bins | Auto-determined optimal |
39+
| **Weights support** | Yes | No |
40+
| **Returns** | `(hist, bin_edges)` | `(hist, bin_edges)` |
41+
42+
#### Usage Comparison
43+
44+
```python
45+
import numpy as np
46+
from khisto import histogram
47+
48+
data = np.random.normal(0, 1, 1000)
49+
50+
# NumPy - fixed 10 bins
51+
np_hist, np_edges = np.histogram(data)
52+
53+
# Khisto - optimal bins (automatic)
54+
khisto_hist, khisto_edges = histogram(data)
55+
56+
# NumPy - specified bin count
57+
np_hist, np_edges = np.histogram(data, bins=20)
58+
59+
# Khisto - maximum bin count
60+
khisto_hist, khisto_edges = histogram(data, max_bins=20)
61+
62+
# Both support density normalization
63+
np_density, _ = np.histogram(data, density=True)
64+
khisto_density, _ = histogram(data, density=True)
65+
66+
# Both support range specification
67+
np_hist, _ = np.histogram(data, range=(-2, 2))
68+
khisto_hist, _ = histogram(data, range=(-2, 2))
69+
```
70+
71+
#### When to Use Each
72+
73+
| Use NumPy | Use Khisto |
74+
|---|---|
75+
| Need fixed-width bins | Want optimal data representation |
76+
| Need weighted histograms | Want automatic bin selection |
77+
| Need specific bin edges | Want adaptive bin widths |
78+
| Performance-critical loops | Data visualization |
79+
80+
---
81+
82+
## Matplotlib Comparison
83+
84+
### `matplotlib.pyplot.hist` vs `khisto.matplotlib.hist`
85+
86+
Khisto's `hist` function works similarly to matplotlib's `hist`, but with optimal binning.
87+
88+
#### Signature Comparison
89+
90+
```python
91+
# Matplotlib
92+
matplotlib.pyplot.hist(
93+
x,
94+
bins=10,
95+
range=None,
96+
density=False,
97+
weights=None,
98+
cumulative=False,
99+
bottom=None,
100+
histtype='bar',
101+
align='mid',
102+
orientation='vertical',
103+
rwidth=None,
104+
log=False,
105+
color=None,
106+
label=None,
107+
stacked=False,
108+
**kwargs,
109+
)
110+
111+
# Khisto
112+
khisto.matplotlib.hist(
113+
x,
114+
range=None,
115+
max_bins=None,
116+
density=False,
117+
cumulative=False,
118+
histtype='bar',
119+
orientation='vertical',
120+
log=False,
121+
color=None,
122+
label=None,
123+
ax=None,
124+
edgecolor=None,
125+
linewidth=None,
126+
alpha=None,
127+
**kwargs,
128+
)
129+
```
130+
131+
#### Key Differences
132+
133+
| Feature | Matplotlib | Khisto |
134+
|---|---|---|
135+
| **Binning** | Fixed-width | Optimal variable-width |
136+
| **Bins param** | `bins` | `max_bins` |
137+
| **Axes param** | Implicit (current) | Optional `ax` parameter |
138+
| **Cumulative** | Supported | Supported |
139+
| **Reverse cumulative** | Supported with negative `cumulative` | Supported with negative `cumulative` |
140+
| **Stacked** | Supported | Not supported |
141+
| **Weights** | Supported | Not supported |
142+
| **Unsupported histogram args** | None | `bins`, `stacked`, and `weights` raise a `TypeError` |
143+
| **Multiple datasets** | Supported | Not supported; only 1-D arrays are accepted |
144+
145+
#### Usage Comparison
146+
147+
```python
148+
import numpy as np
149+
import matplotlib.pyplot as plt
150+
from khisto.matplotlib import hist
151+
152+
data = np.random.normal(0, 1, 1000)
153+
154+
# Matplotlib - fixed bins
155+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
156+
157+
ax1.hist(data, bins=30)
158+
ax1.set_title("Matplotlib (30 bins)")
159+
160+
hist(data, ax=ax2)
161+
ax2.set_title("Khisto (optimal bins)")
162+
163+
plt.tight_layout()
164+
plt.show()
165+
```
166+
167+
#### Common Parameters (Same Behavior)
168+
169+
```python
170+
# Both support these parameters identically:
171+
172+
# density normalization
173+
plt.hist(data, density=True)
174+
hist(data, density=True)
175+
176+
# cumulative view
177+
plt.hist(data, density=True, cumulative=True)
178+
hist(data, density=True, cumulative=True)
179+
180+
# reverse cumulative view
181+
plt.hist(data, cumulative=-1)
182+
hist(data, cumulative=-1)
183+
184+
# histogram type
185+
plt.hist(data, histtype='step')
186+
hist(data, histtype='step')
187+
188+
# orientation
189+
plt.hist(data, orientation='horizontal')
190+
hist(data, orientation='horizontal')
191+
192+
# log scale
193+
plt.hist(data, log=True)
194+
hist(data, log=True)
195+
196+
# color and label
197+
plt.hist(data, color='blue', label='Data')
198+
hist(data, color='blue', label='Data')
199+
```
200+
201+
---
202+
203+
## Migration Guide
204+
205+
### From NumPy
206+
207+
```python
208+
# Before (NumPy)
209+
import numpy as np
210+
hist, edges = np.histogram(data, bins=30)
211+
212+
# After (Khisto)
213+
from khisto import histogram
214+
hist, edges = histogram(data, max_bins=30) # max_bins is optional
215+
```
216+
217+
### From Matplotlib
218+
219+
```python
220+
# Before (Matplotlib)
221+
import matplotlib.pyplot as plt
222+
n, bins, patches = plt.hist(data, bins=30)
223+
224+
# After (Khisto)
225+
from khisto.matplotlib import hist
226+
n, bins, patches = hist(data, max_bins=30) # max_bins is optional
227+
```
228+
229+
---
230+
231+
## Feature Matrix
232+
233+
| Feature | NumPy | Matplotlib | Khisto |
234+
|---|---|---|---|
235+
| Fixed-width bins | Yes | Yes | No |
236+
| Optimal bins | No | No | Yes |
237+
| Variable-width bins | Manual | Manual | Auto |
238+
| Density | Yes | Yes | Yes |
239+
| Range | Yes | Yes | Yes |
240+
| Weights | Yes | Yes | No |
241+
| Cumulative | No | Yes | Yes |
242+
| Reverse cumulative | No | Yes | Yes |
243+
| Plotting | No | Yes | Yes |
244+
| Step histogram | No | Yes | Yes |
245+
| Horizontal | No | Yes | Yes |
246+
| Log scale | No | Yes | Yes |

0 commit comments

Comments
 (0)