-
Notifications
You must be signed in to change notification settings - Fork 68
/
numerical.py
721 lines (609 loc) · 22.2 KB
/
numerical.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE
"""
This module defines an :doc:`uproot.interpretation.Interpretation` for
several numerical types:
* :doc:`uproot.interpretation.numerical.AsDtype`: numbers, which can simply be
described as a ``numpy.dtype``.
* :doc:`uproot.interpretation.numerical.AsDtypeInPlace`: a predefined array
into which data may be overwritten.
* :doc:`uproot.interpretation.numerical.AsDouble32`: ROOT's ``Double32_t``
packed data type.
* :doc:`uproot.interpretation.numerical.AsFloat16`: ROOT's ``Float16_t``
packed data type.
* :doc:`uproot.interpretation.numerical.AsSTLBits`: an ``std::bitset<N>``
for some ``N``.
"""
from __future__ import annotations
import numpy
import uproot
def _dtype_shape(dtype):
shape = ()
while dtype.subdtype is not None:
dtype, s = dtype.subdtype
shape = shape + s
return dtype, shape
class Numerical(uproot.interpretation.Interpretation):
"""
Abstract superclass of numerical interpretations, including
* :doc:`uproot.interpretation.numerical.AsDtype`
* :doc:`uproot.interpretation.numerical.AsSTLBits`
* :doc:`uproot.interpretation.numerical.TruncatedNumerical`
"""
def _wrap_almost_finalized(self, array):
return array
def final_array(
self,
basket_arrays,
entry_start,
entry_stop,
entry_offsets,
library,
branch,
options,
):
self.hook_before_final_array(
basket_arrays=basket_arrays,
entry_start=entry_start,
entry_stop=entry_stop,
entry_offsets=entry_offsets,
library=library,
branch=branch,
)
if entry_start >= entry_stop:
output = self._prepare_output(library, length=0)
else:
length = 0
start = entry_offsets[0]
for _, stop in enumerate(entry_offsets[1:]):
if start <= entry_start and entry_stop <= stop:
length += entry_stop - entry_start
elif start <= entry_start < stop:
length += stop - entry_start
elif start <= entry_stop <= stop:
length += entry_stop - start
elif entry_start < stop and start <= entry_stop:
length += stop - start
start = stop
output = self._prepare_output(library, length)
start = entry_offsets[0]
for basket_num, stop in enumerate(entry_offsets[1:]):
if start <= entry_start and entry_stop <= stop:
local_start = entry_start - start
local_stop = entry_stop - start
basket_array = basket_arrays[basket_num]
output[:] = basket_array[local_start:local_stop]
elif start <= entry_start < stop:
local_start = entry_start - start
local_stop = stop - start
basket_array = basket_arrays[basket_num]
output[: stop - entry_start] = basket_array[local_start:local_stop]
elif start <= entry_stop <= stop:
local_start = 0
local_stop = entry_stop - start
basket_array = basket_arrays[basket_num]
output[start - entry_start :] = basket_array[local_start:local_stop]
elif entry_start < stop and start <= entry_stop:
basket_array = basket_arrays[basket_num]
output[start - entry_start : stop - entry_start] = basket_array
start = stop
output = output.view(output.dtype.newbyteorder("="))
self.hook_before_library_finalize(
basket_arrays=basket_arrays,
entry_start=entry_start,
entry_stop=entry_stop,
entry_offsets=entry_offsets,
library=library,
branch=branch,
output=output,
)
output = self._wrap_almost_finalized(output)
output = library.finalize(
output, branch, self, entry_start, entry_stop, options
)
self.hook_after_final_array(
basket_arrays=basket_arrays,
entry_start=entry_start,
entry_stop=entry_stop,
entry_offsets=entry_offsets,
library=library,
branch=branch,
output=output,
)
return output
def _prepare_output(self, library, length):
"""
Prepare the output array in which the data is stored.
In this default implementation, just create an empty array from the library but specializations might re-use an existing array (ex: :doc:`uproot.interpretation.numerical.AsDtypeInPlace`:)
"""
output = library.empty((length,), self.to_dtype)
return output
_numpy_byteorder_to_cache_key = {
"!": "B",
">": "B",
"<": "L",
"|": "L",
"=": "B" if numpy.dtype(">f8").isnative else "L",
}
_dtype_kind_itemsize_to_typename = {
("b", 1): "bool",
("i", 1): "int8_t",
("u", 1): "uint8_t",
("i", 2): "int16_t",
("u", 2): "uint16_t",
("i", 4): "int32_t",
("u", 4): "uint32_t",
("i", 8): "int64_t",
("u", 8): "uint64_t",
("f", 4): "float",
("f", 8): "double",
}
class AsDtype(Numerical):
"""
Args:
from_dtype (``numpy.dtype`` or its constructor argument): Data type to
*assume* of the raw but uncompressed bytes in the ``TBasket``.
Usually big-endian; may include named fields and a shape.
to_dtype (None, ``numpy.dtype``, or its constructor argument): Data
type to *convert* the data into. Usually native-endian; may include
named fields and a shape. If None, ``to_dtype`` will be set to the
native-endian equivalent of ``from_dtype``.
Interpretation for any array that can be fully described as a
``numpy.dtype``.
"""
def __init__(self, from_dtype, to_dtype=None):
self._from_dtype = numpy.dtype(from_dtype)
if to_dtype is None:
self._to_dtype = self._from_dtype.newbyteorder("=")
else:
self._to_dtype = numpy.dtype(to_dtype)
def __repr__(self):
if self._to_dtype == self._from_dtype.newbyteorder("="):
return f"AsDtype({str(self._from_dtype)!r})"
else:
return f"AsDtype({str(self._from_dtype)!r}, {str(self._to_dtype)!r})"
def __eq__(self, other):
return (
type(other) is AsDtype
and self._from_dtype == other._from_dtype
and self._to_dtype == other._to_dtype
)
@property
def from_dtype(self):
"""
Data type to expect of the raw but uncompressed bytes in the
``TBasket`` data. Usually big-endian; may include named fields and a
shape.
Named fields (``dtype.names``) can be used to construct a NumPy
`structured array <https://numpy.org/doc/stable/user/basics.rec.html>`__.
A shape (``dtype.shape``) can be used to construct a fixed-size array
for each entry. (Not applicable to variable-length lists! See
:doc:`uproot.interpretation.jagged.AsJagged`.) The finalized array's
``array.shape[1:] == dtype.shape``.
"""
return self._from_dtype
@property
def to_dtype(self):
"""
Data type to convert the data into. Usually the native-endian
equivalent of :ref:`uproot.interpretation.numerical.AsDtype.from_dtype`;
may include named fields and a shape.
Named fields (``dtype.names``) can be used to construct a NumPy
`structured array <https://numpy.org/doc/stable/user/basics.rec.html>`__.
A shape (``dtype.shape``) can be used to construct a fixed-size array
for each entry. (Not applicable to variable-length lists! See
:doc:`uproot.interpretation.jagged.AsJagged`.) The finalized array's
``array.shape[1:] == dtype.shape``.
"""
return self._to_dtype
@property
def itemsize(self):
"""
Number of bytes per item of
:ref:`uproot.interpretation.numerical.AsDtype.from_dtype`.
This number of bytes includes the fields and shape, like
``dtype.itemsize`` in NumPy.
"""
return self._from_dtype.itemsize
@property
def inner_shape(self):
_, s = _dtype_shape(self._from_dtype)
return s
@property
def numpy_dtype(self):
return self._to_dtype
def awkward_form(
self,
file,
context=None,
index_format="i64",
header=False,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
context, index_format, header, tobject_header, breadcrumbs
)
awkward = uproot.extras.awkward()
d, s = _dtype_shape(self._to_dtype)
out = uproot._util.awkward_form(d, file, context)
for size in s[::-1]:
out = awkward.forms.RegularForm(out, size)
return out
@property
def cache_key(self):
def form(dtype, name):
d, s = _dtype_shape(dtype)
return "{}{}{}({}{})".format(
_numpy_byteorder_to_cache_key[d.byteorder],
d.kind,
d.itemsize,
",".join(repr(x) for x in s),
name,
)
if self.from_dtype.names is None:
from_dtype = form(self.from_dtype, "")
else:
from_dtype = (
"["
+ ",".join(
form(self.from_dtype[n], "," + repr(n))
for n in self.from_dtype.names
)
+ "]"
)
if self.to_dtype.names is None:
to_dtype = form(self.to_dtype, "")
else:
to_dtype = (
"["
+ ",".join(
form(self.to_dtype[n], "," + repr(n)) for n in self.to_dtype.names
)
+ "]"
)
return f"{type(self).__name__}({from_dtype},{to_dtype})"
@property
def typename(self):
def form(dtype):
d, s = _dtype_shape(dtype)
return _dtype_kind_itemsize_to_typename[d.kind, d.itemsize] + "".join(
"[" + str(dim) + "]" for dim in s
)
if self.from_dtype.names is None:
return form(self.from_dtype)
else:
return (
"struct {"
+ " ".join(
f"{form(self.from_dtype[n])} {n};" for n in self.from_dtype.names
)
+ "}"
)
def basket_array(
self,
data,
byte_offsets,
basket,
branch,
context,
cursor_offset,
library,
options,
):
self.hook_before_basket_array(
data=data,
byte_offsets=byte_offsets,
basket=basket,
branch=branch,
context=context,
cursor_offset=cursor_offset,
library=library,
options=options,
)
dtype, shape = _dtype_shape(self._from_dtype)
try:
output = data.view(dtype).reshape((-1, *shape))
except ValueError as err:
raise ValueError(
f"""basket {basket.basket_num} in tree/branch {branch.object_path} has the wrong number of bytes ({len(data)}) """
f"""for interpretation {self}
in file {branch.file.file_path}"""
) from err
self.hook_after_basket_array(
data=data,
byte_offsets=byte_offsets,
basket=basket,
branch=branch,
context=context,
output=output,
cursor_offset=cursor_offset,
library=library,
options=options,
)
return output
def reshape(self, shape):
d, s = _dtype_shape(self._from_dtype)
self._from_dtype = numpy.dtype((d, shape))
d, s = _dtype_shape(self._to_dtype)
self._to_dtype = numpy.dtype((d, shape))
def inplace(self, array):
"""
Returns a AsDtypeInPlace version of self in order to fill the given array in place.
Example usage :
```
var = np.zeros(N, dtype=np.float32)
b = uproot.openn('afile.root')['treename']['varname']
b.array(library='np', interpretation=b.interpretation.inplace(var) )
```
"""
return AsDtypeInPlace(array, self._from_dtype)
class AsDtypeInPlace(AsDtype):
"""
Like :doc:`uproot.interpretation.numerical.AsDtype`, but a given array is
filled in-place, rather than creating a new output array.
"""
def __init__(self, array, from_dtype):
self._to_fill = array
self._from_dtype = from_dtype
self._to_dtype = numpy.dtype(array.dtype)
def _prepare_output(self, library, length):
"""
Specialized version of _prepare_output : re-use our target array kept in self._to_fill.
"""
if library.name != "np":
raise TypeError(
f"AsDtypeInPlace can only be used with library 'np', not '{library.name}'"
)
output = self._to_fill.view(self.to_dtype)
if length > len(output):
raise ValueError(
f"Requesting to fill an array of size {len(output)} (type {self._to_dtype}) with input of size {length} (type {self._from_dtype})"
)
return output[:length]
class AsSTLBits(Numerical):
"""
Interpretation for ``std::bitset``.
"""
def __init__(self):
raise NotImplementedError
@property
def itemsize(self):
return self._num_bytes + 4
class TruncatedNumerical(Numerical):
"""
Abstract superclass for interpretations that truncate the range and
granularity of the real number line to pack data into fewer bits.
Subclasses are
* :doc:`uproot.interpretation.numerical.AsDouble32`
* :doc:`uproot.interpretation.numerical.AsFloat16`
"""
@property
def low(self):
"""
Lower bound on the range of real numbers this type can express.
"""
return self._low
@property
def high(self):
"""
Upper bound on the range of real numbers this type can express.
"""
return self._high
@property
def num_bits(self):
"""
Number of bytes into which to pack these data.
"""
return self._num_bits
@property
def from_dtype(self):
"""
The ``numpy.dtype`` of the raw but uncompressed data.
May be a
`structured array <https://numpy.org/doc/stable/user/basics.rec.html>`__
of ``"exponent"`` and ``"mantissa"`` or an integer.
"""
if self.is_truncated:
return numpy.dtype(({"exponent": (">u1", 0), "mantissa": (">u2", 1)}, ()))
else:
return numpy.dtype(">u4")
@property
def itemsize(self):
"""
Number of bytes in
:ref:`uproot.interpretation.numerical.TruncatedNumerical.from_dtype`.
"""
return self.from_dtype.itemsize
@property
def to_dims(self):
"""
The ``dtype.shape`` of the ``to_dtype``.
"""
return self._to_dims
@property
def is_truncated(self):
"""
If True (:ref:`uproot.interpretation.numerical.TruncatedNumerical.low`
and :ref:`uproot.interpretation.numerical.TruncatedNumerical.high` are
both ``0``), the data are truly truncated.
"""
return self._low == self._high == 0.0
def __repr__(self):
args = [repr(self._low), repr(self._high), repr(self._num_bits)]
if self._to_dims != ():
args.append(f"to_dims={self._to_dims!r}")
return "{}({})".format(type(self).__name__, ", ".join(args))
def __eq__(self, other):
return (
type(self) == type(other)
and self._low == other._low
and self._high == other._high
and self._num_bits == other._num_bits
and self._to_dims == other._to_dims
)
@property
def numpy_dtype(self):
return self.to_dtype
@property
def cache_key(self):
return f"{type(self).__name__}({self._low},{self._high},{self._num_bits},{self._to_dims})"
def basket_array(
self,
data,
byte_offsets,
basket,
branch,
context,
cursor_offset,
library,
options,
):
self.hook_before_basket_array(
data=data,
byte_offsets=byte_offsets,
basket=basket,
branch=branch,
context=context,
cursor_offset=cursor_offset,
library=library,
options=options,
)
try:
raw = data.view(self.from_dtype)
except ValueError as err:
raise ValueError(
f"""basket {basket.basket_num} in tree/branch {branch.object_path} has the wrong number of bytes ({len(data)}) """
f"""for interpretation {self} (expecting raw array of {self._from_dtype!r})
in file {branch.file.file_path}"""
) from err
if self.is_truncated:
exponent = raw["exponent"].astype(numpy.int32)
mantissa = raw["mantissa"].astype(numpy.int32)
exponent <<= 23
exponent |= (mantissa & ((1 << (self.num_bits + 1)) - 1)) << (
23 - self.num_bits
)
sign = ((1 << (self.num_bits + 1)) & mantissa != 0) * -2 + 1
output = exponent.view(numpy.float32) * sign
d, s = _dtype_shape(self.to_dtype)
output = output.astype(d).reshape((-1, *s))
else:
d, s = _dtype_shape(self.to_dtype)
output = raw.astype(d).reshape((-1, *s))
numpy.multiply(
output,
float(self._high - self._low) / (1 << self._num_bits),
out=output,
)
numpy.add(output, self.low, out=output)
self.hook_after_basket_array(
data=data,
byte_offsets=byte_offsets,
basket=basket,
branch=branch,
context=context,
cursor_offset=cursor_offset,
library=library,
raw=raw,
output=output,
options=options,
)
return output
class AsDouble32(TruncatedNumerical):
"""
Args:
low (float): Lower bound on the range of expressible values.
high (float): Upper bound on the range of expressible values.
num_bits (int): Number of bits in the representation.
to_dims (tuple of ints): Shape of
:ref:`uproot.interpretation.numerical.AsDouble32.to_dtype`.
Interpretation for ROOT's ``Double32_t`` type.
"""
def __init__(self, low, high, num_bits, to_dims=()):
self._low = low
self._high = high
self._num_bits = num_bits
self._to_dims = to_dims
if not uproot._util.isint(num_bits) or not 2 <= num_bits <= 32:
raise TypeError("num_bits must be an integer between 2 and 32 (inclusive)")
if high <= low and not self.is_truncated:
raise ValueError(f"high ({high}) must be strictly greater than low ({low})")
@property
def to_dtype(self):
"""
The ``numpy.dtype`` of the output array.
A shape (``dtype.shape``) can be used to construct a fixed-size array
for each entry. (Not applicable to variable-length lists! See
:doc:`uproot.interpretation.jagged.AsJagged`.) The finalized array's
``array.shape[1:] == dtype.shape``.
"""
return numpy.dtype((numpy.float64, self.to_dims))
@property
def typename(self):
return "Double32_t" + "".join("[" + str(dim) + "]" for dim in self._to_dims)
def awkward_form(
self,
file,
context=None,
index_format="i64",
header=False,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
context, index_format, header, tobject_header, breadcrumbs
)
awkward = uproot.extras.awkward()
out = awkward.forms.NumpyForm("float64")
for size in self._to_dims[::-1]:
out = awkward.forms.RegularForm(out, size)
return out
class AsFloat16(TruncatedNumerical):
"""
Args:
low (float): Lower bound on the range of expressible values.
high (float): Upper bound on the range of expressible values.
num_bits (int): Number of bits in the representation.
to_dims (tuple of ints): Shape of
:ref:`uproot.interpretation.numerical.AsFloat16.to_dtype`.
Interpretation for ROOT's ``Float16_t`` type.
"""
def __init__(self, low, high, num_bits, to_dims=()):
self._low = low
self._high = high
self._num_bits = num_bits
self._to_dims = to_dims
if not uproot._util.isint(num_bits) or not 2 <= num_bits <= 32:
raise TypeError("num_bits must be an integer between 2 and 32 (inclusive)")
if high <= low and not self.is_truncated:
raise ValueError(f"high ({high}) must be strictly greater than low ({low})")
@property
def to_dtype(self):
"""
The ``numpy.dtype`` of the output array.
A shape (``dtype.shape``) can be used to construct a fixed-size array
for each entry. (Not applicable to variable-length lists! See
:doc:`uproot.interpretation.jagged.AsJagged`.) The finalized array's
``array.shape[1:] == dtype.shape``.
"""
return numpy.dtype((numpy.float32, self.to_dims))
@property
def typename(self):
return "Float16_t" + "".join("[" + str(dim) + "]" for dim in self._to_dims)
def awkward_form(
self,
file,
context=None,
index_format="i64",
header=False,
tobject_header=False,
breadcrumbs=(),
):
context = self._make_context(
context, index_format, header, tobject_header, breadcrumbs
)
awkward = uproot.extras.awkward()
out = awkward.forms.NumpyForm("float32")
for size in self._to_dims[::-1]:
out = awkward.forms.RegularForm(out, size)
return out