|CI Status| |asv| |PyPI|
About
-----
Sometimes you just want to compute simple 1D or 2D histograms with regular bins. Fast. No
nonsense. `Numpy's <http://www.numpy.org>`__ histogram functions are
versatile, and can handle for example non-regular binning, but this
versatility comes at the expense of performance.
The **fast-histogram** mini-package aims to provide simple and fast
histogram functions for regular bins that don't compromise on performance. It doesn't do
anything complicated - it just implements a simple histogram algorithm
in C and keeps it simple. The aim is to have functions that are fast but
also robust and reliable. The result is a 1D histogram function here that
is **7-15x faster** than ``numpy.histogram``, and a 2D histogram function
that is **20-25x faster** than ``numpy.histogram2d``.
To install::
pip install fast-histogram
or if you use conda you can instead do::
conda install -c conda-forge fast-histogram
The ``fast_histogram`` module then provides two functions:
``histogram1d`` and ``histogram2d``:
.. code:: python
from fast_histogram import histogram1d, histogram2d
Example
-------
Here's an example of binning 10 million points into a regular 2D
histogram:
.. code:: python
In [1]: import numpy as np
In [2]: x = np.random.random(10_000_000)
In [3]: y = np.random.random(10_000_000)
In [4]: %timeit _ = np.histogram2d(x, y, range=[[-1, 2], [-2, 4]], bins=30)
935 ms ± 58.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [5]: from fast_histogram import histogram2d
In [6]: %timeit _ = histogram2d(x, y, range=[[-1, 2], [-2, 4]], bins=30)
40.2 ms ± 624 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
(note that ``10_000_000`` is possible in Python 3.6 syntax, use ``10000000`` instead in previous versions)
The version here is over 20 times faster! The following plot shows the
speedup as a function of array size for the bin parameters shown above:
.. figure:: https://github.com/astrofrog/fast-histogram/raw/main/speedup_compared.png
:alt: Comparison of performance between Numpy and fast-histogram
as well as results for the 1D case, also with 30 bins. The speedup for
the 2D case is consistently between 20-25x, and for the 1D case goes
from 15x for small arrays to around 7x for large arrays.
Q&A
---
Why don't the histogram functions return the edges?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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``.
Doesn't package X already do this, but better?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This may very well be the case! If this duplicates another package, or
if it is possible to use Numpy in a smarter way to get the same
performance gains, please open an issue and I'll consider deprecating
this package :)
One package that does include fast histogram functions (including in
n-dimensions) and can compute other statistics is
`vaex <https://github.com/maartenbreddels/vaex>`_, so take a look there
if you need more advanced functionality!
Are the 2D histograms not transposed compared to what they should be?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is technically no 'right' and 'wrong' orientation - here we adopt
the convention which gives results consistent with Numpy, so:
.. code:: python
numpy.histogram2d(x, y, range=[[xmin, xmax], [ymin, ymax]], bins=[nx, ny])
should give the same result as:
.. code:: python
fast_histogram.histogram2d(x, y, range=[[xmin, xmax], [ymin, ymax]], bins=[nx, ny])
Why not contribute this to Numpy directly?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As mentioned above, the Numpy functions are much more versatile, so they could not
be replaced by the ones here. One option would be to check in Numpy's functions for
cases that are simple and dispatch to functions such as the ones here, or add
dedicated functions for regular binning. I hope we can get this in Numpy in some form
or another eventually, but for now, the aim is to have this available to packages
that need to support a range of Numpy versions.
Why not use Cython?
~~~~~~~~~~~~~~~~~~~
I originally implemented this in Cython, but found that I could get a
50% performance improvement by going straight to a C extension.
What about using Numba?
~~~~~~~~~~~~~~~~~~~~~~~
I specifically want to keep this package as easy as possible to install,
and while `Numba <https://numba.pydata.org>`__ is a great package, it is
not trivial to install outside of Anaconda.
Could this be parallelized?
~~~~~~~~~~~~~~~~~~~~~~~~~~~
This may benefit from parallelization under certain circumstances. The
easiest solution might be to use OpenMP, but this won't work on all
platforms, so it would need to be made optional.
Couldn't you make it faster by using the GPU?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Almost certainly, though the aim here is to have an easily installable
and portable package, and introducing GPUs is going to affect both of
these.
Why make a package specifically for this? This is a tiny amount of functionality
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Packages that need this could simply bundle their own C extension or
Cython code to do this, but the main motivation for releasing this as a
mini-package is to avoid making pure-Python packages into packages that
require compilation just because of the need to compute fast histograms.
Can I contribute?
~~~~~~~~~~~~~~~~~
Yes please! This is not meant to be a finished package, and I welcome
pull request to improve things.
.. |CI Status| image:: https://github.com/astrofrog/fast-histogram/actions/workflows/main.yml/badge.svg
:target: https://github.com/astrofrog/fast-histogram/actions/workflows/main.yml
.. |asv| image:: https://img.shields.io/badge/benchmarked%20by-asv-brightgreen.svg
:target: https://astrofrog.github.io/fast-histogram
.. |PyPI| image:: https://img.shields.io/pypi/v/fast-histogram.svg
:target: https://pypi.org/project/fast-histogram/
:alt: PyPI release
Raw data
{
"_id": null,
"home_page": "https://github.com/astrofrog/fast-histogram",
"name": "fast-histogram",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": null,
"author": "Thomas Robitaille",
"author_email": "thomas.robitaille@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e8/77/04a9b4b5caa6e6b3a2f633b15dec0996c1559fc26e9ba73bb3d1d844c874/fast_histogram-0.14.tar.gz",
"platform": null,
"description": "|CI Status| |asv| |PyPI|\n\nAbout\n-----\n\nSometimes you just want to compute simple 1D or 2D histograms with regular bins. Fast. No\nnonsense. `Numpy's <http://www.numpy.org>`__ histogram functions are\nversatile, and can handle for example non-regular binning, but this\nversatility comes at the expense of performance.\n\nThe **fast-histogram** mini-package aims to provide simple and fast\nhistogram functions for regular bins that don't compromise on performance. It doesn't do\nanything complicated - it just implements a simple histogram algorithm\nin C and keeps it simple. The aim is to have functions that are fast but\nalso robust and reliable. The result is a 1D histogram function here that\nis **7-15x faster** than ``numpy.histogram``, and a 2D histogram function\nthat is **20-25x faster** than ``numpy.histogram2d``.\n\nTo install::\n\n pip install fast-histogram\n\nor if you use conda you can instead do::\n\n conda install -c conda-forge fast-histogram\n\nThe ``fast_histogram`` module then provides two functions:\n``histogram1d`` and ``histogram2d``:\n\n.. code:: python\n\n from fast_histogram import histogram1d, histogram2d\n\nExample\n-------\n\nHere's an example of binning 10 million points into a regular 2D\nhistogram:\n\n.. code:: python\n\n In [1]: import numpy as np\n\n In [2]: x = np.random.random(10_000_000)\n\n In [3]: y = np.random.random(10_000_000)\n\n In [4]: %timeit _ = np.histogram2d(x, y, range=[[-1, 2], [-2, 4]], bins=30)\n 935 ms \u00b1 58.4 ms per loop (mean \u00b1 std. dev. of 7 runs, 1 loop each)\n\n In [5]: from fast_histogram import histogram2d\n\n In [6]: %timeit _ = histogram2d(x, y, range=[[-1, 2], [-2, 4]], bins=30)\n 40.2 ms \u00b1 624 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)\n\n(note that ``10_000_000`` is possible in Python 3.6 syntax, use ``10000000`` instead in previous versions)\n\nThe version here is over 20 times faster! The following plot shows the\nspeedup as a function of array size for the bin parameters shown above:\n\n.. figure:: https://github.com/astrofrog/fast-histogram/raw/main/speedup_compared.png\n :alt: Comparison of performance between Numpy and fast-histogram\n\nas well as results for the 1D case, also with 30 bins. The speedup for\nthe 2D case is consistently between 20-25x, and for the 1D case goes\nfrom 15x for small arrays to around 7x for large arrays.\n\nQ&A\n---\n\nWhy don't the histogram functions return the edges?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nComputing 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``.\n\nDoesn't package X already do this, but better?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThis may very well be the case! If this duplicates another package, or\nif it is possible to use Numpy in a smarter way to get the same\nperformance gains, please open an issue and I'll consider deprecating\nthis package :)\n\nOne package that does include fast histogram functions (including in\nn-dimensions) and can compute other statistics is\n`vaex <https://github.com/maartenbreddels/vaex>`_, so take a look there\nif you need more advanced functionality!\n\nAre the 2D histograms not transposed compared to what they should be?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThere is technically no 'right' and 'wrong' orientation - here we adopt\nthe convention which gives results consistent with Numpy, so:\n\n.. code:: python\n\n numpy.histogram2d(x, y, range=[[xmin, xmax], [ymin, ymax]], bins=[nx, ny])\n\nshould give the same result as:\n\n.. code:: python\n\n fast_histogram.histogram2d(x, y, range=[[xmin, xmax], [ymin, ymax]], bins=[nx, ny])\n\nWhy not contribute this to Numpy directly?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nAs mentioned above, the Numpy functions are much more versatile, so they could not\nbe replaced by the ones here. One option would be to check in Numpy's functions for\ncases that are simple and dispatch to functions such as the ones here, or add\ndedicated functions for regular binning. I hope we can get this in Numpy in some form\nor another eventually, but for now, the aim is to have this available to packages\nthat need to support a range of Numpy versions.\n\nWhy not use Cython?\n~~~~~~~~~~~~~~~~~~~\n\nI originally implemented this in Cython, but found that I could get a\n50% performance improvement by going straight to a C extension.\n\nWhat about using Numba?\n~~~~~~~~~~~~~~~~~~~~~~~\n\nI specifically want to keep this package as easy as possible to install,\nand while `Numba <https://numba.pydata.org>`__ is a great package, it is\nnot trivial to install outside of Anaconda.\n\nCould this be parallelized?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThis may benefit from parallelization under certain circumstances. The\neasiest solution might be to use OpenMP, but this won't work on all\nplatforms, so it would need to be made optional.\n\nCouldn't you make it faster by using the GPU?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nAlmost certainly, though the aim here is to have an easily installable\nand portable package, and introducing GPUs is going to affect both of\nthese.\n\nWhy make a package specifically for this? This is a tiny amount of functionality\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nPackages that need this could simply bundle their own C extension or\nCython code to do this, but the main motivation for releasing this as a\nmini-package is to avoid making pure-Python packages into packages that\nrequire compilation just because of the need to compute fast histograms.\n\nCan I contribute?\n~~~~~~~~~~~~~~~~~\n\nYes please! This is not meant to be a finished package, and I welcome\npull request to improve things.\n\n.. |CI Status| image:: https://github.com/astrofrog/fast-histogram/actions/workflows/main.yml/badge.svg\n :target: https://github.com/astrofrog/fast-histogram/actions/workflows/main.yml\n\n.. |asv| image:: https://img.shields.io/badge/benchmarked%20by-asv-brightgreen.svg\n :target: https://astrofrog.github.io/fast-histogram\n\n.. |PyPI| image:: https://img.shields.io/pypi/v/fast-histogram.svg\n :target: https://pypi.org/project/fast-histogram/\n :alt: PyPI release\n",
"bugtrack_url": null,
"license": "BSD",
"summary": "Fast simple 1D and 2D histograms",
"version": "0.14",
"project_urls": {
"Homepage": "https://github.com/astrofrog/fast-histogram"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "eca3acf5d7641585da06982027a11727b174c4f9311c13b422111c5f197c1a57",
"md5": "64273769da81992fd9cd1a94560ff7f5",
"sha256": "15876672df4831177344dfd0afbf5fd532c78f7bfca8bfabcb0f3d558f672e99"
},
"downloads": -1,
"filename": "fast_histogram-0.14-cp39-abi3-macosx_10_9_x86_64.whl",
"has_sig": false,
"md5_digest": "64273769da81992fd9cd1a94560ff7f5",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.9",
"size": 21597,
"upload_time": "2024-04-16T20:19:52",
"upload_time_iso_8601": "2024-04-16T20:19:52.579891Z",
"url": "https://files.pythonhosted.org/packages/ec/a3/acf5d7641585da06982027a11727b174c4f9311c13b422111c5f197c1a57/fast_histogram-0.14-cp39-abi3-macosx_10_9_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0c2cd4d96c78e72031f3171fb3a584b557d79d191e9bb4e93747f793c18f8623",
"md5": "b31738c9f07629e0fe9274fcfd8ae6f0",
"sha256": "01f26dd20166040c50b5381f0a76635d81d5db9cfaaed7ec30103edf71e88c3f"
},
"downloads": -1,
"filename": "fast_histogram-0.14-cp39-abi3-macosx_11_0_arm64.whl",
"has_sig": false,
"md5_digest": "b31738c9f07629e0fe9274fcfd8ae6f0",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.9",
"size": 20634,
"upload_time": "2024-04-16T20:19:53",
"upload_time_iso_8601": "2024-04-16T20:19:53.733773Z",
"url": "https://files.pythonhosted.org/packages/0c/2c/d4d96c78e72031f3171fb3a584b557d79d191e9bb4e93747f793c18f8623/fast_histogram-0.14-cp39-abi3-macosx_11_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0ff9524b8a302862bdc7100a5e0662d3fa49500af20badcabaddeec474819b8d",
"md5": "5665df3cf2cc71870cc10df7a6acd43f",
"sha256": "b425d93e4bf1b0cdc223b8fe91ca68aa53c314b8ec374027b9a215a41aa85658"
},
"downloads": -1,
"filename": "fast_histogram-0.14-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
"has_sig": false,
"md5_digest": "5665df3cf2cc71870cc10df7a6acd43f",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.9",
"size": 55480,
"upload_time": "2024-04-16T20:19:54",
"upload_time_iso_8601": "2024-04-16T20:19:54.940796Z",
"url": "https://files.pythonhosted.org/packages/0f/f9/524b8a302862bdc7100a5e0662d3fa49500af20badcabaddeec474819b8d/fast_histogram-0.14-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "503ef0dba6333dbe5c5a338d1466939c8733256a5f6d7e10615b8f96a90277e5",
"md5": "5f48da277b3c89a66dbf3fe12cd15a1e",
"sha256": "1f2f1d4b091fa065fc1991dd10f06812cfba7549622bf63f7888ac1c8c7ed9bb"
},
"downloads": -1,
"filename": "fast_histogram-0.14-cp39-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"has_sig": false,
"md5_digest": "5f48da277b3c89a66dbf3fe12cd15a1e",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.9",
"size": 55426,
"upload_time": "2024-04-16T20:19:56",
"upload_time_iso_8601": "2024-04-16T20:19:56.036429Z",
"url": "https://files.pythonhosted.org/packages/50/3e/f0dba6333dbe5c5a338d1466939c8733256a5f6d7e10615b8f96a90277e5/fast_histogram-0.14-cp39-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e86efdd53002da2c1c5f3694eb98f015728e842c2d26dd28fba618a04efadb4a",
"md5": "b517a670a6d78d6be129da4d82721dc9",
"sha256": "f1a263da3d832e8faa10c7228b23028ac4a406d2dd7cebbe89b2d8a9a6d58a0c"
},
"downloads": -1,
"filename": "fast_histogram-0.14-cp39-abi3-musllinux_1_1_x86_64.whl",
"has_sig": false,
"md5_digest": "b517a670a6d78d6be129da4d82721dc9",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.9",
"size": 43564,
"upload_time": "2024-04-16T20:19:57",
"upload_time_iso_8601": "2024-04-16T20:19:57.317242Z",
"url": "https://files.pythonhosted.org/packages/e8/6e/fdd53002da2c1c5f3694eb98f015728e842c2d26dd28fba618a04efadb4a/fast_histogram-0.14-cp39-abi3-musllinux_1_1_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9abc30658ca273e521b72faa8870dc2e5af0052d92d7e302c2ef50ab81f937cb",
"md5": "a6d15207f04e4fc71a2292ad80bf118e",
"sha256": "b96db6ed1db9d1ce09800e88833cc8c5e9565d44748f7bf623c0694e6cce1e2d"
},
"downloads": -1,
"filename": "fast_histogram-0.14-cp39-abi3-win32.whl",
"has_sig": false,
"md5_digest": "a6d15207f04e4fc71a2292ad80bf118e",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.9",
"size": 21300,
"upload_time": "2024-04-16T20:19:58",
"upload_time_iso_8601": "2024-04-16T20:19:58.278226Z",
"url": "https://files.pythonhosted.org/packages/9a/bc/30658ca273e521b72faa8870dc2e5af0052d92d7e302c2ef50ab81f937cb/fast_histogram-0.14-cp39-abi3-win32.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "fad67bdb0ea7bc96fbd633c028927f51f84982e30b08120b98193535087cc34e",
"md5": "831663baa723a33792ebf593808131f3",
"sha256": "ff9b83b0d9d489e3a59ef3b18342db7cf75f76ae22c7d95ca143783c6cc307a6"
},
"downloads": -1,
"filename": "fast_histogram-0.14-cp39-abi3-win_amd64.whl",
"has_sig": false,
"md5_digest": "831663baa723a33792ebf593808131f3",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.9",
"size": 24116,
"upload_time": "2024-04-16T20:19:59",
"upload_time_iso_8601": "2024-04-16T20:19:59.244828Z",
"url": "https://files.pythonhosted.org/packages/fa/d6/7bdb0ea7bc96fbd633c028927f51f84982e30b08120b98193535087cc34e/fast_histogram-0.14-cp39-abi3-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2f78abd20be4dbf5a34a38c65c13424472fbe3400a790c7cecd54023dab2fc8c",
"md5": "363f3298d6c9a7c9a622809560e7517a",
"sha256": "8245d4de782304eb396581677b43ca0b9e35a0f120ecb207fb353c852c8001a8"
},
"downloads": -1,
"filename": "fast_histogram-0.14-pp39-pypy39_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"has_sig": false,
"md5_digest": "363f3298d6c9a7c9a622809560e7517a",
"packagetype": "bdist_wheel",
"python_version": "pp39",
"requires_python": ">=3.9",
"size": 23044,
"upload_time": "2024-04-16T20:20:01",
"upload_time_iso_8601": "2024-04-16T20:20:01.111552Z",
"url": "https://files.pythonhosted.org/packages/2f/78/abd20be4dbf5a34a38c65c13424472fbe3400a790c7cecd54023dab2fc8c/fast_histogram-0.14-pp39-pypy39_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e87704a9b4b5caa6e6b3a2f633b15dec0996c1559fc26e9ba73bb3d1d844c874",
"md5": "4a9bbb01431024af1c17d7c7c0deb142",
"sha256": "390973b98af22bda85c29dcf6f008ba0d626321e9bd3f5a9d7a43e5690ea69ea"
},
"downloads": -1,
"filename": "fast_histogram-0.14.tar.gz",
"has_sig": false,
"md5_digest": "4a9bbb01431024af1c17d7c7c0deb142",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 47469,
"upload_time": "2024-04-16T20:20:03",
"upload_time_iso_8601": "2024-04-16T20:20:03.510406Z",
"url": "https://files.pythonhosted.org/packages/e8/77/04a9b4b5caa6e6b3a2f633b15dec0996c1559fc26e9ba73bb3d1d844c874/fast_histogram-0.14.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-16 20:20:03",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "astrofrog",
"github_project": "fast-histogram",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "fast-histogram"
}