Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.

Commit 4dbc981

Browse files
committed
truncation tests
1 parent 491525c commit 4dbc981

5 files changed

Lines changed: 74 additions & 20 deletions

File tree

root_numpy/_tree.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ def root2array(filenames,
209209
flatten = branches
210210
branches = [branches]
211211
elif isinstance(branches, tuple):
212+
if len(branches) not in (2, 3):
213+
raise ValueError(
214+
"invalid branch tuple: {0}. "
215+
"A branch tuple must contain two elements "
216+
"(branch_name, fill_value) or three elements "
217+
"(branch_name, fill_value, length) "
218+
"to yield a single value or truncate, respectively".format(branches))
212219
flatten = branches[0]
213220
branches = [branches]
214221
else:
@@ -390,6 +397,13 @@ def tree2array(tree,
390397
flatten = branches
391398
branches = [branches]
392399
elif isinstance(branches, tuple):
400+
if len(branches) not in (2, 3):
401+
raise ValueError(
402+
"invalid branch tuple: {0}. "
403+
"A branch tuple must contain two elements "
404+
"(branch_name, fill_value) or three elements "
405+
"(branch_name, fill_value, length) "
406+
"to yield a single value or truncate, respectively".format(branches))
393407
flatten = branches[0]
394408
branches = [branches]
395409
else:

root_numpy/src/_librootnumpy.cpp

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

root_numpy/src/converters.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ cdef cppclass VaryArrayConverter(ObjectConverterBase):
281281
return np.object
282282

283283
bool can_truncate():
284-
return True
284+
return this.ndim == 1
285285

286286

287287
cdef cppclass FixedArrayConverter(Converter):

root_numpy/src/tree.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ cdef object tree2array(TTree* tree, branches,
267267
branch_dict[branch_spec[0]] = (idx, branch_spec[2])
268268
else:
269269
raise ValueError(
270-
("invalid branch tuple: {0}. "
271-
"A branch tuple must contain two elements "
272-
"(branch_name, default_value) or three elements "
273-
"(branch_name, fill_value, max_length) "
274-
"to yield a single value or truncate, respectively").format(branch_spec))
270+
"invalid branch tuple: {0}. "
271+
"A branch tuple must contain two elements "
272+
"(branch_name, fill_value) or three elements "
273+
"(branch_name, fill_value, length) "
274+
"to yield a single value or truncate, respectively".format(branch_spec))
275275
branch_defaults[branch_spec[0]] = branch_spec[1]
276276
else:
277277
branch_dict[branch_spec] = (idx, 0)

root_numpy/tests/test_tree.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,46 @@ def test_struct():
502502
('branch2_floatleaf', '<f4')]))
503503

504504

505+
def check_truncate_impute(filename):
506+
filename = load(filename)
507+
# first convert array and find object columns
508+
arr = rnp.root2array(filename)
509+
assert_true(len(arr))
510+
object_fields = [field for field in arr.dtype.names if arr.dtype[field] == 'O']
511+
fields_1d = [field for field in object_fields
512+
if arr[field][0].dtype != 'O' and len(arr[field][0].shape) == 1]
513+
fields_md = list(set(object_fields) - set(fields_1d))
514+
assert_true(fields_1d)
515+
assert_true(fields_md)
516+
517+
arr1 = rnp.root2array(filename, branches=[(f, 0) for f in fields_1d])
518+
assert_true(len(arr1))
519+
assert_equal(set(arr1.dtype.names), set(fields_1d))
520+
# Giving length of 1 will result in the same output
521+
arr2 = rnp.root2array(filename, branches=[(f, 0, 1) for f in fields_1d])
522+
assert_array_equal(arr1, arr2)
523+
# fill_value of 1 instead of 0 should change output array
524+
arr2 = rnp.root2array(filename, branches=[(f, 1, 1) for f in fields_1d])
525+
assert_raises(AssertionError, assert_array_equal, arr1, arr2)
526+
# check dtype shape
527+
arr3 = rnp.root2array(filename, branches=[(f, 0, 3) for f in fields_1d])
528+
for field in fields_1d:
529+
assert_equal(arr3.dtype[field].shape, (3,))
530+
531+
# length must be at least 1
532+
assert_raises(ValueError, rnp.root2array, filename, branches=[(fields_1d[0], 0, 0)])
533+
# tuple is not of length 2 or 3
534+
assert_raises(ValueError, rnp.root2array, filename, branches=[(fields_1d[0], 1, 1, 1)])
535+
assert_raises(ValueError, rnp.root2array, filename, branches=(fields_1d[0], 1, 1, 1))
536+
# can only truncate 1d arrays
537+
assert_raises(TypeError, rnp.root2array, filename, branches=(fields_md[0], 0))
538+
539+
540+
def test_truncate_impute():
541+
for filename in ['vector.root', 'vary1.root']:
542+
yield check_truncate_impute, filename
543+
544+
505545
def test_array2tree():
506546
a = np.array([
507547
(12345, 2., 2.1, True),

0 commit comments

Comments
 (0)