Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.

Commit 270c6ec

Browse files
committed
informational; considerably increased test coverage and fixed the revealed bugs
1 parent 565e927 commit 270c6ec

File tree

6 files changed

+98
-28
lines changed

6 files changed

+98
-28
lines changed

tests/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
| nested directories, cycle numbers, '/' and ';' notation | ✓ |
1919
| arrays interface | ✓ |
2020
| iterator interface | ✓ |
21-
| selection by list of branch names | **no!** |
22-
| pass array to fill, rather than `dtype` | **no!** |
23-
| different `outputtypes` | **no!** |
21+
| selection by list of branch names | ✓ |
22+
| pass array to fill, rather than `dtype` | ✓ |
23+
| different `outputtypes` | ✓ |
2424
| memory-mapped files | ✓ |
2525
| standard files (not using; remove?) | **no!** |
2626
| XRootD (would have to get XRootD library into `tests_require` somehow...) | **no!** |
2727
| big files (64-bit addresses in TFile header) | **no!** |
2828
| parallel processing (not deterministic: hard to include in test suite) | **no!** |
2929
| exception raising! | **no!** |
30-
| `repr` print-outs | **no!** |
3130
| informational methods (keys/branches listings) | **partial** |

tests/test_compression.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ def test_compression_identity(self):
4848
self.assertEqual(uproot.open("tests/HZZ-uncompressed.root").compression.level, 0)
4949

5050
def test_compression_keys(self):
51-
keys = uproot.open("tests/Zmumu-uncompressed.root").ls()
52-
self.assertEqual(uproot.open("tests/Zmumu-zlib.root").ls(), keys)
53-
self.assertEqual(uproot.open("tests/Zmumu-lzma.root").ls(), keys)
54-
self.assertEqual(uproot.open("tests/Zmumu-lz4.root").ls(), keys)
55-
56-
keys = uproot.open("tests/HZZ-uncompressed.root").ls()
57-
self.assertEqual(uproot.open("tests/HZZ-zlib.root").ls(), keys)
58-
self.assertEqual(uproot.open("tests/HZZ-lzma.root").ls(), keys)
59-
self.assertEqual(uproot.open("tests/HZZ-lz4.root").ls(), keys)
51+
keys = uproot.open("tests/Zmumu-uncompressed.root").contents
52+
self.assertEqual(uproot.open("tests/Zmumu-zlib.root").contents, keys)
53+
self.assertEqual(uproot.open("tests/Zmumu-lzma.root").contents, keys)
54+
self.assertEqual(uproot.open("tests/Zmumu-lz4.root").contents, keys)
55+
56+
keys = uproot.open("tests/HZZ-uncompressed.root").contents
57+
self.assertEqual(uproot.open("tests/HZZ-zlib.root").contents, keys)
58+
self.assertEqual(uproot.open("tests/HZZ-lzma.root").contents, keys)
59+
self.assertEqual(uproot.open("tests/HZZ-lz4.root").contents, keys)
6060

6161
def test_compression_branches(self):
6262
branches = uproot.open("tests/Zmumu-uncompressed.root")["events"].branchnames

tests/test_tree.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
from collections import namedtuple
1718
import unittest
1819

1920
import numpy
@@ -26,8 +27,12 @@ def runTest(self):
2627

2728
def test_branch_array(self):
2829
file = uproot.open("tests/simple.root")
30+
repr(file)
2931

