scinum provides a simple Number
class that wraps plain floats or NumPy arrays and adds support for multiple uncertainties, automatic (gaussian) error propagation, and scientific rounding.
Note: Support for Python versions 2.7 and 3.6 has been dropped starting from v2.0.0.
Checkout the legacy/v1.4 branch for legacy support.
Usage
The following examples demonstrate the most common use cases.
For more info, see the API documentation or open the example.ipynb notebook on colab or binder.
Number definition
from scinum import Number, UP, DOWN
Number.default_format = "%.2f"
num = Number(5, (2, 1))
print(num)
print(num.nominal)
print(num.n)
print(num())
print(num.get_uncertainty())
print(num.u())
print(num.u(direction=UP))
print(num.get())
print(num.get(UP))
print(num(UP))
print(num.get(DOWN))
print(num(DOWN))
Multiple uncertainties
Use single values to denote symmetric uncertainties, and tuples for asymmetric ones.
Float values refer to absolute values whereas complex numbers (only their imaginary part) define relative effects.
from scinum import Number
num = Number(2.5, {
"sourceA": 0.5,
"sourceB": (1.0, 1.5),
"sourceC": 0.1j,
"sourceD": (0.1j, 0.2j),
"sourceE": (1.0, 0.2j),
"sourceF": (0.3j, 0.3),
})
Correlation handling
When two numbers are combined by means of an operator, the correlation between equally named uncertainties is assumed to be 1.
The example above shows how to configure this correlation coefficient rho
when used with explicit operator methods defined on a number, such as num.add()
or num.mul()
.
However, it is probably more convenient to use Correlation
objects:
from scinum import Number, Correlation
num = Number(2, 5)
print(num * num)
print(num * Correlation(0) * num)
The correlation object is combined with a number through multiplication, resulting in a DeferredResult
object.
The deferred result is used to resolve the actual uncertainty combination once it is applied to another number instance which happens in a second step.
Internally, the above example is handled as
deferred = num * Correlation(0)
print(deferred * num)
and similarly, adding two numbers without correlation can be expressed as
(num * Correlation(0)) + num
When combining numbers with multiple, named uncertainties, correlation coefficients can be controlled per uncertainty by passing names to the Correlation
constructor.
Correlation(1, sourceA=0)
Correlation(sourceA=0)
Formatting and rounding
Number.str()
provides some simple formatting tools, including latex
and root latex
support, as well as scientific rounding rules:
n = Number(8848, 10)
n.str(unit="m")
n.str(unit="m", force_asymmetric=True)
n.str(unit="m", scientific=True)
n.str(unit="m", si=True)
n.str(style="fancy")
n.str(unit="m", style="fancy")
n.str(unit="m", style="latex")
n.str(unit="m", style="latex", si=True)
n.str(unit="m", style="root")
n.str(unit="m", style="root", si=True)
n = Number(17.321, {"a": 1.158, "b": 0.453})
n.str()
n.str("%.1f")
n.str("publication")
n.str("pdg")
For situations that require more sophisticated rounding and formatting rules, you might want to checkout:
Uncertainty propagation
from scinum import Number
num = Number(5, 1)
print(num + 2)
print(num * 3)
num2 = Number(2.5, 1.5)
print(num + num2)
print(num * num2)
num.add(num2, rho=0)
print(num)
Math operations
As a drop-in replacement for the math
module, scinum provides an object ops
that contains math operations that are aware of gaussian error propagation.
from scinum import Number, ops
num = ops.log(Number(5, 2))
print(num)
num = ops.exp(ops.tan(Number(5, 2)))
print(num)
print(num.str("%.2f"))
Custom operations
There might be situations where a specific operation is not (yet) contained in the ops
object.
In this case, you can easily register a new one via:
from scinum import Number, ops
@ops.register
def my_op(x):
return x * 2 + 1
@my_op.derive
def my_op(x):
return 2
num = ops.my_op(Number(5, 2))
print(num)
Please note that there is no need to register simple functions like in the particular example above as most of them are just composite operations whose propagation rules (derivatives) are already known.
NumPy arrays
from scinum import Number
import numpy as np
num = Number(np.array([3, 4, 5]), 2)
print(num)
num = Number(np.array([3, 4, 5]), {
"sourceA": (np.array([0.1, 0.2, 0.3]), 0.5j),
})
print(num)
Installation and dependencies
Via pip
pip install scinum
or by simply copying the file into your project.
Numpy is an optional dependency.
Contributing
If you like to contribute, pull requests are happily accepted.
Just make sure to add a new test cases and run them via:
> python -m unittest tests
Testing
In general, tests should be run for all python versions ≥ 3.7.
To run tests in a docker container, do:
git clone https://github.com/riga/scinum.git
cd scinum
docker run --rm -v `pwd`:/scinum -w /scinum python:3.8 python -m unittest tests
Development