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.

examples.rst 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. Examples
  2. ==============
  3. Before using each example, please make sure to put these lines on the beginnig of your code :
  4. .. code-block:: python
  5. import librariesImport
  6. import gedlibpy
  7. Use your path to access it, without changing the library architecture. After that, you are ready to use the library.
  8. When you want to make new computation, please use this function :
  9. .. code-block:: python
  10. gedlibpy.restart_env()
  11. All the graphs and results will be delete so make sure you don't need it.
  12. Classique case with GXL graphs
  13. ------------------------------------
  14. .. code-block:: python
  15. gedlibpy.load_GXL_graphs('include/gedlib-master/data/datasets/Mutagenicity/data/', 'collections/MUTA_10.xml')
  16. listID = gedlibpy.get_all_graph_ids()
  17. gedlibpy.set_edit_cost("CHEM_1")
  18. gedlibpy.init()
  19. gedlibpy.set_method("IPFP", "")
  20. gedlibpy.init_method()
  21. g = listID[0]
  22. h = listID[1]
  23. gedlibpy.run_method(g,h)
  24. print("Node Map : ", gedlibpy.get_node_map(g,h))
  25. print ("Upper Bound = " + str(gedlibpy.get_upper_bound(g,h)) + ", Lower Bound = " + str(gedlibpy.get_lower_bound(g,h)) + ", Runtime = " + str(gedlibpy.get_runtime(g,h)))
  26. You can also use this function :
  27. .. code-block:: python
  28. compute_edit_distance_on_GXl_graphs(path_folder, path_XML, edit_cost, method, options="", init_option = "EAGER_WITHOUT_SHUFFLED_COPIES")
  29. This function compute all edit distance between all graphs, even itself. You can see the result with some functions and graphs IDs. Please see the documentation of the function for more informations.
  30. Classique case with NX graphs
  31. ------------------------------------
  32. .. code-block:: python
  33. for graph in dataset :
  34. gedlibpy.add_nx_graph(graph, classe)
  35. listID = gedlibpy.get_all_graph_ids()
  36. gedlibpy.set_edit_cost("CHEM_1")
  37. gedlibpy.init()
  38. gedlibpy.set_method("IPFP", "")
  39. gedlibpy.init_method()
  40. g = listID[0]
  41. h = listID[1]
  42. gedlibpy.run_method(g,h)
  43. print("Node Map : ", gedlibpy.get_node_map(g,h))
  44. print ("Upper Bound = " + str(gedlibpy.get_upper_bound(g,h)) + ", Lower Bound = " + str(gedlibpy.get_lower_bound(g,h)) + ", Runtime = " + str(gedlibpy.get_runtime(g,h)))
  45. You can also use this function :
  46. .. code-block:: python
  47. compute_edit_distance_on_nx_graphs(dataset, classes, edit_cost, method, options, init_option = "EAGER_WITHOUT_SHUFFLED_COPIES")
  48. This function compute all edit distance between all graphs, even itself. You can see the result in the return and with some functions and graphs IDs. Please see the documentation of the function for more informations.
  49. Or this function :
  50. .. code-block:: python
  51. compute_ged_on_two_graphs(g1,g2, edit_cost, method, options, init_option = "EAGER_WITHOUT_SHUFFLED_COPIES")
  52. This function allow to compute the edit distance just for two graphs. Please see the documentation of the function for more informations.
  53. Add a graph from scratch
  54. ------------------------------------
  55. .. code-block:: python
  56. currentID = gedlibpy.add_graph()
  57. gedlibpy.add_node(currentID, "_1", {"chem" : "C"})
  58. gedlibpy.add_node(currentID, "_2", {"chem" : "O"})
  59. gedlibpy.add_edge(currentID,"_1", "_2", {"valence": "1"} )
  60. Please make sure as the type are the same (string for Ids and a dictionnary for labels). If you want a symmetrical graph, you can use this function to ensure the symmetry :
  61. .. code-block:: python
  62. add_symmetrical_edge(graph_id, tail, head, edge_label)
  63. If you have a Nx structure, you can use directly this function :
  64. .. code-block:: python
  65. add_nx_graph(g, classe, ignore_duplicates=True)
  66. Even if you have another structure, you can use this function :
  67. .. code-block:: python
  68. add_random_graph(name, classe, list_of_nodes, list_of_edges, ignore_duplicates=True)
  69. Please read the documentation before using and respect the types.
  70. Median computation
  71. ------------------------------------
  72. An example is available in the Median_Example folder. It contains the necessary to compute a median graph. You can launch xp-letter-gbr.py to compute median graph on all letters in the dataset, or median.py for le letter Z.
  73. To summarize the use, you can follow this example :
  74. .. code-block:: python
  75. import pygraph #Available with the median example
  76. from median import draw_Letter_graph, compute_median, compute_median_set
  77. gedlibpy.load_GXL_graphs('../include/gedlib-master/data/datasets/Letter/HIGH/', '../include/gedlib-master/data/collections/Letter_Z.xml')
  78. gedlibpy.set_edit_cost("LETTER")
  79. gedlibpy.init()
  80. gedlibpy.set_method("IPFP", "")
  81. gedlibpy.init_method()
  82. listID = gedlibpy.get_all_graph_ids()
  83. dataset,my_y = pygraph.utils.graphfiles.loadDataset("../include/gedlib-master/data/datasets/Letter/HIGH/Letter_Z.cxl")
  84. median, sod, sods_path,set_median = compute_median(gedlibpy,listID,dataset,verbose=True)
  85. draw_Letter_graph(median)
  86. Please use the function in the median.py code to simplify your use. You can adapt this example to your case. Also, some function in the PythonGedLib module can make the work easier. Ask Benoît Gauzere if you want more information.
  87. Hungarian algorithm
  88. ------------------------------------
  89. LSAPE
  90. ~~~~~~
  91. .. code-block:: python
  92. result = gedlibpy.hungarian_LSAPE(matrixCost)
  93. print("Rho = ", result[0], " Varrho = ", result[1], " u = ", result[2], " v = ", result[3])
  94. LSAP
  95. ~~~~~~
  96. .. code-block:: python
  97. result = gedlibpy.hungarian_LSAP(matrixCost)
  98. print("Rho = ", result[0], " Varrho = ", result[1], " u = ", result[2], " v = ", result[3])

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