Skip to content

Commit 942a236

Browse files
committed
[Python][Hist] Add extra checks for the vectorized fill
1 parent 9976d3b commit 942a236

File tree

2 files changed

+19
-22
lines changed
  • bindings/pyroot/pythonizations/python/ROOT/_pythonization

2 files changed

+19
-22
lines changed

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_th1.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,16 @@ def _FillWithArrayTH1(self, *args):
206206
Raises:
207207
- ValueError: If weights length doesn't match data length
208208
"""
209-
import collections.abc
210-
211-
# If the first argument has no len() method, we don't even need to consider
212-
# the array code path.
213-
if not isinstance(args[0], collections.abc.Sized):
214-
return self._Fill(*args)
215-
216209
import numpy as np
217210

218-
data = np.asanyarray(args[0], dtype=np.float64)
219-
n = len(data)
211+
# Try to convert the first argument to a numpy array
212+
# if it fails, we call the original Fill
213+
try:
214+
data = np.asanyarray(args[0], dtype=np.float64)
215+
n = len(data)
216+
except Exception:
217+
# Not convertible
218+
return self._Fill(*args)
220219

221220
if len(args) >= 2 and args[1] is not None:
222221
weights = np.asanyarray(args[1], dtype=np.float64)

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_th2.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,23 @@ def _FillWithArrayTH2(self, *args):
3232
Raises:
3333
- ValueError: If x, y, and/or weights do not have matching lengths
3434
"""
35-
import collections.abc
36-
37-
# If the first argument has no len() method, we don't even need to consider
38-
# the array code path.
39-
if not isinstance(args[0], collections.abc.Sized):
40-
return self._Fill(*args)
41-
35+
# If there are less than 2 arguments, cannot do vectorized Fill
4236
if len(args) < 2:
43-
raise ValueError("TH2.Fill requires at least x and y array-like arguments")
37+
return self._Fill(*args)
4438

4539
import numpy as np
4640

47-
x = np.asanyarray(args[0], dtype=np.float64)
48-
y = np.asanyarray(args[1], dtype=np.float64)
41+
try:
42+
x = np.asanyarray(args[0], dtype=np.float64)
43+
y = np.asanyarray(args[1], dtype=np.float64)
4944

50-
if len(x) != len(y):
51-
raise ValueError(f"Length mismatch: x length ({len(x)}) != y length ({len(y)})")
45+
if len(x) != len(y):
46+
raise ValueError(f"Length mismatch: x length ({len(x)}) != y length ({len(y)})")
5247

53-
n = len(x)
48+
n = len(x)
49+
except Exception:
50+
# Not convertible
51+
return self._Fill(*args)
5452

5553
if len(args) >= 3 and args[2] is not None:
5654
weights = np.asanyarray(args[2], dtype=np.float64)

0 commit comments

Comments
 (0)