Beispiel #1
0
def test_asmooth_map_dataset_on_off():
    kernel = Tophat2DKernel
    scales = ASmoothMapEstimator.get_scales(3, factor=2,
                                            kernel=kernel) * 0.1 * u.deg

    asmooth = ASmoothMapEstimator(kernel=kernel,
                                  scales=scales,
                                  method="lima",
                                  threshold=2.5)

    counts = WcsNDMap.create(npix=(50, 50), binsz=0.02, unit="")
    counts += 2
    counts_off = WcsNDMap.create(npix=(50, 50), binsz=0.02, unit="")
    counts_off += 3

    acceptance = WcsNDMap.create(npix=(50, 50), binsz=0.02, unit="")
    acceptance += 1

    acceptance_off = WcsNDMap.create(npix=(50, 50), binsz=0.02, unit="")
    acceptance_off += 3

    dataset = MapDatasetOnOff(
        counts=counts,
        counts_off=counts_off,
        acceptance=acceptance,
        acceptance_off=acceptance_off,
    )

    smoothed = asmooth.run(dataset)
    assert_allclose(smoothed["counts"].data[25, 25], 2)
    assert_allclose(smoothed["background"].data[25, 25], 1)
    assert_allclose(smoothed["significance"].data[25, 25], 4.391334)
Beispiel #2
0
def test_map_dataset_on_off_to_image():
    axis = MapAxis.from_energy_bounds(1, 10, 2, unit="TeV")
    geom = WcsGeom.create(npix=(10, 10), binsz=0.05, axes=[axis])

    counts = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    counts_off = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    acceptance = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    acceptance_off = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    acceptance_off *= 2

    dataset = MapDatasetOnOff(
        counts=counts,
        counts_off=counts_off,
        acceptance=acceptance,
        acceptance_off=acceptance_off,
    )
    image_dataset = dataset.to_image()

    assert image_dataset.counts.data.shape == (1, 10, 10)
    assert image_dataset.acceptance_off.data.shape == (1, 10, 10)
    assert_allclose(image_dataset.acceptance, 2)
    assert_allclose(image_dataset.acceptance_off, 4)
    assert_allclose(image_dataset.counts_off, 2)
    assert image_dataset.name != dataset.name

    # Try with a safe_mask
    mask_safe = Map.from_geom(geom, data=np.ones((2, 10, 10), dtype="bool"))
    mask_safe.data[0] = 0
    dataset.mask_safe = mask_safe
    image_dataset = dataset.to_image()

    assert_allclose(image_dataset.acceptance, 1)
    assert_allclose(image_dataset.acceptance_off, 2)
    assert_allclose(image_dataset.counts_off, 1)
Beispiel #3
0
def get_map_dataset_onoff(images, **kwargs):
    """Returns a MapDatasetOnOff"""
    mask_geom = images["counts"].geom
    mask_data = np.ones(images["counts"].data.shape, dtype=bool)
    mask_safe = Map.from_geom(mask_geom, data=mask_data)

    return MapDatasetOnOff(counts=images["counts"],
                           counts_off=images["counts_off"],
                           acceptance=images["acceptance"],
                           acceptance_off=images["acceptance_off"],
                           exposure=images["exposure"],
                           mask_safe=mask_safe,
                           **kwargs)
Beispiel #4
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", correlate_off=False)
    results = estimator.run(dataset)

    # Reproduce safe significance threshold from HESS software
    results["sqrt_ts"].data[results["npred"].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, rtol=1e-5)

    actual = np.nan_to_num(results["npred_background"].crop((11, 11)).data)
    background_corr = image_to_cube(
        Map.read(filename, hdu="BACKGROUNDCORRELATED"), "1 TeV", "100 TeV")
    desired = background_corr.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, rtol=1e-5)
Beispiel #5
0
def get_map_dataset_onoff(images, **kwargs):
    """Returns a MapDatasetOnOff"""
    mask_geom = images["counts"].geom
    mask_data = np.ones(images["counts"].data.shape, dtype=bool)
    mask_safe = Map.from_geom(mask_geom, data=mask_data)
    gti = GTI.create([0 * u.s], [1 * u.h],
                     reference_time="2010-01-01T00:00:00")

    return MapDatasetOnOff(counts=images["counts"],
                           counts_off=images["counts_off"],
                           acceptance=images["acceptance"],
                           acceptance_off=images["acceptance_off"],
                           exposure=images["exposure"],
                           mask_safe=mask_safe,
                           gti=gti,
                           **kwargs)
Beispiel #6
0
def test_asmooth_map_dataset_on_off():
    kernel = Tophat2DKernel
    scales = ASmoothMapEstimator.get_scales(3, factor=2,
                                            kernel=kernel) * 0.1 * u.deg

    asmooth = ASmoothMapEstimator(kernel=kernel,
                                  scales=scales,
                                  method="lima",
                                  threshold=2.5)

    axis = MapAxis.from_energy_bounds("1 TeV", "10 TeV", nbin=1)

    counts = WcsNDMap.create(npix=(50, 50), binsz=0.02, unit="", axes=[axis])
    counts += 2
    counts_off = WcsNDMap.create(npix=(50, 50),
                                 binsz=0.02,
                                 unit="",
                                 axes=[axis])
    counts_off += 3

    acceptance = WcsNDMap.create(npix=(50, 50),
                                 binsz=0.02,
                                 unit="",
                                 axes=[axis])
    acceptance += 1

    acceptance_off = WcsNDMap.create(npix=(50, 50),
                                     binsz=0.02,
                                     unit="",
                                     axes=[axis])
    acceptance_off += 3

    dataset = MapDatasetOnOff(
        counts=counts,
        counts_off=counts_off,
        acceptance=acceptance,
        acceptance_off=acceptance_off,
    )

    smoothed = asmooth.run(dataset)
    assert_allclose(smoothed["counts"].data[25, 25], 2)
    assert_allclose(smoothed["background"].data[25, 25], 1.25)
    assert_allclose(smoothed["significance"].data[25, 25],
                    3.079799117645,
                    rtol=1e-2)
