Exemplo n.º 1
0
class TestBIFReader(unittest.TestCase):

    def setUp(self):

        self.reader = BIFReader(string="""
// Bayesian Network in the Interchange Format
// Produced by BayesianNetworks package in JavaBayes
// Output created Sun Nov 02 17:49:49 GMT+00:00 1997
// Bayesian network
network "Dog-Problem" { //5 variables and 5 probability distributions
        property "credal-set constant-density-bounded 1.1" ;
}
variable  "light-on" { //2 values
        type discrete[2] {  "true"  "false" };
        property "position = (218, 195)" ;
}
variable  "bowel-problem" { //2 values
        type discrete[2] {  "true"  "false" };
        property "position = (335, 99)" ;
}
variable  "dog-out" { //2 values
        type discrete[2] {  "true"  "false" };
        property "position = (300, 195)" ;
}
variable  "hear-bark" { //2 values
        type discrete[2] {  "true"  "false" };
        property "position = (296, 268)" ;
}
variable  "family-out" { //2 values
        type discrete[2] {  "true"  "false" };
        property "position = (257, 99)" ;
}
probability (  "light-on"  "family-out" ) { //2 variable(s) and 4 values
        (true) 0.6 0.4 ;
        (false) 0.05 0.95 ;
}
probability (  "bowel-problem" ) { //1 variable(s) and 2 values
        table 0.01 0.99 ;
}
probability (  "dog-out"  "bowel-problem"  "family-out" ) { //3 variable(s) and 8 values
        table 0.99 0.97 0.9 0.3 0.01 0.03 0.1 0.7 ;
}
probability (  "hear-bark"  "dog-out" ) { //2 variable(s) and 4 values
        table 0.7 0.01 0.3 0.99 ;
}
probability (  "family-out" ) { //1 variable(s) and 2 values
        table 0.15 0.85 ;
}
""")

    def test_network_name(self):

        name_expected = 'Dog-Problem'
        self.assertEqual(self.reader.network_name, name_expected)

    def test_get_variables(self):

        var_expected = ['light-on', 'bowel-problem', 'dog-out',
                        'hear-bark', 'family-out']
        self.assertListEqual(self.reader.get_variables(), var_expected)

    def test_states(self):

        states_expected = {'bowel-problem': ['true', 'false'],
                           'dog-out': ['true', 'false'],
                           'family-out': ['true', 'false'],
                           'hear-bark': ['true', 'false'],
                           'light-on': ['true', 'false']}
        states = self.reader.get_states()
        for variable in states_expected:
            self.assertListEqual(states_expected[variable], states[variable])

    def test_get_property(self):

        property_expected = {'bowel-problem': ['position = (335, 99)'],
                             'dog-out': ['position = (300, 195)'],
                             'family-out': ['position = (257, 99)'],
                             'hear-bark': ['position = (296, 268)'],
                             'light-on': ['position = (218, 195)']}
        prop = self.reader.get_property()
        for variable in property_expected:
            self.assertListEqual(property_expected[variable],
                                 prop[variable])

    def test_get_values(self):

        cpd_expected = {'bowel-problem': np.array([[0.01],
                                                   [0.99]]),
                        'dog-out': np.array([[0.99, 0.97, 0.9, 0.3],
                                             [0.01, 0.03, 0.1, 0.7]]),
                        'family-out': np.array([[0.15],
                                                [0.85]]),
                        'hear-bark': np.array([[0.7, 0.01],
                                               [0.3, 0.99]]),
                        'light-on': np.array([[0.6, 0.05],
                                              [0.4, 0.95]])}
        cpd = self.reader.variable_cpds
        for variable in cpd_expected:
            np_test.assert_array_equal(cpd_expected[variable],
                                       cpd[variable])

    def test_get_parents(self):

        parents_expected = {'bowel-problem': [],
                            'dog-out': ['bowel-problem', 'family-out'],
                            'family-out': [],
                            'hear-bark': ['dog-out'],
                            'light-on': ['family-out']}
        parents = self.reader.get_parents()
        for variable in parents_expected:
            self.assertListEqual(parents_expected[variable],
                                 parents[variable])

    def test_get_edges(self):

        edges_expected = [['family-out', 'dog-out'],
                          ['bowel-problem', 'dog-out'],
                          ['family-out', 'light-on'],
                          ['dog-out', 'hear-bark']]
        self.assertListEqual(sorted(self.reader.variable_edges),
                             sorted(edges_expected))

    def test_get_model(self):
        edges_expected = [('family-out', 'dog-out'),
                          ('bowel-problem', 'dog-out'),
                          ('family-out', 'light-on'),
                          ('dog-out', 'hear-bark')]
        nodes_expected = ['bowel-problem', 'hear-bark', 'light-on',
                          'dog-out', 'family-out']
        edge_expected = {'bowel-problem': {'dog-out': {'weight': None}},
                         'dog-out': {'hear-bark': {'weight': None}},
                         'family-out': {'dog-out': {'weight': None},
                                        'light-on': {'weight': None}},
                         'hear-bark': {},
                         'light-on': {}}
        node_expected = {'bowel-problem': {'weight': None,
                                           'position': '(335, 99)'},
                         'dog-out': {'weight': None,
                                     'position': '(300, 195)'},
                         'family-out': {'weight': None,
                                        'position': '(257, 99)'},
                         'hear-bark': {'weight': None,
                                       'position': '(296, 268)'},
                         'light-on': {'weight': None,
                                      'position': '(218, 195)'}}
        cpds_expected = [np.array([[0.01],
                                   [0.99]]),
                         np.array([[0.99, 0.97, 0.9, 0.3],
                                   [0.01, 0.03, 0.1, 0.7]]),
                         np.array([[0.15],
                                   [0.85]]),
                         np.array([[0.7, 0.01],
                                   [0.3, 0.99]]),
                         np.array([[0.6, 0.05],
                                   [0.4, 0.95]])]
        model = self.reader.get_model()
        for cpd_index in range(0, len(cpds_expected)):
            np_test.assert_array_equal(model.get_cpds()[cpd_index].get_values(),
                                       cpds_expected[cpd_index])
        self.assertDictEqual(model.node, node_expected)
        self.assertDictEqual(model.edge, edge_expected)
        self.assertListEqual(sorted(model.nodes()), sorted(nodes_expected))
        self.assertListEqual(sorted(model.edges()), sorted(edges_expected))

    def tearDown(self):
        del self.reader
