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
5 changes: 5 additions & 0 deletions changelog.d/+petab1to2-empty-transformation-columns.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
`petab1to2`: Fixed a pandas deprecation warning for an all-empty
`observableTransformation` column in the observable table, or an all-empty
`noiseDistribution` column next to an `observableTransformation` column.
With future pandas versions, this would have raised an error
([#635](https://github.com/PEtab-dev/libpetab-python/pull/635))
15 changes: 10 additions & 5 deletions petab/v2/petab1to2.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,18 @@ def v1v2_observable_df(observable_df: pd.DataFrame) -> pd.DataFrame:
# drop observableTransformation, update noiseDistribution
# if there is no observableTransformation, no need to update
if v1.C.OBSERVABLE_TRANSFORMATION in df.columns:
df[v1.C.OBSERVABLE_TRANSFORMATION] = df[
v1.C.OBSERVABLE_TRANSFORMATION
].fillna(v1.C.LIN)
# all-empty columns are read as float, so cast before filling
df[v1.C.OBSERVABLE_TRANSFORMATION] = (
df[v1.C.OBSERVABLE_TRANSFORMATION]
.astype("object")
.fillna(v1.C.LIN)
)

if v1.C.NOISE_DISTRIBUTION in df:
df[v1.C.NOISE_DISTRIBUTION] = df[v1.C.NOISE_DISTRIBUTION].fillna(
v1.C.NORMAL
df[v1.C.NOISE_DISTRIBUTION] = (
df[v1.C.NOISE_DISTRIBUTION]
.astype("object")
.fillna(v1.C.NORMAL)
)
else:
df[v1.C.NOISE_DISTRIBUTION] = v1.C.NORMAL
Expand Down
23 changes: 23 additions & 0 deletions tests/v2/test_conversion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import logging
from pathlib import Path

Expand Down Expand Up @@ -37,6 +38,28 @@ def test_v1v2_observable_df_noise_distribution():
]


def test_v1v2_observable_df_empty_columns():
"""All-empty observableTransformation and noiseDistribution columns,
as read from a file, are treated as defaults."""
observable_df = v1.get_observable_df(
pd.read_csv(
io.StringIO(
f"{v1.C.OBSERVABLE_ID}\t{v1.C.OBSERVABLE_FORMULA}\t"
f"{v1.C.NOISE_FORMULA}\t{v1.C.OBSERVABLE_TRANSFORMATION}\t"
f"{v1.C.NOISE_DISTRIBUTION}\n"
"obs1\ta\t1\t\t\n"
),
sep="\t",
)
)
assert observable_df[v1.C.OBSERVABLE_TRANSFORMATION].dtype == float
assert observable_df[v1.C.NOISE_DISTRIBUTION].dtype == float

new_df = v1v2_observable_df(observable_df)

assert list(new_df[v2.C.NOISE_DISTRIBUTION]) == [v2.C.NORMAL]


def test_v1v2_observable_df_log10():
"""Test that log10-transformed observables are converted to
log-transformed ones with the noise formula scaled by ln(10)."""
Expand Down
Loading