Exemplo n.º 1
0
def test_flux_map_failed_properties(wcs_flux_map, reference_model):
    fluxmap = FluxMaps(wcs_flux_map, reference_model)
    fluxmap.filter_success_nan = False

    assert_allclose(fluxmap.success.data[:, 0, 1], [False, True])
    assert_allclose(fluxmap.flux.data[:, 0, 1], [9.0e-12, 9e-13])
    assert not fluxmap.filter_success_nan
Exemplo n.º 2
0
def test_flux_map_str(wcs_flux_map, reference_model):
    fluxmap = FluxMaps(wcs_flux_map, reference_model)

    fm_str = fluxmap.__str__()

    assert "WcsGeom" in fm_str
    assert "errn" in fm_str
    assert "sqrt_ts" in fm_str
    assert "PowerLawSpectralModel" in fm_str
Exemplo n.º 3
0
def test_flux_map_read_write_missing_reference_model(tmp_path, wcs_flux_map,
                                                     reference_model):
    fluxmap = FluxMaps(wcs_flux_map, reference_model)
    fluxmap.write(tmp_path / "tmp.fits")

    hdulist = fits.open(tmp_path / "tmp.fits")
    hdulist[0].header["MODEL"] = "non_existent"

    with pytest.raises(FileNotFoundError):
        new_fluxmap = FluxMaps.from_hdulist(hdulist)
Exemplo n.º 4
0
def test_flux_map_read_write_no_reference_model(tmp_path, wcs_flux_map, caplog):
    fluxmap = FluxMaps(wcs_flux_map)

    fluxmap.write(tmp_path / "tmp.fits")
    new_fluxmap = FluxMaps.read(tmp_path / "tmp.fits")

    assert new_fluxmap.reference_model.spectral_model.tag[0] == "PowerLawSpectralModel"
    assert "WARNING" in [_.levelname for _ in caplog.records]
    assert f"No reference model set for FluxMaps." in [
        _.message for _ in caplog.records
    ]
Exemplo n.º 5
0
def test_flux_map_str(wcs_flux_map, reference_model):
    fluxmap = FluxMaps(wcs_flux_map, reference_model)

    fm_str = fluxmap.__str__()

    assert "WcsGeom" in fm_str
    assert "errn" in fm_str
    assert "sqrt_ts" in fm_str
    assert "pl" in fm_str
    assert "n_sigma" in fm_str
    assert "n_sigma_ul" in fm_str
    assert "ts_threshold" in fm_str
Exemplo n.º 6
0
def test_flux_map_read_write_gti(tmp_path, partial_wcs_flux_map, reference_model):
    start = u.Quantity([1, 2], "min")
    stop = u.Quantity([1.5, 2.5], "min")
    gti = GTI.create(start, stop)

    fluxmap = FluxMaps(partial_wcs_flux_map, reference_model, gti=gti)

    fluxmap.write(tmp_path / "tmp.fits", sed_type="dnde")
    new_fluxmap = FluxMaps.read(tmp_path / "tmp.fits")

    assert len(new_fluxmap.gti.table) == 2
    assert_allclose(gti.table["START"], start.to_value("s"))
Exemplo n.º 7
0
def test_get_flux_point_missing_map(wcs_flux_map, reference_model):
    other_data = wcs_flux_map.copy()
    other_data.pop("norm_errn")
    other_data.pop("norm_errp")
    fluxmap = FluxMaps(other_data, reference_model)

    coord = SkyCoord(0., 0., unit="deg", frame="galactic")
    fp = fluxmap.get_flux_points(coord)

    assert_allclose(fp.table["e_min"], [0.1, 1.0])
    assert_allclose(fp.table["norm"], [1, 1])
    assert_allclose(fp.table["norm_err"], [0.1, 0.1])
    assert_allclose(fp.table["norm_ul"], [2, 2])
    assert "norm_errn" not in fp.table.columns
Exemplo n.º 8
0
def test_flux_map_check_node_types(wcs_flux_map, region_map_flux_estimate,
                                   reference_model):
    ref_map = FluxMaps(wcs_flux_map, reference_model)
    ref_region_map = FluxMaps(region_map_flux_estimate, reference_model)

    assert ref_map.dnde.geom.axes[0].node_type == 'center'
    assert ref_map.e2dnde.geom.axes[0].node_type == 'center'
    assert ref_map.flux.geom.axes[0].node_type == 'edges'
    assert ref_map.eflux.geom.axes[0].node_type == 'edges'

    assert ref_region_map.dnde_err.geom.axes[0].node_type == 'center'
    assert ref_region_map.e2dnde_ul.geom.axes[0].node_type == 'center'
    assert ref_region_map.flux_err.geom.axes[0].node_type == 'edges'
    assert ref_region_map.eflux_ul.geom.axes[0].node_type == 'edges'
