Esempio n. 1
0
def test_compute_lima_on_off_image():
    """
    Test Li & Ma image with snippet from the H.E.S.S. survey data.
    """
    filename = "$GAMMAPY_DATA/tests/unbundled/hess/survey/hess_survey_snippet.fits.gz"
    n_on = Map.read(filename, hdu="ON")
    counts = image_to_cube(n_on, "1 TeV", "100 TeV")
    n_off = Map.read(filename, hdu="OFF")
    counts_off = image_to_cube(n_off, "1 TeV", "100 TeV")
    a_on = Map.read(filename, hdu="ONEXPOSURE")
    acceptance = image_to_cube(a_on, "1 TeV", "100 TeV")
    a_off = Map.read(filename, hdu="OFFEXPOSURE")
    acceptance_off = image_to_cube(a_off, "1 TeV", "100 TeV")
    dataset = MapDatasetOnOff(
        counts=counts,
        counts_off=counts_off,
        acceptance=acceptance,
        acceptance_off=acceptance_off,
    )

    significance = Map.read(filename, hdu="SIGNIFICANCE")
    significance = image_to_cube(significance, "1 TeV", "10 TeV")
    estimator = ExcessMapEstimator("0.1 deg")
    results = estimator.run(dataset)

    # Reproduce safe significance threshold from HESS software
    results["sqrt_ts"].data[results["counts"].data < 5] = 0

    # crop the image at the boundaries, because the reference image
    # is cut out from a large map, there is no way to reproduce the
    # result with regular boundary handling
    actual = results["sqrt_ts"].crop((11, 11)).data
    desired = significance.crop((11, 11)).data

    # Set boundary to NaN in reference image
    # The absolute tolerance is low because the method used here is slightly different from the one used in HGPS
    # n_off is convolved as well to ensure the method applies to true ON-OFF datasets
    assert_allclose(actual, desired, atol=0.2)
Esempio n. 2
0
def test_compute_lima_image():
    """
    Test Li & Ma image against TS image for Tophat kernel
    """
    filename = "$GAMMAPY_DATA/tests/unbundled/poisson_stats_image/input_all.fits.gz"
    counts = Map.read(filename, hdu="counts")
    counts = image_to_cube(counts, "1 GeV", "100 GeV")
    background = Map.read(filename, hdu="background")
    background = image_to_cube(background, "1 GeV", "100 GeV")
    dataset = MapDataset(counts=counts, background=background)

    estimator = ExcessMapEstimator("0.1 deg")
    result_lima = estimator.run(dataset)

    assert_allclose(result_lima["sqrt_ts"].data[:, 100, 100],
                    30.814916,
                    atol=1e-3)
    assert_allclose(result_lima["sqrt_ts"].data[:, 1, 1], 0.164, atol=1e-3)
    assert_allclose(result_lima["npred_background"].data[:, 1, 1],
                    37,
                    atol=1e-3)
    assert_allclose(result_lima["npred"].data[:, 1, 1], 38, atol=1e-3)
    assert_allclose(result_lima["npred_excess"].data[:, 1, 1], 1, atol=1e-3)
Esempio n. 3
0
    def get_excess_map(self):
        """Calculate excess map with respect to the current model."""
        excess_settings = self.config.excess_map
        log.info("Computing excess maps.")

        # TODO: Here we could possibly stack the datasets if needed
        # or allow to compute the excess map for each dataset
        if len(self.datasets) > 1:
            raise ValueError("Datasets must be stacked to compute the excess map")

        if self.datasets[0].tag not in ["MapDataset", "MapDatasetOnOff"]:
            raise ValueError("Cannot compute excess map for 1D dataset")

        energy_edges = self._make_energy_axis(excess_settings.energy_edges)
        if energy_edges is not None:
            energy_edges = energy_edges.edges

        excess_map_estimator = ExcessMapEstimator(
            correlation_radius=excess_settings.correlation_radius,
            energy_edges=energy_edges,
            **excess_settings.parameters,
        )
        self.excess_map = excess_map_estimator.run(self.datasets[0])
