66from contextlib import suppress
77from itertools import chain
88from pathlib import Path
9+ from tempfile import TemporaryDirectory
910from urllib .parse import urlparse
1011from 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
0 commit comments