From 6323b0bba5fd44f426e06509da3f6a79d90f5bb6 Mon Sep 17 00:00:00 2001 From: kohr-h Date: Thu, 2 Apr 2020 22:04:25 +0200 Subject: [PATCH] Various fixes --- odl/_ufunc/_ufunc_ops.py | 5 ++++- odl/deform/linearized.py | 4 ++-- odl/discr/diff_ops.py | 3 +-- odl/discr/discr_ops.py | 3 +-- odl/discr/partition.py | 18 +++++++++++------- odl/solvers/functional/default_functionals.py | 11 ++++++----- odl/solvers/nonsmooth/douglas_rachford.py | 3 --- odl/space/npy_tensors.py | 5 ++++- odl/space/pspace.py | 6 +----- odl/test/operator/operator_test.py | 5 +---- 10 files changed, 31 insertions(+), 32 deletions(-) diff --git a/odl/_ufunc/_ufunc_ops.py b/odl/_ufunc/_ufunc_ops.py index 1424ec7b36a..50e5a88c9de 100644 --- a/odl/_ufunc/_ufunc_ops.py +++ b/odl/_ufunc/_ufunc_ops.py @@ -1,4 +1,4 @@ -# Copyright 2014-2019 The ODL contributors +# Copyright 2014-2020 The ODL contributors # # This file is part of ODL. # @@ -16,6 +16,7 @@ 'negative', 'degrees', 'rad2deg', 'radians', 'deg2rad', 'add', 'subtract' } + def _ufunc_op_range(domain, nin, nout, types_dict): """Infer the range of a ufunc operator.""" if nin == 1: @@ -551,3 +552,5 @@ def _call(self, x): ufunc_funcs = type( 'ufunc_funcs', (object,), {'__getattr__': staticmethod(ufunc_func_cls)} )() + +# TODO(kohr-h): doctest diff --git a/odl/deform/linearized.py b/odl/deform/linearized.py index f7c05d90463..21cccf46731 100644 --- a/odl/deform/linearized.py +++ b/odl/deform/linearized.py @@ -367,11 +367,11 @@ def __init__(self, domain, displacement, interp='linear'): ) try: - self.__displacement = (self.domain.tangent_bundle).element( + self.__displacement = self.domain.tangent_bundle.element( displacement ) except (ValueError, TypeError): - self.__displacement = (self.domain.tangent_bundle).element( + self.__displacement = self.domain.tangent_bundle.element( [displacement] ) self.__interp_byaxis = _normalize_interp(interp, domain.ndim) diff --git a/odl/discr/diff_ops.py b/odl/discr/diff_ops.py index 6bc6f573efc..d596af237e7 100644 --- a/odl/discr/diff_ops.py +++ b/odl/discr/diff_ops.py @@ -135,7 +135,6 @@ def _call(self, x, out=None): if out is None: out = self.range.element() - # TODO: this pipes CUDA arrays through NumPy. Write native operator. finite_diff(x, axis=self.axis, dx=self.dx, method=self.method, pad_mode=self.pad_mode, pad_const=self.pad_const, out=out) @@ -688,7 +687,7 @@ def __init__(self, domain, range=None, pad_mode='constant', pad_const=0): ''.format(pad_mode_in)) if pad_mode in ('order1', 'order1_adjoint', 'order2', 'order2_adjoint'): - # TODO: Add these pad modes + # TODO: add these pad modes raise ValueError('`pad_mode` {} not implemented for Laplacian.' ''.format(pad_mode_in)) diff --git a/odl/discr/discr_ops.py b/odl/discr/discr_ops.py index 9813e32eecb..e886bb3d2ff 100644 --- a/odl/discr/discr_ops.py +++ b/odl/discr/discr_ops.py @@ -18,8 +18,7 @@ from odl.discr.partition import uniform_partition from odl.operator import Operator from odl.space import tensor_space -from odl.util import ( - normalized_scalar_param_list, resize_array, safe_int_conv) +from odl.util import normalized_scalar_param_list, resize_array, safe_int_conv from odl.util.numerics import _SUPPORTED_RESIZE_PAD_MODES __all__ = ('Resampling', 'ResizingOperator') diff --git a/odl/discr/partition.py b/odl/discr/partition.py index aabacca2b24..4ccec5b544a 100644 --- a/odl/discr/partition.py +++ b/odl/discr/partition.py @@ -1,4 +1,4 @@ -# Copyright 2014-2018 The ODL contributors +# Copyright 2014-2020 The ODL contributors # # This file is part of ODL. # @@ -14,17 +14,18 @@ of partitions of intervals. """ -from __future__ import print_function, division, absolute_import +from __future__ import absolute_import, division, print_function + from builtins import object + import numpy as np from odl.discr.grid import RectGrid, uniform_grid_fromintv from odl.set import IntervalProd from odl.util import ( - normalized_index_expression, normalized_nodes_on_bdry, - normalized_scalar_param_list, safe_int_conv, - signature_string, indent, array_str, npy_printoptions) - + array_str, indent, normalized_index_expression, normalized_nodes_on_bdry, + normalized_scalar_param_list, npy_printoptions, safe_int_conv, + signature_string) __all__ = ('RectPartition', 'uniform_partition_fromintv', 'uniform_partition_fromgrid', 'uniform_partition', @@ -1295,8 +1296,11 @@ def nonuniform_partition(*coord_vecs, **kwargs): Parameters ---------- - coord_vecs1, ... coord_vecsN : `array-like` + coord_vec1, ... coord_vecN : `array-like` Arrays of coordinates of the mid-points of the partition cells. + + Other Parameters + ---------------- min_pt, max_pt : float or sequence of floats, optional Vectors defining the lower/upper limits of the intervals in an `IntervalProd` (a rectangular box). ``None`` entries mean diff --git a/odl/solvers/functional/default_functionals.py b/odl/solvers/functional/default_functionals.py index 1cea6dfeb68..29907dd7c09 100644 --- a/odl/solvers/functional/default_functionals.py +++ b/odl/solvers/functional/default_functionals.py @@ -1104,8 +1104,7 @@ def __init__(self, space, prior=None): Examples -------- - - Test that KullbackLeibler(x,x) = 0 + Test that KullbackLeibler(x, x) = 0 >>> space = odl.rn(3) >>> prior = 3 * space.one() @@ -1113,7 +1112,6 @@ def __init__(self, space, prior=None): >>> func(prior) 0.0 - Test that zeros in the prior are handled correctly >>> prior = space.zero() @@ -1152,7 +1150,10 @@ def _call(self, x): tmp = self.domain.ufuncs.log(x) tmp += 1 tmp -= x - res = -self.domain.inner(tmp, self.domain.one()) + res = self.domain.inner(tmp, self.domain.one()) + if res != 0: + # Avoid -0.0 + res = -res else: # < x - g + xlogy(g, g/x), one > g = self.prior @@ -1164,7 +1165,7 @@ def _call(self, x): tmp = scipy.special.xlogy(g, g / x) tmp -= g tmp += x - res = -self.domain.inner(tmp, self.domain.one()) + res = self.domain.inner(tmp, self.domain.one()) if not np.isfinite(res): # In this case, some element was less than or equal to zero diff --git a/odl/solvers/nonsmooth/douglas_rachford.py b/odl/solvers/nonsmooth/douglas_rachford.py index 416fec149b9..36eb33982c4 100644 --- a/odl/solvers/nonsmooth/douglas_rachford.py +++ b/odl/solvers/nonsmooth/douglas_rachford.py @@ -191,9 +191,6 @@ def douglas_rachford_pd(x, f, g, L, niter, tau=None, sigma=None, else: dom.assign(z1, x) - f.proximal(tau)(z1, out=p1) - # End of z1 as temporary - f.proximal(tau)(z1, out=p1) # Now p1 = prox[tau*f](x - tau/2 * sum(Li^* vi)) # Temporary z1 is no longer needed diff --git a/odl/space/npy_tensors.py b/odl/space/npy_tensors.py index d8c73e08bbc..271e89ad712 100644 --- a/odl/space/npy_tensors.py +++ b/odl/space/npy_tensors.py @@ -12,6 +12,7 @@ from future.utils import native import ctypes +import inspect from builtins import object from functools import partial @@ -20,10 +21,11 @@ from odl.set.sets import ComplexNumbers, RealNumbers from odl.space.base_tensors import TensorSpace from odl.util import ( - dtype_str, getargspec, is_numeric_dtype, is_real_dtype, signature_string) + dtype_str, is_numeric_dtype, is_real_dtype, signature_string) __all__ = ('NumpyTensorSpace',) +getargspec = getattr(inspect, "getfullargspec", inspect.getargspec) _BLAS_DTYPES = (np.dtype('float32'), np.dtype('float64'), np.dtype('complex64'), np.dtype('complex128')) @@ -639,6 +641,7 @@ class NumpyTensorSpaceReduce(object): def __getattr__(self, name): """Return ``self.name``.""" attr = getattr(np, name, None) + attr = getattr(attr, "_implementation", attr) # for numpy >= 1.16 try: spec = getargspec(attr) except (ValueError, TypeError): diff --git a/odl/space/pspace.py b/odl/space/pspace.py index f461fcbd7c4..0d233917175 100644 --- a/odl/space/pspace.py +++ b/odl/space/pspace.py @@ -21,11 +21,7 @@ __all__ = ('ProductSpace',) - -try: - getargspec = inspect.getfullargspec -except AttributeError: - getargspec = inspect.getargspec +getargspec = getattr(inspect, "getfullargspec", inspect.getargspec) class ProductSpace(LinearSpace): diff --git a/odl/test/operator/operator_test.py b/odl/test/operator/operator_test.py index 2ff4d080d91..bc4354ed1e3 100644 --- a/odl/test/operator/operator_test.py +++ b/odl/test/operator/operator_test.py @@ -23,10 +23,7 @@ from odl.util.testutils import ( all_almost_equal, noise_element, noise_elements, simple_fixture) -try: - getargspec = inspect.getfullargspec -except AttributeError: - getargspec = inspect.getargspec +getargspec = getattr(inspect, "getfullargspec", inspect.getargspec) # --- Fixtures --- #