예제 #1
0
def test_grid():
    input = CoordinateSystem('ij', 'input')
    output = CoordinateSystem('xy', 'output')
    def f(ij):
        i = ij[:,0]
        j = ij[:,1]
        return np.array([i**2+j,j**3+i]).T
    cmap = CoordinateMap(input, output, f)
    grid = Grid(cmap)
    eval = ArrayCoordMap.from_shape(cmap, (50,40))
    assert_true(np.allclose(grid[0:50,0:40].values, eval.values))
예제 #2
0
def test_grid32():
    # Check that we can use a float32 input and output
    uv32 = CoordinateSystem('uv', 'input', np.float32)
    xyz32 = CoordinateSystem('xyz', 'output', np.float32)
    surface32 = CoordinateMap(uv32, xyz32, parametric_mapping)
    g = Grid(surface32)
    xyz_grid = g[-1:1:201j, -1:1:101j]
    x, y, z = xyz_grid.transposed_values
    yield assert_equal, x.shape, (201, 101)
    yield assert_equal, y.shape, (201, 101)
    yield assert_equal, z.shape, (201, 101)
    yield assert_equal, x.dtype, np.dtype(np.float32)
예제 #3
0
def test_eval_slice():
    input = CoordinateSystem('ij', 'input')
    output = CoordinateSystem('xy', 'output')
    def f(ij):
        i = ij[:,0]
        j = ij[:,1]
        return np.array([i**2+j,j**3+i]).T

    cmap = CoordinateMap(input, output, f)

    cmap = CoordinateMap(input, output, f)
    grid = Grid(cmap)
    e = grid[0:50,0:40]
    ee = e[0:20:3]

    yield assert_equal, ee.shape, (7,40)
    yield assert_equal, ee.values.shape, (280,2)
    yield assert_equal, ee.transposed_values.shape, (2,7,40)

    ee = e[0:20:2,3]
    yield assert_equal, ee.values.shape, (10,2)
    yield assert_equal, ee.transposed_values.shape, (2,10)
    yield assert_equal, ee.shape, (10,)
예제 #4
0
def test_grid32_c128():
    # Check that we can use a float32 input and complex128 output
    uv32 = CoordinateSystem('uv', 'input', np.float32)
    xyz128 = CoordinateSystem('xyz', 'output', np.complex128)

    def par_c128(x):
        return parametric_mapping(x).astype(np.complex128)

    surface = CoordinateMap(uv32, xyz128, par_c128)
    g = Grid(surface)
    xyz_grid = g[-1:1:201j, -1:1:101j]
    x, y, z = xyz_grid.transposed_values
    yield assert_equal, x.shape, (201, 101)
    yield assert_equal, y.shape, (201, 101)
    yield assert_equal, z.shape, (201, 101)
    yield assert_equal, x.dtype, np.dtype(np.complex128)
예제 #5
0
def load_npz(filename):
    """
    Load an .npz Image, this .npz file must have at least two arrays

    * data: the data array
    * dimnames: the dimension names of the corresponding grid
    * affine: the affine transformation of grid

    The remaining arrays of .npz file are stored as the 'extra' attribute of the Image.
    """
    npzobj = np.load(filename)
    
    im = Image(npzobj['data'], CoordinateMap.from_affine(Affine(npzobj['affine']),
                                                        list(npzobj['dimnames']),
                                                        npzobj['data'].shape))
    im.extra = {}
    for f in npzobj.files:
        if f not in ['affine', 'dimnames', 'data']:
            im.extra[f] = npzobj[f]

    return im
예제 #6
0
    return o


"""
Let's check that indeed this is a parametrization of that surface
"""


def implicit(vals):
    x = vals[:, 0]
    y = vals[:, 1]
    z = vals[:, 2]
    return x**2 - y**2 * z**2 + z**3


surface_param = CoordinateMap(uv, xyz, parametric_mapping)


def test_surface():
    assert np.allclose(
        implicit(parametric_mapping(np.random.standard_normal((40, 2)))), 0)


def test_grid():
    g = Grid(surface_param)
    xyz_grid = g[-1:1:201j, -1:1:101j]
    x, y, z = xyz_grid.transposed_values
    yield assert_equal, x.shape, (201, 101)
    yield assert_equal, y.shape, (201, 101)
    yield assert_equal, z.shape, (201, 101)