Exemplo n.º 9
0
def test_get_flux_point_missing_map(wcs_flux_map, reference_model):
    other_data = wcs_flux_map.copy()
    other_data.pop("norm_errn")
    other_data.pop("norm_errp")
    fluxmap = FluxMaps(other_data, reference_model)

    coord = SkyCoord(0.0, 0.0, unit="deg", frame="galactic")
    table = fluxmap.get_flux_points(coord).to_table()
    assert_allclose(table["e_min"], [0.1, 1.0])
    assert_allclose(table["norm"], [1, 1])
    assert_allclose(table["norm_err"], [0.1, 0.1])
    assert_allclose(table["norm_ul"], [2, 2])
    assert "norm_errn" not in table.columns
    assert table["success"].data.dtype == np.dtype(np.bool)
Exemplo n.º 10
0
def test_flux_map_from_dict_inconsistent_units(wcs_flux_map, reference_model):
    ref_map = FluxMaps(wcs_flux_map, reference_model)
    map_dict = dict()
    map_dict["eflux"] = ref_map.eflux
    map_dict["eflux"].quantity = map_dict["eflux"].quantity.to("keV/m2/s")
    map_dict["eflux_err"] = ref_map.eflux_err
    map_dict["eflux_err"].quantity = map_dict["eflux_err"].quantity.to("keV/m2/s")

    flux_map = FluxMaps.from_maps(map_dict, "eflux", reference_model)

    assert_allclose(flux_map.norm.data, 1)
    assert flux_map.norm.unit == ""
    assert_allclose(flux_map.norm_err.data, 0.1)
    assert flux_map.norm_err.unit == ""
Exemplo n.º 11
0
def test_flux_map_properties(wcs_flux_map, reference_model):
    fluxmap = FluxMaps(wcs_flux_map, reference_model)

    assert_allclose(fluxmap.dnde.data[:, 0, 0], [1e-11, 1e-13])
    assert_allclose(fluxmap.dnde_err.data[:, 0, 0], [1e-12, 1e-14])
    assert_allclose(fluxmap.dnde_err.data[:, 0, 0], [1e-12, 1e-14])
    assert_allclose(fluxmap.dnde_errn.data[:, 0, 0], [2e-12, 2e-14])
    assert_allclose(fluxmap.dnde_errp.data[:, 0, 0], [2e-12, 2e-14])
    assert_allclose(fluxmap.dnde_ul.data[:, 0, 0], [2e-11, 2e-13])

    assert_allclose(fluxmap.flux.data[:, 0, 0], [9e-12, 9e-13])
    assert_allclose(fluxmap.flux_err.data[:, 0, 0], [9e-13, 9e-14])
    assert_allclose(fluxmap.flux_errn.data[:, 0, 0], [18e-13, 18e-14])
    assert_allclose(fluxmap.flux_errp.data[:, 0, 0], [18e-13, 18e-14])
    assert_allclose(fluxmap.flux_ul.data[:, 0, 0], [18e-12, 18e-13])

    assert_allclose(fluxmap.eflux.data[:, 0, 0], [2.302585e-12, 2.302585e-12])
    assert_allclose(fluxmap.eflux_err.data[:, 0, 0],
                    [2.302585e-13, 2.302585e-13])
    assert_allclose(fluxmap.eflux_errp.data[:, 0, 0],
                    [4.60517e-13, 4.60517e-13])
    assert_allclose(fluxmap.eflux_errn.data[:, 0, 0],
                    [4.60517e-13, 4.60517e-13])
    assert_allclose(fluxmap.eflux_ul.data[:, 0, 0], [4.60517e-12, 4.60517e-12])

    assert_allclose(fluxmap.e2dnde.data[:, 0, 0], [1e-12, 1e-12])
    assert_allclose(fluxmap.e2dnde_err.data[:, 0, 0], [1e-13, 1e-13])
    assert_allclose(fluxmap.e2dnde_errn.data[:, 0, 0], [2e-13, 2e-13])
    assert_allclose(fluxmap.e2dnde_errp.data[:, 0, 0], [2e-13, 2e-13])
    assert_allclose(fluxmap.e2dnde_ul.data[:, 0, 0], [2e-12, 2e-12])

    assert_allclose(fluxmap.sqrt_ts.data, 1)
    assert_allclose(fluxmap.ts.data[:, 0, 0], [0, 3])
