Пример #1
0
def test_stats_calc_chisqr_1dint_multi_cvar(usestat, usesys, expected):
    """chi-quare statistics calculates expected values: constvar

    Here multiple datasets are used. To save time, the first
    dataset is re-used, excluding the third point. This means that
    the constvar results are a little-bit different to the other
    statistics.
    """

    data, model = setup_multiple_1dint(usestat, usesys)
    statobj = Chi2ConstVar()
    answer = statobj.calc_chisqr(data, model)
    assert_almost_equal(answer, expected)
Пример #2
0
def test_sherpa_crab_fit():
    from sherpa.models import NormGauss2D, PowLaw1D, TableModel, Const2D
    from sherpa.stats import Chi2ConstVar
    from sherpa.optmethods import LevMar
    from sherpa.fit import Fit
    from ..sherpa_ import Data3D, CombinedModel3D

    filename = gammapy_extra.filename(
        'experiments/sherpa_cube_analysis/counts.fits.gz')
    counts = SkyCube.read(filename)
    cube = counts.to_sherpa_data3d()

    # Set up exposure table model
    filename = gammapy_extra.filename(
        'experiments/sherpa_cube_analysis/exposure.fits.gz')
    exposure_data = fits.getdata(filename)
    exposure = TableModel('exposure')
    exposure.load(None, exposure_data.ravel())

    # Freeze exposure amplitude
    exposure.ampl.freeze()

    # Setup combined spatial and spectral model
    spatial_model = NormGauss2D('spatial-model')
    spectral_model = PowLaw1D('spectral-model')
    source_model = CombinedModel3D(spatial_model=spatial_model,
                                   spectral_model=spectral_model)

    # Set starting values
    source_model.gamma = 2.2
    source_model.xpos = 83.6
    source_model.ypos = 22.01
    source_model.fwhm = 0.12
    source_model.ampl = 0.05

    model = 1E-9 * exposure * (source_model)  # 1E-9 flux factor

    # Fit
    fit = Fit(data=cube, model=model, stat=Chi2ConstVar(), method=LevMar())
    result = fit.fit()

    reference = (0.11925401159500593, 83.640630749333056, 22.020525848447541,
                 0.036353759774770608, 1.1900312815970555)

    assert_allclose(result.parvals, reference, rtol=1E-8)
Пример #3
0
def test_sherpa_crab_fit():
    from sherpa.models import NormGauss2D, PowLaw1D, TableModel, Const2D
    from sherpa.stats import Chi2ConstVar
    from sherpa.optmethods import LevMar
    from sherpa.fit import Fit
    from ..sherpa_ import CombinedModel3D

    filename = gammapy_extra.filename(
        'experiments/sherpa_cube_analysis/counts.fits.gz')
    # Note: The cube is stored in incorrect format
    counts = SkyCube.read(filename, format='fermi-counts')
    cube = counts.to_sherpa_data3d()

    # Set up exposure table model
    filename = gammapy_extra.filename(
        'experiments/sherpa_cube_analysis/exposure.fits.gz')
    exposure_data = fits.getdata(filename)
    exposure = TableModel('exposure')
    exposure.load(None, exposure_data.ravel())

    # Freeze exposure amplitude
    exposure.ampl.freeze()

    # Setup combined spatial and spectral model
    spatial_model = NormGauss2D('spatial-model')
    spectral_model = PowLaw1D('spectral-model')
    source_model = CombinedModel3D(spatial_model=spatial_model,
                                   spectral_model=spectral_model)

    # Set starting values
    source_model.gamma = 2.2
    source_model.xpos = 83.6
    source_model.ypos = 22.01
    source_model.fwhm = 0.12
    source_model.ampl = 0.05

    model = 1E-9 * exposure * source_model  # 1E-9 flux factor

    # Fit
    fit = Fit(data=cube, model=model, stat=Chi2ConstVar(), method=LevMar())
    result = fit.fit()

    reference = [0.121556, 83.625627, 22.015564, 0.096903, 2.240989]

    assert_allclose(result.parvals, reference, rtol=1E-5)
Пример #4
0
def test_chi2constvar_stat(hide_logging, reset_xspec, setup_group):
    fit = Fit(setup_group['data'], setup_group['model'], Chi2ConstVar(),
              LevMar())
    results = fit.fit()

    _fit_chi2constvar_results_bench = {
        'succeeded':
        1,
        'numpoints':
        143,
        'dof':
        140,
        'istatval':
        3903.1647954751857,
        'statval':
        140.1384389790626,
        'parvals':
        numpy.array(
            [1.8081424949164122, 5.461611041944607, -1.9077365276482876])
    }

    compare_results(_fit_chi2constvar_results_bench, results, tol=2e-4)
Пример #5
0
 def test_chi2constvar_stat(self):
     fit = Fit(self.data, self.model, Chi2ConstVar(), NelderMead())
     results = fit.fit()
     self.compare_results(self._fit_chi2constvar_results_bench, results)
Пример #6
0
def main():
    config = read_config('config.yaml')
    # ref_cube = make_ref_cube(config)
    # target_position = SkyCoord(config['model']['ra1'], config['model']['dec1'], unit="deg").galactic

    cubes = load_cubes(config)
    print('which available cubes:', cubes)

    # converting data SkyCube to sherpa-format cube
    counts = cubes['counts'].to_sherpa_data3d() #dstype='Data3DInt')
    print('counts: ', counts)
    # Define a 2D gaussian for the spatial model
    # spatial_model = NormGauss2DInt('spatial-model')

    # Define a power law for the spectral model
    # spectral_model = PowLaw1D('spectral-model')

    coord = cubes['counts'].sky_image_ref.coordinates(mode="edges")
    energies = cubes['counts'].energies(mode='edges').to("TeV")
    print('my energy bins: ', energies)
    # import IPython; IPython.embed();

    # Set up exposure table model
    exposure = TableModel('exposure')
    exposure.load(None, cubes['exposure'].data.ravel())
    exposure.ampl.freeze()

    use_psf = config['model']['use_psf']

    model_gammapy = get_model_gammapy(config)

    spectral_model_sherpa = model_gammapy.spectral_model.to_sherpa()
    spectral_model_sherpa.ampl.thaw()

    spatial_model_sherpa = model_gammapy.spatial_model.to_sherpa()
    spatial_model_sherpa.xpos.freeze()
    spatial_model_sherpa.ypos.freeze()
    spatial_model_sherpa.r0.freeze()
    spatial_model_sherpa.width.freeze()

    source_model = CombinedModel3D(
        spatial_model=spatial_model_sherpa,
        spectral_model=spectral_model_sherpa,
    )

    # source_model = CombinedModel3DInt(
    #     spatial_model=spatial_model_sherpa,
    #     spectral_model=spectral_model_sherpa,
    #     exposure=exposure,
    #     coord=coord,
    #     energies=energies,
    #     use_psf=use_psf,
    #     psf=None,
    # )

    print(source_model)
    # source_model_cube = source_model.evaluate_cube(ref_cube)
    # model = source_model  # + background_model

    # source_model2 = CombinedModel3DInt(
    #     coord=coord,
    #     energies=energies,
    #     use_psf=False,
    #     exposure=cubes['exposure'],
    #     psf=None,
    #     spatial_model=spatial_model,
    #     spectral_model=spectral_model,
    # )

    model = 1e-9 * exposure * source_model  # 1e-9 flux factor

    fit = Fit(
        data=counts,
        model=model,
        stat=Chi2ConstVar(),
        method=LevMar(),
        # estmethod=Covariance(),
    )


    fit_results = fit.fit()
    print(fit_results.format())

    print('------------------------------------ end fitting')