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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, edge_weight = 'bond_type'):
  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. edge_weight : string
  18. edge attribute corresponding to the edge weight. The default edge weight is bond_type.
  19. Return
  20. ------
  21. Kmatrix/Kernel : Numpy matrix/int
  22. Kernel matrix, each element of which is the sp kernel between 2 praphs. / SP Kernel between 2 graphs.
  23. References
  24. ----------
  25. [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
  26. """
  27. if len(args) == 1: # for a list of graphs
  28. Gn = args[0]
  29. Kmatrix = np.zeros((len(Gn), len(Gn)))
  30. Sn = [] # get shortest path graphs of Gn
  31. for i in range(0, len(Gn)):
  32. Sn.append(getSPGraph(Gn[i], edge_weight = edge_weight))
  33. start_time = time.time()
  34. for i in range(0, len(Gn)):
  35. for j in range(i, len(Gn)):
  36. for e1 in Sn[i].edges(data = True):
  37. for e2 in Sn[j].edges(data = True):
  38. 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])):
  39. Kmatrix[i][j] += 1
  40. Kmatrix[j][i] += (0 if i == j else 1)
  41. run_time = time.time() - start_time
  42. print("--- shortest path kernel matrix of size %d built in %s seconds ---" % (len(Gn), run_time))
  43. return Kmatrix, run_time
  44. else: # for only 2 graphs
  45. G1 = getSPGraph(args[0], edge_weight = edge_weight)
  46. G2 = getSPGraph(args[1], edge_weight = edge_weight)
  47. kernel = 0
  48. start_time = time.time()
  49. for e1 in G1.edges(data = True):
  50. for e2 in G2.edges(data = True):
  51. 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])):
  52. kernel += 1
  53. # print("--- shortest path kernel built in %s seconds ---" % (time.time() - start_time))
  54. return kernel

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