Skip to content

Commit

Permalink
ENH: make repr precision more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
kohr-h committed Aug 26, 2018
1 parent 6a7d091 commit ac9aaa8
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 35 deletions.
10 changes: 5 additions & 5 deletions odl/discr/diff_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from odl.operator.tensor_ops import PointwiseTensorFieldOperator
from odl.space import ProductSpace
from odl.util import (
REPR_PRECISION, npy_printoptions, repr_string, signature_string_parts,
repr_precision, npy_printoptions, repr_string, signature_string_parts,
writable_array)

__all__ = ('PartialDerivative', 'Gradient', 'Divergence', 'Laplacian')
Expand Down Expand Up @@ -193,7 +193,7 @@ def __repr__(self):
('pad_mode', self.pad_mode, 'constant'),
('pad_const', self.pad_const, 0)]
optmod = ['', '!r', '', '', '']
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs,
mod=['!r', optmod])
return repr_string(self.__class__.__name__, inner_parts,
Expand Down Expand Up @@ -414,7 +414,7 @@ def __repr__(self):
('pad_mode', self.pad_mode, 'constant'),
('pad_const', self.pad_const, 0)]
optmod = ['!r', '', '', '']
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs,
mod=['!r', optmod])
return repr_string(self.__class__.__name__, inner_parts,
Expand Down Expand Up @@ -624,7 +624,7 @@ def __repr__(self):
('pad_mode', self.pad_mode, 'constant'),
('pad_const', self.pad_const, 0)]
optmod = ['!r', '', '', '']
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs,
mod=['!r', optmod])
return repr_string(self.__class__.__name__, inner_parts,
Expand Down Expand Up @@ -792,7 +792,7 @@ def __repr__(self):
('pad_mode', self.pad_mode, 'constant'),
('pad_const', self.pad_const, 0)]
optmod = ['!r', '', '']
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs,
mod=['!r', optmod])
return repr_string(self.__class__.__name__, inner_parts,
Expand Down
4 changes: 2 additions & 2 deletions odl/discr/discr_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from odl.set import IntervalProd
from odl.space import FunctionSpace, tensor_space
from odl.util import (
REPR_PRECISION, attribute_repr_string, normalized_scalar_param_list,
repr_precision, attribute_repr_string, normalized_scalar_param_list,
npy_printoptions, repr_string, resize_array, safe_int_conv,
signature_string_parts, writable_array)
from odl.util.numerics import _SUPPORTED_RESIZE_PAD_MODES
Expand Down Expand Up @@ -470,7 +470,7 @@ def __repr__(self):
posargs = [self.domain, self.range]
optargs = [('pad_mode', self.pad_mode, 'constant'),
('pad_const', self.pad_const, 0)]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs,
mod=['!r', ''])
return repr_string(self.__class__.__name__, inner_parts,
Expand Down
4 changes: 2 additions & 2 deletions odl/discr/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from odl.set import IntervalProd, Set
from odl.util import (
REPR_PRECISION, array_str, normalized_index_expression,
repr_precision, array_str, normalized_index_expression,
normalized_scalar_param_list, npy_printoptions, repr_string, safe_int_conv,
signature_string_parts)

Expand Down Expand Up @@ -997,7 +997,7 @@ def __repr__(self):
posargs = self.coord_vectors
posmod = array_str

with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, [],
mod=[posmod, ''])
return repr_string(ctor, inner_parts)
Expand Down
6 changes: 3 additions & 3 deletions odl/discr/lp_discr.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from odl.space.entry_points import tensor_space_impl
from odl.space.weighting import ConstWeighting
from odl.util import (
REPR_PRECISION, apply_on_boundary, array_str, attribute_repr_string,
repr_precision, apply_on_boundary, array_str, attribute_repr_string,
dtype_str, is_complex_floating_dtype, is_floating_dtype, is_numeric_dtype,
is_real_dtype, is_string, normalized_nodes_on_bdry,
normalized_scalar_param_list, npy_printoptions, repr_string, safe_int_conv,
Expand Down Expand Up @@ -602,7 +602,7 @@ def __repr__(self):
if self.dtype in (float, complex, int, bool):
optmod[3] = '!s'

with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs,
mod=[posmod, optmod])

