Skip to content

Commit

Permalink
Include endpoint in create_bins_per_decade if it matches the regular …
Browse files Browse the repository at this point in the history
…spacing, fixes #187
  • Loading branch information
maxnoe committed Jul 15, 2022
1 parent 285bca1 commit 9b73191
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 7 additions & 2 deletions pyirf/binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ def create_bins_per_decade(e_min, e_max, bins_per_decade=5):
e_min: u.Quantity[energy]
Minimum energy, inclusive
e_max: u.Quantity[energy]
Maximum energy, exclusive
Maximum energy, non-inclusive
If the endpoint exactly matches the ``n_bins_per_decade`` requirement,
it will be included.
n_bins_per_decade: int
number of bins per decade
Expand All @@ -112,7 +114,10 @@ def create_bins_per_decade(e_min, e_max, bins_per_decade=5):
log_lower = np.log10(e_min.to_value(unit))
log_upper = np.log10(e_max.to_value(unit))

bins = 10 ** np.arange(log_lower, log_upper, 1 / bins_per_decade)
step = 1 / bins_per_decade
# include endpoint if reasonably close
eps = step / 10000
bins = 10 ** np.arange(log_lower, log_upper + eps, step)
return u.Quantity(bins, e_min.unit, copy=False)


Expand Down
11 changes: 9 additions & 2 deletions pyirf/tests/test_binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,25 @@ def test_bins_per_decade():
bins = create_bins_per_decade(100 * u.GeV, 100 * u.TeV)

assert bins.unit == u.GeV
assert len(bins) == 15 # end non-inclusive
assert len(bins) == 16 # end inclusive if exactly fits per_decade

assert bins[0] == 100 * u.GeV
assert np.allclose(np.diff(np.log10(bins.to_value(u.GeV))), 0.2)

bins = create_bins_per_decade(100 * u.GeV, 100 * u.TeV, 10)
assert bins.unit == u.GeV
assert len(bins) == 30 # end non-inclusive
assert len(bins) == 31 # end inclusive since it fits exactly

assert bins[0] == 100 * u.GeV
assert np.allclose(np.diff(np.log10(bins.to_value(u.GeV))), 0.1)

bins = create_bins_per_decade(100 * u.GeV, 105 * u.TeV, 5)
assert bins.unit == u.GeV
assert len(bins) == 16 # end non-inclusive

assert u.isclose(bins[-1], 100 * u.TeV) # last value at correct difference
assert np.allclose(np.diff(np.log10(bins.to_value(u.GeV))), 0.2)


def test_create_histogram_table():
'''Test create histogram table'''
Expand Down

0 comments on commit 9b73191

Please sign in to comment.