Attribute error : run the sampler with 'store==True' before accessing the results

567 views
Skip to first unread message

chakresh singh

unread,
Jun 16, 2022, 2:09:48 AM6/16/22
to emcee users
Hi, 
I am suddenly receiving this error when I use sampler.get_chain(). Has there been a recent update to emcee?
best
Chakresh

Dustin Lang

unread,
Jun 16, 2022, 7:22:41 AM6/16/22
to chakresh singh, emcee users
Hi,

You're going to have to provide a little more information than your subject line :)  Please at least show the code leading up to the function that is failing, and the complete output that you get.  It might look like a mystery to you, but it will make sense to the code authors.

(and it would be store=True, not store==True...)

cheers,
dustin





--
You received this message because you are subscribed to the Google Groups "emcee users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emcee-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emcee-users/73a157b0-3091-402e-a683-9fd5bc63f7acn%40googlegroups.com.

chakresh singh

unread,
Jun 16, 2022, 7:50:29 AM6/16/22
to Dustin Lang, emcee users
Hi, 
So the code snippet is below - 

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import networkx as nx 

#### creating synthetic graphs

G0 = nx.barabasi_albert_graph(50,1)
G1_Edges = list(G0.edges()) + [(1,4),(0,5),(2,7),(2,8)]
G1 = nx.Graph()
G1.add_edges_from(G1_Edges)

#### constants and initial points for the model run
d_max = 10 # diameter of the end graph
k_max = 10 # max_degree to normalize ri,rj
theta = [1,1,1,1,1]

data_list_of_graphs = [G0,G1]


#### MLE for the model
def MLE_forOptimization(params):
   
    alpha_mu,alpha_l,beta,gamma,delta = params
   
    def log_uniform_prior(params):
        alpha_mu,alpha_l,beta,gamma,delta = params
       
        if -10 < alpha_mu < 10 and -10 < alpha_l < 10 and -10 < beta < 10 and -10 < gamma < 10 \
        and -10 < delta < 10:
            return 0.0
       
        return -np.inf
   
    def log_gaussian_prior(params):
        alpha_mu,alpha_l,beta,gamma,delta = params
        mu = 1
        sigma=10
       
        lp=0
       
        for m in [alpha_mu,alpha_l,beta,gamma,delta]:
       
            lp -= 0.5*((m - mu)/sigma)**2
       
        return lp
   
    def f_ij(ni,nj,G):

        """
        This calculates the first part of the PPI model based on the max degree and the min degree of  \\
        the nodes in the network

        f_ij = (max(ri,rj)**a_u)*(min(ri,rj)**a_l)

        """

        ri = G.degree(ni)
        rj = G.degree(nj)
       
               
        return (max([ri,rj])**alpha_mu)*(min([ri,rj])**alpha_l)

    def g_ij(ni,nj,G):

        """
        This functions calculates the second part of the PPI model based on the network topology \\

        g_ij = (dij/d_max)**beta*(1-dij/dmax)**gamma  : if dij < inf
        g_ij = e**delta  : if dij = inf

        """

        try:

            dij = nx.shortest_path_length(G,ni,nj)
            gij = ((dij/d_max)**(beta))*((1-(dij/d_max))**(gamma))

        except :

            gij = np.exp(delta)
       
        return gij

    def P_ij(edge,NF,G_tmin1,Gt):
       
        (i,j) = edge
       
        val = ((f_ij(i,j,G_tmin1)*g_ij(i,j,G_tmin1))+1e-3)/NF

       
        return val

    def sum_logPij_over_edges(G_tmin1,Gt):


        Z = np.sum(np.array([(f_ij(i,j,G_tmin1)*g_ij(i,j,G_tmin1))+1e-3 \
                            for (i,j) in Gt.edges()]))  # Normalizing Factor
       
       
        return np.sum(np.array([np.log(P_ij((i,j),Z,G_tmin1,Gt)) for (i,j) in Gt.edges()]))


    def log_likelihood(list_of_graphs):

        Log_L = 0
       
        prior = log_prior(params)
       
        if not np.isfinite(prior):
            return -np.inf

        if len(list_of_graphs) == 1:

            return sum_logPij_over_edges(list_of_graphs[0],list_of_graphs[0])

        else:

            for i in range(1,len(list_of_graphs)):

                Log_L += sum_logPij_over_edges(list_of_graphs[i-1],list_of_graphs[i])

            return prior + Log_L

    return log_likelihood(data_list_of_graphs)

