Skip to content

Commit 5fb3e40

Browse files
authored
Merge pull request #144 from effigies/fix/fsl_transforms
FIX: Increase FSL serialization precision
2 parents d81d93a + 77d93fd commit 5fb3e40

File tree

6 files changed

+18
-20
lines changed

6 files changed

+18
-20
lines changed

nitransforms/io/fsl.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class FSLLinearTransform(LinearParameters):
2020
def __str__(self):
2121
"""Generate a string representation."""
2222
lines = [
23-
" ".join(["%g" % col for col in row])
23+
" ".join("%.08f" % col for col in row)
2424
for row in self.structarr["parameters"]
2525
]
2626
return "\n".join(lines + [""])
@@ -46,14 +46,14 @@ def from_ras(cls, ras, moving=None, reference=None):
4646

4747
# Adjust for reference image offset and orientation
4848
refswp, refspc = _fsl_aff_adapt(reference)
49-
pre = reference.affine.dot(inv(refspc).dot(inv(refswp)))
49+
pre = reference.affine @ inv(refswp @ refspc)
5050

5151
# Adjust for moving image offset and orientation
5252
movswp, movspc = _fsl_aff_adapt(moving)
53-
post = inv(movswp).dot(movspc.dot(inv(moving.affine)))
53+
post = movswp @ movspc @ inv(moving.affine)
5454

5555
# Compose FSL transform
56-
mat = inv(np.swapaxes(post.dot(ras.dot(pre)), 0, 1))
56+
mat = inv(np.swapaxes(post @ ras @ pre, 0, 1))
5757

5858
tf = cls()
5959
tf.structarr["parameters"] = mat.T
@@ -92,7 +92,7 @@ def to_ras(self, moving=None, reference=None):
9292
pre = refswp @ refspc @ inv(reference.affine)
9393
# Adjust for moving image offset and orientation
9494
movswp, movspc = _fsl_aff_adapt(moving)
95-
post = moving.affine @ inv(movspc) @ inv(movswp)
95+
post = moving.affine @ inv(movswp @ movspc)
9696
mat = self.structarr["parameters"].T
9797
return post @ np.swapaxes(inv(mat), 0, 1) @ pre
9898

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
0.999999 -0.00140494 0.000161717 -3.89014
2-
0.000999999 0.621609 -0.783327 105.905
3-
0.001 0.783327 0.62161 -34.3513
4-
0 0 0 1
1+
0.99999900 -0.00140494 0.00016172 -3.89014000
2+
0.00100000 0.62160900 -0.78332700 105.90500000
3+
0.00100000 0.78332700 0.62161000 -34.35130000
4+
0.00000000 0.00000000 0.00000000 1.00000000
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
0.999999 -0.00140494 -0.000161717 4.14529
2-
0.000999999 0.621609 0.783327 -37.3811
3-
-0.001 -0.783327 0.62161 107.976
4-
0 0 0 1
1+
0.99999900 -0.00140494 -0.00016172 4.14529000
2+
0.00100000 0.62160900 0.78332700 -37.38110000
3+
-0.00100000 -0.78332700 0.62161000 107.97600000
4+
0.00000000 0.00000000 0.00000000 1.00000000
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
0.999998 -0.00181872 -0.0011965 4.26083
2-
0.00206779 0.621609 0.783325 -25.3129
3-
-0.000680894 -0.783326 0.621611 101.967
4-
0 0 0 1
1+
0.99999800 -0.00181872 -0.00119650 4.26083000
2+
0.00206779 0.62160900 0.78332500 -25.31290000
3+
-0.00068089 -0.78332600 0.62161100 101.96700000
4+
0.00000000 0.00000000 0.00000000 1.00000000

nitransforms/tests/test_linear.py

-2
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,11 @@ def test_loadsave(tmp_path, data_path, testdata_path, fmt):
9292
assert np.allclose(
9393
xfm.matrix,
9494
nitl.load(fname, fmt=fmt, reference=ref_file).matrix,
95-
rtol=1e-2, # FSL incurs into large errors due to rounding
9695
)
9796

9897
assert np.allclose(
9998
xfm.matrix,
10099
nitl.load(fname, fmt=fmt, reference=ref_file, moving=ref_file).matrix,
101-
rtol=1e-2, # FSL incurs into large errors due to rounding
102100
)
103101
else:
104102
assert xfm == nitl.load(fname, fmt=fmt, reference=ref_file)

nitransforms/tests/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ def assert_affines_by_filename(affine1, affine2):
2424
else:
2525
xfm1 = np.loadtxt(str(affine1))
2626
xfm2 = np.loadtxt(str(affine2))
27-
np.testing.assert_almost_equal(xfm1, xfm2)
27+
np.allclose(xfm1, xfm2, atol=1e-04)

0 commit comments

Comments
 (0)