From e78fde4bdb71ae66deae938522f0e3f3c2d21aa1 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Wed, 25 Nov 2026 14:15:09 +0100 Subject: [PATCH] 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 --- .../+v2-petab1to2-condition-order.misc.md | 3 +++ petab/v2/petab1to2.py | 26 +++++++++++++++---- tests/v2/test_conversion.py | 26 ++++++++++++++++++- 3 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 changelog.d/+v2-petab1to2-condition-order.misc.md diff --git a/changelog.d/+v2-petab1to2-condition-order.misc.md b/changelog.d/+v2-petab1to2-condition-order.misc.md new file mode 100644 index 00000000..c8dbae0e --- /dev/null +++ b/changelog.d/+v2-petab1to2-condition-order.misc.md @@ -0,0 +1,3 @@ +`petab1to2` writes the changes of each condition as contiguous rows, in the +order of the PEtab v1 condition table rows and columns +([#637](https://github.com/PEtab-dev/libpetab-python/pull/637)) diff --git a/petab/v2/petab1to2.py b/petab/v2/petab1to2.py index cea4a73d..bf01c785 100644 --- a/petab/v2/petab1to2.py +++ b/petab/v2/petab1to2.py @@ -455,11 +455,27 @@ def v1v2_condition_df( # conditionName was dropped in PEtab v2 condition_df = condition_df.drop(columns=[v1.C.CONDITION_NAME]) - condition_df = condition_df.melt( - id_vars=[v1.C.CONDITION_ID], - var_name=v2.C.TARGET_ID, - value_name=v2.C.TARGET_VALUE, - ).dropna(subset=[v2.C.TARGET_VALUE]) + # condition ID -> row index, to keep the rows of a condition together, + # in the original order + row_index = { + condition_id: i + for i, condition_id in enumerate(condition_df[v1.C.CONDITION_ID]) + } + condition_df = ( + condition_df.melt( + id_vars=[v1.C.CONDITION_ID], + var_name=v2.C.TARGET_ID, + value_name=v2.C.TARGET_VALUE, + ) + .dropna(subset=[v2.C.TARGET_VALUE]) + .sort_values( + v1.C.CONDITION_ID, + key=lambda ids: ids.map(row_index), + # keep the column order within each condition + kind="stable", + ) + .reset_index(drop=True) + ) if condition_df.empty: # This happens if there weren't any condition-specific changes diff --git a/tests/v2/test_conversion.py b/tests/v2/test_conversion.py index bb6c3628..0ab1bcca 100644 --- a/tests/v2/test_conversion.py +++ b/tests/v2/test_conversion.py @@ -13,7 +13,31 @@ from petab.v2 import Problem from petab.v2.calculate import calculate_llh as calculate_llh_v2 from petab.v2.math import petab_symbol -from petab.v2.petab1to2 import petab1to2, v1v2_observable_df +from petab.v2.petab1to2 import ( + petab1to2, + v1v2_condition_df, + v1v2_observable_df, +) + + +def test_v1v2_condition_df_order(): + """The changes of each condition are contiguous, in the order of the + v1 rows and columns.""" + v1_df = v1.get_condition_df( + pd.DataFrame( + { + v1.C.CONDITION_ID: ["c2", "c1", "c3"], + "k2": [1.0, 2.0, np.nan], + "k1": [3.0, np.nan, np.nan], + } + ) + ) + v2_df = v1v2_condition_df(v1_df, model=None) + assert v2_df.values.tolist() == [ + ["c2", "k2", 1.0], + ["c2", "k1", 3.0], + ["c1", "k2", 2.0], + ] def test_v1v2_observable_df_noise_distribution():