From beb692afd7dc363b8a2b6fdd1ae001d5986d71bd Mon Sep 17 00:00:00 2001 From: Benjamin Fischer Date: Fri, 18 Feb 2022 17:32:43 +0100 Subject: [PATCH 01/10] fix bad values for high (abs) eta --- src/vector/_compute/spatial/eta.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/vector/_compute/spatial/eta.py b/src/vector/_compute/spatial/eta.py index 35a55777..ba8612b7 100644 --- a/src/vector/_compute/spatial/eta.py +++ b/src/vector/_compute/spatial/eta.py @@ -28,9 +28,7 @@ def xy_z(lib, x, y, z): - return lib.nan_to_num( - lib.arctanh(z / lib.sqrt(x**2 + y**2 + z**2)), nan=0.0 - ) * lib.absolute(lib.sign(z)) + return lib.where(z, lib.arcsinh(z / lib.sqrt(x**2 + y**2)), z) def xy_theta(lib, x, y, theta): @@ -42,9 +40,7 @@ def xy_eta(lib, x, y, eta): def rhophi_z(lib, rho, phi, z): - return lib.nan_to_num( - lib.arctanh(z / lib.sqrt(rho**2 + z**2)), nan=0.0 - ) * lib.absolute(lib.sign(z)) + return lib.where(z, lib.arcsinh(z / rho), z) def rhophi_theta(lib, rho, phi, theta): From 3f66775b3196ffd907135f5a195365f28889dc12 Mon Sep 17 00:00:00 2001 From: Benjamin Fischer Date: Fri, 18 Feb 2022 18:59:54 +0100 Subject: [PATCH 02/10] fix awkward backend test --- tests/backends/test_awkward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/backends/test_awkward.py b/tests/backends/test_awkward.py index f17fb944..8ac37d5f 100644 --- a/tests/backends/test_awkward.py +++ b/tests/backends/test_awkward.py @@ -93,7 +93,7 @@ def test_projection(): { "rho": 6.4031242374328485, "phi": 0.8960553845713439, - "eta": 0.8361481196083127, + "eta": 0.8361481196083128, "tau": 0, "wow": 123, } From 68e935b35449835cd3de28fedf4c6e46a7e950a9 Mon Sep 17 00:00:00 2001 From: Benjamin Fischer Date: Fri, 18 Feb 2022 19:22:06 +0100 Subject: [PATCH 03/10] avoid where function --- src/vector/_compute/spatial/eta.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vector/_compute/spatial/eta.py b/src/vector/_compute/spatial/eta.py index ba8612b7..2856593a 100644 --- a/src/vector/_compute/spatial/eta.py +++ b/src/vector/_compute/spatial/eta.py @@ -28,7 +28,7 @@ def xy_z(lib, x, y, z): - return lib.where(z, lib.arcsinh(z / lib.sqrt(x**2 + y**2)), z) + return lib.arcsinh(z / lib.sqrt(x**2 + y**2)) def xy_theta(lib, x, y, theta): @@ -40,7 +40,7 @@ def xy_eta(lib, x, y, eta): def rhophi_z(lib, rho, phi, z): - return lib.where(z, lib.arcsinh(z / rho), z) + return lib.arcsinh(z / rho) def rhophi_theta(lib, rho, phi, theta): From d316af47cc3be78c0e74d6d05b5b028af24d5dcf Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Fri, 18 Feb 2022 12:34:10 -0600 Subject: [PATCH 04/10] Allow these functions in compute methods. --- tests/test_compute_features.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_compute_features.py b/tests/test_compute_features.py index 0a446c78..d66b70a6 100644 --- a/tests/test_compute_features.py +++ b/tests/test_compute_features.py @@ -371,6 +371,9 @@ def analyze_callable(node, context): "arctan2", "sinh", "cosh", + "tanh", + "arcsinh", + "arccosh", "arctanh", "isclose", ] From 33f241842e788abadff6ba25037211afd07e45b6 Mon Sep 17 00:00:00 2001 From: Benjamin Fischer Date: Fri, 18 Feb 2022 19:39:29 +0100 Subject: [PATCH 05/10] fix handling for z=0 --- src/vector/_compute/spatial/eta.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/vector/_compute/spatial/eta.py b/src/vector/_compute/spatial/eta.py index 2856593a..b44a947b 100644 --- a/src/vector/_compute/spatial/eta.py +++ b/src/vector/_compute/spatial/eta.py @@ -4,6 +4,7 @@ # or https://github.com/scikit-hep/vector for details. import typing +from math import inf, nan """ .. code-block:: python @@ -28,7 +29,12 @@ def xy_z(lib, x, y, z): - return lib.arcsinh(z / lib.sqrt(x**2 + y**2)) + return lib.nan_to_num( + lib.arcsinh(z / lib.sqrt(x**2 + y**2)), + nan=lib.nan_to_num((z != 0) * inf, posinf=nan), + posinf=inf, + neginf=-inf, + ) def xy_theta(lib, x, y, theta): @@ -40,7 +46,12 @@ def xy_eta(lib, x, y, eta): def rhophi_z(lib, rho, phi, z): - return lib.arcsinh(z / rho) + return lib.nan_to_num( + lib.arcsinh(z / rho), + nan=lib.nan_to_num((z != 0) * inf, posinf=nan), + posinf=inf, + neginf=-inf, + ) def rhophi_theta(lib, rho, phi, theta): From 12ae4d6c7117f85c18b67bfe6ddffcd3fb391e26 Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Fri, 18 Feb 2022 13:41:19 -0600 Subject: [PATCH 06/10] This solves the Numba issue; the Awkward one will need an Awkward fix (update ak.nan_to_num). --- src/vector/_backends/numba_object.py | 35 ++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/vector/_backends/numba_object.py b/src/vector/_backends/numba_object.py index 1918e692..cd57234d 100644 --- a/src/vector/_backends/numba_object.py +++ b/src/vector/_backends/numba_object.py @@ -52,16 +52,31 @@ def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None): if isinstance(x, numba.types.Array): - def nan_to_num_impl(x, copy=True, nan=0.0, posinf=None, neginf=None): - out = numpy.copy(x).reshape(-1) if copy else x.reshape(-1) - for i in range(len(out)): - if numpy.isnan(out[i]): - out[i] = nan - if posinf is not None and numpy.isinf(out[i]) and out[i] > 0: - out[i] = posinf - if neginf is not None and numpy.isinf(out[i]) and out[i] < 0: - out[i] = neginf - return out.reshape(x.shape) + if isinstance(nan, numba.types.Array): + + def nan_to_num_impl(x, copy=True, nan=0.0, posinf=None, neginf=None): + out = numpy.copy(x).reshape(-1) if copy else x.reshape(-1) + for i in range(len(out)): + if numpy.isnan(out[i]): + out[i] = nan[i] + if posinf is not None and numpy.isinf(out[i]) and out[i] > 0: + out[i] = posinf + if neginf is not None and numpy.isinf(out[i]) and out[i] < 0: + out[i] = neginf + return out.reshape(x.shape) + + else: + + def nan_to_num_impl(x, copy=True, nan=0.0, posinf=None, neginf=None): + out = numpy.copy(x).reshape(-1) if copy else x.reshape(-1) + for i in range(len(out)): + if numpy.isnan(out[i]): + out[i] = nan + if posinf is not None and numpy.isinf(out[i]) and out[i] > 0: + out[i] = posinf + if neginf is not None and numpy.isinf(out[i]) and out[i] < 0: + out[i] = neginf + return out.reshape(x.shape) else: From d694506da4d619340d19438e033633c09afaf32d Mon Sep 17 00:00:00 2001 From: Benjamin Fischer Date: Wed, 23 Feb 2022 18:14:39 +0100 Subject: [PATCH 07/10] another attempt at using where - should not break numba - should decay 0-rank array to scalar --- src/vector/_compute/spatial/eta.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/vector/_compute/spatial/eta.py b/src/vector/_compute/spatial/eta.py index b44a947b..bf9b0434 100644 --- a/src/vector/_compute/spatial/eta.py +++ b/src/vector/_compute/spatial/eta.py @@ -29,12 +29,7 @@ def xy_z(lib, x, y, z): - return lib.nan_to_num( - lib.arcsinh(z / lib.sqrt(x**2 + y**2)), - nan=lib.nan_to_num((z != 0) * inf, posinf=nan), - posinf=inf, - neginf=-inf, - ) + return lib.where(z == 0, lib.arcsinh(z / lib.sqrt(x**2 + y**2)), z) * 1 def xy_theta(lib, x, y, theta): @@ -46,12 +41,7 @@ def xy_eta(lib, x, y, eta): def rhophi_z(lib, rho, phi, z): - return lib.nan_to_num( - lib.arcsinh(z / rho), - nan=lib.nan_to_num((z != 0) * inf, posinf=nan), - posinf=inf, - neginf=-inf, - ) + return lib.where(z == 0, lib.arcsinh(z / rho), z) * 1 def rhophi_theta(lib, rho, phi, theta): From c0b0f0958a03e56915a5b979138c8216a7770296 Mon Sep 17 00:00:00 2001 From: Benjamin Fischer Date: Wed, 23 Feb 2022 18:17:41 +0100 Subject: [PATCH 08/10] fix inverted condition --- src/vector/_compute/spatial/eta.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vector/_compute/spatial/eta.py b/src/vector/_compute/spatial/eta.py index bf9b0434..5b65bfd0 100644 --- a/src/vector/_compute/spatial/eta.py +++ b/src/vector/_compute/spatial/eta.py @@ -29,7 +29,7 @@ def xy_z(lib, x, y, z): - return lib.where(z == 0, lib.arcsinh(z / lib.sqrt(x**2 + y**2)), z) * 1 + return lib.where(z != 0, lib.arcsinh(z / lib.sqrt(x**2 + y**2)), z) * 1 def xy_theta(lib, x, y, theta): @@ -41,7 +41,7 @@ def xy_eta(lib, x, y, eta): def rhophi_z(lib, rho, phi, z): - return lib.where(z == 0, lib.arcsinh(z / rho), z) * 1 + return lib.where(z != 0, lib.arcsinh(z / rho), z) * 1 def rhophi_theta(lib, rho, phi, theta): From 7bff1902916a1a75b1284bf58020a45b63d38e11 Mon Sep 17 00:00:00 2001 From: Benjamin Fischer Date: Wed, 23 Feb 2022 18:21:49 +0100 Subject: [PATCH 09/10] remove unused imports --- src/vector/_compute/spatial/eta.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/vector/_compute/spatial/eta.py b/src/vector/_compute/spatial/eta.py index 5b65bfd0..1778fa76 100644 --- a/src/vector/_compute/spatial/eta.py +++ b/src/vector/_compute/spatial/eta.py @@ -4,7 +4,6 @@ # or https://github.com/scikit-hep/vector for details. import typing -from math import inf, nan """ .. code-block:: python From dee68b1a65e4ea8b6a77f03a1c970728fe19e61a Mon Sep 17 00:00:00 2001 From: Benjamin Fischer Date: Wed, 2 Mar 2022 13:24:43 +0100 Subject: [PATCH 10/10] revert to nan_to_num implementation --- src/vector/_compute/spatial/eta.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/vector/_compute/spatial/eta.py b/src/vector/_compute/spatial/eta.py index 1778fa76..b44a947b 100644 --- a/src/vector/_compute/spatial/eta.py +++ b/src/vector/_compute/spatial/eta.py @@ -4,6 +4,7 @@ # or https://github.com/scikit-hep/vector for details. import typing +from math import inf, nan """ .. code-block:: python @@ -28,7 +29,12 @@ def xy_z(lib, x, y, z): - return lib.where(z != 0, lib.arcsinh(z / lib.sqrt(x**2 + y**2)), z) * 1 + return lib.nan_to_num( + lib.arcsinh(z / lib.sqrt(x**2 + y**2)), + nan=lib.nan_to_num((z != 0) * inf, posinf=nan), + posinf=inf, + neginf=-inf, + ) def xy_theta(lib, x, y, theta): @@ -40,7 +46,12 @@ def xy_eta(lib, x, y, eta): def rhophi_z(lib, rho, phi, z): - return lib.where(z != 0, lib.arcsinh(z / rho), z) * 1 + return lib.nan_to_num( + lib.arcsinh(z / rho), + nan=lib.nan_to_num((z != 0) * inf, posinf=nan), + posinf=inf, + neginf=-inf, + ) def rhophi_theta(lib, rho, phi, theta):