def test_is_ul(tmp_path): catalog_4fgl = SourceCatalog4FGL( "$GAMMAPY_DATA/catalogs/fermi/gll_psc_v20.fit.gz") src = catalog_4fgl["FGES J1553.8-5325"] fp = src.flux_points is_ul = fp._data["is_ul"].data.squeeze() assert_allclose(fp.is_ul.data.squeeze(), is_ul) table = fp.to_table() assert_allclose(table["is_ul"].data.data, is_ul) fp.sqrt_ts_threshold_ul = 100 assert_allclose(fp.is_ul.data.squeeze(), np.ones(is_ul.shape, dtype=bool)) table = fp.to_table() assert_allclose(table["is_ul"].data.data, np.ones(is_ul.shape, dtype=bool)) table.write(tmp_path / "test_modif_ul_threshold.fits") table_read = Table.read(tmp_path / "test_modif_ul_threshold.fits") assert_allclose(table_read["is_ul"].data.data, np.ones(is_ul.shape, dtype=bool)) fp_read = FluxPoints.from_table(table_read) assert_allclose(fp_read.is_ul.data.squeeze(), np.ones(is_ul.shape, dtype=bool)) assert_allclose(fp_read.to_table()["is_ul"], fp_read.is_ul.data.squeeze()) fp.is_ul = is_ul assert_allclose(fp.is_ul.data.squeeze(), is_ul) table = fp.to_table() assert_allclose(table["is_ul"].data.data, is_ul)
def from_dict(cls, data, **kwargs): """Create flux point dataset from dict. Parameters ---------- data : dict Dict containing data to create dataset from. Returns ------- dataset : `FluxPointsDataset` Flux point datasets. """ from gammapy.estimators import FluxPoints filename = make_path(data["filename"]) table = Table.read(filename) mask_fit = table["mask_fit"].data.astype("bool") mask_safe = table["mask_safe"].data.astype("bool") table.remove_columns(["mask_fit", "mask_safe"]) return cls( name=data["name"], data=FluxPoints.from_table(table), mask_fit=mask_fit, mask_safe=mask_safe, )
def flux_points(self): """Flux points (`~gammapy.estimators.FluxPoints`).""" return FluxPoints.from_table( table=self.flux_points_table, reference_model=self.sky_model(), format="gadf-sed", )
def read(cls, filename, name=None, format="gadf-sed"): """Read pre-computed flux points and create a dataset Parameters ---------- filename : str Filename to read from. name : str Name of the new dataset. format : {"gadf-sed"} Format of the dataset file. Returns ------- dataset : `FluxPointsDataset` FluxPointsDataset """ from gammapy.estimators import FluxPoints filename = make_path(filename) table = Table.read(filename) mask_fit = None mask_safe = None if "mask_safe" in table.colnames: mask_safe = table["mask_safe"].data.astype("bool") if "mask_fit" in table.colnames: mask_fit = table["mask_fit"].data.astype("bool") return cls( name=make_name(name), data=FluxPoints.from_table(table, format=format), mask_fit=mask_fit, mask_safe=mask_safe )
def test_flux_point_dataset_serialization(tmp_path): path = "$GAMMAPY_DATA/tests/spectrum/flux_points/diff_flux_points.fits" table = Table.read(make_path(path)) table["e_ref"] = table["e_ref"].quantity.to("TeV") data = FluxPoints.from_table(table, format="gadf-sed") spectral_model = PowerLawSpectralModel(index=2.3, amplitude="2e-13 cm-2 s-1 TeV-1", reference="1 TeV") model = SkyModel(spectral_model=spectral_model, name="test_model") dataset = FluxPointsDataset(model, data, name="test_dataset") Datasets([dataset]).write( filename=tmp_path / "tmp_datasets.yaml", filename_models=tmp_path / "tmp_models.yaml", ) datasets = Datasets.read( filename=tmp_path / "tmp_datasets.yaml", filename_models=tmp_path / "tmp_models.yaml", ) new_dataset = datasets[0] assert_allclose(new_dataset.data.dnde, dataset.data.dnde, 1e-4) if dataset.mask_fit is None: assert np.all(new_dataset.mask_fit == dataset.mask_safe) assert np.all(new_dataset.mask_safe == dataset.mask_safe) assert new_dataset.name == "test_dataset"
def flux_points(self): """Flux points (`~gammapy.estimators.FluxPoints`).""" reference_model = SkyModel(spectral_model=self.spectral_model(), name=self.name) return FluxPoints.from_table( self.flux_points_table, reference_model=reference_model, )
def test_plot_fp_no_ul(): path = make_path("$GAMMAPY_DATA/tests/spectrum/flux_points/diff_flux_points.fits") table = Table.read(path) table.remove_column('dnde_ul') fp = FluxPoints.from_table(table, sed_type='dnde') with mpl_plot_check(): fp.plot()
def test_flux_points_plot_no_error_bar(): table = Table() pwl = PowerLawSpectralModel() e_ref = np.geomspace(1, 100, 7) * u.TeV table["e_ref"] = e_ref table["dnde"] = pwl(e_ref) table.meta["SED_TYPE"] = "dnde" flux_points = FluxPoints.from_table(table) with mpl_plot_check(): _ = flux_points.plot(sed_type="dnde")
def test_lightcurve_estimator_spectrum_datasets(): # Doing a LC on one hour bin datasets = get_spectrum_datasets() time_intervals = [ Time(["2010-01-01T00:00:00", "2010-01-01T01:00:00"]), Time(["2010-01-01T01:00:00", "2010-01-01T02:00:00"]), ] estimator = LightCurveEstimator( energy_edges=[1, 30] * u.TeV, norm_n_values=3, time_intervals=time_intervals, selection_optional="all", ) lightcurve = estimator.run(datasets) table = lightcurve.to_table(format="lightcurve") assert_allclose(table["time_min"], [55197.0, 55197.041667]) assert_allclose(table["time_max"], [55197.041667, 55197.083333]) assert_allclose(table["e_ref"], [[5.623413], [5.623413]]) assert_allclose(table["e_min"], [[1], [1]]) assert_allclose(table["e_max"], [[31.622777], [31.622777]]) assert_allclose( table["ref_dnde"], [[3.162278e-14], [3.162278e-14]], rtol=1e-5 ) assert_allclose( table["ref_flux"], [[9.683772e-13], [9.683772e-13]], rtol=1e-5 ) assert_allclose( table["ref_eflux"], [[3.453878e-12], [3.453878e-12]], rtol=1e-5 ) assert_allclose(table["stat"], [[16.824042], [17.391981]], rtol=1e-5) assert_allclose(table["norm"], [[0.911963], [0.9069318]], rtol=1e-2) assert_allclose(table["norm_err"], [[0.057769], [0.057835]], rtol=1e-2) assert_allclose(table["counts"], [[[791, np.nan]], [[np.nan, 784]]]) assert_allclose(table["norm_errp"], [[0.058398], [0.058416]], rtol=1e-2) assert_allclose(table["norm_errn"], [[0.057144], [0.057259]], rtol=1e-2) assert_allclose(table["norm_ul"], [[1.029989], [1.025061]], rtol=1e-2) assert_allclose(table["sqrt_ts"], [[19.384781], [19.161769]], rtol=1e-2) assert_allclose(table["ts"], [[375.769735], [367.173374]], rtol=1e-2) assert_allclose(table[0]["norm_scan"], [[0.2, 1.0, 5.0]]) assert_allclose( table[0]["stat_scan"], [[224.058304, 19.074405, 2063.75636]], rtol=1e-5, ) # TODO: fix reference model I/O fp = FluxPoints.from_table( table=table, format="lightcurve", reference_model=PowerLawSpectralModel() ) assert fp.norm.geom.axes.names == ["energy", "time"] assert fp.counts.geom.axes.names == ["dataset", "energy", "time"] assert fp.stat_scan.geom.axes.names == ["norm", "energy", "time"]
def dataset(): path = "$GAMMAPY_DATA/tests/spectrum/flux_points/diff_flux_points.fits" table = Table.read(make_path(path)) table["e_ref"] = table["e_ref"].quantity.to("TeV") data = FluxPoints.from_table(table, format="gadf-sed") model = SkyModel(spectral_model=PowerLawSpectralModel( index=2.3, amplitude="2e-13 cm-2 s-1 TeV-1", reference="1 TeV")) obs_table = Table() obs_table["TELESCOP"] = ["CTA"] obs_table["OBS_ID"] = ["0001"] obs_table["INSTRUME"] = ["South_Z20_50h"] dataset = FluxPointsDataset(model, data, meta_table=obs_table) return dataset
def read_ref_lightcurve(): filename = "reference/Flux_LC_ChandraNight_700GeV.fits" table = Table.read(filename) table["e_min"] = [[700]] * u.GeV table["e_max"] = [[1e5]] * u.TeV table["time_min"].unit = u.day table["time_max"].unit = u.day # fix overlapping time intervals m = np.where((table["time_min"][1:] - table["time_max"][:-1]) >= 0 * u.s) return FluxPoints.from_table( table=table[m], format="lightcurve", sed_type="flux", )
def lc_2d(): meta = dict(TIMESYS="utc", SED_TYPE="flux") table = Table( meta=meta, data=[ Column(Time(["2010-01-01", "2010-01-03"]).mjd, "time_min"), Column(Time(["2010-01-03", "2010-01-10"]).mjd, "time_max"), Column([[1.0, 2.0], [1.0, 2.0]], "e_min", unit="TeV"), Column([[2.0, 4.0], [2.0, 4.0]], "e_max", unit="TeV"), Column([[1e-11, 1e-12], [3e-11, 3e-12]], "flux", unit="cm-2 s-1"), Column([[0.1e-11, 1e-13], [0.3e-11, 3e-13]], "flux_err", unit="cm-2 s-1"), Column([[np.nan, np.nan], [3.6e-11, 3.6e-12]], "flux_ul", unit="cm-2 s-1"), Column([[False, False], [True, True]], "is_ul"), ], ) return FluxPoints.from_table(table=table, format="lightcurve")
def test_flux_points_single_bin_dnde(): path = make_path( "$GAMMAPY_DATA/tests/spectrum/flux_points/diff_flux_points.fits") table = Table.read(path) table_single_bin = table[1:2] fp = FluxPoints.from_table(table_single_bin, sed_type="dnde") with pytest.raises(ValueError): _ = fp.flux_ref with mpl_plot_check(): fp.plot(sed_type="e2dnde") with pytest.raises(ValueError): fp.to_table(sed_type="flux") table = fp.to_table(sed_type="dnde") assert_allclose(table["e_ref"], 153.992 * u.MeV, rtol=0.001) assert "e_min" not in table.colnames assert "e_max" not in table.colnames
def test_dnde_from_flux(): """Tests y-value normalization adjustment method. """ table = Table() table["e_min"] = np.array([10, 20, 30, 40]) table["e_max"] = np.array([20, 30, 40, 50]) table["flux"] = np.array([42, 52, 62, 72]) # 'True' integral flux in this test bin # Get values model = XSqrTestModel() table["e_ref"] = FluxPoints._energy_ref_lafferty(model, table["e_min"], table["e_max"]) dnde = FluxPoints.from_table(table, reference_model=model) # Set up test case comparison dnde_model = model(table["e_ref"]) # Test comparison result desired = model.integral(table["e_min"], table["e_max"]) # Test output result actual = table["flux"] * (dnde_model / dnde) # Compare assert_allclose(actual, desired, rtol=1e-6)
def test_compute_flux_points_dnde_exp(method): """ Tests against analytical result or result from a powerlaw. """ model = ExpTestModel() energy_min = [1.0, 10.0] * u.TeV energy_max = [10.0, 100.0] * u.TeV table = Table() table.meta["SED_TYPE"] = "flux" table["e_min"] = energy_min table["e_max"] = energy_max flux = model.integral(energy_min, energy_max) table["flux"] = flux if method == "log_center": energy_ref = np.sqrt(energy_min * energy_max) elif method == "table": energy_ref = [2.0, 20.0] * u.TeV elif method == "lafferty": energy_ref = FluxPoints._energy_ref_lafferty(model, energy_min, energy_max) table["e_ref"] = energy_ref result = FluxPoints.from_table(table, reference_model=model) # Test energy actual = result.energy_ref assert_quantity_allclose(actual, energy_ref, rtol=1e-8) # Test flux actual = result.dnde desired = model(energy_ref) assert_quantity_allclose(actual, desired, rtol=1e-8)
def test_lightcurve_estimator_spectrum_datasets_2_energy_bins(): # Doing a LC on one hour bin datasets = get_spectrum_datasets() time_intervals = [ Time(["2010-01-01T00:00:00", "2010-01-01T01:00:00"]), Time(["2010-01-01T01:00:00", "2010-01-01T02:00:00"]), ] estimator = LightCurveEstimator( energy_edges=[1, 5, 30] * u.TeV, norm_n_values=3, time_intervals=time_intervals, selection_optional="all", ) lightcurve = estimator.run(datasets) table = lightcurve.to_table(format="lightcurve") assert_allclose(table["time_min"], [55197.0, 55197.041667]) assert_allclose(table["time_max"], [55197.041667, 55197.083333]) assert_allclose(table["e_ref"], [[2.238721, 12.589254], [2.238721, 12.589254]]) assert_allclose(table["e_min"], [[1, 5.011872], [1, 5.011872]]) assert_allclose(table["e_max"], [[5.011872, 31.622777], [5.011872, 31.622777]]) assert_allclose( table["ref_dnde"], [[1.995262e-13, 6.309573e-15], [1.995262e-13, 6.309573e-15]], rtol=1e-5, ) assert_allclose( table["stat"], [[8.234951, 8.30321], [2.037205, 15.300507]], rtol=1e-5, ) assert_allclose( table["norm"], [[0.894723, 0.967419], [0.914283, 0.882351]], rtol=1e-2, ) assert_allclose( table["norm_err"], [[0.065905, 0.121288], [0.06601, 0.119457]], rtol=1e-2, ) assert_allclose( table["counts"], [[[669.0, np.nan], [122.0, np.nan]], [[np.nan, 667.0], [np.nan, 117.0]] ], ) assert_allclose( table["norm_errp"], [[0.06664, 0.124741], [0.066815, 0.122832]], rtol=1e-2, ) assert_allclose( table["norm_errn"], [[0.065176, 0.117904], [0.065212, 0.116169]], rtol=1e-2, ) assert_allclose( table["norm_ul"], [[1.029476, 1.224117], [1.049283, 1.134874]], rtol=1e-2, ) assert_allclose( table["sqrt_ts"], [[16.233236, 10.608376], [16.609784, 9.557339]], rtol=1e-2, ) assert_allclose(table[0]["norm_scan"], [[0.2, 1.0, 5.0], [0.2, 1.0, 5.0]]) assert_allclose( table[0]["stat_scan"], [[153.880281, 10.701492, 1649.609684], [70.178023, 8.372913, 414.146676]], rtol=1e-5, ) # those quantities are currently not part of the table so we test separately npred = lightcurve.npred.data.squeeze() assert_allclose( npred, [[[669.36, np.nan], [121.66, np.nan]], [[np.nan, 664.41], [np.nan, 115.09]]], rtol=1e-3, ) npred_excess_err = lightcurve.npred_excess_err.data.squeeze() assert_allclose( npred_excess_err, [[[26.80, np.nan], [11.31, np.nan]], [[np.nan, 26.85], [np.nan, 11.14]] ], rtol=1e-3, ) npred_excess_errp = lightcurve.npred_excess_errp.data.squeeze() assert_allclose( npred_excess_errp, [[[27.11, np.nan], [11.63, np.nan]], [[np.nan, 27.15], [np.nan, 11.46]] ], rtol=1e-3, ) npred_excess_errn = lightcurve.npred_excess_errn.data.squeeze() assert_allclose( npred_excess_errn, [[[26.50, np.nan], [11.00, np.nan]], [[np.nan, 26.54], [np.nan, 10.84]] ], rtol=1e-3, ) npred_excess_ul = lightcurve.npred_excess_ul.data.squeeze() assert_allclose( npred_excess_ul, [[[418.68, np.nan], [114.19, np.nan]], [[np.nan, 426.74], [np.nan, 105.86]]], rtol=1e-3, ) fp = FluxPoints.from_table(table=table, format="lightcurve", reference_model=PowerLawSpectralModel()) assert fp.norm.geom.axes.names == ["energy", "time"] assert fp.counts.geom.axes.names == ["dataset", "energy", "time"] assert fp.stat_scan.geom.axes.names == ["norm", "energy", "time"]