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.

gk_iam.py 46 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Apr 30 17:07:43 2019
  5. A graph pre-image method combining iterative pre-image method in reference [1]
  6. and the iterative alternate minimizations (IAM) in reference [2].
  7. @author: ljia
  8. @references:
  9. [1] Gökhan H Bakir, Alexander Zien, and Koji Tsuda. Learning to and graph
  10. pre-images. In Joint Pattern Re ognition Symposium , pages 253-261. Springer, 2004.
  11. [2] Generalized median graph via iterative alternate minimization.
  12. """
  13. import sys
  14. import numpy as np
  15. import multiprocessing
  16. from tqdm import tqdm
  17. import networkx as nx
  18. import matplotlib.pyplot as plt
  19. import random
  20. import matplotlib.pyplot as plt
  21. from iam import iam, test_iam_with_more_graphs_as_init, iam_moreGraphsAsInit_tryAllPossibleBestGraphs
  22. sys.path.insert(0, "../")
  23. from pygraph.kernels.marginalizedKernel import marginalizedkernel
  24. from pygraph.kernels.untilHPathKernel import untilhpathkernel
  25. from pygraph.kernels.spKernel import spkernel
  26. import functools
  27. from pygraph.utils.kernels import deltakernel, gaussiankernel, kernelproduct
  28. from pygraph.kernels.structuralspKernel import structuralspkernel
  29. from median import draw_Letter_graph
  30. def gk_iam(Gn, alpha):
  31. """This function constructs graph pre-image by the iterative pre-image
  32. framework in reference [1], algorithm 1, where the step of generating new
  33. graphs randomly is replaced by the IAM algorithm in reference [2].
  34. notes
  35. -----
  36. Every time a better graph is acquired, the older one is replaced by it.
  37. """
  38. pass
  39. # # compute k nearest neighbors of phi in DN.
  40. # dis_list = [] # distance between g_star and each graph.
  41. # for ig, g in tqdm(enumerate(Gn), desc='computing distances', file=sys.stdout):
  42. # dtemp = k_list[ig] - 2 * (alpha * k_g1_list[ig] + (1 - alpha) *
  43. # k_g2_list[ig]) + (alpha * alpha * k_list[idx1] + alpha *
  44. # (1 - alpha) * k_g2_list[idx1] + (1 - alpha) * alpha *
  45. # k_g1_list[idx2] + (1 - alpha) * (1 - alpha) * k_list[idx2])
  46. # dis_list.append(dtemp)
  47. #
  48. # # sort
  49. # sort_idx = np.argsort(dis_list)
  50. # dis_gs = [dis_list[idis] for idis in sort_idx[0:k]]
  51. # g0hat = Gn[sort_idx[0]] # the nearest neighbor of phi in DN
  52. # if dis_gs[0] == 0: # the exact pre-image.
  53. # print('The exact pre-image is found from the input dataset.')
  54. # return 0, g0hat
  55. # dhat = dis_gs[0] # the nearest distance
  56. # Gk = [Gn[ig] for ig in sort_idx[0:k]] # the k nearest neighbors
  57. # gihat_list = []
  58. #
  59. ## i = 1
  60. # r = 1
  61. # while r < r_max:
  62. # print('r =', r)
  63. ## found = False
  64. # Gs_nearest = Gk + gihat_list
  65. # g_tmp = iam(Gs_nearest)
  66. #
  67. # # compute distance between \psi and the new generated graph.
  68. # knew = marginalizedkernel([g_tmp, g1, g2], node_label='atom', edge_label=None,
  69. # p_quit=lmbda, n_iteration=20, remove_totters=False,
  70. # n_jobs=multiprocessing.cpu_count(), verbose=False)
  71. # dnew = knew[0][0, 0] - 2 * (alpha * knew[0][0, 1] + (1 - alpha) *
  72. # knew[0][0, 2]) + (alpha * alpha * k_list[idx1] + alpha *
  73. # (1 - alpha) * k_g2_list[idx1] + (1 - alpha) * alpha *
  74. # k_g1_list[idx2] + (1 - alpha) * (1 - alpha) * k_list[idx2])
  75. # if dnew <= dhat: # the new distance is smaller
  76. # print('I am smaller!')
  77. # dhat = dnew
  78. # g_new = g_tmp.copy() # found better graph.
  79. # gihat_list = [g_new]
  80. # dis_gs.append(dhat)
  81. # r = 0
  82. # else:
  83. # r += 1
  84. #
  85. # ghat = ([g0hat] if len(gihat_list) == 0 else gihat_list)
  86. #
  87. # return dhat, ghat
  88. def gk_iam_nearest(Gn, alpha, idx_gi, Kmatrix, k, r_max):
  89. """This function constructs graph pre-image by the iterative pre-image
  90. framework in reference [1], algorithm 1, where the step of generating new
  91. graphs randomly is replaced by the IAM algorithm in reference [2].
  92. notes
  93. -----
  94. Every time a better graph is acquired, its distance in kernel space is
  95. compared with the k nearest ones, and the k nearest distances from the k+1
  96. distances will be used as the new ones.
  97. """
  98. # compute k nearest neighbors of phi in DN.
  99. dis_list = [] # distance between g_star and each graph.
  100. for ig, g in tqdm(enumerate(Gn), desc='computing distances', file=sys.stdout):
  101. dtemp = dis_gstar(ig, idx_gi, alpha, Kmatrix)
  102. # dtemp = k_list[ig] - 2 * (alpha * k_g1_list[ig] + (1 - alpha) *
  103. # k_g2_list[ig]) + (alpha * alpha * k_list[0] + alpha *
  104. # (1 - alpha) * k_g2_list[0] + (1 - alpha) * alpha *
  105. # k_g1_list[6] + (1 - alpha) * (1 - alpha) * k_list[6])
  106. dis_list.append(dtemp)
  107. # sort
  108. sort_idx = np.argsort(dis_list)
  109. dis_gs = [dis_list[idis] for idis in sort_idx[0:k]] # the k shortest distances
  110. g0hat = Gn[sort_idx[0]] # the nearest neighbor of phi in DN
  111. if dis_gs[0] == 0: # the exact pre-image.
  112. print('The exact pre-image is found from the input dataset.')
  113. return 0, g0hat
  114. dhat = dis_gs[0] # the nearest distance
  115. ghat = g0hat.copy()
  116. Gk = [Gn[ig].copy() for ig in sort_idx[0:k]] # the k nearest neighbors
  117. for gi in Gk:
  118. nx.draw_networkx(gi)
  119. plt.show()
  120. print(gi.nodes(data=True))
  121. print(gi.edges(data=True))
  122. Gs_nearest = Gk.copy()
  123. # gihat_list = []
  124. # i = 1
  125. r = 1
  126. while r < r_max:
  127. print('r =', r)
  128. # found = False
  129. # Gs_nearest = Gk + gihat_list
  130. # g_tmp = iam(Gs_nearest)
  131. g_tmp = test_iam_with_more_graphs_as_init(Gs_nearest, Gs_nearest, c_ei=1, c_er=1, c_es=1)
  132. nx.draw_networkx(g_tmp)
  133. plt.show()
  134. print(g_tmp.nodes(data=True))
  135. print(g_tmp.edges(data=True))
  136. # compute distance between \psi and the new generated graph.
  137. gi_list = [Gn[i] for i in idx_gi]
  138. knew = compute_kernel([g_tmp] + gi_list, 'untilhpathkernel', False)
  139. dnew = dis_gstar(0, range(1, len(gi_list) + 1), alpha, knew)
  140. # dnew = knew[0, 0] - 2 * (alpha[0] * knew[0, 1] + alpha[1] *
  141. # knew[0, 2]) + (alpha[0] * alpha[0] * k_list[0] + alpha[0] *
  142. # alpha[1] * k_g2_list[0] + alpha[1] * alpha[0] *
  143. # k_g1_list[1] + alpha[1] * alpha[1] * k_list[1])
  144. if dnew <= dhat and g_tmp != ghat: # the new distance is smaller
  145. print('I am smaller!')
  146. print(str(dhat) + '->' + str(dnew))
  147. # nx.draw_networkx(ghat)
  148. # plt.show()
  149. # print('->')
  150. # nx.draw_networkx(g_tmp)
  151. # plt.show()
  152. dhat = dnew
  153. g_new = g_tmp.copy() # found better graph.
  154. ghat = g_tmp.copy()
  155. dis_gs.append(dhat) # add the new nearest distance.
  156. Gs_nearest.append(g_new) # add the corresponding graph.
  157. sort_idx = np.argsort(dis_gs)
  158. dis_gs = [dis_gs[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
  159. Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
  160. r = 0
  161. else:
  162. r += 1
  163. return dhat, ghat
  164. #def gk_iam_nearest_multi(Gn, alpha, idx_gi, Kmatrix, k, r_max):
  165. # """This function constructs graph pre-image by the iterative pre-image
  166. # framework in reference [1], algorithm 1, where the step of generating new
  167. # graphs randomly is replaced by the IAM algorithm in reference [2].
  168. #
  169. # notes
  170. # -----
  171. # Every time a set of n better graphs is acquired, their distances in kernel space are
  172. # compared with the k nearest ones, and the k nearest distances from the k+n
  173. # distances will be used as the new ones.
  174. # """
  175. # Gn_median = [Gn[idx].copy() for idx in idx_gi]
  176. # # compute k nearest neighbors of phi in DN.
  177. # dis_list = [] # distance between g_star and each graph.
  178. # for ig, g in tqdm(enumerate(Gn), desc='computing distances', file=sys.stdout):
  179. # dtemp = dis_gstar(ig, idx_gi, alpha, Kmatrix)
  180. ## dtemp = k_list[ig] - 2 * (alpha * k_g1_list[ig] + (1 - alpha) *
  181. ## k_g2_list[ig]) + (alpha * alpha * k_list[0] + alpha *
  182. ## (1 - alpha) * k_g2_list[0] + (1 - alpha) * alpha *
  183. ## k_g1_list[6] + (1 - alpha) * (1 - alpha) * k_list[6])
  184. # dis_list.append(dtemp)
  185. #
  186. # # sort
  187. # sort_idx = np.argsort(dis_list)
  188. # dis_gs = [dis_list[idis] for idis in sort_idx[0:k]] # the k shortest distances
  189. # nb_best = len(np.argwhere(dis_gs == dis_gs[0]).flatten().tolist())
  190. # g0hat_list = [Gn[idx] for idx in sort_idx[0:nb_best]] # the nearest neighbors of phi in DN
  191. # if dis_gs[0] == 0: # the exact pre-image.
  192. # print('The exact pre-image is found from the input dataset.')
  193. # return 0, g0hat_list
  194. # dhat = dis_gs[0] # the nearest distance
  195. # ghat_list = [g.copy() for g in g0hat_list]
  196. # for g in ghat_list:
  197. # nx.draw_networkx(g)
  198. # plt.show()
  199. # print(g.nodes(data=True))
  200. # print(g.edges(data=True))
  201. # Gk = [Gn[ig].copy() for ig in sort_idx[0:k]] # the k nearest neighbors
  202. # for gi in Gk:
  203. # nx.draw_networkx(gi)
  204. # plt.show()
  205. # print(gi.nodes(data=True))
  206. # print(gi.edges(data=True))
  207. # Gs_nearest = Gk.copy()
  208. ## gihat_list = []
  209. #
  210. ## i = 1
  211. # r = 1
  212. # while r < r_max:
  213. # print('r =', r)
  214. ## found = False
  215. ## Gs_nearest = Gk + gihat_list
  216. ## g_tmp = iam(Gs_nearest)
  217. # g_tmp_list = test_iam_moreGraphsAsInit_tryAllPossibleBestGraphs_deleteNodesInIterations(
  218. # Gn_median, Gs_nearest, c_ei=1, c_er=1, c_es=1)
  219. # for g in g_tmp_list:
  220. # nx.draw_networkx(g)
  221. # plt.show()
  222. # print(g.nodes(data=True))
  223. # print(g.edges(data=True))
  224. #
  225. # # compute distance between \psi and the new generated graphs.
  226. # gi_list = [Gn[i] for i in idx_gi]
  227. # knew = compute_kernel(g_tmp_list + gi_list, 'marginalizedkernel', False)
  228. # dnew_list = []
  229. # for idx, g_tmp in enumerate(g_tmp_list):
  230. # dnew_list.append(dis_gstar(idx, range(len(g_tmp_list),
  231. # len(g_tmp_list) + len(gi_list) + 1), alpha, knew))
  232. #
  233. ## dnew = knew[0, 0] - 2 * (alpha[0] * knew[0, 1] + alpha[1] *
  234. ## knew[0, 2]) + (alpha[0] * alpha[0] * k_list[0] + alpha[0] *
  235. ## alpha[1] * k_g2_list[0] + alpha[1] * alpha[0] *
  236. ## k_g1_list[1] + alpha[1] * alpha[1] * k_list[1])
  237. #
  238. # # find the new k nearest graphs.
  239. # dis_gs = dnew_list + dis_gs # add the new nearest distances.
  240. # Gs_nearest = [g.copy() for g in g_tmp_list] + Gs_nearest # add the corresponding graphs.
  241. # sort_idx = np.argsort(dis_gs)
  242. # if len([i for i in sort_idx[0:k] if i < len(dnew_list)]) > 0:
  243. # print('We got better k nearest neighbors! Hurray!')
  244. # dis_gs = [dis_gs[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
  245. # print(dis_gs[-1])
  246. # Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
  247. # nb_best = len(np.argwhere(dis_gs == dis_gs[0]).flatten().tolist())
  248. # if len([i for i in sort_idx[0:nb_best] if i < len(dnew_list)]) > 0:
  249. # print('I have smaller or equal distance!')
  250. # dhat = dis_gs[0]
  251. # print(str(dhat) + '->' + str(dhat))
  252. # idx_best_list = np.argwhere(dnew_list == dhat).flatten().tolist()
  253. # ghat_list = [g_tmp_list[idx].copy() for idx in idx_best_list]
  254. # for g in ghat_list:
  255. # nx.draw_networkx(g)
  256. # plt.show()
  257. # print(g.nodes(data=True))
  258. # print(g.edges(data=True))
  259. # r = 0
  260. # else:
  261. # r += 1
  262. #
  263. # return dhat, ghat_list
  264. def gk_iam_nearest_multi(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max,
  265. gkernel, epsilon=0.001, InitIAMWithAllDk=True,
  266. params_iam={'c_ei': 1, 'c_er': 1, 'c_es': 1,
  267. 'ite_max': 50, 'epsilon': 0.001,
  268. 'removeNodes': True, 'connected': False},
  269. params_ged={'ged_cost': 'CHEM_1', 'ged_method': 'IPFP',
  270. 'saveGXL': 'benoit'}):
  271. """This function constructs graph pre-image by the iterative pre-image
  272. framework in reference [1], algorithm 1, where the step of generating new
  273. graphs randomly is replaced by the IAM algorithm in reference [2].
  274. notes
  275. -----
  276. Every time a set of n better graphs is acquired, their distances in kernel space are
  277. compared with the k nearest ones, and the k nearest distances from the k+n
  278. distances will be used as the new ones.
  279. """
  280. # compute k nearest neighbors of phi in DN.
  281. dis_all = [] # distance between g_star and each graph.
  282. term3 = 0
  283. for i1, a1 in enumerate(alpha):
  284. for i2, a2 in enumerate(alpha):
  285. term3 += a1 * a2 * Kmatrix[idx_gi[i1], idx_gi[i2]]
  286. for ig, g in tqdm(enumerate(Gn_init), desc='computing distances', file=sys.stdout):
  287. dtemp = dis_gstar(ig, idx_gi, alpha, Kmatrix, term3=term3)
  288. # dtemp = k_list[ig] - 2 * (alpha * k_g1_list[ig] + (1 - alpha) *
  289. # k_g2_list[ig]) + (alpha * alpha * k_list[0] + alpha *
  290. # (1 - alpha) * k_g2_list[0] + (1 - alpha) * alpha *
  291. # k_g1_list[6] + (1 - alpha) * (1 - alpha) * k_list[6])
  292. dis_all.append(dtemp)
  293. # sort
  294. sort_idx = np.argsort(dis_all)
  295. dis_k = [dis_all[idis] for idis in sort_idx[0:k]] # the k shortest distances
  296. nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
  297. ghat_list = [Gn_init[idx].copy() for idx in sort_idx[0:nb_best]] # the nearest neighbors of phi in DN
  298. if dis_k[0] == 0: # the exact pre-image.
  299. print('The exact pre-image is found from the input dataset.')
  300. return 0, ghat_list, 0, 0
  301. dhat = dis_k[0] # the nearest distance
  302. # for g in ghat_list:
  303. # draw_Letter_graph(g)
  304. # nx.draw_networkx(g)
  305. # plt.show()
  306. # print(g.nodes(data=True))
  307. # print(g.edges(data=True))
  308. Gk = [Gn_init[ig].copy() for ig in sort_idx[0:k]] # the k nearest neighbors
  309. for gi in Gk:
  310. nx.draw(gi, labels=nx.get_node_attributes(gi, 'atom'), with_labels=True)
  311. # nx.draw_networkx(gi)
  312. plt.show()
  313. # draw_Letter_graph(g)
  314. print(gi.nodes(data=True))
  315. print(gi.edges(data=True))
  316. # gihat_list = []
  317. # i = 1
  318. r = 0
  319. itr_total = 0
  320. # cur_dis = dhat
  321. # old_dis = cur_dis * 2
  322. dis_of_each_itr = [dhat]
  323. found = False
  324. nb_updated = 0
  325. nb_updated_k = 0
  326. while r < r_max:# and not found: # @todo: if not found?# and np.abs(old_dis - cur_dis) > epsilon:
  327. print('\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-')
  328. print('Current preimage iteration =', r)
  329. print('Total preimage iteration =', itr_total, '\n')
  330. found = False
  331. # Gs_nearest = Gk + gihat_list
  332. # g_tmp = iam(Gs_nearest)
  333. Gn_nearest_median = [g.copy() for g in Gk]
  334. if InitIAMWithAllDk: # each graph in D_k is used to initialize IAM.
  335. ghat_new_list = []
  336. for g_tmp in Gk:
  337. Gn_nearest_init = [g_tmp.copy()]
  338. ghat_new_list_tmp, _ = iam_moreGraphsAsInit_tryAllPossibleBestGraphs(
  339. Gn_nearest_median, Gn_nearest_init, params_ged=params_ged,
  340. **params_iam)
  341. ghat_new_list += ghat_new_list_tmp
  342. else: # only the best graph in D_k is used to initialize IAM.
  343. Gn_nearest_init = [g.copy() for g in Gk]
  344. ghat_new_list, _ = iam_moreGraphsAsInit_tryAllPossibleBestGraphs(
  345. Gn_nearest_median, Gn_nearest_init, params_ged=params_ged,
  346. **params_iam)
  347. # for g in g_tmp_list:
  348. # nx.draw_networkx(g)
  349. # plt.show()
  350. # draw_Letter_graph(g)
  351. # print(g.nodes(data=True))
  352. # print(g.edges(data=True))
  353. # compute distance between \psi and the new generated graphs.
  354. knew = compute_kernel(ghat_new_list + Gn_median, gkernel, False)
  355. dhat_new_list = []
  356. for idx, g_tmp in enumerate(ghat_new_list):
  357. # @todo: the term3 below could use the one at the beginning of the function.
  358. dhat_new_list.append(dis_gstar(idx, range(len(ghat_new_list),
  359. len(ghat_new_list) + len(Gn_median) + 1),
  360. alpha, knew, withterm3=False))
  361. for idx_g, ghat_new in enumerate(ghat_new_list):
  362. # ghat_new = ghat_new_list[0].copy()
  363. dhat_new = dhat_new_list[idx_g]
  364. # if the new distance is smaller than the max of D_k.
  365. if dhat_new < dis_k[-1] and np.abs(dhat_new - dis_k[-1]) >= epsilon:
  366. # check if the new distance is the same as one in D_k.
  367. is_duplicate = False
  368. for dis_tmp in dis_k[1:-1]:
  369. if np.abs(dhat_new - dis_tmp) < epsilon:
  370. is_duplicate = True
  371. print('IAM: generated duplicate k nearest graph.')
  372. break
  373. if not is_duplicate:
  374. if np.abs(dhat_new - dhat) < epsilon:
  375. print('IAM: I am equal!')
  376. # dhat = dhat_new
  377. # ghat_list = [ghat_new.copy()]
  378. # is_iam_duplicate = True
  379. else:
  380. print('IAM: we got better k nearest neighbors!')
  381. nb_updated_k += 1
  382. print('the k nearest neighbors are updated',
  383. nb_updated_k, 'times.')
  384. dis_k = [dhat_new] + dis_k[0:k-1] # add the new nearest distance.
  385. Gk = [nx.convert_node_labels_to_integers(ghat_new.copy())] \
  386. + Gk[0:k-1] # add the corresponding graph.
  387. sort_idx = np.argsort(dis_k)
  388. dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
  389. Gk = [Gk[idx] for idx in sort_idx[0:k]]
  390. if dhat_new < dhat:
  391. print('IAM: I have smaller distance!')
  392. print(str(dhat) + '->' + str(dhat_new))
  393. dhat = dhat_new
  394. ghat_list = [Gk[0].copy()]
  395. r = 0
  396. nb_updated += 1
  397. print('the graph is updated', nb_updated, 'times.')
  398. nx.draw(Gk[0], labels=nx.get_node_attributes(Gk[0], 'atom'),
  399. with_labels=True)
  400. ## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
  401. plt.show()
  402. found = True
  403. if not found:
  404. r += 1
  405. # else: # @todo: may not work.
  406. # dis_k = dhat_new_list + dis_k # add the new nearest distances.
  407. # Gk = [nx.convert_node_labels_to_integers(g.copy()) for g
  408. # in ghat_new_list] + Gk # add the corresponding graphs.
  409. # sort_idx = np.argsort(dis_k)
  410. # if len([i for i in sort_idx[0:k] if i < len(dhat_new_list)]) > 0:
  411. # print('We got new k nearest neighbors! Hurray!')
  412. # dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
  413. # # print(dis_k[-1])
  414. # Gk = [Gk[idx] for idx in sort_idx[0:k]]
  415. # nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
  416. # if dhat_new < dhat:
  417. # print('I have smaller distance!')
  418. # print(str(dhat) + '->' + str(dhat_new))
  419. # dhat = dis_k[0]
  420. # idx_best_list = np.argwhere(dis_k == dhat_new).flatten().tolist()
  421. # ghat_list = [Gk[idx].copy() for idx in idx_best_list]
  422. # # for g in ghat_list:
  423. # ## nx.draw_networkx(g)
  424. # ## plt.show()
  425. # # draw_Letter_graph(g)
  426. # # print(g.nodes(data=True))
  427. # # print(g.edges(data=True))
  428. # r = 0
  429. # found = True
  430. # nb_updated += 1
  431. # print('the graph is updated by IAM', nb_updated, 'times.')
  432. #
  433. # nx.draw(ghat_list[0], labels=nx.get_node_attributes(ghat_list[0], 'atom'),
  434. # with_labels=True)
  435. # ## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
  436. # plt.show()
  437. # else:
  438. # dis_k = [dis_k[idx] for idx in sort_idx[0:k]]
  439. # Gk = [Gk[idx] for idx in sort_idx[0:k]]
  440. # # Gn_nearest_median = [g.copy() for g in Gn_nearest_init]
  441. # if not found:
  442. # r += 1
  443. # dnew = knew[0, 0] - 2 * (alpha[0] * knew[0, 1] + alpha[1] *
  444. # knew[0, 2]) + (alpha[0] * alpha[0] * k_list[0] + alpha[0] *
  445. # alpha[1] * k_g2_list[0] + alpha[1] * alpha[0] *
  446. # k_g1_list[1] + alpha[1] * alpha[1] * k_list[1])
  447. # # find the new k nearest graphs.
  448. # dhat_new = min(dnew_list)
  449. # dis_k = dnew_list + dis_k # add the new nearest distances.
  450. # Gs_nearest = [g.copy() for g in g_tmp_list] + Gs_nearest # add the corresponding graphs.
  451. # sort_idx = np.argsort(dis_k)
  452. # if len([i for i in sort_idx[0:k] if i < len(dnew_list)]) > 0:
  453. # print('We got new k nearest neighbors! Hurray!')
  454. # dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
  455. ## print(dis_k[-1])
  456. # Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
  457. # nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
  458. # if dnew_best < dhat and np.abs(dnew_best - dhat) > epsilon:
  459. # print('I have smaller distance!')
  460. # print(str(dhat) + '->' + str(dis_k[0]))
  461. # dhat = dis_k[0]
  462. # idx_best_list = np.argwhere(dnew_list == dhat).flatten().tolist()
  463. # ghat_list = [g_tmp_list[idx].copy() for idx in idx_best_list]
  464. ## for g in ghat_list:
  465. ### nx.draw_networkx(g)
  466. ### plt.show()
  467. ## draw_Letter_graph(g)
  468. ## print(g.nodes(data=True))
  469. ## print(g.edges(data=True))
  470. # r = 0
  471. # found = True
  472. # nb_updated += 1
  473. # elif np.abs(dnew_best - dhat) < epsilon:
  474. # print('I have almost equal distance!')
  475. # print(str(dhat) + '->' + str(dnew_best))
  476. # else:
  477. # dis_k = [dis_k[idx] for idx in sort_idx[0:k]]
  478. # Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
  479. # Gn_nearest_median = [g.copy() for g in Gs_nearest]
  480. # if not found:
  481. # r += 1
  482. # # find the new k nearest graphs.
  483. # dnew_best = min(dnew_list)
  484. # if np.abs(dnew_best - dhat) >= epsilon:
  485. # dis_k = dnew_list + dis_k # add the new nearest distances.
  486. # Gs_nearest_init = [g.copy() for g in g_tmp_list] + Gs_nearest_init # add the corresponding graphs.
  487. # sort_idx = np.argsort(dis_k)
  488. # else: # if the new distance is equal to the old one.
  489. # # @todo: works if only one graph is generated.
  490. # Gs_nearest_init[0] = g_tmp_list[0]_init.copy()
  491. # sort_idx = np.argsort(dis_k)
  492. # if len([i for i in sort_idx[0:k] if i < len(dnew_list)]) > 0:
  493. # print('We got new k nearest neighbors! Hurray!')
  494. # dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
  495. ## print(dis_k[-1])
  496. # Gs_nearest_init = [Gs_nearest_init[idx] for idx in sort_idx[0:k]]
  497. # nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
  498. # if dnew_best < dhat and np.abs(dnew_best - dhat) >= epsilon:
  499. # print('I have smaller distance!')
  500. # print(str(dhat) + '->' + str(dis_k[0]))
  501. # dhat = dis_k[0]
  502. # idx_best_list = np.argwhere(dnew_list == dhat).flatten().tolist()
  503. # ghat_list = [g_tmp_list[idx].copy() for idx in idx_best_list]
  504. ## for g in ghat_list:
  505. ### nx.draw_networkx(g)
  506. ### plt.show()
  507. ## draw_Letter_graph(g)
  508. ## print(g.nodes(data=True))
  509. ## print(g.edges(data=True))
  510. # r = 0
  511. # found = True
  512. # nb_updated += 1
  513. # elif np.abs(dnew_best - dhat) < epsilon:
  514. # print('I have almost equal distance!')
  515. # print(str(dhat) + '->' + str(dnew_best))
  516. # else:
  517. # dis_k = [dis_k[idx] for idx in sort_idx[0:k]]
  518. # Gs_nearest_init = [Gs_nearest_init[idx] for idx in sort_idx[0:k]]
  519. # Gn_nearest_median = [g.copy() for g in Gs_nearest]
  520. # if not found:
  521. # r += 1
  522. # old_dis = cur_dis
  523. # cur_dis = dnew_best
  524. dis_of_each_itr.append(dhat)
  525. itr_total += 1
  526. print('\nthe k shortest distances are', dis_k)
  527. print('the shortest distances for previous iterations are', dis_of_each_itr)
  528. print('\nthe graph is updated', nb_updated, 'times.')
  529. print('\nthe k nearest neighbors are updated', nb_updated_k, 'times.')
  530. print('distances in kernel space:', dis_of_each_itr, '\n')
  531. return dhat, ghat_list, dis_of_each_itr[-1], nb_updated, nb_updated_k
  532. def preimage_iam_random_mix(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max,
  533. l_max, gkernel, epsilon=0.001,
  534. allDkInitRandom=False, InitIAMWithAllDk=False,
  535. params_iam={'c_ei': 1, 'c_er': 1, 'c_es': 1,
  536. 'ite_max': 50, 'epsilon': 0.001,
  537. 'removeNodes': True, 'connected': False},
  538. params_ged={'ged_cost': 'CHEM_1', 'ged_method': 'IPFP',
  539. 'saveGXL': 'benoit'}):
  540. """This function constructs graph pre-image by the iterative pre-image
  541. framework in reference [1], algorithm 1, where new graphs are generated
  542. randomly and by the IAM algorithm in reference [2].
  543. notes
  544. -----
  545. Every time a set of n better graphs is acquired, their distances in kernel space are
  546. compared with the k nearest ones, and the k nearest distances from the k+n
  547. distances will be used as the new ones.
  548. """
  549. Gn_init = [nx.convert_node_labels_to_integers(g) for g in Gn_init]
  550. # compute k nearest neighbors of phi in DN.
  551. dis_all = [] # distance between g_star and each graph.
  552. term3 = 0
  553. for i1, a1 in enumerate(alpha):
  554. for i2, a2 in enumerate(alpha):
  555. term3 += a1 * a2 * Kmatrix[idx_gi[i1], idx_gi[i2]]
  556. for ig, g in tqdm(enumerate(Gn_init), desc='computing distances', file=sys.stdout):
  557. dtemp = dis_gstar(ig, idx_gi, alpha, Kmatrix, term3=term3)
  558. dis_all.append(dtemp)
  559. # sort
  560. sort_idx = np.argsort(dis_all)
  561. dis_k = [dis_all[idis] for idis in sort_idx[0:k]] # the k shortest distances
  562. nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
  563. ghat_list = [Gn_init[idx].copy() for idx in sort_idx[0:nb_best]] # the nearest neighbors of psi in DN
  564. if dis_k[0] == 0: # the exact pre-image.
  565. print('The exact pre-image is found from the input dataset.')
  566. return 0, ghat_list, 0, 0
  567. dhat = dis_k[0] # the nearest distance
  568. # for g in ghat_list:
  569. # draw_Letter_graph(g)
  570. # nx.draw_networkx(g)
  571. # plt.show()
  572. # print(g.nodes(data=True))
  573. # print(g.edges(data=True))
  574. Gk = [Gn_init[ig].copy() for ig in sort_idx[0:k]] # the k nearest neighbors
  575. for gi in Gk:
  576. nx.draw(gi, labels=nx.get_node_attributes(gi, 'atom'), with_labels=True)
  577. # nx.draw_networkx(gi)
  578. plt.show()
  579. # draw_Letter_graph(g)
  580. print(gi.nodes(data=True))
  581. print(gi.edges(data=True))
  582. # gihat_list = []
  583. # i = 1
  584. r = 0
  585. itr_total = 0
  586. # cur_dis = dhat
  587. # old_dis = cur_dis * 2
  588. dis_of_each_itr = [dhat]
  589. found = False
  590. nb_updated_iam = 0
  591. nb_updated_k_iam = 0
  592. nb_updated_random = 0
  593. nb_updated_k_random = 0
  594. is_iam_duplicate = False
  595. while r < r_max: # and not found: # @todo: if not found?# and np.abs(old_dis - cur_dis) > epsilon:
  596. print('\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-')
  597. print('Current preimage iteration =', r)
  598. print('Total preimage iteration =', itr_total, '\n')
  599. found = False
  600. # Gs_nearest = Gk + gihat_list
  601. # g_tmp = iam(Gs_nearest)
  602. if not is_iam_duplicate:
  603. Gn_nearest_init = [g.copy() for g in Gk]
  604. Gn_nearest_median = [g.copy() for g in Gk]
  605. ghat_new_list, _ = iam_moreGraphsAsInit_tryAllPossibleBestGraphs(
  606. Gn_nearest_median, Gn_nearest_init, params_ged=params_ged, **params_iam)
  607. # for g in ghat_new_list:
  608. # nx.draw_networkx(g)
  609. # plt.show()
  610. # draw_Letter_graph(g)
  611. # print(g.nodes(data=True))
  612. # print(g.edges(data=True))
  613. # compute distance between \psi and the new generated graphs.
  614. knew = compute_kernel(ghat_new_list + Gn_median, gkernel, False)
  615. dhat_new_list = []
  616. for idx, g_tmp in enumerate(ghat_new_list):
  617. # @todo: the term3 below could use the one at the beginning of the function.
  618. dhat_new_list.append(dis_gstar(idx, range(len(ghat_new_list),
  619. len(ghat_new_list) + len(Gn_median) + 1),
  620. alpha, knew, withterm3=False))
  621. ghat_new = ghat_new_list[0].copy()
  622. dhat_new = min(dhat_new_list)
  623. # find the new k nearest graphs.
  624. # @todo: for now only consider the situation when only one graph is generated by IAM.
  625. # when new distance is not smaller than the max of D_k, use random generation.
  626. if np.abs(dhat_new - dis_k[-1]) < epsilon or dis_k[-1] < dhat_new or \
  627. is_iam_duplicate:
  628. # Gs_nearest[0] = ghat_new_list[0].copy()
  629. # sort_idx = np.argsort(dis_k)
  630. print('Distance not better, switching to random generation now.')
  631. print(str(dhat) + '->' + str(dhat_new))
  632. if allDkInitRandom: # use all k nearest graphs as the initials.
  633. init_list = [g_init.copy() for g_init in Gk]
  634. else: # use just the nearest graph as the initial.
  635. init_list = [Gk[0].copy()]
  636. # number of edges to be changed.
  637. if len(init_list) == 1:
  638. # @todo what if the log is negetive? how to choose alpha (scalar)? seems fdgs is always 1.
  639. # fdgs = dhat_new
  640. fdgs = nb_updated_random + 1
  641. if fdgs < 1:
  642. fdgs = 1
  643. fdgs = int(np.ceil(np.log(fdgs)))
  644. if fdgs < 1:
  645. fdgs += 1
  646. # fdgs = nb_updated_random + 1 # @todo:
  647. fdgs_list = [fdgs]
  648. else:
  649. # @todo what if the log is negetive? how to choose alpha (scalar)?
  650. fdgs_list = np.array(dis_k[:])
  651. if np.min(fdgs_list) < 1:
  652. fdgs_list /= dis_k[0]
  653. fdgs_list = [int(item) for item in np.ceil(np.log(fdgs_list))]
  654. if np.min(fdgs_list) < 1:
  655. fdgs_list = np.array(fdgs_list) + 1
  656. l = 0
  657. found_random = False
  658. while l < l_max and not found_random:
  659. for idx_g, g_tmp in enumerate(init_list):
  660. # add and delete edges.
  661. ghat_new = nx.convert_node_labels_to_integers(g_tmp.copy())
  662. # @todo: should we use just half of the adjacency matrix for undirected graphs?
  663. nb_vpairs = nx.number_of_nodes(ghat_new) * (nx.number_of_nodes(ghat_new) - 1)
  664. np.random.seed()
  665. # which edges to change.
  666. # @todo: what if fdgs is bigger than nb_vpairs?
  667. idx_change = random.sample(range(nb_vpairs), fdgs_list[idx_g] if
  668. fdgs_list[idx_g] < nb_vpairs else nb_vpairs)
  669. # idx_change = np.random.randint(0, nx.number_of_nodes(gs) *
  670. # (nx.number_of_nodes(gs) - 1), fdgs)
  671. for item in idx_change:
  672. node1 = int(item / (nx.number_of_nodes(ghat_new) - 1))
  673. node2 = (item - node1 * (nx.number_of_nodes(ghat_new) - 1))
  674. if node2 >= node1: # skip the self pair.
  675. node2 += 1
  676. # @todo: is the randomness correct?
  677. if not ghat_new.has_edge(node1, node2):
  678. ghat_new.add_edge(node1, node2)
  679. # nx.draw_networkx(gs)
  680. # plt.show()
  681. # nx.draw_networkx(ghat_new)
  682. # plt.show()
  683. else:
  684. ghat_new.remove_edge(node1, node2)
  685. # nx.draw_networkx(gs)
  686. # plt.show()
  687. # nx.draw_networkx(ghat_new)
  688. # plt.show()
  689. # nx.draw_networkx(ghat_new)
  690. # plt.show()
  691. # compute distance between \psi and the new generated graph.
  692. knew = compute_kernel([ghat_new] + Gn_median, gkernel, verbose=False)
  693. dhat_new = dis_gstar(0, [1, 2], alpha, knew, withterm3=False)
  694. # @todo: the new distance is smaller or also equal?
  695. if dhat_new < dis_k[-1] and np.abs(dhat_new - dis_k[-1]) >= epsilon:
  696. # check if the new distance is the same as one in D_k.
  697. is_duplicate = False
  698. for dis_tmp in dis_k[1:-1]:
  699. if np.abs(dhat_new - dis_tmp) < epsilon:
  700. is_duplicate = True
  701. print('Random: generated duplicate k nearest graph.')
  702. break
  703. if not is_duplicate:
  704. if np.abs(dhat_new - dhat) < epsilon:
  705. print('Random: I am equal!')
  706. # dhat = dhat_new
  707. # ghat_list = [ghat_new.copy()]
  708. else:
  709. print('Random: we got better k nearest neighbors!')
  710. print('l =', str(l))
  711. nb_updated_k_random += 1
  712. print('the k nearest neighbors are updated by random generation',
  713. nb_updated_k_random, 'times.')
  714. dis_k = [dhat_new] + dis_k # add the new nearest distances.
  715. Gk = [ghat_new.copy()] + Gk # add the corresponding graphs.
  716. sort_idx = np.argsort(dis_k)
  717. dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
  718. Gk = [Gk[idx] for idx in sort_idx[0:k]]
  719. if dhat_new < dhat:
  720. print('\nRandom: I am smaller!')
  721. print('l =', str(l))
  722. print(dhat, '->', dhat_new)
  723. dhat = dhat_new
  724. ghat_list = [ghat_new.copy()]
  725. r = 0
  726. nb_updated_random += 1
  727. print('the graph is updated by random generation',
  728. nb_updated_random, 'times.')
  729. nx.draw(ghat_new, labels=nx.get_node_attributes(ghat_new, 'atom'),
  730. with_labels=True)
  731. ## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
  732. plt.show()
  733. found_random = True
  734. break
  735. l += 1
  736. if not found_random: # l == l_max:
  737. r += 1
  738. is_iam_duplicate = False
  739. else: # if the new distance is smaller than the max of D_k.
  740. if len(dhat_new_list) == 1:
  741. # check if the new distance is the same as one in D_k.
  742. is_duplicate = False
  743. for dis_tmp in dis_k[1:-1]:
  744. if np.abs(dhat_new - dis_tmp) < epsilon:
  745. is_duplicate = True
  746. print('IAM: generated duplicate k nearest graph.')
  747. break
  748. if not is_duplicate:
  749. if np.abs(dhat_new - dhat) < epsilon:
  750. print('IAM: I am equal!')
  751. # dhat = dhat_new
  752. # ghat_list = [ghat_new.copy()]
  753. is_iam_duplicate = True
  754. else:
  755. print('IAM: we got better k nearest neighbors!')
  756. nb_updated_k_iam += 1
  757. print('the k nearest neighbors are updated by IAM',
  758. nb_updated_k_iam, 'times.')
  759. dis_k = dhat_new_list + dis_k[0:k-1] # add the new nearest distances.
  760. Gk = [nx.convert_node_labels_to_integers(ghat_new_list[0].copy())] \
  761. + Gk[0:k-1] # add the corresponding graphs.
  762. sort_idx = np.argsort(dis_k)
  763. dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
  764. Gk = [Gk[idx] for idx in sort_idx[0:k]]
  765. if dhat_new < dhat:
  766. print('IAM: I have smaller distance!')
  767. print(str(dhat) + '->' + str(dhat_new))
  768. dhat = dhat_new
  769. ghat_list = [Gk[0].copy()]
  770. r = 0
  771. nb_updated_iam += 1
  772. print('the graph is updated by IAM', nb_updated_iam, 'times.')
  773. nx.draw(Gk[0], labels=nx.get_node_attributes(Gk[0], 'atom'),
  774. with_labels=True)
  775. ## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
  776. plt.show()
  777. else:
  778. is_iam_duplicate = True
  779. else: # @todo: may not work.
  780. dis_k = dhat_new_list + dis_k # add the new nearest distances.
  781. Gk = [nx.convert_node_labels_to_integers(g.copy()) for g
  782. in ghat_new_list] + Gk # add the corresponding graphs.
  783. sort_idx = np.argsort(dis_k)
  784. if len([i for i in sort_idx[0:k] if i < len(dhat_new_list)]) > 0:
  785. print('We got new k nearest neighbors! Hurray!')
  786. dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
  787. # print(dis_k[-1])
  788. Gk = [Gk[idx] for idx in sort_idx[0:k]]
  789. nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
  790. if dhat_new < dhat:
  791. print('I have smaller distance!')
  792. print(str(dhat) + '->' + str(dhat_new))
  793. dhat = dis_k[0]
  794. idx_best_list = np.argwhere(dis_k == dhat_new).flatten().tolist()
  795. ghat_list = [Gk[idx].copy() for idx in idx_best_list]
  796. # for g in ghat_list:
  797. ## nx.draw_networkx(g)
  798. ## plt.show()
  799. # draw_Letter_graph(g)
  800. # print(g.nodes(data=True))
  801. # print(g.edges(data=True))
  802. r = 0
  803. found = True
  804. nb_updated_iam += 1
  805. print('the graph is updated by IAM', nb_updated_iam, 'times.')
  806. nx.draw(ghat_list[0], labels=nx.get_node_attributes(ghat_list[0], 'atom'),
  807. with_labels=True)
  808. ## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
  809. plt.show()
  810. else:
  811. dis_k = [dis_k[idx] for idx in sort_idx[0:k]]
  812. Gk = [Gk[idx] for idx in sort_idx[0:k]]
  813. # Gn_nearest_median = [g.copy() for g in Gn_nearest_init]
  814. if not found:
  815. r += 1
  816. # old_dis = cur_dis
  817. # cur_dis = dnew_best
  818. dis_of_each_itr.append(dhat)
  819. itr_total += 1
  820. print('\nthe k shortest distances are', dis_k)
  821. print('the shortest distances for previous iterations are', dis_of_each_itr)
  822. print('\nthe graph is updated by IAM', nb_updated_iam, 'times, and by random generation',
  823. nb_updated_random, 'times.')
  824. print('\nthe k nearest neighbors are updated by IAM', nb_updated_k_iam,
  825. 'times, and by random generation', nb_updated_k_random, 'times.')
  826. print('distances in kernel space:', dis_of_each_itr, '\n')
  827. return dhat, ghat_list, dis_of_each_itr[-1], \
  828. nb_updated_iam, nb_updated_random, nb_updated_k_iam, nb_updated_k_random
  829. ###############################################################################
  830. # useful functions.
  831. def dis_gstar(idx_g, idx_gi, alpha, Kmatrix, term3=0, withterm3=True):
  832. term1 = Kmatrix[idx_g, idx_g]
  833. term2 = 0
  834. for i, a in enumerate(alpha):
  835. term2 += a * Kmatrix[idx_g, idx_gi[i]]
  836. term2 *= 2
  837. if withterm3 == False:
  838. for i1, a1 in enumerate(alpha):
  839. for i2, a2 in enumerate(alpha):
  840. term3 += a1 * a2 * Kmatrix[idx_gi[i1], idx_gi[i2]]
  841. return np.sqrt(term1 - term2 + term3)
  842. def compute_kernel(Gn, graph_kernel, verbose):
  843. if graph_kernel == 'marginalizedkernel':
  844. Kmatrix, _ = marginalizedkernel(Gn, node_label='atom', edge_label=None,
  845. p_quit=0.03, n_iteration=10, remove_totters=False,
  846. n_jobs=multiprocessing.cpu_count(), verbose=verbose)
  847. elif graph_kernel == 'untilhpathkernel':
  848. Kmatrix, _ = untilhpathkernel(Gn, node_label='atom', edge_label=None,
  849. depth=10, k_func='MinMax', compute_method='trie',
  850. n_jobs=multiprocessing.cpu_count(), verbose=verbose)
  851. elif graph_kernel == 'spkernel':
  852. mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)
  853. Kmatrix, _, _ = spkernel(Gn, node_label='atom', node_kernels=
  854. {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel},
  855. n_jobs=multiprocessing.cpu_count(), verbose=verbose)
  856. elif graph_kernel == 'structuralspkernel':
  857. mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)
  858. Kmatrix, _ = structuralspkernel(Gn, node_label='atom', node_kernels=
  859. {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel},
  860. n_jobs=multiprocessing.cpu_count(), verbose=verbose)
  861. # normalization
  862. Kmatrix_diag = Kmatrix.diagonal().copy()
  863. for i in range(len(Kmatrix)):
  864. for j in range(i, len(Kmatrix)):
  865. Kmatrix[i][j] /= np.sqrt(Kmatrix_diag[i] * Kmatrix_diag[j])
  866. Kmatrix[j][i] = Kmatrix[i][j]
  867. return Kmatrix
  868. def gram2distances(Kmatrix):
  869. dmatrix = np.zeros((len(Kmatrix), len(Kmatrix)))
  870. for i1 in range(len(Kmatrix)):
  871. for i2 in range(len(Kmatrix)):
  872. dmatrix[i1, i2] = Kmatrix[i1, i1] + Kmatrix[i2, i2] - 2 * Kmatrix[i1, i2]
  873. dmatrix = np.sqrt(dmatrix)
  874. return dmatrix

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