import numpy as np import scipy.sparse as sp import networkx as nx from baselines.graphvae.graphvae_model import graph_show from baselines.graphvae.util import * import baselines.graphvae.util as util from data import bfs_seq, encode_adj, decode_adj def sparse_to_tuple(sparse_mx): if not sp.isspmatrix_coo(sparse_mx): sparse_mx = sparse_mx.tocoo() coords = np.vstack((sparse_mx.row, sparse_mx.col)).transpose() values = sparse_mx.data shape = sparse_mx.shape return coords, values, shape def mask_test_edges(adj): # Function to build test set with 10% positive links # NOTE: Splits are randomized and results might slightly deviate from reported numbers in the paper. # TODO: Clean up. # Remove diagonal elements adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape) adj.eliminate_zeros() # Check that diag is zero: assert np.diag(adj.todense()).sum() == 0 adj_triu = sp.triu(adj) adj_tuple = sparse_to_tuple(adj_triu) edges = adj_tuple[0] edges_all = sparse_to_tuple(adj)[0] num_test = int(np.floor(edges.shape[0] / 10.)) num_val = int(np.floor(edges.shape[0] / 20.)) all_edge_idx = list(range(edges.shape[0])) np.random.shuffle(all_edge_idx) val_edge_idx = all_edge_idx[:num_val] test_edge_idx = all_edge_idx[num_val:(num_val + num_test)] test_edges = edges[test_edge_idx] val_edges = edges[val_edge_idx] train_edges = np.delete(edges, np.hstack([test_edge_idx, val_edge_idx]), axis=0) def ismember(a, b, tol=5): rows_close = np.all(np.round(a - b[:, None], tol) == 0, axis=-1) return np.any(rows_close) test_edges_false = [] while len(test_edges_false) < len(test_edges): idx_i = np.random.randint(0, adj.shape[0]) idx_j = np.random.randint(0, adj.shape[0]) if idx_i == idx_j: continue if ismember([idx_i, idx_j], edges_all): continue if test_edges_false: if ismember([idx_j, idx_i], np.array(test_edges_false)): continue if ismember([idx_i, idx_j], np.array(test_edges_false)): continue test_edges_false.append([idx_i, idx_j]) val_edges_false = [] while len(val_edges_false) < len(val_edges): idx_i = np.random.randint(0, adj.shape[0]) idx_j = np.random.randint(0, adj.shape[0]) if idx_i == idx_j: continue if ismember([idx_i, idx_j], train_edges): continue if ismember([idx_j, idx_i], train_edges): continue if ismember([idx_i, idx_j], val_edges): continue if ismember([idx_j, idx_i], val_edges): continue if val_edges_false: if ismember([idx_j, idx_i], np.array(val_edges_false)): continue if ismember([idx_i, idx_j], np.array(val_edges_false)): continue val_edges_false.append([idx_i, idx_j]) assert ~ismember(test_edges_false, edges_all) assert ~ismember(val_edges_false, edges_all) assert ~ismember(val_edges, train_edges) assert ~ismember(test_edges, train_edges) assert ~ismember(val_edges, test_edges) data = np.ones(train_edges.shape[0]) # Re-build adj matrix adj_train = sp.csr_matrix((data, (train_edges[:, 0], train_edges[:, 1])), shape=adj.shape) adj_train = adj_train + adj_train.T # NOTE: these edge lists only contain single direction of edge! return adj_train, train_edges, val_edges, val_edges_false, test_edges, test_edges_false if __name__ == '__main__': colors = [(0.7509279299037631, 0.021203049355839054, 0.24561203044115132)] graphs = [] graph = nx.grid_2d_graph(2, 3) graphs.append(graph) graphs.append(nx.grid_2d_graph(2,4)) adj = nx.to_numpy_matrix(graph) # print("*** before") # print(adj) # graph_show(nx.from_numpy_matrix(adj), "1", colors) # adj = move_random_node_to_the_last_index(adj) # print("*** after") # print(adj) # graph_show(nx.from_numpy_matrix(adj), "2", colors) prepare_kronEM_data(graphs, "salam", True) print(adj) # print("*** 1) ") # print(adj) # # adj_copy = adj.copy() # random_idx_for_delete = np.random.randint(adj.shape[0]) # print("*** index") # print(random_idx_for_delete) # deleted_node = adj[:, random_idx_for_delete].copy() # for i in range(deleted_node.__len__()): # if i >= random_idx_for_delete and i < deleted_node.__len__() - 1: # deleted_node[i] = deleted_node[i + 1] # elif i == deleted_node.__len__() - 1: # deleted_node[i] = 0 # adj[:, random_idx_for_delete:adj.shape[0] - 1] = adj[:, random_idx_for_delete + 1:adj.shape[0]] # adj[random_idx_for_delete:adj.shape[0] - 1, :] = adj[random_idx_for_delete + 1:adj.shape[0], :] # adj = np.delete(adj, -1, axis=1) # adj = np.delete(adj, -1, axis=0) # print("************") # print(adj) # print(deleted_node) # adj = np.concatenate((adj, deleted_node[:deleted_node.shape[0]-1]), axis=1) # adj = np.concatenate((adj, np.transpose(deleted_node)), axis=0) # print("*** 2) ") # print(adj) # graph_show(nx.from_numpy_matrix(adj), "2", colors) # max_prev_node = 12 # len = adj_copy.shape[0] # x = np.zeros((graph.number_of_nodes() - 1, max_prev_node)) # here zeros are padded for small graph # x[0, :] = 1 # the first input token is all ones # y = np.zeros((graph.number_of_nodes() - 1, max_prev_node)) # here zeros are padded for small graph # # column_vector = adj_copy[:, adj_copy.shape[0] - 1] # incomplete_adj = adj_copy.copy() # incomplete_adj = incomplete_adj[:, :incomplete_adj.shape[0] - 1] # incomplete_adj = incomplete_adj[:incomplete_adj.shape[0] - 1, :] # x_idx = np.random.permutation(incomplete_adj.shape[0]) # # x_idx_prime = np.concatenate((x_idx, [adj.shape[0] - 1]), axis=0) # column_vector = column_vector[np.ix_(x_idx_prime)] # # incomplete_adj = incomplete_adj[np.ix_(x_idx, x_idx)] # # # incomplete_matrix = np.asmatrix(incomplete_adj) # G = nx.from_numpy_matrix(incomplete_matrix) # # then do bfs in the permuted G # start_idx = np.random.randint(incomplete_adj.shape[0]) # x_idx = np.array(bfs_seq(G, start_idx)) # incomplete_adj = incomplete_adj[np.ix_(x_idx, x_idx)] # adj_encoded = encode_adj(incomplete_adj.copy(), max_prev_node=12) # # # x_idx_prime = np.concatenate((x_idx, [adj.shape[0] - 1]), axis=0) # column_vector = column_vector[np.ix_(x_idx_prime)] # adj = nx.to_numpy_matrix(graph) # adj_copy = adj.copy() # column_vector = adj_copy[:, adj_copy.shape[0] - 1] # incomplete_adj = adj_copy.copy() # incomplete_adj = incomplete_adj[:, :incomplete_adj.shape[0] - 1] # incomplete_adj = incomplete_adj[:incomplete_adj.shape[0] - 1, :] # x_idx = np.random.permutation(incomplete_adj.shape[0]) # # x_idx_prime = np.concatenate((x_idx, [adj.shape[0] - 1]), axis=0) # column_vector = column_vector[np.ix_(x_idx_prime)] # # incomplete_adj = incomplete_adj[np.ix_(x_idx, x_idx)] # # # incomplete_matrix = np.asmatrix(incomplete_adj) # G = nx.from_numpy_matrix(incomplete_matrix) # # then do bfs in the permuted G # start_idx = np.random.randint(incomplete_adj.shape[0]) # x_idx = np.array(bfs_seq(G, start_idx)) # incomplete_adj = incomplete_adj[np.ix_(x_idx, x_idx)] # # # x_idx_prime = np.concatenate((x_idx, [adj.shape[0] - 1]), axis=0) # column_vector = column_vector[np.ix_(x_idx_prime)] # row_vector = np.transpose(column_vector) # complete_adj = incomplete_adj.copy() # complete_adj = np.concatenate((complete_adj, column_vector[:adj_copy.shape[0] - 1]), axis=1) # complete_adj = np.concatenate((complete_adj, row_vector), axis=0) # adj_encoded = encode_adj(complete_adj.copy(), 12) # decoded = decode_adj(adj_encoded[:adj_encoded.shape[0]-1, :]) # graph_show(nx.from_numpy_matrix(incomplete_adj), "incomplete", colors) # decoded = decode_adj(adj_encoded) # complete = np.concatenate((decoded, column_vector[:column_vector.shape[0]-1]), axis=1) # complete = np.concatenate((complete, np.transpose(column_vector)), axis=0) # graph_show(nx.from_numpy_matrix(complete), "complete", colors) # # graph_show(nx.from_numpy_matrix(adj_copy), "1", colors) # x = np.zeros((graph.number_of_nodes(), args.max_prev_node)) # here zeros are padded for small graph # x[0, :] = 1 # the first input token is all ones # y = np.zeros((graph.number_of_nodes(), args.max_prev_node)) # here zeros are padded for small graph # generate input x, y pairs # graphs.append(graph) # print(nx.to_numpy_matrix(graph)) # graph = nx.grid_2d_graph(2, 2) # print("********************************") # print(nx.to_numpy_matrix(graph)) # graphs.append(graph) # util.prepare_kronEM_data(graphs, "salam") # nx.write_adjlist(graph, "sala.txt") # # colors = [(0.7509279299037631, 0.021203049355839054, 0.24561203044115132)] # # graph_show(graph, "1", colors) # file = open("copy.txt", "w") # adj = nx.to_numpy_matrix(graph) # print(adj) # with file as f: # for line in adj: # np.savetxt(f, line, fmt='%.2f') # print("**** 1)") # print(adj) # random_index = np.random.randint(adj.shape[0]) # random_index = 2 # print("*** random_index") # print(random_index) # adj[:, random_index] = 0 # adj[random_index, :] = 0 # print("**** 2)") # print(adj) # graph_show(nx.from_numpy_matrix(adj), "2", colors) # adj[:, random_index] = 1 # adj[random_index, :] = 1 # print("**** 3)") # print(adj) # graph_show(nx.from_numpy_matrix(adj), "3", colors) # print(adj) # print(adj[:-1, :-1]) # print(adj[:,-1]) # label_padded = np.zeros(10) # label_padded[:4] = adj[-1,:] # print(label_padded) # graph = nx.barabasi_albert_graph(100,4) # matrix = nx.to_numpy_matrix(graph) # G = nx.karate_club_graph() # print("Node Degree") # for v in G: # print('%s %s' % (v, G.degree(v))) # colors = [(0.7509279299037631, 0.021203049355839054, 0.24561203044115132)] # graph_show(G, "Karate", colors) # adj_label = adj + sp.eye(adj.shape[0]) # print(adj) # print(adj_label) # def preprocess_graph(adj): # adj = sp.coo_matrix(adj) # adj_ = adj + sp.eye(adj.shape[0]) # rowsum = np.array(adj_.sum(1)) # degree_mat_inv_sqrt = sp.diags(np.power(rowsum, -0.5).flatten()) # adj_normalized = adj_.dot(degree_mat_inv_sqrt).transpose().dot(degree_mat_inv_sqrt).tocoo() # # return sparse_to_tuple(adj_normalized) # return sparse_mx_to_torch_sparse_tensor(adj_normalized)