jcmsuite-pandas 1.0.0__tar.gz
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.
- jcmsuite_pandas-1.0.0/PKG-INFO +18 -0
- jcmsuite_pandas-1.0.0/README.md +3 -0
- jcmsuite_pandas-1.0.0/pyproject.toml +15 -0
- jcmsuite_pandas-1.0.0/src/jcmsuite_pandas/__init__.py +4 -0
- jcmsuite_pandas-1.0.0/src/jcmsuite_pandas/batch.py +220 -0
- jcmsuite_pandas-1.0.0/src/jcmsuite_pandas/batch_postprocess.py +240 -0
- jcmsuite_pandas-1.0.0/src/jcmsuite_pandas/common.py +80 -0
- jcmsuite_pandas-1.0.0/src/jcmsuite_pandas/keys.py +29 -0
- jcmsuite_pandas-1.0.0/src/jcmsuite_pandas/rows.py +556 -0
- jcmsuite_pandas-1.0.0/src/jcmsuite_pandas/single.py +86 -0
- jcmsuite_pandas-1.0.0/src/jcmsuite_pandas/tables.py +56 -0
- jcmsuite_pandas-1.0.0/src/jcmsuite_pandas/value_tables.py +37 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jcmsuite-pandas
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Optional pandas exports for the matching JCMsuite Python interface.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.13
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
11
|
+
Requires-Dist: jcmsuite (==1.0.0)
|
|
12
|
+
Requires-Dist: pandas (>=2.2.3,<4.0)
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# JCMsuite pandas exports
|
|
16
|
+
|
|
17
|
+
Install the matching companion with `jcmsuite[pandas]` to use DataFrame exports.
|
|
18
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "jcmsuite-pandas"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Optional pandas exports for the matching JCMsuite Python interface."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
requires-python = ">=3.13"
|
|
8
|
+
dependencies = ["jcmsuite==1.0.0", "pandas>=2.2.3,<4.0"]
|
|
9
|
+
|
|
10
|
+
[tool.poetry]
|
|
11
|
+
packages = [{ include = "jcmsuite_pandas", from = "src" }]
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
requires = ["poetry-core"]
|
|
15
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Pandas materialization for batch result exports."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Sequence
|
|
9
|
+
from typing import TYPE_CHECKING, Protocol, cast
|
|
10
|
+
|
|
11
|
+
from jcmsuite.exceptions import InvalidConfigurationError
|
|
12
|
+
from jcmsuite.results.artifacts import Eigenvalues, PostprocessCollection
|
|
13
|
+
from jcmsuite.results.internal.export.pandas.common import (
|
|
14
|
+
expanded_keys,
|
|
15
|
+
is_field_postprocess_record,
|
|
16
|
+
postprocess_value_mapping,
|
|
17
|
+
)
|
|
18
|
+
from jcmsuite.results.internal.export.pandas.payload import (
|
|
19
|
+
payload_to_tabular_rows,
|
|
20
|
+
postprocess_payload_to_long_rows,
|
|
21
|
+
value_mapping,
|
|
22
|
+
)
|
|
23
|
+
from jcmsuite.results.payloads import TablePayload
|
|
24
|
+
|
|
25
|
+
from .common import dataframe_rows, normalize_optional_index_columns
|
|
26
|
+
|
|
27
|
+
if TYPE_CHECKING:
|
|
28
|
+
import pandas as pd
|
|
29
|
+
|
|
30
|
+
from jcmsuite.results.artifacts import PostprocessRecord
|
|
31
|
+
from jcmsuite.results.internal.models.batch import BatchExportRecord
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class _PandasModule(Protocol):
|
|
35
|
+
DataFrame: type["pd.DataFrame"]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def batch_costs_to_df(
|
|
39
|
+
entries: Sequence["BatchExportRecord"], pandas_module: _PandasModule
|
|
40
|
+
) -> "pd.DataFrame":
|
|
41
|
+
rows: list[dict[str, object]] = []
|
|
42
|
+
entry: "BatchExportRecord"
|
|
43
|
+
for entry in entries:
|
|
44
|
+
if entry.payload is None or not isinstance(entry.payload, TablePayload):
|
|
45
|
+
continue
|
|
46
|
+
payload: TablePayload = entry.payload
|
|
47
|
+
tabular_rows = payload_to_tabular_rows(value_mapping(payload.to_dict()))
|
|
48
|
+
if tabular_rows is None:
|
|
49
|
+
raise InvalidConfigurationError(
|
|
50
|
+
"Batch costs payload is not tabular and cannot be exported via to_df()."
|
|
51
|
+
)
|
|
52
|
+
key_columns: dict[str, object] = expanded_keys(entry.keys)
|
|
53
|
+
row_index: int
|
|
54
|
+
row_data: dict[str, object]
|
|
55
|
+
for row_index, row_data in enumerate(tabular_rows):
|
|
56
|
+
merged_row: dict[str, object] = {
|
|
57
|
+
"batch_index": entry.index,
|
|
58
|
+
"row_index": row_index,
|
|
59
|
+
}
|
|
60
|
+
merged_row.update(key_columns)
|
|
61
|
+
merged_row.update(row_data)
|
|
62
|
+
rows.append(merged_row)
|
|
63
|
+
dataframe = pandas_module.DataFrame(rows)
|
|
64
|
+
return cast("pd.DataFrame", normalize_optional_index_columns(dataframe))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def batch_keys_to_df_long(
|
|
68
|
+
entries: Sequence["BatchExportRecord"], pandas_module: _PandasModule
|
|
69
|
+
) -> "pd.DataFrame":
|
|
70
|
+
rows: list[dict[str, object]] = []
|
|
71
|
+
entry: "BatchExportRecord"
|
|
72
|
+
for entry in entries:
|
|
73
|
+
for key_name, key_value in sorted(entry.keys.items()):
|
|
74
|
+
rows.append(
|
|
75
|
+
{"batch_index": entry.index, "name": key_name, "value": key_value}
|
|
76
|
+
)
|
|
77
|
+
return cast("pd.DataFrame", pandas_module.DataFrame(rows))
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def batch_eigenvalues_to_df(
|
|
81
|
+
entries: Sequence["BatchExportRecord"], pandas_module: _PandasModule
|
|
82
|
+
) -> "pd.DataFrame":
|
|
83
|
+
rows: list[dict[str, object]] = []
|
|
84
|
+
expected_columns: tuple[str, ...] | None = None
|
|
85
|
+
entry: "BatchExportRecord"
|
|
86
|
+
for entry in entries:
|
|
87
|
+
if not isinstance(entry.payload, Eigenvalues):
|
|
88
|
+
continue
|
|
89
|
+
tabular_rows = payload_to_tabular_rows(value_mapping(entry.payload.to_dict()))
|
|
90
|
+
if tabular_rows is None:
|
|
91
|
+
raise InvalidConfigurationError(
|
|
92
|
+
"Batch eigenvalue payload is not tabular and cannot be exported via to_df()."
|
|
93
|
+
)
|
|
94
|
+
columns: tuple[str, ...] = (
|
|
95
|
+
tuple(str(column_name) for column_name in tabular_rows[0])
|
|
96
|
+
if tabular_rows
|
|
97
|
+
else ()
|
|
98
|
+
)
|
|
99
|
+
if expected_columns is None:
|
|
100
|
+
expected_columns = columns
|
|
101
|
+
elif columns != expected_columns:
|
|
102
|
+
raise InvalidConfigurationError(
|
|
103
|
+
"Batch eigenvalue payloads have incompatible table schemas."
|
|
104
|
+
)
|
|
105
|
+
key_columns: dict[str, object] = expanded_keys(entry.keys)
|
|
106
|
+
row_index: int
|
|
107
|
+
row_data: dict[str, object]
|
|
108
|
+
for row_index, row_data in enumerate(tabular_rows):
|
|
109
|
+
merged_row: dict[str, object] = {
|
|
110
|
+
"batch_index": entry.index,
|
|
111
|
+
"row_index": row_index,
|
|
112
|
+
}
|
|
113
|
+
merged_row.update(key_columns)
|
|
114
|
+
merged_row.update(row_data)
|
|
115
|
+
rows.append(merged_row)
|
|
116
|
+
dataframe = pandas_module.DataFrame(rows)
|
|
117
|
+
return cast("pd.DataFrame", normalize_optional_index_columns(dataframe))
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def batch_postprocesses_to_dfs(
|
|
121
|
+
entries: Sequence["BatchExportRecord"],
|
|
122
|
+
pandas_module: _PandasModule,
|
|
123
|
+
*,
|
|
124
|
+
warn_skipped: bool = True,
|
|
125
|
+
) -> dict[str, "pd.DataFrame"]:
|
|
126
|
+
import warnings
|
|
127
|
+
|
|
128
|
+
grouped_rows: dict[str, list[dict[str, object]]] = {}
|
|
129
|
+
entry: "BatchExportRecord"
|
|
130
|
+
for entry in entries:
|
|
131
|
+
if not isinstance(entry.payload, PostprocessCollection):
|
|
132
|
+
continue
|
|
133
|
+
postprocess_index: int
|
|
134
|
+
postprocess_record: "PostprocessRecord"
|
|
135
|
+
for postprocess_index, postprocess_record in enumerate(entry.payload):
|
|
136
|
+
if is_field_postprocess_record(postprocess_record):
|
|
137
|
+
if warn_skipped:
|
|
138
|
+
warnings.warn(
|
|
139
|
+
f"Skipping field-export postprocess {postprocess_record.title!r} in to_dfs().",
|
|
140
|
+
stacklevel=2,
|
|
141
|
+
)
|
|
142
|
+
continue
|
|
143
|
+
payload_mapping = postprocess_value_mapping(postprocess_record)
|
|
144
|
+
payload_object: object = (
|
|
145
|
+
payload_mapping
|
|
146
|
+
if payload_mapping is not None
|
|
147
|
+
else postprocess_record._raw
|
|
148
|
+
)
|
|
149
|
+
tabular_rows = postprocess_payload_to_long_rows(payload_object)
|
|
150
|
+
if tabular_rows is None:
|
|
151
|
+
if warn_skipped:
|
|
152
|
+
warnings.warn(
|
|
153
|
+
f"Skipping non-tabular postprocess {postprocess_record.title!r} in to_dfs().",
|
|
154
|
+
stacklevel=2,
|
|
155
|
+
)
|
|
156
|
+
continue
|
|
157
|
+
title_key = (
|
|
158
|
+
postprocess_record.title
|
|
159
|
+
if postprocess_record.title is not None
|
|
160
|
+
else f"postprocess_{postprocess_index}"
|
|
161
|
+
)
|
|
162
|
+
bucket = grouped_rows.setdefault(title_key, [])
|
|
163
|
+
key_columns: dict[str, object] = expanded_keys(entry.keys)
|
|
164
|
+
row_data: dict[str, object]
|
|
165
|
+
for row_data in tabular_rows:
|
|
166
|
+
merged_row: dict[str, object] = {
|
|
167
|
+
"index": entry.index,
|
|
168
|
+
"postprocess_index": postprocess_index,
|
|
169
|
+
"postprocess_title": postprocess_record.title,
|
|
170
|
+
"source_file": str(postprocess_record.source_file),
|
|
171
|
+
}
|
|
172
|
+
merged_row.update(key_columns)
|
|
173
|
+
merged_row.update(row_data)
|
|
174
|
+
bucket.append(merged_row)
|
|
175
|
+
return {
|
|
176
|
+
title: cast(
|
|
177
|
+
"pd.DataFrame",
|
|
178
|
+
normalize_optional_index_columns(pandas_module.DataFrame(rows)),
|
|
179
|
+
)
|
|
180
|
+
for title, rows in grouped_rows.items()
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def batch_postprocesses_to_df(
|
|
185
|
+
entries: Sequence["BatchExportRecord"],
|
|
186
|
+
pandas_module: _PandasModule,
|
|
187
|
+
*,
|
|
188
|
+
index: int,
|
|
189
|
+
) -> "pd.DataFrame":
|
|
190
|
+
rows: list[dict[str, object]] = []
|
|
191
|
+
matching_index_seen = False
|
|
192
|
+
entry: "BatchExportRecord"
|
|
193
|
+
for entry in entries:
|
|
194
|
+
if not isinstance(entry.payload, PostprocessCollection):
|
|
195
|
+
continue
|
|
196
|
+
postprocess_index: int
|
|
197
|
+
postprocess_record: "PostprocessRecord"
|
|
198
|
+
for postprocess_index, postprocess_record in enumerate(entry.payload):
|
|
199
|
+
if postprocess_index != index:
|
|
200
|
+
continue
|
|
201
|
+
matching_index_seen = True
|
|
202
|
+
tabular_rows = dataframe_rows(postprocess_record.to_df(orient="long"))
|
|
203
|
+
key_columns = expanded_keys(entry.keys)
|
|
204
|
+
row_data: dict[str, object]
|
|
205
|
+
for row_data in tabular_rows:
|
|
206
|
+
merged_row: dict[str, object] = {
|
|
207
|
+
"index": entry.index,
|
|
208
|
+
"postprocess_index": postprocess_index,
|
|
209
|
+
"postprocess_title": postprocess_record.title,
|
|
210
|
+
"source_file": str(postprocess_record.source_file),
|
|
211
|
+
}
|
|
212
|
+
merged_row.update(key_columns)
|
|
213
|
+
merged_row.update(row_data)
|
|
214
|
+
rows.append(merged_row)
|
|
215
|
+
if not matching_index_seen:
|
|
216
|
+
raise InvalidConfigurationError(
|
|
217
|
+
f"No postprocess records found for index {index!r}."
|
|
218
|
+
)
|
|
219
|
+
dataframe = pandas_module.DataFrame(rows)
|
|
220
|
+
return cast("pd.DataFrame", normalize_optional_index_columns(dataframe))
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Pandas materialization for batch postprocess exports."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
9
|
+
from typing import TYPE_CHECKING, Protocol, cast
|
|
10
|
+
|
|
11
|
+
from jcmsuite.exceptions import InvalidConfigurationError
|
|
12
|
+
from jcmsuite.results.artifacts import PostprocessHeader
|
|
13
|
+
from jcmsuite.results.internal.export.pandas.common import (
|
|
14
|
+
expanded_keys,
|
|
15
|
+
)
|
|
16
|
+
from jcmsuite.results.payloads import DataFrameLongExportMixin, DictionaryExportMixin
|
|
17
|
+
|
|
18
|
+
from .common import dataframe_rows, normalize_optional_index_columns
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
import pandas as pd
|
|
22
|
+
|
|
23
|
+
from jcmsuite.results.internal.models.batch import BatchExportRecord
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class _PostprocessRecord(Protocol):
|
|
27
|
+
@property
|
|
28
|
+
def available(self) -> bool: ...
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def title(self) -> str | None: ...
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def source_file(self) -> object: ...
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def header(self) -> PostprocessHeader | None: ...
|
|
38
|
+
|
|
39
|
+
def to_df(self, orient: str | None = None) -> "pd.DataFrame": ...
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class _BatchEntry(Protocol):
|
|
43
|
+
@property
|
|
44
|
+
def index(self) -> int: ...
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def keys(self) -> Mapping[str, object]: ...
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class _ResolvedPostprocessRecord(Protocol):
|
|
51
|
+
@property
|
|
52
|
+
def entry(self) -> _BatchEntry: ...
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def postprocess_index(self) -> int: ...
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def record(self) -> _PostprocessRecord: ...
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class _PandasModule(Protocol):
|
|
62
|
+
DataFrame: type["pd.DataFrame"]
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def batch_postprocess_table_payload_to_df(
|
|
66
|
+
records: Sequence["BatchExportRecord"],
|
|
67
|
+
postprocess_index: int,
|
|
68
|
+
load_pandas_module: Callable[[], _PandasModule],
|
|
69
|
+
) -> "pd.DataFrame":
|
|
70
|
+
"""Materialize already-selected table payloads with batch identity."""
|
|
71
|
+
|
|
72
|
+
rows: list[dict[str, object]] = []
|
|
73
|
+
record: "BatchExportRecord"
|
|
74
|
+
for record in records:
|
|
75
|
+
if record.payload is None:
|
|
76
|
+
continue
|
|
77
|
+
payload_mapping = cast(DictionaryExportMixin, record.payload).to_dict()
|
|
78
|
+
title: object = payload_mapping.get("title")
|
|
79
|
+
record_rows = dataframe_rows(
|
|
80
|
+
cast(DataFrameLongExportMixin, record.payload)._to_df_long()
|
|
81
|
+
)
|
|
82
|
+
row: dict[str, object]
|
|
83
|
+
for row in record_rows:
|
|
84
|
+
rows.append(
|
|
85
|
+
{
|
|
86
|
+
"batch_index": record.index,
|
|
87
|
+
"postprocess_index": postprocess_index,
|
|
88
|
+
"postprocess_title": title,
|
|
89
|
+
**row,
|
|
90
|
+
}
|
|
91
|
+
)
|
|
92
|
+
dataframe = load_pandas_module().DataFrame(rows)
|
|
93
|
+
return cast("pd.DataFrame", normalize_optional_index_columns(dataframe))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def batch_postprocess_structured_payload_to_df(
|
|
97
|
+
records: Sequence["BatchExportRecord"],
|
|
98
|
+
postprocess_index: int,
|
|
99
|
+
load_pandas_module: Callable[[], _PandasModule],
|
|
100
|
+
) -> "pd.DataFrame":
|
|
101
|
+
rows: list[dict[str, object]] = []
|
|
102
|
+
record: "BatchExportRecord"
|
|
103
|
+
for record in records:
|
|
104
|
+
if record.payload is None:
|
|
105
|
+
continue
|
|
106
|
+
payload_rows = dataframe_rows(
|
|
107
|
+
cast(DataFrameLongExportMixin, record.payload)._to_df_long()
|
|
108
|
+
)
|
|
109
|
+
rows.extend(
|
|
110
|
+
{
|
|
111
|
+
"batch_index": record.index,
|
|
112
|
+
"postprocess_index": postprocess_index,
|
|
113
|
+
**row,
|
|
114
|
+
}
|
|
115
|
+
for row in payload_rows
|
|
116
|
+
)
|
|
117
|
+
dataframe = load_pandas_module().DataFrame(rows)
|
|
118
|
+
return cast("pd.DataFrame", normalize_optional_index_columns(dataframe))
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def batch_postprocess_collection_payload_to_df(
|
|
122
|
+
records: Sequence["BatchExportRecord"],
|
|
123
|
+
postprocess_index: int,
|
|
124
|
+
table_names: Sequence[str],
|
|
125
|
+
load_pandas_module: Callable[[], _PandasModule],
|
|
126
|
+
) -> "pd.DataFrame":
|
|
127
|
+
from jcmsuite.results.internal.export.pandas.payload import (
|
|
128
|
+
postprocess_payload_to_long_rows,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
rows: list[dict[str, object]] = []
|
|
132
|
+
record: "BatchExportRecord"
|
|
133
|
+
for record in records:
|
|
134
|
+
if record.payload is None:
|
|
135
|
+
continue
|
|
136
|
+
mapping = cast(DictionaryExportMixin, record.payload).to_dict()
|
|
137
|
+
for table_index, table_name in enumerate(table_names):
|
|
138
|
+
table = mapping[table_name]
|
|
139
|
+
if not isinstance(table, Mapping):
|
|
140
|
+
raise InvalidConfigurationError(
|
|
141
|
+
"Batch table-collection payload requires named table mappings."
|
|
142
|
+
)
|
|
143
|
+
values = dict(table)
|
|
144
|
+
values.pop("title", None)
|
|
145
|
+
values.pop("header", None)
|
|
146
|
+
table_rows = postprocess_payload_to_long_rows(values)
|
|
147
|
+
if table_rows is None:
|
|
148
|
+
raise InvalidConfigurationError(
|
|
149
|
+
f"Batch table {table_name!r} is not tabular."
|
|
150
|
+
)
|
|
151
|
+
row: dict[str, object]
|
|
152
|
+
for row in table_rows:
|
|
153
|
+
rows.append(
|
|
154
|
+
{
|
|
155
|
+
"batch_index": record.index,
|
|
156
|
+
"postprocess_index": postprocess_index,
|
|
157
|
+
"table": table_name,
|
|
158
|
+
"table_index": table_index,
|
|
159
|
+
**row,
|
|
160
|
+
}
|
|
161
|
+
)
|
|
162
|
+
dataframe = load_pandas_module().DataFrame(rows)
|
|
163
|
+
return cast("pd.DataFrame", normalize_optional_index_columns(dataframe))
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def selected_batch_postprocess_to_df(
|
|
167
|
+
records: Sequence[_ResolvedPostprocessRecord], pandas_module: _PandasModule
|
|
168
|
+
) -> "pd.DataFrame":
|
|
169
|
+
rows: list[dict[str, object]] = []
|
|
170
|
+
resolved_record: _ResolvedPostprocessRecord
|
|
171
|
+
for resolved_record in records:
|
|
172
|
+
postprocess_record = resolved_record.record
|
|
173
|
+
if not postprocess_record.available:
|
|
174
|
+
raise InvalidConfigurationError(
|
|
175
|
+
f"Selected postprocess index {resolved_record.postprocess_index!r} is unavailable for batch entry {resolved_record.entry.index}."
|
|
176
|
+
)
|
|
177
|
+
tabular_rows = dataframe_rows(postprocess_record.to_df(orient="long"))
|
|
178
|
+
key_columns = expanded_keys(resolved_record.entry.keys)
|
|
179
|
+
row_data: dict[str, object]
|
|
180
|
+
for row_data in tabular_rows:
|
|
181
|
+
merged_row: dict[str, object] = {
|
|
182
|
+
"index": resolved_record.entry.index,
|
|
183
|
+
"postprocess_index": resolved_record.postprocess_index,
|
|
184
|
+
"postprocess_title": postprocess_record.title,
|
|
185
|
+
"source_file": str(postprocess_record.source_file),
|
|
186
|
+
}
|
|
187
|
+
merged_row.update(key_columns)
|
|
188
|
+
merged_row.update(row_data)
|
|
189
|
+
rows.append(merged_row)
|
|
190
|
+
dataframe = pandas_module.DataFrame(rows)
|
|
191
|
+
return cast("pd.DataFrame", normalize_optional_index_columns(dataframe))
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def selected_batch_postprocess_headers_to_df(
|
|
195
|
+
records: Sequence[_ResolvedPostprocessRecord], pandas_module: _PandasModule
|
|
196
|
+
) -> "pd.DataFrame":
|
|
197
|
+
from jcmsuite.results.internal.export.pandas.payload import (
|
|
198
|
+
postprocess_header_to_long_rows,
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
rows: list[dict[str, object]] = []
|
|
202
|
+
resolved_record: _ResolvedPostprocessRecord
|
|
203
|
+
for resolved_record in records:
|
|
204
|
+
postprocess_record = resolved_record.record
|
|
205
|
+
if not postprocess_record.available:
|
|
206
|
+
raise InvalidConfigurationError(
|
|
207
|
+
f"Selected postprocess index {resolved_record.postprocess_index!r} is unavailable for batch entry {resolved_record.entry.index}."
|
|
208
|
+
)
|
|
209
|
+
base_row: dict[str, object] = {
|
|
210
|
+
"index": resolved_record.entry.index,
|
|
211
|
+
"postprocess_index": resolved_record.postprocess_index,
|
|
212
|
+
"postprocess_title": postprocess_record.title,
|
|
213
|
+
"source_file": str(postprocess_record.source_file),
|
|
214
|
+
}
|
|
215
|
+
base_row.update(expanded_keys(resolved_record.entry.keys))
|
|
216
|
+
header_payload = postprocess_record.header
|
|
217
|
+
if header_payload is None:
|
|
218
|
+
merged_row = dict(base_row)
|
|
219
|
+
merged_row["title"] = postprocess_record.title
|
|
220
|
+
merged_row.update({"field_name": None, "field_index": None, "value": None})
|
|
221
|
+
rows.append(merged_row)
|
|
222
|
+
continue
|
|
223
|
+
header_rows = postprocess_header_to_long_rows(header_payload.table)
|
|
224
|
+
if header_rows is None:
|
|
225
|
+
raise InvalidConfigurationError(
|
|
226
|
+
"Selected postprocess header payload is not tabular."
|
|
227
|
+
)
|
|
228
|
+
if not header_rows:
|
|
229
|
+
merged_row = dict(base_row)
|
|
230
|
+
merged_row.update({"field_name": None, "field_index": None, "value": None})
|
|
231
|
+
rows.append(merged_row)
|
|
232
|
+
continue
|
|
233
|
+
header_row: dict[str, object]
|
|
234
|
+
for header_row in header_rows:
|
|
235
|
+
merged_row = dict(base_row)
|
|
236
|
+
merged_row["title"] = postprocess_record.title
|
|
237
|
+
merged_row.update(header_row)
|
|
238
|
+
rows.append(merged_row)
|
|
239
|
+
dataframe = pandas_module.DataFrame(rows)
|
|
240
|
+
return cast("pd.DataFrame", normalize_optional_index_columns(dataframe))
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""DataFrame-specific export helpers."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import math
|
|
9
|
+
from collections.abc import Iterable, Mapping
|
|
10
|
+
from typing import TYPE_CHECKING, cast
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
|
|
14
|
+
from jcmsuite.exceptions import InvalidConfigurationError
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
import pandas as pd
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def dataframe_rows(dataframe: object) -> list[dict[str, object]]:
|
|
21
|
+
"""Normalize a DataFrame-like object into record rows."""
|
|
22
|
+
|
|
23
|
+
to_dict_method: object | None = getattr(dataframe, "to_dict", None)
|
|
24
|
+
if callable(to_dict_method):
|
|
25
|
+
rows_object: object = to_dict_method(orient="records")
|
|
26
|
+
if isinstance(rows_object, list):
|
|
27
|
+
return [cast(dict[str, object], row) for row in rows_object]
|
|
28
|
+
rows_object = getattr(dataframe, "data", [])
|
|
29
|
+
if isinstance(rows_object, list):
|
|
30
|
+
return [cast(dict[str, object], row) for row in rows_object]
|
|
31
|
+
raise InvalidConfigurationError(
|
|
32
|
+
f"Unsupported DataFrame-like object for export: {type(dataframe)!r}"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def normalize_optional_index_columns(dataframe: object) -> object:
|
|
37
|
+
"""Keep optional index-like dataframe columns as `None` instead of `NaN`."""
|
|
38
|
+
|
|
39
|
+
if not hasattr(dataframe, "columns"):
|
|
40
|
+
return dataframe
|
|
41
|
+
|
|
42
|
+
import pandas as pd
|
|
43
|
+
|
|
44
|
+
pandas_dataframe = cast("pd.DataFrame", dataframe)
|
|
45
|
+
candidate_column: str
|
|
46
|
+
for candidate_column in ("field_index", "sample_index"):
|
|
47
|
+
if candidate_column not in pandas_dataframe.columns:
|
|
48
|
+
continue
|
|
49
|
+
series = pandas_dataframe[candidate_column]
|
|
50
|
+
normalized_values: list[object] = [
|
|
51
|
+
_normalize_optional_index_value(value) for value in series.tolist()
|
|
52
|
+
]
|
|
53
|
+
pandas_dataframe[candidate_column] = pd.Series(
|
|
54
|
+
normalized_values,
|
|
55
|
+
index=series.index,
|
|
56
|
+
dtype=object,
|
|
57
|
+
)
|
|
58
|
+
return pandas_dataframe
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _normalize_optional_index_value(value: object) -> object:
|
|
62
|
+
"""Normalize one optional dataframe index value."""
|
|
63
|
+
|
|
64
|
+
if value is None:
|
|
65
|
+
return None
|
|
66
|
+
if isinstance(value, (float, np.floating)) and math.isnan(float(value)):
|
|
67
|
+
return None
|
|
68
|
+
if isinstance(value, (int, np.integer)):
|
|
69
|
+
return int(value)
|
|
70
|
+
if isinstance(value, (float, np.floating)) and float(value).is_integer():
|
|
71
|
+
return int(value)
|
|
72
|
+
return value
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def rows_to_dataframe(rows: Iterable[Mapping[str, object]]) -> "pd.DataFrame":
|
|
76
|
+
"""Convert row dictionaries into a normalized pandas DataFrame."""
|
|
77
|
+
from jcmsuite.results.pandas_export import _load_pandas_module
|
|
78
|
+
|
|
79
|
+
dataframe = _load_pandas_module().DataFrame(list(rows))
|
|
80
|
+
return cast("pd.DataFrame", normalize_optional_index_columns(dataframe))
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""DataFrame conversion for single-solve project keys."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Mapping
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from jcmsuite.results.internal.export.common import materialize_rows
|
|
12
|
+
|
|
13
|
+
from .common import rows_to_dataframe
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
import pandas as pd
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def keys_wide(values: Mapping[str, object]) -> "pd.DataFrame":
|
|
20
|
+
"""Return one row with one column for each project key."""
|
|
21
|
+
return rows_to_dataframe(materialize_rows([values]))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def keys_long(values: Mapping[str, object]) -> "pd.DataFrame":
|
|
25
|
+
"""Return one row for each project key."""
|
|
26
|
+
rows: list[dict[str, object]] = [
|
|
27
|
+
{"key": key, "value": value} for key, value in values.items()
|
|
28
|
+
]
|
|
29
|
+
return rows_to_dataframe(rows)
|
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Row-builder helpers for pandas exports."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Sequence
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
from jcmsuite.results.internal.export.pandas.common import (
|
|
13
|
+
expanded_keys,
|
|
14
|
+
postprocess_value_mapping,
|
|
15
|
+
)
|
|
16
|
+
from jcmsuite.results.internal.export.pandas.payload import (
|
|
17
|
+
flatten_payload,
|
|
18
|
+
payload_to_tabular_rows,
|
|
19
|
+
value_mapping,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from jcmsuite.results.internal.models.bundle import (
|
|
24
|
+
BundledBatchSolveResult,
|
|
25
|
+
BundledSolveEntry,
|
|
26
|
+
BundledSolveResult,
|
|
27
|
+
)
|
|
28
|
+
from jcmsuite.results.models import BatchSolveResult, SolveResult
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def flatten_rows(
|
|
32
|
+
*,
|
|
33
|
+
section: str,
|
|
34
|
+
payload: object,
|
|
35
|
+
source_file: object,
|
|
36
|
+
postprocess_index: int | None,
|
|
37
|
+
postprocess_title: str | None,
|
|
38
|
+
) -> list[dict[str, object]]:
|
|
39
|
+
"""Flatten one payload into long-form section/path/value rows."""
|
|
40
|
+
|
|
41
|
+
flattened: list[tuple[str, object]] = flatten_payload(payload, path_prefix="")
|
|
42
|
+
rows: list[dict[str, object]] = []
|
|
43
|
+
path: str
|
|
44
|
+
value: object
|
|
45
|
+
for path, value in flattened:
|
|
46
|
+
rows.append(
|
|
47
|
+
{
|
|
48
|
+
"section": section,
|
|
49
|
+
"source_file": None if source_file is None else str(source_file),
|
|
50
|
+
"postprocess_index": postprocess_index,
|
|
51
|
+
"postprocess_title": postprocess_title,
|
|
52
|
+
"path": path,
|
|
53
|
+
"value": value,
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
return rows
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def tabular_rows(
|
|
60
|
+
*,
|
|
61
|
+
section: str,
|
|
62
|
+
payload: object,
|
|
63
|
+
source_file: object,
|
|
64
|
+
postprocess_index: int | None,
|
|
65
|
+
postprocess_title: str | None,
|
|
66
|
+
) -> list[dict[str, object]]:
|
|
67
|
+
"""Convert payload to tabular rows, falling back to flattened rows."""
|
|
68
|
+
|
|
69
|
+
tabular_payload_rows: list[dict[str, object]] | None = payload_to_tabular_rows(
|
|
70
|
+
payload
|
|
71
|
+
)
|
|
72
|
+
if tabular_payload_rows is None:
|
|
73
|
+
return flatten_rows(
|
|
74
|
+
section=section,
|
|
75
|
+
payload=payload,
|
|
76
|
+
source_file=source_file,
|
|
77
|
+
postprocess_index=postprocess_index,
|
|
78
|
+
postprocess_title=postprocess_title,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
rows: list[dict[str, object]] = []
|
|
82
|
+
row_data: dict[str, object]
|
|
83
|
+
for row_index, row_data in enumerate(tabular_payload_rows):
|
|
84
|
+
merged_row: dict[str, object] = {
|
|
85
|
+
"section": section,
|
|
86
|
+
"source_file": None if source_file is None else str(source_file),
|
|
87
|
+
"postprocess_index": postprocess_index,
|
|
88
|
+
"postprocess_title": postprocess_title,
|
|
89
|
+
"row_index": row_index,
|
|
90
|
+
}
|
|
91
|
+
merged_row.update(row_data)
|
|
92
|
+
rows.append(merged_row)
|
|
93
|
+
return rows
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def solve_summary_row(result: "SolveResult") -> dict[str, object]:
|
|
97
|
+
"""Build one summary dataframe row for a single solve result."""
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
"project_file": (
|
|
101
|
+
None if result.project_file is None else str(result.project_file)
|
|
102
|
+
),
|
|
103
|
+
"backend_used": result.backend_used,
|
|
104
|
+
"daemon_job_id": result.daemon_job_id,
|
|
105
|
+
"file": None if result.file is None else str(result.file),
|
|
106
|
+
"has_costs": result.costs is not None,
|
|
107
|
+
"has_eigenvalues": result.eigenvalues is not None,
|
|
108
|
+
"postprocess_count": len(result.postprocesses),
|
|
109
|
+
"postprocess_titles": result.postprocesses.titles(),
|
|
110
|
+
"template_render_count": len(result.template_renders),
|
|
111
|
+
"log_return_code": None if result.log is None else result.log.return_code,
|
|
112
|
+
"log_streamed_to_console": (
|
|
113
|
+
None if result.log is None else result.log.streamed_to_console
|
|
114
|
+
),
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def solve_long_rows(result: "SolveResult") -> list[dict[str, object]]:
|
|
119
|
+
"""Flatten one solve result into long-form ``section/path/value`` rows."""
|
|
120
|
+
|
|
121
|
+
rows: list[dict[str, object]] = []
|
|
122
|
+
summary_mapping: dict[str, object] = solve_summary_row(result)
|
|
123
|
+
rows.extend(
|
|
124
|
+
flatten_rows(
|
|
125
|
+
section="summary",
|
|
126
|
+
payload=summary_mapping,
|
|
127
|
+
source_file=None,
|
|
128
|
+
postprocess_index=None,
|
|
129
|
+
postprocess_title=None,
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
if result.costs is not None:
|
|
134
|
+
rows.extend(
|
|
135
|
+
flatten_rows(
|
|
136
|
+
section="costs",
|
|
137
|
+
payload=value_mapping(result.costs.to_dict()),
|
|
138
|
+
source_file=None,
|
|
139
|
+
postprocess_index=None,
|
|
140
|
+
postprocess_title=None,
|
|
141
|
+
)
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
if result.eigenvalues is not None:
|
|
145
|
+
rows.extend(
|
|
146
|
+
flatten_rows(
|
|
147
|
+
section="eigenvalues",
|
|
148
|
+
payload=value_mapping(result.eigenvalues.to_dict()),
|
|
149
|
+
source_file=None,
|
|
150
|
+
postprocess_index=None,
|
|
151
|
+
postprocess_title=None,
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
postprocess_index: int
|
|
156
|
+
for postprocess_index, postprocess in enumerate(result.postprocesses.records):
|
|
157
|
+
if not postprocess.available:
|
|
158
|
+
continue
|
|
159
|
+
payload_mapping = postprocess_value_mapping(postprocess)
|
|
160
|
+
payload_object: object = (
|
|
161
|
+
payload_mapping if payload_mapping is not None else postprocess._raw
|
|
162
|
+
)
|
|
163
|
+
rows.extend(
|
|
164
|
+
flatten_rows(
|
|
165
|
+
section="postprocess",
|
|
166
|
+
payload=payload_object,
|
|
167
|
+
source_file=postprocess.source_file,
|
|
168
|
+
postprocess_index=postprocess_index,
|
|
169
|
+
postprocess_title=postprocess.title,
|
|
170
|
+
)
|
|
171
|
+
)
|
|
172
|
+
return rows
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def solve_value_rows(result: "SolveResult") -> list[dict[str, object]]:
|
|
176
|
+
"""Build tabular value rows for a single solve result payload."""
|
|
177
|
+
|
|
178
|
+
rows: list[dict[str, object]] = []
|
|
179
|
+
|
|
180
|
+
if result.costs is not None:
|
|
181
|
+
rows.extend(
|
|
182
|
+
tabular_rows(
|
|
183
|
+
section="costs",
|
|
184
|
+
payload=value_mapping(result.costs.to_dict()),
|
|
185
|
+
source_file=None,
|
|
186
|
+
postprocess_index=None,
|
|
187
|
+
postprocess_title=None,
|
|
188
|
+
)
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
if result.eigenvalues is not None:
|
|
192
|
+
rows.extend(
|
|
193
|
+
tabular_rows(
|
|
194
|
+
section="eigenvalues",
|
|
195
|
+
payload=value_mapping(result.eigenvalues.to_dict()),
|
|
196
|
+
source_file=None,
|
|
197
|
+
postprocess_index=None,
|
|
198
|
+
postprocess_title=None,
|
|
199
|
+
)
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
postprocess_index: int
|
|
203
|
+
for postprocess_index, postprocess in enumerate(result.postprocesses.records):
|
|
204
|
+
if not postprocess.available:
|
|
205
|
+
continue
|
|
206
|
+
payload_mapping = postprocess_value_mapping(postprocess)
|
|
207
|
+
payload_object = (
|
|
208
|
+
payload_mapping if payload_mapping is not None else postprocess._raw
|
|
209
|
+
)
|
|
210
|
+
rows.extend(
|
|
211
|
+
tabular_rows(
|
|
212
|
+
section="postprocess",
|
|
213
|
+
payload=payload_object,
|
|
214
|
+
source_file=postprocess.source_file,
|
|
215
|
+
postprocess_index=postprocess_index,
|
|
216
|
+
postprocess_title=postprocess.title,
|
|
217
|
+
)
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
return rows
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def batch_summary_rows(batch_result: "BatchSolveResult") -> list[dict[str, object]]:
|
|
224
|
+
"""Build summary dataframe rows for each batch entry."""
|
|
225
|
+
|
|
226
|
+
rows: list[dict[str, object]] = []
|
|
227
|
+
for entry in batch_result:
|
|
228
|
+
summary_row: dict[str, object] = {
|
|
229
|
+
"batch_index": entry.index,
|
|
230
|
+
"success": entry.result is not None,
|
|
231
|
+
"failure_type": (
|
|
232
|
+
None if entry.failure is None else entry.failure.exception_type.__name__
|
|
233
|
+
),
|
|
234
|
+
"failure_message": None if entry.failure is None else entry.failure.message,
|
|
235
|
+
}
|
|
236
|
+
summary_row.update(expanded_keys(entry.keys))
|
|
237
|
+
|
|
238
|
+
if entry.result is not None:
|
|
239
|
+
solve_summary: dict[str, object] = solve_summary_row(entry.result)
|
|
240
|
+
solve_key: str
|
|
241
|
+
solve_value: object
|
|
242
|
+
for solve_key, solve_value in solve_summary.items():
|
|
243
|
+
summary_row[f"result_{solve_key}"] = solve_value
|
|
244
|
+
rows.append(summary_row)
|
|
245
|
+
return rows
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def batch_long_rows(batch_result: "BatchSolveResult") -> list[dict[str, object]]:
|
|
249
|
+
"""Build long-form dataframe rows for each batch entry payload."""
|
|
250
|
+
|
|
251
|
+
rows: list[dict[str, object]] = []
|
|
252
|
+
for entry in batch_result:
|
|
253
|
+
prefix: dict[str, object] = {
|
|
254
|
+
"batch_index": entry.index,
|
|
255
|
+
"success": entry.result is not None,
|
|
256
|
+
}
|
|
257
|
+
prefix.update(expanded_keys(entry.keys))
|
|
258
|
+
|
|
259
|
+
if entry.result is None:
|
|
260
|
+
failure_row: dict[str, object] = dict(prefix)
|
|
261
|
+
failure_row.update(
|
|
262
|
+
{
|
|
263
|
+
"section": "failure",
|
|
264
|
+
"source_file": None,
|
|
265
|
+
"postprocess_index": None,
|
|
266
|
+
"postprocess_title": None,
|
|
267
|
+
"path": "message",
|
|
268
|
+
"value": None if entry.failure is None else entry.failure.message,
|
|
269
|
+
}
|
|
270
|
+
)
|
|
271
|
+
rows.append(failure_row)
|
|
272
|
+
continue
|
|
273
|
+
|
|
274
|
+
solve_row: dict[str, object]
|
|
275
|
+
for solve_row in solve_long_rows(entry.result):
|
|
276
|
+
merged_row: dict[str, object] = dict(prefix)
|
|
277
|
+
merged_row.update(solve_row)
|
|
278
|
+
rows.append(merged_row)
|
|
279
|
+
return rows
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def batch_value_rows(batch_result: "BatchSolveResult") -> list[dict[str, object]]:
|
|
283
|
+
"""Build tabular value rows for each batch entry payload."""
|
|
284
|
+
|
|
285
|
+
rows: list[dict[str, object]] = []
|
|
286
|
+
for entry in batch_result:
|
|
287
|
+
prefix: dict[str, object] = {
|
|
288
|
+
"batch_index": entry.index,
|
|
289
|
+
"success": entry.result is not None,
|
|
290
|
+
}
|
|
291
|
+
prefix.update(expanded_keys(entry.keys))
|
|
292
|
+
|
|
293
|
+
if entry.result is None:
|
|
294
|
+
failure_row: dict[str, object] = dict(prefix)
|
|
295
|
+
failure_row.update(
|
|
296
|
+
{
|
|
297
|
+
"section": "failure",
|
|
298
|
+
"row_index": 0,
|
|
299
|
+
"failure_message": (
|
|
300
|
+
None if entry.failure is None else entry.failure.message
|
|
301
|
+
),
|
|
302
|
+
}
|
|
303
|
+
)
|
|
304
|
+
rows.append(failure_row)
|
|
305
|
+
continue
|
|
306
|
+
|
|
307
|
+
solve_row: dict[str, object]
|
|
308
|
+
for solve_row in solve_value_rows(entry.result):
|
|
309
|
+
merged_row: dict[str, object] = dict(prefix)
|
|
310
|
+
merged_row.update(solve_row)
|
|
311
|
+
rows.append(merged_row)
|
|
312
|
+
return rows
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def bundled_summary_rows(
|
|
316
|
+
bundle_result: "BundledSolveResult",
|
|
317
|
+
) -> list[dict[str, object]]:
|
|
318
|
+
"""Build summary dataframe rows for each project in a bundle."""
|
|
319
|
+
|
|
320
|
+
rows: list[dict[str, object]] = []
|
|
321
|
+
project_index: int
|
|
322
|
+
bundle_entry: "BundledSolveEntry"
|
|
323
|
+
for project_index, bundle_entry in enumerate(bundle_result.entries):
|
|
324
|
+
summary_row: dict[str, object] = {
|
|
325
|
+
"project_index": project_index,
|
|
326
|
+
"project_file": str(bundle_entry.project_file),
|
|
327
|
+
}
|
|
328
|
+
summary_row.update(solve_summary_row(bundle_entry.result))
|
|
329
|
+
summary_row["project_file"] = str(bundle_entry.project_file)
|
|
330
|
+
rows.append(summary_row)
|
|
331
|
+
return rows
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def bundled_long_rows(bundle_result: "BundledSolveResult") -> list[dict[str, object]]:
|
|
335
|
+
"""Build long-form dataframe rows for each project in a bundle."""
|
|
336
|
+
|
|
337
|
+
rows: list[dict[str, object]] = []
|
|
338
|
+
project_index: int
|
|
339
|
+
bundle_entry: "BundledSolveEntry"
|
|
340
|
+
for project_index, bundle_entry in enumerate(bundle_result.entries):
|
|
341
|
+
project_prefix: dict[str, object] = {
|
|
342
|
+
"project_index": project_index,
|
|
343
|
+
"project_file": str(bundle_entry.project_file),
|
|
344
|
+
}
|
|
345
|
+
solve_row: dict[str, object]
|
|
346
|
+
for solve_row in solve_long_rows(bundle_entry.result):
|
|
347
|
+
merged_row: dict[str, object] = dict(project_prefix)
|
|
348
|
+
merged_row.update(solve_row)
|
|
349
|
+
merged_row["project_file"] = str(bundle_entry.project_file)
|
|
350
|
+
rows.append(merged_row)
|
|
351
|
+
return rows
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def bundled_value_rows(bundle_result: "BundledSolveResult") -> list[dict[str, object]]:
|
|
355
|
+
"""Build tabular value rows for each project in a bundle."""
|
|
356
|
+
|
|
357
|
+
rows: list[dict[str, object]] = []
|
|
358
|
+
project_index: int
|
|
359
|
+
bundle_entry: "BundledSolveEntry"
|
|
360
|
+
for project_index, bundle_entry in enumerate(bundle_result.entries):
|
|
361
|
+
project_prefix: dict[str, object] = {
|
|
362
|
+
"project_index": project_index,
|
|
363
|
+
"project_file": str(bundle_entry.project_file),
|
|
364
|
+
}
|
|
365
|
+
solve_row: dict[str, object]
|
|
366
|
+
for solve_row in solve_value_rows(bundle_entry.result):
|
|
367
|
+
merged_row: dict[str, object] = dict(project_prefix)
|
|
368
|
+
merged_row.update(solve_row)
|
|
369
|
+
merged_row["project_file"] = str(bundle_entry.project_file)
|
|
370
|
+
rows.append(merged_row)
|
|
371
|
+
return rows
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
def bundled_batch_summary_rows(
|
|
375
|
+
batch_bundle_result: "BundledBatchSolveResult",
|
|
376
|
+
) -> list[dict[str, object]]:
|
|
377
|
+
"""Build summary dataframe rows for a batch of bundled solves."""
|
|
378
|
+
|
|
379
|
+
rows: list[dict[str, object]] = []
|
|
380
|
+
project_files: tuple[Path | None, ...] = (
|
|
381
|
+
batch_bundle_result._require_project_axis_files()
|
|
382
|
+
)
|
|
383
|
+
project_index: int
|
|
384
|
+
project_file: Path | None
|
|
385
|
+
for project_index, project_file in enumerate(project_files):
|
|
386
|
+
projected_batch: "BatchSolveResult" = batch_bundle_result.project(project_index)
|
|
387
|
+
batch_row: dict[str, object]
|
|
388
|
+
for batch_row in batch_summary_rows(projected_batch):
|
|
389
|
+
merged_row: dict[str, object] = {
|
|
390
|
+
"project_index": project_index,
|
|
391
|
+
"project_file": str(project_file),
|
|
392
|
+
}
|
|
393
|
+
merged_row.update(batch_row)
|
|
394
|
+
rows.append(merged_row)
|
|
395
|
+
return rows
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def bundled_batch_long_rows(
|
|
399
|
+
batch_bundle_result: "BundledBatchSolveResult",
|
|
400
|
+
) -> list[dict[str, object]]:
|
|
401
|
+
"""Build long-form dataframe rows for a batch of bundled solves."""
|
|
402
|
+
|
|
403
|
+
rows: list[dict[str, object]] = []
|
|
404
|
+
project_files: tuple[Path | None, ...] = (
|
|
405
|
+
batch_bundle_result._require_project_axis_files()
|
|
406
|
+
)
|
|
407
|
+
project_index: int
|
|
408
|
+
project_file: Path | None
|
|
409
|
+
for project_index, project_file in enumerate(project_files):
|
|
410
|
+
projected_batch: "BatchSolveResult" = batch_bundle_result.project(project_index)
|
|
411
|
+
batch_row: dict[str, object]
|
|
412
|
+
for batch_row in batch_long_rows(projected_batch):
|
|
413
|
+
merged_row: dict[str, object] = {
|
|
414
|
+
"project_index": project_index,
|
|
415
|
+
"project_file": str(project_file),
|
|
416
|
+
}
|
|
417
|
+
merged_row.update(batch_row)
|
|
418
|
+
rows.append(merged_row)
|
|
419
|
+
return rows
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def bundled_batch_value_rows(
|
|
423
|
+
batch_bundle_result: "BundledBatchSolveResult",
|
|
424
|
+
) -> list[dict[str, object]]:
|
|
425
|
+
"""Build tabular value rows for a batch of bundled solves."""
|
|
426
|
+
|
|
427
|
+
rows: list[dict[str, object]] = []
|
|
428
|
+
project_files: tuple[Path | None, ...] = (
|
|
429
|
+
batch_bundle_result._require_project_axis_files()
|
|
430
|
+
)
|
|
431
|
+
project_index: int
|
|
432
|
+
project_file: Path | None
|
|
433
|
+
for project_index, project_file in enumerate(project_files):
|
|
434
|
+
projected_batch: "BatchSolveResult" = batch_bundle_result.project(project_index)
|
|
435
|
+
batch_row: dict[str, object]
|
|
436
|
+
for batch_row in batch_value_rows(projected_batch):
|
|
437
|
+
merged_row: dict[str, object] = {
|
|
438
|
+
"project_index": project_index,
|
|
439
|
+
"project_file": str(project_file),
|
|
440
|
+
}
|
|
441
|
+
merged_row.update(batch_row)
|
|
442
|
+
rows.append(merged_row)
|
|
443
|
+
return rows
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
def resultbag_summary_rows(
|
|
447
|
+
items: Sequence[tuple[dict[str, object], object]],
|
|
448
|
+
) -> list[dict[str, object]]:
|
|
449
|
+
"""Build summary dataframe rows from resultbag key/payload entries."""
|
|
450
|
+
|
|
451
|
+
from jcmsuite.results.models import SolveResult
|
|
452
|
+
|
|
453
|
+
rows: list[dict[str, object]] = []
|
|
454
|
+
row_index: int
|
|
455
|
+
keys: dict[str, object]
|
|
456
|
+
result_payload: object
|
|
457
|
+
for row_index, (keys, result_payload) in enumerate(items):
|
|
458
|
+
row: dict[str, object] = {
|
|
459
|
+
"index": row_index,
|
|
460
|
+
"result_type": type(result_payload).__name__,
|
|
461
|
+
}
|
|
462
|
+
row.update(expanded_keys(keys))
|
|
463
|
+
|
|
464
|
+
if isinstance(result_payload, SolveResult):
|
|
465
|
+
solve_summary: dict[str, object] = solve_summary_row(result_payload)
|
|
466
|
+
summary_key: str
|
|
467
|
+
summary_value: object
|
|
468
|
+
for summary_key, summary_value in solve_summary.items():
|
|
469
|
+
row[f"result_{summary_key}"] = summary_value
|
|
470
|
+
rows.append(row)
|
|
471
|
+
return rows
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def resultbag_long_rows(
|
|
475
|
+
items: Sequence[tuple[dict[str, object], object]],
|
|
476
|
+
) -> list[dict[str, object]]:
|
|
477
|
+
"""Build long-form dataframe rows from resultbag key/payload entries."""
|
|
478
|
+
|
|
479
|
+
from jcmsuite.results.models import SolveResult
|
|
480
|
+
|
|
481
|
+
rows: list[dict[str, object]] = []
|
|
482
|
+
row_index: int
|
|
483
|
+
keys: dict[str, object]
|
|
484
|
+
result_payload: object
|
|
485
|
+
for row_index, (keys, result_payload) in enumerate(items):
|
|
486
|
+
prefix: dict[str, object] = {
|
|
487
|
+
"index": row_index,
|
|
488
|
+
"result_type": type(result_payload).__name__,
|
|
489
|
+
}
|
|
490
|
+
prefix.update(expanded_keys(keys))
|
|
491
|
+
|
|
492
|
+
if isinstance(result_payload, dict):
|
|
493
|
+
payload_rows: list[dict[str, object]] = flatten_rows(
|
|
494
|
+
section="result_payload",
|
|
495
|
+
payload=result_payload,
|
|
496
|
+
source_file=None,
|
|
497
|
+
postprocess_index=None,
|
|
498
|
+
postprocess_title=None,
|
|
499
|
+
)
|
|
500
|
+
elif isinstance(result_payload, SolveResult):
|
|
501
|
+
payload_rows = solve_long_rows(result_payload)
|
|
502
|
+
else:
|
|
503
|
+
payload_rows = [
|
|
504
|
+
{
|
|
505
|
+
"section": "result_payload",
|
|
506
|
+
"source_file": None,
|
|
507
|
+
"postprocess_index": None,
|
|
508
|
+
"postprocess_title": None,
|
|
509
|
+
"path": "value",
|
|
510
|
+
"value": result_payload,
|
|
511
|
+
}
|
|
512
|
+
]
|
|
513
|
+
|
|
514
|
+
payload_row: dict[str, object]
|
|
515
|
+
for payload_row in payload_rows:
|
|
516
|
+
merged: dict[str, object] = dict(prefix)
|
|
517
|
+
merged.update(payload_row)
|
|
518
|
+
rows.append(merged)
|
|
519
|
+
return rows
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def resultbag_value_rows(
|
|
523
|
+
items: Sequence[tuple[dict[str, object], object]],
|
|
524
|
+
) -> list[dict[str, object]]:
|
|
525
|
+
"""Build tabular value rows from resultbag key/payload entries."""
|
|
526
|
+
|
|
527
|
+
from jcmsuite.results.models import SolveResult
|
|
528
|
+
|
|
529
|
+
rows: list[dict[str, object]] = []
|
|
530
|
+
row_index: int
|
|
531
|
+
keys: dict[str, object]
|
|
532
|
+
result_payload: object
|
|
533
|
+
for row_index, (keys, result_payload) in enumerate(items):
|
|
534
|
+
prefix: dict[str, object] = {
|
|
535
|
+
"index": row_index,
|
|
536
|
+
"result_type": type(result_payload).__name__,
|
|
537
|
+
}
|
|
538
|
+
prefix.update(expanded_keys(keys))
|
|
539
|
+
|
|
540
|
+
payload_rows: list[dict[str, object]]
|
|
541
|
+
if isinstance(result_payload, SolveResult):
|
|
542
|
+
payload_rows = solve_value_rows(result_payload)
|
|
543
|
+
else:
|
|
544
|
+
payload_rows = tabular_rows(
|
|
545
|
+
section="result_payload",
|
|
546
|
+
payload=result_payload,
|
|
547
|
+
source_file=None,
|
|
548
|
+
postprocess_index=None,
|
|
549
|
+
postprocess_title=None,
|
|
550
|
+
)
|
|
551
|
+
payload_row: dict[str, object]
|
|
552
|
+
for payload_row in payload_rows:
|
|
553
|
+
merged: dict[str, object] = dict(prefix)
|
|
554
|
+
merged.update(payload_row)
|
|
555
|
+
rows.append(merged)
|
|
556
|
+
return rows
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Pandas conversion for individual result payloads."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Mapping
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from jcmsuite.exceptions import InvalidConfigurationError
|
|
12
|
+
from jcmsuite.results.internal.export.common import materialize_rows
|
|
13
|
+
from jcmsuite.results.internal.export.pandas.payload import (
|
|
14
|
+
flatten_payload,
|
|
15
|
+
postprocess_header_to_long_rows,
|
|
16
|
+
postprocess_payload_to_long_rows,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
from .common import rows_to_dataframe
|
|
20
|
+
|
|
21
|
+
if TYPE_CHECKING:
|
|
22
|
+
import pandas as pd
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def structured_payload_to_dataframe(
|
|
26
|
+
values: Mapping[object, object],
|
|
27
|
+
) -> "pd.DataFrame":
|
|
28
|
+
"""Convert structured values into path/value rows."""
|
|
29
|
+
|
|
30
|
+
rows: list[dict[str, object]] = [
|
|
31
|
+
{"path": path, "value": value}
|
|
32
|
+
for path, value in flatten_payload(values, path_prefix="")
|
|
33
|
+
]
|
|
34
|
+
return rows_to_dataframe(rows)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def postprocess_payload_to_dataframe(
|
|
38
|
+
values: Mapping[object, object],
|
|
39
|
+
*,
|
|
40
|
+
title: str | None,
|
|
41
|
+
source_file: str,
|
|
42
|
+
) -> "pd.DataFrame":
|
|
43
|
+
"""Convert a single postprocess table into normalized long form."""
|
|
44
|
+
|
|
45
|
+
rows: list[dict[str, object]] | None = postprocess_payload_to_long_rows(values)
|
|
46
|
+
if rows is None:
|
|
47
|
+
raise InvalidConfigurationError("Postprocess payload is not tabular.")
|
|
48
|
+
return rows_to_dataframe(
|
|
49
|
+
materialize_rows(
|
|
50
|
+
rows,
|
|
51
|
+
prefix={"postprocess_title": title, "source_file": source_file},
|
|
52
|
+
)
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def structured_postprocess_to_dataframe(
|
|
57
|
+
values: Mapping[object, object],
|
|
58
|
+
*,
|
|
59
|
+
title: str | None,
|
|
60
|
+
source_file: str,
|
|
61
|
+
) -> "pd.DataFrame":
|
|
62
|
+
"""Convert structured postprocess values into contextual path/value rows."""
|
|
63
|
+
|
|
64
|
+
rows: list[dict[str, object]] = [
|
|
65
|
+
{"path": path, "value": value}
|
|
66
|
+
for path, value in flatten_payload(values, path_prefix="")
|
|
67
|
+
]
|
|
68
|
+
return rows_to_dataframe(
|
|
69
|
+
materialize_rows(
|
|
70
|
+
rows,
|
|
71
|
+
prefix={"postprocess_title": title, "source_file": source_file},
|
|
72
|
+
)
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def postprocess_header_to_dataframe(
|
|
77
|
+
payload: object,
|
|
78
|
+
*,
|
|
79
|
+
invalid_payload_message: str,
|
|
80
|
+
) -> "pd.DataFrame":
|
|
81
|
+
"""Convert postprocess header values into long-form rows."""
|
|
82
|
+
|
|
83
|
+
rows: list[dict[str, object]] | None = postprocess_header_to_long_rows(payload)
|
|
84
|
+
if rows is None:
|
|
85
|
+
raise InvalidConfigurationError(invalid_payload_message)
|
|
86
|
+
return rows_to_dataframe(rows)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""DataFrame export for table collections."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Mapping, Sequence
|
|
9
|
+
from typing import TYPE_CHECKING, cast
|
|
10
|
+
|
|
11
|
+
from jcmsuite.exceptions import InvalidConfigurationError
|
|
12
|
+
from jcmsuite.results.internal.export.pandas.payload import (
|
|
13
|
+
postprocess_payload_to_long_rows,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
from .common import normalize_optional_index_columns
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
import pandas as pd
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _table_collection_to_df(
|
|
23
|
+
tables: Sequence[Mapping[str | int, object]],
|
|
24
|
+
table_names: Sequence[str],
|
|
25
|
+
titles: Sequence[str | None],
|
|
26
|
+
) -> "pd.DataFrame":
|
|
27
|
+
"""Export a named table collection as one long-form DataFrame."""
|
|
28
|
+
from jcmsuite.results.pandas_export import _load_pandas_module, _PandasModule
|
|
29
|
+
|
|
30
|
+
rows: list[dict[str, object]] = []
|
|
31
|
+
table_index: int
|
|
32
|
+
table_payload: Mapping[str | int, object]
|
|
33
|
+
for table_index, table_payload in enumerate(tables):
|
|
34
|
+
normalized_payload: dict[str | int, object] = dict(table_payload)
|
|
35
|
+
normalized_payload.pop("title", None)
|
|
36
|
+
normalized_payload.pop("header", None)
|
|
37
|
+
table_rows: list[dict[str, object]] | None = postprocess_payload_to_long_rows(
|
|
38
|
+
normalized_payload
|
|
39
|
+
)
|
|
40
|
+
if table_rows is None:
|
|
41
|
+
raise InvalidConfigurationError(
|
|
42
|
+
f"Table {table_names[table_index]!r} is not tabular and cannot be exported via to_df()."
|
|
43
|
+
)
|
|
44
|
+
table_row: dict[str, object]
|
|
45
|
+
for table_row in table_rows:
|
|
46
|
+
merged_row: dict[str, object] = {
|
|
47
|
+
"table": table_names[table_index],
|
|
48
|
+
"table_index": table_index,
|
|
49
|
+
"title": titles[table_index],
|
|
50
|
+
}
|
|
51
|
+
merged_row.update(table_row)
|
|
52
|
+
rows.append(merged_row)
|
|
53
|
+
|
|
54
|
+
pandas_module: _PandasModule = _load_pandas_module()
|
|
55
|
+
dataframe = pandas_module.DataFrame(rows)
|
|
56
|
+
return cast("pd.DataFrame", normalize_optional_index_columns(dataframe))
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""DataFrame conversion for single-result value tables."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Mapping
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from jcmsuite.exceptions import InvalidConfigurationError
|
|
12
|
+
from jcmsuite.results.internal.export.common import materialize_rows
|
|
13
|
+
from jcmsuite.results.internal.export.pandas.payload import (
|
|
14
|
+
payload_to_tabular_rows,
|
|
15
|
+
postprocess_payload_to_long_rows,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from .common import rows_to_dataframe
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
import pandas as pd
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def value_table_wide(values: Mapping[object, object], *, name: str) -> "pd.DataFrame":
|
|
25
|
+
"""Return a wide DataFrame for the value table."""
|
|
26
|
+
rows: list[dict[str, object]] | None = payload_to_tabular_rows(values)
|
|
27
|
+
if rows is None:
|
|
28
|
+
raise InvalidConfigurationError(f"{name} payload is not tabular.")
|
|
29
|
+
return rows_to_dataframe(materialize_rows(rows, row_index_key="row_index"))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def value_table_long(values: Mapping[object, object], *, name: str) -> "pd.DataFrame":
|
|
33
|
+
"""Return a long DataFrame for the value table."""
|
|
34
|
+
rows: list[dict[str, object]] | None = postprocess_payload_to_long_rows(values)
|
|
35
|
+
if rows is None:
|
|
36
|
+
raise InvalidConfigurationError(f"{name} payload is not tabular.")
|
|
37
|
+
return rows_to_dataframe(materialize_rows(rows, row_index_key="row_index"))
|