Exemplo n.º 2
0
class TestBIFReader(unittest.TestCase):
    def setUp(self):

        self.reader = BIFReader(string="""
// Bayesian Network in the Interchange Format
// Produced by BayesianNetworks package in JavaBayes
// Output created Sun Nov 02 17:49:49 GMT+00:00 1997
// Bayesian network
network "Dog-Problem" { //5 variables and 5 probability distributions
        property "credal-set constant-density-bounded 1.1" ;
}
variable  "light-on" { //2 values
        type discrete[2] {  "true"  "false" };
        property "position = (218, 195)" ;
}
variable  "bowel-problem" { //2 values
        type discrete[2] {  "true"  "false" };
        property "position = (335, 99)" ;
}
variable  "dog-out" { //2 values
        type discrete[2] {  "true"  "false" };
        property "position = (300, 195)" ;
}
variable  "hear-bark" { //2 values
        type discrete[2] {  "true"  "false" };
        property "position = (296, 268)" ;
}
variable  "family-out" { //2 values
        type discrete[2] {  "true"  "false" };
        property "position = (257, 99)" ;
}
probability (  "light-on"  "family-out" ) { //2 variable(s) and 4 values
        table 0.6 0.05 0.4 0.95 ;
}
probability (  "bowel-problem" ) { //1 variable(s) and 2 values
        table 0.01 0.99 ;
}
probability (  "dog-out"  "bowel-problem"  "family-out" ) { //3 variable(s) and 8 values
        table 0.99 0.97 0.9 0.3 0.01 0.03 0.1 0.7 ;
}
probability (  "hear-bark"  "dog-out" ) { //2 variable(s) and 4 values
        table 0.7 0.01 0.3 0.99 ;
}
probability (  "family-out" ) { //1 variable(s) and 2 values
        table 0.15 0.85 ;
}
""")

    def test_network_name(self):

        name_expected = 'Dog-Problem'
        self.assertEqual(self.reader.network_name, name_expected)

    def test_get_variables(self):

        var_expected = [
            'light-on', 'bowel-problem', 'dog-out', 'hear-bark', 'family-out'
        ]
        self.assertListEqual(self.reader.get_variables(), var_expected)

    def test_states(self):

        states_expected = {
            'bowel-problem': ['true', 'false'],
            'dog-out': ['true', 'false'],
            'family-out': ['true', 'false'],
            'hear-bark': ['true', 'false'],
            'light-on': ['true', 'false']
        }
        states = self.reader.get_states()
        for variable in states_expected:
            self.assertListEqual(states_expected[variable], states[variable])

    def test_get_property(self):

        property_expected = {
            'bowel-problem': ['position = (335, 99)'],
            'dog-out': ['position = (300, 195)'],
            'family-out': ['position = (257, 99)'],
            'hear-bark': ['position = (296, 268)'],
            'light-on': ['position = (218, 195)']
        }
        prop = self.reader.get_property()
        for variable in property_expected:
            self.assertListEqual(property_expected[variable], prop[variable])

    def test_get_cpd(self):

        cpd_expected = {
            'bowel-problem': np.array([[0.01], [0.99]]),
            'dog-out': np.array([[0.99, 0.97, 0.9, 0.3],
                                 [0.01, 0.03, 0.1, 0.7]]),
            'family-out': np.array([[0.15], [0.85]]),
            'hear-bark': np.array([[0.7, 0.01], [0.3, 0.99]]),
            'light-on': np.array([[0.6, 0.05], [0.4, 0.95]])
        }
        cpd = self.reader.variable_cpds
        for variable in cpd_expected:
            np_test.assert_array_equal(cpd_expected[variable], cpd[variable])

    def test_get_parents(self):

        parents_expected = {
            'bowel-problem': [],
            'dog-out': ['bowel-problem', 'family-out'],
            'family-out': [],
            'hear-bark': ['dog-out'],
            'light-on': ['family-out']
        }
        parents = self.reader.get_parents()
        for variable in parents_expected:
            self.assertListEqual(parents_expected[variable], parents[variable])

    def test_get_edges(self):

        edges_expected = [['family-out', 'dog-out'],
                          ['bowel-problem', 'dog-out'],
                          ['family-out', 'light-on'], ['dog-out', 'hear-bark']]
        self.assertListEqual(sorted(self.reader.variable_edges),
                             sorted(edges_expected))

    def test_get_model(self):
        edges_expected = [('family-out', 'dog-out'),
                          ('bowel-problem', 'dog-out'),
                          ('family-out', 'light-on'), ('dog-out', 'hear-bark')]
        nodes_expected = [
            'bowel-problem', 'hear-bark', 'light-on', 'dog-out', 'family-out'
        ]
        edge_expected = {
            'bowel-problem': {
                'dog-out': {}
            },
            'dog-out': {
                'hear-bark': {}
            },
            'family-out': {
                'dog-out': {},
                'light-on': {}
            },
            'hear-bark': {},
            'light-on': {}
        }
        node_expected = {
            'bowel-problem': {
                'position': '(335, 99)'
            },
            'dog-out': {
                'position': '(300, 195)'
            },
            'family-out': {
                'position': '(257, 99)'
            },
            'hear-bark': {
                'position': '(296, 268)'
            },
            'light-on': {
                'position': '(218, 195)'
            }
        }
        cpds_expected = [
            np.array([[0.01], [0.99]]),
            np.array([[0.99, 0.97, 0.9, 0.3], [0.01, 0.03, 0.1, 0.7]]),
            np.array([[0.15], [0.85]]),
            np.array([[0.7, 0.01], [0.3, 0.99]]),
            np.array([[0.6, 0.05], [0.4, 0.95]])
        ]
        model = self.reader.get_model()
        for cpd_index in range(0, len(cpds_expected)):
            np_test.assert_array_equal(model.get_cpds()[cpd_index].get_cpd(),
                                       cpds_expected[cpd_index])
        self.assertDictEqual(model.node, node_expected)
        self.assertDictEqual(model.edge, edge_expected)
        self.assertListEqual(sorted(model.nodes()), sorted(nodes_expected))
        self.assertListEqual(sorted(model.edges()), sorted(edges_expected))

    def tearDown(self):
        del self.reader
