1
1
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2
2
# vi: set ft=python sts=4 ts=4 sw=4 et:
3
- """ Utility routines for working with points and affine transforms
3
+ """Utility routines for working with points and affine transforms
4
4
"""
5
5
import numpy as np
6
6
7
7
from functools import reduce
8
8
9
9
10
10
class AffineError (ValueError ):
11
- """ Errors in calculating or using affines """
11
+ """Errors in calculating or using affines"""
12
+
12
13
# Inherits from ValueError to keep compatibility with ValueError previously
13
14
# raised in append_diag
14
15
pass
15
16
16
17
17
18
def apply_affine (aff , pts , inplace = False ):
18
- """ Apply affine matrix `aff` to points `pts`
19
+ """Apply affine matrix `aff` to points `pts`
19
20
20
21
Returns result of application of `aff` to the *right* of `pts`. The
21
22
coordinate dimension of `pts` should be the last.
@@ -142,7 +143,7 @@ def to_matvec(transform):
142
143
143
144
144
145
def from_matvec (matrix , vector = None ):
145
- """ Combine a matrix and vector into an homogeneous affine
146
+ """Combine a matrix and vector into an homogeneous affine
146
147
147
148
Combine a rotation / scaling / shearing matrix and translation vector into
148
149
a transform in homogeneous coordinates.
@@ -185,14 +186,14 @@ def from_matvec(matrix, vector=None):
185
186
nin , nout = matrix .shape
186
187
t = np .zeros ((nin + 1 , nout + 1 ), matrix .dtype )
187
188
t [0 :nin , 0 :nout ] = matrix
188
- t [nin , nout ] = 1.
189
+ t [nin , nout ] = 1.0
189
190
if vector is not None :
190
191
t [0 :nin , nout ] = vector
191
192
return t
192
193
193
194
194
195
def append_diag (aff , steps , starts = ()):
195
- """ Add diagonal elements `steps` and translations `starts` to affine
196
+ """Add diagonal elements `steps` and translations `starts` to affine
196
197
197
198
Typical use is in expanding 4x4 affines to larger dimensions. Nipy is the
198
199
main consumer because it uses NxM affines, whereas we generally only use
@@ -236,8 +237,7 @@ def append_diag(aff, steps, starts=()):
236
237
raise AffineError ('Steps should have same length as starts' )
237
238
old_n_out , old_n_in = aff .shape [0 ] - 1 , aff .shape [1 ] - 1
238
239
# make new affine
239
- aff_plus = np .zeros ((old_n_out + n_steps + 1 ,
240
- old_n_in + n_steps + 1 ), dtype = aff .dtype )
240
+ aff_plus = np .zeros ((old_n_out + n_steps + 1 , old_n_in + n_steps + 1 ), dtype = aff .dtype )
241
241
# Get stuff from old affine
242
242
aff_plus [:old_n_out , :old_n_in ] = aff [:old_n_out , :old_n_in ]
243
243
aff_plus [:old_n_out , - 1 ] = aff [:old_n_out , - 1 ]
@@ -250,7 +250,7 @@ def append_diag(aff, steps, starts=()):
250
250
251
251
252
252
def dot_reduce (* args ):
253
- r""" Apply numpy dot product function from right to left on arrays
253
+ r"""Apply numpy dot product function from right to left on arrays
254
254
255
255
For passed arrays :math:`A, B, C, ... Z` returns :math:`A \dot B \dot C ...
256
256
\dot Z` where "." is the numpy array dot product.
@@ -270,7 +270,7 @@ def dot_reduce(*args):
270
270
271
271
272
272
def voxel_sizes (affine ):
273
- r""" Return voxel size for each input axis given `affine`
273
+ r"""Return voxel size for each input axis given `affine`
274
274
275
275
The `affine` is the mapping between array (voxel) coordinates and mm
276
276
(world) coordinates.
@@ -308,7 +308,7 @@ def voxel_sizes(affine):
308
308
but in general has length (N-1) where input `affine` is shape (M, N).
309
309
"""
310
310
top_left = affine [:- 1 , :- 1 ]
311
- return np .sqrt (np .sum (top_left ** 2 , axis = 0 ))
311
+ return np .sqrt (np .sum (top_left ** 2 , axis = 0 ))
312
312
313
313
314
314
def obliquity (affine ):
@@ -340,7 +340,7 @@ def obliquity(affine):
340
340
341
341
342
342
def rescale_affine (affine , shape , zooms , new_shape = None ):
343
- """ Return a new affine matrix with updated voxel sizes (zooms)
343
+ """Return a new affine matrix with updated voxel sizes (zooms)
344
344
345
345
This function preserves the rotations and shears of the original
346
346
affine, as well as the RAS location of the central voxel of the
0 commit comments