-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed: setgnn; feat: load_line_expansion_dataset
- Loading branch information
1 parent
9bbe8cc
commit b1ed256
Showing
4 changed files
with
125 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -204,4 +204,5 @@ Icon | |
Network Trash Folder | ||
Temporary Items | ||
.apdisk | ||
allset_test.py | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import torch | ||
import os.path as osp | ||
import numpy as np | ||
import scipy.sparse as sp | ||
from torch_geometric.data import Data | ||
from torch_sparse import coalesce | ||
|
||
__all__ = ["load_line_expansion_dataset"] | ||
def load_line_expansion_dataset(path=None, dataset="cocitation-cora", train_percent = 0.5): | ||
# load edges, features, and labels. | ||
print('Loading {} dataset...'.format(dataset)) | ||
|
||
file_name = f'{dataset}.content' | ||
p2idx_features_labels = osp.join(path, dataset, file_name) | ||
idx_features_labels = np.genfromtxt(p2idx_features_labels, | ||
dtype=np.dtype(str)) | ||
# features = np.array(idx_features_labels[:, 1:-1]) | ||
features = sp.csr_matrix(idx_features_labels[:, 1:-1], dtype=np.float32) | ||
# labels = encode_onehot(idx_features_labels[:, -1]) | ||
labels = torch.LongTensor(idx_features_labels[:, -1].astype(float)) | ||
|
||
print('load features') | ||
|
||
# build graph | ||
idx = np.array(idx_features_labels[:, 0], dtype=np.int32) | ||
idx_map = {j: i for i, j in enumerate(idx)} | ||
|
||
file_name = f'{dataset}.edges' | ||
p2edges_unordered = osp.join(path, dataset, file_name) | ||
edges_unordered = np.genfromtxt(p2edges_unordered, | ||
dtype=np.int32) | ||
|
||
edges = np.array(list(map(idx_map.get, edges_unordered.flatten())), | ||
dtype=np.int32).reshape(edges_unordered.shape) | ||
|
||
print('load edges') | ||
|
||
|
||
# From adjacency matrix to edge_list | ||
edge_index = edges.T | ||
# ipdb.set_trace() | ||
assert edge_index[0].max() == edge_index[1].min() - 1 | ||
|
||
# check if values in edge_index is consecutive. i.e. no missing value for node_id/he_id. | ||
assert len(np.unique(edge_index)) == edge_index.max() + 1 | ||
|
||
num_nodes = edge_index[0].max() + 1 | ||
num_he = edge_index[1].max() - num_nodes + 1 | ||
edge_index = np.hstack((edge_index, edge_index[::-1, :])) | ||
|
||
# build torch data class | ||
data = Data( | ||
x=torch.FloatTensor(np.array(features[:num_nodes].todense())), | ||
edge_index=torch.LongTensor(edge_index), | ||
y=labels[:num_nodes]) | ||
|
||
|
||
# used user function to override the default function. | ||
# the following will also sort the edge_index and remove duplicates. | ||
total_num_node_id_he_id = len(np.unique(edge_index)) | ||
data.edge_index, data.edge_attr = coalesce(data.edge_index, | ||
None, | ||
total_num_node_id_he_id, | ||
total_num_node_id_he_id) | ||
n_x = num_nodes | ||
# n_x = n_expanded | ||
num_class = len(np.unique(labels[:num_nodes].numpy())) | ||
data.n_x = n_x | ||
# add parameters to attribute | ||
|
||
data.train_percent = train_percent | ||
data.num_hyperedges = num_he | ||
|
||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters