<commit_before>import networkx
import pygraphviz
from indra.explanation import paths_graph

source = 'A'
target = 'D'
target_polarity = 0

graph1_s = networkx.DiGraph()
graph1_s.add_nodes_from(['A', 'B', 'C', 'D'])
graph1_s.add_edges_from([('A', 'B', {'sign': 0}),
                         ('B', 'D', {'sign': 0}),
                         ('A', 'C', {'sign': 0}),
                         ('C', 'D', {'sign': 0})])

graph1_uns = networkx.DiGraph()
graph1_uns.add_nodes_from(['A', 'B', 'C', 'D'])
graph1_uns.add_edges_from([('A', 'B'), ('B', 'D'), ('A', 'C'), ('C', 'D')])

"""
graph2 = networkx.DiGraph()
graph2.add_nodes_from(['A', 'B', 'C', 'D'])
graph2.add_edges_from([('A', 'B', {'sign': 0}), ('B', 'A', {'sign': 0}),
                      ('A', 'C', {'sign': 0}), ('C', 'D', {'sign': 0})])

"""

def test_get_reachable_sets_unsigned():
    f_level, b_level = paths_graph.get_reachable_sets(
                                    graph1_uns, source, target, signed=False)
    assert f_level == {0: {'A'}, 1: {'B', 'C'}, 2: {'D'}}
    assert b_level == {0: {'D'}, 1: {'B', 'C'}, 2: {'A'}}


def test_get_reachable_sets_signed():
    f_level, b_level = paths_graph.get_reachable_sets(
                                    graph1_s, source, target, signed=True)
    assert f_level == {0: {('A', 0)}, 1: {('B', 0), ('C', 0)}, 2: {('D', 0)}}
    assert b_level == {0: {('D', 0)}, 1: {('B', 0), ('C', 0)}, 2: {('A', 0)}}


def test_unreachability_unsigned():
    graph = networkx.DiGraph()
    graph.add_nodes_from(['A', 'B', 'C', 'D'])
    graph.add_edges_from([('A', 'B'), ('D', 'B'), ('C', 'A'), ('C', 'D')])
    (f_level, b_level) = paths_graph.get_reachable_sets(graph, source, target,
                                                    max_depth=5, signed=False)
    assert f_level is None
    assert b_level is None


def test_unreachability_signed():
    # First make the unreachability due to the direction of the edges
    graph = networkx.DiGraph()
    graph.add_nodes_from(['A', 'B', 'C', 'D'])
    graph.add_edges_from([('A', 'B', {'sign': 0}),
                          ('D', 'B', {'sign': 0}),
                          ('C', 'A', {'sign': 0}),
                          ('C', 'D', {'sign': 0})])
    (f_level, b_level) = paths_graph.get_reachable_sets(graph, source, target,
                                                    max_depth=5, signed=True)
    assert f_level is None
    assert b_level is None
    # This time, make the unreachability due to the sign
    graph = networkx.DiGraph()
    graph.add_nodes_from(['A', 'B', 'C', 'D'])
    graph.add_edges_from([('A', 'B', {'sign': 0}),
                          ('D', 'B', {'sign': 0}),
                          ('C', 'A', {'sign': 0}),
                          ('C', 'D', {'sign': 0})])
    (f_level, b_level) = paths_graph.get_reachable_sets(graph, source, target,
                                                    max_depth=5, signed=True)
    assert f_level is None
    assert b_level is None


def test_paths_graph_unsigned():
    # Path length 1
    f_level, b_level = paths_graph.get_reachable_sets(graph1_s, source, target,
                                 max_depth=3, signed=False)
    pg = paths_graph.paths_graph(graph1_uns, source, target, 1, f_level,
                                 b_level, signed=False)
    assert len(pg) == 0
    # Path length 2
    pg = paths_graph.paths_graph(graph1_uns, source, target, 2, f_level,
                                 b_level, signed=False)
    paths = list(networkx.shortest_simple_paths(pg, (0, 'A'), (2, 'D')))
    assert len(paths) == 2
    assert [(0, 'A'), (1, 'C'), (2, 'D')] in paths
    assert [(0, 'A'), (1, 'B'), (2, 'D')] in paths
    # Path length 3
    pg = paths_graph.paths_graph(graph1_uns, source, target, 3, f_level,
                                 b_level, signed=False)
    assert len(pg) == 0


