Skip to content

Commit

Permalink
Update uncertainty_containers.py
Browse files Browse the repository at this point in the history
  • Loading branch information
varchasgopalaswamy authored Nov 18, 2024
1 parent a099553 commit a0bdf95
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions auto_uncertainties/uncertainty/uncertainty_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,49 +951,65 @@ def _check_units(value, err) -> tuple[Any, Any, Any]:

def nominal_values(x) -> UType:
"""Return the central value of an `Uncertainty` object if it is one, otherwise returns the object."""
if hasattr(x, "units"):
x_used = x.m
x_units = x.units
else:
x_used = x
x_units = 1
# Is an Uncertainty
if hasattr(x, "_nom"):
return x.value
if hasattr(x_used, "_nom"):
ret_val = x_used.value
else:
if np.ndim(x) > 0:
if np.ndim(x_used) > 0:
try:
x2 = Uncertainty.from_sequence(x)
except Exception:
return x
ret_val = x_used
else:
return x2.value
ret_val = x2.value
else:
try:
x2 = Uncertainty(x)
x2 = Uncertainty(x_used)
except Exception:
return x
ret_val = x_used
else:
if isinstance(x2, float):
return x2
ret_val = x2
else:
return x2.value
ret_val = x2.value

return ret_val * x_units

def std_devs(x) -> UType:
"""Return the uncertainty of an `Uncertainty` object if it is one, otherwise returns zero."""
# Is an Uncertainty
if hasattr(x, "_err"):
return x.error
if hasattr(x, "units"):
x_used = x.m
x_units = x.units
else:
x_used = x
x_units = 1
# Is an Uncertainty
if hasattr(x_used, "_nom"):
ret_val = x_used.error
else:
if np.ndim(x) > 0:
if np.ndim(x_used) > 0:
try:
x2 = Uncertainty.from_sequence(x)
except Exception:
return np.zeros_like(x)
ret_val = np.zeros_like(x)
else:
return x2.error
ret_val = x2.error
else:
try:
x2 = Uncertainty(x)
x2 = Uncertainty(x_used)
except Exception:
return 0
ret_val = 0
else:
if isinstance(x2, float):
return 0
ret_val = 0
else:
return x2.error
ret_val = x2.error

return ret_val * x_units

0 comments on commit a0bdf95

Please sign in to comment.