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.

utils.py 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import networkx as nx
  2. import numpy as np
  3. # from tqdm import tqdm
  4. def getSPLengths(G1):
  5. sp = nx.shortest_path(G1)
  6. distances = np.zeros((G1.number_of_nodes(), G1.number_of_nodes()))
  7. for i in sp.keys():
  8. for j in sp[i].keys():
  9. distances[i, j] = len(sp[i][j]) - 1
  10. return distances
  11. def getSPGraph(G, edge_weight='bond_type'):
  12. """Transform graph G to its corresponding shortest-paths graph.
  13. Parameters
  14. ----------
  15. G : NetworkX graph
  16. The graph to be tramsformed.
  17. edge_weight : string
  18. edge attribute corresponding to the edge weight. The default edge weight is bond_type.
  19. Return
  20. ------
  21. S : NetworkX graph
  22. The shortest-paths graph corresponding to G.
  23. Notes
  24. ------
  25. For an input graph G, its corresponding shortest-paths graph S contains the same set of nodes as G, while there exists an edge between all nodes in S which are connected by a walk in G. Every edge in S between two nodes is labeled by the shortest distance between these two nodes.
  26. References
  27. ----------
  28. [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
  29. """
  30. return floydTransformation(G, edge_weight=edge_weight)
  31. def floydTransformation(G, edge_weight='bond_type'):
  32. """Transform graph G to its corresponding shortest-paths graph using Floyd-transformation.
  33. Parameters
  34. ----------
  35. G : NetworkX graph
  36. The graph to be tramsformed.
  37. edge_weight : string
  38. edge attribute corresponding to the edge weight. The default edge weight is bond_type.
  39. Return
  40. ------
  41. S : NetworkX graph
  42. The shortest-paths graph corresponding to G.
  43. References
  44. ----------
  45. [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
  46. """
  47. spMatrix = nx.floyd_warshall_numpy(G, weight=edge_weight)
  48. S = nx.Graph()
  49. S.add_nodes_from(G.nodes(data=True))
  50. for i in range(0, G.number_of_nodes()):
  51. for j in range(i, G.number_of_nodes()):
  52. S.add_edge(i, j, cost=spMatrix[i, j])
  53. return S
  54. def untotterTransformation(G, node_label, edge_label):
  55. """Transform graph G according to Mahé et al.'s work to filter out tottering patterns of marginalized kernel and tree pattern kernel.
  56. Parameters
  57. ----------
  58. G : NetworkX graph
  59. The graph to be tramsformed.
  60. node_label : string
  61. node attribute used as label. The default node label is 'atom'.
  62. edge_label : string
  63. edge attribute used as label. The default edge label is 'bond_type'.
  64. Return
  65. ------
  66. gt : NetworkX graph
  67. The shortest-paths graph corresponding to G.
  68. References
  69. ----------
  70. [1] Pierre Mahé, Nobuhisa Ueda, Tatsuya Akutsu, Jean-Luc Perret, and Jean-Philippe Vert. Extensions of marginalized graph kernels. In Proceedings of the twenty-first international conference on Machine learning, page 70. ACM, 2004.
  71. """
  72. # arrange all graphs in a list
  73. G = G.to_directed()
  74. gt = nx.Graph()
  75. gt.graph = G.graph
  76. gt.add_nodes_from(G.nodes(data=True))
  77. for edge in G.edges():
  78. gt.add_node(edge)
  79. gt.node[edge].update({node_label: G.node[edge[1]][node_label]})
  80. gt.add_edge(edge[0], edge)
  81. gt.edges[edge[0], edge].update({
  82. edge_label:
  83. G[edge[0]][edge[1]][edge_label]
  84. })
  85. for neighbor in G[edge[1]]:
  86. if neighbor != edge[0]:
  87. gt.add_edge(edge, (edge[1], neighbor))
  88. gt.edges[edge, (edge[1], neighbor)].update({
  89. edge_label:
  90. G[edge[1]][neighbor][edge_label]
  91. })
  92. # nx.draw_networkx(gt)
  93. # plt.show()
  94. # relabel nodes using consecutive integers for convenience of kernel calculation.
  95. gt = nx.convert_node_labels_to_integers(
  96. gt, first_label=0, label_attribute='label_orignal')
  97. return gt

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