GitHub - Bowley-Systems/PicoUnits: A Dynamic Runtime Type System for Dimensional Numerical Quantities.

GitHub

2 min read Original article ↗

PicoUnits logo

A Dynamic Runtime Type System for Dimensional Numerical Quantities.

Define the type. Define the variable. Execute.
Automate physical meaning throughout your pipeline.

Overview

License Python Version Coverage PyPI Downloads

PicoUnits is a dynamic runtime dimensional typing system for numerical quantities. It provides a consistent type system for expressing dimensional quantities throughout your pipeline.

Important

Features:

  • Configurable unit frames with custom symbols and dimension ordering
  • Parses UnitValues language formats: unit types (.ut) and unit-informed values (.uiv)
  • Numerical support for real, complex, and vector quantities

Why convert at all?

PicoUnits removes uncertainty by reducing the set of units to one canonical set defined by the user.

It does not attempt to answer:

How might one convert between systems at a boundary?

3 feet → ? metre (1/3.280839895...?) 
↺ Each iteration

Because for computation, this is quite flawed. It destroys certainty for implementation convenience.

Define unit frame → Define derived units → Work within it, not outside it.

What is a Unit Frame?

A unit frame defines the dimensional system used by an application.

For example:

[symbols]
time: s
length: m
mass: kg
current: A
temperature: K
amount: mol
luminosity: cd
dimensionless: ∅

The dimensional environment is independent of the notation used to represent it. Hence, any semantic representation can be used. However, PicoUnits operates on a fixed set of fundamental dimensions and prefixes.

See the .picounits file for implementation details.

What are .ut and .uiv?

Both are dimensionally aware formats: .ut defines custom units, while .uiv encodes quantities as value prefix(unit) groups.

.ut defines the custom units for your unit system:

[units]
p: kg*m^-1*s^-2                # Defines the unit for pressure (Pascal)

.uiv defines the quantities within your unit system:

[model]
inlet_pressure: 101 k(p)  # 101 kPa using the defined unit p

See the UnitValues repository for notation and language specification.

Quick Start

A standard introduction example is available in example/.

from picounits import expects, VOLTAGE, CURRENT, RESISTANCE
 
@expects(VOLTAGE)
def ohm_law(i, r):
    return i * r
 
# Correct Usage
ohm_law(10 * CURRENT, 5 * RESISTANCE) 
# > Output: 50.0 (kg·m²·s⁻³·A⁻¹)

# Incorrect Usage
ohm_law(10 * CURRENT, 5 * VOLTAGE)
# > DimensionError: 'ohm_law' returned kg·m²·s⁻³, expected kg·m²·s⁻³·A⁻¹

Installation

To install:

Documentation

Full documentation is available in the docs/ folder, including API reference, changelog, and contributors.