diff --git a/changelog.d/+refresh-converted-model.fix.md b/changelog.d/+refresh-converted-model.fix.md new file mode 100644 index 00000000..1f5fe225 --- /dev/null +++ b/changelog.d/+refresh-converted-model.fix.md @@ -0,0 +1,3 @@ +Replace stale model files when converting to PEtab v2 in an existing output +directory +([#618](https://github.com/PEtab-dev/libpetab-python/pull/618)). diff --git a/petab/v2/petab1to2.py b/petab/v2/petab1to2.py index cea4a73d..bf331950 100644 --- a/petab/v2/petab1to2.py +++ b/petab/v2/petab1to2.py @@ -439,11 +439,10 @@ def _copy_file(src: Path | str, dest: Path): dest_handle.write(src_handle.handle.read()) return - try: + with suppress(FileNotFoundError): if dest.samefile(src): return - except FileNotFoundError: - shutil.copy(str(src), str(dest)) + shutil.copy(src, dest) def v1v2_condition_df( diff --git a/tests/v2/test_conversion.py b/tests/v2/test_conversion.py index bb6c3628..88e79332 100644 --- a/tests/v2/test_conversion.py +++ b/tests/v2/test_conversion.py @@ -953,6 +953,8 @@ def _petab1to2_with_conditions( condition_df: pd.DataFrame, conditions: list[tuple[str, str]], output_dir: Path | None = None, + *, + model_parameter_value: float = 0.5, ) -> Problem: """Convert a minimal PEtab v1 problem with the given condition table. @@ -962,7 +964,7 @@ def _petab1to2_with_conditions( """ problem = v1.Problem( model=SbmlModel.from_antimony( - "A = 1; B = 0; k1 = 0.5; A -> B; k1 * A" + f"A = 1; B = 0; k1 = {model_parameter_value}; A -> B; k1 * A" ), condition_df=v1.get_condition_df(condition_df), observable_df=v1.get_observable_df( @@ -1009,6 +1011,24 @@ def _petab1to2_with_conditions( return Problem.from_yaml(output_dir / Path(yaml_file).name) +def test_petab1to2_replaces_stale_model_file(tmp_path): + """Reusing the output directory must copy the current input model.""" + conditions = pd.DataFrame({v1.C.CONDITION_ID: ["c1"]}) + output_dir = tmp_path / "v2" + output_dir.mkdir() + for value in (0.5, 0.7): + converted = _petab1to2_with_conditions( + tmp_path / f"source-{value}", + conditions, + [("", "c1")], + output_dir, + model_parameter_value=value, + ) + assert ( + converted.model.sbml_model.getParameter("k1").getValue() == value + ) + + def _measured_periods( problem: Problem, ) -> list[list[tuple[float, list[str]]] | None]: