Repository: pgmpy/pgmpy
Pull request #892 (2017-08-29 13:55:04)

MarkovModel.py: Updated check_model and get_cardinality method.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/13034685/badge)](https://coveralls.io/builds/13034685)

Coverage decreased (-0.1%) to 94.747% when pulling **af89f8a0f557080200a5fef796ee48fdf5584638 on lohani2280:branch5** into **bfe1da094fd8b7fabb2693c6a86d572a6610ef1f on pgmpy:dev**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/13034688/badge)](https://coveralls.io/builds/13034688)

Coverage remained the same at 94.882% when pulling **af89f8a0f557080200a5fef796ee48fdf5584638 on lohani2280:branch5** into **bfe1da094fd8b7fabb2693c6a86d572a6610ef1f on pgmpy:dev**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/13034878/badge)](https://coveralls.io/builds/13034878)

Coverage decreased (-0.02%) to 94.864% when pulling **1d2a48464aca93e19f6f13130550718e88aa4886 on lohani2280:branch5** into **bfe1da094fd8b7fabb2693c6a86d572a6610ef1f on pgmpy:dev**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/13034878/badge)](https://coveralls.io/builds/13034878)

Coverage decreased (-0.02%) to 94.864% when pulling **1d2a48464aca93e19f6f13130550718e88aa4886 on lohani2280:branch5** into **bfe1da094fd8b7fabb2693c6a86d572a6610ef1f on pgmpy:dev**.


Patch:
diff --git a/pgmpy/models/MarkovModel.py b/pgmpy/models/MarkovModel.py
index 48a5a0b6..5049d321 100644
--- a/pgmpy/models/MarkovModel.py
+++ b/pgmpy/models/MarkovModel.py
@@ -177,17 +177,11 @@ class MarkovModel(UndirectedGraph):
         for factor in factors:
             self.factors.remove(factor)
 
-    def get_cardinality(self, check_cardinality=False):
+    def get_cardinality(self):
         """
         Returns a dictionary with the given factors as keys and their respective
         cardinality as values.
 
-        Parameters
-        ----------
-        check_cardinality: boolean, optional
-            If, check_cardinality=True it checks if cardinality information
-            for all the variables is availble or not. If not it raises an error.
-
         Examples
         --------
         >>> from pgmpy.models import MarkovModel
@@ -203,8 +197,6 @@ class MarkovModel(UndirectedGraph):
         for factor in self.factors:
             for variable, cardinality in zip(factor.scope(), factor.cardinality):
                 cardinalities[variable] = cardinality
-        if check_cardinality and len(self.nodes()) != len(cardinalities):
-            raise ValueError('Factors for all the variables not defined')
         return cardinalities
 
     def check_model(self):
@@ -226,6 +218,8 @@ class MarkovModel(UndirectedGraph):
                 if cardinalities[variable] != cardinality:
                     raise ValueError(
                         'Cardinality of variable {var} not matching among factors'.format(var=variable))
+                if len(self.nodes()) != len(cardinalities):
+                    raise ValueError('Factors for all the variables not defined')
             for var1, var2 in itertools.combinations(factor.variables, 2):
                 if var2 not in self.neighbors(var1):
                     raise ValueError("DiscreteFactor inconsistent with the model.")


Test patch:
diff --git a/pgmpy/tests/test_models/test_MarkovModel.py b/pgmpy/tests/test_models/test_MarkovModel.py
index 5ddc6409..4eaa181e 100644
--- a/pgmpy/tests/test_models/test_MarkovModel.py
+++ b/pgmpy/tests/test_models/test_MarkovModel.py
@@ -115,23 +115,19 @@ class TestMarkovModelMethods(unittest.TestCase):
         self.graph.remove_factors(phi1, phi2, phi3)
         self.assertDictEqual(self.graph.get_cardinality(), {})
 
-    def test_get_cardinality_check_cardinality(self):
+    def test_check_model(self):
         self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
                                    ('d', 'a')])
 
         phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2))
         self.graph.add_factors(phi1)
-        self.assertRaises(ValueError, self.graph.get_cardinality, check_cardinality=True)
+        self.assertRaises(ValueError, self.graph.check_model)
 
         phi2 = DiscreteFactor(['a', 'c'], [1, 2], np.random.rand(2))
         self.graph.add_factors(phi2)
-        self.assertRaises(ValueError, self.graph.get_cardinality, check_cardinality=True)
-
-        phi3 = DiscreteFactor(['c', 'd'], [2, 2], np.random.rand(4))
-        self.graph.add_factors(phi3)
-        self.assertDictEqual(self.graph.get_cardinality(check_cardinality=True), {'d': 2, 'c': 2, 'b': 2, 'a': 1})
+        self.assertRaises(ValueError, self.graph.check_model)
 
-    def test_check_model(self):
+    def test_check_model1(self):
         self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
                                    ('d', 'a')])
         phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2))
@@ -147,7 +143,7 @@ class TestMarkovModelMethods(unittest.TestCase):
         self.graph.add_factors(phi1)
         self.assertTrue(self.graph.check_model())
 
-    def test_check_model1(self):
+    def test_check_model2(self):
         self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
                                    ('d', 'a')])
 
@@ -175,7 +171,7 @@ class TestMarkovModelMethods(unittest.TestCase):
         self.assertRaises(ValueError, self.graph.check_model)
         self.graph.remove_factors(phi2)
 
-    def test_check_model2(self):
+    def test_check_model3(self):
         self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
                                    ('d', 'a')])