Esempio n. 4
0
def test_excess_map_estimator_map_dataset_on_off_with_correlation_mask_fit(
    simple_dataset_on_off, ):
    geom = simple_dataset_on_off.counts.geom
    mask_fit = Map.from_geom(geom, data=1, dtype=bool)
    mask_fit.data[:, :, 10] = False
    mask_fit.data[:, 10, :] = False
    simple_dataset_on_off.mask_fit = mask_fit

    estimator_image = ExcessMapEstimator(0.11 * u.deg, correlate_off=True)

    result_image = estimator_image.run(simple_dataset_on_off)
    assert result_image["npred"].data.shape == (1, 20, 20)

    assert_allclose(result_image["sqrt_ts"].data[0, 10, 10], np.nan, atol=1e-3)
    assert_allclose(result_image["npred"].data[0, 10, 10], np.nan)
    assert_allclose(result_image["npred_excess"].data[0, 10, 10], np.nan)
    assert_allclose(result_image["sqrt_ts"].data[0, 9, 9], 7.186745, atol=1e-3)
    assert_allclose(result_image["npred"].data[0, 9, 9], 304)
    assert_allclose(result_image["npred_excess"].data[0, 9, 9], 152)

    assert result_image["flux"].unit == u.Unit("cm-2s-1")
    assert_allclose(result_image["flux"].data[0, 9, 9],
                    1.190928e-08,
                    rtol=1e-3)
Esempio n. 5
0
def test_significance_map_estimator_incorrect_dataset():
    with pytest.raises(ValueError):
        ExcessMapEstimator("bad")
Esempio n. 6
0
def test_significance_map_estimator_map_dataset_on_off_with_correlation(
    simple_dataset_on_off, ):
    exposure = simple_dataset_on_off.exposure
    exposure.data += 1e6

    # First without exposure
    simple_dataset_on_off.exposure = None

    estimator = ExcessMapEstimator(0.11 * u.deg,
                                   energy_edges=[0.1, 1, 10] * u.TeV,
                                   correlate_off=True)
    result = estimator.run(simple_dataset_on_off)

    assert result["counts"].data.shape == (2, 20, 20)
    assert_allclose(result["counts"].data[:, 10, 10], 194)
    assert_allclose(result["excess"].data[:, 10, 10], 97)
    assert_allclose(result["background"].data[:, 10, 10], 97)
    assert_allclose(result["sqrt_ts"].data[:, 10, 10], 5.741116, atol=1e-5)
    assert_allclose(result["flux"].data[:, 10, 10], np.nan)

    # Test with exposure
    simple_dataset_on_off.exposure = exposure
    estimator_image = ExcessMapEstimator(0.11 * u.deg,
                                         energy_edges=[0.1, 1] * u.TeV,
                                         correlate_off=True)

    result_image = estimator_image.run(simple_dataset_on_off)
    assert result_image["counts"].data.shape == (1, 20, 20)
    assert_allclose(result_image["counts"].data[0, 10, 10], 194)
    assert_allclose(result_image["excess"].data[0, 10, 10], 97)
    assert_allclose(result_image["background"].data[0, 10, 10], 97)
    assert_allclose(result_image["sqrt_ts"].data[0, 10, 10],
                    5.741116,
                    atol=1e-3)
    assert_allclose(result_image["flux"].data[:, 10, 10], 9.7e-9, atol=1e-5)

    # Test with mask fit
    mask_fit = Map.from_geom(
        simple_dataset_on_off._geom,
        data=np.ones(simple_dataset_on_off.counts.data.shape, dtype=bool),
    )
    mask_fit.data[:, :, 10] = False
    mask_fit.data[:, 10, :] = False
    simple_dataset_on_off.mask_fit = mask_fit

    estimator_image = ExcessMapEstimator(0.11 * u.deg,
                                         apply_mask_fit=True,
                                         correlate_off=True)

    result_image = estimator_image.run(simple_dataset_on_off)
    assert result_image["counts"].data.shape == (1, 20, 20)

    assert_allclose(result_image["sqrt_ts"].data[0, 10, 10], np.nan, atol=1e-3)
    assert_allclose(result_image["counts"].data[0, 10, 10], np.nan)
    assert_allclose(result_image["excess"].data[0, 10, 10], np.nan)
    assert_allclose(result_image["background"].data[0, 10, 10], np.nan)
    assert_allclose(result_image["sqrt_ts"].data[0, 9, 9], 7.186745, atol=1e-3)
    assert_allclose(result_image["counts"].data[0, 9, 9], 304)
    assert_allclose(result_image["excess"].data[0, 9, 9], 152)
    assert_allclose(result_image["background"].data[0, 9, 9], 152)

    assert result_image["flux"].unit == u.Unit("cm-2s-1")
    assert_allclose(result_image["flux"].data[0, 9, 9], 1.52e-8, rtol=1e-3)

    simple_dataset_on_off.psf = None

    # TODO: this has never worked...
    model = SkyModel(
        PowerLawSpectralModel(amplitude="1e-9 cm-2 s-1TeV-1"),
        GaussianSpatialModel(lat_0=0.0 * u.deg,
                             lon_0=0.0 * u.deg,
                             sigma=0.1 * u.deg,
                             frame="icrs"),
        name="sky_model",
    )

    simple_dataset_on_off.models = [model]

    estimator_mod = ExcessMapEstimator(0.11 * u.deg,
                                       apply_mask_fit=False,
                                       correlate_off=True)
    result_mod = estimator_mod.run(simple_dataset_on_off)
    assert result_mod["counts"].data.shape == (1, 20, 20)

    assert_allclose(result_mod["sqrt_ts"].data[0, 10, 10], 8.899278, atol=1e-3)

    assert_allclose(result_mod["counts"].data[0, 10, 10], 388)
    assert_allclose(result_mod["excess"].data[0, 10, 10], 190.68057)
    assert_allclose(result_mod["background"].data[0, 10, 10], 197.31943)

    assert result_mod["flux"].unit == "cm-2s-1"
    assert_allclose(result_mod["flux"].data[0, 10, 10],
                    1.906806e-08,
                    rtol=1e-3)
    assert_allclose(result_mod["flux"].data.sum(), 5.920642e-06, rtol=1e-3)

    spectral_model = PowerLawSpectralModel(index=15)
    estimator_mod = ExcessMapEstimator(
        0.11 * u.deg,
        apply_mask_fit=False,
        correlate_off=True,
        spectral_model=spectral_model,
    )
    result_mod = estimator_mod.run(simple_dataset_on_off)

    assert result_mod["flux"].unit == "cm-2s-1"
    assert_allclose(result_mod["flux"].data.sum(), 5.920642e-06, rtol=1e-3)

    reco_exposure = estimate_exposure_reco_energy(
        simple_dataset_on_off, spectral_model=spectral_model)
    assert_allclose(reco_exposure.data.sum(), 7.977796e+12, rtol=0.001)
