Skip to content

Commit cb34ebd

Browse files
committed
v2: Adapt to long conditions table
Update creation of PEtab problems, validation, conversion from v1 to v2,... to the new long condition table.
1 parent d7f7e3a commit cb34ebd

File tree

12 files changed

+472
-114
lines changed

12 files changed

+472
-114
lines changed

petab/v1/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
from ..version import __version__ # noqa: F401, E402
7+
from . import models # noqa: F401, E402
78
from .C import * # noqa: F403, F401, E402
89
from .calculate import * # noqa: F403, F401, E402
910
from .composite_problem import * # noqa: F403, F401, E402
@@ -13,6 +14,7 @@
1314
from .lint import * # noqa: F403, F401, E402
1415
from .mapping import * # noqa: F403, F401, E402
1516
from .measurements import * # noqa: F403, F401, E402
17+
from .models import Model # noqa: F401, E402
1618
from .observables import * # noqa: F403, F401, E402
1719
from .parameter_mapping import * # noqa: F403, F401, E402
1820
from .parameters import * # noqa: F403, F401, E402

petab/v1/lint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _check_df(df: pd.DataFrame, req_cols: Iterable, name: str) -> None:
6767
"""
6868
if missing_cols := set(req_cols) - set(df.columns.values):
6969
raise AssertionError(
70-
f"DataFrame {name} requires the columns {missing_cols}."
70+
f"{name.capitalize()} table requires the columns {missing_cols}."
7171
)
7272

7373

petab/v2/C.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@
136136
# TODO: removed?
137137
#: Condition name column in the condition table
138138
CONDITION_NAME = "conditionName"
139-
140139
#: Column in the condition table with the ID of an entity that is changed
141140
TARGET_ID = "targetId"
142141
#: Column in the condition table with the type of value that is changed
@@ -166,6 +165,8 @@
166165
TARGET_VALUE,
167166
]
168167

168+
CONDITION_DF_REQUIRED_COLS = CONDITION_DF_COLS
169+
169170
# EXPERIMENTS
170171
EXPERIMENT_DF_REQUIRED_COLS = [
171172
EXPERIMENT_ID,

petab/v2/__init__.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,35 @@
44
"""
55
from warnings import warn
66

7-
from ..v1 import * # noqa: F403, F401, E402
8-
from .experiments import ( # noqa: F401
9-
get_experiment_df,
10-
write_experiment_df,
11-
)
12-
13-
# import after v1
14-
from .problem import Problem # noqa: F401
7+
# TODO: remove v1 star imports
8+
from ..v1.calculate import * # noqa: F403, F401, E402
9+
from ..v1.composite_problem import * # noqa: F403, F401, E402
10+
from ..v1.core import * # noqa: F403, F401, E402
11+
from ..v1.format_version import __format_version__ # noqa: F401, E402
12+
from ..v1.mapping import * # noqa: F403, F401, E402
13+
from ..v1.measurements import * # noqa: F403, F401, E402
14+
from ..v1.observables import * # noqa: F403, F401, E402
15+
from ..v1.parameter_mapping import * # noqa: F403, F401, E402
16+
from ..v1.parameters import * # noqa: F403, F401, E402
17+
from ..v1.sampling import * # noqa: F403, F401, E402
18+
from ..v1.sbml import * # noqa: F403, F401, E402
19+
from ..v1.simulate import * # noqa: F403, F401, E402
20+
from ..v1.yaml import * # noqa: F403, F401, E402
1521

1622
warn(
1723
"Support for PEtab2.0 and all of petab.v2 is experimental "
1824
"and subject to changes!",
1925
stacklevel=1,
2026
)
27+
28+
# import after v1
29+
from ..version import __version__ # noqa: F401, E402
30+
from . import models # noqa: F401, E402
31+
from .conditions import * # noqa: F403, F401, E402
32+
from .experiments import ( # noqa: F401, E402
33+
get_experiment_df,
34+
write_experiment_df,
35+
)
36+
from .lint import lint_problem # noqa: F401, E402
37+
from .models import Model # noqa: F401, E402
38+
from .problem import Problem # noqa: F401, E402

petab/v2/_helpers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"""Various internal helper functions."""
2+
from ..v1.core import to_float_if_float # noqa: F401, E402

petab/v2/conditions.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""Functions operating on the PEtab condition table"""
2+
from __future__ import annotations
3+
4+
from pathlib import Path
5+
6+
import pandas as pd
7+
import sympy as sp
8+
9+
from .. import v2
10+
from ..v1.math import sympify_petab
11+
from .C import *
12+
from .lint import assert_no_leading_trailing_whitespace
13+
14+
__all__ = [
15+
"get_condition_df",
16+
"write_condition_df",
17+
]
18+
19+
20+
def get_condition_df(
21+
condition_file: str | pd.DataFrame | Path | None,
22+
) -> pd.DataFrame | None:
23+
"""Read the provided condition file into a ``pandas.Dataframe``.
24+
25+
Arguments:
26+
condition_file: File name of PEtab condition file or pandas.Dataframe
27+
"""
28+
if condition_file is None:
29+
return condition_file
30+
31+
if isinstance(condition_file, str | Path):
32+
condition_file = pd.read_csv(
33+
condition_file, sep="\t", float_precision="round_trip"
34+
)
35+
36+
assert_no_leading_trailing_whitespace(
37+
condition_file.columns.values, "condition"
38+
)
39+
40+
return condition_file
41+
42+
43+
def write_condition_df(df: pd.DataFrame, filename: str | Path) -> None:
44+
"""Write PEtab condition table
45+
46+
Arguments:
47+
df: PEtab condition table
48+
filename: Destination file name
49+
"""
50+
df = get_condition_df(df)
51+
df.to_csv(filename, sep="\t", index=False)
52+
53+
54+
def get_condition_table_free_symbols(problem: v2.Problem) -> set[sp.Basic]:
55+
"""Free symbols from condition table assignments.
56+
57+
Collects all free symbols from the condition table `targetValue` column.
58+
59+
:returns: Set of free symbols.
60+
"""
61+
if problem.condition_df is None:
62+
return set()
63+
64+
free_symbols = set()
65+
for target_value in problem.condition_df[TARGET_VALUE]:
66+
free_symbols |= sympify_petab(target_value).free_symbols
67+
return free_symbols

0 commit comments

Comments
 (0)