-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
rdataframe_makenumpy.py
159 lines (136 loc) · 4.62 KB
/
rdataframe_makenumpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import unittest
import ROOT
import numpy as np
import sys
import gc
class DataFrameFromNumpy(unittest.TestCase):
"""
Tests for the FromNumpy feature enabling to read numpy arrays
with RDataFrame.
"""
dtypes = [
"int32", "int64", "uint32", "uint64", "float32", "float64"
]
def test_dtypes(self):
"""
Test reading different datatypes
"""
for dtype in self.dtypes:
data = {"x": np.array([1, 2, 3], dtype=dtype)}
df = ROOT.RDF.FromNumpy(data)
self.assertEqual(df.Mean("x").GetValue(), 2)
def test_multiple_columns(self):
"""
Test reading multiple columns
"""
data = {}
for dtype in self.dtypes:
data[dtype] = np.array([1, 2, 3], dtype=dtype)
df = ROOT.RDF.FromNumpy(data)
colnames = df.GetColumnNames()
# Test column names
for dtype in colnames:
self.assertIn(dtype, self.dtypes)
# Test mean
for dtype in self.dtypes:
self.assertEqual(df.Mean(dtype).GetValue(), 2)
def test_refcount(self):
"""
Check refcounts of associated PyObjects
"""
data = {"x": np.array([1, 2, 3], dtype="float32")}
gc.collect()
self.assertEqual(sys.getrefcount(data), 2)
self.assertEqual(sys.getrefcount(data["x"]), 2)
df = ROOT.RDF.FromNumpy(data)
gc.collect()
self.assertEqual(sys.getrefcount(df), 2)
self.assertEqual(sys.getrefcount(data["x"]), 3)
def test_transformations(self):
"""
Test the use of transformations
"""
data = {"x": np.array([1, 2, 3], dtype="float32")}
df = ROOT.RDF.FromNumpy(data)
df2 = df.Filter("x>1").Define("y", "2*x")
self.assertEqual(df2.Mean("x").GetValue(), 2.5)
self.assertEqual(df2.Mean("y").GetValue(), 5)
def test_delete_dict(self):
"""
Test behaviour with data dictionary going out of scope
"""
data = {"x": np.array([1, 2, 3], dtype="float32")}
df = ROOT.RDF.FromNumpy(data)
del data
self.assertEqual(df.Mean("x").GetValue(), 2)
def test_delete_numpy_array(self):
"""
Test behaviour with numpy array going out of scope
"""
x = np.array([1, 2, 3], dtype="float32")
data = {"x": x}
df = ROOT.RDF.FromNumpy(data)
del x
self.assertEqual(df.Mean("x").GetValue(), 2)
def test_inplace_dict(self):
"""
Test behaviour with inplace dictionary
"""
df = ROOT.RDF.FromNumpy({"x": np.array([1, 2, 3], dtype="float32")})
self.assertEqual(df.Mean("x").GetValue(), 2)
def test_lifetime_numpy_array(self):
"""
Test lifetime of numpy array
"""
x = np.array([1, 2, 3], dtype="float32")
gc.collect()
ref1 = sys.getrefcount(x)
df = ROOT.RDF.FromNumpy({"x": x})
gc.collect()
ref2 = sys.getrefcount(x)
self.assertEqual(ref2, ref1 + 1)
del df
gc.collect()
ref3 = sys.getrefcount(x)
self.assertEqual(ref1, ref3)
def test_lifetime_datasource(self):
"""
Test lifetime of datasource
Datasource survives until last node of the graph goes out of scope
"""
x = np.array([1, 2, 3], dtype="float32")
gc.collect()
ref1 = sys.getrefcount(x)
# Data source has dictionary with RVecs attached, which take a reference
# to the numpy array
df = ROOT.RDF.FromNumpy({"x": x})
m = df.Mean("x")
gc.collect()
ref2 = sys.getrefcount(x)
self.assertEqual(ref1 + 1, ref2)
# Deleting the root node does not change anything since the datasource
# owns the RVecs
del df
self.assertEqual(m.GetValue(), 2)
gc.collect()
ref3 = sys.getrefcount(x)
self.assertEqual(ref1 + 1, ref3)
# Deleting the last node releases the RVecs and releases the reference
# to the numpy array
del m
gc.collect()
ref4 = sys.getrefcount(x)
self.assertEqual(ref1, ref4)
def test_sliced_array(self):
"""
Test correct reading of a sliced numpy array (#13690)
"""
table = np.array([[1,2], [3,4]], dtype="int64")
columns = {'x': table[:,0], 'y': table[:,1]}
df = ROOT.RDF.FromNumpy(columns)
x_col = df.Take['Long64_t']("x")
y_col = df.Take['Long64_t']("y")
self.assertEqual(list(x_col.GetValue()), [1,3])
self.assertEqual(list(y_col.GetValue()), [2,4])
if __name__ == '__main__':
unittest.main()