Skip to content

Commit 2529bf9

Browse files
authored
Merge branch 'develop' into 330_truncated
2 parents a5d2a3d + ec58463 commit 2529bf9

File tree

16 files changed

+174
-86
lines changed

16 files changed

+174
-86
lines changed

petab/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def __getattr__(name):
2323
return attr
2424
if name == "v1":
2525
return importlib.import_module("petab.v1")
26+
if name == "v2":
27+
return importlib.import_module("petab.v2")
2628
if name != "__path__":
2729
warn(
2830
f"Accessing `petab.{name}` is deprecated and will be removed in "

petab/v1/calculate.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ def calculate_residuals_for_table(
106106
)
107107
residual_df[RESIDUAL] = residual_df[RESIDUAL].astype("float64")
108108
# matching columns
109-
compared_cols = set(MEASUREMENT_DF_COLS)
110-
compared_cols -= {MEASUREMENT}
111-
compared_cols &= set(measurement_df.columns)
112-
compared_cols &= set(simulation_df.columns)
109+
compared_cols = set(measurement_df.columns) & set(simulation_df.columns)
113110

114111
# compute noise formulas for observables
115112
noise_formulas = get_symbolic_noise_formulas(observable_df)
@@ -127,6 +124,16 @@ def calculate_residuals_for_table(
127124
raise ValueError(
128125
f"Could not find simulation for measurement {row}."
129126
)
127+
# if we have multiple matches, check that the rows are all identical
128+
elif (
129+
mask.sum() > 1
130+
and simulation_df.loc[mask].drop_duplicates().shape[0] > 1
131+
):
132+
raise ValueError(
133+
f"Multiple different simulations found for measurement "
134+
f"{row}:\n{simulation_df.loc[mask]}"
135+
)
136+
130137
simulation = simulation_df.loc[mask][SIMULATION].iloc[0]
131138
if scale:
132139
# apply scaling
@@ -343,10 +350,7 @@ def calculate_llh_for_table(
343350
llhs = []
344351

345352
# matching columns
346-
compared_cols = set(MEASUREMENT_DF_COLS)
347-
compared_cols -= {MEASUREMENT}
348-
compared_cols &= set(measurement_df.columns)
349-
compared_cols &= set(simulation_df.columns)
353+
compared_cols = set(measurement_df.columns) & set(simulation_df.columns)
350354

351355
# compute noise formulas for observables
352356
noise_formulas = get_symbolic_noise_formulas(observable_df)

petab/v1/conditions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ def write_condition_df(df: pd.DataFrame, filename: str | Path) -> None:
6060
6161
Arguments:
6262
df: PEtab condition table
63-
filename: Destination file name
63+
filename: Destination file name. The parent directory will be created
64+
if necessary.
6465
"""
6566
df = get_condition_df(df)
67+
Path(filename).parent.mkdir(parents=True, exist_ok=True)
6668
df.to_csv(filename, sep="\t", index=True)
6769

6870

petab/v1/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ def write_simulation_df(df: pd.DataFrame, filename: str | Path) -> None:
6060
6161
Arguments:
6262
df: PEtab simulation table
63-
filename: Destination file name
63+
filename: Destination file name. The parent directory will be created
64+
if necessary.
6465
"""
66+
Path(filename).parent.mkdir(parents=True, exist_ok=True)
6567
df.to_csv(filename, sep="\t", index=False)
6668

6769

petab/v1/mapping.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ def write_mapping_df(df: pd.DataFrame, filename: str | Path) -> None:
6060
6161
Arguments:
6262
df: PEtab mapping table
63-
filename: Destination file name
63+
filename: Destination file name. The parent directory will be created
64+
if necessary.
6465
"""
6566
df = get_mapping_df(df)
67+
Path(filename).parent.mkdir(parents=True, exist_ok=True)
6668
df.to_csv(filename, sep="\t", index=True)
6769

6870

petab/v1/measurements.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ def write_measurement_df(df: pd.DataFrame, filename: str | Path) -> None:
5858
5959
Arguments:
6060
df: PEtab measurement table
61-
filename: Destination file name
61+
filename: Destination file name. The parent directory will be created
62+
if necessary.
6263
"""
6364
df = get_measurement_df(df)
65+
Path(filename).parent.mkdir(parents=True, exist_ok=True)
6466
df.to_csv(filename, sep="\t", index=False)
6567

6668

petab/v1/observables.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ def write_observable_df(df: pd.DataFrame, filename: str | Path) -> None:
6767
6868
Arguments:
6969
df: PEtab observable table
70-
filename: Destination file name
70+
filename: Destination file name. The parent directory will be created
71+
if necessary.
7172
"""
7273
df = get_observable_df(df)
74+
Path(filename).parent.mkdir(parents=True, exist_ok=True)
7375
df.to_csv(filename, sep="\t", index=True)
7476

7577

petab/v1/parameters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ def write_parameter_df(df: pd.DataFrame, filename: str | Path) -> None:
112112
113113
Arguments:
114114
df: PEtab parameter table
115-
filename: Destination file name
115+
filename: Destination file name. The parent directory will be created
116+
if necessary.
116117
"""
117118
df = get_parameter_df(df)
119+
Path(filename).parent.mkdir(parents=True, exist_ok=True)
118120
df.to_csv(filename, sep="\t", index=True)
119121

120122

petab/v1/problem.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import pandas as pd
1414
from pydantic import AnyUrl, BaseModel, Field, RootModel
1515

16+
from ..versions import get_major_version
1617
from . import (
1718
conditions,
1819
core,
19-
format_version,
2020
mapping,
2121
measurements,
2222
observables,
@@ -290,13 +290,13 @@ def get_path(filename):
290290
"petab.CompositeProblem.from_yaml() instead."
291291
)
292292

293-
if yaml_config[FORMAT_VERSION] not in {"1", 1, "1.0.0", "2.0.0"}:
293+
major_version = get_major_version(yaml_config)
294+
if major_version not in {1, 2}:
294295
raise ValueError(
295296
"Provided PEtab files are of unsupported version "
296-
f"{yaml_config[FORMAT_VERSION]}. Expected "
297-
f"{format_version.__format_version__}."
297+
f"{yaml_config[FORMAT_VERSION]}."
298298
)
299-
if yaml_config[FORMAT_VERSION] == "2.0.0":
299+
if major_version == 2:
300300
warn("Support for PEtab2.0 is experimental!", stacklevel=2)
301301
warn(
302302
"Using petab.v1.Problem with PEtab2.0 is deprecated. "
@@ -321,7 +321,7 @@ def get_path(filename):
321321
if config.parameter_file
322322
else None
323323
)
324-
if config.format_version.root in [1, "1", "1.0.0"]:
324+
if major_version == 1:
325325
if len(problem0.sbml_files) > 1:
326326
# TODO https://github.com/PEtab-dev/libpetab-python/issues/6
327327
raise NotImplementedError(
@@ -1074,8 +1074,8 @@ def add_observable(
10741074
def add_parameter(
10751075
self,
10761076
id_: str,
1077-
estimated: bool | str | int = True,
1078-
nominal_value=None,
1077+
estimate: bool | str | int = True,
1078+
nominal_value: Number | None = None,
10791079
scale: str = None,
10801080
lb: Number = None,
10811081
ub: Number = None,
@@ -1089,7 +1089,7 @@ def add_parameter(
10891089
10901090
Arguments:
10911091
id_: The parameter id
1092-
estimated: Whether the parameter is estimated
1092+
estimate: Whether the parameter is estimated
10931093
nominal_value: The nominal value of the parameter
10941094
scale: The parameter scale
10951095
lb: The lower bound of the parameter
@@ -1104,12 +1104,8 @@ def add_parameter(
11041104
record = {
11051105
PARAMETER_ID: [id_],
11061106
}
1107-
if estimated is not None:
1108-
record[ESTIMATE] = [
1109-
int(estimated)
1110-
if isinstance(estimated, bool | int)
1111-
else estimated
1112-
]
1107+
if estimate is not None:
1108+
record[ESTIMATE] = [int(estimate)]
11131109
if nominal_value is not None:
11141110
record[NOMINAL_VALUE] = [nominal_value]
11151111
if scale is not None:

petab/v1/yaml.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
import yaml
1313
from pandas.io.common import get_handle
1414

15+
from ..versions import parse_version
1516
from .C import * # noqa: F403
1617

1718
# directory with PEtab yaml schema files
1819
SCHEMA_DIR = Path(__file__).parent.parent / "schemas"
1920
# map of version number to validation schema
2021
SCHEMAS = {
21-
"1": SCHEMA_DIR / "petab_schema.v1.0.0.yaml",
22-
"1.0.0": SCHEMA_DIR / "petab_schema.v1.0.0.yaml",
23-
"2.0.0": SCHEMA_DIR / "petab_schema.v2.0.0.yaml",
22+
(1, 0): SCHEMA_DIR / "petab_schema.v1.0.0.yaml",
23+
(2, 0): SCHEMA_DIR / "petab_schema.v2.0.0.yaml",
2424
}
2525

2626
__all__ = [
@@ -71,14 +71,18 @@ def validate_yaml_syntax(
7171
yaml_config = load_yaml(yaml_config)
7272

7373
if schema is None:
74-
# try get PEtab version from yaml file
74+
# try to get PEtab version from the yaml file
7575
# if this is not the available, the file is not valid anyways,
7676
# but let's still use the latest PEtab schema for full validation
77+
version = yaml_config.get(FORMAT_VERSION, None)
7778
version = (
78-
yaml_config.get(FORMAT_VERSION, None) or list(SCHEMAS.values())[-1]
79+
parse_version(version)[:2]
80+
if version
81+
else list(SCHEMAS.values())[-1]
7982
)
83+
8084
try:
81-
schema = SCHEMAS[str(version)]
85+
schema = SCHEMAS[version]
8286
except KeyError as e:
8387
raise ValueError(
8488
"Unknown PEtab version given in problem "
@@ -234,8 +238,10 @@ def write_yaml(yaml_config: dict[str, Any], filename: str | Path) -> None:
234238
235239
Arguments:
236240
yaml_config: Data to write
237-
filename: File to create
241+
filename: Destination file name. The parent directory will be created
242+
if necessary.
238243
"""
244+
Path(filename).parent.mkdir(parents=True, exist_ok=True)
239245
with open(filename, "w") as outfile:
240246
yaml.dump(
241247
yaml_config, outfile, default_flow_style=False, sort_keys=False

0 commit comments

Comments
 (0)