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/+v2-petab1to2-condition-order.misc.md
Original file line number Diff line number Diff line change
@@ -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))
26 changes: 21 additions & 5 deletions petab/v2/petab1to2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion tests/v2/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Loading