Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion hypergraphs/productions/p2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']:
Expand All @@ -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]
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion hypergraphs/productions/p4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions hypergraphs/productions/p6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
66 changes: 54 additions & 12 deletions hypergraphs/tests/test_p5_p6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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)]
Expand Down
14 changes: 13 additions & 1 deletion hypergraphs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class HyperEdge(Enum):
I = 'I'
B = 'B'


class Direction(Enum):
N = 1
S = 2
Expand Down Expand Up @@ -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))