def test_ring_background_estimator(images): ring = RingBackgroundEstimator(0.35 * u.deg, 0.3 * u.deg) result = ring.run(images) in_fov = images["background"].data > 0 assert_allclose(result["background_ring"].data[in_fov], 2.0) assert_allclose(result["alpha"].data[in_fov].mean(), 0.003488538457592745) assert_allclose(result["exposure_off"].data[in_fov].mean(), 305.1268970794541) assert_allclose(result["off"].data[in_fov].mean(), 610.2537941589082) assert_allclose(result["off"].data[~in_fov], 0.0) assert_allclose(result["exposure_off"].data[~in_fov], 0.0) assert_allclose(result["alpha"].data[~in_fov], 0.0)
def run_benchmark(): # Set up data store and select N_OBS times the observation OBS_ID data_store = DataStore.from_dir('$GAMMAPY_EXTRA/test_datasets/cta_1dc/') obs_ids = OBS_ID * np.ones(N_OBS) obs_list = data_store.obs_list(obs_id=obs_ids) target_position = SkyCoord(0, 0, unit='deg', frame='galactic') on_radius = 0.2 * u.deg on_region = CircleSkyRegion(center=target_position, radius=on_radius) bkg_estimator = RingBackgroundEstimator( r_in=0.5 * u.deg, width=0.2 * u.deg, ) # Define reference image centered on the target xref = target_position.galactic.l.value yref = target_position.galactic.b.value ref_image = SkyImage.empty( nxpix=800, nypix=600, binsz=0.02, xref=xref, yref=yref, proj='TAN', coordsys='GAL', ) exclusion_mask = ref_image.region_mask(on_region) exclusion_mask.data = 1 - exclusion_mask.data image_estimator = IACTBasicImageEstimator( reference=ref_image, emin=100 * u.GeV, emax=100 * u.TeV, offset_max=3 * u.deg, background_estimator=bkg_estimator, exclusion_mask=exclusion_mask, ) result = image_estimator.run(obs_list)
def test_map_maker_ring(observations): ring_bkg = RingBackgroundEstimator(r_in="0.5 deg", width="0.4 deg") geomd = geom(ebounds=[0.1, 1, 10]) mask = Map.from_geom(geomd) regions = CircleSkyRegion(SkyCoord(0, 0, unit="deg", frame="galactic"), radius=0.5 * u.deg) mask.data = mask.geom.region_mask([regions], inside=False) maker = MapMakerRing(geomd, 2.0 * u.deg, mask, ring_bkg) maps = maker.run(observations) assert_allclose(np.nansum(maps["on"].data), 21981, rtol=1e-2) assert_allclose(np.nansum(maps["exposure_off"].data), 8310.035, rtol=1e-2) assert maps["on"].geom.is_image is False images = maker.run_images(observations) assert_allclose(np.nansum(images["on"].data), 21981, rtol=1e-2) assert_allclose(np.nansum(images["exposure_off"].data), 109751.45, rtol=1e-2) assert images["on"].geom.is_image is True
print(ref_image) # ### Compute images # # We use the ring background estimation method, and an exclusion mask that excludes the bright source at the Galactic center. # In[11]: exclusion_mask = ref_image.region_mask(on_region) exclusion_mask.data = 1 - exclusion_mask.data exclusion_mask.plot() # In[12]: bkg_estimator = RingBackgroundEstimator( r_in=0.5 * u.deg, width=0.2 * u.deg, ) image_estimator = IACTBasicImageEstimator( reference=ref_image, emin=100 * u.GeV, emax=100 * u.TeV, offset_max=3 * u.deg, background_estimator=bkg_estimator, exclusion_mask=exclusion_mask, ) images = image_estimator.run(obs_list) images.names # ### Show images # # Let's define a little helper function and then show all the resulting images that were computed.
# ### Choose an exclusion mask. # We choose an exclusion mask on the position of MSH 1552. # In[ ]: regions = CircleSkyRegion(center=pos_msh1552, radius=0.3 * u.deg) mask = Map.from_geom(geom) mask.data = mask.geom.region_mask([regions], inside=False) mask.get_image_by_idx([0]).plot() # Now, we instantiate the ring background # In[ ]: ring_bkg = RingBackgroundEstimator(r_in="0.5 deg", width="0.3 deg") # To facilitate classical image analysis, we have a special class called `MapMakerRing`. Here, we do the analysis over the integrated energy range. To do an analysis for each image slice of the map (eg: to investigate the significance of the source detection with energy, just call `run` instead of `run_images` # In[ ]: get_ipython().run_cell_magic( 'time', '', 'im = MapMakerRing(\n geom=geom,\n offset_max=2.0 * u.deg,\n exclusion_mask=mask,\n background_estimator=ring_bkg,\n)\nimages = im.run_images(observations)' ) # We will use `compute_lima_on_off_image` in `gammapy.detect` to compute significance and excess maps. A common debug plot during Ring Analysis is to make histograms of the `off` count rates, so we will plot that as well. # # For this we will first create a Tophat2DKernel convolution kernel for the significance maps. To convert from angles to pixel scales, we use `geom.pixel_scales`: # In[ ]:
) # This takes about 10 seconds on my machine result2 = kbe.run(images) print("results2") print(result2.names) ####altro modo stima bkg obs_list = data_store.obs_list(obs_ids) target_position = SkyCoord(0, 0, unit='deg', frame='galactic') on_radius = 0.3 * u.deg on_region = CircleSkyRegion(center=target_position, radius=on_radius) exclusion_mask = ref_image.region_mask(on_region) exclusion_mask.data = 1 - exclusion_mask.data bkg_estimator = RingBackgroundEstimator( r_in=0.5 * u.deg, width=0.2 * u.deg, ) image_estimator = IACTBasicImageEstimator( reference=ref_image, emin=100 * u.GeV, emax=100 * u.TeV, #offset_max=3 * u.deg, background_estimator=bkg_estimator, exclusion_mask=exclusion_mask, ) images_new = image_estimator.run(obs_list) print("\n\n\n") print(images_new.names) print(result2.names) print("\n\n\n") # Let's have a look at the background image and the exclusion mask