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.

pathKernel.py 3.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import sys
  2. import pathlib
  3. sys.path.insert(0, "../")
  4. import networkx as nx
  5. import numpy as np
  6. import time
  7. from pygraph.kernels.deltaKernel import deltakernel
  8. def pathkernel(*args):
  9. """Calculate mean average path kernels between graphs.
  10. Parameters
  11. ----------
  12. Gn : List of NetworkX graph
  13. List of graphs between which the kernels are calculated.
  14. /
  15. G1, G2 : NetworkX graphs
  16. 2 graphs between which the kernel is calculated.
  17. Return
  18. ------
  19. Kmatrix/Kernel : Numpy matrix/int
  20. Kernel matrix, each element of which is the path kernel between 2 praphs. / Path Kernel between 2 graphs.
  21. References
  22. ----------
  23. [1] Suard F, Rakotomamonjy A, Bensrhair A. Kernel on Bag of Paths For Measuring Similarity of Shapes. InESANN 2007 Apr 25 (pp. 355-360).
  24. """
  25. if len(args) == 1: # for a list of graphs
  26. Gn = args[0]
  27. Kmatrix = np.zeros((len(Gn), len(Gn)))
  28. start_time = time.time()
  29. for i in range(0, len(Gn)):
  30. for j in range(i, len(Gn)):
  31. Kmatrix[i][j] = _pathkernel_do(Gn[i], Gn[j])
  32. Kmatrix[j][i] = Kmatrix[i][j]
  33. print("\n --- mean average path kernel matrix of size %d built in %s seconds ---" % (len(Gn), (time.time() - start_time)))
  34. return Kmatrix
  35. else: # for only 2 graphs
  36. start_time = time.time()
  37. kernel = _pathkernel_do(args[0], args[1])
  38. print("\n --- mean average path kernel built in %s seconds ---" % (time.time() - start_time))
  39. return kernel
  40. def _pathkernel_do(G1, G2):
  41. """Calculate mean average path kernels between 2 graphs.
  42. Parameters
  43. ----------
  44. G1, G2 : NetworkX graphs
  45. 2 graphs between which the kernel is calculated.
  46. Return
  47. ------
  48. Kernel : int
  49. Path Kernel between 2 graphs.
  50. """
  51. # calculate shortest paths for both graphs
  52. sp1 = []
  53. num_nodes = G1.number_of_nodes()
  54. for node1 in range(num_nodes):
  55. for node2 in range(node1 + 1, num_nodes):
  56. sp1.append(nx.shortest_path(G1, node1, node2, weight = 'cost'))
  57. sp2 = []
  58. num_nodes = G2.number_of_nodes()
  59. for node1 in range(num_nodes):
  60. for node2 in range(node1 + 1, num_nodes):
  61. sp2.append(nx.shortest_path(G2, node1, node2, weight = 'cost'))
  62. # calculate kernel
  63. kernel = 0
  64. for path1 in sp1:
  65. for path2 in sp2:
  66. if len(path1) == len(path2):
  67. kernel_path = deltakernel(G1.node[path1[0]]['label'] == G2.node[path2[0]]['label'])
  68. if kernel_path:
  69. for i in range(1, len(path1)):
  70. # kernel = 1 if all corresponding nodes and edges in the 2 paths have same labels, otherwise 0
  71. kernel_path *= deltakernel(G1[path1[i - 1]][path1[i]]['label'] == G2[path2[i - 1]][path2[i]]['label']) * deltakernel(G1.node[path1[i]]['label'] == G2.node[path2[i]]['label'])
  72. kernel += kernel_path # add up kernels of all paths
  73. kernel = kernel / (len(sp1) * len(sp2)) # calculate mean average
  74. return kernel

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