Explicit Units and Dimensional Analysis for Scientific Python
Project description
Explicit units and dimensional analysis for scientific Python – Built with Correctness by William Bowley
Writing experimental physics models? Need to check dimensions at boundaries? Annoyed by vague configuration files? PicoUnits might be what you're looking for.
PicoUnits is a lightweight dimensional analysis library and DSL for writing dimensionally explicit Python code. PicoUnits support real numbers, complex numbers, and arrays based vectors. It is also a custom DSL called .uiv (unit-informed values), which allows for explicit units in configuration or reference libraries.
PicoUnits: Usage
Picounit is a single dependency (numpy) Python package that does dimensional analysis at runtime. Key features:
- Pluggable unit systems: Define custom "Unit Frames" for your domain
- Configuration format:
.uivfiles with embedded, validated units &.utfor unit types - Boundary validation:
@unit_validatordecorators catch errors at function interfaces - Full numeric support: Real, complex, and array-based vectors
But instead of talking about it, let's see some examples:
>>> from PicoUnits import MILLI, LENGTH
>>> 12 * MILLI * LENGTH + 10 * LENGTH
>>> 10.012 (m)
As expected, it returns 10.01 meters, but what is a unit? A more exotic feature of PicoUnits is that its fundamental units are fully abstract. We call this the user's "Unit Frame"; by default, it's SI metric, but it could be astronomical units:
# With a custom .picounit file defining light-years as LENGTH:
>>> from PicoUnits import MILLI, LENGTH
>>> 12 * MILLI * LENGTH + 10 * LENGTH
>>> 10.012 (ly)
All depends on the users .picounit file, which can be generated via the command PicoUnits generate. Another feature is the ability to use the .ut (unit types) and .uiv (unit-informed values) formats, which PicoUnits loads in via recursive attribute injection. So instead of a nested list, you get a wonderful object-based loader.
[version]
format: 0.1.0
unit_frame: units.ut
[model]
voltage: 18 (V) # volts
current_limit: 40 (A)
time_steps: 50 u(s)
Your .picounit file defines your Unit Frame, your .ut defines any derived units and your .uiv files defines value:unit pairs. This ensures consistency-you can't accidentally load a config file expecting SI metric when your code is running in natural units.
>>> from PicoUnits.parser import Parser
>>> p = Parser(parameters.uiv)
>>> p.model.voltage
>>> 18 (V)
Well, we’ve looked at simple calculations, changing unit frames, and importing units. But what about dimensional checks? Well, the main one is the unit_validator, which is a decorator that checks dimensionally out of a function. Let's intentionally pass wrong units to see what happens:
>>> from PicoUnits import unit_validator, VOLTAGE, IMPEDANCE, CURRENT
>>> @unit_validator(VOLTAGE)
>>> def calculate_voltage(current, impedance):
>>> return current * impedance
>>>
>>> calculate_voltage((10+1j) * CURRENT ** 2, 10 * IMPEDANCE)
>>> DimensionError: 'calculate_voltage' returned kg·m²·s⁻³, expected kg·m²·s⁻³·A⁻¹
The unit_validator checks the dimensionality of the output to ensure it matches the expected type. It's very useful when prototyping as it compartmentalizes dimensional checking, decreasing mental overhead.
For a complete worked example, see the multi-stage coilgun simulation which uses PicoUnits for electromagnetic physics calculations.
Documentation
Full documentation is available at docs, and simple to advanced examples are available at examples
Installation
To install, simply:
pip install PicoUnits
or use setuptools locally:
git clone https://github.com/wgbowley/PicoUnits.git
cd PicoUnits
pip install -e .
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file picounits-0.1.0.tar.gz.
File metadata
- Download URL: picounits-0.1.0.tar.gz
- Upload date:
- Size: 73.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d28d2ab3407167fe55cd29e210330bd2050ed62fcc3b92de9c4695f9c806cfb6
|
|
| MD5 |
7bf5a982ed89b3527cb4168e77d840ee
|
|
| BLAKE2b-256 |
7ad95ed8a46d2a24fc7a6cbb21cbd374a16a1e5efd4b71e61ebc9e74b3a8a265
|
Provenance
The following attestation bundles were made for picounits-0.1.0.tar.gz:
Publisher:
publish_picounits.yml on wgbowley/PicoUnits
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
picounits-0.1.0.tar.gz -
Subject digest:
d28d2ab3407167fe55cd29e210330bd2050ed62fcc3b92de9c4695f9c806cfb6 - Sigstore transparency entry: 1199729979
- Sigstore integration time:
-
Permalink:
wgbowley/PicoUnits@465318a750a087c92997ffe4787f92312bcf7802 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/wgbowley
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_picounits.yml@465318a750a087c92997ffe4787f92312bcf7802 -
Trigger Event:
push
-
Statement type:
File details
Details for the file picounits-0.1.0-py3-none-any.whl.
File metadata
- Download URL: picounits-0.1.0-py3-none-any.whl
- Upload date:
- Size: 55.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc50368908e40c727a8a3b26584c4c91dee3484c62f9a5653aabc0805d3646ff
|
|
| MD5 |
7be6310a270e09c1bfe20eb218f60912
|
|
| BLAKE2b-256 |
d0769b4c427be493189e90849b9b86077009189e617b82efca22795a787726c7
|
Provenance
The following attestation bundles were made for picounits-0.1.0-py3-none-any.whl:
Publisher:
publish_picounits.yml on wgbowley/PicoUnits
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
picounits-0.1.0-py3-none-any.whl -
Subject digest:
bc50368908e40c727a8a3b26584c4c91dee3484c62f9a5653aabc0805d3646ff - Sigstore transparency entry: 1199729989
- Sigstore integration time:
-
Permalink:
wgbowley/PicoUnits@465318a750a087c92997ffe4787f92312bcf7802 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/wgbowley
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_picounits.yml@465318a750a087c92997ffe4787f92312bcf7802 -
Trigger Event:
push
-
Statement type: