Skip to content

Commit ac9aaa8

Browse files
committed
ENH: make repr precision more flexible
1 parent 6a7d091 commit ac9aaa8

File tree

9 files changed

+55
-35
lines changed

9 files changed

+55
-35
lines changed

odl/discr/diff_ops.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from odl.operator.tensor_ops import PointwiseTensorFieldOperator
1717
from odl.space import ProductSpace
1818
from odl.util import (
19-
REPR_PRECISION, npy_printoptions, repr_string, signature_string_parts,
19+
repr_precision, npy_printoptions, repr_string, signature_string_parts,
2020
writable_array)
2121

2222
__all__ = ('PartialDerivative', 'Gradient', 'Divergence', 'Laplacian')
@@ -193,7 +193,7 @@ def __repr__(self):
193193
('pad_mode', self.pad_mode, 'constant'),
194194
('pad_const', self.pad_const, 0)]
195195
optmod = ['', '!r', '', '', '']
196-
with npy_printoptions(precision=REPR_PRECISION):
196+
with npy_printoptions(precision=repr_precision()):
197197
inner_parts = signature_string_parts(posargs, optargs,
198198
mod=['!r', optmod])
199199
return repr_string(self.__class__.__name__, inner_parts,
@@ -414,7 +414,7 @@ def __repr__(self):
414414
('pad_mode', self.pad_mode, 'constant'),
415415
('pad_const', self.pad_const, 0)]
416416
optmod = ['!r', '', '', '']
417-
with npy_printoptions(precision=REPR_PRECISION):
417+
with npy_printoptions(precision=repr_precision()):
418418
inner_parts = signature_string_parts(posargs, optargs,
419419
mod=['!r', optmod])
420420
return repr_string(self.__class__.__name__, inner_parts,
@@ -624,7 +624,7 @@ def __repr__(self):
624624
('pad_mode', self.pad_mode, 'constant'),
625625
('pad_const', self.pad_const, 0)]
626626
optmod = ['!r', '', '', '']
627-
with npy_printoptions(precision=REPR_PRECISION):
627+
with npy_printoptions(precision=repr_precision()):
628628
inner_parts = signature_string_parts(posargs, optargs,
629629
mod=['!r', optmod])
630630
return repr_string(self.__class__.__name__, inner_parts,
@@ -792,7 +792,7 @@ def __repr__(self):
792792
('pad_mode', self.pad_mode, 'constant'),
793793
('pad_const', self.pad_const, 0)]
794794
optmod = ['!r', '', '']
795-
with npy_printoptions(precision=REPR_PRECISION):
795+
with npy_printoptions(precision=repr_precision()):
796796
inner_parts = signature_string_parts(posargs, optargs,
797797
mod=['!r', optmod])
798798
return repr_string(self.__class__.__name__, inner_parts,

odl/discr/discr_ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from odl.set import IntervalProd
1818
from odl.space import FunctionSpace, tensor_space
1919
from odl.util import (
20-
REPR_PRECISION, attribute_repr_string, normalized_scalar_param_list,
20+
repr_precision, attribute_repr_string, normalized_scalar_param_list,
2121
npy_printoptions, repr_string, resize_array, safe_int_conv,
2222
signature_string_parts, writable_array)
2323
from odl.util.numerics import _SUPPORTED_RESIZE_PAD_MODES
@@ -470,7 +470,7 @@ def __repr__(self):
470470
posargs = [self.domain, self.range]
471471
optargs = [('pad_mode', self.pad_mode, 'constant'),
472472
('pad_const', self.pad_const, 0)]
473-
with npy_printoptions(precision=REPR_PRECISION):
473+
with npy_printoptions(precision=repr_precision()):
474474
inner_parts = signature_string_parts(posargs, optargs,
475475
mod=['!r', ''])
476476
return repr_string(self.__class__.__name__, inner_parts,

odl/discr/grid.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from odl.set import IntervalProd, Set
2020
from odl.util import (
21-
REPR_PRECISION, array_str, normalized_index_expression,
21+
repr_precision, array_str, normalized_index_expression,
2222
normalized_scalar_param_list, npy_printoptions, repr_string, safe_int_conv,
2323
signature_string_parts)
2424

@@ -997,7 +997,7 @@ def __repr__(self):
997997
posargs = self.coord_vectors
998998
posmod = array_str
999999

1000-
with npy_printoptions(precision=REPR_PRECISION):
1000+
with npy_printoptions(precision=repr_precision()):
10011001
inner_parts = signature_string_parts(posargs, [],
10021002
mod=[posmod, ''])
10031003
return repr_string(ctor, inner_parts)

odl/discr/lp_discr.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from odl.space.entry_points import tensor_space_impl
2727
from odl.space.weighting import ConstWeighting
2828
from odl.util import (
29-
REPR_PRECISION, apply_on_boundary, array_str, attribute_repr_string,
29+
repr_precision, apply_on_boundary, array_str, attribute_repr_string,
3030
dtype_str, is_complex_floating_dtype, is_floating_dtype, is_numeric_dtype,
3131
is_real_dtype, is_string, normalized_nodes_on_bdry,
3232
normalized_scalar_param_list, npy_printoptions, repr_string, safe_int_conv,
@@ -602,7 +602,7 @@ def __repr__(self):
602602
if self.dtype in (float, complex, int, bool):
603603
optmod[3] = '!s'
604604

605-
with npy_printoptions(precision=REPR_PRECISION):
605+
with npy_printoptions(precision=repr_precision()):
606606
inner_parts = signature_string_parts(posargs, optargs,
607607
mod=[posmod, optmod])
608608

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

614-
with npy_printoptions(precision=REPR_PRECISION):
614+
with npy_printoptions(precision=repr_precision()):
615615
inner_parts = signature_string_parts(posargs, optargs)
616616

617617
return repr_string(ctor, inner_parts, allow_mixed_seps=True)

odl/discr/partition.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from odl.discr.grid import RectGrid, uniform_grid_fromintv
2424
from odl.set import IntervalProd
2525
from odl.util import (
26-
REPR_PRECISION, array_str, attribute_repr_string,
26+
repr_precision, array_str, attribute_repr_string,
2727
normalized_index_expression, normalized_nodes_on_bdry,
2828
normalized_scalar_param_list, npy_printoptions, repr_string, safe_int_conv,
2929
signature_string_parts)
@@ -898,7 +898,7 @@ def __repr__(self):
898898

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

901-
with npy_printoptions(precision=REPR_PRECISION):
901+
with npy_printoptions(precision=repr_precision()):
902902
inner_parts = signature_string_parts(posargs, optargs,
903903
mod=[posmod, ''])
904904
else:
@@ -930,7 +930,7 @@ def __repr__(self):
930930
optargs.append(('min_pt', self.min_pt[0], None))
931931
optmod.append('')
932932
else:
933-
with npy_printoptions(precision=REPR_PRECISION):
933+
with npy_printoptions(precision=repr_precision()):
934934
optargs.append(
935935
('min_pt', array_str(self.min_pt), ''))
936936
optmod.append('!s')
@@ -940,12 +940,12 @@ def __repr__(self):
940940
optargs.append(('max_pt', self.max_pt[0], None))
941941
optmod.append('')
942942
else:
943-
with npy_printoptions(precision=REPR_PRECISION):
943+
with npy_printoptions(precision=repr_precision()):
944944
optargs.append(
945945
('max_pt', array_str(self.max_pt), ''))
946946
optmod.append('!s')
947947

948-
with npy_printoptions(precision=REPR_PRECISION):
948+
with npy_printoptions(precision=repr_precision()):
949949
inner_parts = signature_string_parts(posargs, optargs,
950950
mod=[posmod, optmod])
951951

odl/operator/default_ops.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from odl.set import Field, LinearSpace, RealNumbers
2121
from odl.space import ProductSpace
2222
from odl.util import (
23-
REPR_PRECISION, npy_printoptions, repr_string, signature_string_parts,
23+
repr_precision, npy_printoptions, repr_string, signature_string_parts,
2424
attribute_repr_string, method_repr_string)
2525

2626
__all__ = ('ScalingOperator', 'ZeroOperator', 'IdentityOperator',
@@ -201,7 +201,7 @@ def __repr__(self):
201201
posargs = [self.domain]
202202
optargs = [('scalar', self.scalar, None),
203203
('range', self.range, self.domain)]
204-
with npy_printoptions(precision=REPR_PRECISION):
204+
with npy_printoptions(precision=repr_precision()):
205205
inner_parts = signature_string_parts(posargs, optargs)
206206
return repr_string(self.__class__.__name__, inner_parts)
207207

@@ -442,7 +442,7 @@ def __repr__(self):
442442
optargs = [('domain', self.domain,
443443
getattr(self.multiplicand, 'space', None)),
444444
('range', self.range, self.domain)]
445-
with npy_printoptions(precision=REPR_PRECISION):
445+
with npy_printoptions(precision=repr_precision()):
446446
inner_parts = signature_string_parts(posargs, optargs)
447447
return repr_string(self.__class__.__name__, inner_parts)
448448

@@ -562,7 +562,7 @@ def __repr__(self):
562562
posargs = [self.domain]
563563
optargs = [('exponent', self.exponent, None),
564564
('range', self.range, self.domain)]
565-
with npy_printoptions(precision=REPR_PRECISION):
565+
with npy_printoptions(precision=repr_precision()):
566566
inner_parts = signature_string_parts(posargs, optargs)
567567
return repr_string(self.__class__.__name__, inner_parts)
568568

@@ -703,7 +703,7 @@ def __repr__(self):
703703
optargs = [('domain', self.domain,
704704
getattr(self.vector, 'space', None)),
705705
('range', self.range, self.domain.field)]
706-
with npy_printoptions(precision=REPR_PRECISION):
706+
with npy_printoptions(precision=repr_precision()):
707707
inner_parts = signature_string_parts(posargs, optargs)
708708
return repr_string(self.__class__.__name__, inner_parts)
709709

@@ -936,7 +936,7 @@ def __repr__(self):
936936
optargs = [('domain', self.domain,
937937
getattr(self.vector, 'space', None)),
938938
('range', self.range, RealNumbers())]
939-
with npy_printoptions(precision=REPR_PRECISION):
939+
with npy_printoptions(precision=repr_precision()):
940940
inner_parts = signature_string_parts(posargs, optargs)
941941
return repr_string(self.__class__.__name__, inner_parts)
942942

@@ -1028,7 +1028,7 @@ def __repr__(self):
10281028
optargs = [('domain', self.domain, self.range),
10291029
('range', self.range,
10301030
getattr(self.constant, 'space', None))]
1031-
with npy_printoptions(precision=REPR_PRECISION):
1031+
with npy_printoptions(precision=repr_precision()):
10321032
inner_parts = signature_string_parts(posargs, optargs)
10331033
return repr_string(self.__class__.__name__, inner_parts)
10341034

@@ -1597,7 +1597,7 @@ def __repr__(self):
15971597
posargs = [self.domain]
15981598
optargs = [('range', self.range, self.domain.complex_space),
15991599
('scalar', self.scalar, 1.0)]
1600-
with npy_printoptions(precision=REPR_PRECISION):
1600+
with npy_printoptions(precision=repr_precision()):
16011601
inner_parts = signature_string_parts(posargs, optargs)
16021602
return repr_string(self.__class__.__name__, inner_parts)
16031603

odl/operator/operator.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from odl.set import Field, LinearSpace, Set
2121
from odl.set.space import LinearSpaceElement
2222
from odl.util import (
23-
REPR_PRECISION, cache_arguments, npy_printoptions, repr_string,
23+
repr_precision, cache_arguments, npy_printoptions, repr_string,
2424
signature_string_parts)
2525

2626
__all__ = ('Operator', 'OperatorComp', 'OperatorSum', 'OperatorVectorSum',
@@ -1311,7 +1311,7 @@ def derivative(self, point):
13111311
def __repr__(self):
13121312
"""Return ``repr(self)``."""
13131313
posargs = [self.operator, self.vector]
1314-
with npy_printoptions(precision=REPR_PRECISION):
1314+
with npy_printoptions(precision=repr_precision()):
13151315
inner_parts = signature_string_parts(posargs, [])
13161316
return repr_string(self.__class__.__name__, inner_parts,
13171317
allow_mixed_seps=False)
@@ -1687,7 +1687,7 @@ def adjoint(self):
16871687
def __repr__(self):
16881688
"""Return ``repr(self)``."""
16891689
posargs = [self.operator, self.scalar]
1690-
with npy_printoptions(precision=REPR_PRECISION):
1690+
with npy_printoptions(precision=repr_precision()):
16911691
inner_parts = signature_string_parts(posargs, [])
16921692
return repr_string(self.__class__.__name__, inner_parts,
16931693
allow_mixed_seps=False)
@@ -1871,7 +1871,7 @@ def adjoint(self):
18711871
def __repr__(self):
18721872
"""Return ``repr(self)``."""
18731873
posargs = [self.operator, self.scalar]
1874-
with npy_printoptions(precision=REPR_PRECISION):
1874+
with npy_printoptions(precision=repr_precision()):
18751875
inner_parts = signature_string_parts(posargs, [])
18761876
return repr_string(self.__class__.__name__, inner_parts,
18771877
allow_mixed_seps=False)
@@ -1990,7 +1990,7 @@ def adjoint(self):
19901990
def __repr__(self):
19911991
"""Return ``repr(self)``."""
19921992
posargs = [self.functional, self.vector]
1993-
with npy_printoptions(precision=REPR_PRECISION):
1993+
with npy_printoptions(precision=repr_precision()):
19941994
inner_parts = signature_string_parts(posargs, [])
19951995
return repr_string(self.__class__.__name__, inner_parts,
19961996
allow_mixed_seps=False)
@@ -2106,7 +2106,7 @@ def adjoint(self):
21062106
def __repr__(self):
21072107
"""Return ``repr(self)``."""
21082108
posargs = [self.operator, self.vector]
2109-
with npy_printoptions(precision=REPR_PRECISION):
2109+
with npy_printoptions(precision=repr_precision()):
21102110
inner_parts = signature_string_parts(posargs, [])
21112111
return repr_string(self.__class__.__name__, inner_parts,
21122112
allow_mixed_seps=False)
@@ -2229,7 +2229,7 @@ def adjoint(self):
22292229
def __repr__(self):
22302230
"""Return ``repr(self)``."""
22312231
posargs = [self.operator, self.vector]
2232-
with npy_printoptions(precision=REPR_PRECISION):
2232+
with npy_printoptions(precision=repr_precision()):
22332233
inner_parts = signature_string_parts(posargs, [])
22342234
return repr_string(self.__class__.__name__, inner_parts,
22352235
allow_mixed_seps=False)

odl/operator/tensor_ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from odl.space.base_tensors import TensorSpace
2121
from odl.space.weighting import ArrayWeighting
2222
from odl.util import (
23-
REPR_PRECISION, array_str, attribute_repr_string, dtype_repr, moveaxis,
23+
repr_precision, array_str, attribute_repr_string, dtype_repr, moveaxis,
2424
npy_printoptions, repr_string, signature_string_parts, writable_array)
2525

2626
__all__ = ('PointwiseNorm', 'PointwiseInner', 'PointwiseSum', 'MatrixOperator',
@@ -385,7 +385,7 @@ def __repr__(self):
385385
if self.is_weighted:
386386
optargs.append(('weighting', array_str(self.weights), ''))
387387
optmod.append('!s')
388-
with npy_printoptions(precision=REPR_PRECISION):
388+
with npy_printoptions(precision=repr_precision()):
389389
inner_parts = signature_string_parts(posargs, optargs,
390390
mod=['!r', optmod])
391391
return repr_string(self.__class__.__name__, inner_parts,

odl/util/utility.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
'real_dtype', 'complex_dtype', 'is_string', 'nd_iterator', 'conj_exponent',
3030
'writable_array', 'run_from_ipython', 'NumpyRandomSeed',
3131
'cache_arguments', 'unique',
32-
'REPR_PRECISION')
32+
'repr_precision')
3333

3434

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

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

14431444

1445+
def repr_precision():
1446+
"""Return the print precision for floating point numbers.
1447+
1448+
By default, the value of the global variable ``REPR_PRECISION``
1449+
is returned, unless the user changed the value of the ``'precision'``
1450+
entry in `numpy.get_printoptions`, e.g., via ::
1451+
1452+
with npy_printoptions(precision=10):
1453+
...
1454+
1455+
"""
1456+
cur_npy_prec = np.get_printoptions()['precision']
1457+
if cur_npy_prec == NPY_DEFAULT_PRECISION:
1458+
# No change by the user
1459+
return REPR_PRECISION
1460+
else:
1461+
return cur_npy_prec
1462+
1463+
14441464
def run_from_ipython():
14451465
"""If the process is run from IPython."""
14461466
return '__IPYTHON__' in globals()

0 commit comments

Comments
 (0)