Ejemplo n.º 1
0
def test_contains():
    vec1 = np.arange(2, 6)
    vec2 = np.arange(-4, 5, 2)

    grid = TensorGrid(vec1, vec2)

    point_list = []
    for x in vec1:
        for y in vec2:
            point_list.append((x, y))

    assert all(p in grid for p in point_list)

    assert not (0, 0) in grid
    assert (0, 0) not in grid
    assert (2, 0, 0) not in grid

    # Fuzzy check
    assert grid.approx_contains((2.1, -2.1), tol=0.15)
    assert not grid.approx_contains((2.2, -2.1), tol=0.15)

    # 1d points
    grid = TensorGrid(vec1)
    assert 3 in grid
    assert 7 not in grid
Ejemplo n.º 2
0
def test_equals():
    vec1 = np.arange(2, 6)
    vec2 = np.arange(-4, 5, 2)

    grid1 = TensorGrid(vec1)
    grid2 = TensorGrid(vec1, vec2)
    grid2_again = TensorGrid(vec1, vec2)
    grid2_rev = TensorGrid(vec2, vec1)

    assert grid1 == grid1
    assert not grid1 != grid1
    assert grid2 == grid2
    assert not grid2 != grid2
    assert grid2 == grid2_again
    assert not grid1 == grid2
    assert not grid2 == grid2_rev
    assert not grid2 == (vec1, vec2)

    # Fuzzy check
    grid1 = TensorGrid(vec1, vec2)
    grid2 = TensorGrid(vec1 + (0.1, 0.05, 0, -0.1),
                       vec2 + (0.1, 0.05, 0, -0.1, -0.1))
    assert grid1.approx_equals(grid2, tol=0.15)
    assert grid2.approx_equals(grid1, tol=0.15)

    grid2 = TensorGrid(vec1 + (0.11, 0.05, 0, -0.1),
                       vec2 + (0.1, 0.05, 0, -0.1, -0.1))
    assert not grid1.approx_equals(grid2, tol=0.1)
    grid2 = TensorGrid(vec1 + (0.1, 0.05, 0, -0.1),
                       vec2 + (0.1, 0.05, 0, -0.11, -0.1))
    assert not grid1.approx_equals(grid2, tol=0.1)
Ejemplo n.º 3
0
def test_element():
    vec1 = np.arange(2, 6)
    vec2 = np.arange(-4, 5, 2)

    grid = TensorGrid(vec1, vec2)
    some_pt = grid.element()
    assert some_pt in grid
Ejemplo n.º 4
0
Archivo: grid_test.py Proyecto: wjp/odl
def test_meshgrid(as_midp):
    vec1 = (0, 1)
    vec2 = (-1, 0, 1)
    vec3 = (2, 3, 4, 5)

    # Sparse meshgrid
    mgx = np.array(vec1)[:, np.newaxis, np.newaxis]
    mgy = np.array(vec2)[np.newaxis, :, np.newaxis]
    mgz = np.array(vec3)[np.newaxis, np.newaxis, :]

    grid = TensorGrid(vec1, vec2, vec3, as_midp=as_midp)
    xx, yy, zz = grid.meshgrid()
    assert all_equal(mgx, xx)
    assert all_equal(mgy, yy)
    assert all_equal(mgz, zz)

    xx, yy, zz = grid.meshgrid()
    assert all_equal(mgx, xx)
    assert all_equal(mgy, yy)
    assert all_equal(mgz, zz)

    # Fortran ordering
    grid = TensorGrid(vec1, vec2, vec3, order="F", as_midp=as_midp)
    xx, yy, zz = grid.meshgrid()
    assert all(arr.flags.f_contiguous for arr in (xx, yy, zz))
Ejemplo n.º 5
0
def test_cell_sizes():
    vec1 = np.array([1, 3])
    vec2 = np.array([-1, 0, 1])
    vec3 = np.array([2, 4, 5, 10])
    scalar = 0.5

    cs1, cs2, cs3 = [np.diff(vec) for vec in (vec1, vec2, vec3)]
    csscal = 0

    # Grid as set
    grid = TensorGrid(vec1, vec2, vec3, as_midp=False)
    assert all_equal(grid.cell_sizes(), (cs1, cs2, cs3))

    grid = TensorGrid(vec1, scalar, vec3, as_midp=False)
    assert all_equal(grid.cell_sizes(), (cs1, csscal, cs3))

    # Grid as tesselation
    cs1 = (2, 2)
    cs2 = (1, 1, 1)
    cs3 = (2, 1.5, 3, 5)

    grid = TensorGrid(vec1, vec2, vec3, as_midp=True)
    assert all_equal(grid.cell_sizes(), (cs1, cs2, cs3))

    grid = TensorGrid(vec1, scalar, vec3, as_midp=True)
    assert all_equal(grid.cell_sizes(), (cs1, csscal, cs3))
