def create_simple_graph(): """ Creates a simple directed graph for testing. The node ids are integers. Returns: A small, directed graph with 3 nodes and 2 edges in StellarDiGraph format. """ nodes = pd.DataFrame([-1, -2, -3], index=[1, 2, 3]) edges = pd.DataFrame([(1, 2), (2, 3)], columns=["source", "target"]) return StellarDiGraph(nodes, edges)
def create_simple_graph(): """ Creates a simple directed graph for testing. The node ids are integers. Returns: A small, directed graph with 3 nodes and 2 edges in StellarDiGraph format. """ g = nx.DiGraph() edges = [(1, 2), (2, 3)] g.add_edges_from(edges) nodes = list(g.nodes()) features = [(node, -1.0 * node) for node in nodes] df = pd.DataFrame(features, columns=["id", "f0"]).set_index("id") return StellarDiGraph(g, node_features=df)
def create_simple_graph(): """ Creates a simple directed graph for testing. The node ids are string or integers. :return: A small, directed graph with 4 nodes and 6 edges in StellarDiGraph format. """ g = nx.DiGraph() edges = [ ("root", 2), ("root", 1), ("root", "0"), (2, "c2.1"), (2, "c2.2"), (1, "c1.1"), ] g.add_edges_from(edges) return StellarDiGraph(g)
def create_test_graph(): """ Creates a simple graph for testing. The node ids are string or integers. :return: A simple graph with 13 nodes and 24 edges (including self loops for all but two of the nodes) in StellarDiGraph format. """ g = nx.DiGraph() edges = [ ("0", 1), ("0", 2), (1, 3), (1, 4), (3, 6), (4, 7), (4, 8), (2, 5), (5, 9), (5, 10), ("0", "0"), (1, 1), (3, 3), (6, 6), (4, 4), (7, 7), (8, 8), (2, 2), (5, 5), (9, 9), ( "self loner", "self loner", ), # node that is not connected with any other nodes but has self loop ] g.add_edges_from(edges) g.add_node( "loner" ) # node that is not connected to any other nodes and not having a self loop return StellarDiGraph(g)
def test_weighted_all_zero(self): edges = pd.DataFrame({ "source": [0, 0], "target": [1, 2], "weight": [0.0, 0] }) g = StellarDiGraph(edges=edges) bfw = DirectedBreadthFirstNeighbours(g) walks = bfw.run(nodes=[0], n=10, in_size=[20, 20], out_size=[20, 20], weighted=True) assert len(walks) == 10 for walk in walks: assert len(walk) == 7 assert walk[0] == [0] for hop in walk: np.testing.assert_array_equal(hop[1:], -1)
def test_directed_walk_generation_single_root_node(self): g = nx.DiGraph() edges = [ ("root", 2), ("root", 1), ("root", "0"), (2, "c2.1"), (2, "c2.2"), (1, "c1.1"), ] g.add_edges_from(edges) g = StellarDiGraph(g) def _check_directed_walk(walk, n_size): if len(n_size) > 1 and n_size[0] > 0 and n_size[1] > 0: for child_pos in range(n_size[0]): child = walk[child_pos + 1] grandchildren_start = 1 + n_size[0] + child_pos * n_size[1] grandchildren_end = grandchildren_start + n_size[1] grandchildren = walk[grandchildren_start:grandchildren_end] if child == "root": # node with three children for grandchild in grandchildren: assert grandchild in [0, 1, 2] elif child == "0": # node without children for grandchild in grandchildren: assert grandchild == "root" elif child == 1: # node with single child for grandchild in grandchildren: assert grandchild in ["c1.1", "root"] elif child == 2: # node with two children for grandchild in grandchildren: assert grandchild in ["c2.1", "c2.2", "root"] else: assert 1 == 0 bfw = SampledBreadthFirstWalk(g) nodes = ["root"] n = 1 n_size = [0] subgraphs = bfw.run(nodes=nodes, n=n, n_size=n_size) assert len(subgraphs) == n assert len(subgraphs[0]) == 1 # all elements should be the same node assert subgraphs[0][0] == "root" n_size = [1] subgraphs = bfw.run(nodes=nodes, n=n, n_size=n_size) assert len(subgraphs) == n assert len(subgraphs[0]) == expected_bfw_size( n_size) # "root" plus child assert subgraphs[0][0] == "root" n_size = [2, 2] subgraphs = bfw.run(nodes=nodes, n=n, n_size=n_size) assert len(subgraphs) == n # "root" plus 2 * child + 2 * 2 * grandchild or None assert len(subgraphs[0]) == expected_bfw_size(n_size) assert subgraphs[0][0] == "root" assert subgraphs[0][1] is not None assert subgraphs[0][2] is not None _check_directed_walk(subgraphs[0], n_size) n_size = [3, 2] subgraphs = bfw.run(nodes=nodes, n=n, n_size=n_size) assert len(subgraphs) == n # "root" plus 3 * child + 3 * 2 * grandchild or None assert len(subgraphs[0]) == expected_bfw_size(n_size) assert subgraphs[0][0] == "root" _check_directed_walk(subgraphs[0], n_size) n = 3 n_size = [0] subgraphs = bfw.run(nodes=nodes, n=n, n_size=n_size) assert len(subgraphs) == n for subgraph in subgraphs: assert len(subgraph) == 1 # root node only assert subgraph[0] == "root" n_size = [1] subgraphs = bfw.run(nodes=nodes, n=n, n_size=n_size) assert len(subgraphs) == n for subgraph in subgraphs: # "root" plus child assert len(subgraph) == expected_bfw_size(n_size) assert subgraph[0] == "root" n = 99 n_size = [2, 2] subgraphs = bfw.run(nodes=nodes, n=n, n_size=n_size) assert len(subgraphs) == n for subgraph in subgraphs: # "root" plus 2 * child + 2 * 2 * grandchild or None assert len(subgraph) == expected_bfw_size(n_size) assert subgraph[0] == "root" _check_directed_walk(subgraph, n_size) n = 17 n_size = [3, 2] subgraphs = bfw.run(nodes=nodes, n=n, n_size=n_size) assert len(subgraphs) == n for subgraph in subgraphs: # "root" plus 3 * child + 3 * 2 * grandchild or None assert len(subgraph) == expected_bfw_size(n_size) assert subgraph[0] == "root"