Exemplo n.º 12
0
def test_missing_column(region_map_flux_estimate):
    del region_map_flux_estimate["norm_errn"]
    model = SkyModel(PowerLawSpectralModel(amplitude="1e-10 cm-2s-1TeV-1", index=2))
    fe = FluxMaps(data=region_map_flux_estimate, reference_model=model)

    with pytest.raises(AttributeError):
        fe.dnde_errn
Exemplo n.º 13
0
def test_map_properties(map_flux_estimate):
    model = SkyModel(PowerLawSpectralModel(amplitude="1e-10 cm-2s-1TeV-1", index=2))
    fe = FluxMaps(data=map_flux_estimate, reference_model=model)

    assert fe.dnde.unit == u.Unit("cm-2s-1TeV-1")
    assert_allclose(fe.dnde.quantity.value[:, 2, 2], [1e-9, 1e-11])
    assert_allclose(fe.dnde_err.quantity.value[:, 2, 2], [1e-10, 1e-12])
    assert_allclose(fe.dnde_errn.quantity.value[:, 2, 2], [2e-10, 2e-12])
    assert_allclose(fe.dnde_errp.quantity.value[:, 2, 2], [1.5e-10, 1.5e-12])
    assert_allclose(fe.dnde_ul.quantity.value[:, 2, 2], [2e-9, 2e-11])

    assert fe.e2dnde.unit == u.Unit("TeV cm-2s-1")
    assert_allclose(fe.e2dnde.quantity.value[:, 2, 2], [1e-10, 1e-10])
    assert_allclose(fe.e2dnde_err.quantity.value[:, 2, 2], [1e-11, 1e-11])
    assert_allclose(fe.e2dnde_errn.quantity.value[:, 2, 2], [2e-11, 2e-11])
    assert_allclose(fe.e2dnde_errp.quantity.value[:, 2, 2], [1.5e-11, 1.5e-11])
    assert_allclose(fe.e2dnde_ul.quantity.value[:, 2, 2], [2e-10, 2e-10])

    assert fe.flux.unit == u.Unit("cm-2s-1")
    assert_allclose(fe.flux.quantity.value[:, 2, 2], [9e-10, 9e-11])
    assert_allclose(fe.flux_err.quantity.value[:, 2, 2], [9e-11, 9e-12])
    assert_allclose(fe.flux_errn.quantity.value[:, 2, 2], [1.8e-10, 1.8e-11])
    assert_allclose(fe.flux_errp.quantity.value[:, 2, 2], [1.35e-10, 1.35e-11])
    assert_allclose(fe.flux_ul.quantity.value[:, 2, 2], [1.8e-9, 1.8e-10])

    assert fe.eflux.unit == u.Unit("TeV cm-2s-1")
    assert_allclose(fe.eflux.quantity.value[:, 2, 2], [2.302585e-10, 2.302585e-10])
    assert_allclose(fe.eflux_err.quantity.value[:, 2, 2], [2.302585e-11, 2.302585e-11])
    assert_allclose(fe.eflux_errn.quantity.value[:, 2, 2], [4.60517e-11, 4.60517e-11])
    assert_allclose(
        fe.eflux_errp.quantity.value[:, 2, 2], [3.4538775e-11, 3.4538775e-11]
    )
    assert_allclose(fe.eflux_ul.quantity.value[:, 2, 2], [4.60517e-10, 4.60517e-10])
Exemplo n.º 14
0
def test_get_flux_point(wcs_flux_map, reference_model):
    fluxmap = FluxMaps(wcs_flux_map, reference_model)

    coord = SkyCoord(0., 0., unit="deg", frame="galactic")
    fp = fluxmap.get_flux_points(coord)

    assert_allclose(fp.table["e_min"], [0.1, 1.0])
    assert_allclose(fp.table["norm"], [1, 1])
    assert_allclose(fp.table["norm_err"], [0.1, 0.1])
    assert_allclose(fp.table["norm_errn"], [0.2, 0.2])
    assert_allclose(fp.table["norm_errp"], [0.2, 0.2])
    assert_allclose(fp.table["norm_ul"], [2, 2])
    assert_allclose(fp.table["sqrt_ts"], [1, 1])
    assert_allclose(fp.table["ts"], [0, 3])

    assert_allclose(fp.table["dnde"], [1e-11, 1e-13])
    assert fp.table["dnde"].unit == "cm-2s-1TeV-1"

    with mpl_plot_check():
        fp.plot()