def test_paths_graph_signed():
    # Path length 1
    f_level, b_level = paths_graph.get_reachable_sets(graph1_s, source, target,
                                 signed=True, max_depth=3)
    pg = paths_graph.paths_graph(graph1_s, source, target, 1, f_level, b_level,
                                 signed=True, target_polarity=0)
    assert not pg
    # Path length 2
    pg = paths_graph.paths_graph(graph1_s, source, target, 2, f_level, b_level, 
                                 signed=True, target_polarity=0)
    paths = list(networkx.shortest_simple_paths(pg, (0, ('A', 0)),
                                                    (2, ('D', 0))))
    assert len(paths) == 2
    assert [(0, ('A', 0)), (1, ('C', 0)), (2, ('D', 0))] in paths
    assert [(0, ('A', 0)), (1, ('B', 0)), (2, ('D', 0))] in paths
    # Path length 3
    pg = paths_graph.paths_graph(graph1_s, source, target, 3, f_level, b_level,
                                 signed=True, target_polarity=0)
    assert not pg


def test_pg_check_unreachable_unsigned():
    graph = networkx.DiGraph()
    graph.add_nodes_from(['A', 'B', 'C', 'D'])
    graph.add_edges_from([('A', 'B'), ('D', 'B'), ('C', 'A'), ('C', 'D')])
    (f_level, b_level) = paths_graph.get_reachable_sets(graph, source, target,
                                                    max_depth=5, signed=False)
    assert f_level is None
    assert b_level is None
    pg = paths_graph.paths_graph(graph, source, target, 2, f_level,
                                 b_level, signed=False)
    assert not pg
    # A graph where there is a path, but not of the given length (3)
    (f_level, b_level) = paths_graph.get_reachable_sets(graph1_s, source,
                                            target, max_depth=5, signed=False)
    pg = paths_graph.paths_graph(graph, source, target, 3, f_level, b_level,
                                 signed=False)
    assert not pg


def test_multidigraph_signed():
    graph = networkx.MultiDiGraph()
    graph.add_edges_from([('A', 'B', {'sign': 0}), ('A', 'B', {'sign': 1})])
    f_level, b_level = paths_graph.get_reachable_sets(graph, 'A', 'B',
                                                      max_depth=3, signed=True)
    assert f_level[0] == {('A', 0)}
    assert f_level[1] == {('B', 0), ('B', 1)}
    assert b_level[0] == {('B', 0)}
    assert b_level[1] == {('A', 0), ('A', 1)}


if __name__ == '__main__':
    test_multidigraph_signed()
    #test_regression_on_er_graph()
    #test_get_reachable_sets_unsigned()
    #test_paths_graph_unsigned()
    #test_simple_unreachability_signed()
<commit_msg>Update paths_graph test to point to new module<commit_after>import networkx
import pygraphviz
from indra.explanation.paths_graph import paths_graph

source = 'A'
target = 'D'
target_polarity = 0

graph1_s = networkx.DiGraph()
graph1_s.add_nodes_from(['A', 'B', 'C', 'D'])
graph1_s.add_edges_from([('A', 'B', {'sign': 0}),
                         ('B', 'D', {'sign': 0}),
                         ('A', 'C', {'sign': 0}),
                         ('C', 'D', {'sign': 0})])

graph1_uns = networkx.DiGraph()
graph1_uns.add_nodes_from(['A', 'B', 'C', 'D'])
graph1_uns.add_edges_from([('A', 'B'), ('B', 'D'), ('A', 'C'), ('C', 'D')])

"""
graph2 = networkx.DiGraph()
graph2.add_nodes_from(['A', 'B', 'C', 'D'])
graph2.add_edges_from([('A', 'B', {'sign': 0}), ('B', 'A', {'sign': 0}),
                      ('A', 'C', {'sign': 0}), ('C', 'D', {'sign': 0})])

"""

def test_get_reachable_sets_unsigned():
    f_level, b_level = paths_graph.get_reachable_sets(
                                    graph1_uns, source, target, signed=False)
    assert f_level == {0: {'A'}, 1: {'B', 'C'}, 2: {'D'}}
    assert b_level == {0: {'D'}, 1: {'B', 'C'}, 2: {'A'}}


def test_get_reachable_sets_signed():
    f_level, b_level = paths_graph.get_reachable_sets(
                                    graph1_s, source, target, signed=True)
    assert f_level == {0: {('A', 0)}, 1: {('B', 0), ('C', 0)}, 2: {('D', 0)}}
    assert b_level == {0: {('D', 0)}, 1: {('B', 0), ('C', 0)}, 2: {('A', 0)}}


def test_unreachability_unsigned():
    graph = networkx.DiGraph()
    graph.add_nodes_from(['A', 'B', 'C', 'D'])
    graph.add_edges_from([('A', 'B'), ('D', 'B'), ('C', 'A'), ('C', 'D')])
    (f_level, b_level) = paths_graph.get_reachable_sets(graph, source, target,
                                                    max_depth=5, signed=False)
    assert f_level is None
    assert b_level is None


