Skip to content

Commit 11b8d2e

Browse files
committed
Replace interp2d with RectBivariateSpline
interp2d is gone in scipy 1.14, and RecBivariateSpline is 5x faster to evaluate. While the numerical results are identical, RectBivariateSpline returns a 2D array for scalar inputs, whereas interp2d returned a 1D array. Work around this by removing all 1-entry dimensions from the result. A notable exception is the acceptance in StandardLLH, which appears to have relied on the result being 1D (i.e. tests fail if the interpolation returns a scalar). Fixes #270
1 parent 49e712b commit 11b8d2e

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

flarestack/core/llh.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ def create_acceptance_function(self):
826826
with open(acc_path, "rb") as f:
827827
[dec_bins, gamma_bins, acc] = pickle.load(f)
828828

829-
f = scipy.interpolate.interp2d(dec_bins, gamma_bins, acc.T, kind="linear")
829+
f = scipy.interpolate.RectBivariateSpline(dec_bins, gamma_bins, acc, kx=1, ky=1)
830830
return f
831831

832832
def new_acceptance(self, source, params=None):
@@ -845,7 +845,7 @@ def new_acceptance(self, source, params=None):
845845
dec = source["dec_rad"]
846846
gamma = params[-1]
847847

848-
return self.acceptance_f(dec, gamma)
848+
return self.acceptance_f(dec, gamma)[0, :]
849849

850850
def create_kwargs(self, data, pull_corrector, weight_f=None):
851851
kwargs = dict()
@@ -1480,9 +1480,8 @@ def create_kwargs(self, data, pull_corrector, weight_f=None):
14801480
coincident_data = data[coincident_nu_mask]
14811481
coincident_sources = self.sources[coincident_source_mask]
14821482

1483-
season_weight = lambda x: weight_f([1.0, x], self.season)[
1484-
coincident_source_mask
1485-
]
1483+
def season_weight(x):
1484+
return weight_f([1.0, x], self.season)[coincident_source_mask]
14861485

14871486
SoB_energy_cache = self.create_SoB_energy_cache(coincident_data)
14881487

flarestack/icecube_utils/reference_sensitivity.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import logging
2-
import os
32

43
import numpy as np
5-
from scipy.interpolate import interp1d, interp2d
4+
from scipy.interpolate import RectBivariateSpline
65

76
from flarestack.data.icecube.ic_season import get_published_sens_ref_dir
87

@@ -62,10 +61,12 @@ def reference_7year_sensitivity(sindec=np.array(0.0), gamma=2.0):
6261

6362
sens = np.vstack((sens[0], sens))
6463
sens = np.vstack((sens, sens[-1]))
65-
sens_ref = interp2d(np.array(sindecs), np.array(gammas), np.log(sens.T))
64+
sens_ref = RectBivariateSpline(
65+
np.array(sindecs), np.array(gammas), np.log(sens), kx=1, ky=1
66+
)
6667

6768
if np.array(sindec).ndim > 0:
68-
return np.array([np.exp(sens_ref(x, gamma))[0] for x in sindec])
69+
return np.array([np.exp(sens_ref(x, gamma).squeeze()) for x in sindec])
6970
else:
7071
return np.exp(sens_ref(sindec, gamma))
7172

@@ -98,12 +99,14 @@ def reference_7year_discovery_potential(sindec=0.0, gamma=2.0):
9899

99100
disc = np.vstack((disc[0], disc))
100101
disc = np.vstack((disc, disc[-1]))
101-
disc_ref = interp2d(np.array(sindecs), np.array(gammas), np.log(disc.T))
102+
disc_ref = RectBivariateSpline(
103+
np.array(sindecs), np.array(gammas), np.log(disc), kx=1, ky=1
104+
)
102105

103106
if np.array(sindec).ndim > 0:
104-
return np.array([np.exp(disc_ref(x, gamma))[0] for x in sindec])
107+
return np.array([np.exp(disc_ref(x, gamma).squeeze()) for x in sindec])
105108
else:
106-
return np.exp(disc_ref(sindec, gamma))
109+
return np.exp(disc_ref(sindec, gamma).squeeze())
107110

108111

109112
def reference_10year_sensitivity(sindec=np.array(0.0), gamma=2.0):
@@ -130,12 +133,14 @@ def reference_10year_sensitivity(sindec=np.array(0.0), gamma=2.0):
130133
scaling = np.array([10 ** (3 * (i)) for i in range(2)])
131134
sens *= scaling
132135

133-
sens_ref = interp2d(np.array(sindecs), np.array(gammas), np.log(sens.T))
136+
sens_ref = RectBivariateSpline(
137+
np.array(sindecs), np.array(gammas), np.log(sens), kx=1, ky=1
138+
)
134139

135140
if np.array(sindec).ndim > 0:
136-
return np.array([np.exp(sens_ref(x, gamma))[0] for x in sindec])
141+
return np.array([np.exp(sens_ref(x, gamma).squeeze()) for x in sindec])
137142
else:
138-
return np.exp(sens_ref(sindec, gamma))
143+
return np.exp(sens_ref(sindec, gamma).squeeze())
139144

140145

141146
def reference_10year_discovery_potential(sindec=np.array(0.0), gamma=2.0):
@@ -162,9 +167,11 @@ def reference_10year_discovery_potential(sindec=np.array(0.0), gamma=2.0):
162167
scaling = np.array([10 ** (3 * i) for i in range(2)])
163168
sens *= scaling
164169

165-
sens_ref = interp2d(np.array(sindecs), np.array(gammas), np.log(sens.T))
170+
sens_ref = RectBivariateSpline(
171+
np.array(sindecs), np.array(gammas), np.log(sens), kx=1, ky=1
172+
)
166173

167174
if np.array(sindec).ndim > 0:
168-
return np.array([np.exp(sens_ref(x, gamma))[0] for x in sindec])
175+
return np.array([np.exp(sens_ref(x, gamma).squeeze()) for x in sindec])
169176
else:
170-
return np.exp(sens_ref(sindec, gamma))
177+
return np.exp(sens_ref(sindec, gamma).squeeze())

flarestack/utils/percentile_SoB.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ def make_plot(hist, savepath, normed=True):
244244

245245
order = 1
246246

247-
spline = scipy.interpolate.interp2d(x, y, np.log(ratio))
247+
spline = scipy.interpolate.RectBivariateSpline(
248+
x, y, np.log(ratio.T), kx=order, ky=order
249+
)
248250

249251
for x_val in [2.0, 3.0, 7.0]:
250252
print(x_val, spline(0.0, x_val), spline(0.5, x_val))

0 commit comments

Comments
 (0)