Exemplo n.º 15
0
def test_flux_map_init_no_reference_model(wcs_flux_map, caplog):
    fluxmap = FluxMaps(wcs_flux_map)

    assert fluxmap.reference_model.spectral_model.tag[
        0] == "PowerLawSpectralModel"
    assert fluxmap.reference_model.spatial_model.tag[0] == "PointSpatialModel"
    assert fluxmap.reference_model.spectral_model.index.value == 2

    assert caplog.records[-1].levelname == "WARNING"
    assert f"No reference model set for FluxMaps." in caplog.records[
        -1].message
Exemplo n.º 16
0
def test_partial_flux_map_read_write(tmp_path, partial_wcs_flux_map,
                                     reference_model, sed_type):
    fluxmap = FluxMaps(partial_wcs_flux_map, reference_model)

    fluxmap.write(tmp_path / "tmp.fits", sed_type=sed_type)
    new_fluxmap = FluxMaps.read(tmp_path / "tmp.fits")

    assert_allclose(new_fluxmap.norm.data[:, 0, 0], [1, 1])
    assert_allclose(new_fluxmap.norm_err.data[:, 0, 0], [0.1, 0.1])

    # check model
    assert new_fluxmap.reference_model.spectral_model.tag[
        0] == "PowerLawSpectralModel"
    assert new_fluxmap.reference_model.spectral_model.index.value == 2

    # check existence and content of additional map
    assert_allclose(new_fluxmap.data["sqrt_ts"].data, 1.0)

    # the TS map shouldn't exist
    with pytest.raises(KeyError):
        new_fluxmap.data["ts"]
Exemplo n.º 17
0
def test_flux_map_read_write(tmp_path, wcs_flux_map, logpar_reference_model,
                             sed_type):
    fluxmap = FluxMaps(wcs_flux_map, logpar_reference_model)

    fluxmap.write(tmp_path / "tmp.fits", sed_type=sed_type)
    new_fluxmap = FluxMaps.read(tmp_path / "tmp.fits")

    assert_allclose(new_fluxmap.norm.data[:, 0, 0], [1, 1])
    assert_allclose(new_fluxmap.norm_err.data[:, 0, 0], [0.1, 0.1])
    assert_allclose(new_fluxmap.norm_errn.data[:, 0, 0], [0.2, 0.2])
    assert_allclose(new_fluxmap.norm_ul.data[:, 0, 0], [2, 2])

    # check model
    assert new_fluxmap.reference_model.spectral_model.tag[
        0] == "LogParabolaSpectralModel"
    assert new_fluxmap.reference_model.spectral_model.alpha.value == 1.5
    assert new_fluxmap.reference_model.spectral_model.beta.value == 0.5
    assert new_fluxmap.reference_model.spectral_model.amplitude.value == 2e-12

    # check existence and content of additional map
    assert_allclose(new_fluxmap.data["sqrt_ts"].data, 1.0)
Exemplo n.º 18
0
def test_table_properties(region_map_flux_estimate):
    model = SkyModel(PowerLawSpectralModel(amplitude="1e-10 cm-2s-1TeV-1", index=2))

    fe = FluxMaps(data=region_map_flux_estimate, reference_model=model)

    assert fe.dnde.unit == u.Unit("cm-2s-1TeV-1")
    assert_allclose(fe.dnde.data.flat, [1e-9, 1e-11])
    assert_allclose(fe.dnde_err.data.flat, [1e-10, 1e-12])
    assert_allclose(fe.dnde_errn.data.flat, [2e-10, 2e-12])
    assert_allclose(fe.dnde_errp.data.flat, [1.5e-10, 1.5e-12])
    assert_allclose(fe.dnde_ul.data.flat, [2e-9, 2e-11])

    assert fe.e2dnde.unit == u.Unit("TeV cm-2s-1")
    assert_allclose(fe.e2dnde.data.flat, [1e-10, 1e-10])

    assert fe.flux.unit == u.Unit("cm-2s-1")
    assert_allclose(fe.flux.data.flat, [9e-10, 9e-11])

    assert fe.eflux.unit == u.Unit("TeV cm-2s-1")
    assert_allclose(fe.eflux.data.flat, [2.302585e-10, 2.302585e-10])