Beispiel #7
0
def test_plot_residual_onoff():
    axis = MapAxis.from_energy_bounds(1, 10, 2, unit="TeV")
    geom = WcsGeom.create(npix=(10, 10), binsz=0.05, axes=[axis])

    counts = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    counts_off = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    acceptance = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    acceptance_off = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    acceptance_off *= 2

    dataset = MapDatasetOnOff(
        counts=counts,
        counts_off=counts_off,
        acceptance=acceptance,
        acceptance_off=acceptance_off,
    )
    with mpl_plot_check():
        dataset.plot_residuals_spatial()
Beispiel #8
0
def test_map_dataset_on_off_to_spectrum_dataset_weights():
    e_reco = MapAxis.from_bounds(1, 10, nbin=3, unit="TeV", name="energy")

    geom = WcsGeom.create(skydir=(0, 0),
                          width=(2.5, 2.5),
                          binsz=0.5,
                          axes=[e_reco],
                          frame="galactic")
    counts = Map.from_geom(geom)
    counts.data += 1
    counts_off = Map.from_geom(geom)
    counts_off.data += 2
    acceptance = Map.from_geom(geom)
    acceptance.data += 1
    acceptance_off = Map.from_geom(geom)
    acceptance_off.data += 4

    weights = Map.from_geom(geom, dtype='bool')
    weights.data[1:, 2:4, 2] = True

    gti = GTI.create([0 * u.s], [1 * u.h],
                     reference_time="2010-01-01T00:00:00")

    dataset = MapDatasetOnOff(
        counts=counts,
        counts_off=counts_off,
        acceptance=acceptance,
        acceptance_off=acceptance_off,
        mask_safe=weights,
        gti=gti,
    )

    on_region = CircleSkyRegion(center=dataset.counts.geom.center_skydir,
                                radius=1.5 * u.deg)

    spectrum_dataset = dataset.to_spectrum_dataset(on_region)

    assert_allclose(spectrum_dataset.counts.data[:, 0, 0], [0, 2, 2])
    assert_allclose(spectrum_dataset.counts_off.data[:, 0, 0], [0, 4, 4])
    assert_allclose(spectrum_dataset.acceptance.data[:, 0, 0], [0, 0.08, 0.08])
    assert_allclose(spectrum_dataset.acceptance_off.data[:, 0, 0],
                    [0, 0.32, 0.32])
    assert_allclose(spectrum_dataset.alpha.data[:, 0, 0], [0, 0.25, 0.25])
Beispiel #9
0
def test_downsample_onoff():
    axis = MapAxis.from_energy_bounds(1, 10, 4, unit="TeV")
    geom = WcsGeom.create(npix=(10, 10), binsz=0.05, axes=[axis])

    counts = Map.from_geom(geom, data=np.ones((4, 10, 10)))
    counts_off = Map.from_geom(geom, data=np.ones((4, 10, 10)))
    acceptance = Map.from_geom(geom, data=np.ones((4, 10, 10)))
    acceptance_off = Map.from_geom(geom, data=np.ones((4, 10, 10)))
    acceptance_off *= 2

    dataset_onoff = MapDatasetOnOff(
        counts=counts,
        counts_off=counts_off,
        acceptance=acceptance,
        acceptance_off=acceptance_off,
    )

    downsampled = dataset_onoff.downsample(2, axis_name="energy")

    assert downsampled.counts.data.shape == (2, 10, 10)
    assert downsampled.counts.data.sum() == dataset_onoff.counts.data.sum()
    assert downsampled.counts_off.data.sum() == dataset_onoff.counts_off.data.sum()
    assert_allclose(downsampled.alpha.data, 0.5)
Beispiel #10
0
def test_to_map_dataset():
    axis = MapAxis.from_energy_bounds(1, 10, 2, unit="TeV")
    geom = WcsGeom.create(npix=(10, 10), binsz=0.05, axes=[axis])

    counts = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    counts_off = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    acceptance = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    acceptance_off = Map.from_geom(geom, data=np.ones((2, 10, 10)))
    acceptance_off *= 2

    dataset_onoff = MapDatasetOnOff(
        counts=counts,
        counts_off=counts_off,
        acceptance=acceptance,
        acceptance_off=acceptance_off,
    )

    dataset = dataset_onoff.to_map_dataset(name="ds")

    assert dataset.name == "ds"
    assert_allclose(dataset.npred_background().data.sum(), 100)
    assert isinstance(dataset, MapDataset)
    assert dataset.counts == dataset_onoff.counts