Skip to content

Commit

Permalink
Feature/remove unicode pm (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
jagerber48 committed Nov 7, 2023
2 parents f1b130e + 1babd83 commit e1c0ef0
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 212 deletions.
16 changes: 13 additions & 3 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ Unreleased
Fixed
^^^^^

* Fixed a bug where the `repr` for `FormatOptions` would return a string
containing information about the global format options rather than
about the specific `FormatOptions` instance.
* Fixed a bug where the ``repr`` for ``FormatOptions`` would return a
string containing information about the global format options rather
than about the specific ``FormatOptions`` instance.
[`#75 <https://github.com/jagerber48/sciform/issues/75>`_]

Removed
^^^^^^^

* Removed the ``unicode_pm`` feature which allowed toggling between
using ``'+/-'`` or ``'±'`` in value/uncertainty strings. Previously
``unicode_pm`` defaulted to ``False`` so that ``'+/-'`` was the
default behavior. Now the default behavior is to use ``'±'`` and there
is no way to change to the old ``'+/-'`` behavior.
[`#10 <https://github.com/jagerber48/sciform/discussions/10>`_]

----

0.29.1 (2023-10-22)
Expand Down
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ using the ``SciNumUnc`` object.
... upper_separator=GroupingSeparator.SPACE,
... lower_separator=GroupingSeparator.SPACE))
>>> print(sform(123456.654321, 0.0034))
123 456.654 3 +/- 0.003 4
123 456.654 3 ± 0.003 4
>>> sform = Formatter(FormatOptions(
... ndigits=4,
... exp_mode=ExpMode.ENGINEERING))
>>> print(sform(123456.654321, 0.0034))
(123.456654321 +/- 0.000003400)e+03
(123.456654321 ± 0.000003400)e+03

>>> from sciform import SciNumUnc
>>> num = SciNumUnc(123456.654321, 0.0034)
>>> print(f'{num:_!2f}')
123_456.6543 +/- 0.0034
123_456.6543 ± 0.0034
>>> print(f'{num:_!2f()}')
123_456.6543(34)

Expand All @@ -130,5 +130,5 @@ Acknowledgements

``sciform`` was heavily motivated by the prefix formatting provided in
the `prefixed <https://github.com/Rockhopper-Technologies/prefixed>`_
package and the value +/- uncertainty formatting in the
package and the value ± uncertainty formatting in the
`uncertainties <https://github.com/lebigot/uncertainties>`_ package.
6 changes: 2 additions & 4 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ available formatting options.
>>> unc = 1618
>>> sform = Formatter()
>>> print(sform(num, unc))
3141593 +/- 1618
3141593 ± 1618
>>> sform = Formatter(FormatOptions(
... exp_mode=ExpMode.ENGINEERING,
... exp_format=ExpFormat.PREFIX,
... pdg_sig_figs=True,
... unicode_pm=True,
... unc_pm_whitespace=False))
>>> print(sform(num, unc))
(3.1416±0.0016) M
Expand Down Expand Up @@ -136,10 +135,9 @@ instead.

>>> num_unc = SciNumUnc(3141592.7, 1618)
>>> print(f'{num_unc}')
3141593 +/- 1618
3141593 ± 1618
>>> with GlobalDefaultsContext(FormatOptions(
... pdg_sig_figs=True,
... unicode_pm=True,
... unc_pm_whitespace=False)):
... print(f'{num_unc:rp}')
(3.1416±0.0016) M
Expand Down
54 changes: 17 additions & 37 deletions docs/source/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ directly with no extra exponent.
>>> print(sform(123.456))
123.456
>>> print(sform(123.456, 0.001))
123.456 +/- 0.001
123.456 ± 0.001

.. _percent_mode:

Expand All @@ -55,7 +55,7 @@ appended to the end of the formatted string.
>>> print(sform(0.12345))
12.345%
>>> print(sform(0.12345, 0.001))
(12.3 +/- 0.1)%
(12.3 ± 0.1)%

.. _scientific:

Expand All @@ -70,7 +70,7 @@ mantissa ``m`` satisfies ``1 <= m < 10``.
>>> print(sform(123.456))
1.23456e+02
>>> print(sform(123.456, 0.001))
(1.23456 +/- 0.00001)e+02
(1.23456 ± 0.00001)e+02

Note that, for all exponent modes, the exponent integer is always
displayed with a sign symbol (+ or -) and is left padded with a zero so
Expand Down Expand Up @@ -103,7 +103,7 @@ notation.
>>> print(sform(123.456))
123.456e+00
>>> print(sform(123.456, 0.001))
(123.456 +/- 0.001)e+00
(123.456 ± 0.001)e+00

.. _engineering_shifted:

Expand All @@ -118,7 +118,7 @@ the exponent is chosen so that the mantissa ``m`` satisfies
>>> print(sform(123.456))
0.123456e+03
>>> print(sform(123.456, 0.001))
(0.123456 +/- 0.000001)e+03
(0.123456 ± 0.000001)e+03

.. _binary:

Expand Down Expand Up @@ -570,15 +570,15 @@ The latex format makes the following changes:
superscript strings like ``'\times 10^{+2}``
* Replace ``'('`` and ``')'`` by latex size-aware delimiters
``'\left('`` and ``'\right)'``.
* Replace ``'+/-'`` by ``'\pm'``
* Replace ``'±'`` by ``'\pm'``
* Replace ``'_'`` by ``'\_'``
* Replace ``'%'`` by ``'\%'``
* Exponent replacements such as ``'M'``, ``'Ki'``, or ``'ppb'`` and
non-finite numbers such as ``'nan'``, ``'NAN'``, ``'inf'``, and
``'INF'`` are wrapped in ``'\text{}'``.

Note that use of ``latex`` renders the use of ``unicode_pm`` and
``superscript_exp`` meaningless.
Note that use of ``latex`` renders the use of the ``superscript_exp``
option meaningless.

Include Exponent on nan and inf
===============================
Expand Down Expand Up @@ -633,7 +633,7 @@ according to the options below.

>>> sform = Formatter()
>>> print(sform(123.456, 0.789))
123.456 +/- 0.789
123.456 ± 0.789

.. _pdg_sig_figs:

Expand Down Expand Up @@ -678,42 +678,22 @@ mode with :class:`AutoDigits` precision and the ``pdg_sig_figs`` flag.
... ndigits=AutoDigits,
... pdg_sig_figs=True))
>>> print(sform(1, 0.0123))
1.000 +/- 0.012
1.000 ± 0.012
>>> print(sform(1, 0.0483))
1.00 +/- 0.05
1.00 ± 0.05
>>> print(sform(1, 0.0997))
1.00 +/- 0.10
1.00 ± 0.10

If ``ndigits`` is specified (i.e. not ``None``) but
``ndigits!=AutoDigits`` with ``pdg_sig_figs=True`` then ``ValueError`` is
raised.

Plus Minus Symbol Formatting
----------------------------

The user can enable (default) or disable white space around the plus/minus
symbol when formatting value/uncertainties.

>>> sform = Formatter()
>>> print(sform(123.456, 0.789))
123.456 +/- 0.789
>>> sform = Formatter(FormatOptions(unc_pm_whitespace=False))
>>> print(sform(123.456, 0.789))
123.456+/-0.789

The user can also replace the ``'+/-'`` symbol with a unicode ``'±'``
symbol using the ``unicode_pm`` option.

>>> sform = Formatter(FormatOptions(unicode_pm=True))
>>> print(sform(123.456, 0.789))
123.456 ± 0.789

.. _bracket_uncertainty:

Bracket Uncertainty
-------------------

Instead of displaying ``123.456 +/- 0.789``, there is a notation where
Instead of displaying ``123.456 ± 0.789``, there is a notation where
the uncertainty is shown in brackets after the value as
``123.456(789)``.
Here the ``(789)`` in parentheses is meant to be "matched up" with the
Expand Down Expand Up @@ -762,10 +742,10 @@ Remove Separators for Bracket Uncertainty

In some cases using bracket uncertainty results in digits such that the
decimal point could appear in the uncertainty in the brackets.
For example: ``18.4 +/- 2.1 -> 18.4(2.1)``.
For example: ``18.4 ± 2.1 -> 18.4(2.1)``.
In such cases, there is no official guidance on if the decimal symbol
should be included in the bracket symbols or not.
That is, one may format ``18.4 +/- 2.1 -> 18.4(21)``.
That is, one may format ``18.4 ± 2.1 -> 18.4(21)``.
The interpretation here is that the uncertainty is 21 tenths, since the
least significant digit of the value is in the tenths place.
The author's preference is to keep the decimal symbol because it allows
Expand Down Expand Up @@ -831,10 +811,10 @@ This feature is accessed with the ``val_unc_match_widths`` option.
... top_dig_place=2,
... val_unc_match_widths=False))
>>> print(sform(12345, 1.23))
12345.00 +/- 001.23
12345.00 ± 001.23
>>> sform = Formatter(FormatOptions(
... fill_mode=FillMode.ZERO,
... top_dig_place=2,
... val_unc_match_widths=True))
>>> print(sform(12345, 1.23))
12345.00 +/- 00001.23
12345.00 ± 00001.23
12 changes: 5 additions & 7 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Value/Uncertainty Formatting

One of the most important use cases for scientific formatting is
formatting a value together with its specified uncertainty, e.g.
``84.3 +/- 0.2``.
``84.3 ± 0.2``.
:mod:`sciform` provides the ability to format pairs of numbers into
value/uncertainty strings.
:mod:`sciform` attempts to follow
Expand All @@ -94,11 +94,11 @@ using the :class:`SciNumUnc` object.
>>> unc = 0.2
>>> sform = Formatter(FormatOptions(ndigits=2))
>>> print(sform(val, unc))
84.30 +/- 0.20
84.30 ± 0.20
>>> from sciform import SciNumUnc
>>> val_unc = SciNumUnc(val, unc)
>>> print(f'{val_unc:!2}')
84.30 +/- 0.20
84.30 ± 0.20