Exemplo n.º 3
0
class Transformer:
    def __init__(self, pathfile):
        self.reader = BIFReader(pathfile)
        self.node_topo_sorted = list()

        # Find Topo order
        self.__find_topo_sorted()


    def __find_topo_sorted(self):
        parents = self.reader.get_parents()
        queue = deque()
        queue.extend(self.__find_root(parents))

        while len(queue):
            node  = queue.popleft()

            # Add to totpo list
            self.node_topo_sorted.append(node)
            
            # Remove node in parrents
            parents.pop(node)
            
            # Remove in child
            for parent in parents:
                if node in parents[parent]:
                    parents[parent].remove(node)

            # When queue emty try find child
            if len(queue) == 0:
                queue.extend(self.__find_root(parents))


    def __find_root(self, parrents):
        root = list()
        for parrent in parrents:
            if len(parrents[parrent]) == 0:
                root.append(parrent)

        return root

    def write(self, pathfile):
        parrents = self.reader.get_parents()
        states = self.reader.get_states()
        values = self.reader.get_values()

        with open(pathfile, 'w') as writer:
            # Write number of node
            writer.write( str(len(self.node_topo_sorted)) + '\n')

            # Write each node 
            for node in self.node_topo_sorted:

                # Node Name
                writer.write(node + ';')

                # Parent of node
                node_parrents = parrents[node]
                writer.write(','.join(node_parrents) + ';')
                
                # Write state
                state = states[node]
                writer.write(','.join(state) + ';')

                # Find dim
                dim = list()
                for node_parrent in node_parrents:
                    dim.append(len(states[node_parrent]))

                dim.append(len(state))

                # Write dim
                writer.write(','.join(map(str,dim)) + ';')

                # Write propabilities
                value = map(str, list(np.transpose(values[node]).ravel()))
                writer.write(','.join(value) )
            
                writer.write('\n')