def test_apply_indices(calc_pi_hpct_db): gf = GraphFrame.from_hpctoolkit(str(calc_pi_hpct_db)) main = gf.graph.roots[0].children[0] path = [ { "name": "[0-9]*:?MPI_.*" }, ("*", { "name": "^((?!MPID).)*" }), { "name": "[0-9]*:?MPID.*" }, ] matches = [ [ main.children[0], main.children[0].children[0], main.children[0].children[0].children[0], main.children[0].children[0].children[0].children[0], ], [ main.children[1], main.children[1].children[0], main.children[1].children[0].children[0], ], ] query = QueryMatcher(path) assert query.apply(gf) == matches gf.drop_index_levels() assert query.apply(gf) == matches
def test_high_level_multi_condition_one_attribute(mock_graph_literal): gf = GraphFrame.from_literal(mock_graph_literal) query = QueryMatcher([("*", {"time (inc)": [">= 20", "<= 60"]})]) roots = gf.graph.roots matches = [ [roots[0].children[0]], [ roots[0].children[1], roots[0].children[1].children[0], roots[0].children[1].children[0].children[0], roots[0].children[1].children[0].children[0].children[0], ], [ roots[0].children[1], roots[0].children[1].children[0], roots[0].children[1].children[0].children[0], ], [ roots[0].children[1].children[0], roots[0].children[1].children[0].children[0], roots[0].children[1].children[0].children[0].children[0], ], [ roots[0].children[1].children[0], roots[0].children[1].children[0].children[0], ], [ roots[0].children[1].children[0].children[0], roots[0].children[1].children[0].children[0].children[0], ], [roots[0].children[1].children[0].children[0]], [roots[0].children[1].children[0].children[0].children[0]], [ roots[0].children[2], roots[0].children[2].children[0], roots[0].children[2].children[0].children[1], roots[0].children[2].children[0].children[1].children[0], ], [roots[0].children[2], roots[0].children[2].children[0]], [roots[0].children[2]], [ roots[0].children[2].children[0], roots[0].children[2].children[0].children[1], roots[0].children[2].children[0].children[1].children[0], ], [roots[0].children[2].children[0]], [ roots[0].children[2].children[0].children[1], roots[0].children[2].children[0].children[1].children[0], ], [roots[0].children[2].children[0].children[1].children[0]], [roots[1], roots[1].children[0]], [roots[1].children[0]], ] assert sorted(query.apply(gf)) == sorted(matches)
def test_high_level_hatchet_nid(mock_graph_literal): gf = GraphFrame.from_literal(mock_graph_literal) query = QueryMatcher([("*", {"node_id": ">= 20"})]) root = gf.graph.roots[1] matches = [ [root, root.children[0], root.children[0].children[0]], [root.children[0], root.children[0].children[0]], [root.children[0].children[0]], [root, root.children[0], root.children[0].children[1]], [root.children[0], root.children[0].children[1]], [root.children[0].children[1]], ] assert sorted(query.apply(gf)) == sorted(matches) query = QueryMatcher([{"node_id": 0}]) assert query.apply(gf) == [[gf.graph.roots[0]]] with pytest.raises(InvalidQueryFilter): query = QueryMatcher([{"node_id": "hello"}]) query.apply(gf)
def test_high_level_node_id_index_levels(calc_pi_hpct_db): gf = GraphFrame.from_hpctoolkit(str(calc_pi_hpct_db)) root = gf.graph.roots[0] query = QueryMatcher([("*", {"node_id": "<= 2"})]) matches = [ [root, root.children[0]], [root.children[0]], [root, root.children[0], root.children[0].children[0]], [root.children[0], root.children[0].children[0]], [root.children[0].children[0]], ] assert sorted(query.apply(gf)) == sorted(matches) query = QueryMatcher([("*", {"node_id": 0})]) matches = [[root]] assert query.apply(gf) == matches with pytest.raises(InvalidQueryFilter): query = QueryMatcher([{"node_id": "hello"}]) query.apply(gf)
def test_high_level_depth(mock_graph_literal): gf = GraphFrame.from_literal(mock_graph_literal) query = QueryMatcher([("*", {"depth": 1})]) roots = gf.graph.roots matches = [[c] for r in roots for c in r.children] assert query.apply(gf) == matches query = QueryMatcher([("*", {"depth": "<= 2"})]) matches = [ [roots[0], roots[0].children[0], roots[0].children[0].children[0]], [roots[0].children[0], roots[0].children[0].children[0]], [roots[0].children[0].children[0]], [roots[0], roots[0].children[0], roots[0].children[0].children[1]], [roots[0].children[0], roots[0].children[0].children[1]], [roots[0].children[0].children[1]], [roots[0], roots[0].children[1], roots[0].children[1].children[0]], [roots[0].children[1], roots[0].children[1].children[0]], [roots[0].children[1].children[0]], [roots[0], roots[0].children[2], roots[0].children[2].children[0]], [roots[0].children[2], roots[0].children[2].children[0]], [roots[0].children[2].children[0]], [roots[0], roots[0].children[2], roots[0].children[2].children[1]], [roots[0].children[2], roots[0].children[2].children[1]], [roots[0].children[2].children[1]], [roots[1], roots[1].children[0], roots[1].children[0].children[0]], [roots[1].children[0], roots[1].children[0].children[0]], [roots[1].children[0].children[0]], [roots[1], roots[1].children[0], roots[1].children[0].children[1]], [roots[1].children[0], roots[1].children[0].children[1]], [roots[1].children[0].children[1]], ] assert sorted(query.apply(gf)) == sorted(matches) with pytest.raises(InvalidQueryFilter): query = QueryMatcher([{"depth": "hello"}]) query.apply(gf)
def test_apply(mock_graph_literal): gf = GraphFrame.from_literal(mock_graph_literal) path = [ { "time (inc)": ">= 30.0" }, (2, { "name": "[^b][a-z]+" }), ("*", { "name": "[^b][a-z]+" }), { "name": "gr[a-z]+" }, ] root = gf.graph.roots[0] match = [ [ root, root.children[1], root.children[1].children[0], root.children[1].children[0].children[0], root.children[1].children[0].children[0].children[1], ], [ root.children[1], root.children[1].children[0], root.children[1].children[0].children[0], root.children[1].children[0].children[0].children[1], ], ] query = QueryMatcher(path) assert query.apply(gf) == match path = [{"time (inc)": ">= 30.0"}, ".", {"name": "bar"}, "*"] match = [ [ root.children[1].children[0], root.children[1].children[0].children[0], root.children[1].children[0].children[0].children[0], root.children[1].children[0].children[0].children[0].children[0], ], [ root.children[1].children[0], root.children[1].children[0].children[0], root.children[1].children[0].children[0].children[0], root.children[1].children[0].children[0].children[0].children[1], ], ] query = QueryMatcher(path) assert query.apply(gf) == match path = [{"name": "foo"}, {"name": "bar"}, {"time": 5.0}] match = [[root, root.children[0], root.children[0].children[0]]] query = QueryMatcher(path) assert query.apply(gf) == match path = [{"name": "foo"}, {"name": "qux"}, ("+", {"time (inc)": "> 15.0"})] match = [ [ root, root.children[1], root.children[1].children[0], root.children[1].children[0].children[0], root.children[1].children[0].children[0].children[0], ], [ root, root.children[1], root.children[1].children[0], root.children[1].children[0].children[0], ], ] query = QueryMatcher(path) assert query.apply(gf) == match path = [{"name": "this"}, ("*", {"name": "is"}), {"name": "nonsense"}] query = QueryMatcher(path) assert query.apply(gf) == [] path = [{"name": 5}, "*", {"name": "whatever"}] query = QueryMatcher(path) with pytest.raises(InvalidQueryFilter): query.apply(gf) path = [{"time": "badstring"}, "*", {"name": "whatever"}] query = QueryMatcher(path) with pytest.raises(InvalidQueryFilter): query.apply(gf) class DummyType: def __init__(self): self.x = 5.0 self.y = -1 self.z = "hello" bad_field_test_dict = list(mock_graph_literal) bad_field_test_dict[0]["children"][0]["children"][0]["metrics"][ "list"] = DummyType() gf = GraphFrame.from_literal(bad_field_test_dict) path = [{"name": "foo"}, {"name": "bar"}, {"list": DummyType()}] query = QueryMatcher(path) with pytest.raises(InvalidQueryFilter): query.apply(gf) path = ["*", {"name": "bar"}, {"name": "grault"}, "*"] match = [ [root, root.children[0], root.children[0].children[1]], [root.children[0], root.children[0].children[1]], [ root, root.children[1], root.children[1].children[0], root.children[1].children[0].children[0], root.children[1].children[0].children[0].children[0], root.children[1].children[0].children[0].children[0].children[1], ], [ root.children[1], root.children[1].children[0], root.children[1].children[0].children[0], root.children[1].children[0].children[0].children[0], root.children[1].children[0].children[0].children[0].children[1], ], [ root.children[1].children[0], root.children[1].children[0].children[0], root.children[1].children[0].children[0].children[0], root.children[1].children[0].children[0].children[0].children[1], ], [ root.children[1].children[0].children[0], root.children[1].children[0].children[0].children[0], root.children[1].children[0].children[0].children[0].children[1], ], [ root.children[1].children[0].children[0].children[0], root.children[1].children[0].children[0].children[0].children[1], ], [ gf.graph.roots[1], gf.graph.roots[1].children[0], gf.graph.roots[1].children[0].children[1], ], [ gf.graph.roots[1].children[0], gf.graph.roots[1].children[0].children[1] ], ] query = QueryMatcher(path) assert sorted(query.apply(gf)) == sorted(match) path = ["*", {"name": "bar"}, {"name": "grault"}, "+"] query = QueryMatcher(path) assert query.apply(gf) == []