npred = evaluator.compute_npred() npred_map = WcsNDMap(geom, npred) fig, ax, cbar = npred_map.sum_over_axes().plot(add_cbar=True) ax.scatter( [lon_0_1, lon_0_2, pointing.galactic.l.degree], [lat_0_1, lat_0_2, pointing.galactic.b.degree], transform=ax.get_transform("galactic"), marker="+", color="cyan", ) # plt.show() plt.clf() rng = get_random_state(42) counts = rng.poisson(npred) counts_map = WcsNDMap(geom, counts) counts_map.sum_over_axes().plot() # plt.show() plt.clf() compound_model.parameters.set_error(2, 0.1 * u.deg) compound_model.parameters.set_error(4, 1e-12 * u.Unit("cm-2 s-1 TeV-1")) compound_model.parameters.set_error(8, 0.1 * u.deg) compound_model.parameters.set_error(10, 1e-12 * u.Unit("cm-2 s-1 TeV-1")) fit = MapFit(model=compound_model, counts=counts_map, exposure=exposure_map) fit.run()
# ## Fit # # Finally, the big finale: let's do a 3D map fit for the source at the Galactic center, to measure it's position and spectrum. # In[ ]: model = SkyModel( SkyPointSource("0 deg", "0 deg"), PowerLaw(index=2.5, amplitude="1e-11 cm-2 s-1 TeV-1", reference="100 GeV"), ) fit = MapFit( model=model, counts=counts, exposure=exposure, background=background, psf=psf_kernel, ) result = fit.run() # In[ ]: print(result) # ## Exercises # # - Fit the position and spectrum of the source [SNR G0.9+0.1](http://gamma-sky.net/#/cat/tev/110).
npred = evaluator.compute_npred() npred_map = WcsNDMap(geom, npred) fig, ax, cbar = npred_map.sum_over_axes().plot(add_cbar=True) ax.scatter( [lon_0_1, lon_0_2, pointing.galactic.l.degree], [lat_0_1, lat_0_2, pointing.galactic.b.degree], transform=ax.get_transform("galactic"), marker="+", color="cyan", ) # plt.show() plt.clf() rng = get_random_state(42) counts = rng.poisson(npred) counts_map = WcsNDMap(geom, counts) counts_map.sum_over_axes().plot() # plt.show() plt.clf() models.parameters.set_error(2, 0.1 * u.deg) models.parameters.set_error(4, 1e-12 * u.Unit("cm-2 s-1 TeV-1")) models.parameters.set_error(8, 0.1 * u.deg) models.parameters.set_error(10, 1e-12 * u.Unit("cm-2 s-1 TeV-1")) fit = MapFit(model=models, counts=counts_map, exposure=exposure_map) fit.run()
spatial_model = SkyPointSource(lon_0="0.01 deg", lat_0="0.01 deg") spectral_model = PowerLaw(index=2.2, amplitude="3e-12 cm-2 s-1 TeV-1", reference="1 TeV") model = SkyModel(spatial_model=spatial_model, spectral_model=spectral_model) # Now we set up the `MapFit` object by passing the prepared maps, IRFs as well as the model: # In[ ]: fit = MapFit( model=model, counts=cmaps["counts"], exposure=cmaps["exposure"], background=cmaps["background"], mask=mask, psf=psf_kernel, edisp=edisp, ) # No we run the model fit: # In[ ]: get_ipython().run_cell_magic( 'time', '', 'result = fit.run(optimize_opts={"print_level": 1})') # ### Check model fit # # Finally we check the model fit by cmputing a residual image. For this we first get the number of predicted counts from the fit evaluator:
# In[ ]: spatial_model = SkyPointSource(lon_0="0.01 deg", lat_0="0.01 deg") spectral_model = PowerLaw2(emin=emin, emax=emax, index=2.0, amplitude="3e-12 cm-2 s-1") model = SkyModel(spatial_model=spatial_model, spectral_model=spectral_model) model.parameters["index"].frozen = True # In[ ]: fit = MapFit( model=model, counts=maps2D["counts"], exposure=maps2D["exposure"], background=maps2D["background"], mask=mask, psf=psf_kernel, ) # In[ ]: get_ipython().run_cell_magic('time', '', 'result = fit.run()') # To see the actual best-fit parameters, do a print on the result # In[ ]: print(model) # ## Todo: Demonstrate plotting a flux map
spatial_model = SkyPointSource(lon_0="0.01 deg", lat_0="0.01 deg") spectral_model = PowerLaw(index=2.2, amplitude="3e-12 cm-2 s-1 TeV-1", reference="1 TeV") model = SkyModel(spatial_model=spatial_model, spectral_model=spectral_model) # Now we set up the `MapFit` object by passing the prepared maps, IRFs as well as the model: # In[ ]: fit = MapFit( model=model, counts=cmaps["counts"], exposure=cmaps["exposure"], background=cmaps["background"], mask=mask, psf=psf_kernel, edisp=edisp, ) # No we run the model fit: # In[ ]: get_ipython().run_cell_magic( 'time', '', 'result = fit.run(optimize_opts={"print_level": 1})') # In[ ]: print(model)