Skip to content

Commit 4c9a3ed

Browse files
Julien Marabottooesteban
authored andcommitted
X5.py created
(cherry picked from commit 49bf4c3)
1 parent eff0b71 commit 4c9a3ed

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

nitransforms/io/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
22
# vi: set ft=python sts=4 ts=4 sw=4 et:
33
"""Read and write transforms."""
4-
from nitransforms.io import afni, fsl, itk, lta
4+
from nitransforms.io import afni, fsl, itk, lta, x5
55
from nitransforms.io.base import TransformIOError, TransformFileError
66

77
__all__ = [
@@ -22,6 +22,7 @@
2222
"fs": (lta, "FSLinearTransform"),
2323
"fsl": (fsl, "FSLLinearTransform"),
2424
"afni": (afni, "AFNILinearTransform"),
25+
"x5": (x5, "X5LinearTransform")
2526
}
2627

2728

nitransforms/io/x5.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)