Exemplo n.º 1
0
    def contains_all(self, other):
        """Test if all points defined by ``other`` are contained.

        Parameters
        ----------
        other : object
            Can be a single point, a ``(d, N)`` array where ``d`` is the
            number of dimensions or a length-``d`` meshgrid sequence

        Returns
        -------
        contains : `bool`
            `True` if all points are contained, `False` otherwise

        Examples
        --------
        >>> b, e = [-1, 0, 2], [-0.5, 0, 3]
        >>> rbox = IntervalProd(b, e)
        ...
        ... # Arrays are expected in (ndim, npoints) shape
        >>> arr = np.array([[-1, 0, 2],   # defining one point at a time
        ...                 [-0.5, 0, 2]])
        >>> rbox.contains_all(arr.T)
        True
        >>> # Implicit meshgrids defined by coordinate vectors
        >>> from odl.discr.grid import sparse_meshgrid
        >>> vec1 = (-1, -0.9, -0.7)
        >>> vec2 = (0, 0, 0)
        >>> vec3 = (2.5, 2.75, 3)
        >>> mg = sparse_meshgrid(vec1, vec2, vec3)
        >>> rbox.contains_all(mg)
        True
        """
        if other in self:
            return True
        elif is_valid_input_meshgrid(other, self.ndim):
            order = meshgrid_input_order(other)
            vecs = vecs_from_meshgrid(other, order)
            mins = np.fromiter((np.min(vec) for vec in vecs), dtype=float)
            maxs = np.fromiter((np.max(vec) for vec in vecs), dtype=float)
            return np.all(mins >= self.begin) and np.all(maxs <= self.end)
        elif is_valid_input_array(other, self.ndim):
            if self.ndim == 1:
                mins = np.min(other)
                maxs = np.max(other)
            else:
                mins = np.min(other, axis=1)
                maxs = np.max(other, axis=1)
            return np.all(mins >= self.begin) and np.all(maxs <= self.end)
        else:
            return False
Exemplo n.º 2
0
def test_vecs_from_meshgrid():

    # 1d
    x = np.zeros(2)

    mg = sparse_meshgrid(x, order='C')
    vx = vecs_from_meshgrid(mg, order='C')[0]
    assert x.shape == vx.shape
    assert all_equal(x, vx)

    mg = sparse_meshgrid(x, order='F')
    vx = vecs_from_meshgrid(mg, order='F')[0]
    assert x.shape == vx.shape
    assert all_equal(x, vx)

    # 3d
    x, y, z = np.zeros(2), np.zeros(3), np.zeros(4)

    mg = sparse_meshgrid(x, y, z, order='C')
    vx, vy, vz = vecs_from_meshgrid(mg, order='C')
    assert x.shape == vx.shape
    assert all_equal(x, vx)
    assert y.shape == vy.shape
    assert all_equal(y, vy)
    assert z.shape == vz.shape
    assert all_equal(z, vz)

    mg = sparse_meshgrid(x, y, z, order='F')
    vx, vy, vz = vecs_from_meshgrid(mg, order='F')
    assert x.shape == vx.shape
    assert all_equal(x, vx)
    assert y.shape == vy.shape
    assert all_equal(y, vy)
    assert z.shape == vz.shape
    assert all_equal(z, vz)

    # 3d, fleshed out meshgrids
    x, y, z = np.zeros(2), np.zeros(3), np.zeros(4)
    mg = np.meshgrid(x, y, z, sparse=False, indexing='ij', copy=True)
    vx, vy, vz = vecs_from_meshgrid(mg, order='C')
    assert x.shape == vx.shape
    assert all_equal(x, vx)
    assert y.shape == vy.shape
    assert all_equal(y, vy)
    assert z.shape == vz.shape
    assert all_equal(z, vz)

    mg = np.meshgrid(x, y, z, sparse=False, indexing='ij', copy=True)
    mg = tuple(reversed([np.asfortranarray(arr) for arr in mg]))
    vx, vy, vz = vecs_from_meshgrid(mg, order='F')
    assert x.shape == vx.shape
    assert all_equal(x, vx)
    assert y.shape == vy.shape
    assert all_equal(y, vy)
    assert z.shape == vz.shape
    assert all_equal(z, vz)