class Test_nodedistributionsetup(unittest.TestCase):
    """
    Test class for creating graphical model scaffolds from phylogeny files
    """

    def setUp(self):
        """
        Loads a phylogeny. 
        """
        phylo_file = os.path.dirname(os.path.realpath(__file__)) + "/example_data/Asp_protease_2.xml"
        self.phylo_graph = BioBayesGraph()
        self.graph = self.phylo_graph.populate_from_phyloxml(phylo_file)

    def testSetLeafNodeVariableInitialization(self):
        """
        Tries to set up graphical model leaf node variables properly. 
        """
        # Sets all nodes to have 3 variables each.
        # And defines the domain of each explicitly
        card_sum1 = 0.0
        for node_index in self.phylo_graph.iterleafnodes():
            # print graph.vertex_properties["name"][graph.vertex(node_index)]
            self.phylo_graph.set_node_variable_count(node_index=node_index, num_vars=3)
            # So var1 can take values in 0 or 1,
            # var2 can take values in {0,1,2,3}
            # and var3 can take values in {'a','b','c'}
            self.phylo_graph.set_node_variable_domains(
                node_index=node_index, var_domains=[(0, 1), (0, 1, 2, 3), ("a", "b", "c")]
            )
            for v_ind in range(self.phylo_graph.get_node_variable_count(node_index)):
                card_sum1 += len(self.phylo_graph.get_node_variable_domain(node_index, v_ind))
        self.assertEqual(card_sum1, 2745.0, "Leaf node variable cardinality sum check didn't pass")  # 305*(2+4+3)

    def testSetInternalNodeVariableInitialization(self):
        """
        Tries to set up graphical model internal node variables properly. 
        """
        card_sum2 = 0.0
        for node_index in self.phylo_graph.iterinternalnodes():
            # print graph.vertex_properties["name"][graph.vertex(node_index)]
            self.phylo_graph.set_node_variable_count(node_index=node_index, num_vars=3)
            # So var1 can take values in 0 or 1,
            # var2 can take values in {0,1,2,3}
            # and var3 can take values in {'a','b','c'}
            self.phylo_graph.set_node_variable_domains(node_index=node_index, var_domains=[(0, 1), (0, 1), (0, 1)])
            for v_ind in range(self.phylo_graph.get_node_variable_count(node_index)):
                card_sum2 += len(self.phylo_graph.get_node_variable_domain(node_index, v_ind))
        self.assertEqual(card_sum2, 1818.0, "Internal node variable cardinality sum check didn't pass")  # 303*(2+2+2)

    def testProbabilityDistInitialization(self):
        """
        Tries to set probability distributions for leaf nodes.
        """
        self.testSetLeafNodeVariableInitialization()

        card_sum1 = 0.0
        for node_index in self.phylo_graph.iterleafnodes():
            # print graph.vertex_properties["name"][graph.vertex(node_index)]
            self.phylo_graph.set_node_variable_count(node_index=node_index, num_vars=3)

            for v_ind in range(self.phylo_graph.get_node_variable_count(node_index)):
                card_sum1 += len(self.phylo_graph.get_node_variable_domain(node_index, v_ind))
        self.assertEqual(card_sum1, 2745.0, "Leaf node variable cardinality sum check didn't pass")  # 305*(2+4+3)