Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Condition-table override referencing a parameter with a non-numeric InitialAssignment crashes with an opaque KeyError

Abierto
#519 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python
Área
backend

Línea de trabajo

Start in petab/v1/parameter_mapping.py at _apply_parameter_table, then inspect SbmlModel.get_free_parameter_ids_with_values() and the condition-table validation checks. Reproduce the minimal example, and make the unresolved condition-table reference produce an informative validation error naming the column and value rather than an unhandled KeyError.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Summary

When a condition-table cell overrides a model entity with the ID of another SBML parameter, and that referenced parameter's value can only be given via an InitialAssignment that depends on another parameter (i.e. it can't be reduced to a plain number without already knowing that other parameter's value), get_optimization_to_simulation_parameter_mapping crashes with an unhandled KeyError instead of a clear validation error.

This surfaced via AMICI-dev/AMICI#2418. The behavior described below is still present in the latest release (0.9.0).

Root cause

SbmlModel.get_free_parameter_ids_with_values() seeds the parameter-mapping dict with every SBML parameter's value, evaluating each one via get_initial():

def get_initial(p):
    if ia := self.sbml_model.getInitialAssignmentBySymbol(p.getId()):
        sym_expr = sympify_sbml(ia.getMath())
        return (
            float(sym_expr.evalf())
            if sym_expr.evalf().is_Number
            else None
        )
    return p.getValue()

If the InitialAssignment doesn't evaluate to a plain number (because it references another, possibly-estimated, parameter), get_initial() returns None, and that parameter is silently excluded from get_free_parameter_ids_with_values() — regardless of whether it's ever referenced from a condition table.

Later, in _apply_parameter_table (petab/v1/parameter_mapping.py), any condition-table cell value that's still an unresolved string is looked up two ways: as another condition-table column, or as a row in the parameter table. A parameter excluded above is neither, so:

try:
    par_mapping[problem_par] = par_mapping[sim_par]   # KeyError, caught
    ...
except KeyError:
    ...
    scale = parameter_df.loc[sim_par, PARAMETER_SCALE]  # KeyError, NOT caught

the fallback branch assumes sim_par is a parameter-table index and raises an unhandled KeyError if it isn't.

Minimal example

import pandas as pd
import petab.v1 as petab
from petab.v1.models.sbml_model import SbmlModel

C = petab.C

problem = petab.Problem()
problem.model = SbmlModel.from_antimony(
    "k_base = 1.5; k_derived = k_base + 1; k_decay = 1; "
    "conc = 1; conc' = -k_decay * conc;"
)
problem.condition_df = pd.DataFrame(
    {"k_decay": ["k_derived"]},
    index=pd.Index(["condition1"], name=C.CONDITION_ID),
)
problem.parameter_df = pd.DataFrame(
    {
        C.PARAMETER_SCALE: ["log10"],
        C.LOWER_BOUND: [1e-3],
        C.UPPER_BOUND: [1e3],
        C.NOMINAL_VALUE: [1.5],
        C.ESTIMATE: [1],
    },
    index=pd.Index(["k_base"], name=C.PARAMETER_ID),
)

petab.get_optimization_to_simulation_parameter_mapping(
    condition_df=problem.condition_df,
    measurement_df=None,
    parameter_df=problem.parameter_df,
    observable_df=None,
    model=problem.model,
)
KeyError: 'k_derived'

petab.lint.lint_problem(problem) reports no issues for this problem — none of the existing checks (check_condition_df, assert_model_parameters_in_condition_or_parameter_table) inspect condition-table cell values, only column names and parameter-table row IDs.

Note: this is not specific to InitialAssignment

The same failure occurs for any condition-table override value that can't be resolved through the two supported paths (another condition-table column, or a parameter-table row) — InitialAssignment is just the most common way to end up with such a value. A plain SBML parameter without any InitialAssignment resolves fine, since get_free_parameter_ids_with_values() can always give it a value.

Suggested fix

At minimum, _apply_parameter_table's fallback branch should raise an informative error (naming the unresolved column and value) instead of letting a raw KeyError escape. Ideally, condition-table validation (check_condition_df or a new check) would catch this ahead of time, since the format doc ("Values for these condition parameters may be provided ... as IDs defined in the SBML model, the parameter table or both") doesn't obviously rule this out, and a validated problem shouldn't crash with an internal KeyError.

Whether actually resolving such references (evaluating the InitialAssignment using the current value of its dependencies) is worth supporting is a separate, larger question — this issue is about the crash/error-message gap either way.


🤖 This issue was generated with AI assistance (Claude Code).

Lenguaje dominante
Python
Estrellas
18
Forks
9
Merge medio
1 d 8 h
PR fusionados (30 d)
21

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de PEtab-dev/libpetab-python

Todos los issues de PEtab-dev/libpetab-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.