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

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

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