Repository: pgmpy/pgmpy
Pull request #819 (2017-01-11 15:36:41)

Issue#722 : changed get_cpd in TabularCPD to get_values
In reference to issue #722 : get_cpd in TabularCPD does not seem to be a suitable name; (Closed the old PR due to unintended changes made to the dev branch and commit history)


Patch:
diff --git a/pgmpy/factors/discrete/CPD.py b/pgmpy/factors/discrete/CPD.py
index 4b678be6..77de7668 100644
--- a/pgmpy/factors/discrete/CPD.py
+++ b/pgmpy/factors/discrete/CPD.py
@@ -94,7 +94,7 @@ class TabularCPD(DiscreteFactor):
 
     Public Methods
     --------------
-    get_cpd()
+    get_values()
     marginalize([variables_list])
     normalize()
     reduce([values_list])
@@ -146,7 +146,7 @@ class TabularCPD(DiscreteFactor):
 
         return var_str + evidence_str + ') at {address}>'.format(address=hex(id(self)))
 
-    def get_cpd(self):
+    def get_values(self):
         """
         Returns the cpd
         >>> from pgmpy.factors import TabularCPD
@@ -154,7 +154,7 @@ class TabularCPD(DiscreteFactor):
         ...                               [0.1, 0.1],
         ...                               [0.8, 0.8]],
         ...                  evidence='evi1', evidence_card=2)
-        >>> cpd.get_cpd()
+        >>> cpd.get_values()
         array([[ 0.1,  0.1],
                [ 0.1,  0.1],
                [ 0.8,  0.8]])
@@ -202,7 +202,7 @@ class TabularCPD(DiscreteFactor):
         else:
             variable_array = [['{s}_{d}'.format(s=self.variable, d=i) for i in range(self.variable_card)]]
         # Stack with data
-        labeled_rows = np.hstack((np.array(variable_array).T, self.get_cpd())).tolist()
+        labeled_rows = np.hstack((np.array(variable_array).T, self.get_values())).tolist()
         # No support for multi-headers in tabulate
         cdf_str = tabulate(headers_list + labeled_rows, tablefmt=tablefmt)
         return cdf_str
@@ -233,7 +233,7 @@ class TabularCPD(DiscreteFactor):
         """
         evidence = self.variables[1:] if len(self.variables) > 1 else None
         evidence_card = self.cardinality[1:] if len(self.variables) > 1 else None
-        return TabularCPD(self.variable, self.variable_card, self.get_cpd(),
+        return TabularCPD(self.variable, self.variable_card, self.get_values(),
                           evidence, evidence_card)
 
     def normalize(self, inplace=True):
@@ -253,12 +253,12 @@ class TabularCPD(DiscreteFactor):
         ...                        [[0.7, 0.2, 0.6, 0.2],[0.4, 0.4, 0.4, 0.8]],
         ...                        ['intel', 'diff'], [2, 2])
         >>> cpd_table.normalize()
-        >>> cpd_table.get_cpd()
+        >>> cpd_table.get_values()
         array([[ 0.63636364,  0.33333333,  0.6       ,  0.2       ],
                [ 0.36363636,  0.66666667,  0.4       ,  0.8       ]])
         """
         tabular_cpd = self if inplace else self.copy()
-        cpd = tabular_cpd.get_cpd()
+        cpd = tabular_cpd.get_values()
         tabular_cpd.values = (cpd / cpd.sum(axis=0)).reshape(tabular_cpd.cardinality)
         if not inplace:
             return tabular_cpd
@@ -283,7 +283,7 @@ class TabularCPD(DiscreteFactor):
         ...                        [[0.7, 0.6, 0.6, 0.2],[0.3, 0.4, 0.4, 0.8]],
         ...                        ['intel', 'diff'], [2, 2])
         >>> cpd_table.marginalize(['diff'])
-        >>> cpd_table.get_cpd()
+        >>> cpd_table.get_values()
         array([[ 0.65,  0.4 ],
                 [ 0.35,  0.6 ]])
         """
@@ -319,7 +319,7 @@ class TabularCPD(DiscreteFactor):
         ...                        [[0.7, 0.6, 0.6, 0.2],[0.3, 0.4, 0.4, 0.8]],
         ...                        ['intel', 'diff'], [2, 2])
         >>> cpd_table.reduce([('diff', 0)])
