Skip to content

Commit

Permalink
MAINT: Add odl and np to default namespace for doctests, closes #410
Browse files Browse the repository at this point in the history
  • Loading branch information
adler-j committed Aug 23, 2016
1 parent a21f7a9 commit 92d6099
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 154 deletions.
47 changes: 45 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,31 @@

import pytest
import odl
import numpy as np
from odl.trafos.wavelet import PYWAVELETS_AVAILABLE


# --- Add numpy and ODL to all doctests ---


@pytest.fixture(autouse=True)
def add_np(doctest_namespace):
doctest_namespace['np'] = np
doctest_namespace['odl'] = odl


# --- Files to be ignored by the tests ---


collect_ignore = ['setup.py', 'run_tests.py']

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


# --- Options flags for the tests ---


def pytest_addoption(parser):
parser.addoption('--largescale', action='store_true',
help='Run large and slow tests')
Expand All @@ -42,21 +59,25 @@ def pytest_addoption(parser):
help='Run examples')


# reusable fixtures
# --- Reusable fixtures ---

fn_impl_params = odl.FN_IMPLS.keys()
fn_impl_ids = [" impl='{}' ".format(p) for p in fn_impl_params]


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

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


@pytest.fixture(scope="module", ids=ntuples_impl_ids, params=ntuples_impl_params)
@pytest.fixture(scope="module", ids=ntuples_impl_ids,
params=ntuples_impl_params)
def ntuples_impl(request):
"""String with a available `NtuplesBase` implementation name."""
return request.param

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

@pytest.fixture(scope="module", ids=ufunc_ids, params=ufunc_params)
def ufunc(request):
"""Tuple with information on a ufunc.
Returns
-------
name : `str`
Name of the ufunc.
n_in : `int`
Number of input values of the ufunc.
n_out : `int`
Number of output values of the ufunc.
doc : `str`
Docstring for the ufunc.
"""
return request.param


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

@pytest.fixture(scope="module", ids=reduction_ids, params=reduction_params)
def reduction(request):
"""Tuple with information on a reduction.
Returns
-------
name : `str`
Name of the reduction.
doc : `str`
Docstring for the reduction.
"""
return request.param
1 change: 1 addition & 0 deletions doc/source/dev/document.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Some short tips
```numpy.ndarray```).
* Make sure that the first line is short and descriptive.
* Examples are often better than long descriptions.
* Numpy and ODL are both imported by default in doctests, so there is no need for ``import numpy as np`` or ``import odl``.

