power-grid-model 1.10.17__py3-none-win_amd64.whl → 1.12.119__py3-none-win_amd64.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.
Potentially problematic release.
This version of power-grid-model might be problematic. Click here for more details.
- power_grid_model/__init__.py +54 -29
- power_grid_model/_core/__init__.py +3 -3
- power_grid_model/_core/buffer_handling.py +507 -478
- power_grid_model/_core/data_handling.py +195 -141
- power_grid_model/_core/data_types.py +142 -0
- power_grid_model/_core/dataset_definitions.py +109 -109
- power_grid_model/_core/enum.py +226 -0
- power_grid_model/_core/error_handling.py +215 -198
- power_grid_model/_core/errors.py +134 -0
- power_grid_model/_core/index_integer.py +17 -17
- power_grid_model/_core/options.py +71 -69
- power_grid_model/_core/power_grid_core.py +577 -562
- power_grid_model/_core/power_grid_dataset.py +545 -490
- power_grid_model/_core/power_grid_meta.py +262 -244
- power_grid_model/_core/power_grid_model.py +1025 -687
- power_grid_model/_core/power_grid_model_c/__init__.py +3 -0
- power_grid_model/_core/power_grid_model_c/bin/power_grid_model_c.dll +0 -0
- power_grid_model/_core/power_grid_model_c/get_pgm_dll_path.py +63 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/basics.h +251 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/buffer.h +108 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/dataset.h +332 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/dataset_definitions.h +1060 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/handle.h +111 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/meta_data.h +189 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/model.h +130 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/options.h +142 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/serialization.h +118 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_c.h +36 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/basics.hpp +65 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/buffer.hpp +61 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/dataset.hpp +224 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/handle.hpp +108 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/meta_data.hpp +84 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/model.hpp +63 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/options.hpp +52 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/serialization.hpp +124 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/utils.hpp +81 -0
- power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp.hpp +19 -0
- power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelConfig.cmake +37 -0
- power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelConfigVersion.cmake +65 -0
- power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelTargets-release.cmake +19 -0
- power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelTargets.cmake +144 -0
- power_grid_model/_core/power_grid_model_c/lib/power_grid_model_c.lib +0 -0
- power_grid_model/_core/power_grid_model_c/share/LICENSE +292 -0
- power_grid_model/_core/power_grid_model_c/share/README.md +15 -0
- power_grid_model/_core/serialization.py +319 -317
- power_grid_model/_core/typing.py +20 -0
- power_grid_model/{_utils.py → _core/utils.py} +798 -783
- power_grid_model/data_types.py +321 -319
- power_grid_model/enum.py +27 -214
- power_grid_model/errors.py +37 -119
- power_grid_model/typing.py +43 -48
- power_grid_model/utils.py +529 -400
- power_grid_model/validation/__init__.py +25 -10
- power_grid_model/validation/{rules.py → _rules.py} +1167 -962
- power_grid_model/validation/{validation.py → _validation.py} +1172 -1015
- power_grid_model/validation/assertions.py +93 -92
- power_grid_model/validation/errors.py +602 -524
- power_grid_model/validation/utils.py +313 -318
- {power_grid_model-1.10.17.dist-info → power_grid_model-1.12.119.dist-info}/METADATA +162 -165
- power_grid_model-1.12.119.dist-info/RECORD +65 -0
- {power_grid_model-1.10.17.dist-info → power_grid_model-1.12.119.dist-info}/WHEEL +1 -1
- power_grid_model-1.12.119.dist-info/entry_points.txt +3 -0
- power_grid_model/_core/_power_grid_core.dll +0 -0
- power_grid_model-1.10.17.dist-info/RECORD +0 -32
- power_grid_model-1.10.17.dist-info/top_level.txt +0 -1
- {power_grid_model-1.10.17.dist-info → power_grid_model-1.12.119.dist-info/licenses}/LICENSE +0 -0
|
@@ -1,478 +1,507 @@
|
|
|
1
|
-
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
|
|
2
|
-
#
|
|
3
|
-
# SPDX-License-Identifier: MPL-2.0
|
|
4
|
-
|
|
5
|
-
"""
|
|
6
|
-
Power grid model buffer handler
|
|
7
|
-
"""
|
|
8
|
-
|
|
9
|
-
from dataclasses import dataclass
|
|
10
|
-
from typing import cast
|
|
11
|
-
|
|
12
|
-
import numpy as np
|
|
13
|
-
|
|
14
|
-
from power_grid_model._core.
|
|
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
|
-
Returns:
|
|
418
|
-
|
|
419
|
-
"""
|
|
420
|
-
if
|
|
421
|
-
return
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
(properties
|
|
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
|
-
return
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
def
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
1
|
+
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
|
|
5
|
+
"""
|
|
6
|
+
Power grid model buffer handler
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from typing import cast, overload
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
|
|
14
|
+
from power_grid_model._core.data_types import (
|
|
15
|
+
AttributeType,
|
|
16
|
+
ComponentData,
|
|
17
|
+
DenseBatchArray,
|
|
18
|
+
DenseBatchColumnarData,
|
|
19
|
+
DenseBatchData,
|
|
20
|
+
IndexPointer,
|
|
21
|
+
SingleArray,
|
|
22
|
+
SingleColumnarData,
|
|
23
|
+
SingleComponentData,
|
|
24
|
+
SparseBatchArray,
|
|
25
|
+
SparseBatchData,
|
|
26
|
+
)
|
|
27
|
+
from power_grid_model._core.error_handling import VALIDATOR_MSG
|
|
28
|
+
from power_grid_model._core.index_integer import IdxC, IdxNp
|
|
29
|
+
from power_grid_model._core.power_grid_core import IdxPtr, VoidPtr
|
|
30
|
+
from power_grid_model._core.power_grid_meta import ComponentMetaData
|
|
31
|
+
from power_grid_model._core.utils import (
|
|
32
|
+
_extract_data_from_component_data,
|
|
33
|
+
_extract_indptr,
|
|
34
|
+
check_indptr_consistency,
|
|
35
|
+
is_columnar,
|
|
36
|
+
is_sparse,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class BufferProperties:
|
|
42
|
+
"""
|
|
43
|
+
Helper class to collect info on the dataset.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
is_sparse: bool
|
|
47
|
+
is_batch: bool
|
|
48
|
+
batch_size: int
|
|
49
|
+
n_elements_per_scenario: int
|
|
50
|
+
n_total_elements: int
|
|
51
|
+
columns: list[AttributeType] | None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# prepared attribute data for c api
|
|
55
|
+
@dataclass
|
|
56
|
+
class CAttributeBuffer:
|
|
57
|
+
"""
|
|
58
|
+
Buffer for a single attribute.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
data: VoidPtr # type: ignore
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# prepared component data for c api
|
|
65
|
+
@dataclass
|
|
66
|
+
class CBuffer:
|
|
67
|
+
"""
|
|
68
|
+
Buffer for a single component.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
data: VoidPtr | None
|
|
72
|
+
indptr: IdxPtr | None # type: ignore
|
|
73
|
+
n_elements_per_scenario: int
|
|
74
|
+
batch_size: int
|
|
75
|
+
total_elements: int
|
|
76
|
+
attribute_data: dict[AttributeType, CAttributeBuffer]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _get_raw_data_view(data: np.ndarray, dtype: np.dtype) -> VoidPtr:
|
|
80
|
+
"""
|
|
81
|
+
Get a raw view on the data.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
data: the data.
|
|
85
|
+
dtype: the dtype the raw buffer should obey.
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
a raw view on the data set.
|
|
89
|
+
"""
|
|
90
|
+
if data.dtype != dtype:
|
|
91
|
+
raise ValueError(f"Data type does not match schema. {VALIDATOR_MSG}")
|
|
92
|
+
return np.ascontiguousarray(data, dtype=dtype).ctypes.data_as(VoidPtr)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@overload
|
|
96
|
+
def _get_raw_component_data_view(data: np.ndarray, schema: ComponentMetaData) -> VoidPtr: ...
|
|
97
|
+
@overload
|
|
98
|
+
def _get_raw_component_data_view(data: dict[AttributeType, np.ndarray], schema: ComponentMetaData) -> None: ...
|
|
99
|
+
def _get_raw_component_data_view(
|
|
100
|
+
data: np.ndarray | dict[AttributeType, np.ndarray], schema: ComponentMetaData
|
|
101
|
+
) -> VoidPtr | None:
|
|
102
|
+
"""
|
|
103
|
+
Get a raw view on the data.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
data: the data.
|
|
107
|
+
schema: the schema the raw buffer should obey.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
a raw view on the data set.
|
|
111
|
+
"""
|
|
112
|
+
if isinstance(data, np.ndarray):
|
|
113
|
+
return _get_raw_data_view(data, dtype=schema.dtype)
|
|
114
|
+
return None
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _get_raw_attribute_data_view(data: np.ndarray, schema: ComponentMetaData, attribute: AttributeType) -> VoidPtr:
|
|
118
|
+
"""
|
|
119
|
+
Get a raw view on the data.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
data: the data.
|
|
123
|
+
schema: the schema the raw buffer should obey.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
a raw view on the data set.
|
|
127
|
+
"""
|
|
128
|
+
dense_batch_ndim = 2
|
|
129
|
+
|
|
130
|
+
attr_schema = schema.dtype[attribute]
|
|
131
|
+
attr_shape_start = data.ndim - attr_schema.ndim
|
|
132
|
+
dataset_shape = data.shape[:attr_shape_start]
|
|
133
|
+
attr_shape = data.shape[attr_shape_start:]
|
|
134
|
+
if len(dataset_shape) <= dense_batch_ndim and attr_shape == attr_schema.shape:
|
|
135
|
+
return _get_raw_data_view(data, dtype=schema.dtype[attribute].base)
|
|
136
|
+
raise ValueError("Given data has a different schema than supported.")
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _get_indptr_view(indptr: np.ndarray) -> IdxPtr: # type: ignore[valid-type]
|
|
140
|
+
"""
|
|
141
|
+
Get a raw view on the index pointer.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
indptr: the index pointer.
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
a raw view on the index pointer.
|
|
148
|
+
"""
|
|
149
|
+
return np.ascontiguousarray(indptr, dtype=IdxNp).ctypes.data_as(IdxPtr)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _get_dense_buffer_properties(
|
|
153
|
+
data: ComponentData,
|
|
154
|
+
schema: ComponentMetaData,
|
|
155
|
+
is_batch: bool | None,
|
|
156
|
+
batch_size: int | None,
|
|
157
|
+
) -> BufferProperties:
|
|
158
|
+
"""
|
|
159
|
+
Extract the properties of the uniform batch dataset component.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
data (ComponentData): the dataset component.
|
|
163
|
+
schema (ComponentMetaData): the dataset type.
|
|
164
|
+
is_batch (bool | None): whether the data is a batch dataset.
|
|
165
|
+
batch_size (int | None): the batch size.
|
|
166
|
+
|
|
167
|
+
Raises:
|
|
168
|
+
KeyError: if the dataset component is not sparse.
|
|
169
|
+
ValueError: if the dataset component contains conflicting or bad data.
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
the properties of the dataset component.
|
|
173
|
+
"""
|
|
174
|
+
if is_batch is not None and batch_size is not None and batch_size != 1 and not is_batch:
|
|
175
|
+
raise ValueError(f"Inconsistent 'is batch' and 'batch size'. {VALIDATOR_MSG}")
|
|
176
|
+
|
|
177
|
+
is_sparse_property = False
|
|
178
|
+
|
|
179
|
+
sub_data = _extract_data_from_component_data(data)
|
|
180
|
+
if not is_columnar(data):
|
|
181
|
+
actual_ndim = sub_data.ndim
|
|
182
|
+
shape: tuple[int] = sub_data.shape
|
|
183
|
+
columns = None
|
|
184
|
+
else:
|
|
185
|
+
if not sub_data:
|
|
186
|
+
raise ValueError(f"Empty columnar buffer is ambiguous. {VALIDATOR_MSG}")
|
|
187
|
+
attribute, attribute_data = next(iter(sub_data.items()))
|
|
188
|
+
actual_ndim = attribute_data.ndim - schema.dtype[attribute].ndim
|
|
189
|
+
shape = attribute_data.shape[:actual_ndim]
|
|
190
|
+
columns = list(sub_data)
|
|
191
|
+
|
|
192
|
+
for attribute, attribute_data in sub_data.items():
|
|
193
|
+
if (
|
|
194
|
+
attribute_data.ndim != actual_ndim + schema.dtype[attribute].ndim
|
|
195
|
+
or attribute_data.shape[:actual_ndim] != shape
|
|
196
|
+
):
|
|
197
|
+
raise ValueError(f"Data buffers must be consistent. {VALIDATOR_MSG}")
|
|
198
|
+
|
|
199
|
+
if actual_ndim not in (1, 2):
|
|
200
|
+
raise ValueError(f"Array can only be 1D or 2D. {VALIDATOR_MSG}")
|
|
201
|
+
|
|
202
|
+
single_dataset_ndim = 1
|
|
203
|
+
batch_dataset_ndim = 2
|
|
204
|
+
|
|
205
|
+
actual_is_batch = actual_ndim == batch_dataset_ndim
|
|
206
|
+
actual_batch_size = shape[0] if actual_is_batch else single_dataset_ndim
|
|
207
|
+
n_elements_per_scenario = shape[-1]
|
|
208
|
+
n_total_elements = actual_batch_size * n_elements_per_scenario
|
|
209
|
+
|
|
210
|
+
if is_batch is not None and is_batch != actual_is_batch:
|
|
211
|
+
raise ValueError(
|
|
212
|
+
f"Incorrect/inconsistent data provided: {'batch' if actual_is_batch else 'single'} "
|
|
213
|
+
f"data provided but {'batch' if is_batch else 'single'} data expected. {VALIDATOR_MSG}"
|
|
214
|
+
)
|
|
215
|
+
if batch_size is not None and batch_size != actual_batch_size:
|
|
216
|
+
raise ValueError(
|
|
217
|
+
f"Incorrect/inconsistent batch size provided: {actual_batch_size} scenarios provided "
|
|
218
|
+
f"but {batch_size} scenarios expected. {VALIDATOR_MSG}"
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
return BufferProperties(
|
|
222
|
+
is_sparse=is_sparse_property,
|
|
223
|
+
is_batch=actual_is_batch,
|
|
224
|
+
batch_size=actual_batch_size,
|
|
225
|
+
n_elements_per_scenario=n_elements_per_scenario,
|
|
226
|
+
n_total_elements=n_total_elements,
|
|
227
|
+
columns=columns,
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _get_sparse_buffer_properties(
|
|
232
|
+
data: ComponentData,
|
|
233
|
+
schema: ComponentMetaData,
|
|
234
|
+
batch_size: int | None,
|
|
235
|
+
) -> BufferProperties:
|
|
236
|
+
"""
|
|
237
|
+
Extract the properties of the sparse batch dataset component.
|
|
238
|
+
|
|
239
|
+
Args:
|
|
240
|
+
data (ComponentData): the sparse dataset component.
|
|
241
|
+
schema (ComponentMetaData | None): the dataset type.
|
|
242
|
+
batch_size (int | None): the batch size.
|
|
243
|
+
|
|
244
|
+
Raises:
|
|
245
|
+
KeyError: if the dataset component is not sparse.
|
|
246
|
+
ValueError: if the dataset component contains conflicting or bad data.
|
|
247
|
+
|
|
248
|
+
Returns:
|
|
249
|
+
the properties of the dataset component.
|
|
250
|
+
"""
|
|
251
|
+
is_sparse_property = True
|
|
252
|
+
|
|
253
|
+
contents = _extract_data_from_component_data(data)
|
|
254
|
+
indptr = _extract_indptr(data)
|
|
255
|
+
|
|
256
|
+
ndim = 1
|
|
257
|
+
columns: list[AttributeType] | None = None
|
|
258
|
+
if not is_columnar(data):
|
|
259
|
+
shape: tuple[int, ...] = contents.shape
|
|
260
|
+
else:
|
|
261
|
+
if not contents:
|
|
262
|
+
raise ValueError(f"Empty columnar buffer is ambiguous. {VALIDATOR_MSG}")
|
|
263
|
+
attribute_data = next(iter(contents.values()))
|
|
264
|
+
shape = attribute_data.shape[:ndim]
|
|
265
|
+
columns = list(contents)
|
|
266
|
+
for attribute, attribute_data in contents.items():
|
|
267
|
+
if attribute_data.ndim != ndim + schema.dtype[attribute].ndim or attribute_data.shape[:ndim] != shape:
|
|
268
|
+
raise ValueError(f"Data buffers must be consistent. {VALIDATOR_MSG}")
|
|
269
|
+
|
|
270
|
+
contents_size = shape[0]
|
|
271
|
+
check_indptr_consistency(indptr, batch_size, contents_size)
|
|
272
|
+
|
|
273
|
+
is_batch = True
|
|
274
|
+
n_elements_per_scenario = -1
|
|
275
|
+
n_total_elements = contents_size
|
|
276
|
+
|
|
277
|
+
return BufferProperties(
|
|
278
|
+
is_sparse=is_sparse_property,
|
|
279
|
+
is_batch=is_batch,
|
|
280
|
+
batch_size=indptr.size - 1,
|
|
281
|
+
n_elements_per_scenario=n_elements_per_scenario,
|
|
282
|
+
n_total_elements=n_total_elements,
|
|
283
|
+
columns=columns,
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def get_buffer_properties(
|
|
288
|
+
data: ComponentData,
|
|
289
|
+
schema: ComponentMetaData,
|
|
290
|
+
is_batch: bool | None = None,
|
|
291
|
+
batch_size: int | None = None,
|
|
292
|
+
) -> BufferProperties:
|
|
293
|
+
"""
|
|
294
|
+
Extract the properties of the dataset component
|
|
295
|
+
|
|
296
|
+
Args:
|
|
297
|
+
data (ComponentData): the dataset component.
|
|
298
|
+
schema (ComponentMetaData | None): the dataset type [optional if data is not columnar]
|
|
299
|
+
is_batch (bool | None): whether the data is a batch dataset. [optional]
|
|
300
|
+
batch_size (int | None): the batch size. [optional]
|
|
301
|
+
|
|
302
|
+
Raises:
|
|
303
|
+
ValueError: if the dataset component contains conflicting or bad data.
|
|
304
|
+
|
|
305
|
+
Returns:
|
|
306
|
+
the properties of the dataset component.
|
|
307
|
+
"""
|
|
308
|
+
if not is_sparse(data):
|
|
309
|
+
return _get_dense_buffer_properties(data=data, schema=schema, is_batch=is_batch, batch_size=batch_size)
|
|
310
|
+
|
|
311
|
+
if is_batch is not None and not is_batch:
|
|
312
|
+
raise ValueError("Sparse data must be batch data")
|
|
313
|
+
|
|
314
|
+
return _get_sparse_buffer_properties(data=cast(SparseBatchArray, data), schema=schema, batch_size=batch_size)
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def _get_attribute_buffer_views(
|
|
318
|
+
data: np.ndarray | dict[AttributeType, np.ndarray], schema: ComponentMetaData
|
|
319
|
+
) -> dict[AttributeType, CAttributeBuffer]:
|
|
320
|
+
"""
|
|
321
|
+
Get C API compatible views on attribute buffers.
|
|
322
|
+
|
|
323
|
+
Args:
|
|
324
|
+
data (dict[AttributeType, np.ndarray]): the data.
|
|
325
|
+
schema (ComponentMetaData): the schema that the data should obey.
|
|
326
|
+
|
|
327
|
+
Returns:
|
|
328
|
+
dict[AttributeType, CAttributeBuffer]: the C API attribute buffer view per attribute.
|
|
329
|
+
"""
|
|
330
|
+
if isinstance(data, np.ndarray):
|
|
331
|
+
return {}
|
|
332
|
+
|
|
333
|
+
return {
|
|
334
|
+
attribute: CAttributeBuffer(
|
|
335
|
+
data=_get_raw_attribute_data_view(data=attribute_data, schema=schema, attribute=attribute)
|
|
336
|
+
)
|
|
337
|
+
for attribute, attribute_data in data.items()
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def _get_uniform_buffer_view(
|
|
342
|
+
data: DenseBatchData,
|
|
343
|
+
schema: ComponentMetaData,
|
|
344
|
+
is_batch: bool | None,
|
|
345
|
+
batch_size: int | None,
|
|
346
|
+
) -> CBuffer:
|
|
347
|
+
"""
|
|
348
|
+
Get a C API compatible view on a uniform buffer.
|
|
349
|
+
|
|
350
|
+
Args:
|
|
351
|
+
data: the data.
|
|
352
|
+
schema: the schema that the data should obey.
|
|
353
|
+
is_batch (bool | None): whether the data is a batch dataset.
|
|
354
|
+
batch_size (int | None): the batch size.
|
|
355
|
+
|
|
356
|
+
Returns:
|
|
357
|
+
the C API buffer view.
|
|
358
|
+
"""
|
|
359
|
+
properties = _get_dense_buffer_properties(data, schema=schema, is_batch=is_batch, batch_size=batch_size)
|
|
360
|
+
|
|
361
|
+
return CBuffer(
|
|
362
|
+
data=_get_raw_component_data_view(data=data, schema=schema),
|
|
363
|
+
indptr=IdxPtr(),
|
|
364
|
+
n_elements_per_scenario=properties.n_elements_per_scenario,
|
|
365
|
+
batch_size=properties.batch_size,
|
|
366
|
+
total_elements=properties.n_total_elements,
|
|
367
|
+
attribute_data=_get_attribute_buffer_views(data=data, schema=schema),
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def _get_sparse_buffer_view(
|
|
372
|
+
data: SparseBatchArray,
|
|
373
|
+
schema: ComponentMetaData,
|
|
374
|
+
batch_size: int | None,
|
|
375
|
+
) -> CBuffer:
|
|
376
|
+
"""
|
|
377
|
+
Get a C API compatible view on a sparse buffer.
|
|
378
|
+
|
|
379
|
+
Args:
|
|
380
|
+
data: the data.
|
|
381
|
+
schema: the schema that the data should obey.
|
|
382
|
+
batch_size (int | None): the batch size.
|
|
383
|
+
|
|
384
|
+
Returns:
|
|
385
|
+
the C API buffer view.
|
|
386
|
+
"""
|
|
387
|
+
contents = data["data"]
|
|
388
|
+
indptr = data["indptr"]
|
|
389
|
+
|
|
390
|
+
properties = _get_sparse_buffer_properties(data, schema=schema, batch_size=batch_size)
|
|
391
|
+
|
|
392
|
+
return CBuffer(
|
|
393
|
+
data=_get_raw_component_data_view(data=contents, schema=schema),
|
|
394
|
+
indptr=_get_indptr_view(indptr),
|
|
395
|
+
n_elements_per_scenario=properties.n_elements_per_scenario,
|
|
396
|
+
batch_size=properties.batch_size,
|
|
397
|
+
total_elements=properties.n_total_elements,
|
|
398
|
+
attribute_data=_get_attribute_buffer_views(data=contents, schema=schema),
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def get_buffer_view(
|
|
403
|
+
data: ComponentData,
|
|
404
|
+
schema: ComponentMetaData,
|
|
405
|
+
is_batch: bool | None = None,
|
|
406
|
+
batch_size: int | None = None,
|
|
407
|
+
) -> CBuffer:
|
|
408
|
+
"""
|
|
409
|
+
Get a C API compatible view on a buffer.
|
|
410
|
+
|
|
411
|
+
Args:
|
|
412
|
+
data: the data.
|
|
413
|
+
schema: the schema that the data should obey.
|
|
414
|
+
is_batch (bool | None): whether the data is a batch dataset. [optional]
|
|
415
|
+
batch_size (int | None): the batch size. [optional]
|
|
416
|
+
|
|
417
|
+
Returns:
|
|
418
|
+
the C API buffer view.
|
|
419
|
+
"""
|
|
420
|
+
if not is_sparse(data):
|
|
421
|
+
return _get_uniform_buffer_view(cast(DenseBatchData, data), schema, is_batch, batch_size)
|
|
422
|
+
|
|
423
|
+
if is_batch is not None and not is_batch:
|
|
424
|
+
raise ValueError("Sparse data must be batch data")
|
|
425
|
+
|
|
426
|
+
return _get_sparse_buffer_view(cast(SparseBatchArray, data), schema, batch_size)
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
def create_buffer(properties: BufferProperties, schema: ComponentMetaData) -> ComponentData:
|
|
430
|
+
"""
|
|
431
|
+
Create a buffer with the provided properties and type.
|
|
432
|
+
|
|
433
|
+
Args:
|
|
434
|
+
properties: the desired buffer properties.
|
|
435
|
+
schema: the data type of the buffer.
|
|
436
|
+
|
|
437
|
+
Raises:
|
|
438
|
+
ValueError: if the buffer properties are not consistent.
|
|
439
|
+
|
|
440
|
+
Returns:
|
|
441
|
+
np.ndarray | dict[[str, np.ndarray]: a buffer with the correct properties.
|
|
442
|
+
"""
|
|
443
|
+
if properties.is_sparse:
|
|
444
|
+
return _create_sparse_buffer(properties=properties, schema=schema)
|
|
445
|
+
|
|
446
|
+
return _create_uniform_buffer(properties=properties, schema=schema)
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def _create_uniform_buffer(properties: BufferProperties, schema: ComponentMetaData) -> DenseBatchData:
|
|
450
|
+
"""
|
|
451
|
+
Create a uniform buffer with the provided properties and type.
|
|
452
|
+
|
|
453
|
+
Args:
|
|
454
|
+
properties: the desired buffer properties.
|
|
455
|
+
schema: the data type of the buffer.
|
|
456
|
+
|
|
457
|
+
Raises:
|
|
458
|
+
ValueError: if the buffer properties are not uniform.
|
|
459
|
+
|
|
460
|
+
Returns:
|
|
461
|
+
A uniform buffer with the correct properties.
|
|
462
|
+
"""
|
|
463
|
+
if properties.is_sparse:
|
|
464
|
+
raise ValueError(f"A uniform buffer cannot be sparse. {VALIDATOR_MSG}")
|
|
465
|
+
|
|
466
|
+
shape: int | tuple[int, int] = (
|
|
467
|
+
(properties.batch_size, properties.n_elements_per_scenario)
|
|
468
|
+
if properties.is_batch
|
|
469
|
+
else properties.n_elements_per_scenario
|
|
470
|
+
)
|
|
471
|
+
return _create_contents_buffer(shape=shape, dtype=schema.dtype, columns=properties.columns)
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def _create_sparse_buffer(properties: BufferProperties, schema: ComponentMetaData) -> SparseBatchData:
|
|
475
|
+
"""
|
|
476
|
+
Create a sparse buffer with the provided properties and type.
|
|
477
|
+
|
|
478
|
+
Args:
|
|
479
|
+
properties: the desired buffer properties.
|
|
480
|
+
schema: the data type of the buffer.
|
|
481
|
+
|
|
482
|
+
Raises:
|
|
483
|
+
ValueError: if the buffer properties are not sparse.
|
|
484
|
+
|
|
485
|
+
Returns:
|
|
486
|
+
A sparse buffer with the correct properties.
|
|
487
|
+
"""
|
|
488
|
+
data: SingleComponentData = _create_contents_buffer(
|
|
489
|
+
shape=properties.n_total_elements,
|
|
490
|
+
dtype=schema.dtype,
|
|
491
|
+
columns=properties.columns,
|
|
492
|
+
)
|
|
493
|
+
indptr: IndexPointer = np.array([0] * properties.batch_size + [properties.n_total_elements], dtype=IdxC)
|
|
494
|
+
return cast(SparseBatchData, {"data": data, "indptr": indptr})
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
@overload
|
|
498
|
+
def _create_contents_buffer(shape, dtype, columns: None) -> SingleArray | DenseBatchArray: ...
|
|
499
|
+
@overload
|
|
500
|
+
def _create_contents_buffer(
|
|
501
|
+
shape, dtype, columns: list[AttributeType]
|
|
502
|
+
) -> SingleColumnarData | DenseBatchColumnarData: ...
|
|
503
|
+
def _create_contents_buffer(shape, dtype, columns):
|
|
504
|
+
if columns is None:
|
|
505
|
+
return np.empty(shape=shape, dtype=dtype)
|
|
506
|
+
|
|
507
|
+
return {attribute: np.empty(shape=shape, dtype=dtype[attribute]) for attribute in columns}
|