Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/graphpro/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ def to_networkx(self) -> nx.Graph:

def to_data(self, node_encoders = [], target: Target = None) -> Data:
""" Return a PyG object from this existing graph"""
directed = torch.tensor([[edge[0],edge[1]] for edge in self.to_networkx().edges], dtype=torch.long)
inversed = torch.tensor([[edge[1],edge[0]] for edge in self.to_networkx().edges], dtype=torch.long)
cco = torch.cat((directed, inversed), 0).t().contiguous()
row, col = np.nonzero(self.adjacency)
values = self.adjacency[row, col]
indices = torch.tensor(np.array([row,col], dtype=int), dtype=torch.long)
values = torch.tensor(values, dtype=torch.float)
cco = torch.sparse_coo_tensor(indices, values, self.adjacency.shape).coalesce()

x = None
y = None
Expand All @@ -82,7 +84,7 @@ def to_data(self, node_encoders = [], target: Target = None) -> Data:
if target:
y = target.encode(self)

return Data(x=x, edge_index=cco, y=y)
return Data(x=x, edge_index=cco.indices(), y=y)

def nodes(self) -> list[int]:
""" Return node list """
Expand Down
4 changes: 2 additions & 2 deletions test/graphpro/graph_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def test_graph_plot():
SIMPLE_G.plot(show=False)

def test_to_data_index():
edge_index = torch.tensor([[0, 0, 1, 0, 1, 1],
[0, 1, 1, 0, 0, 1]], dtype=torch.long)
edge_index = torch.tensor([[0, 0, 1, 1],[0, 1, 0, 1]])
assert(torch.allclose(SIMPLE_G.to_data().edge_index, edge_index))
assert(SIMPLE_G.to_data().is_directed() == False)

def test_to_data_x_transformer():
assert SIMPLE_G_ATTR.to_data(node_encoders=[ResidueType()]).x.size() == (2,22)
Expand Down