Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_create_from_elist():
elist = [(2, 1), (1, 0), (2, 0), (3, 0), (0, 2)]
g = create_graph_index(elist)
for i, (u, v) in enumerate(elist):
assert g.edge_id(u, v)[0] == i
# immutable graph
def generate_from_edgelist():
edges = [[2, 3], [2, 5], [3, 0], [6, 10], [10, 3], [10, 15]]
g = create_graph_index(edges, multigraph=False, readonly=False)
ig = create_graph_index(edges, multigraph=False, readonly=True)
return g, ig
def generate_from_networkx():
edges = [[2, 3], [2, 5], [3, 0], [1, 0], [4, 3], [4, 5]]
nx_graph = nx.DiGraph()
nx_graph.add_edges_from(edges)
g = create_graph_index(nx_graph, multigraph=False, readonly=False)
ig = create_graph_index(nx_graph, multigraph=False, readonly=True)
return g, ig
def test_nx():
gi = create_graph_index(multigraph=True)
gi.add_nodes(2)
gi.add_edge(0, 1)
nxg = gi.to_networkx()
assert len(nxg.nodes) == 2
assert len(nxg.edges(0, 1)) == 1
gi.add_edge(0, 1)
nxg = gi.to_networkx()
assert len(nxg.edges(0, 1)) == 2
nxg = nx.DiGraph()
nxg.add_edge(0, 1)
gi = create_graph_index(nxg)
assert not gi.is_multigraph()
assert gi.number_of_nodes() == 2
assert gi.number_of_edges() == 1
def test_pickling_graph_index():
gi = create_graph_index(None, False, False)
gi.add_nodes(3)
src_idx = toindex([0, 0])
dst_idx = toindex([1, 2])
gi.add_edges(src_idx, dst_idx)
gi2 = _reconstruct_pickle(gi)
assert gi2.number_of_nodes() == gi.number_of_nodes()
src_idx2, dst_idx2, _ = gi2.edges()
assert F.array_equal(src_idx.tousertensor(), src_idx2.tousertensor())
assert F.array_equal(dst_idx.tousertensor(), dst_idx2.tousertensor())
def generate_from_networkx():
edges = [[2, 3], [2, 5], [3, 0], [1, 0], [4, 3], [4, 5]]
nx_graph = nx.DiGraph()
nx_graph.add_edges_from(edges)
g = create_graph_index(nx_graph, multigraph=False, readonly=False)
ig = create_graph_index(nx_graph, multigraph=False, readonly=True)
return g, ig
def generate_rand_graph(n):
arr = (sp.sparse.random(n, n, density=0.1, format='coo') != 0).astype(np.int64)
g = create_graph_index(arr)
ig = create_graph_index(arr, readonly=True)
return g, ig
def test_predsucc():
gi = create_graph_index(multigraph=True)
gi.add_nodes(4)
gi.add_edge(0, 1)
gi.add_edge(0, 1)
gi.add_edge(0, 2)
gi.add_edge(2, 0)
gi.add_edge(3, 0)
gi.add_edge(0, 0)
gi.add_edge(0, 0)
pred = gi.predecessors(0)
assert len(pred) == 3
assert 2 in pred
assert 3 in pred
assert 0 in pred
assert gi.number_of_edges() == 1
assert gi.edge_id(0, 1)[0] == 0
nxg = nx.MultiDiGraph()
nxg.add_edge(0, 1)
nxg.add_edge(0, 1)
gi = create_graph_index(nxg, True)
assert gi.is_multigraph()
assert gi.number_of_nodes() == 2
assert gi.number_of_edges() == 2
assert 0 in gi.edge_id(0, 1)
assert 1 in gi.edge_id(0, 1)
nxg = nx.DiGraph()
nxg.add_nodes_from(range(3))
gi = create_graph_index(nxg)
assert gi.number_of_nodes() == 3
assert gi.number_of_edges() == 0
gi = create_graph_index()
gi.add_nodes(3)
nxg = gi.to_networkx()
assert len(nxg.nodes) == 3
assert len(nxg.edges) == 0
nxg = nx.DiGraph()
nxg.add_edge(0, 1, id=0)
nxg.add_edge(1, 2, id=1)
gi = create_graph_index(nxg)
assert 0 in gi.edge_id(0, 1)
assert 1 in gi.edge_id(1, 2)
assert gi.number_of_edges() == 2
def test_edges():
gi = create_graph_index()
gi.add_nodes(10)
gi.add_edges(toindex([5,5,5,5]), toindex([6,7,8,9]))
gi.add_edges(toindex([0,0,0,0]), toindex([1,2,3,4]))
gi.add_edges(toindex([1,1,1,1]), toindex([2,3,4,5]))
src, dst, eid = gi.edges()
src0, dst0, eid0 = src.tonumpy(), dst.tonumpy(), eid.tonumpy()
gi.readonly()
src, dst, eid = gi.edges()
src, dst, eid = src.tonumpy(), dst.tonumpy(), eid.tonumpy()
import numpy as np
assert np.array_equal(src, src0)
assert np.array_equal(dst, dst0)
assert np.array_equal(eid, eid0)