I am trying to reproduce the network growth model from the paper - (https://doi.org/10.1073/pnas.1509757112)
at this moment on synthetic graphs G0,G1 as my test set. 

#### initializing the sampler
import emcee
eps=0.01
nwalkers = 50
ndim=5
p0 = np.ones((nwalkers, ndim)) + eps*np.random.randn(nwalkers,ndim)

sampler = emcee.EnsembleSampler(nwalkers, ndim, MLE_forOptimization)
state = sampler.run_mcmc(p0,500,progress=True)
sampler.reset()

params = [r'$\alpha_\mu$', r'$alpha_l$',r'$\beta$',r'$\gamma$',r'$\delta$']

thin, discard = 15, 100
# plot traces of parameters

plt.figure(figsize=(15,3))
for i in range(ndim):
    plt.subplot(1, 5, i+1)
    plt.plot(sampler.get_chain(thin=thin, discard=discard)[:, :, i]\
    , color='black', alpha=0.1, lw=2)
    plt.xlabel('Iteration')
    plt.ylabel(params[i])
plt.tight_layout()

import corner
# plot 1-D, 2-D marginal distributions
fig=corner.corner(sampler.get_chain(thin=thin, discard=discard, flat=True),\
              labels=params, show_titles=True);

plt.show()

ERROR -- i receive

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-204-6a188a55c126> in <cell line: 7>()
      7 for i in range(ndim):
      8     plt.subplot(1, 5, i+1)
----> 9     plt.plot(sampler.get_chain(thin=thin, discard=discard)[:, :, i]\
     10     , color='black', alpha=0.1, lw=2)
     11     plt.xlabel('Iteration')

~/.local/lib/python3.8/site-packages/emcee/ensemble.py in get_chain(self, **kwargs)
    580
    581     def get_chain(self, **kwargs):
--> 582         return self.get_value("chain", **kwargs)
    583
    584     get_chain.__doc__ = Backend.get_chain.__doc__

~/.local/lib/python3.8/site-packages/emcee/ensemble.py in get_value(self, name, **kwargs)
    600
    601     def get_value(self, name, **kwargs):
--> 602         return self.backend.get_value(name, **kwargs)
    603
    604     def get_autocorr_time(self, **kwargs):

~/.local/lib/python3.8/site-packages/emcee/backends/backend.py in get_value(self, name, flat, thin, discard)
     42     def get_value(self, name, flat=False, thin=1, discard=0):
     43         if self.iteration <= 0:
---> 44             raise AttributeError(
     45                 "you must run the sampler with "
     46                 "'store == True' before accessing the "

AttributeError: you must run the sampler with 'store == True' before accessing the results. 

------------------------------------------------------------------------------------

However, I recently figured out that if I comment "sampler.reset()" it works well. However, I didn't have to do it yesterday which confused me. Also I am not sure if I am getting the right results but that is still under study :)

Thanks for your comments. happy to discuss more. apologies for the unclarity before
best
Chakresh

Dan Foreman-Mackey

unread,
Jun 16, 2022, 8:21:20 AM6/16/22
to emcee...@googlegroups.com
sampler.reset() clears all the stored data so I'm confident that running sampler.reset() and then trying to access the chains has never worked with any version of emcee!
Hope this helps
Dan
Reply all
Reply to author
Forward
0 new messages