Ejemplo n.º 6
0
def test_meshgrid():
    vec1 = (0, 1)
    vec2 = (-1, 0, 1)
    vec3 = (2, 3, 4, 5)

    # Sparse meshgrid
    mgx = np.array(vec1)[:, np.newaxis, np.newaxis]
    mgy = np.array(vec2)[np.newaxis, :, np.newaxis]
    mgz = np.array(vec3)[np.newaxis, np.newaxis, :]

    grid = TensorGrid(vec1, vec2, vec3)
    xx, yy, zz = grid.meshgrid()
    assert all_equal(mgx, xx)
    assert all_equal(mgy, yy)
    assert all_equal(mgz, zz)

    xx, yy, zz = grid.meshgrid(sparse=True)
    assert all_equal(mgx, xx)
    assert all_equal(mgy, yy)
    assert all_equal(mgz, zz)

    # Dense meshgrid
    mgx = np.empty((2, 3, 4))
    for i in range(2):
        mgx[i, :, :] = vec1[i]

    mgy = np.empty((2, 3, 4))
    for i in range(3):
        mgy[:, i, :] = vec2[i]

    mgz = np.empty((2, 3, 4))
    for i in range(4):
        mgz[:, :, i] = vec3[i]

    xx, yy, zz = grid.meshgrid(sparse=False)
    assert all_equal(mgx, xx)
    assert all_equal(mgy, yy)
    assert all_equal(mgz, zz)

    assert all_equal(xx.shape, (2, 3, 4))
Ejemplo n.º 7
0
def test_convex_hull():
    vec1 = (1, 3)
    vec2 = (-1, 0, 1)
    vec3 = (2, 4, 5, 10)
    scalar = 0.5

    # Grid as set
    grid = TensorGrid(vec1, vec2, vec3, as_midp=False)
    begin = (vec1[0], vec2[0], vec3[0])
    end = (vec1[-1], vec2[-1], vec3[-1])
    chull = odl.IntervalProd(begin, end)
    assert grid.convex_hull() == chull

    # With degenerate axis
    grid = TensorGrid(vec1, vec2, scalar, as_midp=False)
    begin = (vec1[0], vec2[0], scalar)
    end = (vec1[-1], vec2[-1], scalar)
    chull = odl.IntervalProd(begin, end)
    assert grid.convex_hull() == chull

    # Grid as tesselation
    grid = TensorGrid(vec1, vec2, vec3, as_midp=True)
    cs1 = (2, 2)
    cs2 = (1, 1, 1)
    cs3 = (2, 1.5, 3, 5)
    begin = (vec1[0] - cs1[0] / 2.,
             vec2[0] - cs2[0] / 2.,
             vec3[0] - cs3[0] / 2.)
    end = (vec1[-1] + cs1[-1] / 2.,
           vec2[-1] + cs2[-1] / 2.,
           vec3[-1] + cs3[-1] / 2.)
    chull = odl.IntervalProd(begin, end)
    assert grid.convex_hull() == chull

    # With degenerate axis
    grid = TensorGrid(vec1, vec2, scalar, as_midp=True)
    begin = (vec1[0] - cs1[0] / 2., vec2[0] - cs2[0] / 2., scalar)
    end = (vec1[-1] + cs1[-1] / 2., vec2[-1] + cs2[-1] / 2., scalar)
    chull = odl.IntervalProd(begin, end)
    assert grid.convex_hull() == chull
Ejemplo n.º 8
0
def test_corners():
    vec1 = np.arange(2, 6)
    vec2 = np.arange(-4, 5, 2)
    vec3 = np.arange(-1, 1)
    scalar = 0.5

    minmax1 = (vec1[0], vec1[-1])
    minmax2 = (vec2[0], vec2[-1])
    minmax3 = (vec3[0], vec3[-1])

    # C ordering
    corners = []
    for x1 in minmax1:
        for x2 in minmax2:
            for x3 in minmax3:
                corners.append(np.array((x1, x2, x3), dtype=float))

    grid = TensorGrid(vec1, vec2, vec3)
    assert all_equal(corners, grid.corners())
    assert all_equal(corners, grid.corners(order='C'))

    # minpt and maxpt should appear at the beginning and the end, resp.
    assert all_equal(grid.min_pt, grid.corners()[0])
    assert all_equal(grid.max_pt, grid.corners()[-1])

    # F ordering
    corners = []
    for x3 in minmax3:
        for x2 in minmax2:
            for x1 in minmax1:
                corners.append(np.array((x1, x2, x3), dtype=float))

    assert all_equal(corners, grid.corners(order='F'))

    # Degenerate axis 1
    corners = []
    for x2 in minmax2:
        for x3 in minmax3:
            corners.append(np.array((scalar, x2, x3), dtype=float))

    grid = TensorGrid(scalar, vec2, vec3)
    assert all_equal(corners, grid.corners())

    # Degenerate axis 2
    corners = []
    for x1 in minmax1:
        for x3 in minmax3:
            corners.append(np.array((x1, scalar, x3), dtype=float))

    grid = TensorGrid(vec1, scalar, vec3)
    assert all_equal(corners, grid.corners())

    # Degenerate axis 3
    corners = []
    for x1 in minmax1:
        for x2 in minmax2:
            corners.append(np.array((x1, x2, scalar), dtype=float))

    grid = TensorGrid(vec1, vec2, scalar)
    assert all_equal(corners, grid.corners())

    # All degenerate
    corners = [(scalar, scalar)]
    grid = TensorGrid(scalar, scalar)
    assert all_equal(corners, grid.corners())
