tools for dealing with physical quantities: uncertainty propagation and unit conversion
MetroloPy requires Python 3.10 or later and depends on NumPy, SciPy, pandas, matplotlib and ipython. It looks best in a Jupyter Notebook.
Install MetroloPy with pip install metrolopy or
conda install -c conda-forge metrolopy.
Alternatively, add it to your project, e.g. uv or pixi.
Physical quantities can then be represented in Python as gummy objects with an uncertainty and (or) a unit:
>>> import metrolopy as uc
>>> a = uc.gummy(1.2345,u=0.0234,unit='cm')
>>> a
1.234(23) cm
>>> b = uc.gummy(3.034,u=0.174,unit='mm')
>>> f = uc.gummy(uc.UniformDist(center=0.9345,half_width=0.096),unit='N')
>>> p = f/(a*b)
>>> p
2.50(21) N/cm2
>>> p.unit = 'kPa'
>>> p.uunit = '%'
>>> p
25.0 kPa ± 8.5%
MetroloPy can do much more including Monte-Carlo uncertainty propagation, generating uncertainty budget tables, and curve fitting. It can also handle expanded uncertainties, degrees of freedom, correlated quantities, and complex valued quantities. Also gummys work with many numpy functions with no wrapping.
See:
- a tutorial (or download the tutorial as Jupyter notebook)
- the documentation
- the issues page on GitHub
- a list of the units built into MetroloPy
- a list of the physical constants built into MetroloPy
-
The continuous and discrete distributions defined in
scipy.statscan now be used directly used with gummys. -
The
DistFitclass for fitting distributions and theDoFclass for descibing quantities drawn from the same underlying distribution have been added. -
The legacy
numpy.random.RandomStaterandom number generator has been replaced with the newernumpy.random.Generatorfor Monte-Carlo uncertainty propagation. Thescipy.optimize.leastsqfunction has been replaced with the newerscipy.optimize.least_squaresfunction as the solver for nonlinear least squares fitting. And the depreciatedscipy.odrpackage has been replaced withodrpackfor orthogonal distance regression. -
A class property has been added to
gummyto control the separator between the digit groupings when displaying long numbers. -
Lazy loading for gummy module components has been implemented and added
lazy_loaderas a dependancy. -
Fixed issues that affected the
gummy.applyandgummy.napplymethod when broadcasing over arguments and and applying functions that have array like return values. -
Fixed an number of bugs in the fitting module, bugs affecting some built-in distributions and incorrect derviatives for arccos and arccosh.
-
The calculation of effective degrees of freedom has been improved. In previous versions, in a multi-step calculation, the effective degree of freedom were calculated at each step based on the degrees of freedom calculated for the previous step (using a modified Welch-Satterthwaite approximation). Now effective degrees of freedom are always calculated directly from the independent variables using the standard Welch-Satterthwaite approximation.
-
CODATA 2022 values instead of 2018 values are used in the Constants module.
-
The significance value in budget table has been redefined from (sensitivity coefficient * standard uncertainty/combined uncertainty) to the square of that value so that the significance values in a budget sum to one.
-
Units can now be raised to a fractional power and many other bug fixes.
-
A constant library has been added with physical constants that can be accessed by name or alias with the
constantfunction. Thesearch_constantsfunction with no argument gives a listing of all built-in constants. Each constant definition includes any correlations with other constants. -
The
Quantityclass has been added to represent a general numerical value multiplied by a unit and theunitfunction has been added to retrieveUnitinstances from the unit library by name or alias.Unitinstances can now be multiplied and divided by otherUnitinstances to produce composite units, can be multiplied and divided by numbers to produceQuantityinstances or multiply or divideQuantityinstances. Thegummyclass is now a subclass ofQuantitywith anummyvalue rather than a subclass ofnummy. AQuantityArrayclass has been introduced to represent an array of values all with the same unit. Multiplying aUnitinstance by a list, tuple, or numpy array produces aQuantityArrayinstance. -
The
immyclass has been introduced as anummyvalued counterpart of thejummyclass for representing complex values with uncertainties.immyandjummyvalues can now be displayed in a polar representation in addition to a cartesian representation.immyandjummy.r and .phi properties have been added to access the magnitude and argument of the values as a complement to the .real and .imag properties.