# Path: tensorprob/samplers/base.py
# class BaseSampler(object):
# 
#     def __init__(self, session=None):
#         self._session = session or tf.get_default_session()
# 
#     def sample(self, variables, cost, gradient=None):
#         raise NotImplementedError
# 
#     @property
#     def session(self):
#         return self._session
# 
#     @session.setter
#     def session(self, session):
#         self._session = session


import tensorflow as tf
import numpy as np
            import emcee
from .base import BaseSampler

    def __init__(self, walkers=100, **kwargs):
        try:
        except ImportError:
            raise ImportError("The emcee package needs to be installed in order to use EmceeSampler")

        self.emcee = emcee
        self.walkers = walkers
        super(EmceeSampler, self).__init__(**kwargs)

    def sample(self, variables, cost, gradient=None, samples=None):
        # Check if variables is iterable
        try:
            iter(variables)
        except TypeError:
            raise ValueError("Variables parameter is not iterable")

        inits = self.session.run(variables)

        for v in variables:
            if not isinstance(v, tf.Variable):
                raise ValueError("Parameter {} is not a tensorflow variable".format(v))

        def objective(xs):
            feed_dict = { k: v for k, v in zip(variables, xs) }
            out = self.session.run(cost, feed_dict=feed_dict)
            if np.isnan(out):
                return np.inf
            return -out


# Path: tensorprob/samplers/base.py
# class BaseSampler(object):
# 
#     def __init__(self, session=None):
#         self._session = session or tf.get_default_session()
# 
#     def sample(self, variables, cost, gradient=None):
#         raise NotImplementedError
# 
#     @property
#     def session(self):
#         return self._session
# 
#     @session.setter
#     def session(self, session):
#         self._session = session


# Path: tensorprob/samplers/emcee.py
import tensorflow as tf
import numpy as np
            import emcee
from .base import BaseSampler


    def __init__(self, walkers=100, **kwargs):
        try:
        except ImportError:
            raise ImportError("The emcee package needs to be installed in order to use EmceeSampler")

        self.emcee = emcee
        self.walkers = walkers
        super(EmceeSampler, self).__init__(**kwargs)

    def sample(self, variables, cost, gradient=None, samples=None):
        # Check if variables is iterable
        try:
            iter(variables)
        except TypeError:
            raise ValueError("Variables parameter is not iterable")

        inits = self.session.run(variables)

        for v in variables:
            if not isinstance(v, tf.Variable):
                raise ValueError("Parameter {} is not a tensorflow variable".format(v))

        def objective(xs):
            feed_dict = { k: v for k, v in zip(variables, xs) }
            out = self.session.run(cost, feed_dict=feed_dict)
            if np.isnan(out):
                return np.inf
            return -out


        all_inits = self.emcee.utils.sample_ball(inits, [1e-1] * len(inits), self.walkers)