Skip to content

Commit 92d6099

Browse files
committed
MAINT: Add odl and np to default namespace for doctests, closes #410
1 parent a21f7a9 commit 92d6099

23 files changed

+91
-154
lines changed

conftest.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,31 @@
2323

2424
import pytest
2525
import odl
26+
import numpy as np
2627
from odl.trafos.wavelet import PYWAVELETS_AVAILABLE
2728

29+
30+
# --- Add numpy and ODL to all doctests ---
31+
32+
33+
@pytest.fixture(autouse=True)
34+
def add_np(doctest_namespace):
35+
doctest_namespace['np'] = np
36+
doctest_namespace['odl'] = odl
37+
38+
39+
# --- Files to be ignored by the tests ---
40+
41+
2842
collect_ignore = ['setup.py', 'run_tests.py']
2943

3044
if not PYWAVELETS_AVAILABLE:
3145
collect_ignore.append('odl/trafos/wavelet.py')
3246

3347

48+
# --- Options flags for the tests ---
49+
50+
3451
def pytest_addoption(parser):
3552
parser.addoption('--largescale', action='store_true',
3653
help='Run large and slow tests')
@@ -42,21 +59,25 @@ def pytest_addoption(parser):
4259
help='Run examples')
4360

4461

45-
# reusable fixtures
62+
# --- Reusable fixtures ---
63+
4664
fn_impl_params = odl.FN_IMPLS.keys()
4765
fn_impl_ids = [" impl='{}' ".format(p) for p in fn_impl_params]
4866

4967

5068
@pytest.fixture(scope="module", ids=fn_impl_ids, params=fn_impl_params)
5169
def fn_impl(request):
70+
"""String with a available `FnBase` implementation name."""
5271
return request.param
5372

5473
ntuples_impl_params = odl.NTUPLES_IMPLS.keys()
5574
ntuples_impl_ids = [" impl='{}' ".format(p) for p in ntuples_impl_params]
5675

5776

58-
@pytest.fixture(scope="module", ids=ntuples_impl_ids, params=ntuples_impl_params)
77+
@pytest.fixture(scope="module", ids=ntuples_impl_ids,
78+
params=ntuples_impl_params)
5979
def ntuples_impl(request):
80+
"""String with a available `NtuplesBase` implementation name."""
6081
return request.param
6182

6283
ufunc_params = [ufunc for ufunc in odl.util.ufuncs.UFUNCS]
@@ -65,6 +86,19 @@ def ntuples_impl(request):
6586

6687
@pytest.fixture(scope="module", ids=ufunc_ids, params=ufunc_params)
6788
def ufunc(request):
89+
"""Tuple with information on a ufunc.
90+
91+
Returns
92+
-------
93+
name : `str`
94+
Name of the ufunc.
95+
n_in : `int`
96+
Number of input values of the ufunc.
97+
n_out : `int`
98+
Number of output values of the ufunc.
99+
doc : `str`
100+
Docstring for the ufunc.
101+
"""
68102
return request.param
69103

70104

@@ -74,4 +108,13 @@ def ufunc(request):
74108

75109
@pytest.fixture(scope="module", ids=reduction_ids, params=reduction_params)
76110
def reduction(request):
111+
"""Tuple with information on a reduction.
112+
113+
Returns
114+
-------
115+
name : `str`
116+
Name of the reduction.
117+
doc : `str`
118+
Docstring for the reduction.
119+
"""
77120
return request.param

doc/source/dev/document.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Some short tips
8686
```numpy.ndarray```).
8787
* Make sure that the first line is short and descriptive.
8888
* Examples are often better than long descriptions.
89+
* Numpy and ODL are both imported by default in doctests, so there is no need for ``import numpy as np`` or ``import odl``.
8990

9091
Quick summary of `PEP257`_
9192
--------------------------

