Skip to content

Commit 69d0b1d

Browse files
bfisjpivarski
andauthored
fix bad values for high (abs) eta (#172)
* fix bad values for high (abs) eta * fix awkward backend test * avoid where function * Allow these functions in compute methods. * fix handling for z=0 * This solves the Numba issue; the Awkward one will need an Awkward fix (update ak.nan_to_num). * another attempt at using where - should not break numba - should decay 0-rank array to scalar * fix inverted condition * remove unused imports * revert to nan_to_num implementation Co-authored-by: Jim Pivarski <[email protected]>
1 parent 5cd6324 commit 69d0b1d

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

src/vector/_backends/numba_object.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,31 @@
5252
def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):
5353
if isinstance(x, numba.types.Array):
5454

55-
def nan_to_num_impl(x, copy=True, nan=0.0, posinf=None, neginf=None):
56-
out = numpy.copy(x).reshape(-1) if copy else x.reshape(-1)
57-
for i in range(len(out)):
58-
if numpy.isnan(out[i]):
59-
out[i] = nan
60-
if posinf is not None and numpy.isinf(out[i]) and out[i] > 0:
61-
out[i] = posinf
62-
if neginf is not None and numpy.isinf(out[i]) and out[i] < 0:
63-
out[i] = neginf
64-
return out.reshape(x.shape)
55+
if isinstance(nan, numba.types.Array):
56+
57+
def nan_to_num_impl(x, copy=True, nan=0.0, posinf=None, neginf=None):
58+
out = numpy.copy(x).reshape(-1) if copy else x.reshape(-1)
59+
for i in range(len(out)):
60+
if numpy.isnan(out[i]):
61+
out[i] = nan[i]
62+
if posinf is not None and numpy.isinf(out[i]) and out[i] > 0:
63+
out[i] = posinf
64+
if neginf is not None and numpy.isinf(out[i]) and out[i] < 0:
65+
out[i] = neginf
66+
return out.reshape(x.shape)
67+
68+
else:
69+
70+
def nan_to_num_impl(x, copy=True, nan=0.0, posinf=None, neginf=None):
71+
out = numpy.copy(x).reshape(-1) if copy else x.reshape(-1)
72+
for i in range(len(out)):
73+
if numpy.isnan(out[i]):
74+
out[i] = nan
75+
if posinf is not None and numpy.isinf(out[i]) and out[i] > 0:
76+
out[i] = posinf
77+
if neginf is not None and numpy.isinf(out[i]) and out[i] < 0:
78+
out[i] = neginf
79+
return out.reshape(x.shape)
6580

6681
else:
6782

src/vector/_compute/spatial/eta.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# or https://github.com/scikit-hep/vector for details.
55

66
import typing
7+
from math import inf, nan
78

89
"""
910
.. code-block:: python
@@ -29,8 +30,11 @@
2930

3031
def xy_z(lib, x, y, z):
3132
return lib.nan_to_num(
32-
lib.arctanh(z / lib.sqrt(x**2 + y**2 + z**2)), nan=0.0
33-
) * lib.absolute(lib.sign(z))
33+
lib.arcsinh(z / lib.sqrt(x**2 + y**2)),
34+
nan=lib.nan_to_num((z != 0) * inf, posinf=nan),
35+
posinf=inf,
36+
neginf=-inf,
37+
)
3438

3539

3640
def xy_theta(lib, x, y, theta):
@@ -43,8 +47,11 @@ def xy_eta(lib, x, y, eta):
4347

4448
def rhophi_z(lib, rho, phi, z):
4549
return lib.nan_to_num(
46-
lib.arctanh(z / lib.sqrt(rho**2 + z**2)), nan=0.0
47-
) * lib.absolute(lib.sign(z))
50+
lib.arcsinh(z / rho),
51+
nan=lib.nan_to_num((z != 0) * inf, posinf=nan),
52+
posinf=inf,
53+
neginf=-inf,
54+
)
4855

4956

5057
def rhophi_theta(lib, rho, phi, theta):

tests/backends/test_awkward.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_projection():
9393
{
9494
"rho": 6.4031242374328485,
9595
"phi": 0.8960553845713439,
96-
"eta": 0.8361481196083127,
96+
"eta": 0.8361481196083128,
9797
"tau": 0,
9898
"wow": 123,
9999
}

tests/test_compute_features.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ def analyze_callable(node, context):
371371
"arctan2",
372372
"sinh",
373373
"cosh",
374+
"tanh",
375+
"arcsinh",
376+
"arccosh",
374377
"arctanh",
375378
"isclose",
376379
]

0 commit comments

Comments
 (0)