Repository: top-tier-ci-coders/pgmpy

Issue:
* Rename `DirectedGraph` to `DAG`

Discussion:

Patch:
diff --git a/pgmpy/base/DirectedGraph.py b/pgmpy/base/DAG.py
similarity index 85%
rename from pgmpy/base/DirectedGraph.py
rename to pgmpy/base/DAG.py
index 68ee7d4..28a1cc6 100644
--- a/pgmpy/base/DirectedGraph.py
+++ b/pgmpy/base/DAG.py
@@ -7,7 +7,7 @@ import networkx as nx
 from pgmpy.base import UndirectedGraph
 
 
-class DirectedGraph(nx.DiGraph):
+class DAG(nx.DiGraph):
     """
     Base class for all Directed Graphical Models.
 
@@ -23,10 +23,10 @@ class DirectedGraph(nx.DiGraph):
 
     Examples
     --------
-    Create an empty DirectedGraph with no nodes and no edges
+    Create an empty DAG with no nodes and no edges
 
-    >>> from pgmpy.base import DirectedGraph
-    >>> G = DirectedGraph()
+    >>> from pgmpy.base import DAG
+    >>> G = DAG()
 
     G can be grown in several ways:
 
@@ -68,7 +68,7 @@ class DirectedGraph(nx.DiGraph):
     """
 
     def __init__(self, ebunch=None):
-        super(DirectedGraph, self).__init__(ebunch)
+        super(DAG, self).__init__(ebunch)
 
     def add_node(self, node, weight=None):
         """
@@ -84,8 +84,8 @@ class DirectedGraph(nx.DiGraph):
 
         Examples
         --------
-        >>> from pgmpy.base import DirectedGraph
-        >>> G = DirectedGraph()
+        >>> from pgmpy.base import DAG
+        >>> G = DAG()
         >>> G.add_node(node='A')
         >>> sorted(G.nodes())
         ['A']
@@ -108,7 +108,7 @@ class DirectedGraph(nx.DiGraph):
         else:
             attrs = {'weight': weight}
 
-        super(DirectedGraph, self).add_node(node, weight=weight)
+        super(DAG, self).add_node(node, weight=weight)
 
     def add_nodes_from(self, nodes, weights=None):
         """
@@ -128,8 +128,8 @@ class DirectedGraph(nx.DiGraph):
 
         Examples
         --------
-        >>> from pgmpy.base import DirectedGraph
-        >>> G = DirectedGraph()
+        >>> from pgmpy.base import DAG
+        >>> G = DAG()
         >>> G.add_nodes_from(nodes=['A', 'B', 'C'])
         >>> sorted(G.nodes())
         ['A', 'B', 'C']
@@ -172,8 +172,8 @@ class DirectedGraph(nx.DiGraph):
 
         Examples
         --------
-        >>> from pgmpy.base import DirectedGraph
-        >>> G = DirectedGraph()
+        >>> from pgmpy.base import DAG
+        >>> G = DAG()
         >>> G.add_nodes_from(nodes=['Alice', 'Bob', 'Charles'])
         >>> G.add_edge(u='Alice', v='Bob')
         >>> G.nodes()
@@ -193,7 +193,7 @@ class DirectedGraph(nx.DiGraph):
         >>> G.edge['Ankur']['Maria']
         {'weight': 0.1}
         """
-        super(DirectedGraph, self).add_edge(u, v, weight=weight)
+        super(DAG, self).add_edge(u, v, weight=weight)
 
     def add_edges_from(self, ebunch, weights=None):
         """
@@ -217,8 +217,8 @@ class DirectedGraph(nx.DiGraph):
 
         Examples
         --------
-        >>> from pgmpy.base import DirectedGraph
-        >>> G = DirectedGraph()
+        >>> from pgmpy.base import DAG
+        >>> G = DAG()
         >>> G.add_nodes_from(nodes=['Alice', 'Bob', 'Charles'])
         >>> G.add_edges_from(ebunch=[('Alice', 'Bob'), ('Bob', 'Charles')])
         >>> G.nodes()
@@ -267,8 +267,8 @@ class DirectedGraph(nx.DiGraph):
 
         Examples
         --------
-        >>> from pgmpy.base import DirectedGraph
-        >>> G = DirectedGraph(ebunch=[('diff', 'grade'), ('intel', 'grade')])
+        >>> from pgmpy.base import DAG
+        >>> G = DAG(ebunch=[('diff', 'grade'), ('intel', 'grade')])
         >>> G.get_parents(node='grade')
         ['diff', 'intel']
         """
@@ -276,7 +276,7 @@ class DirectedGraph(nx.DiGraph):
 
     def moralize(self):
         """
