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_ged_env.py 1.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """Tests of GEDEnv.
  2. """
  3. def test_GEDEnv():
  4. """Test GEDEnv.
  5. """
  6. """**1. Get dataset.**"""
  7. from gklearn.utils import Dataset
  8. # Predefined dataset name, use dataset "MUTAG".
  9. ds_name = 'MUTAG'
  10. # Initialize a Dataset.
  11. dataset = Dataset()
  12. # Load predefined dataset "MUTAG".
  13. dataset.load_predefined_dataset(ds_name)
  14. graph1 = dataset.graphs[0]
  15. graph2 = dataset.graphs[1]
  16. """**2. Compute graph edit distance.**"""
  17. try:
  18. from gklearn.ged.env import GEDEnv
  19. ged_env = GEDEnv() # initailize GED environment.
  20. ged_env.set_edit_cost('CONSTANT', # GED cost type.
  21. edit_cost_constants=[3, 3, 1, 3, 3, 1] # edit costs.
  22. )
  23. ged_env.add_nx_graph(graph1, '') # add graph1
  24. ged_env.add_nx_graph(graph2, '') # add graph2
  25. listID = ged_env.get_all_graph_ids() # get list IDs of graphs
  26. ged_env.init(init_type='LAZY_WITHOUT_SHUFFLED_COPIES') # initialize GED environment.
  27. options = {'initialization_method': 'RANDOM', # or 'NODE', etc.
  28. 'threads': 1 # parallel threads.
  29. }
  30. ged_env.set_method('BIPARTITE', # GED method.
  31. options # options for GED method.
  32. )
  33. ged_env.init_method() # initialize GED method.
  34. ged_env.run_method(listID[0], listID[1]) # run.
  35. pi_forward = ged_env.get_forward_map(listID[0], listID[1]) # forward map.
  36. pi_backward = ged_env.get_backward_map(listID[0], listID[1]) # backward map.
  37. dis = ged_env.get_upper_bound(listID[0], listID[1]) # GED bewteen two graphs.
  38. import networkx as nx
  39. assert len(pi_forward) == nx.number_of_nodes(graph1), len(pi_backward) == nx.number_of_nodes(graph2)
  40. except Exception as exception:
  41. assert False, exception
  42. if __name__ == "__main__":
  43. test_GEDEnv()

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