def get_nd_regular_implicit_graph(shape, neighbour_list): """ Creates an implicit regular graph of the given :attr:`shape` with the adjacency given as a :attr:`neighbour_list`. See the helper function :func:`~higra.mask_2_neighbours` to create a suitable :attr:`neighbour_list`. :Example: Create a 2d 4-adjacency implicit graph of size ``(13, 24)``: >>> graph = get_nd_regular_implicit_graph((13, 24), ((-1, 0), (0, -1), (0, 1), (1, 0))) Create a 3d 6-adjacency implicit graph of size ``(10, 13, 24)``: >>> mask = [[[0, 0, 0], [0, 1, 0], [0, 0, 0]], >>> [[0, 1, 0], [1, 0, 1], [0, 1, 0]], >>> [[0, 0, 0], [0, 1, 0], [0, 0, 0]]] >>> neighbours = mask_2_neighbours(mask) >>> graph = get_nd_regular_implicit_graph((10, 13, 24), neighbours) :param shape: a tuple of :math:`n` elements representing the dimension of the graph vertices. :param neighbour_list: a 2d array of :math:`k` :math:`n`-d integer vectors :return: an implicit regular graph """ neighbour_list = np.asarray(neighbour_list) if not np.issubdtype(neighbour_list.dtype, np.integer): raise ValueError("'neighbour_list' must be of integral type.") if neighbour_list.ndim != 2: raise ValueError("'neighbour_list' must be a 2d array.") shape = hg.normalize_shape(shape) if len(shape) != neighbour_list.shape[1]: raise ValueError( "Shape size does not match provided adjacency dimension.") if len(shape) > 5 or len(shape) == 0: raise ValueError("Shape size must between 1 and 5 (included).") if len(shape) == 1: graph = hg.RegularGraph1d(shape, neighbour_list) elif len(shape) == 2: graph = hg.RegularGraph2d(shape, neighbour_list) elif len(shape) == 3: graph = hg.RegularGraph3d(shape, neighbour_list) elif len(shape) == 4: graph = hg.RegularGraph4d(shape, neighbour_list) elif len(shape) == 5: graph = hg.RegularGraph5d(shape, neighbour_list) hg.CptGridGraph.link(graph, shape) hg.set_attribute(graph, "no_border_vertex_out_degree", neighbour_list.shape[0]) return graph
def test_create_graph(self): shape = (2, 3) nl = ((-1, 0), (0, -1), (0, 1), (1, 0)) g1 = hg.RegularGraph2d(hg.EmbeddingGrid2d(shape), nl) g2 = hg.get_4_adjacency_implicit_graph(shape) g3 = hg.get_8_adjacency_implicit_graph(shape) for g in (g1, g2, g3): self.assertTrue(g.num_vertices() == 6) self.assertTrue(np.all(g1.shape() == shape)) self.assertTrue(np.all(g1.neighbour_list() == nl)) g4 = hg.RegularGraph2d(g1.shape(), g1.neighbour_list()) self.assertTrue(np.all(g4.shape() == shape)) self.assertTrue(np.all(g4.neighbour_list() == nl))
def test_create_graph(self): shape = (2, 3) nl = ((-1, 0), (0, -1), (0, 1), (1, 0)) g1 = hg.RegularGraph2d(hg.EmbeddingGrid2d(shape), nl) g2 = hg.get_4_adjacency_implicit_graph(shape) g3 = hg.get_8_adjacency_implicit_graph(shape) for g in (g1, g2, g3): self.assertTrue(g.num_vertices() == 6)
def get_4_adjacency_implicit_graph(shape): """ Create an implicit undirected 4 adjacency graph of the given shape (edges are not stored). :param shape: a pair (height, width) :return: a graph (Concept :class:`~higra.CptGridGraph`) """ shape = hg.normalize_shape(shape) if len(shape) != 2: raise ValueError("Shape must be a 1d array of size 2.") neighbours = np.array(((-1, 0), (0, -1), (0, 1), (1, 0)), dtype=np.int64) graph = hg.RegularGraph2d(shape, neighbours) hg.CptGridGraph.link(graph, shape) hg.set_attribute(graph, "no_border_vertex_out_degree", 4) return graph