4
4
5
5
6
6
class ITKLinearTransform (StringBasedStruct ):
7
+ """A string-based structure for ITK linear transforms."""
8
+
7
9
template_dtype = np .dtype ([
8
10
('type' , 'i4' ),
9
- ('id ' , 'i4' ),
11
+ ('index ' , 'i4' ),
10
12
('parameters' , 'f4' , (4 , 4 )),
11
13
('offset' , 'f4' , 3 ), # Center of rotation
12
14
])
13
15
dtype = template_dtype
14
16
15
17
def __init__ (self ):
18
+ """Initialize with default offset and index."""
16
19
super ().__init__ ()
17
20
self .structarr ['offset' ] = [0 , 0 , 0 ]
18
- self .structarr ['id' ] = 1
21
+ self .structarr ['index' ] = 1
22
+ self .structarr ['parameters' ] = np .eye (4 )
19
23
20
- def to_string (self , banner = True ):
24
+ def __str__ (self ):
25
+ """Generate a string representation."""
21
26
sa = self .structarr
22
27
lines = [
23
- '#Transform {:d}' .format (sa ['id ' ]),
28
+ '#Transform {:d}' .format (sa ['index ' ]),
24
29
'Transform: MatrixOffsetTransformBase_double_3_3' ,
25
30
'Parameters: {}' .format (' ' .join (
26
31
['%g' % p
@@ -29,12 +34,22 @@ def to_string(self, banner=True):
29
34
'FixedParameters: {:g} {:g} {:g}' .format (* sa ['offset' ]),
30
35
'' ,
31
36
]
32
- if banner :
33
- lines .insert (0 , '#Insight Transform File V1.0' )
34
37
return '\n ' .join (lines )
35
38
39
+ def to_string (self , banner = None ):
40
+ """Convert to a string directly writeable to file."""
41
+ string = '%s'
42
+
43
+ if banner is None :
44
+ banner = self .structarr ['index' ] == 1
45
+
46
+ if banner :
47
+ string = '#Insight Transform File V1.0\n %s'
48
+ return string % self
49
+
36
50
@classmethod
37
51
def from_string (klass , string ):
52
+ """Read the struct from string."""
38
53
tf = klass ()
39
54
sa = tf .structarr
40
55
lines = [l for l in string .splitlines ()
@@ -44,7 +59,7 @@ def from_string(klass, string):
44
59
lines = lines [1 :] # Drop banner with version
45
60
46
61
parameters = np .eye (4 , dtype = 'f4' )
47
- sa ['id ' ] = int (lines [0 ][lines [0 ].index ('T' ):].split ()[1 ])
62
+ sa ['index ' ] = int (lines [0 ][lines [0 ].index ('T' ):].split ()[1 ])
48
63
sa ['offset' ] = np .genfromtxt ([lines [3 ].split (':' )[- 1 ].encode ()],
49
64
dtype = klass .dtype ['offset' ])
50
65
vals = np .genfromtxt ([lines [2 ].split (':' )[- 1 ].encode ()],
@@ -56,10 +71,13 @@ def from_string(klass, string):
56
71
57
72
@classmethod
58
73
def from_fileobj (klass , fileobj , check = True ):
74
+ """Read the struct from a file object."""
59
75
return klass .from_string (fileobj .read ())
60
76
61
77
62
78
class ITKLinearTransformArray (StringBasedStruct ):
79
+ """A string-based structure for series of ITK linear transforms."""
80
+
63
81
template_dtype = np .dtype ([('nxforms' , 'i4' )])
64
82
dtype = template_dtype
65
83
_xforms = None
@@ -69,6 +87,7 @@ def __init__(self,
69
87
binaryblock = None ,
70
88
endianness = None ,
71
89
check = True ):
90
+ """Initialize with (optionally) a list of transforms."""
72
91
super ().__init__ (binaryblock , endianness , check )
73
92
self ._xforms = []
74
93
for mat in xforms or []:
@@ -77,25 +96,25 @@ def __init__(self,
77
96
self ._xforms .append (xfm )
78
97
79
98
def __getitem__ (self , idx ):
99
+ """Allow dictionary access to the transforms."""
80
100
if idx == 'xforms' :
81
101
return self ._xforms
82
102
if idx == 'nxforms' :
83
103
return len (self ._xforms )
84
104
return super ().__getitem__ (idx )
85
105
86
106
def to_string (self ):
107
+ """Convert to a string directly writeable to file."""
87
108
strings = []
88
109
for i , xfm in enumerate (self ._xforms ):
89
- xfm .structarr ['id' ] = i + 1
90
- strings .append (xfm .to_string (banner = False ))
91
- strings .insert (0 , '#Insight Transform File V1.0' )
110
+ xfm .structarr ['index' ] = i + 1
111
+ strings .append (xfm .to_string ())
92
112
return '\n ' .join (strings )
93
113
94
114
@classmethod
95
115
def from_string (klass , string ):
116
+ """Read the struct from string."""
96
117
_self = klass ()
97
- sa = _self .structarr
98
-
99
118
lines = [l .strip () for l in string .splitlines ()
100
119
if l .strip ()]
101
120
@@ -110,4 +129,5 @@ def from_string(klass, string):
110
129
111
130
@classmethod
112
131
def from_fileobj (klass , fileobj , check = True ):
132
+ """Read the struct from a file object."""
113
133
return klass .from_string (fileobj .read ())
0 commit comments