hgrecco / pint

Operate and manipulate physical quantities in Python
http://pint.readthedocs.org/
Other
2.41k stars 472 forks source link

Measurements, Uncertainties, and Arrays #1665

Open SamG-01 opened 2 years ago

SamG-01 commented 2 years ago

I want to build an array with Measurements 1 +/- 0.1 km, 2 +/- 0.1 km, and 3 +/- 0.1 km. I start with the list [1, 2, 3] and the error 0.1, so I try to make a Measurement object:

from pint import UnitRegistry
ureg = UnitRegistry()

n = [1, 2, 3]
s = 0.1

x1 = ureg.Measurement(n, s, ureg.km)

However, this returns an error AttributeError: 'list' object has no attribute 'strip' from uncertainties/core.py.

Other than a manual loop

import numpy as np
x2 = np.array([ureg.Measurement(val, s, ureg.km) for val in n])

, the only way to get something close (but not quite sufficient) is to create a Quantity instead (see #1614):

import uncertainties.unumpy as unp

x3 = ureg.Quantity(unp.uarray(n, s), ureg.km)
vasilisniaouris commented 1 year ago

I am having the same issue! Is there any intention to add this functionality?

vasilisniaouris commented 1 year ago

I followed the error. It turns out that Measurement in pint only defines a ufloat. In order to remove this error, you need to define the Measurement with an uncertainty.unumpy.uarray instead. I am not sure what the correct pint-ian(?) way of implementing an uncertainty.unumpy.uarray would be.

Related side-note: There is another error raised if you add a list of std_dev instead of a single std_dev : ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() That comes from the if error < 0: ... line, which could be replaced by if np.any(error < 0): ... to alleviate the issue.