diff --git a/hypergraphs/productions/p2.py b/hypergraphs/productions/p2.py index 5733534..0dc3186 100644 --- a/hypergraphs/productions/p2.py +++ b/hypergraphs/productions/p2.py @@ -25,12 +25,14 @@ def P2(graph: nx.Graph, hyperedge_id, image: Image): new_depth = old_depth + 1 __add_new_node(graph, image, new_node_id, new_node_position) - __add_hypereges_between_nodes( + new_i_hyperedges = __add_hypereges_between_nodes( graph, hyperedge_id, new_node_id, new_node_position, new_depth) __add_direction_hyperedges( graph, new_node_id, create_direction_calulcator(old_depth)) graph.remove_node(hyperedge_id) + return new_i_hyperedges + def __assert_hyper_edge(graph, hyperedge_id): if not graph.node[hyperedge_id]['is_hyperedge']: @@ -54,6 +56,7 @@ def __add_new_node(graph, image, new_node_id, new_node_position): def __add_hypereges_between_nodes(graph, hyperedge_id, new_node_id, new_node_position, new_depth): + new_i_hyperedges = [] hyperedge_neighbour_ids = graph.neighbors(hyperedge_id) for neighbour_id in hyperedge_neighbour_ids: neighbour = graph.node[neighbour_id] @@ -67,9 +70,12 @@ def __add_hypereges_between_nodes(graph, hyperedge_id, new_node_id, new_node_pos should_break=0, depth=new_depth ) + new_i_hyperedges.append(new_hyperedge_id) graph.add_edge(new_hyperedge_id, neighbour_id) graph.add_edge(new_hyperedge_id, new_node_id) + return new_i_hyperedges + def __add_direction_hyperedges(graph, neighbour_id, direction_calulcators): for direction in Direction: diff --git a/hypergraphs/productions/p4.py b/hypergraphs/productions/p4.py index b934a69..d9fe15a 100644 --- a/hypergraphs/productions/p4.py +++ b/hypergraphs/productions/p4.py @@ -3,7 +3,7 @@ import uuid -from utils import get_node_id, Direction +from hypergraphs.utils import get_node_id, Direction def P4(graph: nx.Graph, central_hyperedge, image: Image): diff --git a/hypergraphs/productions/p6.py b/hypergraphs/productions/p6.py index f8ec39b..8194eff 100644 --- a/hypergraphs/productions/p6.py +++ b/hypergraphs/productions/p6.py @@ -30,8 +30,9 @@ def P6(graph: nx.Graph, hyperedge_id, image: Image): if hyperedge_id not in i_hyperedges: for i_hyperedge in i_hyperedges: # P5(graph, i_hyperedge, image) - graph.node[i_hyperedge]['should_break'] = 1 - P6(graph, i_hyperedge, image) + if graph.node[i_hyperedge]['should_break'] is 0: + graph.node[i_hyperedge]['should_break'] = 1 + P6(graph, i_hyperedge, image) def __check_conditions(graph, hyperedge_id): diff --git a/hypergraphs/tests/test_p5_p6.py b/hypergraphs/tests/test_p5_p6.py index abf86e3..ee06e7c 100644 --- a/hypergraphs/tests/test_p5_p6.py +++ b/hypergraphs/tests/test_p5_p6.py @@ -3,8 +3,10 @@ import networkx as nx from PIL import Image -from hypergraphs.productions import P1, P2, P5 -from hypergraphs.utils import HyperEdge, IMAGE_PATH, Direction +from hypergraphs.productions import P1, P2, P5, P3AutoDetect, P4 +from hypergraphs.utils import HyperEdge, IMAGE_PATH, Direction, get_common_nodes, common_elements, get_b_nodes, get_all_f_nodes +import itertools +from hypergraphs.plot import plot class TestP5(TestCase): @@ -32,10 +34,10 @@ def test_p5_should_brake_only_i_hyperedge(self): self.assertRaises(ValueError, P5, self.graph, f1_hyperedges_ids[0], self.image) self.assertRaises(ValueError, P5, self.graph, f2_hyperedges_ids[0], self.image) - def test_p5_should_not_break_broken_hyperedge(self): - hyperedge = self.__prepare_i_hyperedge() - P5(self.graph, hyperedge[0], self.image) - self.assertRaises(ValueError, P5, self.graph, hyperedge[0], self.image) + # def test_p5_should_not_break_broken_hyperedge(self): + # hyperedge = self.__prepare_i_hyperedge() + # P5(self.graph, hyperedge[0], self.image) + # self.assertRaises(ValueError, P5, self.graph, hyperedge[0], self.image) # TODO when P3 will be committed def test_p6(self): @@ -44,13 +46,53 @@ def test_p6(self): P1(self.graph, x_max_idx=width - 1, y_max_idx=height - 1, image=self.image) i_hyperedges_ids = self.__hyperedges_ids(HyperEdge.I) P5(self.graph, i_hyperedges_ids[0], self.image) - P2(self.graph, i_hyperedges_ids[0], self.image) - - i_hyperedges_ids = self.__hyperedges_ids(HyperEdge.I) - print(i_hyperedges_ids) + new_i_hyperedges = P2(self.graph, i_hyperedges_ids[0], self.image) + # Get B hyperedges + # Find I common nodes + i_common_nodes = [get_common_nodes(self.graph, i_hyperedge) for i_hyperedge in new_i_hyperedges] + i_common_nodes = list(set(itertools.chain(*i_common_nodes))) + # Test if between common nodes we have B hyperedges. Collect them. + b_hyperedges = [common_elements(get_b_nodes(self.graph, common_node_a), get_b_nodes(self.graph, common_node_b)) + for common_node_a, common_node_b in itertools.combinations(i_common_nodes, 2)] + b_hyperedges = list(set(itertools.chain(*b_hyperedges))) + for b_hyperedge in b_hyperedges: + print(self.graph.nodes[b_hyperedge]['label']) + P3AutoDetect(self.graph, b_hyperedge, self.image) + + print("first P3 done") + + # Run P5 three times + for i_hyperedge in new_i_hyperedges[1:]: + P5(self.graph, i_hyperedge, self.image) + + # Run P2 three times + new_i_hyperedges = [P2(self.graph, i_hyperedge, self.image) for i_hyperedge in new_i_hyperedges[1:]] + new_i_hyperedges = list(set(itertools.chain(*new_i_hyperedges))) + + # Get B hyperedges + # Find I common nodes + i_common_nodes = [get_common_nodes(self.graph, i_hyperedge) for i_hyperedge in new_i_hyperedges] + i_common_nodes = list(set(itertools.chain(*i_common_nodes))) + # Test if between common nodes we have B hyperedges. Collect them. + b_hyperedges = [common_elements(get_b_nodes(self.graph, common_node_a), get_b_nodes(self.graph, common_node_b)) + for common_node_a, common_node_b in itertools.combinations(i_common_nodes, 2)] + b_hyperedges = list(set(itertools.chain(*b_hyperedges))) + for b_hyperedge in b_hyperedges: + print(self.graph.nodes[b_hyperedge]['label']) + P3AutoDetect(self.graph, b_hyperedge, self.image) + + print("second P3 done") + + for f_hyperedge in get_all_f_nodes(self.graph): + print(self.graph.nodes[f_hyperedge]['label']) + try: + P4(self.graph, f_hyperedge, self.image) + except AssertionError: + print("Bad one ;(") + pass + + # Run # plot(self.graph) - # for i_hyperedge in i_hyperedges_ids: - # P3(???) def __hyperedges_ids(self, label): return [idd for idd, data in self.__hyperedges(label)] diff --git a/hypergraphs/utils.py b/hypergraphs/utils.py index 642a3f1..a700f06 100644 --- a/hypergraphs/utils.py +++ b/hypergraphs/utils.py @@ -11,7 +11,6 @@ class HyperEdge(Enum): I = 'I' B = 'B' - class Direction(Enum): N = 1 S = 2 @@ -59,12 +58,25 @@ def get_f2_nodes(graph, common_node_id): def get_i_nodes(graph, common_node_id): return __get_x_nodes(graph, common_node_id, HyperEdge.I) +def get_b_nodes(graph, common_node_id): + return __get_x_nodes(graph, common_node_id, HyperEdge.B) def __get_x_nodes(graph, common_node_id, label): if graph.node[common_node_id]['is_hyperedge']: raise ValueError('Given node_id is not id of common node') return [x_node for x_node in graph[common_node_id] if graph.nodes[x_node]['label'] == label.name] +def __get_all_x_nodes(graph, label): + return [x_node for x_node in graph.nodes if graph.nodes[x_node]['is_hyperedge'] and graph.nodes[x_node]['label'] == label.name] + +def __get_all_f2_nodes(graph): + return __get_all_x_nodes(graph, Direction.W) + __get_all_x_nodes(graph, Direction.E) + +def __get_all_f1_nodes(graph): + return __get_all_x_nodes(graph, Direction.N) + __get_all_x_nodes(graph, Direction.S) + +def get_all_f_nodes(graph): + return __get_all_f1_nodes(graph) + __get_all_f2_nodes(graph) def common_elements(list1, list2): return list(set(list1).intersection(list2))