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

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

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