Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flatten_timepoint_specific_output_overrides #239

Merged
merged 1 commit into from
Dec 18, 2023
Merged
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 petab/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import numpy as np
import pandas as pd
from pandas.api.types import is_string_dtype

from . import yaml
from .C import * # noqa: F403
Expand Down Expand Up @@ -282,6 +283,10 @@ def flatten_timepoint_specific_output_overrides(
if field not in measurements:
continue

if not is_string_dtype(type(observable[target])):
# if not a string, we don't have to substitute anything
continue

hyperparameter_replacement_id = get_hyperparameter_replacement_id(
hyperparameter_type=hyperparameter_type,
observable_replacement_id=observable_replacement_id,
Expand Down
49 changes: 36 additions & 13 deletions tests/test_petab.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,14 @@ def test_flatten_timepoint_specific_output_overrides():
"""Test flatten_timepoint_specific_output_overrides"""
observable_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["obs1"],
OBSERVABLE_ID: ["obs1", "obs2"],
OBSERVABLE_FORMULA: [
"observableParameter1_obs1 + observableParameter2_obs1"
"observableParameter1_obs1 + observableParameter2_obs1",
"x",
],
NOISE_FORMULA: [
"(observableParameter1_obs1 + observableParameter2_obs1) * noiseParameter1_obs1"
"(observableParameter1_obs1 + observableParameter2_obs1) * noiseParameter1_obs1",
1,
],
}
)
Expand All @@ -366,11 +368,17 @@ def test_flatten_timepoint_specific_output_overrides():
obs1_2_2_1 = "obs1__obsParOverride2_1_0__noiseParOverride2__condition1"
observable_df_expected = pd.DataFrame(
data={
OBSERVABLE_ID: [obs1_1_1_1, obs1_2_1_1, obs1_2_2_1],
OBSERVABLE_ID: [
obs1_1_1_1,
obs1_2_1_1,
obs1_2_2_1,
"obs2__condition1",
],
OBSERVABLE_FORMULA: [
f"observableParameter1_{obs1_1_1_1} + observableParameter2_{obs1_1_1_1}",
f"observableParameter1_{obs1_2_1_1} + observableParameter2_{obs1_2_1_1}",
f"observableParameter1_{obs1_2_2_1} + observableParameter2_{obs1_2_2_1}",
"x",
],
NOISE_FORMULA: [
f"(observableParameter1_{obs1_1_1_1} + observableParameter2_{obs1_1_1_1})"
Expand All @@ -379,6 +387,7 @@ def test_flatten_timepoint_specific_output_overrides():
f" * noiseParameter1_{obs1_2_1_1}",
f"(observableParameter1_{obs1_2_2_1} + observableParameter2_{obs1_2_2_1})"
f" * noiseParameter1_{obs1_2_2_1}",
1,
],
}
)
Expand All @@ -387,54 +396,66 @@ def test_flatten_timepoint_specific_output_overrides():
# Measurement table with timepoint-specific overrides
measurement_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["obs1", "obs1", "obs1", "obs1"],
OBSERVABLE_ID: ["obs1", "obs1", "obs1", "obs1", "obs2"],
SIMULATION_CONDITION_ID: [
"condition1",
"condition1",
"condition1",
"condition1",
"condition1",
],
PREEQUILIBRATION_CONDITION_ID: ["", "", "", ""],
TIME: [1.0, 1.0, 2.0, 2.0],
MEASUREMENT: [0.1] * 4,
PREEQUILIBRATION_CONDITION_ID: ["", "", "", "", ""],
TIME: [1.0, 1.0, 2.0, 2.0, 3.0],
MEASUREMENT: [0.1] * 5,
OBSERVABLE_PARAMETERS: [
"obsParOverride1;1.0",
"obsParOverride2;1.0",
"obsParOverride2;1.0",
"obsParOverride2;1.0",
"",
],
NOISE_PARAMETERS: [
"noiseParOverride1",
"noiseParOverride1",
"noiseParOverride2",
"noiseParOverride2",
"",
],
}
)

measurement_df_expected = pd.DataFrame(
data={
OBSERVABLE_ID: [obs1_1_1_1, obs1_2_1_1, obs1_2_2_1, obs1_2_2_1],
OBSERVABLE_ID: [
obs1_1_1_1,
obs1_2_1_1,
obs1_2_2_1,
obs1_2_2_1,
"obs2__condition1",
],
SIMULATION_CONDITION_ID: [
"condition1",
"condition1",
"condition1",
"condition1",
"condition1",
],
PREEQUILIBRATION_CONDITION_ID: ["", "", "", ""],
TIME: [1.0, 1.0, 2.0, 2.0],
MEASUREMENT: [0.1] * 4,
PREEQUILIBRATION_CONDITION_ID: ["", "", "", "", ""],
TIME: [1.0, 1.0, 2.0, 2.0, 3.0],
MEASUREMENT: [0.1] * 5,
OBSERVABLE_PARAMETERS: [
"obsParOverride1;1.0",
"obsParOverride2;1.0",
"obsParOverride2;1.0",
"obsParOverride2;1.0",
"",
],
NOISE_PARAMETERS: [
"noiseParOverride1",
"noiseParOverride1",
"noiseParOverride2",
"noiseParOverride2",
"",
],
}
)
Expand Down Expand Up @@ -483,7 +504,9 @@ def test_flatten_timepoint_specific_output_overrides():
petab_problem=unflattened_problem,
)
# The unflattened simulation dataframe has the original observable IDs.
assert (unflattened_simulation_df[OBSERVABLE_ID] == "obs1").all()
assert (
unflattened_simulation_df[OBSERVABLE_ID] == ["obs1"] * 4 + ["obs2"]
).all()


def test_flatten_timepoint_specific_output_overrides_special_cases():
Expand Down