Skip to content

Commit b48fb17

Browse files
authored
Merge pull request #918 from jond01/rf-docstrings
STY: Use triple double-quoted string in docstrings (PEP-257)
2 parents 966a01e + 29ad025 commit b48fb17

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+715
-715
lines changed

bin/nib-nifti-dx

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# copyright and license terms.
88
#
99
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
10-
''' Print nifti diagnostics for header files '''
10+
""" Print nifti diagnostics for header files """
1111

1212
from nibabel.cmdline.nifti_dx import main
1313

doc/source/dicom/derivations/dicom_mosaic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
''' Just showing the mosaic simplification '''
1+
""" Just showing the mosaic simplification """
22

33
from sympy import Matrix, Symbol, symbols, simplify
44

@@ -19,12 +19,12 @@ def numbered_vector(nrows, symbol_prefix):
1919
'md_{cols} md_{rows} rd_{cols} rd_{rows}')
2020

2121
md_adj = Matrix((mdc - 1, mdr - 1, 0)) / -2
22-
rd_adj = Matrix((rdc - 1 , rdr - 1, 0)) / -2
22+
rd_adj = Matrix((rdc - 1, rdr - 1, 0)) / -2
2323

2424
adj = -(RS * md_adj) + RS * rd_adj
2525
adj.simplify()
2626

