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_env.py 26 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Jun 17 12:02:36 2020
  5. @author: ljia
  6. """
  7. import numpy as np
  8. import networkx as nx
  9. from gklearn.ged.env import Options, OptionsStringMap
  10. from gklearn.ged.env import GEDData
  11. class GEDEnv(object):
  12. def __init__(self):
  13. self.__initialized = False
  14. self.__new_graph_ids = []
  15. self.__ged_data = GEDData()
  16. # Variables needed for approximating ged_instance_.
  17. self.__lower_bounds = {}
  18. self.__upper_bounds = {}
  19. self.__runtimes = {}
  20. self.__node_maps = {}
  21. self.__original_to_internal_node_ids = []
  22. self.__internal_to_original_node_ids = []
  23. self.__ged_method = None
  24. def set_edit_cost(self, edit_cost, edit_cost_constants=[]):
  25. """
  26. /*!
  27. * @brief Sets the edit costs to one of the predefined edit costs.
  28. * @param[in] edit_costs Select one of the predefined edit costs.
  29. * @param[in] edit_cost_constants Constants passed to the constructor of the edit cost class selected by @p edit_costs.
  30. */
  31. """
  32. self.__ged_data._set_edit_cost(edit_cost, edit_cost_constants)
  33. def add_graph(self, graph_name='', graph_class=''):
  34. """
  35. /*!
  36. * @brief Adds a new uninitialized graph to the environment. Call init() after calling this method.
  37. * @param[in] graph_name The name of the added graph. Empty if not specified.
  38. * @param[in] graph_class The class of the added graph. Empty if not specified.
  39. * @return The ID of the newly added graph.
  40. */
  41. """
  42. # @todo: graphs are not uninitialized.
  43. self.__initialized = False
  44. graph_id = self.__ged_data._num_graphs_without_shuffled_copies
  45. self.__ged_data._num_graphs_without_shuffled_copies += 1
  46. self.__new_graph_ids.append(graph_id)
  47. self.__ged_data._graphs.append(nx.Graph())
  48. self.__ged_data._graph_names.append(graph_name)
  49. self.__ged_data._graph_classes.append(graph_class)
  50. self.__original_to_internal_node_ids.append({})
  51. self.__internal_to_original_node_ids.append({})
  52. self.__ged_data._strings_to_internal_node_ids.append({})
  53. self.__ged_data._internal_node_ids_to_strings.append({})
  54. return graph_id
  55. def clear_graph(self, graph_id):
  56. """
  57. /*!
  58. * @brief Clears and de-initializes a graph that has previously been added to the environment. Call init() after calling this method.
  59. * @param[in] graph_id ID of graph that has to be cleared.
  60. */
  61. """
  62. if graph_id > self.__ged_data.num_graphs_without_shuffled_copies():
  63. raise Exception('The graph', self.get_graph_name(graph_id), 'has not been added to the environment.')
  64. self.__ged_data._graphs[graph_id].clear()
  65. self.__original_to_internal_node_ids[graph_id].clear()
  66. self.__internal_to_original_node_ids[graph_id].clear()
  67. self.__ged_data._strings_to_internal_node_ids[graph_id].clear()
  68. self.__ged_data._internal_node_ids_to_strings[graph_id].clear()
  69. self.__initialized = False
  70. def add_node(self, graph_id, node_id, node_label):
  71. """
  72. /*!
  73. * @brief Adds a labeled node.
  74. * @param[in] graph_id ID of graph that has been added to the environment.
  75. * @param[in] node_id The user-specific ID of the vertex that has to be added.
  76. * @param[in] node_label The label of the vertex that has to be added. Set to ged::NoLabel() if template parameter @p UserNodeLabel equals ged::NoLabel.
  77. */
  78. """
  79. # @todo: check ids.
  80. self.__initialized = False
  81. internal_node_id = nx.number_of_nodes(self.__ged_data._graphs[graph_id])
  82. self.__ged_data._graphs[graph_id].add_node(internal_node_id, label=node_label)
  83. self.__original_to_internal_node_ids[graph_id][node_id] = internal_node_id
  84. self.__internal_to_original_node_ids[graph_id][internal_node_id] = node_id
  85. self.__ged_data._strings_to_internal_node_ids[graph_id][str(node_id)] = internal_node_id
  86. self.__ged_data._internal_node_ids_to_strings[graph_id][internal_node_id] = str(node_id)
  87. self.__ged_data._node_label_to_id(node_label)
  88. label_id = self.__ged_data._node_label_to_id(node_label)
  89. # @todo: ged_data_.graphs_[graph_id].set_label
  90. def add_edge(self, graph_id, nd_from, nd_to, edge_label, ignore_duplicates=True):
  91. """
  92. /*!
  93. * @brief Adds a labeled edge.
  94. * @param[in] graph_id ID of graph that has been added to the environment.
  95. * @param[in] tail The user-specific ID of the tail of the edge that has to be added.
  96. * @param[in] head The user-specific ID of the head of the edge that has to be added.
  97. * @param[in] edge_label The label of the vertex that has to be added. Set to ged::NoLabel() if template parameter @p UserEdgeLabel equals ged::NoLabel.
  98. * @param[in] ignore_duplicates If @p true, duplicate edges are ignores. Otherwise, an exception is thrown if an existing edge is added to the graph.
  99. */
  100. """
  101. # @todo: check everything.
  102. self.__initialized = False
  103. # @todo: check ignore_duplicates.
  104. self.__ged_data._graphs[graph_id].add_edge(self.__original_to_internal_node_ids[graph_id][nd_from], self.__original_to_internal_node_ids[graph_id][nd_to], label=edge_label)
  105. label_id = self.__ged_data._edge_label_to_id(edge_label)
  106. # @todo: ged_data_.graphs_[graph_id].set_label
  107. def add_nx_graph(self, g, classe, ignore_duplicates=True) :
  108. """
  109. Add a Graph (made by networkx) on the environment. Be careful to respect the same format as GXL graphs for labelling nodes and edges.
  110. :param g: The graph to add (networkx graph)
  111. :param ignore_duplicates: If True, duplicate edges are ignored, otherwise it's raise an error if an existing edge is added. True by default
  112. :type g: networkx.graph
  113. :type ignore_duplicates: bool
  114. :return: The ID of the newly added graphe
  115. :rtype: size_t
  116. .. note:: The NX graph must respect the GXL structure. Please see how a GXL graph is construct.
  117. """
  118. graph_id = self.add_graph(g.name, classe) # check if the graph name already exists.
  119. for node in g.nodes: # @todo: if the keys of labels include int and str at the same time.
  120. self.add_node(graph_id, node, tuple(sorted(g.nodes[node].items(), key=lambda kv: kv[0])))
  121. for edge in g.edges:
  122. self.add_edge(graph_id, edge[0], edge[1], tuple(sorted(g.edges[(edge[0], edge[1])].items(), key=lambda kv: kv[0])), ignore_duplicates)
  123. return graph_id
  124. def load_nx_graph(self, nx_graph, graph_id, graph_name='', graph_class=''):
  125. """
  126. Loads NetworkX Graph into the GED environment.
  127. Parameters
  128. ----------
  129. nx_graph : NetworkX Graph object
  130. The graph that should be loaded.
  131. graph_id : int or None
  132. The ID of a graph contained the environment (overwrite existing graph) or add new graph if `None`.
  133. graph_name : string, optional
  134. The name of newly added graph. The default is ''. Has no effect unless `graph_id` equals `None`.
  135. graph_class : string, optional
  136. The class of newly added graph. The default is ''. Has no effect unless `graph_id` equals `None`.
  137. Returns
  138. -------
  139. int
  140. The ID of the newly loaded graph.
  141. """
  142. if graph_id is None: # @todo: undefined.
  143. graph_id = self.add_graph(graph_name, graph_class)
  144. else:
  145. self.clear_graph(graph_id)
  146. for node in nx_graph.nodes:
  147. self.add_node(graph_id, node, tuple(sorted(nx_graph.nodes[node].items(), key=lambda kv: kv[0])))
  148. for edge in nx_graph.edges:
  149. self.add_edge(graph_id, edge[0], edge[1], tuple(sorted(nx_graph.edges[(edge[0], edge[1])].items(), key=lambda kv: kv[0])))
  150. return graph_id
  151. def init(self, init_type=Options.InitType.EAGER_WITHOUT_SHUFFLED_COPIES, print_to_stdout=False):
  152. if isinstance(init_type, str):
  153. init_type = OptionsStringMap.InitType[init_type]
  154. # Throw an exception if no edit costs have been selected.
  155. if self.__ged_data._edit_cost is None:
  156. raise Exception('No edit costs have been selected. Call set_edit_cost() before calling init().')
  157. # Return if the environment is initialized.
  158. if self.__initialized:
  159. return
  160. # Set initialization type.
  161. self.__ged_data._init_type = init_type
  162. # @todo: Construct shuffled graph copies if necessary.
  163. # Re-initialize adjacency matrices (also previously initialized graphs must be re-initialized because of possible re-allocation).
  164. # @todo: setup_adjacency_matrix, don't know if neccessary.
  165. self.__ged_data._max_num_nodes = np.max([nx.number_of_nodes(g) for g in self.__ged_data._graphs])
  166. self.__ged_data._max_num_edges = np.max([nx.number_of_edges(g) for g in self.__ged_data._graphs])
  167. # Initialize cost matrices if necessary.
  168. if self.__ged_data._eager_init():
  169. pass # @todo: init_cost_matrices_: 1. Update node cost matrix if new node labels have been added to the environment; 2. Update edge cost matrix if new edge labels have been added to the environment.
  170. # Mark environment as initialized.
  171. self.__initialized = True
  172. self.__new_graph_ids.clear()
  173. def is_initialized(self):
  174. """
  175. /*!
  176. * @brief Check if the environment is initialized.
  177. * @return True if the environment is initialized.
  178. */
  179. """
  180. return self.__initialized
  181. def get_init_type(self):
  182. """
  183. /*!
  184. * @brief Returns the initialization type of the last initialization.
  185. * @return Initialization type.
  186. */
  187. """
  188. return self.__ged_data._init_type
  189. def set_method(self, method, options=''):
  190. """
  191. /*!
  192. * @brief Sets the GEDMethod to be used by run_method().
  193. * @param[in] method Select the method that is to be used.
  194. * @param[in] options An options string of the form @"[--@<option@> @<arg@>] [...]@" passed to the selected method.
  195. */
  196. """
  197. del self.__ged_method
  198. if isinstance(method, str):
  199. method = OptionsStringMap.GEDMethod[method]
  200. if method == Options.GEDMethod.BRANCH:
  201. self.__ged_method = Branch(self.__ged_data)
  202. elif method == Options.GEDMethod.BRANCH_FAST:
  203. self.__ged_method = BranchFast(self.__ged_data)
  204. elif method == Options.GEDMethod.BRANCH_FAST:
  205. self.__ged_method = BranchFast(self.__ged_data)
  206. elif method == Options.GEDMethod.BRANCH_TIGHT:
  207. self.__ged_method = BranchTight(self.__ged_data)
  208. elif method == Options.GEDMethod.BRANCH_UNIFORM:
  209. self.__ged_method = BranchUniform(self.__ged_data)
  210. elif method == Options.GEDMethod.BRANCH_COMPACT:
  211. self.__ged_method = BranchCompact(self.__ged_data)
  212. elif method == Options.GEDMethod.PARTITION:
  213. self.__ged_method = Partition(self.__ged_data)
  214. elif method == Options.GEDMethod.HYBRID:
  215. self.__ged_method = Hybrid(self.__ged_data)
  216. elif method == Options.GEDMethod.RING:
  217. self.__ged_method = Ring(self.__ged_data)
  218. elif method == Options.GEDMethod.ANCHOR_AWARE_GED:
  219. self.__ged_method = AnchorAwareGED(self.__ged_data)
  220. elif method == Options.GEDMethod.WALKS:
  221. self.__ged_method = Walks(self.__ged_data)
  222. elif method == Options.GEDMethod.IPFP:
  223. self.__ged_method = IPFP(self.__ged_data)
  224. elif method == Options.GEDMethod.BIPARTITE:
  225. from gklearn.ged.methods import Bipartite
  226. self.__ged_method = Bipartite(self.__ged_data)
  227. elif method == Options.GEDMethod.SUBGRAPH:
  228. self.__ged_method = Subgraph(self.__ged_data)
  229. elif method == Options.GEDMethod.NODE:
  230. self.__ged_method = Node(self.__ged_data)
  231. elif method == Options.GEDMethod.RING_ML:
  232. self.__ged_method = RingML(self.__ged_data)
  233. elif method == Options.GEDMethod.BIPARTITE_ML:
  234. self.__ged_method = BipartiteML(self.__ged_data)
  235. elif method == Options.GEDMethod.REFINE:
  236. self.__ged_method = Refine(self.__ged_data)
  237. elif method == Options.GEDMethod.BP_BEAM:
  238. self.__ged_method = BPBeam(self.__ged_data)
  239. elif method == Options.GEDMethod.SIMULATED_ANNEALING:
  240. self.__ged_method = SimulatedAnnealing(self.__ged_data)
  241. elif method == Options.GEDMethod.HED:
  242. self.__ged_method = HED(self.__ged_data)
  243. elif method == Options.GEDMethod.STAR:
  244. self.__ged_method = STAR(self.__ged_data)
  245. # #ifdef GUROBI
  246. elif method == Options.GEDMethod.F1:
  247. self.__ged_method = F1(self.__ged_data)
  248. elif method == Options.GEDMethod.F2:
  249. self.__ged_method = F2(self.__ged_data)
  250. elif method == Options.GEDMethod.COMPACT_MIP:
  251. self.__ged_method = CompactMIP(self.__ged_data)
  252. elif method == Options.GEDMethod.BLP_NO_EDGE_LABELS:
  253. self.__ged_method = BLPNoEdgeLabels(self.__ged_data)
  254. self.__ged_method.set_options(options)
  255. def run_method(self, g_id, h_id):
  256. """
  257. /*!
  258. * @brief Runs the GED method specified by call to set_method() between the graphs with IDs @p g_id and @p h_id.
  259. * @param[in] g_id ID of an input graph that has been added to the environment.
  260. * @param[in] h_id ID of an input graph that has been added to the environment.
  261. */
  262. """
  263. if g_id >= self.__ged_data.num_graphs():
  264. raise Exception('The graph with ID', str(g_id), 'has not been added to the environment.')
  265. if h_id >= self.__ged_data.num_graphs():
  266. raise Exception('The graph with ID', str(h_id), 'has not been added to the environment.')
  267. if not self.__initialized:
  268. raise Exception('The environment is uninitialized. Call init() after adding all graphs to the environment.')
  269. if self.__ged_method is None:
  270. raise Exception('No method has been set. Call set_method() before calling run().')
  271. # Call selected GEDMethod and store results.
  272. if self.__ged_data.shuffled_graph_copies_available() and (g_id == h_id):
  273. self.__ged_method.run(g_id, self.__ged_data.id_shuffled_graph_copy(h_id)) # @todo: why shuffle?
  274. else:
  275. self.__ged_method.run(g_id, h_id)
  276. self.__lower_bounds[(g_id, h_id)] = self.__ged_method.get_lower_bound()
  277. self.__upper_bounds[(g_id, h_id)] = self.__ged_method.get_upper_bound()
  278. self.__runtimes[(g_id, h_id)] = self.__ged_method.get_runtime()
  279. self.__node_maps[(g_id, h_id)] = self.__ged_method.get_node_map()
  280. def init_method(self):
  281. """Initializes the method specified by call to set_method().
  282. """
  283. if not self.__initialized:
  284. raise Exception('The environment is uninitialized. Call init() before calling init_method().')
  285. if self.__ged_method is None:
  286. raise Exception('No method has been set. Call set_method() before calling init_method().')
  287. self.__ged_method.init()
  288. def get_num_node_labels(self):
  289. """
  290. /*!
  291. * @brief Returns the number of node labels.
  292. * @return Number of pairwise different node labels contained in the environment.
  293. * @note If @p 1 is returned, the nodes are unlabeled.
  294. */
  295. """
  296. return len(self.__ged_data._node_labels)
  297. def get_node_label(self, label_id, to_dict=True):
  298. """
  299. /*!
  300. * @brief Returns node label.
  301. * @param[in] label_id ID of node label that should be returned. Must be between 1 and num_node_labels().
  302. * @return Node label for selected label ID.
  303. */
  304. """
  305. if label_id < 1 or label_id > self.get_num_node_labels():
  306. raise Exception('The environment does not contain a node label with ID', str(label_id), '.')
  307. if to_dict:
  308. return dict(self.__ged_data._node_labels[label_id - 1])
  309. return self.__ged_data._node_labels[label_id - 1]
  310. def get_num_edge_labels(self):
  311. """
  312. /*!
  313. * @brief Returns the number of edge labels.
  314. * @return Number of pairwise different edge labels contained in the environment.
  315. * @note If @p 1 is returned, the edges are unlabeled.
  316. */
  317. """
  318. return len(self.__ged_data._edge_labels)
  319. def get_edge_label(self, label_id, to_dict=True):
  320. """
  321. /*!
  322. * @brief Returns edge label.
  323. * @param[in] label_id ID of edge label that should be returned. Must be between 1 and num_node_labels().
  324. * @return Edge label for selected label ID.
  325. */
  326. """
  327. if label_id < 1 or label_id > self.get_num_edge_labels():
  328. raise Exception('The environment does not contain an edge label with ID', str(label_id), '.')
  329. if to_dict:
  330. return dict(self.__ged_data._edge_labels[label_id - 1])
  331. return self.__ged_data._edge_labels[label_id - 1]
  332. def get_upper_bound(self, g_id, h_id):
  333. """
  334. /*!
  335. * @brief Returns upper bound for edit distance between the input graphs.
  336. * @param[in] g_id ID of an input graph that has been added to the environment.
  337. * @param[in] h_id ID of an input graph that has been added to the environment.
  338. * @return Upper bound computed by the last call to run_method() with arguments @p g_id and @p h_id.
  339. */
  340. """
  341. if (g_id, h_id) not in self.__upper_bounds:
  342. raise Exception('Call run(' + str(g_id) + ',' + str(h_id) + ') before calling get_upper_bound(' + str(g_id) + ',' + str(h_id) + ').')
  343. return self.__upper_bounds[(g_id, h_id)]
  344. def get_lower_bound(self, g_id, h_id):
  345. """
  346. /*!
  347. * @brief Returns lower bound for edit distance between the input graphs.
  348. * @param[in] g_id ID of an input graph that has been added to the environment.
  349. * @param[in] h_id ID of an input graph that has been added to the environment.
  350. * @return Lower bound computed by the last call to run_method() with arguments @p g_id and @p h_id.
  351. */
  352. """
  353. if (g_id, h_id) not in self.__lower_bounds:
  354. raise Exception('Call run(' + str(g_id) + ',' + str(h_id) + ') before calling get_lower_bound(' + str(g_id) + ',' + str(h_id) + ').')
  355. return self.__lower_bounds[(g_id, h_id)]
  356. def get_runtime(self, g_id, h_id):
  357. """
  358. /*!
  359. * @brief Returns runtime.
  360. * @param[in] g_id ID of an input graph that has been added to the environment.
  361. * @param[in] h_id ID of an input graph that has been added to the environment.
  362. * @return Runtime of last call to run_method() with arguments @p g_id and @p h_id.
  363. */
  364. """
  365. if (g_id, h_id) not in self.__runtimes:
  366. raise Exception('Call run(' + str(g_id) + ',' + str(h_id) + ') before calling get_runtime(' + str(g_id) + ',' + str(h_id) + ').')
  367. return self.__runtimes[(g_id, h_id)]
  368. def get_init_time(self):
  369. """
  370. /*!
  371. * @brief Returns initialization time.
  372. * @return Runtime of the last call to init_method().
  373. */
  374. """
  375. return self.__ged_method.get_init_time()
  376. def get_node_map(self, g_id, h_id):
  377. """
  378. /*!
  379. * @brief Returns node map between the input graphs.
  380. * @param[in] g_id ID of an input graph that has been added to the environment.
  381. * @param[in] h_id ID of an input graph that has been added to the environment.
  382. * @return Node map computed by the last call to run_method() with arguments @p g_id and @p h_id.
  383. */
  384. """
  385. if (g_id, h_id) not in self.__node_maps:
  386. raise Exception('Call run(' + str(g_id) + ',' + str(h_id) + ') before calling get_node_map(' + str(g_id) + ',' + str(h_id) + ').')
  387. return self.__node_maps[(g_id, h_id)]
  388. def get_forward_map(self, g_id, h_id) :
  389. """
  390. Returns the forward map (or the half of the adjacence matrix) between nodes of the two indicated graphs.
  391. :param g: The Id of the first compared graph
  392. :param h: The Id of the second compared graph
  393. :type g: size_t
  394. :type h: size_t
  395. :return: The forward map to the adjacence matrix between nodes of the two graphs
  396. :rtype: list[npy_uint32]
  397. .. seealso:: run_method(), get_upper_bound(), get_lower_bound(), get_backward_map(), get_runtime(), quasimetric_cost(), get_node_map(), get_assignment_matrix()
  398. .. warning:: run_method() between the same two graph must be called before this function.
  399. .. note:: I don't know how to connect the two map to reconstruct the adjacence matrix. Please come back when I know how it's work !
  400. """
  401. return self.get_node_map(g_id, h_id).forward_map
  402. def get_backward_map(self, g_id, h_id) :
  403. """
  404. Returns the backward map (or the half of the adjacence matrix) between nodes of the two indicated graphs.
  405. :param g: The Id of the first compared graph
  406. :param h: The Id of the second compared graph
  407. :type g: size_t
  408. :type h: size_t
  409. :return: The backward map to the adjacence matrix between nodes of the two graphs
  410. :rtype: list[npy_uint32]
  411. .. seealso:: run_method(), get_upper_bound(), get_lower_bound(), get_forward_map(), get_runtime(), quasimetric_cost(), get_node_map(), get_assignment_matrix()
  412. .. warning:: run_method() between the same two graph must be called before this function.
  413. .. note:: I don't know how to connect the two map to reconstruct the adjacence matrix. Please come back when I know how it's work !
  414. """
  415. return self.get_node_map(g_id, h_id).backward_map
  416. def compute_induced_cost(self, g_id, h_id, node_map):
  417. """
  418. /*!
  419. * @brief Computes the edit cost between two graphs induced by a node map.
  420. * @param[in] g_id ID of input graph.
  421. * @param[in] h_id ID of input graph.
  422. * @param[in,out] node_map Node map whose induced edit cost is to be computed.
  423. */
  424. """
  425. self.__ged_data.compute_induced_cost(self.__ged_data._graphs[g_id], self.__ged_data._graphs[h_id], node_map)
  426. def get_nx_graph(self, graph_id):
  427. """
  428. * @brief Returns NetworkX.Graph() representation.
  429. * @param[in] graph_id ID of the selected graph.
  430. """
  431. graph = nx.Graph() # @todo: add graph attributes.
  432. graph.graph['id'] = graph_id
  433. nb_nodes = self.get_graph_num_nodes(graph_id)
  434. original_node_ids = self.get_original_node_ids(graph_id)
  435. node_labels = self.get_graph_node_labels(graph_id, to_dict=True)
  436. graph.graph['original_node_ids'] = original_node_ids
  437. for node_id in range(0, nb_nodes):
  438. graph.add_node(node_id, **node_labels[node_id])
  439. edges = self.get_graph_edges(graph_id, to_dict=True)
  440. for (head, tail), labels in edges.items():
  441. graph.add_edge(head, tail, **labels)
  442. return graph
  443. def get_graph_node_labels(self, graph_id, to_dict=True):
  444. """
  445. Searchs and returns all the labels of nodes on a graph, selected by its ID.
  446. :param graph_id: The ID of the wanted graph
  447. :type graph_id: size_t
  448. :return: The list of nodes' labels on the selected graph
  449. :rtype: list[dict{string : string}]
  450. .. seealso:: get_graph_internal_id(), get_graph_num_nodes(), get_graph_num_edges(), get_original_node_ids(), get_graph_edges(), get_graph_adjacence_matrix()
  451. .. note:: These functions allow to collect all the graph's informations.
  452. """
  453. graph = self.__ged_data.graph(graph_id)
  454. node_labels = []
  455. for n in graph.nodes():
  456. node_labels.append(graph.nodes[n]['label'])
  457. if to_dict:
  458. return [dict(i) for i in node_labels]
  459. return node_labels
  460. def get_graph_edges(self, graph_id, to_dict=True):
  461. """
  462. Searchs and returns all the edges on a graph, selected by its ID.
  463. :param graph_id: The ID of the wanted graph
  464. :type graph_id: size_t
  465. :return: The list of edges on the selected graph
  466. :rtype: dict{tuple(size_t, size_t) : dict{string : string}}
  467. .. seealso::get_graph_internal_id(), get_graph_num_nodes(), get_graph_num_edges(), get_original_node_ids(), get_graph_node_labels(), get_graph_adjacence_matrix()
  468. .. note:: These functions allow to collect all the graph's informations.
  469. """
  470. graph = self.__ged_data.graph(graph_id)
  471. if to_dict:
  472. edges = {}
  473. for n1, n2, attr in graph.edges(data=True):
  474. edges[(n1, n2)] = dict(attr['label'])
  475. return edges
  476. return {(n1, n2): attr['label'] for n1, n2, attr in graph.edges(data=True)}
  477. def get_graph_name(self, graph_id):
  478. """
  479. /*!
  480. * @brief Returns the graph name.
  481. * @param[in] graph_id ID of an input graph that has been added to the environment.
  482. * @return Name of the input graph.
  483. */
  484. """
  485. return self.__ged_data._graph_names[graph_id]
  486. def get_graph_num_nodes(self, graph_id):
  487. """
  488. /*!
  489. * @brief Returns the number of nodes.
  490. * @param[in] graph_id ID of an input graph that has been added to the environment.
  491. * @return Number of nodes in the graph.
  492. */
  493. """
  494. return nx.number_of_nodes(self.__ged_data.graph(graph_id))
  495. def get_original_node_ids(self, graph_id):
  496. """
  497. Searchs and returns all th Ids of nodes on a graph, selected by its ID.
  498. :param graph_id: The ID of the wanted graph
  499. :type graph_id: size_t
  500. :return: The list of IDs's nodes on the selected graph
  501. :rtype: list[string]
  502. .. seealso::get_graph_internal_id(), get_graph_num_nodes(), get_graph_num_edges(), get_graph_node_labels(), get_graph_edges(), get_graph_adjacence_matrix()
  503. .. note:: These functions allow to collect all the graph's informations.
  504. """
  505. return [i for i in self.__internal_to_original_node_ids[graph_id].values()]
  506. def get_node_rel_cost(self, node_label_1, node_label_2):
  507. """
  508. /*!
  509. * @brief Returns node relabeling cost.
  510. * @param[in] node_label_1 First node label.
  511. * @param[in] node_label_2 Second node label.
  512. * @return Node relabeling cost for the given node labels.
  513. */
  514. """
  515. if isinstance(node_label_1, dict):
  516. node_label_1 = tuple(sorted(node_label_1.items(), key=lambda kv: kv[0]))
  517. if isinstance(node_label_2, dict):
  518. node_label_2 = tuple(sorted(node_label_2.items(), key=lambda kv: kv[0]))
  519. return self.__ged_data._edit_cost.node_rel_cost_fun(node_label_1, node_label_2)
  520. def get_node_del_cost(self, node_label):
  521. """
  522. /*!
  523. * @brief Returns node deletion cost.
  524. * @param[in] node_label Node label.
  525. * @return Cost of deleting node with given label.
  526. */
  527. """
  528. if isinstance(node_label, dict):
  529. node_label = tuple(sorted(node_label.items(), key=lambda kv: kv[0]))
  530. return self.__ged_data._edit_cost.node_del_cost_fun(node_label)
  531. def get_node_ins_cost(self, node_label):
  532. """
  533. /*!
  534. * @brief Returns node insertion cost.
  535. * @param[in] node_label Node label.
  536. * @return Cost of inserting node with given label.
  537. */
  538. """
  539. if isinstance(node_label, dict):
  540. node_label = tuple(sorted(node_label.items(), key=lambda kv: kv[0]))
  541. return self.__ged_data._edit_cost.node_ins_cost_fun(node_label)
  542. def get_edge_rel_cost(self, edge_label_1, edge_label_2):
  543. """
  544. /*!
  545. * @brief Returns edge relabeling cost.
  546. * @param[in] edge_label_1 First edge label.
  547. * @param[in] edge_label_2 Second edge label.
  548. * @return Edge relabeling cost for the given edge labels.
  549. */
  550. """
  551. if isinstance(edge_label_1, dict):
  552. edge_label_1 = tuple(sorted(edge_label_1.items(), key=lambda kv: kv[0]))
  553. if isinstance(edge_label_2, dict):
  554. edge_label_2 = tuple(sorted(edge_label_2.items(), key=lambda kv: kv[0]))
  555. return self.__ged_data._edit_cost.edge_rel_cost_fun(edge_label_1, edge_label_2)
  556. def get_edge_del_cost(self, edge_label):
  557. """
  558. /*!
  559. * @brief Returns edge deletion cost.
  560. * @param[in] edge_label Edge label.
  561. * @return Cost of deleting edge with given label.
  562. */
  563. """
  564. if isinstance(edge_label, dict):
  565. edge_label = tuple(sorted(edge_label.items(), key=lambda kv: kv[0]))
  566. return self.__ged_data._edit_cost.edge_del_cost_fun(edge_label)
  567. def get_edge_ins_cost(self, edge_label):
  568. """
  569. /*!
  570. * @brief Returns edge insertion cost.
  571. * @param[in] edge_label Edge label.
  572. * @return Cost of inserting edge with given label.
  573. */
  574. """
  575. if isinstance(edge_label, dict):
  576. edge_label = tuple(sorted(edge_label.items(), key=lambda kv: kv[0]))
  577. return self.__ged_data._edit_cost.edge_ins_cost_fun(edge_label)
  578. def get_all_graph_ids(self):
  579. return [i for i in range(0, self.__ged_data._num_graphs_without_shuffled_copies)]

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