Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method error_contribution_from. #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

natfarleydev
Copy link

The function error_contribution_from allows developers to discover the complete contribution from a set of tags specified by a predicate function.

This is part way to address #70. Since Python is awesome (or uncertainties' authors are, reader's choice), tuples can be used for tags instead of strings. This means that with the new function error_contribution_from one can:

from uncertainties import *
x = ufloat(1, 1, tag=("x", "statistical"))
y = ufloat(1, 1, tag=("y", "statistical"))
z = ufloat(1, 1, tag=("z", "systematic"))

w = x + y + z
w_systematic_error = w.error_contribution_from(lambda t: "systematic" in t)
w_statistical_error = w.error_contribution_from(lambda t: "statistical" in t)

Note

This pull request does not (yet!) include documentation changes. These will be added when the code is agreed upon.

@natfarleydev
Copy link
Author

@lebigot Any thoughts?

@lebigot
Copy link
Collaborator

lebigot commented Feb 21, 2018

Sorry for the delay in responding.

This function is useful, but it can really be a simple one-liner, so I prefer not to include it, because I want to keep the module as small as possible while making even powerful things easy enough to implement (like here):

def error_contribution_from(self, filter_tag):
    """
    Return the error contribution from specific error components.

    Return the error contribution from error components for
    which filter_tag(tag) is true.
    """
    return sqrt(sum(error**2 for (var, error) in self.error_components().iteritems() 
                    if filter_tag(var.tag)))

@lebigot lebigot closed this Feb 21, 2018
@natfarleydev
Copy link
Author

I can see where you're coming from (keeping libraries small is good) but I think a function like this makes uncertainties much more valuable. Relying on the user to figure this out and reimplement it every time instead of providing it in the interface is an unnecessary barrier to entry.

I'm not wedded to the idea of using a function like this; I think there must be a cleaner/clearer way to implement feature. Either way, I think it's worth thinking about how to expose this functionality straight from a ufloat. (Maybe through multiple errors per value?)

I use this feature every day for my analysis and it's extremely useful (saving a lot of time).

@natfarleydev
Copy link
Author

Perhaps an extras library would be good for stuff like this? Then there can be these helpful functions without clogging up the main library.

@lebigot
Copy link
Collaborator

lebigot commented Feb 21, 2018

I like the idea of extending the UFloat class with this, thanks! I'm reopening this pull request as a kind of reminder.

@lebigot lebigot reopened this Feb 21, 2018
@codecov-io
Copy link

codecov-io commented Feb 21, 2018

Codecov Report

Merging #72 into master will decrease coverage by 0.28%.
The diff coverage is 16.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #72      +/-   ##
==========================================
- Coverage   94.62%   94.33%   -0.29%     
==========================================
  Files          12       12              
  Lines        1954     1960       +6     
==========================================
  Hits         1849     1849              
- Misses        105      111       +6
Impacted Files Coverage Δ
uncertainties/core.py 96.31% <16.66%> (-0.72%) ⬇️
uncertainties/umath_core.py 98.91% <0%> (-1.09%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8c7a833...842a45b. Read the comment docs.

The function error_contribution_from allows developers to discover the complete
contribution from a set of tags specified by a predicate function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants