Example #1
0
    def test_ARFModelPHA(self):
        from sherpa.astro import ui
        ui.load_pha(self.make_path("3c120_meg_1.pha"))

        # remove the RMF to ensure this is an ARF-only analysis
        # (which is what is needed to trigger the bug that lead to #699)
        ui.get_data().set_rmf(None)

        ui.group_counts(20)
        ui.notice(0.5, 6)
        ui.subtract()
        ui.set_model(ui.xsphabs.abs1 * (ui.xsapec.bubble + ui.powlaw1d.p1))
        ui.set_xsabund('angr')
        ui.set_xsxsect('vern')
        abs1.nh = 0.163
        abs1.nh.freeze()
        p1.ampl = 0.017
        p1.gamma = 1.9
        bubble.kt = 0.5
        bubble.norm = 4.2e-5
        tol = 1.0e-2
        ui.set_method_opt('ftol', tol)
        ui.fit()
        result = ui.get_fit_results()
        assert result.numpoints == self._fit_using_ARFModelPHA['numpoints']
        assert result.dof == self._fit_using_ARFModelPHA['dof']
Example #2
0
# Set up the model and load it as a Sherpa model
def boxbod_func(pars, x):
    b1, b2 = pars
    return BoxBOD.model(x, b1, b2)

ui.load_user_model(boxbod_func, "boxbod")
ui.add_user_pars("boxbod", ["b1", "b2"])
bb = boxbod
ui.set_model(bb)
bb.b1, bb.b2 = BoxBOD.p0[0], BoxBOD.p0[1]

# Perform fit
ui.set_stat('chi2datavar')
#ui.set_method('levmar') #  ['levmar', 'moncar', 'neldermead', 'simplex']
ui.set_method_opt('xtol', 1e-10)
ui.fit() # Compute best-fit parameters
ui.set_covar_opt('eps', 1e-5) # @todo: Why does this parameter have no effect
ui.covariance() # Compute covariance matrix (i.e. errors)
#ui.conf() # Compute profile errors
#ui.show_all() # Print a very nice summary of your session to less

# Report results
fr = ui.get_fit_results()
cr = ui.get_covar_results()

# Report results (we have to apply the s factor ourselves)
popt = np.array(fr.parvals)
chi2 = fr.statval
s_factor = np.sqrt(chi2 / fr.dof)
perr = s_factor * np.array(cr.parmaxes)
Example #3
0
# pure python verstion of single_gaussian_component_model.ipynb because this is faster
import sherpa.astro.ui as shp
import utils

shp.set_stat('cash')
shp.set_method('simplex')
shp.set_method_opt('verbose', 1)

utils.setup('numosaic_fin_combined_40-50keV.img')

utils.load_components('40-50 keV/bg 150-212.5.model')

# CHXE model
shp.set_full_model(psf(shp.gauss2d.chxe * emap) + bg * emap)

# inner radii chosen
for r in [20, 24, 28, 32]:
    # from KP15, (ra, dec) = (266.4172 deg, -29.00716 deg)
    # systematic shifting error = 6" = 2.4 px
    # ellip = .52
    # theta = 57 deg (from positive north)
    shp.set_par(chxe.xpos, 497.4, 497.4 - 4, 497.4 + 4)
    shp.set_par(chxe.ypos, 499.1, 499.1 - 4, 499.1 + 4)
    shp.set_par(chxe.fwhm, 30, 1, 200)
    shp.set_par(chxe.ellip, .5)
    shp.set_par(chxe.theta, -.9948)
    shp.set_par(chxe.ampl, 1e-5)
    shp.thaw(chxe.ellip, chxe.xpos, chxe.ypos)

    print(shp.get_model())
Example #4
0
try:
    import matplotlib.pyplot as plt
    has_mpl = True
except ImportError:
    has_mpl = False

class NoPlottingSystemError(Exception):
    pass


import filili
import filili.multilist

#ui.set_stat('cstat')
ui.set_method_opt('epsfcn', 2.2e-16)


#ui.set_model('gauss1d.baseline')
#baseline = ui.get_model_component('baseline')
#baseline.ampl.min = 0.
#baseline.fwhm.max = .2
#baseline.fwhm.min = .01
#baseline.fwhm = .01
#baseline.fwhm.freeze()


def fit_lines(linelist, id = None, delta_lam = .2, plot = False, outfile = None):
    mymodel = filili.multilinemanager.GaussLines('const1d', id = id, baseline = baseline)
    linelist['fililiname'] = [''] * len(linelist['linename'])
    for i in range(len(linelist['linename'])):
