@@ -209,29 +209,55 @@ def from_string(klass, string):
209
209
210
210
val = np .genfromtxt ([valstring .encode ()],
211
211
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
- )
216
212
sa [key ] = val .reshape (sa [key ].shape ) if val .size else ''
217
213
for _ in range (sa ['nxforms' ]):
218
214
lta ._xforms .append (
219
215
LinearTransform .from_string ('\n ' .join (lines [:25 ])))
220
216
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
227
224
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 ''
231
228
232
229
assert len (lta ._xforms ) == sa ['nxforms' ]
233
230
return lta
234
231
235
232
@classmethod
236
233
def from_fileobj (klass , fileobj , check = True ):
237
234
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
+ )
0 commit comments