Skip to content

Commit a529432

Browse files
committed
enh: apply some early comments from @effigies.
1 parent f34affc commit a529432

File tree

5 files changed

+51
-56
lines changed

5 files changed

+51
-56
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,3 @@ Thumbs.db
8484
doc/source/reference
8585
venv/
8686
.buildbot.patch
87-
nibabel/maths/bspline.c

nibabel/transform/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
# copyright and license terms.
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9-
"""Geometric transforms
9+
"""
10+
Geometric transforms.
1011
1112
.. currentmodule:: nibabel.transform
1213
@@ -15,7 +16,6 @@
1516
1617
transform
1718
"""
18-
from __future__ import absolute_import
1919
from .linear import Affine
2020
from .nonlinear import DeformationFieldTransform
2121

nibabel/transform/base.py

+14-20
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
# copyright and license terms.
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9-
''' Common interface for transforms '''
10-
from __future__ import division, print_function, absolute_import
9+
"""Common interface for transforms."""
1110
import numpy as np
1211
import h5py
1312

1413
from scipy import ndimage as ndi
1514

1615

1716
class ImageSpace(object):
18-
'''Class to represent spaces of gridded data (images)'''
17+
"""Class to represent spaces of gridded data (images)."""
18+
1919
__slots__ = ['_affine', '_shape', '_ndim', '_ndindex', '_coords', '_nvox',
2020
'_inverse']
2121

@@ -92,9 +92,8 @@ def __eq__(self, other):
9292

9393

9494
class TransformBase(object):
95-
'''
96-
Abstract image class to represent transforms
97-
'''
95+
"""Abstract image class to represent transforms."""
96+
9897
__slots__ = ['_reference']
9998

10099
def __init__(self):
@@ -117,11 +116,11 @@ def ndim(self):
117116

118117
def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
119118
output_dtype=None):
120-
'''Resample the moving image in reference space
119+
"""
120+
Resample the moving image in reference space.
121121
122122
Parameters
123123
----------
124-
125124
moving : `spatialimage`
126125
The image object containing the data to be resampled in reference
127126
space
@@ -144,16 +143,14 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
144143
145144
Returns
146145
-------
147-
148146
moved_image : `spatialimage`
149147
The moving imaged after resampling to reference space.
150148
151-
'''
152-
149+
"""
150+
moving_data = np.asanyarray(moving.dataobj)
153151
if output_dtype is None:
154-
output_dtype = moving.header.get_data_dtype()
152+
output_dtype = moving_data.dtype
155153

156-
moving_data = moving.get_data()
157154
moved = ndi.geometric_transform(
158155
moving_data,
159156
mapping=self.map_voxel,
@@ -171,22 +168,19 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
171168
return moved_image
172169

173170
def map_point(self, coords):
174-
'''Find the coordinates in moving space corresponding to the
175-
input reference coordinates'''
171+
"""Apply y = f(x), where x is the argument `coords`."""
176172
raise NotImplementedError
177173

178174
def map_voxel(self, index, moving=None):
179-
'''Find the voxel indices in the moving image corresponding to the
180-
input reference voxel'''
175+
"""Apply ijk' = f_ijk((i, j, k)), equivalent to the above with indexes."""
181176
raise NotImplementedError
182177

183178
def _to_hdf5(self, x5_root):
184-
'''Serialize this object into the x5 file format'''
179+
"""Serialize this object into the x5 file format."""
185180
raise NotImplementedError
186181

187182
def to_filename(self, filename, fmt='X5'):
188-
'''Store the transform in BIDS-Transforms HDF5 file format (.x5).
189-
'''
183+
"""Store the transform in BIDS-Transforms HDF5 file format (.x5)."""
190184
with h5py.File(filename, 'w') as out_file:
191185
out_file.attrs['Format'] = 'X5'
192186
out_file.attrs['Version'] = np.uint16(1)

nibabel/transform/linear.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
# copyright and license terms.
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9-
''' Linear transforms '''
10-
from __future__ import division, print_function, absolute_import
9+
"""Linear transforms."""
1110
import sys
1211
import numpy as np
1312
from scipy import ndimage as ndi
@@ -21,15 +20,16 @@
2120

2221

2322
class 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

259257
def 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

295292
def _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

nibabel/transform/nonlinear.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
# copyright and license terms.
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9-
''' Common interface for transforms '''
10-
from __future__ import division, print_function, absolute_import
9+
"""Nonlinear transforms."""
1110
import numpy as np
1211
from scipy import ndimage as ndi
1312
# from gridbspline.maths import cubic
@@ -19,14 +18,13 @@
1918

2019

2120
class DeformationFieldTransform(TransformBase):
22-
'''Represents a dense field of displacements (one vector per voxel)'''
21+
"""Represents a dense field of displacements (one vector per voxel)."""
22+
2323
__slots__ = ['_field', '_moving', '_moving_space']
2424
__s = (slice(None), )
2525

2626
def __init__(self, field, reference=None):
27-
'''
28-
Create a dense deformation field transform
29-
'''
27+
"""Create a dense deformation field transform."""
3028
super(DeformationFieldTransform, self).__init__()
3129
self._field = field.get_data()
3230

@@ -103,11 +101,11 @@ def _cache_moving(self, moving):
103101

104102
def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
105103
output_dtype=None):
106-
'''
104+
"""
105+
Resample the `moving` image applying the deformation field.
107106
108107
Examples
109108
--------
110-
111109
>>> import numpy as np
112110
>>> import nibabel as nb
113111
>>> ref = nb.load('t1_weighted.nii.gz')
@@ -118,16 +116,18 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
118116
>>> new = xfm.resample(ref)
119117
>>> new.to_filename('deffield.nii.gz')
120118
121-
'''
119+
"""
122120
self._cache_moving(moving)
123121
return super(DeformationFieldTransform, self).resample(
124122
moving, order=order, mode=mode, cval=cval, prefilter=prefilter)
125123

