Repository: pgmpy/pgmpy (python)

Issue:
Remove nodes without CPD
[This line](https://github.com/pgmpy/pgmpy/blob/dev/pgmpy/models/BayesianModel.py#L165) checks whether the node has CPD by getting its CPD. However, get_cpds will raise an error if the node doesn't have a CPD. So that line will break when there is no CPD for the node.

@ankurankan I can fix it, but I need your input on whether to have get_cpds return None if the CPD doesn't exist, or to check the existence of CPD in other ways?

Pull request description:
Fixed #872
@ankurankan Please review

Patch:
diff --git a/pgmpy/models/BayesianModel.py b/pgmpy/models/BayesianModel.py
index 7d8cb8ba..1823c05c 100644
--- a/pgmpy/models/BayesianModel.py
+++ b/pgmpy/models/BayesianModel.py
@@ -285,7 +285,8 @@ class BayesianModel(DirectedGraph):
             for cpd in self.cpds:
                 if cpd.variable == node:
                     return cpd
-            raise ValueError("CPD not added for the node: {node}".format(node=node))
+            else:
+                return None
         else:
             return self.cpds
 
@@ -346,7 +347,9 @@ class BayesianModel(DirectedGraph):
         for node in self.nodes():
             cpd = self.get_cpds(node=node)
 
-            if isinstance(cpd, TabularCPD):
+            if cpd is None:
+                raise ValueError('No CPD associated with {}'.format(node))
+            elif isinstance(cpd, TabularCPD):
                 evidence = cpd.variables[:0:-1]
                 parents = self.get_parents(node)
                 if set(evidence if evidence else []) != set(parents if parents else []):


Test patch:
diff --git a/pgmpy/tests/test_models/test_BayesianModel.py b/pgmpy/tests/test_models/test_BayesianModel.py
index 7f876519..8d5a304e 100644
--- a/pgmpy/tests/test_models/test_BayesianModel.py
+++ b/pgmpy/tests/test_models/test_BayesianModel.py
@@ -282,7 +282,7 @@ class TestBayesianModelCPD(unittest.TestCase):
         self.assertRaises(ValueError, self.model.get_cpds, 'B')
 
         self.model.add_node('B')
-        self.assertRaises(ValueError, self.model.get_cpds, 'B')
+        self.assertIsNone(self.model.get_cpds('B'))
 
     def test_add_single_cpd(self):
         cpd_s = TabularCPD('s', 2, np.random.rand(2, 2), ['i'], [2])