def test_v_connected_with_f_hyperedge(self): edges = self.hyperedges[Direction.N] self.run_P3(edges) bx, by = edges['b'][1]['x'], edges['b'][1]['y'] new_node_id = get_node_id((bx, by)) neighbours = list(self.graph.neighbors(new_node_id)) self.assertTrue(edges['f'][0] in neighbours)
def setUp(self): self.graph = nx.Graph() self.image = Image.open(IMAGE_PATH) self.hyperedge = self.prepare_graph_and_get_central_hyperedge() self.added_node_position = (self.hyperedge[1]['x'], self.hyperedge[1]['y']) self.added_node_id = get_node_id(self.added_node_position) # plot(self.graph) P2(self.graph, hyperedge_id=self.hyperedge[0], image=self.image)
def test_v_connected_with_b_hyperedges(self): edges = self.hyperedges[Direction.N] self.run_P3(edges) bx, by = edges['b'][1]['x'], edges['b'][1]['y'] new_node_id = get_node_id((bx, by)) neighbours = list(self.graph.neighbors(new_node_id)) bs = [(x, y) for x, y in self.graph.nodes(data=True) if x in neighbours and 'label' in y.keys() and y['label'] == 'B'] self.assertTrue(len(bs) == 2)
def P3(graph: nx.Graph, hyp_b, hyp_is, hyp_f, image: Image): __assert_hyper_edge(graph, [hyp_b], 'B') __assert_hyper_edge(graph, hyp_is, 'I') __assert_hyper_edge(graph, [hyp_f]) hyp_f_data = graph.node[hyp_f] hyp_b_data = graph.node[hyp_b] hyp_f_neighbour_ids = list(graph.neighbors(hyp_f)) if len(hyp_f_neighbour_ids) != 1: raise ValueError('F should have 1 neighbour') for hyp_i in hyp_is: if hyp_f_neighbour_ids[0] not in list(graph.neighbors(hyp_i)): raise ValueError('I is not connected with F1 via neighbour') hyp_f_neighbour_data = graph.node[hyp_f_neighbour_ids[0]] if hyp_f_data['label'] == Direction.N.name: if hyp_f_data['x'] != hyp_f_neighbour_data['x'] or hyp_f_data['y'] <= hyp_f_neighbour_data['y']: raise ValueError('F hyperedge has weird position') if hyp_f_data['x'] != hyp_b_data['x'] or hyp_f_data['y'] >= hyp_b_data['y']: raise ValueError('F hyperedge has weird position (B)') elif hyp_f_data['label'] == Direction.S.name: if hyp_f_data['x'] != hyp_f_neighbour_data['x'] or hyp_f_data['y'] >= hyp_f_neighbour_data['y']: raise ValueError('F hyperedge has weird position') if hyp_f_data['x'] != hyp_b_data['x'] or hyp_f_data['y'] <= hyp_b_data['y']: raise ValueError('F hyperedge has weird position (B)') elif hyp_f_data['label'] == Direction.E.name: if hyp_f_data['x'] <= hyp_f_neighbour_data['x'] or hyp_f_data['y'] != hyp_f_neighbour_data['y']: raise ValueError('F hyperedge has weird position') if hyp_f_data['x'] >= hyp_b_data['x'] or hyp_f_data['y'] != hyp_b_data['y']: raise ValueError('F hyperedge has weird position (B)') elif hyp_f_data['label'] == Direction.W.name: if hyp_f_data['x'] >= hyp_f_neighbour_data['x'] or hyp_f_data['y'] != hyp_f_neighbour_data['y']: raise ValueError('F hyperedge has weird position') if hyp_f_data['x'] <= hyp_b_data['x'] or hyp_f_data['y'] != hyp_b_data['y']: raise ValueError('F hyperedge has weird position (B)') else: raise ValueError('F hyperedge has weird label') new_node_position = (hyp_b_data['x'], hyp_b_data['y']) new_node_id = get_node_id(new_node_position) __add_new_node(graph, image, new_node_id, new_node_position) # add v __add_hyperedges_between_neighbour_nodes(graph, hyp_b, new_node_id, new_node_position) # add 1-b-v-b-2 for hyp_i in hyp_is: __add_edges_between_nodes(graph, new_node_id, hyp_i) # add v-i and v-i __add_edges_between_nodes(graph, new_node_id, hyp_f) # add v-f1 graph.remove_node(hyp_b)
def add_nodes(graph: Graph, image: Image, node_positions: List[Tuple[int, int]]) -> None: for node_position in node_positions: colors = image.getpixel(node_position) graph.add_node( get_node_id(node_position), x=node_position[0], y=node_position[1], is_hyperedge=False, r=colors[0], g=colors[1], b=colors[2], )
def P2(graph: nx.Graph, hyperedge_id, image: Image): __assert_hyper_edge(graph, hyperedge_id) hyperedge_data = graph.node[hyperedge_id] new_node_position = (hyperedge_data['x'], hyperedge_data['y']) new_node_id = get_node_id(new_node_position) old_depth = hyperedge_data.get("depth", 0) new_depth = old_depth + 1 __add_new_node(graph, image, new_node_id, new_node_position) __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)
def add_hyperedge_edges(graph: Graph, hyperedge_id: UUID, neighbors: List[Tuple[int, int]]) -> None: for neighbor in neighbors: graph.add_edge(hyperedge_id, get_node_id(neighbor))
def P1(graph: nx.Graph, x_max_idx: int, y_max_idx: int, image: Image): """ Node ids are x,y position pairs Hyperedge ids are uuid :param graph: an empty nx.Graph object :param y_max_idx: maximum index of picture y (picture y size - 1) :param x_max_idx: picture x size (picture x size - 1) :return: None """ node_positions = ( (0, 0), (0, y_max_idx), (x_max_idx, y_max_idx), (x_max_idx, 0), ) # add common nodes for node_position in node_positions: rgb = image.getpixel((node_position)) graph.add_node( get_node_id(node_position), x=node_position[0], y=node_position[1], is_hyperedge=False, r=rgb[0], g=rgb[1], b=rgb[2], ) hyper_node_positions = ((0, y_max_idx // 2), (x_max_idx // 2, y_max_idx), (x_max_idx, y_max_idx // 2), (x_max_idx // 2, 0)) hyper_node_connections = (((0, 0), (0, y_max_idx)), ((0, y_max_idx), (x_max_idx, y_max_idx)), ((x_max_idx, y_max_idx), (x_max_idx, 0)), ((x_max_idx, 0), (0, 0))) # add hyper nodes for hyper_node_position, hyper_node_connection in zip( hyper_node_positions, hyper_node_connections): hyper_node_id = uuid.uuid4() graph.add_node( hyper_node_id, x=hyper_node_position[0], y=hyper_node_position[1], is_hyperedge=True, label='B', ) for connection in hyper_node_connection: graph.add_edge(hyper_node_id, get_node_id(connection)) # create the hyperedge and connect it with four created nodes hyperedge_id = uuid.uuid4() hyperedge_position = x_max_idx // 2, y_max_idx // 2 graph.add_node( hyperedge_id, x=hyperedge_position[0], y=hyperedge_position[1], is_hyperedge=True, label='I', should_break=0, ) for node_position in node_positions: graph.add_edge(hyperedge_id, get_node_id(node_position))