# Path: pyglm/utils/parallel_util.py
# def parallel_compute_log_p(dview,
#                            master,
#                            v,
#                            N):
#     """ Compute the log prob in parallel
#     """
# 
#     # Compute the log probabaility of global variables
#     # (e.g. the network) on the first node
#     lp_tot = 0
# 
#     @interactive
#     def _compute_network_lp(vs):
#         print "Computing log prob for network"
#         syms = popn.get_variables()
#         nvars = popn.extract_vars(vs,0)
#         lp = seval(popn.network.log_p,
#                    syms,
#                    nvars)
#         #lp = popn.network.log_p.eval(dict(zip(_flatten(tmpsyms),
#                                      #        _flatten(tmpnvars))),
#                                      #on_unused_input='ignore')
#         return lp
# 
#     lp_tot += master.apply_sync(_compute_network_lp, v)
# 
#     # Decorate with @interactive to ensure that the function runs
#     # in the __main__ namespace that contains 'popn'
#     @interactive
#     def _compute_glm_lp(n, vs):
#         print "Computing lp for GLM %d" % n
#         syms = popn.get_variables()
#         nvars = popn.extract_vars(vs, n)
#         lp = seval(popn.glm.log_p,
#                    syms,
#                    nvars)
#         return lp
# 
#     lp_glms = dview.map_async(_compute_glm_lp,
#                               range(N),
#                               [v]*N)
#     # print lp_glms.get()
#     # lp_glms.display_outputs()
# 
#     lp_tot += sum(lp_glms.get())
#     return lp_tot


import sys
import numpy as np
    import os
    import cPickle
from scipy.misc import logsumexp
from IPython.parallel.util import interactive
from pyglm.utils.parallel_util import parallel_compute_log_p
            # print "M: %d\tBeta: %.3f" % (m,beta)

            # Set the likelihood scale (beta) in the graph model
            _set_beta(dview, beta)

            # Take many samples to mix over this beta
            for s in range(steps_per_B):
                sys.stdout.write("M: %d\tBeta: %.3f\tSample: %d \r" % (m,beta,s))
                sys.stdout.flush()

                # Go through variables, sampling one at a time, in parallel where possible
                for i in range(n_parallel_updates):
                    xs = dview.map_async(_parallel_update,
                                         [i]*N,
                                         [x]*N,
                                         range(N))

                    # wait_watching_stdout(xs, interval=interval)
                    concatenate_parallel_updates(xs.get(), x)

                # Sample serial updates
                for i in range(n_serial_updates):
                    x = master.apply(_serial_update, i, x).get()


            # Compute the ratio of this sample under this distribution and the previous distribution
            curr_lkhd = parallel_compute_log_p(dview, master, x, N)

            # import pdb; pdb.set_trace()
            _set_beta(dview, betas[n-1])

# Path: pyglm/utils/parallel_util.py
# def parallel_compute_log_p(dview,
#                            master,
#                            v,
#                            N):
#     """ Compute the log prob in parallel
#     """
# 
#     # Compute the log probabaility of global variables
#     # (e.g. the network) on the first node
#     lp_tot = 0
# 
#     @interactive
#     def _compute_network_lp(vs):
#         print "Computing log prob for network"
#         syms = popn.get_variables()
#         nvars = popn.extract_vars(vs,0)
#         lp = seval(popn.network.log_p,
#                    syms,
#                    nvars)
#         #lp = popn.network.log_p.eval(dict(zip(_flatten(tmpsyms),
#                                      #        _flatten(tmpnvars))),
#                                      #on_unused_input='ignore')
#         return lp
# 
#     lp_tot += master.apply_sync(_compute_network_lp, v)
# 
#     # Decorate with @interactive to ensure that the function runs
#     # in the __main__ namespace that contains 'popn'
#     @interactive
#     def _compute_glm_lp(n, vs):
#         print "Computing lp for GLM %d" % n
#         syms = popn.get_variables()
#         nvars = popn.extract_vars(vs, n)
#         lp = seval(popn.glm.log_p,
#                    syms,
#                    nvars)
#         return lp
# 
#     lp_glms = dview.map_async(_compute_glm_lp,
#                               range(N),
#                               [v]*N)
#     # print lp_glms.get()
#     # lp_glms.display_outputs()
# 
#     lp_tot += sum(lp_glms.get())
#     return lp_tot


# Path: pyglm/inference/parallel_ais.py
import sys
import numpy as np
    import os
    import cPickle
from scipy.misc import logsumexp
from IPython.parallel.util import interactive
from pyglm.utils.parallel_util import parallel_compute_log_p

            # print "M: %d\tBeta: %.3f" % (m,beta)

            # Set the likelihood scale (beta) in the graph model
            _set_beta(dview, beta)

            # Take many samples to mix over this beta
            for s in range(steps_per_B):
                sys.stdout.write("M: %d\tBeta: %.3f\tSample: %d \r" % (m,beta,s))
                sys.stdout.flush()

                # Go through variables, sampling one at a time, in parallel where possible
                for i in range(n_parallel_updates):
                    xs = dview.map_async(_parallel_update,
                                         [i]*N,
                                         [x]*N,
                                         range(N))

                    # wait_watching_stdout(xs, interval=interval)
                    concatenate_parallel_updates(xs.get(), x)

                # Sample serial updates
                for i in range(n_serial_updates):
                    x = master.apply(_serial_update, i, x).get()


            # Compute the ratio of this sample under this distribution and the previous distribution
            curr_lkhd = parallel_compute_log_p(dview, master, x, N)

            # import pdb; pdb.set_trace()
            _set_beta(dview, betas[n-1])

            prev_lkhd = parallel_compute_log_p(dview, master, x, N)