Exemplo n.º 1
0
def test_regular_is_subgrid():
    minpt = (0.75, 0, -5)
    maxpt = (1.25, 0, 1)
    shape = (2, 1, 5)

    # Optimized cases
    grid = RegularGrid(minpt, maxpt, shape)
    assert grid.is_subgrid(grid)

    smaller_shape = (1, 1, 5)
    not_sup_grid = RegularGrid(minpt, maxpt, smaller_shape)
    assert not grid.is_subgrid(not_sup_grid)

    larger_minpt = (0.85, 0, -4)
    not_sup_grid = RegularGrid(larger_minpt, maxpt, shape)
    assert not grid.is_subgrid(not_sup_grid)

    smaller_maxpt = (1.15, 0, 0)
    not_sup_grid = RegularGrid(minpt, smaller_maxpt, shape)
    assert not grid.is_subgrid(not_sup_grid)

    # Real checks
    minpt_sup1 = (-0.25, -2, -5)
    maxpt_sup1 = (1.25, 2, 1)
    shape_sup1 = (4, 3, 9)
    sup_grid = RegularGrid(minpt_sup1, maxpt_sup1, shape_sup1)
    assert grid.is_subgrid(sup_grid)
    assert not sup_grid.is_subgrid(grid)

    minpt_sup2 = (0.5, 0, -5)
    maxpt_sup2 = (1.5, 0, 1)
    shape_sup2 = (5, 1, 9)
    sup_grid = RegularGrid(minpt_sup2, maxpt_sup2, shape_sup2)
    assert grid.is_subgrid(sup_grid)
    assert not sup_grid.is_subgrid(grid)

    shape_not_sup1 = (4, 3, 10)
    not_sup_grid = RegularGrid(minpt_sup1, maxpt_sup1, shape_not_sup1)
    assert not grid.is_subgrid(not_sup_grid)
    assert not not_sup_grid.is_subgrid(grid)

    minpt_not_sup1 = (-0.25, -2.5, -5)
    not_sup_grid = RegularGrid(minpt_not_sup1, maxpt_sup1, shape_sup1)
    assert not grid.is_subgrid(not_sup_grid)
    assert not not_sup_grid.is_subgrid(grid)

    maxpt_not_sup1 = (1.35, 2.0001, 1)
    not_sup_grid = RegularGrid(minpt_sup1, maxpt_not_sup1, shape_sup1)
    assert not grid.is_subgrid(not_sup_grid)
    assert not not_sup_grid.is_subgrid(grid)

    # Should also work for TensorGrid's
    vec1_sup = (0.75, 1, 1.25, 7)
    vec2_sup = (0,)
    vec3_sup = (-5, -3.5, -3, -2, -0.5, 0, 1, 9.5)

    tensor_sup_grid = TensorGrid(vec1_sup, vec2_sup, vec3_sup)
    assert grid.is_subgrid(tensor_sup_grid)

    vec1_not_sup = (1, 1.25, 7)
    vec2_not_sup = (0,)
    vec3_not_sup = (-4, -2, 1)

    tensor_not_sup_grid = TensorGrid(vec1_not_sup, vec2_not_sup,
                                     vec3_not_sup)
    assert not grid.is_subgrid(tensor_not_sup_grid)

    # Fuzzy check
    shape_sup = (4, 3, 9)

    minpt_fuzzy_sup1 = (-0.24, -2, -5.01)
    minpt_fuzzy_sup2 = (-0.24, -2, -5)
    maxpt_fuzzy_sup1 = (1.24, 2, 1)
    maxpt_fuzzy_sup2 = (1.25, 2, 1.01)

    fuzzy_sup_grid = RegularGrid(minpt_fuzzy_sup1, maxpt_fuzzy_sup1,
                                 shape_sup)
    assert grid.is_subgrid(fuzzy_sup_grid, tol=0.015)
    assert not grid.is_subgrid(fuzzy_sup_grid, tol=0.005)

    fuzzy_sup_grid = RegularGrid(minpt_fuzzy_sup2, maxpt_fuzzy_sup2,
                                 shape_sup)
    assert grid.is_subgrid(fuzzy_sup_grid, tol=0.015)
    assert not grid.is_subgrid(fuzzy_sup_grid, tol=0.005)
Exemplo n.º 2
0
def test_regular_grid_init():
    minpt = (0.75, 0, -5)
    maxpt = (1.25, 0, 1)
    shape = (2, 1, 3)

    # Check correct initialization of coord_vectors
    grid = RegularGrid(minpt, maxpt, shape)
    vec1 = (0.75, 1.25)
    vec2 = (0,)
    vec3 = (-5, -2, 1)
    assert all_equal(grid.coord_vectors, (vec1, vec2, vec3))

    # Check different error scenarios
    nonpos_shape1 = (2, 0, 3)
    nonpos_shape2 = (-2, 1, 3)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt, nonpos_shape1)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt, nonpos_shape2)

    minpt_with_nan = (0.75, 0, np.nan)
    minpt_with_inf = (0.75, 0, np.inf)
    maxpt_with_nan = (1.25, np.nan, 1)
    maxpt_with_inf = (1.25, np.inf, 1)
    shape_with_nan = (2, np.nan, 3)
    shape_with_inf = (2, np.inf, 3)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt_with_nan, maxpt, shape)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt_with_inf, maxpt, shape)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt_with_nan, shape)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt_with_inf, shape)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt, shape_with_nan)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt, shape_with_inf)

    maxpt_smaller_minpt1 = (0.7, 0, 1)
    maxpt_smaller_minpt2 = (1.25, -1, 1)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt_smaller_minpt1, shape)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt_smaller_minpt2, shape)

    too_short_minpt = (0.75, 0)
    too_long_minpt = (0.75, 0, -5, 2)
    too_short_maxpt = (0, 1)
    too_long_maxpt = (1.25, 0, 1, 25)
    too_short_shape = (2, 3)
    too_long_shape = (2, 1, 4, 3)

    with pytest.raises(ValueError):
        grid = RegularGrid(too_short_minpt, maxpt, shape)

    with pytest.raises(ValueError):
        grid = RegularGrid(too_long_minpt, maxpt, shape)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, too_short_maxpt, shape)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, too_long_maxpt, shape)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt, too_short_shape)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt, too_long_shape)

    maxpt_eq_minpt_at_shape_larger_than_1 = (0.75, 0, 1)

    with pytest.raises(ValueError):
        grid = RegularGrid(minpt, maxpt_eq_minpt_at_shape_larger_than_1,
                           shape)

    # Overrides for exact min and max
    exact_min = np.array((0, 1, 0), dtype=float)
    exact_max = np.array((1, 1, 3), dtype=float)
    shape = np.array((3, 1, 7), dtype=int)
    shift = (exact_max - exact_min) / (2 * shape)

    minpt = exact_min + shift
    maxpt = exact_max - shift

    grid = RegularGrid(minpt, maxpt, shape, as_midp=True,
                       _exact_min=exact_min, _exact_max=exact_max)
    assert all_equal(grid.min(), exact_min)
    assert all_equal(grid.max(), exact_max)