Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change Log
===================

3.2.4a 2026-January-22
Comment thread
jagerber48 marked this conversation as resolved.
Outdated
-----------------------

Adds:
- Add new internal _derivatives and _error_components properties for internal use without deprecation warning

3.2.4 2026-January-9
-----------------------

Expand Down
4 changes: 2 additions & 2 deletions tests/test_uncertainties.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def test_basic_access_to_data():
# Details on the sources of error:
a = ufloat(-1, 0.001)
y = 2 * x + 3 * x + 2 + a
error_sources = y.error_components()
error_sources = y._error_components()
assert len(error_sources) == 2 # 'a' and 'x'
assert error_sources[x] == 0.05
assert error_sources[a] == 0.001
Expand All @@ -514,7 +514,7 @@ def test_basic_access_to_data():

# Modification of the standard deviation of variables:
x.std_dev = 1
assert y.error_components()[x] == 5 # New error contribution!
assert y._error_components()[x] == 5 # New error contribution!

# Calculated values with uncertainties should not have a settable
# standard deviation:
Expand Down
57 changes: 34 additions & 23 deletions uncertainties/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,8 @@ def nominal_value(self):

############################################################

# Making derivatives a property gives the user a clean syntax,
# which is consistent with derivatives becoming a dictionary.
@property
def derivatives(self):
def _derivatives(self):
"""
Return a mapping from each Variable object on which the function
(self) depends to the value of the derivative with respect to
Expand All @@ -445,13 +443,6 @@ def derivatives(self):

This mapping is cached, for subsequent calls.
"""
warn(
f"{self.__class__.__name__}.derivatives() is deprecated. It will "
f"be removed in a future release.",
FutureWarning,
stacklevel=2,
)

if not self._linear_part.expanded():
self._linear_part.expand()
# Attempts to get the contribution of a variable that the
Expand All @@ -460,20 +451,27 @@ def derivatives(self):

return self._linear_part.linear_combo

# Making derivatives a property gives the user a clean syntax,
# which is consistent with derivatives becoming a dictionary.
@property
def derivatives(self):
"""
Public wrapper for _derivatives.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the historic docstring on the public, not private derivatives property.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved historic docstring to public api.

"""
warn(
f"{self.__class__.__name__}.derivatives() is deprecated. It will "
f"be removed in a future release.",
FutureWarning,
stacklevel=2,
)
return self._derivatives

########################################

# Uncertainties handling:

def error_components(self):
"""
Individual components of the standard deviation of the affine
function (in absolute value), returned as a dictionary with
Variable objects as keys. The returned variables are the
independent variables that the affine function depends on.

This method assumes that the derivatives contained in the
object take scalar values (and are not a tuple, like what
math.frexp() returns, for instance).
Publich wrapper of _error_compents with FutureWarning.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the historic docstring on the public, not private, error_components() method.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved historic docstring to public api.

"""
warn(
f"{self.__class__.__name__}.error_components() is currently an "
Expand All @@ -485,10 +483,23 @@ def error_components(self):
stacklevel=2,
)

return self._error_components()

def _error_components(self):
"""
Individual components of the standard deviation of the affine
function (in absolute value), returned as a dictionary with
Variable objects as keys. The returned variables are the
independent variables that the affine function depends on.

This method assumes that the derivatives contained in the
object take scalar values (and are not a tuple, like what
math.frexp() returns, for instance).
"""
# Calculation of the variance:
error_components = {}

for variable, derivative in self.derivatives.items():
for variable, derivative in self._derivatives.items():
# print "TYPE", type(variable), type(derivative)

# Individual standard error due to variable:
Expand Down Expand Up @@ -523,7 +534,7 @@ def std_dev(self):
# std_dev value (in fact, many intermediate AffineScalarFunc do
# not need to have their std_dev calculated: only the final
# AffineScalarFunc returned to the user does).
return float(sqrt(sum(delta**2 for delta in self.error_components().values())))
return float(sqrt(sum(delta**2 for delta in self._error_components().values())))

# Abbreviation (for formulas, etc.):
s = std_dev
Expand Down Expand Up @@ -920,12 +931,12 @@ def covariance_matrix(nums_with_uncert):

covariance_matrix = []
for i1, expr1 in enumerate(nums_with_uncert, 1):
derivatives1 = expr1.derivatives # Optimization
derivatives1 = expr1._derivatives # Optimization
vars1 = set(derivatives1) # !! Python 2.7+: viewkeys() would work
coefs_expr1 = []

for expr2 in nums_with_uncert[:i1]:
derivatives2 = expr2.derivatives # Optimization
derivatives2 = expr2._derivatives # Optimization
coefs_expr1.append(
sum(
(
Expand Down
6 changes: 3 additions & 3 deletions uncertainties/unumpy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def derivative(u, var):
"""
if isinstance(u, uncert_core.AffineScalarFunc):
try:
return u.derivatives[var]
return u._derivatives[var]
except KeyError:
return 0.0
else:
Expand Down Expand Up @@ -195,7 +195,7 @@ def wrapped_func(arr, *args, **kwargs):
# working with a large number of arrays?
#
# !! set() is only needed for Python 2 compatibility:
variables |= set(element.derivatives.keys())
variables |= set(element._derivatives.keys())

# If the matrix has no variables, then the function value can be
# directly returned:
Expand Down Expand Up @@ -415,7 +415,7 @@ def wrapped_func(array_like, *args, **kwargs):
# floats, etc. might be present
if isinstance(element, uncert_core.AffineScalarFunc):
# !!! set() is only needed for Python 2 compatibility:
variables |= set(element.derivatives.keys())
variables |= set(element._derivatives.keys())

array_nominal = nominal_values(array_version)
# Function value, then derivatives at array_nominal (the
Expand Down