66# copyright and license terms.
77#
88### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9- ''' Linear transforms '''
10- from __future__ import division , print_function , absolute_import
9+ """Linear transforms."""
1110import sys
1211import numpy as np
1312from scipy import ndimage as ndi
2120
2221
2322class Affine (TransformBase ):
24- '''Represents linear transforms on image data'''
23+ """Represents linear transforms on image data."""
24+
2525 __slots__ = ['_matrix' ]
2626
2727 def __init__ (self , matrix = None , reference = None ):
28- '''Initialize a transform
28+ """
29+ Initialize a linear transform.
2930
3031 Parameters
3132 ----------
32-
3333 matrix : ndarray
3434 The inverse coordinate transformation matrix **in physical
3535 coordinates**, mapping coordinates from *reference* space
@@ -38,15 +38,15 @@ def __init__(self, matrix=None, reference=None):
3838
3939 Examples
4040 --------
41-
4241 >>> xfm = Affine([[1, 0, 0, 4], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
4342 >>> xfm.matrix # doctest: +NORMALIZE_WHITESPACE
4443 array([[1, 0, 0, 4],
4544 [0, 1, 0, 0],
4645 [0, 0, 1, 0],
4746 [0, 0, 0, 1]])
4847
49- '''
48+ """
49+ super (Affine , self ).__init__ ()
5050 if matrix is None :
5151 matrix = [np .eye (4 )]
5252
@@ -56,7 +56,6 @@ def __init__(self, matrix=None, reference=None):
5656 self ._matrix = np .array (matrix )
5757 assert self ._matrix .ndim == 3 , 'affine matrix should be 3D'
5858 assert self ._matrix .shape [- 2 ] == self ._matrix .shape [- 1 ], 'affine matrix is not square'
59- super (Affine , self ).__init__ ()
6059
6160 if reference :
6261 if isinstance (reference , str ):
@@ -69,11 +68,11 @@ def matrix(self):
6968
7069 def resample (self , moving , order = 3 , mode = 'constant' , cval = 0.0 , prefilter = True ,
7170 output_dtype = None ):
72- '''Resample the moving image in reference space
71+ """
72+ Resample the moving image in reference space.
7373
7474 Parameters
7575 ----------
76-
7776 moving : `spatialimage`
7877 The image object containing the data to be resampled in reference
7978 space
@@ -96,21 +95,19 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
9695
9796 Returns
9897 -------
99-
10098 moved_image : `spatialimage`
10199 The moving imaged after resampling to reference space.
102100
103101
104102 Examples
105103 --------
106-
107104 >>> import nibabel as nib
108105 >>> xfm = Affine([[1, 0, 0, 4], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
109106 >>> ref = nib.load('image.nii.gz')
110107 >>> xfm.reference = ref
111108 >>> xfm.resample(ref, order=0)
112109
113- '''
110+ """
114111 if output_dtype is None :
115112 output_dtype = moving .header .get_data_dtype ()
116113
@@ -163,13 +160,15 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
163160 return moved_image
164161
165162 def map_point (self , coords , index = 0 , forward = True ):
163+ """Apply y = f(x), where x is the argument `coords`."""
166164 coords = np .array (coords )
167165 if coords .shape [0 ] == self ._matrix [index ].shape [0 ] - 1 :
168166 coords = np .append (coords , [1 ])
169167 affine = self ._matrix [index ] if forward else np .linalg .inv (self ._matrix [index ])
170168 return affine .dot (coords )[:- 1 ]
171169
172170 def map_voxel (self , index , nindex = 0 , moving = None ):
171+ """Apply ijk' = f_ijk((i, j, k)), equivalent to the above with indexes."""
173172 try :
174173 reference = self .reference
175174 except ValueError :
@@ -191,6 +190,7 @@ def map_voxel(self, index, nindex=0, moving=None):
191190 return tuple (matrix .dot (index )[:- 1 ])
192191
193192 def _to_hdf5 (self , x5_root ):
193+ """Serialize this object into the x5 file format."""
194194 xform = x5_root .create_dataset ('Transform' , data = self ._matrix )
195195 xform .attrs ['Type' ] = 'affine'
196196 x5_root .create_dataset ('Inverse' , data = np .linalg .inv (self ._matrix ))
@@ -199,9 +199,7 @@ def _to_hdf5(self, x5_root):
199199 self .reference ._to_hdf5 (x5_root .create_group ('Reference' ))
200200
201201 def to_filename (self , filename , fmt = 'X5' , moving = None ):
202- '''Store the transform in BIDS-Transforms HDF5 file format (.x5).
203- '''
204-
202+ """Store the transform in BIDS-Transforms HDF5 file format (.x5)."""
205203 if fmt .lower () in ['itk' , 'ants' , 'elastix' , 'nifty' ]:
206204 with open (filename , 'w' ) as f :
207205 f .write ('#Insight Transform File V1.0\n ' )
@@ -257,8 +255,7 @@ def to_filename(self, filename, fmt='X5', moving=None):
257255
258256
259257def load (filename , fmt = 'X5' , reference = None ):
260- ''' Load a linear transform '''
261-
258+ """Load a linear transform."""
262259 if fmt .lower () in ['itk' , 'ants' , 'elastix' , 'nifty' ]:
263260 with open (filename ) as itkfile :
264261 itkxfm = itkfile .read ().splitlines ()
@@ -293,7 +290,10 @@ def load(filename, fmt='X5', reference=None):
293290
294291
295292def _fsl_aff_adapt (space ):
296- """Calculates a matrix to convert from the original RAS image
293+ """
294+ Adapt FSL affines.
295+
296+ Calculates a matrix to convert from the original RAS image
297297 coordinates to FSL's internal coordinate system of transforms
298298 """
299299 aff = space .affine
0 commit comments