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

test_preimage_iam.py 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Sep 5 15:59:00 2019
  5. @author: ljia
  6. """
  7. import numpy as np
  8. import networkx as nx
  9. import matplotlib.pyplot as plt
  10. import time
  11. import random
  12. #from tqdm import tqdm
  13. #import os
  14. import sys
  15. sys.path.insert(0, "../")
  16. from pygraph.utils.graphfiles import loadDataset
  17. from utils import remove_edges, compute_kernel, get_same_item_indices
  18. from ged import ged_median
  19. from preimage_iam import preimage_iam
  20. ###############################################################################
  21. # tests on different values on grid of median-sets and k.
  22. def test_preimage_iam_grid_k_median_nb():
  23. ds = {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt',
  24. 'extra_params': {}} # node/edge symb
  25. Gn, y_all = loadDataset(ds['dataset'], extra_params=ds['extra_params'])
  26. # Gn = Gn[0:50]
  27. remove_edges(Gn)
  28. gkernel = 'marginalizedkernel'
  29. lmbda = 0.03 # termination probalility
  30. r_max = 5 # iteration limit for pre-image.
  31. # alpha_range = np.linspace(0.5, 0.5, 1)
  32. # k = 5 # k nearest neighbors
  33. epsilon = 1e-6
  34. InitIAMWithAllDk = True
  35. # parameters for GED function
  36. ged_cost='CHEM_1'
  37. ged_method='IPFP'
  38. saveGXL='gedlib'
  39. # parameters for IAM function
  40. c_ei=1
  41. c_er=1
  42. c_es=1
  43. ite_max_iam = 50
  44. epsilon_iam = 0.001
  45. removeNodes = True
  46. connected_iam = False
  47. # number of graphs; we what to compute the median of these graphs.
  48. nb_median_range = [2, 3, 4, 5, 10, 20, 30, 40, 50, 100]
  49. # number of nearest neighbors.
  50. k_range = [5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 100]
  51. # find out all the graphs classified to positive group 1.
  52. idx_dict = get_same_item_indices(y_all)
  53. Gn = [Gn[i] for i in idx_dict[1]]
  54. # # compute Gram matrix.
  55. # time0 = time.time()
  56. # km = compute_kernel(Gn, gkernel, True)
  57. # time_km = time.time() - time0
  58. # # write Gram matrix to file.
  59. # np.savez('results/gram_matrix_marg_itr10_pq0.03_mutag_positive.gm', gm=km, gmtime=time_km)
  60. time_list = []
  61. dis_ks_min_list = []
  62. sod_gs_list = []
  63. sod_gs_min_list = []
  64. nb_updated_list = []
  65. nb_updated_k_list = []
  66. g_best = []
  67. for idx_nb, nb_median in enumerate(nb_median_range):
  68. print('\n-------------------------------------------------------')
  69. print('number of median graphs =', nb_median)
  70. random.seed(1)
  71. idx_rdm = random.sample(range(len(Gn)), nb_median)
  72. print('graphs chosen:', idx_rdm)
  73. Gn_median = [Gn[idx].copy() for idx in idx_rdm]
  74. # for g in Gn_median:
  75. # nx.draw(g, labels=nx.get_node_attributes(g, 'atom'), with_labels=True)
  76. ## plt.savefig("results/preimage_mix/mutag.png", format="PNG")
  77. # plt.show()
  78. # plt.clf()
  79. ###################################################################
  80. gmfile = np.load('results/gram_matrix_marg_itr10_pq0.03_mutag_positive.gm.npz')
  81. km_tmp = gmfile['gm']
  82. time_km = gmfile['gmtime']
  83. # modify mixed gram matrix.
  84. km = np.zeros((len(Gn) + nb_median, len(Gn) + nb_median))
  85. for i in range(len(Gn)):
  86. for j in range(i, len(Gn)):
  87. km[i, j] = km_tmp[i, j]
  88. km[j, i] = km[i, j]
  89. for i in range(len(Gn)):
  90. for j, idx in enumerate(idx_rdm):
  91. km[i, len(Gn) + j] = km[i, idx]
  92. km[len(Gn) + j, i] = km[i, idx]
  93. for i, idx1 in enumerate(idx_rdm):
  94. for j, idx2 in enumerate(idx_rdm):
  95. km[len(Gn) + i, len(Gn) + j] = km[idx1, idx2]
  96. ###################################################################
  97. alpha_range = [1 / nb_median] * nb_median
  98. time_list.append([])
  99. dis_ks_min_list.append([])
  100. sod_gs_list.append([])
  101. sod_gs_min_list.append([])
  102. nb_updated_list.append([])
  103. nb_updated_k_list.append([])
  104. g_best.append([])
  105. for k in k_range:
  106. print('\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n')
  107. print('k =', k)
  108. time0 = time.time()
  109. dhat, ghat_list, dis_of_each_itr, nb_updated, nb_updated_k = \
  110. preimage_iam(Gn, Gn_median,
  111. alpha_range, range(len(Gn), len(Gn) + nb_median), km, k, r_max,
  112. gkernel, epsilon=epsilon, InitIAMWithAllDk=InitIAMWithAllDk,
  113. params_iam={'c_ei': c_ei, 'c_er': c_er, 'c_es': c_es,
  114. 'ite_max': ite_max_iam, 'epsilon': epsilon_iam,
  115. 'removeNodes': removeNodes, 'connected': connected_iam},
  116. params_ged={'ged_cost': ged_cost, 'ged_method': ged_method,
  117. 'saveGXL': saveGXL})
  118. time_total = time.time() - time0 + time_km
  119. print('time: ', time_total)
  120. time_list[idx_nb].append(time_total)
  121. print('\nsmallest distance in kernel space: ', dhat)
  122. dis_ks_min_list[idx_nb].append(dhat)
  123. g_best[idx_nb].append(ghat_list)
  124. print('\nnumber of updates of the best graph by IAM: ', nb_updated)
  125. nb_updated_list[idx_nb].append(nb_updated)
  126. print('\nnumber of updates of k nearest graphs by IAM: ', nb_updated_k)
  127. nb_updated_k_list[idx_nb].append(nb_updated_k)
  128. # show the best graph and save it to file.
  129. print('the shortest distance is', dhat)
  130. print('one of the possible corresponding pre-images is')
  131. nx.draw(ghat_list[0], labels=nx.get_node_attributes(ghat_list[0], 'atom'),
  132. with_labels=True)
  133. plt.savefig('results/preimage_iam/mutag_median_nb' + str(nb_median) +
  134. '_k' + str(k) + '.png', format="PNG")
  135. # plt.show()
  136. plt.clf()
  137. # print(ghat_list[0].nodes(data=True))
  138. # print(ghat_list[0].edges(data=True))
  139. # compute the corresponding sod in graph space.
  140. sod_tmp, _ = ged_median([ghat_list[0]], Gn_median, ged_cost=ged_cost,
  141. ged_method=ged_method, saveGXL=saveGXL)
  142. sod_gs_list[idx_nb].append(sod_tmp)
  143. sod_gs_min_list[idx_nb].append(np.min(sod_tmp))
  144. print('\nsmallest sod in graph space: ', np.min(sod_tmp))
  145. print('\nsods in graph space: ', sod_gs_list)
  146. print('\nsmallest sod in graph space for each set of median graphs and k: ',
  147. sod_gs_min_list)
  148. print('\nsmallest distance in kernel space for each set of median graphs and k: ',
  149. dis_ks_min_list)
  150. print('\nnumber of updates of the best graph for each set of median graphs and k by IAM: ',
  151. nb_updated_list)
  152. print('\nnumber of updates of k nearest graphs for each set of median graphs and k by IAM: ',
  153. nb_updated_k_list)
  154. print('\ntimes:', time_list)
  155. ###############################################################################
  156. # tests on different numbers of median-sets.
  157. def test_preimage_iam_median_nb():
  158. ds = {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt',
  159. 'extra_params': {}} # node/edge symb
  160. Gn, y_all = loadDataset(ds['dataset'], extra_params=ds['extra_params'])
  161. # Gn = Gn[0:50]
  162. remove_edges(Gn)
  163. gkernel = 'marginalizedkernel'
  164. lmbda = 0.03 # termination probalility
  165. r_max = 10 # iteration limit for pre-image.
  166. # alpha_range = np.linspace(0.5, 0.5, 1)
  167. k = 5 # k nearest neighbors
  168. epsilon = 1e-6
  169. InitIAMWithAllDk = True
  170. # parameters for GED function
  171. ged_cost='CHEM_1'
  172. ged_method='IPFP'
  173. saveGXL='gedlib'
  174. # parameters for IAM function
  175. c_ei=1
  176. c_er=1
  177. c_es=1
  178. ite_max_iam = 50
  179. epsilon_iam = 0.001
  180. removeNodes = True
  181. connected_iam = False
  182. # number of graphs; we what to compute the median of these graphs.
  183. nb_median_range = [2, 3, 4, 5, 10, 20, 30, 40, 50, 100]
  184. # find out all the graphs classified to positive group 1.
  185. idx_dict = get_same_item_indices(y_all)
  186. Gn = [Gn[i] for i in idx_dict[1]]
  187. # # compute Gram matrix.
  188. # time0 = time.time()
  189. # km = compute_kernel(Gn, gkernel, True)
  190. # time_km = time.time() - time0
  191. # # write Gram matrix to file.
  192. # np.savez('results/gram_matrix_marg_itr10_pq0.03_mutag_positive.gm', gm=km, gmtime=time_km)
  193. time_list = []
  194. dis_ks_min_list = []
  195. sod_gs_list = []
  196. sod_gs_min_list = []
  197. nb_updated_list = []
  198. nb_updated_k_list = []
  199. g_best = []
  200. for nb_median in nb_median_range:
  201. print('\n-------------------------------------------------------')
  202. print('number of median graphs =', nb_median)
  203. random.seed(1)
  204. idx_rdm = random.sample(range(len(Gn)), nb_median)
  205. print('graphs chosen:', idx_rdm)
  206. Gn_median = [Gn[idx].copy() for idx in idx_rdm]
  207. # for g in Gn_median:
  208. # nx.draw(g, labels=nx.get_node_attributes(g, 'atom'), with_labels=True)
  209. ## plt.savefig("results/preimage_mix/mutag.png", format="PNG")
  210. # plt.show()
  211. # plt.clf()
  212. ###################################################################
  213. gmfile = np.load('results/gram_matrix_marg_itr10_pq0.03_mutag_positive.gm.npz')
  214. km_tmp = gmfile['gm']
  215. time_km = gmfile['gmtime']
  216. # modify mixed gram matrix.
  217. km = np.zeros((len(Gn) + nb_median, len(Gn) + nb_median))
  218. for i in range(len(Gn)):
  219. for j in range(i, len(Gn)):
  220. km[i, j] = km_tmp[i, j]
  221. km[j, i] = km[i, j]
  222. for i in range(len(Gn)):
  223. for j, idx in enumerate(idx_rdm):
  224. km[i, len(Gn) + j] = km[i, idx]
  225. km[len(Gn) + j, i] = km[i, idx]
  226. for i, idx1 in enumerate(idx_rdm):
  227. for j, idx2 in enumerate(idx_rdm):
  228. km[len(Gn) + i, len(Gn) + j] = km[idx1, idx2]
  229. ###################################################################
  230. alpha_range = [1 / nb_median] * nb_median
  231. time0 = time.time()
  232. dhat, ghat_list, dis_of_each_itr, nb_updated, nb_updated_k = \
  233. preimage_iam(Gn, Gn_median,
  234. alpha_range, range(len(Gn), len(Gn) + nb_median), km, k, r_max,
  235. gkernel, epsilon=epsilon, InitIAMWithAllDk=InitIAMWithAllDk,
  236. params_iam={'c_ei': c_ei, 'c_er': c_er, 'c_es': c_es,
  237. 'ite_max': ite_max_iam, 'epsilon': epsilon_iam,
  238. 'removeNodes': removeNodes, 'connected': connected_iam},
  239. params_ged={'ged_cost': ged_cost, 'ged_method': ged_method,
  240. 'saveGXL': saveGXL})
  241. time_total = time.time() - time0 + time_km
  242. print('\ntime: ', time_total)
  243. time_list.append(time_total)
  244. print('\nsmallest distance in kernel space: ', dhat)
  245. dis_ks_min_list.append(dhat)
  246. g_best.append(ghat_list)
  247. print('\nnumber of updates of the best graph: ', nb_updated)
  248. nb_updated_list.append(nb_updated)
  249. print('\nnumber of updates of k nearest graphs: ', nb_updated_k)
  250. nb_updated_k_list.append(nb_updated_k)
  251. # show the best graph and save it to file.
  252. print('the shortest distance is', dhat)
  253. print('one of the possible corresponding pre-images is')
  254. nx.draw(ghat_list[0], labels=nx.get_node_attributes(ghat_list[0], 'atom'),
  255. with_labels=True)
  256. # plt.show()
  257. plt.savefig('results/preimage_iam/mutag_median_nb' + str(nb_median) +
  258. '.png', format="PNG")
  259. plt.clf()
  260. # print(ghat_list[0].nodes(data=True))
  261. # print(ghat_list[0].edges(data=True))
  262. # compute the corresponding sod in graph space.
  263. sod_tmp, _ = ged_median([ghat_list[0]], Gn_median, ged_cost=ged_cost,
  264. ged_method=ged_method, saveGXL=saveGXL)
  265. sod_gs_list.append(sod_tmp)
  266. sod_gs_min_list.append(np.min(sod_tmp))
  267. print('\nsmallest sod in graph space: ', np.min(sod_tmp))
  268. print('\nsods in graph space: ', sod_gs_list)
  269. print('\nsmallest sod in graph space for each set of median graphs: ', sod_gs_min_list)
  270. print('\nsmallest distance in kernel space for each set of median graphs: ',
  271. dis_ks_min_list)
  272. print('\nnumber of updates of the best graph for each set of median graphs by IAM: ',
  273. nb_updated_list)
  274. print('\nnumber of updates of k nearest graphs for each set of median graphs by IAM: ',
  275. nb_updated_k_list)
  276. print('\ntimes:', time_list)
  277. ###############################################################################
  278. # test on the combination of the two randomly chosen graphs. (the same as in the
  279. # random pre-image paper.)
  280. def test_gkiam_2combination_all_pairs():
  281. ds = {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt',
  282. 'extra_params': {}} # node/edge symb
  283. Gn, y_all = loadDataset(ds['dataset'], extra_params=ds['extra_params'])
  284. # Gn = Gn[0:50]
  285. remove_edges(Gn)
  286. gkernel = 'marginalizedkernel'
  287. lmbda = 0.03 # termination probalility
  288. r_max = 10 # iteration limit for pre-image.
  289. alpha_range = np.linspace(0.5, 0.5, 1)
  290. k = 5 # k nearest neighbors
  291. epsilon = 1e-6
  292. InitIAMWithAllDk = False
  293. # parameters for GED function
  294. ged_cost='CHEM_1'
  295. ged_method='IPFP'
  296. saveGXL='gedlib'
  297. # parameters for IAM function
  298. c_ei=1
  299. c_er=1
  300. c_es=1
  301. ite_max_iam = 50
  302. epsilon_iam = 0.001
  303. removeNodes = True
  304. connected_iam = False
  305. nb_update_mat = np.full((len(Gn), len(Gn)), np.inf)
  306. # test on each pair of graphs.
  307. # for idx1 in range(len(Gn) - 1, -1, -1):
  308. # for idx2 in range(idx1, -1, -1):
  309. for idx1 in range(187, 188):
  310. for idx2 in range(167, 168):
  311. g1 = Gn[idx1].copy()
  312. g2 = Gn[idx2].copy()
  313. # Gn[10] = []
  314. # Gn[10] = []
  315. nx.draw(g1, labels=nx.get_node_attributes(g1, 'atom'), with_labels=True)
  316. plt.savefig("results/gk_iam/all_pairs/mutag187.png", format="PNG")
  317. plt.show()
  318. plt.clf()
  319. nx.draw(g2, labels=nx.get_node_attributes(g2, 'atom'), with_labels=True)
  320. plt.savefig("results/gk_iam/all_pairs/mutag167.png", format="PNG")
  321. plt.show()
  322. plt.clf()
  323. ###################################################################
  324. # Gn_mix = [g.copy() for g in Gn]
  325. # Gn_mix.append(g1.copy())
  326. # Gn_mix.append(g2.copy())
  327. #
  328. # # compute
  329. # time0 = time.time()
  330. # km = compute_kernel(Gn_mix, gkernel, True)
  331. # time_km = time.time() - time0
  332. #
  333. # # write Gram matrix to file and read it.
  334. # np.savez('results/gram_matrix_uhpath_itr7_pq0.8.gm', gm=km, gmtime=time_km)
  335. ###################################################################
  336. gmfile = np.load('results/gram_matrix_marg_itr10_pq0.03.gm.npz')
  337. km = gmfile['gm']
  338. time_km = gmfile['gmtime']
  339. # modify mixed gram matrix.
  340. for i in range(len(Gn)):
  341. km[i, len(Gn)] = km[i, idx1]
  342. km[i, len(Gn) + 1] = km[i, idx2]
  343. km[len(Gn), i] = km[i, idx1]
  344. km[len(Gn) + 1, i] = km[i, idx2]
  345. km[len(Gn), len(Gn)] = km[idx1, idx1]
  346. km[len(Gn), len(Gn) + 1] = km[idx1, idx2]
  347. km[len(Gn) + 1, len(Gn)] = km[idx2, idx1]
  348. km[len(Gn) + 1, len(Gn) + 1] = km[idx2, idx2]
  349. ###################################################################
  350. # # use only the two graphs in median set as candidates.
  351. # Gn = [g1.copy(), g2.copy()]
  352. # Gn_mix = Gn + [g1.copy(), g2.copy()]
  353. # # compute
  354. # time0 = time.time()
  355. # km = compute_kernel(Gn_mix, gkernel, True)
  356. # time_km = time.time() - time0
  357. time_list = []
  358. dis_ks_min_list = []
  359. sod_gs_list = []
  360. sod_gs_min_list = []
  361. nb_updated_list = []
  362. nb_updated_k_list = []
  363. g_best = []
  364. # for each alpha
  365. for alpha in alpha_range:
  366. print('\n-------------------------------------------------------\n')
  367. print('alpha =', alpha)
  368. time0 = time.time()
  369. dhat, ghat_list, sod_ks, nb_updated, nb_updated_k = \
  370. preimage_iam(Gn, [g1, g2],
  371. [alpha, 1 - alpha], range(len(Gn), len(Gn) + 2), km, k, r_max,
  372. gkernel, epsilon=epsilon, InitIAMWithAllDk=InitIAMWithAllDk,
  373. params_iam={'c_ei': c_ei, 'c_er': c_er, 'c_es': c_es,
  374. 'ite_max': ite_max_iam, 'epsilon': epsilon_iam,
  375. 'removeNodes': removeNodes, 'connected': connected_iam},
  376. params_ged={'ged_cost': ged_cost, 'ged_method': ged_method,
  377. 'saveGXL': saveGXL})
  378. time_total = time.time() - time0 + time_km
  379. print('time: ', time_total)
  380. time_list.append(time_total)
  381. dis_ks_min_list.append(dhat)
  382. g_best.append(ghat_list)
  383. nb_updated_list.append(nb_updated)
  384. nb_updated_k_list.append(nb_updated_k)
  385. # show best graphs and save them to file.
  386. for idx, item in enumerate(alpha_range):
  387. print('when alpha is', item, 'the shortest distance is', dis_ks_min_list[idx])
  388. print('one of the possible corresponding pre-images is')
  389. nx.draw(g_best[idx][0], labels=nx.get_node_attributes(g_best[idx][0], 'atom'),
  390. with_labels=True)
  391. plt.savefig('results/gk_iam/mutag' + str(idx1) + '_' + str(idx2)
  392. + '_alpha' + str(item) + '.png', format="PNG")
  393. # plt.show()
  394. plt.clf()
  395. # print(g_best[idx][0].nodes(data=True))
  396. # print(g_best[idx][0].edges(data=True))
  397. # for g in g_best[idx]:
  398. # draw_Letter_graph(g, savepath='results/gk_iam/')
  399. ## nx.draw_networkx(g)
  400. ## plt.show()
  401. # print(g.nodes(data=True))
  402. # print(g.edges(data=True))
  403. # compute the corresponding sod in graph space.
  404. for idx, item in enumerate(alpha_range):
  405. sod_tmp, _ = ged_median([g_best[0]], [g1, g2], ged_cost=ged_cost,
  406. ged_method=ged_method, saveGXL=saveGXL)
  407. sod_gs_list.append(sod_tmp)
  408. sod_gs_min_list.append(np.min(sod_tmp))
  409. print('\nsods in graph space: ', sod_gs_list)
  410. print('\nsmallest sod in graph space for each alpha: ', sod_gs_min_list)
  411. print('\nsmallest distance in kernel space for each alpha: ', dis_ks_min_list)
  412. print('\nnumber of updates of the best graph for each alpha: ',
  413. nb_updated_list)
  414. print('\nnumber of updates of the k nearest graphs for each alpha: ',
  415. nb_updated_k_list)
  416. print('\ntimes:', time_list)
  417. nb_update_mat[idx1, idx2] = nb_updated_list[0]
  418. str_fw = 'graphs %d and %d: %d.\n' % (idx1, idx2, nb_updated_list[0])
  419. with open('results/gk_iam/all_pairs/nb_updates.txt', 'r+') as file:
  420. content = file.read()
  421. file.seek(0, 0)
  422. file.write(str_fw + content)
  423. def test_gkiam_2combination():
  424. from gk_iam import gk_iam_nearest_multi
  425. ds = {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt',
  426. 'extra_params': {}} # node/edge symb
  427. Gn, y_all = loadDataset(ds['dataset'], extra_params=ds['extra_params'])
  428. # Gn = Gn[0:50]
  429. remove_edges(Gn)
  430. gkernel = 'marginalizedkernel'
  431. lmbda = 0.03 # termination probalility
  432. r_max = 10 # iteration limit for pre-image.
  433. alpha_range = np.linspace(0.5, 0.5, 1)
  434. k = 20 # k nearest neighbors
  435. epsilon = 1e-6
  436. ged_cost='CHEM_1'
  437. ged_method='IPFP'
  438. saveGXL='gedlib'
  439. c_ei=1
  440. c_er=1
  441. c_es=1
  442. # randomly select two molecules
  443. np.random.seed(1)
  444. idx_gi = [10, 11] # np.random.randint(0, len(Gn), 2)
  445. g1 = Gn[idx_gi[0]].copy()
  446. g2 = Gn[idx_gi[1]].copy()
  447. # Gn[10] = []
  448. # Gn[10] = []
  449. # nx.draw(g1, labels=nx.get_node_attributes(g1, 'atom'), with_labels=True)
  450. # plt.savefig("results/random_preimage/mutag10.png", format="PNG")
  451. # plt.show()
  452. # nx.draw(g2, labels=nx.get_node_attributes(g2, 'atom'), with_labels=True)
  453. # plt.savefig("results/random_preimage/mutag11.png", format="PNG")
  454. # plt.show()
  455. Gn_mix = [g.copy() for g in Gn]
  456. Gn_mix.append(g1.copy())
  457. Gn_mix.append(g2.copy())
  458. # compute
  459. # time0 = time.time()
  460. # km = compute_kernel(Gn_mix, gkernel, True)
  461. # time_km = time.time() - time0
  462. # write Gram matrix to file and read it.
  463. # np.savez('results/gram_matrix.gm', gm=km, gmtime=time_km)
  464. gmfile = np.load('results/gram_matrix.gm.npz')
  465. km = gmfile['gm']
  466. time_km = gmfile['gmtime']
  467. time_list = []
  468. dis_ks_min_list = []
  469. sod_gs_list = []
  470. sod_gs_min_list = []
  471. nb_updated_list = []
  472. g_best = []
  473. # for each alpha
  474. for alpha in alpha_range:
  475. print('\n-------------------------------------------------------\n')
  476. print('alpha =', alpha)
  477. time0 = time.time()
  478. dhat, ghat_list, sod_ks, nb_updated = gk_iam_nearest_multi(Gn, [g1, g2],
  479. [alpha, 1 - alpha], range(len(Gn), len(Gn) + 2), km, k, r_max,
  480. gkernel, c_ei=c_ei, c_er=c_er, c_es=c_es, epsilon=epsilon,
  481. ged_cost=ged_cost, ged_method=ged_method, saveGXL=saveGXL)
  482. time_total = time.time() - time0 + time_km
  483. print('time: ', time_total)
  484. time_list.append(time_total)
  485. dis_ks_min_list.append(dhat)
  486. g_best.append(ghat_list)
  487. nb_updated_list.append(nb_updated)
  488. # show best graphs and save them to file.
  489. for idx, item in enumerate(alpha_range):
  490. print('when alpha is', item, 'the shortest distance is', dis_ks_min_list[idx])
  491. print('one of the possible corresponding pre-images is')
  492. nx.draw(g_best[idx][0], labels=nx.get_node_attributes(g_best[idx][0], 'atom'),
  493. with_labels=True)
  494. plt.savefig('results/gk_iam/mutag_alpha' + str(item) + '.png', format="PNG")
  495. plt.show()
  496. print(g_best[idx][0].nodes(data=True))
  497. print(g_best[idx][0].edges(data=True))
  498. # for g in g_best[idx]:
  499. # draw_Letter_graph(g, savepath='results/gk_iam/')
  500. ## nx.draw_networkx(g)
  501. ## plt.show()
  502. # print(g.nodes(data=True))
  503. # print(g.edges(data=True))
  504. # compute the corresponding sod in graph space.
  505. for idx, item in enumerate(alpha_range):
  506. sod_tmp, _ = ged_median([g_best[0]], [g1, g2], ged_cost=ged_cost,
  507. ged_method=ged_method, saveGXL=saveGXL)
  508. sod_gs_list.append(sod_tmp)
  509. sod_gs_min_list.append(np.min(sod_tmp))
  510. print('\nsods in graph space: ', sod_gs_list)
  511. print('\nsmallest sod in graph space for each alpha: ', sod_gs_min_list)
  512. print('\nsmallest distance in kernel space for each alpha: ', dis_ks_min_list)
  513. print('\nnumber of updates for each alpha: ', nb_updated_list)
  514. print('\ntimes:', time_list)
  515. ###############################################################################
  516. if __name__ == '__main__':
  517. ###############################################################################
  518. # test on the combination of the two randomly chosen graphs. (the same as in the
  519. # random pre-image paper.)
  520. # test_gkiam_2combination()
  521. # test_gkiam_2combination_all_pairs()
  522. ###############################################################################
  523. # tests on different numbers of median-sets.
  524. test_preimage_iam_median_nb()
  525. ###############################################################################
  526. # tests on different values on grid of median-sets and k.
  527. # test_preimage_iam_grid_k_median_nb()

A Python package for graph kernels, graph edit distances and graph pre-image problem.