Ejemplo n.º 1
0
    def setUp(self):

        reader_file = UAIReader('pgmpy/tests/test_readwrite/testdata/grid4x4_with_triplets.uai')
        self.markov_model = reader_file.get_model()

        for factor in self.markov_model.factors:
            factor.values = np.log(factor.values)
        self.mplp = Mplp(self.markov_model)
    def setUp(self):

        reader_file = UAIReader(
            "pgmpy/tests/test_readwrite/testdata/grid4x4_with_triplets.uai")
        self.markov_model = reader_file.get_model()

        for factor in self.markov_model.factors:
            factor.values = np.log(factor.values)
        self.mplp = Mplp(self.markov_model)
Ejemplo n.º 3
0
class TestUAIReader(unittest.TestCase):
    def setUp(self):
        string = """MARKOV
3
2 2 3
2
2 0 1
3 0 1 2

4
 4.000 2.400
 1.000 0.000

12
 2.2500 3.2500 3.7500
 0.0000 0.0000 10.0000
 1.8750 4.0000 3.3330
 2.0000 2.0000 3.4000"""
        self.maxDiff = None
        self.reader_string = UAIReader(string=string)
        self.reader_file = UAIReader(
            'pgmpy/tests/test_readwrite/testdata/grid4x4.uai')

    def test_get_network_type(self):
        network_type_expected = "MARKOV"
        self.assertEqual(self.reader_string.network_type,
                         network_type_expected)

    def test_get_variables(self):
        variables_expected = ['var_0', 'var_1', 'var_2']
        self.assertListEqual(self.reader_string.variables, variables_expected)

    def test_get_domain(self):
        domain_expected = {'var_1': '2', 'var_2': '3', 'var_0': '2'}
        self.assertDictEqual(self.reader_string.domain, domain_expected)

    def test_get_edges(self):
        edges_expected = {('var_0', 'var_1'), ('var_0', 'var_2'),
                          ('var_1', 'var_2')}
        self.assertSetEqual(self.reader_string.edges, edges_expected)

    def test_get_tables(self):
        tables_expected = [(['var_0',
                             'var_1'], ['4.000', '2.400', '1.000', '0.000']),
                           (['var_0', 'var_1', 'var_2'], [
                               '2.2500', '3.2500', '3.7500', '0.0000',
                               '0.0000', '10.0000', '1.8750', '4.0000',
                               '3.3330', '2.0000', '2.0000', '3.4000'
                           ])]
        self.assertListEqual(self.reader_string.tables, tables_expected)

    def test_get_model(self):
        model = self.reader_string.get_model()
        edge_expected = {
            'var_2': {
                'var_0': {},
                'var_1': {}
            },
            'var_0': {
                'var_2': {},
                'var_1': {}
            },
            'var_1': {
                'var_2': {},
                'var_0': {}
            }
        }
        self.assertListEqual(sorted(model.nodes()),
                             sorted(['var_0', 'var_2', 'var_1']))
        self.assertDictEqual(model.edge, edge_expected)

    def test_read_file(self):
        model = self.reader_file.get_model()
        node_expected = {
            'var_3': {},
            'var_8': {},
            'var_5': {},
            'var_14': {},
            'var_15': {},
            'var_0': {},
            'var_9': {},
            'var_7': {},
            'var_6': {},
            'var_13': {},
            'var_10': {},
            'var_12': {},
            'var_1': {},
            'var_11': {},
            'var_2': {},
            'var_4': {}
        }
        self.assertDictEqual(model.node, node_expected)