-        >>> cpd_table.get_cpd()
+        >>> cpd_table.get_values()
         array([[ 0.7,  0.6],
                [ 0.3,  0.4]])
         """
@@ -456,12 +456,12 @@ class TabularCPD(DiscreteFactor):
                     variables = [self.variables[0]] + new_order
                     cardinality = [self.variable_card] + [card_map[var] for var in new_order]
                     super(TabularCPD, self).__init__(variables, cardinality, new_values.flatten('C'))
-                    return self.get_cpd()
+                    return self.get_values()
                 else:
                     return new_values.reshape(self.cardinality[0], np.prod([card_map[var] for var in new_order]))
             else:
                 warn("Same ordering provided as current")
-                return self.get_cpd()
+                return self.get_values()
 
     def get_evidence(self):
         return self.variables[:0:-1]
diff --git a/pgmpy/readwrite/BIF.py b/pgmpy/readwrite/BIF.py
index 63ae4bf2..fac86ab9 100644
--- a/pgmpy/readwrite/BIF.py
+++ b/pgmpy/readwrite/BIF.py
@@ -61,7 +61,7 @@ class BIFReader(object):
         self.variable_states = self.get_states()
         self.variable_properties = self.get_property()
         self.variable_parents = self.get_parents()
-        self.variable_cpds = self.get_cpd()
+        self.variable_cpds = self.get_values()
         self.variable_edges = self.get_edges()
 
     def get_variable_grammar(self):
@@ -214,7 +214,7 @@ class BIFReader(object):
             variable_parents[names[0]] = names[1:]
         return variable_parents
 
-    def get_cpd(self):
+    def get_values(self):
         """
         Returns the CPD of the variables present in the network
 
@@ -222,7 +222,7 @@ class BIFReader(object):
         --------
         >>> from pgmpy.readwrite import BIFReader
         >>> reader = BIFReader("bif_test.bif")
