From a0501f04a3d23ec77bbcac070173a4eba1091a1f Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Thu, 3 Apr 2025 12:58:59 +0200 Subject: [PATCH 1/2] Add v2.Problem.model_dump Convert a Problem to a dict. --- petab/v2/problem.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/petab/v2/problem.py b/petab/v2/problem.py index b0b76aa9..4d3f5b7a 100644 --- a/petab/v2/problem.py +++ b/petab/v2/problem.py @@ -1096,6 +1096,46 @@ def __iadd__(self, other): ) return self + def model_dump(self, **kwargs) -> dict[str, Any]: + """Convert this Problem to a dictionary. + + See `pydantic.BaseModel.model_dump `__ + for details. + + :example: + + >>> from pprint import pprint + >>> p = Problem() + >>> p += core.Parameter(id="par", lb=0, ub=1) + >>> pprint(p.model_dump()) + {'conditions': [], + 'config': {'extensions': [], + 'format_version': '2.0.0', + 'parameter_file': None, + 'problems': []}, + 'experiments': [], + 'mappings': [], + 'measurements': [], + 'observables': [], + 'parameters': [{'estimate': 'true', + 'id': 'par', + 'lb': 0.0, + 'nominal_value': None, + 'scale': , + 'ub': 1.0}]} + """ + res = { + "config": (self.config or ProblemConfig()).model_dump(**kwargs), + } + res |= self.mapping_table.model_dump(**kwargs) + res |= self.condition_table.model_dump(**kwargs) + res |= self.experiment_table.model_dump(**kwargs) + res |= self.observable_table.model_dump(**kwargs) + res |= self.measurement_table.model_dump(**kwargs) + res |= self.parameter_table.model_dump(**kwargs) + + return res + class ModelFile(BaseModel): """A file in the PEtab problem configuration.""" From 19359c9b46eb106f8f2148ab38f567a99410fe01 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Wed, 23 Apr 2025 10:41:46 +0200 Subject: [PATCH 2/2] doc --- petab/v2/problem.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/petab/v2/problem.py b/petab/v2/problem.py index 4d3f5b7a..294256a8 100644 --- a/petab/v2/problem.py +++ b/petab/v2/problem.py @@ -10,7 +10,7 @@ from math import nan from numbers import Number from pathlib import Path -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import pandas as pd import sympy as sp @@ -1099,6 +1099,12 @@ def __iadd__(self, other): def model_dump(self, **kwargs) -> dict[str, Any]: """Convert this Problem to a dictionary. + This function is intended for debugging purposes and should not be + used for serialization. The output of this function may change + without notice. + + The output includes all PEtab tables, but not the model itself. + See `pydantic.BaseModel.model_dump `__ for details.