| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: fast-histogram |
| 3 |
Version: 0.14 |
| 4 |
Summary: Fast simple 1D and 2D histograms |
| 5 |
Home-page: https://github.com/astrofrog/fast-histogram |
| 6 |
Author: Thomas Robitaille |
| 7 |
Author-email: [email protected] |
| 8 |
License: BSD |
| 9 |
Requires-Python: >=3.9 |
| 10 |
License-File: LICENSE |
| 11 |
Requires-Dist: numpy |
| 12 |
Provides-Extra: test |
| 13 |
Requires-Dist: pytest; extra == "test" |
| 14 |
Requires-Dist: hypothesis[numpy]; extra == "test" |
| 15 |
|
| 16 |
|CI Status| |asv| |PyPI| |
| 17 |
|
| 18 |
About |
| 19 |
----- |
| 20 |
|
| 21 |
Sometimes you just want to compute simple 1D or 2D histograms with regular bins. Fast. No |
| 22 |
nonsense. `Numpy's <http://www.numpy.org>`__ histogram functions are |
| 23 |
versatile, and can handle for example non-regular binning, but this |
| 24 |
versatility comes at the expense of performance. |
| 25 |
|
| 26 |
The **fast-histogram** mini-package aims to provide simple and fast |
| 27 |
histogram functions for regular bins that don't compromise on performance. It doesn't do |
| 28 |
anything complicated - it just implements a simple histogram algorithm |
| 29 |
in C and keeps it simple. The aim is to have functions that are fast but |
| 30 |
also robust and reliable. The result is a 1D histogram function here that |
| 31 |
is **7-15x faster** than ``numpy.histogram``, and a 2D histogram function |
| 32 |
that is **20-25x faster** than ``numpy.histogram2d``. |
| 33 |
|
| 34 |
To install:: |
| 35 |
|
| 36 |
pip install fast-histogram |
| 37 |
|
| 38 |
or if you use conda you can instead do:: |
| 39 |
|
| 40 |
conda install -c conda-forge fast-histogram |
| 41 |
|
| 42 |
The ``fast_histogram`` module then provides two functions: |
| 43 |
``histogram1d`` and ``histogram2d``: |
| 44 |
|
| 45 |
.. code:: python |
| 46 |
|
| 47 |
from fast_histogram import histogram1d, histogram2d |
| 48 |
|
| 49 |
Example |
| 50 |
------- |
| 51 |
|
| 52 |
Here's an example of binning 10 million points into a regular 2D |
| 53 |
histogram: |
| 54 |
|
| 55 |
.. code:: python |
| 56 |
|
| 57 |
In [1]: import numpy as np |
| 58 |
|
| 59 |
In [2]: x = np.random.random(10_000_000) |
| 60 |
|
| 61 |
In [3]: y = np.random.random(10_000_000) |
| 62 |
|
| 63 |
In [4]: %timeit _ = np.histogram2d(x, y, range=[[-1, 2], [-2, 4]], bins=30) |
| 64 |
935 ms ± 58.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) |
| 65 |
|
| 66 |
In [5]: from fast_histogram import histogram2d |
| 67 |
|
| 68 |
In [6]: %timeit _ = histogram2d(x, y, range=[[-1, 2], [-2, 4]], bins=30) |
| 69 |
40.2 ms ± 624 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) |
| 70 |
|
| 71 |
(note that ``10_000_000`` is possible in Python 3.6 syntax, use ``10000000`` instead in previous versions) |
| 72 |
|
| 73 |
The version here is over 20 times faster! The following plot shows the |
| 74 |
speedup as a function of array size for the bin parameters shown above: |
| 75 |
|
| 76 |
.. figure:: https://github.com/astrofrog/fast-histogram/raw/main/speedup_compared.png |
| 77 |
:alt: Comparison of performance between Numpy and fast-histogram |
| 78 |
|
| 79 |
as well as results for the 1D case, also with 30 bins. The speedup for |
| 80 |
the 2D case is consistently between 20-25x, and for the 1D case goes |
| 81 |
from 15x for small arrays to around 7x for large arrays. |
| 82 |
|
| 83 |
Q&A |
| 84 |
--- |
| 85 |
|
| 86 |
Why don't the histogram functions return the edges? |
| 87 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 88 |
|
| 89 |
Computing and returning the edges may seem trivial but it can slow things down by a factor of a few when computing histograms of 10^5 or fewer elements, so not returning the edges is a deliberate decision related to performance. You can easily compute the edges yourself if needed though, using ``numpy.linspace``. |
| 90 |
|
| 91 |
Doesn't package X already do this, but better? |
| 92 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 93 |
|
| 94 |
This may very well be the case! If this duplicates another package, or |
| 95 |
if it is possible to use Numpy in a smarter way to get the same |
| 96 |
performance gains, please open an issue and I'll consider deprecating |
| 97 |
this package :) |
| 98 |
|
| 99 |
One package that does include fast histogram functions (including in |
| 100 |
n-dimensions) and can compute other statistics is |
| 101 |
`vaex <https://github.com/maartenbreddels/vaex>`_, so take a look there |
| 102 |
if you need more advanced functionality! |
| 103 |
|
| 104 |
Are the 2D histograms not transposed compared to what they should be? |
| 105 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 106 |
|
| 107 |
There is technically no 'right' and 'wrong' orientation - here we adopt |
| 108 |
the convention which gives results consistent with Numpy, so: |
| 109 |
|
| 110 |
.. code:: python |
| 111 |
|
| 112 |
numpy.histogram2d(x, y, range=[[xmin, xmax], [ymin, ymax]], bins=[nx, ny]) |
| 113 |
|
| 114 |
should give the same result as: |
| 115 |
|
| 116 |
.. code:: python |
| 117 |
|
| 118 |
fast_histogram.histogram2d(x, y, range=[[xmin, xmax], [ymin, ymax]], bins=[nx, ny]) |
| 119 |
|
| 120 |
Why not contribute this to Numpy directly? |
| 121 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 122 |
|
| 123 |
As mentioned above, the Numpy functions are much more versatile, so they could not |
| 124 |
be replaced by the ones here. One option would be to check in Numpy's functions for |
| 125 |
cases that are simple and dispatch to functions such as the ones here, or add |
| 126 |
dedicated functions for regular binning. I hope we can get this in Numpy in some form |
| 127 |
or another eventually, but for now, the aim is to have this available to packages |
| 128 |
that need to support a range of Numpy versions. |
| 129 |
|
| 130 |
Why not use Cython? |
| 131 |
~~~~~~~~~~~~~~~~~~~ |
| 132 |
|
| 133 |
I originally implemented this in Cython, but found that I could get a |
| 134 |
50% performance improvement by going straight to a C extension. |
| 135 |
|
| 136 |
What about using Numba? |
| 137 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 138 |
|
| 139 |
I specifically want to keep this package as easy as possible to install, |
| 140 |
and while `Numba <https://numba.pydata.org>`__ is a great package, it is |
| 141 |
not trivial to install outside of Anaconda. |
| 142 |
|
| 143 |
Could this be parallelized? |
| 144 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 145 |
|
| 146 |
This may benefit from parallelization under certain circumstances. The |
| 147 |
easiest solution might be to use OpenMP, but this won't work on all |
| 148 |
platforms, so it would need to be made optional. |
| 149 |
|
| 150 |
Couldn't you make it faster by using the GPU? |
| 151 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 152 |
|
| 153 |
Almost certainly, though the aim here is to have an easily installable |
| 154 |
and portable package, and introducing GPUs is going to affect both of |
| 155 |
these. |
| 156 |
|
| 157 |
Why make a package specifically for this? This is a tiny amount of functionality |
| 158 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 159 |
|
| 160 |
Packages that need this could simply bundle their own C extension or |
| 161 |
Cython code to do this, but the main motivation for releasing this as a |
| 162 |
mini-package is to avoid making pure-Python packages into packages that |
| 163 |
require compilation just because of the need to compute fast histograms. |
| 164 |
|
| 165 |
Can I contribute? |
| 166 |
~~~~~~~~~~~~~~~~~ |
| 167 |
|
| 168 |
Yes please! This is not meant to be a finished package, and I welcome |
| 169 |
pull request to improve things. |
| 170 |
|
| 171 |
.. |CI Status| image:: https://github.com/astrofrog/fast-histogram/actions/workflows/main.yml/badge.svg |
| 172 |
:target: https://github.com/astrofrog/fast-histogram/actions/workflows/main.yml |
| 173 |
|
| 174 |
.. |asv| image:: https://img.shields.io/badge/benchmarked%20by-asv-brightgreen.svg |
| 175 |
:target: https://astrofrog.github.io/fast-histogram |
| 176 |
|
| 177 |
.. |PyPI| image:: https://img.shields.io/pypi/v/fast-histogram.svg |
| 178 |
:target: https://pypi.org/project/fast-histogram/ |
| 179 |
:alt: PyPI release |