Skip to content

Commit 9766f48

Browse files
authored
Fix validation for output parameters columns in the condition table (#161)
Parameters introduced via `observableFormula` or `noiseFormula` are allowed to occur as condition table columns. This was not handled correctly by `petablint`. Parameters introduced via `observableFormula` or `noiseFormula` are only required in the parameter table if they are not already overridden for *all* conditions. This was not handled correctly by `petablint`.
1 parent 286494a commit 9766f48

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

petab/lint.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,16 @@ def assert_no_leading_trailing_whitespace(
8585

8686

8787
def check_condition_df(
88-
df: pd.DataFrame, model: Optional[Model] = None) -> None:
88+
df: pd.DataFrame,
89+
model: Optional[Model] = None,
90+
observable_df: Optional[pd.DataFrame] = None
91+
) -> None:
8992
"""Run sanity checks on PEtab condition table
9093
9194
Arguments:
9295
df: PEtab condition DataFrame
9396
model: Model for additional checking of parameter IDs
97+
observable_df: PEtab observables DataFrame
9498
9599
Raises:
96100
AssertionError: in case of problems
@@ -119,6 +123,9 @@ def check_condition_df(
119123

120124
if model is not None:
121125
allowed_cols = set(model.get_valid_ids_for_condition_table())
126+
if observable_df is not None:
127+
allowed_cols |= set(petab.get_output_parameters(
128+
model=model, observable_df=observable_df))
122129
for column_name in df.columns:
123130
if column_name != CONDITION_NAME \
124131
and column_name not in allowed_cols:
@@ -795,7 +802,8 @@ def lint_problem(problem: 'petab.Problem') -> bool:
795802
if problem.condition_df is not None:
796803
logger.info("Checking condition table...")
797804
try:
798-
check_condition_df(problem.condition_df, problem.model)
805+
check_condition_df(problem.condition_df, problem.model,
806+
problem.observable_df)
799807
except AssertionError as e:
800808
logger.error(e)
801809
errors_occurred = True

petab/parameters.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ def append_overrides(overrides):
265265
if not model.has_entity_with_id(p):
266266
parameter_ids[p] = None
267267

268+
# remove parameters that occur in the condition table and are overridden
269+
# for ALL conditions
270+
for p in condition_df.columns[~condition_df.isnull().any()]:
271+
try:
272+
del parameter_ids[p]
273+
except KeyError:
274+
pass
268275
return parameter_ids.keys()
269276

270277

tests/test_lint.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,14 @@ def test_check_condition_df():
419419
with pytest.raises(AssertionError):
420420
lint.check_condition_df(condition_df, model)
421421

422-
# fix:
422+
# fix by adding output parameter
423+
observable_df = pd.DataFrame({
424+
OBSERVABLE_ID: ["obs1"],
425+
OBSERVABLE_FORMULA: ["p1"],
426+
})
427+
lint.check_condition_df(condition_df, model, observable_df)
428+
429+
# fix by adding parameter
423430
ss_model.addParameter('p1', 1.0)
424431
lint.check_condition_df(condition_df, model)
425432

@@ -452,7 +459,7 @@ def test_check_ids():
452459

453460

454461
def test_check_parameter_df():
455-
"""Check parameters.normalize_parameter_df."""
462+
"""Check lint.check_parameter_df."""
456463

457464
parameter_df = pd.DataFrame({
458465
PARAMETER_ID: ['par0', 'par1', 'par2'],

0 commit comments

Comments
 (0)