99import pandas as pd
1010from pandas .io .common import get_handle , is_url
1111
12- import petab .v1 .C
13- from petab .models import MODEL_TYPE_SBML
14- from petab .v1 import Problem as ProblemV1
15- from petab .yaml import get_path_prefix
16-
1712from .. import v1 , v2
18- from ..v1 .yaml import load_yaml , validate , write_yaml
13+ from ..v1 import Problem as ProblemV1
14+ from ..v1 .yaml import get_path_prefix , load_yaml , validate , write_yaml
15+ from ..v2 .models import MODEL_TYPE_SBML
1916from ..versions import get_major_version
2017
2118__all__ = ["petab1to2" ]
@@ -63,6 +60,10 @@ def petab1to2(yaml_config: Path | str, output_dir: Path | str = None):
6360 if get_major_version (yaml_config ) != 1 :
6461 raise ValueError ("PEtab problem is not version 1." )
6562 petab_problem = ProblemV1 .from_yaml (yaml_file or yaml_config )
63+ # get rid of conditionName column if present (unsupported in v2)
64+ petab_problem .condition_df = petab_problem .condition_df .drop (
65+ columns = [v1 .C .CONDITION_NAME ], errors = "ignore"
66+ )
6667 if v1 .lint_problem (petab_problem ):
6768 raise ValueError ("Provided PEtab problem does not pass linting." )
6869
@@ -72,8 +73,6 @@ def petab1to2(yaml_config: Path | str, output_dir: Path | str = None):
7273 # Write new YAML file
7374 output_dir = Path (output_dir )
7475 output_dir .mkdir (parents = True , exist_ok = True )
75- new_yaml_file = output_dir / Path (yaml_file ).name
76- write_yaml (new_yaml_config , new_yaml_file )
7776
7877 # Update tables
7978 # condition tables, observable tables, SBML files, parameter table:
@@ -104,6 +103,19 @@ def petab1to2(yaml_config: Path | str, output_dir: Path | str = None):
104103 def create_experiment_id (sim_cond_id : str , preeq_cond_id : str ) -> str :
105104 if not sim_cond_id and not preeq_cond_id :
106105 return ""
106+ # check whether the conditions will exist in the v2 condition table
107+ sim_cond_exists = (
108+ petab_problem .condition_df .loc [sim_cond_id ].notna ().any ()
109+ )
110+ preeq_cond_exists = (
111+ preeq_cond_id
112+ and petab_problem .condition_df .loc [preeq_cond_id ].notna ().any ()
113+ )
114+ if not sim_cond_exists and not preeq_cond_exists :
115+ # if we have only all-NaN conditions, we don't create a new
116+ # experiment
117+ return ""
118+
107119 if preeq_cond_id :
108120 preeq_cond_id = f"{ preeq_cond_id } _"
109121 exp_id = f"experiment__{ preeq_cond_id } __{ sim_cond_id } "
@@ -126,6 +138,8 @@ def create_experiment_id(sim_cond_id: str, preeq_cond_id: str) -> str:
126138 sim_cond_id = row [v1 .C .SIMULATION_CONDITION_ID ]
127139 preeq_cond_id = row .get (v1 .C .PREEQUILIBRATION_CONDITION_ID , "" )
128140 exp_id = create_experiment_id (sim_cond_id , preeq_cond_id )
141+ if not exp_id :
142+ continue
129143 if preeq_cond_id :
130144 experiments .append (
131145 {
@@ -167,8 +181,8 @@ def create_experiment_id(sim_cond_id: str, preeq_cond_id: str) -> str:
167181 if v1 .C .PREEQUILIBRATION_CONDITION_ID in measurement_df .columns :
168182 measurement_df [
169183 v1 .C .PREEQUILIBRATION_CONDITION_ID
170- ] = measurement_df [v1 .C .PREEQUILIBRATION_CONDITION_ID ].astype (
171- str
184+ ] = measurement_df [v1 .C .PREEQUILIBRATION_CONDITION_ID ].fillna (
185+ ""
172186 )
173187 else :
174188 measurement_df [v1 .C .PREEQUILIBRATION_CONDITION_ID ] = ""
@@ -177,7 +191,7 @@ def create_experiment_id(sim_cond_id: str, preeq_cond_id: str) -> str:
177191 petab_problem .condition_df is not None
178192 and len (
179193 set (petab_problem .condition_df .columns )
180- - {petab . v1 .C .CONDITION_NAME }
194+ - {v1 .C .CONDITION_NAME }
181195 )
182196 == 0
183197 ):
@@ -209,6 +223,9 @@ def create_experiment_id(sim_cond_id: str, preeq_cond_id: str) -> str:
209223 measurement_df , get_dest_path (measurement_file )
210224 )
211225
226+ new_yaml_file = output_dir / Path (yaml_file ).name
227+ write_yaml (new_yaml_config , new_yaml_file )
228+
212229 # validate updated Problem
213230 validation_issues = v2 .lint_problem (new_yaml_file )
214231
0 commit comments