Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion petab/v2/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1096,6 +1096,52 @@ def __iadd__(self, other):
)
return self

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 <https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_dump>`__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe note that only the tables are round-trip, not the SBML model.

Add a test that it is round-trip? Or is there a different purpose?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is there a different purpose?

So far, this is mostly to get a quick overview of some (small) PEtab problem without calling each model_dump on the attributes individually.
I don't think we need additional full serialization on top of the regular PEtab yaml/tsv format, or do we? Problem is pickleable already.
Maybe I should make it private or label it experimental for now?
Will add a comment regarding the model.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is how we want to distribute PETab problems via the web.
Personally I would prefer the COMBINE archive approach, i.e., adding all files to the archive and then sending the archives as files. In this case no full serializability to JSON would be required. The main issue is the serialization of the models.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we should prefer COMBINE archives.

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': <ParameterScale.LIN: 'lin'>,
'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."""
Expand Down