def sherpa_spec_fit(in_files, model, noplot, manual, reproj, do_conf): """Reads a set of pha files and performs a maximum likelihood fit. """ import logging logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(message)s') # from sherpa.astro.ui import * # TEST if needed (only when outside sherpa env) import sherpa.astro.ui as sau from . import load_model from . import make_plot from .specsource import SpecSource logger = logging.getLogger("sherpa") logger.setLevel(logging.ERROR) # Read and load the data and model: list_data = [] emax = 9e10 # Maximum energy taken into account for the fit --> Compute properly (as a function of the max E event?) # We don't use a specific maximum reduced statistic value since we don't expect the cstat to be anywhere near the large number limit sau.set_conf_opt("max_rstat", 10000) p1 = load_model.load_model(model[0]) # [-1] #load model returns an array of model components # where the last component is the total model spec = SpecSource('SRC', in_files) if reproj == 0: myspec = spec elif reproj == 3: myspec = spec.reproject(nbins={'offset': 5, 'eff': 10, 'zen': 10}) elif reproj == 2: myspec = spec.reproject(nbins={'offset': 5, 'eff': 20, 'zen': 12}) elif reproj == 1: myspec = spec.reproject(nbins={'offset': 25, 'eff': 40, 'zen': 30}) elif reproj == 4: myspec = spec.reproject(nbins={'offset': 1, 'eff': 1, 'zen': 1}) myspec.set_source(p1) if manual: load_model.set_manual_model(sau.get_model(datid)) # Parameters for all runs are linked else: print('Using default initial values for model parameters') myspec.fit(do_conf=do_conf) if noplot: quit() make_plot.make_plot(list_data, p1) raw_input('Press <ENTER> to continue')
def set_sherpa_env(stat=None): shp.clean() dsmod.clean() shp.set_conf_opt("sigma", 1.6) shp.set_conf_opt("numcores", 4) shp.set_proj_opt("sigma", 1.6) shp.set_proj_opt("numcores", 4) if stat: shp.set_stat(stat)
def fit(self, do_covar=False, do_conf=False): """Perform fit using profile likelihood technique for background estimation and subtraction.""" listnames = self.get_noticed_list() # [ids.name for ids in self.listids[self.noticed_ids]] if len(listnames) > 0: wfit(listnames) print_fit() if do_covar is True: sau.covar(*listnames) if do_conf is True: sau.set_conf_opt('max_rstat', 10000) sau.conf(*listnames) print_conf() else: print("Empty noticed runs list. No fit")
def sherpa_spec_fit(in_files, model, noplot, manual, reproj, do_conf): """Reads a set of pha files and performs a maximum likelihood fit. """ # from sherpa.astro.ui import * # TEST if needed (only when outside sherpa env) import sherpa.astro.ui as sau from . import load_model from . import make_plot from .specsource import SpecSource # Read and load the data and model: list_data = [] emax = 9e10 # Maximum energy taken into account for the fit --> Compute properly (as a function of the max E event?) # We don't use a specific maximum reduced statistic value since we don't expect the cstat to be anywhere near the large number limit sau.set_conf_opt("max_rstat", 10000) p1 = load_model.load_model( model[0]) # [-1] #load model returns an array of model components # where the last component is the total model spec = SpecSource('SRC', in_files) if reproj == 0: myspec = spec elif reproj == 3: myspec = spec.reproject(nbins={'offset': 5, 'eff': 10, 'zen': 10}) elif reproj == 2: myspec = spec.reproject(nbins={'offset': 5, 'eff': 20, 'zen': 12}) elif reproj == 1: myspec = spec.reproject(nbins={'offset': 25, 'eff': 40, 'zen': 30}) elif reproj == 4: myspec = spec.reproject(nbins={'offset': 1, 'eff': 1, 'zen': 1}) myspec.set_source(p1) if manual: load_model.set_manual_model( sau.get_model(datid)) # Parameters for all runs are linked else: print('Using default initial values for model parameters') myspec.fit(do_conf=do_conf) if noplot: quit() make_plot.make_plot(list_data, p1) raw_input('Press <ENTER> to continue')
def run_hspec_fit(self, model, thres_low, thres_high): """Run the gammapy.hspec fit Parameters ---------- model : str Sherpa model thres_high : `~gammapy.spectrum.Energy` Upper threshold of the spectral fit thres_low : `~gammapy.spectrum.Energy` Lower threshold of the spectral fit """ log.info("Starting HSPEC") import sherpa.astro.ui as sau from ..hspec import wstat from sherpa.models import PowLaw1D if model == 'PL': p1 = PowLaw1D('p1') p1.gamma = 2.2 p1.ref = 1e9 p1.ampl = 6e-19 else: raise ValueError('Desired Model is not defined') thres = thres_low.to('keV').value emax = thres_high.to('keV').value sau.freeze(p1.ref) sau.set_conf_opt("max_rstat", 100) list_data = [] for obs in self.observations: datid = obs.phafile.parts[-1][7:12] sau.load_data(datid, str(obs.phafile)) sau.notice_id(datid, thres, emax) sau.set_source(datid, p1) list_data.append(datid) wstat.wfit(list_data) sau.covar() fit_val = sau.get_covar_results() fit_attrs = ('parnames', 'parvals', 'parmins', 'parmaxes') fit = dict((attr, getattr(fit_val, attr)) for attr in fit_attrs) fit = self.apply_containment(fit) sau.clean() self.fit = fit
def wfit(dataids=None): listids = () if dataids is None: listids = sau.list_data_ids() else: listids = dataids wstat = w_statistic(listids) sau.load_user_stat("mystat", wstat, wstat.CATstat_err_LV) sau.set_stat(mystat) sau.set_method("neldermead") # set_method("moncar") sau.set_conf_opt("max_rstat", 1000) # We don't use a specific maximum reduced statistic value # since we don't expect the cstat to be anywhere near the # large number limit sau.fit(*listids) sau.conf()
def wfit(dataids=None): listids = () if dataids is None: listids = sau.list_data_ids() else: listids = dataids wstat = w_statistic(listids) sau.load_user_stat("mystat", wstat, wstat.CATstat_err_LV) sau.set_stat(mystat) sau.set_method("neldermead") # set_method("moncar") sau.set_conf_opt( "max_rstat", 1000) # We don't use a specific maximum reduced statistic value # since we don't expect the cstat to be anywhere near the # large number limit sau.fit(*listids) sau.conf()
def _run_hspec_fit(self): """Run the gammapy.hspec fit """ log.info("Starting HSPEC") import sherpa.astro.ui as sau from ..hspec import wstat sau.set_conf_opt("max_rstat", 100) thres_lo = self.energy_threshold_low.to('keV').value thres_hi = self.energy_threshold_high.to('keV').value sau.freeze(self.model.ref) list_data = [] for pha in self.pha: datid = pha.parts[-1][7:12] sau.load_data(datid, str(pha)) sau.notice_id(datid, thres_lo, thres_hi) sau.set_source(datid, self.model) list_data.append(datid) wstat.wfit(list_data)
# 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()) shp.ignore2d('circle(500,500,1000)') shp.notice2d('circle(500,500,60)') shp.ignore2d('circle(500,500,' + str(r) + ')') shp.fit() print(shp.get_model()) shp.set_conf_opt('numcores', 3) shp.set_conf_opt('maxiters', 50) shp.set_conf_opt('fast', True) shp.set_conf_opt('remin', 10000.0) shp.set_conf_opt('soft_limits', True) shp.freeze(chxe.xpos, chxe.ypos) #shp.conf(chxe.ellip, chxe.fwhm) utils.save_components('40-50 keV/chxe' + str(int(r * 2.5)) + '.model', ['chxe']) shp.save('40-50 keV/fits/chxe150/chxe' + str(int(r * 2.5)) + 'fit.sav')