From 524472d90dc554c5665147e9029f17d06da4ad1c Mon Sep 17 00:00:00 2001 From: Yueqi Chen <245663479+ChenYueqi2024@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:19:43 +0800 Subject: [PATCH 1/2] Report estimated model parameters missing from every mapping --- .../+unmapped-estimated-parameters.fix.md | 2 + petab/v1/parameter_mapping.py | 80 ++++++++++--------- tests/v1/test_parameter_mapping.py | 37 +++++++++ 3 files changed, 80 insertions(+), 39 deletions(-) create mode 100644 changelog.d/+unmapped-estimated-parameters.fix.md diff --git a/changelog.d/+unmapped-estimated-parameters.fix.md b/changelog.d/+unmapped-estimated-parameters.fix.md new file mode 100644 index 00000000..58cf2ea2 --- /dev/null +++ b/changelog.d/+unmapped-estimated-parameters.fix.md @@ -0,0 +1,2 @@ +Report estimated model parameters with no usable value that would otherwise +be silently omitted from all simulation parameter mappings. diff --git a/petab/v1/parameter_mapping.py b/petab/v1/parameter_mapping.py index 66d0a388..e84e1bc4 100644 --- a/petab/v1/parameter_mapping.py +++ b/petab/v1/parameter_mapping.py @@ -117,6 +117,10 @@ def get_optimization_to_simulation_parameter_mapping( condition. If no preequilibration condition is defined, the respective dicts will be empty. ``NaN`` is used where no mapping exists. + + Raises: + ValueError: An estimated model parameter with no usable model value + is absent from all simulation and preequilibration mappings. """ if sbml_model: warnings.warn( @@ -154,50 +158,48 @@ def get_optimization_to_simulation_parameter_mapping( for par_id in output_parameters: simulation_parameters[par_id] = np.nan + args = _map_condition_arg_packer( + simulation_conditions, + measurement_df, + condition_df, + parameter_df, + mapping_df, + model, + simulation_parameters, + warn_unmapped, + scaled_parameters, + fill_fixed_parameters, + allow_timepoint_specific_numeric_noise_parameters, + ) num_threads = int(os.environ.get(ENV_NUM_THREADS, "1")) # If sequential execution is requested, let's not create any # thread-allocation overhead if num_threads == 1: - mapping = map( - _map_condition, - _map_condition_arg_packer( - simulation_conditions, - measurement_df, - condition_df, - parameter_df, - mapping_df, - model, - simulation_parameters, - warn_unmapped, - scaled_parameters, - fill_fixed_parameters, - allow_timepoint_specific_numeric_noise_parameters, - ), - ) - return list(mapping) - - # Run multi-threaded - from concurrent.futures import ThreadPoolExecutor - - with ThreadPoolExecutor(max_workers=num_threads) as executor: - mapping = executor.map( - _map_condition, - _map_condition_arg_packer( - simulation_conditions, - measurement_df, - condition_df, - parameter_df, - mapping_df, - model, - simulation_parameters, - warn_unmapped, - scaled_parameters, - fill_fixed_parameters, - allow_timepoint_specific_numeric_noise_parameters, - ), - ) - return list(mapping) + mappings = list(map(_map_condition, args)) + else: + from concurrent.futures import ThreadPoolExecutor + + with ThreadPoolExecutor(max_workers=num_threads) as executor: + mappings = list(executor.map(_map_condition, args)) + + if mappings and parameter_df is not None and ESTIMATE in parameter_df: + estimated = parameter_df.index[parameter_df[ESTIMATE] == 1] + unresolved = set(model.get_parameter_ids()).intersection(estimated) + unresolved.difference_update(simulation_parameters) + if unresolved: + # A condition or output override may still supply the parameter. + for preeq, simulation, _, _ in mappings: + unresolved.difference_update( + preeq.values(), simulation.values() + ) + if unresolved: + names = ", ".join(sorted(unresolved)) + raise ValueError( + f"Estimated model parameter(s) {names} have no usable " + "model value and are absent from all parameter mappings." + ) + return mappings def _map_condition_arg_packer( diff --git a/tests/v1/test_parameter_mapping.py b/tests/v1/test_parameter_mapping.py index ddb5ca99..309a7bbf 100644 --- a/tests/v1/test_parameter_mapping.py +++ b/tests/v1/test_parameter_mapping.py @@ -3,6 +3,7 @@ import numpy as np import pandas as pd +import pytest import petab.v1 as petab from petab import ENV_NUM_THREADS @@ -693,6 +694,42 @@ def test_parameterized_condition_table_changed_scale(): assert actual == expected +@pytest.mark.parametrize("num_threads", ["1", "2"]) +@pytest.mark.parametrize("condition_override", [False, True]) +def test_estimated_parameter_with_unresolved_model_value( + monkeypatch, num_threads, condition_override +): + """An estimated model parameter must appear in a simulation mapping.""" + monkeypatch.setenv(ENV_NUM_THREADS, num_threads) + model = SbmlModel.from_antimony( + "base = 1.5; derived = base + 1; conc = 1; conc' = -derived * conc;" + ) + condition_df = pd.DataFrame( + {"derived": [2.5]} if condition_override else {}, + index=pd.Index(["c1"], name=CONDITION_ID), + ) + parameter_df = pd.DataFrame( + {ESTIMATE: [1], NOMINAL_VALUE: [2.5], PARAMETER_SCALE: [LIN]}, + index=pd.Index(["derived"], name=PARAMETER_ID), + ) + arguments = { + "condition_df": condition_df, + "measurement_df": pd.DataFrame({SIMULATION_CONDITION_ID: ["c1"]}), + "model": model, + "parameter_df": parameter_df, + } + if condition_override: + [mapping] = petab.get_optimization_to_simulation_parameter_mapping( + **arguments + ) + assert mapping[1]["derived"] == "derived" + else: + with pytest.raises( + ValueError, match=r"Estimated model parameter.*derived" + ): + petab.get_optimization_to_simulation_parameter_mapping(**arguments) + + def test_fill_in_nominal_values(): parameter_df = pd.DataFrame( data={ From 7cd8113b8708ab7c1781b00a363b5512d9f0f61d Mon Sep 17 00:00:00 2001 From: Yueqi Chen <245663479+ChenYueqi2024@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:19:53 +0800 Subject: [PATCH 2/2] Link changelog entry to PR 619 --- changelog.d/+unmapped-estimated-parameters.fix.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog.d/+unmapped-estimated-parameters.fix.md b/changelog.d/+unmapped-estimated-parameters.fix.md index 58cf2ea2..75d2cc78 100644 --- a/changelog.d/+unmapped-estimated-parameters.fix.md +++ b/changelog.d/+unmapped-estimated-parameters.fix.md @@ -1,2 +1,3 @@ Report estimated model parameters with no usable value that would otherwise -be silently omitted from all simulation parameter mappings. +be silently omitted from all simulation parameter mappings +([#619](https://github.com/PEtab-dev/libpetab-python/pull/619)).