Skip to content

Commit 7874190

Browse files
committed
upconversion
1 parent cd6cd3e commit 7874190

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

petab/v2/petab1to2.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from contextlib import suppress
77
from itertools import chain
88
from pathlib import Path
9+
from tempfile import TemporaryDirectory
910
from urllib.parse import urlparse
1011
from uuid import uuid4
1112

@@ -27,26 +28,37 @@ def petab1to2(
2728
2829
Convert a PEtab problem from PEtab 1.0 to PEtab 2.0 format.
2930
30-
Parameters
31-
----------
32-
yaml_config: dict | Path | str
31+
:param yaml_config:
3332
The PEtab problem as dictionary or YAML file name.
34-
output_dir: Path | str
33+
:param output_dir:
3534
The output directory to save the converted PEtab problem, or ``None``,
3635
to return a :class:`petab.v2.Problem` instance.
3736
38-
Raises
39-
------
40-
ValueError
37+
:raises ValueError:
4138
If the input is invalid or does not pass linting or if the generated
4239
files do not pass linting.
4340
"""
44-
if output_dir is None:
45-
# TODO requires petab.v2.Problem
46-
raise NotImplementedError("Not implemented yet.")
47-
elif isinstance(yaml_config, dict):
48-
raise ValueError("If output_dir is given, yaml_config must be a file.")
41+
if output_dir is not None:
42+
return petab_files_1to2(yaml_config, output_dir)
4943

44+
with TemporaryDirectory() as tmp_dir:
45+
petab_files_1to2(yaml_config, tmp_dir)
46+
return v2.Problem.from_yaml(Path(tmp_dir, Path(yaml_config).name))
47+
48+
49+
def petab_files_1to2(yaml_config: Path | str, output_dir: Path | str):
50+
"""Convert PEtab files from PEtab 1.0 to PEtab 2.0.
51+
52+
53+
:param yaml_config:
54+
The PEtab problem as dictionary or YAML file name.
55+
:param output_dir:
56+
The output directory to save the converted PEtab problem.
57+
58+
:raises ValueError:
59+
If the input is invalid or does not pass linting or if the generated
60+
files do not pass linting.
61+
"""
5062
if isinstance(yaml_config, Path | str):
5163
yaml_file = str(yaml_config)
5264
path_prefix = get_path_prefix(yaml_file)
@@ -64,6 +76,7 @@ def petab1to2(
6476
if get_major_version(yaml_config) != 1:
6577
raise ValueError("PEtab problem is not version 1.")
6678
petab_problem = v1.Problem.from_yaml(yaml_file or yaml_config)
79+
# TODO: move to mapping table
6780
# get rid of conditionName column if present (unsupported in v2)
6881
petab_problem.condition_df = petab_problem.condition_df.drop(
6982
columns=[v1.C.CONDITION_NAME], errors="ignore"
@@ -317,18 +330,4 @@ def v1v2_condition_df(
317330
]
318331
)
319332

320-
targets = set(condition_df[v2.C.TARGET_ID].unique())
321-
valid_cond_pars = set(model.get_valid_parameters_for_parameter_table())
322-
# entities to which we assign constant values
323-
constant = targets & valid_cond_pars
324-
# entities to which we assign initial values
325-
initial = set()
326-
for target in targets - constant:
327-
if model.is_state_variable(target):
328-
initial.add(target)
329-
else:
330-
raise NotImplementedError(
331-
f"Unable to determine value type {target} in the condition "
332-
"table."
333-
)
334333
return condition_df

tests/v2/test_conversion.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33

44
import pytest
55

6+
from petab.v2 import Problem
67
from petab.v2.petab1to2 import petab1to2
78

89

910
def test_petab1to2_remote():
11+
"""Test that we can upgrade a remote PEtab 1.0.0 problem."""
1012
yaml_url = (
1113
"https://raw.githubusercontent.com/PEtab-dev/petab_test_suite"
1214
"/main/petabtests/cases/v1.0.0/sbml/0001/_0001.yaml"
1315
)
1416

15-
with tempfile.TemporaryDirectory(prefix="test_petab1to2") as tmpdirname:
16-
# TODO verify that the v2 files match "ground truth"
17-
# in `petabtests/cases/v2.0.0/sbml/0001/_0001.yaml`
18-
petab1to2(yaml_url, tmpdirname)
17+
problem = petab1to2(yaml_url)
18+
assert isinstance(problem, Problem)
19+
assert len(problem.measurement_table.measurements)
1920

2021

2122
try:

0 commit comments

Comments
 (0)