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_data.py 7.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Jun 17 15:05:01 2020
  5. @author: ljia
  6. """
  7. from gklearn.ged.env import Options, OptionsStringMap
  8. from gklearn.ged.edit_costs import Constant
  9. from gklearn.utils import SpecialLabel, dummy_node
  10. class GEDData(object):
  11. def __init__(self):
  12. self._graphs = []
  13. self._graph_names = []
  14. self._graph_classes = []
  15. self._num_graphs_without_shuffled_copies = 0
  16. self._strings_to_internal_node_ids = []
  17. self._internal_node_ids_to_strings = []
  18. self._edit_cost = None
  19. self._node_costs = None
  20. self._edge_costs = None
  21. self._node_labels = []
  22. self._edge_labels = []
  23. self._init_type = Options.InitType.EAGER_WITHOUT_SHUFFLED_COPIES
  24. self._delete_edit_cost = True
  25. self._max_num_nodes = 0
  26. self._max_num_edges = 0
  27. def num_graphs(self):
  28. """
  29. /*!
  30. * @brief Returns the number of graphs.
  31. * @return Number of graphs in the instance.
  32. */
  33. """
  34. return len(self._graphs)
  35. def shuffled_graph_copies_available(self):
  36. """
  37. /*!
  38. * @brief Checks if shuffled graph copies are available.
  39. * @return Boolean @p true if shuffled graph copies are available.
  40. */
  41. """
  42. return (self._init_type == Options.InitType.EAGER_WITH_SHUFFLED_COPIES or self._init_type == Options.InitType.LAZY_WITH_SHUFFLED_COPIES)
  43. def node_cost(self, label1, label2):
  44. """
  45. /*!
  46. * @brief Returns node relabeling, insertion, or deletion cost.
  47. * @param[in] label1 First node label.
  48. * @param[in] label2 Second node label.
  49. * @return Node relabeling cost if @p label1 and @p label2 are both different from ged::dummy_label(),
  50. * node insertion cost if @p label1 equals ged::dummy_label and @p label2 does not,
  51. * node deletion cost if @p label1 does not equal ged::dummy_label and @p label2 does,
  52. * and 0 otherwise.
  53. */
  54. """
  55. if self._eager_init(): # @todo: check if correct
  56. return self._node_costs[label1, label2]
  57. if label1 == label2:
  58. return 0
  59. if label1 == SpecialLabel.DUMMY: # @todo: check dummy
  60. return self._edit_cost.node_ins_cost_fun(label2) # self._node_labels[label2 - 1]) # @todo: check
  61. if label2 == SpecialLabel.DUMMY: # @todo: check dummy
  62. return self._edit_cost.node_del_cost_fun(label1) # self._node_labels[label1 - 1])
  63. return self._edit_cost.node_rel_cost_fun(label1, label2) # self._node_labels[label1 - 1], self._node_labels[label2 - 1])
  64. def edge_cost(self, label1, label2):
  65. """
  66. /*!
  67. * @brief Returns edge relabeling, insertion, or deletion cost.
  68. * @param[in] label1 First edge label.
  69. * @param[in] label2 Second edge label.
  70. * @return Edge relabeling cost if @p label1 and @p label2 are both different from ged::dummy_label(),
  71. * edge insertion cost if @p label1 equals ged::dummy_label and @p label2 does not,
  72. * edge deletion cost if @p label1 does not equal ged::dummy_label and @p label2 does,
  73. * and 0 otherwise.
  74. */
  75. """
  76. if self._eager_init(): # @todo: check if correct
  77. return self._node_costs[label1, label2]
  78. if label1 == label2:
  79. return 0
  80. if label1 == SpecialLabel.DUMMY:
  81. return self._edit_cost.edge_ins_cost_fun(label2) # self._edge_labels[label2 - 1])
  82. if label2 == SpecialLabel.DUMMY:
  83. return self._edit_cost.edge_del_cost_fun(label1) # self._edge_labels[label1 - 1])
  84. return self._edit_cost.edge_rel_cost_fun(label1, label2) # self._edge_labels[label1 - 1], self._edge_labels[label2 - 1])
  85. def compute_induced_cost(self, g, h, node_map):
  86. """
  87. /*!
  88. * @brief Computes the edit cost between two graphs induced by a node map.
  89. * @param[in] g Input graph.
  90. * @param[in] h Input graph.
  91. * @param[in,out] node_map Node map whose induced edit cost is to be computed.
  92. */
  93. """
  94. cost = 0
  95. # collect node costs
  96. for node in g.nodes():
  97. image = node_map.image(node)
  98. label2 = (SpecialLabel.DUMMY if image == dummy_node() else h.nodes[image]['label'])
  99. cost += self.node_cost(g.nodes[node]['label'], label2)
  100. for node in h.nodes():
  101. pre_image = node_map.pre_image(node)
  102. if pre_image == dummy_node():
  103. cost += self.node_cost(SpecialLabel.DUMMY, h.nodes[node]['label'])
  104. # collect edge costs
  105. for (n1, n2) in g.edges():
  106. image1 = node_map.image(n1)
  107. image2 = node_map.image(n2)
  108. label2 = (h.edges[(image2, image1)]['label'] if h.has_edge(image2, image1) else SpecialLabel.DUMMY)
  109. cost += self.edge_cost(g.edges[(n1, n2)]['label'], label2)
  110. for (n1, n2) in h.edges():
  111. if not g.has_edge(node_map.pre_image(n2), node_map.pre_image(n1)):
  112. cost += self.edge_cost(SpecialLabel.DUMMY, h.edges[(n1, n2)]['label'])
  113. node_map.set_induced_cost(cost)
  114. def _set_edit_cost(self, edit_cost, edit_cost_constants):
  115. if self._delete_edit_cost:
  116. self._edit_cost = None
  117. if isinstance(edit_cost, str):
  118. edit_cost = OptionsStringMap.EditCosts[edit_cost]
  119. if edit_cost == Options.EditCosts.CHEM_1:
  120. if len(edit_cost_constants) == 4:
  121. self._edit_cost = CHEM1(edit_cost_constants[0], edit_cost_constants[1], edit_cost_constants[2], edit_cost_constants[3])
  122. elif len(edit_cost_constants) == 0:
  123. self._edit_cost = CHEM1()
  124. else:
  125. raise Exception('Wrong number of constants for selected edit costs Options::EditCosts::CHEM_1. Expected: 4 or 0; actual:', len(edit_cost_constants), '.')
  126. elif edit_cost == Options.EditCosts.LETTER:
  127. if len(edit_cost_constants) == 3:
  128. self._edit_cost = Letter(edit_cost_constants[0], edit_cost_constants[1], edit_cost_constants[2])
  129. elif len(edit_cost_constants) == 0:
  130. self._edit_cost = Letter()
  131. else:
  132. raise Exception('Wrong number of constants for selected edit costs Options::EditCosts::LETTER. Expected: 3 or 0; actual:', len(edit_cost_constants), '.')
  133. elif edit_cost == Options.EditCosts.LETTER2:
  134. if len(edit_cost_constants) == 5:
  135. self._edit_cost = Letter2(edit_cost_constants[0], edit_cost_constants[1], edit_cost_constants[2], edit_cost_constants[3], edit_cost_constants[4])
  136. elif len(edit_cost_constants) == 0:
  137. self._edit_cost = Letter2()
  138. else:
  139. raise Exception('Wrong number of constants for selected edit costs Options::EditCosts::LETTER2. Expected: 5 or 0; actual:', len(edit_cost_constants), '.')
  140. elif edit_cost == Options.EditCosts.NON_SYMBOLIC:
  141. if len(edit_cost_constants) == 6:
  142. self._edit_cost = NonSymbolic(edit_cost_constants[0], edit_cost_constants[1], edit_cost_constants[2], edit_cost_constants[3], edit_cost_constants[4], edit_cost_constants[5])
  143. elif len(edit_cost_constants) == 0:
  144. self._edit_cost = NonSymbolic()
  145. else:
  146. raise Exception('Wrong number of constants for selected edit costs Options::EditCosts::NON_SYMBOLIC. Expected: 6 or 0; actual:', len(edit_cost_constants), '.')
  147. elif edit_cost == Options.EditCosts.CONSTANT:
  148. if len(edit_cost_constants) == 6:
  149. self._edit_cost = Constant(edit_cost_constants[0], edit_cost_constants[1], edit_cost_constants[2], edit_cost_constants[3], edit_cost_constants[4], edit_cost_constants[5])
  150. elif len(edit_cost_constants) == 0:
  151. self._edit_cost = Constant()
  152. else:
  153. raise Exception('Wrong number of constants for selected edit costs Options::EditCosts::CONSTANT. Expected: 6 or 0; actual:', len(edit_cost_constants), '.')
  154. self._delete_edit_cost = True
  155. def _eager_init(self):
  156. return (self._init_type == Options.InitType.EAGER_WITHOUT_SHUFFLED_COPIES or self._init_type == Options.InitType.EAGER_WITH_SHUFFLED_COPIES)

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