-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from causy-dev/d-separation_check
D separation check
- Loading branch information
Showing
4 changed files
with
326 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
from causy.edge_types import DirectedEdge | ||
from causy.graph import GraphBaseAccessMixin, GraphManager | ||
from tests.utils import CausyTestCase | ||
|
||
|
||
class GraphTestCase(CausyTestCase): | ||
def test_all_paths_on_underlying_undirected_graph(self): | ||
new_graph_manager = GraphManager | ||
new_graph_manager.__bases__ = ( | ||
GraphBaseAccessMixin, | ||
DirectedEdge.GraphAccessMixin, | ||
) | ||
graph = new_graph_manager() | ||
node1 = graph.add_node("test1", [1, 2, 3]) | ||
node2 = graph.add_node("test2", [1, 2, 3]) | ||
node3 = graph.add_node("test3", [1, 2, 3]) | ||
graph.add_directed_edge(node1, node2, {"test": "test"}) | ||
graph.add_directed_edge(node2, node3, {"test": "test"}) | ||
self.assertIn( | ||
[node1, node2, node3], | ||
[ | ||
x | ||
for x in [ | ||
l | ||
for l in graph.all_paths_on_underlying_undirected_graph( | ||
node1, node3 | ||
) | ||
] | ||
], | ||
) | ||
self.assertNotIn( | ||
[node1, node2], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
self.assertNotIn( | ||
[node2, node3], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
self.assertNotIn( | ||
[node1, node3], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
|
||
def test_all_paths_on_underlying_undirected_graph_2(self): | ||
new_graph_manager = GraphManager | ||
new_graph_manager.__bases__ = ( | ||
GraphBaseAccessMixin, | ||
DirectedEdge.GraphAccessMixin, | ||
) | ||
graph = new_graph_manager() | ||
node1 = graph.add_node("test1", [1, 2, 3]) | ||
node2 = graph.add_node("test2", [1, 2, 3]) | ||
node3 = graph.add_node("test3", [1, 2, 3]) | ||
graph.add_directed_edge(node3, node2, {"test": "test"}) | ||
graph.add_directed_edge(node2, node1, {"test": "test"}) | ||
self.assertIn( | ||
[node1, node2, node3], | ||
[ | ||
x | ||
for x in [ | ||
l | ||
for l in graph.all_paths_on_underlying_undirected_graph( | ||
node1, node3 | ||
) | ||
] | ||
], | ||
) | ||
self.assertNotIn( | ||
[node1, node2], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
self.assertNotIn( | ||
[node2, node3], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
self.assertNotIn( | ||
[node1, node3], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
|
||
def test_all_paths_on_underlying_undirected_graph_collider_path(self): | ||
new_graph_manager = GraphManager | ||
new_graph_manager.__bases__ = ( | ||
GraphBaseAccessMixin, | ||
DirectedEdge.GraphAccessMixin, | ||
) | ||
graph = new_graph_manager() | ||
node1 = graph.add_node("test1", [1, 2, 3]) | ||
node2 = graph.add_node("test2", [1, 2, 3]) | ||
node3 = graph.add_node("test3", [1, 2, 3]) | ||
graph.add_directed_edge(node3, node2, {"test": "test"}) | ||
graph.add_directed_edge(node1, node2, {"test": "test"}) | ||
self.assertIn( | ||
[node1, node2, node3], | ||
[ | ||
x | ||
for x in [ | ||
l | ||
for l in graph.all_paths_on_underlying_undirected_graph( | ||
node1, node3 | ||
) | ||
] | ||
], | ||
) | ||
self.assertNotIn( | ||
[node1, node2], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
self.assertNotIn( | ||
[node2, node3], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
self.assertNotIn( | ||
[node1, node3], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
|
||
def test_all_paths_on_underlying_undirected_graph_several_paths(self): | ||
new_graph_manager = GraphManager | ||
new_graph_manager.__bases__ = ( | ||
GraphBaseAccessMixin, | ||
DirectedEdge.GraphAccessMixin, | ||
) | ||
graph = new_graph_manager() | ||
node1 = graph.add_node("test1", [1, 2, 3]) | ||
node2 = graph.add_node("test2", [1, 2, 3]) | ||
node3 = graph.add_node("test3", [1, 2, 3]) | ||
graph.add_directed_edge(node1, node2, {"test": "test"}) | ||
graph.add_directed_edge(node2, node3, {"test": "test"}) | ||
graph.add_directed_edge(node1, node3, {"test": "test"}) | ||
self.assertIn( | ||
[node1, node2, node3], | ||
[ | ||
x | ||
for x in [ | ||
l | ||
for l in graph.all_paths_on_underlying_undirected_graph( | ||
node1, node3 | ||
) | ||
] | ||
], | ||
) | ||
self.assertIn( | ||
[node1, node3], | ||
[ | ||
x | ||
for x in [ | ||
l | ||
for l in graph.all_paths_on_underlying_undirected_graph( | ||
node1, node3 | ||
) | ||
] | ||
], | ||
) | ||
self.assertNotIn( | ||
[node1, node2], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
self.assertNotIn( | ||
[node2, node3], | ||
[l for l in graph.all_paths_on_underlying_undirected_graph(node1, node3)], | ||
) | ||
|
||
def test_are_nodes_d_separated(self): | ||
new_graph_manager = GraphManager | ||
new_graph_manager.__bases__ = ( | ||
GraphBaseAccessMixin, | ||
DirectedEdge.GraphAccessMixin, | ||
) | ||
graph = new_graph_manager() | ||
node1 = graph.add_node("test1", [1, 2, 3]) | ||
node2 = graph.add_node("test2", [1, 2, 3]) | ||
node3 = graph.add_node("test3", [1, 2, 3]) | ||
graph.add_directed_edge(node1, node2, {"test": "test"}) | ||
graph.add_directed_edge(node2, node3, {"test": "test"}) | ||
self.assertFalse(graph.are_nodes_d_separated(node1, node3, [])) | ||
|
||
def test_are_nodes_d_separated_2(self): | ||
new_graph_manager = GraphManager | ||
new_graph_manager.__bases__ = ( | ||
GraphBaseAccessMixin, | ||
DirectedEdge.GraphAccessMixin, | ||
) | ||
graph = new_graph_manager() | ||
node1 = graph.add_node("test1", [1, 2, 3]) | ||
node2 = graph.add_node("test2", [1, 2, 3]) | ||
node3 = graph.add_node("test3", [1, 2, 3]) | ||
graph.add_directed_edge(node1, node2, {"test": "test"}) | ||
graph.add_directed_edge(node2, node3, {"test": "test"}) | ||
self.assertTrue(graph.are_nodes_d_separated(node1, node3, [node2])) | ||
|
||
def test_are_nodes_d_separated_3(self): | ||
new_graph_manager = GraphManager | ||
new_graph_manager.__bases__ = ( | ||
GraphBaseAccessMixin, | ||
DirectedEdge.GraphAccessMixin, | ||
) | ||
graph = new_graph_manager() | ||
node1 = graph.add_node("test1", [1, 2, 3]) | ||
node2 = graph.add_node("test2", [1, 2, 3]) | ||
node3 = graph.add_node("test3", [1, 2, 3]) | ||
graph.add_directed_edge(node1, node2, {"test": "test"}) | ||
graph.add_directed_edge(node3, node2, {"test": "test"}) | ||
self.assertTrue(graph.are_nodes_d_separated(node1, node3, [])) | ||
|
||
def test_are_nodes_d_separated_4(self): | ||
new_graph_manager = GraphManager | ||
new_graph_manager.__bases__ = ( | ||
GraphBaseAccessMixin, | ||
DirectedEdge.GraphAccessMixin, | ||
) | ||
graph = new_graph_manager() | ||
node1 = graph.add_node("test1", [1, 2, 3]) | ||
node2 = graph.add_node("test2", [1, 2, 3]) | ||
node3 = graph.add_node("test3", [1, 2, 3]) | ||
graph.add_directed_edge(node1, node2, {"test": "test"}) | ||
graph.add_directed_edge(node3, node2, {"test": "test"}) | ||
self.assertFalse(graph.are_nodes_d_separated(node1, node3, [node2])) | ||
|
||
def test_are_nodes_d_separated_3(self): | ||
new_graph_manager = GraphManager | ||
new_graph_manager.__bases__ = ( | ||
GraphBaseAccessMixin, | ||
DirectedEdge.GraphAccessMixin, | ||
) | ||
graph = new_graph_manager() | ||
node1 = graph.add_node("test1", [1, 2, 3]) | ||
node2 = graph.add_node("test2", [1, 2, 3]) | ||
node3 = graph.add_node("test3", [1, 2, 3]) | ||
graph.add_directed_edge(node1, node2, {"test": "test"}) | ||
graph.add_directed_edge(node3, node2, {"test": "test"}) | ||
graph.add_directed_edge(node1, node3, {"test": "test"}) | ||
self.assertFalse(graph.are_nodes_d_separated(node1, node3, [])) |
Oops, something went wrong.