Exemplo n.º 1
0
def test_field_description():

    a = Struct(field_shape=(20, 20),
               field_sampling=(2, 1),
               field_origin=(9, 9))
    fd = FD(a)
    assert fd.shape == (20, 20)
    assert fd.sampling == (2, 1)
    assert fd.origin == (9, 9)
    assert fd.ndim == 2
    assert fd.defined_sampling
    assert fd.defined_origin

    a = Struct(field_shape=(20, 20, 20))
    fd = FD(a)
    assert fd.shape == (20, 20, 20)
    assert fd.sampling == (1, 1, 1)
    assert fd.origin == (0, 0, 0)
    assert fd.ndim == 3
    assert not fd.defined_sampling
    assert not fd.defined_origin

    with raises(TypeError):
        FD('meh')
    with raises(TypeError):
        FD((20, 20), 'meh')
    with raises(TypeError):
        FD((20, 20), (2, 1), 'meh')
def test_warp_fails_and_conversions():
    
    # Prepare data
    data = np.array([[10, 21, 31], [40, 50, 60], [70, 80, 90]]).astype('float32')
    samples = np.array([1, 2, 1]), np.array([0, 1, 0])
    order = 1
    
    # Default
    result = pirt.warp(data, samples, order)
    assert result.tolist() == [21, 60, 21]
    
    # data argument
    
    # Wrong type
    with raises(ValueError):
        pirt.warp('not_array', samples, order)
    
    # Wrong shape
    with raises(ValueError):
        pirt.warp(data.reshape(-1, 1, 1, 1), samples, order)
    
    # samples argument
    
    # Samples as list -> tuple
    result = pirt.warp(data, list(samples), order)
    assert result.tolist() == [21, 60, 21]
    
    # Samples as one nd-array (skimage api)
    result = pirt.warp(data, np.stack(reversed(samples)), order)
    assert result.tolist() == [21, 60, 21]
    
    # Wrong type
    with raises(ValueError):
        pirt.warp(data, 'wrong', order)
    
    # Inside samples
    samples2 = (np.array([1, 2, 1]), )
    with raises(ValueError):
        pirt.warp(data, samples2, order)
    samples2 = np.array([1, 2, 1]), 'meh'
    with raises(ValueError):
        pirt.warp(data, samples2, order)
    samples2 = np.array([1, 2, 1]), np.array([0, 1, 0, 2])
    with raises(ValueError):
        pirt.warp(data, samples2, order)
    
    # order argument
    
    # Wrong type
    with raises(ValueError):
        pirt.warp(data, samples, [0])
    
    # Wrong text
    with raises(ValueError):
        pirt.warp(data, samples, 'unknown order')
    
    # Wrong int
    with raises(ValueError):
        pirt.warp(data, samples, 4)
Exemplo n.º 3
0
def test_zoom():

    im = np.zeros((64, 64), np.float32)
    im = pirt.Aarray(im, (1, 2))

    im3 = pirt.imzoom(im, 0.5)
    assert im3.shape == (32, 32)

    im3 = pirt.imzoom(im, np.array(0.25))
    assert im3.shape == (16, 16)

    # Raises
    with raises(ValueError):
        pirt.zoom(im, 'meh')
    with raises(ValueError):
        pirt.zoom(im, (3, 3, 3))
Exemplo n.º 4
0
def test_resize():

    im = np.zeros((64, 64), np.float32)
    im = pirt.Aarray(im, (1, 2))

    im2 = pirt.imresize(im, (50, 50))
    assert im2.shape == (50, 50)

    im2 = pirt.resize(im, (50, 50))  # extra=False
    assert im2.shape == (50, 50)

    # Raises
    with raises(ValueError):
        pirt.resize(im, 'meh')
    with raises(ValueError):
        pirt.resize(im, (3, 3, 3))
Exemplo n.º 5
0
def test_grid_container():
    class ColorGridContainer(GridContainer):
        def __init__(self, shape, grid_sampling):
            super().__init__(shape, grid_sampling)
            for i in range(3):
                self._grids.append(SplineGrid(shape, grid_sampling))

    gc = ColorGridContainer((100, 100), 3)
    assert gc.field_shape == (100, 100)

    assert len(gc) == 3
    assert len(gc.grids) == 3
    for grid in gc:
        assert isinstance(grid, SplineGrid)
    assert list(gc) == [gc[0], gc[1], gc[2]]
    with raises(IndexError):
        gc[3]
    with raises(IndexError):
        gc[-1]
    with raises(IndexError):
        gc[:]

    # resize
    gc2 = gc.resize_field((60, 60))
    assert gc2.field_shape == (60, 60)

    # add
    gc3 = gc.add(gc)
    assert gc3.field_shape == (100, 100)
    assert gc3 is not gc  # not in-place
    for i in range(3):
        gc3[i] is not gc[i]

    # refine
    gc4 = gc.refine()
    assert gc4.field_shape == (100, 100)
    assert gc4.grid_sampling == gc.grid_sampling / 2
    for i in range(3):
        gc4[i].grid_sampling == gc4.grid_sampling

    # copy
    gc5 = gc.add(gc)
    assert gc5.field_shape == (100, 100)
    assert gc5 is not gc  # not in-place
    for i in range(3):
        gc5[i] is not gc[i]
