|
9 | 9 |
|
10 | 10 | from .pdf import InterpolatedKingPDF, TemplateSmearedKingPDF |
11 | 11 | from .fitting import KingPSFFitter |
12 | | -from .utils import angular_distance |
| 12 | +from .utils import angular_distance, _interp1d |
13 | 13 |
|
14 | 14 |
|
15 | 15 | class KingSpatialLikelihood: |
@@ -133,12 +133,15 @@ def __init__( |
133 | 133 | return |
134 | 134 |
|
135 | 135 | def events_match(self, events: npt.NDArray[Any]): |
| 136 | + if self.events is None: |
| 137 | + return False |
136 | 138 | 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 |
142 | 145 | except ValueError: |
143 | 146 | return np.array_equal(self.events, events) |
144 | 147 |
|
@@ -181,14 +184,14 @@ def set_events( |
181 | 184 | # Begin by finding the per-event alpha and beta values via interpolation. |
182 | 185 | # Extract the bin centers and keys for each event. The stored bins are |
183 | 186 | # edges, but interpn requires coordinates matching the values shape. |
184 | | - keys, bins = [], [] |
185 | | - |
| 187 | + keys, bins, event_param_values = [], [], [] |
186 | 188 | for key, edges in self.parametrization_bins.items(): |
187 | 189 | keys.append(key) |
188 | 190 | bins.append((edges[:-1] + edges[1:]) / 2) |
| 191 | + event_param_values.append(events[key]) |
189 | 192 |
|
190 | 193 | # 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 |
192 | 195 |
|
193 | 196 | # Interpolate to get the alpha and beta values for each spectral index for |
194 | 197 | # each event in the given sample. |
@@ -232,10 +235,10 @@ def set_events( |
232 | 235 | # Otherwise, we can calculate the angular separation for each event now. |
233 | 236 | # TODO: Ensure the broadcasting works properly here if we have multiple sources... |
234 | 237 | else: |
| 238 | + assert source_ras is not None |
235 | 239 | self.event_distances = angular_distance( |
236 | 240 | events["ra"], events["dec"], source_ras, source_decs |
237 | 241 | ) |
238 | | - assert source_ras is not None |
239 | 242 | if (not self.multiple_source_warning_logged) and (len(source_ras) > 1): |
240 | 243 | logging.warning( |
241 | 244 | "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 |
267 | 270 | return cast(npt.NDArray[np.floating], self.event_pvalue[gamma]) |
268 | 271 | else: |
269 | 272 | # Otherwise we have to interpolate the gamma values. |
270 | | - pvalues = np.array([self.event_pvalue[g] for g in self.spectral_indices]) |
271 | 273 | idx = np.clip( |
272 | 274 | np.searchsorted(self.spectral_indices, gamma) - 1, 0, len(self.spectral_indices) - 2 |
273 | 275 | ) |
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], |
276 | 283 | ) |
277 | | - return (1 - t) * pvalues[idx] + t * pvalues[idx + 1] # type: ignore[no-any-return] |
|
0 commit comments