|
4 | 4 | import logging |
5 | 5 | import numbers |
6 | 6 | import re |
7 | | -from collections import Counter |
| 7 | +from collections import Counter, defaultdict |
8 | 8 | from collections.abc import Iterable |
9 | 9 | from typing import Any |
10 | 10 |
|
|
35 | 35 | "assert_parameter_prior_parameters_are_valid", |
36 | 36 | "assert_parameter_prior_type_is_valid", |
37 | 37 | "assert_parameter_scale_is_valid", |
38 | | - "assert_placeholders_not_in_model", |
| 38 | + "assert_placeholders_only_used_in_own_observable", |
39 | 39 | "assert_unique_observable_ids", |
40 | 40 | "assert_unique_parameter_ids", |
41 | 41 | "check_condition_df", |
@@ -350,30 +350,108 @@ def check_observable_df(observable_df: pd.DataFrame) -> None: |
350 | 350 | ) from e |
351 | 351 |
|
352 | 352 |
|
353 | | -def assert_placeholders_not_in_model( |
354 | | - observable_df: pd.DataFrame, model: Model |
| 353 | +def assert_placeholders_only_used_in_own_observable( |
| 354 | + problem: "petab.Problem", |
355 | 355 | ) -> None: |
356 | | - """Check that no placeholder parameter is a model entity. |
| 356 | + """Check that placeholders are not defined or used elsewhere. |
357 | 357 |
|
358 | | - Placeholders are overridden per measurement. A model entity with the |
359 | | - same ID would be a second, conflicting definition. |
| 358 | + Placeholder parameters are overridden per measurement. A model entity, |
| 359 | + a condition table column, a mapping table entry, or an observable with |
| 360 | + the same ID conflicts with that, as does the use of the placeholder in |
| 361 | + another observable, the condition table, or the measurement table. |
| 362 | + Noise placeholders must not be used in the observable formula. |
| 363 | + Missing tables or a missing model are skipped. |
360 | 364 |
|
361 | 365 | Arguments: |
362 | | - observable_df: PEtab observable DataFrame |
363 | | - model: The model |
| 366 | + problem: PEtab problem to check |
364 | 367 |
|
365 | 368 | Raises: |
366 | 369 | AssertionError: in case of problems |
367 | 370 | """ |
368 | | - from .observables import get_placeholders |
| 371 | + from .observables import get_formula_placeholders |
| 372 | + |
| 373 | + if (observable_df := problem.observable_df) is None: |
| 374 | + return |
| 375 | + model = problem.model |
| 376 | + condition_df = problem.condition_df |
| 377 | + mapping_df = problem.mapping_df |
| 378 | + measurement_df = problem.measurement_df |
| 379 | + |
| 380 | + def symbols(expression) -> set[str]: |
| 381 | + if not isinstance(expression, str): |
| 382 | + return set() |
| 383 | + return {str(s) for s in sympify_petab(expression).free_symbols} |
| 384 | + |
| 385 | + # placeholder -> ID of the observable that declares it |
| 386 | + placeholders: dict[str, str] = {} |
| 387 | + # ID -> (where it is used or defined, observable ID or None) |
| 388 | + uses: dict[str, list[tuple[str, str | None]]] = defaultdict(list) |
| 389 | + |
| 390 | + for observable_id, row in observable_df.iterrows(): |
| 391 | + uses[observable_id].append(("observable ID", None)) |
| 392 | + formula = row.get(OBSERVABLE_FORMULA) |
| 393 | + noise_formula = row.get(NOISE_FORMULA) |
| 394 | + formula_symbols = symbols(formula) |
| 395 | + for symbol in formula_symbols | symbols(noise_formula): |
| 396 | + uses[symbol].append( |
| 397 | + (f"used in observable `{observable_id}`", observable_id) |
| 398 | + ) |
| 399 | + noise_placeholders = get_formula_placeholders( |
| 400 | + noise_formula, observable_id, "noise" |
| 401 | + ) |
| 402 | + for placeholder in ( |
| 403 | + get_formula_placeholders(formula, observable_id, "observable") |
| 404 | + + noise_placeholders |
| 405 | + ): |
| 406 | + placeholders[placeholder] = observable_id |
| 407 | + for placeholder in formula_symbols.intersection(noise_placeholders): |
| 408 | + uses[placeholder].append(("used in the observable formula", None)) |
369 | 409 |
|
370 | | - if clashes := [ |
371 | | - placeholder |
372 | | - for placeholder in get_placeholders(observable_df) |
373 | | - if model.has_entity_with_id(placeholder) |
374 | | - ]: |
| 410 | + if condition_df is not None: |
| 411 | + for column in condition_df.columns.drop( |
| 412 | + CONDITION_NAME, errors="ignore" |
| 413 | + ): |
| 414 | + uses[column].append(("condition table column", None)) |
| 415 | + for value in condition_df[column].unique(): |
| 416 | + for symbol in symbols(value): |
| 417 | + uses[symbol].append(("used in the condition table", None)) |
| 418 | + |
| 419 | + if mapping_df is not None: |
| 420 | + for petab_id in mapping_df.index: |
| 421 | + uses[petab_id].append(("mapping table petabEntityId", None)) |
| 422 | + |
| 423 | + if measurement_df is not None: |
| 424 | + for column in (OBSERVABLE_PARAMETERS, NOISE_PARAMETERS): |
| 425 | + if column not in measurement_df: |
| 426 | + continue |
| 427 | + for value in measurement_df[column].unique(): |
| 428 | + for override in measurements.split_parameter_replacement_list( |
| 429 | + value |
| 430 | + ): |
| 431 | + if isinstance(override, str): |
| 432 | + uses[override].append( |
| 433 | + ("used in the measurement table", None) |
| 434 | + ) |
| 435 | + |
| 436 | + clashes = [] |
| 437 | + for placeholder, observable_id in placeholders.items(): |
| 438 | + where = [ |
| 439 | + use |
| 440 | + for use, owner in dict.fromkeys(uses.get(placeholder, [])) |
| 441 | + if owner != observable_id |
| 442 | + ] |
| 443 | + if model is not None and model.has_entity_with_id(placeholder): |
| 444 | + where.append("model entity") |
| 445 | + if where: |
| 446 | + clashes.append( |
| 447 | + f"`{placeholder}` of observable `{observable_id}` " |
| 448 | + f"({', '.join(where)})" |
| 449 | + ) |
| 450 | + |
| 451 | + if clashes: |
375 | 452 | raise AssertionError( |
376 | | - f"Placeholder parameters must not be model entities: {clashes}" |
| 453 | + "Placeholder parameters must only be used in their own " |
| 454 | + "observable: " + "; ".join(clashes) |
377 | 455 | ) |
378 | 456 |
|
379 | 457 |
|
@@ -957,20 +1035,18 @@ def lint_problem(problem: "petab.Problem") -> bool: |
957 | 1035 | except AssertionError as e: |
958 | 1036 | logger.error(e) |
959 | 1037 | errors_occurred = True |
| 1038 | + try: |
| 1039 | + assert_placeholders_only_used_in_own_observable(problem) |
| 1040 | + except AssertionError as e: |
| 1041 | + logger.error(e) |
| 1042 | + errors_occurred = True |
960 | 1043 | if problem.model is not None: |
961 | 1044 | for obs_id in problem.observable_df.index: |
962 | 1045 | if problem.model.has_entity_with_id(obs_id): |
963 | 1046 | logger.error( |
964 | 1047 | f"Observable ID {obs_id} shadows model entity." |
965 | 1048 | ) |
966 | 1049 | errors_occurred = True |
967 | | - try: |
968 | | - assert_placeholders_not_in_model( |
969 | | - problem.observable_df, problem.model |
970 | | - ) |
971 | | - except AssertionError as e: |
972 | | - logger.error(e) |
973 | | - errors_occurred = True |
974 | 1050 | else: |
975 | 1051 | logger.warning("Observable table not available. Skipping.") |
976 | 1052 |
|
|
0 commit comments