@@ -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+
505545def test_array2tree ():
506546 a = np .array ([
507547 (12345 , 2. , 2.1 , True ),
0 commit comments