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])
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')
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
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]]))
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)
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
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'))
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