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
Example #2
0
def test_model_fit(sed_data=None, prefit_jet=None, plot=True,sed_number=1,minimizer='lsb'):
    from jetset.minimizer import fit_SED,ModelMinimizer
    from jetset.model_manager import FitModel

    template, jet,sed_data=test_prepare_fit(sed_data=sed_data,prefit_jet=prefit_jet,plot=plot,sed_number=sed_number)

    fit_model = FitModel(jet=jet, name='SSC-best-fit-minuit', template=template)
    fit_model.freeze('jet_leptonic','z_cosm')
    fit_model.freeze('jet_leptonic','R_H')
    fit_model.jet_leptonic.parameters.beam_obj.fit_range = [5, 50]
    fit_model.jet_leptonic.parameters.R.fit_range = [10 ** 15.5, 10 ** 17.5]
    if minimizer == 'minuit':
        fit_model.jet_leptonic.parameters.gmin.fit_range = [2, 200]
        fit_model.jet_leptonic.parameters.gmax.fit_range = [1E5, 1E7]
        fit_model.jet_leptonic.parameters.B.fit_range = [1E-3,2]

    if template is not None:
        fit_model.host_galaxy.parameters.nuFnu_p_host.frozen = False
        fit_model.host_galaxy.parameters.nu_scale.frozen = True

    model_minimizer = ModelMinimizer(minimizer)
    best_fit = model_minimizer.fit(fit_model, sed_data, 10 ** 11., 10 ** 29.0,
                                                 fitname='SSC-best-fit-minuit', repeat=1)
    best_fit.show_report()
    best_fit.save_report('best-fit-%s-report.pkl'%minimizer)
    best_fit.bestfit_table.write('best-fit-%s-report.ecsv'%minimizer)
    model_minimizer.save_model('model_minimizer_%s.pkl'%minimizer)

    model_minimizer = ModelMinimizer.load_model('model_minimizer_%s.pkl'%minimizer)
    fit_model.save_model('fit_model_%s.pkl'%minimizer)

    return fit_model, model_minimizer,sed_data
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_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))
Example #5
0
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')
Example #6
0
def test_model_fit_dep_pars(sed_data=None, prefit_jet=None, plot=True,sed_number=1,minimizer='lsb'):
    from jetset.minimizer import fit_SED,ModelMinimizer
    from jetset.model_manager import FitModel

    template, jet,sed_data=test_prepare_fit(sed_data=sed_data,prefit_jet=prefit_jet,plot=plot,sed_number=sed_number)

    fit_model = FitModel(jet=jet, name='SSC-best-fit-minuit', template=template)
    fit_model.freeze('jet_leptonic','z_cosm')
    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

    fit_model.jet_leptonic.make_dependent_par(par='B', depends_on=['B0', 'R0', 'R_H'], par_expr='B0*(R0/R_H)')

    if minimizer == 'minuit':
        fit_model.jet_leptonic.parameters.gmin.fit_range = [2, 200]
        fit_model.jet_leptonic.parameters.gmax.fit_range = [1E5, 1E7]

    if template is not None:
        fit_model.host_galaxy.parameters.nuFnu_p_host.frozen = False
        fit_model.host_galaxy.parameters.nu_scale.frozen = True

    model_minimizer = ModelMinimizer(minimizer)
    best_fit = model_minimizer.fit(fit_model, sed_data, 10 ** 11., 10 ** 29.0,
                                                 fitname='SSC-best-fit-minuit', repeat=1)
    best_fit.show_report()
    best_fit.save_report('best-fit-%s-report.pkl'%minimizer)
    best_fit.bestfit_table.write('best-fit-%s-report.ecsv'%minimizer)
    model_minimizer.save_model('model_minimizer_%s.pkl'%minimizer)

    model_minimizer = ModelMinimizer.load_model('model_minimizer_%s.pkl'%minimizer)
    fit_model.save_model('fit_model_%s.pkl'%minimizer)

    return fit_model, model_minimizer,sed_data
Example #7
0
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)
Example #8
0
def test_ebl_jet_fit(plot=True,sed_number=2,minimizer='lsb'):
    from .test_model_fit import test_prepare_fit

    template, jet,sed_data = test_prepare_fit(sed_number=sed_number)

    from jetset.template_2Dmodel import EBLAbsorptionTemplate
    ebl_franceschini = EBLAbsorptionTemplate.from_name('Franceschini_2008')
    ebl_franceschini.show_model()

    from jetset.model_manager import FitModel
    composite_model = FitModel(nu_size=500, name='EBL corrected', template=template)
    composite_model.add_component(jet)
    composite_model.add_component(ebl_franceschini)
    composite_model.link_par(par_name='z_cosm', from_model='Franceschini_2008', to_model=jet.name)

    if template is not None:
        composite_model.composite_expr = '(%s+host_galaxy)*Franceschini_2008'%jet.name
    else:
        composite_model.composite_expr = '(%s)*Franceschini_2008' % jet.name

    assert (composite_model.Franceschini_2008.parameters.z_cosm.val == composite_model.jet_leptonic.parameters.z_cosm.val)
    assert (composite_model.Franceschini_2008.parameters.z_cosm.linked is True)

    composite_model.show_model()
    composite_model.eval()

    if plot is True:
        composite_model.plot_model()

    from jetset.minimizer import ModelMinimizer

    composite_model.freeze(jet, 'R_H')
    composite_model.freeze(jet, 'z_cosm')

    if minimizer == 'minuit':
        composite_model.jet_leptonic.parameters.gmin.fit_range = [2, 200]
        composite_model.jet_leptonic.parameters.gmax.fit_range = [1E5, 1E7]

    if template is not None:
        composite_model.host_galaxy.parameters.nuFnu_p_host.frozen = False
        composite_model.host_galaxy.parameters.nu_scale.frozen = True

    composite_model.jet_leptonic.parameters.beam_obj.fit_range = [5, 50]
    composite_model.jet_leptonic.parameters.R.fit_range = [10 ** 15.5, 10 ** 17.5]
    composite_model.jet_leptonic.parameters.gmax.fit_range = [1E4, 1E8]


    composite_model.jet_leptonic.nu_size = 200
    composite_model.jet_leptonic.IC_nu_size = 100

    model_minimizer = ModelMinimizer(minimizer)
    best_fit = model_minimizer.fit(composite_model, sed_data, 10 ** 11., 10 ** 29.0,
                                   fitname='SSC-best-fit-minuit', repeat=1)
    best_fit.show_report()
    best_fit.save_report('best-fit-%s-report.pkl' % minimizer)
    best_fit.bestfit_table.write('best-fit-%s-report.ecsv' % minimizer)
    model_minimizer.save_model('model_minimizer_%s.pkl' % minimizer)

    model_minimizer = ModelMinimizer.load_model('model_minimizer_%s.pkl' % minimizer)
    composite_model.save_model('fit_model_%s.pkl' % minimizer)