Exemplo n.º 6
0
def test_deform():

    im = np.zeros((64, 64), np.float32)
    im = pirt.Aarray(im, (1, 2))

    deltas = np.zeros((64, 64), np.float32), np.zeros((64, 64), np.float32)

    result = pirt.deform_backward(im, deltas)
    assert result.shape == im.shape

    result = pirt.deform_forward(im, deltas)
    assert result.shape == im.shape

    with raises(ValueError):
        pirt.deform_backward(im, deltas[1:])
    with raises(ValueError):
        pirt.deform_forward(im, deltas[1:])
Exemplo n.º 7
0
def test_meshgrid():
    
    res = pirt.meshgrid(2, 3)
    assert len(res) == 2
    assert res[0].dtype == 'float32'
    assert res[0].shape == (3, 2)
    assert res[1].shape == (3, 2)
    
    assert res[0].ravel().tolist() == [0, 1] * 3
    assert res[1].ravel().tolist() == [0, 0, 1, 1, 2, 2]
    
    res = pirt.meshgrid(4, 5)
    assert len(res) == 2
    assert res[0].dtype == 'float32'
    assert res[0].shape == (5, 4)
    assert res[1].shape == (5, 4)
    
    res = pirt.meshgrid(4, 5, 6)
    assert len(res) == 3
    assert res[0].dtype == 'float32'
    assert res[0].shape == (6, 5, 4)
    assert res[1].shape == (6, 5, 4)
    assert res[2].shape == (6, 5, 4)
    
    assert res[0].ravel().tolist() == [0, 1, 2, 3] * 5 * 6
    assert res[1].ravel().tolist() == ([0] * 4 + [1] * 4 + [2] * 4 + [3] * 4 + [4] * 4) * 6
    assert res[2].ravel().tolist() == [0] * 20 + [1] * 20 + [2] * 20 + [3] * 20 + [4] * 20 + [5] * 20
    
    # Auto-conversions
    
    res2 = pirt.meshgrid([4, 5, 6])
    assert np.all(res[0] == res2[0])
    
    with raises(ValueError):
        pirt.meshgrid('meh')
    with raises(ValueError):
        pirt.meshgrid(3, 'meh')
    with raises(ValueError):
        pirt.meshgrid([3, 'meh'])
    
    print('meshgrid ok')
Exemplo n.º 8
0
def test_cubic():
    
    assert pirt.get_cubic_spline_coefs(0.46, 'nearest') == (0, 1, 0, 0)
    assert pirt.get_cubic_spline_coefs(0.54, 'nearest') == (0, 0, 1, 0)
    assert pirt.get_cubic_spline_coefs(0.44, 'linear') == (0, 1 - 0.44, 0.44, 0)
    
    cc1 = pirt.get_cubic_spline_coefs(0.44, 'catmull–rom')
    cc2 = pirt.get_cubic_spline_coefs(0.44, 'cardinal')
    cc3 = pirt.get_cubic_spline_coefs(0.44, 0.0)
    assert cc1 == cc2 == cc3
    
    # Wrong spline type
    with raises(ValueError):
        pirt.get_cubic_spline_coefs(0.44, 'unknown_spline_type')
        
    # Wrong cardinal spline tension
    with raises(ValueError):
        pirt.get_cubic_spline_coefs(0.44, -1.01)
    with raises(ValueError):
        pirt.get_cubic_spline_coefs(0.44, +1.01)
    
    # Our of range t is ok
    pirt.get_cubic_spline_coefs(-0.2, 0.0)
    pirt.get_cubic_spline_coefs(1.2, 0.0)
    
    # Iterate all existing splines
    for spline_type in ('nearest', 'linear', 'quadratic', 'lanczos',
                        'cardinal', 'basic', 'hermite', 'lagrange'):
        cc = pirt.get_cubic_spline_coefs(0.44, spline_type)
        assert len(cc) == 4
        assert 0.97 < sum(cc) < 1.03
    
    # We also do look up tables, for histroric reasons
    n = 1000
    lut = pirt.interp._cubic.get_lut(0.0, n)
    assert lut.shape == (4 * (n + 2), )
def test_aproject():
    
    data = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]).astype('float32')
    samples = (np.array([[0, 1, 2], [0, 1, 2], [0, 1, 2]]) * 2,
               np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2]]) / 2)
    
    # Cannot use data like this
    with raises(ValueError):
        pirt.aproject(data, samples)
    
    # Data must have sampling and origin
    data = pirt.Aarray(data, (0.5, 2.0))
    
    # Check that using normal project fails
    result = pirt.project(data, samples)
    assert result.tolist() != data.tolist()

    result = pirt.aproject(data, samples)
    assert result.tolist() == data.tolist()