Ejemplo n.º 4
0
class TestUAIReader(unittest.TestCase):
    def setUp(self):
        string = """MARKOV
3
2 2 3
2
2 0 1
3 0 1 2

4
 4.000 2.400
 1.000 0.000

12
 2.2500 3.2500 3.7500
 0.0000 0.0000 10.0000
 1.8750 4.0000 3.3330
 2.0000 2.0000 3.4000"""
        self.maxDiff = None
        self.reader_string = UAIReader(string=string)
        self.reader_file = UAIReader('pgmpy/tests/test_readwrite/testdata/grid4x4.uai')

    def test_get_network_type(self):
        network_type_expected = "MARKOV"
        self.assertEqual(self.reader_string.network_type, network_type_expected)

    def test_get_variables(self):
        variables_expected = ['var_0', 'var_1', 'var_2']
        self.assertListEqual(self.reader_string.variables, variables_expected)

    def test_get_domain(self):
        domain_expected = {'var_1': '2', 'var_2': '3', 'var_0': '2'}
        self.assertDictEqual(self.reader_string.domain, domain_expected)

    def test_get_edges(self):
        edges_expected = {('var_0', 'var_1'), ('var_0', 'var_2'), ('var_1', 'var_2')}
        self.assertSetEqual(self.reader_string.edges, edges_expected)

    def test_get_tables(self):
        tables_expected = [(['var_0', 'var_1'],
                            ['4.000', '2.400', '1.000', '0.000']),
                           (['var_0', 'var_1', 'var_2'],
                            ['2.2500', '3.2500', '3.7500', '0.0000', '0.0000', '10.0000',
                             '1.8750', '4.0000', '3.3330', '2.0000', '2.0000', '3.4000'])]
        self.assertListEqual(self.reader_string.tables, tables_expected)

    def test_get_model(self):
        model = self.reader_string.get_model()
        edge_expected = {
            'var_2': {'var_0': {'weight': None},
                      'var_1': {'weight': None}},
            'var_0': {'var_2': {'weight': None},
                      'var_1': {'weight': None}},
            'var_1': {'var_2': {'weight': None},
                      'var_0': {'weight': None}}}
        self.assertListEqual(sorted(model.nodes()), sorted(['var_0', 'var_2', 'var_1']))
        self.assertDictEqual(model.edge, edge_expected)

    def test_read_file(self):
        model = self.reader_file.get_model()
        node_expected = {'var_3': {}, 'var_8': {}, 'var_5': {}, 'var_14': {},
                         'var_15': {}, 'var_0': {}, 'var_9': {}, 'var_7': {},
                         'var_6': {}, 'var_13': {}, 'var_10': {}, 'var_12': {},
                         'var_1': {}, 'var_11': {}, 'var_2': {}, 'var_4': {}}
        self.assertDictEqual(model.node, node_expected)
class TestUAIReader(unittest.TestCase):
    def setUp(self):
        string = """MARKOV
3
2 2 3
2
2 0 1
3 0 1 2

4
 4.000 2.400
 1.000 0.000

12
 2.2500 3.2500 3.7500
 0.0000 0.0000 10.0000
 1.8750 4.0000 3.3330
 2.0000 2.0000 3.4000"""
        self.maxDiff = None
        self.reader_string = UAIReader(string=string)
        self.reader_file = UAIReader("pgmpy/tests/test_readwrite/testdata/grid4x4.uai")

    def test_get_network_type(self):
        network_type_expected = "MARKOV"
        self.assertEqual(self.reader_string.network_type, network_type_expected)

    def test_get_variables(self):
        variables_expected = ["var_0", "var_1", "var_2"]
        self.assertListEqual(self.reader_string.variables, variables_expected)

    def test_get_domain(self):
        domain_expected = {"var_1": "2", "var_2": "3", "var_0": "2"}
        self.assertDictEqual(self.reader_string.domain, domain_expected)

    def test_get_edges(self):
        edges_expected = {("var_0", "var_1"), ("var_0", "var_2"), ("var_1", "var_2")}
        self.assertSetEqual(self.reader_string.edges, edges_expected)

    def test_get_tables(self):
        tables_expected = [
            (["var_0", "var_1"], ["4.000", "2.400", "1.000", "0.000"]),
            (
                ["var_0", "var_1", "var_2"],
                [
                    "2.2500",
                    "3.2500",
                    "3.7500",
                    "0.0000",
                    "0.0000",
                    "10.0000",
                    "1.8750",
                    "4.0000",
                    "3.3330",
                    "2.0000",
                    "2.0000",
                    "3.4000",
                ],
            ),
        ]
        self.assertListEqual(self.reader_string.tables, tables_expected)

    def test_get_model(self):
        model = self.reader_string.get_model()
        edge_expected = {
            "var_2": {"var_0": {"weight": None}, "var_1": {"weight": None}},
            "var_0": {"var_2": {"weight": None}, "var_1": {"weight": None}},
            "var_1": {"var_2": {"weight": None}, "var_0": {"weight": None}},
        }

        self.assertListEqual(sorted(model.nodes()), sorted(["var_0", "var_2", "var_1"]))
        if nx.__version__.startswith("1"):
            self.assertDictEqual(dict(model.edge), edge_expected)
        else:
            self.assertDictEqual(dict(model.adj), edge_expected)

    def test_read_file(self):
        model = self.reader_file.get_model()
        node_expected = {
            "var_3": {},
            "var_8": {},
            "var_5": {},
            "var_14": {},
            "var_15": {},
            "var_0": {},
            "var_9": {},
            "var_7": {},
            "var_6": {},
            "var_13": {},
            "var_10": {},
            "var_12": {},
            "var_1": {},
            "var_11": {},
            "var_2": {},
            "var_4": {},
        }
        self.assertDictEqual(dict(model.nodes), node_expected)