Expand All @@ -611,7 +611,7 @@ def __repr__(self):
posargs = [self.fspace, self.partition, self.tspace]
optargs = [('interp', self.interp, 'nearest')]

with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs)

return repr_string(ctor, inner_parts, allow_mixed_seps=True)
Expand Down
10 changes: 5 additions & 5 deletions odl/discr/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from odl.discr.grid import RectGrid, uniform_grid_fromintv
from odl.set import IntervalProd
from odl.util import (
REPR_PRECISION, array_str, attribute_repr_string,
repr_precision, array_str, attribute_repr_string,
normalized_index_expression, normalized_nodes_on_bdry,
normalized_scalar_param_list, npy_printoptions, repr_string, safe_int_conv,
signature_string_parts)
Expand Down Expand Up @@ -898,7 +898,7 @@ def __repr__(self):

optargs = [('nodes_on_bdry', self.nodes_on_bdry, False)]

with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs,
mod=[posmod, ''])
else:
Expand Down Expand Up @@ -930,7 +930,7 @@ def __repr__(self):
optargs.append(('min_pt', self.min_pt[0], None))
optmod.append('')
else:
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
optargs.append(
('min_pt', array_str(self.min_pt), ''))
optmod.append('!s')
Expand All @@ -940,12 +940,12 @@ def __repr__(self):
optargs.append(('max_pt', self.max_pt[0], None))
optmod.append('')
else:
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
optargs.append(
('max_pt', array_str(self.max_pt), ''))
optmod.append('!s')

with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs,
mod=[posmod, optmod])

Expand Down
16 changes: 8 additions & 8 deletions odl/operator/default_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from odl.set import Field, LinearSpace, RealNumbers
from odl.space import ProductSpace
from odl.util import (
REPR_PRECISION, npy_printoptions, repr_string, signature_string_parts,
repr_precision, npy_printoptions, repr_string, signature_string_parts,
attribute_repr_string, method_repr_string)

__all__ = ('ScalingOperator', 'ZeroOperator', 'IdentityOperator',
Expand Down Expand Up @@ -201,7 +201,7 @@ def __repr__(self):
posargs = [self.domain]
optargs = [('scalar', self.scalar, None),
('range', self.range, self.domain)]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs)
return repr_string(self.__class__.__name__, inner_parts)

Expand Down Expand Up @@ -442,7 +442,7 @@ def __repr__(self):
optargs = [('domain', self.domain,
getattr(self.multiplicand, 'space', None)),
('range', self.range, self.domain)]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs)
return repr_string(self.__class__.__name__, inner_parts)

Expand Down Expand Up @@ -562,7 +562,7 @@ def __repr__(self):
posargs = [self.domain]
optargs = [('exponent', self.exponent, None),
('range', self.range, self.domain)]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs)
return repr_string(self.__class__.__name__, inner_parts)

Expand Down Expand Up @@ -703,7 +703,7 @@ def __repr__(self):
optargs = [('domain', self.domain,
getattr(self.vector, 'space', None)),
('range', self.range, self.domain.field)]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs)
return repr_string(self.__class__.__name__, inner_parts)

Expand Down Expand Up @@ -936,7 +936,7 @@ def __repr__(self):
optargs = [('domain', self.domain,
getattr(self.vector, 'space', None)),
('range', self.range, RealNumbers())]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs)
return repr_string(self.__class__.__name__, inner_parts)

Expand Down Expand Up @@ -1028,7 +1028,7 @@ def __repr__(self):
optargs = [('domain', self.domain, self.range),
('range', self.range,
getattr(self.constant, 'space', None))]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs)
return repr_string(self.__class__.__name__, inner_parts)

Expand Down Expand Up @@ -1597,7 +1597,7 @@ def __repr__(self):
posargs = [self.domain]
optargs = [('range', self.range, self.domain.complex_space),
('scalar', self.scalar, 1.0)]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs)
return repr_string(self.__class__.__name__, inner_parts)

Expand Down
14 changes: 7 additions & 7 deletions odl/operator/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from odl.set import Field, LinearSpace, Set
from odl.set.space import LinearSpaceElement
from odl.util import (
REPR_PRECISION, cache_arguments, npy_printoptions, repr_string,
repr_precision, cache_arguments, npy_printoptions, repr_string,
signature_string_parts)

