Skip to content

Commit e78fde4

Browse files
dweindlclaude
andcommitted
petab1to2: keep the changes of each condition together
`melt` produced the v2 condition table column by column, so the changes of a condition were spread over the whole table. Now, they are contiguous, in the order of the v1 rows, and, within a condition, of the v1 columns. For the benchmark problems, only the row order of `conditions.tsv` changes; the loaded problems are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bacc686 commit e78fde4

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`petab1to2` writes the changes of each condition as contiguous rows, in the
2+
order of the PEtab v1 condition table rows and columns
3+
([#637](https://github.com/PEtab-dev/libpetab-python/pull/637))

‎petab/v2/petab1to2.py‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,27 @@ def v1v2_condition_df(
455455
# conditionName was dropped in PEtab v2
456456
condition_df = condition_df.drop(columns=[v1.C.CONDITION_NAME])
457457

458-
condition_df = condition_df.melt(
459-
id_vars=[v1.C.CONDITION_ID],
460-
var_name=v2.C.TARGET_ID,
461-
value_name=v2.C.TARGET_VALUE,
462-
).dropna(subset=[v2.C.TARGET_VALUE])
458+
# condition ID -> row index, to keep the rows of a condition together,
459+
# in the original order
460+
row_index = {
461+
condition_id: i
462+
for i, condition_id in enumerate(condition_df[v1.C.CONDITION_ID])
463+
}
464+
condition_df = (
465+
condition_df.melt(
466+
id_vars=[v1.C.CONDITION_ID],
467+
var_name=v2.C.TARGET_ID,
468+
value_name=v2.C.TARGET_VALUE,
469+
)
470+
.dropna(subset=[v2.C.TARGET_VALUE])
471+
.sort_values(
472+
v1.C.CONDITION_ID,
473+
key=lambda ids: ids.map(row_index),
474+
# keep the column order within each condition
475+
kind="stable",
476+
)
477+
.reset_index(drop=True)
478+
)
463479

464480
if condition_df.empty:
465481
# This happens if there weren't any condition-specific changes

‎tests/v2/test_conversion.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,31 @@
1313
from petab.v2 import Problem
1414
from petab.v2.calculate import calculate_llh as calculate_llh_v2
1515
from petab.v2.math import petab_symbol
16-
from petab.v2.petab1to2 import petab1to2, v1v2_observable_df
16+
from petab.v2.petab1to2 import (
17+
petab1to2,
18+
v1v2_condition_df,
19+
v1v2_observable_df,
20+
)
21+
22+
23+
def test_v1v2_condition_df_order():
24+
"""The changes of each condition are contiguous, in the order of the
25+
v1 rows and columns."""
26+
v1_df = v1.get_condition_df(
27+
pd.DataFrame(
28+
{
29+
v1.C.CONDITION_ID: ["c2", "c1", "c3"],
30+
"k2": [1.0, 2.0, np.nan],
31+
"k1": [3.0, np.nan, np.nan],
32+
}
33+
)
34+
)
35+
v2_df = v1v2_condition_df(v1_df, model=None)
36+
assert v2_df.values.tolist() == [
37+
["c2", "k2", 1.0],
38+
["c2", "k1", 3.0],
39+
["c1", "k2", 2.0],
40+
]
1741

1842

1943
def test_v1v2_observable_df_noise_distribution():

0 commit comments

Comments
 (0)