Repository navigation
Fix np.digitize mapping values at the upper bin edge to an out-of-range index #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ed3028b
Fix np.digitize mapping values at the upper bin edge to an out-of-ran…
tomaskontrimas c5a0007
Document that the last bin includes the upper edge in smearing matrix…
tomaskontrimas 4173646
Use get_bin_indices_from_binedges when loading the effective area
tomaskontrimas 40ae2e6
Use get_bin_indices_from_binedges instead of clipped np.digitize for …
tomaskontrimas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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