Skip to content

Commit

Permalink
v2: Adapt to long conditions table
Browse files Browse the repository at this point in the history
Update creation of PEtab problems, validation, conversion from v1 to v2,... to the new long condition table.
  • Loading branch information
dweindl committed Dec 9, 2024
1 parent d7f7e3a commit cb34ebd
Show file tree
Hide file tree
Showing 12 changed files with 472 additions and 114 deletions.
2 changes: 2 additions & 0 deletions petab/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

from ..version import __version__ # noqa: F401, E402
from . import models # noqa: F401, E402
from .C import * # noqa: F403, F401, E402
from .calculate import * # noqa: F403, F401, E402
from .composite_problem import * # noqa: F403, F401, E402
Expand All @@ -13,6 +14,7 @@
from .lint import * # noqa: F403, F401, E402
from .mapping import * # noqa: F403, F401, E402
from .measurements import * # noqa: F403, F401, E402
from .models import Model # noqa: F401, E402
from .observables import * # noqa: F403, F401, E402
from .parameter_mapping import * # noqa: F403, F401, E402
from .parameters import * # noqa: F403, F401, E402
Expand Down
2 changes: 1 addition & 1 deletion petab/v1/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _check_df(df: pd.DataFrame, req_cols: Iterable, name: str) -> None:
"""
if missing_cols := set(req_cols) - set(df.columns.values):
raise AssertionError(
f"DataFrame {name} requires the columns {missing_cols}."
f"{name.capitalize()} table requires the columns {missing_cols}."
)


Expand Down
3 changes: 2 additions & 1 deletion petab/v2/C.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@
# TODO: removed?
#: Condition name column in the condition table
CONDITION_NAME = "conditionName"

#: Column in the condition table with the ID of an entity that is changed
TARGET_ID = "targetId"
#: Column in the condition table with the type of value that is changed
Expand Down Expand Up @@ -166,6 +165,8 @@
TARGET_VALUE,
]

CONDITION_DF_REQUIRED_COLS = CONDITION_DF_COLS

# EXPERIMENTS
EXPERIMENT_DF_REQUIRED_COLS = [
EXPERIMENT_ID,
Expand Down
34 changes: 26 additions & 8 deletions petab/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@
"""
from warnings import warn

from ..v1 import * # noqa: F403, F401, E402
from .experiments import ( # noqa: F401
get_experiment_df,
write_experiment_df,
)

# import after v1
from .problem import Problem # noqa: F401
# TODO: remove v1 star imports
from ..v1.calculate import * # noqa: F403, F401, E402
from ..v1.composite_problem import * # noqa: F403, F401, E402
from ..v1.core import * # noqa: F403, F401, E402
from ..v1.format_version import __format_version__ # noqa: F401, E402
from ..v1.mapping import * # noqa: F403, F401, E402
from ..v1.measurements import * # noqa: F403, F401, E402
from ..v1.observables import * # noqa: F403, F401, E402
from ..v1.parameter_mapping import * # noqa: F403, F401, E402
from ..v1.parameters import * # noqa: F403, F401, E402
from ..v1.sampling import * # noqa: F403, F401, E402
from ..v1.sbml import * # noqa: F403, F401, E402
from ..v1.simulate import * # noqa: F403, F401, E402
from ..v1.yaml import * # noqa: F403, F401, E402

warn(
"Support for PEtab2.0 and all of petab.v2 is experimental "
"and subject to changes!",
stacklevel=1,
)

# import after v1
from ..version import __version__ # noqa: F401, E402
from . import models # noqa: F401, E402
from .conditions import * # noqa: F403, F401, E402
from .experiments import ( # noqa: F401, E402
get_experiment_df,
write_experiment_df,
)
from .lint import lint_problem # noqa: F401, E402
from .models import Model # noqa: F401, E402
from .problem import Problem # noqa: F401, E402
2 changes: 2 additions & 0 deletions petab/v2/_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"""Various internal helper functions."""
from ..v1.core import to_float_if_float # noqa: F401, E402
67 changes: 67 additions & 0 deletions petab/v2/conditions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Functions operating on the PEtab condition table"""
from __future__ import annotations

from pathlib import Path

import pandas as pd
import sympy as sp

from .. import v2
from ..v1.math import sympify_petab
from .C import *
from .lint import assert_no_leading_trailing_whitespace

__all__ = [
"get_condition_df",
"write_condition_df",
]


def get_condition_df(
condition_file: str | pd.DataFrame | Path | None,
) -> pd.DataFrame | None:
"""Read the provided condition file into a ``pandas.Dataframe``.
Arguments:
condition_file: File name of PEtab condition file or pandas.Dataframe
"""
if condition_file is None:
return condition_file

if isinstance(condition_file, str | Path):
condition_file = pd.read_csv(
condition_file, sep="\t", float_precision="round_trip"
)

assert_no_leading_trailing_whitespace(
condition_file.columns.values, "condition"
)

return condition_file


def write_condition_df(df: pd.DataFrame, filename: str | Path) -> None:
"""Write PEtab condition table
Arguments:
df: PEtab condition table
filename: Destination file name
"""
df = get_condition_df(df)
df.to_csv(filename, sep="\t", index=False)


def get_condition_table_free_symbols(problem: v2.Problem) -> set[sp.Basic]:
"""Free symbols from condition table assignments.
Collects all free symbols from the condition table `targetValue` column.
:returns: Set of free symbols.
"""
if problem.condition_df is None:
return set()

free_symbols = set()
for target_value in problem.condition_df[TARGET_VALUE]:
free_symbols |= sympify_petab(target_value).free_symbols
return free_symbols
Loading

0 comments on commit cb34ebd

Please sign in to comment.