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 27 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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_label_costs(self, label_costs):
  190. """Set the costs between labels.
  191. """
  192. self.__ged_data._node_label_costs = label_costs
  193. def set_method(self, method, options=''):
  194. """
  195. /*!
  196. * @brief Sets the GEDMethod to be used by run_method().
  197. * @param[in] method Select the method that is to be used.
  198. * @param[in] options An options string of the form @"[--@<option@> @<arg@>] [...]@" passed to the selected method.
  199. */
  200. """
  201. del self.__ged_method
  202. if isinstance(method, str):
  203. method = OptionsStringMap.GEDMethod[method]
  204. if method == Options.GEDMethod.BRANCH:
  205. self.__ged_method = Branch(self.__ged_data)
  206. elif method == Options.GEDMethod.BRANCH_FAST:
  207. self.__ged_method = BranchFast(self.__ged_data)
  208. elif method == Options.GEDMethod.BRANCH_FAST:
  209. self.__ged_method = BranchFast(self.__ged_data)
  210. elif method == Options.GEDMethod.BRANCH_TIGHT:
  211. self.__ged_method = BranchTight(self.__ged_data)
  212. elif method == Options.GEDMethod.BRANCH_UNIFORM:
  213. self.__ged_method = BranchUniform(self.__ged_data)
  214. elif method == Options.GEDMethod.BRANCH_COMPACT:
  215. self.__ged_method = BranchCompact(self.__ged_data)
  216. elif method == Options.GEDMethod.PARTITION:
  217. self.__ged_method = Partition(self.__ged_data)
  218. elif method == Options.GEDMethod.HYBRID:
  219. self.__ged_method = Hybrid(self.__ged_data)
  220. elif method == Options.GEDMethod.RING:
  221. self.__ged_method = Ring(self.__ged_data)
  222. elif method == Options.GEDMethod.ANCHOR_AWARE_GED:
  223. self.__ged_method = AnchorAwareGED(self.__ged_data)
  224. elif method == Options.GEDMethod.WALKS:
  225. self.__ged_method = Walks(self.__ged_data)
  226. elif method == Options.GEDMethod.IPFP:
  227. self.__ged_method = IPFP(self.__ged_data)
  228. elif method == Options.GEDMethod.BIPARTITE:
  229. from gklearn.ged.methods import Bipartite
  230. self.__ged_method = Bipartite(self.__ged_data)
  231. elif method == Options.GEDMethod.SUBGRAPH:
  232. self.__ged_method = Subgraph(self.__ged_data)
  233. elif method == Options.GEDMethod.NODE:
  234. self.__ged_method = Node(self.__ged_data)
  235. elif method == Options.GEDMethod.RING_ML:
  236. self.__ged_method = RingML(self.__ged_data)
  237. elif method == Options.GEDMethod.BIPARTITE_ML:
  238. self.__ged_method = BipartiteML(self.__ged_data)
  239. elif method == Options.GEDMethod.REFINE:
  240. self.__ged_method = Refine(self.__ged_data)
  241. elif method == Options.GEDMethod.BP_BEAM:
  242. self.__ged_method = BPBeam(self.__ged_data)
  243. elif method == Options.GEDMethod.SIMULATED_ANNEALING:
  244. self.__ged_method = SimulatedAnnealing(self.__ged_data)
  245. elif method == Options.GEDMethod.HED:
  246. self.__ged_method = HED(self.__ged_data)
  247. elif method == Options.GEDMethod.STAR:
  248. self.__ged_method = STAR(self.__ged_data)
  249. # #ifdef GUROBI
  250. elif method == Options.GEDMethod.F1:
  251. self.__ged_method = F1(self.__ged_data)
  252. elif method == Options.GEDMethod.F2:
  253. self.__ged_method = F2(self.__ged_data)
  254. elif method == Options.GEDMethod.COMPACT_MIP:
  255. self.__ged_method = CompactMIP(self.__ged_data)
  256. elif method == Options.GEDMethod.BLP_NO_EDGE_LABELS:
  257. self.__ged_method = BLPNoEdgeLabels(self.__ged_data)
  258. self.__ged_method.set_options(options)
  259. def run_method(self, g_id, h_id):
  260. """
  261. /*!
  262. * @brief Runs the GED method specified by call to set_method() between the graphs with IDs @p g_id and @p h_id.
  263. * @param[in] g_id ID of an input graph that has been added to the environment.
  264. * @param[in] h_id ID of an input graph that has been added to the environment.
  265. */
  266. """
  267. if g_id >= self.__ged_data.num_graphs():
  268. raise Exception('The graph with ID', str(g_id), 'has not been added to the environment.')
  269. if h_id >= self.__ged_data.num_graphs():
  270. raise Exception('The graph with ID', str(h_id), 'has not been added to the environment.')
  271. if not self.__initialized:
  272. raise Exception('The environment is uninitialized. Call init() after adding all graphs to the environment.')
  273. if self.__ged_method is None:
  274. raise Exception('No method has been set. Call set_method() before calling run().')
  275. # Call selected GEDMethod and store results.
  276. if self.__ged_data.shuffled_graph_copies_available() and (g_id == h_id):
  277. self.__ged_method.run(g_id, self.__ged_data.id_shuffled_graph_copy(h_id)) # @todo: why shuffle?
  278. else:
  279. self.__ged_method.run(g_id, h_id)
  280. self.__lower_bounds[(g_id, h_id)] = self.__ged_method.get_lower_bound()
  281. self.__upper_bounds[(g_id, h_id)] = self.__ged_method.get_upper_bound()
  282. self.__runtimes[(g_id, h_id)] = self.__ged_method.get_runtime()
  283. self.__node_maps[(g_id, h_id)] = self.__ged_method.get_node_map()
  284. def init_method(self):
  285. """Initializes the method specified by call to set_method().
  286. """
  287. if not self.__initialized:
  288. raise Exception('The environment is uninitialized. Call init() before calling init_method().')
  289. if self.__ged_method is None:
  290. raise Exception('No method has been set. Call set_method() before calling init_method().')
  291. self.__ged_method.init()
  292. def get_num_node_labels(self):
  293. """
  294. /*!
  295. * @brief Returns the number of node labels.
  296. * @return Number of pairwise different node labels contained in the environment.
  297. * @note If @p 1 is returned, the nodes are unlabeled.
  298. */
  299. """
  300. return len(self.__ged_data._node_labels)
  301. def get_node_label(self, label_id, to_dict=True):
  302. """
  303. /*!
  304. * @brief Returns node label.
  305. * @param[in] label_id ID of node label that should be returned. Must be between 1 and num_node_labels().
  306. * @return Node label for selected label ID.
  307. */
  308. """
  309. if label_id < 1 or label_id > self.get_num_node_labels():
  310. raise Exception('The environment does not contain a node label with ID', str(label_id), '.')
  311. if to_dict:
  312. return dict(self.__ged_data._node_labels[label_id - 1])
  313. return self.__ged_data._node_labels[label_id - 1]
  314. def get_num_edge_labels(self):
  315. """
  316. /*!
  317. * @brief Returns the number of edge labels.
  318. * @return Number of pairwise different edge labels contained in the environment.
  319. * @note If @p 1 is returned, the edges are unlabeled.
  320. */
  321. """
  322. return len(self.__ged_data._edge_labels)
  323. def get_edge_label(self, label_id, to_dict=True):
  324. """
  325. /*!
  326. * @brief Returns edge label.
  327. * @param[in] label_id ID of edge label that should be returned. Must be between 1 and num_node_labels().
  328. * @return Edge label for selected label ID.
  329. */
  330. """
  331. if label_id < 1 or label_id > self.get_num_edge_labels():
  332. raise Exception('The environment does not contain an edge label with ID', str(label_id), '.')
  333. if to_dict:
  334. return dict(self.__ged_data._edge_labels[label_id - 1])
  335. return self.__ged_data._edge_labels[label_id - 1]
  336. def get_upper_bound(self, g_id, h_id):
  337. """
  338. /*!
  339. * @brief Returns upper bound for edit distance between the input graphs.
  340. * @param[in] g_id ID of an input graph that has been added to the environment.
  341. * @param[in] h_id ID of an input graph that has been added to the environment.
  342. * @return Upper bound computed by the last call to run_method() with arguments @p g_id and @p h_id.
  343. */
  344. """
  345. if (g_id, h_id) not in self.__upper_bounds:
  346. raise Exception('Call run(' + str(g_id) + ',' + str(h_id) + ') before calling get_upper_bound(' + str(g_id) + ',' + str(h_id) + ').')
  347. return self.__upper_bounds[(g_id, h_id)]
  348. def get_lower_bound(self, g_id, h_id):
  349. """
  350. /*!
  351. * @brief Returns lower bound for edit distance between the input graphs.
  352. * @param[in] g_id ID of an input graph that has been added to the environment.
  353. * @param[in] h_id ID of an input graph that has been added to the environment.
  354. * @return Lower bound computed by the last call to run_method() with arguments @p g_id and @p h_id.
  355. */
  356. """
  357. if (g_id, h_id) not in self.__lower_bounds:
  358. raise Exception('Call run(' + str(g_id) + ',' + str(h_id) + ') before calling get_lower_bound(' + str(g_id) + ',' + str(h_id) + ').')
  359. return self.__lower_bounds[(g_id, h_id)]
  360. def get_runtime(self, g_id, h_id):
  361. """
  362. /*!
  363. * @brief Returns runtime.
  364. * @param[in] g_id ID of an input graph that has been added to the environment.
  365. * @param[in] h_id ID of an input graph that has been added to the environment.
  366. * @return Runtime of last call to run_method() with arguments @p g_id and @p h_id.
  367. */
  368. """
  369. if (g_id, h_id) not in self.__runtimes:
  370. raise Exception('Call run(' + str(g_id) + ',' + str(h_id) + ') before calling get_runtime(' + str(g_id) + ',' + str(h_id) + ').')
  371. return self.__runtimes[(g_id, h_id)]
  372. def get_init_time(self):
  373. """
  374. /*!
  375. * @brief Returns initialization time.
  376. * @return Runtime of the last call to init_method().
  377. */
  378. """
  379. return self.__ged_method.get_init_time()
  380. def get_node_map(self, g_id, h_id):
  381. """
  382. /*!
  383. * @brief Returns node map between the input graphs.
  384. * @param[in] g_id ID of an input graph that has been added to the environment.
  385. * @param[in] h_id ID of an input graph that has been added to the environment.
  386. * @return Node map computed by the last call to run_method() with arguments @p g_id and @p h_id.
  387. */
  388. """
  389. if (g_id, h_id) not in self.__node_maps:
  390. raise Exception('Call run(' + str(g_id) + ',' + str(h_id) + ') before calling get_node_map(' + str(g_id) + ',' + str(h_id) + ').')
  391. return self.__node_maps[(g_id, h_id)]
  392. def get_forward_map(self, g_id, h_id) :
  393. """
  394. Returns the forward map (or the half of the adjacence matrix) between nodes of the two indicated graphs.
  395. :param g: The Id of the first compared graph
  396. :param h: The Id of the second compared graph
  397. :type g: size_t
  398. :type h: size_t
  399. :return: The forward map to the adjacence matrix between nodes of the two graphs
  400. :rtype: list[npy_uint32]
  401. .. seealso:: run_method(), get_upper_bound(), get_lower_bound(), get_backward_map(), get_runtime(), quasimetric_cost(), get_node_map(), get_assignment_matrix()
  402. .. warning:: run_method() between the same two graph must be called before this function.
  403. .. 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 !
  404. """
  405. return self.get_node_map(g_id, h_id).forward_map
  406. def get_backward_map(self, g_id, h_id) :
  407. """
  408. Returns the backward map (or the half of the adjacence matrix) between nodes of the two indicated graphs.
  409. :param g: The Id of the first compared graph
  410. :param h: The Id of the second compared graph
  411. :type g: size_t
  412. :type h: size_t
  413. :return: The backward map to the adjacence matrix between nodes of the two graphs
  414. :rtype: list[npy_uint32]
  415. .. seealso:: run_method(), get_upper_bound(), get_lower_bound(), get_forward_map(), get_runtime(), quasimetric_cost(), get_node_map(), get_assignment_matrix()
  416. .. warning:: run_method() between the same two graph must be called before this function.
  417. .. 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 !
  418. """
  419. return self.get_node_map(g_id, h_id).backward_map
  420. def compute_induced_cost(self, g_id, h_id, node_map):
  421. """
  422. /*!
  423. * @brief Computes the edit cost between two graphs induced by a node map.
  424. * @param[in] g_id ID of input graph.
  425. * @param[in] h_id ID of input graph.
  426. * @param[in,out] node_map Node map whose induced edit cost is to be computed.
  427. */
  428. """
  429. self.__ged_data.compute_induced_cost(self.__ged_data._graphs[g_id], self.__ged_data._graphs[h_id], node_map)
  430. def get_nx_graph(self, graph_id):
  431. """
  432. * @brief Returns NetworkX.Graph() representation.
  433. * @param[in] graph_id ID of the selected graph.
  434. """
  435. graph = nx.Graph() # @todo: add graph attributes.
  436. graph.graph['id'] = graph_id
  437. nb_nodes = self.get_graph_num_nodes(graph_id)
  438. original_node_ids = self.get_original_node_ids(graph_id)
  439. node_labels = self.get_graph_node_labels(graph_id, to_dict=True)
  440. graph.graph['original_node_ids'] = original_node_ids
  441. for node_id in range(0, nb_nodes):
  442. graph.add_node(node_id, **node_labels[node_id])
  443. edges = self.get_graph_edges(graph_id, to_dict=True)
  444. for (head, tail), labels in edges.items():
  445. graph.add_edge(head, tail, **labels)
  446. return graph
  447. def get_graph_node_labels(self, graph_id, to_dict=True):
  448. """
  449. Searchs and returns all the labels of nodes on a graph, selected by its ID.
  450. :param graph_id: The ID of the wanted graph
  451. :type graph_id: size_t
  452. :return: The list of nodes' labels on the selected graph
  453. :rtype: list[dict{string : string}]
  454. .. seealso:: get_graph_internal_id(), get_graph_num_nodes(), get_graph_num_edges(), get_original_node_ids(), get_graph_edges(), get_graph_adjacence_matrix()
  455. .. note:: These functions allow to collect all the graph's informations.
  456. """
  457. graph = self.__ged_data.graph(graph_id)
  458. node_labels = []
  459. for n in graph.nodes():
  460. node_labels.append(graph.nodes[n]['label'])
  461. if to_dict:
  462. return [dict(i) for i in node_labels]
  463. return node_labels
  464. def get_graph_edges(self, graph_id, to_dict=True):
  465. """
  466. Searchs and returns all the edges on a graph, selected by its ID.
  467. :param graph_id: The ID of the wanted graph
  468. :type graph_id: size_t
  469. :return: The list of edges on the selected graph
  470. :rtype: dict{tuple(size_t, size_t) : dict{string : string}}
  471. .. 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()
  472. .. note:: These functions allow to collect all the graph's informations.
  473. """
  474. graph = self.__ged_data.graph(graph_id)
  475. if to_dict:
  476. edges = {}
  477. for n1, n2, attr in graph.edges(data=True):
  478. edges[(n1, n2)] = dict(attr['label'])
  479. return edges
  480. return {(n1, n2): attr['label'] for n1, n2, attr in graph.edges(data=True)}
  481. def get_graph_name(self, graph_id):
  482. """
  483. /*!
  484. * @brief Returns the graph name.
  485. * @param[in] graph_id ID of an input graph that has been added to the environment.
  486. * @return Name of the input graph.
  487. */
  488. """
  489. return self.__ged_data._graph_names[graph_id]
  490. def get_graph_num_nodes(self, graph_id):
  491. """
  492. /*!
  493. * @brief Returns the number of nodes.
  494. * @param[in] graph_id ID of an input graph that has been added to the environment.
  495. * @return Number of nodes in the graph.
  496. */
  497. """
  498. return nx.number_of_nodes(self.__ged_data.graph(graph_id))
  499. def get_original_node_ids(self, graph_id):
  500. """
  501. Searchs and returns all th Ids of nodes on a graph, selected by its ID.
  502. :param graph_id: The ID of the wanted graph
  503. :type graph_id: size_t
  504. :return: The list of IDs's nodes on the selected graph
  505. :rtype: list[string]
  506. .. seealso::get_graph_internal_id(), get_graph_num_nodes(), get_graph_num_edges(), get_graph_node_labels(), get_graph_edges(), get_graph_adjacence_matrix()
  507. .. note:: These functions allow to collect all the graph's informations.
  508. """
  509. return [i for i in self.__internal_to_original_node_ids[graph_id].values()]
  510. def get_node_rel_cost(self, node_label_1, node_label_2):
  511. """
  512. /*!
  513. * @brief Returns node relabeling cost.
  514. * @param[in] node_label_1 First node label.
  515. * @param[in] node_label_2 Second node label.
  516. * @return Node relabeling cost for the given node labels.
  517. */
  518. """
  519. if isinstance(node_label_1, dict):
  520. node_label_1 = tuple(sorted(node_label_1.items(), key=lambda kv: kv[0]))
  521. if isinstance(node_label_2, dict):
  522. node_label_2 = tuple(sorted(node_label_2.items(), key=lambda kv: kv[0]))
  523. return self.__ged_data._edit_cost.node_rel_cost_fun(node_label_1, node_label_2)
  524. def get_node_del_cost(self, node_label):
  525. """
  526. /*!
  527. * @brief Returns node deletion cost.
  528. * @param[in] node_label Node label.
  529. * @return Cost of deleting node with given label.
  530. */
  531. """
  532. if isinstance(node_label, dict):
  533. node_label = tuple(sorted(node_label.items(), key=lambda kv: kv[0]))
  534. return self.__ged_data._edit_cost.node_del_cost_fun(node_label)
  535. def get_node_ins_cost(self, node_label):
  536. """
  537. /*!
  538. * @brief Returns node insertion cost.
  539. * @param[in] node_label Node label.
  540. * @return Cost of inserting node with given label.
  541. */
  542. """
  543. if isinstance(node_label, dict):
  544. node_label = tuple(sorted(node_label.items(), key=lambda kv: kv[0]))
  545. return self.__ged_data._edit_cost.node_ins_cost_fun(node_label)
  546. def get_edge_rel_cost(self, edge_label_1, edge_label_2):
  547. """
  548. /*!
  549. * @brief Returns edge relabeling cost.
  550. * @param[in] edge_label_1 First edge label.
  551. * @param[in] edge_label_2 Second edge label.
  552. * @return Edge relabeling cost for the given edge labels.
  553. */
  554. """
  555. if isinstance(edge_label_1, dict):
  556. edge_label_1 = tuple(sorted(edge_label_1.items(), key=lambda kv: kv[0]))
  557. if isinstance(edge_label_2, dict):
  558. edge_label_2 = tuple(sorted(edge_label_2.items(), key=lambda kv: kv[0]))
  559. return self.__ged_data._edit_cost.edge_rel_cost_fun(edge_label_1, edge_label_2)
  560. def get_edge_del_cost(self, edge_label):
  561. """
  562. /*!
  563. * @brief Returns edge deletion cost.
  564. * @param[in] edge_label Edge label.
  565. * @return Cost of deleting edge with given label.
  566. */
  567. """
  568. if isinstance(edge_label, dict):
  569. edge_label = tuple(sorted(edge_label.items(), key=lambda kv: kv[0]))
  570. return self.__ged_data._edit_cost.edge_del_cost_fun(edge_label)
  571. def get_edge_ins_cost(self, edge_label):
  572. """
  573. /*!
  574. * @brief Returns edge insertion cost.
  575. * @param[in] edge_label Edge label.
  576. * @return Cost of inserting edge with given label.
  577. */
  578. """
  579. if isinstance(edge_label, dict):
  580. edge_label = tuple(sorted(edge_label.items(), key=lambda kv: kv[0]))
  581. return self.__ged_data._edit_cost.edge_ins_cost_fun(edge_label)
  582. def get_all_graph_ids(self):
  583. 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.