.. _sherpa_plugin: Example to use the Sherpa plugin with the sherpa interface ========================================================== In this tutorial we show how to import a jetset model into Sherpa, and finally we perform a model fitting with Sherpa. To run this plugin you have to install Sherpa: https://sherpa.readthedocs.io/en/latest/install.html .. code:: ipython3 import warnings warnings.filterwarnings('ignore') import matplotlib.pylab as plt import jetset from jetset.test_data_helper import test_SEDs from jetset.data_loader import ObsData,Data from jetset.plot_sedfit import PlotSED from jetset.test_data_helper import test_SEDs .. code:: ipython3 test_SEDs .. parsed-literal:: ['/Users/orion/anaconda3/envs/jetset/lib/python3.8/site-packages/jetset/test_data/SEDs_data/SED_3C345.ecsv', '/Users/orion/anaconda3/envs/jetset/lib/python3.8/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk421_EBL_DEABS.ecsv', '/Users/orion/anaconda3/envs/jetset/lib/python3.8/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk501_EBL_ABS.ecsv', '/Users/orion/anaconda3/envs/jetset/lib/python3.8/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk501_EBL_DEABS.ecsv'] Loading data ------------ see the :ref:`data_format` user guide for further information about loading data .. code:: ipython3 print(test_SEDs[2]) data=Data.from_file(test_SEDs[2]) .. parsed-literal:: /Users/orion/anaconda3/envs/jetset/lib/python3.8/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk501_EBL_ABS.ecsv .. code:: ipython3 %matplotlib inline sed_data=ObsData(data_table=data) sed_data.group_data(bin_width=0.2) sed_data.add_systematics(0.1,[10.**6,10.**29]) p=sed_data.plot_sed() .. parsed-literal:: ================================================================================ *** binning data *** ---> N bins= 90 ---> bin_widht= 0.2 ================================================================================ .. image:: sherpa-plugin-sherpa-interface_files/sherpa-plugin-sherpa-interface_8_1.png .. code:: ipython3 sed_data.save('Mrk_501.pkl') phenomenological model constraining ----------------------------------- see the :ref:`phenom_constr` user guide for further information about phenomenological constraining spectral indices ~~~~~~~~~~~~~~~~ .. code:: ipython3 from jetset.sed_shaper import SEDShape my_shape=SEDShape(sed_data) my_shape.eval_indices(minimizer='lsb',silent=True) p=my_shape.plot_indices() p.setlim(y_min=1E-15,y_max=1E-6) .. parsed-literal:: ================================================================================ *** evaluating spectral indices for data *** ================================================================================ .. image:: sherpa-plugin-sherpa-interface_files/sherpa-plugin-sherpa-interface_13_1.png sed shaper ~~~~~~~~~~ .. code:: ipython3 mm,best_fit=my_shape.sync_fit(check_host_gal_template=True, Ep_start=None, minimizer='lsb', silent=True, fit_range=[10.,21.]) .. parsed-literal:: ================================================================================ *** Log-Polynomial fitting of the synchrotron component *** ---> first blind fit run, fit range: [10.0, 21.0] ---> class: HSP ---> class: HSP .. raw:: html Table length=6
model namenamevalbestfit valerr +err -start valfit range minfit range maxfrozen
LogCubicb-6.411143e-02-6.411143e-027.838958e-03---4.778764e-02-1.000000e+010.000000e+00False
LogCubicc-1.751705e-03-1.751705e-031.127020e-03--3.576201e-03-1.000000e+011.000000e+01False
LogCubicEp1.703747e+011.703747e+019.437333e-02--1.626870e+010.000000e+003.000000e+01False
LogCubicSp-1.030068e+01-1.030068e+011.884116e-02---1.025412e+01-3.000000e+010.000000e+00False
host_galaxynuFnu_p_host-1.006556e+01-1.006556e+015.462500e-02---1.025412e+01-1.225412e+01-8.254123e+00False
host_galaxynu_scale1.730750e-021.730750e-023.694838e-03--0.000000e+00-5.000000e-015.000000e-01False
.. parsed-literal:: ---> sync nu_p=+1.703747e+01 (err=+9.437333e-02) nuFnu_p=-1.030068e+01 (err=+1.884116e-02) curv.=-6.411143e-02 (err=+7.838958e-03) ================================================================================ .. code:: ipython3 my_shape.IC_fit(fit_range=[23.,29.],minimizer='minuit',silent=True) p=my_shape.plot_shape_fit() p.setlim(y_min=1E-15) .. parsed-literal:: ================================================================================ *** Log-Polynomial fitting of the IC component *** ---> fit range: [23.0, 29.0] ---> LogCubic fit .. raw:: html Table length=4
model namenamevalbestfit valerr +err -start valfit range minfit range maxfrozen
LogCubicb-1.565399e-01-1.565399e-012.551779e-02---1.000000e+00-1.000000e+010.000000e+00False
LogCubicc-4.351917e-02-4.351917e-022.032066e-02---1.000000e+00-1.000000e+011.000000e+01False
LogCubicEp2.529709e+012.529709e+011.817241e-01--2.536916e+010.000000e+003.000000e+01False
LogCubicSp-1.058825e+01-1.058825e+015.046950e-02---1.000000e+01-3.000000e+010.000000e+00False
.. parsed-literal:: ---> IC nu_p=+2.529709e+01 (err=+1.817241e-01) nuFnu_p=-1.058825e+01 (err=+5.046950e-02) curv.=-1.565399e-01 (err=+2.551779e-02) ================================================================================ .. image:: sherpa-plugin-sherpa-interface_files/sherpa-plugin-sherpa-interface_16_3.png Model constraining ~~~~~~~~~~~~~~~~~~ In this step we are not fitting the model, we are just obtaining the phenomenological ``pre_fit`` model, that will be fitted in using minuit ore least-square bound, as shown below .. code:: ipython3 from jetset.obs_constrain import ObsConstrain from jetset.model_manager import FitModel sed_obspar=ObsConstrain(beaming=25, B_range=[0.001,0.1], distr_e='lppl', t_var_sec=3*86400, nu_cut_IR=1E12, SEDShape=my_shape) prefit_jet=sed_obspar.constrain_SSC_model(electron_distribution_log_values=False,silent=True) prefit_jet.save_model('prefit_jet.pkl') .. parsed-literal:: ================================================================================ *** constrains parameters from observable *** .. raw:: html Table length=12
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
jet_leptonicRregion_sizecm1.056958e+161.000000e+031.000000e+30FalseFalse
jet_leptonicR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
jet_leptonicBmagnetic_fieldgauss5.050000e-020.000000e+00--FalseFalse
jet_leptonicNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e-010.000000e+00--FalseTrue
jet_leptonicbeam_objbeaminglorentz-factor*2.500000e+011.000000e-04--FalseFalse
jet_leptonicz_cosmredshift3.360000e-020.000000e+00--FalseFalse
jet_leptonicgminlow-energy-cut-offlorentz-factor*4.703917e+021.000000e+001.000000e+09FalseFalse
jet_leptonicgmaxhigh-energy-cut-offlorentz-factor*2.310708e+061.000000e+001.000000e+15FalseFalse
jet_leptonicNemitters_density1 / cm37.087120e+000.000000e+00--FalseFalse
jet_leptonicgamma0_log_parabturn-over-energylorentz-factor*1.045836e+041.000000e+001.000000e+09FalseFalse
jet_leptonicsLE_spectral_slope2.248787e+00-1.000000e+011.000000e+01FalseFalse
jet_leptonicrspectral_curvature3.205571e-01-1.500000e+011.500000e+01FalseFalse
.. parsed-literal:: ================================================================================ .. code:: ipython3 pl=prefit_jet.plot_model(sed_data=sed_data) pl.add_model_residual_plot(prefit_jet,sed_data) pl.setlim(y_min=1E-15,x_min=1E7,x_max=1E29) .. image:: sherpa-plugin-sherpa-interface_files/sherpa-plugin-sherpa-interface_20_0.png Model fitting with using a Sherpa model --------------------------------------- we show now, how to import a jetset model into a Sherpa model .. code:: ipython3 from jetset.sherpa_plugin import JetsetSherpaModel .. code:: ipython3 from jetset.template_2Dmodel import EBLAbsorptionTemplate ebl_franceschini=EBLAbsorptionTemplate.from_name('Franceschini_2008') .. code:: ipython3 from jetset.jet_model import Jet prefit_jet=Jet.load_model('prefit_jet.pkl') .. raw:: html Table length=12
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
jet_leptonicgminlow-energy-cut-offlorentz-factor*4.703917e+021.000000e+001.000000e+09FalseFalse
jet_leptonicgmaxhigh-energy-cut-offlorentz-factor*2.310708e+061.000000e+001.000000e+15FalseFalse
jet_leptonicNemitters_density1 / cm37.087120e+000.000000e+00--FalseFalse
jet_leptonicgamma0_log_parabturn-over-energylorentz-factor*1.045836e+041.000000e+001.000000e+09FalseFalse
jet_leptonicsLE_spectral_slope2.248787e+00-1.000000e+011.000000e+01FalseFalse
jet_leptonicrspectral_curvature3.205571e-01-1.500000e+011.500000e+01FalseFalse
jet_leptonicRregion_sizecm1.056958e+161.000000e+031.000000e+30FalseFalse
jet_leptonicR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
jet_leptonicBmagnetic_fieldgauss5.050000e-020.000000e+00--FalseFalse
jet_leptonicNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e-010.000000e+00--FalseTrue
jet_leptonicbeam_objbeaminglorentz-factor*2.500000e+011.000000e-04--FalseFalse
jet_leptonicz_cosmredshift3.360000e-020.000000e+00--FalseFalse
We remove the paramter ``NH_cold_to_rel_e``, not used in the fit, because of problem encountered with the ``IntervalProjection`` Sherpa method .. code:: ipython3 p=prefit_jet.parameters.get_par_by_name('NH_cold_to_rel_e') prefit_jet.parameters.del_par(p) The following instructions create a Sherpa model for each of the existing jetset models. .. code:: ipython3 sherpa_model_jet=JetsetSherpaModel(prefit_jet) sherpa_model_gal=JetsetSherpaModel(my_shape.host_gal) sherpa_model_ebl=JetsetSherpaModel(ebl_franceschini) .. parsed-literal:: jetset model name R renamed to R_sh due to sherpa internal naming convention .. code:: ipython3 sherpa_model=(sherpa_model_jet+sherpa_model_gal)*sherpa_model_ebl .. code:: ipython3 sherpa_model .. raw:: html
<BinaryOpModel model instance '((jet_leptonic + host_galaxy) * Franceschini_2008)'>
.. code:: ipython3 sherpa_model_ebl.z_cosm = sherpa_model_jet.z_cosm .. code:: ipython3 sherpa_model .. raw:: html
<BinaryOpModel model instance '((jet_leptonic + host_galaxy) * Franceschini_2008)'>
.. code:: ipython3 sherpa_model_jet.R_H.freeze() sherpa_model_jet.z_cosm.freeze() sherpa_model_gal.nu_scale.freeze() .. code:: ipython3 sherpa_model_jet.beam_obj.min = 5 sherpa_model_jet.beam_obj.max = 50. sherpa_model_jet.R_sh.min = 10**15. sherpa_model_jet.R_sh.max = 10**17.5 sherpa_model_jet.gmax.min = 1E5 sherpa_model_jet.gmax.max = 1E7 .. code:: ipython3 sherpa_model .. raw:: html
<BinaryOpModel model instance '((jet_leptonic + host_galaxy) * Franceschini_2008)'>
.. code:: ipython3 from sherpa import data from sherpa.fit import Fit from sherpa.stats import Chi2 from sherpa.optmethods import LevMar, NelderMead .. code:: ipython3 sherpa_data=data.Data1D("sed", sed_data.table['nu_data'], sed_data.table['nuFnu_data'], staterror=sed_data.table['dnuFnu_data']) .. code:: ipython3 fitter = Fit(sherpa_data, sherpa_model, stat=Chi2(), method=LevMar()) fit_range=[1e11,1e29] sherpa_data.notice(fit_range[0], fit_range[1]) .. code:: ipython3 results = fitter.fit() .. code:: ipython3 print("fit succeeded", results.succeeded) .. parsed-literal:: fit succeeded True .. code:: ipython3 results .. raw:: html
<Fit results instance>
.. code:: ipython3 sherpa_model .. raw:: html
<BinaryOpModel model instance '((jet_leptonic + host_galaxy) * Franceschini_2008)'>
.. code:: ipython3 from jetset.sherpa_plugin import plot_sherpa_model .. code:: ipython3 p=plot_sherpa_model(sherpa_model_jet,label='SSC',line_style='--') p=plot_sherpa_model(sherpa_model_gal,plot_obj=p,label='host gal',line_style='--') p=plot_sherpa_model(sherpa_model=sherpa_model,plot_obj=p,sed_data=sed_data,fit_range=fit_range,add_res=True,label='(SSC+host gal)*ebl') .. image:: sherpa-plugin-sherpa-interface_files/sherpa-plugin-sherpa-interface_47_0.png You can access all the sherpa fetarues https://sherpa.readthedocs.io/en/latest/fit/index.html .. code:: ipython3 from sherpa.plot import IntervalProjection iproj = IntervalProjection() iproj.prepare(fac=5, nloop=15) iproj.calc(fitter, sherpa_model_jet.s) iproj.plot() .. parsed-literal:: WARNING: hard minimum hit for parameter jet_leptonic.gmin WARNING: hard maximum hit for parameter jet_leptonic.gmin WARNING: hard minimum hit for parameter jet_leptonic.gmax WARNING: hard maximum hit for parameter jet_leptonic.gmax WARNING: hard minimum hit for parameter jet_leptonic.N WARNING: hard maximum hit for parameter jet_leptonic.N WARNING: hard minimum hit for parameter jet_leptonic.R_sh WARNING: hard maximum hit for parameter jet_leptonic.R_sh WARNING: hard minimum hit for parameter jet_leptonic.B WARNING: hard maximum hit for parameter jet_leptonic.B WARNING: hard minimum hit for parameter jet_leptonic.beam_obj WARNING: hard maximum hit for parameter jet_leptonic.beam_obj .. image:: sherpa-plugin-sherpa-interface_files/sherpa-plugin-sherpa-interface_49_1.png