Skip to main content

Python parser & interface for UCUM (Unified Code for Units of Measure).

Project description

Easier access to UCUM from Python

Feedback welcome! Currently only the conversion direction from UCM to pint is supported. Please review the definitions before you trust them. While we have many tests in place and reviewed the mappings carefully, bugs may still be present.

UCUM (Unified Code for Units of Measure) is a code system intended to cover all units of measures. It provides a formalism to express units in an unambiguous way suitable for electronic communication. Note that UCUM does non provide a canonical representation, e.g. m/s and m.s-1 are expressing the same unit in two ways.

ucumvert is a pip-installable Python package. Features:

  • Parser for UCUM unit strings that implements the full grammar.
  • Converter for creating pint units from UCUM unit strings.
  • A pint unit definition file pint_ucum_defs.txt that extends pint´s default units with UCUM units. All UCUM units from Version 2.1 of the specification are included.

ucumvert generates the UCUM grammar by filling a template with unit codes, prefixes etc. from the official ucum-essence.xml file (a copy is included in this repo). So updating the parser for new UCUM releases is straight forward. The parser is built with the great lark parser toolkit. The generated lark grammar file for case-sensitive UCUM codes is included in the repository, see ucum_grammar.lark.

Some of the UCUM unit atoms are invalid unit names in pint, for example cal_[15], m[H2O], 10*, [in_i'H2O]. For all of them we define mappings to valid pint unit names in ucum_pint.py, e.g. {"cal_[15]": "cal_15"}.

Install

Installation from git in developer mode including creation of a virtual environment (pip should be newer than 23.1):

Linux

git clone https://github.com/dalito/ucumvert.git
cd ucumvert
python -m venv .venv
source .venv/bin/activate
pip install -e .[dev]

Windows

git clone https://github.com/dalito/ucumvert.git
cd ucumvert
py -m venv .venv
.venv\Scripts\activate.bat
pip install -e .[dev]

Optionally you can visualize the parse trees with Graphviz as shown below. It requires the additional package pydot; install by running pip install pydot.

Demo

We provide a basic command line interface.

(.venv) $ ucumvert

It has an interactive mode to test parsing UCUM codes:

(.venv) $ ucumvert -i
Enter UCUM unit code to parse, or 'q' to quit.
> m/s2.kg
Created visualization of parse tree (parse_tree.png).
main_term
  term
    term
      simple_unit       m
      /
      annotatable
        simple_unit     s
        2
    .
    simple_unit
      k
      g
--> Pint <Quantity(1.0, 'kilogram * meter / second ** 2')>
> q

So the intermediate result is a tree which is then traversed to convert the elements to pint quantities (or pint-compatible strings):

parse tree

The package includes an UCUM-aware pint UnitRegistry which loads all definitions for UCUM units on instantiation. It comes with an additional method from_ucum to convert UCUM codes to pint.

>>> from ucumvert import PintUcumRegistry
>>> ureg = PintUcumRegistry()
>>> ureg.from_ucum("m/s2.kg")
<Quantity(1.0, 'kilogram * meter / second ** 2')>
>>> ureg.from_ucum("m[H2O]{35Cel}")  # UCUM code with annotation
<Quantity(1, 'm_H2O')>
>>> _.to("mbar")
<Quantity(98.0665, 'millibar')>
>>> ureg("degC")   # a standard pint unit
<Quantity(1, 'degree_Celsius')>
>>>

Tests

The unit tests include parsing and converting all common UCUM unit codes from the official repo. Run the test suite by:

pytest

The common UCUM unit codes are available only in binary form (xlsx, docs, pdf). Here we keep a copy in tsv-format ucum_examples.tsv. To (re)generate this tsv-file from the official xlsx-file in the UCUM repository run

pip install openpyxl
python src/src/ucumvert/vendor/get_ucum_example_as_tsv.py

Useful links

License

The code in this repository is distributed under MIT license with the exception of the ucum-*.* files in the directory src/ucumvert/vendor that fall under the UCUM Copyright Notice and License (Version 1.0). We consider ucumvert according to §1.3 not as "Derivative Works" of UCUM because ucumvert only "interoperates with an unmodified instance of the Work".

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ucumvert-0.1.0.tar.gz (51.7 kB view details)

Uploaded Source

Built Distribution

ucumvert-0.1.0-py3-none-any.whl (52.6 kB view details)

Uploaded Python 3

File details

Details for the file ucumvert-0.1.0.tar.gz.

File metadata

  • Download URL: ucumvert-0.1.0.tar.gz
  • Upload date:
  • Size: 51.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for ucumvert-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4f23071eb1b2df2e1ab6746997ba0af2588790da30bc8d3b8c8bb0673dcbe10a
MD5 1d78662f0eb63f7a954fa118e57f6812
BLAKE2b-256 f9990b781db016a81e3c3714d50d1cf41ec04eb9908f46192b2ff96d9c2649f3

See more details on using hashes here.

File details

Details for the file ucumvert-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ucumvert-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 52.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for ucumvert-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1c1ca3d8f84f7864d8e5760ccd70c11ef8325940a71b878c348227e8bc4c385
MD5 a1570fc8f7cb551e60654dc25a9df1a6
BLAKE2b-256 d4eb94d889bd6f552d1c60652fb6e2e2ab62722eb31e04792c5d566288784827

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page