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
4 changes: 4 additions & 0 deletions changelog.d/+align-diagnostic-plots.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Match simulated values to measurements in goodness-of-fit and residual plots,
so shuffled simulation tables and repeated measurements produce correct
scatter points and regression statistics
([#612](https://github.com/PEtab-dev/libpetab-python/pull/612)).
46 changes: 25 additions & 21 deletions petab/v1/calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@ def calculate_residuals(
return residual_dfs


def _get_simulation_for_measurement(
row: pd.Series, simulation_df: pd.DataFrame, compared_cols: set[str]
) -> float:
"""Match a measurement using shared columns, ignoring empty values."""
masks = [
(simulation_df[col] == row[col]) | petab.is_empty(row[col])
for col in compared_cols
]
mask = reduce(operator.and_, masks)
if mask.sum() == 0:
raise ValueError(f"Could not find simulation for measurement {row}.")
if (
mask.sum() > 1
and simulation_df.loc[mask].drop_duplicates().shape[0] > 1
):
raise ValueError(
f"Multiple different simulations found for measurement "
f"{row}:\n{simulation_df.loc[mask]}"
)
return simulation_df.loc[mask][SIMULATION].iloc[0]


def calculate_residuals_for_table(
measurement_df: pd.DataFrame,
simulation_df: pd.DataFrame,
Expand Down Expand Up @@ -115,27 +137,9 @@ def calculate_residuals_for_table(
# iterate over measurements, find corresponding simulations
for irow, row in measurement_df.iterrows():
measurement = row[MEASUREMENT]
# look up in simulation df
masks = [
(simulation_df[col] == row[col]) | petab.is_empty(row[col])
for col in compared_cols
]
mask = reduce(lambda x, y: x & y, masks)
if mask.sum() == 0:
raise ValueError(
f"Could not find simulation for measurement {row}."
)
# if we have multiple matches, check that the rows are all identical
elif (
mask.sum() > 1
and simulation_df.loc[mask].drop_duplicates().shape[0] > 1
):
raise ValueError(
f"Multiple different simulations found for measurement "
f"{row}:\n{simulation_df.loc[mask]}"
)

simulation = simulation_df.loc[mask][SIMULATION].iloc[0]
simulation = _get_simulation_for_measurement(
row, simulation_df, compared_cols
)
if scale:
# apply scaling
observable = observable_df.loc[row[OBSERVABLE_ID]]
Expand Down
37 changes: 28 additions & 9 deletions petab/v1/visualize/plot_residuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@
from scipy import stats

from ..C import *
from ..calculate import calculate_residuals
from ..calculate import _get_simulation_for_measurement, calculate_residuals
from ..core import get_simulation_df
from ..problem import Problem

__all__ = ["plot_goodness_of_fit", "plot_residuals_vs_simulation"]


def _aligned_simulations(
measurements: pd.DataFrame, simulations: pd.DataFrame
) -> np.ndarray:
"""Return simulation values in measurement order, including replicates."""
compared_cols = set(measurements.columns) & set(simulations.columns)
return np.array(
[
_get_simulation_for_measurement(row, simulations, compared_cols)
for _, row in measurements.iterrows()
]
)


def plot_residuals_vs_simulation(
petab_problem: Problem,
simulations_df: str | Path | pd.DataFrame,
Expand Down Expand Up @@ -87,22 +100,25 @@ def plot_residuals_vs_simulation(
normal_residuals = residual_df[
residual_df[OBSERVABLE_ID].isin(observable_ids)
]
simulations_normal = simulations_df[
simulations_df[OBSERVABLE_ID].isin(observable_ids)
]
simulations_normal = _aligned_simulations(
petab_problem.measurement_df[
petab_problem.measurement_df[OBSERVABLE_ID].isin(observable_ids)
],
simulations_df,
)

# compare to standard normal distribution
ks_result = stats.kstest(normal_residuals[RESIDUAL], stats.norm.cdf)

# plot the residuals plot
axes[0].hlines(
y=0,
xmin=min(simulations_normal[SIMULATION]),
xmax=max(simulations_normal[SIMULATION]),
xmin=min(simulations_normal),
xmax=max(simulations_normal),
ls="--",
color="gray",
)
axes[0].scatter(simulations_normal[SIMULATION], normal_residuals[RESIDUAL])
axes[0].scatter(simulations_normal, normal_residuals[RESIDUAL])
axes[0].text(
0.15,
0.85,
Expand Down Expand Up @@ -193,8 +209,11 @@ def plot_goodness_of_fit(
error_name = "mean of squared residuals"
error = np.mean(np.power(residual_df["residual"], 2))

simulations = _aligned_simulations(
petab_problem.measurement_df, simulations_df
)
slope, intercept, r_value, p_value, _std_err = stats.linregress(
simulations_df["simulation"],
simulations,
petab_problem.measurement_df["measurement"],
) # x, y

Expand All @@ -203,7 +222,7 @@ def plot_goodness_of_fit(
fig.set_layout_engine("tight")

ax.scatter(
simulations_df["simulation"],
simulations,
petab_problem.measurement_df["measurement"],
c=color,
)
Expand Down
67 changes: 67 additions & 0 deletions tests/v1/test_residual_plot_alignment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Regression tests for matching plotted simulations to measurements."""

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytest

from petab.v1 import Problem
from petab.v1.visualize import (
plot_goodness_of_fit,
plot_residuals_vs_simulation,
)

plt.switch_backend("agg")


@pytest.mark.parametrize(
"plot", [plot_goodness_of_fit, plot_residuals_vs_simulation]
)
@pytest.mark.parametrize("replicates", [False, True])
def test_simulation_order_does_not_change_plot(plot, replicates):
"""Reordering simulation rows must not change pairs or statistics."""
measurements = pd.DataFrame(
{
"observableId": ["obs", "other", "obs"],
"simulationConditionId": ["c1", "c2", "c1"],
"time": [0, 0, 1],
"measurement": [2.0, 7.0, 12.0],
"preequilibrationConditionId": [np.nan] * 3,
},
index=[5, 5, 9],
)
simulations = measurements.rename(
columns={"measurement": "simulation"}
).copy()
simulations["simulation"] = [1.0, 3.0, 5.0]
if replicates:
measurements = pd.concat([measurements, measurements.iloc[[0]]])
problem = Problem(
measurement_df=measurements,
observable_df=pd.DataFrame(
{"noiseFormula": ["1", "1"]}, index=["obs", "other"]
),
parameter_df=pd.DataFrame(),
)
shuffled = simulations.iloc[[2, 0, 1]]
before = shuffled.copy(deep=True)
measurements_before = measurements.copy(deep=True)
try:
result = plot(problem, shuffled)
ax = result if plot is plot_goodness_of_fit else result[0]
x = [1, 3, 5] + ([1] if replicates else [])
y = [2, 7, 12] if plot is plot_goodness_of_fit else [1, 4, 7]
if replicates:
y += y[:1]
np.testing.assert_allclose(
ax.collections[
0 if plot is plot_goodness_of_fit else 1
].get_offsets(),
np.column_stack([x, y]),
)
if plot is plot_goodness_of_fit and not replicates:
assert "slope: 2.50" in ax.texts[0].get_text()
pd.testing.assert_frame_equal(shuffled, before)
pd.testing.assert_frame_equal(measurements, measurements_before)
finally:
plt.close("all")