Beispiel #1
0
def test_RectGrid_points():
    vec1 = np.array([2, 3, 4, 5])
    vec2 = np.array([-4, -2, 0, 2, 4])
    scalar = 0.5

    # C ordering
    points = []
    for x1 in vec1:
        for x2 in vec2:
            points.append(np.array((x1, x2), dtype=float))

    grid = RectGrid(vec1, vec2)
    assert all_equal(points, grid.points())
    assert all_equal(points, grid.points(order='C'))
    assert all_equal(grid.min_pt, grid.points()[0])
    assert all_equal(grid.max_pt, grid.points()[-1])

    # F ordering
    points = []
    for x2 in vec2:
        for x1 in vec1:
            points.append(np.array((x1, x2), dtype=float))

    grid = RectGrid(vec1, vec2)
    assert all_equal(points, grid.points(order='F'))

    # Degenerate axis 1
    points = []
    for x1 in vec1:
        for x2 in vec2:
            points.append(np.array((scalar, x1, x2), dtype=float))

    grid = RectGrid(scalar, vec1, vec2)
    assert all_equal(points, grid.points())

    # Degenerate axis 2
    points = []
    for x1 in vec1:
        for x2 in vec2:
            points.append(np.array((x1, scalar, x2), dtype=float))

    grid = RectGrid(vec1, scalar, vec2)
    assert all_equal(points, grid.points())

    # Degenerate axis 3
    points = []
    for x1 in vec1:
        for x2 in vec2:
            points.append(np.array((x1, x2, scalar), dtype=float))

    grid = RectGrid(vec1, vec2, scalar)
    assert all_equal(points, grid.points())

    # Bad input
    with pytest.raises(ValueError):
        grid.points(order='A')
    def corners(self, order='C'):
        """Return the corner points as a single array.

        Parameters
        ----------
        order : {'C', 'F'}, optional
            Ordering of the axes in which the corners appear in
            the output. ``'C'`` means that the first axis varies slowest
            and the last one fastest, vice versa in ``'F'`` ordering.

        Returns
        -------
        corners : `numpy.ndarray`
            Array containing the corner coordinates. The size of the
            array is ``2^m x ndim``, where ``m`` is the number of
            non-degenerate axes, i.e. the corners are stored as rows.

        Examples
        --------
        >>> intv = IntervalProd([-1, 2, 0], [-0.5, 3, 0.5])
        >>> intv.corners()
        array([[-1. ,  2. ,  0. ],
               [-1. ,  2. ,  0.5],
               [-1. ,  3. ,  0. ],
               [-1. ,  3. ,  0.5],
               [-0.5,  2. ,  0. ],
               [-0.5,  2. ,  0.5],
               [-0.5,  3. ,  0. ],
               [-0.5,  3. ,  0.5]])
        >>> intv.corners(order='F')
        array([[-1. ,  2. ,  0. ],
               [-0.5,  2. ,  0. ],
               [-1. ,  3. ,  0. ],
               [-0.5,  3. ,  0. ],
               [-1. ,  2. ,  0.5],
               [-0.5,  2. ,  0.5],
               [-1. ,  3. ,  0.5],
               [-0.5,  3. ,  0.5]])
        """
        from odl.discr.grid import RectGrid

        minmax_vecs = [0] * self.ndim
        for axis in np.where(~self.nondegen_byaxis)[0]:
            minmax_vecs[axis] = self.min_pt[axis]
        for axis in np.where(self.nondegen_byaxis)[0]:
            minmax_vecs[axis] = (self.min_pt[axis], self.max_pt[axis])

        minmax_grid = RectGrid(*minmax_vecs)
        return minmax_grid.points(order=order)
Beispiel #3
0
    def corners(self, order='C'):
        """Return the corner points as a single array.

        Parameters
        ----------
        order : {'C', 'F'}, optional
            Ordering of the axes in which the corners appear in
            the output. ``'C'`` means that the first axis varies slowest
            and the last one fastest, vice versa in ``'F'`` ordering.

        Returns
        -------
        corners : `numpy.ndarray`
            Array containing the corner coordinates. The size of the
            array is ``2^m x ndim``, where ``m`` is the number of
            non-degenerate axes, i.e. the corners are stored as rows.

        Examples
        --------
        >>> rbox = IntervalProd([-1, 2, 0], [-0.5, 3, 0.5])
        >>> rbox.corners()
        array([[-1. ,  2. ,  0. ],
               [-1. ,  2. ,  0.5],
               [-1. ,  3. ,  0. ],
               ...,
               [-0.5,  2. ,  0.5],
               [-0.5,  3. ,  0. ],
               [-0.5,  3. ,  0.5]])
        >>> rbox.corners(order='F')
        array([[-1. ,  2. ,  0. ],
               [-0.5,  2. ,  0. ],
               [-1. ,  3. ,  0. ],
               ...,
               [-0.5,  2. ,  0.5],
               [-1. ,  3. ,  0.5],
               [-0.5,  3. ,  0.5]])
        """
        from odl.discr.grid import RectGrid

        minmax_vecs = [0] * self.ndim
        for axis in np.where(~self.nondegen_byaxis)[0]:
            minmax_vecs[axis] = self.min_pt[axis]
        for axis in np.where(self.nondegen_byaxis)[0]:
            minmax_vecs[axis] = (self.min_pt[axis], self.max_pt[axis])

        minmax_grid = RectGrid(*minmax_vecs)
        return minmax_grid.points(order=order)
Beispiel #4
0
def test_empty_grid():
    """Check if empty grids behave as expected and all methods work."""
    grid = RectGrid()

    assert grid.ndim == grid.size == len(grid) == 0
    assert grid.shape == ()

    assert grid.coord_vectors == ()
    assert grid.nondegen_byaxis == ()
    assert np.array_equal(grid.min_pt, [])
    assert np.array_equal(grid.max_pt, [])
    assert np.array_equal(grid.mid_pt, [])
    assert np.array_equal(grid.stride, [])
    assert np.array_equal(grid.extent, [])
    out = np.array([])
    grid.min(out=out)
    grid.max(out=out)

    assert grid.is_uniform
    assert grid.convex_hull() == odl.IntervalProd([], [])

    same = RectGrid()
    assert grid == same
    assert hash(grid) == hash(same)
    other = RectGrid([0, 2, 3])
    assert grid != other
    assert grid.is_subgrid(other)
    assert [] in grid
    assert 1.0 not in grid

    assert grid.insert(0, other) == other
    assert other.insert(0, grid) == other
    assert other.insert(1, grid) == other
    assert grid.squeeze() == grid
    assert np.array_equal(grid.points(), np.array([]).reshape((0, 0)))
    assert grid.corner_grid() == grid
    assert np.array_equal(grid.corners(), np.array([]).reshape((0, 0)))
    assert grid.meshgrid == ()

    assert grid[[]] == grid
    assert np.array_equal(np.asarray(grid), np.array([]).reshape((0, 0)))
    assert grid == uniform_grid([], [], ())
    repr(grid)