Package: misu
misumisuis short for “misura”, which meansmeasurement(in
Italian).misuis a package for doing calculations with in consistent
units of measurement.InstallOn Windows, precompiled wheels are provided so all you have to do is
this:pipinstallmisuOn Linux, you have to install from a source distribution (sdist). This is
also on PyPI, but you must already have Cython and numpy present in your
target environment. This is because they are required to buildmisu.
Thus, you need something like this on Linux:$python3.7-mvenvvenv$sourcevenv/bin/activate(venv)$pipinstallCythonnumpy(venv)$pipinstallmisu<lotsofcompileroutput>If you have have experience with makingmanylinuxwheels for Linux, I
would love to get your help to make them formisutoo!DemoMost of the time you will probably work withmisuinteractively, and
it will be most convenient to import the entire namespace:frommisuimport*mass=100*kgprint(mass>>lb)The symbolkggot imported from themisupackage. We redefine
the shift operator to perform inline conversions. The code above
produces:220.46226218487757There are many units already defined, and it is easy to add more. Here
we convert the same quantity into ounces:print(mass>>oz)output:3571.4285714285716What you see above would be useless on its own. What you really need is
to be able to perform consistent calculations with quantities expressed
in different, but compatible units:mass=10*kg+20*lbprint(mass)output:19.07 kgFor addition and subtraction,misuwill ensure that only consistent
units can be used. Multiplication and division will produce new units:distance=100*metrestime=9.2*secondsspeed=distance/timeprint(speed)output:10.87 m/sAs before, it is trivially easy to express that quantity in different
units of compatible dimensions:print(speed>>km/hr)output:39.130434782608695Introductionmisuis a package of handling physical quantities with dimensions.
This means performing calculations with all the units being tracked
correctly. It is possible to addkilograms per hourtoounces per
minute, obtain the correct answer, and have that answer be reported in,
say,pounds per week.misugrew out of a personal need. I have used this code personally
in a (chemical) engineering context for well over a year now (at time of
writing, Feb 2015). Every feature has been added in response to a
personal need.FeaturesSpeed optimized.misuis very fast! Heavy math code in Python
will be around only 5X slower when used withmisu. This is much
faster than other quantities packages for Python.Written as a Cython extension module. Speed benefits carry over when
usingmisufrom your own Cython module (a.pxdis provided
for linking).When an operation involving incompatible units is attempted, anEIncompatibleUnitsexception is raised, with a clear explanation
message about which units were inconsistent.Decorators for functions to enforce dimensions@dimensions(x='Length',y='Mass')deff(x,y):returnx/yf(2*m,3*kg)# Worksf(200*feet,3*tons)# Worksf(2*joules,3*kelvin)# raises AssertionErrorf(2*m,3)# raises AssertionErrorAn operator for easily stripping the units component to obtain a
plain numerical valuemass=100*kgmass_lb=mass>>lbduty=50*MWduty_BTU_hr=duty>>BTU/hrAn enormous amount of redundancy in the naming of various units. This
means thatm,metre,metres,METRE,METRESwill
all work. The reason for this is that from my own experience, when
working interactively (e.g. in the IPython Notebook) it can be very
distracting to incorrectly guess the name for a particular unit, and
have to look it up.ft,footandfeetall work,m3meansm**3and so on.You can specify areporting unitfor a dimension, meaning that you
could have all lengths be reported in “feet” by default for example.You can specify areporting formatfor a particular unit.There are other projects, whymisu?There are several units systems for Python, but the primary motivating
use-case is thatmisuis written as a Cython module and is by far
the fastest* for managing units available in Python.*Except for ``NumericalUnits``, which is a special case**I haven’t actually checked that this statement is true for all of
them yet.General usageFor speed-critical code, the application of unit operations can still be
too slow. In these situations it is typical to first cast quantities
into numerical values (doubles, say), perform the speed-critical
calculations (perhaps call into a C-library), and then re-cast the
result back into a quantity and return that from a function.@dimensions(x='Length',y='Mass')deff(x,y):x=x>>metrey=y>>ounces<codethatassumesmetersandounces,returnsvalueinBTU>returnanswer*BTUThis way you can still easily wrap performance-critical calculations
with robust unit-handling.InspirationThe inspiration formisuwasFrinkby Alan Eliasen. It iswonderful, but I need to work with units in the IPython Notebook, and
with all my other Python code.There are a bunch of other similar projects. I have not used any of them
enough yet to provide a fair comparison:astropy.unitsBuckinghamDimPyMagnitudeNumericalUnitsPintPython-quantitiesScalarScientific.Physics.PhysicalQuantitiesSciMathsympy.physics.unitsudunitspyUnitsUnum