|
| 1 | +"""Read/write x5 transforms.""" |
| 2 | +import warnings |
| 3 | +import numpy as np |
| 4 | +from scipy.io import loadmat as _read_mat, savemat as _save_mat |
| 5 | +from h5py import File as H5File |
| 6 | +from nibabel import Nifti1Header, Nifti1Image |
| 7 | +from nibabel.affines import from_matvec |
| 8 | +from nitransforms.io.base import ( |
| 9 | + BaseLinearTransformList, |
| 10 | + DisplacementsField, |
| 11 | + LinearParameters, |
| 12 | + TransformIOError, |
| 13 | + TransformFileError, |
| 14 | +) |
| 15 | + |
| 16 | +LPS = np.diag([-1, -1, 1, 1]) |
| 17 | + |
| 18 | +class X5LinearTransform(LinearParameters): |
| 19 | + """A string-based structure for X5 linear transforms.""" |
| 20 | + |
| 21 | + template_dtype = np.dtype( |
| 22 | + [ |
| 23 | + ("type", "i4"), |
| 24 | + ("index", "i4"), |
| 25 | + ("parameters", "f8", (4, 4)), |
| 26 | + ("offset", "f4", 3), # Center of rotation |
| 27 | + ] |
| 28 | + ) |
| 29 | + dtype = template_dtype |
| 30 | + |
| 31 | + def __init__(self, parameters=None, offset=None): |
| 32 | + return |
| 33 | + |
| 34 | + def __str__(self): |
| 35 | + return |
| 36 | + |
| 37 | + def to_filename(self, filename): |
| 38 | + """Store this transform to a file with the appropriate format.""" |
| 39 | + sa = self.structarr |
| 40 | + affine = np.array( |
| 41 | + np.hstack( |
| 42 | + (sa["parameters"][:3, :3].reshape(-1), sa["parameters"][:3, 3]) |
| 43 | + )[..., np.newaxis], |
| 44 | + dtype="f8", |
| 45 | + ) |
| 46 | + return |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def from_filename(cls, filename): |
| 50 | + """Read the struct from a X5 file given its path.""" |
| 51 | + if str(filename).endswith(".h5"): |
| 52 | + with H5File(str(filename)) as f: |
| 53 | + return cls.from_h5obj(f) |
0 commit comments