model = EBL_transmitance.transmission(data) * IC.flux( data, distance=1.0 * u.kpc) + SYN.flux(data, distance=1.0 * u.kpc) # The first array returned will be compared to the observed spectrum for # fitting. All subsequent objects will be stored in the sampler metadata # blobs. return model, IC.compute_We(Eemin=1 * u.TeV) if __name__ == "__main__": # Some random values for a "beautiful double peak structure p0 = np.array((31.0, 1.0, 0.35, 1.5, 2.3, 0.06)) labels = [ "log10(norm)", "log10(Energy_Break)", "index1", "index2", "B", "redshift", ] # Run interactive fitter, to show the very high energy absorption imf = naima.InteractiveModelFitter( ElectronEblAbsorbedSynIC, p0, labels=labels, e_range=[1e-09 * u.GeV, 1e05 * u.GeV], )
def fitter(self, p0, labels, datatable): ''' This is the actual fitting function. Parameters: ------------ p0: list free parameters; 1st guess (compact using InteractiveModelFitter) labels: list names of the free parameters data_table: astropy Table list of data tables for fitting Results of the fit (an astropy table with best param estimate and uncertainties & the sed fit) are stored inside 'results_ssc_fit' ''' print("Executing the fit...") # An interactive window helps to adjust the starting point of sampling # before calling run_sampler. imf = naima.InteractiveModelFitter(self.model_func, p0, sed=True, e_range=[1e-3 * u.eV, 1e15 * u.eV], e_npoints=self.e_npoints, labels=labels) p0 = imf.pars nwalkers = 100 nparams = len(p0) if nparams > nwalkers: raise RandomWalkError("The number of walkers should be atleast" "greater than the number of parameters!!") # Numbers for nwalkers, nburn, nrun are only preliminary here # to achieve fast computation. sampler, pos = naima.run_sampler(data_table=datatable, p0=p0, labels=labels, model=self.model_func, prior=self.prior_func, nwalkers=nwalkers, nburn=50, nrun=100, threads=12, prefit=False, data_sed=True, interactive=False) naima.save_results_table('./results_ssc_fit/data_fit_table', sampler) fit_sed = naima.plot_fit(sampler, n_samples=50, e_range=[1e-3 * u.eV, 1e15 * u.eV], e_npoints=self.e_npoints) fit_sed.savefig("./results_ssc_fit/likelihoodfitresult_sed.png") labels = sampler.labels for index, name in enumerate(labels): chain = naima.plot_chain(sampler, p=index) chain.savefig( "./results_ssc_fit/plot_chain/{}_chain.png".format(name)) corner_plt = naima.plot_corner(sampler, show_ML=True) corner_plt.savefig("./results_ssc_fit/corner_plots.png")