Exemplo n.º 1
0
def test_product():
    cm1 = Affine.from_params('i', 'x', np.diag([2, 1]))
    cm2 = Affine.from_params('j', 'y', np.diag([3, 1]))
    cm = product(cm1, cm2)
    yield assert_equal, cm.input_coords.coord_names, ('i', 'j')
    yield assert_equal, cm.output_coords.coord_names, ('x', 'y')
    yield assert_equal, cm.affine, np.diag([2, 3, 1])
Exemplo n.º 2
0
def test_affine_copy():
    incs, outcs, aff = affine_v2w()
    cm = Affine(aff, incs, outcs)
    cmcp = cm.copy()
    yield assert_equal, cmcp.affine, cm.affine
    yield assert_equal, cmcp.input_coords, cm.input_coords
    yield assert_equal, cmcp.output_coords, cm.output_coords
Exemplo n.º 3
0
def test_affine_inverse():
    incs, outcs, aff = affine_v2w()
    inv = np.linalg.inv(aff)
    cm = Affine(aff, incs, outcs)
    invmap = cm.inverse_mapping
    x = np.array([10, 20, 30])
    x_roundtrip = cm.mapping(invmap(x))
    yield assert_equal, x_roundtrip, x
    badaff = np.array([[1,2,3],[4,5,6]])
    badcm = Affine(aff, incs, outcs)
    badcm._affine = badaff
    yield assert_raises, ValueError, getattr, badcm, 'inverse_mapping'
Exemplo n.º 4
0
def test_drop_io_dim():
    aff = np.diag([1,2,3,1])
    in_dims = list('ijk')
    out_dims = list('xyz')
    cm = Affine.from_params(in_dims, out_dims, aff)
    yield assert_raises(ValueError, drop_io_dim, cm, 'q')
    for i, d in enumerate(in_dims):
        cm2 = drop_io_dim(cm, d)
        yield assert_equal(cm2.ndim, (2,2))
        ods = out_dims[:]
        ids = in_dims[:]
        ods.pop(i)
        ids.pop(i)
        yield assert_equal(ods, cm2.output_coords.coord_names)
        yield assert_equal(ids, cm2.input_coords.coord_names)
        a2 = cm2.affine
        yield assert_equal(a2.shape, (3,3))
        ind_a = np.ones((4,4), dtype=np.bool)
        ind_a[:,i] = False
        ind_a[i,:] = False
        aff2 = cm.affine[ind_a].reshape((3,3))
        yield assert_array_equal(aff2, a2)
        # Check non-orth case
        waff = np.diag([1,2,3,1])
        wrong_i = (i+1) % 3
        waff[wrong_i,i] = 2 # not OK if in same column as that being removed
        wcm = Affine.from_params(in_dims, out_dims, waff)
        yield assert_raises(ValueError, drop_io_dim, wcm, d)
        raff = np.diag([1,2,3,1])
        raff[i,wrong_i] = 2 # is OK if in same row
        rcm = Affine.from_params(in_dims, out_dims, raff)
        cm2 = drop_io_dim(rcm, d)
    # dims out of range give error
    yield assert_raises(ValueError, drop_io_dim, cm, 'p')
    # only works for affine case
    cm = CoordinateMap(lambda x:x, cm.input_coords, cm.output_coords)
    yield assert_raises(AttributeError, drop_io_dim, cm, 'i')
    # Check non-square case
    aff = np.array([[1.,0,0,0],
                    [0,2,0,0],
                    [0,0,3,0],
                    [0,0,0,1],
                    [0,0,0,1]])
    cm = Affine.from_params(in_dims, out_dims + ['t'], aff)
    cm2 = drop_io_dim(cm, 'i')
    yield assert_array_equal(cm2.affine,
                             [[2,0,0],
                              [0,3,0],
                              [0,0,1],
                              [0,0,1]])
    yield assert_raises(ValueError, drop_io_dim, cm, 't')
Exemplo n.º 5
0
def test_affine_identity():
    aff = Affine.identity('ijk')
    yield assert_equal, aff.affine, np.eye(4)
    yield assert_equal, aff.input_coords, aff.output_coords
    x = np.array([3, 4, 5])
    y = aff.mapping(x)
    yield assert_equal, y, x
Exemplo n.º 6
0
def fromarray(data, innames, outnames, coordmap=None):
    """Create an image from a numpy array.

    Parameters
    ----------
    data : numpy array
        A numpy array of three dimensions.
    innames : sequence
       a list of input axis names
    innames : sequence
       a list of output axis names
    coordmap : A `CoordinateMap`
        If not specified, an identity coordinate map is created.

    Returns
    -------
    image : An `Image` object

    See Also
    --------
    load : function for loading images
    save : function for saving images

    """
    ndim = len(data.shape)
    if not coordmap:
        coordmap = Affine.from_start_step(innames,
                                          outnames,
                                          (0.,)*ndim,
                                          (1.,)*ndim)
    return Image(data, coordmap)
