Skip to main content

Automated error propagation for physical experiments: symbolic derivatives, uncertainty propagation, and LaTeX output for reports.

Project description

Uncertainty Calculator

Python 3.12+ CI Status codecov Code style: ruff Checked with mypy pre-commit License: MIT Open in Colab

"Help you get rid of the Physical Chemistry Experiment course in CCME at PKU!"

A Python tool for automated error propagation in physical experiments. This calculator uses symbolic differentiation to compute partial derivatives and propagate uncertainties, generating detailed LaTeX output for your reports.


📑 Table of Contents

✨ Features

  • Symbolic Differentiation: Automatically calculates partial derivatives using sympy.
  • Automated Error Propagation: Computes the final uncertainty based on the standard error propagation formula.
  • LaTeX Output: Generates ready-to-use LaTeX code for equations, derivatives, and substitution steps.
  • Customizable: Configurable decimal precision, units, and output formatting.

🚀 Installation

Requirements

  • Python: 3.12+
  • Dependencies: sympy

Install via uv

We recommend using uv for fast environment management:

uv venv --python 3.12
source .venv/bin/activate
uv pip install -e .[dev]

📖 Usage

The calculator is designed to be used as a Python module. Below is a complete example of how to configure and run calculation.

Code layout

The core logic is organized by responsibility:

  • calculator.py: orchestrates the pipeline
  • parsers.py: builds symbols/mappings from inputs
  • compute.py: performs derivatives and numeric propagation
  • render.py: produces LaTeX output
  • _types.py: input dataclasses and type aliases
  • format.py / validation.py: shared helpers

1. Define the Equation

The equation can be defined using the Equation class. latex_name is the rendered result symbol, while expression is the internal symbolic formula written with variable names.

from uncertainty_calculator import Equation

# Define equation
equation = Equation(
    latex_name=r"\zeta",
    expression=r"(K*pi*eta*u*l)/(4*pi*phi*e_0*e_r)",
)

2. Define Variables

Variables can be defined using the Variable class for structured input.

from uncertainty_calculator import Variable

# Define variables
variables = [
    Variable(name="K", value=4.0, uncertainty=0.0, latex_name="K"),
    Variable(name="eta", value=0.9358e-3, uncertainty=5.773502691896258e-05, latex_name=r"\eta"),
    Variable(name="u", value=3.68e-5, uncertainty=0.11e-5, latex_name="u"),
    Variable(name="l", value=0.2256, uncertainty=0.0019, latex_name="l"),
    Variable(name="phi", value=100.0, uncertainty=0.5773502691896257, latex_name=r"\varphi"),
    Variable(name="e_0", value=8.8541878128e-12, uncertainty=0.0, latex_name=r"\varepsilon_0"),
    Variable(
        name="e_r", value=78.7, uncertainty=0.057735026918962574, latex_name=r"\varepsilon_\text{r}"
    ),
]

3. Configuration

Configure the output format, precision, and units.

from uncertainty_calculator import Digits

# Set digits of results
digits = Digits(mu=3, sigma=3)

# Set units of results
last_unit = r"\text{V}"  # Use None if dimensionless
# last_unit = None

# Print separately or integrally
separate = False

# Insert numbers or not
insert = False

# Include equation number or not
include_equation_number = True

4. Run the Calculator

Import the UncertaintyCalculator and execute the calculation.

from uncertainty_calculator import UncertaintyCalculator

calculator = UncertaintyCalculator(
    digits=digits,
    last_unit=last_unit,
    separate=separate,
    insert=insert,
    include_equation_number=include_equation_number,
)

latex_string = calculator.run(equation=equation, variables=variables)

print(latex_string)

5. Render the LaTeX String

from IPython.display import Latex

Latex(latex_string)

📊 Output Example

The tool generates LaTeX code that renders to standard physical chemistry calculation steps:

\begin{equation}
\begin{aligned}
\zeta&=\frac{K \eta l u}{4 \varepsilon_0 \varepsilon_\text{r} \varphi}=0.111\ \text{V}\\
\\
\frac{\partial \zeta }{\partial \eta }&=\frac{K l u}{4 \varepsilon_0 \varepsilon_\text{r} \varphi}=1.2 \times 10^{2}\\
\frac{\partial \zeta }{\partial u }&=\frac{K \eta l}{4 \varepsilon_0 \varepsilon_\text{r} \varphi}=3.0 \times 10^{3}\\
\frac{\partial \zeta }{\partial l }&=\frac{K \eta u}{4 \varepsilon_0 \varepsilon_\text{r} \varphi}=0.49\\
\frac{\partial \zeta }{\partial \varphi }&=- \frac{K \eta l u}{4 \varepsilon_0 \varepsilon_\text{r} \varphi^{2}}=-0.0011\\
\frac{\partial \zeta }{\partial \varepsilon_\text{r} }&=- \frac{K \eta l u}{4 \varepsilon_0 \varepsilon_\text{r}^{2} \varphi}=-0.0014\\
\\
\sigma_{\zeta}&=\sqrt{\left(\frac{\partial \zeta }{\partial \eta } \sigma_{\eta}\right)^2+\left(\frac{\partial \zeta }{\partial u } \sigma_{u}\right)^2+\left(\frac{\partial \zeta }{\partial l } \sigma_{l}\right)^2+\left(\frac{\partial \zeta }{\partial \varphi } \sigma_{\varphi}\right)^2+\left(\frac{\partial \zeta }{\partial \varepsilon_\text{r} } \sigma_{\varepsilon_\text{r}}\right)^2}\\
&=\sqrt{\left(0.0069\right)^2+\left(0.0033\right)^2+\left(0.00094\right)^2+\left(-0.00064\right)^2+\left(-8.2 \times 10^{-5}\right)^2}\\
&=0.00773\ \text{V}\\
\\
\zeta&=\left (0.111 \pm 0.00773 \right )\ \text{V}
\end{aligned}
\end{equation}

Rendered results:

Rendered results

🙌 Acknowledgements

📄 License

This project is licensed under the MIT License.

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

uncertainty_calculator-0.2.0.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

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

uncertainty_calculator-0.2.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file uncertainty_calculator-0.2.0.tar.gz.

File metadata

  • Download URL: uncertainty_calculator-0.2.0.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for uncertainty_calculator-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1f45844ad1e2e080b899c1784fe229eac075d95030907cde91c55a0d3c0dd909
MD5 1a2ff88a005d44d2d7007680e3bb7d84
BLAKE2b-256 dbaca873a49616b1fd8cbf4a5e186565f1b2d5a4f8a744a7dcb7eb86353f15e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for uncertainty_calculator-0.2.0.tar.gz:

Publisher: publish.yml on FridrichMethod/UncertaintyCalculator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file uncertainty_calculator-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for uncertainty_calculator-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b82ce889f4389c17e9e9b0fc4b6ebd8507eecc338b7dc0fc02524ad90ea9431d
MD5 892c9448dfeced1260bfe2594d792bf1
BLAKE2b-256 f4f85d07df7ef2519553357b82a789be7b79d551eb4ec22237561caddbae4164

See more details on using hashes here.

Provenance

The following attestation bundles were made for uncertainty_calculator-0.2.0-py3-none-any.whl:

Publisher: publish.yml on FridrichMethod/UncertaintyCalculator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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