Skip to content

Commit 55c98b6

Browse files
committed
fix: itk text transform file indices start from zero
1 parent e4b93aa commit 55c98b6

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

nitransforms/io/itk.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33
from scipy.io import savemat as _save_mat
44
from nibabel.affines import from_matvec
5-
from .base import StringBasedStruct, _read_mat
5+
from .base import StringBasedStruct, _read_mat, TransformFileError
66

77
LPS = np.diag([-1, -1, 1, 1])
88

@@ -23,7 +23,7 @@ class ITKLinearTransform(StringBasedStruct):
2323
def __init__(self, parameters=None, offset=None):
2424
"""Initialize with default offset and index."""
2525
super().__init__()
26-
self.structarr['index'] = 1
26+
self.structarr['index'] = 0
2727
self.structarr['offset'] = offset or [0, 0, 0]
2828
self.structarr['parameters'] = np.eye(4)
2929
if parameters is not None:
@@ -76,7 +76,7 @@ def to_string(self, banner=None):
7676
string = '%s'
7777

7878
if banner is None:
79-
banner = self.structarr['index'] == 1
79+
banner = self.structarr['index'] == 0
8080

8181
if banner:
8282
string = '#Insight Transform File V1.0\n%s'
@@ -103,7 +103,7 @@ def from_matlab_dict(cls, mdict, index=None):
103103
if index is not None:
104104
raise NotImplementedError
105105

106-
sa['index'] = 1
106+
sa['index'] = 0
107107
parameters = np.eye(4, dtype='f4')
108108
parameters[:3, :3] = mdict['AffineTransform_float_3_3'][:-3].reshape((3, 3))
109109
parameters[:3, 3] = mdict['AffineTransform_float_3_3'][-3:].flatten()
@@ -116,7 +116,7 @@ def from_ras(cls, ras, index=0):
116116
"""Create an ITK affine from a nitransform's RAS+ matrix."""
117117
tf = cls()
118118
sa = tf.structarr
119-
sa['index'] = index + 1
119+
sa['index'] = index
120120
sa['parameters'] = LPS.dot(ras.dot(LPS))
121121
return tf
122122

@@ -171,7 +171,7 @@ def xforms(self, value):
171171

172172
# Update indexes
173173
for i, val in enumerate(self._xforms):
174-
val['index'] = i + 1
174+
val['index'] = i
175175

176176
def __getitem__(self, idx):
177177
"""Allow dictionary access to the transforms."""
@@ -197,7 +197,7 @@ def to_string(self):
197197
"""Convert to a string directly writeable to file."""
198198
strings = []
199199
for i, xfm in enumerate(self.xforms):
200-
xfm.structarr['index'] = i + 1
200+
xfm.structarr['index'] = i
201201
strings.append(xfm.to_string())
202202
return '\n'.join(strings)
203203

nitransforms/tests/data/itktflist.tfm

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
#Insight Transform File V1.0
2-
#Transform 1
2+
#Transform 0
33
Transform: AffineTransform_float_3_3
44
Parameters: 1 0 0 0 1 0 0 0 1 0 0 0
55
FixedParameters: 10 10 10
66

7-
#Transform 2
7+
#Transform 1
88
Transform: AffineTransform_float_3_3
99
Parameters: 1 0 0 0 1 0 0 0 1 0 0 0
1010
FixedParameters: 10 10 10
1111

12-
#Transform 3
12+
#Transform 2
1313
Transform: AffineTransform_float_3_3
1414
Parameters: 1 0 0 0 1 0 0 0 1 0 0 0
1515
FixedParameters: 10 10 10
1616

17-
#Transform 4
17+
#Transform 3
1818
Transform: AffineTransform_float_3_3
1919
Parameters: 1 0 0 0 1 0 0 0 1 0 0 0
2020
FixedParameters: 10 10 10
2121

22-
#Transform 5
22+
#Transform 4
2323
Transform: AffineTransform_float_3_3
2424
Parameters: -1.53626 0.71973 -0.639856 -0.190759 -1.80082 -0.915885 0.502537 1.12532 0.275748 0.393413 1.13855 0.761131
2525
FixedParameters: -0.0993171 0.364984 1.99264
2626

27-
#Transform 6
27+
#Transform 5
2828
Transform: AffineTransform_float_3_3
2929
Parameters: -0.130507 -1.03017 2.08189 -1.51723 1.37849 -0.0890962 -0.656323 0.242694 2.15801 -1.26689 0.367131 1.23616
3030
FixedParameters: 0.626607 0.15351 1.24982
3131

32-
#Transform 7
32+
#Transform 6
3333
Transform: AffineTransform_float_3_3
3434
Parameters: -1.55395 -0.36383 -0.17749 1.3387 -0.384534 -0.901462 -1.06598 -0.448228 -1.07535 1.92599 0.454696 0.576697
3535
FixedParameters: -0.425602 0.333406 -1.14957
3636

37-
#Transform 8
37+
#Transform 7
3838
Transform: AffineTransform_float_3_3
3939
Parameters: 0.723719 -1.05617 -0.800562 -2.47048 -1.76301 -1.4447 -0.749896 1.29774 -1.48893 1.02789 0.65017 -1.48326
4040
FixedParameters: 0.800882 -1.20202 1.25495
4141

42-
#Transform 9
42+
#Transform 8
4343
Transform: AffineTransform_float_3_3
4444
Parameters: 1.24025 -0.77628 0.618013 -0.523829 1.09471 1.66921 0.73753 -1.33588 -0.627659 -0.449913 -0.00124181 0.21433
4545
FixedParameters: -0.226504 -0.877893 0.2608

nitransforms/tests/test_io.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_ITKLinearTransformArray(tmpdir, data_path):
113113
for _ in range(4)])
114114

115115
assert itklist['nxforms'] == 4
116-
assert itklist['xforms'][1].structarr['index'] == 2
116+
assert itklist['xforms'][1].structarr['index'] == 1
117117

118118
with pytest.raises(KeyError):
119119
itklist['invalid_key']

0 commit comments

Comments
 (0)