jcmsuite 7.4.2__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.
Potentially problematic release.
This version of jcmsuite might be problematic. Click here for more details.
- jcmsuite/__init__.py +22 -0
- jcmsuite/_build_info.py +4 -0
- jcmsuite/context/__init__.py +44 -0
- jcmsuite/context/interface_metadata.py +158 -0
- jcmsuite/context/models.py +213 -0
- jcmsuite/context/scope.py +210 -0
- jcmsuite/context/solver_context.py +569 -0
- jcmsuite/context/subprocess_tools.py +172 -0
- jcmsuite/contract.py +47 -0
- jcmsuite/daemon/__init__.py +19 -0
- jcmsuite/daemon/handler.py +925 -0
- jcmsuite/daemon/internal/__init__.py +4 -0
- jcmsuite/daemon/internal/bundle_protocol.py +64 -0
- jcmsuite/daemon/internal/handler_helpers.py +368 -0
- jcmsuite/daemon/internal/handler_resource_jobs.py +227 -0
- jcmsuite/daemon/internal/handler_wait.py +564 -0
- jcmsuite/daemon/models.py +349 -0
- jcmsuite/daemon/namespaces.py +250 -0
- jcmsuite/daemon/protocol.py +801 -0
- jcmsuite/daemon/resources.py +407 -0
- jcmsuite/daemon/scope.py +53 -0
- jcmsuite/daemon/transport.py +223 -0
- jcmsuite/editing/__init__.py +10 -0
- jcmsuite/editing/editor.py +274 -0
- jcmsuite/exceptions/__init__.py +32 -0
- jcmsuite/exceptions/errors.py +173 -0
- jcmsuite/geo/__init__.py +10 -0
- jcmsuite/geo/mesh.py +302 -0
- jcmsuite/info/__init__.py +10 -0
- jcmsuite/info/runtime_info.py +45 -0
- jcmsuite/internal/__init__.py +2 -0
- jcmsuite/internal/diagnostics.py +74 -0
- jcmsuite/internal/progress.py +80 -0
- jcmsuite/loaders/__init__.py +18 -0
- jcmsuite/loaders/blob_header.py +315 -0
- jcmsuite/loaders/cartesian_field_reader.py +249 -0
- jcmsuite/loaders/cartesian_fields.py +324 -0
- jcmsuite/loaders/nested_mapping.py +69 -0
- jcmsuite/loaders/powerflux.py +136 -0
- jcmsuite/loaders/table.py +637 -0
- jcmsuite/logging/__init__.py +22 -0
- jcmsuite/logging/runtime.py +210 -0
- jcmsuite/results/__init__.py +126 -0
- jcmsuite/results/artifacts.py +806 -0
- jcmsuite/results/discovery.py +82 -0
- jcmsuite/results/field_exports.py +1062 -0
- jcmsuite/results/field_payloads.py +453 -0
- jcmsuite/results/internal/__init__.py +4 -0
- jcmsuite/results/internal/export/__init__.py +4 -0
- jcmsuite/results/internal/export/common.py +56 -0
- jcmsuite/results/internal/export/pandas/__init__.py +4 -0
- jcmsuite/results/internal/export/pandas/common.py +165 -0
- jcmsuite/results/internal/export/pandas/payload.py +480 -0
- jcmsuite/results/internal/export/pandas/rows.py +484 -0
- jcmsuite/results/internal/export/preview.py +178 -0
- jcmsuite/results/internal/export/xarray/__init__.py +4 -0
- jcmsuite/results/internal/export/xarray/export.py +688 -0
- jcmsuite/results/internal/export/xarray/grouped.py +608 -0
- jcmsuite/results/internal/export/xarray/headers.py +290 -0
- jcmsuite/results/internal/export/xarray/runtime.py +99 -0
- jcmsuite/results/internal/export/xarray/shared.py +246 -0
- jcmsuite/results/internal/export/xarray/tabular.py +304 -0
- jcmsuite/results/internal/export/xarray/types.py +28 -0
- jcmsuite/results/internal/models/__init__.py +4 -0
- jcmsuite/results/internal/models/batch.py +1081 -0
- jcmsuite/results/internal/models/batch_postprocess.py +1728 -0
- jcmsuite/results/internal/models/bundle.py +343 -0
- jcmsuite/results/internal/models/core.py +556 -0
- jcmsuite/results/internal/models/submission.py +1157 -0
- jcmsuite/results/internal/resultbag/__init__.py +4 -0
- jcmsuite/results/internal/resultbag/access.py +489 -0
- jcmsuite/results/internal/resultbag/iteration.py +199 -0
- jcmsuite/results/internal/resultbag/query.py +226 -0
- jcmsuite/results/internal/resultbag/raw.py +407 -0
- jcmsuite/results/internal/resultbag/source_files.py +142 -0
- jcmsuite/results/internal/resultbag/store.py +881 -0
- jcmsuite/results/internal/resultbag/utils.py +258 -0
- jcmsuite/results/models.py +76 -0
- jcmsuite/results/opening.py +55 -0
- jcmsuite/results/pandas_export.py +325 -0
- jcmsuite/results/payloads.py +478 -0
- jcmsuite/results/postprocess.py +97 -0
- jcmsuite/results/postprocess_payloads.py +757 -0
- jcmsuite/results/postprocess_record.py +418 -0
- jcmsuite/results/resultbag.py +279 -0
- jcmsuite/results/resultbag_management.py +433 -0
- jcmsuite/results/structured_derivatives.py +387 -0
- jcmsuite/solve/__init__.py +10 -0
- jcmsuite/solve/daemon_bundle_staging.py +100 -0
- jcmsuite/solve/daemon_bundle_wait.py +89 -0
- jcmsuite/solve/orchestration.py +1576 -0
- jcmsuite/solve/orchestration_execution.py +180 -0
- jcmsuite/solve/orchestration_routing.py +128 -0
- jcmsuite/solve/orchestration_support.py +323 -0
- jcmsuite/solve/path_resolution.py +98 -0
- jcmsuite/solve/progress.py +20 -0
- jcmsuite/solve/solver.py +709 -0
- jcmsuite/solve/staging.py +264 -0
- jcmsuite/solve/types.py +25 -0
- jcmsuite/solve/working_dirs.py +227 -0
- jcmsuite/templates/__init__.py +11 -0
- jcmsuite/templates/converter.py +71 -0
- jcmsuite/templates/formatting.py +335 -0
- jcmsuite/templates/formatting_cli.py +47 -0
- jcmsuite/templates/rendering.py +666 -0
- jcmsuite/torch/__init__.py +25 -0
- jcmsuite/torch/autograd.py +488 -0
- jcmsuite/torch/coordinate_policy.py +82 -0
- jcmsuite/torch/core.py +312 -0
- jcmsuite/torch/results/__init__.py +51 -0
- jcmsuite/torch/types.py +84 -0
- jcmsuite/torch/validation.py +208 -0
- jcmsuite/torch/wrappers.py +4332 -0
- jcmsuite/view/__init__.py +10 -0
- jcmsuite/view/viewer.py +122 -0
- jcmsuite-7.4.2.dist-info/METADATA +180 -0
- jcmsuite-7.4.2.dist-info/RECORD +119 -0
- jcmsuite-7.4.2.dist-info/WHEEL +4 -0
- jcmsuite-7.4.2.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Payload flattening and tabularization helpers for pandas export."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Mapping, Sequence
|
|
9
|
+
from typing import cast
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def value_mapping(payload: Mapping[object, object]) -> Mapping[object, object]:
|
|
15
|
+
"""Return table values without title/header metadata envelopes."""
|
|
16
|
+
|
|
17
|
+
values: dict[object, object] = dict(payload)
|
|
18
|
+
values.pop("title", None)
|
|
19
|
+
values.pop("header", None)
|
|
20
|
+
return values
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def flatten_rows(
|
|
24
|
+
*,
|
|
25
|
+
section: str,
|
|
26
|
+
payload: object,
|
|
27
|
+
source_file: object,
|
|
28
|
+
postprocess_index: int | None,
|
|
29
|
+
postprocess_title: str | None,
|
|
30
|
+
) -> list[dict[str, object]]:
|
|
31
|
+
"""Flatten one payload into long-form section/path/value rows."""
|
|
32
|
+
|
|
33
|
+
flattened: list[tuple[str, object]] = flatten_payload(payload, path_prefix="")
|
|
34
|
+
rows: list[dict[str, object]] = []
|
|
35
|
+
path: str
|
|
36
|
+
value: object
|
|
37
|
+
for path, value in flattened:
|
|
38
|
+
rows.append(
|
|
39
|
+
{
|
|
40
|
+
"section": section,
|
|
41
|
+
"source_file": None if source_file is None else str(source_file),
|
|
42
|
+
"postprocess_index": postprocess_index,
|
|
43
|
+
"postprocess_title": postprocess_title,
|
|
44
|
+
"path": path,
|
|
45
|
+
"value": value,
|
|
46
|
+
}
|
|
47
|
+
)
|
|
48
|
+
return rows
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def tabular_rows(
|
|
52
|
+
*,
|
|
53
|
+
section: str,
|
|
54
|
+
payload: object,
|
|
55
|
+
source_file: object,
|
|
56
|
+
postprocess_index: int | None,
|
|
57
|
+
postprocess_title: str | None,
|
|
58
|
+
) -> list[dict[str, object]]:
|
|
59
|
+
"""Convert payload to tabular rows, falling back to flattened rows."""
|
|
60
|
+
|
|
61
|
+
tabular_payload_rows: list[dict[str, object]] | None = payload_to_tabular_rows(
|
|
62
|
+
payload
|
|
63
|
+
)
|
|
64
|
+
if tabular_payload_rows is None:
|
|
65
|
+
return flatten_rows(
|
|
66
|
+
section=section,
|
|
67
|
+
payload=payload,
|
|
68
|
+
source_file=source_file,
|
|
69
|
+
postprocess_index=postprocess_index,
|
|
70
|
+
postprocess_title=postprocess_title,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
rows: list[dict[str, object]] = []
|
|
74
|
+
row_data: dict[str, object]
|
|
75
|
+
for row_index, row_data in enumerate(tabular_payload_rows):
|
|
76
|
+
merged_row: dict[str, object] = {
|
|
77
|
+
"section": section,
|
|
78
|
+
"source_file": None if source_file is None else str(source_file),
|
|
79
|
+
"postprocess_index": postprocess_index,
|
|
80
|
+
"postprocess_title": postprocess_title,
|
|
81
|
+
"row_index": row_index,
|
|
82
|
+
}
|
|
83
|
+
merged_row.update(row_data)
|
|
84
|
+
rows.append(merged_row)
|
|
85
|
+
return rows
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def payload_to_tabular_rows(payload: object) -> list[dict[str, object]] | None:
|
|
89
|
+
"""Attempt to normalize a payload into rectangular tabular rows."""
|
|
90
|
+
|
|
91
|
+
if not isinstance(payload, Mapping):
|
|
92
|
+
return None
|
|
93
|
+
|
|
94
|
+
flat_columns: dict[str, object] = {}
|
|
95
|
+
if not flatten_mapping_to_columns(payload, flat_columns, path_prefix=""):
|
|
96
|
+
return None
|
|
97
|
+
if not flat_columns:
|
|
98
|
+
return [{}]
|
|
99
|
+
|
|
100
|
+
row_count: int = 1
|
|
101
|
+
column_lengths: dict[str, int] = {}
|
|
102
|
+
column_name: str
|
|
103
|
+
column_value: object
|
|
104
|
+
for column_name, column_value in flat_columns.items():
|
|
105
|
+
column_length: int | None = column_length_of(column_value)
|
|
106
|
+
if column_length is None:
|
|
107
|
+
continue
|
|
108
|
+
column_lengths[column_name] = column_length
|
|
109
|
+
if column_length > row_count:
|
|
110
|
+
row_count = column_length
|
|
111
|
+
|
|
112
|
+
for column_name, column_length in column_lengths.items():
|
|
113
|
+
if column_length not in {1, row_count}:
|
|
114
|
+
_ = column_name
|
|
115
|
+
return None
|
|
116
|
+
|
|
117
|
+
rows: list[dict[str, object]] = []
|
|
118
|
+
for row_index in range(row_count):
|
|
119
|
+
row: dict[str, object] = {}
|
|
120
|
+
for column_name, column_value in flat_columns.items():
|
|
121
|
+
row[column_name] = column_value_at(column_value, row_index)
|
|
122
|
+
rows.append(row)
|
|
123
|
+
return rows
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def payload_to_single_row(payload: object) -> dict[str, object] | None:
|
|
127
|
+
"""Normalize payload into one row without expanding vector columns."""
|
|
128
|
+
|
|
129
|
+
if not isinstance(payload, Mapping):
|
|
130
|
+
return None
|
|
131
|
+
flat_columns: dict[str, object] = {}
|
|
132
|
+
if not flatten_mapping_to_columns(payload, flat_columns, path_prefix=""):
|
|
133
|
+
return None
|
|
134
|
+
return flat_columns
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def postprocess_payload_to_long_rows(payload: object) -> list[dict[str, object]] | None:
|
|
138
|
+
"""Convert one postprocess payload into uniform long-form rows.
|
|
139
|
+
|
|
140
|
+
Parameters
|
|
141
|
+
----------
|
|
142
|
+
payload : object
|
|
143
|
+
Mapping-like postprocess payload with title/header already stripped when
|
|
144
|
+
normalized mapping access is used.
|
|
145
|
+
|
|
146
|
+
Returns
|
|
147
|
+
-------
|
|
148
|
+
list[dict[str, object]] | None
|
|
149
|
+
Long-form row dictionaries, or `None` when the payload is not
|
|
150
|
+
mapping-like.
|
|
151
|
+
"""
|
|
152
|
+
|
|
153
|
+
if not isinstance(payload, Mapping):
|
|
154
|
+
return None
|
|
155
|
+
|
|
156
|
+
rows: list[dict[str, object]] = []
|
|
157
|
+
field_key: object
|
|
158
|
+
field_value: object
|
|
159
|
+
for field_key, field_value in payload.items():
|
|
160
|
+
field_name: str = str(field_key)
|
|
161
|
+
if isinstance(field_value, Mapping) and is_numeric_key_mapping(field_value):
|
|
162
|
+
field_index: object
|
|
163
|
+
for field_index in sorted(field_value.keys(), key=_numeric_key_sort_key):
|
|
164
|
+
indexed_value: object = field_value[field_index]
|
|
165
|
+
_append_postprocess_value_rows(
|
|
166
|
+
rows,
|
|
167
|
+
field_name=field_name,
|
|
168
|
+
field_index=field_index,
|
|
169
|
+
field_value=indexed_value,
|
|
170
|
+
)
|
|
171
|
+
continue
|
|
172
|
+
|
|
173
|
+
_append_postprocess_value_rows(
|
|
174
|
+
rows,
|
|
175
|
+
field_name=field_name,
|
|
176
|
+
field_index=None,
|
|
177
|
+
field_value=field_value,
|
|
178
|
+
)
|
|
179
|
+
return rows
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def postprocess_header_to_long_rows(payload: object) -> list[dict[str, object]] | None:
|
|
183
|
+
"""Convert one postprocess header payload into long-form rows.
|
|
184
|
+
|
|
185
|
+
Header dataframe export stays row-oriented, but does not explode vector or
|
|
186
|
+
matrix values into per-component/per-entry rows. Arrays remain intact in the
|
|
187
|
+
`value` column.
|
|
188
|
+
"""
|
|
189
|
+
|
|
190
|
+
if not isinstance(payload, Mapping):
|
|
191
|
+
return None
|
|
192
|
+
|
|
193
|
+
rows: list[dict[str, object]] = []
|
|
194
|
+
_append_postprocess_header_rows(rows, payload=payload, field_prefix="")
|
|
195
|
+
return rows
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _append_postprocess_header_rows(
|
|
199
|
+
rows: list[dict[str, object]],
|
|
200
|
+
*,
|
|
201
|
+
payload: Mapping[object, object],
|
|
202
|
+
field_prefix: str,
|
|
203
|
+
) -> None:
|
|
204
|
+
"""Append long-form header rows for a possibly nested header mapping."""
|
|
205
|
+
|
|
206
|
+
field_key: object
|
|
207
|
+
field_value: object
|
|
208
|
+
for field_key, field_value in payload.items():
|
|
209
|
+
field_name: str = (
|
|
210
|
+
f"{field_prefix}.{field_key}" if field_prefix else str(field_key)
|
|
211
|
+
)
|
|
212
|
+
if isinstance(field_value, Mapping):
|
|
213
|
+
if is_numeric_key_mapping(field_value):
|
|
214
|
+
field_index: object
|
|
215
|
+
for field_index in sorted(
|
|
216
|
+
field_value.keys(), key=_numeric_key_sort_key
|
|
217
|
+
):
|
|
218
|
+
rows.append(
|
|
219
|
+
{
|
|
220
|
+
"field_name": field_name,
|
|
221
|
+
"field_index": field_index,
|
|
222
|
+
"value": field_value[field_index],
|
|
223
|
+
}
|
|
224
|
+
)
|
|
225
|
+
continue
|
|
226
|
+
_append_postprocess_header_rows(
|
|
227
|
+
rows,
|
|
228
|
+
payload=field_value,
|
|
229
|
+
field_prefix=field_name,
|
|
230
|
+
)
|
|
231
|
+
continue
|
|
232
|
+
|
|
233
|
+
value: object = field_value
|
|
234
|
+
if isinstance(field_value, np.ndarray) and field_value.ndim == 0:
|
|
235
|
+
value = cast(object, field_value[()])
|
|
236
|
+
rows.append(
|
|
237
|
+
{
|
|
238
|
+
"field_name": field_name,
|
|
239
|
+
"field_index": None,
|
|
240
|
+
"value": value,
|
|
241
|
+
}
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _append_postprocess_value_rows(
|
|
246
|
+
rows: list[dict[str, object]],
|
|
247
|
+
*,
|
|
248
|
+
field_name: str,
|
|
249
|
+
field_index: object | None,
|
|
250
|
+
field_value: object,
|
|
251
|
+
) -> None:
|
|
252
|
+
"""Append long-form postprocess rows for one field payload."""
|
|
253
|
+
|
|
254
|
+
if isinstance(field_value, np.ndarray):
|
|
255
|
+
if field_value.ndim == 0:
|
|
256
|
+
rows.append(
|
|
257
|
+
{
|
|
258
|
+
"field_name": field_name,
|
|
259
|
+
"field_index": field_index,
|
|
260
|
+
"sample_index": None,
|
|
261
|
+
"value": cast(object, field_value[()]),
|
|
262
|
+
}
|
|
263
|
+
)
|
|
264
|
+
return
|
|
265
|
+
sample_index: int
|
|
266
|
+
for sample_index in range(int(field_value.shape[0])):
|
|
267
|
+
rows.append(
|
|
268
|
+
{
|
|
269
|
+
"field_name": field_name,
|
|
270
|
+
"field_index": field_index,
|
|
271
|
+
"sample_index": sample_index,
|
|
272
|
+
"value": cast(object, field_value[sample_index]),
|
|
273
|
+
}
|
|
274
|
+
)
|
|
275
|
+
return
|
|
276
|
+
|
|
277
|
+
if isinstance(field_value, Sequence) and not isinstance(
|
|
278
|
+
field_value, (str, bytes, bytearray)
|
|
279
|
+
):
|
|
280
|
+
sample_index = 0
|
|
281
|
+
sequence_item: object
|
|
282
|
+
for sample_index, sequence_item in enumerate(field_value):
|
|
283
|
+
rows.append(
|
|
284
|
+
{
|
|
285
|
+
"field_name": field_name,
|
|
286
|
+
"field_index": field_index,
|
|
287
|
+
"sample_index": sample_index,
|
|
288
|
+
"value": sequence_item,
|
|
289
|
+
}
|
|
290
|
+
)
|
|
291
|
+
return
|
|
292
|
+
|
|
293
|
+
rows.append(
|
|
294
|
+
{
|
|
295
|
+
"field_name": field_name,
|
|
296
|
+
"field_index": field_index,
|
|
297
|
+
"sample_index": None,
|
|
298
|
+
"value": field_value,
|
|
299
|
+
}
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def flatten_mapping_to_columns(
|
|
304
|
+
payload: Mapping[object, object],
|
|
305
|
+
columns: dict[str, object],
|
|
306
|
+
*,
|
|
307
|
+
path_prefix: str,
|
|
308
|
+
) -> bool:
|
|
309
|
+
"""Flatten nested mapping payloads into dot-delimited columns."""
|
|
310
|
+
|
|
311
|
+
mapping_key: object
|
|
312
|
+
child_value: object
|
|
313
|
+
for mapping_key, child_value in payload.items():
|
|
314
|
+
child_path: str = (
|
|
315
|
+
f"{path_prefix}.{mapping_key}" if path_prefix else str(mapping_key)
|
|
316
|
+
)
|
|
317
|
+
if isinstance(child_value, Mapping):
|
|
318
|
+
if is_numeric_key_mapping(child_value):
|
|
319
|
+
if _should_expand_numeric_key_mapping(
|
|
320
|
+
child_value,
|
|
321
|
+
path_prefix=child_path,
|
|
322
|
+
):
|
|
323
|
+
if not _expand_numeric_key_mapping_to_columns(
|
|
324
|
+
child_value,
|
|
325
|
+
columns,
|
|
326
|
+
path_prefix=child_path,
|
|
327
|
+
):
|
|
328
|
+
return False
|
|
329
|
+
continue
|
|
330
|
+
# Keep non-expandable numeric-key mappings intact as one column value.
|
|
331
|
+
columns[child_path] = child_value
|
|
332
|
+
continue
|
|
333
|
+
if not flatten_mapping_to_columns(
|
|
334
|
+
child_value, columns, path_prefix=child_path
|
|
335
|
+
):
|
|
336
|
+
return False
|
|
337
|
+
continue
|
|
338
|
+
columns[child_path] = child_value
|
|
339
|
+
return True
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def _should_expand_numeric_key_mapping(
|
|
343
|
+
payload: Mapping[object, object],
|
|
344
|
+
*,
|
|
345
|
+
path_prefix: str,
|
|
346
|
+
) -> bool:
|
|
347
|
+
"""Return whether a numeric-key mapping should expand into columns.
|
|
348
|
+
|
|
349
|
+
Header metadata stays intact as one column value. Homogeneous numeric-key
|
|
350
|
+
mappings of arrays elsewhere are treated as real tabular axes.
|
|
351
|
+
"""
|
|
352
|
+
|
|
353
|
+
if path_prefix == "header" or path_prefix.startswith("header."):
|
|
354
|
+
return False
|
|
355
|
+
value_shapes: list[tuple[int, ...]] = []
|
|
356
|
+
child_value: object
|
|
357
|
+
for child_value in payload.values():
|
|
358
|
+
if not isinstance(child_value, np.ndarray):
|
|
359
|
+
return False
|
|
360
|
+
if child_value.ndim == 0:
|
|
361
|
+
return False
|
|
362
|
+
value_shapes.append(tuple(int(axis_size) for axis_size in child_value.shape))
|
|
363
|
+
if not value_shapes:
|
|
364
|
+
return False
|
|
365
|
+
first_shape: tuple[int, ...] = value_shapes[0]
|
|
366
|
+
return all(shape == first_shape for shape in value_shapes[1:])
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def _expand_numeric_key_mapping_to_columns(
|
|
370
|
+
payload: Mapping[object, object],
|
|
371
|
+
columns: dict[str, object],
|
|
372
|
+
*,
|
|
373
|
+
path_prefix: str,
|
|
374
|
+
) -> bool:
|
|
375
|
+
"""Expand homogeneous numeric-key array mappings into dotted columns."""
|
|
376
|
+
|
|
377
|
+
numeric_key: object
|
|
378
|
+
for numeric_key in sorted(payload.keys(), key=_numeric_key_sort_key):
|
|
379
|
+
array_value_object: object = payload[numeric_key]
|
|
380
|
+
if not isinstance(array_value_object, np.ndarray):
|
|
381
|
+
return False
|
|
382
|
+
array_value: np.ndarray = array_value_object
|
|
383
|
+
if array_value.ndim == 1:
|
|
384
|
+
columns[f"{path_prefix}.{numeric_key}"] = array_value
|
|
385
|
+
continue
|
|
386
|
+
trailing_index: tuple[int, ...]
|
|
387
|
+
for trailing_index in np.ndindex(array_value.shape[1:]):
|
|
388
|
+
trailing_suffix: str = ".".join(str(index) for index in trailing_index)
|
|
389
|
+
column_name: str = f"{path_prefix}.{numeric_key}.{trailing_suffix}"
|
|
390
|
+
columns[column_name] = array_value[(slice(None),) + trailing_index]
|
|
391
|
+
return True
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def _numeric_key_sort_key(mapping_key: object) -> int | str:
|
|
395
|
+
"""Sort numeric mapping keys deterministically."""
|
|
396
|
+
|
|
397
|
+
if isinstance(mapping_key, int):
|
|
398
|
+
return mapping_key
|
|
399
|
+
if isinstance(mapping_key, str) and mapping_key.isdigit():
|
|
400
|
+
return int(mapping_key)
|
|
401
|
+
return str(mapping_key)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def is_numeric_key_mapping(payload: Mapping[object, object]) -> bool:
|
|
405
|
+
"""Return whether mapping keys are purely numeric/index-like."""
|
|
406
|
+
|
|
407
|
+
if not payload:
|
|
408
|
+
return False
|
|
409
|
+
|
|
410
|
+
mapping_key: object
|
|
411
|
+
for mapping_key in payload.keys():
|
|
412
|
+
if isinstance(mapping_key, bool):
|
|
413
|
+
return False
|
|
414
|
+
if isinstance(mapping_key, int):
|
|
415
|
+
continue
|
|
416
|
+
if isinstance(mapping_key, str) and mapping_key.isdigit():
|
|
417
|
+
continue
|
|
418
|
+
return False
|
|
419
|
+
return True
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def column_length_of(value: object) -> int | None:
|
|
423
|
+
"""Return sequence length when a value can act as a dataframe column."""
|
|
424
|
+
|
|
425
|
+
if isinstance(value, np.ndarray):
|
|
426
|
+
if value.ndim == 0:
|
|
427
|
+
return None
|
|
428
|
+
return int(value.shape[0])
|
|
429
|
+
if isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)):
|
|
430
|
+
return len(value)
|
|
431
|
+
return None
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def column_value_at(value: object, row_index: int) -> object:
|
|
435
|
+
"""Select one row value from a scalar/sequence column payload."""
|
|
436
|
+
|
|
437
|
+
column_length: int | None = column_length_of(value)
|
|
438
|
+
if column_length is None:
|
|
439
|
+
return value
|
|
440
|
+
if column_length == 0:
|
|
441
|
+
return None
|
|
442
|
+
|
|
443
|
+
if isinstance(value, np.ndarray):
|
|
444
|
+
selected_index: int = row_index if column_length > 1 else 0
|
|
445
|
+
return cast(object, value[selected_index])
|
|
446
|
+
|
|
447
|
+
sequence_value: Sequence[object] = cast(Sequence[object], value)
|
|
448
|
+
selected_index = row_index if column_length > 1 else 0
|
|
449
|
+
return sequence_value[selected_index]
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def flatten_payload(payload: object, *, path_prefix: str) -> list[tuple[str, object]]:
|
|
453
|
+
"""Flatten nested payload objects to ``(path, value)`` pairs."""
|
|
454
|
+
|
|
455
|
+
if isinstance(payload, np.ndarray):
|
|
456
|
+
return [(path_prefix if path_prefix else "value", payload)]
|
|
457
|
+
|
|
458
|
+
if isinstance(payload, Mapping):
|
|
459
|
+
rows: list[tuple[str, object]] = []
|
|
460
|
+
mapping_key: object
|
|
461
|
+
for mapping_key in sorted(payload.keys(), key=lambda item: str(item)):
|
|
462
|
+
child_value: object = payload[mapping_key]
|
|
463
|
+
child_path: str = (
|
|
464
|
+
f"{path_prefix}.{mapping_key}" if path_prefix else str(mapping_key)
|
|
465
|
+
)
|
|
466
|
+
rows.extend(flatten_payload(child_value, path_prefix=child_path))
|
|
467
|
+
return rows
|
|
468
|
+
|
|
469
|
+
if isinstance(payload, Sequence) and not isinstance(
|
|
470
|
+
payload, (str, bytes, bytearray)
|
|
471
|
+
):
|
|
472
|
+
rows = []
|
|
473
|
+
index: int
|
|
474
|
+
item: object
|
|
475
|
+
for index, item in enumerate(payload):
|
|
476
|
+
child_path = f"{path_prefix}[{index}]" if path_prefix else f"[{index}]"
|
|
477
|
+
rows.extend(flatten_payload(item, path_prefix=child_path))
|
|
478
|
+
return rows
|
|
479
|
+
|
|
480
|
+
return [(path_prefix if path_prefix else "value", payload)]
|