joshspeagle / dynesty

Dynamic Nested Sampling package for computing Bayesian posteriors and evidences
https://dynesty.readthedocs.io/
MIT License
347 stars 76 forks source link

Using dyplot via importing samples #421

Closed camarman closed 1 year ago

camarman commented 1 year ago

Dynesty version I have installed SimpleMC, where dynesty is implemented into the program. It seems that they are using the dynesty version from 2 years ago.

Background

I can triangle_plot via getdist for nested sampling in SimpleMC by using the following steps:

First obtaining the samples from SimpleMC for a given config:

analysis = DriverMC(model='LCDM', datasets='SN', analyzername="nested")
res = analysis1.executer(nestedType='multi', nlivepoints=50, accuracy=0.01, priortype='u', sigma=2, nproc=1)
samples = res1['result']['samples']
labels =  ['{}'.format(p.Ltxname) for p in analysis1.pars_info]

(where DriverMC and executer belongs to SimpleMC.) Later on, I can define samples via MCSamples and plot anything via getdist

samp = MCSamples(samples=samples1, names=labels1, labels=labels1, label='SN', sampler='nested')
g = plots.get_subplot_plotter()
g.settings.figure_legend_frame = False
g.settings.title_limit_fontsize = 11
g.triangle_plot(samp1,
                filled=True,
                legend_labels=['SN'],
                legend_loc='upper right',
                line_args=[{'ls':'-.', 'lw':2, 'color':'red'}],
                contour_colors=['red'],
                settings={'smooth_scale_2D':0.3, 'smooth_scale_1D':0.6},
                title_limit=1)

The full example can be seen from here.

Your question

1) Is there a similar procedure that I can follow so that I use dyplot ?

or

2) Is there a way to use dyplot by using these files types obtained from SimpleMC ?

segasai commented 1 year ago

I'm not sure I can help/understand, it seems the question is more about simplemc than about dynesty. The docs on dynesty.plot are here https://dynesty.readthedocs.io/en/stable/api.html#module-dynesty.plotting and the demos https://github.com/joshspeagle/dynesty/tree/master/demos also have many examples of what dynesty.plot can do.

camarman commented 1 year ago

Thanks for the reply. I have done some tests, and it seems the problem is about SimpleMC. It's using an old and modified version of the dynesty.

For instance, the SimpleMC defines

sampler = NestedSampler(self.logLike, self.priorTransform, self.dims,
                      bound=nestedType, sample = 'unif', nlive = nlivepoints,
                      pool = pool, queue_size=nprocess, use_pool={'loglikelihood': False})
sampler.run_nested(dlogz=accuracy, outputname=self.outputpath,
                                addDerived=self.addDerived, simpleLike=self.L)
M = sampler.results

when I try to get importance_weights() via print(M.importance_weights()), SimpleMC complains;

    return self[name]
           ~~~~^^^^^^
KeyError: 'importance_weights'

and

    raise AttributeError(name)
AttributeError: importance_weights

Due to this error I cannot perform any plotting operations, since importance_weights() is not defined in SimpleMC and it seems dyplot need that attribute for plotting.

I'm not sure I can help/understand

I am sorry about that. I have also realized that I wasn't clear enough. I was trying to ask if there is a feature in dynesty that I can import data from chains. By doing that, I thought I could perform the plotting operations in SimpleMC more easily.

It turns out that I can implement them easily, but now I am getting this error.

segasai commented 1 year ago

importance_weights is a new attribute introduced in recent version of dynesty. if you wish you can use dyplot from an older version.

if you want to make dyplot work with non-dynesty objects, something like this works fine


class Res:
    def __init__(self, samples, whts):
        self.D={}
        self.D['samples'] = samples
        self.whts = whts
    def __getitem__(self,x):
        return self.D[x]
    def importance_weights(self):
        return self.whts
whts=np.random.uniform(size=10000);
dyplot.cornerplot(Res(np.random.normal(size=(10000,2)),whts/whts.sum()))

I think you either should just use the released version of dynesty then I'll be happy to answer questions about it, but if you're using some modified version of dynesty, I don't think I can help.

camarman commented 1 year ago

if you wish you can use dyplot from an older version.

Thank you very much!! I have never thought installing the older version of the dynesty. Now, I can plot perfectly fine :)

I think you either should just use the released version of dynesty then I'll be happy to answer questions about it, but if you're using some modified version of dynesty, I don't think I can help.

Sadly, there's not much I can do since I am using another program (SimpleMC) to do the data analysis. I kind of contacted with the developers of the SimpleMC. Maybe they update in the future. Also, it seems they have modified the dynest somehow but I am not sure exactly how. Nevertheless, I have just run dyplot.cornerpoints and dyplot.traceplot without any problem.