3032
tree = file["tree"]
33+
repr(tree)
34+
repr(tree.branch("one"))
35+
repr(tree.branch("one"))
3136
self.assertEqual(tree.branch("one").array().tolist(), [1, 2, 3, 4])
3237
self.assertEqual(tree.branch("two").array().tolist(), numpy.array([1.1, 2.2, 3.3, 4.4], dtype=numpy.float32).tolist())
3338
self.assertEqual(tree.branch("three").array().tolist(), [b"uno", b"dos", b"tres", b"quatro"])
@@ -243,3 +248,71 @@ def test_directories(self):
243248
self.assertEqual(dict((name, array.tolist()) for name, array in file["one/tree;1"].arrays(["one", "two", "three"]).items()), {b"one": [1, 2, 3, 4], b"two": [1.100000023841858, 2.200000047683716, 3.299999952316284, 4.400000095367432], b"three": [b"uno", b"dos", b"tres", b"quatro"]})
244249
self.assertEqual(file["one/two/tree;1"].array("Int32").shape, (100,))
245250
self.assertEqual(file["three/tree;1"].array("I32").shape, (100,))
251+
252+
def test_cast(self):
253+
tree = uproot.open("tests/Zmumu.root")["events"]
254+
one = numpy.cast[numpy.int32](numpy.floor(tree.array("M")))
255+
two = tree.array("M", numpy.int32)
256+
self.assertEqual(one.dtype, two.dtype)
257+
self.assertEqual(one.shape, two.shape)
258+
self.assertTrue(numpy.array_equal(one, two))
259+
260+
for (one,) in tree.iterator(10000, "M", outputtype=tuple):
261+
one = numpy.cast[numpy.int32](numpy.floor(one))
262+
for (two,) in tree.iterator(10000, {"M": numpy.int32}, outputtype=tuple):
263+
pass
264+
self.assertEqual(one.dtype, two.dtype)
265+
self.assertEqual(one.shape, two.shape)
266+
self.assertTrue(numpy.array_equal(one, two))
267+
268+
def test_pass_array(self):
269+
tree = uproot.open("tests/Zmumu.root")["events"]
270+
one = numpy.cast[numpy.int32](numpy.floor(tree.array("M")))
271+
two = numpy.zeros(one.shape, dtype=one.dtype)
272+
tree.array("M", two)
273+
self.assertTrue(numpy.array_equal(one, two))
274+
275+
for (one,) in tree.iterator(10000, "M", outputtype=tuple):
276+
one = numpy.cast[numpy.int32](numpy.floor(one))
277+
two = numpy.zeros(one.shape, dtype=one.dtype)
278+
for (two,) in tree.iterator(10000, {"M": numpy.int32}, outputtype=tuple):
279+
self.assertTrue(numpy.array_equal(one, two))
280+
281+
def test_outputtype(self):
282+
tree = uproot.open("tests/simple.root")["tree"]
283+
284+
arrays = tree.arrays(["three", "two", "one"], outputtype=dict)
285+
self.assertTrue(isinstance(arrays, dict))
286+
self.assertEqual(arrays[b"one"].tolist(), [1, 2, 3, 4])
287+
self.assertEqual(arrays[b"three"].tolist(), [b"uno", b"dos", b"tres", b"quatro"])
288+
289+
arrays = tree.arrays(["three", "two", "one"], outputtype=tuple)
290+
self.assertTrue(isinstance(arrays, tuple))
291+
self.assertEqual(arrays[2].tolist(), [1, 2, 3, 4])
292+
self.assertEqual(arrays[0].tolist(), [b"uno", b"dos", b"tres", b"quatro"])
293+
294+
arrays = tree.arrays(["three", "two", "one"], outputtype=namedtuple)
295+
self.assertEqual(arrays.one.tolist(), [1, 2, 3, 4])
296+
self.assertEqual(arrays.three.tolist(), [b"uno", b"dos", b"tres", b"quatro"])
297+
298+
arrays = tree.arrays(["three", "two", "one"], outputtype=list)
299+
self.assertTrue(isinstance(arrays, list))
300+
self.assertEqual(arrays[2].tolist(), [1, 2, 3, 4])
301+
self.assertEqual(arrays[0].tolist(), [b"uno", b"dos", b"tres", b"quatro"])
302+
303+
class Awesome(object):
304+
def __init__(self, one, two, three):
305+
self.one = one
306+
self.two = two
307+
self.three = three
308+
309+
arrays = tree.arrays(["one", "two", "three"], outputtype=Awesome)
310+
self.assertTrue(isinstance(arrays, Awesome))
311+
self.assertEqual(arrays.one.tolist(), [1, 2, 3, 4])
312+
self.assertEqual(arrays.three.tolist(), [b"uno", b"dos", b"tres", b"quatro"])
313+
314+
def test_informational(self):
315+
self.assertEqual(uproot.open("tests/simple.root")["tree"].branchnames, [b"one", b"two", b"three"])
316+
self.assertEqual(uproot.open("tests/simple.root")["tree"].branchtypes, {b"two": numpy.dtype(">f4"), b"one": numpy.dtype(">i4"), b"three": numpy.dtype("O")})
317+
self.assertEqual(uproot.open("tests/small-evnt-tree-fullsplit.root")["tree"].allbranchnames, ["evt", b"Beg", b"I16", b"I32", b"I64", b"U16", b"U32", b"U64", b"F32", b"F64", b"Str", b"P3.Px", b"P3.Py", b"P3.Pz", b"ArrayI16[10]", b"ArrayI32[10]", b"ArrayI64[10]", b"ArrayU16[10]", b"ArrayU32[10]", b"ArrayU64[10]", b"ArrayF32[10]", b"ArrayF64[10]", b"N", b"SliceI16", b"SliceI32", b"SliceI64", b"SliceU16", b"SliceU32", b"SliceU64", b"SliceF32", b"SliceF64", b"End"])
318+
self.assertEqual(uproot.open("tests/small-evnt-tree-fullsplit.root")["tree"].allbranchtypes, {b"Str": numpy.dtype("O"), b"P3.Px": numpy.dtype(">i4"), b"I64": numpy.dtype(">i8"), b"U64": numpy.dtype(">u8"), b"ArrayF32[10]": numpy.dtype(">f4"), b"SliceI16": numpy.dtype(">i2"), b"ArrayI64[10]": numpy.dtype(">i8"), b"evt": numpy.dtype(">i4"), b"SliceF64": numpy.dtype(">f8"), b"End": numpy.dtype("O"), b"U32": numpy.dtype(">u4"), b"Beg": numpy.dtype("O"), b"I32": numpy.dtype(">i4"), b"N": numpy.dtype(">i4"), b"SliceI32": numpy.dtype(">i4"), b"P3.Py": numpy.dtype(">f8"), b"U16": numpy.dtype(">u2"), b"SliceU32": numpy.dtype(">u4"), b"P3.Pz": numpy.dtype(">i4"), b"ArrayI32[10]": numpy.dtype(">i4"), b"ArrayF64[10]": numpy.dtype(">f8"), b"I16": numpy.dtype(">i2"), b"SliceU64": numpy.dtype(">u8"), b"F64": numpy.dtype(">f8"), b"ArrayI16[10]": numpy.dtype(">i2"), b"ArrayU16[10]": numpy.dtype(">u2"), b"ArrayU32[10]": numpy.dtype(">u4"), b"F32": numpy.dtype(">f4"), b"SliceF32": numpy.dtype(">f4"), b"ArrayU64[10]": numpy.dtype(">u8"), b"SliceU16": numpy.dtype(">u2"), b"SliceI64": numpy.dtype(">i8")})

0 commit comments

Comments
 (0)