6
6
import numpy as np
7
7
import ROOT
8
8
import os
9
+ import warnings
9
10
10
11
def test_read_write ():
11
12
df = pd .DataFrame ({'x' : [1 ,2 ,3 ]})
@@ -110,34 +111,69 @@ def test_flatten():
110
111
111
112
length = np .array ([3 ])
112
113
x = np .array ([0 , 1 , 2 ], dtype = 'float64' )
114
+ y = np .array ([6 , 7 , 8 ], dtype = 'float64' )
113
115
tt .Branch ('length' , length , 'length/I' )
114
116
tt .Branch ('x' , x , 'x[length]/D' )
115
-
117
+ tt . Branch ( 'y' , y , 'y[length]/D' )
116
118
tt .Fill ()
117
119
x [0 ] = 3
118
120
x [1 ] = 4
119
121
x [2 ] = 5
122
+ y [0 ] = 9
123
+ y [1 ] = 10
124
+ y [2 ] = 11
120
125
tt .Fill ()
121
126
122
127
tf .Write ()
123
128
tf .Close ()
124
129
125
130
branches = list_branches ('tmp.root' )
126
131
127
- df_ = read_root ('tmp.root' , flatten = True )
128
132
133
+ # flatten one out of two array branches
134
+ with warnings .catch_warnings ():
135
+ warnings .simplefilter ("ignore" )
136
+ df_ = read_root ('tmp.root' , flatten = ['x' ])
129
137
assert ('__array_index' in df_ .columns )
130
138
assert (len (df_ ) == 6 )
139
+ assert ('length' in df_ .columns .values )
140
+ assert ('x' in df_ .columns .values )
141
+ assert ('y' not in df_ .columns .values )
131
142
assert (np .all (df_ ['__array_index' ] == np .array ([0 , 1 , 2 , 0 , 1 , 2 ])))
143
+ assert (np .all (df_ ['x' ] == np .array ([0 , 1 , 2 , 3 , 4 , 5 ])))
132
144
133
- # Also flatten chunked data
134
145
135
- for df_ in read_root ('tmp.root' , flatten = True , chunksize = 1 ):
146
+ # flatten both array branches
147
+ df_ = read_root ('tmp.root' , flatten = ['x' ,'y' ])
148
+ assert ('__array_index' in df_ .columns )
149
+ assert (len (df_ ) == 6 )
150
+ assert (np .all (df_ ['__array_index' ] == np .array ([0 , 1 , 2 , 0 , 1 , 2 ])))
151
+ assert ('length' in df_ .columns .values )
152
+ assert ('x' in df_ .columns .values )
153
+ assert ('y' in df_ .columns .values )
154
+ assert (np .all (df_ ['x' ] == np .array ([0 , 1 , 2 , 3 , 4 , 5 ])))
155
+ assert (np .all (df_ ['y' ] == np .array ([6 , 7 , 8 , 9 , 10 , 11 ])))
156
+
157
+
158
+ # Also flatten chunked data
159
+ for df_ in read_root ('tmp.root' , flatten = ['x' ], chunksize = 1 ):
136
160
assert (len (df_ ) == 3 )
137
161
assert (np .all (df_ ['__array_index' ] == np .array ([0 , 1 , 2 ])))
138
162
163
+ # Also test deprecated behaviour
164
+ with warnings .catch_warnings ():
165
+ warnings .simplefilter ("ignore" )
166
+ df_ = read_root ('tmp.root' , flatten = True )
167
+ assert ('__array_index' in df_ .columns )
168
+ assert (len (df_ ) == 6 )
169
+ assert (np .all (df_ ['__array_index' ] == np .array ([0 , 1 , 2 , 0 , 1 , 2 ])))
170
+
171
+
139
172
os .remove ('tmp.root' )
140
173
174
+
175
+
176
+
141
177
def test_drop_nonscalar_columns ():
142
178
array = np .array ([1 , 2 , 3 ])
143
179
matrix = np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]])
@@ -157,11 +193,12 @@ def test_drop_nonscalar_columns():
157
193
158
194
path = 'tmp.root'
159
195
array2root (arr , path , 'ntuple' , mode = 'recreate' )
160
-
161
- df = read_root (path , flatten = False )
162
- # the above line throws an error if flatten=True because nonscalar columns
163
- # are dropped only after the flattening is applied. However, the flattening
164
- # algorithm can not deal with arrays of more than one dimension.
196
+ with warnings .catch_warnings ():
197
+ warnings .simplefilter ("ignore" )
198
+ df = read_root (path , flatten = False )
199
+ # the above line throws an error if flatten=True because nonscalar columns
200
+ # are dropped only after the flattening is applied. However, the flattening
201
+ # algorithm can not deal with arrays of more than one dimension.
165
202
assert (len (df .columns ) == 2 )
166
203
assert (np .all (df .index .values == np .array ([0 , 1 ])))
167
204
assert (np .all (df .a .values == np .array ([3 , 2 ])))
0 commit comments