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

Commit d7841dc

Browse files
committed
fixes #26: finally added std::bitset<N>
1 parent dd980e1 commit d7841dc

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

uproot/interp/auto.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import uproot.const
3737
from uproot.interp.numerical import asdtype
3838
from uproot.interp.numerical import asarray
39+
from uproot.interp.numerical import asstlbitset
3940
from uproot.interp.jagged import asjagged
4041
from uproot.interp.jagged import asstlvector
4142
from uproot.interp.jagged import asstlvectorvector
@@ -204,6 +205,11 @@ def interpret(branch, classes=None, swapbytes=True):
204205
except _NotNumerical:
205206
pass
206207

208+
if hasattr(branch._streamer, "fTypeName"):
209+
m = re.match(b"bitset<([1-9][0-9]*)>", branch._streamer.fTypeName)
210+
if m is not None:
211+
return asstlbitset(int(m.group(1)))
212+
207213
if getattr(branch._streamer, "fTypeName", None) == b"vector<bool>":
208214
return asstlvector(asdtype("bool"))
209215
elif getattr(branch._streamer, "fTypeName", None) == b"vector<char>":
@@ -252,6 +258,10 @@ def interpret(branch, classes=None, swapbytes=True):
252258
elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<double> >":
253259
return asstlvectorvector(">f8")
254260

261+
m = re.match(b"bitset<([1-9][0-9]*)>", branch.fClassName)
262+
if m is not None:
263+
return asstlbitset(int(m.group(1)))
264+
255265
if branch.fClassName == b"vector<bool>":
256266
return asstlvector(asdtype("bool"))
257267
elif branch.fClassName == b"vector<char>":

uproot/interp/numerical.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
else:
4040
string_types = (str, bytes)
4141

42+
def _dimsprod(dims):
43+
out = 1
44+
for x in dims:
45+
out *= x
46+
return out
47+
4248
class asdtype(Interpretation):
4349
# makes __doc__ attribute mutable before Python 3.3
4450
__metaclass__ = type.__new__(type, "type", (Interpretation.__metaclass__,), {})
@@ -100,7 +106,8 @@ def empty(self):
100106
return numpy.empty((0,) + self.todims, dtype=self.todtype)
101107

102108
def compatible(self, other):
103-
return isinstance(other, (asdtype, asarray)) and self.todtype == other.todtype and self.todims == other.todims
109+
return (isinstance(other, (asdtype, asarray)) and self.todtype == other.todtype and self.todims == other.todims) or \
110+
(isinstance(other, asstlbitset) and self.todtype == other.dtype and self.todims == (other.numbytes,))
104111

105112
def numitems(self, numbytes, numentries):
106113
return numbytes // self.fromdtype.itemsize
@@ -140,6 +147,8 @@ def clip(self, destination, itemstart, itemstop, entrystart, entrystop):
140147
assert itemstart % product == 0
141148
assert itemstop % product == 0
142149
return destination[itemstart // product : itemstop // product]
150+
# FIXME: isn't the above equivalent to the following?
151+
# return destination[entrystart:entrystop]
143152

144153
def finalize(self, destination):
145154
return destination
@@ -195,8 +204,46 @@ def finalize(self, destination):
195204
array, stop = destination
196205
return array[:stop]
197206

198-
def _dimsprod(dims):
199-
out = 1
200-
for x in dims:
201-
out *= x
202-
return out
207+
class asstlbitset(Interpretation):
208+
# makes __doc__ attribute mutable before Python 3.3
209+
__metaclass__ = type.__new__(type, "type", (Interpretation.__metaclass__,), {})
210+
211+
dtype = numpy.dtype(numpy.bool_)
212+
213+
def __init__(self, numbytes):
214+
self.numbytes = numbytes
215+
216+
def __repr__(self):
217+
return self.identifier
218+
219+
@property
220+
def identifier(self):
221+
return "asstlbitset({0})".format(self.numbytes)
222+
223+
def empty(self):
224+
return numpy.empty((0,), dtype=self.dtype)
225+
226+
def compatible(self, other):
227+
return (isinstance(other, asstlbitset) and self.numbytes == other.numbytes) or \
228+
(isinstance(other, (asdtype, asarray)) and self.dtype == other.todtype and (self.numbytes,) == other.todims)
229+
230+
def numitems(self, numbytes, numentries):
231+
return max(0, numbytes // self.dtype.itemsize - 10*numentries)
232+
233+
def source_numitems(self, source):
234+
return _dimsprod(source.shape)
235+
236+
def fromroot(self, data, offsets, local_entrystart, local_entrystop):
237+
return data.view(self.dtype).reshape((-1, self.numbytes + 10))[local_entrystart:local_entrystop,10:]
238+
239+
def destination(self, numitems, numentries):
240+
return numpy.empty((numitems // self.numbytes, self.numbytes), dtype=self.dtype)
241+
242+
def fill(self, source, destination, itemstart, itemstop, entrystart, entrystop):
243+
destination[entrystart:entrystop] = source
244+
245+
def clip(self, destination, itemstart, itemstop, entrystart, entrystop):
246+
return destination[entrystart:entrystop]
247+
248+
def finalize(self, destination):
249+
return destination

uproot/tree.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,19 @@ def _normalize_branches(self, arg):
552552
def _normalize_entrystartstop(self, entrystart, entrystop):
553553
if entrystart is None:
554554
entrystart = 0
555+
elif entrystart < 0:
556+
entrystart += self.numentries
557+
entrystart = min(self.numentries, max(0, entrystart))
558+
555559
if entrystop is None:
556560
entrystop = self.numentries
561+
elif entrystop < 0:
562+
entrystop += self.numentries
563+
entrystop = min(self.numentries, max(0, entrystop))
564+
557565
if entrystop < entrystart:
558566
raise IndexError("entrystop must be greater than or equal to entrystart")
567+
559568
return entrystart, entrystop
560569

561570
def __len__(self):

uproot/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
import re
3232

33-
__version__ = "2.6.7"
33+
__version__ = "2.6.8"
3434
version = __version__
3535
version_info = tuple(re.split(r"[-\.]", __version__))
3636

0 commit comments

Comments
 (0)