# Path: pyBN/utils/random_sample.py
# def random_sample(bn, n=1000):
#     """
#     Take a random sample of "n" observations from a
#     BayesNet object. This is essentially just the
#     forward sample algorithm that returns the samples.
# 
#     Parameters
#     ----------
#     *bn* : a BayesNet object from which to sample
# 
#     *n* : an integer
#         The number of observations to take
# 
#     *evidence* : a dictionary, key=rv & value=instantiation
#         Evidence to pass in
# 
#     Returns
#     -------
#     *sample_dict* : a list of samples, where each sample
#         is a list of values in bn.nodes() (topsort) order
# 
#     Notes
#     -----
# 
#     """
#     sample = np.empty((n,bn.num_nodes()),dtype=np.int)
# 
#     rv_map = dict([(rv,idx) for idx,rv in enumerate(bn.nodes())])
#     factor_map = dict([(rv,Factor(bn,rv)) for rv in bn.nodes()])
#     
#     for i in range(n):
#         for rv in bn.nodes():
#             f = deepcopy(factor_map[rv])
#             # reduce_factor by parent samples
#             for p in bn.parents(rv):
#                 f.reduce_factor(p,bn.values(p)[sample[i][rv_map[p]]])
#             choice_vals = bn.values(rv)
#             choice_probs = f.cpt
#             chosen_val = np.random.choice(choice_vals, p=choice_probs)
#             sample[i][rv_map[rv]] = bn.values(rv).index(chosen_val)
# 
#     return sample


import unittest
import os
import numpy as np
from os.path import dirname
from pyBN.utils.random_sample import random_sample
from pyBN.readwrite.read import read_bn
"""
********
UnitTest
Misc
********

"""
__author__ = """Nicholas Cullen <ncullen.th@dartmouth.edu>"""




class RandomSampleTestCase(unittest.TestCase):

	def setUp(self):
		self.dpath = os.path.join(dirname(dirname(dirname(dirname(__file__)))),'data')	
		self.bn = read_bn(os.path.join(self.dpath,'cancer.bif'))

	def tearDown(self):
		pass

	def test_random_sample(self):
		np.random.seed(3636)

# Path: pyBN/utils/random_sample.py
# def random_sample(bn, n=1000):
#     """
#     Take a random sample of "n" observations from a
#     BayesNet object. This is essentially just the
#     forward sample algorithm that returns the samples.
# 
#     Parameters
#     ----------
#     *bn* : a BayesNet object from which to sample
# 
#     *n* : an integer
#         The number of observations to take
# 
#     *evidence* : a dictionary, key=rv & value=instantiation
#         Evidence to pass in
# 
#     Returns
#     -------
#     *sample_dict* : a list of samples, where each sample
#         is a list of values in bn.nodes() (topsort) order
# 
#     Notes
#     -----
# 
#     """
#     sample = np.empty((n,bn.num_nodes()),dtype=np.int)
# 
#     rv_map = dict([(rv,idx) for idx,rv in enumerate(bn.nodes())])
#     factor_map = dict([(rv,Factor(bn,rv)) for rv in bn.nodes()])
#     
#     for i in range(n):
#         for rv in bn.nodes():
#             f = deepcopy(factor_map[rv])
#             # reduce_factor by parent samples
#             for p in bn.parents(rv):
#                 f.reduce_factor(p,bn.values(p)[sample[i][rv_map[p]]])
#             choice_vals = bn.values(rv)
#             choice_probs = f.cpt
#             chosen_val = np.random.choice(choice_vals, p=choice_probs)
#             sample[i][rv_map[rv]] = bn.values(rv).index(chosen_val)
# 
#     return sample


# Path: pyBN/utils/_tests/test_random_sample.py
import unittest
import os
import numpy as np
from os.path import dirname
from pyBN.utils.random_sample import random_sample
from pyBN.readwrite.read import read_bn

"""
********
UnitTest
Misc
********

"""
__author__ = """Nicholas Cullen <ncullen.th@dartmouth.edu>"""




class RandomSampleTestCase(unittest.TestCase):

	def setUp(self):
		self.dpath = os.path.join(dirname(dirname(dirname(dirname(__file__)))),'data')	
		self.bn = read_bn(os.path.join(self.dpath,'cancer.bif'))

	def tearDown(self):
		pass

	def test_random_sample(self):
		np.random.seed(3636)

		sample = random_sample(self.bn,5)