Exemplo n.º 7
0
def test_affine_start_step():
    incs, outcs, aff = affine_v2w()
    start = aff[:3, 3]
    step = aff.diagonal()[:3]
    cm = Affine.from_start_step(incs.coord_names, outcs.coord_names,
                                start, step)
    yield assert_equal, cm.affine, aff
    yield assert_raises, ValueError, Affine.from_start_step, 'ijk', 'xy', \
        start, step
Exemplo n.º 8
0
def test_kernel():
    # Verify the the convolution with a delta function
    # gives the correct answer.
    tol = 0.9999
    sdtol = 1.0e-8
    for x in range(6):
        shape = randint(30,60,(3,))
        ii, jj, kk = randint(11,17, (3,))

        coordmap = Affine.from_start_step('ijk', 'xyz', 
                                          randint(5,20,(3,))*0.25,
                                          randint(5,10,(3,))*0.5)

        signal = np.zeros(shape)
        signal[ii,jj,kk] = 1.
        signal = Image(signal, coordmap=coordmap)
    
        kernel = LinearFilter(coordmap, shape, 
                              fwhm=randint(50,100)/10.)
        ssignal = kernel.smooth(signal)
        ssignal = np.asarray(ssignal)
        ssignal[:] *= kernel.norms[kernel.normalization]

        I = np.indices(ssignal.shape)
        I.shape = (kernel.coordmap.ndim[0], np.product(shape))
        i, j, k = I[:,np.argmax(ssignal[:].flat)]

        yield assert_equal, (i,j,k), (ii,jj,kk)

        Z = kernel.coordmap(I) - kernel.coordmap([i,j,k])

        _k = kernel(Z)
        _k.shape = ssignal.shape

        yield assert_true, (np.corrcoef(_k[:].flat, ssignal[:].flat)[0,1] > tol)

        yield assert_true, ((_k[:] - ssignal[:]).std() < sdtol)
            
        def _indices(i,j,k,axis):
            I = np.zeros((3,20))
            I[0] += i
            I[1] += j
            I[2] += k
            I[axis] += np.arange(-10,10)
            return I

        vx = ssignal[i,j,(k-10):(k+10)]
        vvx = coordmap(_indices(i,j,k,2)) - coordmap([[i],[j],[k]])
        yield assert_true, (np.corrcoef(vx, kernel(vvx))[0,1] > tol)

        vy = ssignal[i,(j-10):(j+10),k]
        vvy = coordmap(_indices(i,j,k,1)) - coordmap([[i],[j],[k]])
        yield assert_true, (np.corrcoef(vy, kernel(vvy))[0,1] > tol)

        vz = ssignal[(i-10):(i+10),j,k]
        vvz = coordmap(_indices(i,j,k,0)) - coordmap([[i],[j],[k]])
        yield assert_true, (np.corrcoef(vz, kernel(vvz))[0,1] > tol)
Exemplo n.º 9
0
def test_linearize():
    aff = np.diag([1,2,3,1])
    cm = Affine.from_params('ijk', 'xyz', aff)
    lincm = linearize(cm.mapping, cm.ndim[0])
    yield assert_equal, lincm, aff
    origin = np.array([10, 20, 30], dtype=cm.input_coords.coord_dtype)
    lincm = linearize(cm.mapping, cm.ndim[0], origin=origin)
    xform = np.array([[  1.,   0.,   0.,  10.],
                      [  0.,   2.,   0.,  40.],
                      [  0.,   0.,   3.,  90.],
                      [  0.,   0.,   0.,   1.]])
    yield assert_equal, lincm, xform
Exemplo n.º 10
0
def setup():
    def f(x):
        return 2*x
    def g(x):
        return x/2.0
    x = CoordinateSystem('x', 'x')
    E.a = CoordinateMap(f, x, x)
    E.b = CoordinateMap(f, x, x, inverse_mapping=g)
    E.c = CoordinateMap(g, x, x)        
    E.d = CoordinateMap(g, x, x, inverse_mapping=f)        
    E.e = Affine.identity('ijk')

    A = np.identity(4)
    A[0:3] = np.random.standard_normal((3,4))
    E.mapping = Affine.from_params('ijk' ,'xyz', A)
    
    E.singular = Affine.from_params('ijk', 'xyzt',
                                    np.array([[ 0,  1,  2,  3],
                                              [ 4,  5,  6,  7],
                                              [ 8,  9, 10, 11],
                                              [ 8,  9, 10, 11],
                                              [ 0,  0,  0,  1]]))
Exemplo n.º 11
0
def test_ArrayLikeObj():
    obj = ArrayLikeObj()
    # create simple coordmap
    xform = np.eye(4)
    coordmap = Affine.from_params('xyz', 'ijk', xform)
    
    # create image form array-like object and coordmap
    img = image.Image(obj, coordmap)
    yield assert_true, img.ndim == 3
    yield assert_true, img.shape == (2,3,4)
    yield assert_true, np.allclose(np.asarray(img), 1)
    yield assert_true, np.allclose(img[:], 1)
    img[:] = 4
    yield assert_true, np.allclose(img[:], 4)
