Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Aug 22, 2023
1 parent db105c1 commit 1164644
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 21 deletions.
2 changes: 2 additions & 0 deletions pyirf/interpolation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
BaseInterpolator,
DiscretePDFInterpolator,
ParametrizedInterpolator,
PDFNormalization,
)
from .component_estimators import (
BaseComponentEstimator,
Expand Down Expand Up @@ -39,6 +40,7 @@
"BaseInterpolator",
"BaseNearestNeighborSearcher",
"BaseExtrapolator",
"PDFNormalization",
"DiscretePDFExtrapolator",
"ParametrizedExtrapolator",
"DiscretePDFComponentEstimator",
Expand Down
9 changes: 5 additions & 4 deletions pyirf/interpolation/base_extrapolators.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,17 @@ def __init__(self, grid_points, bin_edges, binned_pdf, normalization=PDFNormaliz
Parameters
----------
grid_points: np.ndarray, shape=(n_points, n_dims)
grid_points : np.ndarray, shape=(n_points, n_dims)
Grid points at which templates exist
bin_edges: np.ndarray, shape=(n_bins+1)
bin_edges : np.ndarray, shape=(n_bins+1)
Edges of the data binning
binned_pdf: np.ndarray, shape=(n_points, ..., n_bins)
binned_pdf : np.ndarray, shape=(n_points, ..., n_bins)
Content of each bin in bin_edges for
each point in grid_points. First dimesion has to correspond to number
of grid_points, last dimension has to correspond to number of bins for
the quantity that should be extrapolated (e.g. the Migra axis for EDisp)
normalization : PDFNormalization
How the PDF is normalized
Note
----
Expand Down
13 changes: 0 additions & 13 deletions pyirf/interpolation/base_interpolators.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
"""Base classes for interpolators"""
from abc import ABCMeta, abstractmethod
import enum
import astropy.units as u

import numpy as np

from ..binning import bin_center
from ..utils import cone_solid_angle

__all__ = [
"BaseInterpolator",
"ParametrizedInterpolator",
"DiscretePDFInterpolator",
"PDFNormalization",
"get_bin_width",
]


Expand All @@ -27,16 +24,6 @@ class PDFNormalization(enum.Enum):
CONE_SOLID_ANGLE = enum.auto()


def get_bin_width(bin_edges, normalization):
if normalization is PDFNormalization.AREA:
return np.diff(bin_edges)

if normalization is PDFNormalization.CONE_SOLID_ANGLE:
return np.diff(cone_solid_angle(bin_edges).to_value(u.sr))

raise ValueError(f"Invalid PDF normalization: {normalization}")


class BaseInterpolator(metaclass=ABCMeta):
"""
Base class for all interpolators, only knowing grid-points,
Expand Down
9 changes: 8 additions & 1 deletion pyirf/interpolation/moment_morph_interpolator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from pyirf.binning import bin_center, calculate_bin_indices
from scipy.spatial import Delaunay

from .base_interpolators import DiscretePDFInterpolator, PDFNormalization, get_bin_width
from .base_interpolators import DiscretePDFInterpolator, PDFNormalization
from .utils import get_bin_width

__all__ = [
"MomentMorphInterpolator",
Expand All @@ -19,6 +20,8 @@ def _estimate_mean_std(bin_edges, binned_pdf, normalization):
Array of common bin-edges for binned_pdf
binned_pdf: np.ndarray, shape=(N, ..., M)
PDF values from which to compute mean and std
normalization : PDFNormalization
How the PDF is normalized
Returns
-------
Expand Down Expand Up @@ -189,6 +192,8 @@ def moment_morph_estimation(bin_edges, binned_pdf, coefficients, normalization):
Array of bin-entries, actual
coefficients: np.ndarray, shape=(N)
Estimation coefficients for each entry in binned_pdf
normalization : PDFNormalization
How the PDF is normalized
Returns
-------
Expand Down Expand Up @@ -273,6 +278,8 @@ def __init__(
the quantity that should be interpolated (e.g. the Migra axis for EDisp)
has to be at axis specified by axis-keyword as well as having entries
corresponding to the number of bins given through bin_edges keyword.
normalization : PDFNormalization
How the PDF is normalized
Note
----
Expand Down
5 changes: 3 additions & 2 deletions pyirf/interpolation/nearest_simplex_extrapolator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import numpy as np
from scipy.spatial import Delaunay

from pyirf.interpolation.base_interpolators import get_bin_width

from .base_extrapolators import DiscretePDFExtrapolator, ParametrizedExtrapolator, PDFNormalization
from .moment_morph_interpolator import (
barycentric_2D_interpolation_coefficients,
linesegment_1D_interpolation_coefficients,
moment_morph_estimation,
)
from .utils import find_nearest_simplex
from .utils import find_nearest_simplex, get_bin_width

__all__ = [
"MomentMorphNearestSimplexExtrapolator",
Expand Down Expand Up @@ -145,6 +144,8 @@ def __init__(self, grid_points, bin_edges, binned_pdf, normalization=PDFNormaliz
the quantity that should be interpolated (e.g. the Migra axis for EDisp)
has to be at the last axis as well as having entries
corresponding to the number of bins given through bin_edges keyword.
normalization : PDFNormalization
How the PDF is normalized
Note
----
Expand Down
3 changes: 2 additions & 1 deletion pyirf/interpolation/quantile_interpolator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
from scipy.interpolate import griddata, interp1d

from .base_interpolators import DiscretePDFInterpolator, PDFNormalization, get_bin_width
from .base_interpolators import DiscretePDFInterpolator, PDFNormalization
from .utils import get_bin_width

__all__ = ["QuantileInterpolator"]

Expand Down
12 changes: 12 additions & 0 deletions pyirf/interpolation/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,15 @@ def test_find_nearest_simplex(non_rect_grid):
assert find_nearest_simplex(non_rect_grid, np.array([-10, -10])) == 0
assert find_nearest_simplex(non_rect_grid, np.array([10, 30])) == 1
assert find_nearest_simplex(non_rect_grid, np.array([20.00000000001, -10])) == 2


def test_get_bin_width():
from pyirf.interpolation.utils import get_bin_width
from pyirf.interpolation import PDFNormalization

bins = np.array([0, 1, 3])
np.testing.assert_allclose(get_bin_width(bins, PDFNormalization.AREA), [1, 2])

bins = np.array([0, np.pi / 3, np.pi / 2])
width = get_bin_width(bins, PDFNormalization.CONE_SOLID_ANGLE)
np.testing.assert_allclose(width, [np.pi, np.pi])
14 changes: 14 additions & 0 deletions pyirf/interpolation/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import astropy.units as u
import numpy as np

from ..utils import cone_solid_angle
from .base_interpolators import PDFNormalization


def get_bin_width(bin_edges, normalization):
if normalization is PDFNormalization.AREA:
return np.diff(bin_edges)

if normalization is PDFNormalization.CONE_SOLID_ANGLE:
return np.diff(cone_solid_angle(bin_edges).to_value(u.sr))

raise ValueError(f"Invalid PDF normalization: {normalization}")

Check warning on line 15 in pyirf/interpolation/utils.py

View check run for this annotation

Codecov / codecov/patch

pyirf/interpolation/utils.py#L15

Added line #L15 was not covered by tests


def plumb_point_dist(line, target):
"""
Expand Down

0 comments on commit 1164644

Please sign in to comment.