Value/uncertainty pairs can also be formatted using a parentheses
notation in which the uncertainty is displayed in parentheses following
Expand Down Expand Up @@ -164,7 +164,6 @@ package default settings):
'pdg_sig_figs': False,
'val_unc_match_widths': False,
'bracket_unc_remove_seps': False,
'unicode_pm': False,
'unc_pm_whitespace': True}

The global default settings can be modified by passing
Expand Down Expand Up @@ -203,7 +202,6 @@ unchanged.
'pdg_sig_figs': False,
'val_unc_match_widths': False,
'bracket_unc_remove_seps': False,
'unicode_pm': False,
'unc_pm_whitespace': True}

The global default settings can be reset to the :mod:`sciform` defaults
Expand Down Expand Up @@ -252,7 +250,7 @@ values.
Note that the :ref:`FSML <fsml>` does not provide complete control over
all possible format options.
For example, there is no code in the :ref:`FSML <fsml>` for configuring
the ``unicode_pm`` option.
the ``pdg_sig_figs`` option.
If the user wishes to configure these options, but also use the
:ref:`FSML <fsml>`, then they must do so by modifying the global default
settings.
Expand Down Expand Up @@ -304,7 +302,7 @@ concerned with the exact decimal representation of their numerical data.
for its :class:`float`.
In this format, a :class:`float` occupies 64 bits of memory: 52 bits
for the mantissa, 11 bits for the exponent and 1 bit for the sign.
* Any decimal with 15 digits between about ``+/- 1.8e+308`` can be
* Any decimal with 15 digits between about ``± 1.8e+308`` can be
uniquely represented by a :class:`float`.
However, two decimals with more than 15 digits may map to the same
:class:`float`.
Expand Down
6 changes: 0 additions & 6 deletions src/sciform/format_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class RenderedFormatOptions:
pdg_sig_figs: bool
val_unc_match_widths: bool
bracket_unc_remove_seps: bool
unicode_pm: bool
unc_pm_whitespace: bool

def __post_init__(self):
Expand Down Expand Up @@ -164,9 +163,6 @@ class FormatOptions:
separator symbols should be removed from the uncertainty when
using bracket uncertainty mode. E.g. expressing ``123.4 +/- 2.3``
as ``123.4(23)`` instead of ``123.4(2.3)``.
:param unicode_pm: :class:`bool` indicating if the ``'+/-'``
separator should be replaced with the unicode plus minus symbol
``'±'``.
:param unc_pm_whitespace: :class:`bool` indicating if there should be
whitespace surrounding the ``'+/-'`` symbols when formatting. E.g.
``123.4+/-2.3`` compared to ``123.4 +/- 2.3``.
Expand Down Expand Up @@ -200,7 +196,6 @@ class FormatOptions:
pdg_sig_figs: Optional[bool] = None
val_unc_match_widths: Optional[bool] = None
bracket_unc_remove_seps: Optional[bool] = None
unicode_pm: Optional[bool] = None
unc_pm_whitespace: Optional[bool] = None

add_c_prefix: InitVar[bool] = False
Expand Down Expand Up @@ -348,7 +343,6 @@ def validate_options(options: Union[FormatOptions, RenderedFormatOptions]):
pdg_sig_figs=False,
val_unc_match_widths=False,
bracket_unc_remove_seps=False,
unicode_pm=False,
unc_pm_whitespace=True
)

Expand Down
2 changes: 1 addition & 1 deletion src/sciform/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Formatter:
... round_mode=RoundMode.SIG_FIG,
... ndigits=2))
>>> print(sform(12345.678, 3.4))
(12.3457 +/- 0.0034)e+03
(12.3457 ± 0.0034)e+03
:param format_options: :class:`FormatOptions` indicating which
format options should be used for formatting.
Expand Down
4 changes: 1 addition & 3 deletions src/sciform/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,8 @@ def format_val_unc(val: Decimal, unc: Decimal,
if not options.bracket_unc:
if options.latex:
pm_symb = r'\pm'
elif options.unicode_pm:
pm_symb = '±'
else:
pm_symb = '+/-'
pm_symb = '±'

if options.unc_pm_whitespace:
pm_symb = f' {pm_symb} '
Expand Down
2 changes: 0 additions & 2 deletions tests/test_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def test_print_global_defaults(self):
" 'pdg_sig_figs': False,\n"
" 'val_unc_match_widths': False,\n"
" 'bracket_unc_remove_seps': False,\n"
" 'unicode_pm': False,\n"
" 'unc_pm_whitespace': True}\n"
)
self.assertEqual(actual_printout, expected_printout)
Expand Down Expand Up @@ -67,7 +66,6 @@ def test_unrendered_options_repr(self):
" 'pdg_sig_figs': False,\n"
" 'val_unc_match_widths': False,\n"
" 'bracket_unc_remove_seps': False,\n"
" 'unicode_pm': False,\n"
" 'unc_pm_whitespace': True}\n"
)
self.assertEqual(actual_printout, expected_printout)
Loading

0 comments on commit e1c0ef0

Please sign in to comment.