def test_custom_emitters(plot=True): from jetset.jet_model import Jet from jetset.jet_emitters import EmittersDistribution import numpy as np def distr_func_bkn(gamma_break, gamma, s1, s2): return np.power(gamma, -s1) * (1. + (gamma / gamma_break))**(-(s2 - s1)) n_e = EmittersDistribution('custom_bkn', spectral_type='bkn') n_e.add_par('gamma_break', par_type='turn-over-energy', val=1E3, vmin=1., vmax=None, unit='lorentz-factor') n_e.add_par('s1', par_type='LE_spectral_slope', val=2.5, vmin=-10., vmax=10, unit='') n_e.add_par('s2', par_type='LE_spectral_slope', val=3.2, vmin=-10., vmax=10, unit='') n_e.set_distr_func(distr_func_bkn) n_e.parameters.show_pars() n_e.parameters.s1.val = 2.0 n_e.parameters.s2.val = 3.5 if plot is True: n_e.plot() my_jet = Jet(emitters_distribution=n_e) my_jet.Norm_distr = True my_jet.parameters.N.val = 5E4 my_jet.eval() np.testing.assert_allclose(my_jet.emitters_distribution.eval_N(), my_jet.parameters.N.val, rtol=1E-5) print(my_jet.emitters_distribution.eval_N(), my_jet.parameters.N.val) print(n_e.eval_N(), my_jet.parameters.N.val) assert (my_jet.emitters_distribution.emitters_type == 'electrons') my_jet.save_model('test_jet_custom_emitters.pkl') my_jet = Jet.load_model('test_jet_custom_emitters.pkl') my_jet.eval()
def model_fit_minuit(sed_data, my_shape, plot=True): from jetset.minimizer import fit_SED from jetset.model_manager import FitModel from jetset.jet_model import Jet jet_minuit = Jet.load_model('prefit_jet_gal_templ.pkl') jet_minuit.set_gamma_grid_size(200) fit_model_minuit = FitModel(jet=jet_minuit, name='SSC-best-fit-minuit', template=my_shape.host_gal) fit_model_minuit.freeze('z_cosm') fit_model_minuit.freeze('R_H') fit_model_minuit.jet_leptonic.parameters.beam_obj.fit_range = [5, 50] fit_model_minuit.jet_leptonic.parameters.R.fit_range = [10**15.5, 10**17.5] fit_model_minuit.host_galaxy.parameters.nuFnu_p_host.frozen = False fit_model_minuit.host_galaxy.parameters.nu_scale.frozen = True model_minimizer_minuit, best_fit_minuit = fit_SED( fit_model_minuit, sed_data, 10.0**11, 10**29.0, fitname='SSC-best-fit-minuit', minimizer='minuit') best_fit_minuit.save_report('best-fit-minuit-report.txt') fit_model_minuit.save_model('fit_model_minuit.pkl') return jet_minuit, model_minimizer_minuit
def test_ebl_jet(plot=True,): from jetset.jet_model import Jet from jetset.template_2Dmodel import EBLAbsorptionTemplate from jetset.model_manager import FitModel my_jet = Jet(electron_distribution='lppl', name='jet_flaring') my_jet.parameters.z_cosm.val = 0.01 ebl_franceschini = EBLAbsorptionTemplate.from_name('Franceschini_2008') composite_model = FitModel(nu_size=500, name='EBL corrected') composite_model.add_component(my_jet) composite_model.add_component(ebl_franceschini) composite_model.show_pars() composite_model.link_par(par_name='z_cosm', from_model='Franceschini_2008', to_model='jet_flaring') v=0.03001 my_jet.parameters.z_cosm.val = v assert (composite_model.Franceschini_2008.parameters.z_cosm.val==v) assert (composite_model.Franceschini_2008.parameters.z_cosm.linked==True) assert (composite_model.Franceschini_2008.parameters.z_cosm.val == composite_model.jet_flaring.parameters.z_cosm.val) composite_model.composite_expr = '%s*%s'%(my_jet.name,ebl_franceschini.name) composite_model.eval() if plot is True: composite_model.plot_model() composite_model.save_model('ebl_jet.pkl') new_composite_model=FitModel.load_model('ebl_jet.pkl') v=2.0 new_composite_model.jet_flaring.parameters.z_cosm.val=v assert (new_composite_model.Franceschini_2008.parameters.z_cosm.val == v) assert (new_composite_model.Franceschini_2008.parameters.z_cosm.linked == True)
def model_fit_lsb(sed_data, my_shape, plot=True): from jetset.minimizer import fit_SED, ModelMinimizer from jetset.model_manager import FitModel from jetset.jet_model import Jet jet_lsb = Jet.load_model('prefit_jet_gal_templ.pkl') jet_lsb.set_gamma_grid_size(200) fit_model_lsb = FitModel(jet=jet_lsb, name='SSC-best-fit-lsb', template=my_shape.host_gal) fit_model_lsb.freeze('jet_leptonic', 'z_cosm') fit_model_lsb.freeze('jet_leptonic', 'R_H') fit_model_lsb.jet_leptonic.parameters.beam_obj.fit_range = [5, 50] fit_model_lsb.jet_leptonic.parameters.R.fit_range = [10**15.5, 10**17.5] fit_model_lsb.jet_leptonic.parameters.gmax.fit_range = [1E4, 1E8] fit_model_lsb.host_galaxy.parameters.nuFnu_p_host.frozen = False fit_model_lsb.host_galaxy.parameters.nu_scale.frozen = True model_minimizer_lsb, best_fit_lsb = fit_SED(fit_model_lsb, sed_data, 10.0**11, 10**29.0, fitname='SSC-best-fit-lsb', minimizer='lsb') best_fit_lsb.save_report('best-fit-minuit-report.txt') fit_model_lsb.save_model('fit_model_lsb.pkl') fit_model_lsb_new = FitModel.load_model('fit_model_lsb.pkl') model_minimizer_lsb.save_model('model_minimizer_lsb.pkl') model_minimizer_lsb_new = ModelMinimizer.load_model( 'model_minimizer_lsb.pkl') return jet_lsb, model_minimizer_lsb_new, fit_model_lsb_new
def test_dep_par_jet(plot=False): import numpy as np from jetset.jet_model import Jet j = Jet(emitters_distribution='plc') j.add_user_par(name='B0',units='G',val=1E-5,val_min=0,val_max=None) j.make_dependent_par(par='B',depends_on=['B0'],par_expr='B0*5') np.testing.assert_allclose(j.parameters.B.val, j.parameters.B0.val*5) j.save_model('test.pkl') new_j=Jet.load_model('test.pkl') new_j.parameters.B0.val=1 np.testing.assert_allclose(new_j.parameters.B.val, new_j.parameters.B0.val*5)
def custom_emitters(plot=True): from jetset.jet_model import Jet from jetset.jet_emitters import EmittersDistribution import numpy as np def distr_func_bkn(gamma_break, gamma, s1, s2): return np.power(gamma, -s1) * (1. + (gamma / gamma_break))**(-(s2 - s1)) n_e = EmittersDistribution('bkn') n_e.add_par('gamma_break', par_type='turn-over-energy', val=1E3, vmin=1., vmax=None, unit='lorentz-factor') n_e.add_par('s1', par_type='LE_spectral_slope', val=2.5, vmin=-10., vmax=10, unit='') n_e.add_par('s2', par_type='LE_spectral_slope', val=3.2, vmin=-10., vmax=10, unit='') n_e.set_distr_func(distr_func_bkn) n_e.parameters.show_pars() n_e.parameters.s1.val = 2.0 n_e.parameters.s2.val = 3.5 if plot is True: n_e.plot() my_jet = Jet(electron_distribution=n_e) my_jet.Norm_distr = True my_jet.parameters.N.val = 5E4 my_jet.eval() diff = np.fabs( np.trapz(n_e.n_gamma_e, n_e.gamma_e) - my_jet.parameters.N.val) print('diff', diff) assert (diff < 1E-3)
def test_dep_par(plot=False): from jetset.jet_emitters import EmittersDistribution import numpy as np def distr_func_bkn(gamma_break, gamma, s1, s2): return np.power(gamma, -s1) * (1. + (gamma / gamma_break)) ** (-(s2 - s1)) n_e_bkn = EmittersDistribution('bkn', spectral_type='bkn') n_e_bkn.add_par('gamma_break', par_type='turn-over-energy', val=1E3, vmin=1., vmax=None, unit='lorentz-factor') n_e_bkn.add_par('s1', par_type='LE_spectral_slope', val=2.5, vmin=-10., vmax=10, unit='') n_e_bkn.add_par('s2', par_type='LE_spectral_slope', val=3.2, vmin=-10., vmax=10, unit='') n_e_bkn.set_distr_func(distr_func_bkn) n_e_bkn.parameters.show_pars() n_e_bkn.parameters.s1.val = 2.0 n_e_bkn.parameters.s2.val = 3.5 n_e_bkn.update() n_e_bkn.parameters.show_pars() from jetset.jet_model import Jet j = Jet(emitters_distribution=n_e_bkn) # def par_func(s1): # return s1+1 j.make_dependent_par(par='s2', depends_on=['s1'], par_expr='s1+1') print('here') j.parameters.s1.val = 3 print('done') np.testing.assert_allclose(j.parameters.s2.val, j.parameters.s1.val + 1) j.save_model('jet.pkl') new_jet = Jet.load_model('jet.pkl') print('here') new_jet.parameters.s1.val = 2 print('done') np.testing.assert_allclose(new_jet.parameters.s2.val, new_jet.parameters.s1.val + 1) j.eval() new_jet.show_model()
def model_fit_lsb(sed_data, ): from jetset.minimizer import fit_SED from jetset.model_manager import FitModel from jetset.jet_model import Jet jet = Jet.load_model('prefit_jet.dat') fit_model = FitModel(jet=jet, name='SSC-best-fit', template=None) fit_model.freeze('z_cosm') fit_model.freeze('R_H') fit_model.parameters.R.fit_range = [10**15.5, 10**17.5] fit_model.parameters.gmax.fit_range = [1E4, 1E8] model_minimizer_lsb, best_fit_lsb = fit_SED(fit_model, sed_data, 10.0**11, 10**29.0, fitname='SSC-best-fit', minimizer='lsb') best_fit_lsb.save_report('best-fit-lsb-report.txt') fit_model.save_model('fit_model_lsb.dat')
def test_prepare_fit(sed_data=None, prefit_jet=None, plot=True,sed_number=1): from jetset.jet_model import Jet if sed_data is None: from .test_data import test_data_loader sed_data = test_data_loader(plot=plot, sed_number=sed_number) if prefit_jet is None: from .test_phenom_constr import test_model_constr prefit_jet, my_shape = test_model_constr(sed_data=sed_data) if hasattr(my_shape, 'host_gal'): template = my_shape.host_gal else: template = None jet = Jet.load_model('prefit_jet.pkl') jet.set_gamma_grid_size(200) return template,jet,sed_data
def test_dep_par_composite_model(plot=False): import numpy as np from jetset.model_manager import FitModel from jetset.jet_model import Jet jet = Jet(emitters_distribution='plc') fit_model = FitModel(jet=jet, name='SSC-best-fit-lsb', template=None) fit_model.jet_leptonic.parameters.beam_obj.fit_range = [5, 50] fit_model.jet_leptonic.parameters.R_H.val = 5E17 fit_model.jet_leptonic.parameters.R_H.frozen = False fit_model.jet_leptonic.parameters.R_H.fit_range = [1E15, 1E19] fit_model.jet_leptonic.parameters.R.fit_range = [10 ** 15.5, 10 ** 17.5] fit_model.jet_leptonic.add_user_par(name='B0', units='G', val=1E3, val_min=0, val_max=None) fit_model.jet_leptonic.add_user_par(name='R0', units='cm', val=5E13, val_min=0, val_max=None) fit_model.jet_leptonic.parameters.R0.frozen = True fit_model.jet_leptonic.parameters.B0.frozen = True par_expr = 'B0*(R0/R_H)' fit_model.jet_leptonic.make_dependent_par(par='B', depends_on=['B0', 'R0', 'R_H'], par_expr=par_expr) B0=fit_model.jet_leptonic.parameters.B0.val R0 = fit_model.jet_leptonic.parameters.R0.val R_H = fit_model.jet_leptonic.parameters.R_H.val np.testing.assert_allclose(fit_model.jet_leptonic.parameters.B.val, eval(par_expr)) fit_model.save_model('test_composite.pkl') new_fit_model=FitModel.load_model('test_composite.pkl') new_fit_model.jet_leptonic.parameters.B0.val=1E4 B0 = new_fit_model.jet_leptonic.parameters.B0.val R0 = new_fit_model.jet_leptonic.parameters.R0.val R_H = new_fit_model.jet_leptonic.parameters.R_H.val np.testing.assert_allclose(new_fit_model.jet_leptonic.parameters.B.val, eval(par_expr))
def test_my_foo(): from jetset.plot_sedfit import plt plt.ioff() j = Jet() j.eval() j.energetic_report()
def test_custom_emitters_array(plot=True): from jetset.jet_model import Jet from jetset.jet_emitters import EmittersArrayDistribution import numpy as np # gamma array gamma = np.logspace(1, 8, 500) # gamma array this is n(\gamma) in 1/cm^3/gamma n_gamma = gamma**-2 * 1E-5 * np.exp(-gamma / 1E5) N1 = np.trapz(n_gamma, gamma) n_distr = EmittersArrayDistribution(name='array_distr', emitters_type='protons', gamma_array=gamma, n_gamma_array=n_gamma, normalize=False) N2 = np.trapz(n_distr._array_n_gamma, n_distr._array_gamma) j = Jet(emitters_distribution=n_distr, verbose=False) j.parameters.z_cosm.val = z = 0.001 j.parameters.beam_obj.val = 1 j.parameters.N.val = 1 j.parameters.NH_pp.val = 1 j.parameters.B.val = 0.01 j.parameters.R.val = 1E18 j.set_IC_nu_size(100) j.gamma_grid_size = 200 N3 = np.trapz(j.emitters_distribution.n_gamma_p, j.emitters_distribution.gamma_p) np.testing.assert_allclose(N1, N2, rtol=1E-5) np.testing.assert_allclose(N1, N3, rtol=1E-2) np.testing.assert_allclose(N1, j.emitters_distribution.eval_N(), rtol=1E-2) assert (j.emitters_distribution.emitters_type == 'protons') j.eval() j.save_model('test_jet_custom_emitters_array.pkl') j = Jet.load_model('test_jet_custom_emitters_array.pkl') j.eval()
# generate a SSA synchrotron SED to be confronted with the one produced by agnpy from jetset.jet_model import Jet import numpy as np import astropy.units as u import matplotlib.pyplot as plt # jet with power-law electron distribution pwl_jet = Jet( name="", electron_distribution="pl", electron_distribution_log_values=False ) pwl_jet.set_nu_grid(1e9, 1e20, 50) pwl_jet.show_model() pwl_jet.eval() synch_nu = pwl_jet.spectral_components.Sync.SED.nu synch_sed = pwl_jet.spectral_components.Sync.SED.nuFnu plt.loglog(synch_nu, synch_sed) plt.ylim([1e-20, 1e-9]) plt.show() condition = synch_sed.value > 1e-20 nu = synch_nu.value[condition] sed = synch_sed.value[condition] np.savetxt("synch_ssa_pwl_jetset_1.1.2.txt", np.asarray([nu, sed]).T, delimiter=",") # jet with broken power-law electron distribution bpl_jet = Jet(name="", electron_distribution="bkn") bpl_jet.set_nu_grid(1e9, 1e20, 50) bpl_jet.show_model() bpl_jet.eval() synch_nu = bpl_jet.spectral_components.Sync.SED.nu synch_sed = bpl_jet.spectral_components.Sync.SED.nuFnu plt.loglog(synch_nu, synch_sed)
# generate a synchrotron + SSC SED to be confronted with the one produced by # agnpy and Figure 7.4 of Dermer 2009 from jetset.jet_model import Jet import numpy as np import astropy.units as u import matplotlib.pyplot as plt # jet with power-law electron distribution pwl_jet = Jet(name="", electron_distribution="pl", electron_distribution_log_values=False) # set parameters according to Figure 7.4 of Dermer 2009 pwl_jet.set_par("N", 1298.13238394) pwl_jet.set_par("p", 2.8) pwl_jet.set_par("gmin", 1e2) pwl_jet.set_par("gmax", 1e5) pwl_jet.set_par("B", 1) pwl_jet.set_par("R", 1e16) pwl_jet.set_par("beam_obj", 10) pwl_jet.set_par("z_cosm", 0.07) # remove SSA pwl_jet.spectral_components.Sync.state = "on" # synchrotron emission pwl_jet.set_nu_grid(1e9, 1e19, 100) pwl_jet.show_model() pwl_jet.eval() synch_nu = pwl_jet.spectral_components.Sync.SED.nu synch_sed = pwl_jet.spectral_components.Sync.SED.nuFnu plt.loglog(synch_nu, synch_sed)
def test_jet(plot=True): print('--------> test_jet', plot) from jetset.jet_model import Jet j = Jet() j.eval() j.energetic_report() sum1 = j.spectral_components.Sum.SED.nuFnu if plot is True: j.plot_model() j.emitters_distribution.plot() j.emitters_distribution.plot2p() j.emitters_distribution.plot3p() j.emitters_distribution.plot3p(energy_unit='eV') j.emitters_distribution.plot3p(energy_unit='erg') j.save_model('test_jet.pkl') j_new = Jet.load_model('test_jet.pkl') j_new.eval() sum2 = j_new.spectral_components.Sum.SED.nuFnu np.testing.assert_allclose(sum2, sum1, rtol=1E-5)
def test_jet(plot=True): #print('--------> plot',plot) from jetset.jet_model import Jet j = Jet() j.eval() j.energetic_report() if plot is True: j.plot_model() j.emitters_distribution.plot() j.emitters_distribution.plot2p() j.emitters_distribution.plot3p() j.emitters_distribution.plot3p(energy_unit='eV') j.emitters_distribution.plot3p(energy_unit='erg') j.save_model('test_jet.pkl') j_new = Jet.load_model('test_jet.pkl')
def test_build_bessel(): from jetset.jet_model import Jet Jet().eval()
def test_hadronic_jet(plot=True): from jetset.jet_model import Jet j = Jet(proton_distribution='plc') j.parameters.gmin.val = 2 j.parameters.gmax.val = 1E8 j.parameters.NH_pp.val = 1E10 j.parameters.N.val = 1E1 j.parameters.B.val = 80 j.parameters.p.val = 2.5 j.eval() j.show_model() sum1 = j.spectral_components.Sum.SED.nuFnu if plot is True: j.plot_model() j.save_model('test_jet_hadronic.pkl') j_new = Jet.load_model('test_jet_hadronic.pkl') j_new.eval() sum2 = j_new.spectral_components.Sum.SED.nuFnu np.testing.assert_allclose(sum2, sum1, rtol=1E-5)
def test_jet(): from jetset.jet_model import Jet j = Jet() j.eval() j.energetic_report()