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

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

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