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.

ged_com.py 3.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu May 5 14:02:17 2022
  5. @author: ljia
  6. """
  7. import sys
  8. from gklearn.ged.model.distances import euclid_d
  9. from gklearn.ged.util import pairwise_ged, get_nb_edit_operations
  10. from gklearn.utils import get_iters
  11. def compute_ged(Gi, Gj, edit_cost, method='BIPARTITE', **kwargs):
  12. """
  13. Compute GED between two graph according to edit_cost
  14. """
  15. ged_options = {'edit_cost': 'CONSTANT',
  16. 'method': method,
  17. 'edit_cost_constants': edit_cost}
  18. node_labels = kwargs.get('node_labels', [])
  19. edge_labels = kwargs.get('edge_labels', [])
  20. dis, pi_forward, pi_backward = pairwise_ged(Gi, Gj, ged_options, repeats=10)
  21. n_eo_tmp = get_nb_edit_operations(Gi, Gj, pi_forward, pi_backward, edit_cost='CONSTANT', node_labels=node_labels, edge_labels=edge_labels)
  22. return dis, n_eo_tmp
  23. def compute_ged_all_dataset(Gn, edit_cost, ed_method, **kwargs):
  24. N = len(Gn)
  25. G_pairs = []
  26. for i in range(N):
  27. for j in range(i, N):
  28. G_pairs.append([i, j])
  29. return compute_geds(G_pairs, Gn, edit_cost, ed_method, **kwargs)
  30. def compute_geds(G_pairs, Gn, edit_cost, ed_method, verbose=True, **kwargs):
  31. """
  32. Compute GED between all indexes in G_pairs given edit_cost
  33. :return: ged_vec : the list of computed distances, n_edit_operations : the list of edit operations
  34. """
  35. ged_vec = []
  36. n_edit_operations = []
  37. for k in get_iters(range(len(G_pairs)), desc='Computing GED', file=sys.stdout, length=len(G_pairs), verbose=verbose):
  38. [i, j] = G_pairs[k]
  39. dis, n_eo_tmp = compute_ged(
  40. Gn[i], Gn[j], edit_cost=edit_cost, method=ed_method, **kwargs)
  41. ged_vec.append(dis)
  42. n_edit_operations.append(n_eo_tmp)
  43. return ged_vec, n_edit_operations
  44. def compute_D(G_app, edit_cost, G_test=None, ed_method='BIPARTITE', **kwargs):
  45. import numpy as np
  46. N = len(G_app)
  47. D_app = np.zeros((N, N))
  48. for i, G1 in get_iters(enumerate(G_app), desc='Computing D - app', file=sys.stdout, length=N):
  49. for j, G2 in enumerate(G_app[i+1:], i+1):
  50. D_app[i, j], _ = compute_ged(G1, G2, edit_cost, method=ed_method, **kwargs)
  51. D_app[j, i] = D_app[i, j]
  52. if (G_test is None):
  53. return D_app, edit_cost
  54. else:
  55. D_test = np.zeros((len(G_test), N))
  56. for i, G1 in get_iters(enumerate(G_test), desc='Computing D - test', file=sys.stdout, length=len(G_test)):
  57. for j, G2 in enumerate(G_app):
  58. D_test[i, j], _ = compute_ged(G1, G2, edit_cost, method=ed_method, **kwargs)
  59. return D_app, D_test, edit_cost
  60. def compute_D_random(G_app, G_test=None, ed_method='BIPARTITE', **kwargs):
  61. import numpy as np
  62. edit_costs = np.random.rand(6)
  63. return compute_D(G_app, edit_costs, G_test, ed_method=ed_method, **kwargs)
  64. def compute_D_expert(G_app, G_test=None, ed_method='BIPARTITE', **kwargs):
  65. edit_cost = [3, 3, 1, 3, 3, 1]
  66. return compute_D(G_app, edit_cost, G_test, ed_method=ed_method, **kwargs)
  67. def compute_D_fitted(G_app, y_app, G_test=None, y_distance=euclid_d,
  68. mode='reg', unlabeled=False, ed_method='BIPARTITE', **kwargs):
  69. from gklearn.ged.models.optim_costs import compute_optimal_costs
  70. costs_optim = compute_optimal_costs(
  71. G_app, y_app, y_distance=y_distance,
  72. mode=mode, unlabeled=unlabeled, ed_method=ed_method, **kwargs)
  73. return compute_D(G_app, costs_optim, G_test, ed_method=ed_method, **kwargs)
  74. def compute_D_GH2020(G_app, G_test=None, ed_method='BIPARTITE', **kwargs):
  75. from gklearn.ged.optim_costs import get_optimal_costs_GH2020
  76. costs_optim = get_optimal_costs_GH2020(**kwargs)
  77. return compute_D(G_app, costs_optim, G_test, ed_method=ed_method, **kwargs)

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