def test_multigraph(graph_file): # FIXME: Migrate to new test fixtures for Graph setup once available cuM = utils.read_csv_file(graph_file) G = cugraph.MultiDiGraph() G.from_cudf_edgelist(cuM, source="0", destination="1", edge_attr="2") nxM = utils.read_csv_for_nx(graph_file, read_weights_in_sp=True) Gnx = nx.from_pandas_edgelist( nxM, source="0", target="1", edge_attr="weight", create_using=nx.MultiDiGraph(), ) assert G.number_of_edges() == Gnx.number_of_edges() assert G.number_of_nodes() == Gnx.number_of_nodes() cuedges = cugraph.to_pandas_edgelist(G) cuedges.rename(columns={"src": "source", "dst": "target", "weights": "weight"}, inplace=True) cuedges["weight"] = cuedges["weight"].round(decimals=3) nxedges = nx.to_pandas_edgelist(Gnx).astype(dtype={"source": "int32", "target": "int32", "weight": "float32"}) cuedges = cuedges.sort_values(by=["source", "target"]).\ reset_index(drop=True) nxedges = nxedges.sort_values(by=["source", "target"]).\ reset_index(drop=True) nxedges["weight"] = nxedges["weight"].round(decimals=3) assert nxedges.equals(cuedges[["source", "target", "weight"]])
def test_to_from_pandas(graph_file): # Read in the graph M = utils.read_csv_for_nx(graph_file, read_weights_in_sp=True) # create a NetworkX DiGraph and convert to pandas adjacency nxG = nx.from_pandas_edgelist(M, source="0", target="1", edge_attr="weight", create_using=nx.DiGraph) nx_pdf = nx.to_pandas_adjacency(nxG) nx_pdf = nx_pdf[sorted(nx_pdf.columns)] nx_pdf.sort_index(inplace=True) # create a cugraph DiGraph and convert to pandas adjacency cuG = cugraph.from_pandas_edgelist(M, source="0", destination="1", edge_attr="weight", create_using=cugraph.DiGraph) cu_pdf = cugraph.to_pandas_adjacency(cuG) cu_pdf = cu_pdf[sorted(cu_pdf.columns)] cu_pdf.sort_index(inplace=True) # Compare pandas adjacency list assert nx_pdf.equals(cu_pdf) # Convert pandas adjacency list to graph new_nxG = nx.from_pandas_adjacency(nx_pdf, create_using=nx.DiGraph) new_cuG = cugraph.from_pandas_adjacency(cu_pdf, create_using=cugraph.DiGraph) # Compare pandas edgelist exp_pdf = nx.to_pandas_edgelist(new_nxG) res_pdf = cugraph.to_pandas_edgelist(new_cuG) exp_pdf = exp_pdf.rename(columns={ "source": "src", "target": "dst", "weight": "weights" }) exp_pdf = exp_pdf.sort_values(by=["src", "dst"]).reset_index(drop=True) res_pdf = res_pdf.sort_values(by=["src", "dst"]).reset_index(drop=True) res_pdf = res_pdf[['src', 'dst', 'weights']] assert exp_pdf.equals(res_pdf)
def test_from_to_numpy(graph_file): # Read in the graph M = utils.read_csv_for_nx(graph_file, read_weights_in_sp=True) # create NetworkX and cugraph DiGraph nxG = nx.from_pandas_edgelist(M, source="0", target="1", edge_attr="weight", create_using=nx.DiGraph) cuG = cugraph.from_pandas_edgelist(M, source="0", destination="1", edge_attr="weight", create_using=cugraph.DiGraph) # convert graphs to numpy array nparray_nx = nx.to_numpy_array(nxG, nodelist=cuG.nodes().values_host) nparray_cu = cugraph.to_numpy_array(cuG) npmatrix_nx = nx.to_numpy_matrix(nxG, nodelist=cuG.nodes().values_host) npmatrix_cu = cugraph.to_numpy_matrix(cuG) # Compare arrays and matrices assert np.array_equal(nparray_nx, nparray_cu) assert np.array_equal(np.asarray(npmatrix_nx), np.asarray(npmatrix_cu)) # Create graphs from numpy array new_nxG = nx.from_numpy_array(nparray_nx, create_using=nx.DiGraph) new_cuG = cugraph.from_numpy_array(nparray_cu, create_using=cugraph.DiGraph) # Assert graphs are same exp_pdf = nx.to_pandas_edgelist(new_nxG) res_pdf = cugraph.to_pandas_edgelist(new_cuG) exp_pdf = exp_pdf.rename(columns={ "source": "src", "target": "dst", "weight": "weights" }) exp_pdf = exp_pdf.sort_values(by=["src", "dst"]).reset_index(drop=True) res_pdf = res_pdf.sort_values(by=["src", "dst"]).reset_index(drop=True) res_pdf = res_pdf[['src', 'dst', 'weights']] assert exp_pdf.equals(res_pdf) # Create graphs from numpy matrix new_nxG = nx.from_numpy_matrix(npmatrix_nx, create_using=nx.DiGraph) new_cuG = cugraph.from_numpy_matrix(npmatrix_cu, create_using=cugraph.DiGraph) # Assert graphs are same exp_pdf = nx.to_pandas_edgelist(new_nxG) res_pdf = cugraph.to_pandas_edgelist(new_cuG) exp_pdf = exp_pdf.rename(columns={ "source": "src", "target": "dst", "weight": "weights" }) exp_pdf = exp_pdf.sort_values(by=["src", "dst"]).reset_index(drop=True) res_pdf = res_pdf.sort_values(by=["src", "dst"]).reset_index(drop=True) res_pdf = res_pdf[['src', 'dst', 'weights']] assert exp_pdf.equals(res_pdf)