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.

untilHPathKernel.py 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. """
  2. @author: linlin
  3. @references: Liva Ralaivola, Sanjay J Swamidass, Hiroto Saigo, and Pierre
  4. Baldi. Graph kernels for chemical informatics. Neural networks,
  5. 18(8):1093–1110, 2005.
  6. """
  7. import sys
  8. sys.path.insert(0, "../")
  9. import time
  10. from collections import Counter
  11. from itertools import chain
  12. from functools import partial
  13. from multiprocessing import Pool
  14. from tqdm import tqdm
  15. import networkx as nx
  16. import numpy as np
  17. from pygraph.utils.graphdataset import get_dataset_attributes
  18. from pygraph.utils.parallel import parallel_gm
  19. from pygraph.utils.trie import Trie
  20. def untilhpathkernel(*args,
  21. node_label='atom',
  22. edge_label='bond_type',
  23. depth=10,
  24. k_func='MinMax',
  25. compute_method='trie',
  26. n_jobs=None,
  27. verbose=True):
  28. """Calculate path graph kernels up to depth/hight h between graphs.
  29. Parameters
  30. ----------
  31. Gn : List of NetworkX graph
  32. List of graphs between which the kernels are calculated.
  33. /
  34. G1, G2 : NetworkX graphs
  35. Two graphs between which the kernel is calculated.
  36. node_label : string
  37. Node attribute used as label. The default node label is atom.
  38. edge_label : string
  39. Edge attribute used as label. The default edge label is bond_type.
  40. depth : integer
  41. Depth of search. Longest length of paths.
  42. k_func : function
  43. A kernel function applied using different notions of fingerprint
  44. similarity, defining the type of feature map and normalization method
  45. applied for the graph kernel. The Following choices are available:
  46. 'MinMax': use the MiniMax kernel and counting feature map.
  47. 'tanimoto': use the Tanimoto kernel and binary feature map.
  48. compute_method : string
  49. Computation method to store paths and compute the graph kernel. The
  50. Following choices are available:
  51. 'trie': store paths as tries.
  52. 'naive': store paths to lists.
  53. n_jobs : int
  54. Number of jobs for parallelization.
  55. Return
  56. ------
  57. Kmatrix : Numpy matrix
  58. Kernel matrix, each element of which is the path kernel up to h between
  59. 2 praphs.
  60. """
  61. # pre-process
  62. depth = int(depth)
  63. Gn = args[0] if len(args) == 1 else [args[0], args[1]]
  64. Gn = [g.copy() for g in Gn]
  65. Kmatrix = np.zeros((len(Gn), len(Gn)))
  66. ds_attrs = get_dataset_attributes(
  67. Gn,
  68. attr_names=['node_labeled', 'edge_labeled', 'is_directed'],
  69. node_label=node_label, edge_label=edge_label)
  70. if not ds_attrs['node_labeled']:
  71. for G in Gn:
  72. nx.set_node_attributes(G, '0', 'atom')
  73. if not ds_attrs['edge_labeled']:
  74. for G in Gn:
  75. nx.set_edge_attributes(G, '0', 'bond_type')
  76. start_time = time.time()
  77. # ---- use pool.imap_unordered to parallel and track progress. ----
  78. # get all paths of all graphs before calculating kernels to save time,
  79. # but this may cost a lot of memory for large datasets.
  80. pool = Pool(n_jobs)
  81. itr = zip(Gn, range(0, len(Gn)))
  82. if len(Gn) < 100 * n_jobs:
  83. chunksize = int(len(Gn) / n_jobs) + 1
  84. else:
  85. chunksize = 100
  86. all_paths = [[] for _ in range(len(Gn))]
  87. if compute_method == 'trie':
  88. getps_partial = partial(wrapper_find_all_path_as_trie, depth,
  89. ds_attrs, node_label, edge_label)
  90. else:
  91. getps_partial = partial(wrapper_find_all_paths_until_length, depth,
  92. ds_attrs, node_label, edge_label)
  93. if verbose:
  94. iterator = tqdm(pool.imap_unordered(getps_partial, itr, chunksize),
  95. desc='getting paths', file=sys.stdout)
  96. else:
  97. iterator = pool.imap_unordered(getps_partial, itr, chunksize)
  98. for i, ps in iterator:
  99. all_paths[i] = ps
  100. pool.close()
  101. pool.join()
  102. # for g in Gn:
  103. # if compute_method == 'trie':
  104. # find_all_path_as_trie(g, depth, ds_attrs, node_label, edge_label)
  105. # else:
  106. # find_all_paths_until_length(g, depth, ds_attrs, node_label, edge_label)
  107. ## size = sys.getsizeof(all_paths)
  108. ## for item in all_paths:
  109. ## size += sys.getsizeof(item)
  110. ## for pppps in item:
  111. ## size += sys.getsizeof(pppps)
  112. ## print(size)
  113. #
  114. ## ttt = time.time()
  115. ## # ---- ---- use pool.map to parallel ----
  116. ## for i, ps in tqdm(
  117. ## pool.map(getps_partial, range(0, len(Gn))),
  118. ## desc='getting paths', file=sys.stdout):
  119. ## all_paths[i] = ps
  120. ## print(time.time() - ttt)
  121. if compute_method == 'trie':
  122. def init_worker(trie_toshare):
  123. global G_trie
  124. G_trie = trie_toshare
  125. do_partial = partial(wrapper_uhpath_do_trie, k_func)
  126. parallel_gm(do_partial, Kmatrix, Gn, init_worker=init_worker,
  127. glbv=(all_paths,), n_jobs=n_jobs, verbose=verbose)
  128. else:
  129. def init_worker(plist_toshare):
  130. global G_plist
  131. G_plist = plist_toshare
  132. do_partial = partial(wrapper_uhpath_do_naive, k_func)
  133. parallel_gm(do_partial, Kmatrix, Gn, init_worker=init_worker,
  134. glbv=(all_paths,), n_jobs=n_jobs, verbose=verbose)
  135. # # ---- direct running, normally use single CPU core. ----
  136. # all_paths = [
  137. # find_all_paths_until_length(
  138. # Gn[i],
  139. # depth,
  140. # ds_attrs,
  141. # node_label=node_label,
  142. # edge_label=edge_label) for i in tqdm(
  143. # range(0, len(Gn)), desc='getting paths', file=sys.stdout)
  144. # ]
  145. #
  146. # if compute_method == 'trie':
  147. # pbar = tqdm(
  148. # total=((len(Gn) + 1) * len(Gn) / 2),
  149. # desc='calculating kernels',
  150. # file=sys.stdout)
  151. # for i in range(0, len(Gn)):
  152. # for j in range(i, len(Gn)):
  153. # Kmatrix[i][j] = _untilhpathkernel_do_trie(all_paths[i],
  154. # all_paths[j], k_func)
  155. # Kmatrix[j][i] = Kmatrix[i][j]
  156. # pbar.update(1)
  157. # else:
  158. # pbar = tqdm(
  159. # total=((len(Gn) + 1) * len(Gn) / 2),
  160. # desc='calculating kernels',
  161. # file=sys.stdout)
  162. # for i in range(0, len(Gn)):
  163. # for j in range(i, len(Gn)):
  164. # Kmatrix[i][j] = _untilhpathkernel_do_naive(all_paths[i], all_paths[j],
  165. # k_func)
  166. # Kmatrix[j][i] = Kmatrix[i][j]
  167. # pbar.update(1)
  168. run_time = time.time() - start_time
  169. if verbose:
  170. print("\n --- kernel matrix of path kernel up to %d of size %d built in %s seconds ---"
  171. % (depth, len(Gn), run_time))
  172. # print(Kmatrix[0][0:10])
  173. return Kmatrix, run_time
  174. def _untilhpathkernel_do_trie(trie1, trie2, k_func):
  175. """Calculate path graph kernels up to depth d between 2 graphs using trie.
  176. Parameters
  177. ----------
  178. trie1, trie2 : list
  179. Tries that contains all paths in 2 graphs.
  180. k_func : function
  181. A kernel function applied using different notions of fingerprint
  182. similarity.
  183. Return
  184. ------
  185. kernel : float
  186. Path kernel up to h between 2 graphs.
  187. """
  188. if k_func == 'tanimoto':
  189. # traverse all paths in graph1 and search them in graph2. Deep-first
  190. # search is applied.
  191. def traverseTrie1t(root, trie2, setlist, pcurrent=[]):
  192. for key, node in root['children'].items():
  193. pcurrent.append(key)
  194. if node['isEndOfWord']:
  195. setlist[1] += 1
  196. count2 = trie2.searchWord(pcurrent)
  197. if count2 != 0:
  198. setlist[0] += 1
  199. if node['children'] != {}:
  200. traverseTrie1t(node, trie2, setlist, pcurrent)
  201. else:
  202. del pcurrent[-1]
  203. if pcurrent != []:
  204. del pcurrent[-1]
  205. # traverse all paths in graph2 and find out those that are not in
  206. # graph1. Deep-first search is applied.
  207. def traverseTrie2t(root, trie1, setlist, pcurrent=[]):
  208. for key, node in root['children'].items():
  209. pcurrent.append(key)
  210. if node['isEndOfWord']:
  211. # print(node['count'])
  212. count1 = trie1.searchWord(pcurrent)
  213. if count1 == 0:
  214. setlist[1] += 1
  215. if node['children'] != {}:
  216. traverseTrie2t(node, trie1, setlist, pcurrent)
  217. else:
  218. del pcurrent[-1]
  219. if pcurrent != []:
  220. del pcurrent[-1]
  221. setlist = [0, 0] # intersection and union of path sets of g1, g2.
  222. # print(trie1.root)
  223. # print(trie2.root)
  224. traverseTrie1t(trie1.root, trie2, setlist)
  225. # print(setlist)
  226. traverseTrie2t(trie2.root, trie1, setlist)
  227. # print(setlist)
  228. kernel = setlist[0] / setlist[1]
  229. else: # MinMax kernel
  230. # traverse all paths in graph1 and search them in graph2. Deep-first
  231. # search is applied.
  232. def traverseTrie1m(root, trie2, sumlist, pcurrent=[]):
  233. for key, node in root['children'].items():
  234. pcurrent.append(key)
  235. if node['isEndOfWord']:
  236. # print(node['count'])
  237. count1 = node['count']
  238. count2 = trie2.searchWord(pcurrent)
  239. sumlist[0] += min(count1, count2)
  240. sumlist[1] += max(count1, count2)
  241. if node['children'] != {}:
  242. traverseTrie1m(node, trie2, sumlist, pcurrent)
  243. else:
  244. del pcurrent[-1]
  245. if pcurrent != []:
  246. del pcurrent[-1]
  247. # traverse all paths in graph2 and find out those that are not in
  248. # graph1. Deep-first search is applied.
  249. def traverseTrie2m(root, trie1, sumlist, pcurrent=[]):
  250. for key, node in root['children'].items():
  251. pcurrent.append(key)
  252. if node['isEndOfWord']:
  253. # print(node['count'])
  254. count1 = trie1.searchWord(pcurrent)
  255. if count1 == 0:
  256. sumlist[1] += node['count']
  257. if node['children'] != {}:
  258. traverseTrie2m(node, trie1, sumlist, pcurrent)
  259. else:
  260. del pcurrent[-1]
  261. if pcurrent != []:
  262. del pcurrent[-1]
  263. sumlist = [0, 0] # sum of mins and sum of maxs
  264. # print(trie1.root)
  265. # print(trie2.root)
  266. traverseTrie1m(trie1.root, trie2, sumlist)
  267. # print(sumlist)
  268. traverseTrie2m(trie2.root, trie1, sumlist)
  269. # print(sumlist)
  270. kernel = sumlist[0] / sumlist[1]
  271. return kernel
  272. def wrapper_uhpath_do_trie(k_func, itr):
  273. i = itr[0]
  274. j = itr[1]
  275. return i, j, _untilhpathkernel_do_trie(G_trie[i], G_trie[j], k_func)
  276. def _untilhpathkernel_do_naive(paths1, paths2, k_func):
  277. """Calculate path graph kernels up to depth d between 2 graphs naively.
  278. Parameters
  279. ----------
  280. paths_list : list of list
  281. List of list of paths in all graphs, where for unlabeled graphs, each
  282. path is represented by a list of nodes; while for labeled graphs, each
  283. path is represented by a string consists of labels of nodes and/or
  284. edges on that path.
  285. k_func : function
  286. A kernel function applied using different notions of fingerprint
  287. similarity.
  288. Return
  289. ------
  290. kernel : float
  291. Path kernel up to h between 2 graphs.
  292. """
  293. all_paths = list(set(paths1 + paths2))
  294. if k_func == 'tanimoto':
  295. length_union = len(set(paths1 + paths2))
  296. kernel = (len(set(paths1)) + len(set(paths2)) -
  297. length_union) / length_union
  298. # vector1 = [(1 if path in paths1 else 0) for path in all_paths]
  299. # vector2 = [(1 if path in paths2 else 0) for path in all_paths]
  300. # kernel_uv = np.dot(vector1, vector2)
  301. # kernel = kernel_uv / (len(set(paths1)) + len(set(paths2)) - kernel_uv)
  302. else: # MinMax kernel
  303. path_count1 = Counter(paths1)
  304. path_count2 = Counter(paths2)
  305. vector1 = [(path_count1[key] if (key in path_count1.keys()) else 0)
  306. for key in all_paths]
  307. vector2 = [(path_count2[key] if (key in path_count2.keys()) else 0)
  308. for key in all_paths]
  309. kernel = np.sum(np.minimum(vector1, vector2)) / \
  310. np.sum(np.maximum(vector1, vector2))
  311. return kernel
  312. def wrapper_uhpath_do_naive(k_func, itr):
  313. i = itr[0]
  314. j = itr[1]
  315. return i, j, _untilhpathkernel_do_naive(G_plist[i], G_plist[j], k_func)
  316. # @todo: (can be removed maybe) this method find paths repetively, it could be faster.
  317. def find_all_paths_until_length(G,
  318. length,
  319. ds_attrs,
  320. node_label='atom',
  321. edge_label='bond_type'):
  322. """Find all paths no longer than a certain maximum length in a graph. A
  323. recursive depth first search is applied.
  324. Parameters
  325. ----------
  326. G : NetworkX graphs
  327. The graph in which paths are searched.
  328. length : integer
  329. The maximum length of paths.
  330. ds_attrs: dict
  331. Dataset attributes.
  332. node_label : string
  333. Node attribute used as label. The default node label is atom.
  334. edge_label : string
  335. Edge attribute used as label. The default edge label is bond_type.
  336. Return
  337. ------
  338. path : list
  339. List of paths retrieved, where for unlabeled graphs, each path is
  340. represented by a list of nodes; while for labeled graphs, each path is
  341. represented by a list of strings consists of labels of nodes and/or
  342. edges on that path.
  343. """
  344. # path_l = [tuple([n]) for n in G.nodes] # paths of length l
  345. # all_paths = path_l[:]
  346. # for l in range(1, length + 1):
  347. # path_l_new = []
  348. # for path in path_l:
  349. # for neighbor in G[path[-1]]:
  350. # if len(path) < 2 or neighbor != path[-2]:
  351. # tmp = path + (neighbor, )
  352. # if tuple(tmp[::-1]) not in path_l_new:
  353. # path_l_new.append(tuple(tmp))
  354. # all_paths += path_l_new
  355. # path_l = path_l_new[:]
  356. path_l = [[n] for n in G.nodes] # paths of length l
  357. all_paths = path_l[:]
  358. for l in range(1, length + 1):
  359. path_lplus1 = []
  360. for path in path_l:
  361. for neighbor in G[path[-1]]:
  362. if neighbor not in path:
  363. tmp = path + [neighbor]
  364. # if tmp[::-1] not in path_lplus1:
  365. path_lplus1.append(tmp)
  366. all_paths += path_lplus1
  367. path_l = path_lplus1[:]
  368. # for i in range(0, length + 1):
  369. # new_paths = find_all_paths(G, i)
  370. # if new_paths == []:
  371. # break
  372. # all_paths.extend(new_paths)
  373. # consider labels
  374. # print(paths2labelseqs(all_paths, G, ds_attrs, node_label, edge_label))
  375. return paths2labelseqs(all_paths, G, ds_attrs, node_label, edge_label)
  376. def wrapper_find_all_paths_until_length(length, ds_attrs, node_label,
  377. edge_label, itr_item):
  378. g = itr_item[0]
  379. i = itr_item[1]
  380. return i, find_all_paths_until_length(g, length, ds_attrs,
  381. node_label=node_label, edge_label=edge_label)
  382. def find_all_path_as_trie(G,
  383. length,
  384. ds_attrs,
  385. node_label='atom',
  386. edge_label='bond_type'):
  387. # time1 = time.time()
  388. # all_path = find_all_paths_until_length(G, length, ds_attrs,
  389. # node_label=node_label,
  390. # edge_label=edge_label)
  391. # ptrie = Trie()
  392. # for path in all_path:
  393. # ptrie.insertWord(path)
  394. # ptrie = Trie()
  395. # path_l = [[n] for n in G.nodes] # paths of length l
  396. # path_l_str = paths2labelseqs(path_l, G, ds_attrs, node_label, edge_label)
  397. # for p in path_l_str:
  398. # ptrie.insertWord(p)
  399. # for l in range(1, length + 1):
  400. # path_lplus1 = []
  401. # for path in path_l:
  402. # for neighbor in G[path[-1]]:
  403. # if neighbor not in path:
  404. # tmp = path + [neighbor]
  405. ## if tmp[::-1] not in path_lplus1:
  406. # path_lplus1.append(tmp)
  407. # path_l = path_lplus1[:]
  408. # # consider labels
  409. # path_l_str = paths2labelseqs(path_l, G, ds_attrs, node_label, edge_label)
  410. # for p in path_l_str:
  411. # ptrie.insertWord(p)
  412. #
  413. # print(time.time() - time1)
  414. # print(ptrie.root)
  415. # print()
  416. # traverse all paths up to length h in a graph and construct a trie with
  417. # them. Deep-first search is applied. Notice the reverse of each path is
  418. # also stored to the trie.
  419. def traverseGraph(root, ptrie, length, G, ds_attrs, node_label, edge_label,
  420. pcurrent=[]):
  421. if len(pcurrent) < length + 1:
  422. for neighbor in G[root]:
  423. if neighbor not in pcurrent:
  424. pcurrent.append(neighbor)
  425. plstr = paths2labelseqs([pcurrent], G, ds_attrs,
  426. node_label, edge_label)
  427. ptrie.insertWord(plstr[0])
  428. traverseGraph(neighbor, ptrie, length, G, ds_attrs,
  429. node_label, edge_label, pcurrent)
  430. del pcurrent[-1]
  431. ptrie = Trie()
  432. path_l = [[n] for n in G.nodes] # paths of length l
  433. path_l_str = paths2labelseqs(path_l, G, ds_attrs, node_label, edge_label)
  434. for p in path_l_str:
  435. ptrie.insertWord(p)
  436. for n in G.nodes:
  437. traverseGraph(n, ptrie, length, G, ds_attrs, node_label, edge_label,
  438. pcurrent=[n])
  439. # def traverseGraph(root, all_paths, length, G, ds_attrs, node_label, edge_label,
  440. # pcurrent=[]):
  441. # if len(pcurrent) < length + 1:
  442. # for neighbor in G[root]:
  443. # if neighbor not in pcurrent:
  444. # pcurrent.append(neighbor)
  445. # plstr = paths2labelseqs([pcurrent], G, ds_attrs,
  446. # node_label, edge_label)
  447. # all_paths.append(pcurrent[:])
  448. # traverseGraph(neighbor, all_paths, length, G, ds_attrs,
  449. # node_label, edge_label, pcurrent)
  450. # del pcurrent[-1]
  451. #
  452. #
  453. # path_l = [[n] for n in G.nodes] # paths of length l
  454. # all_paths = path_l[:]
  455. # path_l_str = paths2labelseqs(path_l, G, ds_attrs, node_label, edge_label)
  456. ## for p in path_l_str:
  457. ## ptrie.insertWord(p)
  458. # for n in G.nodes:
  459. # traverseGraph(n, all_paths, length, G, ds_attrs, node_label, edge_label,
  460. # pcurrent=[n])
  461. # print(ptrie.root)
  462. return ptrie
  463. def wrapper_find_all_path_as_trie(length, ds_attrs, node_label,
  464. edge_label, itr_item):
  465. g = itr_item[0]
  466. i = itr_item[1]
  467. return i, find_all_path_as_trie(g, length, ds_attrs,
  468. node_label=node_label, edge_label=edge_label)
  469. def paths2labelseqs(plist, G, ds_attrs, node_label, edge_label):
  470. if ds_attrs['node_labeled']:
  471. if ds_attrs['edge_labeled']:
  472. path_strs = [
  473. tuple(
  474. list(
  475. chain.from_iterable(
  476. (G.node[node][node_label],
  477. G[node][path[idx + 1]][edge_label])
  478. for idx, node in enumerate(path[:-1]))) +
  479. [G.node[path[-1]][node_label]]) for path in plist
  480. ]
  481. # path_strs = []
  482. # for path in all_paths:
  483. # strlist = list(
  484. # chain.from_iterable((G.node[node][node_label],
  485. # G[node][path[idx + 1]][edge_label])
  486. # for idx, node in enumerate(path[:-1])))
  487. # strlist.append(G.node[path[-1]][node_label])
  488. # path_strs.append(tuple(strlist))
  489. else:
  490. path_strs = [
  491. tuple([G.node[node][node_label] for node in path])
  492. for path in plist
  493. ]
  494. return path_strs
  495. else:
  496. if ds_attrs['edge_labeled']:
  497. return [
  498. tuple([] if len(path) == 1 else [
  499. G[node][path[idx + 1]][edge_label]
  500. for idx, node in enumerate(path[:-1])
  501. ]) for path in plist
  502. ]
  503. else:
  504. return [tuple(['0' for node in path]) for path in plist]
  505. # return [tuple([len(path)]) for path in all_paths]
  506. #
  507. #def paths2GSuffixTree(paths):
  508. # return Tree(paths, builder=ukkonen.Builder)
  509. # def find_paths(G, source_node, length):
  510. # """Find all paths no longer than a certain length those start from a source node. A recursive depth first search is applied.
  511. # Parameters
  512. # ----------
  513. # G : NetworkX graphs
  514. # The graph in which paths are searched.
  515. # source_node : integer
  516. # The number of the node from where all paths start.
  517. # length : integer
  518. # The length of paths.
  519. # Return
  520. # ------
  521. # path : list of list
  522. # List of paths retrieved, where each path is represented by a list of nodes.
  523. # """
  524. # return [[source_node]] if length == 0 else \
  525. # [[source_node] + path for neighbor in G[source_node]
  526. # for path in find_paths(G, neighbor, length - 1) if source_node not in path]
  527. # def find_all_paths(G, length):
  528. # """Find all paths with a certain length in a graph. A recursive depth first search is applied.
  529. # Parameters
  530. # ----------
  531. # G : NetworkX graphs
  532. # The graph in which paths are searched.
  533. # length : integer
  534. # The length of paths.
  535. # Return
  536. # ------
  537. # path : list of list
  538. # List of paths retrieved, where each path is represented by a list of nodes.
  539. # """
  540. # all_paths = []
  541. # for node in G:
  542. # all_paths.extend(find_paths(G, node, length))
  543. # # The following process is not carried out according to the original article
  544. # # all_paths_r = [ path[::-1] for path in all_paths ]
  545. # # # For each path, two presentation are retrieved from its two extremities. Remove one of them.
  546. # # for idx, path in enumerate(all_paths[:-1]):
  547. # # for path2 in all_paths_r[idx+1::]:
  548. # # if path == path2:
  549. # # all_paths[idx] = []
  550. # # break
  551. # # return list(filter(lambda a: a != [], all_paths))
  552. # return all_paths

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