Ejemplo n.º 1
0
        results.write(f, format='ascii.fixed_width_two_line')

    alabaster_width = 660
    alabaster_dpi = 100 * alabaster_width / 800

    print('Plotting chains...')
    f = naima.plot_chain(sampler, 1)
    f.savefig('RXJ1713_IC_chain_index.png', dpi=alabaster_dpi)
    f = naima.plot_chain(sampler, 2)
    f.savefig('RXJ1713_IC_chain_cutoff.png', dpi=alabaster_dpi)

    e_range = [100 * u.GeV, 500 * u.TeV]

    # with samples
    print('Plotting samples...')
    f = naima.plot_fit(sampler, 0, ML_info=False)
    f.axes[0].set_ylim(1e-13, 2e-10)
    f.tight_layout()
    f.subplots_adjust(hspace=0)
    f.savefig('RXJ1713_IC_model_samples.png', dpi=alabaster_dpi)
    print('Plotting samples with e_range...')
    f = naima.plot_fit(
        sampler, 0, e_range=e_range, ML_info=False, n_samples=500)
    f.axes[0].set_ylim(1e-13, 2e-10)
    f.tight_layout()
    f.subplots_adjust(hspace=0)
    f.savefig('RXJ1713_IC_model_samples_erange.png', dpi=alabaster_dpi)

    # with confs
    print('Plotting confs...')
    f = naima.plot_fit(sampler, 0, ML_info=False, confs=[3, 1],
Ejemplo n.º 2
0
# In[51]:

#Plot the fit results.
#Try confs=None, or try e_range=None

f = naima.plot_fit(sampler,
                   modelidx=0,
                   label=None,
                   sed=True,
                   last_step=False,
                   n_samples=100,
                   confs=[3, 2, 1],
                   ML_info=True,
                   figure=None,
                   plotdata=True,
                   plotresiduals=True,
                   e_unit=None,
                   e_range=(300 * u.GeV, 200 * u.TeV),
                   e_npoints=200,
                   threads=3,
                   xlabel=None,
                   ylabel=None,
                   ulim_opts={},
                   errorbar_opts={})
f.savefig("RXJ1713_SED.png")

#corner plot shows 1-D and 2-D projections of posterior probability.
#Useful to check correlations between parameters, see if the sampling was run
#with enough statistics etc.
Ejemplo n.º 3
0
    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")
Ejemplo n.º 4
0
        results.write(f, format='ascii.fixed_width_two_line')

    alabaster_width = 660
    alabaster_dpi = 100 * alabaster_width / 800

    print('Plotting chains...')
    f = naima.plot_chain(sampler, 1)
    f.savefig('RXJ1713_IC_chain_index.png', dpi=alabaster_dpi)
    f = naima.plot_chain(sampler, 2)
    f.savefig('RXJ1713_IC_chain_cutoff.png', dpi=alabaster_dpi)

    e_range = [100 * u.GeV, 500 * u.TeV]

    # with samples
    print('Plotting samples...')
    f = naima.plot_fit(sampler, 0, ML_info=False)
    f.axes[0].set_ylim(1e-13, 2e-10)
    f.tight_layout()
    f.subplots_adjust(hspace=0)
    f.savefig('RXJ1713_IC_model_samples.png', dpi=alabaster_dpi)
    print('Plotting samples with e_range...')
    f = naima.plot_fit(sampler,
                       0,
                       e_range=e_range,
                       ML_info=False,
                       n_samples=500)
    f.axes[0].set_ylim(1e-13, 2e-10)
    f.tight_layout()
    f.subplots_adjust(hspace=0)
    f.savefig('RXJ1713_IC_model_samples_erange.png', dpi=alabaster_dpi)
Ejemplo n.º 5
0
    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")
out_root = "RXJ1713_IC_minimal"
naima.save_run(out_root, sampler)

## Save diagnostic plots and results table
naima.save_diagnostic_plots(out_root, sampler, sed=True)
naima.save_results_table(out_root, sampler)


# In[51]:

#Plot the fit results.
#Try confs=None, or try e_range=None

f = naima.plot_fit(sampler, modelidx=0, label=None, sed=True, last_step=False, 
               n_samples=100, confs=[3,2,1], ML_info=True, 
               figure=None, plotdata=True, plotresiduals=True, 
               e_unit=None, e_range=(300*u.GeV,200*u.TeV), e_npoints=200, threads=3, 
               xlabel=None, ylabel=None, ulim_opts={}, errorbar_opts={})
f.savefig("RXJ1713_SED.png" )



#corner plot shows 1-D and 2-D projections of posterior probability.
#Useful to check correlations between parameters, see if the sampling was run
#with enough statistics etc.

#Chain plots show the 1-D projection of the posterior distribution for a given parameter
#as well as the evolution of the parameter during the sampling.