Skip to content

Commit d81d93a

Browse files
authored
Merge pull request #145 from poldracklab/fix/lta-load-array-xfail
FIX: Refactor of LTA implementation
2 parents b3b188e + a5b9752 commit d81d93a

File tree

13 files changed

+316
-199
lines changed

13 files changed

+316
-199
lines changed

nitransforms/io/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22
# vi: set ft=python sts=4 ts=4 sw=4 et:
33
"""Read and write transforms."""
44
from . import afni, fsl, itk, lta
5-
from .lta import LinearTransform, LinearTransformArray, VolumeGeometry
6-
75

86
__all__ = [
97
"afni",
108
"fsl",
119
"itk",
1210
"lta",
13-
"LinearTransform",
14-
"LinearTransformArray",
15-
"VolumeGeometry",
1611
]

nitransforms/io/base.py

Lines changed: 45 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,24 @@ def __init__(self, binaryblock=None, endianness=None, check=True):
2222
if binaryblock is not None and _dtype == self.dtype:
2323
self._structarr = binaryblock.copy()
2424
return
25-
super(StringBasedStruct, self).__init__(binaryblock, endianness, check)
25+
super().__init__(binaryblock, endianness, check)
2626

2727
def __array__(self):
2828
"""Return the internal structure array."""
2929
return self._structarr
3030

31+
def to_string(self):
32+
"""Convert to a string directly writeable to file."""
33+
raise NotImplementedError
3134

32-
class LinearParameters(StringBasedStruct):
33-
"""
34-
A string-based structure for linear transforms.
35-
36-
Examples
37-
--------
38-
>>> lp = LinearParameters()
39-
>>> np.all(lp.structarr['parameters'] == np.eye(4))
40-
True
41-
42-
>>> p = np.diag([2., 2., 2., 1.])
43-
>>> lp = LinearParameters(p)
44-
>>> np.all(lp.structarr['parameters'] == p)
45-
True
35+
@classmethod
36+
def from_string(cls, string):
37+
"""Read the struct from string."""
38+
raise NotImplementedError
4639

47-
"""
4840

49-
template_dtype = np.dtype([("parameters", "f8", (4, 4))])
50-
dtype = template_dtype
51-
52-
def __init__(self, parameters=None):
53-
"""Initialize with default parameters."""
54-
super().__init__()
55-
self.structarr["parameters"] = np.eye(4)
56-
if parameters is not None:
57-
self.structarr["parameters"] = parameters
41+
class LinearTransformStruct(StringBasedStruct):
42+
"""File data structure from linear transforms."""
5843

5944
def to_filename(self, filename):
6045
"""Store this transform to a file with the appropriate format."""
@@ -78,12 +63,44 @@ def from_fileobj(cls, fileobj, check=True):
7863
return cls.from_string(fileobj.read())
7964

8065
@classmethod
81-
def from_string(cls, string):
82-
"""Read the struct from string."""
66+
def from_ras(cls, ras, moving=None, reference=None):
67+
"""Create an affine from a nitransform's RAS+ matrix."""
8368
raise NotImplementedError
8469

8570

86-
class BaseLinearTransformList(StringBasedStruct):
71+
class LinearParameters(LinearTransformStruct):
72+
"""
73+
A string-based structure for linear transforms.
74+
75+
Examples
76+
--------
77+
>>> lp = LinearParameters()
78+
>>> np.all(lp.structarr['parameters'] == np.eye(4))
79+
True
80+
81+
>>> p = np.diag([2., 2., 2., 1.])
82+
>>> lp = LinearParameters(p)
83+
>>> np.all(lp.structarr['parameters'] == p)
84+
True
85+
86+
"""
87+
88+
template_dtype = np.dtype([("parameters", "f8", (4, 4))])
89+
dtype = template_dtype
90+
91+
def __init__(self, parameters=None):
92+
"""
93+
Initialize with default parameters.
94+
95+
96+
"""
97+
super().__init__()
98+
self.structarr["parameters"] = np.eye(4)
99+
if parameters is not None:
100+
self.structarr["parameters"] = parameters
101+
102+
103+
class BaseLinearTransformList(LinearTransformStruct):
87104
"""A string-based structure for series of linear transforms."""
88105

89106
template_dtype = np.dtype([("nxforms", "i4")])
@@ -113,41 +130,6 @@ def __getitem__(self, idx):
113130
return len(self._xforms)
114131
raise KeyError(idx)
115132

116-
def to_filename(self, filename):
117-
"""Store this transform to a file with the appropriate format."""
118-
with open(str(filename), "w") as f:
119-
f.write(self.to_string())
120-
121-
def to_ras(self, moving=None, reference=None):
122-
"""Return a nitransforms' internal RAS matrix."""
123-
raise NotImplementedError
124-
125-
def to_string(self):
126-
"""Convert to a string directly writeable to file."""
127-
raise NotImplementedError
128-
129-
@classmethod
130-
def from_filename(cls, filename):
131-
"""Read the struct from a file given its path."""
132-
with open(str(filename)) as f:
133-
string = f.read()
134-
return cls.from_string(string)
135-
136-
@classmethod
137-
def from_fileobj(cls, fileobj, check=True):
138-
"""Read the struct from a file object."""
139-
return cls.from_string(fileobj.read())
140-
141-
@classmethod
142-
def from_ras(cls, ras, moving=None, reference=None):
143-
"""Create an ITK affine from a nitransform's RAS+ matrix."""
144-
raise NotImplementedError
145-
146-
@classmethod
147-
def from_string(cls, string):
148-
"""Read the struct from string."""
149-
raise NotImplementedError
150-
151133

152134
class DisplacementsField:
153135
"""A data structure representing displacements fields."""

nitransforms/io/fsl.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def from_string(cls, string):
6868
if not lines or len(lines) < 4:
6969
raise TransformFileError
7070

71-
print(lines)
7271
sa["parameters"] = np.genfromtxt(
7372
["\n".join(lines)], dtype=cls.dtype["parameters"]
7473
)

0 commit comments

Comments
 (0)