Quick summary of `PEP257`_
--------------------------
Expand Down
3 changes: 0 additions & 3 deletions odl/deform/linearized.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def _linear_deform(template, displacement, out=None):
In the 4-th point, the value is taken from 0.2 (one cell) to the
left, i.e. 1.0.
>>> import odl
>>> space = odl.uniform_discr(0, 1, 5)
>>> disp_field_space = space.vector_field_space
>>> template = space.element([0, 0, 1, 0, 0])
Expand Down Expand Up @@ -153,7 +152,6 @@ def __init__(self, template):
In the 4-th point, the value is taken from 0.2 (one cell) to the
left, i.e. 1.0.
>>> import odl
>>> space = odl.uniform_discr(0, 1, 5, interp='nearest')
>>> template = space.element([0, 0, 1, 0, 0])
>>> op = LinDeformFixedTempl(template)
Expand Down Expand Up @@ -282,7 +280,6 @@ def __init__(self, displacement, templ_space=None):
In the 4-th point, the value is taken from 0.2 (one cell) to the
left, i.e. 1.0.
>>> import odl
>>> space = odl.uniform_discr(0, 1, 5)
>>> disp_field = space.vector_field_space.element([[0, 0, 0, -0.2, 0]])
>>> op = LinDeformFixedDisp(disp_field)
Expand Down
1 change: 0 additions & 1 deletion odl/diagnostics/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def samples(*sets):
Examples
--------
>>> import odl
>>> R, C = odl.RealNumbers(), odl.ComplexNumbers()
>>> for [name_x, x], [name_y, y] in samples(R, C): pass # use examples
"""
Expand Down
12 changes: 3 additions & 9 deletions odl/discr/diff_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def _call(self, x, out=None):
Examples
--------
>>> import odl
>>> data = np.array([[ 0., 1., 2., 3., 4.],
... [ 0., 2., 4., 6., 8.]])
>>> discr = odl.uniform_discr([0, 0], [2, 1], data.shape)
Expand Down Expand Up @@ -219,7 +218,6 @@ def __init__(self, domain=None, range=None, method='forward',
Examples
--------
>>> import odl
>>> dom = odl.uniform_discr([0, 0], [1, 1], (10, 20))
>>> ran = odl.ProductSpace(dom, dom.ndim) # 2-dimensional
>>> grad_op = Gradient(dom)
Expand Down Expand Up @@ -273,10 +271,9 @@ def _call(self, x, out=None):
Examples
--------
>>> from odl import uniform_discr
>>> data = np.array([[ 0., 1., 2., 3., 4.],
... [ 0., 2., 4., 6., 8.]])
>>> discr = uniform_discr([0, 0], [2, 5], data.shape)
>>> discr = odl.uniform_discr([0, 0], [2, 5], data.shape)
>>> f = discr.element(data)
>>> grad = Gradient(discr)
>>> grad_f = grad(f)
Expand Down Expand Up @@ -400,7 +397,6 @@ def __init__(self, domain=None, range=None, method='forward',
Examples
--------
>>> import odl
>>> ran = odl.uniform_discr([0, 0], [1, 1], (10, 20))
>>> dom = odl.ProductSpace(ran, ran.ndim) # 2-dimensional
>>> div_op = Divergence(dom)
Expand Down Expand Up @@ -455,11 +451,10 @@ def _call(self, x, out=None):
Examples
--------
>>> from odl import uniform_discr
>>> data = np.array([[0., 1., 2., 3., 4.],
... [1., 2., 3., 4., 5.],
... [2., 3., 4., 5., 6.]])
>>> space = uniform_discr([0, 0], [3, 5], data.shape)
>>> space = odl.uniform_discr([0, 0], [3, 5], data.shape)
>>> div = Divergence(range=space)
>>> f = div.domain.element([data, data])
>>> div_f = div(f)
Expand Down Expand Up @@ -591,11 +586,10 @@ def _call(self, x, out=None):
Examples
--------
>>> from odl import uniform_discr
>>> data = np.array([[ 0., 0., 0.],
... [ 0., 1., 0.],
... [ 0., 0., 0.]])
>>> space = uniform_discr([0, 0], [3, 3], data.shape)
>>> space = odl.uniform_discr([0, 0], [3, 3], data.shape)
>>> f = space.element(data)
>>> lap = Laplacian(space)
>>> print(lap(f))
Expand Down
21 changes: 8 additions & 13 deletions odl/discr/discr_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,11 @@ def _call(self, func, out=None, **kwargs):
Define a set of functions from the rectangle [1, 3] x [2, 5]
to the real numbers:
>>> from odl import FunctionSpace, Rectangle
>>> rect = Rectangle([1, 3], [2, 5])
>>> funcset = FunctionSpace(rect)
>>> rect = odl.Rectangle([1, 3], [2, 5])
>>> funcset = odl.FunctionSpace(rect)
Partition the rectangle by a tensor grid:
>>> import odl
>>> rect = odl.Rectangle([1, 3], [2, 5])
>>> grid = odl.TensorGrid([1, 2], [3, 4, 5])
>>> partition = odl.RectPartition(rect, grid)
Expand Down Expand Up @@ -398,22 +396,19 @@ def _call(self, x, out=None):
We test nearest neighbor interpolation with a non-scalar
data type in 2d:
>>> import numpy as np
>>> from odl import Rectangle, Strings, FunctionSet
>>> rect = Rectangle([0, 0], [1, 1])
>>> strings = Strings(1) # 1-char strings
>>> space = FunctionSet(rect, strings)
>>> rect = odl.Rectangle([0, 0], [1, 1])
>>> strings = odl.Strings(1) # 1-char strings
>>> space = odl.FunctionSet(rect, strings)
Partitioning the domain uniformly with no nodes on the boundary
(will shift the grid points):
>>> from odl import uniform_partition_fromintv, ntuples
>>> part = uniform_partition_fromintv(rect, [4, 2],
... nodes_on_bdry=False)
>>> part = odl.uniform_partition_fromintv(rect, [4, 2],
... nodes_on_bdry=False)
>>> part.grid.coord_vectors
(array([ 0.125, 0.375, 0.625, 0.875]), array([ 0.25, 0.75]))
>>> dspace = ntuples(part.size, dtype='U1')
>>> dspace = odl.ntuples(part.size, dtype='U1')
Now we initialize the operator and test it with some points:
Expand Down
3 changes: 0 additions & 3 deletions odl/discr/discr_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def __init__(self, domain, range):
Create two spaces with different number of points and a resampling
operator.
>>> import odl
>>> X = odl.uniform_discr(0, 1, 3)
>>> Y = odl.uniform_discr(0, 1, 6)
>>> resampling = Resampling(X, Y)
Expand All @@ -79,7 +78,6 @@ def _call(self, x, out=None):
Create two spaces with different number of points and create resampling
operator. Apply operator to vector.
>>> import odl
>>> X = odl.uniform_discr(0, 1, 3)
>>> Y = odl.uniform_discr(0, 1, 6)
>>> resampling = Resampling(X, Y)
Expand Down Expand Up @@ -130,7 +128,6 @@ def adjoint(self):
--------
Create resampling operator and inverse
>>> import odl
>>> X = odl.uniform_discr(0, 1, 3)
>>> Y = odl.uniform_discr(0, 1, 6)
>>> resampling = Resampling(X, Y)
Expand Down
6 changes: 0 additions & 6 deletions odl/discr/discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,6 @@ def sampling(self, ufunc, **kwargs):
Examples
--------
>>> import odl
>>> import numpy as np
Create discretization
>>> X = odl.uniform_discr(0, 1, 5)
Expand Down Expand Up @@ -377,9 +374,6 @@ def interpolation(self):
Examples
--------
>>> import odl
>>> import numpy as np
Create continuous version of a discrete 1d function with nearest
neighbour interpolation:
Expand Down
9 changes: 3 additions & 6 deletions odl/discr/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ def min(self, **kwargs):
Also works with numpy
>>> import numpy
>>> numpy.min(g)
>>> np.min(g)
array([ 1., -2.])
"""
out = kwargs.get('out', None)
Expand Down Expand Up @@ -319,8 +318,7 @@ def max(self, **kwargs):
Also works with numpy
>>> import numpy
>>> numpy.max(g)
>>> np.max(g)
array([ 5., 2.])
"""
out = kwargs.get('out', None)
Expand Down Expand Up @@ -1188,8 +1186,7 @@ def uniform_sampling_fromintv(intv_prod, num_nodes, nodes_on_bdry=True):
Examples
--------
>>> from odl import IntervalProd
>>> rbox = IntervalProd([-1.5, 2], [-0.5, 3])
>>> rbox = odl.IntervalProd([-1.5, 2], [-0.5, 3])
>>> grid = uniform_sampling_fromintv(rbox, (3, 3))
>>> grid.coord_vectors
(array([-1.5, -1. , -0.5]), array([ 2. , 2.5, 3. ]))
Expand Down
12 changes: 4 additions & 8 deletions odl/discr/lp_discr.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ def element(self, inp=None, **kwargs):
Elements can be created from array-like objects that represent
an already discretized function:
>>> import odl
>>> space = odl.uniform_discr(-1, 1, 4)
>>> space.element([1, 2, 3, 4])
uniform_discr(-1.0, 1.0, 4).element([1.0, 2.0, 3.0, 4.0])
Expand Down Expand Up @@ -876,8 +875,7 @@ def uniform_discr_frompartition(partition, exponent=2.0, interp='nearest',
Examples
--------
>>> from odl import uniform_partition
>>> part = uniform_partition(0, 1, 10)
>>> part = odl.uniform_partition(0, 1, 10)
>>> uniform_discr_frompartition(part)
uniform_discr(0.0, 1.0, 10)
Expand Down Expand Up @@ -982,9 +980,8 @@ def uniform_discr_fromspace(fspace, nsamples, exponent=2.0, interp='nearest',
Examples
--------
>>> from odl import Interval, FunctionSpace
>>> intv = Interval(0, 1)
>>> space = FunctionSpace(intv)
>>> intv = odl.Interval(0, 1)
>>> space = odl.FunctionSpace(intv)
>>> uniform_discr_fromspace(space, 10)
uniform_discr(0.0, 1.0, 10)
Expand Down Expand Up @@ -1099,8 +1096,7 @@ def uniform_discr_fromintv(interval, nsamples, exponent=2.0, interp='nearest',
Examples
--------
>>> from odl import Interval
>>> intv = Interval(0, 1)
>>> intv = odl.Interval(0, 1)
>>> uniform_discr_fromintv(intv, 10)
uniform_discr(0.0, 1.0, 10)
Expand Down
2 changes: 0 additions & 2 deletions odl/discr/tensor_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def __init__(self, vfspace, exponent=None, weight=None):
standard point-wise norm operator on that space. The operator
maps a vector field to a scalar function:
>>> import odl
>>> spc = odl.uniform_discr([-1, -1], [1, 1], (1, 2))
>>> vfspace = odl.ProductSpace(spc, 2)
>>> pw_norm = PointwiseNorm(vfspace)
Expand Down Expand Up @@ -509,7 +508,6 @@ def __init__(self, vfspace, vecfield, weight=None):
point-wise inner product operator with a fixed vector field.
The operator maps a vector field to a scalar function:
>>> import odl
>>> spc = odl.uniform_discr([-1, -1], [1, 1], (1, 2))
>>> vfspace = odl.ProductSpace(spc, 2)
>>> fixed_vf = np.array([[[0, 1]],
Expand Down

0 comments on commit 92d6099

Please sign in to comment.