Esempio n. 7
0
def test_significance_map_estimator_map_dataset_on_off(simple_dataset_on_off):
    exposure = simple_dataset_on_off.exposure
    exposure.data += 1e6

    # First without exposure
    simple_dataset_on_off.exposure = None

    estimator = ExcessMapEstimator(
        0.11 * u.deg,
        selection_optional=None,
        energy_edges=[0.1, 1, 10] * u.TeV,
    )
    result = estimator.run(simple_dataset_on_off)

    assert result["counts"].data.shape == (2, 20, 20)
    assert_allclose(result["counts"].data[:, 10, 10], 194)
    assert_allclose(result["excess"].data[:, 10, 10], 97)
    assert_allclose(result["background"].data[:, 10, 10], 97)
    assert_allclose(result["sqrt_ts"].data[:, 10, 10], 5.741116, atol=1e-5)
    assert_allclose(result["flux"].data[:, 10, 10], np.nan)

    # Test with exposure
    simple_dataset_on_off.exposure = exposure
    estimator_image = ExcessMapEstimator(0.11 * u.deg,
                                         energy_edges=[0.1, 1] * u.TeV)

    result_image = estimator_image.run(simple_dataset_on_off)
    assert result_image["counts"].data.shape == (1, 20, 20)
    assert_allclose(result_image["counts"].data[0, 10, 10], 194)
    assert_allclose(result_image["excess"].data[0, 10, 10], 97)
    assert_allclose(result_image["background"].data[0, 10, 10], 97)
    assert_allclose(result_image["sqrt_ts"].data[0, 10, 10],
                    5.741116,
                    atol=1e-3)
    assert_allclose(result_image["flux"].data[:, 10, 10], 9.7e-9, atol=1e-5)

    # Test with mask fit
    mask_fit = Map.from_geom(
        simple_dataset_on_off._geom,
        data=np.ones(simple_dataset_on_off.counts.data.shape, dtype=bool),
    )
    mask_fit.data[:, :, 10] = False
    mask_fit.data[:, 10, :] = False
    simple_dataset_on_off.mask_fit = mask_fit

    estimator_image = ExcessMapEstimator(0.11 * u.deg, apply_mask_fit=True)

    result_image = estimator_image.run(simple_dataset_on_off)
    assert result_image["counts"].data.shape == (1, 20, 20)

    assert_allclose(result_image["sqrt_ts"].data[0, 10, 10], np.nan, atol=1e-3)
    assert_allclose(result_image["counts"].data[0, 10, 10], np.nan)
    assert_allclose(result_image["excess"].data[0, 10, 10], np.nan)
    assert_allclose(result_image["background"].data[0, 10, 10], np.nan)
    assert_allclose(result_image["sqrt_ts"].data[0, 9, 9], 7.186745, atol=1e-3)
    assert_allclose(result_image["counts"].data[0, 9, 9], 304)
    assert_allclose(result_image["excess"].data[0, 9, 9], 152)
    assert_allclose(result_image["background"].data[0, 9, 9], 152)

    assert result_image["flux"].unit == u.Unit("cm-2s-1")
    assert_allclose(result_image["flux"].data[0, 9, 9], 1.52e-8, rtol=1e-3)

    simple_dataset_on_off.psf = None

    # TODO: this has never worked...
    model = SkyModel(
        PowerLawSpectralModel(amplitude="1e-9 cm-2 s-1TeV-1"),
        GaussianSpatialModel(lat_0=0.0 * u.deg,
                             lon_0=0.0 * u.deg,
                             sigma=0.1 * u.deg,
                             frame="icrs"),
        name="sky_model",
    )
    #
    simple_dataset_on_off.models = [model]

    estimator_mod = ExcessMapEstimator(0.11 * u.deg, apply_mask_fit=False)
    result_mod = estimator_mod.run(simple_dataset_on_off)
    assert result_mod["counts"].data.shape == (1, 20, 20)

    assert_allclose(result_mod["sqrt_ts"].data[0, 10, 10], 6.240846, atol=1e-3)

    assert_allclose(result_mod["counts"].data[0, 10, 10], 388)
    assert_allclose(result_mod["excess"].data[0, 10, 10], 148.68057)
    assert_allclose(result_mod["background"].data[0, 10, 10], 239.31943)

    assert result_mod["flux"].unit == u.Unit("cm-2s-1")
    assert_allclose(result_mod["flux"].data[0, 10, 10],
                    1.486806e-08,
                    rtol=1e-3)
