legend-pydataobj 1.11.7__py3-none-any.whl → 1.12.0a2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {legend_pydataobj-1.11.7.dist-info → legend_pydataobj-1.12.0a2.dist-info}/METADATA +1 -1
- {legend_pydataobj-1.11.7.dist-info → legend_pydataobj-1.12.0a2.dist-info}/RECORD +23 -24
- lgdo/__init__.py +5 -4
- lgdo/_version.py +2 -2
- lgdo/lh5/__init__.py +1 -3
- lgdo/lh5/_serializers/read/composite.py +1 -3
- lgdo/lh5/concat.py +3 -9
- lgdo/lh5/core.py +21 -30
- lgdo/lh5/iterator.py +48 -27
- lgdo/lh5/store.py +15 -68
- lgdo/lh5/tools.py +0 -111
- lgdo/types/array.py +84 -15
- lgdo/types/encoded.py +25 -20
- lgdo/types/histogram.py +1 -1
- lgdo/types/lgdo.py +50 -0
- lgdo/types/table.py +49 -28
- lgdo/types/vectorofvectors.py +72 -76
- lgdo/types/vovutils.py +14 -4
- lgdo/types/waveformtable.py +19 -21
- lgdo/lh5_store.py +0 -284
- {legend_pydataobj-1.11.7.dist-info → legend_pydataobj-1.12.0a2.dist-info}/WHEEL +0 -0
- {legend_pydataobj-1.11.7.dist-info → legend_pydataobj-1.12.0a2.dist-info}/entry_points.txt +0 -0
- {legend_pydataobj-1.11.7.dist-info → legend_pydataobj-1.12.0a2.dist-info}/licenses/LICENSE +0 -0
- {legend_pydataobj-1.11.7.dist-info → legend_pydataobj-1.12.0a2.dist-info}/top_level.txt +0 -0
lgdo/lh5/tools.py
CHANGED
@@ -1,16 +1,10 @@
|
|
1
1
|
from __future__ import annotations
|
2
2
|
|
3
3
|
import fnmatch
|
4
|
-
import glob
|
5
4
|
import logging
|
6
|
-
import os
|
7
5
|
from copy import copy
|
8
|
-
from warnings import warn
|
9
6
|
|
10
7
|
import h5py
|
11
|
-
import numpy as np
|
12
|
-
import pandas as pd
|
13
|
-
from numpy.typing import NDArray
|
14
8
|
|
15
9
|
from . import utils
|
16
10
|
from .store import LH5Store
|
@@ -223,108 +217,3 @@ def show(
|
|
223
217
|
break
|
224
218
|
|
225
219
|
key = k_new
|
226
|
-
|
227
|
-
|
228
|
-
def load_nda(
|
229
|
-
f_list: str | list[str],
|
230
|
-
par_list: list[str],
|
231
|
-
lh5_group: str = "",
|
232
|
-
idx_list: list[NDArray | list | tuple] | None = None,
|
233
|
-
) -> dict[str, NDArray]:
|
234
|
-
r"""Build a dictionary of :class:`numpy.ndarray`\ s from LH5 data.
|
235
|
-
|
236
|
-
Given a list of files, a list of LH5 table parameters, and an optional
|
237
|
-
group path, return a NumPy array with all values for each parameter.
|
238
|
-
|
239
|
-
Parameters
|
240
|
-
----------
|
241
|
-
f_list
|
242
|
-
A list of files. Can contain wildcards.
|
243
|
-
par_list
|
244
|
-
A list of parameters to read from each file.
|
245
|
-
lh5_group
|
246
|
-
group path within which to find the specified parameters.
|
247
|
-
idx_list
|
248
|
-
for fancy-indexed reads. Must be one index array for each file in
|
249
|
-
`f_list`.
|
250
|
-
|
251
|
-
Returns
|
252
|
-
-------
|
253
|
-
par_data
|
254
|
-
A dictionary of the parameter data keyed by the elements of `par_list`.
|
255
|
-
Each entry contains the data for the specified parameter concatenated
|
256
|
-
over all files in `f_list`.
|
257
|
-
"""
|
258
|
-
warn(
|
259
|
-
"load_nda() is deprecated. "
|
260
|
-
"Please replace it with LH5Store.read(...).view_as('np'), "
|
261
|
-
"or just read_as(..., 'np'). "
|
262
|
-
"load_nda() will be removed in a future release.",
|
263
|
-
DeprecationWarning,
|
264
|
-
stacklevel=2,
|
265
|
-
)
|
266
|
-
|
267
|
-
if isinstance(f_list, str):
|
268
|
-
f_list = [f_list]
|
269
|
-
if idx_list is not None:
|
270
|
-
idx_list = [idx_list]
|
271
|
-
if idx_list is not None and len(f_list) != len(idx_list):
|
272
|
-
msg = f"f_list length ({len(f_list)}) != idx_list length ({len(idx_list)})!"
|
273
|
-
raise ValueError(msg)
|
274
|
-
|
275
|
-
# Expand wildcards
|
276
|
-
f_list = [f for f_wc in f_list for f in sorted(glob.glob(os.path.expandvars(f_wc)))]
|
277
|
-
|
278
|
-
sto = LH5Store()
|
279
|
-
par_data = {par: [] for par in par_list}
|
280
|
-
for ii, ff in enumerate(f_list):
|
281
|
-
f = sto.gimme_file(ff, "r")
|
282
|
-
for par in par_list:
|
283
|
-
if f"{lh5_group}/{par}" not in f:
|
284
|
-
msg = f"'{lh5_group}/{par}' not in file {ff}"
|
285
|
-
raise RuntimeError(msg)
|
286
|
-
|
287
|
-
if idx_list is None:
|
288
|
-
data, _ = sto.read(f"{lh5_group}/{par}", f)
|
289
|
-
else:
|
290
|
-
data, _ = sto.read(f"{lh5_group}/{par}", f, idx=idx_list[ii])
|
291
|
-
if not data:
|
292
|
-
continue
|
293
|
-
par_data[par].append(data.nda)
|
294
|
-
return {par: np.concatenate(par_data[par]) for par in par_list}
|
295
|
-
|
296
|
-
|
297
|
-
def load_dfs(
|
298
|
-
f_list: str | list[str],
|
299
|
-
par_list: list[str],
|
300
|
-
lh5_group: str = "",
|
301
|
-
idx_list: list[NDArray | list | tuple] | None = None,
|
302
|
-
) -> pd.DataFrame:
|
303
|
-
"""Build a :class:`pandas.DataFrame` from LH5 data.
|
304
|
-
|
305
|
-
Given a list of files (can use wildcards), a list of LH5 columns, and
|
306
|
-
optionally the group path, return a :class:`pandas.DataFrame` with all
|
307
|
-
values for each parameter.
|
308
|
-
|
309
|
-
See Also
|
310
|
-
--------
|
311
|
-
:func:`load_nda`
|
312
|
-
|
313
|
-
Returns
|
314
|
-
-------
|
315
|
-
dataframe
|
316
|
-
contains columns for each parameter in `par_list`, and rows containing
|
317
|
-
all data for the associated parameters concatenated over all files in
|
318
|
-
`f_list`.
|
319
|
-
"""
|
320
|
-
warn(
|
321
|
-
"load_dfs() is deprecated. "
|
322
|
-
"Please replace it with LH5Store.read(...).view_as('pd'), "
|
323
|
-
"or just read_as(..., 'pd'). "
|
324
|
-
"load_dfs() will be removed in a future release.",
|
325
|
-
DeprecationWarning,
|
326
|
-
stacklevel=2,
|
327
|
-
)
|
328
|
-
return pd.DataFrame(
|
329
|
-
load_nda(f_list, par_list, lh5_group=lh5_group, idx_list=idx_list)
|
330
|
-
)
|
lgdo/types/array.py
CHANGED
@@ -6,7 +6,7 @@ corresponding utilities.
|
|
6
6
|
from __future__ import annotations
|
7
7
|
|
8
8
|
import logging
|
9
|
-
from collections.abc import Iterator
|
9
|
+
from collections.abc import Collection, Iterator
|
10
10
|
from typing import Any
|
11
11
|
|
12
12
|
import awkward as ak
|
@@ -17,12 +17,12 @@ import pint_pandas # noqa: F401
|
|
17
17
|
|
18
18
|
from .. import utils
|
19
19
|
from ..units import default_units_registry as u
|
20
|
-
from .lgdo import
|
20
|
+
from .lgdo import LGDOCollection
|
21
21
|
|
22
22
|
log = logging.getLogger(__name__)
|
23
23
|
|
24
24
|
|
25
|
-
class Array(
|
25
|
+
class Array(LGDOCollection):
|
26
26
|
r"""Holds an :class:`numpy.ndarray` and attributes.
|
27
27
|
|
28
28
|
:class:`Array` (and the other various array types) holds an `nda` instead
|
@@ -78,11 +78,7 @@ class Array(LGDO):
|
|
78
78
|
elif isinstance(nda, Array):
|
79
79
|
nda = nda.nda
|
80
80
|
|
81
|
-
elif not isinstance(nda, np.ndarray):
|
82
|
-
nda = np.array(nda)
|
83
|
-
|
84
81
|
self.nda = nda
|
85
|
-
self.dtype = self.nda.dtype
|
86
82
|
|
87
83
|
super().__init__(attrs)
|
88
84
|
|
@@ -96,18 +92,91 @@ class Array(LGDO):
|
|
96
92
|
return dt + "<" + nd + ">{" + et + "}"
|
97
93
|
|
98
94
|
def __len__(self) -> int:
|
99
|
-
return
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
return self.
|
95
|
+
return self._size
|
96
|
+
|
97
|
+
@property
|
98
|
+
def nda(self):
|
99
|
+
return self._nda[: self._size, ...] if self._nda.shape != () else self._nda
|
100
|
+
|
101
|
+
@nda.setter
|
102
|
+
def nda(self, value):
|
103
|
+
self._nda = value if isinstance(value, np.ndarray) else np.array(value)
|
104
|
+
self._size = len(self._nda) if self._nda.shape != () else 0
|
105
|
+
|
106
|
+
@property
|
107
|
+
def dtype(self):
|
108
|
+
return self._nda.dtype
|
109
|
+
|
110
|
+
@property
|
111
|
+
def shape(self):
|
112
|
+
return (len(self),) + self._nda.shape[1:]
|
113
|
+
|
114
|
+
def reserve_capacity(self, capacity: int) -> None:
|
115
|
+
"Set size (number of rows) of internal memory buffer"
|
116
|
+
if capacity < len(self):
|
117
|
+
msg = "Cannot reduce capacity below Array length"
|
118
|
+
raise ValueError(msg)
|
119
|
+
self._nda.resize((capacity,) + self._nda.shape[1:], refcheck=False)
|
120
|
+
|
121
|
+
def get_capacity(self) -> int:
|
122
|
+
"Get capacity (i.e. max size before memory must be re-allocated)"
|
123
|
+
return len(self._nda)
|
124
|
+
|
125
|
+
def trim_capacity(self) -> None:
|
126
|
+
"Set capacity to be minimum needed to support Array size"
|
127
|
+
self.reserve_capacity(np.prod(self.shape))
|
128
|
+
|
129
|
+
def resize(self, new_size: int | Collection[int], trim=False) -> None:
|
130
|
+
"""Set size of Array in rows. Only change capacity if it must be
|
131
|
+
increased to accommodate new rows; in this case double capacity.
|
132
|
+
If trim is True, capacity will be set to match size. If new_size
|
133
|
+
is an int, do not change size of inner dimensions.
|
134
|
+
|
135
|
+
If new_size is a collection, internal memory will be re-allocated, so
|
136
|
+
this should be done only rarely!"""
|
137
|
+
|
138
|
+
if isinstance(new_size, Collection):
|
139
|
+
self._size = new_size[0]
|
140
|
+
self._nda.resize(new_size)
|
141
|
+
else:
|
142
|
+
self._size = new_size
|
143
|
+
|
144
|
+
if trim and new_size != self.get_capacity:
|
145
|
+
self.reserve_capacity(new_size)
|
146
|
+
|
147
|
+
# If capacity is not big enough, set to next power of 2 big enough
|
148
|
+
if new_size > self.get_capacity():
|
149
|
+
self.reserve_capacity(int(2 ** (np.ceil(np.log2(new_size)))))
|
104
150
|
|
105
151
|
def append(self, value: np.ndarray) -> None:
|
106
|
-
|
107
|
-
self.
|
152
|
+
"Append value to end of array (with copy)"
|
153
|
+
self.insert(len(self), value)
|
108
154
|
|
109
155
|
def insert(self, i: int, value: int | float) -> None:
|
110
|
-
|
156
|
+
"Insert value into row i (with copy)"
|
157
|
+
if i > len(self):
|
158
|
+
msg = f"index {i} is out of bounds for array with size {len(self)}"
|
159
|
+
raise IndexError(msg)
|
160
|
+
|
161
|
+
value = np.array(value)
|
162
|
+
if value.shape == self.shape[1:]:
|
163
|
+
self.resize(len(self) + 1)
|
164
|
+
self[i + 1 :] = self[i:-1]
|
165
|
+
self[i] = value
|
166
|
+
elif value.shape[1:] == self.shape[1:]:
|
167
|
+
self.resize(len(self) + len(value))
|
168
|
+
self[i + len(value) :] = self[i : -len(value)]
|
169
|
+
self[i : i + len(value)] = value
|
170
|
+
else:
|
171
|
+
msg = f"Could not insert value with shape {value.shape} into Array with shape {self.shape}"
|
172
|
+
raise ValueError(msg)
|
173
|
+
|
174
|
+
def replace(self, i: int, value: int | float) -> None:
|
175
|
+
"Replace value at row i"
|
176
|
+
if i >= len(self):
|
177
|
+
msg = f"index {i} is out of bounds for array with size {len(self)}"
|
178
|
+
raise IndexError(msg)
|
179
|
+
self[i] = value
|
111
180
|
|
112
181
|
def __getitem__(self, key):
|
113
182
|
return self.nda[key]
|
lgdo/types/encoded.py
CHANGED
@@ -11,12 +11,12 @@ from numpy.typing import NDArray
|
|
11
11
|
|
12
12
|
from .. import utils
|
13
13
|
from .array import Array
|
14
|
-
from .lgdo import
|
14
|
+
from .lgdo import LGDOCollection
|
15
15
|
from .scalar import Scalar
|
16
16
|
from .vectorofvectors import VectorOfVectors
|
17
17
|
|
18
18
|
|
19
|
-
class VectorOfEncodedVectors(
|
19
|
+
class VectorOfEncodedVectors(LGDOCollection):
|
20
20
|
"""An array of variable-length encoded arrays.
|
21
21
|
|
22
22
|
Used to represent an encoded :class:`.VectorOfVectors`. In addition to an
|
@@ -92,6 +92,17 @@ class VectorOfEncodedVectors(LGDO):
|
|
92
92
|
|
93
93
|
return False
|
94
94
|
|
95
|
+
def reserve_capacity(self, *capacity: int) -> None:
|
96
|
+
self.encoded_data.reserve_capacity(*capacity)
|
97
|
+
self.decoded_size.reserve_capacity(capacity[0])
|
98
|
+
|
99
|
+
def get_capacity(self) -> tuple:
|
100
|
+
return (self.decoded_size.get_capacity, *self.encoded_data.get_capacity())
|
101
|
+
|
102
|
+
def trim_capacity(self) -> None:
|
103
|
+
self.encoded_data.trim_capacity()
|
104
|
+
self.decoded_size.trim_capacity()
|
105
|
+
|
95
106
|
def resize(self, new_size: int) -> None:
|
96
107
|
"""Resize vector along the first axis.
|
97
108
|
|
@@ -102,21 +113,6 @@ class VectorOfEncodedVectors(LGDO):
|
|
102
113
|
self.encoded_data.resize(new_size)
|
103
114
|
self.decoded_size.resize(new_size)
|
104
115
|
|
105
|
-
def append(self, value: tuple[NDArray, int]) -> None:
|
106
|
-
"""Append a 1D encoded vector at the end.
|
107
|
-
|
108
|
-
Parameters
|
109
|
-
----------
|
110
|
-
value
|
111
|
-
a tuple holding the encoded array and its decoded size.
|
112
|
-
|
113
|
-
See Also
|
114
|
-
--------
|
115
|
-
.VectorOfVectors.append
|
116
|
-
"""
|
117
|
-
self.encoded_data.append(value[0])
|
118
|
-
self.decoded_size.append(value[1])
|
119
|
-
|
120
116
|
def insert(self, i: int, value: tuple[NDArray, int]) -> None:
|
121
117
|
"""Insert an encoded vector at index `i`.
|
122
118
|
|
@@ -282,7 +278,7 @@ class VectorOfEncodedVectors(LGDO):
|
|
282
278
|
raise ValueError(msg)
|
283
279
|
|
284
280
|
|
285
|
-
class ArrayOfEncodedEqualSizedArrays(
|
281
|
+
class ArrayOfEncodedEqualSizedArrays(LGDOCollection):
|
286
282
|
"""An array of encoded arrays with equal decoded size.
|
287
283
|
|
288
284
|
Used to represent an encoded :class:`.ArrayOfEqualSizedArrays`. In addition
|
@@ -349,14 +345,23 @@ class ArrayOfEncodedEqualSizedArrays(LGDO):
|
|
349
345
|
|
350
346
|
return False
|
351
347
|
|
352
|
-
def
|
348
|
+
def reserve_capacity(self, *capacity: int) -> None:
|
349
|
+
self.encoded_data.reserve_capacity(capacity)
|
350
|
+
|
351
|
+
def get_capacity(self) -> tuple:
|
352
|
+
return self.encoded_data.get_capacity()
|
353
|
+
|
354
|
+
def trim_capacity(self) -> None:
|
355
|
+
self.encoded_data.trim_capacity()
|
356
|
+
|
357
|
+
def resize(self, new_size: int, trim: bool = False) -> None:
|
353
358
|
"""Resize array along the first axis.
|
354
359
|
|
355
360
|
See Also
|
356
361
|
--------
|
357
362
|
.VectorOfVectors.resize
|
358
363
|
"""
|
359
|
-
self.encoded_data.resize(new_size)
|
364
|
+
self.encoded_data.resize(new_size, trim)
|
360
365
|
|
361
366
|
def append(self, value: NDArray) -> None:
|
362
367
|
"""Append a 1D encoded array at the end.
|
lgdo/types/histogram.py
CHANGED
@@ -424,7 +424,7 @@ class Histogram(Struct):
|
|
424
424
|
dict.__setitem__(self, name, obj)
|
425
425
|
else:
|
426
426
|
msg = "histogram fields cannot be mutated "
|
427
|
-
raise
|
427
|
+
raise AttributeError(msg)
|
428
428
|
|
429
429
|
def __getattr__(self, name: str) -> None:
|
430
430
|
# do not allow for new attributes on this
|
lgdo/types/lgdo.py
CHANGED
@@ -92,3 +92,53 @@ class LGDO(ABC):
|
|
92
92
|
|
93
93
|
def __repr__(self) -> str:
|
94
94
|
return self.__class__.__name__ + f"(attrs={self.attrs!r})"
|
95
|
+
|
96
|
+
|
97
|
+
class LGDOCollection(LGDO):
|
98
|
+
"""Abstract base class representing a LEGEND Collection Object (LGDO).
|
99
|
+
This defines the interface for classes used as table columns.
|
100
|
+
"""
|
101
|
+
|
102
|
+
@abstractmethod
|
103
|
+
def __init__(self, attrs: dict[str, Any] | None = None) -> None:
|
104
|
+
super().__init__(attrs)
|
105
|
+
|
106
|
+
@abstractmethod
|
107
|
+
def __len__(self) -> int:
|
108
|
+
"""Provides ``__len__`` for this array-like class."""
|
109
|
+
|
110
|
+
@abstractmethod
|
111
|
+
def reserve_capacity(self, capacity: int) -> None:
|
112
|
+
"""Reserve capacity (in rows) for later use. Internal memory buffers
|
113
|
+
will have enough entries to store this many rows.
|
114
|
+
"""
|
115
|
+
|
116
|
+
@abstractmethod
|
117
|
+
def get_capacity(self) -> int:
|
118
|
+
"get reserved capacity of internal memory buffers in rows"
|
119
|
+
|
120
|
+
@abstractmethod
|
121
|
+
def trim_capacity(self) -> None:
|
122
|
+
"""set capacity to only what is required to store current contents
|
123
|
+
of LGDOCollection
|
124
|
+
"""
|
125
|
+
|
126
|
+
@abstractmethod
|
127
|
+
def resize(self, new_size: int, trim: bool = False) -> None:
|
128
|
+
"""Return this LGDO's datatype attribute string."""
|
129
|
+
|
130
|
+
def append(self, val) -> None:
|
131
|
+
"append val to end of LGDOCollection"
|
132
|
+
self.insert(len(self), val)
|
133
|
+
|
134
|
+
@abstractmethod
|
135
|
+
def insert(self, i: int, val) -> None:
|
136
|
+
"insert val into LGDOCollection at position i"
|
137
|
+
|
138
|
+
@abstractmethod
|
139
|
+
def replace(self, i: int, val) -> None:
|
140
|
+
"replace item at position i with val in LGDOCollection"
|
141
|
+
|
142
|
+
def clear(self, trim: bool = False) -> None:
|
143
|
+
"set size of LGDOCollection to zero"
|
144
|
+
self.resize(0, trim=trim)
|
lgdo/types/table.py
CHANGED
@@ -19,7 +19,7 @@ from pandas.io.formats import format as fmt
|
|
19
19
|
|
20
20
|
from .array import Array
|
21
21
|
from .arrayofequalsizedarrays import ArrayOfEqualSizedArrays
|
22
|
-
from .lgdo import LGDO
|
22
|
+
from .lgdo import LGDO, LGDOCollection
|
23
23
|
from .scalar import Scalar
|
24
24
|
from .struct import Struct
|
25
25
|
from .vectorofvectors import VectorOfVectors
|
@@ -27,13 +27,9 @@ from .vectorofvectors import VectorOfVectors
|
|
27
27
|
log = logging.getLogger(__name__)
|
28
28
|
|
29
29
|
|
30
|
-
class Table(Struct):
|
30
|
+
class Table(Struct, LGDOCollection):
|
31
31
|
"""A special struct of arrays or subtable columns of equal length.
|
32
32
|
|
33
|
-
Holds onto an internal read/write location ``loc`` that is useful in
|
34
|
-
managing table I/O using functions like :meth:`push_row`, :meth:`is_full`,
|
35
|
-
and :meth:`clear`.
|
36
|
-
|
37
33
|
Note
|
38
34
|
----
|
39
35
|
If you write to a table and don't fill it up to its total size, be sure to
|
@@ -49,7 +45,7 @@ class Table(Struct):
|
|
49
45
|
|
50
46
|
def __init__(
|
51
47
|
self,
|
52
|
-
col_dict: Mapping[str,
|
48
|
+
col_dict: Mapping[str, LGDOCollection] | pd.DataFrame | ak.Array | None = None,
|
53
49
|
size: int | None = None,
|
54
50
|
attrs: Mapping[str, Any] | None = None,
|
55
51
|
) -> None:
|
@@ -65,7 +61,7 @@ class Table(Struct):
|
|
65
61
|
col_dict
|
66
62
|
instantiate this table using the supplied mapping of column names
|
67
63
|
and array-like objects. Supported input types are: mapping of
|
68
|
-
strings to
|
64
|
+
strings to LGDOCollections, :class:`pd.DataFrame` and :class:`ak.Array`.
|
69
65
|
Note 1: no copy is performed, the objects are used directly (unless
|
70
66
|
:class:`ak.Array` is provided). Note 2: if `size` is not ``None``,
|
71
67
|
all arrays will be resized to match it. Note 3: if the arrays have
|
@@ -85,7 +81,8 @@ class Table(Struct):
|
|
85
81
|
col_dict = _ak_to_lgdo_or_col_dict(col_dict)
|
86
82
|
|
87
83
|
# call Struct constructor
|
88
|
-
|
84
|
+
Struct.__init__(self, obj_dict=col_dict)
|
85
|
+
LGDOCollection.__init__(self, attrs=attrs)
|
89
86
|
|
90
87
|
# if col_dict is not empty, set size according to it
|
91
88
|
# if size is also supplied, resize all fields to match it
|
@@ -93,13 +90,10 @@ class Table(Struct):
|
|
93
90
|
if col_dict is not None and len(col_dict) > 0:
|
94
91
|
self.resize(new_size=size, do_warn=(size is None))
|
95
92
|
|
96
|
-
# if no col_dict, just set the size
|
93
|
+
# if no col_dict, just set the size
|
97
94
|
else:
|
98
95
|
self.size = size if size is not None else None
|
99
96
|
|
100
|
-
# always start at loc=0
|
101
|
-
self.loc = 0
|
102
|
-
|
103
97
|
def datatype_name(self) -> str:
|
104
98
|
return "table"
|
105
99
|
|
@@ -107,7 +101,31 @@ class Table(Struct):
|
|
107
101
|
"""Provides ``__len__`` for this array-like class."""
|
108
102
|
return self.size
|
109
103
|
|
110
|
-
def
|
104
|
+
def reserve_capacity(self, capacity: int | list) -> None:
|
105
|
+
"Set size (number of rows) of internal memory buffer"
|
106
|
+
if isinstance(capacity, int):
|
107
|
+
for obj in self.values():
|
108
|
+
obj.reserve_capacity(capacity)
|
109
|
+
else:
|
110
|
+
if len(capacity) != len(self.keys()):
|
111
|
+
msg = "List of capacities must have same length as number of keys"
|
112
|
+
raise ValueError(msg)
|
113
|
+
|
114
|
+
for obj, cap in zip(self.values(), capacity):
|
115
|
+
obj.reserve_capacity(cap)
|
116
|
+
|
117
|
+
def get_capacity(self) -> int:
|
118
|
+
"Get list of capacities for each key"
|
119
|
+
return [v.get_capacity() for v in self.values()]
|
120
|
+
|
121
|
+
def trim_capacity(self) -> int:
|
122
|
+
"Set capacity to be minimum needed to support Array size"
|
123
|
+
for v in self.values():
|
124
|
+
v.trim_capacity()
|
125
|
+
|
126
|
+
def resize(
|
127
|
+
self, new_size: int | None = None, do_warn: bool = False, trim: bool = False
|
128
|
+
) -> None:
|
111
129
|
# if new_size = None, use the size from the first field
|
112
130
|
for field, obj in self.items():
|
113
131
|
if new_size is None:
|
@@ -119,21 +137,20 @@ class Table(Struct):
|
|
119
137
|
f"with size {len(obj)} != {new_size}"
|
120
138
|
)
|
121
139
|
if isinstance(obj, Table):
|
122
|
-
obj.resize(new_size)
|
140
|
+
obj.resize(new_size, trim)
|
123
141
|
else:
|
124
|
-
obj.resize(new_size)
|
142
|
+
obj.resize(new_size, trim)
|
125
143
|
self.size = new_size
|
126
144
|
|
127
|
-
def
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
def clear(self) -> None:
|
134
|
-
self.loc = 0
|
145
|
+
def insert(self, i: int, vals: dict) -> None:
|
146
|
+
"Insert vals into table at row i. Vals is a mapping from table key to val"
|
147
|
+
for k, ar in self.items():
|
148
|
+
ar.insert(i, vals[k])
|
149
|
+
self.size += 1
|
135
150
|
|
136
|
-
def add_field(
|
151
|
+
def add_field(
|
152
|
+
self, name: str, obj: LGDOCollection, use_obj_size: bool = False
|
153
|
+
) -> None:
|
137
154
|
"""Add a field (column) to the table.
|
138
155
|
|
139
156
|
Use the name "field" here to match the terminology used in
|
@@ -170,7 +187,9 @@ class Table(Struct):
|
|
170
187
|
new_size = len(obj) if use_obj_size else self.size
|
171
188
|
self.resize(new_size=new_size)
|
172
189
|
|
173
|
-
def add_column(
|
190
|
+
def add_column(
|
191
|
+
self, name: str, obj: LGDOCollection, use_obj_size: bool = False
|
192
|
+
) -> None:
|
174
193
|
"""Alias for :meth:`.add_field` using table terminology 'column'."""
|
175
194
|
self.add_field(name, obj, use_obj_size=use_obj_size)
|
176
195
|
|
@@ -201,8 +220,10 @@ class Table(Struct):
|
|
201
220
|
set to ``False`` to turn off warnings associated with mismatched
|
202
221
|
`loc` parameter or :meth:`add_column` warnings.
|
203
222
|
"""
|
204
|
-
if other_table
|
205
|
-
log.warning(
|
223
|
+
if len(other_table) != len(self) and do_warn:
|
224
|
+
log.warning(
|
225
|
+
f"len(other_table) ({len(other_table)}) != len(self) ({len(self)})"
|
226
|
+
)
|
206
227
|
if cols is None:
|
207
228
|
cols = other_table.keys()
|
208
229
|
for name in cols:
|