Skip to content

Commit bfaf9e4

Browse files
FFroehlichclaude
andcommitted
petab1to2: reuse v1 get_formula_placeholders for placeholder extraction
Use the helper that the v1 linter and the v1 override-count check use, instead of re-implementing the index ordering and the consecutive-numbering check. This also makes the placeholders consistent with v1 when sympy would cancel a placeholder from the formula. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 17e118b commit bfaf9e4

2 files changed

Lines changed: 12 additions & 24 deletions

File tree

‎petab/v2/petab1to2.py‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
import re
65
import shutil
76
import warnings
87
from contextlib import suppress
@@ -15,7 +14,6 @@
1514
from pandas.io.common import get_handle, is_url
1615

1716
from .. import v1, v2
18-
from ..v1.math import sympify_petab
1917
from ..v1.yaml import get_path_prefix, load_yaml, validate
2018
from ..versions import get_major_version
2119
from .models import MODEL_TYPE_SBML
@@ -435,29 +433,19 @@ def extract_placeholders(row: pd.Series, type_: str) -> str:
435433
if pd.isna(formula):
436434
return ""
437435

438-
t = f"{re.escape(type_)}Parameter"
439-
o = re.escape(row[v1.C.OBSERVABLE_ID])
440-
441-
pattern = re.compile(rf"(?:^|\W)({t}(\d+)_{o})(?=\W|$)")
442-
443-
expr = sympify_petab(formula)
444436
# v1 overrides are positional: the i-th override replaces
445-
# `{type_}Parameter{i}_{observableId}`. Therefore, sort by index
446-
# (not as text, which would put 10 before 2) and require 1..n.
447-
placeholders = sorted(
448-
(int(match.group(2)), str(sym))
449-
for sym in expr.free_symbols
450-
if sym.is_Symbol and (match := pattern.match(str(sym)))
451-
)
452-
if [i for i, _ in placeholders] != list(
453-
range(1, len(placeholders) + 1)
454-
):
455-
raise ValueError(
456-
f"The {type_} placeholders of observable "
457-
f"`{row[v1.C.OBSERVABLE_ID]}' are not numbered consecutively "
458-
f"starting from 1: {[p for _, p in placeholders]}."
437+
# `{type_}Parameter{i}_{observableId}`. This returns the placeholders
438+
# ordered by index and fails if they are not numbered 1..n.
439+
try:
440+
placeholders = v1.get_formula_placeholders(
441+
formula, row[v1.C.OBSERVABLE_ID], type_
459442
)
460-
return v2.C.PARAMETER_SEPARATOR.join(p for _, p in placeholders)
443+
except AssertionError as e:
444+
raise ValueError(
445+
f"Cannot convert the {type_} placeholders of observable "
446+
f"`{row[v1.C.OBSERVABLE_ID]}': {e}"
447+
) from e
448+
return v2.C.PARAMETER_SEPARATOR.join(placeholders)
461449

462450
df[v2.C.OBSERVABLE_PLACEHOLDERS] = df.apply(
463451
extract_placeholders, args=("observable",), axis=1

‎tests/v2/test_conversion.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def test_v1v2_observable_df_placeholder_gap(type_):
132132
}
133133
).set_index(v1.C.OBSERVABLE_ID)
134134

135-
with pytest.raises(ValueError, match="not numbered consecutively"):
135+
with pytest.raises(ValueError, match="Non-consecutive numbering"):
136136
v1v2_observable_df(observable_df)
137137

138138

0 commit comments

Comments
 (0)