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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 + 1, G.number_of_nodes()):
  52. if spMatrix[i, j] != np.inf:
  53. S.add_edge(i, j, cost=spMatrix[i, j])
  54. return S
  55. def untotterTransformation(G, node_label, edge_label):
  56. """Transform graph G according to Mahé et al.'s work to filter out tottering patterns of marginalized kernel and tree pattern kernel.
  57. Parameters
  58. ----------
  59. G : NetworkX graph
  60. The graph to be tramsformed.
  61. node_label : string
  62. node attribute used as label. The default node label is 'atom'.
  63. edge_label : string
  64. edge attribute used as label. The default edge label is 'bond_type'.
  65. Return
  66. ------
  67. gt : NetworkX graph
  68. The transformed graph corresponding to G.
  69. References
  70. ----------
  71. [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.
  72. """
  73. # arrange all graphs in a list
  74. G = G.to_directed()
  75. gt = nx.Graph()
  76. gt.graph = G.graph
  77. gt.add_nodes_from(G.nodes(data=True))
  78. for edge in G.edges():
  79. gt.add_node(edge)
  80. gt.node[edge].update({node_label: G.node[edge[1]][node_label]})
  81. gt.add_edge(edge[0], edge)
  82. gt.edges[edge[0], edge].update({
  83. edge_label:
  84. G[edge[0]][edge[1]][edge_label]
  85. })
  86. for neighbor in G[edge[1]]:
  87. if neighbor != edge[0]:
  88. gt.add_edge(edge, (edge[1], neighbor))
  89. gt.edges[edge, (edge[1], neighbor)].update({
  90. edge_label:
  91. G[edge[1]][neighbor][edge_label]
  92. })
  93. # nx.draw_networkx(gt)
  94. # plt.show()
  95. # relabel nodes using consecutive integers for convenience of kernel calculation.
  96. gt = nx.convert_node_labels_to_integers(
  97. gt, first_label=0, label_attribute='label_orignal')
  98. return gt
  99. def direct_product(G1, G2, node_label, edge_label):
  100. """Return the direct/tensor product of directed graphs G1 and G2.
  101. Parameters
  102. ----------
  103. G1, G2 : NetworkX graph
  104. The original graphs.
  105. node_label : string
  106. node attribute used as label. The default node label is 'atom'.
  107. edge_label : string
  108. edge attribute used as label. The default edge label is 'bond_type'.
  109. Return
  110. ------
  111. gt : NetworkX graph
  112. The direct product graph of G1 and G2.
  113. Notes
  114. -----
  115. 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 the direct product graph.
  116. References
  117. ----------
  118. [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.
  119. """
  120. # arrange all graphs in a list
  121. from itertools import product
  122. # G = G.to_directed()
  123. gt = nx.DiGraph()
  124. # add nodes
  125. for u, v in product(G1, G2):
  126. if G1.nodes[u][node_label] == G2.nodes[v][node_label]:
  127. gt.add_node((u, v))
  128. gt.nodes[(u, v)].update({node_label: G1.nodes[u][node_label]})
  129. # add edges, faster for sparse graphs (no so many edges), which is the most case for now.
  130. for (u1, v1), (u2, v2) in product(G1.edges, G2.edges):
  131. if (u1, u2) in gt and (
  132. v1, v2
  133. ) in gt and G1.edges[u1, v1][edge_label] == G2.edges[u2,
  134. v2][edge_label]:
  135. gt.add_edge((u1, u2), (v1, v2))
  136. gt.edges[(u1, u2), (v1, v2)].update({
  137. edge_label:
  138. G1.edges[u1, v1][edge_label]
  139. })
  140. # # add edges, faster for dense graphs (a lot of edges, complete graph would be super).
  141. # for u, v in product(gt, gt):
  142. # if (u[0], v[0]) in G1.edges and (
  143. # u[1], v[1]
  144. # ) in G2.edges and G1.edges[u[0],
  145. # v[0]][edge_label] == G2.edges[u[1],
  146. # v[1]][edge_label]:
  147. # gt.add_edge((u[0], u[1]), (v[0], v[1]))
  148. # gt.edges[(u[0], u[1]), (v[0], v[1])].update({
  149. # edge_label:
  150. # G1.edges[u[0], v[0]][edge_label]
  151. # })
  152. # relabel nodes using consecutive integers for convenience of kernel calculation.
  153. # gt = nx.convert_node_labels_to_integers(
  154. # gt, first_label=0, label_attribute='label_orignal')
  155. return gt

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