Example #5
0
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Compute results with Sherpa"""
from __future__ import print_function, division
# __doctest_skip__
__doctest_skip__ = ['*']
import numpy as np
import sherpa.astro.ui as sau

sau.load_data('counts.fits.gz')
sau.set_source('normgauss2d.source + const2d.background')
sau.set_stat('cstat')
# Ask for high-precision results
sau.set_method_opt('ftol', 1e-20)
sau.set_covar_opt('eps', 1e-20)

# Set start parameters close to simulation values to make the fit converge
sau.set_par('source.xpos', 101)
sau.set_par('source.ypos', 101)
sau.set_par('source.ampl', 1.1e3)
sau.set_par('source.fwhm', 10)
sau.set_par('background.c0', 1.1)

# Run fit and covariance estimation
# Results are automatically printed to the screen
sau.fit()
sau.covar()

# Sherpa uses fwhm instead of sigma as extension parameter ... need to convert
# http://cxc.harvard.edu/sherpa/ahelp/gauss2d.html
fwhm_to_sigma = 1. / np.sqrt(8 * np.log(2))
cov = sau.get_covar_results()
sh.set_stat("cash")
sh.set_method("simplex")

sh.load_image("analysis_3d/counts_2D.fits")
sh.set_coord("logical")

sh.load_table_model("expo", "analysis_3d/exposure_2D.fits")
sh.load_table_model("bkg", "analysis_3d/background_2D.fits")
sh.load_psf("psf", "analysis_3d/psf_2D.fits")

# To speed up this tutorial, we change the fit optimazation method to Levenberg-Marquardt and fix a required tolerance. This can make the fitting less robust, and in practise, you can skip this step unless you understand what is going on.

# In[ ]:

sh.set_method("levmar")
sh.set_method_opt("xtol", 1e-5)
sh.set_method_opt("ftol", 1e-5)
sh.set_method_opt("gtol", 1e-5)
sh.set_method_opt("epsfcn", 1e-5)

# In[ ]:

print(sh.get_method())

# In principle one might first want to fit the background amplitude. However the background estimation method already yields the correct normalization, so we freeze the background amplitude to unity instead of adjusting it. The (smoothed) residuals from this background model are then computed and shown.

# In[ ]:

sh.set_full_model(bkg)
bkg.ampl = 1
sh.freeze(bkg)
Example #7
0
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Compute results with Sherpa"""

import numpy as np
import sherpa.astro.ui as sau

sau.load_data('counts.fits.gz')
sau.set_source('normgauss2d.source + const2d.background')
sau.set_stat('cstat')
# Ask for high-precision results
sau.set_method_opt('ftol', 1e-20)
sau.set_covar_opt('eps', 1e-20)

# Set start parameters close to simulation values to make the fit converge
sau.set_par('source.xpos', 101)
sau.set_par('source.ypos', 101)
sau.set_par('source.ampl', 1.1e3)
sau.set_par('source.fwhm', 10)
sau.set_par('background.c0', 1.1)

# Run fit and covariance estimation
# Results are automatically printed to the screen
sau.fit()
sau.covar()

# Sherpa uses fwhm instead of sigma as extension parameter ... need to convert
# http://cxc.harvard.edu/sherpa/ahelp/gauss2d.html
fwhm_to_sigma = 1. / np.sqrt(8 * np.log(2))
cov = sau.get_covar_results()
sigma = fwhm_to_sigma * cov.parvals[0]
sigma_err = fwhm_to_sigma * cov.parmaxes[0]
Example #8
0
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Compute results with Sherpa"""
from __future__ import print_function, division
import numpy as np
import sherpa.astro.ui as sau

sau.load_data("counts.fits.gz")
sau.set_source("normgauss2d.source + const2d.background")
sau.set_stat("cstat")
# Ask for high-precision results
sau.set_method_opt("ftol", 1e-20)
sau.set_covar_opt("eps", 1e-20)

# Set start parameters close to simulation values to make the fit converge
sau.set_par("source.xpos", 101)
sau.set_par("source.ypos", 101)
sau.set_par("source.ampl", 1.1e3)
sau.set_par("source.fwhm", 10)
sau.set_par("background.c0", 1.1)

# Run fit and covariance estimation
# Results are automatically printed to the screen
sau.fit()
sau.covar()

# Sherpa uses fwhm instead of sigma as extension parameter ... need to convert
# http://cxc.harvard.edu/sherpa/ahelp/gauss2d.html
fwhm_to_sigma = 1.0 / np.sqrt(8 * np.log(2))
cov = sau.get_covar_results()
sigma = fwhm_to_sigma * cov.parvals[0]
sigma_err = fwhm_to_sigma * cov.parmaxes[0]