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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. @author: linlin
  3. @references: Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
  4. """
  5. import sys
  6. import pathlib
  7. sys.path.insert(0, "../")
  8. import networkx as nx
  9. import numpy as np
  10. import time
  11. from pygraph.utils.utils import getSPGraph
  12. def spkernel(*args, edge_weight = 'bond_type'):
  13. """Calculate shortest-path kernels between graphs.
  14. Parameters
  15. ----------
  16. Gn : List of NetworkX graph
  17. List of graphs between which the kernels are calculated.
  18. /
  19. G1, G2 : NetworkX graphs
  20. 2 graphs between which the kernel is calculated.
  21. edge_weight : string
  22. edge attribute corresponding to the edge weight. The default edge weight is bond_type.
  23. Return
  24. ------
  25. Kmatrix/kernel : Numpy matrix/float
  26. Kernel matrix, each element of which is the sp kernel between 2 praphs. / SP kernel between 2 graphs.
  27. """
  28. Gn = args[0] if len(args) == 1 else [args[0], args[1]] # arrange all graphs in a list
  29. Kmatrix = np.zeros((len(Gn), len(Gn)))
  30. start_time = time.time()
  31. Gn = [ getSPGraph(G, edge_weight = edge_weight) for G in args[0] ] # get shortest path graphs of Gn
  32. for i in range(0, len(Gn)):
  33. for j in range(i, len(Gn)):
  34. # kernel_t = [ 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])) \
  35. # for e1 in Sn[i].edges(data = True) for e2 in Sn[j].edges(data = True) ]
  36. # Kmatrix[i][j] = np.sum(kernel_t)
  37. # Kmatrix[j][i] = Kmatrix[i][j]
  38. for e1 in Gn[i].edges(data = True):
  39. for e2 in Gn[j].edges(data = True):
  40. 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])):
  41. Kmatrix[i][j] += 1
  42. Kmatrix[j][i] = Kmatrix[i][j]
  43. run_time = time.time() - start_time
  44. print("--- shortest path kernel matrix of size %d built in %s seconds ---" % (len(Gn), run_time))
  45. return Kmatrix, run_time

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