Repository: pyg-team/pytorch_geometric

Issue:
to_dgl does not convert graph with isolated nodes or zero edges graph
### 🐛 Describe the bug

```
import torch
from torch_geometric.data import Data
from torch_geometric.utils.convert import to_dgl

# 1nd example zero edge

data = Data()
data['x'] = torch.tensor([[0, 1, 2], [2,3,4]])
data.validate()
to_dgl(data)
# AttributeError: 'GlobalStorage' object has no attribute 'adj_t'

# 2nd example, isolated nodes
data = Data()
data['x'] = torch.tensor([[0, 1, 2], [2,3,4]])
data['edge_index'] = torch.tensor([[0],[0]])
data.validate()
to_dgl(data)`
# DGLError: Expect number of features to match number of nodes (len(u)). Got 2 and 1 instead.
```
The function initialized the dgl graph using data from the edges first. Hence any isolated nodes afterwards will cause node number mismatch.



### Versions

Collecting environment information...
PyTorch version: 2.2.2+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Microsoft Windows 10 Enterprise
GCC version: Could not collect
Clang version: Could not collect
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.19 | packaged by conda-forge | (main, Mar 20 2024, 12:38:46) [MSC v.1929 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.19045-SP0
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:










Revision=

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] numpydoc==1.7.0
[pip3] torch==2.2.2
[pip3] torch_geometric==2.5.2
[pip3] torchdata==0.7.1
[conda] libblas                   3.9.0              22_win64_mkl    conda-forge
[conda] libcblas                  3.9.0              22_win64_mkl    conda-forge
[conda] liblapack                 3.9.0              22_win64_mkl    conda-forge
[conda] mkl                       2024.1.0           h66d3029_692    conda-forge
[conda] numpy                     1.26.4           py39hddb5d58_0    conda-forge
[conda] numpydoc                  1.7.0                    pypi_0    pypi
[conda] torch                     2.2.2                    pypi_0    pypi
[conda] torch-geometric           2.5.2                    pypi_0    pypi
[conda] torchdata                 0.7.1                    pypi_0    pypi



Discussion:

Resolving patch:
diff --git a/torch_geometric/utils/convert.py b/torch_geometric/utils/convert.py
--- a/torch_geometric/utils/convert.py
+++ b/torch_geometric/utils/convert.py
@@ -527,10 +527,14 @@ def to_dgl(
     if isinstance(data, Data):
         if data.edge_index is not None:
             row, col = data.edge_index
-        else:
+        elif 'adj' in data:
+            row, col, _ = data.adj.coo()
+        elif 'adj_t' in data:
             row, col, _ = data.adj_t.t().coo()
+        else:
+            row, col = [], []
 
-        g = dgl.graph((row, col))
+        g = dgl.graph((row, col), num_nodes=data.num_nodes)
 
         for attr in data.node_attrs():
             g.ndata[attr] = data[attr]


Test patch: