Skip to content

Commit

Permalink
Return Graph in-edges as (source, target)
Browse files Browse the repository at this point in the history
  • Loading branch information
funkey committed Sep 24, 2024
1 parent d7c50fc commit d25696d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
28 changes: 25 additions & 3 deletions spatial_graph/graph/wrapper_template.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,19 @@ cdef class Graph:
def ${prefix}edges(self, node=None, bint data=False):

if node is not None:
yield from self._${prefix}neighbors(<NodeType>node, data)
for neighbor in self._${prefix}neighbors(<NodeType>node, data):
if data:
%if $prefix == "in_"
yield (neighbor[0], node), data
%else
yield (node, neighbor[0]), data
%end if
else:
%if $prefix == "in_"
yield (neighbor, node)
%else
yield (node, neighbor)
%end if
return

cdef NodeIterator node_it = self._graph.begin()
Expand All @@ -314,11 +326,16 @@ cdef class Graph:
inc(it)
continue
%end if
%if $prefix == "in_"
edge = (v, u)
%else
edge = (u, v)
%end if
if data:
edge_data.set_ptr(&deref(it).second.prop())
yield (u, v), edge_data
yield edge, edge_data
else:
yield (u, v)
yield edge
inc(it)
inc(node_it)

Expand All @@ -340,8 +357,13 @@ cdef class Graph:
end = view.second
while it != end:
v = deref(it).first
%if $prefix == "in_"
edges[i, 0] = v
edges[i, 1] = u
%else
edges[i, 0] = u
edges[i, 1] = v
%end if
i += 1
inc(it)

Expand Down
32 changes: 31 additions & 1 deletion tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_operations(directed):
assert attrs.score == edge[0] * 100 + edge[1]

for edge, attrs in graph.in_edges(data=True):
assert attrs.score == edge[1] * 100 + edge[0]
assert attrs.score == edge[0] * 100 + edge[1]

else:
assert graph.num_edges() == (len(nodes) ** 2 - len(nodes)) / 2
Expand All @@ -75,6 +75,36 @@ def test_operations(directed):
assert attrs.score == edge[0] * 100 + edge[1]


def test_directed_edges():

graph = sg.Graph("uint64", directed=True)
graph.add_nodes(np.array([0, 1, 2], dtype="uint64"))
graph.add_edges(np.array([[0, 1], [1, 2], [2, 0]], dtype="uint64"))

# all in edges
in_edges = sorted(list(graph.in_edges()))
assert len(in_edges) == 3
np.testing.assert_array_equal(in_edges, [[0, 1], [1, 2], [2, 0]])

# all out edges
out_edges = sorted(list(graph.out_edges()))
assert len(out_edges) == 3
np.testing.assert_array_equal(out_edges, [[0, 1], [1, 2], [2, 0]])

# in/out edges per node
in_edges_0 = list(graph.in_edges(0))
assert len(in_edges_0) == 1
np.testing.assert_array_equal(in_edges_0, [[2, 0]])
out_edges_0 = list(graph.out_edges(0))
assert len(out_edges_0) == 1
np.testing.assert_array_equal(out_edges_0, [[0, 1]])

# in/out edges for list of nodes
in_edges_01 = graph.in_edges_by_nodes(np.array([0, 1], dtype="uint64"))
np.testing.assert_array_equal(in_edges_01, [[2, 0], [0, 1]])
out_edges_01 = graph.out_edges_by_nodes(np.array([0, 1], dtype="uint64"))
np.testing.assert_array_equal(out_edges_01, [[0, 1], [1, 2]])

def test_attribute_modification():
graph = sg.Graph(
"uint64",
Expand Down

0 comments on commit d25696d

Please sign in to comment.