|
| 1 | +from inspect import cleandoc |
| 2 | +from numpy import inf |
| 3 | + |
| 4 | +from petab.v2.C import * |
| 5 | +from petab.v2 import Problem |
| 6 | +from petabtests import ( |
| 7 | + PetabV2TestCase, |
| 8 | + antimony_to_sbml_str, |
| 9 | +) |
| 10 | +from pathlib import Path |
| 11 | +from petab.v2 import PriorDistribution |
| 12 | + |
| 13 | +DESCRIPTION = cleandoc(r""" |
| 14 | +## Objective |
| 15 | +
|
| 16 | +This case tests different non-truncated prior distributions, as well as |
| 17 | +implicit uniform priors and fixed parameters. |
| 18 | +
|
| 19 | +## Model |
| 20 | +
|
| 21 | +A simple model with all constant parameters. |
| 22 | +""") |
| 23 | + |
| 24 | +# problem -------------------------------------------------------------------- |
| 25 | + |
| 26 | +priors = [ |
| 27 | + (PriorDistribution.UNIFORM, (2, 8), 2, 8), |
| 28 | + (PriorDistribution.NORMAL, (4, 2), -inf, inf), |
| 29 | + (PriorDistribution.LOG_NORMAL, (5, 2), 0.0, inf), |
| 30 | + (PriorDistribution.CAUCHY, (3, 5), -inf, inf), |
| 31 | + (PriorDistribution.CHI_SQUARED, (4), 0.0, inf), |
| 32 | + (PriorDistribution.EXPONENTIAL, (3), 0.0, inf), |
| 33 | + (PriorDistribution.GAMMA, (3, 5), 0, inf), |
| 34 | + (PriorDistribution.LAPLACE, (3, 5), -inf, inf), |
| 35 | + (PriorDistribution.LOG_LAPLACE, (3, 5), 0, inf), |
| 36 | + (PriorDistribution.LOG_UNIFORM, (3, 5), 3, 5), |
| 37 | + (PriorDistribution.RAYLEIGH, (3), 0, inf), |
| 38 | +] |
| 39 | + |
| 40 | +tested_prior_distrs = {pd for pd, _, _, _ in priors} |
| 41 | +untested_distrs = [ |
| 42 | + pd.value for pd in PriorDistribution if pd not in tested_prior_distrs |
| 43 | +] |
| 44 | +if untested_distrs: |
| 45 | + print("Untested prior distributions:", untested_distrs) |
| 46 | + |
| 47 | +sbml_file = Path(__file__).parent / "_model.xml" |
| 48 | + |
| 49 | +parameters = "\n".join( |
| 50 | + f"p_{prior_type.value.replace('-', '_')} = 5;" for prior_type, _, _, _ in priors |
| 51 | +) |
| 52 | +ant_model = f""" |
| 53 | +model petab_test_0025 |
| 54 | + {parameters} |
| 55 | +end |
| 56 | +""" |
| 57 | +sbml_file.write_text(antimony_to_sbml_str(ant_model)) |
| 58 | + |
| 59 | +problem = Problem() |
| 60 | +for prior_type, prior_pars, support_lb, support_ub in priors: |
| 61 | + problem.add_parameter( |
| 62 | + f"p_{prior_type.value.replace('-', '_')}", |
| 63 | + estimate=True, |
| 64 | + lb=support_lb, |
| 65 | + ub=support_ub, |
| 66 | + nominal_value=5, |
| 67 | + prior_distribution=prior_type, |
| 68 | + prior_parameters=prior_pars, |
| 69 | + ) |
| 70 | +# implicit uniform prior |
| 71 | +problem.add_parameter("p1", estimate=True, nominal_value=1, lb=0, ub=2) |
| 72 | +# fixed, i.e., no prior |
| 73 | +problem.add_parameter("p_fixed", estimate=False, nominal_value=1) |
| 74 | +# we need some observable and measurement |
| 75 | +problem.add_observable("obs_p1", "p1", noise_formula="p_fixed") |
| 76 | +problem.add_measurement("obs_p1", experiment_id="", time=0, measurement=1) |
| 77 | + |
| 78 | +# solutions ------------------------------------------------------------------ |
| 79 | + |
| 80 | +simulation_df = problem.measurement_df.copy(deep=True).rename( |
| 81 | + columns={MEASUREMENT: SIMULATION} |
| 82 | +) |
| 83 | +simulation_df[SIMULATION] = [ |
| 84 | + 1, |
| 85 | +] |
| 86 | + |
| 87 | +case = PetabV2TestCase.from_problem( |
| 88 | + id=25, |
| 89 | + brief="Non-truncated prior distributions.", |
| 90 | + description=DESCRIPTION, |
| 91 | + model=sbml_file, |
| 92 | + problem=problem, |
| 93 | + simulation_df=simulation_df, |
| 94 | +) |
0 commit comments