def test_confidence_frozen(backend): dataset = MyDataset() dataset.models.parameters["x"].frozen = True fit = Fit([dataset]) fit.optimize(backend=backend) result = fit.confidence("y") assert result["success"] is True assert_allclose(result["errp"], 1) assert_allclose(result["errn"], 1)
def test_confidence(backend): dataset = MyDataset() fit = Fit([dataset]) fit.optimize(backend=backend) result = fit.confidence("x") assert result["success"] is True assert_allclose(result["errp"], 1) assert_allclose(result["errn"], 1) # Check that original value state wasn't changed assert_allclose(dataset.models.parameters["x"].value, 2)
class LightCurveEstimator: """Estimates flux values of model component in time intervals and returns a `gammapy.time.LightCurve` object. The estimator will fit the source model component to datasets in each of the time intervals provided. If no time intervals are provided, the estimator will use the time intervals defined by the datasets GTIs. To be included in the estimation, the dataset must have their GTI fully overlapping a time interval. Parameters ---------- datasets : list of `~gammapy.spectrum.SpectrumDataset` or `~gammapy.cube.MapDataset` Spectrum or Map datasets. time_intervals : list of `astropy.time.Time` Start and stop time for each interval to compute the LC source : str For which source in the model to compute the flux points. Default is '' norm_min : float Minimum value for the norm used for the fit statistic profile evaluation. norm_max : float Maximum value for the norm used for the fit statistic profile evaluation. norm_n_values : int Number of norm values used for the fit statistic profile. norm_values : `numpy.ndarray` Array of norm values to be used for the fit statistic profile. sigma : int Sigma to use for asymmetric error computation. sigma_ul : int Sigma to use for upper limit computation. reoptimize : bool reoptimize other parameters during fit statistic scan? """ def __init__( self, datasets, time_intervals=None, source="", norm_min=0.2, norm_max=5, norm_n_values=11, norm_values=None, sigma=1, sigma_ul=2, reoptimize=False, ): self.datasets = Datasets(datasets) if not self.datasets.is_all_same_type and self.datasets.is_all_same_shape: raise ValueError( "Light Curve estimation requires a list of datasets" " of the same type and data shape.") if time_intervals is None: time_intervals = [ Time([d.gti.time_start[0], d.gti.time_stop[-1]]) for d in self.datasets ] self._check_and_sort_time_intervals(time_intervals) dataset = self.datasets[0] if isinstance(dataset, SpectrumDatasetOnOff): model = dataset.model else: model = dataset.model[source].spectral_model self.model = ScaleSpectralModel(model) self.model.norm.min = 0 self.model.norm.max = 1e5 if norm_values is None: norm_values = np.logspace(np.log10(norm_min), np.log10(norm_max), norm_n_values) self.norm_values = norm_values self.sigma = sigma self.sigma_ul = sigma_ul self.reoptimize = reoptimize self.source = source self.group_table_info = None def _check_and_sort_time_intervals(self, time_intervals): """Sort the time_intervals by increasing time if not already ordered correctly. Parameters ---------- time_intervals : list of `astropy.time.Time` Start and stop time for each interval to compute the LC """ time_start = Time([interval[0] for interval in time_intervals]) time_stop = Time([interval[1] for interval in time_intervals]) sorted_indices = time_start.argsort() time_start_sorted = time_start[sorted_indices] time_stop_sorted = time_stop[sorted_indices] diff_time_stop = np.diff(time_stop_sorted) diff_time_interval_edges = time_start_sorted[1:] - time_stop_sorted[:-1] if np.any(diff_time_stop < 0) or np.any(diff_time_interval_edges < 0): raise ValueError( "LightCurveEstimator requires non-overlapping time bins.") else: self.time_intervals = [ Time([tstart, tstop]) for tstart, tstop in zip(time_start_sorted, time_stop_sorted) ] def _set_scale_model(self, datasets): """ Parameters ---------- datasets : `~gammapy.modeling.Datasets` the list of dataset object """ # set the model on all datasets for dataset in datasets: if isinstance(dataset, SpectrumDatasetOnOff): dataset.model = self.model else: dataset.model[self.source].spectral_model = self.model @property def ref_model(self): return self.model.model def run(self, e_ref, e_min, e_max, steps="all", atol="1e-6 s"): """Run light curve extraction. Normalize integral and energy flux between emin and emax. Parameters ---------- e_ref : `~astropy.units.Quantity` reference energy of dnde flux normalization e_min : `~astropy.units.Quantity` minimum energy of integral and energy flux normalization interval e_max : `~astropy.units.Quantity` minimum energy of integral and energy flux normalization interval steps : list of str Which steps to execute. Available options are: * "err": estimate symmetric error. * "errn-errp": estimate asymmetric errors. * "ul": estimate upper limits. * "ts": estimate ts and sqrt(ts) values. * "norm-scan": estimate fit statistic profiles. By default all steps are executed. atol : `~astropy.units.Quantity` Tolerance value for time comparison with different scale. Default 1e-6 sec. Returns ------- lightcurve : `~gammapy.time.LightCurve` the Light Curve object """ atol = u.Quantity(atol) self.e_ref = e_ref self.e_min = e_min self.e_max = e_max rows = [] self.group_table_info = group_datasets_in_time_interval( datasets=self.datasets, time_intervals=self.time_intervals, atol=atol) if np.all(self.group_table_info["Group_ID"] == -1): raise ValueError( "LightCurveEstimator: No datasets in time intervals") for igroup, time_interval in enumerate(self.time_intervals): index_dataset = np.where( self.group_table_info["Group_ID"] == igroup)[0] if len(index_dataset) == 0: log.debug("No Dataset for the time interval " + str(igroup)) continue row = { "time_min": time_interval[0].mjd, "time_max": time_interval[1].mjd } interval_list_dataset = Datasets( [self.datasets[int(_)].copy() for _ in index_dataset]) self._set_scale_model(interval_list_dataset) row.update( self.estimate_time_bin_flux(interval_list_dataset, time_interval, steps)) rows.append(row) table = table_from_row_data(rows=rows, meta={"SED_TYPE": "likelihood"}) table = FluxPoints(table).to_sed_type("flux").table return LightCurve(table) def estimate_time_bin_flux(self, datasets, time_interval, steps="all"): """Estimate flux point for a single energy group. Parameters ---------- datasets : `~gammapy.modeling.Datasets` the list of dataset object time_interval : astropy.time.Time` Start and stop time for each interval steps : list of str Which steps to execute. Available options are: * "err": estimate symmetric error. * "errn-errp": estimate asymmetric errors. * "ul": estimate upper limits. * "ts": estimate ts and sqrt(ts) values. * "norm-scan": estimate likelihood profiles. By default all steps are executed. Returns ------- result : dict Dict with results for the flux point. """ self.fit = Fit([datasets]) result = { "e_ref": self.e_ref, "e_min": self.e_min, "e_max": self.e_max, "ref_dnde": self.ref_model(self.e_ref), "ref_flux": self.ref_model.integral(self.e_min, self.e_max), "ref_eflux": self.ref_model.energy_flux(self.e_min, self.e_max), "ref_e2dnde": self.ref_model(self.e_ref) * self.e_ref**2, } result.update(self.estimate_norm()) if not result.pop("success"): log.warning("Fit failed for time bin between {t_min} and {t_max}," " setting NaN.".format(t_min=time_interval[0].mjd, t_max=time_interval[1].mjd)) if steps == "all": steps = ["err", "counts", "errp-errn", "ul", "ts", "norm-scan"] if "err" in steps: result.update(self.estimate_norm_err()) if "counts" in steps: result.update(self.estimate_counts(datasets)) if "ul" in steps: result.update(self.estimate_norm_ul(datasets)) if "errp-errn" in steps: result.update(self.estimate_norm_errn_errp()) if "ts" in steps: result.update(self.estimate_norm_ts(datasets)) if "norm-scan" in steps: result.update(self.estimate_norm_scan()) return result # TODO : most of the following code is copied from FluxPointsEstimator, can it be restructured? def estimate_norm_errn_errp(self): """Estimate asymmetric errors for a flux point. Returns ------- result : dict Dict with asymmetric errors for the flux point norm. """ result = self.fit.confidence(parameter=self.model.norm, sigma=self.sigma) return {"norm_errp": result["errp"], "norm_errn": result["errn"]} def estimate_norm_err(self): """Estimate covariance errors for a flux point. Returns ------- result : dict Dict with symmetric error for the flux point norm. """ result = self.fit.covariance() norm_err = result.parameters.error(self.model.norm) return {"norm_err": norm_err} def estimate_counts(self, datasets): """Estimate counts for the flux point. Parameters ---------- datasets : `~gammapy.modeling.Datasets` the list of dataset object Returns ------- result : dict Dict with an array with one entry per dataset with counts for the flux point. """ counts = [] for dataset in datasets: mask = dataset.mask counts.append(dataset.counts.data[mask].sum()) return {"counts": np.array(counts, dtype=int).sum()} def estimate_norm_ul(self, datasets): """Estimate upper limit for a flux point. Parameters ---------- datasets : `~gammapy.modeling.Datasets` the list of dataset object Returns ------- result : dict Dict with upper limit for the flux point norm. """ norm = self.model.norm # TODO: the minuit backend has convergence problems when the fit statistic is not # of parabolic shape, which is the case, when there are zero counts in the # energy bin. For this case we change to the scipy backend. counts = self.estimate_counts(datasets)["counts"] if np.all(counts == 0): result = self.fit.confidence( parameter=norm, sigma=self.sigma_ul, backend="scipy", reoptimize=self.reoptimize, ) else: result = self.fit.confidence(parameter=norm, sigma=self.sigma_ul) return {"norm_ul": result["errp"] + norm.value} def estimate_norm_ts(self, datasets): """Estimate ts and sqrt(ts) for the flux point. Parameters ---------- datasets : `~gammapy.modeling.Datasets` the list of dataset object Returns ------- result : dict Dict with ts and sqrt(ts) for the flux point. """ stat = datasets.stat_sum() # store best fit amplitude, set amplitude of fit model to zero self.model.norm.value = 0 self.model.norm.frozen = True if self.reoptimize: _ = self.fit.optimize() stat_null = datasets.stat_sum() # compute sqrt TS ts = np.abs(stat_null - stat) sqrt_ts = np.sqrt(ts) return {"sqrt_ts": sqrt_ts, "ts": ts} def estimate_norm_scan(self): """Estimate fit statistic profile for the norm parameter. Returns ------- result : dict Keys "norm_scan", "stat_scan" """ result = self.fit.stat_profile(self.model.norm, values=self.norm_values, reoptimize=self.reoptimize) return {"norm_scan": result["values"], "stat_scan": result["stat"]} def estimate_norm(self): """Fit norm of the flux point. Returns ------- result : dict Dict with "norm" and "stat" for the flux point. """ # start optimization with norm=1 self.model.norm.value = 1.0 self.model.norm.frozen = False result = self.fit.optimize() if result.success: norm = self.model.norm.value else: norm = np.nan return { "norm": norm, "stat": result.total_stat, "success": result.success }
class ParameterEstimator(Estimator): """Model parameter estimator. Estimates a model parameter for a group of datasets. Compute best fit value, symmetric and asymmetric errors, delta TS for a given null value as well as parameter upper limit and fit statistic profile. Parameters ---------- sigma : int Sigma to use for asymmetric error computation. sigma_ul : int Sigma to use for upper limit computation. reoptimize : bool Re-optimize other free model parameters. Default is True. n_scan_values : int Number of values used to scan fit stat profile scan_n_err : float Range to scan in number of parameter error """ tag = "ParameterEstimator" def __init__( self, sigma=1, sigma_ul=2, reoptimize=True, n_scan_values=30, scan_n_err=3, ): self.sigma = sigma self.sigma_ul = sigma_ul self.reoptimize = reoptimize self.n_scan_values = n_scan_values self.scan_n_err = scan_n_err def __str__(self): s = f"{self.__class__.__name__}:\n" s += str(self.datasets) + "\n" return s def _check_datasets(self, datasets): """Check datasets geometry consistency and return Datasets object""" if not isinstance(datasets, Datasets): datasets = Datasets(datasets) return datasets def _freeze_parameters(self, parameter): """Freeze all other parameters""" for par in self.datasets.parameters: if par is not parameter: par.frozen = True def _compute_scan_values(self, value, value_error, par_min, par_max): """Define parameter value range to be scanned""" min_range = value - self.scan_n_err * value_error if not np.isnan(par_min): min_range = np.maximum(par_min, min_range) max_range = value + self.scan_n_err * value_error if not np.isnan(par_max): max_range = np.minimum(par_max, max_range) return np.linspace(min_range, max_range, self.n_scan_values) def _find_best_fit(self, parameter): """Find the best fit solution and store results.""" fit_result = self.fit.optimize() if fit_result.success: value = parameter.value else: value = np.nan result = { parameter.name: value, "stat": fit_result.total_stat, "success": fit_result.success, } self.fit_result = fit_result return result def _estimate_ts_for_null_value(self, parameter, null_value=1e-150): """Returns the fit statistic value for a given null value of the parameter.""" with self.datasets.parameters.restore_values: parameter.value = null_value parameter.frozen = True result = self.fit.optimize() if not result.success: log.warning( "Fit failed for parameter null value, returning NaN. Check input null value." ) return np.nan return result.total_stat def run( self, datasets, parameter, steps="all", null_value=1e-150, scan_values=None ): """Run the parameter estimator. Parameters ---------- datasets : `~gammapy.datasets.Datasets` The datasets used to estimate the model parameter parameter : `~gammapy.modeling.Parameter` the parameter to be estimated steps : list of str Which steps to execute. Available options are: * "err": estimate symmetric error from covariance * "ts": estimate delta TS with parameter null (reference) value * "errn-errp": estimate asymmetric errors. * "ul": estimate upper limits. * "scan": estimate fit statistic profiles. By default all steps are executed. null_value : float the null value to be used for delta TS estimation. Default is 1e-150 since 0 can be an issue for some parameters. scan_values : `numpy.ndarray` Array of parameter values to be used for the fit statistic profile. If set to None, scan values are automatically calculated. Default is None. Returns ------- result : dict Dict with the various parameter estimation values. """ self.datasets = self._check_datasets(datasets) self.fit = Fit(datasets) self.fit_result = None with self.datasets.parameters.restore_values: if not self.reoptimize: self._freeze_parameters(parameter) if steps == "all": steps = ["err", "ts", "errp-errn", "ul", "scan"] result = self._find_best_fit(parameter) TS1 = result["stat"] value_max = result[parameter.name] if "err" in steps: res = self.fit.covariance() value_err = res.parameters[parameter].error result.update({f"{parameter.name}_err": value_err}) if "errp-errn" in steps: res = self.fit.confidence(parameter=parameter, sigma=self.sigma) result.update( { f"{parameter.name}_errp": res["errp"], f"{parameter.name}_errn": res["errn"], } ) if "ul" in steps: res = self.fit.confidence(parameter=parameter, sigma=self.sigma_ul) result.update({f"{parameter.name}_ul": res["errp"] + value_max}) if "ts" in steps: TS0 = self._estimate_ts_for_null_value(parameter, null_value) res = TS0 - TS1 result.update( {"sqrt_ts": np.sqrt(res), "ts": res, "null_value": null_value} ) # TODO: should not need this self.fit.optimize() if "scan" in steps: if scan_values is None: scan_values = self._compute_scan_values( value_max, value_err, parameter.min, parameter.max ) res = self.fit.stat_profile( parameter, values=scan_values, reoptimize=self.reoptimize ) result.update( {f"{parameter.name}_scan": res["values"], "stat_scan": res["stat"]} ) return result
class ParameterEstimator(Estimator): """Model parameter estimator. Estimates a model parameter for a group of datasets. Compute best fit value, symmetric and delta TS for a given null value. Additionnally asymmetric errors as well as parameter upper limit and fit statistic profile can be estimated. Parameters ---------- n_sigma : int Sigma to use for asymmetric error computation. Default is 1. n_sigma_ul : int Sigma to use for upper limit computation. Default is 2. null_value : float Which null value to use for the parameter scan_n_sigma : int Range to scan in number of parameter error scan_min : float Minimum value to use for the stat scan scan_max : int Maximum value to use for the stat scan scan_n_values : int Number of values used to scan fit stat profile scan_values : `~numpy.ndarray` Values to use for the scan. reoptimize : bool Re-optimize other free model parameters. Default is True. selection_optional : list of str Which additional quantities to estimate. Available options are: * "all": all the optional steps are executed * "errn-errp": estimate asymmetric errors on parameter best fit value. * "ul": estimate upper limits. * "scan": estimate fit statistic profiles. Default is None so the optionnal steps are not executed. """ tag = "ParameterEstimator" _available_selection_optional = ["errn-errp", "ul", "scan"] def __init__( self, n_sigma=1, n_sigma_ul=2, null_value=1e-150, scan_n_sigma=3, scan_min=None, scan_max=None, scan_n_values=30, scan_values=None, reoptimize=True, selection_optional=None, ): self.n_sigma = n_sigma self.n_sigma_ul = n_sigma_ul self.null_value = null_value # scan parameters self.scan_n_sigma = scan_n_sigma self.scan_n_values = scan_n_values self.scan_values = scan_values self.scan_min = scan_min self.scan_max = scan_max self.reoptimize = reoptimize self.selection_optional = selection_optional self._fit = None def _setup_fit(self, datasets): # TODO: make fit stateless and configurable if self._fit is None or datasets is not self._fit.datasets: self._fit = Fit(datasets) def estimate_best_fit(self, datasets, parameter): """Estimate parameter assymetric errors Parameters ---------- datasets : `~gammapy.datasets.Datasets` Datasets parameter : `Parameter` For which parameter to get the value Returns ------- result : dict Dict with the various parameter estimation values. """ self._setup_fit(datasets) result_fit = self._fit.run() return { f"{parameter.name}": parameter.value, "stat": result_fit.total_stat, "success": result_fit.success, f"{parameter.name}_err": parameter.error * self.n_sigma, } def estimate_ts(self, datasets, parameter): """Estimate parameter ts Parameters ---------- datasets : `~gammapy.datasets.Datasets` Datasets parameter : `Parameter` For which parameter to get the value Returns ------- result : dict Dict with the various parameter estimation values. """ stat = datasets.stat_sum() with datasets.parameters.restore_status(): # compute ts value parameter.value = self.null_value if self.reoptimize: parameter.frozen = True _ = self._fit.optimize() ts = datasets.stat_sum() - stat return {"ts": ts} def estimate_errn_errp(self, datasets, parameter): """Estimate parameter assymetric errors Parameters ---------- datasets : `~gammapy.datasets.Datasets` Datasets parameter : `Parameter` For which parameter to get the value Returns ------- result : dict Dict with the various parameter estimation values. """ # TODO: make Fit stateless and configurable self._setup_fit(datasets) self._fit.optimize() res = self._fit.confidence(parameter=parameter, sigma=self.n_sigma, reoptimize=self.reoptimize) return { f"{parameter.name}_errp": res["errp"], f"{parameter.name}_errn": res["errn"], } def estimate_scan(self, datasets, parameter): """Estimate parameter stat scan. Parameters ---------- datasets : `~gammapy.datasets.Datasets` The datasets used to estimate the model parameter parameter : `Parameter` For which parameter to get the value Returns ------- result : dict Dict with the various parameter estimation values. """ self._setup_fit(datasets) self._fit.optimize() if self.scan_min and self.scan_max: bounds = (self.scan_min, self.scan_max) else: bounds = self.scan_n_sigma profile = self._fit.stat_profile( parameter=parameter, values=self.scan_values, bounds=bounds, nvalues=self.scan_n_values, reoptimize=self.reoptimize, ) return { f"{parameter.name}_scan": profile[f"{parameter.name}_scan"], "stat_scan": profile["stat_scan"], } def estimate_ul(self, datasets, parameter): """Estimate parameter ul. Parameters ---------- datasets : `~gammapy.datasets.Datasets` The datasets used to estimate the model parameter parameter : `Parameter` For which parameter to get the value Returns ------- result : dict Dict with the various parameter estimation values. """ self._setup_fit(datasets) self._fit.optimize() res = self._fit.confidence(parameter=parameter, sigma=self.n_sigma_ul, backend="scipy") return {f"{parameter.name}_ul": res["errp"] + parameter.value} def run(self, datasets, parameter): """Run the parameter estimator. Parameters ---------- datasets : `~gammapy.datasets.Datasets` The datasets used to estimate the model parameter parameter : `str` or `Parameter` For which parameter to run the estimator Returns ------- result : dict Dict with the various parameter estimation values. """ datasets = Datasets(datasets) parameter = datasets.parameters[parameter] with datasets.parameters.restore_status(): if not self.reoptimize: datasets.parameters.freeze_all() parameter.frozen = False result = self.estimate_best_fit(datasets, parameter) result.update(self.estimate_ts(datasets, parameter)) if "errn-errp" in self.selection_optional: result.update(self.estimate_errn_errp(datasets, parameter)) if "ul" in self.selection_optional: result.update(self.estimate_ul(datasets, parameter)) if "scan" in self.selection_optional: result.update(self.estimate_scan(datasets, parameter)) return result
class LightCurveEstimator: """Estimate flux points for a given list of datasets, each per time bin. Parameters ---------- datasets : list of `~gammapy.spectrum.SpectrumDataset` or `~gammapy.cube.MapDataset` Spectrum or Map datasets. source : str For which source in the model to compute the flux points. Default is '' norm_min : float Minimum value for the norm used for the likelihood profile evaluation. norm_max : float Maximum value for the norm used for the likelihood profile evaluation. norm_n_values : int Number of norm values used for the likelihood profile. norm_values : `numpy.ndarray` Array of norm values to be used for the likelihood profile. sigma : int Sigma to use for asymmetric error computation. sigma_ul : int Sigma to use for upper limit computation. reoptimize : bool reoptimize other parameters during likelihod scan """ def __init__( self, datasets, source="", norm_min=0.2, norm_max=5, norm_n_values=11, norm_values=None, sigma=1, sigma_ul=2, reoptimize=False, ): if not isinstance(datasets, Datasets): datasets = Datasets(datasets) self.datasets = datasets if not datasets.is_all_same_type and datasets.is_all_same_shape: raise ValueError( "Light Curve estimation requires a list of datasets" " of the same type and data shape." ) dataset = self.datasets.datasets[0] if isinstance(dataset, SpectrumDatasetOnOff): model = dataset.model else: model = dataset.model[source].spectral_model self.model = ScaleSpectralModel(model) self.model.norm.min = 0 self.model.norm.max = 1e5 if norm_values is None: norm_values = np.logspace( np.log10(norm_min), np.log10(norm_max), norm_n_values ) self.norm_values = norm_values self.sigma = sigma self.sigma_ul = sigma_ul self.reoptimize = reoptimize self.source = source self._set_scale_model() def _set_scale_model(self): # set the model on all datasets for dataset in self.datasets.datasets: if isinstance(dataset, SpectrumDatasetOnOff): dataset.model = self.model else: dataset.model[self.source].spectral_model = self.model @property def ref_model(self): return self.model.model def run(self, e_ref, e_min, e_max, steps="all"): """Run light curve extraction. Normalize integral and energy flux between emin and emax. Parameters ---------- e_ref : `~astropy.units.Quantity` reference energy of dnde flux normalization e_min : `~astropy.units.Quantity` minimum energy of integral and energy flux normalization interval e_max : `~astropy.units.Quantity` minimum energy of integral and energy flux normalization interval steps : list of str Which steps to execute. Available options are: * "err": estimate symmetric error. * "errn-errp": estimate asymmetric errors. * "ul": estimate upper limits. * "ts": estimate ts and sqrt(ts) values. * "norm-scan": estimate likelihood profiles. By default all steps are executed. Returns ------- lightcurve : `~gammapy.time.LightCurve` the Light Curve object """ self.e_ref = e_ref self.e_min = e_min self.e_max = e_max rows = [] for dataset in self.datasets.datasets: row = { "time_min": dataset.counts.meta["t_start"].mjd, "time_max": dataset.counts.meta["t_stop"].mjd, } row.update(self.estimate_time_bin_flux(dataset, steps)) rows.append(row) table = table_from_row_data(rows=rows, meta={"SED_TYPE": "likelihood"}) table = FluxPoints(table).to_sed_type("flux").table return LightCurve(table) def estimate_time_bin_flux(self, dataset, steps="all"): """Estimate flux point for a single energy group. Parameters ---------- steps : list of str Which steps to execute. Available options are: * "err": estimate symmetric error. * "errn-errp": estimate asymmetric errors. * "ul": estimate upper limits. * "ts": estimate ts and sqrt(ts) values. * "norm-scan": estimate likelihood profiles. By default all steps are executed. Returns ------- result : dict Dict with results for the flux point. """ self.fit = Fit(dataset) result = { "e_ref": self.e_ref, "e_min": self.e_min, "e_max": self.e_max, "ref_dnde": self.ref_model(self.e_ref), "ref_flux": self.ref_model.integral(self.e_min, self.e_max), "ref_eflux": self.ref_model.energy_flux(self.e_min, self.e_max), "ref_e2dnde": self.ref_model(self.e_ref) * self.e_ref ** 2, } result.update(self.estimate_norm()) if not result.pop("success"): log.warning( "Fit failed for time bin between {t_min} and {t_max}," " setting NaN.".format( t_min=dataset.counts.meta["t_start"], t_max=dataset.counts.meta["t_stop"], ) ) if steps == "all": steps = ["err", "counts", "errp-errn", "ul", "ts", "norm-scan"] if "err" in steps: result.update(self.estimate_norm_err()) if "counts" in steps: result.update(self.estimate_counts(dataset)) if "errp-errn" in steps: result.update(self.estimate_norm_errn_errp()) if "ul" in steps: result.update(self.estimate_norm_ul(dataset)) if "ts" in steps: result.update(self.estimate_norm_ts()) if "norm-scan" in steps: result.update(self.estimate_norm_scan()) return result # TODO : most of the following code is copied from FluxPointsEstimator, can it be restructured? def estimate_norm_errn_errp(self): """Estimate asymmetric errors for a flux point. Returns ------- result : dict Dict with asymmetric errors for the flux point norm. """ result = self.fit.confidence(parameter=self.model.norm, sigma=self.sigma) return {"norm_errp": result["errp"], "norm_errn": result["errn"]} def estimate_norm_err(self): """Estimate covariance errors for a flux point. Returns ------- result : dict Dict with symmetric error for the flux point norm. """ result = self.fit.covariance() norm_err = result.parameters.error(self.model.norm) return {"norm_err": norm_err} def estimate_counts(self, dataset): """Estimate counts for the flux point. Parameters ---------- dataset : `~gammapy.modeling.Dataset` the dataset object Returns ------- result : dict Dict with an array with one entry per dataset with counts for the flux point. """ # TODO : use e_min and e_max interval for counts calculation # TODO : add off counts and excess? for DatasetOnOff # TODO : this may require a loop once we support Datasets per time bin mask = dataset.mask if dataset.mask_safe is not None: mask &= dataset.mask_safe counts = dataset.counts.data[mask].sum() return {"counts": counts} def estimate_norm_ul(self, dataset): """Estimate upper limit for a flux point. Returns ------- result : dict Dict with upper limit for the flux point norm. """ norm = self.model.norm # TODO: the minuit backend has convergence problems when the likelihood is not # of parabolic shape, which is the case, when there are zero counts in the # bin. For this case we change to the scipy backend. counts = self.estimate_counts(dataset)["counts"] if np.all(counts == 0): result = self.fit.confidence( parameter=norm, sigma=self.sigma_ul, backend="scipy", reoptimize=self.reoptimize, ) else: result = self.fit.confidence(parameter=norm, sigma=self.sigma_ul) return {"norm_ul": result["errp"] + norm.value} def estimate_norm_ts(self): """Estimate ts and sqrt(ts) for the flux point. Returns ------- result : dict Dict with ts and sqrt(ts) for the flux point. """ loglike = self.datasets.likelihood() # store best fit amplitude, set amplitude of fit model to zero self.model.norm.value = 0 self.model.norm.frozen = True if self.reoptimize: _ = self.fit.optimize() loglike_null = self.datasets.likelihood() # compute sqrt TS ts = np.abs(loglike_null - loglike) sqrt_ts = np.sqrt(ts) return {"sqrt_ts": sqrt_ts, "ts": ts} def estimate_norm_scan(self): """Estimate likelihood profile for the norm parameter. Returns ------- result : dict Dict with norm_scan and dloglike_scan for the flux point. """ result = self.fit.likelihood_profile( self.model.norm, values=self.norm_values, reoptimize=self.reoptimize ) dloglike_scan = result["likelihood"] return {"norm_scan": result["values"], "dloglike_scan": dloglike_scan} def estimate_norm(self): """Fit norm of the flux point. Returns ------- result : dict Dict with "norm" and "loglike" for the flux point. """ # start optimization with norm=1 self.model.norm.value = 1.0 self.model.norm.frozen = False result = self.fit.optimize() if result.success: norm = self.model.norm.value else: norm = np.nan return {"norm": norm, "loglike": result.total_stat, "success": result.success}
class FluxPointsEstimator: """Flux points estimator. Estimates flux points for a given list of spectral datasets, energies and spectral model. To estimate the flux point the amplitude of the reference spectral model is fitted within the energy range defined by the energy group. This is done for each group independently. The amplitude is re-normalized using the "norm" parameter, which specifies the deviation of the flux from the reference model in this energy group. See https://gamma-astro-data-formats.readthedocs.io/en/latest/spectra/binned_likelihoods/index.html for details. The method is also described in the Fermi-LAT catalog paper https://ui.adsabs.harvard.edu/#abs/2015ApJS..218...23A or the HESS Galactic Plane Survey paper https://ui.adsabs.harvard.edu/#abs/2018A%26A...612A...1H Parameters ---------- datasets : list of `~gammapy.spectrum.SpectrumDataset` Spectrum datasets. e_edges : `~astropy.units.Quantity` Energy edges of the flux point bins. source : str For which source in the model to compute the flux points. norm_min : float Minimum value for the norm used for the likelihood profile evaluation. norm_max : float Maximum value for the norm used for the likelihood profile evaluation. norm_n_values : int Number of norm values used for the likelihood profile. norm_values : `numpy.ndarray` Array of norm values to be used for the likelihood profile. sigma : int Sigma to use for asymmetric error computation. sigma_ul : int Sigma to use for upper limit computation. reoptimize : bool Re-optimize other free model parameters. """ def __init__( self, datasets, e_edges, source="", norm_min=0.2, norm_max=5, norm_n_values=11, norm_values=None, sigma=1, sigma_ul=2, reoptimize=False, ): # make a copy to not modify the input datasets if not isinstance(datasets, Datasets): datasets = Datasets(datasets) if not datasets.is_all_same_type and datasets.is_all_same_shape: raise ValueError( "Flux point estimation requires a list of datasets" " of the same type and data shape.") self.datasets = datasets.copy() self.e_edges = e_edges dataset = self.datasets.datasets[0] if isinstance(dataset, SpectrumDatasetOnOff): model = dataset.model else: model = dataset.model[source].spectral_model self.model = ScaleSpectralModel(model) self.model.norm.min = 0 self.model.norm.max = 1e3 if norm_values is None: norm_values = np.logspace(np.log10(norm_min), np.log10(norm_max), norm_n_values) self.norm_values = norm_values self.sigma = sigma self.sigma_ul = sigma_ul self.reoptimize = reoptimize self.source = source self.fit = Fit(self.datasets) self._set_scale_model() def _freeze_parameters(self): # freeze other parameters for par in self.datasets.parameters: if par is not self.model.norm: par.frozen = True def _freeze_empty_background(self): from gammapy.cube import MapDataset counts_all = self.estimate_counts()["counts"] for counts, dataset in zip(counts_all, self.datasets.datasets): if isinstance(dataset, MapDataset) and counts == 0: if dataset.background_model is not None: dataset.background_model.parameters.freeze_all() def _set_scale_model(self): # set the model on all datasets for dataset in self.datasets.datasets: if isinstance(dataset, SpectrumDatasetOnOff): dataset.model = self.model else: dataset.model[self.source].spectral_model = self.model @property def ref_model(self): return self.model.model @property def e_groups(self): """Energy grouping table `~astropy.table.Table`""" dataset = self.datasets.datasets[0] if isinstance(dataset, SpectrumDatasetOnOff): energy_axis = dataset.counts.energy else: energy_axis = dataset.counts.geom.get_axis_by_name("energy") return energy_axis.group_table(self.e_edges) def __str__(self): s = f"{self.__class__.__name__}:\n" s += str(self.datasets) + "\n" s += str(self.e_edges) + "\n" s += str(self.model) + "\n" return s def run(self, steps="all"): """Run the flux point estimator for all energy groups. Returns ------- flux_points : `FluxPoints` Estimated flux points. steps : list of str Which steps to execute. See `estimate_flux_point` for details and available options. """ rows = [] for e_group in self.e_groups: if e_group["bin_type"].strip() != "normal": log.debug( "Skipping under-/ overflow bin in flux point estimation.") continue row = self.estimate_flux_point(e_group, steps=steps) rows.append(row) table = table_from_row_data(rows=rows, meta={"SED_TYPE": "likelihood"}) return FluxPoints(table).to_sed_type("dnde") def _energy_mask(self, e_group): energy_mask = np.zeros(self.datasets.datasets[0].data_shape) energy_mask[e_group["idx_min"]:e_group["idx_max"] + 1] = 1 return energy_mask.astype(bool) def estimate_flux_point(self, e_group, steps="all"): """Estimate flux point for a single energy group. Parameters ---------- e_group : `~astropy.table.Row` Energy group to compute the flux point for. steps : list of str Which steps to execute. Available options are: * "err": estimate symmetric error. * "errn-errp": estimate asymmetric errors. * "ul": estimate upper limits. * "ts": estimate ts and sqrt(ts) values. * "norm-scan": estimate likelihood profiles. By default all steps are executed. Returns ------- result : dict Dict with results for the flux point. """ e_min, e_max = e_group["energy_min"], e_group["energy_max"] # Put at log center of the bin e_ref = np.sqrt(e_min * e_max) result = { "e_ref": e_ref, "e_min": e_min, "e_max": e_max, "ref_dnde": self.ref_model(e_ref), "ref_flux": self.ref_model.integral(e_min, e_max), "ref_eflux": self.ref_model.energy_flux(e_min, e_max), "ref_e2dnde": self.ref_model(e_ref) * e_ref**2, } contribute_to_likelihood = False for dataset in self.datasets.datasets: dataset.mask_fit = self._energy_mask(e_group) mask = dataset.mask_fit if dataset.mask_safe is not None: mask &= dataset.mask_safe contribute_to_likelihood |= mask.any() if not contribute_to_likelihood: raise ValueError( "No dataset contributes to the likelihood between" " {e_min:.3f} and {e_max:.3f}. Please adapt the " "flux point energy edges or check the dataset masks.".format( e_min=e_min, e_max=e_max)) with self.datasets.parameters.restore_values: self._freeze_empty_background() if not self.reoptimize: self._freeze_parameters() result.update(self.estimate_norm()) if not result.pop("success"): log.warning( "Fit failed for flux point between {e_min:.3f} and {e_max:.3f}," " setting NaN.".format(e_min=e_min, e_max=e_max)) if steps == "all": steps = ["err", "counts", "errp-errn", "ul", "ts", "norm-scan"] if "err" in steps: result.update(self.estimate_norm_err()) if "counts" in steps: result.update(self.estimate_counts()) if "errp-errn" in steps: result.update(self.estimate_norm_errn_errp()) if "ul" in steps: result.update(self.estimate_norm_ul()) if "ts" in steps: result.update(self.estimate_norm_ts()) if "norm-scan" in steps: result.update(self.estimate_norm_scan()) return result def estimate_norm_errn_errp(self): """Estimate asymmetric errors for a flux point. Returns ------- result : dict Dict with asymmetric errors for the flux point norm. """ result = self.fit.confidence(parameter=self.model.norm, sigma=self.sigma) return {"norm_errp": result["errp"], "norm_errn": result["errn"]} def estimate_norm_err(self): """Estimate covariance errors for a flux point. Returns ------- result : dict Dict with symmetric error for the flux point norm. """ result = self.fit.covariance() norm_err = result.parameters.error(self.model.norm) return {"norm_err": norm_err} def estimate_counts(self): """Estimate counts for the flux point. Returns ------- result : dict Dict with an array with one entry per dataset with counts for the flux point. """ counts = [] for dataset in self.datasets.datasets: mask = dataset.mask_fit if dataset.mask_safe is not None: mask &= dataset.mask_safe counts.append(dataset.counts.data[mask].sum()) return {"counts": np.array(counts, dtype=int)} def estimate_norm_ul(self): """Estimate upper limit for a flux point. Returns ------- result : dict Dict with upper limit for the flux point norm. """ norm = self.model.norm # TODO: the minuit backend has convergence problems when the likelihood is not # of parabolic shape, which is the case, when there are zero counts in the # energy bin. For this case we change to the scipy backend. counts = self.estimate_counts()["counts"] if np.all(counts == 0): result = self.fit.confidence( parameter=norm, sigma=self.sigma_ul, backend="scipy", reoptimize=self.reoptimize, ) else: result = self.fit.confidence(parameter=norm, sigma=self.sigma_ul) return {"norm_ul": result["errp"] + norm.value} def estimate_norm_ts(self): """Estimate ts and sqrt(ts) for the flux point. Returns ------- result : dict Dict with ts and sqrt(ts) for the flux point. """ loglike = self.datasets.likelihood() # store best fit amplitude, set amplitude of fit model to zero self.model.norm.value = 0 self.model.norm.frozen = True if self.reoptimize: _ = self.fit.optimize() loglike_null = self.datasets.likelihood() # compute sqrt TS ts = np.abs(loglike_null - loglike) sqrt_ts = np.sqrt(ts) return {"sqrt_ts": sqrt_ts, "ts": ts} def estimate_norm_scan(self): """Estimate likelihood profile for the norm parameter. Returns ------- result : dict Dict with norm_scan and dloglike_scan for the flux point. """ result = self.fit.likelihood_profile(self.model.norm, values=self.norm_values, reoptimize=self.reoptimize) dloglike_scan = result["likelihood"] return {"norm_scan": result["values"], "dloglike_scan": dloglike_scan} def estimate_norm(self): """Fit norm of the flux point. Returns ------- result : dict Dict with "norm" and "loglike" for the flux point. """ # start optimization with norm=1 self.model.norm.value = 1.0 self.model.norm.frozen = False result = self.fit.optimize() if result.success: norm = self.model.norm.value else: norm = np.nan return { "norm": norm, "loglike": result.total_stat, "success": result.success }