Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to_dense_adj with empty edge_index #5476

Merged
merged 2 commits into from
Sep 20, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added `BaseStorage.get()` functionality ([#5240](https://github.com/pyg-team/pytorch_geometric/pull/5240))
- Added a test to confirm that `to_hetero` works with `SparseTensor` ([#5222](https://github.com/pyg-team/pytorch_geometric/pull/5222))
### Changed
- Fix `to_dense_adj` with empty `edge_index` ([#5476](https://github.com/pyg-team/pytorch_geometric/pull/5476))
- The `AttentionalAggregation` module can now be applied to compute attentin on a per-feature level ([#5449](https://github.com/pyg-team/pytorch_geometric/pull/5449))
- Ensure equal lenghts of `num_neighbors` across edge types in `NeighborLoader` ([#5444](https://github.com/pyg-team/pytorch_geometric/pull/5444))
- Fixed a bug in `TUDataset` in which node features were wrongly constructed whenever `node_attributes` only hold a single feature (*e.g.*, in `PROTEINS`) ([#5441](https://github.com/pyg-team/pytorch_geometric/pull/5441))
Expand Down
17 changes: 17 additions & 0 deletions test/utils/test_to_dense_adj.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ def test_to_dense_adj():
assert adj[0].nonzero(as_tuple=False).t().tolist() == edge_index.tolist()


def test_to_dense_adj_with_empty_edge_index():
edge_index = torch.tensor([[], []], dtype=torch.long)
batch = torch.tensor([0, 0, 1, 1, 1])

adj = to_dense_adj(edge_index)
assert adj.size() == (1, 0, 0)

adj = to_dense_adj(edge_index, max_num_nodes=10)
assert adj.size() == (1, 10, 10) and adj.sum() == 0

adj = to_dense_adj(edge_index, batch)
assert adj.size() == (2, 3, 3) and adj.sum() == 0

adj = to_dense_adj(edge_index, batch, max_num_nodes=10)
assert adj.size() == (2, 10, 10) and adj.sum() == 0


def test_to_dense_adj_with_duplicate_entries():
edge_index = torch.tensor([
[0, 0, 0, 1, 2, 3, 3, 4],
Expand Down
8 changes: 5 additions & 3 deletions torch_geometric/utils/to_dense_adj.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ def to_dense_adj(edge_index, batch=None, edge_attr=None, max_num_nodes=None):
[5., 0.]]])
"""
if batch is None:
batch = edge_index.new_zeros(edge_index.max().item() + 1)
num_nodes = int(edge_index.max()) + 1 if edge_index.numel() > 0 else 0
batch = edge_index.new_zeros(num_nodes)

batch_size = batch.max().item() + 1
batch_size = int(batch.max()) + 1 if batch.numel() > 0 else 1
one = batch.new_ones(batch.size(0))
num_nodes = scatter(one, batch, dim=0, dim_size=batch_size, reduce='add')
cum_nodes = torch.cat([batch.new_zeros(1), num_nodes.cumsum(dim=0)])
Expand All @@ -61,7 +62,8 @@ def to_dense_adj(edge_index, batch=None, edge_attr=None, max_num_nodes=None):
if max_num_nodes is None:
max_num_nodes = num_nodes.max().item()

elif idx1.max() >= max_num_nodes or idx2.max() >= max_num_nodes:
elif ((idx1.numel() > 0 and idx1.max() >= max_num_nodes)
or (idx2.numel() > 0 and idx2.max() >= max_num_nodes)):
mask = (idx1 < max_num_nodes) & (idx2 < max_num_nodes)
idx0 = idx0[mask]
idx1 = idx1[mask]
Expand Down