Metadata-Version: 2.4
Name: PicoUnits
Version: 1.0.4
Summary: Explicit Units and Dimensional Analysis for Scientific Python
Project-URL: Homepage, https://github.com/wgbowley/picounits
Project-URL: Bug Tracker, https://github.com/wgbowley/picounits/issues
Author-email: William Bowley <wgrantbowley@gmail.com>
Maintainer-email: William Bowley <wgrantbowley@gmail.com>
License: MIT License
        
        Copyright (c) 2025 William Bowley
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: error-checking,minimal,physics,si,units
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.8
Requires-Dist: numpy
Description-Content-Type: text/markdown

<p align="center">
  <img src="https://raw.githubusercontent.com/wgbowley/PicoUnits/refs/heads/main/media/picounit_logo.png" alt="PicoUnits" style="max-width:600px;">
  <br>
  <br>
  <em>Explicit units and dimensional analysis for scientific Python – Built with care by <a style="color: #861211" href="https://github.com/wgbowley">William Bowley</a></em>
  
</p>

---


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
<!-- NOTE: Python version needs to checked before release, I am not sure the specific version  -->
![Python Version](https://img.shields.io/badge/python-3.10+-red)
![License](https://img.shields.io/badge/license-MIT-white)

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**:  `.uiv` files with embedded, validated units & `.ut` for unit types
- **Boundary validation**:  `@unit_validator` decorators 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:

```py
>>> 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:

```py
# 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.

```yaml
[version]
format: 0.1.0
unit_frame: units.ut

[model]
voltage: 18 (V)
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.

```py
>>> 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:

```py
>>> 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 <a style="color: #861211" href="examples/coilgun/">multi-stage coilgun simulation</a>
 which uses PicoUnits for electromagnetic physics calculations.

## Documentation 
Full documentation is available at <a style="color: #861211" href="/docs/">docs</a>, and simple to advanced examples are available at <a style="color: #861211" href="/examples/">examples</a>


### Installation
To install, simply:
```bash
pip install PicoUnits
```
or use setuptools locally:

```bash
git clone https://github.com/wgbowley/PicoUnits.git
cd PicoUnits
pip install -e .
```
 
