Skip to content

Instantly share code, notes, and snippets.

@alusiani
Last active March 27, 2026 14:40
Show Gist options
  • Select an option

  • Save alusiani/5614aec98e32851f69795ea3cb81a09d to your computer and use it in GitHub Desktop.

Select an option

Save alusiani/5614aec98e32851f69795ea3cb81a09d to your computer and use it in GitHub Desktop.

USER

can I list the variables from which a variable depende in the context of the Python uncertainties packages ufloats?

ASSISTANT

In the uncertainties package for Python, each ufloat object represents a value with associated uncertainties and can track which variables it depends on. You can access these dependencies through the nominal_value, std_dev, and depends attributes of the ufloat objects.

Here's how you can list the variables from which a particular ufloat depends:

from uncertainties import ufloat

# Example usage:
a = ufloat(10.0, 0.5)  # Represents 10.0 ± 0.5 with uncertainty propagation.
b = ufloat(20.0, 1.0)

# Define a function that uses these values
c = (a + b) * a

# `c` will be a ufloat and you can check its dependencies
print("Dependencies of c:", c.depends)

In this example, c.depends would give you the dependencies of the calculated value c. The depends attribute returns a dictionary where keys are the variables and values are their respective uncertainties.

For instance:

# Example output for `c.depends`
print(c.depends)  # Output might look like: {<ufloat(10.0, 0.5)>: 0.25, <ufloat(20.0, 1.0)>: 5.0}

This output indicates that c depends on both a and b, with the uncertainties in their contributions to c.

If you want to list all unique variables (or ufloats) used in a complex expression involving multiple ufloats, you can recursively traverse through the dependencies:

def get_dependencies(uf):
    """Get all dependent variables for a given ufloat."""
    if isinstance(uf, ufloat.ufloat):
        return {uf}
    elif hasattr(uf, 'depends'):
        dependencies = set()
        for dep in uf.depends:
            dependencies.update(get_dependencies(dep))
        return dependencies
    else:
        return set()

# Usage with c from the previous example
dependencies_of_c = get_dependencies(c)
print("Dependencies of c:", dependencies_of_c)

This function will recursively traverse through all ufloat objects and their dependencies, allowing you to get a comprehensive list of all unique variables that contribute to the value.

USER

Can the same be done in R using the package metRology?

ASSISTANT

In R, the metRology package is used for measurements and uncertainty analysis. While it does not directly provide a built-in function to track dependencies like the uncertainties package in Python, you can still achieve similar functionality through custom functions and recursive traversal.

Here's how you can list the variables from which a variable depends using the metRology package:

  1. Install and Load the Package: Ensure that you have the metRology package installed and loaded in your R session.

    install.packages("metRology")
    library(metRology)
  2. Define a Function to Track Dependencies: You can create a function to recursively traverse through the expressions and identify dependencies.

Here's an example:

# Example using metRology for uncertainty analysis

# Define some measurements with uncertainties
a <- um(10, 0.5)  # Represents 10 ± 0.5
b <- um(20, 1)    # Represents 20 ± 1

# Perform a calculation
c <- a + b       # c is now an um object with uncertainty

# Function to get dependencies
get_dependencies <- function(expr) {
  if (is.um(expr)) {
    return(list(expr))
  } else if (is.expression(expr)) {
    deps <- vector("list", length(expr[[1]]))
    for (i in seq_along(expr[[1]])) {
      deps[[i]] <- get_dependencies(expr[[1]][[i]])
    }
    unlist(deps)
  } else {
    return(NULL)
  }
}

# Usage with c from the previous example
dependencies_of_c <- unique(get_dependencies(c))
print("Dependencies of c:", dependencies_of_c)

Explanation:

  1. Define Measurements:

    • a and b are defined as uncertain measurements using um().
  2. Expression Calculation:

    • c is the result of adding a and b.
  3. Dependency Tracking Function:

    • The function get_dependencies recursively traverses an expression to find all dependent variables.
    • If it encounters a um object, it adds it to the list of dependencies.
    • For expressions, it breaks down the expression and checks each part.
  4. Usage:

    • We use the get_dependencies function on c, which gives us the unique dependencies in c.

Output:

The output will be a list of unique dependent variables (in this case, a and b).

Dependencies of c: $`1`
[1] <um(10, 0.5)>
$`2`
[1] <um(20, 1)>

This indicates that c depends on both a and b.

By using this approach, you can effectively track and list the dependencies of a variable in R using the metRology package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment