Skip to content

Commit 6b47c72

Browse files
committed
fix+enh: fixes from @oesteban code review
1 parent a1c5033 commit 6b47c72

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

nitransforms/io/lta.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from nibabel.volumeutils import Recoder
44
from nibabel.affines import voxel_sizes
55

6-
from .base import StringBasedStruct
6+
from .base import StringBasedStruct, TransformFileError
77

88

99
transform_codes = Recoder((
@@ -20,10 +20,10 @@ class VolumeGeometry(StringBasedStruct):
2020
('valid', 'i4'), # Valid values: 0, 1
2121
('volume', 'i4', (3, 1)), # width, height, depth
2222
('voxelsize', 'f4', (3, 1)), # xsize, ysize, zsize
23-
('xras', 'f4', (3, 1)), # x_r, x_a, x_s
24-
('yras', 'f4', (3, 1)), # y_r, y_a, y_s
25-
('zras', 'f4', (3, 1)), # z_r, z_a, z_s
26-
('cras', 'f4', (3, 1)), # c_r, c_a, c_s
23+
('xras', 'f8', (3, 1)), # x_r, x_a, x_s
24+
('yras', 'f8', (3, 1)), # y_r, y_a, y_s
25+
('zras', 'f8', (3, 1)), # z_r, z_a, z_s
26+
('cras', 'f8', (3, 1)), # c_r, c_a, c_s
2727
('filename', 'U1024')]) # Not conformant (may be >1024 bytes)
2828
dtype = template_dtype
2929

@@ -80,7 +80,7 @@ def from_string(klass, string):
8080
lines = string.splitlines()
8181
for key in ('valid', 'filename', 'volume', 'voxelsize',
8282
'xras', 'yras', 'zras', 'cras'):
83-
label, valstring = lines.pop(0).split(' = ')
83+
label, valstring = lines.pop(0).split(' =')
8484
assert label.strip() == key
8585

8686
val = np.genfromtxt([valstring.encode()],
@@ -94,9 +94,9 @@ class LinearTransform(StringBasedStruct):
9494
template_dtype = np.dtype([
9595
('mean', 'f4', (3, 1)), # x0, y0, z0
9696
('sigma', 'f4'),
97-
('m_L', 'f4', (4, 4)),
98-
('m_dL', 'f4', (4, 4)),
99-
('m_last_dL', 'f4', (4, 4)),
97+
('m_L', 'f8', (4, 4)),
98+
('m_dL', 'f8', (4, 4)),
99+
('m_last_dL', 'f8', (4, 4)),
100100
('src', VolumeGeometry),
101101
('dst', VolumeGeometry),
102102
('label', 'i4')])
@@ -191,13 +191,10 @@ def from_string(klass, string):
191191
lta = klass()
192192
sa = lta.structarr
193193
lines = string.splitlines()
194-
while lines:
195-
if not lines[0].startswith('type'):
196-
lines.pop(0)
197-
continue
198-
break
199-
if not lines:
200-
raise Exception("Invalid LTA format")
194+
lines = [l.strip() for l in string.splitlines()
195+
if l.strip() and not l.strip().startswith('#')]
196+
if not lines or not lines[0].startswith('type'):
197+
raise TransformFileError("Invalid LTA format")
201198
for key in ('type', 'nxforms'):
202199
label, valstring = lines.pop(0).split(' = ')
203200
assert label.strip() == key
@@ -214,12 +211,16 @@ def from_string(klass, string):
214211
# Optional keys
215212
if not lines[0].startswith(key):
216213
continue
217-
label, valstring = lines.pop(0).split(' ')
218-
assert label.strip() == key
214+
try:
215+
label, valstring = lines.pop(0).split(' ')
216+
except ValueError:
217+
sa[key] = ''
218+
else:
219+
assert label.strip() == key
219220

220-
val = np.genfromtxt([valstring.encode()],
221-
dtype=klass.dtype[key])
222-
sa[key] = val.reshape(sa[key].shape) if val.size else ''
221+
val = np.genfromtxt([valstring.encode()],
222+
dtype=klass.dtype[key])
223+
sa[key] = val.reshape(sa[key].shape) if val.size else ''
223224

224225
assert len(lta._xforms) == sa['nxforms']
225226
return lta

nitransforms/linear.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ def load(filename, fmt='X5', reference=None):
247247
if lta['nxforms'] > 1:
248248
raise NotImplementedError("Multiple transforms are not yet supported.")
249249
if lta['type'] != 1:
250-
warnings.warn("Converting LTA to RAS2RAS")
250+
# To make transforms generalize across use-cases, LTA transforms
251+
# are converted to RAS-to-RAS.
251252
lta.set_type(1)
252253
matrix = lta['xforms'][0]['m_L']
253254
elif fmt.lower() in ('x5', 'bids'):

nitransforms/tests/data/affine-RAS.fs.v2v.lta

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
# transform file affine-RAS.fs.v2v.lta
2+
# created by a user on Mon Oct 28 10:09:56 2019
3+
14
type = 0 # LINEAR_VOX_TO_VOX
25
nxforms = 1
36
mean = 0.0000 0.0000 0.0000
47
sigma = 1.0000
58
1 4 4
6-
9.999989867210388e-01 -9.999993490055203e-04 9.999998146668077e-04 1.454572677612305e+00
7-
1.404936425387859e-03 6.216088533401489e-01 -7.833265066146851e-01 3.912971496582031e+01
8-
1.617172238184139e-04 7.833272218704224e-01 6.216096878051758e-01 -1.376794815063477e+01
9-
0.000000000000000e+00 0.000000000000000e+00 0.000000000000000e+00 1.000000000000000e+00
9+
9.999989867210388e-01 -9.999993490055203e-04 9.999998146668077e-04 1.454572677612305e+00
10+
1.404936425387859e-03 6.216088533401489e-01 -7.833265066146851e-01 3.912971496582031e+01
11+
1.617172238184139e-04 7.833272218704224e-01 6.216096878051758e-01 -1.376794815063477e+01
12+
0.000000000000000e+00 0.000000000000000e+00 0.000000000000000e+00 1.000000000000000e+00
1013
src volume info
1114
valid = 1 # volume info valid
1215
filename = unknown

0 commit comments

Comments
 (0)