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

Allow dtypes with byteorders via lists. Fixes #434 #435

Merged
merged 2 commits into from
Jan 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added tests/samples/issue434.root
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,16 @@ def test_issue429(self):
dtype = [(fix(x._fName), "float32" if type(x).__name__ == "TLeafF" else "int32") for x in branch._fLeaves]
array = branch.array(uproot.asdtype(dtype + [("padding", "S4")]))
assert (array["padding"] == b"\xff\xff\xff\xff").all()

def test_issue434(self):
f = uproot.open("tests/samples/issue434.root")
fromdtype = [("pmt", "u1"), ("tdc", "<u4"), ("tot", "u1")]
todtype = [("pmt", "u1"), ("tdc", ">u4"), ("tot", "u1")]
tree = f[b'KM3NET_TIMESLICE_L1'][b'KM3NETDAQ::JDAQTimeslice']
superframes = tree[b'vector<KM3NETDAQ::JDAQSuperFrame>']
hits_buffer = superframes[b'vector<KM3NETDAQ::JDAQSuperFrame>.buffer']
hits = hits_buffer.lazyarray(
uproot.asjagged(
uproot.astable(
uproot.asdtype(fromdtype, todtype)), skipbytes=6))
assert 486480 == hits['tdc'][0][0]
11 changes: 9 additions & 2 deletions uproot/interp/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
else:
string_types = (str, bytes)

BYTEORDER_INDICATORS = (">", "<", "=", "|", b">", b"<", b"=", b"|")


def _dtypeshape(obj):
out = ()
while obj.subdtype is not None:
Expand Down Expand Up @@ -85,7 +88,9 @@ class asdtype(_asnumeric):
def __init__(self, fromdtype, todtype=None):
if isinstance(fromdtype, self.awkward.numpy.dtype):
self.fromdtype = fromdtype
elif isinstance(fromdtype, string_types) and len(fromdtype) > 0 and fromdtype[0] in (">", "<", "=", "|", b">", b"<", b"=", b"|"):
elif isinstance(fromdtype, string_types) and len(fromdtype) > 0 and fromdtype[0] in BYTEORDER_INDICATORS:
self.fromdtype = self.awkward.numpy.dtype(fromdtype)
elif isinstance(fromdtype, list) and any(e[1][0] in BYTEORDER_INDICATORS for e in fromdtype):
self.fromdtype = self.awkward.numpy.dtype(fromdtype)
else:
self.fromdtype = self.awkward.numpy.dtype(fromdtype).newbyteorder(">")
Expand All @@ -94,7 +99,9 @@ def __init__(self, fromdtype, todtype=None):
self.todtype = self.fromdtype.newbyteorder("=")
elif isinstance(todtype, self.awkward.numpy.dtype):
self.todtype = todtype
elif isinstance(todtype, string_types) and len(todtype) > 0 and todtype[0] in (">", "<", "=", "|", b">", b"<", b"=", b"|"):
elif isinstance(todtype, string_types) and len(todtype) > 0 and todtype[0] in BYTEORDER_INDICATORS:
self.todtype = self.awkward.numpy.dtype(todtype)
elif isinstance(todtype, list) and any(e[1][0] in BYTEORDER_INDICATORS for e in todtype):
self.todtype = self.awkward.numpy.dtype(todtype)
else:
self.todtype = self.awkward.numpy.dtype(todtype).newbyteorder("=")
Expand Down