本文整理汇总了Python中nipy.core.reference.coordinate_map.AffineTransform类的典型用法代码示例。如果您正苦于以下问题:Python AffineTransform类的具体用法?Python AffineTransform怎么用?Python AffineTransform使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AffineTransform类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_equivalent
def test_equivalent():
ijk = CoordinateSystem("ijk")
xyz = CoordinateSystem("xyz")
T = np.random.standard_normal((4, 4))
T[-1] = [0, 0, 0, 1]
A = AffineTransform(ijk, xyz, T)
# now, cycle through
# all possible permutations of
# 'ijk' and 'xyz' and confirm that
# the mapping is equivalent
yield assert_false, equivalent(A, A.renamed_domain({"i": "foo"}))
try:
import itertools
for pijk in itertools.permutations("ijk"):
for pxyz in itertools.permutations("xyz"):
B = A.reordered_domain(pijk).reordered_range(pxyz)
yield assert_true, equivalent(A, B)
except ImportError:
# just do some if we can't find itertools
for pijk in ["ikj", "kij"]:
for pxyz in ["xzy", "yxz"]:
B = A.reordered_domain(pijk).reordered_range(pxyz)
yield assert_true, equivalent(A, B)
示例2: test_drop_io_dim
def test_drop_io_dim():
# test ordinary case of 4d to 3d
cm4d = AffineTransform.from_params('ijkl', 'xyzt', np.diag([1,2,3,4,1]))
cm3d = drop_io_dim(cm4d, 't')
yield assert_array_equal(cm3d.affine, np.diag([1, 2, 3, 1]))
# 3d to 2d
cm3d = AffineTransform.from_params('ijk', 'xyz', np.diag([1,2,3,1]))
cm2d = drop_io_dim(cm3d, 'z')
yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1]))
# test zero scaling for dropped dimension
cm3d = AffineTransform.from_params('ijk', 'xyz', np.diag([1, 2, 0, 1]))
cm2d = drop_io_dim(cm3d, 'z')
yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1]))
# test not diagonal but orthogonal
aff = np.array([[1, 0, 0, 0],
[0, 0, 2, 0],
[0, 3, 0, 0],
[0, 0, 0, 1]])
cm3d = AffineTransform.from_params('ijk', 'xyz', aff)
cm2d = drop_io_dim(cm3d, 'z')
yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1]))
cm2d = drop_io_dim(cm3d, 'k')
yield assert_array_equal(cm2d.affine, np.diag([1, 3, 1]))
# and with zeros scaling for orthogonal dropped dimension
aff[2] = 0
cm3d = AffineTransform.from_params('ijk', 'xyz', aff)
cm2d = drop_io_dim(cm3d, 'z')
yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1]))
示例3: test_compose
def test_compose():
value = np.array([[1.0, 2.0, 3.0]]).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])
affine1 = AffineTransform.from_params("ijk", "xyz", aff1)
aff2 = np.diag([4, 5, 6, 1])
affine2 = AffineTransform.from_params("xyz", "abc", aff2)
# compose mapping from 'ijk' to 'abc'
compcm = compose(affine2, affine1)
yield assert_equal, compcm.function_domain.coord_names, ("i", "j", "k")
yield assert_equal, compcm.function_range.coord_names, ("a", "b", "c")
yield assert_equal, compcm.affine, np.dot(aff2, aff1)
# check invalid coordinate mappings
yield assert_raises, ValueError, compose, affine1, affine2
yield assert_raises, ValueError, compose, affine1, "foo"
cm1 = CoordinateMap(CoordinateSystem("ijk"), CoordinateSystem("xyz"), np.log)
cm2 = CoordinateMap(CoordinateSystem("xyz"), CoordinateSystem("abc"), np.exp)
yield assert_raises, ValueError, compose, cm1, cm2
示例4: test_equivalent
def test_equivalent():
ijk = CoordinateSystem('ijk')
xyz = CoordinateSystem('xyz')
T = np.random.standard_normal((4,4))
T[-1] = [0,0,0,1]
A = AffineTransform(ijk, xyz, T)
# now, cycle through
# all possible permutations of
# 'ijk' and 'xyz' and confirm that
# the mapping is equivalent
yield assert_false, equivalent(A, A.renamed_domain({'i':'foo'}))
try:
import itertools
for pijk in itertools.permutations('ijk'):
for pxyz in itertools.permutations('xyz'):
B = A.reordered_domain(pijk).reordered_range(pxyz)
yield assert_true, equivalent(A, B)
except (ImportError, AttributeError):
# just do some if we can't find itertools, or if itertools
# doesn't have permutations
for pijk in ['ikj', 'kij']:
for pxyz in ['xzy', 'yxz']:
B = A.reordered_domain(pijk).reordered_range(pxyz)
yield assert_true, equivalent(A, B)
示例5: test_synchronized_order
def test_synchronized_order():
data = np.random.standard_normal((3,4,7,5))
im = Image(data, AffineTransform.from_params('ijkl', 'xyzt', np.diag([1,2,3,4,1])))
im_scrambled = im.reordered_axes('iljk').reordered_reference('xtyz')
im_unscrambled = image.synchronized_order(im_scrambled, im)
yield assert_equal, im_unscrambled.coordmap, im.coordmap
yield assert_almost_equal, im_unscrambled.get_data(), im.get_data()
yield assert_equal, im_unscrambled, im
yield assert_true, im_unscrambled == im
yield assert_false, im_unscrambled != im
# the images don't have to be the same shape
data2 = np.random.standard_normal((3,11,9,4))
im2 = Image(data, AffineTransform.from_params('ijkl', 'xyzt', np.diag([1,2,3,4,1])))
im_scrambled2 = im2.reordered_axes('iljk').reordered_reference('xtyz')
im_unscrambled2 = image.synchronized_order(im_scrambled2, im)
yield assert_equal, im_unscrambled2.coordmap, im.coordmap
# or the same coordmap
data3 = np.random.standard_normal((3,11,9,4))
im3 = Image(data, AffineTransform.from_params('ijkl', 'xyzt', np.diag([1,9,3,-2,1])))
im_scrambled3 = im3.reordered_axes('iljk').reordered_reference('xtyz')
im_unscrambled3 = image.synchronized_order(im_scrambled3, im)
yield assert_equal, im_unscrambled3.axes, im.axes
yield assert_equal, im_unscrambled3.reference, im.reference
示例6: test_compose
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])
affine1 = AffineTransform.from_params('ijk', 'xyz', aff1)
aff2 = np.diag([4,5,6,1])
affine2 = AffineTransform.from_params('xyz', 'abc', aff2)
# compose mapping from 'ijk' to 'abc'
compcm = compose(affine2, affine1)
yield assert_equal, compcm.function_domain.coord_names, ('i', 'j', 'k')
yield assert_equal, compcm.function_range.coord_names, ('a', 'b', 'c')
yield assert_equal, compcm.affine, np.dot(aff2, aff1)
# check invalid coordinate mappings
yield assert_raises, ValueError, compose, affine1, affine2
yield assert_raises, ValueError, compose, affine1, 'foo'
cm1 = CoordinateMap(CoordinateSystem('ijk'),
CoordinateSystem('xyz'), np.log)
cm2 = CoordinateMap(CoordinateSystem('xyz'),
CoordinateSystem('abc'), np.exp)
yield assert_raises, ValueError, compose, cm1, cm2
示例7: test_affine_inverse
def test_affine_inverse():
incs, outcs, aff = affine_v2w()
inv = np.linalg.inv(aff)
cm = AffineTransform(incs, outcs, aff)
x = np.array([10, 20, 30], np.float)
x_roundtrip = cm(cm.inverse()(x))
yield assert_equal, x_roundtrip, x
badaff = np.array([[1, 2, 3], [0, 0, 1]])
badcm = AffineTransform(CoordinateSystem("ij"), CoordinateSystem("x"), badaff)
yield assert_equal, badcm.inverse(), None
示例8: test_rollaxis
def test_rollaxis():
data = np.random.standard_normal((3,4,7,5))
im = Image(data, AffineTransform.from_params('ijkl', 'xyzt', np.diag([1,2,3,4,1])))
# for the inverse we must specify an integer
yield assert_raises, ValueError, image.rollaxis, im, 'i', True
# Check that rollaxis preserves diagonal affines, as claimed
yield assert_almost_equal, image.rollaxis(im, 1).affine, np.diag([2,1,3,4,1])
yield assert_almost_equal, image.rollaxis(im, 2).affine, np.diag([3,1,2,4,1])
yield assert_almost_equal, image.rollaxis(im, 3).affine, np.diag([4,1,2,3,1])
# Check that ambiguous axes raise an exception
# 'l' appears both as an axis and a reference coord name
# and in different places
im_amb = Image(data, AffineTransform.from_params('ijkl', 'xylt', np.diag([1,2,3,4,1])))
yield assert_raises, ValueError, image.rollaxis, im_amb, 'l'
# But if it's unambiguous, then
# 'l' can appear both as an axis and a reference coord name
im_unamb = Image(data, AffineTransform.from_params('ijkl', 'xyzl', np.diag([1,2,3,4,1])))
im_rolled = image.rollaxis(im_unamb, 'l')
yield assert_almost_equal, im_rolled.get_data(), \
im_unamb.get_data().transpose([3,0,1,2])
for i, o, n in zip('ijkl', 'xyzt', range(4)):
im_i = image.rollaxis(im, i)
im_o = image.rollaxis(im, o)
im_n = image.rollaxis(im, n)
yield assert_almost_equal, im_i.get_data(), \
im_o.get_data()
yield assert_almost_equal, im_i.affine, \
im_o.affine
yield assert_almost_equal, im_n.get_data(), \
im_o.get_data()
for _im in [im_n, im_o, im_i]:
im_n_inv = image.rollaxis(_im, n, inverse=True)
yield assert_almost_equal, im_n_inv.affine, \
im.affine
yield assert_almost_equal, im_n_inv.get_data(), \
im.get_data()
示例9: test__eq__
def test__eq__():
yield assert_true, E.a == E.a
yield assert_false, E.a != E.a
yield assert_false, E.a == E.b
yield assert_true, E.a != E.b
yield assert_true, E.singular == E.singular
yield assert_false, E.singular != E.singular
A = AffineTransform.from_params('ijk', 'xyz', np.diag([4,3,2,1]))
B = AffineTransform.from_params('ijk', 'xyz', np.diag([4,3,2,1]))
yield assert_true, A == B
yield assert_false, A != B
示例10: fromarray
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 = AffineTransform.from_start_step(innames,
outnames,
(0.,)*ndim,
(1.,)*ndim)
return Image(data, coordmap)
示例11: test_affine_start_step
def test_affine_start_step():
incs, outcs, aff = affine_v2w()
start = aff[:3, 3]
step = aff.diagonal()[:3]
cm = AffineTransform.from_start_step(incs.coord_names, outcs.coord_names, start, step)
yield assert_equal, cm.affine, aff
yield assert_raises, ValueError, AffineTransform.from_start_step, "ijk", "xy", start, step
示例12: test_renamed
def test_renamed():
A = AffineTransform.from_params("ijk", "xyz", np.identity(4))
ijk = CoordinateSystem("ijk")
xyz = CoordinateSystem("xyz")
C = CoordinateMap(ijk, xyz, np.log)
for B in [A, C]:
B_re = B.renamed_domain({"i": "foo"})
yield assert_equal, B_re.function_domain.coord_names, ("foo", "j", "k")
B_re = B.renamed_domain({"i": "foo", "j": "bar"})
yield assert_equal, B_re.function_domain.coord_names, ("foo", "bar", "k")
B_re = B.renamed_range({"y": "foo"})
yield assert_equal, B_re.function_range.coord_names, ("x", "foo", "z")
B_re = B.renamed_range({0: "foo", 1: "bar"})
yield assert_equal, B_re.function_range.coord_names, ("foo", "bar", "z")
B_re = B.renamed_domain({0: "foo", 1: "bar"})
yield assert_equal, B_re.function_domain.coord_names, ("foo", "bar", "k")
B_re = B.renamed_range({"y": "foo", "x": "bar"})
yield assert_equal, B_re.function_range.coord_names, ("bar", "foo", "z")
yield assert_raises, ValueError, B.renamed_range, {"foo": "y"}
yield assert_raises, ValueError, B.renamed_domain, {"foo": "y"}
示例13: test_renamed
def test_renamed():
A = AffineTransform.from_params('ijk', 'xyz', np.identity(4))
ijk = CoordinateSystem('ijk')
xyz = CoordinateSystem('xyz')
C = CoordinateMap(ijk, xyz, np.log)
for B in [A,C]:
B_re = B.renamed_domain({'i':'foo'})
yield assert_equal, B_re.function_domain.coord_names, ('foo', 'j', 'k')
B_re = B.renamed_domain({'i':'foo','j':'bar'})
yield assert_equal, B_re.function_domain.coord_names, ('foo', 'bar', 'k')
B_re = B.renamed_range({'y':'foo'})
yield assert_equal, B_re.function_range.coord_names, ('x', 'foo', 'z')
B_re = B.renamed_range({0:'foo',1:'bar'})
yield assert_equal, B_re.function_range.coord_names, ('foo', 'bar', 'z')
B_re = B.renamed_domain({0:'foo',1:'bar'})
yield assert_equal, B_re.function_domain.coord_names, ('foo', 'bar', 'k')
B_re = B.renamed_range({'y':'foo','x':'bar'})
yield assert_equal, B_re.function_range.coord_names, ('bar', 'foo', 'z')
yield assert_raises, ValueError, B.renamed_range, {'foo':'y'}
yield assert_raises, ValueError, B.renamed_domain, {'foo':'y'}
示例14: test_affine_identity
def test_affine_identity():
aff = AffineTransform.identity('ijk')
yield assert_equal, aff.affine, np.eye(4)
yield assert_equal, aff.function_domain, aff.function_range
x = np.array([3, 4, 5])
# AffineTransform's aren't CoordinateMaps, so
# they don't have "function" attributes
yield assert_false, hasattr(aff, 'function')
示例15: test_product
def test_product():
affine1 = AffineTransform.from_params("i", "x", np.diag([2, 1]))
affine2 = AffineTransform.from_params("j", "y", np.diag([3, 1]))
affine = product(affine1, affine2)
cm1 = CoordinateMap(CoordinateSystem("i"), CoordinateSystem("x"), np.log)
cm2 = CoordinateMap(CoordinateSystem("j"), CoordinateSystem("y"), np.log)
cm = product(cm1, cm2)
yield assert_equal, affine.function_domain.coord_names, ("i", "j")
yield assert_equal, affine.function_range.coord_names, ("x", "y")
yield assert_almost_equal, cm([3, 4]), np.log([3, 4])
yield assert_almost_equal, cm.function([[3, 4], [5, 6]]), np.log([[3, 4], [5, 6]])
yield assert_equal, affine.function_domain.coord_names, ("i", "j")
yield assert_equal, affine.function_range.coord_names, ("x", "y")
yield assert_equal, affine.affine, np.diag([2, 3, 1])