Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

measurements with numpy: doc and exceptions #918

Open
crusaderky opened this issue Dec 6, 2019 · 1 comment
Open

measurements with numpy: doc and exceptions #918

crusaderky opened this issue Dec 6, 2019 · 1 comment

Comments

@crusaderky
Copy link
Contributor

crusaderky commented Dec 6, 2019

As of git tip, pint doesn't support measurements and numpy at the same time.
XREF: #128, #350

Also related: #734

Short of a proper implementation as discussed in the linked documents, there are a few things that bug me:

  1. this command:
pint.Quantity([1,2], "m").plus_minus(.5)

produces a very dirty Exception:

Traceback (most recent call last):
  File "/Users/crusaderky/miniconda3/envs/pint/lib/python3.6/site-packages/uncertainties/core.py", line 3233, in ufloat
    return Variable(nominal_value, std_dev, tag=tag)
  File "/Users/crusaderky/miniconda3/envs/pint/lib/python3.6/site-packages/uncertainties/core.py", line 2719, in __init__
    value = float(value)
TypeError: only size-1 arrays can be converted to Python scalars

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/crusaderky/miniconda3/envs/pint/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-20-36473b74231e>", line 1, in <module>
    pint.Quantity([1,2], "m").plus_minus(.5)
  File "/Users/crusaderky/PycharmProjects/pint/pint/quantity.py", line 1718, in plus_minus
    return self._REGISTRY.Measurement(copy.copy(self.magnitude), error, self._units)
  File "/Users/crusaderky/PycharmProjects/pint/pint/measurement.py", line 48, in __new__
    mag = ufloat(value, error)
  File "/Users/crusaderky/miniconda3/envs/pint/lib/python3.6/site-packages/uncertainties/core.py", line 3246, in ufloat
    final_ufloat = ufloat_obsolete(nominal_value, tag_arg)
  File "/Users/crusaderky/miniconda3/envs/pint/lib/python3.6/site-packages/uncertainties/core.py", line 3189, in ufloat_obsolete
    return ufloat_fromstr(representation, tag)
  File "/Users/crusaderky/miniconda3/envs/pint/lib/python3.6/site-packages/uncertainties/core.py", line 3173, in ufloat_fromstr
    representation.strip())
AttributeError: 'numpy.ndarray' object has no attribute 'strip'

I would expect a clean TypeError "numpy and measurements are not supported together".

  1. Same problem wiith this, but the error is different:
pint.Quantity("1 kg").plus_minus(pint.Quantity([1, 2], "kg"))
Traceback (most recent call last):
  File "/Users/crusaderky/miniconda3/envs/pint/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-25-f57a4fd9ba92>", line 1, in <module>
    pint.Quantity("1 kg").plus_minus(pint.Quantity([1, 2], "kg"))
  File "/Users/crusaderky/PycharmProjects/pint/pint/quantity.py", line 1718, in plus_minus
    return self._REGISTRY.Measurement(copy.copy(self.magnitude), error, self._units)
  File "/Users/crusaderky/PycharmProjects/pint/pint/measurement.py", line 45, in __new__
    elif error < 0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
  1. https://pint.readthedocs.io/en/latest/measurement.html starts with import numpy as np, which is then not used in the document. On the opposite, the document should clearly state that measurements and numpy don't work together.
@miketynes
Copy link

As of most recent release, using numpy and uncertainties together still is impossible. Is there any development plan to make this possible? This would be a very convenient feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants