Skip to content

Commit 09910e6

Browse files
committed
[Python] Fix RDF Pythonization tests with builtin_llvm=OFF
On my machine, the RDF pythonization tests don't run when I build ROOT with `builtin_llvm=OFF`. There is a crash unless I do `import numba` in the beginning. I guess it's not a priority to understand the underlying problem because not many people build ROOT like this and the workaround is easy, but at least the tests should be green. We do the same "magically ordered imports" also in other Python tests where ROOT doesn't work together with xgboost because of `std::regexp` symbol clashes, unless you do the import in a certain order: #15183
1 parent 25cdc8d commit 09910e6

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

bindings/pyroot/pythonizations/test/rdf_define_pyz.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import unittest
2-
import ROOT
2+
3+
import numba # noqa: F401
34
import numpy as np
5+
import ROOT
6+
7+
# numba is not used directly, but tests can crash when ROOT is built with
8+
# builtin_llvm=OFF and numba is not imported at the beginning:
9+
410

511
class PyDefine(unittest.TestCase):
612
"""
Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import os
12
import unittest
2-
import ROOT
3+
4+
import numba # noqa: F401
35
import numpy as np
6+
import ROOT
7+
from rdf_filter_pyz_helper import TYPE_TO_SYMBOL, CreateData, filter_dict
8+
9+
# numba is not used directly, but tests can crash when ROOT is built with
10+
# builtin_llvm=OFF and numba is not imported at the beginning:
411

5-
import os
6-
from rdf_filter_pyz_helper import CreateData, TYPE_TO_SYMBOL, filter_dict
712

813
class PyFilter(unittest.TestCase):
914
"""
@@ -12,16 +17,17 @@ class PyFilter(unittest.TestCase):
1217

1318
def test_with_dtypes(self):
1419
"""
15-
Tests the pythonized filter with all the tree datatypes and
20+
Tests the pythonized filter with all the tree datatypes and
1621
"""
1722
CreateData()
1823
rdf = ROOT.RDataFrame("TestData", "./RDF_Filter_Pyz_TestData.root")
19-
test_cols =[str(c) for c in rdf.GetColumnNames()]
24+
test_cols = [str(c) for c in rdf.GetColumnNames()]
2025
for col_name in test_cols:
21-
func = filter_dict[TYPE_TO_SYMBOL[col_name]] # filter function
26+
func = filter_dict[TYPE_TO_SYMBOL[col_name]] # filter function
2227
x = rdf.Mean(col_name).GetValue()
23-
if col_name == 'Bool_t': x = True
24-
filtered = rdf.Filter(func, extra_args = {'x':x})
28+
if col_name == "Bool_t":
29+
x = True
30+
filtered = rdf.Filter(func, extra_args={"x": x})
2531
res_root = filtered.AsNumpy()[col_name]
2632
if not isinstance(x, bool):
2733
filtered2 = rdf.Filter(f"{col_name} > {x}")
@@ -31,19 +37,21 @@ def test_with_dtypes(self):
3137
else:
3238
filtered2 = rdf.Filter(f"{col_name} == false")
3339
res_root2 = filtered2.AsNumpy()[col_name]
34-
self.assertTrue(np.array_equal(res_root,res_root2))
40+
self.assertTrue(np.array_equal(res_root, res_root2))
3541

3642
os.remove("./RDF_Filter_Pyz_TestData.root")
3743

38-
# CPP Overload 1: Filter(callable, col_list = [], name = "") => 3 Possibilities
44+
# CPP Overload 1: Filter(callable, col_list = [], name = "") => 3 Possibilities
3945
def test_filter_overload1_a(self):
4046
"""
4147
Test to verify the first overload (1.a) of filter
4248
Filter(callable, col_list, name)
4349
"""
4450
rdf = ROOT.RDataFrame(5).Define("x", "(double) rdfentry_")
51+
4552
def x_greater_than_2(x):
46-
return x>2
53+
return x > 2
54+
4755
fil1 = rdf.Filter(x_greater_than_2, ["x"], "x is more than 2")
4856
self.assertTrue(np.array_equal(fil1.AsNumpy()["x"], np.array([3, 4])))
4957

@@ -53,37 +61,43 @@ def test_filter_overload1_b(self):
5361
Filter(callable, col_list)
5462
"""
5563
rdf = ROOT.RDataFrame(5).Define("x", "(double) rdfentry_")
56-
fil1 = rdf.Filter(lambda x: x>2, ["x"])
64+
fil1 = rdf.Filter(lambda x: x > 2, ["x"])
5765
self.assertTrue(np.array_equal(fil1.AsNumpy()["x"], np.array([3, 4])))
58-
66+
5967
def test_filter_overload1_c(self):
6068
"""
6169
Test to verify the first overload (1.c) of filter
6270
Filter(callable)
6371
"""
6472
rdf = ROOT.RDataFrame(5).Define("x", "(double) rdfentry_")
73+
6574
def x_greater_than_2(x):
66-
return x>2
75+
return x > 2
76+
6777
fil1 = rdf.Filter(x_greater_than_2)
6878
self.assertTrue(np.array_equal(fil1.AsNumpy()["x"], np.array([3, 4])))
69-
79+
7080
# CPP Overload 3: Filter(callable, name)
7181
def test_filter_overload3(self):
7282
"""
7383
Test to verify the third overload of filter
7484
Filter(callable, name)
7585
"""
7686
rdf = ROOT.RDataFrame(5).Define("x", "(double) rdfentry_")
87+
7788
def x_greater_than_2(x):
78-
return x>2
89+
return x > 2
90+
7991
fil1 = rdf.Filter(x_greater_than_2, "x is greater than 2")
8092
self.assertTrue(np.array_equal(fil1.AsNumpy()["x"], np.array([3, 4])))
81-
93+
8294
def test_capture_from_scope(self):
8395
rdf = ROOT.RDataFrame(5).Define("x", "(double) rdfentry_")
8496
y = 2
97+
8598
def x_greater_than_y(x):
8699
return x > y
100+
87101
fil1 = rdf.Filter(x_greater_than_y, "x is greater than 2")
88102
self.assertTrue(np.array_equal(fil1.AsNumpy()["x"], np.array([3, 4])))
89103

@@ -93,12 +107,14 @@ def test_cpp_functor(self):
93107
Filter operation.
94108
"""
95109

96-
ROOT.gInterpreter.Declare("""
110+
ROOT.gInterpreter.Declare(
111+
"""
97112
struct MyFunctor
98113
{
99114
bool operator()(ULong64_t l) { return l == 0; };
100115
};
101-
""")
116+
"""
117+
)
102118
f = ROOT.MyFunctor()
103119

104120
rdf = ROOT.RDataFrame(5)
@@ -112,15 +128,17 @@ def test_std_function(self):
112128
Filter operation.
113129
"""
114130

115-
ROOT.gInterpreter.Declare("""
131+
ROOT.gInterpreter.Declare(
132+
"""
116133
std::function<bool(ULong64_t)> myfun = [](ULong64_t l) { return l == 0; };
117-
""")
134+
"""
135+
)
118136

119137
rdf = ROOT.RDataFrame(5)
120138
c = rdf.Filter(ROOT.myfun, ["rdfentry_"]).Count().GetValue()
121139

122140
self.assertEqual(c, 1)
123141

124-
125-
if __name__ == '__main__':
142+
143+
if __name__ == "__main__":
126144
unittest.main()

0 commit comments

Comments
 (0)