From b1618509ab1253aa4cfd98629411398ba7437f07 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Tue, 21 May 2024 21:06:45 +0200 Subject: [PATCH 1/3] offsets as deltas --- pint/facets/numpy/numpy_func.py | 6 +++--- pint/testsuite/test_numpy.py | 10 +++++++--- pint/testsuite/test_numpy_func.py | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/pint/facets/numpy/numpy_func.py b/pint/facets/numpy/numpy_func.py index 29724837f..e59205be6 100644 --- a/pint/facets/numpy/numpy_func.py +++ b/pint/facets/numpy/numpy_func.py @@ -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": @@ -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", @@ -438,7 +438,7 @@ def implementation(*args, **kwargs): "cbrt": "cbrt", "square": "square", "reciprocal": "reciprocal", - "std": "sum", + "std": "delta", "sum": "sum", "cumsum": "sum", "matmul": "mul", diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 69c8128c0..19a28b248 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -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): @@ -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): diff --git a/pint/testsuite/test_numpy_func.py b/pint/testsuite/test_numpy_func.py index 979b6ee25..95ebbbcfd 100644 --- a/pint/testsuite/test_numpy_func.py +++ b/pint/testsuite/test_numpy_func.py @@ -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 From 655e3daaf01dba4d571901d228398ddd65b8def0 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Tue, 21 May 2024 21:45:22 +0200 Subject: [PATCH 2/3] offsets as deltas --- pint/facets/numpy/numpy_func.py | 6 +++--- pint/testsuite/test_numpy.py | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pint/facets/numpy/numpy_func.py b/pint/facets/numpy/numpy_func.py index e59205be6..8b95fb322 100644 --- a/pint/facets/numpy/numpy_func.py +++ b/pint/facets/numpy/numpy_func.py @@ -1004,8 +1004,6 @@ def implementation(a, *args, **kwargs): # Handle functions with output unit defined by operation for func_str in ( - "std", - "nanstd", "sum", "nansum", "cumsum", @@ -1013,7 +1011,9 @@ def implementation(a, *args, **kwargs): "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") diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 19a28b248..e4059ccf4 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -850,11 +850,14 @@ 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): 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): From cf6c27686dadec9bded36c549d70023d56c19c0d Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Tue, 21 May 2024 21:51:27 +0200 Subject: [PATCH 3/3] lint --- pint/facets/numpy/numpy_func.py | 4 +--- pint/testsuite/test_numpy.py | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/pint/facets/numpy/numpy_func.py b/pint/facets/numpy/numpy_func.py index 8b95fb322..2ba2a663a 100644 --- a/pint/facets/numpy/numpy_func.py +++ b/pint/facets/numpy/numpy_func.py @@ -1011,9 +1011,7 @@ def implementation(a, *args, **kwargs): "linalg.norm", ): implement_func("function", func_str, input_units=None, output_unit="sum") -for func_str in ("diff", "ediff1d", - "std", - "nanstd",): +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") diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index e4059ccf4..fbe506c26 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -858,7 +858,6 @@ 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): helpers.assert_quantity_almost_equal( @@ -879,7 +878,7 @@ def test_std_numpy_func(self): np.std(self.q), 1.11803 * self.ureg.m, rtol=1e-5 ) helpers.assert_quantity_almost_equal( - np.std(self.q_temperature) , 1.11803 * self.ureg.delta_degC, rtol=1e-5 + np.std(self.q_temperature), 1.11803 * self.ureg.delta_degC, rtol=1e-5 ) def test_cumprod(self):