Package: scinum
scinum provides a simpleNumberclass that wraps plain floats orNumPyarrays 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 thelegacy/v1.4 branchfor legacy support.UsageThe following examples demonstrate the most common use cases.
For more info, see theAPI documentationor open theexample.ipynbnotebook on colab or binder.Number definitionfromscinumimportNumber,UP,DOWNNumber.default_format="%.2f"num=Number(5,(2,1))print(num)# -> 5.00 +2.00-1.00# get the nominal valueprint(num.nominal)# -> 5.0print(num.n)# -> 5.0 (shorthand)print(num())# -> 5.0 (shorthand)# get uncertaintiesprint(num.get_uncertainty())# -> (2.0, 1.0)print(num.u())# -> (2.0, 1.0) (shorthand)print(num.u(direction=UP))# -> 2.0# get shifted valuesprint(num.get())# -> 5.0 (no shift)print(num.get(UP))# -> 7.0 (up shift)print(num(UP))# -> 7.0 (up shift, shorthand)print(num.get(DOWN))# -> 4.0 (down shift)print(num(DOWN))# -> 4.0 (down shift, shorthand)Multiple uncertaintiesUse 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.fromscinumimportNumbernum=Number(2.5,{"sourceA":0.5,# absolute 0.5, both up and down"sourceB":(1.0,1.5),# absolute 1.0 up, 1.5 down"sourceC":0.1j,# relative 10%, both up and down"sourceD":(0.1j,0.2j),# relative 10% up, relative 20% down"sourceE":(1.0,0.2j),# absolute 1.0 up, relative 20% down"sourceF":(0.3j,0.3),# relative 30% up, absolute 0.3 down})Correlation handlingWhen 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 coefficientrhowhen used with explicit operator methods defined on a number, such asnum.add()ornum.mul().However, it is probably more convenient to useCorrelationobjects:fromscinumimportNumber,Correlationnum=Number(2,5)print(num*num)# -> '4.0 +-20.0', fully correlated by default# same as# print(num**2)# print(num.pow(2, inplace=False))print(num*Correlation(0)*num)# -> '4.0 +-14.14', no correlation# same as# print(num.pow(2, rho=0, inplace=False))The correlation object is combined with a number through multiplication, resulting in aDeferredResultobject.
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 asdeferred=num*Correlation(0)print(deferred*num)and similarly, adding two numbers without correlation can be expressed as(num*Correlation(0))+numWhen combining numbers with multiple, named uncertainties, correlation coefficients can be controlled per uncertainty by passing names to theCorrelationconstructor.Correlation(1,sourceA=0)# zero correlation for sourceA, all others default to 1Correlation(sourceA=0)# zero correlation for sourceA, no defaultFormatting and roundingNumber.str()provides some simple formatting tools, includinglatexandroot latexsupport, as well as scientific rounding rules:# output formattingn=Number(8848,10)n.str(unit="m")# -> "8848.0 +-10.0 m"n.str(unit="m",force_asymmetric=True)# -> "8848.0 +10.0-10.0 m"n.str(unit="m",scientific=True)# -> "8.848 +-0.01 x 1E3 m"n.str(unit="m",si=True)# -> "8.848 +-0.01 km"n.str(style="fancy")# -> "$8848.0 ±10.0$"n.str(unit="m",style="fancy")# -> "$8848.0 ±10.0\,m$"n.str(unit="m",style="latex")# -> "$8848.0 \pm 10.0\,m$"n.str(unit="m",style="latex",si=True)# -> "8.848 \pm 0.01\,km"n.str(unit="m",style="root")# -> "8848.0 #pm 10.0 m"n.str(unit="m",style="root",si=True)# -> "8.848 #pm 0.01 km"# output roundingn=Number(17.321,{"a":1.158,"b":0.453})n.str()# -> '17.321 +-1.158 (a) +-0.453 (b)'n.str("%.1f")# -> '17.3 +-1.2 (a) +-0.5 (b)'n.str("publication")# -> '17.32 +-1.16 (a) +-0.45 (b)'n.str("pdg")# -> '17.3 +-1.2 (a) +-0.5 (b)'For situations that require more sophisticated rounding and formatting rules, you might want to checkout:sn.split_value()sn.match_precision()sn.round_uncertainty()sn.round_value()sn.infer_si_prefix()Uncertainty propagationfromscinumimportNumbernum=Number(5,1)print(num+2)# -> '7.0 +-1.0'print(num*3)# -> '15.0 +-3.0'num2=Number(2.5,1.5)print(num+num2)# -> '7.5 +-2.5'print(num*num2)# -> '12.5 +-10.0'# add num2 to num and consider their uncertainties to be fully uncorrelated, i.e. rho = 0num.add(num2,rho=0)print(num)# -> '7.5 +-1.80277563773'Math operationsAs a drop-in replacement for themathmodule, scinum provides an objectopsthat contains math operations that are aware of gaussian error propagation.fromscinumimportNumber,opsnum=ops.log(Number(5,2))print(num)# -> 1.60943791243 +-0.4num=ops.exp(ops.tan(Number(5,2)))print(num)# -> 0.0340299245972 +-0.845839754815print(num.str("%.2f"))# -> 0.03 +-0.85Custom operationsThere might be situations where a specific operation is not (yet) contained in theopsobject.
In this case, you can easily register a new one via:fromscinumimportNumber,ops@ops.registerdefmy_op(x):returnx*2+1@my_op.derivedefmy_op(x):return2num=ops.my_op(Number(5,2))print(num)# -> 11.00 (+4.00, -4.00)Please note that there is no need to registersimplefunctions like in the particular example above as most of them are just composite operations whose propagation rules (derivatives) are already known.NumPy arraysfromscinumimportNumberimportnumpyasnpnum=Number(np.array([3,4,5]),2)print(num)# [ 3.  4.  5.]# + [ 2.  2.  2.]# - [ 2.  2.  2.]num=Number(np.array([3,4,5]),{"sourceA":(np.array([0.1,0.2,0.3]),0.5j),# absolute values for up, 50% down})print(num)# [ 3.  4.  5.]# + sourceA [ 0.1  0.2  0.3]# - sourceA [ 1.5  2.   2.5]Installation and dependenciesViapippipinstallscinumor by simply copying the file into your project.Numpy is an optional dependency.ContributingIf you like to contribute, pull requests are happily accepted.
Just make sure to add a new test cases and run them via:>python-munittesttestsTestingIn general, tests should be run for all python versions ≥ 3.7.To run tests in a docker container, do:gitclonehttps://github.com/riga/scinum.gitcdscinum

dockerrun--rm-v`pwd`:/scinum-w/scinumpython:3.8python-munittesttestsDevelopmentSource hosted atGitHubReport issues, questions, feature requests onGitHub Issues