def test_unreachability_signed():
    # First make the unreachability due to the direction of the edges
    graph = networkx.DiGraph()
    graph.add_nodes_from(['A', 'B', 'C', 'D'])
    graph.add_edges_from([('A', 'B', {'sign': 0}),
                          ('D', 'B', {'sign': 0}),
                          ('C', 'A', {'sign': 0}),
                          ('C', 'D', {'sign': 0})])
    (f_level, b_level) = paths_graph.get_reachable_sets(graph, source, target,
                                                    max_depth=5, signed=True)
    assert f_level is None
    assert b_level is None
    # This time, make the unreachability due to the sign
    graph = networkx.DiGraph()
    graph.add_nodes_from(['A', 'B', 'C', 'D'])
    graph.add_edges_from([('A', 'B', {'sign': 0}),
                          ('D', 'B', {'sign': 0}),
                          ('C', 'A', {'sign': 0}),
                          ('C', 'D', {'sign': 0})])
    (f_level, b_level) = paths_graph.get_reachable_sets(graph, source, target,
                                                    max_depth=5, signed=True)
    assert f_level is None
    assert b_level is None


def test_paths_graph_unsigned():
    # Path length 1
    f_level, b_level = paths_graph.get_reachable_sets(graph1_s, source, target,
                                 max_depth=3, signed=False)
    pg = paths_graph.paths_graph(graph1_uns, source, target, 1, f_level,
                                 b_level, signed=False)
    assert len(pg) == 0
    # Path length 2
    pg = paths_graph.paths_graph(graph1_uns, source, target, 2, f_level,
                                 b_level, signed=False)
    paths = list(networkx.shortest_simple_paths(pg, (0, 'A'), (2, 'D')))
    assert len(paths) == 2
    assert [(0, 'A'), (1, 'C'), (2, 'D')] in paths
    assert [(0, 'A'), (1, 'B'), (2, 'D')] in paths
    # Path length 3
    pg = paths_graph.paths_graph(graph1_uns, source, target, 3, f_level,
                                 b_level, signed=False)
    assert len(pg) == 0


def test_paths_graph_signed():
    # Path length 1
    f_level, b_level = paths_graph.get_reachable_sets(graph1_s, source, target,
                                 signed=True, max_depth=3)
    pg = paths_graph.paths_graph(graph1_s, source, target, 1, f_level, b_level,
                                 signed=True, target_polarity=0)
    assert not pg
    # Path length 2
    pg = paths_graph.paths_graph(graph1_s, source, target, 2, f_level, b_level, 
                                 signed=True, target_polarity=0)
    paths = list(networkx.shortest_simple_paths(pg, (0, ('A', 0)),
                                                    (2, ('D', 0))))
    assert len(paths) == 2
    assert [(0, ('A', 0)), (1, ('C', 0)), (2, ('D', 0))] in paths
    assert [(0, ('A', 0)), (1, ('B', 0)), (2, ('D', 0))] in paths
    # Path length 3
    pg = paths_graph.paths_graph(graph1_s, source, target, 3, f_level, b_level,
                                 signed=True, target_polarity=0)
    assert not pg


def test_pg_check_unreachable_unsigned():
    graph = networkx.DiGraph()
    graph.add_nodes_from(['A', 'B', 'C', 'D'])
    graph.add_edges_from([('A', 'B'), ('D', 'B'), ('C', 'A'), ('C', 'D')])
    (f_level, b_level) = paths_graph.get_reachable_sets(graph, source, target,
                                                    max_depth=5, signed=False)
    assert f_level is None
    assert b_level is None
    pg = paths_graph.paths_graph(graph, source, target, 2, f_level,
                                 b_level, signed=False)
    assert not pg
    # A graph where there is a path, but not of the given length (3)
    (f_level, b_level) = paths_graph.get_reachable_sets(graph1_s, source,
                                            target, max_depth=5, signed=False)
    pg = paths_graph.paths_graph(graph, source, target, 3, f_level, b_level,
                                 signed=False)
    assert not pg


def test_multidigraph_signed():
    graph = networkx.MultiDiGraph()
    graph.add_edges_from([('A', 'B', {'sign': 0}), ('A', 'B', {'sign': 1})])
    f_level, b_level = paths_graph.get_reachable_sets(graph, 'A', 'B',
                                                      max_depth=3, signed=True)
    assert f_level[0] == {('A', 0)}
    assert f_level[1] == {('B', 0), ('B', 1)}
    assert b_level[0] == {('B', 0)}
    assert b_level[1] == {('A', 0), ('A', 1)}


if __name__ == '__main__':
    test_multidigraph_signed()
    #test_regression_on_er_graph()
    #test_get_reachable_sets_unsigned()
    #test_paths_graph_unsigned()
    #test_simple_unreachability_signed()

{'fraction_non_alphanumeric': 0.1763522401919059, 'fraction_numerical': 0.01903582759421187, 'mean_word_length': 2.2529574628744022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7367793', 'n_tokens_mistral': 4508, 'n_tokens_neox': 4329, 'n_words': 1093}