OnDiskDataset for Homogeneous Graphο
This tutorial shows how to create OnDiskDataset
for homogeneous graph that could be used in GraphBolt framework.
By the end of this tutorial, you will be able to
organize graph structure data.
organize feature data.
organize training/validation/test set for specific tasks.
To create an OnDiskDataset
object, you need to organize all the data including graph structure, feature data and tasks into a directory. The directory should contain a metadata.yaml
file that describes the metadata of the dataset.
Now letβs generate various data step by step and organize them together to instantiate OnDiskDataset
finally.
Install DGL packageο
[1]:
# Install required packages.
import os
import torch
import numpy as np
os.environ['TORCH'] = torch.__version__
os.environ['DGLBACKEND'] = "pytorch"
# Install the CPU version.
device = torch.device("cpu")
!pip install --pre dgl -f https://data.dgl.ai/wheels-test/repo.html
try:
import dgl
import dgl.graphbolt as gb
installed = True
except ImportError as error:
installed = False
print(error)
print("DGL installed!" if installed else "DGL not found!")
Looking in links: https://data.dgl.ai/wheels-test/repo.html
Requirement already satisfied: dgl in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages/dgl-2.2.1-py3.8-linux-x86_64.egg (2.2.1)
Requirement already satisfied: numpy>=1.14.0 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from dgl) (1.24.4)
Requirement already satisfied: scipy>=1.1.0 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from dgl) (1.10.1)
Requirement already satisfied: networkx>=2.1 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from dgl) (3.1)
Requirement already satisfied: requests>=2.19.0 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from dgl) (2.31.0)
Requirement already satisfied: tqdm in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from dgl) (4.66.4)
Requirement already satisfied: psutil>=5.8.0 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from dgl) (5.9.8)
Requirement already satisfied: torchdata>=0.5.0 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from dgl) (0.7.1)
Requirement already satisfied: pandas in /home/ubuntu/.pyenv/versions/miniconda3-latest/lib/python3.8/site-packages (from dgl) (2.0.3)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from requests>=2.19.0->dgl) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from requests>=2.19.0->dgl) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from requests>=2.19.0->dgl) (2.2.1)
Requirement already satisfied: certifi>=2017.4.17 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from requests>=2.19.0->dgl) (2024.2.2)
Requirement already satisfied: torch>=2 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torchdata>=0.5.0->dgl) (2.0.0)
Requirement already satisfied: python-dateutil>=2.8.2 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from pandas->dgl) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from pandas->dgl) (2024.1)
Requirement already satisfied: tzdata>=2022.1 in /home/ubuntu/.pyenv/versions/miniconda3-latest/lib/python3.8/site-packages (from pandas->dgl) (2024.1)
Requirement already satisfied: six>=1.5 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from python-dateutil>=2.8.2->pandas->dgl) (1.16.0)
Requirement already satisfied: filelock in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (3.14.0)
Requirement already satisfied: typing-extensions in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (4.11.0)
Requirement already satisfied: sympy in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (1.12)
Requirement already satisfied: jinja2 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (3.1.4)
Requirement already satisfied: nvidia-cuda-nvrtc-cu11==11.7.99 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (11.7.99)
Requirement already satisfied: nvidia-cuda-runtime-cu11==11.7.99 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (11.7.99)
Requirement already satisfied: nvidia-cuda-cupti-cu11==11.7.101 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (11.7.101)
Requirement already satisfied: nvidia-cudnn-cu11==8.5.0.96 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (8.5.0.96)
Requirement already satisfied: nvidia-cublas-cu11==11.10.3.66 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (11.10.3.66)
Requirement already satisfied: nvidia-cufft-cu11==10.9.0.58 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (10.9.0.58)
Requirement already satisfied: nvidia-curand-cu11==10.2.10.91 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (10.2.10.91)
Requirement already satisfied: nvidia-cusolver-cu11==11.4.0.1 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (11.4.0.1)
Requirement already satisfied: nvidia-cusparse-cu11==11.7.4.91 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (11.7.4.91)
Requirement already satisfied: nvidia-nccl-cu11==2.14.3 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (2.14.3)
Requirement already satisfied: nvidia-nvtx-cu11==11.7.91 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (11.7.91)
Requirement already satisfied: triton==2.0.0 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from torch>=2->torchdata>=0.5.0->dgl) (2.0.0)
Requirement already satisfied: setuptools in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from nvidia-cublas-cu11==11.10.3.66->torch>=2->torchdata>=0.5.0->dgl) (69.2.0)
Requirement already satisfied: wheel in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from nvidia-cublas-cu11==11.10.3.66->torch>=2->torchdata>=0.5.0->dgl) (0.43.0)
Requirement already satisfied: cmake in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from triton==2.0.0->torch>=2->torchdata>=0.5.0->dgl) (3.29.2)
Requirement already satisfied: lit in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from triton==2.0.0->torch>=2->torchdata>=0.5.0->dgl) (18.1.4)
Requirement already satisfied: MarkupSafe>=2.0 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from jinja2->torch>=2->torchdata>=0.5.0->dgl) (2.1.5)
Requirement already satisfied: mpmath>=0.19 in /home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages (from sympy->torch>=2->torchdata>=0.5.0->dgl) (1.3.0)
DGL installed!
Data preparationο
In order to demonstrate how to organize various data, letβs create a base directory first.
[2]:
base_dir = './ondisk_dataset_homograph'
os.makedirs(base_dir, exist_ok=True)
print(f"Created base directory: {base_dir}")
Created base directory: ./ondisk_dataset_homograph
Generate graph structure dataο
For homogeneous graph, we just need to save edges(namely seeds) into Numpy or CSV file.
Note: - when saving to Numpy, the array requires to be in shape of (2, N)
. This format is recommended as constructing graph from it is much faster than CSV file. - when saving to CSV file, do not save index and header.
[3]:
import numpy as np
import pandas as pd
num_nodes = 1000
num_edges = 10 * num_nodes
edges_path = os.path.join(base_dir, "edges.csv")
edges = np.random.randint(0, num_nodes, size=(num_edges, 2))
print(f"Part of edges: {edges[:5, :]}")
df = pd.DataFrame(edges)
df.to_csv(edges_path, index=False, header=False)
print(f"Edges are saved into {edges_path}")
Part of edges: [[890 944]
[183 149]
[947 686]
[939 285]
[820 251]]
Edges are saved into ./ondisk_dataset_homograph/edges.csv
Generate feature data for graphο
For feature data, numpy arrays and torch tensors are supported for now.
[4]:
# Generate node feature in numpy array.
node_feat_0_path = os.path.join(base_dir, "node-feat-0.npy")
node_feat_0 = np.random.rand(num_nodes, 5)
print(f"Part of node feature [feat_0]: {node_feat_0[:3, :]}")
np.save(node_feat_0_path, node_feat_0)
print(f"Node feature [feat_0] is saved to {node_feat_0_path}\n")
# Generate another node feature in torch tensor
node_feat_1_path = os.path.join(base_dir, "node-feat-1.pt")
node_feat_1 = torch.rand(num_nodes, 5)
print(f"Part of node feature [feat_1]: {node_feat_1[:3, :]}")
torch.save(node_feat_1, node_feat_1_path)
print(f"Node feature [feat_1] is saved to {node_feat_1_path}\n")
# Generate edge feature in numpy array.
edge_feat_0_path = os.path.join(base_dir, "edge-feat-0.npy")
edge_feat_0 = np.random.rand(num_edges, 5)
print(f"Part of edge feature [feat_0]: {edge_feat_0[:3, :]}")
np.save(edge_feat_0_path, edge_feat_0)
print(f"Edge feature [feat_0] is saved to {edge_feat_0_path}\n")
# Generate another edge feature in torch tensor
edge_feat_1_path = os.path.join(base_dir, "edge-feat-1.pt")
edge_feat_1 = torch.rand(num_edges, 5)
print(f"Part of edge feature [feat_1]: {edge_feat_1[:3, :]}")
torch.save(edge_feat_1, edge_feat_1_path)
print(f"Edge feature [feat_1] is saved to {edge_feat_1_path}\n")
Part of node feature [feat_0]: [[0.03505474 0.20627255 0.2343166 0.42653033 0.20813628]
[0.71773796 0.32422585 0.74991281 0.71295535 0.82648485]
[0.2375176 0.61722111 0.66834531 0.67080465 0.96139979]]
Node feature [feat_0] is saved to ./ondisk_dataset_homograph/node-feat-0.npy
Part of node feature [feat_1]: tensor([[0.9856, 0.2285, 0.6046, 0.7058, 0.2040],
[0.6838, 0.3148, 0.0609, 0.2369, 0.2482],
[0.5177, 0.0569, 0.9640, 0.2306, 0.4499]])
Node feature [feat_1] is saved to ./ondisk_dataset_homograph/node-feat-1.pt
Part of edge feature [feat_0]: [[0.20969905 0.08945929 0.08730196 0.20651042 0.1274021 ]
[0.02716949 0.72664455 0.98973366 0.86179266 0.3046178 ]
[0.62186735 0.02552082 0.82493334 0.58541577 0.4256453 ]]
Edge feature [feat_0] is saved to ./ondisk_dataset_homograph/edge-feat-0.npy
Part of edge feature [feat_1]: tensor([[0.5470, 0.1422, 0.8522, 0.4386, 0.3475],
[0.8643, 0.3016, 0.5732, 0.5025, 0.8706],
[0.2292, 0.9007, 0.2978, 0.2307, 0.2727]])
Edge feature [feat_1] is saved to ./ondisk_dataset_homograph/edge-feat-1.pt
Generate tasksο
OnDiskDataset
supports multiple tasks. For each task, we need to prepare training/validation/test sets respectively. Such sets usually vary among different tasks. In this tutorial, letβs create a Node Classification task and Link Prediction task.
Node Classification Taskο
For node classification task, we need node IDs and corresponding labels for each training/validation/test set. Like feature data, numpy arrays and torch tensors are supported for these sets.
[5]:
num_trains = int(num_nodes * 0.6)
num_vals = int(num_nodes * 0.2)
num_tests = num_nodes - num_trains - num_vals
ids = np.arange(num_nodes)
np.random.shuffle(ids)
nc_train_ids_path = os.path.join(base_dir, "nc-train-ids.npy")
nc_train_ids = ids[:num_trains]
print(f"Part of train ids for node classification: {nc_train_ids[:3]}")
np.save(nc_train_ids_path, nc_train_ids)
print(f"NC train ids are saved to {nc_train_ids_path}\n")
nc_train_labels_path = os.path.join(base_dir, "nc-train-labels.pt")
nc_train_labels = torch.randint(0, 10, (num_trains,))
print(f"Part of train labels for node classification: {nc_train_labels[:3]}")
torch.save(nc_train_labels, nc_train_labels_path)
print(f"NC train labels are saved to {nc_train_labels_path}\n")
nc_val_ids_path = os.path.join(base_dir, "nc-val-ids.npy")
nc_val_ids = ids[num_trains:num_trains+num_vals]
print(f"Part of val ids for node classification: {nc_val_ids[:3]}")
np.save(nc_val_ids_path, nc_val_ids)
print(f"NC val ids are saved to {nc_val_ids_path}\n")
nc_val_labels_path = os.path.join(base_dir, "nc-val-labels.pt")
nc_val_labels = torch.randint(0, 10, (num_vals,))
print(f"Part of val labels for node classification: {nc_val_labels[:3]}")
torch.save(nc_val_labels, nc_val_labels_path)
print(f"NC val labels are saved to {nc_val_labels_path}\n")
nc_test_ids_path = os.path.join(base_dir, "nc-test-ids.npy")
nc_test_ids = ids[-num_tests:]
print(f"Part of test ids for node classification: {nc_test_ids[:3]}")
np.save(nc_test_ids_path, nc_test_ids)
print(f"NC test ids are saved to {nc_test_ids_path}\n")
nc_test_labels_path = os.path.join(base_dir, "nc-test-labels.pt")
nc_test_labels = torch.randint(0, 10, (num_tests,))
print(f"Part of test labels for node classification: {nc_test_labels[:3]}")
torch.save(nc_test_labels, nc_test_labels_path)
print(f"NC test labels are saved to {nc_test_labels_path}\n")
Part of train ids for node classification: [771 803 744]
NC train ids are saved to ./ondisk_dataset_homograph/nc-train-ids.npy
Part of train labels for node classification: tensor([7, 5, 4])
NC train labels are saved to ./ondisk_dataset_homograph/nc-train-labels.pt
Part of val ids for node classification: [180 231 146]
NC val ids are saved to ./ondisk_dataset_homograph/nc-val-ids.npy
Part of val labels for node classification: tensor([4, 2, 4])
NC val labels are saved to ./ondisk_dataset_homograph/nc-val-labels.pt
Part of test ids for node classification: [202 41 113]
NC test ids are saved to ./ondisk_dataset_homograph/nc-test-ids.npy
Part of test labels for node classification: tensor([6, 5, 0])
NC test labels are saved to ./ondisk_dataset_homograph/nc-test-labels.pt
Link Prediction Taskο
For link prediction task, we need seeds or corresponding labels and indexes which representing the pos/neg property and group of the seeds for each training/validation/test set. Like feature data, numpy arrays and torch tensors are supported for these sets.
[6]:
num_trains = int(num_edges * 0.6)
num_vals = int(num_edges * 0.2)
num_tests = num_edges - num_trains - num_vals
lp_train_seeds_path = os.path.join(base_dir, "lp-train-seeds.npy")
lp_train_seeds = edges[:num_trains, :]
print(f"Part of train seeds for link prediction: {lp_train_seeds[:3]}")
np.save(lp_train_seeds_path, lp_train_seeds)
print(f"LP train seeds are saved to {lp_train_seeds_path}\n")
lp_val_seeds_path = os.path.join(base_dir, "lp-val-seeds.npy")
lp_val_seeds = edges[num_trains:num_trains+num_vals, :]
lp_val_neg_dsts = np.random.randint(0, num_nodes, (num_vals, 10)).reshape(-1)
lp_val_neg_srcs = np.repeat(lp_val_seeds[:,0], 10)
lp_val_neg_seeds = np.concatenate((lp_val_neg_srcs, lp_val_neg_dsts)).reshape(2,-1).T
lp_val_seeds = np.concatenate((lp_val_seeds, lp_val_neg_seeds))
print(f"Part of val seeds for link prediction: {lp_val_seeds[:3]}")
np.save(lp_val_seeds_path, lp_val_seeds)
print(f"LP val seeds are saved to {lp_val_seeds_path}\n")
lp_val_labels_path = os.path.join(base_dir, "lp-val-labels.npy")
lp_val_labels = np.empty(num_vals * (10 + 1))
lp_val_labels[:num_vals] = 1
lp_val_labels[num_vals:] = 0
print(f"Part of val labels for link prediction: {lp_val_labels[:3]}")
np.save(lp_val_labels_path, lp_val_labels)
print(f"LP val labels are saved to {lp_val_labels_path}\n")
lp_val_indexes_path = os.path.join(base_dir, "lp-val-indexes.npy")
lp_val_indexes = np.arange(0, num_vals)
lp_val_neg_indexes = np.repeat(lp_val_indexes, 10)
lp_val_indexes = np.concatenate([lp_val_indexes, lp_val_neg_indexes])
print(f"Part of val indexes for link prediction: {lp_val_indexes[:3]}")
np.save(lp_val_indexes_path, lp_val_indexes)
print(f"LP val indexes are saved to {lp_val_indexes_path}\n")
lp_test_seeds_path = os.path.join(base_dir, "lp-test-seeds.npy")
lp_test_seeds = edges[-num_tests:, :]
lp_test_neg_dsts = np.random.randint(0, num_nodes, (num_tests, 10)).reshape(-1)
lp_test_neg_srcs = np.repeat(lp_test_seeds[:,0], 10)
lp_test_neg_seeds = np.concatenate((lp_test_neg_srcs, lp_test_neg_dsts)).reshape(2,-1).T
lp_test_seeds = np.concatenate((lp_test_seeds, lp_test_neg_seeds))
print(f"Part of test seeds for link prediction: {lp_test_seeds[:3]}")
np.save(lp_test_seeds_path, lp_test_seeds)
print(f"LP test seeds are saved to {lp_test_seeds_path}\n")
lp_test_labels_path = os.path.join(base_dir, "lp-test-labels.npy")
lp_test_labels = np.empty(num_tests * (10 + 1))
lp_test_labels[:num_tests] = 1
lp_test_labels[num_tests:] = 0
print(f"Part of val labels for link prediction: {lp_test_labels[:3]}")
np.save(lp_test_labels_path, lp_test_labels)
print(f"LP test labels are saved to {lp_test_labels_path}\n")
lp_test_indexes_path = os.path.join(base_dir, "lp-test-indexes.npy")
lp_test_indexes = np.arange(0, num_tests)
lp_test_neg_indexes = np.repeat(lp_test_indexes, 10)
lp_test_indexes = np.concatenate([lp_test_indexes, lp_test_neg_indexes])
print(f"Part of test indexes for link prediction: {lp_test_indexes[:3]}")
np.save(lp_test_indexes_path, lp_test_indexes)
print(f"LP test indexes are saved to {lp_test_indexes_path}\n")
Part of train seeds for link prediction: [[890 944]
[183 149]
[947 686]]
LP train seeds are saved to ./ondisk_dataset_homograph/lp-train-seeds.npy
Part of val seeds for link prediction: [[522 886]
[816 387]
[ 83 526]]
LP val seeds are saved to ./ondisk_dataset_homograph/lp-val-seeds.npy
Part of val labels for link prediction: [1. 1. 1.]
LP val labels are saved to ./ondisk_dataset_homograph/lp-val-labels.npy
Part of val indexes for link prediction: [0 1 2]
LP val indexes are saved to ./ondisk_dataset_homograph/lp-val-indexes.npy
Part of test seeds for link prediction: [[619 416]
[116 314]
[325 318]]
LP test seeds are saved to ./ondisk_dataset_homograph/lp-test-seeds.npy
Part of val labels for link prediction: [1. 1. 1.]
LP test labels are saved to ./ondisk_dataset_homograph/lp-test-labels.npy
Part of test indexes for link prediction: [0 1 2]
LP test indexes are saved to ./ondisk_dataset_homograph/lp-test-indexes.npy
Organize Data into YAML Fileο
Now we need to create a metadata.yaml
file which contains the paths, dadta types of graph structure, feature data, training/validation/test sets.
Notes: - all path should be relative to metadata.yaml
. - Below fields are optional and not specified in below example. - in_memory
: indicates whether to load dada into memory or mmap
. Default is True
.
Please refer to YAML specification for more details.
[7]:
yaml_content = f"""
dataset_name: homogeneous_graph_nc_lp
graph:
nodes:
- num: {num_nodes}
edges:
- format: csv
path: {os.path.basename(edges_path)}
feature_data:
- domain: node
name: feat_0
format: numpy
path: {os.path.basename(node_feat_0_path)}
- domain: node
name: feat_1
format: torch
path: {os.path.basename(node_feat_1_path)}
- domain: edge
name: feat_0
format: numpy
path: {os.path.basename(edge_feat_0_path)}
- domain: edge
name: feat_1
format: torch
path: {os.path.basename(edge_feat_1_path)}
tasks:
- name: node_classification
num_classes: 10
train_set:
- data:
- name: seeds
format: numpy
path: {os.path.basename(nc_train_ids_path)}
- name: labels
format: torch
path: {os.path.basename(nc_train_labels_path)}
validation_set:
- data:
- name: seeds
format: numpy
path: {os.path.basename(nc_val_ids_path)}
- name: labels
format: torch
path: {os.path.basename(nc_val_labels_path)}
test_set:
- data:
- name: seeds
format: numpy
path: {os.path.basename(nc_test_ids_path)}
- name: labels
format: torch
path: {os.path.basename(nc_test_labels_path)}
- name: link_prediction
num_classes: 10
train_set:
- data:
- name: seeds
format: numpy
path: {os.path.basename(lp_train_seeds_path)}
validation_set:
- data:
- name: seeds
format: numpy
path: {os.path.basename(lp_val_seeds_path)}
- name: labels
format: numpy
path: {os.path.basename(lp_val_labels_path)}
- name: indexes
format: numpy
path: {os.path.basename(lp_val_indexes_path)}
test_set:
- data:
- name: seeds
format: numpy
path: {os.path.basename(lp_test_seeds_path)}
- name: labels
format: numpy
path: {os.path.basename(lp_test_labels_path)}
- name: indexes
format: numpy
path: {os.path.basename(lp_test_indexes_path)}
"""
metadata_path = os.path.join(base_dir, "metadata.yaml")
with open(metadata_path, "w") as f:
f.write(yaml_content)
Instantiate OnDiskDataset
ο
Now weβre ready to load dataset via dgl.graphbolt.OnDiskDataset
. When instantiating, we just pass in the base directory where metadata.yaml
file lies.
During first instantiation, GraphBolt preprocesses the raw data such as constructing FusedCSCSamplingGraph
from edges. All data including graph, feature data, training/validation/test sets are put into preprocessed
directory after preprocessing. Any following dataset loading will skip the preprocess stage.
After preprocessing, load()
is required to be called explicitly in order to load graph, feature data and tasks.
[8]:
dataset = gb.OnDiskDataset(base_dir).load()
graph = dataset.graph
print(f"Loaded graph: {graph}\n")
feature = dataset.feature
print(f"Loaded feature store: {feature}\n")
tasks = dataset.tasks
nc_task = tasks[0]
print(f"Loaded node classification task: {nc_task}\n")
lp_task = tasks[1]
print(f"Loaded link prediction task: {lp_task}\n")
Start to preprocess the on-disk dataset.
Finish preprocessing the on-disk dataset.
Loaded graph: FusedCSCSamplingGraph(csc_indptr=tensor([ 0, 12, 24, ..., 9981, 9991, 10000], dtype=torch.int32),
indices=tensor([790, 931, 142, ..., 951, 444, 280], dtype=torch.int32),
total_num_nodes=1000, num_edges=10000,)
Loaded feature store: TorchBasedFeatureStore(
{(<OnDiskFeatureDataDomain.NODE: 'node'>, None, 'feat_0'): TorchBasedFeature(
feature=tensor([[0.0351, 0.2063, 0.2343, 0.4265, 0.2081],
[0.7177, 0.3242, 0.7499, 0.7130, 0.8265],
[0.2375, 0.6172, 0.6683, 0.6708, 0.9614],
...,
[0.9450, 0.9673, 0.5648, 0.8805, 0.6811],
[0.7800, 0.9511, 0.2878, 0.3314, 0.0265],
[0.9299, 0.4989, 0.8476, 0.3453, 0.3147]], dtype=torch.float64),
metadata={},
), (<OnDiskFeatureDataDomain.NODE: 'node'>, None, 'feat_1'): TorchBasedFeature(
feature=tensor([[0.9856, 0.2285, 0.6046, 0.7058, 0.2040],
[0.6838, 0.3148, 0.0609, 0.2369, 0.2482],
[0.5177, 0.0569, 0.9640, 0.2306, 0.4499],
...,
[0.9251, 0.3845, 0.9484, 0.9832, 0.2558],
[0.9860, 0.5216, 0.4398, 0.1711, 0.0892],
[0.2908, 0.2223, 0.9831, 0.4345, 0.7248]]),
metadata={},
), (<OnDiskFeatureDataDomain.EDGE: 'edge'>, None, 'feat_0'): TorchBasedFeature(
feature=tensor([[0.2097, 0.0895, 0.0873, 0.2065, 0.1274],
[0.0272, 0.7266, 0.9897, 0.8618, 0.3046],
[0.6219, 0.0255, 0.8249, 0.5854, 0.4256],
...,
[0.9413, 0.1016, 0.2372, 0.7718, 0.2183],
[0.0224, 0.1658, 0.6894, 0.5015, 0.1779],
[0.6581, 0.6534, 0.2304, 0.6568, 0.9792]], dtype=torch.float64),
metadata={},
), (<OnDiskFeatureDataDomain.EDGE: 'edge'>, None, 'feat_1'): TorchBasedFeature(
feature=tensor([[0.5470, 0.1422, 0.8522, 0.4386, 0.3475],
[0.8643, 0.3016, 0.5732, 0.5025, 0.8706],
[0.2292, 0.9007, 0.2978, 0.2307, 0.2727],
...,
[0.2477, 0.3347, 0.5781, 0.9754, 0.9883],
[0.7115, 0.9300, 0.4792, 0.5849, 0.3721],
[0.8965, 0.0112, 0.9746, 0.1442, 0.9519]]),
metadata={},
)}
)
Loaded node classification task: OnDiskTask(validation_set=ItemSet(
items=(tensor([180, 231, 146, 692, 624, 963, 900, 484, 975, 564, 406, 778, 690, 845,
952, 297, 48, 90, 201, 200, 673, 520, 253, 794, 271, 698, 802, 974,
68, 948, 293, 417, 796, 940, 460, 812, 613, 117, 617, 449, 689, 830,
144, 864, 561, 799, 152, 260, 726, 541, 63, 839, 172, 309, 798, 955,
480, 425, 814, 438, 699, 755, 198, 657, 408, 264, 913, 579, 36, 298,
870, 601, 290, 66, 844, 777, 162, 119, 416, 522, 728, 32, 49, 367,
159, 924, 474, 896, 513, 281, 171, 428, 683, 340, 823, 110, 782, 631,
123, 797, 518, 156, 708, 158, 717, 851, 458, 614, 124, 109, 347, 582,
893, 176, 912, 67, 321, 42, 64, 360, 116, 982, 13, 980, 722, 585,
509, 382, 7, 278, 906, 772, 737, 966, 145, 71, 490, 719, 154, 884,
276, 874, 317, 193, 569, 96, 235, 969, 647, 696, 619, 307, 3, 215,
341, 221, 545, 979, 676, 827, 670, 148, 701, 939, 324, 577, 714, 210,
370, 4, 628, 56, 749, 386, 122, 483, 143, 133, 366, 351, 706, 599,
376, 60, 314, 43, 890, 932, 418, 62, 195, 542, 703, 806, 391, 847,
838, 242, 407, 234], dtype=torch.int32), tensor([4, 2, 4, 5, 9, 4, 9, 6, 9, 7, 2, 0, 8, 5, 7, 2, 0, 7, 1, 5, 4, 8, 1, 0,
1, 3, 5, 8, 6, 6, 1, 5, 2, 6, 8, 7, 5, 6, 4, 7, 4, 5, 9, 5, 8, 5, 4, 9,
7, 2, 6, 8, 5, 6, 0, 8, 5, 3, 0, 9, 6, 0, 7, 6, 7, 1, 7, 4, 7, 4, 1, 6,
8, 9, 2, 8, 1, 2, 7, 7, 7, 3, 4, 1, 4, 8, 3, 5, 7, 6, 9, 6, 3, 3, 0, 6,
5, 9, 4, 6, 2, 4, 3, 6, 2, 3, 4, 4, 1, 9, 2, 0, 6, 2, 4, 0, 4, 8, 8, 1,
3, 1, 0, 7, 7, 9, 3, 6, 1, 2, 3, 0, 7, 4, 0, 3, 1, 7, 1, 2, 3, 8, 6, 7,
3, 6, 3, 2, 4, 4, 7, 1, 7, 2, 8, 0, 1, 3, 2, 5, 0, 4, 4, 9, 7, 1, 0, 9,
9, 5, 9, 0, 6, 2, 3, 0, 1, 9, 3, 4, 9, 9, 2, 6, 1, 3, 4, 8, 9, 6, 1, 7,
8, 7, 7, 9, 6, 5, 4, 7])),
names=('seeds', 'labels'),
),
train_set=ItemSet(
items=(tensor([771, 803, 744, 711, 403, 629, 467, 759, 213, 587, 475, 79, 245, 186,
911, 991, 53, 861, 807, 681, 761, 941, 753, 904, 682, 323, 422, 471,
337, 263, 849, 179, 103, 95, 420, 635, 907, 786, 385, 547, 140, 203,
277, 910, 993, 332, 295, 5, 533, 748, 326, 18, 233, 604, 246, 562,
441, 8, 945, 313, 863, 500, 791, 256, 432, 850, 374, 362, 435, 927,
872, 654, 835, 204, 960, 770, 784, 265, 161, 595, 774, 995, 873, 679,
442, 283, 280, 346, 592, 618, 169, 322, 100, 108, 212, 118, 862, 11,
746, 225, 348, 29, 928, 491, 287, 984, 227, 917, 439, 496, 967, 693,
498, 976, 622, 757, 69, 250, 767, 526, 553, 538, 16, 436, 25, 584,
593, 147, 852, 470, 894, 548, 59, 996, 55, 305, 603, 20, 378, 843,
921, 942, 339, 134, 880, 517, 375, 230, 206, 383, 34, 985, 80, 868,
241, 724, 248, 773, 473, 633, 77, 616, 52, 957, 598, 455, 157, 973,
445, 605, 704, 51, 207, 272, 832, 818, 371, 855, 84, 216, 447, 58,
627, 137, 456, 492, 556, 389, 621, 733, 2, 228, 434, 267, 399, 697,
163, 662, 691, 466, 626, 815, 153, 660, 968, 486, 877, 887, 86, 0,
933, 846, 502, 800, 684, 865, 205, 300, 576, 646, 487, 729, 372, 831,
529, 65, 261, 268, 620, 970, 986, 168, 444, 611, 669, 720, 735, 765,
640, 46, 524, 335, 836, 350, 594, 964, 426, 937, 694, 898, 885, 510,
909, 93, 240, 92, 44, 75, 769, 31, 255, 578, 949, 315, 121, 494,
303, 14, 792, 452, 586, 329, 700, 508, 330, 947, 680, 804, 142, 74,
567, 30, 405, 209, 555, 33, 289, 364, 903, 899, 926, 136, 908, 107,
503, 521, 813, 760, 312, 61, 184, 454, 822, 530, 936, 742, 891, 801,
842, 223, 871, 575, 606, 989, 892, 951, 573, 443, 953, 856, 747, 658,
12, 990, 368, 88, 580, 751, 934, 70, 331, 723, 930, 558, 790, 325,
588, 745, 685, 674, 946, 132, 448, 26, 914, 950, 552, 525, 45, 224,
98, 352, 652, 130, 365, 91, 833, 596, 789, 857, 356, 590, 816, 398,
641, 129, 427, 867, 643, 841, 511, 916, 876, 73, 338, 743, 988, 738,
634, 707, 19, 430, 17, 166, 554, 311, 648, 319, 219, 404, 28, 858,
150, 400, 805, 457, 185, 825, 357, 182, 709, 854, 672, 105, 750, 318,
415, 196, 497, 214, 817, 102, 678, 659, 741, 961, 135, 304, 327, 463,
37, 962, 50, 649, 829, 664, 922, 788, 247, 489, 727, 783, 211, 111,
334, 279, 308, 138, 262, 811, 623, 306, 881, 412, 462, 481, 421, 288,
165, 257, 249, 882, 57, 270, 666, 22, 546, 433, 935, 394, 104, 10,
141, 944, 373, 531, 929, 736, 574, 752, 570, 549, 540, 15, 568, 358,
208, 197, 106, 779, 345, 464, 477, 918, 446, 414, 396, 1, 888, 994,
615, 739, 413, 431, 998, 840, 915, 191, 787, 762, 740, 971, 251, 919,
630, 516, 688, 972, 983, 469, 766, 409, 638, 131, 859, 507, 384, 285,
809, 869, 410, 239, 506, 85, 175, 637, 566, 780, 544, 810, 437, 583,
282, 97, 379, 294, 828, 821, 328, 177, 834, 527, 173, 120, 675, 686,
485, 756, 901, 897, 302, 853, 252, 114, 954, 194, 411, 343, 236, 572,
226, 695, 183, 468, 192, 190, 515, 943, 361, 731, 600, 889, 523, 401,
902, 608, 826, 344, 292, 243, 429, 987, 83, 139, 189, 644, 534, 734,
178, 377, 519, 181, 254, 359, 656, 266, 199, 718, 188, 187],
dtype=torch.int32), tensor([7, 5, 4, 2, 6, 1, 9, 4, 8, 9, 8, 1, 6, 5, 1, 0, 6, 0, 8, 2, 5, 1, 1, 6,
5, 4, 6, 8, 5, 7, 8, 2, 9, 4, 7, 7, 9, 8, 4, 0, 2, 9, 8, 5, 0, 6, 3, 1,
1, 9, 6, 4, 3, 9, 4, 9, 9, 2, 4, 2, 1, 1, 0, 4, 0, 3, 9, 1, 3, 1, 3, 7,
9, 0, 5, 3, 1, 9, 2, 8, 7, 5, 4, 6, 3, 0, 7, 9, 3, 6, 5, 3, 8, 6, 1, 4,
8, 1, 7, 0, 9, 4, 7, 9, 7, 9, 3, 1, 5, 5, 2, 5, 4, 9, 3, 1, 0, 1, 1, 3,
3, 9, 2, 5, 8, 5, 3, 2, 4, 1, 6, 7, 0, 9, 6, 8, 0, 1, 5, 8, 8, 9, 0, 9,
3, 6, 8, 9, 8, 4, 9, 9, 6, 1, 0, 9, 7, 0, 5, 4, 0, 5, 3, 4, 8, 7, 4, 0,
2, 1, 9, 7, 3, 6, 9, 7, 1, 3, 3, 1, 1, 3, 9, 7, 6, 1, 1, 0, 6, 2, 6, 5,
5, 3, 4, 4, 0, 3, 1, 1, 6, 7, 2, 1, 9, 6, 1, 0, 5, 8, 1, 7, 2, 3, 7, 0,
2, 2, 8, 8, 9, 3, 9, 9, 0, 3, 7, 9, 7, 4, 6, 5, 7, 3, 4, 1, 4, 1, 4, 7,
8, 8, 2, 7, 5, 6, 6, 6, 5, 0, 5, 0, 7, 1, 3, 2, 9, 4, 3, 6, 0, 8, 2, 4,
6, 5, 8, 0, 2, 4, 1, 6, 9, 0, 3, 7, 8, 3, 4, 6, 4, 3, 4, 8, 0, 5, 7, 6,
0, 6, 7, 1, 9, 9, 0, 9, 4, 7, 1, 4, 9, 0, 7, 3, 9, 9, 1, 2, 1, 5, 8, 7,
1, 6, 3, 3, 9, 0, 4, 5, 5, 0, 5, 1, 3, 2, 8, 4, 7, 0, 5, 6, 0, 6, 0, 4,
2, 4, 9, 1, 4, 6, 4, 7, 3, 0, 0, 3, 2, 3, 3, 2, 0, 5, 9, 8, 0, 7, 2, 7,
2, 3, 3, 5, 4, 5, 1, 8, 7, 1, 9, 0, 3, 7, 8, 8, 5, 3, 6, 9, 7, 5, 8, 5,
5, 6, 6, 7, 5, 8, 2, 8, 7, 7, 1, 0, 0, 6, 6, 1, 5, 3, 2, 9, 9, 2, 8, 4,
0, 7, 1, 0, 6, 7, 7, 2, 4, 8, 4, 8, 5, 3, 4, 2, 2, 8, 1, 8, 1, 7, 8, 6,
6, 0, 2, 5, 5, 4, 7, 0, 6, 0, 7, 6, 6, 1, 6, 2, 3, 2, 9, 7, 7, 1, 4, 4,
3, 5, 3, 7, 4, 9, 8, 3, 4, 9, 7, 6, 6, 8, 8, 8, 8, 0, 2, 1, 5, 6, 5, 3,
4, 6, 8, 3, 0, 4, 6, 3, 7, 4, 3, 9, 3, 3, 0, 5, 1, 8, 8, 1, 3, 9, 2, 3,
0, 5, 5, 3, 2, 3, 1, 3, 6, 3, 5, 0, 5, 2, 1, 6, 6, 7, 4, 7, 3, 7, 8, 5,
0, 6, 8, 2, 5, 3, 7, 1, 5, 1, 6, 5, 4, 3, 1, 6, 5, 8, 0, 1, 2, 0, 9, 3,
7, 6, 2, 2, 7, 4, 5, 6, 6, 8, 6, 0, 9, 8, 3, 6, 1, 3, 0, 2, 8, 5, 6, 3,
9, 5, 8, 0, 6, 8, 6, 5, 9, 2, 1, 5, 0, 3, 1, 0, 0, 2, 2, 6, 1, 9, 5, 0])),
names=('seeds', 'labels'),
),
test_set=ItemSet(
items=(tensor([202, 41, 113, 819, 479, 424, 353, 39, 167, 164, 218, 559, 355, 661,
612, 392, 959, 632, 72, 286, 999, 459, 393, 793, 848, 160, 423, 721,
112, 349, 76, 504, 965, 710, 310, 453, 514, 725, 824, 775, 9, 115,
501, 636, 981, 38, 591, 275, 886, 316, 716, 274, 238, 536, 23, 781,
333, 639, 232, 977, 512, 35, 388, 667, 732, 602, 712, 461, 645, 493,
997, 505, 78, 866, 702, 273, 258, 229, 663, 925, 625, 472, 978, 655,
808, 820, 342, 713, 336, 101, 539, 419, 609, 597, 476, 222, 764, 563,
269, 54, 363, 259, 754, 47, 155, 550, 126, 369, 89, 651, 296, 650,
395, 299, 705, 571, 24, 402, 125, 174, 354, 21, 387, 956, 532, 879,
320, 535, 81, 931, 758, 551, 589, 220, 878, 557, 938, 543, 27, 560,
488, 465, 87, 668, 610, 451, 992, 244, 82, 768, 958, 151, 40, 607,
478, 499, 381, 128, 687, 380, 671, 450, 537, 581, 730, 397, 860, 390,
291, 715, 565, 6, 170, 528, 482, 653, 776, 883, 440, 642, 301, 895,
875, 905, 795, 237, 920, 99, 217, 94, 149, 785, 763, 677, 837, 665,
495, 923, 284, 127], dtype=torch.int32), tensor([6, 5, 0, 3, 8, 2, 3, 6, 5, 2, 7, 5, 0, 6, 5, 8, 7, 7, 5, 8, 3, 6, 0, 3,
8, 6, 2, 1, 7, 7, 8, 5, 5, 5, 1, 2, 4, 0, 6, 2, 5, 1, 5, 0, 8, 8, 1, 8,
5, 7, 6, 0, 5, 2, 4, 8, 4, 3, 5, 3, 3, 5, 3, 0, 7, 3, 7, 4, 9, 7, 2, 4,
4, 8, 9, 0, 6, 3, 4, 0, 8, 9, 0, 9, 8, 0, 2, 9, 7, 8, 7, 8, 3, 3, 8, 6,
4, 2, 7, 7, 3, 4, 8, 1, 0, 3, 8, 0, 1, 4, 6, 5, 0, 5, 5, 0, 6, 1, 1, 2,
3, 0, 8, 2, 2, 4, 2, 4, 2, 4, 3, 2, 7, 3, 2, 3, 4, 2, 1, 2, 1, 8, 0, 3,
7, 6, 9, 0, 9, 5, 9, 9, 8, 9, 7, 8, 4, 3, 2, 3, 0, 2, 2, 3, 1, 4, 6, 6,
9, 3, 9, 8, 4, 2, 2, 6, 7, 2, 8, 5, 2, 9, 3, 3, 3, 1, 6, 8, 4, 7, 3, 3,
5, 1, 4, 4, 4, 7, 3, 4])),
names=('seeds', 'labels'),
),
metadata={'name': 'node_classification', 'num_classes': 10},)
Loaded link prediction task: OnDiskTask(validation_set=ItemSet(
items=(tensor([[522, 886],
[816, 387],
[ 83, 526],
...,
[268, 542],
[268, 739],
[268, 235]], dtype=torch.int32), tensor([1., 1., 1., ..., 0., 0., 0.], dtype=torch.float64), tensor([ 0, 1, 2, ..., 1999, 1999, 1999])),
names=('seeds', 'labels', 'indexes'),
),
train_set=ItemSet(
items=(tensor([[890, 944],
[183, 149],
[947, 686],
...,
[791, 722],
[ 21, 198],
[836, 820]], dtype=torch.int32),),
names=('seeds',),
),
test_set=ItemSet(
items=(tensor([[619, 416],
[116, 314],
[325, 318],
...,
[432, 849],
[432, 846],
[432, 854]], dtype=torch.int32), tensor([1., 1., 1., ..., 0., 0., 0.], dtype=torch.float64), tensor([ 0, 1, 2, ..., 1999, 1999, 1999])),
names=('seeds', 'labels', 'indexes'),
),
metadata={'name': 'link_prediction', 'num_classes': 10},)
/home/ubuntu/prod-doc/readthedocs.org/user_builds/dgl/envs/2.2.x/lib/python3.8/site-packages/dgl-2.2.1-py3.8-linux-x86_64.egg/dgl/graphbolt/impl/ondisk_dataset.py:460: DGLWarning: Edge feature is stored, but edge IDs are not saved.
dgl_warning("Edge feature is stored, but edge IDs are not saved.")