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.

test_fitDistance.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Oct 24 11:50:56 2019
  5. @author: ljia
  6. """
  7. from matplotlib import pyplot as plt
  8. import numpy as np
  9. from pygraph.utils.graphfiles import loadDataset
  10. from utils import remove_edges
  11. from fitDistance import fit_GED_to_kernel_distance
  12. from utils import normalize_distance_matrix
  13. def test_anycosts():
  14. ds = {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt',
  15. 'extra_params': {}} # node/edge symb
  16. Gn, y_all = loadDataset(ds['dataset'], extra_params=ds['extra_params'])
  17. # Gn = Gn[0:10]
  18. remove_edges(Gn)
  19. gkernel = 'marginalizedkernel'
  20. itr_max = 10
  21. edit_costs, residual_list, edit_cost_list, dis_k_mat, ged_mat, time_list = \
  22. fit_GED_to_kernel_distance(Gn, gkernel, itr_max)
  23. total_time = np.sum(time_list)
  24. print('\nedit_costs:', edit_costs)
  25. print('\nresidual_list:', residual_list)
  26. print('\nedit_cost_list:', edit_cost_list)
  27. print('\ndistance matrix in kernel space:', dis_k_mat)
  28. print('\nged matrix:', ged_mat)
  29. print('total time:', total_time)
  30. np.savez('results/fit_distance.any_costs.gm', edit_costs=edit_costs,
  31. residual_list=residual_list, edit_cost_list=edit_cost_list,
  32. dis_k_mat=dis_k_mat, ged_mat=ged_mat, time_list=time_list,
  33. total_time=total_time)
  34. # normalized distance matrices.
  35. # gmfile = np.load('results/fit_distance.any_costs.gm.npz')
  36. # edit_costs = gmfile['edit_costs']
  37. # residual_list = gmfile['residual_list']
  38. # edit_cost_list = gmfile['edit_cost_list']
  39. # dis_k_mat = gmfile['dis_k_mat']
  40. # ged_mat = gmfile['ged_mat']
  41. # total_time = gmfile['total_time']
  42. norm_dis_k_mat = normalize_distance_matrix(dis_k_mat)
  43. plt.imshow(norm_dis_k_mat)
  44. plt.colorbar()
  45. plt.savefig('results/norm_dis_k_mat.any_costs' + '.eps', format='eps', dpi=300)
  46. # plt.savefig('results/norm_dis_k_mat.any_costs' + '.jpg', format='jpg')
  47. # plt.show()
  48. plt.clf()
  49. norm_ged_mat = normalize_distance_matrix(ged_mat)
  50. plt.imshow(norm_ged_mat)
  51. plt.colorbar()
  52. plt.savefig('results/norm_ged_mat.any_costs' + '.eps', format='eps', dpi=300)
  53. # plt.savefig('results/norm_ged_mat.any_costs' + '.jpg', format='jpg')
  54. # plt.show()
  55. plt.clf()
  56. def test_cs_leq_ci_plus_cr():
  57. """c_vs <= c_vi + c_vr, c_es <= c_ei + c_er
  58. """
  59. ds = {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt',
  60. 'extra_params': {}} # node/edge symb
  61. Gn, y_all = loadDataset(ds['dataset'], extra_params=ds['extra_params'])
  62. # Gn = Gn[0:10]
  63. remove_edges(Gn)
  64. gkernel = 'marginalizedkernel'
  65. itr_max = 10
  66. edit_costs, residual_list, edit_cost_list, dis_k_mat, ged_mat, time_list = \
  67. fit_GED_to_kernel_distance(Gn, gkernel, itr_max)
  68. total_time = np.sum(time_list)
  69. print('\nedit_costs:', edit_costs)
  70. print('\nresidual_list:', residual_list)
  71. print('\nedit_cost_list:', edit_cost_list)
  72. print('\ndistance matrix in kernel space:', dis_k_mat)
  73. print('\nged matrix:', ged_mat)
  74. print('total time:', total_time)
  75. np.savez('results/fit_distance.cs_leq_ci_plus_cr.gm', edit_costs=edit_costs,
  76. residual_list=residual_list, edit_cost_list=edit_cost_list,
  77. dis_k_mat=dis_k_mat, ged_mat=ged_mat, time_list=time_list,
  78. total_time=total_time)
  79. # normalized distance matrices.
  80. # gmfile = np.load('results/fit_distance.cs_leq_ci_plus_cr.gm.npz')
  81. # edit_costs = gmfile['edit_costs']
  82. # residual_list = gmfile['residual_list']
  83. # edit_cost_list = gmfile['edit_cost_list']
  84. # dis_k_mat = gmfile['dis_k_mat']
  85. # ged_mat = gmfile['ged_mat']
  86. # total_time = gmfile['total_time']
  87. norm_dis_k_mat = normalize_distance_matrix(dis_k_mat)
  88. plt.imshow(norm_dis_k_mat)
  89. plt.colorbar()
  90. plt.savefig('results/norm_dis_k_mat.cs_leq_ci_plus_cr' + '.eps', format='eps', dpi=300)
  91. # plt.savefig('results/norm_dis_k_mat.cs_leq_ci_plus_cr' + '.jpg', format='jpg')
  92. # plt.show()
  93. plt.clf()
  94. norm_ged_mat = normalize_distance_matrix(ged_mat)
  95. plt.imshow(norm_ged_mat)
  96. plt.colorbar()
  97. plt.savefig('results/norm_ged_mat.cs_leq_ci_plus_cr' + '.eps', format='eps', dpi=300)
  98. # plt.savefig('results/norm_ged_mat.cs_leq_ci_plus_cr' + '.jpg', format='jpg')
  99. # plt.show()
  100. plt.clf()
  101. if __name__ == '__main__':
  102. test_anycosts()
  103. test_cs_leq_ci_plus_cr()

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