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

delta units for std, var #1992

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 4 additions & 6 deletions pint/facets/numpy/numpy_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def get_op_output_unit(unit_op, first_input_units, all_args=None, size=None):
product /= x.units
result_unit = product
elif unit_op == "variance":
result_unit = ((1 * first_input_units + 1 * first_input_units) ** 2).units
result_unit = ((1 * first_input_units - 1 * first_input_units) ** 2).units
elif unit_op == "square":
result_unit = first_input_units**2
elif unit_op == "sqrt":
Expand Down Expand Up @@ -429,7 +429,7 @@ def implementation(*args, **kwargs):
]
copy_units_output_ufuncs = ["ldexp", "fmod", "mod", "remainder"]
op_units_output_ufuncs = {
"var": "square",
"var": "variance",
"multiply": "mul",
"true_divide": "div",
"divide": "div",
Expand All @@ -438,7 +438,7 @@ def implementation(*args, **kwargs):
"cbrt": "cbrt",
"square": "square",
"reciprocal": "reciprocal",
"std": "sum",
"std": "delta",
"sum": "sum",
"cumsum": "sum",
"matmul": "mul",
Expand Down Expand Up @@ -1004,16 +1004,14 @@ def implementation(a, *args, **kwargs):

# Handle functions with output unit defined by operation
for func_str in (
"std",
"nanstd",
"sum",
"nansum",
"cumsum",
"nancumsum",
"linalg.norm",
):
implement_func("function", func_str, input_units=None, output_unit="sum")
for func_str in ("diff", "ediff1d"):
for func_str in ("diff", "ediff1d", "std", "nanstd"):
implement_func("function", func_str, input_units=None, output_unit="delta")
for func_str in ("gradient",):
implement_func("function", func_str, input_units=None, output_unit="delta,div")
Expand Down
10 changes: 8 additions & 2 deletions pint/testsuite/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,10 +851,12 @@ def test_nanmedian_numpy_func(self):

def test_var(self):
assert self.q.var() == 1.25 * self.ureg.m**2
assert self.q_temperature.var() == 1.25 * self.ureg.delta_degC**2

@helpers.requires_array_function_protocol()
def test_var_numpy_func(self):
assert np.var(self.q) == 1.25 * self.ureg.m**2
assert np.var(self.q_temperature) == 1.25 * self.ureg.delta_degC**2

@helpers.requires_array_function_protocol()
def test_nanvar_numpy_func(self):
Expand All @@ -866,14 +868,18 @@ def test_std(self):
helpers.assert_quantity_almost_equal(
self.q.std(), 1.11803 * self.ureg.m, rtol=1e-5
)
helpers.assert_quantity_almost_equal(
self.q_temperature.std(), 1.11803 * self.ureg.delta_degC, rtol=1e-5
)

@helpers.requires_array_function_protocol()
def test_std_numpy_func(self):
helpers.assert_quantity_almost_equal(
np.std(self.q), 1.11803 * self.ureg.m, rtol=1e-5
)
with pytest.raises(OffsetUnitCalculusError):
np.std(self.q_temperature)
helpers.assert_quantity_almost_equal(
np.std(self.q_temperature), 1.11803 * self.ureg.delta_degC, rtol=1e-5
)

def test_cumprod(self):
with pytest.raises(DimensionalityError):
Expand Down
4 changes: 2 additions & 2 deletions pint/testsuite/test_numpy_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ def test_op_output_unit_div(self):

def test_op_output_unit_variance(self):
assert get_op_output_unit("variance", self.ureg.m) == self.ureg.m**2
with pytest.raises(OffsetUnitCalculusError):
get_op_output_unit("variance", self.ureg.degC)
# with pytest.raises(OffsetUnitCalculusError):
assert get_op_output_unit("variance", self.ureg.degC) == self.ureg.delta_degC**2

def test_op_output_unit_square(self):
assert get_op_output_unit("square", self.ureg.m) == self.ureg.m**2
Expand Down
Loading