def test_significance_map_estimator_map_dataset(simple_dataset): estimator = LiMaMapEstimator(0.1 * u.deg) result = estimator.run(simple_dataset) assert_allclose(result["counts"].data[0, 25, 25], 162) assert_allclose(result["excess"].data[0, 25, 25], 81) assert_allclose(result["background"].data[0, 25, 25], 81) assert_allclose(result["significance"].data[0, 25, 25], 7.910732)
def test_significance_map_estimator_map_dataset_on_off(simple_dataset_on_off): estimator = LiMaMapEstimator(0.1 * u.deg) result = estimator.run(simple_dataset_on_off) assert_allclose(result["n_on"].data[0, 25, 25], 162) assert_allclose(result["excess"].data[0, 25, 25], 81) assert_allclose(result["background"].data[0, 25, 25], 81) assert_allclose(result["significance"].data[0, 25, 25], 5.246298)
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") n_off = Map.read(filename, hdu="OFF") a_on = Map.read(filename, hdu="ONEXPOSURE") a_off = Map.read(filename, hdu="OFFEXPOSURE") significance = Map.read(filename, hdu="SIGNIFICANCE") kernel = Tophat2DKernel(5) results = LiMaMapEstimator.compute_lima_on_off_image( n_on, n_off, a_on, a_off, kernel) # Reproduce safe significance threshold from HESS software results["significance"].data[results["n_on"].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["significance"].crop(kernel.shape).data desired = significance.crop(kernel.shape).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)
def plot_residual_distribution(dataset, obs_id): # plot residual significance distribution model = dataset.models[1] if model.tag == "SkyDiffuseCube": log.info(f"SkyDiffuseCube: no spectral model to plot") else: tophat_2D_kernel = Tophat2DKernel(5) l_m = lima.compute_lima_image( dataset.counts.sum_over_axes(keepdims=False), dataset.npred().sum_over_axes(keepdims=False), tophat_2D_kernel, ) sig_resid = l_m["significance"].data[np.isfinite( l_m["significance"].data)] # resid = dataset.residuals() # sig_resid = resid.data[np.isfinite(resid.data)] plt.hist( sig_resid, density=True, alpha=0.5, color="red", bins=100, ) mu, std = norm.fit(sig_resid) # replace with log.info() print("Fit results: mu = {:.2f}, std = {:.2f}".format(mu, std)) x = np.linspace(-8, 8, 50) p = norm.pdf(x, mu, std) plt.plot( x, p, lw=2, color="black", label="Fit results: mu = {:.2f}, std = {:.2f}".format(mu, std), ) plt.legend() plt.xlabel("Significance") plt.yscale("log") plt.ylim(1e-5, 1) xmin, xmax = np.min(sig_resid), np.max(sig_resid) plt.xlim(xmin, xmax) obs_id = int(obs_id) filename = f"results/models/{model.name}/plots/residuals-distribution/residuals-distribution_{obs_id:04d}.png" save_figure(filename)
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") background = Map.read(filename, hdu="background") kernel = Tophat2DKernel(5) result_lima = LiMaMapEstimator.compute_lima_image(counts, background, kernel) assert_allclose(result_lima["significance"].data[100, 100], 30.814916, atol=1e-3) assert_allclose(result_lima["significance"].data[1, 1], 0.164, atol=1e-3)
def test_significance_map_estimator_incorrect_dataset(): estimator = LiMaMapEstimator("0.1 deg") with pytest.raises(ValueError): estimator.run("bad")