From 017d2c7561b68922fc0cd2da81c31b8feb893386 Mon Sep 17 00:00:00 2001 From: David J Pugh <6003255+djpugh@users.noreply.github.com> Date: Sat, 6 Nov 2021 18:00:58 +0000 Subject: [PATCH 01/13] Update numpy_func.py --- pint/numpy_func.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pint/numpy_func.py b/pint/numpy_func.py index 38aab1ae5..b67642767 100644 --- a/pint/numpy_func.py +++ b/pint/numpy_func.py @@ -709,6 +709,22 @@ def _prod(a, *args, **kwargs): return registry.Quantity(result, units) +@implements("broadcast_arrays", "function") +def _broadcast_arrays(*args, **kwargs): + # Simply need to map input units to onto list of outputs + input_units = [] + unitless_args = [] + for x in args: + if hasattr(x, 'units'): + input_units.append(x.units) + unitless_args.append(x.m) + else: + input_units.append(1) + unitless_args.append(x) + + res = np.broadcast_arrays(*unitless_args, **kwargs) + return [out * unit for out, unit in zip(res, input_units)] + # Implement simple matching-unit or stripped-unit functions based on signature From 8199cd25c6c0342d4e9ffce6cafa1eba3ba0ae42 Mon Sep 17 00:00:00 2001 From: David J Pugh <6003255+djpugh@users.noreply.github.com> Date: Sat, 6 Nov 2021 18:04:19 +0000 Subject: [PATCH 02/13] Updating tests --- pint/testsuite/test_numpy.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index ce337b24b..10a77428f 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -184,6 +184,13 @@ def test_broadcast_to(self): np.array([[2, 4], [2, 4]]) * self.ureg.m, ) + @helpers.requires_array_function_protocol() + def test_broadcast_arrays(self): + helpers.assert_quantity_equal( + np.broadcast_array(self.q[:, 1], np.array([1,2])), + [np.array([[2, 4], [2, 4]]) * self.ureg.m, np.array([1, 2])] + ) + @helpers.requires_array_function_protocol() def test_expand_dims(self): helpers.assert_quantity_equal( From a36740fa5e2ecbe7716c6dbb5847095b03d9cdd4 Mon Sep 17 00:00:00 2001 From: David J Pugh <6003255+djpugh@users.noreply.github.com> Date: Sat, 6 Nov 2021 18:11:02 +0000 Subject: [PATCH 03/13] Update test_numpy.py --- pint/testsuite/test_numpy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 10a77428f..9b88212ac 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -187,7 +187,7 @@ def test_broadcast_to(self): @helpers.requires_array_function_protocol() def test_broadcast_arrays(self): helpers.assert_quantity_equal( - np.broadcast_array(self.q[:, 1], np.array([1,2])), + np.broadcast_arrays(self.q[:, 1], np.array([1,2])), [np.array([[2, 4], [2, 4]]) * self.ureg.m, np.array([1, 2])] ) From 67857b87b3bb114aba852764959ac8e055d61750 Mon Sep 17 00:00:00 2001 From: David J Pugh <6003255+djpugh@users.noreply.github.com> Date: Sat, 6 Nov 2021 18:11:25 +0000 Subject: [PATCH 04/13] Update CHANGES --- CHANGES | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index bd36bc515..c54e1fb49 100644 --- a/CHANGES +++ b/CHANGES @@ -4,7 +4,7 @@ Pint Changelog 0.19 (unreleased) ----------------- -- Nothing changed yet. +- Implement numpy broadcast_array (Related to issue #981) 0.18 (2021-10-26) From 52fdb8bf680d5d3367ea82594529666e4f44e3ac Mon Sep 17 00:00:00 2001 From: David J Pugh <6003255+djpugh@users.noreply.github.com> Date: Sat, 6 Nov 2021 18:15:13 +0000 Subject: [PATCH 05/13] Update test_numpy.py --- pint/testsuite/test_numpy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 9b88212ac..4a7248897 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -187,10 +187,10 @@ def test_broadcast_to(self): @helpers.requires_array_function_protocol() def test_broadcast_arrays(self): helpers.assert_quantity_equal( - np.broadcast_arrays(self.q[:, 1], np.array([1,2])), + np.broadcast_arrays(self.q[:, 1], np.array([1, 2])), [np.array([[2, 4], [2, 4]]) * self.ureg.m, np.array([1, 2])] ) - + @helpers.requires_array_function_protocol() def test_expand_dims(self): helpers.assert_quantity_equal( From 05b36332945707ff422bde67a9630cf7d2b2efc6 Mon Sep 17 00:00:00 2001 From: David J Pugh <6003255+djpugh@users.noreply.github.com> Date: Sat, 6 Nov 2021 18:18:29 +0000 Subject: [PATCH 06/13] Black changes --- pint/numpy_func.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint/numpy_func.py b/pint/numpy_func.py index b67642767..f07f48805 100644 --- a/pint/numpy_func.py +++ b/pint/numpy_func.py @@ -715,7 +715,7 @@ def _broadcast_arrays(*args, **kwargs): input_units = [] unitless_args = [] for x in args: - if hasattr(x, 'units'): + if hasattr(x, "units"): input_units.append(x.units) unitless_args.append(x.m) else: From 7525dea4304acf558c9285e3b93b7262923686a1 Mon Sep 17 00:00:00 2001 From: David J Pugh <6003255+djpugh@users.noreply.github.com> Date: Sat, 6 Nov 2021 18:19:00 +0000 Subject: [PATCH 07/13] Update test_numpy.py --- pint/testsuite/test_numpy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 4a7248897..abf5860e6 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -188,7 +188,7 @@ def test_broadcast_to(self): def test_broadcast_arrays(self): helpers.assert_quantity_equal( np.broadcast_arrays(self.q[:, 1], np.array([1, 2])), - [np.array([[2, 4], [2, 4]]) * self.ureg.m, np.array([1, 2])] + [np.array([[2, 4], [2, 4]]) * self.ureg.m, np.array([1, 2])], ) @helpers.requires_array_function_protocol() From 0035de82a7fceb04b630300fa25a880c6953c368 Mon Sep 17 00:00:00 2001 From: David J Pugh <6003255+djpugh@users.noreply.github.com> Date: Sat, 6 Nov 2021 18:19:45 +0000 Subject: [PATCH 08/13] Update numpy_func.py --- pint/numpy_func.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pint/numpy_func.py b/pint/numpy_func.py index f07f48805..247be2071 100644 --- a/pint/numpy_func.py +++ b/pint/numpy_func.py @@ -725,6 +725,7 @@ def _broadcast_arrays(*args, **kwargs): res = np.broadcast_arrays(*unitless_args, **kwargs) return [out * unit for out, unit in zip(res, input_units)] + # Implement simple matching-unit or stripped-unit functions based on signature From f2d31743a7819073da00d3b819ead3db64648da8 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Sun, 12 May 2024 21:21:38 +0100 Subject: [PATCH 09/13] Update CHANGES --- CHANGES | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index ffd95bc78..9a7e056d2 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,7 @@ Pint Changelog 0.24 (unreleased) ----------------- +- Implement numpy broadcast_array (Related to issue #981) - Nothing changed yet. @@ -53,7 +54,6 @@ Pint Changelog 0.21 (2023-05-01) ----------------- -- Implement numpy broadcast_array (Related to issue #981) - Add PEP621/631 support. (Issue #1647) - Exposed matplotlib unit formatter (PR #1703) From d6258c20a1d43269383f67443698fdf90fa6f249 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Sun, 12 May 2024 21:25:38 +0100 Subject: [PATCH 10/13] Update numpy_func.py --- pint/facets/numpy/numpy_func.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint/facets/numpy/numpy_func.py b/pint/facets/numpy/numpy_func.py index 71131d8a0..cfd55e027 100644 --- a/pint/facets/numpy/numpy_func.py +++ b/pint/facets/numpy/numpy_func.py @@ -757,7 +757,7 @@ def _broadcast_arrays(*args, **kwargs): res = np.broadcast_arrays(*unitless_args, **kwargs) return [out * unit for out, unit in zip(res, input_units)] - + # Handle mutliplicative functions separately to deal with non-multiplicative units def _base_unit_if_needed(a): if a._is_multiplicative: From b81b900a7851f8f935038551f8740024a8e8a343 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Sun, 12 May 2024 22:47:26 +0100 Subject: [PATCH 11/13] Update numpy_func.py --- pint/facets/numpy/numpy_func.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pint/facets/numpy/numpy_func.py b/pint/facets/numpy/numpy_func.py index cfd55e027..554be9a7a 100644 --- a/pint/facets/numpy/numpy_func.py +++ b/pint/facets/numpy/numpy_func.py @@ -1007,7 +1007,6 @@ def implementation(a, *args, **kwargs): "vstack", "dstack", "column_stack", - "broadcast_arrays", ): implement_func( "function", func_str, input_units="all_consistent", output_unit="match_input" From e4424117c77f94709cab325e6c78ec0e7342879e Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Sun, 12 May 2024 22:48:59 +0100 Subject: [PATCH 12/13] Update test_numpy.py --- pint/testsuite/test_numpy.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 5409d4c15..b950d227d 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -186,13 +186,6 @@ def test_broadcast_to(self): np.array([[2, 4], [2, 4]]) * self.ureg.m, ) - @helpers.requires_array_function_protocol() - def test_broadcast_arrays(self): - helpers.assert_quantity_equal( - np.broadcast_arrays(self.q[:, 1], np.array([1, 2])), - [np.array([[2, 4], [2, 4]]) * self.ureg.m, np.array([1, 2])], - ) - @helpers.requires_array_function_protocol() def test_expand_dims(self): helpers.assert_quantity_equal( @@ -294,6 +287,11 @@ def test_broadcast_arrays(self): result = np.broadcast_arrays(x, y, subok=True) helpers.assert_quantity_equal(result, expected) + + helpers.assert_quantity_equal( + np.broadcast_arrays(self.q[:, 1], np.array([1, 2])), + [np.array([[2, 4], [2, 4]]) * self.ureg.m, np.array([1, 2])], + ) def test_roll(self): helpers.assert_quantity_equal( From 87c18c74c5d35b181866d7bbb5d35161b71789e6 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Sun, 12 May 2024 22:50:24 +0100 Subject: [PATCH 13/13] lint --- pint/testsuite/test_numpy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index b950d227d..d7221fa40 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -287,7 +287,7 @@ def test_broadcast_arrays(self): result = np.broadcast_arrays(x, y, subok=True) helpers.assert_quantity_equal(result, expected) - + helpers.assert_quantity_equal( np.broadcast_arrays(self.q[:, 1], np.array([1, 2])), [np.array([[2, 4], [2, 4]]) * self.ureg.m, np.array([1, 2])],