Exemplo n.º 10
0
def test_awarp():
    
    data = np.array([[10, 21, 31], [40, 50, 60], [70, 80, 90]]).astype('float32')
    samples = np.array([1, 2, 1]) * 2, np.array([0, 1, 0]) / 2
    order = 1
    
    # Cannot use data like this
    with raises(ValueError):
        pirt.awarp(data, samples, order)
    
    # Data must have sampling and origin
    data = pirt.Aarray(data, (0.5, 2.0))
    
    # Check that using normal warp fails
    result = pirt.warp(data, samples, order)
    assert result.tolist() != [21, 60, 21]
    
    result = pirt.awarp(data, samples, order)
    assert result.tolist() == [21, 60, 21]
Exemplo n.º 11
0
def test_make_samples_absolute():
    
    # 1D
    samples1 = np.array([0, 1, 0]), 
    samples2 = pirt.interp.make_samples_absolute(samples1)
    assert len(samples2) == 1
    assert list(samples2[0]) == [0, 2, 2]
    
    # 1D anisotropic
    samples1 = pirt.Aarray(np.array([0, 1, 0], np.float32), (2, )), 
    samples2 = pirt.interp.make_samples_absolute(samples1)
    assert len(samples2) == 1
    assert list(samples2[0]) == [0, 1.5, 2]
    
    # 1D anisotropic - note that origin is ignored
    samples1 = pirt.Aarray(np.array([0, 1, 0], np.float32), (0.5, ), (7, )), 
    samples2 = pirt.interp.make_samples_absolute(samples1)
    assert len(samples2) == 1
    assert list(samples2[0]) == [0, 3, 2]
    
    # 2D - wrong
    samples1 = np.array([0, 1, 0]), np.array([0, 0, 1])
    with raises(ValueError):
        pirt.interp.make_samples_absolute(samples1)
    
    # 2D
    samples1 = np.array([[0, 1, 0], [0, 1, 0]]), np.array([[0, 0, 0], [1, 1, 1]])
    samples2 = pirt.interp.make_samples_absolute(samples1)
    assert len(samples2) == 2
    assert list(samples2[0].flat) == [0, 2, 2,  0, 2, 2]
    assert list(samples2[1].flat) == [0, 0, 0,  2, 2, 2]
    
    # 3D
    samples1 = (np.array([[[0, 1, 0], [0, 1, 0]], [[0, 1, 0], [0, 1, 0]]]),
                np.array([[[0, 0, 0], [1, 1, 1]], [[0, 0, 0], [1, 1, 1]]]),
                np.array([[[1, 1, 1], [1, 1, 1]], [[0, 0, 0], [0, 0, 0]]]))
    samples2 = pirt.interp.make_samples_absolute(samples1)
    assert len(samples2) == 3
    assert list(samples2[0].flat) == [0, 2, 2,  0, 2, 2,  0, 2, 2,  0, 2, 2]
    assert list(samples2[1].flat) == [0, 0, 0,  2, 2, 2,  0, 0, 0,  2, 2, 2]
    assert list(samples2[2].flat) == [1, 1, 1,  1, 1, 1,  1, 1, 1,  1, 1, 1]
Exemplo n.º 12
0
def test_project_fails_and_conversions():
    
    data = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]).astype('float32')
    samples = np.array([[0, 1, 2], [0, 1, 2], [0, 1, 2]]), np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2]])
    
    # default
    result = pirt.project(data, samples)
    assert result.tolist() == data.tolist()
    
    # data argument
    
    # Wrong type
    with raises(ValueError):
        pirt.project('not_array', samples)
    
    # Wrong shape
    with raises(ValueError):
        pirt.project(data.reshape(-1, 1, 1, 1), samples)
    
    # samples argument
    
    # Samples as list -> tuple
    result = pirt.project(data, list(samples))
    assert result.tolist() == data.tolist()
    
    # Samples as one nd-array (skimage api)
    result = pirt.project(data, np.stack(reversed(samples)))
    assert result.tolist() == data.tolist()
    
    # Wrong type
    with raises(ValueError):
        pirt.project(data, 'wrong')
    
    # inside samples - project is more restrictive than warp
    samples = (np.array([[0, 1, 2], [0, 1, 2], [0, 1, 2]]), )
    with raises(ValueError):
        pirt.project(data, samples)
    samples = np.array([[0, 1, 2], [0, 1, 2], [0, 1, 2]]), 'meh'
    with raises(ValueError):
        pirt.project(data, samples)
    samples = np.array([[0, 1, 2], [0, 1, 2]]), np.array([[0, 0, 0], [1, 1, 1]])
    with raises(ValueError):
        pirt.project(data, samples)