Skip to content

Commit 181bf16

Browse files
committed
Fix handling of events_match check so it can manually handle the csky arrays being passed in. It was always returning False before, meaning we wasted time in processing.
1 parent 7ac3fe9 commit 181bf16

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

kingmaker/wrapper.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from .pdf import InterpolatedKingPDF, TemplateSmearedKingPDF
1111
from .fitting import KingPSFFitter
12-
from .utils import angular_distance
12+
from .utils import angular_distance, _interp1d
1313

1414

1515
class KingSpatialLikelihood:
@@ -133,12 +133,15 @@ def __init__(
133133
return
134134

135135
def events_match(self, events: npt.NDArray[Any]):
136+
if self.events is None:
137+
return False
136138
try:
137-
events_match = self.events == events
138-
if isinstance(events_match, bool):
139-
return events_match
140-
else:
141-
return all(events_match)
139+
events_match = True
140+
for key in self.events.keys():
141+
events_match &= all(self.events[key] == events[key])
142+
if not events_match:
143+
return False
144+
return events_match
142145
except ValueError:
143146
return np.array_equal(self.events, events)
144147

@@ -181,14 +184,14 @@ def set_events(
181184
# Begin by finding the per-event alpha and beta values via interpolation.
182185
# Extract the bin centers and keys for each event. The stored bins are
183186
# edges, but interpn requires coordinates matching the values shape.
184-
keys, bins = [], []
185-
187+
keys, bins, event_param_values = [], [], []
186188
for key, edges in self.parametrization_bins.items():
187189
keys.append(key)
188190
bins.append((edges[:-1] + edges[1:]) / 2)
191+
event_param_values.append(events[key])
189192

190193
# Get the event parameter values for each parameter
191-
event_param_values = np.array([events[key] for key in keys]).T
194+
event_param_values = np.array(event_param_values).T
192195

193196
# Interpolate to get the alpha and beta values for each spectral index for
194197
# each event in the given sample.
@@ -232,10 +235,10 @@ def set_events(
232235
# Otherwise, we can calculate the angular separation for each event now.
233236
# TODO: Ensure the broadcasting works properly here if we have multiple sources...
234237
else:
238+
assert source_ras is not None
235239
self.event_distances = angular_distance(
236240
events["ra"], events["dec"], source_ras, source_decs
237241
)
238-
assert source_ras is not None
239242
if (not self.multiple_source_warning_logged) and (len(source_ras) > 1):
240243
logging.warning(
241244
"Multiple source positions provided. This has not been tested and"
@@ -267,11 +270,14 @@ def evaluate_pdf(self, events: npt.NDArray[Any], gamma: float = 2) -> npt.NDArra
267270
return cast(npt.NDArray[np.floating], self.event_pvalue[gamma])
268271
else:
269272
# Otherwise we have to interpolate the gamma values.
270-
pvalues = np.array([self.event_pvalue[g] for g in self.spectral_indices])
271273
idx = np.clip(
272274
np.searchsorted(self.spectral_indices, gamma) - 1, 0, len(self.spectral_indices) - 2
273275
)
274-
t = (gamma - self.spectral_indices[idx]) / (
275-
self.spectral_indices[idx + 1] - self.spectral_indices[idx]
276+
gamma_low, gamma_high = self.spectral_indices[idx], self.spectral_indices[idx + 1]
277+
return _interp1d(
278+
gamma,
279+
gamma_low,
280+
gamma_high,
281+
self.event_pvalue[gamma_low],
282+
self.event_pvalue[gamma_high],
276283
)
277-
return (1 - t) * pvalues[idx] + t * pvalues[idx + 1] # type: ignore[no-any-return]

0 commit comments

Comments
 (0)