def clustering(G, nodes=None, weight=None): # FIXME(weibin): clustering now only correct in directed graph. # FIXME: nodes and weight not support. pg = G.project_to_simple() ctx = graphscope.clustering(pg) return ctx.to_dataframe({"node": "v.id", "result": "r"})
def test_run_app_on_directed_graph( p2p_project_directed_graph, sssp_result, pagerank_result, hits_result, bfs_result, clustering_result, dc_result, ev_result, katz_result, ): # sssp ctx1 = sssp(p2p_project_directed_graph, src=6) r1 = (ctx1.to_dataframe({ "node": "v.id", "r": "r" }).sort_values(by=["node"]).to_numpy(dtype=float)) r1[r1 == 1.7976931348623157e308] = float( "inf") # replace limit::max with inf assert np.allclose(r1, sssp_result["directed"]) ctx2 = sssp(p2p_project_directed_graph, 6) r2 = (ctx2.to_dataframe({ "node": "v.id", "r": "r" }).sort_values(by=["node"]).to_numpy(dtype=float)) r2[r2 == 1.7976931348623157e308] = float( "inf") # replace limit::max with inf assert np.allclose(r2, sssp_result["directed"]) assert np.allclose( ctx2.to_dataframe({ "node": "v.id", "r": "r" }, vertex_range={ "begin": 1, "end": 4 }).sort_values(by=["node"]).to_numpy(), [[1.0, 260.0], [2.0, 229.0], [3.0, 310.0]], ) assert np.allclose( sorted(ctx1.to_numpy("r", vertex_range={ "begin": 1, "end": 4 })), sorted([260.0, 229.0, 310.0]), ) r3 = sssp(p2p_project_directed_graph, 100000000) assert r3 is not None # pagerank ctx_pr = pagerank(p2p_project_directed_graph, delta=0.85, max_round=10) ret_pr = (ctx_pr.to_dataframe({ "node": "v.id", "r": "r" }).sort_values(by=["node"]).to_numpy(dtype=float)) assert np.allclose(ret_pr, pagerank_result["directed"]) # hits ctx_hits = hits(p2p_project_directed_graph, tolerance=0.001) ret_hub = (ctx_hits.to_dataframe({ "node": "v.id", "hub": "r.hub" }).sort_values(by=["node"]).to_numpy(dtype=float)) ret_auth = (ctx_hits.to_dataframe({ "node": "v.id", "auth": "r.auth" }).sort_values(by=["node"]).to_numpy(dtype=float)) assert np.allclose(ret_hub, hits_result["hub"]) assert np.allclose(ret_auth, hits_result["auth"]) # bfs ctx4 = bfs(p2p_project_directed_graph, src=6) r4 = (ctx4.to_dataframe({ "node": "v.id", "r": "r" }).sort_values(by=["node"]).to_numpy(dtype=int)) assert np.all(r4 == bfs_result["directed"]) ctx5 = bfs(p2p_project_directed_graph, 6) r5 = (ctx5.to_dataframe({ "node": "v.id", "r": "r" }).sort_values(by=["node"]).to_numpy(dtype=int)) assert np.all(r5 == bfs_result["directed"]) assert np.all( ctx5.to_dataframe( { "node": "v.id", "r": "r" }, vertex_range={ "begin": 1, "end": 4 }).sort_values(by=["node"]).to_numpy() == [[1, 5], [2, 5], [3, 6]]) assert np.all( sorted(ctx5.to_numpy("r", vertex_range={ "begin": 1, "end": 4 })) == [5, 5, 6]) # simple_path assert is_simple_path(p2p_project_directed_graph, [1, 10]) with pytest.raises(InvalidArgumentError, match="Louvain not support directed graph."): louvain(p2p_project_directed_graph) # clustering ctx_clustering = clustering(p2p_project_directed_graph) ret_clustering = (ctx_clustering.to_dataframe({ "node": "v.id", "r": "r" }).sort_values(by=["node"]).to_numpy(dtype=float)) assert np.allclose(ret_clustering, clustering_result["directed"]) # degree_centrality ctx_dc = degree_centrality(p2p_project_directed_graph) ret_dc = (ctx_dc.to_dataframe({ "node": "v.id", "r": "r" }).sort_values(by=["node"]).to_numpy(dtype=float)) assert np.allclose(ret_dc, dc_result["directed"]) # eigenvector_centrality ctx_ev = eigenvector_centrality(p2p_project_directed_graph) ret_ev = (ctx_ev.to_dataframe({ "node": "v.id", "r": "r" }).sort_values(by=["node"]).to_numpy(dtype=float)) assert np.allclose(ret_ev, ev_result["directed"]) # katz_centrality ctx_katz = katz_centrality(p2p_project_directed_graph)
def _clustering(G): return graphscope.clustering(G)