-
Notifications
You must be signed in to change notification settings - Fork 76
/
grouped.py
155 lines (135 loc) · 4.36 KB
/
grouped.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
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE
"""
This module defines an :doc:`uproot.interpretation.Interpretation` and
temporary array for grouped data; usually applied to a ``TBranch`` that does
not contain data but has subbranches that do.
"""
from __future__ import annotations
import uproot
class AsGrouped(uproot.interpretation.Interpretation):
"""
Args:
branch (:doc:`uproot.behaviors.TBranch.TBranch`): The ``TBranch`` that
represents the group.
subbranches (dict of str \u2192 :doc:`uproot.behaviors.TBranch.TBranch`): Names
and interpretations of the ``TBranches`` that actually contain data.
typename (None or str): If None, construct a plausible C++ typename.
Otherwise, take the suggestion as given.
Interpretation for a group of arrays, usually because they are all
subbranches of the same :doc:`uproot.behaviors.TBranch.TBranch`.
Each :doc:`uproot.interpretation.library.Library` presents a group
differently: :doc:`uproot.interpretation.library.NumPy` puts arrays
in a dict, :doc:`uproot.interpretation.library.Awkward` makes an
array of records, :doc:`uproot.interpretation.library.Pandas` makes
a ``pandas.DataFrame``, etc.
"""
def __init__(self, branch, subbranches, typename=None):
self._branch = branch
self._subbranches = subbranches
self._typename = typename
@property
def branch(self):
"""
The ``TBranch`` that represents the group.
"""
return self._branch
@property
def subbranches(self):
"""
The ``TBranches`` that contain the actual data.
"""
return self._subbranches
def __repr__(self):
return f"AsGroup({self._branch}, {self._subbranches})"
def __eq__(self, other):
return (
isinstance(other, AsGrouped)
and self._branch == other._branch
and self._subbranches == other._subbranches
)
@property
def cache_key(self):
return "{}({},[{}])".format(
type(self).__name__,
self._branch.name,
",".join(
f"{x!r}:{y.cache_key}"
for x, y in self._subbranches.items()
if y is not None
),
)
@property
def typename(self):
if self._typename is not None:
return self._typename
else:
return "(group of {})".format(
", ".join(
f"{x}:{y.typename}"
for x, y in self._subbranches.items()
if y is not None
)
)
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()
names = []
fields = []
for x, y in self._subbranches.items():
if y is not None:
names.append(x)
fields.append(y.awkward_form(file, context))
return awkward.forms.RecordForm(fields, names)
def basket_array(
self,
data,
byte_offsets,
basket,
branch,
context,
cursor_offset,
library,
options,
):
raise ValueError(
"""grouping branches like {} should not be read directly; instead read the subbranches:
{}
in file {}
in object {}""".format(
repr(self._branch.name),
", ".join(repr(x) for x in self._subbranches),
self._branch.file.file_path,
self._branch.object_path,
)
)
def final_array(
self,
basket_arrays,
entry_start,
entry_stop,
entry_offsets,
library,
branch,
options,
):
raise ValueError(
"""grouping branches like {} should not be read directly; instead read the subbranches:
{}
in file {}
in object {}""".format(
repr(self._branch.name),
", ".join(repr(x) for x in self._subbranches),
self._branch.file.file_path,
self._branch.object_path,
)
)