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 67f5b17
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 46 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,
npy_printoptions, repr_precision, 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
6 changes: 3 additions & 3 deletions odl/discr/discr_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
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,
npy_printoptions, repr_string, resize_array, safe_int_conv,
attribute_repr_string, normalized_scalar_param_list, npy_printoptions,
repr_precision, 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
6 changes: 3 additions & 3 deletions odl/discr/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

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

__all__ = ('RectGrid', 'uniform_grid', 'uniform_grid_fromintv')
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
12 changes: 6 additions & 6 deletions odl/discr/lp_discr.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
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,
dtype_str, is_complex_floating_dtype, is_floating_dtype, is_numeric_dtype,
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,
signature_string_parts)
normalized_scalar_param_list, npy_printoptions, repr_precision,
repr_string, safe_int_conv, signature_string_parts)

__all__ = ('DiscreteLp', 'DiscreteLpElement',
'uniform_discr_frompartition', 'uniform_discr_fromspace',
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
15 changes: 7 additions & 8 deletions odl/discr/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
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,
normalized_index_expression, normalized_nodes_on_bdry,
normalized_scalar_param_list, npy_printoptions, repr_string, safe_int_conv,
signature_string_parts)
array_str, attribute_repr_string, normalized_index_expression,
normalized_nodes_on_bdry, normalized_scalar_param_list, npy_printoptions,
repr_precision, repr_string, safe_int_conv, signature_string_parts)

__all__ = ('RectPartition', 'uniform_partition_fromintv',
'uniform_partition_fromgrid', 'uniform_partition',
Expand Down Expand Up @@ -898,7 +897,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 +929,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 +939,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
18 changes: 9 additions & 9 deletions odl/operator/default_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
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,
attribute_repr_string, method_repr_string)
attribute_repr_string, method_repr_string, npy_printoptions,
repr_precision, repr_string, signature_string_parts)

__all__ = ('ScalingOperator', 'ZeroOperator', 'IdentityOperator',
'LinCombOperator', 'MultiplyOperator', 'PowerOperator',
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,
cache_arguments, npy_printoptions, repr_precision, 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
6 changes: 3 additions & 3 deletions odl/operator/tensor_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
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,
npy_printoptions, repr_string, signature_string_parts, writable_array)
array_str, attribute_repr_string, dtype_repr, moveaxis, npy_printoptions,
repr_precision, repr_string, signature_string_parts, writable_array)

__all__ = ('PointwiseNorm', 'PointwiseInner', 'PointwiseSum', 'MatrixOperator',
'SamplingOperator', 'WeightedSumSamplingOperator',
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
24 changes: 22 additions & 2 deletions odl/util/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
"""Utilities mainly for internal use."""

from __future__ import absolute_import, division, print_function
from future.moves.itertools import zip_longest

import inspect
import sys
from builtins import object
from collections import OrderedDict
from functools import wraps
from future.moves.itertools import zip_longest
from itertools import product

import numpy as np
Expand All @@ -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 67f5b17

Please sign in to comment.