27-
Q = RS[:,:2] * Matrix((
27+
Q = RS[:, :2] * Matrix((
2828
(mdc - rdc) / 2,
2929
(mdr - rdr) / 2))
3030

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
''' Symbolic versions of the DICOM orientation mathemeatics.
1+
""" Symbolic versions of the DICOM orientation mathemeatics.
22
33
Notes on the SPM orientation machinery.
44
55
There are symbolic versions of the code in ``spm_dicom_convert``,
66
``write_volume`` subfunction, around line 509 in the version I have (SPM8, late
77
2009 vintage).
8-
'''
8+
"""
99

1010
import numpy as np
1111

@@ -16,21 +16,21 @@
1616
# The code below is general (independent of SPMs code)
1717
def numbered_matrix(nrows, ncols, symbol_prefix):
1818
return Matrix(nrows, ncols, lambda i, j: Symbol(
19-
symbol_prefix + '_{%d%d}' % (i+1, j+1)))
19+
symbol_prefix + '_{%d%d}' % (i + 1, j + 1)))
2020

2121

2222
def numbered_vector(nrows, symbol_prefix):
2323
return Matrix(nrows, 1, lambda i, j: Symbol(
24-
symbol_prefix + '_{%d}' % (i+1)))
24+
symbol_prefix + '_{%d}' % (i + 1)))
2525

2626

2727
# premultiplication matrix to go from 0 based to 1 based indexing
2828
one_based = eye(4)
29-
one_based[:3,3] = (1,1,1)
29+
one_based[:3, 3] = (1, 1, 1)
3030
# premult for swapping row and column indices
3131
row_col_swap = eye(4)
32-
row_col_swap[:,0] = eye(4)[:,1]
33-
row_col_swap[:,1] = eye(4)[:,0]
32+
row_col_swap[:, 0] = eye(4)[:, 1]
33+
row_col_swap[:, 1] = eye(4)[:, 0]
3434

3535
# various worming matrices
3636
orient_pat = numbered_matrix(3, 2, 'F')
@@ -40,47 +40,49 @@ def numbered_vector(nrows, symbol_prefix):
4040
pos_pat_N = numbered_vector(3, 'T^N')
4141
pixel_spacing = symbols((r'\Delta{r}', r'\Delta{c}'))
4242
NZ = Symbol('N')
43-
slice_spacing = Symbol('\Delta{s}')
43+
slice_spacing = Symbol(r'\Delta{s}')
4444

4545
R3 = orient_pat * np.diag(pixel_spacing)
4646
R = zeros(4, 2)
47-
R[:3,:] = R3
47+
R[:3, :] = R3
4848

4949
# The following is specific to the SPM algorithm.
5050
x1 = ones(4, 1)
5151
y1 = ones(4, 1)
52-
y1[:3,:] = pos_pat_0
52+
y1[:3, :] = pos_pat_0
5353

5454
to_inv = zeros(4, 4)
55-
to_inv[:,0] = x1
56-
to_inv[:,1] = symbols('a b c d')
57-
to_inv[0,2] = 1
58-
to_inv[1,3] = 1
55+
to_inv[:, 0] = x1
56+
to_inv[:, 1] = symbols('a b c d')
57+
to_inv[0, 2] = 1
58+
to_inv[1, 3] = 1
5959
inv_lhs = zeros(4, 4)
60-
inv_lhs[:,0] = y1
61-
inv_lhs[:,1] = symbols('e f g h')
62-
inv_lhs[:,2:] = R
60+
inv_lhs[:, 0] = y1
61+
inv_lhs[:, 1] = symbols('e f g h')
62+
inv_lhs[:, 2:] = R
63+
6364

6465
def spm_full_matrix(x2, y2):
65-
rhs = to_inv[:,:]
66-
rhs[:,1] = x2
67-
lhs = inv_lhs[:,:]
68-
lhs[:,1] = y2
66+
rhs = to_inv[:, :]
67+
rhs[:, 1] = x2
68+
lhs = inv_lhs[:, :]
69+
lhs[:, 1] = y2
6970
return lhs * rhs.inv()
7071

72+
7173
# single slice case
7274
orient = zeros(3, 3)
73-
orient[:3,:2] = orient_pat
74-
orient[:,2] = orient_cross
75-
x2_ss = Matrix((0,0,1,0))
75+
orient[:3, :2] = orient_pat
76+
orient[:, 2] = orient_cross
77+
x2_ss = Matrix((0, 0, 1, 0))
7678
y2_ss = zeros(4, 1)
77-
y2_ss[:3,:] = orient * Matrix((0,0,slice_spacing))
79+
y2_ss[:3, :] = orient * Matrix((0, 0, slice_spacing))
7880
A_ss = spm_full_matrix(x2_ss, y2_ss)
7981

8082
# many slice case
81-
x2_ms = Matrix((1,1,NZ,1))
83+
x2_ms = Matrix((1, 1, NZ, 1))
8284
y2_ms = ones(4, 1)
83-
y2_ms[:3,:] = pos_pat_N
85+
y2_ms[:3, :] = pos_pat_N
8486
A_ms = spm_full_matrix(x2_ms, y2_ms)
8587

8688
# End of SPM algorithm
@@ -92,22 +94,22 @@ def spm_full_matrix(x2, y2):
9294
single_aff = eye(4)
9395
rot = orient
9496
rot_scale = rot * np.diag(pixel_spacing[:] + (slice_spacing,))
95-
single_aff[:3,:3] = rot_scale
96-
single_aff[:3,3] = pos_pat_0
97+
single_aff[:3, :3] = rot_scale
98+
single_aff[:3, 3] = pos_pat_0
9799

98100
# For multi-slice case, we have the start and the end slice position
99101
# patient. This gives us the third column of the affine, because,
100102
# ``pat_pos_N = aff * [[0,0,ZN-1,1]].T
101103
multi_aff = eye(4)
102-
multi_aff[:3,:2] = R3
103-
trans_z_N = Matrix((0,0, NZ-1, 1))
104+
multi_aff[:3, :2] = R3
105+
trans_z_N = Matrix((0, 0, NZ - 1, 1))
104106
multi_aff[:3, 2] = missing_r_col
105107
multi_aff[:3, 3] = pos_pat_0
106108
est_pos_pat_N = multi_aff * trans_z_N
107-
eqns = tuple(est_pos_pat_N[:3,0] - pos_pat_N)
108-
solved = sympy.solve(eqns, tuple(missing_r_col))
109-
multi_aff_solved = multi_aff[:,:]
110-
multi_aff_solved[:3,2] = multi_aff_solved[:3,2].subs(solved)
109+
eqns = tuple(est_pos_pat_N[:3, 0] - pos_pat_N)
110+
solved = sympy.solve(eqns, tuple(missing_r_col))
111+
multi_aff_solved = multi_aff[:, :]
112+
multi_aff_solved[:3, 2] = multi_aff_solved[:3, 2].subs(solved)
111113

112114
# Check that SPM gave us the same result
113115
A_ms_0based = A_ms * one_based
@@ -121,10 +123,10 @@ def spm_full_matrix(x2, y2):
121123
A_i = single_aff
122124
nz_trans = eye(4)
123125
NZT = Symbol('d')
124-
nz_trans[2,3] = NZT
126+
nz_trans[2, 3] = NZT
125127
A_j = A_i * nz_trans
126-
IPP_i = A_i[:3,3]
127-
IPP_j = A_j[:3,3]
128+
IPP_i = A_i[:3, 3]
129+
IPP_j = A_j[:3, 3]
128130

129131
# SPM does it with the inner product of the vectors
130132
spm_z = IPP_j.T * orient_cross
@@ -135,11 +137,13 @@ def spm_full_matrix(x2, y2):
135137
ipp_sum_div = sum(IPP_j) / sum(orient_cross)
136138
ipp_sum_div = sympy.simplify(ipp_sum_div)
137139

140+
138141
# Dump out the formulae here to latex for the RST docs
139142
def my_latex(expr):
140143
S = sympy.latex(expr)
141144
return S[1:-1]
142145

146+
143147
print('Latex stuff')
144148
print(' R = ' + my_latex(to_inv))
145149
print(' ')
@@ -159,4 +163,4 @@ def my_latex(expr):
159163
print()
160164
print(' T^j = ' + my_latex(IPP_j))
161165
print()
162-
print(' T^j \cdot \mathbf{c} = ' + my_latex(spm_z))
166+
print(r' T^j \cdot \mathbf{c} = ' + my_latex(spm_z))

doc/tools/apigen.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030

3131
class ApiDocWriter(object):
32-
''' Class for automatic detection and parsing of API docs
33-
to Sphinx-parsable reST format'''
32+
""" Class for automatic detection and parsing of API docs
33+
to Sphinx-parsable reST format"""
3434

3535
# only separating first two levels
3636
rst_section_levels = ['*', '=', '-', '~', '^']
@@ -42,7 +42,7 @@ def __init__(self,
4242
module_skip_patterns=None,
4343
other_defines=True
4444
):
45-
''' Initialize package for parsing
45+
""" Initialize package for parsing
4646
4747
Parameters
4848
----------
@@ -70,7 +70,7 @@ def __init__(self,
7070
other_defines : {True, False}, optional
7171
Whether to include classes and functions that are imported in a
7272
particular module but not defined there.
73-
'''
73+
"""
7474
if package_skip_patterns is None:
7575
package_skip_patterns = ['\\.tests$']
7676
if module_skip_patterns is None:
@@ -85,7 +85,7 @@ def get_package_name(self):
8585
return self._package_name
8686

8787
def set_package_name(self, package_name):
88-
''' Set package_name
88+
""" Set package_name
8989
9090
>>> docwriter = ApiDocWriter('sphinx')
9191
>>> import sphinx
@@ -95,7 +95,7 @@ def set_package_name(self, package_name):
9595
>>> import docutils
9696
>>> docwriter.root_path == docutils.__path__[0]
9797
True
98-
'''
98+
"""
9999
# It's also possible to imagine caching the module parsing here
100100
self._package_name = package_name
101101
root_module = self._import(package_name)
@@ -106,30 +106,30 @@ def set_package_name(self, package_name):
106106
'get/set package_name')
107107

108108
def _import(self, name):
109-
''' Import namespace package '''
109+
""" Import namespace package """
110110
mod = __import__(name)
111111
components = name.split('.')
112112
for comp in components[1:]:
113113
mod = getattr(mod, comp)
114114
return mod
115115

116116
def _get_object_name(self, line):
117-
''' Get second token in line
117+
""" Get second token in line
118118
>>> docwriter = ApiDocWriter('sphinx')
119119
>>> docwriter._get_object_name(" def func(): ")
120120
'func'
121121
>>> docwriter._get_object_name(" class Klass(object): ")
122122
'Klass'
123123
>>> docwriter._get_object_name(" class Klass: ")
124124
'Klass'
125-
'''
125+
"""
126126
name = line.split()[1].split('(')[0].strip()
127127
# in case we have classes which are not derived from object
128128
# ie. old style classes
129129
return name.rstrip(':')
130130

131131
def _uri2path(self, uri):
132-
''' Convert uri to absolute filepath
132+
""" Convert uri to absolute filepath
133133
134134
Parameters
135135
----------
@@ -155,7 +155,7 @@ def _uri2path(self, uri):
155155
True
156156
>>> docwriter._uri2path('sphinx.does_not_exist')
157157
158-
'''
158+
"""
159159
if uri == self.package_name:
160160
return os.path.join(self.root_path, '__init__.py')
161161
path = uri.replace(self.package_name + '.', '')
@@ -171,15 +171,15 @@ def _uri2path(self, uri):
171171
return path
172172

173173
def _path2uri(self, dirpath):
174-
''' Convert directory path to uri '''
174+
""" Convert directory path to uri """
175175
package_dir = self.package_name.replace('.', os.path.sep)
176176
relpath = dirpath.replace(self.root_path, package_dir)
177177
if relpath.startswith(os.path.sep):
178178
relpath = relpath[1:]
179179
return relpath.replace(os.path.sep, '.')
180180

181181
def _parse_module(self, uri):
182-
''' Parse module defined in *uri* '''
182+
""" Parse module defined in *uri* """
183183
filename = self._uri2path(uri)
184184
if filename is None:
185185
print(filename, 'erk')
@@ -233,7 +233,7 @@ def _parse_module_with_import(self, uri):
233233
return functions, classes
234234

235235
def _parse_lines(self, linesource):
236-
''' Parse lines of text for functions and classes '''
236+
""" Parse lines of text for functions and classes """
237237
functions = []
238238
classes = []
239239
for line in linesource:
@@ -254,7 +254,7 @@ def _parse_lines(self, linesource):
254254
return functions, classes
255255

256256
def generate_api_doc(self, uri):
257-
'''Make autodoc documentation template string for a module
257+
"""Make autodoc documentation template string for a module
258258
259259
Parameters
260260
----------
@@ -267,7 +267,7 @@ def generate_api_doc(self, uri):
267267
Module name, table of contents.
268268
body : string
269269
Function and class docstrings.
270-
'''
270+
"""
271271
# get the names of all classes and functions
272272
functions, classes = self._parse_module_with_import(uri)
273273
if not len(functions) and not len(classes) and DEBUG:
@@ -317,7 +317,7 @@ def generate_api_doc(self, uri):
317317
return head, body
318318

319319
def _survives_exclude(self, matchstr, match_type):
320-
''' Returns True if *matchstr* does not match patterns
320+
""" Returns True if *matchstr* does not match patterns
321321
322322
``self.package_name`` removed from front of string if present
323323
@@ -336,7 +336,7 @@ def _survives_exclude(self, matchstr, match_type):
336336
>>> dw.module_skip_patterns.append('^\\.badmod$')
337337
>>> dw._survives_exclude('sphinx.badmod', 'module')
338338
False
339-
'''
339+
"""
340340
if match_type == 'module':
341341
patterns = self.module_skip_patterns
342342
elif match_type == 'package':
@@ -359,7 +359,7 @@ def _survives_exclude(self, matchstr, match_type):
359359
return True
360360

361361
def discover_modules(self):
362-
''' Return module sequence discovered from ``self.package_name``
362+
""" Return module sequence discovered from ``self.package_name``
363363
364364
365365
Parameters
@@ -381,7 +381,7 @@ def discover_modules(self):
381381
>>> 'sphinx.util' in dw.discover_modules()
382382
False
383383
>>>
384-
'''
384+
"""
385385
modules = [self.package_name]
386386
# raw directory parsing
387387
for dirpath, dirnames, filenames in os.walk(self.root_path):

0 commit comments

Comments
 (0)