Skip to content

Commit b9a56b8

Browse files
authored
refactor!: match API with Scikit-HEP (#22)
* refactor!: match API with Scikit-HEP * refactor!: match API with Scikit-HEP * tests + __getitem__ * docs * Update README.md * Update README.md
1 parent d1444a4 commit b9a56b8

File tree

8 files changed

+854
-1319
lines changed

8 files changed

+854
-1319
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ repos:
77
rev: "1.18.0"
88
hooks:
99
- id: blacken-docs
10+
args: ["-E"]
1011
additional_dependencies: [black==24.*]
1112

1213
- repo: https://github.com/pre-commit/pre-commit-hooks

README.md

Lines changed: 194 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,203 @@
1313
[![Conda latest release][conda-version]][conda-link]
1414
[![LICENSE][license-badge]][license-link] [![Scikit-HEP][sk-badge]][sk-link]
1515

16-
`cuda-histogram` is a histogram filling package for GPUs. The package follows
17-
[UHI](https://uhi.readthedocs.io) and keeps its API similar to
16+
`cuda-histogram` is a histogram filling package for GPUs. The package tries to
17+
follow [UHI](https://uhi.readthedocs.io) and keeps its API similar to
1818
[boost-histogram](https://github.com/scikit-hep/boost-histogram) and
1919
[hist](https://github.com/scikit-hep/hist).
2020

21-
The package is under active development at the moment.
21+
Main features of cuda-histogram:
22+
23+
- Implements a subset of the features of boost-histogram using CuPy (see API
24+
documentation for a complete list):
25+
- Axes
26+
- `Regular` and `Variable` axes
27+
- `edges()`
28+
- `centers()`
29+
- `index(...)`
30+
- ...
31+
- Histogram
32+
- `fill(..., weight=...)` (including `Nan` flow)
33+
- simple indexing with slicing (see example below)
34+
- `values(flow=...)`
35+
- `variance(flow=...)`
36+
- Allows users to detach the generated GPU histogram to CPU -
37+
- `to_boost()` - converts to `boost-histogram.Histogram`
38+
- `to_hist()` - converts to `hist.Hist`
39+
40+
Near future goals for the package -
41+
42+
- Implement support for `Categorical` axes (exists internally but need
43+
refactoring to match boost-histogram's API)
44+
- Improve indexing (`__getitem__`) to exactly match boost-histogram's API
45+
46+
## Installation
47+
48+
cuda-histogram is available on [PyPI](https://pypi.org/project/cuda-histogram/)
49+
as well as on [conda](https://anaconda.org/conda-forge/cuda-histogram). The
50+
library can be installed using `pip` -
51+
52+
```
53+
pip install cuda-histogram
54+
```
55+
56+
or using `conda` -
57+
58+
```
59+
conda install -c conda-forge cuda-histogram
60+
```
61+
62+
## Usage
63+
64+
Ideally, a user would want to create a cuda-histogram, fill values on GPU, and
65+
convert the filled histogram to boost-histogram/Hist object to access all the
66+
UHI functionalities.
67+
68+
### Creating a histogram
69+
70+
```py
71+
import cuda_histogram; import cupy as cp
72+
73+
ax1 = cuda_histogram.axis.Regular(10, 0, 1)
74+
ax2 = cuda_histogram.axis.Variable([0, 2, 3, 6])
75+
76+
h = cuda_histogram.Hist(ax1, ax2)
77+
78+
>>> ax1, ax2, h
79+
(Regular(10, 0, 1), Variable([0. 2. 3. 6.]), Hist(Regular(10, 0, 1), Variable([0. 2. 3. 6.])))
80+
```
81+
82+
### Filling a histogram
83+
84+
Differences in API (from boost-histogram) -
85+
86+
- Has an additional `NaN` flow
87+
- Accepts only CuPy arrays
88+
89+
```py
90+
h.fill(cp.random.normal(size=1_000_000), cp.random.normal(size=1_000_000)) # set weight=... for weighted fills
91+
92+
>>> h.values(), type(h.values()) # set flow=True for flow bins (underflow, overflow, nanflow)
93+
(array([[28532., 1238., 64.],
94+
[29603., 1399., 61.],
95+
[30543., 1341., 78.],
96+
[31478., 1420., 98.],
97+
[32692., 1477., 92.],
98+
[32874., 1441., 96.],
99+
[33584., 1515., 88.],
100+
[34304., 1490., 114.],
101+
[34887., 1598., 116.],
102+
[35341., 1472., 103.]]), <class 'cupy.ndarray'>)
103+
```
104+
105+
### Indexing axes and histograms
106+
107+
Differences in API (from boost-histogram) -
108+
109+
- `underflow` is indexed as `0` and not `-1`
110+
- `ax[...]` will return a `cuda_histogram.Interval` object
111+
- No interpolation is performed
112+
- `Hist` indices should be in the range of bin edges, instead of integers
113+
114+
```py
115+
>>> ax1.index(0.5)
116+
array([6])
117+
118+
>>> ax1.index(-1)
119+
array([0])
120+
121+
>>> ax1[0]
122+
<Interval ((-inf, 0.0)) instance at 0x1c905208790>
123+
124+
>>> h[0, 0], type(h[0, 0])
125+
(Hist(Regular(1, 0.0, 0.1), Variable([0. 2.])), <class 'cuda_histogram.hist.Hist'>)
126+
127+
>>> h[0, 0].values(), type(h[0, 0].values())
128+
(array([[28532.]]), <class 'cupy.ndarray'>)
129+
130+
>>> h[0, :].values(), type(h[0, 0].values())
131+
(array([[28532., 1238., 64.]]), <class 'cupy.ndarray'>)
132+
133+
>>> h[0.2, :].values(), type(h[0, 0].values()) # indices in range of bin edges
134+
(array([[30543., 1341., 78.]]), <class 'cupy.ndarray'>)
135+
136+
>>> h[:, 1:2].values(), type(h[0, 0].values()) # no interpolation
137+
C:\Users\Saransh\Saransh_softwares\OpenSource\Python\cuda-histogram\src\cuda_histogram\axis\__init__.py:580: RuntimeWarning: Reducing along axis Variable([0. 2. 3. 6.]): requested start 1 between bin boundaries, no interpolation is performed
138+
warnings.warn(
139+
(array([[28532.],
140+
[29603.],
141+
[30543.],
142+
[31478.],
143+
[32692.],
144+
[32874.],
145+
[33584.],
146+
[34304.],
147+
[34887.],
148+
[35341.]]), <class 'cupy.ndarray'>)
149+
```
150+
151+
### Converting to CPU
152+
153+
All the existing functionalities of boost-histogram and Hist can be used on the
154+
converted histogram.
155+
156+
```py
157+
h.to_boost()
158+
159+
>>> h.to_boost().values(), type(h.to_boost().values())
160+
(array([[28532., 1238., 64.],
161+
[29603., 1399., 61.],
162+
[30543., 1341., 78.],
163+
[31478., 1420., 98.],
164+
[32692., 1477., 92.],
165+
[32874., 1441., 96.],
166+
[33584., 1515., 88.],
167+
[34304., 1490., 114.],
168+
[34887., 1598., 116.],
169+
[35341., 1472., 103.]]), <class 'numpy.ndarray'>)
170+
171+
h.to_hist()
172+
173+
>>> h.to_hist().values(), type(h.to_hist().values())
174+
(array([[28532., 1238., 64.],
175+
[29603., 1399., 61.],
176+
[30543., 1341., 78.],
177+
[31478., 1420., 98.],
178+
[32692., 1477., 92.],
179+
[32874., 1441., 96.],
180+
[33584., 1515., 88.],
181+
[34304., 1490., 114.],
182+
[34887., 1598., 116.],
183+
[35341., 1472., 103.]]), <class 'numpy.ndarray'>)
184+
```
185+
186+
## Getting help
187+
188+
- `cuda-histogram`'s code is hosted on
189+
[GitHub](https://github.com/Saransh-cpp/cuda-histogram).
190+
- If something is not working the way it should, or if you want to request a new
191+
feature, create a new
192+
[issue](https://github.com/Saransh-cpp/cuda-histogram/issues) on GitHub.
193+
- To discuss something related to `cuda-histogram`, use the
194+
[discussions](https://github.com/Saransh-cpp/cuda-histogram/discussions/) tab
195+
on GitHub.
196+
197+
## Contributing
198+
199+
Contributions of any kind welcome! See
200+
[CONTRIBUTING.md](./.github/CONTRIBUTING.md) for information on setting up a
201+
development environment.
202+
203+
## Acknowledgements
204+
205+
This library was primarily developed by Lindsey Gray, Saransh Chopra, and Jim
206+
Pivarski.
207+
208+
Support for this work was provided by the National Science Foundation
209+
cooperative agreement OAC-1836650 and PHY-2323298 (IRIS-HEP). Any opinions,
210+
findings, conclusions or recommendations expressed in this material are those of
211+
the authors and do not necessarily reflect the views of the National Science
212+
Foundation.
22213

23214
<!-- prettier-ignore-start -->
24215
[actions-badge]: https://github.com/Saransh-cpp/cuda-histogram/workflows/CI/badge.svg

docs/api/cuda_histogram.axis.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ cuda_histogram.axis package
55
:members:
66
:undoc-members:
77
:show-inheritance:
8+
:private-members:

docs/index.md

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,6 @@
1010
:start-after: <!-- SPHINX-START -->
1111
```
1212

13-
## Installation
14-
15-
cuda-histogram is available on [PyPI](https://pypi.org/project/cuda-histogram/)
16-
as well as on [conda](https://anaconda.org/conda-forge/cuda-histogram). The
17-
library can be installed using `pip` -
18-
19-
```{code-block}
20-
21-
pip install cuda-histogram
22-
23-
```
24-
25-
or using `conda` -
26-
27-
```{code-block}
28-
29-
conda install -c conda-forge cuda-histogram
30-
31-
```
32-
3313
## Changes in cuda-histogram's API
3414

3515
The `changelog` file describes the changes in `cuda-histogram`'s API and usage
@@ -41,17 +21,6 @@ introduced in every new version.
4121
changelog
4222
```
4323

44-
## Getting help
45-
46-
- `cuda-histogram`'s code is hosted on
47-
[GitHub](https://github.com/Saransh-cpp/cuda-histogram).
48-
- If something is not working the way it should, or if you want to request a new
49-
feature, create a new
50-
[issue](https://github.com/Saransh-cpp/cuda-histogram/issues) on GitHub.
51-
- To discuss something related to `cuda-histogram`, use the
52-
[discussions](https://github.com/Saransh-cpp/cuda-histogram/discussions/) tab
53-
on GitHub.
54-
5524
## API reference
5625

5726
```{toctree}

pyproject.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@ maintainers = [
99
{ name = "The Scikit-HEP admins", email = "[email protected]" },
1010
]
1111
authors = [
12-
{ name = "Lindsey Gray, Saransh Chopra", email = "[email protected]" },
12+
{ name = "Lindsey Gray", email = "[email protected]" },
13+
{ name = "Saransh Chopra", email = "[email protected]" },
1314
]
1415
description = "Histogramming tools on CUDA."
1516
readme = "README.md"
1617
license.file = "LICENSE"
1718
requires-python = ">=3.8"
1819
classifiers = [
19-
"Development Status :: 1 - Planning",
20+
"Development Status :: 4 - Beta",
2021
"Intended Audience :: Science/Research",
2122
"Intended Audience :: Developers",
2223
"License :: OSI Approved :: BSD License",
2324
"Operating System :: OS Independent",
2425
"Programming Language :: Python",
2526
"Programming Language :: Python :: 3",
2627
"Programming Language :: Python :: 3 :: Only",
28+
"Programming Language :: Python :: 3.8",
2729
"Programming Language :: Python :: 3.9",
2830
"Programming Language :: Python :: 3.10",
2931
"Programming Language :: Python :: 3.11",

0 commit comments

Comments
 (0)