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_mix.py 24 kB

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

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