|
13 | 13 | [![Conda latest release][conda-version]][conda-link]
|
14 | 14 | [![LICENSE][license-badge]][license-link] [![Scikit-HEP][sk-badge]][sk-link]
|
15 | 15 |
|
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 |
18 | 18 | [boost-histogram](https://github.com/scikit-hep/boost-histogram) and
|
19 | 19 | [hist](https://github.com/scikit-hep/hist).
|
20 | 20 |
|
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. |
22 | 213 |
|
23 | 214 | <!-- prettier-ignore-start -->
|
24 | 215 | [actions-badge]: https://github.com/Saransh-cpp/cuda-histogram/workflows/CI/badge.svg
|
|
0 commit comments