Skip to main content

A helper to calculate the gaussian error propagation.

Project description

Secondary Value

This is a utility to simplify the calculation of values and their uncertaintues from symbolic formulas by using sympy and numpy.

Installation

Just a quick pip install SecondaryValue will do.

Examples

(A Documentation will follow soon. For now: look at the docstrings!)

Basic Usage

from SecondaryValue import SecondaryValue

# create a secondary value
# the argument can be either a string or a sympy expression
x = SecondaryValue("a*b/sqrt(c)")

# Calculate a result value by substituting the keyword arguments
# where a keyword agument may consist of (value, error_1, error_2, ...)
# and (...) stands for any iterable.
result = x(a=(1, 20), b=(2,  30), c=2)

# The calculation returns a numpy array with the length of the longest
# keyword argument above: [value, error_1, error_2]
# For each error_n the uncertainties error_n of the keyword args above are
# used if present. This may be useful to calculate statistical and systemic
# errors in one go.
print(result)
# >> array([ 1.41421356, 35.35533906])

# As a goodie, you can print out the gaussian error distribution in
# symbolic form. (Works best in Jupyter Notebooks)
x.pretty_gauss_propagation('a', 'b', 'c')

Default Values

To reduce boilerplate one can set default substitutions for symbols. This especially usefull for constants.

from SecondaryValue import SecondaryValue

# create a secondary value with default arguments
x = SecondaryValue("a + b", defaults=dict(b=1/2))

# this works because `b` is substituted from the defaults
result = x(b=1/2)
print(result)
# >> 1.0

# As a goodie, you can print out the gaussian error distribution in
# symbolic form. (Works best in Jupyter Notebooks)
x.pretty_gauss_propagation('a', 'b', 'c')

Vectorized Input

SecondaryValue supports vectorized input. As a rule-of-thump: Put the iterable (list, np.array) where you would put scalars.

You can mix scalars and vectors as long as all errors and values are either scalar or have the same length.

from SecondaryValue import SecondaryValue
x = SecondaryValue('a**2+b')

x(a=[[1,2,3]], b=1)
# >> array([ 2.,  5., 10.])

x(a=([1,2,3], 1), b=1)
# >> (array([ 2.,  5., 10.]), array([2., 4., 6.]))

x(a=([1,2,3], [1,2,3]), b=1)
# >> (array([ 2.,  5., 10.]), array([ 2.,  8., 18.]))

x(a=([1,2,3], [1,2,3]), b=([1,2,3], 1))
# >> (array([ 2.,  6., 12.]), array([ 2.23606798,  8.06225775, 18.02775638]))

# THAT DOES NOT WORK:
x(a=([1,2,3], [1,2,3]), b=([1,2], 1))

Dependencies

To make the calculation of complex values easier, one can define dependencies for a SecondaryValue:

from SecondaryValue import SecondaryValue

dep = SecondaryValue('u')
x = SecondaryValue("a + b", dependencies={'a': dep})

# x will now accept u as an additional kwarg and calculate d==dep on the fly
# and return a dictionary containing it as a second return value.
print(x(b=1, u=(1, 2)))
# >> (array([2., 2.]), {'a': array([1., 2.])})

# you can overwrite the dependency calculation
print(x(b=1/2, a=1/2))
# >> 1.0

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

SecondaryValue-0.1.1.tar.gz (4.9 kB view details)

Uploaded Source

File details

Details for the file SecondaryValue-0.1.1.tar.gz.

File metadata

  • Download URL: SecondaryValue-0.1.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for SecondaryValue-0.1.1.tar.gz
Algorithm Hash digest
SHA256 73ea1c7abbed7e35b47963c2c5aa62a49fc9c6e50fc3cd589e157003cf6f7307
MD5 25aa0520a897fddcd688ab93231ed4fb
BLAKE2b-256 a895f69c79c1ce397752bcde45d23eb7750e484b719c3f4bb77ed59a4ffa79cb

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