Ejemplo n.º 9
0
def test_points():
    vec1 = np.arange(2, 6)
    vec2 = np.arange(-4, 5, 2)
    scalar = 0.5

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

    grid = TensorGrid(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 = TensorGrid(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 = TensorGrid(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 = TensorGrid(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 = TensorGrid(vec1, vec2, scalar)
    assert all_equal(points, grid.points())
Ejemplo n.º 10
0
def test_tensor_is_subgrid():
    vec1 = np.arange(2, 6)
    vec1_sup = np.arange(2, 8)
    vec2 = np.arange(-4, 5, 2)
    vec2_sup = np.arange(-6, 7, 2)
    vec2_sub = np.arange(-4, 3, 2)
    scalar = 0.5

    grid = TensorGrid(vec1, vec2)
    assert grid.is_subgrid(grid)

    sup_grid = TensorGrid(vec1_sup, vec2_sup)
    assert grid.is_subgrid(sup_grid)
    assert not sup_grid.is_subgrid(grid)

    not_sup_grid = TensorGrid(vec1_sup, vec2_sub)
    assert not grid.is_subgrid(not_sup_grid)
    assert not not_sup_grid.is_subgrid(grid)

    # Fuzzy check
    fuzzy_vec1_sup = vec1_sup + (0.1, 0.05, 0, -0.1, 0, 0.1)
    fuzzy_vec2_sup = vec2_sup + (0.1, 0.05, 0, -0.1, 0, 0.1, 0.05)
    fuzzy_sup_grid = TensorGrid(fuzzy_vec1_sup, fuzzy_vec2_sup)
    assert grid.is_subgrid(fuzzy_sup_grid, tol=0.15)

    fuzzy_vec2_sup = vec2_sup + (0.1, 0.05, 0, -0.1, 0, 0.11, 0.05)
    fuzzy_sup_grid = TensorGrid(fuzzy_vec1_sup, fuzzy_vec2_sup)
    assert not grid.is_subgrid(fuzzy_sup_grid, tol=0.1)

    # Changes in the non-overlapping part don't matter
    fuzzy_vec2_sup = vec2_sup + (0.1, 0.05, 0, -0.1, 0, 0.05, 0.11)
    fuzzy_sup_grid = TensorGrid(fuzzy_vec1_sup, fuzzy_vec2_sup)
    assert grid.is_subgrid(fuzzy_sup_grid, tol=0.15)

    # With degenerate axis
    grid = TensorGrid(vec1, scalar, vec2)
    sup_grid = TensorGrid(vec1_sup, scalar, vec2_sup)
    assert grid.is_subgrid(sup_grid)

    fuzzy_sup_grid = TensorGrid(vec1, scalar + 0.1, vec2)
    assert grid.is_subgrid(fuzzy_sup_grid, tol=0.15)
Ejemplo n.º 11
0
def test_min_max():
    vec1 = np.arange(2, 6)
    vec2 = np.arange(-4, 5, 2)
    vec3 = np.arange(-1, 1)
    scalar = 0.5

    grid = TensorGrid(vec1, vec2, vec3, as_midp=False)
    assert all_equal(grid.min(), (2, -4, -1))
    assert all_equal(grid.max(), (5, 4, 0))

    grid = TensorGrid(vec1, scalar, vec2, scalar, as_midp=False)
    assert all_equal(grid.min(), (2, 0.5, -4, 0.5))
    assert all_equal(grid.max(), (5, 0.5, 4, 0.5))

    grid = TensorGrid(vec1, vec2, vec3, as_midp=True)
    assert all_equal(grid.min(), (1.5, -5, -1.5))
    assert all_equal(grid.max(), (5.5, 5, 0.5))

    grid = TensorGrid(vec1, scalar, vec2, scalar, as_midp=True)
    assert all_equal(grid.min(), (1.5, 0.5, -5, 0.5))
    assert all_equal(grid.max(), (5.5, 0.5, 5, 0.5))