-        Removes all the immoralities in the DirectedGraph and creates a moral
+        Removes all the immoralities in the DAG and creates a moral
         graph (UndirectedGraph).
 
         A v-structure X->Z<-Y is an immorality if there is no directed edge
@@ -284,8 +284,8 @@ class DirectedGraph(nx.DiGraph):
 
         Examples
         --------
-        >>> from pgmpy.base import DirectedGraph
-        >>> G = DirectedGraph(ebunch=[('diff', 'grade'), ('intel', 'grade')])
+        >>> from pgmpy.base import DAG
+        >>> G = DAG(ebunch=[('diff', 'grade'), ('intel', 'grade')])
         >>> moral_graph = G.moralize()
         >>> moral_graph.edges()
         [('intel', 'grade'), ('intel', 'diff'), ('grade', 'diff')]
@@ -304,8 +304,8 @@ class DirectedGraph(nx.DiGraph):
 
         Examples
         --------
-        >>> from pgmpy.base import DirectedGraph
-        >>> graph = DirectedGraph([('A', 'B'), ('B', 'C'), ('B', 'D')])
+        >>> from pgmpy.base import DAG
+        >>> graph = DAG([('A', 'B'), ('B', 'C'), ('B', 'D')])
         >>> graph.get_leaves()
         ['C', 'D']
         """
@@ -314,13 +314,13 @@ class DirectedGraph(nx.DiGraph):
 
     def out_degree_iter(self, nbunch=None, weight=None):
         if nx.__version__.startswith('1'):
-            return super(DirectedGraph, self).out_degree_iter(nbunch, weight)
+            return super(DAG, self).out_degree_iter(nbunch, weight)
         else:
             return iter(self.out_degree(nbunch, weight))
 
     def in_degree_iter(self, nbunch=None, weight=None):
         if nx.__version__.startswith('1'):
-            return super(DirectedGraph, self).in_degree_iter(nbunch, weight)
+            return super(DAG, self).in_degree_iter(nbunch, weight)
         else:
             return iter(self.in_degree(nbunch, weight))
 
@@ -330,8 +330,8 @@ class DirectedGraph(nx.DiGraph):
 
         Examples
         --------
-        >>> from pgmpy.base import DirectedGraph
-        >>> graph = DirectedGraph([('A', 'B'), ('B', 'C'), ('B', 'D'), ('E', 'B')])
+        >>> from pgmpy.base import DAG
+        >>> graph = DAG([('A', 'B'), ('B', 'C'), ('B', 'D'), ('E', 'B')])
         >>> graph.get_roots()
         ['A', 'E']
         """
@@ -349,8 +349,8 @@ class DirectedGraph(nx.DiGraph):
 
         Examples
         --------
