Skip to main content

A library containing functions used for estimating uncertainties

Project description

uncertaintylib logo

PyPI version Python versions Tests License: MIT

uncertaintylib is a Python library for estimating and propagating uncertainties in engineering and scientific calculations. The library is developed by Equinor K-lab Technology Test Center. It is designed to work with any Python function whose inputs and outputs are flat dictionaries.

Key Principles

  • Function-agnostic: You can pass any Python function to the library, as long as its inputs and outputs are flat dictionaries (no nested dicts or lists).
  • Standard-inspired: Attempts to follow uncertainty propagation principles outlined in JCGM 100:2008 (Guide to the Expression of Uncertainty in Measurement).

Installation

Install from PyPI:

pip install uncertaintylib

Usage

The main interface is through functions in uncertaintylib.uncertainty_functions. You provide:

  • A Python function (inputs: flat dict, outputs: flat dict)
  • A dictionary containing 'mean' and 'standard_uncertainty' for each variable. These are provided as sub dictionaries.
  • The standard uncertainty can also be given in relative terms (%), using the 'standard_uncertainty_percent' key
  • If both 'standard_uncertainty' and 'standard_uncertainty_percent', the largest of the two will be used. This can be useful for variables where for example noise dominates the uncertainty in the lower region (such as zero stability of a coriolis massflow meter)
  • 'min' and 'max' are only used in Monte Carlo calculations, to address the issue of non-physical distributions (for example distribution of mole-% of a component going below 0)
  • 'distribution' is used mainly by Monte Carlo, but also in cases where some of the inputs are settings (for example 0 or 1), where you don't want to perturb or calculate sensitivity coefficients for that specific input variable. In this case 'distribution' can be set to 'none', which will ignore that parameter.

Example usage (standard uncertainty calculation):

from uncertaintylib import uncertainty_functions

inputs = {
    'mean': {'Q': 370, 'rho': 54},
    'standard_uncertainty': {'Q': 1, 'rho': 0.03},
    'standard_uncertainty_percent': {'Q': 0.25, 'rho': 0.1},
    'distribution': {'Q': 'normal', 'rho': 'normal'},
    'min': {'Q': 0, 'rho': 0},
    'max': {'Q': None, 'rho': None}
}

def calculate_massflow(inputs):
    outputs = {}
    
    outputs['MassFlow'] = inputs['Q']*inputs['rho']

    return outputs

results = uncertainty_functions.calculate_uncertainty(
    indata=inputs, 
    function=calculate_massflow
    )

print(results)

Another example, Example 06, demonstrates uncertainty calculations for a natural gas metering station, demonstrating usage of many of the functions available in this library.

Uncertainty Models

uncertaintylib.uncertainty_models provides specialized uncertainty estimation methods for specific measurement types:

  • Gas Composition Uncertainty (gas_composition): Three methods for estimating GC analysis uncertainties:
    • ASTM D1945 (reproducibility method for pipeline quality gas)
    • NORSOK I-106 (molar mass ratio method)
    • Hagenvik 2024 (empirical power law from parallel gas samples)

See the uncertainty_models documentation for detailed guidance on method selection and Example 09 for practical comparison.

Plotting Functionalities

uncertaintylib includes plotting utilities based on matplotlib, available in uncertaintylib.plot_functions. These functions help visualize uncertainty propagation and contributions:

  • Monte Carlo Distribution Plots: Visualize the distribution of output properties from Monte Carlo simulations, including summary tables of mean and uncertainty.
  • Uncertainty Contribution Plots: Show the percentage contribution of each input variable to the total expanded uncertainty of an output property.

All plots are generated using matplotlib and can be customized or saved using standard matplotlib methods. See the API docstrings in plot_functions.py for details.

Documentation & Examples

  • See the examples/ folder for practical scripts demonstrating usage.
  • API documentation is available in the source code docstrings.

Running Tests

Tests are located in the tests/ folder and are run with pytest. Two additional packages are required:

pip install pytest pvtlib
pytest tests

License

MIT License. See LICENSE for details.

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

uncertaintylib-1.1.2.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uncertaintylib-1.1.2-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file uncertaintylib-1.1.2.tar.gz.

File metadata

  • Download URL: uncertaintylib-1.1.2.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for uncertaintylib-1.1.2.tar.gz
Algorithm Hash digest
SHA256 b687d7c6d2f51169ba63618efb6a5a0af82cfe8ba02df853822f8d65cde44124
MD5 6604c1531a6272bc2c374f5b2a1d5252
BLAKE2b-256 536fe2cc8aa43d2ecee995a6b76610e460a3cbadb6131e30c3d389bb195fb241

See more details on using hashes here.

File details

Details for the file uncertaintylib-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: uncertaintylib-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for uncertaintylib-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8e308209fc873154d1d6e0738a902b2d558ff65afc8a70c03b34d5815e5f0a8
MD5 7d1e9a1ebcfc7692f6352b02ea5846f1
BLAKE2b-256 a94048c92304598dcf02558d1fdcc7cc4af56406e4ed9dc9f25e59494deff05b

See more details on using hashes here.

Supported by

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