You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

faez_test.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Gmatch4py use networkx graph
  2. import networkx as nx
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # import the GED using the munkres algorithm
  6. import gmatch4py as gm
  7. # g1=nx.complete_bipartite_graph(2,3)
  8. # g2=nx.complete_bipartite_graph(2,4)
  9. # g3=nx.complete_bipartite_graph(2,5)
  10. # ged=gm.GraphEditDistance(1,1,1,1) # all edit costs are equal to 1
  11. # result=ged.compare([g1,g2, g3],None)
  12. # print(result[0][1])
  13. # ged=gm.GreedyEditDistance(1,1,1,1)
  14. # result=ged.compare([g1,g2],None)
  15. # print(result)
  16. # ged=gm.HED(1,1,1,1)
  17. # result=ged.compare([g1,g2],None)
  18. # print(result)
  19. # ged=gm.BP_2(1,1,1,1)
  20. # result=ged.compare([g1,g2],None)
  21. # print(result)
  22. from temp.args import GraphVAE_Args
  23. def graph_show(G, title):
  24. pos = nx.spring_layout(G, scale=2)
  25. nx.draw(G, pos, with_labels=True)
  26. fig = plt.gcf()
  27. fig.canvas.set_window_title(title)
  28. plt.show()
  29. plt.savefig('foo.png')
  30. def bfs_seq(G, start_id):
  31. '''
  32. get a bfs node sequence
  33. :param G:
  34. :param start_id:
  35. :return:
  36. '''
  37. dictionary = dict(nx.bfs_successors(G, start_id))
  38. start = [start_id]
  39. output = [start_id]
  40. while len(start) > 0:
  41. next = []
  42. while len(start) > 0:
  43. current = start.pop(0)
  44. neighbor = dictionary.get(current)
  45. if neighbor is not None:
  46. #### a wrong example, should not permute here!
  47. # shuffle(neighbor)
  48. next = next + neighbor
  49. output = output + next
  50. start = next
  51. return output
  52. my_graph = nx.Graph()
  53. # Add edges to to the graph object
  54. # Each tuple represents an edge between two nodes
  55. my_graph.add_edges_from([
  56. (0, 1),
  57. (0, 2),
  58. (0, 7),
  59. (1, 2),
  60. (6, 3),
  61. (4, 1),
  62. (7, 2),
  63. (5, 3)
  64. ])
  65. # graphs = list(nx.connected_component_subgraphs(my_graph))
  66. # grid = nx.grid_2d_graph(2, 3)
  67. # adj = nx.to_numpy_matrix(grid)
  68. # args = GraphVAE_Args()
  69. # if args.permutation_mode:
  70. # x_idx = np.random.permutation(adj.shape[0])
  71. # adj = adj[np.ix_(x_idx, x_idx)]
  72. # adj = np.asmatrix(adj)
  73. # random_idx_for_delete = np.random.randint(adj.shape[0])
  74. # deleted_node = adj[:, random_idx_for_delete].copy()
  75. # for i in range(deleted_node.__len__()):
  76. # if i >= random_idx_for_delete and i < deleted_node.__len__() - 1:
  77. # deleted_node[i] = deleted_node[i + 1]
  78. # elif i == deleted_node.__len__() - 1:
  79. # deleted_node[i] = 0
  80. # adj[:, random_idx_for_delete:adj.shape[0] - 1] = adj[:, random_idx_for_delete + 1:adj.shape[0]]
  81. # adj[random_idx_for_delete:adj.shape[0] - 1, :] = adj[random_idx_for_delete + 1:adj.shape[0], :]
  82. # adj = np.delete(adj, -1, axis=1)
  83. # adj = np.delete(adj, -1, axis=0)
  84. # G = nx.from_numpy_matrix(adj)
  85. # # then do bfs in the permuted G
  86. # print(adj)
  87. # degree_arr = np.sum(adj, axis=0)
  88. # print(degree_arr)
  89. # print(np.argmax(degree_arr))
  90. # start_idx = np.random.randint(adj.shape[0])
  91. # x_idx = np.array(bfs_seq(G, start_idx))
  92. # adj = adj[np.ix_(x_idx, x_idx)]
  93. # x_idx = np.insert(x_idx, x_idx.size, x_idx.size)
  94. # deleted_node = deleted_node[np.ix_(x_idx)]
  95. # graph_show(nx.from_numpy_matrix(adj), "2")
  96. # adj = np.append(adj, deleted_node[:-1], axis=1)
  97. # deleted_node = deleted_node.reshape(1, -1)
  98. # adj = np.vstack([adj, deleted_node])
  99. # graph_show(nx.from_numpy_matrix(adj), "3")
  100. bfs_flag = True
  101. most_connected_node_flag = True
  102. bfs_mode_with_arbitrary_node_deleted = True
  103. distinct_matrix_list = []
  104. for i in range(5000):
  105. grid = nx.grid_2d_graph(3, 2)
  106. adj = nx.to_numpy_matrix(grid)
  107. x_idx = np.random.permutation(adj.shape[0])
  108. adj = adj[np.ix_(x_idx, x_idx)]
  109. adj = np.asmatrix(adj)
  110. if bfs_flag:
  111. if bfs_mode_with_arbitrary_node_deleted:
  112. random_idx_for_delete = np.random.randint(adj.shape[0])
  113. deleted_node = adj[:, random_idx_for_delete].copy()
  114. for i in range(deleted_node.__len__()):
  115. if i >= random_idx_for_delete and i < deleted_node.__len__() - 1:
  116. deleted_node[i] = deleted_node[i + 1]
  117. elif i == deleted_node.__len__() - 1:
  118. deleted_node[i] = 0
  119. adj[:, random_idx_for_delete:adj.shape[0] - 1] = adj[:, random_idx_for_delete + 1:adj.shape[0]]
  120. adj[random_idx_for_delete:adj.shape[0] - 1, :] = adj[random_idx_for_delete + 1:adj.shape[0], :]
  121. adj = np.delete(adj, -1, axis=1)
  122. adj = np.delete(adj, -1, axis=0)
  123. G = nx.from_numpy_matrix(adj)
  124. # then do bfs in the permuted G
  125. degree_arr = np.sum(adj, axis=0)
  126. start_idx = np.argmax(degree_arr)
  127. # start_idx = np.random.randint(adj.shape[0])
  128. x_idx = np.array(bfs_seq(G, start_idx))
  129. adj = adj[np.ix_(x_idx, x_idx)]
  130. # x_idx = np.insert(x_idx, x_idx.size, x_idx.size)
  131. # deleted_node = deleted_node[np.ix_(x_idx)]
  132. # adj = np.append(adj, deleted_node[:-1], axis=1)
  133. # deleted_node = deleted_node.reshape(1, -1)
  134. # adj = np.vstack([adj, deleted_node])
  135. else:
  136. G = nx.from_numpy_matrix(adj)
  137. start_idx = np.random.randint(adj.shape[0])
  138. if most_connected_node_flag:
  139. degree_arr = np.sum(adj, axis=0)
  140. start_idx = np.argmax(degree_arr)
  141. x_idx = np.array(bfs_seq(G, start_idx))
  142. adj = adj[np.ix_(x_idx, x_idx)]
  143. add_to_list_flag = True
  144. for j in range(len(distinct_matrix_list)):
  145. if np.array_equal(adj, distinct_matrix_list[j]):
  146. add_to_list_flag = False
  147. if add_to_list_flag:
  148. distinct_matrix_list.append(adj)
  149. print(len(distinct_matrix_list))