Skip to content

Commit

Permalink
offsets as deltas
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed May 21, 2024
1 parent f2e4081 commit b161850
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 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
10 changes: 7 additions & 3 deletions pint/testsuite/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ def test_nanmedian_numpy_func(self):
assert np.nanmedian(self.q_nan) == 2 * self.ureg.m

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):
Expand All @@ -866,14 +866,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

0 comments on commit b161850

Please sign in to comment.