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 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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=None):
  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.
  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=None):
  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 transformed 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
  98. def direct_product(G1, G2, node_label, edge_label):
  99. """Return the direct/tensor product of G1 and G2.
  100. Parameters
  101. ----------
  102. G1, G2 : NetworkX graph
  103. The original graphs.
  104. node_label : string
  105. node attribute used as label. The default node label is 'atom'.
  106. edge_label : string
  107. edge attribute used as label. The default edge label is 'bond_type'.
  108. Return
  109. ------
  110. gt : NetworkX graph
  111. The direct product graph of G1 and G2.
  112. Notes
  113. -----
  114. This method differs from networkx.tensor_product in that this method only adds nodes and edges in G1 and G2 that have the same labels to direct product graph.
  115. References
  116. ----------
  117. [1] Thomas Gärtner, Peter Flach, and Stefan Wrobel. On graph kernels: Hardness results and efficient alternatives. Learning Theory and Kernel Machines, pages 129–143, 2003.
  118. """
  119. # arrange all graphs in a list
  120. from itertools import product
  121. # G = G.to_directed()
  122. gt = nx.Graph()
  123. # add nodes
  124. for u, v in product(G1, G2):
  125. if G1.nodes[u][node_label] == G2.nodes[v][node_label]:
  126. gt.add_node((u, v))
  127. gt.nodes[(u, v)].update({node_label: G1.nodes[u][node_label]})
  128. # add edges
  129. for u, v in product(gt, gt):
  130. if (u[0], v[0]) in G1.edges and (
  131. u[1], v[1]
  132. ) in G2.edges and G1.edges[u[0],
  133. v[0]][edge_label] == G2.edges[u[1],
  134. v[1]][edge_label]:
  135. gt.add_edge((u[0], u[1]), (v[0], v[1]))
  136. gt.edges[(u[0], u[1]), (v[0], v[1])].update({
  137. edge_label:
  138. G1.edges[u[0], v[0]][edge_label]
  139. })
  140. # relabel nodes using consecutive integers for convenience of kernel calculation.
  141. # gt = nx.convert_node_labels_to_integers(
  142. # gt, first_label=0, label_attribute='label_orignal')
  143. return gt

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