odl/deform/linearized.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def _linear_deform(template, displacement, out=None):
6666
In the 4-th point, the value is taken from 0.2 (one cell) to the
6767
left, i.e. 1.0.
6868
69-
>>> import odl
7069
>>> space = odl.uniform_discr(0, 1, 5)
7170
>>> disp_field_space = space.vector_field_space
7271
>>> template = space.element([0, 0, 1, 0, 0])
@@ -153,7 +152,6 @@ def __init__(self, template):
153152
In the 4-th point, the value is taken from 0.2 (one cell) to the
154153
left, i.e. 1.0.
155154
156-
>>> import odl
157155
>>> space = odl.uniform_discr(0, 1, 5, interp='nearest')
158156
>>> template = space.element([0, 0, 1, 0, 0])
159157
>>> op = LinDeformFixedTempl(template)
@@ -282,7 +280,6 @@ def __init__(self, displacement, templ_space=None):
282280
In the 4-th point, the value is taken from 0.2 (one cell) to the
283281
left, i.e. 1.0.
284282
285-
>>> import odl
286283
>>> space = odl.uniform_discr(0, 1, 5)
287284
>>> disp_field = space.vector_field_space.element([[0, 0, 0, -0.2, 0]])
288285
>>> op = LinDeformFixedDisp(disp_field)