Esempio n. 8
0
def test_significance_map_estimator_map_dataset_on_off(simple_dataset_on_off):
    estimator = ExcessMapEstimator(
        0.11 * u.deg,
        selection_optional=None,
        e_edges=[0.1 * u.TeV, 1 * u.TeV, 10 * u.TeV],
    )
    result = estimator.run(simple_dataset_on_off)

    assert result["counts"].data.shape == (2, 20, 20)
    assert_allclose(result["counts"].data[:, 10, 10], 194)
    assert_allclose(result["excess"].data[:, 10, 10], 97)
    assert_allclose(result["background"].data[:, 10, 10], 97)
    assert_allclose(result["significance"].data[:, 10, 10], 5.741116, atol=1e-5)

    estimator_image = ExcessMapEstimator(0.11 * u.deg, e_edges=[0.1 * u.TeV, 1 * u.TeV])

    result_image = estimator_image.run(simple_dataset_on_off)
    assert result_image["counts"].data.shape == (1, 20, 20)
    assert_allclose(result_image["significance"].data[0, 10, 10], 5.741116, atol=1e-3)

    mask_fit = Map.from_geom(
        simple_dataset_on_off._geom,
        data=np.ones(simple_dataset_on_off.counts.data.shape, dtype=bool),
    )
    mask_fit.data[:, :, 10] = False
    mask_fit.data[:, 10, :] = False
    simple_dataset_on_off.mask_fit = mask_fit

    estimator_image = ExcessMapEstimator(0.11 * u.deg, apply_mask_fit=True)

    simple_dataset_on_off.exposure.data = (
        np.ones(simple_dataset_on_off.exposure.data.shape) * 1e6
    )
    result_image = estimator_image.run(simple_dataset_on_off)
    assert result_image["counts"].data.shape == (1, 20, 20)

    assert_allclose(result_image["significance"].data[0, 10, 10], 7.186745, atol=1e-3)

    assert_allclose(result_image["counts"].data[0, 10, 10], 304)
    assert_allclose(result_image["excess"].data[0, 10, 10], 152)
    assert_allclose(result_image["background"].data[0, 10, 10], 152)

    assert result_image["flux"].unit == u.Unit("cm-2s-1")
    assert_allclose(result_image["flux"].data[0, 10, 10], 7.6e-9, rtol=1e-3)

    # test with an npred()
    simple_dataset_on_off.exposure.data = (
        np.ones(simple_dataset_on_off.exposure.data.shape) * 1e10
    )
    simple_dataset_on_off.psf = None
    model = SkyModel(
        PowerLawSpectralModel(),
        GaussianSpatialModel(
            lat_0=0.0 * u.deg, lon_0=0.0 * u.deg, sigma=0.1 * u.deg, frame="icrs"
        ),
        datasets_names=[simple_dataset_on_off.name],
        name="sky_model",
    )
    simple_dataset_on_off.models.append(model)
    estimator_mod = ExcessMapEstimator(0.11 * u.deg, apply_mask_fit=False)
    result_mod = estimator_mod.run(simple_dataset_on_off)
    assert result_mod["counts"].data.shape == (1, 20, 20)

    assert_allclose(result_mod["significance"].data[0, 10, 10], 8.119164, atol=1e-3)

    assert_allclose(result_mod["counts"].data[0, 10, 10], 388)
    assert_allclose(result_mod["excess"].data[0, 10, 10], 194)
    assert_allclose(result_mod["background"].data[0, 10, 10], 194)

    assert result_mod["flux"].unit == u.Unit("cm-2s-1")
    assert_allclose(result_image["flux"].data[0, 10, 10], 7.6e-9, rtol=1e-3)
