Skip to content

Commit 9557221

Browse files
committed
enh: interal vox2ras conversion
1 parent 515fb0a commit 9557221

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

nitransforms/io.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,29 +209,55 @@ def from_string(klass, string):
209209

210210
val = np.genfromtxt([valstring.encode()],
211211
dtype=klass.dtype[key])
212-
if val not in (0, 1):
213-
raise NotImplementedError(
214-
"LTA type {0} is not currently supported".format(transform_codes.label[val])
215-
)
216212
sa[key] = val.reshape(sa[key].shape) if val.size else ''
217213
for _ in range(sa['nxforms']):
218214
lta._xforms.append(
219215
LinearTransform.from_string('\n'.join(lines[:25])))
220216
lines = lines[25:]
221-
for key in ('subject', 'fscale'):
222-
# Optional keys
223-
if not lines[0].startswith(key):
224-
continue
225-
label, valstring = lines.pop(0).split(' ')
226-
assert label.strip() == key
217+
if lines:
218+
for key in ('subject', 'fscale'):
219+
# Optional keys
220+
if not lines[0].startswith(key):
221+
continue
222+
label, valstring = lines.pop(0).split(' ')
223+
assert label.strip() == key
227224

228-
val = np.genfromtxt([valstring.encode()],
229-
dtype=klass.dtype[key])
230-
sa[key] = val.reshape(sa[key].shape) if val.size else ''
225+
val = np.genfromtxt([valstring.encode()],
226+
dtype=klass.dtype[key])
227+
sa[key] = val.reshape(sa[key].shape) if val.size else ''
231228

232229
assert len(lta._xforms) == sa['nxforms']
233230
return lta
234231

235232
@classmethod
236233
def from_fileobj(klass, fileobj, check=True):
237234
return klass.from_string(fileobj.read())
235+
236+
def as_type(self, target):
237+
"""
238+
Convert the internal transformation matrix to a different type inplace
239+
240+
Parameters
241+
----------
242+
target : str, int
243+
Tranformation type
244+
"""
245+
assert self['nxforms'] == 1, "Cannot convert multiple transformations"
246+
xform = self['xforms'][0]
247+
src = xform['src']
248+
dst = xform['dst']
249+
current = self['type']
250+
if isinstance(target, str):
251+
target = transform_codes.code[target]
252+
253+
# VOX2VOX -> RAS2RAS
254+
if current == 0 and target == 1:
255+
M = np.linalg.inv(src.as_affine()).dot(xform['m_L']).dot(dst.as_affine())
256+
xform['m_L'] = M
257+
else:
258+
raise NotImplementedError(
259+
"Converting {0} to {1} is not yet available".format(
260+
transform_codes.label[current],
261+
transform_codes.label[target]
262+
)
263+
)

nitransforms/linear.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
151151
singlemat = np.linalg.inv(movaff).dot(self._matrix[0].dot(reference.affine))
152152

153153
if singlemat is not None and nvols > nmats:
154-
print('Warning: resampling a 4D volume with a single affine matrix',
155-
file=sys.stderr)
154+
warnings.warn('Resampling a 4D volume with a single affine matrix')
156155

157156
# Compose an index to index affine matrix
158157
moved = []
@@ -274,7 +273,7 @@ def to_filename(self, filename, fmt='X5', moving=None):
274273
# for FSL / FS information
275274
if not moving:
276275
moving = self.reference
277-
elif isinstance(moving, str):
276+
if isinstance(moving, str):
278277
moving = loadimg(moving)
279278

280279
if fmt.lower() == 'fsl':
@@ -347,7 +346,9 @@ def load(filename, fmt='X5', reference=None):
347346
with open(filename) as ltafile:
348347
lta = LinearTransformArray.from_fileobj(ltafile)
349348
assert lta['nxforms'] == 1 # ever have multiple transforms?
350-
matrix = lta._xforms[0]['m_L']
349+
if lta['type'] != 1:
350+
lta.as_type(1)
351+
matrix = lta['xforms'][0]['m_L']
351352
elif fmt.lower() in ('x5', 'bids'):
352353
raise NotImplementedError
353354
else:

0 commit comments

Comments
 (0)