Uncertainties are present in any experiment and it is often hard to specify and propagate them in the calculations.

The uncertainties package is very helpful because it allows you to define variables with uncertainties that automatically propagates the uncertainties when you do your calculations.

conda install uncertainties

or

pip install uncertainties

Then, in python you import

from uncertainties import ufloat
from uncertainties import numpy as unp # for numpy support

Example

Beer-Lamberts law

We want to compute the thickness of an item in transmission imaging. We know

  • The attenuation coefficient $\mu=4.3\pm 0.1$
  • The open beam intensity $I_0=10356\pm112$
  • The open beam intensity $I=7654\pm80$

Define the variables

I0=ufloat(10356,112)
I=ufloat(7654,80)
mu=ufloat(4.3,0.1)

A basic calculation

T=I/I0
print(T)

Result: 0.739+/-0.011

Using numpy

Numpy is not directly supported in its normal form, but the uncertainty package has an alternative that can handle the new data type

d=-unp.log(T)/mu
print(d)

Result: 0.070+/-0.004

Accesing nominal and uncertainty values

This can be used to compute the relative uncertainty of a variable

d.std_dev/d.nominal_value

Open In Colab