-        >>> from pgmpy.base import DirectedGraph
-        >>> g = DirectedGraph(ebunch=[('A', 'B'), ('C', 'B'), ('B', 'D'),
+        >>> from pgmpy.base import DAG
+        >>> g = DAG(ebunch=[('A', 'B'), ('C', 'B'), ('B', 'D'),
                                       ('B', 'E'), ('B', 'F'), ('E', 'G')])
         >>> g.get_children(node='B')
         ['D', 'E', 'F']
diff --git a/pgmpy/base/__init__.py b/pgmpy/base/__init__.py
index ef47194..8df8a4d 100644
--- a/pgmpy/base/__init__.py
+++ b/pgmpy/base/__init__.py
@@ -1,5 +1,5 @@
 from .UndirectedGraph import UndirectedGraph
-from .DirectedGraph import DirectedGraph
+from .DAG import DAG
 
 __all__ = ['UndirectedGraph',
-           'DirectedGraph', ]
+           'DAG', ]
diff --git a/pgmpy/estimators/ConstraintBasedEstimator.py b/pgmpy/estimators/ConstraintBasedEstimator.py
index b3ba7d4..06c88de 100644
--- a/pgmpy/estimators/ConstraintBasedEstimator.py
+++ b/pgmpy/estimators/ConstraintBasedEstimator.py
@@ -240,7 +240,7 @@ class ConstraintBasedEstimator(StructureEstimator):
 
         Parameters
         ----------
-        pdag: DirectedGraph
+        pdag: DAG
             A directed acyclic graph pattern, consisting in (acyclic) directed edges
             as well as "undirected" edges, represented as both-way edges between
             nodes.
@@ -264,7 +264,7 @@ class ConstraintBasedEstimator(StructureEstimator):
         --------
         >>> import pandas as pd
         >>> import numpy as np
-        >>> from pgmpy.base import DirectedGraph
+        >>> from pgmpy.base import DAG
         >>> from pgmpy.estimators import ConstraintBasedEstimator
         >>> data = pd.DataFrame(np.random.randint(0, 4, size=(5000, 3)), columns=list('ABD'))
         >>> data['C'] = data['A'] - data['B']
@@ -277,12 +277,12 @@ class ConstraintBasedEstimator(StructureEstimator):
         [('B', 'C'), ('A', 'D'), ('A', 'C')]
 
         >>> # pdag_to_dag is static:
-        ... pdag1 = DirectedGraph([('A', 'B'), ('C', 'B'), ('C', 'D'), ('D', 'C'), ('D', 'A'), ('A', 'D')])
+        ... pdag1 = DAG([('A', 'B'), ('C', 'B'), ('C', 'D'), ('D', 'C'), ('D', 'A'), ('A', 'D')])
         >>> ConstraintBasedEstimator.pdag_to_dag(pdag1).edges()
         [('D', 'C'), ('C', 'B'), ('A', 'B'), ('A', 'D')]
 
         >>> # example of a pdag with no faithful extension:
-        ... pdag2 = DirectedGraph([('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'B')])
+        ... pdag2 = DAG([('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'B')])
         >>> ConstraintBasedEstimator.pdag_to_dag(pdag2).edges()
         UserWarning: PDAG has no faithful extension (= no oriented DAG with the same v-structures as PDAG).
         Remaining undirected PDAG edges oriented arbitrarily.
@@ -354,7 +354,7 @@ class ConstraintBasedEstimator(StructureEstimator):
     @staticmethod
     def skeleton_to_pdag(skel, separating_sets):
         """Orients the edges of a graph skeleton based on information from
-        `separating_sets` to form a DAG pattern (DirectedGraph).
+        `separating_sets` to form a DAG pattern (DAG).
 
         Parameters
         ----------
@@ -369,7 +369,7 @@ class ConstraintBasedEstimator(StructureEstimator):
 
         Returns
         -------
-        pdag: DirectedGraph
+        pdag: DAG
             An estimate for the DAG pattern of the BN underlying the data. The
             graph might contain some nodes with both-way edges (X->Y and Y->X).
             Any completion by (removing one of the both-way edges for each such
diff --git a/pgmpy/models/BayesianModel.py b/pgmpy/models/BayesianModel.py
index 00d361b..338424d 100644
--- a/pgmpy/models/BayesianModel.py
+++ b/pgmpy/models/BayesianModel.py
@@ -9,7 +9,7 @@ import networkx as nx
 import numpy as np
 import pandas as pd
 
-from pgmpy.base import DirectedGraph
+from pgmpy.base import DAG
 from pgmpy.factors.discrete import TabularCPD, JointProbabilityDistribution, DiscreteFactor
 from pgmpy.factors.continuous import ContinuousFactor
 from pgmpy.independencies import Independencies
@@ -18,7 +18,7 @@ from pgmpy.extern.six.moves import range, reduce
 from pgmpy.models.MarkovModel import MarkovModel
 
 
-class BayesianModel(DirectedGraph):
+class BayesianModel(DAG):
     """
     Base class for bayesian model.
 
diff --git a/pgmpy/models/DynamicBayesianNetwork.py b/pgmpy/models/DynamicBayesianNetwork.py
index e85b98d..67b968d 100644
--- a/pgmpy/models/DynamicBayesianNetwork.py
+++ b/pgmpy/models/DynamicBayesianNetwork.py
@@ -5,10 +5,10 @@ import numpy as np
 import networkx as nx
 
 from pgmpy.factors.discrete import TabularCPD
-from pgmpy.base import DirectedGraph
+from pgmpy.base import DAG
 
 
-class DynamicBayesianNetwork(DirectedGraph):
+class DynamicBayesianNetwork(DAG):
     def __init__(self, ebunch=None):
         """
         Base class for Dynamic Bayesian Network


Test patch:
diff --git a/pgmpy/tests/test_base/test_DirectedGraph.py b/pgmpy/tests/test_base/test_DAG.py
similarity index 93%
rename from pgmpy/tests/test_base/test_DirectedGraph.py
rename to pgmpy/tests/test_base/test_DAG.py
index e2d4030..60926e7 100644
--- a/pgmpy/tests/test_base/test_DirectedGraph.py
+++ b/pgmpy/tests/test_base/test_DAG.py
@@ -2,20 +2,20 @@
 
 import unittest
 
-from pgmpy.base import DirectedGraph
+from pgmpy.base import DAG
 import pgmpy.tests.help_functions as hf
 import networkx as nx
 
 
-class TestDirectedGraphCreation(unittest.TestCase):
+class TestDAGCreation(unittest.TestCase):
     def setUp(self):
-        self.graph = DirectedGraph()
+        self.graph = DAG()
 
     def test_class_init_without_data(self):
-        self.assertIsInstance(self.graph, DirectedGraph)
+        self.assertIsInstance(self.graph, DAG)
 
     def test_class_init_with_data_string(self):
-        self.graph = DirectedGraph([('a', 'b'), ('b', 'c')])
+        self.graph = DAG([('a', 'b'), ('b', 'c')])
         self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'c'])
         self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                              [['a', 'b'], ['b', 'c']])
@@ -98,7 +98,7 @@ class TestDirectedGraphCreation(unittest.TestCase):
             self.assertEqual(self.graph.adj['e']['f']['weight'], None)
 
     def test_update_node_parents_bm_constructor(self):
-        self.graph = DirectedGraph([('a', 'b'), ('b', 'c')])
+        self.graph = DAG([('a', 'b'), ('b', 'c')])
         self.assertListEqual(list(self.graph.predecessors('a')), [])
         self.assertListEqual(list(self.graph.predecessors('b')), ['a'])
         self.assertListEqual(list(self.graph.predecessors('c')), ['b'])
@@ -127,9 +127,9 @@ class TestDirectedGraphCreation(unittest.TestCase):
         del self.graph
 
 
-class TestDirectedGraphMoralization(unittest.TestCase):
+class TestDAGMoralization(unittest.TestCase):
     def setUp(self):
-        self.graph = DirectedGraph()
+        self.graph = DAG()
         self.graph.add_edges_from([('diff', 'grade'), ('intel', 'grade')])
 
     def test_get_parents(self):
diff --git a/pgmpy/tests/test_estimators/test_ConstraintBasedEstimator.py b/pgmpy/tests/test_estimators/test_ConstraintBasedEstimator.py
index 9ed59f5..6446632 100644
--- a/pgmpy/tests/test_estimators/test_ConstraintBasedEstimator.py
+++ b/pgmpy/tests/test_estimators/test_ConstraintBasedEstimator.py
@@ -6,7 +6,7 @@ import numpy as np
 from pgmpy.estimators import ConstraintBasedEstimator
 from pgmpy.independencies import Independencies
 from pgmpy.models import BayesianModel
-from pgmpy.base import DirectedGraph, UndirectedGraph
+from pgmpy.base import DAG, UndirectedGraph
 
 
 class TestConstraintBasedEstimator(unittest.TestCase):
@@ -54,18 +54,18 @@ class TestConstraintBasedEstimator(unittest.TestCase):
                             set([('A', 'B'), ('B', 'A'), ('A', 'C'), ('C', 'A')]))
 
     def test_pdag_to_dag(self):
-        pdag1 = DirectedGraph([('A', 'B'), ('C', 'B'), ('C', 'D'), ('D', 'C'), ('D', 'A'), ('A', 'D')])
+        pdag1 = DAG([('A', 'B'), ('C', 'B'), ('C', 'D'), ('D', 'C'), ('D', 'A'), ('A', 'D')])
         dag1 = ConstraintBasedEstimator.pdag_to_dag(pdag1)
         self.assertTrue(('A', 'B') in dag1.edges() and
                         ('C', 'B') in dag1.edges() and
                         len(dag1.edges()) == 4)
 
-        pdag2 = DirectedGraph([('B', 'C'), ('D', 'A'), ('A', 'D'), ('A', 'C')])
+        pdag2 = DAG([('B', 'C'), ('D', 'A'), ('A', 'D'), ('A', 'C')])
         dag2 = ConstraintBasedEstimator.pdag_to_dag(pdag2)
         self.assertTrue(set(dag2.edges()) == set([('B', 'C'), ('A', 'D'), ('A', 'C')]) or
                         set(dag2.edges()) == set([('B', 'C'), ('D', 'A'), ('A', 'C')]))
 
-        pdag3 = DirectedGraph([('B', 'C'), ('D', 'C'), ('C', 'D'), ('A', 'C')])
+        pdag3 = DAG([('B', 'C'), ('D', 'C'), ('C', 'D'), ('A', 'C')])
         dag3 = ConstraintBasedEstimator.pdag_to_dag(pdag3)
         self.assertSetEqual(set([('B', 'C'), ('C', 'D'), ('A', 'C')]),
                             set(dag3.edges()))
diff --git a/pgmpy/tests/test_models/test_BayesianModel.py b/pgmpy/tests/test_models/test_BayesianModel.py
index 7e3045c..2c7ecf0 100644
--- a/pgmpy/tests/test_models/test_BayesianModel.py
+++ b/pgmpy/tests/test_models/test_BayesianModel.py
@@ -551,7 +551,7 @@ class TestBayesianModelFitPredict(unittest.TestCase):
         del self.model_disconnected
 
 
-class TestDirectedGraphCPDOperations(unittest.TestCase):
+class TestDAGCPDOperations(unittest.TestCase):
 
     def setUp(self):
         self.graph = BayesianModel()