Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions skyllh/analyses/i3/publicdata_ps/aeff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from scipy import interpolate

from skyllh.core.binning import (
get_bin_indices_from_binedges,
get_bin_indices_from_lower_and_upper_binedges,
get_bincenters_from_binedges,
)
Expand Down Expand Up @@ -78,8 +79,13 @@ def load_effective_area_array(
# Construct the 2d array for the effective area.
aeff_decnu_log10enu = np.zeros((nbins_decnu, nbins_log10_enu), dtype=np.double)

decnu_idx = np.digitize(0.5 * (data['decnu_min'] + data['decnu_max']), decnu_binedges_lower) - 1
log10enu_idx = np.digitize(0.5 * (data['log10_enu_min'] + data['log10_enu_max']), log10_enu_binedges_lower) - 1
decnu_binedges = np.append(decnu_binedges_lower, decnu_binedges_upper[-1])
log10_enu_binedges = np.append(log10_enu_binedges_lower, log10_enu_binedges_upper[-1])

decnu_idx = get_bin_indices_from_binedges(decnu_binedges, 0.5 * (data['decnu_min'] + data['decnu_max']))
log10enu_idx = get_bin_indices_from_binedges(
log10_enu_binedges, 0.5 * (data['log10_enu_min'] + data['log10_enu_max'])
)

aeff_decnu_log10enu[decnu_idx, log10enu_idx] = data['a_eff']

Expand Down Expand Up @@ -238,6 +244,32 @@ def aeff_decnu_log10enu(self):
"""
return self._aeff_decnu_log10enu

def get_decnu_idx(self, decnu: float) -> int:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is here so that it merges with #342 with less conflicts

"""Returns the index of the true neutrino declination bin for the given declination.

Parameters
----------
decnu
The true neutrino declination.

Returns
-------
decnu_idx
The index of the true neutrino declination bin.

Raises
------
ValueError
If the given declination is outside the declination binning of the
effective area.
"""
if (decnu < self._decnu_binedges[0]) or (decnu > self._decnu_binedges[-1]):
raise ValueError(f'The declination {decnu} radians is not supported by the effective area!')

decnu_idx = get_bin_indices_from_binedges(self._decnu_binedges, decnu)

return int(decnu_idx)

def get_aeff_for_decnu(self, decnu: float) -> np.ndarray:
"""Retrieves the effective area as function of log10_enu.

Expand All @@ -252,9 +284,7 @@ def get_aeff_for_decnu(self, decnu: float) -> np.ndarray:
The effective area in cm^2 for the given true neutrino declination
as a function of log10 true neutrino energy.
"""
decnu_idx = np.digitize(decnu, self._decnu_binedges) - 1

aeff = self._aeff_decnu_log10enu[decnu_idx]
aeff = self._aeff_decnu_log10enu[self.get_decnu_idx(decnu)]

return aeff

Expand Down
5 changes: 3 additions & 2 deletions skyllh/analyses/i3/publicdata_ps/backgroundpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from skyllh.core.binning import (
BinningDefinition,
UsesBinning,
get_bin_indices_from_binedges,
)
from skyllh.core.pdf import (
EnergyPDF,
Expand Down Expand Up @@ -568,8 +569,8 @@ def get_pd(
log10emu = tdm['log_energy']
sindecmu = tdm['sin_dec']

log10emu_idxs = np.digitize(log10emu, self.get_binning('log_energy').binedges) - 1
sindecmu_idxs = np.digitize(sindecmu, self.get_binning('sin_dec').binedges) - 1
log10emu_idxs = get_bin_indices_from_binedges(self.get_binning('log_energy').binedges, log10emu)
sindecmu_idxs = get_bin_indices_from_binedges(self.get_binning('sin_dec').binedges, sindecmu)

with TaskTimer(tl, 'Evaluating sindecmu-log10emu PDF.'):
pd = self._hist_logE_sinDec[(log10emu_idxs, sindecmu_idxs)]
Expand Down
3 changes: 2 additions & 1 deletion skyllh/analyses/i3/publicdata_ps/signal_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from skyllh.analyses.i3.publicdata_ps.utils import (
psi_to_dec_and_ra,
)
from skyllh.core.binning import get_bin_indices_from_binedges
from skyllh.core.dataset import Dataset
from skyllh.core.flux_model import (
TimeFluxProfile,
Expand Down Expand Up @@ -359,7 +360,7 @@ def _draw_signal_events_for_source(

events['log_true_energy'] = log_true_e

log_true_e_idxs = np.digitize(log_true_e, bins=sm.true_e_bin_edges) - 1
log_true_e_idxs = get_bin_indices_from_binedges(sm.true_e_bin_edges, log_true_e)

# Sample reconstructed energies given true neutrino energies.
(log_e_idxs, log_e) = sm.sample_log_e(rss, dec_idx, log_true_e_idxs)
Expand Down
14 changes: 8 additions & 6 deletions skyllh/analyses/i3/publicdata_ps/smearing_matrix.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np

from skyllh.core.binning import get_bin_indices_from_binedges
from skyllh.core.random import RandomStateService
from skyllh.core.storage import create_FileLoader

Expand Down Expand Up @@ -318,12 +319,12 @@ def get_true_dec_idx(self, true_dec: float) -> int:
-------
true_dec_idx
The index of the declination bin for the given declination value.
The last bin includes the upper-most bin edge.
"""
if (true_dec < self.true_dec_bin_edges[0]) or (true_dec > self.true_dec_bin_edges[-1]):
raise ValueError(f'The declination {true_dec} radians is not supported by the smearing matrix!')

# np.digitize maps the upper-most edge to n_bins, so clamp it into the last bin.
true_dec_idx = min(np.digitize(true_dec, self.true_dec_bin_edges) - 1, self.n_true_dec_bins - 1)
true_dec_idx = get_bin_indices_from_binedges(self.true_dec_bin_edges, true_dec)

return int(true_dec_idx)

Expand All @@ -335,9 +336,10 @@ def get_log10_true_e_idx(self, log10_true_e: float, upper_edge: bool = False) ->
log10_true_e
The log10 value of the true energy.
upper_edge
If False (default), return the bin index whose lower edge is
less than or equal to ``log10_true_e`` and whose upper edge is
strictly greater than ``log10_true_e``.
If False (default), return the index of the bin containing
``log10_true_e``. Bins are half-open ``[lower, upper)``, except
the last bin, which is closed ``[lower, upper]``, so a value
equal to the upper-most edge maps to the last bin.
If True, return the enclosing upper-edge index. This is useful
to align an upper range boundary to the nearest bin edge without
adding +1 at the call site.
Expand All @@ -362,7 +364,7 @@ def get_log10_true_e_idx(self, log10_true_e: float, upper_edge: bool = False) ->
if upper_edge:
return int(np.digitize(log10_true_e, self._true_e_bin_edges, right=True))

log10_true_e_idx = np.digitize(log10_true_e, self._true_e_bin_edges) - 1
log10_true_e_idx = get_bin_indices_from_binedges(self._true_e_bin_edges, log10_true_e)

return int(log10_true_e_idx)

Expand Down
37 changes: 36 additions & 1 deletion skyllh/core/binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,41 @@ def get_binedges_from_bincenters(centers: np.ndarray) -> np.ndarray:
return edges


def get_bin_indices_from_binedges(binedges: np.ndarray, values: np.ndarray | float) -> np.ndarray:
"""Returns the bin indices of the given values for the given bin edges.

This is equivalent to ``np.digitize(values, binedges) - 1``, except that a
value equal to the upper-most bin edge is assigned to the last bin, i.e.
the last bin is closed, like it is for ``np.histogram``.
``np.digitize`` would assign it the out-of-range index ``nbins``.

Note: Values outside the binning range are not treated specially. They
get the index -1 (below the lowest edge) or ``nbins`` (above the highest
edge). Callers must check the range themselves if required.

Parameters
----------
binedges
The (nbins+1,)-shaped 1D ndarray holding the monotonically increasing
bin edges.
values
The value(s) for which to get the bin indices.

Returns
-------
idxs
The bin indices of the given values, with the same shape as
``values``.
"""
binedges = np.asarray(binedges)
values = np.asarray(values)

idxs = np.digitize(values, binedges) - 1
idxs = np.where(values == binedges[-1], len(binedges) - 2, idxs)

return idxs


def get_bin_indices_from_lower_and_upper_binedges(le: np.ndarray, ue: np.ndarray, values: np.ndarray) -> np.ndarray:
"""Returns the bin indices for the given values which must fall into bins
defined by the given lower and upper bin edges.
Expand Down Expand Up @@ -202,7 +237,7 @@ def any_data_out_of_range(self, data: np.ndarray) -> bool:

def get_binwidth_from_value(self, value):
"""Returns the width of the bin the given value falls into."""
idx = np.digitize(value, self._binedges) - 1
idx = get_bin_indices_from_binedges(self._binedges, value)

bin_width = self.binwidths[idx]

Expand Down
6 changes: 3 additions & 3 deletions skyllh/i3/pdf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from skyllh.core.binning import BinningDefinition, UsesBinning
from skyllh.core.binning import BinningDefinition, UsesBinning, get_bin_indices_from_binedges
from skyllh.core.logging import (
get_logger,
)
Expand Down Expand Up @@ -289,8 +289,8 @@ def get_pd( # pyright: ignore[reportIncompatibleMethodOverride]
log10_energy_binning = self.get_binning('log_energy')
sin_dec_binning = self.get_binning('sin_dec')

log10_energy_idx = np.digitize(tdm['log_energy'], log10_energy_binning.binedges) - 1
sin_dec_idx = np.digitize(tdm['sin_dec'], sin_dec_binning.binedges) - 1
log10_energy_idx = get_bin_indices_from_binedges(log10_energy_binning.binedges, tdm['log_energy'])
sin_dec_idx = get_bin_indices_from_binedges(sin_dec_binning.binedges, tdm['sin_dec'])

with TaskTimer(tl, 'Evaluating log10_energy-sin_dec histogram.'):
pd = self._hist_log10_energy_sin_dec[(log10_energy_idx, sin_dec_idx)]
Expand Down
48 changes: 48 additions & 0 deletions tests/core/test_binning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import unittest

import numpy as np

from skyllh.core.binning import (
BinningDefinition,
get_bin_indices_from_binedges,
)


class GetBinIndicesFromBinedgesTestCase(unittest.TestCase):
def setUp(self):
self.edges = np.array([-1.0, -0.5, 0.0, 0.5, 1.0])
self.nbins = len(self.edges) - 1

def test_matches_digitize_inside_range(self):
values = np.linspace(self.edges[0], self.edges[-1], 1001)[:-1]
np.testing.assert_array_equal(
get_bin_indices_from_binedges(self.edges, values),
np.digitize(values, self.edges) - 1,
)

def test_upper_edge_is_last_bin(self):
self.assertEqual(int(get_bin_indices_from_binedges(self.edges, 1.0)), self.nbins - 1)
np.testing.assert_array_equal(
get_bin_indices_from_binedges(self.edges, np.array([-1.0, 1.0])),
[0, self.nbins - 1],
)

def test_out_of_range_is_unchanged(self):
np.testing.assert_array_equal(
get_bin_indices_from_binedges(self.edges, np.array([-1.5, 1.5])),
[-1, self.nbins],
)

def test_scalar_returns_0d(self):
self.assertEqual(np.ndim(get_bin_indices_from_binedges(self.edges, 0.2)), 0)


class BinningDefinitionTestCase(unittest.TestCase):
def test_get_binwidth_from_value_at_upper_edge(self):
binning = BinningDefinition('x', np.array([0.0, 1.0, 3.0]))
self.assertEqual(binning.get_binwidth_from_value(3.0), 2.0)
self.assertEqual(binning.get_binwidth_from_value(0.0), 1.0)


if __name__ == '__main__':
unittest.main()
69 changes: 69 additions & 0 deletions tests/publicdata_ps/test_aeff_decnu_idx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import unittest

import numpy as np

from skyllh.analyses.i3.publicdata_ps.aeff import PDAeff


class GetAeffForDecnuTestCase(unittest.TestCase):
"""Tests for PDAeff.get_aeff_for_decnu, in particular the handling of the
declination poles (-90 and +90 degrees).
"""

def setUp(self):
self.edges = np.radians([-90.0, -30.0, 0.0, 30.0, 90.0])
self.n_bins = len(self.edges) - 1
self.aeff = object.__new__(PDAeff)
self.aeff._decnu_binedges = self.edges
# Each row holds its own declination bin index.
self.aeff._aeff_decnu_log10enu = np.repeat(np.arange(self.n_bins, dtype=np.double)[:, np.newaxis], 3, axis=1)

def test_south_pole_is_first_bin(self):
np.testing.assert_array_equal(self.aeff.get_aeff_for_decnu(-np.pi / 2), 0)

def test_north_pole_is_last_bin(self):
np.testing.assert_array_equal(self.aeff.get_aeff_for_decnu(np.pi / 2), self.n_bins - 1)

def test_interior_values_fall_in_containing_bin(self):
for dec in np.linspace(self.edges[0], self.edges[-1], 101)[:-1]:
idx = int(self.aeff.get_aeff_for_decnu(dec)[0])
self.assertTrue(self.edges[idx] <= dec < self.edges[idx + 1])

def test_outside_range_raises(self):
with self.assertRaises(ValueError):
self.aeff.get_aeff_for_decnu(np.pi / 2 + 1e-6)
with self.assertRaises(ValueError):
self.aeff.get_aeff_for_decnu(-np.pi / 2 - 1e-6)


class GetDecnuIdxTestCase(unittest.TestCase):
"""Tests for PDAeff.get_decnu_idx at the declination poles and edges."""

def setUp(self):
self.edges = np.radians([-90.0, -30.0, 0.0, 30.0, 90.0])
self.n_bins = len(self.edges) - 1
self.aeff = object.__new__(PDAeff)
self.aeff._decnu_binedges = self.edges

def test_returns_python_int(self):
self.assertIs(type(self.aeff.get_decnu_idx(0.1)), int)

def test_south_pole_is_first_bin(self):
self.assertEqual(self.aeff.get_decnu_idx(-np.pi / 2), 0)

def test_north_pole_is_last_bin(self):
self.assertEqual(self.aeff.get_decnu_idx(np.pi / 2), self.n_bins - 1)

def test_inner_edges_belong_to_upper_bin(self):
for i, edge in enumerate(self.edges[1:-1], start=1):
self.assertEqual(self.aeff.get_decnu_idx(edge), i)

def test_outside_range_raises(self):
with self.assertRaises(ValueError):
self.aeff.get_decnu_idx(np.pi / 2 + 1e-6)
with self.assertRaises(ValueError):
self.aeff.get_decnu_idx(-np.pi / 2 - 1e-6)


if __name__ == '__main__':
unittest.main()
48 changes: 48 additions & 0 deletions tests/publicdata_ps/test_backgroundpdf_get_pd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import unittest

import numpy as np

from skyllh.analyses.i3.publicdata_ps.backgroundpdf import PDMCBackgroundI3EnergyPDF
from skyllh.core.binning import BinningDefinition
from skyllh.core.config import Config


class PDMCBackgroundI3EnergyPDFGetPdTestCase(unittest.TestCase):
"""Tests the bin lookup of PDMCBackgroundI3EnergyPDF.get_pd at the
edges of its binning. Out-of-range data is rejected beforehand by
assert_is_valid_for_trial_data.
"""

def setUp(self):
self.log10emu_edges = np.array([1.0, 2.0, 3.0, 4.0])
self.sindecmu_edges = np.array([-1.0, 0.0, 1.0])
# Unique value per bin: 10*log10emu_idx + sindecmu_idx + 1.
hist = (
10 * np.arange(len(self.log10emu_edges) - 1)[:, np.newaxis]
+ np.arange(len(self.sindecmu_edges) - 1)[np.newaxis, :]
+ 1.0
)
self.pdf = PDMCBackgroundI3EnergyPDF(
pdf_log10emu_sindecmu=hist,
log10emu_binning=BinningDefinition('log_energy', self.log10emu_edges),
sindecmu_binning=BinningDefinition('sin_dec', self.sindecmu_edges),
cfg=Config(),
)

def _get_pd(self, log_energy, sin_dec):
tdm = {'log_energy': np.asarray(log_energy), 'sin_dec': np.asarray(sin_dec)}
(pd, grads) = self.pdf.get_pd(tdm=tdm) # pyright: ignore[reportArgumentType]
self.assertEqual(grads, {})
return pd

def test_upper_edges_are_last_bins(self):
pd = self._get_pd([4.0, 1.0, 4.0], [1.0, 1.0, -1.0])
np.testing.assert_array_equal(pd, [22.0, 2.0, 21.0])

def test_interior_values(self):
pd = self._get_pd([1.5, 3.5, 2.0], [-0.5, 0.5, 0.0])
np.testing.assert_array_equal(pd, [1.0, 22.0, 12.0])


if __name__ == '__main__':
unittest.main()
Loading
Loading