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.

GedLibBind.hpp 21 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /****************************************************************************
  2. * *
  3. * Copyright (C) 2019-2020 by Natacha Lambert, David B. Blumenthal and *
  4. * Linlin Jia *
  5. * *
  6. * This file should be used by Python. *
  7. * Please call the Python module if you want to use GedLib with this code.*
  8. * *
  9. * Otherwise, you can directly use GedLib for C++. *
  10. * *
  11. ***************************************************************************/
  12. /*!
  13. * @file GedLibBind.hpp
  14. * @brief Classe and function declarations to call easly GebLib in Python without Gedlib's types
  15. */
  16. #ifndef GEDLIBBIND_HPP
  17. #define GEDLIBBIND_HPP
  18. //Include standard libraries.
  19. #include <string>
  20. #include <vector>
  21. #include <map>
  22. #include <list>
  23. #include <iostream>
  24. #include "../include/gedlib-master/src/env/ged_env.hpp"
  25. #include "../include/gedlib-master/src/env/node_map.hpp"
  26. /*!
  27. * @namespace pyged
  28. * @brief Global namespace for gedlibpy.
  29. */
  30. namespace pyged {
  31. /*!
  32. * @brief Get list of available edit cost functions readable by Python.
  33. */
  34. std::vector<std::string> getEditCostStringOptions();
  35. /*!
  36. * @brief Get list of available computation methods readable by Python.
  37. */
  38. std::vector<std::string> getMethodStringOptions();
  39. /*!
  40. * @brief Get list of available initilaization options readable by Python.
  41. */
  42. std::vector<std::string> getInitStringOptions();
  43. /*!
  44. * @brief Returns a dummy node.
  45. * @return ID of dummy node.
  46. */
  47. static std::size_t getDummyNode();
  48. /*!
  49. * @brief Provides the API of GEDLIB for Python.
  50. */
  51. class PyGEDEnv {
  52. public:
  53. /*!
  54. * @brief Constructor.
  55. */
  56. PyGEDEnv();
  57. // PyGEDEnv();
  58. /*!
  59. * @brief Destructor.
  60. */
  61. ~PyGEDEnv();
  62. /*!
  63. * @brief Tests if the environment is initialized or not.
  64. * @return Boolean @p true if the environment is initialized and @p false otherwise.
  65. */
  66. bool isInitialized();
  67. /*!
  68. * @brief Restart the environment (recall a new empty environment).
  69. */
  70. void restartEnv();
  71. /*!
  72. * @brief Loads graph given in the [GXL file format](http://www.gupro.de/GXL/).
  73. * @param[in] pathFolder The path to the directory containing the graphs.
  74. * @param[in] pathXML The path to a XML file thats lists the graphs contained in @p pathFolder that should be loaded.
  75. * @param[in] node_type Select if nodes are labeled or unlabeled.
  76. * @param[in] edge_type Select if edges are labeled or unlabeled.
  77. */
  78. void loadGXLGraph(const std::string & pathFolder, const std::string & pathXML, bool node_type, bool edge_type);
  79. /*!
  80. * @brief Provides access to the IDs of the graphs contained in the environment.
  81. * @return Pair <tt>(ID of first graphs, ID of last graph + 1)</tt> of graph IDs.
  82. * If both entries equal 0, the environment does not contain any graphs.
  83. */
  84. std::pair<std::size_t,std::size_t> getGraphIds() const;
  85. /*!
  86. * @brief Returns the list of graphs IDs which are loaded in the environment.
  87. * @return A vector which contains all the graphs Ids.
  88. */
  89. std::vector<std::size_t> getAllGraphIds();
  90. /*!
  91. * @brief Returns the graph class.
  92. * @param[in] id ID of an input graph that has been added to the environment.
  93. * @return Class of the input graph.
  94. */
  95. const std::string getGraphClass(std::size_t id) const;
  96. /*!
  97. * @brief Returns the graph name.
  98. * @param[in] id ID of an input graph that has been added to the environment.
  99. * @return Name of the input graph.
  100. */
  101. const std::string getGraphName(std::size_t id) const;
  102. /*!
  103. * @brief Adds a new uninitialized graph to the environment. Call initEnv() after calling this method.
  104. * @param[in] name The name of the added graph. Empty if not specified.
  105. * @param[in] class The class of the added graph. Empty if not specified.
  106. * @return The ID of the newly added graph.
  107. */
  108. std::size_t addGraph(const std::string & graph_name, const std::string & graph_class);
  109. /*!
  110. * @brief Adds a labeled node.
  111. * @param[in] graphId ID of graph that has been added to the environment.
  112. * @param[in] nodeId The user-specific ID of the vertex that has to be added.
  113. * @param[in] nodeLabel The label of the vertex that has to be added.
  114. */
  115. void addNode(std::size_t graphId, const std::string & nodeId, const std::map<std::string, std::string> & nodeLabel);
  116. /*!
  117. * @brief Adds a labeled edge.
  118. * @param[in] graphId ID of graph that has been added to the environment.
  119. * @param[in] tail The user-specific ID of the tail of the edge that has to be added.
  120. * @param[in] head The user-specific ID of the head of the edge that has to be added.
  121. * @param[in] edgeLabel The label of the vertex that has to be added.
  122. * @param[in] ignoreDuplicates If @p true, duplicate edges are ignores. Otherwise, an exception is thrown if an existing edge is added to the graph.
  123. */
  124. void addEdge(std::size_t graphId, const std::string & tail, const std::string & head, const std::map<std::string, std::string> & edgeLabel, bool ignoreDuplicates = true);
  125. /*!
  126. * @brief Clears and de-initializes a graph that has previously been added to the environment. Call initEnv() after calling this method.
  127. * @param[in] graphId ID of graph that has to be cleared.
  128. */
  129. void clearGraph(std::size_t graphId);
  130. /*!
  131. * @brief Returns ged::ExchangeGraph representation.
  132. * @param graphId ID of the selected graph.
  133. * @return ged::ExchangeGraph representation of the selected graph.
  134. */
  135. ged::ExchangeGraph<ged::GXLNodeID, ged::GXLLabel, ged::GXLLabel> getGraph(std::size_t graphId) const;
  136. /*!
  137. * @brief Returns the internal Id of a graph, selected by its ID.
  138. * @param[in] graphId ID of an input graph that has been added to the environment.
  139. * @return The internal ID of the selected graph
  140. */
  141. std::size_t getGraphInternalId(std::size_t graphId);
  142. /*!
  143. * @brief Returns all the number of nodes on a graph, selected by its ID.
  144. * @param[in] graphId ID of an input graph that has been added to the environment.
  145. * @return The number of nodes on the selected graph
  146. */
  147. std::size_t getGraphNumNodes(std::size_t graphId);
  148. /*!
  149. * @brief Returns all the number of edges on a graph, selected by its ID.
  150. * @param[in] graphId ID of an input graph that has been added to the environment.
  151. * @return The number of edges on the selected graph
  152. */
  153. std::size_t getGraphNumEdges(std::size_t graphId);
  154. /*!
  155. * @brief Returns all th Ids of nodes on a graph, selected by its ID.
  156. * @param[in] graphId ID of an input graph that has been added to the environment.
  157. * @return The list of IDs's nodes on the selected graph
  158. */
  159. std::vector<std::string> getGraphOriginalNodeIds(std::size_t graphId);
  160. /*!
  161. * @brief Returns all the labels of nodes on a graph, selected by its ID.
  162. * @param[in] graphId ID of an input graph that has been added to the environment.
  163. * @return The list of labels's nodes on the selected graph
  164. */
  165. std::vector<std::map<std::string, std::string>> getGraphNodeLabels(std::size_t graphId);
  166. /*!
  167. * @brief Returns all the edges on a graph, selected by its ID.
  168. * @param[in] graphId ID of an input graph that has been added to the environment.
  169. * @return The list of edges on the selected graph
  170. */
  171. std::map<std::pair<std::size_t, std::size_t>, std::map<std::string, std::string>> getGraphEdges(std::size_t graphId);
  172. /*!
  173. * @brief Returns the adjacence list of a graph, selected by its ID.
  174. * @param[in] graphId ID of an input graph that has been added to the environment.
  175. * @return The adjacence list of the selected graph
  176. */
  177. std::vector<std::vector<std::size_t>> getGraphAdjacenceMatrix(std::size_t graphId);
  178. /*!
  179. * @brief Sets the edit costs to one of the predefined edit costs.
  180. * @param[in] editCost Select one of the predefined edit costs.
  181. * @param[in] editCostConstants Parameters for the edit cost, empty by default.
  182. */
  183. void setEditCost(std::string editCost, std::vector<double> editCostConstants = {});
  184. /*!
  185. * @brief Sets the edit costs to a personal Edit Cost Class.
  186. * @param[in] editCostConstants Parameters for the edit cost, empty by default.
  187. * @note You have to add your class, which should inherit from EditCost class, in the function. After that, you can compile and use it in Python
  188. */
  189. void setPersonalEditCost(std::vector<double> editCostConstants = {});
  190. /*!
  191. * @brief Initializes the environment.
  192. * @param[in] initOption Select initialization options.
  193. * @param[in] print_to_stdout If set to @p true, the progress of the initialization is printed to std::out.
  194. */
  195. void initEnv(std::string initOption = "EAGER_WITH_SHUFFLED_COPIES", bool print_to_stdout = false);
  196. /*!
  197. * @brief Sets the GEDMethod to be used by run_method().
  198. * @param[in] method Select the method that is to be used.
  199. * @param[in] options An options string of the form @"[--@<option@> @<arg@>] [...]@" passed to the selected method.
  200. */
  201. void setMethod(std::string method, const std::string & options);
  202. /*!
  203. * @brief Initializes the method specified by call to set_method().
  204. */
  205. void initMethod();
  206. /*!
  207. * @brief Returns initialization time.
  208. * @return Runtime of the last call to init_method().
  209. */
  210. double getInitime() const;
  211. /*!
  212. * @brief Runs the GED method specified by call to set_method() between the graphs with IDs @p g and @p h.
  213. * @param[in] g ID of an input graph that has been added to the environment.
  214. * @param[in] h ID of an input graph that has been added to the environment.
  215. */
  216. void runMethod(std::size_t g, std::size_t h);
  217. /*!
  218. * @brief Returns upper bound for edit distance between the input graphs.
  219. * @param[in] g ID of an input graph that has been added to the environment.
  220. * @param[in] h ID of an input graph that has been added to the environment.
  221. * @return Upper bound computed by the last call to run_method() with arguments @p g and @p h.
  222. */
  223. double getUpperBound(std::size_t g, std::size_t h) const;
  224. /*!
  225. * @brief Returns lower bound for edit distance between the input graphs.
  226. * @param[in] g ID of an input graph that has been added to the environment.
  227. * @param[in] h ID of an input graph that has been added to the environment.
  228. * @return Lower bound computed by the last call to run_method() with arguments @p g and @p h.
  229. */
  230. double getLowerBound(std::size_t g,std::size_t h) const;
  231. /*!
  232. * @brief Returns the forward map between nodes of the two indicated graphs.
  233. * @param[in] g ID of an input graph that has been added to the environment.
  234. * @param[in] h ID of an input graph that has been added to the environment.
  235. * @return The forward map to the adjacence matrix computed by the last call to run_method() with arguments @p g and @p h.
  236. */
  237. std::vector<long unsigned int> getForwardMap(std::size_t g, std::size_t h) const;
  238. /*!
  239. * @brief Returns the backward map between nodes of the two indicated graphs.
  240. * @param[in] g ID of an input graph that has been added to the environment.
  241. * @param[in] h ID of an input graph that has been added to the environment.
  242. * @return The backward map to the adjacence matrix computed by the last call to run_method() with arguments @p g and @p h.
  243. */
  244. std::vector<long unsigned int> getBackwardMap(std::size_t g, std::size_t h) const;
  245. /*!
  246. * @brief Returns image of a node.
  247. * @param[in] g ID of an input graph that has been added to the environment.
  248. * @param[in] h ID of an input graph that has been added to the environment.
  249. * @param[in] nodeId Node whose image is to be returned.
  250. * @return Node to which node @p node is assigned.
  251. */
  252. std::size_t getNodeImage(std::size_t g, std::size_t h, std::size_t nodeId) const;
  253. /*!
  254. * @brief Returns pre-image of a node.
  255. * @param[in] g ID of an input graph that has been added to the environment.
  256. * @param[in] h ID of an input graph that has been added to the environment.
  257. * @param[in] nodeId Node whose pre-image is to be returned.
  258. * @return Node to which node @p node is assigned.
  259. */
  260. std::size_t getNodePreImage(std::size_t g, std::size_t h, std::size_t nodeId) const;
  261. /*!
  262. * @brief Returns the induced cost between the two indicated graphs.
  263. * @param[in] g ID of an input graph that has been added to the environment.
  264. * @param[in] h ID of an input graph that has been added to the environment.
  265. * @return The induced cost between the two indicated graphs.
  266. */
  267. double getInducedCost(std::size_t g, std::size_t h) const;
  268. /*!
  269. * @brief Returns node map between the input graphs. This function duplicates datas.
  270. * @param[in] g ID of an input graph that has been added to the environment.
  271. * @param[in] h ID of an input graph that has been added to the environment.
  272. * @return Node map computed by the last call to run_method() with arguments @p g and @p h.
  273. */
  274. std::vector<std::pair<std::size_t, std::size_t>> getNodeMap(std::size_t g, std::size_t h);
  275. /*!
  276. * @brief Returns assignment matrix between the input graphs. This function duplicates datas.
  277. * @param[in] g ID of an input graph that has been added to the environment.
  278. * @param[in] h ID of an input graph that has been added to the environment.
  279. * @return Assignment matrix computed by the last call to run_method() with arguments @p g and @p h.
  280. */
  281. std::vector<std::vector<int>> getAssignmentMatrix(std::size_t g, std::size_t h);
  282. /*!
  283. * @brief Returns a vector which contains the forward and the backward maps between nodes of the two indicated graphs.
  284. * @param[in] g ID of an input graph that has been added to the environment.
  285. * @param[in] h ID of an input graph that has been added to the environment.
  286. * @return The forward and backward maps to the adjacence matrix computed by the last call to run_method() with arguments @p g and @p h.
  287. */
  288. std::vector<std::vector<unsigned long int>> getAllMap(std::size_t g, std::size_t h);
  289. /*!
  290. * @brief Returns runtime.
  291. * @param[in] g ID of an input graph that has been added to the environment.
  292. * @param[in] h ID of an input graph that has been added to the environment.
  293. * @return Runtime of last call to run_method() with arguments @p g and @p h.
  294. */
  295. double getRuntime(std::size_t g, std::size_t h) const;
  296. /*!
  297. * @brief Checks if the edit costs are quasimetric.
  298. * @return Boolean @p true if the edit costs are quasimetric and @p false, otherwise.
  299. */
  300. bool quasimetricCosts() const;
  301. /*!
  302. * @brief Applies the hungarian algorithm (LSAP) to a matrix cost.
  303. * @param[in] matrixCost The matrix cost.
  304. * @return the values of rho, varrho, u and v, in this order.
  305. */
  306. std::vector<std::vector<size_t>> hungarianLSAP(std::vector<std::vector<std::size_t>> matrixCost);
  307. /*!
  308. * @brief Applies the hungarian algorithm (LSAPE) to a matrix cost.
  309. * @param[in] matrixCost The matrix cost.
  310. * @return the values of rho, varrho, u and v, in this order.
  311. */
  312. std::vector<std::vector<double>> hungarianLSAPE(std::vector<std::vector<double>> matrixCost);
  313. /*!
  314. * @brief Returns the number of node labels.
  315. * @return Number of pairwise different node labels contained in the environment.
  316. * @note If @p 1 is returned, the nodes are unlabeled.
  317. */
  318. std::size_t getNumNodeLabels() const;
  319. /*!
  320. * @brief Returns node label.
  321. * @param[in] label_id ID of node label that should be returned. Must be between 1 and num_node_labels().
  322. * @return Node label for selected label ID.
  323. */
  324. std::map<std::string, std::string> getNodeLabel(std::size_t label_id) const;
  325. /*!
  326. * @brief Returns the number of edge labels.
  327. * @return Number of pairwise different edge labels contained in the environment.
  328. * @note If @p 1 is returned, the edges are unlabeled.
  329. */
  330. std::size_t getNumEdgeLabels() const;
  331. /*!
  332. * @brief Returns edge label.
  333. * @param[in] label_id ID of edge label that should be returned. Must be between 1 and num_node_labels().
  334. * @return Edge label for selected label ID.
  335. */
  336. std::map<std::string, std::string> getEdgeLabel(std::size_t label_id) const;
  337. // /*!
  338. // * @brief Returns the number of nodes.
  339. // * @param[in] graph_id ID of an input graph that has been added to the environment.
  340. // * @return Number of nodes in the graph.
  341. // */
  342. // std::size_t getNumNodes(std::size_t graph_id) const;
  343. /*!
  344. * @brief Returns average number of nodes.
  345. * @return Average number of nodes of the graphs contained in the environment.
  346. */
  347. double getAvgNumNodes() const;
  348. /*!
  349. * @brief Returns node relabeling cost.
  350. * @param[in] node_label_1 First node label.
  351. * @param[in] node_label_2 Second node label.
  352. * @return Node relabeling cost for the given node labels.
  353. */
  354. double getNodeRelCost(const std::map<std::string, std::string> & node_label_1, const std::map<std::string, std::string> & node_label_2) const;
  355. /*!
  356. * @brief Returns node deletion cost.
  357. * @param[in] node_label Node label.
  358. * @return Cost of deleting node with given label.
  359. */
  360. double getNodeDelCost(const std::map<std::string, std::string> & node_label) const;
  361. /*!
  362. * @brief Returns node insertion cost.
  363. * @param[in] node_label Node label.
  364. * @return Cost of inserting node with given label.
  365. */
  366. double getNodeInsCost(const std::map<std::string, std::string> & node_label) const;
  367. /*!
  368. * @brief Computes median node label.
  369. * @param[in] node_labels The node labels whose median should be computed.
  370. * @return Median of the given node labels.
  371. */
  372. std::map<std::string, std::string> getMedianNodeLabel(const std::vector<std::map<std::string, std::string>> & node_labels) const;
  373. /*!
  374. * @brief Returns edge relabeling cost.
  375. * @param[in] edge_label_1 First edge label.
  376. * @param[in] edge_label_2 Second edge label.
  377. * @return Edge relabeling cost for the given edge labels.
  378. */
  379. double getEdgeRelCost(const std::map<std::string, std::string> & edge_label_1, const std::map<std::string, std::string> & edge_label_2) const;
  380. /*!
  381. * @brief Returns edge deletion cost.
  382. * @param[in] edge_label Edge label.
  383. * @return Cost of deleting edge with given label.
  384. */
  385. double getEdgeDelCost(const std::map<std::string, std::string> & edge_label) const;
  386. /*!
  387. * @brief Returns edge insertion cost.
  388. * @param[in] edge_label Edge label.
  389. * @return Cost of inserting edge with given label.
  390. */
  391. double getEdgeInsCost(const std::map<std::string, std::string> & edge_label) const;
  392. /*!
  393. * @brief Computes median edge label.
  394. * @param[in] edge_labels The edge labels whose median should be computed.
  395. * @return Median of the given edge labels.
  396. */
  397. std::map<std::string, std::string> getMedianEdgeLabel(const std::vector<std::map<std::string, std::string>> & edge_labels) const;
  398. /*!
  399. * @brief Returns the initialization type of the last initialization.
  400. * @return Initialization type in string.
  401. */
  402. std::string getInitType() const;
  403. /*!
  404. * @brief Computes the edit cost between two graphs induced by a node map.
  405. * @param[in] g_id ID of input graph.
  406. * @param[in] h_id ID of input graph.
  407. * @return Computed induced cost.
  408. */
  409. double computeInducedCost(std::size_t g_id, std::size_t h_id, std::vector<pair<std::size_t, std::size_t>> relation) const;
  410. // /*!
  411. // * @brief Returns node relabeling, insertion, or deletion cost.
  412. // * @param[in] label1 First node label.
  413. // * @param[in] label2 Second node label.
  414. // * @return Node relabeling cost if @p label1 and @p label2 are both different from ged::dummy_label(),
  415. // * node insertion cost if @p label1 equals ged::dummy_label and @p label2 does not,
  416. // * node deletion cost if @p label1 does not equal ged::dummy_label and @p label2 does,
  417. // * and 0 otherwise.
  418. // */
  419. // double getNodeCost(std::size_t label1, std::size_t label2) const;
  420. private:
  421. ged::GEDEnv<ged::GXLNodeID, ged::GXLLabel, ged::GXLLabel> * env_; // environment variable
  422. bool initialized; // initialization boolean (because env has one but not accessible)
  423. };
  424. }
  425. #include "GedLibBind.ipp"
  426. #endif /* SRC_GEDLIB_BIND_HPP */
  427. // namespace shapes {
  428. // class Rectangle {
  429. // public:
  430. // int x0, y0, x1, y1;
  431. // Rectangle();
  432. // Rectangle(int x0, int y0, int x1, int y1);
  433. // ~Rectangle();
  434. // int getArea();
  435. // void getSize(int* width, int* height);
  436. // void move(int dx, int dy);
  437. // };
  438. // }

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