indraroy
commited on
Commit
·
73d6f53
1
Parent(s):
b658338
Add custom dataloader and Hub loader wrapper
Browse files- isonetpp_loader.py +115 -0
- subiso_dataset.py +220 -0
isonetpp_loader.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# isonetpp_loader.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
import os
|
| 4 |
+
import pickle
|
| 5 |
+
from typing import Literal, Optional, Dict
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
from subiso_dataset import SubgraphIsomorphismDataset, TRAIN_MODE, VAL_MODE, TEST_MODE, BROAD_TEST_MODE, GMN_DATA_TYPE, PYG_DATA_TYPE
|
| 10 |
+
except Exception as e:
|
| 11 |
+
raise ImportError(
|
| 12 |
+
"Make sure `subiso_dataset.py` (with SubgraphIsomorphismDataset) is in the same repo.\n"
|
| 13 |
+
f"Import error: {e}"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
Mode = Literal["train", "val", "test", "Extra_test_300"]
|
| 17 |
+
Size = Literal["small", "large"]
|
| 18 |
+
Name = Literal["aids240k", "mutag240k", "ptc_fm240k", "ptc_fr240k", "ptc_mm240k", "ptc_mr240k"]
|
| 19 |
+
|
| 20 |
+
def _mode_prefix(mode: str) -> str:
|
| 21 |
+
# Your file naming uses "test" prefix for Extra_test_300 as well
|
| 22 |
+
return "test" if "test" in mode.lower() else mode
|
| 23 |
+
|
| 24 |
+
def _pair_count(dataset_size: Size) -> str:
|
| 25 |
+
return "80k" if dataset_size == "small" else "240k"
|
| 26 |
+
|
| 27 |
+
def _ensure_paths(
|
| 28 |
+
repo_id: str,
|
| 29 |
+
mode: Mode,
|
| 30 |
+
dataset_name: Name,
|
| 31 |
+
dataset_size: Size,
|
| 32 |
+
local_root: Optional[str] = None,
|
| 33 |
+
) -> Dict[str, str]:
|
| 34 |
+
"""
|
| 35 |
+
Download the three files needed for a given split into local cache (or local_root if set):
|
| 36 |
+
- <mode>_<name><pairs>_query_subgraphs.pkl
|
| 37 |
+
- <mode>_<name><pairs>_rel_nx_is_subgraph_iso.pkl
|
| 38 |
+
- <name><pairs>_corpus_subgraphs.pkl (lives next to splits in our layout under `corpus/`)
|
| 39 |
+
Returns local file paths.
|
| 40 |
+
"""
|
| 41 |
+
prefix = _mode_prefix(mode)
|
| 42 |
+
pairs = _pair_count(dataset_size)
|
| 43 |
+
|
| 44 |
+
# Expected layout in your dataset repo:
|
| 45 |
+
# corpus/<name>_corpus_subgraphs.pkl
|
| 46 |
+
# splits/<mode>/<mode>_<name>_query_subgraphs.pkl
|
| 47 |
+
# splits/<mode>/<mode>_<name>_rel_nx_is_subgraph_iso.pkl
|
| 48 |
+
query_fname = f"{prefix}_{dataset_name}_{'query_subgraphs' if '_' in dataset_name else 'query_subgraphs'}.pkl"
|
| 49 |
+
rel_fname = f"{prefix}_{dataset_name}_{'rel_nx_is_subgraph_iso' if '_' in dataset_name else 'rel_nx_is_subgraph_iso'}.pkl"
|
| 50 |
+
|
| 51 |
+
# Your actual saved names were like: train_aids240k_query_subgraphs.pkl (without extra underscore)
|
| 52 |
+
# So fix the minor formatting exactly:
|
| 53 |
+
query_fname = f"{prefix}_{dataset_name}_query_subgraphs.pkl"
|
| 54 |
+
rel_fname = f"{prefix}_{dataset_name}_rel_nx_is_subgraph_iso.pkl"
|
| 55 |
+
corpus_fname = f"{dataset_name}_corpus_subgraphs.pkl"
|
| 56 |
+
|
| 57 |
+
# Where files are in repo
|
| 58 |
+
repo_query_path = f"splits/{mode}/{query_fname}"
|
| 59 |
+
repo_rel_path = f"splits/{mode}/{rel_fname}"
|
| 60 |
+
repo_corpus_path = f"corpus/{corpus_fname}"
|
| 61 |
+
|
| 62 |
+
# Download to cache (or local_root if provided)
|
| 63 |
+
kwargs = dict(repo_id=repo_id, repo_type="dataset")
|
| 64 |
+
query_path = hf_hub_download(filename=repo_query_path, **kwargs, local_dir=local_root, local_dir_use_symlinks=False)
|
| 65 |
+
rel_path = hf_hub_download(filename=repo_rel_path, **kwargs, local_dir=local_root, local_dir_use_symlinks=False)
|
| 66 |
+
corpus_path= hf_hub_download(filename=repo_corpus_path,**kwargs, local_dir=local_root, local_dir_use_symlinks=False)
|
| 67 |
+
|
| 68 |
+
return {"query": query_path, "rel": rel_path, "corpus": corpus_path}
|
| 69 |
+
|
| 70 |
+
def load_isonetpp_benchmark(
|
| 71 |
+
repo_id: str = "structlearning/isonetpp-benchmark",
|
| 72 |
+
mode: Mode = "train",
|
| 73 |
+
dataset_name: Name = "aids240k",
|
| 74 |
+
dataset_size: Size = "large",
|
| 75 |
+
batch_size: int = 128,
|
| 76 |
+
data_type: str = "pyg",
|
| 77 |
+
device: Optional[str] = None,
|
| 78 |
+
download_root: Optional[str] = None,
|
| 79 |
+
):
|
| 80 |
+
"""
|
| 81 |
+
Returns: an initialized SubgraphIsomorphismDataset with files downloaded from the HF Hub.
|
| 82 |
+
"""
|
| 83 |
+
# Map to your class constants
|
| 84 |
+
mode_map = {
|
| 85 |
+
"train": TRAIN_MODE, "val": VAL_MODE, "test": TEST_MODE, "extra_test_300": BROAD_TEST_MODE, "Extra_test_300": BROAD_TEST_MODE
|
| 86 |
+
}
|
| 87 |
+
mode_norm = mode_map.get(mode, mode)
|
| 88 |
+
|
| 89 |
+
paths = _ensure_paths(
|
| 90 |
+
repo_id=repo_id,
|
| 91 |
+
mode=mode_norm,
|
| 92 |
+
dataset_name=dataset_name,
|
| 93 |
+
dataset_size=dataset_size,
|
| 94 |
+
local_root=download_root,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Your class expects dataset_base_path + "splits/<mode>/..." and "corpus/..."
|
| 98 |
+
# We'll set dataset_base_path to the parent of the downloaded structure and override "dataset_path_override"
|
| 99 |
+
base_path = os.path.dirname(os.path.dirname(paths["query"])) # points to .../splits/
|
| 100 |
+
dataset_base_path = os.path.dirname(base_path) # parent folder containing `splits` and `corpus`
|
| 101 |
+
|
| 102 |
+
dataset_config = dict(
|
| 103 |
+
mode=mode_norm,
|
| 104 |
+
dataset_name=dataset_name,
|
| 105 |
+
dataset_size=dataset_size,
|
| 106 |
+
batch_size=batch_size,
|
| 107 |
+
data_type=data_type,
|
| 108 |
+
dataset_base_path=dataset_base_path,
|
| 109 |
+
experiment=None,
|
| 110 |
+
dataset_path_override=None,
|
| 111 |
+
device=device,
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
ds = SubgraphIsomorphismDataset(**dataset_config)
|
| 115 |
+
return ds
|
subiso_dataset.py
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import copy
|
| 3 |
+
import math
|
| 4 |
+
import pickle
|
| 5 |
+
import random
|
| 6 |
+
import collections
|
| 7 |
+
import numpy as np
|
| 8 |
+
import networkx as nx
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn.functional as F
|
| 11 |
+
from torch_geometric.data import Data
|
| 12 |
+
|
| 13 |
+
TRAIN_MODE = "train"
|
| 14 |
+
VAL_MODE = "val"
|
| 15 |
+
TEST_MODE = "test"
|
| 16 |
+
BROAD_TEST_MODE = "Extra_test_300"
|
| 17 |
+
|
| 18 |
+
GMN_DATA_TYPE = "gmn"
|
| 19 |
+
PYG_DATA_TYPE = "pyg"
|
| 20 |
+
|
| 21 |
+
GraphCollection = collections.namedtuple(
|
| 22 |
+
'GraphCollection',
|
| 23 |
+
['from_idx', 'to_idx', 'node_features', 'edge_features', 'graph_idx', 'num_graphs']
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
class SubgraphIsomorphismDataset:
|
| 27 |
+
def __init__(self, mode, dataset_name, dataset_size, batch_size, data_type, dataset_base_path, experiment, dataset_path_override=None, device=None):
|
| 28 |
+
assert mode in [TRAIN_MODE, VAL_MODE, TEST_MODE, BROAD_TEST_MODE]
|
| 29 |
+
self.mode = mode
|
| 30 |
+
self.dataset_name = dataset_name
|
| 31 |
+
self.dataset_size = dataset_size
|
| 32 |
+
self.max_node_set_size = {"small": 15, "large": 20}[dataset_size]
|
| 33 |
+
self.batch_size = batch_size
|
| 34 |
+
self.data_type = data_type
|
| 35 |
+
self.dataset_base_path = dataset_base_path
|
| 36 |
+
self.device = experiment.device if experiment else (device if device else 'cuda:0')
|
| 37 |
+
self.batch_setting = None
|
| 38 |
+
self.dataset_path_override = dataset_path_override
|
| 39 |
+
|
| 40 |
+
self.load_graphs(experiment=experiment)
|
| 41 |
+
self.preprocess_subgraphs_to_pyG_data()
|
| 42 |
+
self.build_adjacency_info()
|
| 43 |
+
|
| 44 |
+
self.max_edge_set_size = max(
|
| 45 |
+
max([graph.number_of_edges() for graph in self.query_graphs]),
|
| 46 |
+
max([graph.number_of_edges() for graph in self.corpus_graphs])
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
def load_graphs(self, experiment):
|
| 50 |
+
dataset_accessor = lambda file_name: os.path.join(
|
| 51 |
+
self.dataset_base_path, self.dataset_path_override or f"{self.dataset_size}_dataset",
|
| 52 |
+
"splits", self.mode, file_name
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Load query graphs
|
| 56 |
+
pair_count = f"{80 if self.dataset_size == 'small' else 240}k"
|
| 57 |
+
mode_prefix = "test" if "test" in self.mode else self.mode
|
| 58 |
+
query_graph_file = dataset_accessor(f"{mode_prefix}_{self.dataset_name}{pair_count}_query_subgraphs.pkl")
|
| 59 |
+
self.query_graphs = pickle.load(open(query_graph_file, 'rb'))
|
| 60 |
+
num_query_graphs = len(self.query_graphs)
|
| 61 |
+
if experiment:
|
| 62 |
+
experiment.log("loaded %s query graphs from %s", self.mode, query_graph_file)
|
| 63 |
+
|
| 64 |
+
# Load subgraph isomorphism relationships of query vs corpus graphs
|
| 65 |
+
relationships_file = query_graph_file.replace("query_subgraphs", "rel_nx_is_subgraph_iso")
|
| 66 |
+
self.relationships = pickle.load(open(relationships_file, 'rb'))
|
| 67 |
+
if experiment:
|
| 68 |
+
experiment.log("loaded %s relationships from %s", self.mode, relationships_file)
|
| 69 |
+
|
| 70 |
+
assert list(self.relationships.keys()) == list(range(num_query_graphs))
|
| 71 |
+
|
| 72 |
+
# Load corpus graphs
|
| 73 |
+
corpus_graph_file = os.path.join(
|
| 74 |
+
os.path.dirname(os.path.dirname(query_graph_file)),
|
| 75 |
+
f"{self.dataset_name}{pair_count}_corpus_subgraphs.pkl"
|
| 76 |
+
)
|
| 77 |
+
self.corpus_graphs = pickle.load(open(corpus_graph_file, 'rb'))
|
| 78 |
+
if experiment:
|
| 79 |
+
experiment.log("loaded corpus graphs from %s", corpus_graph_file)
|
| 80 |
+
|
| 81 |
+
self.pos_pairs, self.neg_pairs = [], []
|
| 82 |
+
for query_idx in range(num_query_graphs):
|
| 83 |
+
for corpus_idx in self.relationships[query_idx]['pos']:
|
| 84 |
+
self.pos_pairs.append((query_idx, corpus_idx))
|
| 85 |
+
for corpus_idx in self.relationships[query_idx]['neg']:
|
| 86 |
+
self.neg_pairs.append((query_idx, corpus_idx))
|
| 87 |
+
|
| 88 |
+
def create_pyG_object(self, graph):
|
| 89 |
+
num_nodes = graph.number_of_nodes()
|
| 90 |
+
features = torch.ones(num_nodes, 1, dtype=torch.float, device=self.device)
|
| 91 |
+
|
| 92 |
+
edges = list(graph.edges)
|
| 93 |
+
doubled_edges = [[x, y] for (x, y) in edges] + [[y, x] for (x, y) in edges]
|
| 94 |
+
edge_index = torch.tensor(np.array(doubled_edges).T, dtype=torch.int64, device=self.device)
|
| 95 |
+
return Data(x = features, edge_index = edge_index), num_nodes
|
| 96 |
+
|
| 97 |
+
def preprocess_subgraphs_to_pyG_data(self):
|
| 98 |
+
self.query_graph_data, self.query_graph_sizes = zip(
|
| 99 |
+
*[self.create_pyG_object(query_graph) for query_graph in self.query_graphs]
|
| 100 |
+
)
|
| 101 |
+
self.corpus_graph_data, self.corpus_graph_sizes = zip(
|
| 102 |
+
*[self.create_pyG_object(corpus_graph) for corpus_graph in self.corpus_graphs]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
def build_adjacency_info(self):
|
| 106 |
+
def adj_list_from_graph_list(graphs):
|
| 107 |
+
adj_list = []
|
| 108 |
+
for graph in graphs:
|
| 109 |
+
unpadded_adj = torch.tensor(nx.adjacency_matrix(graph).todense(), dtype=torch.float, device=self.device)
|
| 110 |
+
assert unpadded_adj.shape[0] == unpadded_adj.shape[1]
|
| 111 |
+
num_nodes = len(unpadded_adj)
|
| 112 |
+
padded_adj = F.pad(unpadded_adj, pad = (0, self.max_node_set_size - num_nodes, 0, self.max_node_set_size - num_nodes))
|
| 113 |
+
adj_list.append(padded_adj)
|
| 114 |
+
return adj_list
|
| 115 |
+
|
| 116 |
+
self.query_adj_list = adj_list_from_graph_list(self.query_graphs)
|
| 117 |
+
self.corpus_adj_list = adj_list_from_graph_list(self.corpus_graphs)
|
| 118 |
+
|
| 119 |
+
def _pack_batch(self, graphs):
|
| 120 |
+
from_idx = []
|
| 121 |
+
to_idx = []
|
| 122 |
+
graph_idx = []
|
| 123 |
+
all_graphs = [individual_graph for graph_tuple in graphs for individual_graph in graph_tuple]
|
| 124 |
+
|
| 125 |
+
total_nodes, total_edges = 0, 0
|
| 126 |
+
for idx, graph in enumerate(all_graphs):
|
| 127 |
+
num_nodes = graph.number_of_nodes()
|
| 128 |
+
num_edges = graph.number_of_edges()
|
| 129 |
+
edges = np.array(graph.edges(), dtype=np.int32)
|
| 130 |
+
|
| 131 |
+
from_idx.append(edges[:, 0] + total_nodes)
|
| 132 |
+
to_idx.append(edges[:, 1] + total_nodes)
|
| 133 |
+
graph_idx.append(np.ones(num_nodes, dtype=np.int32) * idx)
|
| 134 |
+
|
| 135 |
+
total_nodes += num_nodes
|
| 136 |
+
total_edges += num_edges
|
| 137 |
+
|
| 138 |
+
return GraphCollection(
|
| 139 |
+
from_idx = torch.tensor(np.concatenate(from_idx, axis=0), dtype=torch.int64, device=self.device),
|
| 140 |
+
to_idx = torch.tensor(np.concatenate(to_idx, axis=0), dtype=torch.int64, device=self.device),
|
| 141 |
+
graph_idx = torch.tensor(np.concatenate(graph_idx, axis=0), dtype=torch.int64, device=self.device),
|
| 142 |
+
num_graphs = len(all_graphs),
|
| 143 |
+
node_features = torch.ones(total_nodes, 1, dtype=torch.float, device=self.device),
|
| 144 |
+
edge_features = torch.ones(total_edges, 1, dtype=torch.float, device=self.device)
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
def create_stratified_batches(self):
|
| 148 |
+
self.batch_setting = 'stratified'
|
| 149 |
+
random.shuffle(self.pos_pairs), random.shuffle(self.neg_pairs)
|
| 150 |
+
pos_to_neg_ratio = len(self.pos_pairs) / len(self.neg_pairs)
|
| 151 |
+
|
| 152 |
+
num_pos_per_batch = math.ceil(pos_to_neg_ratio/(1 + pos_to_neg_ratio) * self.batch_size)
|
| 153 |
+
num_neg_per_batch = self.batch_size - num_pos_per_batch
|
| 154 |
+
|
| 155 |
+
batches_pos, batches_neg = [], []
|
| 156 |
+
labels_pos, labels_neg = [], []
|
| 157 |
+
for idx in range(0, len(self.pos_pairs), num_pos_per_batch):
|
| 158 |
+
elements_remaining = len(self.pos_pairs) - idx
|
| 159 |
+
elements_chosen = min(num_pos_per_batch, elements_remaining)
|
| 160 |
+
batches_pos.append(self.pos_pairs[idx : idx + elements_chosen])
|
| 161 |
+
labels_pos.append([1.0] * elements_chosen)
|
| 162 |
+
for idx in range(0, len(self.neg_pairs), num_neg_per_batch):
|
| 163 |
+
elements_remaining = len(self.neg_pairs) - idx
|
| 164 |
+
elements_chosen = min(num_neg_per_batch, elements_remaining)
|
| 165 |
+
batches_neg.append(self.neg_pairs[idx : idx + elements_chosen])
|
| 166 |
+
labels_neg.append([0.0] * elements_chosen)
|
| 167 |
+
|
| 168 |
+
self.num_batches = min(len(batches_pos), len(batches_neg))
|
| 169 |
+
self.batches = [pos + neg for (pos, neg) in zip(batches_pos[:self.num_batches], batches_neg[:self.num_batches])]
|
| 170 |
+
self.labels = [pos + neg for (pos, neg) in zip(labels_pos[:self.num_batches], labels_neg[:self.num_batches])]
|
| 171 |
+
|
| 172 |
+
return self.num_batches
|
| 173 |
+
|
| 174 |
+
def create_custom_batches(self, pair_list):
|
| 175 |
+
self.batch_setting = 'custom'
|
| 176 |
+
self.batches = []
|
| 177 |
+
for idx in range(0, len(pair_list), self.batch_size):
|
| 178 |
+
self.batches.append(pair_list[idx : idx + self.batch_size])
|
| 179 |
+
|
| 180 |
+
self.num_batches = len(self.batches)
|
| 181 |
+
return self.num_batches
|
| 182 |
+
|
| 183 |
+
def fetch_batch_by_id(self, idx):
|
| 184 |
+
assert idx < self.num_batches
|
| 185 |
+
batch = self.batches[idx]
|
| 186 |
+
|
| 187 |
+
query_graph_idxs, corpus_graph_idxs = zip(*batch)
|
| 188 |
+
|
| 189 |
+
if self.data_type == GMN_DATA_TYPE:
|
| 190 |
+
query_graphs = [self.query_graphs[idx] for idx in query_graph_idxs]
|
| 191 |
+
corpus_graphs = [self.corpus_graphs[idx] for idx in corpus_graph_idxs]
|
| 192 |
+
all_graphs = self._pack_batch(zip(query_graphs, corpus_graphs))
|
| 193 |
+
elif self.data_type == PYG_DATA_TYPE:
|
| 194 |
+
query_graphs = [self.query_graph_data[idx] for idx in query_graph_idxs]
|
| 195 |
+
corpus_graphs = [self.corpus_graph_data[idx] for idx in corpus_graph_idxs]
|
| 196 |
+
all_graphs = list(zip(query_graphs, corpus_graphs))
|
| 197 |
+
|
| 198 |
+
query_graph_sizes = [self.query_graph_sizes[idx] for idx in query_graph_idxs]
|
| 199 |
+
corpus_graph_sizes = [self.corpus_graph_sizes[idx] for idx in corpus_graph_idxs]
|
| 200 |
+
all_graph_sizes = list(zip(query_graph_sizes, corpus_graph_sizes))
|
| 201 |
+
|
| 202 |
+
query_graph_adjs = [self.query_adj_list[idx] for idx in query_graph_idxs]
|
| 203 |
+
corpus_graph_adjs = [self.corpus_adj_list[idx] for idx in corpus_graph_idxs]
|
| 204 |
+
all_graph_adjs = list(zip(query_graph_adjs, corpus_graph_adjs))
|
| 205 |
+
|
| 206 |
+
if self.batch_setting == 'stratified':
|
| 207 |
+
target = torch.tensor(np.array(self.labels[idx]), dtype=torch.float, device=self.device)
|
| 208 |
+
return all_graphs, all_graph_sizes, target, all_graph_adjs
|
| 209 |
+
elif self.batch_setting == 'custom':
|
| 210 |
+
return all_graphs, all_graph_sizes, None, all_graph_adjs
|
| 211 |
+
else:
|
| 212 |
+
raise NotImplementedError
|
| 213 |
+
|
| 214 |
+
def get_datasets(dataset_config, experiment, data_type, modes=['train', 'val']):
|
| 215 |
+
return {
|
| 216 |
+
mode: SubgraphIsomorphismDataset(
|
| 217 |
+
mode = mode, experiment = experiment,
|
| 218 |
+
data_type = data_type, **dataset_config
|
| 219 |
+
) for mode in modes
|
| 220 |
+
}
|