Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/+refresh-converted-model.fix.md
Original file line number Diff line number Diff line change
@@ -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)).
5 changes: 2 additions & 3 deletions petab/v2/petab1to2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 21 additions & 1 deletion tests/v2/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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(
Expand Down Expand Up @@ -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]:
Expand Down