MCPcopy Create free account
hub / github.com/DropEdge/DropEdge / load_citation

Function load_citation

src/utils.py:37–105  ·  view source on GitHub ↗

Load Citation Networks Datasets.

(dataset_str="cora", normalization="AugNormAdj", porting_to_torch=True,data_path=datadir, task_type="full")

Source from the content-addressed store, hash-verified

35
36
37def load_citation(dataset_str="cora", normalization="AugNormAdj", porting_to_torch=True,data_path=datadir, task_type="full"):
38 """
39 Load Citation Networks Datasets.
40 """
41 names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
42 objects = []
43 for i in range(len(names)):
44 with open(os.path.join(data_path, "ind.{}.{}".format(dataset_str.lower(), names[i])), 'rb') as f:
45 if sys.version_info > (3, 0):
46 objects.append(pkl.load(f, encoding='latin1'))
47 else:
48 objects.append(pkl.load(f))
49
50 x, y, tx, ty, allx, ally, graph = tuple(objects)
51 test_idx_reorder = parse_index_file(os.path.join(data_path, "ind.{}.test.index".format(dataset_str)))
52 test_idx_range = np.sort(test_idx_reorder)
53
54 if dataset_str == 'citeseer':
55 # Fix citeseer dataset (there are some isolated nodes in the graph)
56 # Find isolated nodes, add them as zero-vecs into the right position
57 test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder)+1)
58 tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1]))
59 tx_extended[test_idx_range-min(test_idx_range), :] = tx
60 tx = tx_extended
61 ty_extended = np.zeros((len(test_idx_range_full), y.shape[1]))
62 ty_extended[test_idx_range-min(test_idx_range), :] = ty
63 ty = ty_extended
64
65 features = sp.vstack((allx, tx)).tolil()
66 features[test_idx_reorder, :] = features[test_idx_range, :]
67 G = nx.from_dict_of_lists(graph)
68 adj = nx.adjacency_matrix(G)
69 adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
70 # degree = np.asarray(G.degree)
71 degree = np.sum(adj, axis=1)
72
73 labels = np.vstack((ally, ty))
74 labels[test_idx_reorder, :] = labels[test_idx_range, :]
75
76 if task_type == "full":
77 print("Load full supervised task.")
78 #supervised setting
79 idx_test = test_idx_range.tolist()
80 idx_train = range(len(ally)- 500)
81 idx_val = range(len(ally) - 500, len(ally))
82 elif task_type == "semi":
83 print("Load semi-supervised task.")
84 #semi-supervised setting
85 idx_test = test_idx_range.tolist()
86 idx_train = range(len(y))
87 idx_val = range(len(y), len(y)+500)
88 else:
89 raise ValueError("Task type: %s is not supported. Available option: full and semi.")
90
91 adj, features = preprocess_citation(adj, features, normalization)
92 features = np.array(features.todense())
93 labels = np.argmax(labels, axis=1)
94 # porting to pytorch

Callers 1

data_loaderFunction · 0.85

Calls 3

parse_index_fileFunction · 0.85
preprocess_citationFunction · 0.85

Tested by

no test coverage detected