diff --git a/README.rst b/README.rst index ff43ea9..fbbf564 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,7 @@ ############# Binning Utils ############# -|TestStatus| |PyPiStatus| |BlackStyle| +|TestStatus| |PyPiStatus| |BlackStyle| |BlackPackStyle| |MITLicenseBadge| A collection of tools to help with binning. @@ -9,7 +9,7 @@ A collection of tools to help with binning. power10 ******* -Create binning with power-space which is aligned to decades. +Create binning in geomspace which is aligned to decades. .. code:: python @@ -24,11 +24,39 @@ Create binning with power-space which is aligned to decades. array([ 1., 2.15, 4.64, 10., 21.54, 46.41, 100.]) +********** +powerspace +********** + +To make bin edges for distributions occuring in power laws. +For example to histogram the energies of cosmic rays which occur in a +power law with slope ``-2.7`` + +.. code:: python + + import binning_utils + binning_utils.powerspace( + start=1, + stop=10, + power_slope=-2.7, + size=10, + ) + array([ 1. , 1.07017144, 1.15544801, 1.26196439, 1.39995703, + 1.58808152, 1.86493297, 2.32807878, 3.33799855, 10. ]) + + + .. |BlackStyle| image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/psf/black +.. |BlackPackStyle| image:: https://img.shields.io/badge/pack%20style-black-000000.svg + :target: https://github.com/cherenkov-plenoscope/black_pack + .. |TestStatus| image:: https://github.com/cherenkov-plenoscope/binning_utils/actions/workflows/test.yml/badge.svg?branch=main :target: https://github.com/cherenkov-plenoscope/binning_utils/actions/workflows/test.yml +.. |MITLicenseBadge| image:: https://img.shields.io/badge/License-GPL%20v3-blue.svg + :target: https://opensource.org/licenses/MIT + .. |PyPiStatus| image:: https://img.shields.io/pypi/v/binning_utils_sebastian-achim-mueller :target: https://pypi.org/project/binning_utils_sebastian-achim-mueller diff --git a/binning_utils/__init__.py b/binning_utils/__init__.py index d7f30ed..a868ef9 100644 --- a/binning_utils/__init__.py +++ b/binning_utils/__init__.py @@ -1,6 +1,7 @@ from .version import __version__ from . import power10 from . import sphere +from .powerspace import powerspace import numpy as np diff --git a/binning_utils/powerspace.py b/binning_utils/powerspace.py new file mode 100644 index 0000000..f89744b --- /dev/null +++ b/binning_utils/powerspace.py @@ -0,0 +1,32 @@ +import numpy as np + + +def powerspace(start, stop, power_slope, size): + """ + Parameters + ---------- + start : float + Lower limit of the space. + stop : float + Upper limit of the space. + power_slope : float + Slope of the power law. + size : int + The number of samples. + + Returns + ------- + x : array_like + Values between 'start' and 'stop' spaced to histogram a power law + with slope 'power_slope' in equal amounts in each bin. + """ + # Adopted from CORSIKA + rd = np.linspace(0.0, 1.0, size) + if power_slope != -1.0: + ll = start ** (power_slope + 1.0) + ul = stop ** (power_slope + 1.0) + slex = 1.0 / (power_slope + 1.0) + return (rd * ul + (1.0 - rd) * ll) ** slex + else: + ll = stop / start + return start * ll**rd diff --git a/binning_utils/version.py b/binning_utils/version.py index b2f0155..6e2648a 100644 --- a/binning_utils/version.py +++ b/binning_utils/version.py @@ -1 +1 @@ -__version__ = "0.0.11" +__version__ = "0.0.12"