Skip to content

Commit

Permalink
Fix cli uncertainty package import (#2032)
Browse files Browse the repository at this point in the history
  • Loading branch information
daspk04 committed Jul 8, 2024
1 parent f9c381c commit b2fc74a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ Pint Changelog
0.25 (unreleased)
-----------------

- Fix the default behaviour for pint-convert (cli) for importing uncertainties package
- Added mu and mc as alternatives for SI micro prefix
- Added ℓ as alternative for liter
- Fix the default behaviour for pint-convert (cli) for importing uncertainties package (PR #2032, Issue #2016)
- Support permille units and `‰` symbol (PR #2033, Issue #1963)


Expand Down
27 changes: 15 additions & 12 deletions docs/dev/pint-convert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,39 @@ With the `uncertainties` package, the experimental uncertainty in the physical
constants is considered, and the result is given in compact notation, with the
uncertainty in the last figures in parentheses:

The uncertainty can be enabled with `-U` (by default it is not enabled):

.. code-block:: console
$ pint-convert -p 20 -U Eh eV
1 hartree = 27.211386245988(52) eV
.. code-block:: console
$ pint-convert Eh eV
$ pint-convert -U Eh eV
1 hartree = 27.21138624599(5) eV
The precision is limited by both the maximum number of significant digits (`-p`)
and the maximum number of uncertainty digits (`-u`, 2 by default)::

$ pint-convert -p 20 Eh eV
$ pint-convert -U -p 20 Eh eV
1 hartree = 27.211386245988(52) eV

$ pint-convert -p 20 -u 4 Eh eV
$ pint-convert -U -p 20 -u 4 Eh eV
1 hartree = 27.21138624598847(5207) eV

The uncertainty can be disabled with `-U`):

.. code-block:: console
$ pint-convert -p 20 -U Eh eV
1 hartree = 27.211386245988471444 eV
Correlations between experimental constants are also known, and taken into
account. Use `-C` to disable it:
account if uncertainties `-U` is enabled. Use `-C` to disable it:

.. code-block:: console
$ pint-convert --sys atomic m_p
1 proton_mass = 1836.15267344 m_e
$ pint-convert -U --sys atomic m_p
1 proton_mass = 1836.15267344(11) m_e
$ pint-convert --sys atomic -C m_p
$ pint-convert -U --sys atomic -C m_p
1 proton_mass = 1836.15267344(79) m_e
Again, note that results may differ slightly, usually in the last figure, from
Expand Down
28 changes: 20 additions & 8 deletions pint/pint_convert.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
)
parser.add_argument(
"-U",
"--no-unc",
"--with-unc",
dest="unc",
action="store_false",
help="ignore uncertainties in constants",
action="store_true",
help="consider uncertainties in constants",
)
parser.add_argument(
"-C",
Expand Down Expand Up @@ -77,7 +77,12 @@ def _set(key: str, value):


if args.unc:
import uncertainties
try:
import uncertainties
except ImportError:
raise Exception(
"Failed to import uncertainties library!\n Please install uncertainties package"
)

# Measured constants subject to correlation
# R_i: Rydberg constant
Expand All @@ -103,9 +108,14 @@ def _set(key: str, value):
[0.00194, 0.97560, 0.98516, 0.98058, 1.0, 0.51521], # m_p
[0.00233, 0.52445, 0.52959, 0.52714, 0.51521, 1.0],
] # m_n
(R_i, g_e, m_u, m_e, m_p, m_n) = uncertainties.correlated_values_norm(
[R_i, g_e, m_u, m_e, m_p, m_n], corr
)
try:
(R_i, g_e, m_u, m_e, m_p, m_n) = uncertainties.correlated_values_norm(
[R_i, g_e, m_u, m_e, m_p, m_n], corr
)
except AttributeError:
raise Exception(
"Correlation cannot be calculated!\n Please install numpy package"
)
else:
R_i = uncertainties.ufloat(*R_i)
g_e = uncertainties.ufloat(*g_e)
Expand Down Expand Up @@ -160,6 +170,7 @@ def _set(key: str, value):


def convert(u_from, u_to=None, unc=None, factor=None):
prec_unc = 0
q = ureg.Quantity(u_from)
fmt = f".{args.prec}g"
if unc:
Expand All @@ -171,7 +182,8 @@ def convert(u_from, u_to=None, unc=None, factor=None):
if factor:
q *= ureg.Quantity(factor)
nq *= ureg.Quantity(factor).to_base_units()
prec_unc = use_unc(nq.magnitude, fmt, args.prec_unc)
if args.unc:
prec_unc = use_unc(nq.magnitude, fmt, args.prec_unc)
if prec_unc > 0:
fmt = f".{prec_unc}uS"
else:
Expand Down

0 comments on commit b2fc74a

Please sign in to comment.