Skip to content

Commit

Permalink
new feature powerspace
Browse files Browse the repository at this point in the history
  • Loading branch information
relleums committed Mar 2, 2024
1 parent 6696e4b commit b55ab07
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
32 changes: 30 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#############
Binning Utils
#############
|TestStatus| |PyPiStatus| |BlackStyle|
|TestStatus| |PyPiStatus| |BlackStyle| |BlackPackStyle| |MITLicenseBadge|

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
Expand All @@ -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
1 change: 1 addition & 0 deletions binning_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .version import __version__
from . import power10
from . import sphere
from .powerspace import powerspace
import numpy as np


Expand Down
32 changes: 32 additions & 0 deletions binning_utils/powerspace.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion binning_utils/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.11"
__version__ = "0.0.12"

0 comments on commit b55ab07

Please sign in to comment.