Skip to content

Commit 258abc9

Browse files
authored
Add v2.Problem.model_dump (#372)
Convert a Problem to a dict.
1 parent 8b4bf3f commit 258abc9

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

petab/v2/problem.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from math import nan
1111
from numbers import Number
1212
from pathlib import Path
13-
from typing import TYPE_CHECKING
13+
from typing import TYPE_CHECKING, Any
1414

1515
import pandas as pd
1616
import sympy as sp
@@ -1096,6 +1096,52 @@ def __iadd__(self, other):
10961096
)
10971097
return self
10981098

1099+
def model_dump(self, **kwargs) -> dict[str, Any]:
1100+
"""Convert this Problem to a dictionary.
1101+
1102+
This function is intended for debugging purposes and should not be
1103+
used for serialization. The output of this function may change
1104+
without notice.
1105+
1106+
The output includes all PEtab tables, but not the model itself.
1107+
1108+
See `pydantic.BaseModel.model_dump <https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_dump>`__
1109+
for details.
1110+
1111+
:example:
1112+
1113+
>>> from pprint import pprint
1114+
>>> p = Problem()
1115+
>>> p += core.Parameter(id="par", lb=0, ub=1)
1116+
>>> pprint(p.model_dump())
1117+
{'conditions': [],
1118+
'config': {'extensions': [],
1119+
'format_version': '2.0.0',
1120+
'parameter_file': None,
1121+
'problems': []},
1122+
'experiments': [],
1123+
'mappings': [],
1124+
'measurements': [],
1125+
'observables': [],
1126+
'parameters': [{'estimate': 'true',
1127+
'id': 'par',
1128+
'lb': 0.0,
1129+
'nominal_value': None,
1130+
'scale': <ParameterScale.LIN: 'lin'>,
1131+
'ub': 1.0}]}
1132+
"""
1133+
res = {
1134+
"config": (self.config or ProblemConfig()).model_dump(**kwargs),
1135+
}
1136+
res |= self.mapping_table.model_dump(**kwargs)
1137+
res |= self.condition_table.model_dump(**kwargs)
1138+
res |= self.experiment_table.model_dump(**kwargs)
1139+
res |= self.observable_table.model_dump(**kwargs)
1140+
res |= self.measurement_table.model_dump(**kwargs)
1141+
res |= self.parameter_table.model_dump(**kwargs)
1142+
1143+
return res
1144+
10991145

11001146
class ModelFile(BaseModel):
11011147
"""A file in the PEtab problem configuration."""

0 commit comments

Comments
 (0)