Esempio n. 9
0
def estimate_significance_luca(
    dataset, 
    first_guess_exclusion,
    correlation_radii,
    energy_edgess,
    n_iter,
    low,
    high,
    negative=False
):    
    for nn in range(n_iter):
        print(f"###########")
        print(nn)
        print(f"###########")

        results = []
        for correlation_radius, energy_edges in zip(correlation_radii, energy_edgess):
            estimator_excess = ExcessMapEstimator(
                correlation_radius=correlation_radius,
                n_sigma=1, 
                n_sigma_ul=3,
                selection_optional=None,
                energy_edges=energy_edges, 
            )
            results.append(estimator_excess.run(dataset))

        def _reproject_exclusion_mask(geom, exclusion):
            """Reproject the exclusion on the dataset geometry"""
            mask_map = Map.from_geom(geom)
            coords = geom.get_coord()
            vals = exclusion.get_by_coord(coords)
            mask_map.data += vals
            return mask_map
        first_guess_exclusion = _reproject_exclusion_mask(dataset.counts.geom, first_guess_exclusion).slice_by_idx({"energy" : 0})

        plt.figure(figsize=(8, 6))
        for correlation_radius, energy_edges, result in zip(correlation_radii, energy_edgess, results):
            (result["sqrt_ts"].slice_by_idx({"energy" : 0})*first_guess_exclusion).plot(add_cbar=True, cmap="coolwarm", vmin=-5, vmax=5)
            plt.title(f"{correlation_radius} {energy_edges}")
            plt.show()
            plt.close()

        exclusion_enlarged = first_guess_exclusion.copy()
        # Enlarge exclusion mask
        for result, radius in zip(results, correlation_radii):
            mask_map_significance = result["sqrt_ts"].copy().slice_by_idx({"energy" : 0})
            mask_map_significance.data = np.nan_to_num(mask_map_significance.data)
            mask_map = mask_map_significance.copy()
            mask_map.data = ~apply_hysteresis_threshold(mask_map_significance.data, low=low, high=high)
            if negative == True:
                mask_map.data *= ~apply_hysteresis_threshold(-mask_map_significance.data, low=low, high=high)
            exclusion_enlarged *= mask_map

        plt.figure(figsize=(8, 6))
        first_guess_exclusion.plot()
        plt.contour(exclusion_enlarged.data, levels=1, colors="r", inewidths=2)
        plt.show()
        plt.close()

        for correlation_radius, energy_edges, result in zip(correlation_radii, energy_edgess, results):
            pretty_plot_1d_significance(result, correlation_radius, energy_edges, exclusion_enlarged)
            
        first_guess_exclusion = exclusion_enlarged
        
    print(f"###########")
    print("Final result")
    print(f"###########")

    for correlation_radius, energy_edges, result in zip(correlation_radii, energy_edgess, results):
        plt.figure(figsize=(8, 6))
        final = (result["sqrt_ts"].slice_by_idx({"energy" : 0}))
        final.plot(add_cbar=True, cmap="coolwarm", vmin=-5, vmax=5)
        cs = plt.contour(final.data, levels=(-15, -10, -7, -5, -3, 3, 5, 7, 10, 15), colors="k")
        plt.clabel(cs, colors="k")
        plt.title(f"{correlation_radius} {energy_edges}")
        plt.show()
        plt.close()