Beispiel #1
0
def get_two_images():

    # Create test image
    im0 = np.zeros((100, 100), np.float32)
    im0 = pirt.Aarray(im0, (0.6, 2.0))
    im0[30:40, 40:50] = 1.0

    # Create test image
    im1 = np.zeros((100, 100), np.float32)
    im1 = pirt.Aarray(im1, (0.6, 2.0))
    im1[40:50, 44:54] = 1.0

    return im0, im1
Beispiel #2
0
def get_data_big_deform():
    """ Create an image with a block if white pixels and two deforms, one to
    move it down, and another to move it right, in the form of a field and
    a pointset.
    """
    
    # Create test image
    im0 = np.zeros((100, 100), np.float32)
    im0 = pirt.Aarray(im0, (0.25, 2.0))
    im0[30:40, 40:50] = 1.0
    c0 = cog(im0)
    
    # Create test deformation fields - 50 px down and 40 px right
    dfield1 = np.zeros((100, 100), np.float32), np.zeros((100, 100), np.float32)
    weight1 = np.zeros((100, 100), np.float32)
    dfield1[0][35+50, 45] = -50 * im0.sampling[0]  # because backward, correct for sampling
    weight1[35+50, 45] = 1
    #
    dfield2 = np.zeros((100, 100), np.float32), np.zeros((100, 100), np.float32)
    weight2 = np.zeros((100, 100), np.float32)
    dfield2[1][85, 45+40] = -40 * im0.sampling[1]
    weight2[85, 45+40] = 1
    
    # Create test deformation pointsets - 50 px down and 40 px right
    pp1, pp2, pp3 = PointSet(2), PointSet(2), PointSet(2)
    pp1.append(45, 35) # begin pos
    pp2.append(45, 85) # intermediate
    pp3.append(85, 85) # end pos
    for pp in (pp1, pp2, pp3):
        pp[:] *= PointSet(reversed(im0.sampling))
    
    return im0, c0, dfield1, weight1, dfield2, weight2, pp1, pp2, pp3
Beispiel #3
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]
Beispiel #4
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))
Beispiel #5
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))
Beispiel #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:])
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()
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]
Beispiel #9
0
def create_random_deformation_gaussian(im,
                                       amplitude=1,
                                       min_sigma=10,
                                       nblobs=50,
                                       seed=None):
    """ create_random_deformation(im, amplitude=1, min_sigma=10, nblobs=50, seed=None)
    
    Create a random deformation using Gaussian blobs or different scales.
    Returns a DeformationField instance.
    
    See also the class RandomDeformations.
    
    Parameters
    ----------
    im : numpy array
        The image to create a deformation field for.
    amplitude : scalar
        The relative amplitude of the deformations.
    min_sigma : scalar
        The smallest sigma to create Gaussian blobs for. The largest sigma
        is a quarter of the maximum shape element of the image.
    nblobs : integer
        The amount of Gaussian blobs to compose the deformation with.
    seed : int or None
        Seed for the random numbers to draw. If you want to repeat the 
        same deformations, apply the same seed multiple times.
    
    """

    # Seed generator
    np.random.seed(seed)

    fields = []
    for dimension in range(len(im.shape)):

        # Make field that preserves sampling if it is an Aarray, and is
        # expressed using float32 dtype.
        field = pirt.Aarray(im.shape, fill=0.0, dtype='float32')
        if hasattr(im, 'sampling'):
            field.sampling = im.sampling

        for iter in range(nblobs):

            # Get randomly sampled variables
            if True:
                # Get sigma
                max_sigma = max(field.shape) / 4
                t = 2**np.random.uniform(0, 1)  # between 1 and 2
                sigma = (t - 1) * (max_sigma - min_sigma) + min_sigma

                # Get amplitude
                amp = np.random.uniform(-1, 1) * sigma**0.5 * amplitude

                # Get position
                pos = []
                for d in range(field.ndim):
                    tmp = np.random.uniform(0, field.shape[d])
                    pos.append(int(tmp))

            # Create patch
            patch = pirt.gaussfun.gaussiankernel2(sigma, 0, 0)
            patch = amp * patch / patch.max()

            # Get tail
            tail = int(np.ceil(patch.shape[0] / 2))

            # Put the patch in
            if True:

                # Get upper right and lower left
                pos1, pos2 = [], []
                for d in range(field.ndim):
                    pos1.append(pos[d] - tail)
                    pos2.append(pos[d] + tail)

                # Get patch indices
                pos3, pos4 = [], []
                for d in range(field.ndim):
                    pos3.append(0)
                    pos4.append(tail * 2)

                # Correct indices
                for d in range(field.ndim):
                    if pos1[d] < 0:
                        pos3[d] = -pos1[d]
                        pos1[d] = 0
                    if pos2[d] >= field.shape[d]:
                        pos4[d] = field.shape[d] - pos2[d] - 2
                        pos2[d] = field.shape[d] - 1

                # Build slice objects
                slices_field = []
                slices_patch = []
                for d in range(field.ndim):
                    slices_field.append(slice(pos1[d], pos2[d] + 1))
                    slices_patch.append(slice(pos3[d], pos4[d] + 1))

                # Put patch in
                field[tuple(slices_field)] += patch[tuple(slices_patch)]

        # Store field
        fields.append(field)

    # Make sure the deform is injectrive, and has frozenedges if required
    gridsampling = min_sigma / 2.0
    deform = pirt.DeformationFieldBackward.from_field_multiscale(
        fields, gridsampling, injective=True, frozenedge=True)
    # Apply a bit of Gaussian diffusion
    fields = []
    for field in deform:
        fields.append(pirt.diffuse(field, 1.0))

    # Done
    return pirt.DeformationFieldBackward(*fields)