-        >>> reader.get_cpd()
+        >>> reader.get_values()
         {'bowel-problem': np.array([[0.01],
                                     [0.99]]),
         'dog-out': np.array([[0.99, 0.97, 0.9, 0.3],
diff --git a/pgmpy/readwrite/XMLBIF.py b/pgmpy/readwrite/XMLBIF.py
index 94c7dc92..3dc5bb2d 100644
--- a/pgmpy/readwrite/XMLBIF.py
+++ b/pgmpy/readwrite/XMLBIF.py
@@ -60,7 +60,7 @@ class XMLBIFReader(object):
         self.variable_parents = self.get_parents()
         self.edge_list = self.get_edges()
         self.variable_states = self.get_states()
-        self.variable_CPD = self.get_cpd()
+        self.variable_CPD = self.get_values()
         self.variable_property = self.get_property()
 
     def get_variables(self):
@@ -129,14 +129,14 @@ class XMLBIFReader(object):
                             for definition in self.network.findall('DEFINITION')}
         return variable_parents
 
-    def get_cpd(self):
+    def get_values(self):
         """
         Returns the CPD of the variables present in the network
 
         Examples
         --------
         >>> reader = XMLBIF.XMLBIFReader("xmlbif_test.xml")
-        >>> reader.get_cpd()
+        >>> reader.get_values()
         {'bowel-problem': array([[ 0.01],
                                  [ 0.99]]),
          'dog-out': array([[ 0.99,  0.01,  0.97,  0.03],
@@ -239,7 +239,7 @@ class XMLBIFWriter(object):
         self.states = self.get_states()
         self.properties = self.get_properties()
         self.definition = self.get_definition()
-        self.tables = self.get_cpd()
+        self.tables = self.get_values()
 
     def __str__(self):
         """
@@ -396,7 +396,7 @@ class XMLBIFWriter(object):
 
         return definition_tag
 
-    def get_cpd(self):
+    def get_values(self):
         """
         Add Table to XMLBIF.
 
@@ -407,7 +407,7 @@ class XMLBIFWriter(object):
         Examples
         -------
         >>> writer = XMLBIFWriter(model)
-        >>> writer.get_cpd()
+        >>> writer.get_values()
         {'dog-out': <Element TABLE at 0x7f240726f3c8>,
          'light-on': <Element TABLE at 0x7f240726f488>,
          'bowel-problem': <Element TABLE at 0x7f240726f388>,
@@ -420,7 +420,7 @@ class XMLBIFWriter(object):
         for cpd in cpds:
             table_tag[cpd.variable] = etree.SubElement(definition_tag[cpd.variable], "TABLE")
             table_tag[cpd.variable].text = ''
-            for val in cpd.get_cpd().ravel(order="F"):
+            for val in cpd.get_values().ravel(order="F"):
                 table_tag[cpd.variable].text += str(val) + ' '
 
         return table_tag


Test patch:
diff --git a/pgmpy/tests/test_factors/test_discrete/test_Factor.py b/pgmpy/tests/test_factors/test_discrete/test_Factor.py
index b8b17192..deda235b 100644
--- a/pgmpy/tests/test_factors/test_discrete/test_Factor.py
+++ b/pgmpy/tests/test_factors/test_discrete/test_Factor.py
@@ -616,30 +616,30 @@ class TestTabularCPDMethods(unittest.TestCase):
 
     def test_copy(self):
         copy_cpd = self.cpd.copy()
-        np_test.assert_array_equal(self.cpd.get_cpd(), copy_cpd.get_cpd())
+        np_test.assert_array_equal(self.cpd.get_values(), copy_cpd.get_values())
 
     def test_copy_original_safe(self):
         copy_cpd = self.cpd.copy()
         copy_cpd.reorder_parents(['diff', 'intel'])
-        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
+        np_test.assert_array_equal(self.cpd.get_values(), np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                                                  [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                                                  [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]]))
 
     def test_reduce_1(self):
         self.cpd.reduce([('diff', 0)])
-        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1, 0.1],
+        np_test.assert_array_equal(self.cpd.get_values(), np.array([[0.1, 0.1, 0.1],
                                                                  [0.1, 0.1, 0.1],
                                                                  [0.8, 0.8, 0.8]]))
 
     def test_reduce_2(self):
         self.cpd.reduce([('intel', 0)])
-        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1],
+        np_test.assert_array_equal(self.cpd.get_values(), np.array([[0.1, 0.1],
                                                                  [0.1, 0.1],
                                                                  [0.8, 0.8]]))
 
     def test_reduce_3(self):
         self.cpd.reduce([('intel', 0), ('diff', 0)])
-        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1],
+        np_test.assert_array_equal(self.cpd.get_values(), np.array([[0.1],
                                                                  [0.1],
                                                                  [0.8]]))
 
@@ -653,8 +653,8 @@ class TestTabularCPDMethods(unittest.TestCase):
         self.cpd.reduce([('diff', 1)])
         np_test.assert_array_almost_equal(self.cpd.values, copy_cpd.values)
 
-    def test_get_cpd(self):
-        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
+    def test_get_values(self):
+        np_test.assert_array_equal(self.cpd.get_values(), np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                                                  [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                                                  [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]]))
 
@@ -662,7 +662,7 @@ class TestTabularCPDMethods(unittest.TestCase):
         new_vals = self.cpd2.reorder_parents(['B', 'A', 'C'])
         np_test.assert_array_equal(new_vals, np.array([[0.9, 0.3, 0.8, 0.8, 0.9, 0.3, 0.4, 0.4],
                                                        [0.1, 0.7, 0.2, 0.2, 0.1, 0.7, 0.6, 0.6]]))
-        np_test.assert_array_equal(self.cpd2.get_cpd(), np.array([[0.9, 0.3, 0.8, 0.8, 0.9, 0.3, 0.4, 0.4],
+        np_test.assert_array_equal(self.cpd2.get_values(), np.array([[0.9, 0.3, 0.8, 0.8, 0.9, 0.3, 0.4, 0.4],
                                                                   [0.1, 0.7, 0.2, 0.2, 0.1, 0.7, 0.6, 0.6]]))
 
     def test_reorder_parents(self):
@@ -672,7 +672,7 @@ class TestTabularCPDMethods(unittest.TestCase):
 
     def test_reorder_parents_no_effect(self):
         self.cpd2.reorder_parents(['C', 'A', 'B'], inplace=False)
-        np_test.assert_array_equal(self.cpd2.get_cpd(), np.array([[0.9, 0.3, 0.9, 0.3, 0.8, 0.8, 0.4, 0.4],
+        np_test.assert_array_equal(self.cpd2.get_values(), np.array([[0.9, 0.3, 0.9, 0.3, 0.8, 0.8, 0.4, 0.4],
                                                                   [0.1, 0.7, 0.1, 0.7, 0.2, 0.2, 0.6, 0.6]]))
 
     def test_reorder_parents_warning(self):
@@ -680,7 +680,7 @@ class TestTabularCPDMethods(unittest.TestCase):
             warnings.simplefilter("always")
             self.cpd2.reorder_parents(['A', 'B', 'C'], inplace=False)
             assert("Same ordering provided as current" in str(w[-1].message))
-            np_test.assert_array_equal(self.cpd2.get_cpd(), np.array([[0.9, 0.3, 0.9, 0.3, 0.8, 0.8, 0.4, 0.4],
+            np_test.assert_array_equal(self.cpd2.get_values(), np.array([[0.9, 0.3, 0.9, 0.3, 0.8, 0.8, 0.4, 0.4],
                                                                       [0.1, 0.7, 0.1, 0.7, 0.2, 0.2, 0.6, 0.6]]))
 
     def tearDown(self):
diff --git a/pgmpy/tests/test_models/test_BayesianModel.py b/pgmpy/tests/test_models/test_BayesianModel.py
index 78f51a09..07e847c2 100644
--- a/pgmpy/tests/test_models/test_BayesianModel.py
+++ b/pgmpy/tests/test_models/test_BayesianModel.py
@@ -555,7 +555,7 @@ class TestDirectedGraphCPDOperations(unittest.TestCase):
         self.graph.remove_cpds('diff', 'grade')
         self.assertListEqual(self.graph.get_cpds(), [cpd2])
 
-    def test_get_cpd_for_node(self):
+    def test_get_values_for_node(self):
         cpd1 = TabularCPD('diff', 2, values=np.random.rand(2, 1))
         cpd2 = TabularCPD('intel', 2, values=np.random.rand(2, 1))
         cpd3 = TabularCPD('grade', 2, values=np.random.rand(2, 4),
@@ -566,7 +566,7 @@ class TestDirectedGraphCPDOperations(unittest.TestCase):
         self.assertEqual(self.graph.get_cpds('intel'), cpd2)
         self.assertEqual(self.graph.get_cpds('grade'), cpd3)
 
-    def test_get_cpd_raises_error(self):
+    def test_get_values_raises_error(self):
         cpd1 = TabularCPD('diff', 2, values=np.random.rand(2, 1))
         cpd2 = TabularCPD('intel', 2, values=np.random.rand(2, 1))
         cpd3 = TabularCPD('grade', 2, values=np.random.rand(2, 4),
diff --git a/pgmpy/tests/test_readwrite/test_BIF.py b/pgmpy/tests/test_readwrite/test_BIF.py
index 39560ff2..a7e1f5ff 100644
--- a/pgmpy/tests/test_readwrite/test_BIF.py
+++ b/pgmpy/tests/test_readwrite/test_BIF.py
@@ -93,7 +93,7 @@ probability (  "family-out" ) { //1 variable(s) and 2 values
             self.assertListEqual(property_expected[variable],
                                  prop[variable])
 
-    def test_get_cpd(self):
+    def test_get_values(self):
 
         cpd_expected = {'bowel-problem': np.array([[0.01],
                                                    [0.99]]),
@@ -166,7 +166,7 @@ probability (  "family-out" ) { //1 variable(s) and 2 values
                                    [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(),
+            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)
diff --git a/pgmpy/tests/test_readwrite/test_ProbModelXML.py b/pgmpy/tests/test_readwrite/test_ProbModelXML.py
index 60400afa..39a8fb40 100644
--- a/pgmpy/tests/test_readwrite/test_ProbModelXML.py
+++ b/pgmpy/tests/test_readwrite/test_ProbModelXML.py
@@ -611,7 +611,7 @@ class TestProbModelXMLReaderString(unittest.TestCase):
                          np.array([[0.99, 0.01], [0.95, 0.05]]),
                          np.array([[1, 0, 0, 1], [0, 1, 0, 1]])]
         for cpd_index in range(0, len(cpds_expected)):
-            np_test.assert_array_equal(model.get_cpds()[cpd_index].get_cpd(),
+            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)
diff --git a/pgmpy/tests/test_readwrite/test_XMLBIF.py b/pgmpy/tests/test_readwrite/test_XMLBIF.py
index 425472eb..b0007445 100644
--- a/pgmpy/tests/test_readwrite/test_XMLBIF.py
+++ b/pgmpy/tests/test_readwrite/test_XMLBIF.py
@@ -164,7 +164,7 @@ class TestXMLBIFReaderMethods(unittest.TestCase):
         self.assertListEqual(sorted(self.reader.edge_list),
                              sorted(edges_expected))
 
-    def test_get_cpd(self):
+    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],
@@ -240,7 +240,7 @@ class TestXMLBIFReaderMethodsFile(unittest.TestCase):
         self.assertListEqual(sorted(self.reader.edge_list),
                              sorted(edges_expected))
 
-    def test_get_cpd(self):
+    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],
diff --git a/pgmpy/tests/test_readwrite/test_XMLBeliefNetwork.py b/pgmpy/tests/test_readwrite/test_XMLBeliefNetwork.py
index 7aeedae5..7141359b 100644
--- a/pgmpy/tests/test_readwrite/test_XMLBeliefNetwork.py
+++ b/pgmpy/tests/test_readwrite/test_XMLBeliefNetwork.py
@@ -229,7 +229,7 @@ class TestXBNReader(unittest.TestCase):
                          'd': np.array([[0.8, 0.2, 0.9, 0.1],
                                         [0.7, 0.3, 0.05, 0.95]])}
         for cpd in model.get_cpds():
-            np_test.assert_array_equal(cpd.get_cpd(), cpds_expected[cpd.variable])
+            np_test.assert_array_equal(cpd.get_values(), cpds_expected[cpd.variable])
         self.assertListEqual(sorted(model.edges()), sorted([('b', 'd'), ('a', 'b'), ('a', 'c'),
                                                             ('c', 'd'), ('c', 'e')]))
         self.assertDictEqual(model.node, node_expected)
diff --git a/pgmpy/tests/test_utils/test_state_name.py b/pgmpy/tests/test_utils/test_state_name.py
index 40fced54..871e917a 100644
--- a/pgmpy/tests/test_utils/test_state_name.py
+++ b/pgmpy/tests/test_utils/test_state_name.py
@@ -144,7 +144,7 @@ class StateNameDecorator(unittest.TestCase):
         cpd.reduce([('diff', 'high')])
         self.assertEqual(cpd.variable, 'grade')
         self.assertEqual(cpd.variables, ['grade', 'intel'])
-        np_test.assert_array_equal(cpd.get_cpd(), np.array([[0.1, 0.1, 0.1],
+        np_test.assert_array_equal(cpd.get_values(), np.array([[0.1, 0.1, 0.1],
                                                             [0.1, 0.1, 0.1],
                                                             [0.8, 0.8, 0.8]]))
 
@@ -156,7 +156,7 @@ class StateNameDecorator(unittest.TestCase):
         cpd.reduce([('diff', 0)])
         self.assertEqual(cpd.variable, 'grade')
         self.assertEqual(cpd.variables, ['grade', 'intel'])
-        np_test.assert_array_equal(cpd.get_cpd(), np.array([[0.1, 0.1, 0.1],
+        np_test.assert_array_equal(cpd.get_values(), np.array([[0.1, 0.1, 0.1],
                                                             [0.1, 0.1, 0.1],
                                                             [0.8, 0.8, 0.8]]))
 
@@ -168,7 +168,7 @@ class StateNameDecorator(unittest.TestCase):
         cpd = cpd.reduce([('diff', 'high')], inplace=False)
         self.assertEqual(cpd.variable, 'grade')
         self.assertEqual(cpd.variables, ['grade', 'intel'])
-        np_test.assert_array_equal(cpd.get_cpd(), np.array([[0.1, 0.1, 0.1],
+        np_test.assert_array_equal(cpd.get_values(), np.array([[0.1, 0.1, 0.1],
                                                             [0.1, 0.1, 0.1],
                                                             [0.8, 0.8, 0.8]]))
 
@@ -180,7 +180,7 @@ class StateNameDecorator(unittest.TestCase):
         cpd = cpd.reduce([('diff', 0)], inplace=False)
         self.assertEqual(cpd.variable, 'grade')
         self.assertEqual(cpd.variables, ['grade', 'intel'])
-        np_test.assert_array_equal(cpd.get_cpd(), np.array([[0.1, 0.1, 0.1],
+        np_test.assert_array_equal(cpd.get_values(), np.array([[0.1, 0.1, 0.1],
                                                             [0.1, 0.1, 0.1],
                                                             [0.8, 0.8, 0.8]]))