def mwl_fit_low_level(): """Use high-level Sherpa API. Low-level = no session, classes. Example: http://python4astronomers.github.io/fitting/low-level.html """ fermi_data = FermiData().sherpa_data hess_data = IACTData().sherpa_data # spec_model = PowLaw1D('spec_model') spec_model = LogParabola('spec_model') spec_model.c1 = 0.5 spec_model.c2 = 0.2 spec_model.ampl = 5e-11 data = DataSimulFit(name='global_data', datasets=[fermi_data, hess_data]) # TODO: Figure out how to notice using the low-level API # data.notice(mins=1e-3, maxes=None, axislist=None) model = SimulFitModel(name='global_model', parts=[spec_model, spec_model]) stat = FermiStat() method = LevMar() fit = Fit(data=data, model=model, stat=stat, method=method) result = fit.fit() # IPython.embed() return Bunch(results=result, model=spec_model)
def test(): """Check that the model parametrizations are identical now.""" amplitude = 1E-12 * u.Unit("cm-2 s-1 TeV-1") reference = 2 * u.TeV alpha = 2.3 beta = 0.1 model_gammapy = Log10Parabola(amplitude, reference, alpha, beta) print(model_gammapy) from sherpa.models import LogParabola model_sherpa = LogParabola() model_sherpa.ampl = amplitude.value model_sherpa.c1 = alpha model_sherpa.c2 = beta model_sherpa.ref = reference.value print(model_sherpa) # compare energy = 4.2 * u.TeV dnde_gammapy = model_gammapy(energy) print(dnde_gammapy) dnde_sherpa = model_sherpa(energy.value) print(dnde_sherpa) from numpy.testing import assert_allclose assert_allclose(dnde_gammapy.value, dnde_sherpa)
def mwl_fit_low_level_calling_fermi(): """Example how to do a Sherpa model fit, but use the Fermi ScienceTools to evaluate the likelihood for the Fermi dataset. """ spec_model = LogParabola('spec_model') spec_model.c1 = 0.5 spec_model.c2 = 0.2 spec_model.ampl = 5e-11 model = spec_model data = FermiDataShim() stat = FermiStatShim() method = LevMar() fit = Fit(data=data, model=model, stat=stat, method=method) result = fit.fit() return dict(results=result, model=spec_model)
def mwl_fit_low_level_calling_fermi(): """Example how to do a Sherpa model fit, but use the Fermi ScienceTools to evaluate the likelihood for the Fermi dataset. """ spec_model = LogParabola('spec_model') spec_model.c1 = 0.5 spec_model.c2 = 0.2 spec_model.ampl = 5e-11 model = spec_model data = FermiDataShim() stat = FermiStatShim() method = LevMar() fit = Fit(data=data, model=model, stat=stat, method=method) result = fit.fit() # IPython.embed() return Bunch(results=result, model=spec_model)