Skip to content
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

Update interpolation code to pyirf v0.10 API #1184

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ dependencies:
- seaborn
- ctapipe_io_lst=0.22
- pytest
- pyirf=0.8
- pyirf~=0.10.0

56 changes: 29 additions & 27 deletions lstchain/high_level/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
)
from pyirf.interpolation import (
GridDataInterpolator,
interpolate_effective_area_per_energy_and_fov,
interpolate_energy_dispersion,
interpolate_psf_table,
interpolate_rad_max,
EnergyDispersionEstimator,
EffectiveAreaEstimator,
PSFTableEstimator,
RadMaxEstimator,
)
from scipy.spatial import Delaunay, distance, QhullError

Expand All @@ -34,6 +34,7 @@

log = logging.getLogger(__name__)


def interp_params(params_list, data):
"""
From a given list of angular parameters, to be used for interpolation,
Expand Down Expand Up @@ -361,8 +362,10 @@
----------
.. [1] https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html
"""
interp = GridDataInterpolator(grid_points=grid_points, params=gh_cuts)
gh_cuts_interp = interp(target_point, method=method)
interp = GridDataInterpolator(
grid_points=grid_points, params=gh_cuts, method=method
)
gh_cuts_interp = interp(target_point)
Comment on lines +364 to +367
Copy link
Member

Choose a reason for hiding this comment

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

If extrapolation is needed I'd suggest to create a Estimator object (same for the AL_CUTS), just like it is done for RAD_MAX tables in pyirf. If you don't want extrapolation this should be fine.


return gh_cuts_interp

Expand Down Expand Up @@ -402,8 +405,10 @@
----------
.. [1] https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html
"""
interp = GridDataInterpolator(grid_points=grid_points, params=al_cuts)
al_cuts_interp = interp(target_point, method=method)
interp = GridDataInterpolator(
grid_points=grid_points, params=al_cuts, method=method
)
al_cuts_interp = interp(target_point)

return al_cuts_interp

Expand Down Expand Up @@ -507,12 +512,11 @@
e_true = np.append(temp_irf["ENERG_LO"][0], temp_irf["ENERG_HI"][0][-1])
fov_off = np.append(temp_irf["THETA_LO"][0], temp_irf["THETA_HI"][0][-1])

aeff_interp = interpolate_effective_area_per_energy_and_fov(
effective_area=effarea_list,
aeff_estimator = EffectiveAreaEstimator(
grid_points=irf_pars_sel,
target_point=interp_pars_sel,
method=interp_method
effective_area=effarea_list,
)
aeff_interp = aeff_estimator(interp_pars_sel)

aeff_hdu_interp = create_aeff2d_hdu(
effective_area=aeff_interp.T[0],
Expand Down Expand Up @@ -540,13 +544,12 @@
e_migra = np.append(temp_irf["MIGRA_LO"][0], temp_irf["MIGRA_HI"][0][-1])
fov_off = np.append(temp_irf["THETA_LO"][0], temp_irf["THETA_HI"][0][-1])

edisp_interp = interpolate_energy_dispersion(
migra_bins=e_migra,
edisps=edisp_list,
edisp_estimator = EnergyDispersionEstimator(
grid_points=irf_pars_sel,
target_point=interp_pars_sel,
quantile_resolution=1e-3
migra_bins=e_migra,
energy_dispersion=edisp_list,
)
edisp_interp = edisp_estimator(interp_pars_sel)

edisp_hdu_interp = create_energy_dispersion_hdu(
energy_dispersion=edisp_interp[0],
Expand Down Expand Up @@ -575,7 +578,7 @@
gh_cuts=gh_cuts_list,
grid_points=irf_pars_sel,
target_point=interp_pars_sel,
method=interp_method
method=interp_method,
)

gh_header = fits.Header()
Expand All @@ -599,12 +602,11 @@
radmax_list = load_irf_grid(irfs, extname="RAD_MAX", interp_col="RAD_MAX")
temp_irf = QTable.read(irfs[0], hdu="RAD_MAX")

rad_max_interp = interpolate_rad_max(
rad_max=radmax_list,
rad_max_estimator = RadMaxEstimator(
grid_points=irf_pars_sel,
target_point=interp_pars_sel,
method=interp_method
rad_max=radmax_list,
)
rad_max_interp = rad_max_estimator(interp_pars_sel)

temp_irf["RAD_MAX"] = rad_max_interp[0].T[np.newaxis, ...] * u.deg

Expand Down Expand Up @@ -661,13 +663,13 @@
src_bins = np.append(temp_irf["RAD_LO"][0], temp_irf["RAD_HI"][0][-1])
fov_off = np.append(temp_irf["THETA_LO"][0], temp_irf["THETA_HI"][0][-1])

psf_interp = interpolate_psf_table(
source_offset_bins=src_bins,
psfs=psf_list,
psf_estimator = PSFTableEstimator(

Check warning on line 666 in lstchain/high_level/interpolate.py

View check run for this annotation

Codecov / codecov/patch

lstchain/high_level/interpolate.py#L666

Added line #L666 was not covered by tests
grid_points=irf_pars_sel,
target_point=interp_pars_sel,
quantile_resolution=1e-3,
source_offset_bins=src_bins,
psf=psf_list,
)
psf_interp = psf_estimator(interp_pars_sel)

Check warning on line 671 in lstchain/high_level/interpolate.py

View check run for this annotation

Codecov / codecov/patch

lstchain/high_level/interpolate.py#L671

Added line #L671 was not covered by tests

psf_hdu_interp = create_psf_table_hdu(
psf=psf_interp[0],
true_energy=e_true,
Expand Down
3 changes: 2 additions & 1 deletion lstchain/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,8 @@ def read_mc_dl2_to_QTable(filename):
energy_max=simu_info.energy_range_max,
max_impact=simu_info.max_scatter_range,
spectral_index=simu_info.spectral_index,
viewcone=simu_info.max_viewcone_radius
viewcone_min=simu_info.min_viewcone_radius,
viewcone_max=simu_info.max_viewcone_radius
)

events = pd.read_hdf(filename, key=dl2_params_lstcam_key)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def find_scripts(script_dir, prefix):
'numpy',
'pandas',
'protobuf~=3.20.0',
'pyirf~=0.8.0',
'pyirf~=0.10.0',
'scipy>=1.8',
'seaborn',
'scikit-learn~=1.2',
Expand Down
Loading