Exemplo n.º 12
0
def test_compose():
    value = np.array([[1., 2., 3.]]).T
    aa = compose(E.a, E.a)
    yield assert_true, aa.inverse is None
    yield assert_true, np.allclose(aa(value), 4*value)
    ab = compose(E.a,E.b)
    yield assert_true, ab.inverse is None
    assert_true, np.allclose(ab(value), 4*value)
    ac = compose(E.a,E.c)
    yield assert_true, ac.inverse is None
    yield assert_true, np.allclose(ac(value), value)
    bb = compose(E.b,E.b)
    yield assert_true, bb.inverse is not None
    aff1 = np.diag([1,2,3,1])
    cm1 = Affine.from_params('ijk', 'xyz', aff1)
    aff2 = np.diag([4,5,6,1])
    cm2 = Affine.from_params('xyz', 'abc', aff2)
    # compose mapping from 'ijk' to 'abc'
    compcm = compose(cm2, cm1)
    yield assert_equal, compcm.input_coords.coord_names, ('i', 'j', 'k')
    yield assert_equal, compcm.output_coords.coord_names, ('a', 'b', 'c')
    yield assert_equal, compcm.affine, np.dot(aff2, aff1)
    # check invalid coordinate mappings
    yield assert_raises, ValueError, compose, cm1, cm2
Exemplo n.º 13
0
def test_append_io_dim():
    aff = np.diag([1,2,3,1])
    in_dims = list('ijk')
    out_dims = list('xyz')
    cm = Affine.from_params(in_dims, out_dims, aff)
    cm2 = append_io_dim(cm, 'l', 't')
    yield assert_array_equal(cm2.affine, np.diag([1,2,3,1,1]))
    yield assert_equal(cm2.output_coords.coord_names,
                       out_dims + ['t'])
    yield assert_equal(cm2.input_coords.coord_names,
                       in_dims + ['l'])
    cm2 = append_io_dim(cm, 'l', 't', 9, 5)
    a2 = np.diag([1,2,3,5,1])
    a2[3,4] = 9
    yield assert_array_equal(cm2.affine, a2)
    yield assert_equal(cm2.output_coords.coord_names,
                       out_dims + ['t'])
    yield assert_equal(cm2.input_coords.coord_names,
                       in_dims + ['l'])
    # non square case
    aff = np.array([[2,0,0],
                    [0,3,0],
                    [0,0,1],
                    [0,0,1]])
    cm = Affine.from_params('ij', 'xyz', aff)
    cm2 = append_io_dim(cm, 'q', 't', 9, 5)
    a2 = np.array([[2,0,0,0],
                   [0,3,0,0],
                   [0,0,0,1],
                   [0,0,5,9],
                   [0,0,0,1]])
    yield assert_array_equal(cm2.affine, a2)
    yield assert_equal(cm2.output_coords.coord_names,
                       list('xyzt'))
    yield assert_equal(cm2.input_coords.coord_names,
                       list('ijq'))
Exemplo n.º 14
0
    def test_box_slice(self):
        coordmap = Affine.identity(names)
        t = zslice(5, [0, 9], [0, 9], coordmap.output_coords, (10,10))
        assert_almost_equal(t.coordmap.affine, [[ 0.,  0.,  5.],
                                                [ 1.,  0.,  0.],
                                                [ 0.,  1.,  0.],
                                                [ 0.,  0.,  1.]])
        

        t = yslice(4, [0, 9], [0, 9], coordmap.output_coords, (10,10))
        assert_almost_equal(t.coordmap.affine, [[ 1.,  0.,  0.],
                                                [ 0.,  0.,  4.],
                                                [ 0.,  1.,  0.],
                                                [ 0.,  0.,  1.]])
        
        t = xslice(3, [0, 9], [0, 9], coordmap.output_coords, (10,10))
        assert_almost_equal(t.coordmap.affine, [[ 1.,  0.,  0.],
                                                [ 0.,  1.,  0.],
                                                [ 0.,  0.,  3.],
                                                [ 0.,  0.,  1.]])
Exemplo n.º 15
0
def test_affine_from_params():
    incs, outcs, aff = affine_v2w()
    cm = Affine.from_params('ijk', 'xyz', aff)
    yield assert_equal, cm.affine, aff
    badaff = np.array([[1,2,3],[4,5,6]])
    yield assert_raises, ValueError, Affine.from_params, 'ijk', 'xyz', badaff
Exemplo n.º 16
0
 def test_bounding_box(self):
     shape = (10, 10, 10)
     coordmap = Affine.identity(names)
     #print coordmap.affine.dtype, 'affine'
     self.assertEqual(bounding_box(coordmap, shape), [[0., 9.], [0, 9], [0, 9]])