def check_dag_equal(): chain = Node.from_lists(("a", ("b", ("c", ("d", ))))) d = Node(Frame(name="d")) diamond = Node.from_lists(("a", ("b", d), ("c", d))) tree = Node.from_lists(("a", ("b", "e", "f", "g"), ("c", "e", "f", "g"), ("d", "e", "f", "g"))) assert chain.dag_equal(chain) assert chain.dag_equal(chain.copy()) assert diamond.dag_equal(diamond) assert diamond.dag_equal(diamond.copy()) assert tree.dag_equal(tree) assert tree.dag_equal(tree.copy()) assert not chain.dag_equal(tree) assert not chain.dag_equal(diamond) assert not tree.dag_equal(chain) assert not tree.dag_equal(diamond) assert not diamond.dag_equal(chain) assert not diamond.dag_equal(tree)
def test_filter_squash_bunny_to_goat(): r"""Test squash on a "bunny" shaped graph: This one is more complex because there are more transitive edges to maintain between the roots (e, g) and b and c. e g e g / \ / \ /|\ /|\ f a h remove ac f | b | h / \ ----------> | | | b c \|/ \ / d d """ d = Node(Frame(name="d")) diamond = Node.from_lists(("a", ("b", d), ("c", d))) new_d = Node(Frame(name="d")) new_b = Node.from_lists(("b", new_d)) check_filter_squash( GraphFrame.from_lists(("e", "f", diamond), ("g", diamond, "h")), lambda row: row["node"].frame["name"] not in ("a", "c"), Graph.from_lists(("e", new_b, new_d, "f"), ("g", new_b, new_d, "h")), [4, 2, 1, 1, 4, 1], # e, b, d, f, g, h ) check_filter_no_squash( GraphFrame.from_lists(("e", "f", diamond), ("g", diamond, "h")), lambda row: row["node"].frame["name"] not in ("a", "c"), 6, # e, b, d, f, g, h )
def test_paths(): d = Node(Frame(name="d")) Node.from_lists(["a", ["b", d], ["c", d]]) with pytest.raises(MultiplePathError): d.path() assert d.paths() == [ (Node(Frame(name="a")), Node(Frame(name="b")), Node(Frame(name="d"))), (Node(Frame(name="a")), Node(Frame(name="c")), Node(Frame(name="d"))), ]
def test_paths(): d = Node(Frame(name="d")) Node.from_lists(["a", ["b", d], ["c", d]]) with pytest.raises(MultiplePathError): d.path() assert d.paths() == [ (Frame(name="a"), Frame(name="b"), Frame(name="d")), (Frame(name="a"), Frame(name="c"), Frame(name="d")), ] assert d.paths(attrs="name") == [("a", "b", "d"), ("a", "c", "d")]
def test_from_lists(): node = Node.from_lists("a") assert node.frame == Frame(name="a") a = Frame(name="a") b = Frame(name="b") c = Frame(name="c") node = Node.from_lists(["a", ["b", "c"]]) assert node.frame == a assert node.children[0].frame == b assert node.children[0].children[0].frame == c
def test_traverse_pre(): node = Node(Frame(name="a")) assert list(node.traverse(attrs="name")) == ["a"] node = Node.from_lists(["a", ["b", "d", "e"], ["c", "f", "g"]]) assert list( node.traverse(attrs="name")) == ["a", "b", "d", "e", "c", "f", "g"]
def test_copy(): d = Node(Frame(name="d")) diamond_subdag = Node.from_lists(("a", ("b", d), ("c", d))) g = Graph.from_lists(("e", "f", diamond_subdag), ("g", diamond_subdag, "h")) assert g.copy() == g
def test_filter_squash_bunny(): r"""Test squash on a complicated "bunny" shaped graph. This has multiple roots as well as multiple parents that themselves have parents. e g / \ / \ f a h remove abc e g / \ -----------> / \ / \ b c f d h \ / d """ d = Node(Frame(name="d")) diamond = Node.from_lists(("a", ("b", d), ("c", d))) new_d = Node(Frame(name="d")) check_filter_squash( GraphFrame.from_lists(("e", "f", diamond), ("g", diamond, "h")), lambda row: row["node"].frame["name"] not in ("a", "b", "c"), Graph.from_lists(("e", new_d, "f"), ("g", new_d, "h")), [3, 1, 1, 3, 1], # e, d, f, g, h ) check_filter_no_squash( GraphFrame.from_lists(("e", "f", diamond), ("g", diamond, "h")), lambda row: row["node"].frame["name"] not in ("a", "b", "c"), 5, # e, d, f, g, h )
def test_filter_squash_bunny_to_goat_with_merge(): r"""Test squash on a "bunny" shaped graph: This one is more complex because there are more transitive edges to maintain between the roots (e, g) and b and c. e g / \ / \ f a h remove ac e g / \ ----------> / \ / \ b c f b h \ / b """ b = Node(Frame(name="b")) diamond = Node.from_lists(("a", ("b", b), ("c", b))) new_b = Node(Frame(name="b")) check_filter_squash( GraphFrame.from_lists(("e", "f", diamond), ("g", diamond, "h")), lambda row: row["node"].frame["name"] not in ("a", "c"), Graph.from_lists(("e", new_b, "f"), ("g", new_b, "h")), [4, 2, 1, 4, 1], # e, b, f, g, h ) check_filter_no_squash( GraphFrame.from_lists(("e", "f", diamond), ("g", diamond, "h")), lambda row: row["node"].frame["name"] not in ("a", "c"), 5, # e, b, f, g, h )
def test_traverse_paths(): d = Node(Frame(name="d")) diamond_subdag = Node.from_lists(("a", ("b", d), ("c", d))) g = Graph.from_lists(("e", "f", diamond_subdag), ("g", diamond_subdag, "h")) assert list( g.traverse(attrs="name")) == ["e", "a", "b", "d", "c", "f", "g", "h"]
def test_union_dag(): # make graphs g1, g2, and g3, where you know g3 is the union of g1 and g2 c = Node.from_lists(("c", "d")) g1 = Graph.from_lists(("a", ("b", c), ("e", c, "f"))) d = Node(Frame(name="d")) g2 = Graph.from_lists(("a", ("b", ("c", d)), ("e", d, "f"))) d2 = Node(Frame(name="d")) c2 = Node.from_lists(("c", d2)) g3 = Graph.from_lists(("a", ("b", c2), ("e", c2, d2, "f"))) assert g1 != g2 g4 = g1.union(g2) assert g4 == g3
def test_from_lists(): """Ensure we can traverse roots in correct order without repeating a shared subdag. """ d = Node(Frame(name="d")) diamond_subdag = Node.from_lists(("a", ("b", d), ("c", d))) g = Graph.from_lists(("e", "f", diamond_subdag), ("g", diamond_subdag, "h")) assert list(g.traverse(attrs="name")) == ["e", "a", "b", "d", "c", "f", "g", "h"]
def test_traverse_post(): node = Node.from_lists(["a", ["b", "d", "e"], ["c", "f", "g"]]) assert list(node.traverse(order="post", attrs="name")) == [ "d", "e", "b", "f", "g", "c", "a", ]
def test_dag_is_not_tree(): g = Graph.from_lists(("b", "c"), ("d", "e")) assert not g.is_tree() d = Node(Frame(name="d")) diamond_subdag = Node.from_lists(("a", ("b", d), ("c", d))) g = Graph([diamond_subdag]) assert not g.is_tree() g = Graph.from_lists(("e", "f", diamond_subdag), ("g", diamond_subdag, "h")) assert not g.is_tree()
def test_path(): d = Node(Frame(name="d")) node = Node.from_lists(["a", ["b", d]]) assert d.path() == (Frame(name="a"), Frame(name="b"), Frame(name="d")) assert d.parents[0].path() == (Frame(name="a"), Frame(name="b")) assert node.path() == (Frame(name="a"), ) assert d.path(attrs="name") == ("a", "b", "d") assert d.parents[0].path(attrs="name") == ("a", "b") assert node.path(attrs="name") == ("a", )
def test_path(): d = Node(Frame(name="d", type="function")) node = Node.from_lists(["a", ["b", d]]) assert d.path() == ( Node(Frame(name="a")), Node(Frame(name="b")), Node(Frame(name="d", type="function")), ) assert d.parents[0].path() == (Node(Frame(name="a")), Node(Frame(name="b"))) assert node.path() == (Node(Frame(name="a")), )
def test_from_lists_value_error(): with pytest.raises(ValueError): Node.from_lists(object())
def test_traverse_dag(): d = Node(Frame(name="d")) node = Node.from_lists(["a", ["b", d], ["c", d]]) assert list(node.traverse(attrs="name")) == ["a", "b", "d", "c"]