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

    grid = RectGrid(vec1, vec2)
    grid2 = RectGrid(scalar, vec3)

    # Test all positions
    ins_grid = grid.insert(0, grid2)
    assert ins_grid == RectGrid(scalar, vec3, vec1, vec2)

    ins_grid = grid.insert(1, grid2)
    assert ins_grid == RectGrid(vec1, scalar, vec3, vec2)

    ins_grid = grid.insert(2, grid2)
    assert ins_grid == RectGrid(vec1, vec2, scalar, vec3)

    ins_grid = grid.insert(-1, grid2)
    assert ins_grid == RectGrid(vec1, scalar, vec3, vec2)

    with pytest.raises(IndexError):
        grid.insert(3, grid2)

    with pytest.raises(IndexError):
        grid.insert(-4, grid2)
Example #2
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)