From f96c572b2d6351e20b6c57e22e43278e984ae291 Mon Sep 17 00:00:00 2001 From: Yueqi Chen <245663479+ChenYueqi2024@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:29:46 +0800 Subject: [PATCH 1/2] Fix measurement alignment in diagnostic plots --- changelog.d/+align-diagnostic-plots.fix.md | 3 + petab/v1/calculate.py | 46 ++++++++------- petab/v1/visualize/plot_residuals.py | 37 +++++++++--- tests/v1/test_residual_plot_alignment.py | 67 ++++++++++++++++++++++ 4 files changed, 123 insertions(+), 30 deletions(-) create mode 100644 changelog.d/+align-diagnostic-plots.fix.md create mode 100644 tests/v1/test_residual_plot_alignment.py diff --git a/changelog.d/+align-diagnostic-plots.fix.md b/changelog.d/+align-diagnostic-plots.fix.md new file mode 100644 index 00000000..9812f229 --- /dev/null +++ b/changelog.d/+align-diagnostic-plots.fix.md @@ -0,0 +1,3 @@ +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. diff --git a/petab/v1/calculate.py b/petab/v1/calculate.py index f8c58f2a..e30cff8a 100644 --- a/petab/v1/calculate.py +++ b/petab/v1/calculate.py @@ -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, @@ -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]] diff --git a/petab/v1/visualize/plot_residuals.py b/petab/v1/visualize/plot_residuals.py index c0b95fcc..f8c984ea 100644 --- a/petab/v1/visualize/plot_residuals.py +++ b/petab/v1/visualize/plot_residuals.py @@ -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, @@ -87,9 +100,12 @@ 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) @@ -97,12 +113,12 @@ def plot_residuals_vs_simulation( # 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, @@ -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 @@ -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, ) diff --git a/tests/v1/test_residual_plot_alignment.py b/tests/v1/test_residual_plot_alignment.py new file mode 100644 index 00000000..8ac03c94 --- /dev/null +++ b/tests/v1/test_residual_plot_alignment.py @@ -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") From cc121f222817cb19a04d83be7bb5f0de179ab4b0 Mon Sep 17 00:00:00 2001 From: Yueqi Chen <245663479+ChenYueqi2024@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:30:51 +0800 Subject: [PATCH 2/2] Link the diagnostic plot changelog entry to PR 612 --- changelog.d/+align-diagnostic-plots.fix.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog.d/+align-diagnostic-plots.fix.md b/changelog.d/+align-diagnostic-plots.fix.md index 9812f229..b3c3a101 100644 --- a/changelog.d/+align-diagnostic-plots.fix.md +++ b/changelog.d/+align-diagnostic-plots.fix.md @@ -1,3 +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. +scatter points and regression statistics +([#612](https://github.com/PEtab-dev/libpetab-python/pull/612)).