126124
def map_voxel(self, index, moving=None):
125+
"""Apply ijk' = f_ijk((i, j, k)), equivalent to the above with indexes."""
127126
return tuple(self._moving[index + self.__s])
128127

129128
def map_coordinates(self, coordinates, order=3, mode='mirror', cval=0.0,
130129
prefilter=True):
130+
"""Apply y = f(x), where x is the argument `coords`."""
131131
coordinates = np.array(coordinates)
132132
# Extract shapes and dimensions, then flatten
133133
ndim = coordinates.shape[-1]
@@ -154,11 +154,13 @@ def map_coordinates(self, coordinates, order=3, mode='mirror', cval=0.0,
154154

155155

156156
class BSplineFieldTransform(TransformBase):
157+
"""Represent a nonlinear transform parameterized by BSpline basis."""
158+
157159
__slots__ = ['_coeffs', '_knots', '_refknots', '_order', '_moving']
158160
__s = (slice(None), )
159161

160162
def __init__(self, reference, coefficients, order=3):
161-
'''Create a smooth deformation field using B-Spline basis'''
163+
"""Create a smooth deformation field using B-Spline basis."""
162164
super(BSplineFieldTransform, self).__init__()
163165
self._order = order
164166
self.reference = reference
@@ -216,16 +218,16 @@ def _interp_transform(self, coords):
216218
return self.reference.inverse.dot(np.hstack((coords, 1)))[:3]
217219

218220
def map_voxel(self, index, moving=None):
219-
'''Find the corresponding coordinates for a voxel in reference space'''
221+
"""Apply ijk' = f_ijk((i, j, k)), equivalent to the above with indexes."""
220222
return tuple(self._moving[index + self.__s])
221223

222224
def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
223225
output_dtype=None):
224-
'''
226+
"""
227+
Resample the `moving` image applying the deformation field.
225228
226229
Examples
227230
--------
228-
229231
>>> import numpy as np
230232
>>> import nibabel as nb
231233
>>> ref = nb.load('t1_weighted.nii.gz')
@@ -238,7 +240,7 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
238240
>>> new = xfm.resample(ref)
239241
>>> new.to_filename('deffield.nii.gz')
240242
241-
'''
243+
"""
242244
self._cache_moving()
243245
return super(BSplineFieldTransform, self).resample(
244246
moving, order=order, mode=mode, cval=cval, prefilter=prefilter)

0 commit comments

Comments
 (0)