def test_constant(): t = tr.Constant(10) assert callable(t) g = Graph(x=x, a=a, e=e, y=y_gl) t(g) g = Graph(x=None, a=a, e=e, y=y_gl) t(g)
def test_gcn_filter(): t = tr.GCNFilter() assert callable(t) g = Graph(x=x, a=a, e=e, y=y_nl) t(g) g = Graph(x=x, a=a.A, e=e, y=y_nl) t(g)
def test_degree(): t = tr.Degree(10) assert callable(t) g = Graph(x=x, a=a, e=e, y=y_gl) t(g) g = Graph(x=None, a=a, e=e, y=y_gl) t(g)
def test_normalize_adj(): t = tr.NormalizeAdj() assert callable(t) g = Graph(x=x, a=a, e=e, y=y_nl) t(g) g = Graph(x=x, a=a.A, e=e, y=y_nl) t(g)
def test_clustering_coeff(): t = tr.ClusteringCoeff() assert callable(t) g = Graph(x=x, a=a, e=e, y=y_gl) t(g) g = Graph(x=None, a=a, e=e, y=y_gl) t(g)
def test_delaunay(): t = tr.Delaunay() assert callable(t) x = np.random.rand(N, 2) g = Graph(x=x, a=a, e=e, y=y_nl) t(g) g = Graph(x=x, a=a.A, e=e, y=y_nl) t(g)
def test_one_hot(): t = tr.OneHotLabels(depth=2) assert callable(t) g = Graph(x=x, a=a, e=e, y=y_gl) t(g) g = Graph(x=x, a=a, e=e, y=y_nl) t(g) g = Graph(x=x, a=a, e=e, y=y_sc) t(g)
def test_laplacian_pe(): k = 3 t = tr.LaplacianPE(k) assert callable(t) g = Graph(x=x, a=a, e=e, y=y_gl) t(g) g = Graph(x=None, a=a, e=e, y=y_gl) g_ = t(g) assert g_.x.shape[-1] == k
def read(self): data = np.load(os.path.join(self.path, f'graph.npz'), allow_pickle=True) x, a, y = data['x'].astype(np.float32), data['a'].tolist(), data['y'].astype(np.uint8) self.mask_tr, self.mask_va, self.mask_te = masks_from_gt(ground_truth.data.array[0][:, 1800:3600]) return [Graph(x=x, a=a, y=y)]
def read(self): circuits = [] # circs = [ # "c6288", # "c5315", # "c432", # "c499", # "c880", # "c1355", # "c1908", # "c3540", # "adder.bench", # "arbiter.bench", # "cavlc.bench", # "dec.bench", # "voter.bench", # "sin.bench", # "priority.bench", # ] path = self.path if self.path else "../data/output" circs = self.circs if self.circs else [] for circ in circs: A, X, labels = load_data(circ, path, normalize="") circuits.append(Graph(x=X.toarray(), a=A, y=labels)) print(f"{circ}: {sum(labels)}, {len(labels)}") return circuits
def load_graph(components, adj, omitted_idx=None, shuffle=False): embedding_size = len(h.component_types) all_component_types = np.array([ h.get_component_type_index(c) for c in components ]) omitted_type = all_component_types[omitted_idx] if omitted_idx is not None else 0 included_idx = [ i for i in range(len(all_component_types)) if i != omitted_idx] component_types = all_component_types[included_idx] component_count = component_types.size # nodes... x = np.zeros((component_count, embedding_size)) x[np.arange(component_types.size), component_types] = 1 expanded_adj = np.zeros((x.shape[0], x.shape[0])) for (new_i, old_i) in enumerate(included_idx): expanded_adj[new_i] = adj[old_i,included_idx] # labels... y = np.zeros((len(h.component_types),)) y[omitted_type] = 1 # FIXME: this is not shuffled correctly # if shuffle: # indices = np.arange(x.shape[0]) # np.random.shuffle(indices) # x = np.take(x, indices, axis=0) # expanded_adj = np.take(expanded_adj, indices, axis=0) a = sp.csr_matrix(expanded_adj) return Graph(x=x, a=a, y=y)
def make_graph(n): x = self.xarr[n] y = self.yarr[n] a = self.aarr[n] e = self.edge_attrarr[n] g=Graph(x=x, y=y, a=a,e=e) return g
def test_layer_preprocess(): from spektral.layers import GCNConv t = tr.LayerPreprocess(GCNConv) assert callable(t) g = Graph(x=x, a=a, e=e, y=y_nl) t(g)
def read(self): f = np.load(osp.join(self.path, "dblp.npz")) x = sp.csr_matrix( (f["attr_data"], f["attr_indices"], f["attr_indptr"]), f["attr_shape"] ).toarray() x[x > 0] = 1 if self.normalize_x: print("Pre-processing node features") x = _preprocess_features(x) a = sp.csr_matrix( (f["adj_data"], f["adj_indices"], f["adj_indptr"]), f["adj_shape"] ) # .tocoo() y = f["labels"] y = label_to_one_hot(y, np.unique(y)) return [ Graph( x=x.astype(self.dtype), a=a.astype(self.dtype), y=y.astype(self.dtype), ) ]
def read(self): print("Loading QM9 dataset.") sdf_file = osp.join(self.path, "gdb9.sdf") data = load_sdf(sdf_file, amount=self.amount) # Internal SDF format def read_mol(mol): x = np.array([atom_to_feature(atom) for atom in mol["atoms"]]) a, e = mol_to_adj(mol) return x, a, e data = Parallel(n_jobs=self.n_jobs)( delayed(read_mol)(mol) for mol in tqdm(data, ncols=80) ) x_list, a_list, e_list = list(zip(*data)) # Load labels labels_file = osp.join(self.path, "gdb9.sdf.csv") labels = load_csv(labels_file) labels = labels.set_index("mol_id").values if self.amount is not None: labels = labels[: self.amount] return [ Graph(x=x, a=a, e=e, y=y) for x, a, e, y in zip(x_list, a_list, e_list, labels) ]
def read(self): objects = [_read_file(self.path, self.name, s) for s in self.suffixes] objects = [o.A if sp.issparse(o) else o for o in objects] x, y, tx, ty, allx, ally, graph, idx_te = objects # Public Planetoid splits. This is the default idx_tr = np.arange(y.shape[0]) idx_va = np.arange(y.shape[0], y.shape[0] + 500) idx_te = idx_te.astype(int) idx_te_sort = np.sort(idx_te) # Fix disconnected nodes in Citeseer if self.name == "citeseer": idx_te_len = idx_te.max() - idx_te.min() + 1 tx_ext = np.zeros((idx_te_len, x.shape[1])) tx_ext[idx_te_sort - idx_te.min(), :] = tx tx = tx_ext ty_ext = np.zeros((idx_te_len, y.shape[1])) ty_ext[idx_te_sort - idx_te.min(), :] = ty ty = ty_ext x = np.vstack((allx, tx)) y = np.vstack((ally, ty)) x[idx_te, :] = x[idx_te_sort, :] y[idx_te, :] = y[idx_te_sort, :] # Row-normalize the features if self.normalize_x: print("Pre-processing node features") x = _preprocess_features(x) if self.random_split: # Throw away public splits and compute random ones like Shchur et al. indices = np.arange(y.shape[0]) n_classes = y.shape[1] idx_tr, idx_te, _, y_te = train_test_split( indices, y, train_size=20 * n_classes, stratify=y ) idx_va, idx_te = train_test_split( idx_te, train_size=30 * n_classes, stratify=y_te ) # Adjacency matrix a = nx.adjacency_matrix(nx.from_dict_of_lists(graph)) # CSR a.setdiag(0) a.eliminate_zeros() # Train/valid/test masks self.mask_tr = _idx_to_mask(idx_tr, y.shape[0]) self.mask_va = _idx_to_mask(idx_va, y.shape[0]) self.mask_te = _idx_to_mask(idx_te, y.shape[0]) return [ Graph( x=x.astype(self.dtype), a=a.astype(self.dtype), y=y.astype(self.dtype), ) ]
def read(self): return [ Graph( x=np.random.rand(n, 2), a=np.random.randint(0, 2, (n, n)), y=np.array([0.0, 1.0]), ) for n in range(size) ]
def load_graph(components, adj): component_count = len(components) component_types = np.array([ h.get_component_type_index(c) for c in components ]) # nodes... x = np.zeros((component_count, embedding_size)) x[np.arange(component_types.size), component_types] = 1 a = sp.csr_matrix(adj) return Graph(x=x, a=a)
def read(self): data = np.load(os.path.join(self.path, f'graph.npz'), allow_pickle=True) x, a, y = data['x'].astype(np.float32), data['a'].tolist(), data['y'].astype(np.uint8) # Train/valid/test masks self.mask_tr = np.array([True] + [False] * 32 + [True]) self.mask_va = np.array([False if i % 2 == 0 else True for i in range(34)]) self.mask_va[[0, -1]] = False self.mask_te = ~(self.mask_tr + self.mask_va) return [Graph(x=x, a=a, y=y)]
def read(self): self.a = _mnist_grid_graph(self.k) self.a = _flip_random_edges(self.a, self.p_flip) (x_train, y_train), (x_test, y_test) = m.load_data() x = np.vstack((x_train, x_test)) x = x / 255.0 y = np.concatenate((y_train, y_test), 0) x = x.reshape(-1, MNIST_SIZE**2, 1) return [Graph(x=x_, y=y_) for x_, y_ in zip(x, y)]
def _get_graph(n_nodes, n_features, n_edge_features=None, sparse=False): x = np.random.rand(n_nodes, n_features) a = np.random.randint(0, 2, (n_nodes, n_nodes)).astype("f4") e = ( np.random.rand(np.count_nonzero(a), n_edge_features) if n_edge_features is not None else None ) if sparse: a = sp.csr_matrix(a) return Graph(x=x, a=a, e=e)
def read(self): circuits = [] # circs = ['c6288','c5315','c432', 'c499', 'c17', 'c880', 'c1355', 'c1908', 'c3540', 'adder.bench', 'arbiter.bench', 'cavlc.bench', 'dec.bench', 'voter.bench', "sin.bench","priority.bench", "multiplier.bench", "max.bench"] circs = ["adder.bench","arbiter.bench","c1355","c1908","c3540","c432","c499","c5315","c6288","c880","cavlc.bench","dec.bench","int2float.bench","max.bench","multiplier.bench","priority.bench","sin.bench","voter.bench"] # circs = ["log2.bench"] # circs = ['adder.bench', "arbiter.bench", "sin.bench", "multiplier.bench", "voter.bench", "priority.bench"] for circ in circs: A, X, labels = load_data(circ, '../data/output', normalize="") # if sum(labels) >= 500: print(f"{circ}: {sum(labels)}, {len(labels)}") circuits.append(Graph(x=X.toarray(), a=A, y=labels)) return circuits
def read(self): npz_file = osp.join(self.path, self.name) + '.npz' data = np.load(npz_file) x = data['x'] a = sp.csr_matrix( (data['adj_data'], (data['adj_row'], data['adj_col'])), shape=data['adj_shape']) y = data['y'] self.mask_tr = data['mask_tr'] self.mask_va = data['mask_va'] self.mask_te = data['mask_te'] return [Graph(x=x, a=a, y=y)]
def graph_generator(): # Loop over files for xy_path, a_path in zip(x_files, a_files): xy_file = pickle.load(open(xy_path, "rb")) # print(xy_file) xs, ys = xy_file As = pickle.load(open(a_path, "rb")) # Loop over data for x, y, a in zip(xs, ys, As): yield Graph(x=x, a=a, y=y)
def read(self): npz_file = osp.join(self.path, self.name) + ".npz" data = np.load(npz_file) x = data["x"] a = sp.csr_matrix( (data["adj_data"], (data["adj_row"], data["adj_col"])), shape=data["adj_shape"], ) y = data["y"] self.mask_tr = data["mask_tr"] self.mask_va = data["mask_va"] self.mask_te = data["mask_te"] return [Graph(x=x, a=a, y=y)]
def load_graph(self, components, adj, omitted_idx): component_count = len(components) action_component_count = len(all_component_types) total_components = component_count + action_component_count - 1 component_types = np.array( [h.get_component_type_index(c) for c in components]) omitted_type = component_types[omitted_idx] # nodes... x = np.zeros((total_components, embedding_size)) x[np.arange(component_types.size), component_types] = 1 # prototype nodes... action_offset = component_types.size num_actions = len(all_component_types) action_indices = np.zeros(num_actions).astype(int) action_indices[0] = omitted_idx action_indices[1:] = np.arange(action_offset, action_offset + num_actions - 1) action_types = [ idx for idx in range(len(all_component_types)) if idx != omitted_type ] action_types.insert(0, omitted_type) action_types = np.array(action_types).astype(int) x[action_indices, action_index] = 1 x[action_indices, action_types] = 1 expanded_adj = np.zeros((x.shape[0], x.shape[0])) # add connectivity to the action nodes (unidirectional) expanded_adj[:component_count, action_indices] = 1 expanded_adj[omitted_idx, :] = 0 # labels... -1 for nodes to mask y = np.zeros((total_components, )) y[np.arange(component_types.size)] = -1 y[omitted_idx] = 1 if self.shuffle: indices = np.arange(x.shape[0]) np.random.shuffle(indices) x = np.take(x, indices, axis=0) y = np.take(y, indices, axis=0) expanded_adj = np.take(expanded_adj, indices, axis=0) a = sp.csr_matrix(expanded_adj) return Graph(x=x, a=a, y=y)
def read(self): print('Loading QM7 dataset.') mat_file = osp.join(self.path, 'qm7b.mat') data = loadmat(mat_file) coulomb_matrices = data['X'] labels = data['T'] output = [] for i in range(len(coulomb_matrices)): row, col, data = sp.find(coulomb_matrices[i]) a = sp.csr_matrix((np.ones_like(data), (row, col))) e = data[:, None] y = labels[i] output.append(Graph(a=a, e=e, y=y)) return output
def load_graph(components, adj, omitted_idx=None): component_count = len(components) action_component_count = len(all_component_types) if omitted_idx is not None: total_components = component_count + action_component_count - 1 else: total_components = component_count + action_component_count component_types = np.array([ h.get_component_type_index(c) for c in components ]) # nodes... x = np.zeros((total_components, embedding_size)) x[np.arange(component_types.size), component_types] = 1 # prototype nodes... action_offset = component_types.size num_actions = len(all_component_types) action_indices = np.zeros(num_actions).astype(int) if omitted_idx is not None: action_indices[0] = omitted_idx action_indices[1:] = np.arange(action_offset, action_offset + num_actions - 1) omitted_type = component_types[omitted_idx] action_types = [idx for idx in range(len(all_component_types)) if idx != omitted_type] action_types.insert(0, omitted_type) else: action_indices = np.arange(action_offset, action_offset + num_actions) action_types = [idx for idx in range(len(all_component_types))] action_types = np.array(action_types).astype(int) x[action_indices, action_index] = 1 x[action_indices, action_types] = 1 expanded_adj = np.zeros((x.shape[0], x.shape[0])) expanded_adj[0:adj.shape[0], 0:adj.shape[1]] = adj # if self.shuffle: # indices = np.arange(x.shape[0]) # np.random.shuffle(indices) # x = np.take(x, indices, axis=0) # y = np.take(y, indices, axis=0) # expanded_adj = np.take(expanded_adj, indices, axis=0) a = sp.csr_matrix(expanded_adj) return Graph(x=x, a=a)
def make_graph(): n = np.random.randint(self.n_min, self.n_max) colors = np.random.randint(0, self.n_colors, size=n) # Node features x = np.zeros((n, self.n_colors)) x[np.arange(n), colors] = 1 # Edges a = np.random.rand(n, n) <= self.p a = np.maximum(a, a.T).astype(int) a = sp.csr_matrix(a) # Labels y = np.zeros((self.n_colors, )) color_counts = x.sum(0) y[np.argmax(color_counts)] = 1 return Graph(x=x, a=a, y=y)
def read(self): print("Loading QM7 dataset.") mat_file = osp.join(self.path, "qm7b.mat") data = loadmat(mat_file) coulomb_matrices = data["X"] labels = data["T"] output = [] for i in range(len(coulomb_matrices)): row, col, data = sp.find(coulomb_matrices[i]) edge_index = np.array([row, col]).T a, e = sparse.edge_index_to_matrix(edge_index, np.ones_like(data), data) e = data[:, None] y = labels[i] output.append(Graph(a=a, e=e, y=y)) return output