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.

spkernel.py 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.utils.utils import getSPGraph
  8. def spkernel(*args):
  9. """Calculate shortest-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 sp kernel between 2 praphs. / SP Kernel between 2 graphs.
  21. References
  22. ----------
  23. [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
  24. """
  25. if len(args) == 1: # for a list of graphs
  26. Gn = args[0]
  27. Kmatrix = np.zeros((len(Gn), len(Gn)))
  28. Sn = [] # get shortest path graphs of Gn
  29. for i in range(0, len(Gn)):
  30. Sn.append(getSPGraph(Gn[i]))
  31. start_time = time.time()
  32. for i in range(0, len(Gn)):
  33. for j in range(i, len(Gn)):
  34. for e1 in Sn[i].edges(data = True):
  35. for e2 in Sn[j].edges(data = True):
  36. if e1[2]['cost'] != 0 and e1[2]['cost'] == e2[2]['cost'] and ((e1[0] == e2[0] and e1[1] == e2[1]) or (e1[0] == e2[1] and e1[1] == e2[0])):
  37. Kmatrix[i][j] += 1
  38. Kmatrix[j][i] += (0 if i == j else 1)
  39. print("--- shortest path kernel matrix of size %d built in %s seconds ---" % (len(Gn), (time.time() - start_time)))
  40. return Kmatrix
  41. else: # for only 2 graphs
  42. G1 = getSPGraph(args[0])
  43. G2 = getSPGraph(args[1])
  44. kernel = 0
  45. start_time = time.time()
  46. for e1 in G1.edges(data = True):
  47. for e2 in G2.edges(data = True):
  48. if e1[2]['cost'] != 0 and e1[2]['cost'] == e2[2]['cost'] and ((e1[0] == e2[0] and e1[1] == e2[1]) or (e1[0] == e2[1] and e1[1] == e2[0])):
  49. kernel += 1
  50. # print("--- shortest path kernel built in %s seconds ---" % (time.time() - start_time))
  51. return kernel

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