Natural units in Python
Project description
Natural units in Python
natu is a free, open-source package to represent physical quantities. There are many Python packages that deal with units and quantities (see the “See also” list below), but natu is unique because it is truly system-independent. The units are derived from physical constants with adjustable values and dimensions. The value of a unit is factored into a quantity so that the quantity is not “in” any particular unit. This offers the following advantages:
Flexibility: Different unit systems, including natural units (hence the name “natu”), can be represented by simply adjusting the physical constants.
Simplicity: Unit conversion is inherent. This results in quick computations and a small code base (<1500 lines). By default, dimensions and display units are tracked to catch errors and for string formatting, respectively. However, this feature can be turned off to reduce the computational overhead to nearly zero while still allowing input and output in mixed units.
Scalability: Numerical scaling is independent of the units used to create quantities. This can be used to prevent exponent overflow [Davies2012].
Intuitive: Each unit is a fixed quantity which can be treated as a mathematical entity. A variable quantity is expressed as the product of a number and a unit, as stated in [BIPM2006].
Representative: The structure of the package reflects the way modern units are defined: standards organizations such as NIST assign values to universal physical constants so that the values of units can be determined by physical experiments instead of prototypes.
Please view the tutorial to see how natu is used. natu incorporates some of the best features of the existing packages and introduces some novel features:
All units are defined in INI files. Units can be added or removed.
Units can involve offsets (e.g., degree Celsius (degC)) or even nonlinear functions (e.g., decibel (dB)).
Display units can be simplified automatically using coherent relations gathered from the unit definitions.
All units can be imported directly (from natu.units import *), selectively imported (from natu.units import m, kg, s), or used from within a package (from natu import units as U; length = 10*U.m).
Prefixes are automatically applied to units upon import or access.
Units are automatically copied and sorted into convenient groups (see natu.groups).
Rationalized or unrationalized unit systems can be represented.
Modules are provided as drop-in quantity-aware replacements for math and numpy.
There are no external dependencies. Only the Python Standard Library is required; numpy is optional.
natu runs in Python 2 and 3.
Fractional exponents can be used for units as well as quantities (e.g., in the definition of the statcoulomb).
For the full documentation and more examples, please see the main website.
For a list of changes, please see the change log.
Installation
The easiest way to install natu is to use pip:
> pip install natu
On Linux, it may be necessary to have root privileges:
$ sudo pip install natu
Another way is to download and extract a copy of the package from the sidebar on the left. Run the following command from the base folder:
> python setup.py install
Or, on Linux:
$ sudo python setup.py install
License terms and development
natu is published under a BSD-compatible license. Please share any improvements you make, preferably as a pull request to the master branch of the GitHub repository. There are useful development scripts in the hooks folder. If you find a bug or have a suggestion, please open an issue.
See also
References
K. Davies and C. Paredis, “Natural Unit Representation in Modelica,” in Modelica Conference (Munich, Germany), Modelica Assoc., Sep. 2012.
International Bureau of Weights and Measures (BIPM), “The International System of Units (SI),” 8th ed., 2006.