__all__ = ('Operator', 'OperatorComp', 'OperatorSum', 'OperatorVectorSum',
Expand Down Expand Up @@ -1311,7 +1311,7 @@ def derivative(self, point):
def __repr__(self):
"""Return ``repr(self)``."""
posargs = [self.operator, self.vector]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, [])
return repr_string(self.__class__.__name__, inner_parts,
allow_mixed_seps=False)
Expand Down Expand Up @@ -1687,7 +1687,7 @@ def adjoint(self):
def __repr__(self):
"""Return ``repr(self)``."""
posargs = [self.operator, self.scalar]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, [])
return repr_string(self.__class__.__name__, inner_parts,
allow_mixed_seps=False)
Expand Down Expand Up @@ -1871,7 +1871,7 @@ def adjoint(self):
def __repr__(self):
"""Return ``repr(self)``."""
posargs = [self.operator, self.scalar]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, [])
return repr_string(self.__class__.__name__, inner_parts,
allow_mixed_seps=False)
Expand Down Expand Up @@ -1990,7 +1990,7 @@ def adjoint(self):
def __repr__(self):
"""Return ``repr(self)``."""
posargs = [self.functional, self.vector]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, [])
return repr_string(self.__class__.__name__, inner_parts,
allow_mixed_seps=False)
Expand Down Expand Up @@ -2106,7 +2106,7 @@ def adjoint(self):
def __repr__(self):
"""Return ``repr(self)``."""
posargs = [self.operator, self.vector]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, [])
return repr_string(self.__class__.__name__, inner_parts,
allow_mixed_seps=False)
Expand Down Expand Up @@ -2229,7 +2229,7 @@ def adjoint(self):
def __repr__(self):
"""Return ``repr(self)``."""
posargs = [self.operator, self.vector]
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, [])
return repr_string(self.__class__.__name__, inner_parts,
allow_mixed_seps=False)
Expand Down
4 changes: 2 additions & 2 deletions odl/operator/tensor_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from odl.space.base_tensors import TensorSpace
from odl.space.weighting import ArrayWeighting
from odl.util import (
REPR_PRECISION, array_str, attribute_repr_string, dtype_repr, moveaxis,
repr_precision, array_str, attribute_repr_string, dtype_repr, moveaxis,
npy_printoptions, repr_string, signature_string_parts, writable_array)

__all__ = ('PointwiseNorm', 'PointwiseInner', 'PointwiseSum', 'MatrixOperator',
Expand Down Expand Up @@ -385,7 +385,7 @@ def __repr__(self):
if self.is_weighted:
optargs.append(('weighting', array_str(self.weights), ''))
optmod.append('!s')
with npy_printoptions(precision=REPR_PRECISION):
with npy_printoptions(precision=repr_precision()):
inner_parts = signature_string_parts(posargs, optargs,
mod=['!r', optmod])
return repr_string(self.__class__.__name__, inner_parts,
Expand Down
22 changes: 21 additions & 1 deletion odl/util/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
'real_dtype', 'complex_dtype', 'is_string', 'nd_iterator', 'conj_exponent',
'writable_array', 'run_from_ipython', 'NumpyRandomSeed',
'cache_arguments', 'unique',
'REPR_PRECISION')
'repr_precision')


REPR_PRECISION = 4 # For printing scalars and array entries
NPY_DEFAULT_PRECISION = np.get_printoptions()['precision']
TYPE_MAP_R2C = {np.dtype(dtype): np.result_type(dtype, 1j)
for dtype in np.sctypes['float']}

Expand Down Expand Up @@ -1441,6 +1442,25 @@ class that is created through a method, for instance ::
return '.'.join(init_parts) + meth_call_str


def repr_precision():
"""Return the print precision for floating point numbers.
By default, the value of the global variable ``REPR_PRECISION``
is returned, unless the user changed the value of the ``'precision'``
entry in `numpy.get_printoptions`, e.g., via ::
with npy_printoptions(precision=10):
...
"""
cur_npy_prec = np.get_printoptions()['precision']
if cur_npy_prec == NPY_DEFAULT_PRECISION:
# No change by the user
return REPR_PRECISION
else:
return cur_npy_prec


def run_from_ipython():
"""If the process is run from IPython."""
return '__IPYTHON__' in globals()
Expand Down

0 comments on commit ac9aaa8

Please sign in to comment.