# Adding this constant background components the fit works with cash statistics as well #spatial_model_bkg = Const2D('spatial-model-bkg') #spectral_model_bkg = PowLaw1D('spectral-model-bkg') #bkg_model = CombinedModel3D(spatial_model=spatial_model_bkg, spectral_model=spectral_model_bkg) bkg = TableModel('bkg') bkg.load(None, bkg_3D.data.value.ravel()) # Freeze bkg amplitude bkg.ampl=1 bkg.ampl.freeze() model = bkg+1E-11 * (source_model) # Fit # For now only Chi2 statistics seems to work, using Cash, the optimizer doesn't run at all, # maybe because of missing background model? fit = Fit(data=cube, model=model, stat=Cash(), method=NelderMead(), estmethod=Covariance()) result = fit.fit() err=fit.est_errors() print(err) def PWL(E,phi_0,gamma): return phi_0*E**(-gamma) def EXP(E,phi_0,gamma,beta): return phi_0*E**(-gamma)*np.exp(-beta*E) coord=exposure_3D.sky_image_ref.coordinates(mode="edges") d = coord.separation(center) pix_size=exposure_3D.wcs.to_header()["CDELT2"] i=np.where(d<pix_size*u.deg) #i permet de faire la moyenne exposure autour de pixel autour de la source mean_exposure=list()
def testCombinedModel3DIntConvolveEdisp(): from sherpa.models import PowLaw1D, TableModel from sherpa.estmethods import Covariance from sherpa.optmethods import NelderMead from sherpa.stats import Cash from sherpa.fit import Fit from ..sherpa_ import CombinedModel3DIntConvolveEdisp, NormGauss2DInt # Set the counts filename = gammapy_extra.filename('test_datasets/cube/counts_cube.fits') counts_3d = SkyCube.read(filename) cube = counts_3d.to_sherpa_data3d(dstype='Data3DInt') # Set the bkg filename = gammapy_extra.filename('test_datasets/cube/bkg_cube.fits') bkg_3d = SkyCube.read(filename) bkg = TableModel('bkg') bkg.load(None, bkg_3d.data.value.ravel()) bkg.ampl = 1 bkg.ampl.freeze() # Set the exposure filename = gammapy_extra.filename( 'test_datasets/cube/exposure_cube_etrue.fits') exposure_3d = SkyCube.read(filename) i_nan = np.where(np.isnan(exposure_3d.data)) exposure_3d.data[i_nan] = 0 # In order to have the exposure in cm2 s exposure_3d.data = exposure_3d.data * 1e4 # Set the mean psf model filename = gammapy_extra.filename('test_datasets/cube/psf_cube_etrue.fits') psf_3d = SkyCube.read(filename) # Set the mean rmf filename = gammapy_extra.filename('test_datasets/cube/rmf.fits') rmf = EnergyDispersion.read(filename) # Setup combined spatial and spectral model spatial_model = NormGauss2DInt('spatial-model') spectral_model = PowLaw1D('spectral-model') coord = counts_3d.sky_image_ref.coordinates(mode="edges") energies = counts_3d.energies(mode='edges').to("TeV") source_model = CombinedModel3DIntConvolveEdisp( coord=coord, energies=energies, use_psf=True, exposure=exposure_3d, psf=psf_3d, spatial_model=spatial_model, spectral_model=spectral_model, edisp=rmf.data.data, ) # Set starting values center = SkyCoord(83.633083, 22.0145, unit="deg").galactic source_model.gamma = 2.2 source_model.xpos = center.l.value source_model.ypos = center.b.value source_model.fwhm = 0.12 source_model.ampl = 1.0 # Fit model = bkg + 1e-11 * (source_model) fit = Fit(data=cube, model=model, stat=Cash(), method=NelderMead(), estmethod=Covariance()) result = fit.fit() # TODO: The fact that it doesn't converge to the right Crab postion, flux and source size is due to the dummy psf reference = [ 1.841920e+02, -6.175650e+00, 6.227756e+00, 7.129763e-02, 2.269026e+00 ] assert_allclose(result.parvals, reference, rtol=1e-3) # Add a region to exclude in the fit: Here we will exclude some events from the Crab since there is no region to # exclude in the FOV for this example. It's just an example to show how it works and how to proceed in the fit. # Read the mask for the exclude region filename_mask = gammapy_extra.filename('test_datasets/cube/mask.fits') cube_mask = SkyCube.read(filename_mask) index_region_selected_3d = np.where(cube_mask.data.value == 1) # Set the counts and create a gammapy Data3DInt object # on which we apply a mask for the region we don't want to use in the fit cube = counts_3d.to_sherpa_data3d(dstype='Data3DInt') cube.mask = cube_mask.data.value.ravel() # Set the bkg and select only the data points of the selected region bkg = TableModel('bkg') bkg.load(None, bkg_3d.data.value[index_region_selected_3d].ravel()) bkg.ampl = 1 bkg.ampl.freeze() # The model is evaluated on all the points then it is compared with the data only on the selected_region source_model = CombinedModel3DIntConvolveEdisp( coord=coord, energies=energies, use_psf=True, exposure=exposure_3d, psf=psf_3d, spatial_model=spatial_model, spectral_model=spectral_model, edisp=rmf.data.data, select_region=True, index_selected_region=index_region_selected_3d, ) # Set starting values source_model.gamma = 2.2 source_model.xpos = center.l.value source_model.ypos = center.b.value source_model.fwhm = 0.12 source_model.ampl = 1.0 # Fit model = bkg + 1e-11 * (source_model) fit = Fit(data=cube, model=model, stat=Cash(), method=NelderMead(), estmethod=Covariance()) result2 = fit.fit() # TODO: The fact that it doesn't converge to the right Crab postion is due to the dummy psf reference2 = [ 1.841921e+02, -6.169094e+00, 5.497368e+00, 7.513465e-02, 2.250965e+00 ] assert_allclose(result2.parvals, reference2, rtol=1e-3)
def test_cash_stat(self): fit = Fit(self.data, self.model, Cash(), NelderMead()) results = fit.fit() self.compare_results(self._fit_mycash_results_bench, results)