odl/diagnostics/examples.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ def samples(*sets):
4141
4242
Examples
4343
--------
44-
>>> import odl
4544
>>> R, C = odl.RealNumbers(), odl.ComplexNumbers()
4645
>>> for [name_x, x], [name_y, y] in samples(R, C): pass # use examples
4746
"""

odl/discr/diff_ops.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ def _call(self, x, out=None):
118118
119119
Examples
120120
--------
121-
>>> import odl
122121
>>> data = np.array([[ 0., 1., 2., 3., 4.],
123122
... [ 0., 2., 4., 6., 8.]])
124123
>>> discr = odl.uniform_discr([0, 0], [2, 1], data.shape)
@@ -219,7 +218,6 @@ def __init__(self, domain=None, range=None, method='forward',
219218
220219
Examples
221220
--------
222-
>>> import odl
223221
>>> dom = odl.uniform_discr([0, 0], [1, 1], (10, 20))
224222
>>> ran = odl.ProductSpace(dom, dom.ndim) # 2-dimensional
225223
>>> grad_op = Gradient(dom)
@@ -273,10 +271,9 @@ def _call(self, x, out=None):
273271
274272
Examples
275273
--------
276-
>>> from odl import uniform_discr
277274
>>> data = np.array([[ 0., 1., 2., 3., 4.],
278275
... [ 0., 2., 4., 6., 8.]])
279-
>>> discr = uniform_discr([0, 0], [2, 5], data.shape)
276+
>>> discr = odl.uniform_discr([0, 0], [2, 5], data.shape)
280277
>>> f = discr.element(data)
281278
>>> grad = Gradient(discr)
282279
>>> grad_f = grad(f)
@@ -400,7 +397,6 @@ def __init__(self, domain=None, range=None, method='forward',
400397
401398
Examples
402399
--------
403-
>>> import odl
404400
>>> ran = odl.uniform_discr([0, 0], [1, 1], (10, 20))
405401
>>> dom = odl.ProductSpace(ran, ran.ndim) # 2-dimensional
406402
>>> div_op = Divergence(dom)
@@ -455,11 +451,10 @@ def _call(self, x, out=None):
455451
456452
Examples
457453
--------
458-
>>> from odl import uniform_discr
459454
>>> data = np.array([[0., 1., 2., 3., 4.],
460455
... [1., 2., 3., 4., 5.],
461456
... [2., 3., 4., 5., 6.]])
462-
>>> space = uniform_discr([0, 0], [3, 5], data.shape)
457+
>>> space = odl.uniform_discr([0, 0], [3, 5], data.shape)
463458
>>> div = Divergence(range=space)
464459
>>> f = div.domain.element([data, data])
465460
>>> div_f = div(f)
@@ -591,11 +586,10 @@ def _call(self, x, out=None):
591586
592587
Examples
593588
--------
594-
>>> from odl import uniform_discr
595589
>>> data = np.array([[ 0., 0., 0.],
596590
... [ 0., 1., 0.],
597591
... [ 0., 0., 0.]])
598-
>>> space = uniform_discr([0, 0], [3, 3], data.shape)
592+
>>> space = odl.uniform_discr([0, 0], [3, 3], data.shape)
599593
>>> f = space.element(data)
600594
>>> lap = Laplacian(space)
601595
>>> print(lap(f))

odl/discr/discr_mappings.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,11 @@ def _call(self, func, out=None, **kwargs):
236236
Define a set of functions from the rectangle [1, 3] x [2, 5]
237237
to the real numbers:
238238
239-
>>> from odl import FunctionSpace, Rectangle
240-
>>> rect = Rectangle([1, 3], [2, 5])
241-
>>> funcset = FunctionSpace(rect)
239+
>>> rect = odl.Rectangle([1, 3], [2, 5])
240+
>>> funcset = odl.FunctionSpace(rect)
242241
243242
Partition the rectangle by a tensor grid:
244243
245-
>>> import odl
246244
>>> rect = odl.Rectangle([1, 3], [2, 5])
247245
>>> grid = odl.TensorGrid([1, 2], [3, 4, 5])
248246
>>> partition = odl.RectPartition(rect, grid)
@@ -398,22 +396,19 @@ def _call(self, x, out=None):
398396
We test nearest neighbor interpolation with a non-scalar
399397
data type in 2d:
400398
401-
>>> import numpy as np
402-
>>> from odl import Rectangle, Strings, FunctionSet
403-
>>> rect = Rectangle([0, 0], [1, 1])
404-
>>> strings = Strings(1) # 1-char strings
405-
>>> space = FunctionSet(rect, strings)
399+
>>> rect = odl.Rectangle([0, 0], [1, 1])
400+
>>> strings = odl.Strings(1) # 1-char strings
401+
>>> space = odl.FunctionSet(rect, strings)
406402
407403
Partitioning the domain uniformly with no nodes on the boundary
408404
(will shift the grid points):
409405
410-
>>> from odl import uniform_partition_fromintv, ntuples
411-
>>> part = uniform_partition_fromintv(rect, [4, 2],
412-
... nodes_on_bdry=False)
406+
>>> part = odl.uniform_partition_fromintv(rect, [4, 2],
407+
... nodes_on_bdry=False)
413408
>>> part.grid.coord_vectors
414409
(array([ 0.125, 0.375, 0.625, 0.875]), array([ 0.25, 0.75]))
415410
416-
>>> dspace = ntuples(part.size, dtype='U1')
411+
>>> dspace = odl.ntuples(part.size, dtype='U1')
417412
418413
Now we initialize the operator and test it with some points:
419414

odl/discr/discr_ops.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def __init__(self, domain, range):
5656
Create two spaces with different number of points and a resampling
5757
operator.
5858
59-
>>> import odl
6059
>>> X = odl.uniform_discr(0, 1, 3)
6160
>>> Y = odl.uniform_discr(0, 1, 6)
6261
>>> resampling = Resampling(X, Y)
@@ -79,7 +78,6 @@ def _call(self, x, out=None):
7978
Create two spaces with different number of points and create resampling
8079
operator. Apply operator to vector.
8180
82-
>>> import odl
8381
>>> X = odl.uniform_discr(0, 1, 3)
8482
>>> Y = odl.uniform_discr(0, 1, 6)
8583
>>> resampling = Resampling(X, Y)
@@ -130,7 +128,6 @@ def adjoint(self):
130128
--------
131129
Create resampling operator and inverse
132130
133-
>>> import odl
134131
>>> X = odl.uniform_discr(0, 1, 3)
135132
>>> Y = odl.uniform_discr(0, 1, 6)
136133
>>> resampling = Resampling(X, Y)

odl/discr/discretization.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,6 @@ def sampling(self, ufunc, **kwargs):
345345
346346
Examples
347347
--------
348-
>>> import odl
349-
>>> import numpy as np
350-
351348
Create discretization
352349
353350
>>> X = odl.uniform_discr(0, 1, 5)
@@ -377,9 +374,6 @@ def interpolation(self):
377374
378375
Examples
379376
--------
380-
>>> import odl
381-
>>> import numpy as np
382-
383377
Create continuous version of a discrete 1d function with nearest
384378
neighbour interpolation:
385379

odl/discr/grid.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ def min(self, **kwargs):
287287
288288
Also works with numpy
289289
290-
>>> import numpy
291-
>>> numpy.min(g)
290+
>>> np.min(g)
292291
array([ 1., -2.])
293292
"""
294293
out = kwargs.get('out', None)
@@ -319,8 +318,7 @@ def max(self, **kwargs):
319318
320319
Also works with numpy
321320
322-
>>> import numpy
323-
>>> numpy.max(g)
321+
>>> np.max(g)
324322
array([ 5., 2.])
325323
"""
326324
out = kwargs.get('out', None)
@@ -1188,8 +1186,7 @@ def uniform_sampling_fromintv(intv_prod, num_nodes, nodes_on_bdry=True):
11881186
11891187
Examples
11901188
--------
1191-
>>> from odl import IntervalProd
1192-
>>> rbox = IntervalProd([-1.5, 2], [-0.5, 3])
1189+
>>> rbox = odl.IntervalProd([-1.5, 2], [-0.5, 3])
11931190
>>> grid = uniform_sampling_fromintv(rbox, (3, 3))
11941191
>>> grid.coord_vectors
11951192
(array([-1.5, -1. , -0.5]), array([ 2. , 2.5, 3. ]))

odl/discr/lp_discr.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ def element(self, inp=None, **kwargs):
274274
Elements can be created from array-like objects that represent
275275
an already discretized function:
276276
277-
>>> import odl
278277
>>> space = odl.uniform_discr(-1, 1, 4)
279278
>>> space.element([1, 2, 3, 4])
280279
uniform_discr(-1.0, 1.0, 4).element([1.0, 2.0, 3.0, 4.0])
@@ -876,8 +875,7 @@ def uniform_discr_frompartition(partition, exponent=2.0, interp='nearest',
876875
877876
Examples
878877
--------
879-
>>> from odl import uniform_partition
880-
>>> part = uniform_partition(0, 1, 10)
878+
>>> part = odl.uniform_partition(0, 1, 10)
881879
>>> uniform_discr_frompartition(part)
882880
uniform_discr(0.0, 1.0, 10)
883881
@@ -982,9 +980,8 @@ def uniform_discr_fromspace(fspace, nsamples, exponent=2.0, interp='nearest',
982980
983981
Examples
984982
--------
985-
>>> from odl import Interval, FunctionSpace
986-
>>> intv = Interval(0, 1)
987-
>>> space = FunctionSpace(intv)
983+
>>> intv = odl.Interval(0, 1)
984+
>>> space = odl.FunctionSpace(intv)
988985
>>> uniform_discr_fromspace(space, 10)
989986
uniform_discr(0.0, 1.0, 10)
990987
@@ -1099,8 +1096,7 @@ def uniform_discr_fromintv(interval, nsamples, exponent=2.0, interp='nearest',
10991096
11001097
Examples
11011098
--------
1102-
>>> from odl import Interval
1103-
>>> intv = Interval(0, 1)
1099+
>>> intv = odl.Interval(0, 1)
11041100
>>> uniform_discr_fromintv(intv, 10)
11051101
uniform_discr(0.0, 1.0, 10)
11061102

0 commit comments

Comments
 (0)