diff --git a/README.rst b/README.rst index 5f760b76..11dcac7c 100644 --- a/README.rst +++ b/README.rst @@ -42,17 +42,17 @@ this command: pip install maud-metabolic-models -Cmdstanpy depends on `cmdstan `_, -which in turn requires a c++ toolchain. On some computers you will have to -install these in order to use Maud. You will hit an error at the next step if +Cmdstanpy depends on `cmdstan `_, +which in turn requires a c++ toolchain. On some computers you will have to +install these in order to use Maud. You will hit an error at the next step if this applies to your computer. Luckily cmdstanpy comes with commands that -can do the necessary installing for you. On windows the c++ toolchain can be installed with +can do the necessary installing for you. On windows the c++ toolchain can be installed with the following powershell commands: Usage ===== -Maud is used from the command line. To see all the available commands try running +Maud is used from the command line. To see all the available commands try running .. code-block:: console diff --git a/docs/inputting.md b/docs/inputting.md index a7ac963d..5ab5f5d3 100644 --- a/docs/inputting.md +++ b/docs/inputting.md @@ -46,7 +46,27 @@ The following optional fields can also be specified: * `molecule_unit` A unit for counting molecules, like 'mol' or 'mmol' * `volume_unit` A unit for measuring volume, like 'L' * `energy_unit` A unit for measuring energy, like 'J' or 'kJ' - +* `algebra_solve` A boolean argument that specifies if the powell algebra solver will be used to determine the steady state +* `adjoint_solve` A boolean argument that specifies if the adjoint ode solver will be used in place of the bdf solver. + +The following fields can be specified by the ODE solver. The variables and definitions can be found both in the [`Stan documentation`](`https://mc-stan.org/docs/functions-reference/functions-ode-solver.html`) with more detail in the [`Sundials documentation`](`https://sundials.readthedocs.io/en/latest/cvodes/Usage/SIM.html#c.CVodeSStolerances`): + +* `rel_tol` Relative tolerance of the BDF solver +* `abs_tol` Absolute tolerance of the BDF solver -- applied to every state variable. +* `max_num_steps` Maximum number of steps taken by both the algebra and ODE solvers. +* `timepoint` The integration time solved for either ODE solver. +* `rel_tol_forward` The relative tolerance used during the forward solve +* `abs_tol_forward` The absolute tolerance used during the backwards solve -- can be converted into a vector +* `rel_tol_backward` The relative tolerance used during the backwards solve +* `abs_tol_backward` The absolute tolerance used during the backwards solve -- can be converted into a vector +* `rel_tol_quadrature` The relative tolerance used in the backwards quadrature problem +* `abs_tol_quadrature` The absolute tolerance used in the backwards quadrature problem +* `num_steps_between_checkpoints` number of steps between checkpoints in forward solution +* `interpolation_polynomial` 1 for hermite and 2 for polynomial +* `solver_forward` Solver used for forward ODE problem: 1=Adams (non-stiff), 2=BDF (stiff) +* `solver_backward` Solver used for backward ODE problem: 1=Adams (non-stiff), 2=BDF (stiff) +* `algebra_func_tol` After convergence of the solver, the proposed solution is plugged into the algebraic system and its norm is compared to the function tolerance. If the norm is below the function tolerance, the solution is deemed acceptable +* `algebra_rel_tol` The relative tolerance is the estimated relative error of the solver and serves to test if a satisfactory solution has been found Here is an example configuration file: @@ -71,6 +91,18 @@ Here is an example configuration file: rel_tol = 1e-4 max_num_steps = 1e6 timepoint = 1e3 + rel_tol_forward = 1e-9 + abs_tol_forward = 1e-9 + rel_tol_backward = 1e-9 + abs_tol_backward = 1e-9 + rel_tol_quadrature = 1e-9 + abs_tol_quadrature = 1e-9 + num_steps_between_checkpoints = int(100000) + interpolation_polynomial = 2 + solver_forward = 2 + solver_backward = 2 + algebra_func_tol = 1e-6 + algebra_rel_tol = 1e-10 This file tells Maud that a file representing a kinetic model can be found at the relative path `kinetic_model.toml`, and that priors and experimental diff --git a/docs/theory.md b/docs/theory.md index ee6200e6..90804af3 100644 --- a/docs/theory.md +++ b/docs/theory.md @@ -44,7 +44,7 @@ want. Questions about the network can be formulated in terms of integrals over the posterior distribution. For example, suppose that our thermokinetic model says that the flux through a reaction is given by the function $flux: \Theta\rightarrow\mathbb{R}$; our model's probability, given measurements $y$, -that the flux is greater than $x$ is then $\int_x^\inftyp(\theta\mid +that the flux is greater than $x$ is then $\int_x^\infty(\theta\mid y)d\theta$. The rest of this page sets out how Maud fleshes out the three sub-models and @@ -128,7 +128,7 @@ metabolite's 'formation energy'. * $n$ is a vector representing the number of charges transported by each reaction. * $F$ is the Faraday constant (a number) -* $\psi$ is a vector representin each reaction's membrane potential (these +* $\psi$ is a vector representing each reaction's membrane potential (these numbers only matter for reactions that transport non-zero charge) Note that, for reactions with zero transported charge, the thermodynamic effect @@ -277,7 +277,7 @@ model for these measurements is imperfect in at least these ways: absolute concentrations. These problems also apply to flux measurements, but there is another issue: -flux meausrements are derived from k +flux measurements are derived from k (sec-prior-model)= ### Prior model diff --git a/maud/data/example_inputs/example_ode/config.toml b/maud/data/example_inputs/example_ode/config.toml index 2d828ba2..6cb6f983 100644 --- a/maud/data/example_inputs/example_ode/config.toml +++ b/maud/data/example_inputs/example_ode/config.toml @@ -1,8 +1,10 @@ -name = "test-ode" +name = "test-ode-adjoint" kinetic_model_file = "kinetic_model.toml" priors_file = "priors.toml" experiments_file = "experiments.toml" likelihood = true +adjoint_solve = true +algebra_solve = false [cmdstanpy_config] iter_warmup = 0 @@ -10,5 +12,19 @@ iter_sampling = 1 chains = 1 [ode_config] +rel_tol = 1e-9 +abs_tol = 1e-9 max_num_steps = 1e9 -timepoint = 1e3 +timepoint = 500 +rel_tol_forward = 1e-12 +abs_tol_forward = 1e-12 +rel_tol_backward = 1e-12 +abs_tol_backward = 1e-12 +rel_tol_quadrature = 1e-12 +abs_tol_quadrature = 1e-12 +num_steps_between_checkpoints = 100000 +interpolation_polynomial = 2 +solver_forward = 2 +solver_backward = 2 +algebra_func_tol = 1e-6 +algebra_rel_tol = 1e-10 diff --git a/maud/data/example_inputs/example_ode/input_data_train.json b/maud/data/example_inputs/example_ode/input_data_train.json deleted file mode 100644 index ad32cdd8..00000000 --- a/maud/data/example_inputs/example_ode/input_data_train.json +++ /dev/null @@ -1 +0,0 @@ -{"N_mic": 4, "N_edge_sub": 5, "N_edge_prod": 5, "N_edge": 5, "N_unbalanced": 2, "N_enzyme": 5, "N_phosphorylation": 0, "N_pme": 0, "N_competitive_inhibition": 1, "N_allostery": 2, "N_allosteric_enzyme": 2, "N_drain": 0, "N_km": 10, "N_sub_km": 5, "N_prod_km": 5, "S": [[-1, -1, -1, 0, 0], [1, 0, 0, -1, 0], [0, 1, 1, 0, -1], [0, 0, 0, 1, 1]], "N_reaction": 4, "N_metabolite": 4, "balanced_mic_ix": [2, 3], "unbalanced_mic_ix": [1, 4], "ci_mic_ix": [4], "edge_type": [1, 1, 1, 1, 1], "edge_to_enzyme": [1, 2, 3, 4, 5], "edge_to_tc": [0, 1, 2, 0, 0], "edge_to_drain": [0, 0, 0, 0, 0], "edge_to_reaction": [1, 2, 2, 3, 4], "water_stoichiometry": [0.0, 0.0, 0.0, 0.0, 0.0], "transported_charge": [0.0, 0.0, 0.0, 0.0, 0.0], "mic_to_met": [1, 2, 3, 4], "subunits": [1, 1, 1, 1, 1], "sub_by_edge_long": [1, 1, 1, 2, 3], "sub_by_edge_bounds": [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]], "prod_by_edge_long": [2, 3, 3, 4, 4], "prod_by_edge_bounds": [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]], "sub_km_ix_by_edge_long": [1, 3, 5, 7, 9], "sub_km_ix_by_edge_bounds": [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]], "prod_km_ix_by_edge_long": [2, 4, 6, 8, 10], "prod_km_ix_by_edge_bounds": [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]], "ci_ix_long": [1], "ci_ix_bounds": [[1, 1], [2, 1], [2, 1], [2, 1], [2, 1]], "allostery_ix_long": [1, 2], "allostery_ix_bounds": [[1, 0], [1, 1], [2, 2], [3, 2], [3, 2]], "allostery_type": [1, 2], "allostery_mic": [3, 3], "phosphorylation_ix_long": [], "phosphorylation_ix_bounds": [[1, 0], [1, 0], [1, 0], [1, 0], [1, 0]], "phosphorylation_type": [], "phosphorylation_pme": [], "priors_km": [[-0.6931471805599453, 0.0, 0.6931471805599453, -0.6931471805599453, 0.0, 0.6931471805599453, -0.6931471805599453, 0.6931471805599453, 0.0, 1.0986122886681098], [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2]], "priors_ki": [[0.0], [0.2]], "priors_kcat": [[-0.6931471805599453, 0.6931471805599453, 0.0, 0.6931471805599453, 0.0], [0.2, 0.2, 0.2, 0.2, 0.2]], "priors_dissociation_constant": [[-1.2039728043259361, -0.10536051565782628], [0.2, 0.2]], "priors_transfer_constant": [[0.0, 0.0], [0.2, 0.2]], "priors_kcat_pme": [[], []], "priors_drain_train": [[[]], [[]]], "priors_drain_test": [[[]], [[]]], "priors_conc_enzyme_train": [[[0.0, 0.6931471805599453, 1.0986122886681098, 0.6931471805599453, 1.0986122886681098]], [[0.2, 0.2, 0.2, 0.2, 0.2]]], "priors_conc_enzyme_test": [[[0.0, 0.6931471805599453, 1.0986122886681098, 0.6931471805599453, 1.0986122886681098]], [[0.2, 0.2, 0.2, 0.2, 0.2]]], "priors_conc_unbalanced_train": [[[1.6094379124341003, -0.6931471805599453]], [[0.2, 0.2]]], "priors_conc_unbalanced_test": [[[1.6094379124341003, -0.6931471805599453]], [[0.2, 0.2]]], "priors_conc_pme_train": [[[]], [[]]], "priors_conc_pme_test": [[[]], [[]]], "priors_psi_train": [[0.0], [2.0]], "prior_loc_dgf": [10.0, 2.0, 5.0, 0.0], "prior_cov_dgf": [[0.2, 0.0, 0.0, 0.0], [0.0, 0.2, 0.0, 0.0], [0.0, 0.0, 0.2, 0.0], [0.0, 0.0, 0.0, 0.2]], "N_experiment_train": 1, "N_flux_measurement_train": 2, "N_enzyme_measurement_train": 0, "N_conc_measurement_train": 2, "N_enzyme_knockout_train": 0, "N_pme_knockout_train": 0, "temperature_train": [298.15], "enzyme_knockout_train_long": [], "enzyme_knockout_train_bounds": [[1, 0]], "pme_knockout_train_long": [], "pme_knockout_train_bounds": [[1, 0]], "yconc_train": [0.323117, 3.02187], "sigma_yconc_train": [0.1, 0.1], "experiment_yconc_train": [1, 1], "mic_ix_yconc_train": [2, 3], "yflux_train": [0.421816, 2.11674], "sigma_yflux_train": [0.01, 0.01], "experiment_yflux_train": [1, 1], "reaction_yflux_train": [3, 4], "yenz_train": [], "sigma_yenz_train": [], "experiment_yenz_train": [], "enzyme_yenz_train": [], "likelihood": 1, "drain_small_conc_corrector": 1e-06, "reject_non_steady": 1, "steady_state_threshold_abs": 1e-08, "steady_state_threshold_rel": 0.001, "rel_tol": 1e-09, "abs_tol": 1e-09, "timepoint": 1000.0, "max_num_steps": 1000000000, "conc_init": [[0.323117, 3.02187]]} diff --git a/maud/data/example_inputs/example_ode/input_data_train_adjoint.json b/maud/data/example_inputs/example_ode/input_data_train_adjoint.json new file mode 100644 index 00000000..08782dce --- /dev/null +++ b/maud/data/example_inputs/example_ode/input_data_train_adjoint.json @@ -0,0 +1 @@ +{"N_mic":4,"N_edge_sub":5,"N_edge_prod":5,"N_edge":5,"N_unbalanced":2,"N_enzyme":5,"N_phosphorylation":0,"N_pme":0,"N_competitive_inhibition":1,"N_allostery":2,"N_allosteric_enzyme":2,"N_drain":0,"N_km":10,"N_sub_km":5,"N_prod_km":5,"S":[[-1,-1,-1,0,0],[1,0,0,-1,0],[0,1,1,0,-1],[0,0,0,1,1]],"N_reaction":4,"N_metabolite":4,"balanced_mic_ix":[2,3],"unbalanced_mic_ix":[1,4],"ci_mic_ix":[4],"edge_type":[1,1,1,1,1],"edge_to_enzyme":[1,2,3,4,5],"edge_to_tc":[0,1,2,0,0],"edge_to_drain":[0,0,0,0,0],"edge_to_reaction":[1,2,2,3,4],"water_stoichiometry":[0.0,0.0,0.0,0.0,0.0],"transported_charge":[0.0,0.0,0.0,0.0,0.0],"mic_to_met":[1,2,3,4],"subunits":[1,1,1,1,1],"sub_by_edge_long":[1,1,1,2,3],"sub_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"prod_by_edge_long":[2,3,3,4,4],"prod_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"sub_km_ix_by_edge_long":[1,3,5,7,9],"sub_km_ix_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"prod_km_ix_by_edge_long":[2,4,6,8,10],"prod_km_ix_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"ci_ix_long":[1],"ci_ix_bounds":[[1,1],[2,1],[2,1],[2,1],[2,1]],"allostery_ix_long":[1,2],"allostery_ix_bounds":[[1,0],[1,1],[2,2],[3,2],[3,2]],"allostery_type":[1,2],"allostery_mic":[3,3],"phosphorylation_ix_long":[],"phosphorylation_ix_bounds":[[1,0],[1,0],[1,0],[1,0],[1,0]],"phosphorylation_type":[],"phosphorylation_pme":[],"priors_km":[[-0.6931471805599453,0.0,0.6931471805599453,-0.6931471805599453,0.0,0.6931471805599453,-0.6931471805599453,0.6931471805599453,0.0,1.0986122886681098],[0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2]],"priors_ki":[[0.0],[0.2]],"priors_kcat":[[-0.6931471805599453,0.6931471805599453,0.0,0.6931471805599453,0.0],[0.2,0.2,0.2,0.2,0.2]],"priors_dissociation_constant":[[-1.2039728043259361,-0.10536051565782628],[0.2,0.2]],"priors_transfer_constant":[[0.0,0.0],[0.2,0.2]],"priors_kcat_pme":[[],[]],"priors_drain_train":[[[]],[[]]],"priors_conc_enzyme_train":[[[0.0,0.6931471805599453,1.0986122886681098,0.6931471805599453,1.0986122886681098]],[[0.2,0.2,0.2,0.2,0.2]]],"priors_conc_unbalanced_train":[[[1.6094379124341003,-0.6931471805599453]],[[0.2,0.2]]],"priors_conc_pme_train":[[[]],[[]]],"priors_psi_train":[[0.0],[2.0]],"prior_loc_dgf":[10.0,2.0,5.0,0.0],"prior_cov_dgf":[[0.2,0.0,0.0,0.0],[0.0,0.2,0.0,0.0],[0.0,0.0,0.2,0.0],[0.0,0.0,0.0,0.2]],"N_experiment_train":1,"N_flux_measurement_train":2,"N_enzyme_measurement_train":0,"N_conc_measurement_train":2,"N_enzyme_knockout_train":0,"N_pme_knockout_train":0,"temperature_train":[298.15],"enzyme_knockout_train_long":[],"enzyme_knockout_train_bounds":[[1,0]],"pme_knockout_train_long":[],"pme_knockout_train_bounds":[[1,0]],"yconc_train":[0.323117,3.02187],"sigma_yconc_train":[0.1,0.1],"experiment_yconc_train":[1,1],"mic_ix_yconc_train":[2,3],"yflux_train":[0.421816,2.11674],"sigma_yflux_train":[0.01,0.01],"experiment_yflux_train":[1,1],"reaction_yflux_train":[3,4],"yenz_train":[],"sigma_yenz_train":[],"experiment_yenz_train":[],"enzyme_yenz_train":[],"likelihood":1,"drain_small_conc_corrector":1e-6,"reject_non_steady":1,"steady_state_threshold_abs":1e-8,"steady_state_threshold_rel":0.001,"rel_tol":1e-9,"abs_tol":1e-9,"timepoint":500.0,"max_num_steps":1000000000,"adjoint_solve":1,"algebra_solve":0,"rel_tol_forward":1e-12,"abs_tol_forward":1e-12,"rel_tol_backward":1e-12,"abs_tol_backward":1e-12,"rel_tol_quadrature":1e-12,"abs_tol_quadrature":1e-12,"num_steps_between_checkpoints":100000,"interpolation_polynomial":2,"solver_forward":2,"solver_backward":2,"algebra_func_tol":1e-6,"algebra_rel_tol":1e-10,"conc_init":[[0.323117,3.02187]]} diff --git a/maud/data/example_inputs/example_ode/input_data_train_adjoint_algebra.json b/maud/data/example_inputs/example_ode/input_data_train_adjoint_algebra.json new file mode 100644 index 00000000..2c161d48 --- /dev/null +++ b/maud/data/example_inputs/example_ode/input_data_train_adjoint_algebra.json @@ -0,0 +1 @@ +{"N_mic":4,"N_edge_sub":5,"N_edge_prod":5,"N_edge":5,"N_unbalanced":2,"N_enzyme":5,"N_phosphorylation":0,"N_pme":0,"N_competitive_inhibition":1,"N_allostery":2,"N_allosteric_enzyme":2,"N_drain":0,"N_km":10,"N_sub_km":5,"N_prod_km":5,"S":[[-1,-1,-1,0,0],[1,0,0,-1,0],[0,1,1,0,-1],[0,0,0,1,1]],"N_reaction":4,"N_metabolite":4,"balanced_mic_ix":[2,3],"unbalanced_mic_ix":[1,4],"ci_mic_ix":[4],"edge_type":[1,1,1,1,1],"edge_to_enzyme":[1,2,3,4,5],"edge_to_tc":[0,1,2,0,0],"edge_to_drain":[0,0,0,0,0],"edge_to_reaction":[1,2,2,3,4],"water_stoichiometry":[0.0,0.0,0.0,0.0,0.0],"transported_charge":[0.0,0.0,0.0,0.0,0.0],"mic_to_met":[1,2,3,4],"subunits":[1,1,1,1,1],"sub_by_edge_long":[1,1,1,2,3],"sub_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"prod_by_edge_long":[2,3,3,4,4],"prod_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"sub_km_ix_by_edge_long":[1,3,5,7,9],"sub_km_ix_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"prod_km_ix_by_edge_long":[2,4,6,8,10],"prod_km_ix_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"ci_ix_long":[1],"ci_ix_bounds":[[1,1],[2,1],[2,1],[2,1],[2,1]],"allostery_ix_long":[1,2],"allostery_ix_bounds":[[1,0],[1,1],[2,2],[3,2],[3,2]],"allostery_type":[1,2],"allostery_mic":[3,3],"phosphorylation_ix_long":[],"phosphorylation_ix_bounds":[[1,0],[1,0],[1,0],[1,0],[1,0]],"phosphorylation_type":[],"phosphorylation_pme":[],"priors_km":[[-0.6931471805599453,0.0,0.6931471805599453,-0.6931471805599453,0.0,0.6931471805599453,-0.6931471805599453,0.6931471805599453,0.0,1.0986122886681098],[0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2]],"priors_ki":[[0.0],[0.2]],"priors_kcat":[[-0.6931471805599453,0.6931471805599453,0.0,0.6931471805599453,0.0],[0.2,0.2,0.2,0.2,0.2]],"priors_dissociation_constant":[[-1.2039728043259361,-0.10536051565782628],[0.2,0.2]],"priors_transfer_constant":[[0.0,0.0],[0.2,0.2]],"priors_kcat_pme":[[],[]],"priors_drain_train":[[[]],[[]]],"priors_conc_enzyme_train":[[[0.0,0.6931471805599453,1.0986122886681098,0.6931471805599453,1.0986122886681098]],[[0.2,0.2,0.2,0.2,0.2]]],"priors_conc_unbalanced_train":[[[1.6094379124341003,-0.6931471805599453]],[[0.2,0.2]]],"priors_conc_pme_train":[[[]],[[]]],"priors_psi_train":[[0.0],[2.0]],"prior_loc_dgf":[10.0,2.0,5.0,0.0],"prior_cov_dgf":[[0.2,0.0,0.0,0.0],[0.0,0.2,0.0,0.0],[0.0,0.0,0.2,0.0],[0.0,0.0,0.0,0.2]],"N_experiment_train":1,"N_flux_measurement_train":2,"N_enzyme_measurement_train":0,"N_conc_measurement_train":2,"N_enzyme_knockout_train":0,"N_pme_knockout_train":0,"temperature_train":[298.15],"enzyme_knockout_train_long":[],"enzyme_knockout_train_bounds":[[1,0]],"pme_knockout_train_long":[],"pme_knockout_train_bounds":[[1,0]],"yconc_train":[0.323117,3.02187],"sigma_yconc_train":[0.1,0.1],"experiment_yconc_train":[1,1],"mic_ix_yconc_train":[2,3],"yflux_train":[0.421816,2.11674],"sigma_yflux_train":[0.01,0.01],"experiment_yflux_train":[1,1],"reaction_yflux_train":[3,4],"yenz_train":[],"sigma_yenz_train":[],"experiment_yenz_train":[],"enzyme_yenz_train":[],"likelihood":1,"drain_small_conc_corrector":1e-6,"reject_non_steady":1,"steady_state_threshold_abs":1e-8,"steady_state_threshold_rel":0.001,"rel_tol":1e-9,"abs_tol":1e-9,"timepoint":500.0,"max_num_steps":1000000000,"adjoint_solve":1,"algebra_solve":1,"rel_tol_forward":1e-12,"abs_tol_forward":1e-12,"rel_tol_backward":1e-12,"abs_tol_backward":1e-12,"rel_tol_quadrature":1e-12,"abs_tol_quadrature":1e-12,"num_steps_between_checkpoints":100000,"interpolation_polynomial":2,"solver_forward":2,"solver_backward":2,"algebra_func_tol":1e-6,"algebra_rel_tol":1e-10,"conc_init":[[0.323117,3.02187]]} diff --git a/maud/data/example_inputs/example_ode/input_data_train_bdf.json b/maud/data/example_inputs/example_ode/input_data_train_bdf.json new file mode 100644 index 00000000..f49d3a42 --- /dev/null +++ b/maud/data/example_inputs/example_ode/input_data_train_bdf.json @@ -0,0 +1 @@ +{"N_mic":4,"N_edge_sub":5,"N_edge_prod":5,"N_edge":5,"N_unbalanced":2,"N_enzyme":5,"N_phosphorylation":0,"N_pme":0,"N_competitive_inhibition":1,"N_allostery":2,"N_allosteric_enzyme":2,"N_drain":0,"N_km":10,"N_sub_km":5,"N_prod_km":5,"S":[[-1,-1,-1,0,0],[1,0,0,-1,0],[0,1,1,0,-1],[0,0,0,1,1]],"N_reaction":4,"N_metabolite":4,"balanced_mic_ix":[2,3],"unbalanced_mic_ix":[1,4],"ci_mic_ix":[4],"edge_type":[1,1,1,1,1],"edge_to_enzyme":[1,2,3,4,5],"edge_to_tc":[0,1,2,0,0],"edge_to_drain":[0,0,0,0,0],"edge_to_reaction":[1,2,2,3,4],"water_stoichiometry":[0.0,0.0,0.0,0.0,0.0],"transported_charge":[0.0,0.0,0.0,0.0,0.0],"mic_to_met":[1,2,3,4],"subunits":[1,1,1,1,1],"sub_by_edge_long":[1,1,1,2,3],"sub_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"prod_by_edge_long":[2,3,3,4,4],"prod_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"sub_km_ix_by_edge_long":[1,3,5,7,9],"sub_km_ix_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"prod_km_ix_by_edge_long":[2,4,6,8,10],"prod_km_ix_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"ci_ix_long":[1],"ci_ix_bounds":[[1,1],[2,1],[2,1],[2,1],[2,1]],"allostery_ix_long":[1,2],"allostery_ix_bounds":[[1,0],[1,1],[2,2],[3,2],[3,2]],"allostery_type":[1,2],"allostery_mic":[3,3],"phosphorylation_ix_long":[],"phosphorylation_ix_bounds":[[1,0],[1,0],[1,0],[1,0],[1,0]],"phosphorylation_type":[],"phosphorylation_pme":[],"priors_km":[[-0.6931471805599453,0.0,0.6931471805599453,-0.6931471805599453,0.0,0.6931471805599453,-0.6931471805599453,0.6931471805599453,0.0,1.0986122886681098],[0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2]],"priors_ki":[[0.0],[0.2]],"priors_kcat":[[-0.6931471805599453,0.6931471805599453,0.0,0.6931471805599453,0.0],[0.2,0.2,0.2,0.2,0.2]],"priors_dissociation_constant":[[-1.2039728043259361,-0.10536051565782628],[0.2,0.2]],"priors_transfer_constant":[[0.0,0.0],[0.2,0.2]],"priors_kcat_pme":[[],[]],"priors_drain_train":[[[]],[[]]],"priors_conc_enzyme_train":[[[0.0,0.6931471805599453,1.0986122886681098,0.6931471805599453,1.0986122886681098]],[[0.2,0.2,0.2,0.2,0.2]]],"priors_conc_unbalanced_train":[[[1.6094379124341003,-0.6931471805599453]],[[0.2,0.2]]],"priors_conc_pme_train":[[[]],[[]]],"priors_psi_train":[[0.0],[2.0]],"prior_loc_dgf":[10.0,2.0,5.0,0.0],"prior_cov_dgf":[[0.2,0.0,0.0,0.0],[0.0,0.2,0.0,0.0],[0.0,0.0,0.2,0.0],[0.0,0.0,0.0,0.2]],"N_experiment_train":1,"N_flux_measurement_train":2,"N_enzyme_measurement_train":0,"N_conc_measurement_train":2,"N_enzyme_knockout_train":0,"N_pme_knockout_train":0,"temperature_train":[298.15],"enzyme_knockout_train_long":[],"enzyme_knockout_train_bounds":[[1,0]],"pme_knockout_train_long":[],"pme_knockout_train_bounds":[[1,0]],"yconc_train":[0.323117,3.02187],"sigma_yconc_train":[0.1,0.1],"experiment_yconc_train":[1,1],"mic_ix_yconc_train":[2,3],"yflux_train":[0.421816,2.11674],"sigma_yflux_train":[0.01,0.01],"experiment_yflux_train":[1,1],"reaction_yflux_train":[3,4],"yenz_train":[],"sigma_yenz_train":[],"experiment_yenz_train":[],"enzyme_yenz_train":[],"likelihood":1,"drain_small_conc_corrector":1e-6,"reject_non_steady":1,"steady_state_threshold_abs":1e-8,"steady_state_threshold_rel":0.001,"rel_tol":1e-9,"abs_tol":1e-9,"timepoint":500.0,"max_num_steps":1000000000,"adjoint_solve":0,"algebra_solve":0,"rel_tol_forward":1e-12,"abs_tol_forward":1e-12,"rel_tol_backward":1e-12,"abs_tol_backward":1e-12,"rel_tol_quadrature":1e-12,"abs_tol_quadrature":1e-12,"num_steps_between_checkpoints":100000,"interpolation_polynomial":2,"solver_forward":2,"solver_backward":2,"algebra_func_tol":1e-6,"algebra_rel_tol":1e-10,"conc_init":[[0.323117,3.02187]]} diff --git a/maud/data/example_inputs/example_ode/input_data_train_bdf_algebra.json b/maud/data/example_inputs/example_ode/input_data_train_bdf_algebra.json new file mode 100644 index 00000000..66db535b --- /dev/null +++ b/maud/data/example_inputs/example_ode/input_data_train_bdf_algebra.json @@ -0,0 +1 @@ +{"N_mic":4,"N_edge_sub":5,"N_edge_prod":5,"N_edge":5,"N_unbalanced":2,"N_enzyme":5,"N_phosphorylation":0,"N_pme":0,"N_competitive_inhibition":1,"N_allostery":2,"N_allosteric_enzyme":2,"N_drain":0,"N_km":10,"N_sub_km":5,"N_prod_km":5,"S":[[-1,-1,-1,0,0],[1,0,0,-1,0],[0,1,1,0,-1],[0,0,0,1,1]],"N_reaction":4,"N_metabolite":4,"balanced_mic_ix":[2,3],"unbalanced_mic_ix":[1,4],"ci_mic_ix":[4],"edge_type":[1,1,1,1,1],"edge_to_enzyme":[1,2,3,4,5],"edge_to_tc":[0,1,2,0,0],"edge_to_drain":[0,0,0,0,0],"edge_to_reaction":[1,2,2,3,4],"water_stoichiometry":[0.0,0.0,0.0,0.0,0.0],"transported_charge":[0.0,0.0,0.0,0.0,0.0],"mic_to_met":[1,2,3,4],"subunits":[1,1,1,1,1],"sub_by_edge_long":[1,1,1,2,3],"sub_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"prod_by_edge_long":[2,3,3,4,4],"prod_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"sub_km_ix_by_edge_long":[1,3,5,7,9],"sub_km_ix_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"prod_km_ix_by_edge_long":[2,4,6,8,10],"prod_km_ix_by_edge_bounds":[[1,1],[2,2],[3,3],[4,4],[5,5]],"ci_ix_long":[1],"ci_ix_bounds":[[1,1],[2,1],[2,1],[2,1],[2,1]],"allostery_ix_long":[1,2],"allostery_ix_bounds":[[1,0],[1,1],[2,2],[3,2],[3,2]],"allostery_type":[1,2],"allostery_mic":[3,3],"phosphorylation_ix_long":[],"phosphorylation_ix_bounds":[[1,0],[1,0],[1,0],[1,0],[1,0]],"phosphorylation_type":[],"phosphorylation_pme":[],"priors_km":[[-0.6931471805599453,0.0,0.6931471805599453,-0.6931471805599453,0.0,0.6931471805599453,-0.6931471805599453,0.6931471805599453,0.0,1.0986122886681098],[0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2]],"priors_ki":[[0.0],[0.2]],"priors_kcat":[[-0.6931471805599453,0.6931471805599453,0.0,0.6931471805599453,0.0],[0.2,0.2,0.2,0.2,0.2]],"priors_dissociation_constant":[[-1.2039728043259361,-0.10536051565782628],[0.2,0.2]],"priors_transfer_constant":[[0.0,0.0],[0.2,0.2]],"priors_kcat_pme":[[],[]],"priors_drain_train":[[[]],[[]]],"priors_conc_enzyme_train":[[[0.0,0.6931471805599453,1.0986122886681098,0.6931471805599453,1.0986122886681098]],[[0.2,0.2,0.2,0.2,0.2]]],"priors_conc_unbalanced_train":[[[1.6094379124341003,-0.6931471805599453]],[[0.2,0.2]]],"priors_conc_pme_train":[[[]],[[]]],"priors_psi_train":[[0.0],[2.0]],"prior_loc_dgf":[10.0,2.0,5.0,0.0],"prior_cov_dgf":[[0.2,0.0,0.0,0.0],[0.0,0.2,0.0,0.0],[0.0,0.0,0.2,0.0],[0.0,0.0,0.0,0.2]],"N_experiment_train":1,"N_flux_measurement_train":2,"N_enzyme_measurement_train":0,"N_conc_measurement_train":2,"N_enzyme_knockout_train":0,"N_pme_knockout_train":0,"temperature_train":[298.15],"enzyme_knockout_train_long":[],"enzyme_knockout_train_bounds":[[1,0]],"pme_knockout_train_long":[],"pme_knockout_train_bounds":[[1,0]],"yconc_train":[0.323117,3.02187],"sigma_yconc_train":[0.1,0.1],"experiment_yconc_train":[1,1],"mic_ix_yconc_train":[2,3],"yflux_train":[0.421816,2.11674],"sigma_yflux_train":[0.01,0.01],"experiment_yflux_train":[1,1],"reaction_yflux_train":[3,4],"yenz_train":[],"sigma_yenz_train":[],"experiment_yenz_train":[],"enzyme_yenz_train":[],"likelihood":1,"drain_small_conc_corrector":1e-6,"reject_non_steady":1,"steady_state_threshold_abs":1e-8,"steady_state_threshold_rel":0.001,"rel_tol":1e-9,"abs_tol":1e-9,"timepoint":500.0,"max_num_steps":1000000000,"adjoint_solve":0,"algebra_solve":1,"rel_tol_forward":1e-12,"abs_tol_forward":1e-12,"rel_tol_backward":1e-12,"abs_tol_backward":1e-12,"rel_tol_quadrature":1e-12,"abs_tol_quadrature":1e-12,"num_steps_between_checkpoints":100000,"interpolation_polynomial":2,"solver_forward":2,"solver_backward":2,"algebra_func_tol":1e-6,"algebra_rel_tol":1e-10,"conc_init":[[0.323117,3.02187]]} diff --git a/maud/data/example_inputs/linear/expected_stan_input.json b/maud/data/example_inputs/linear/expected_stan_input.json index 47adf305..36b40a52 100644 --- a/maud/data/example_inputs/linear/expected_stan_input.json +++ b/maud/data/example_inputs/linear/expected_stan_input.json @@ -1 +1 @@ -{"N_mic": 4, "N_edge_sub": 3, "N_edge_prod": 3, "N_edge": 3, "N_unbalanced": 2, "N_enzyme": 3, "N_phosphorylation": 0, "N_pme": 0, "N_competitive_inhibition": 1, "N_allostery": 2, "N_allosteric_enzyme": 2, "N_drain": 0, "N_km": 5, "N_sub_km": 3, "N_prod_km": 2, "S": [[-1, 0, 0], [1, -1, 0], [0, 1, -1], [0, 0, 1]], "N_reaction": 3, "N_metabolite": 2, "balanced_mic_ix": [2, 3], "unbalanced_mic_ix": [1, 4], "ci_mic_ix": [2], "edge_type": [1, 2, 1], "edge_to_enzyme": [1, 2, 3], "edge_to_tc": [1, 2, 0], "edge_to_drain": [0, 0, 0], "edge_to_reaction": [1, 2, 3], "water_stoichiometry": [0.0, 0.0, 0.0], "transported_charge": [0.0, 0.0, 1.0], "mic_to_met": [1, 1, 2, 2], "subunits": [1, 1, 1], "sub_by_edge_long": [1, 2, 3], "sub_by_edge_bounds": [[1, 1], [2, 2], [3, 3]], "prod_by_edge_long": [2, 3, 4], "prod_by_edge_bounds": [[1, 1], [2, 2], [3, 3]], "sub_km_ix_by_edge_long": [1, 3, 4], "sub_km_ix_by_edge_bounds": [[1, 1], [2, 2], [3, 3]], "prod_km_ix_by_edge_long": [2, 5], "prod_km_ix_by_edge_bounds": [[1, 1], [2, 1], [2, 2]], "ci_ix_long": [1], "ci_ix_bounds": [[1, 0], [1, 1], [2, 1]], "allostery_ix_long": [1, 2], "allostery_ix_bounds": [[1, 1], [2, 2], [3, 2]], "allostery_type": [1, 2], "allostery_mic": [3, 2], "phosphorylation_ix_long": [], "phosphorylation_ix_bounds": [[1, 0], [1, 0], [1, 0]], "phosphorylation_type": [], "phosphorylation_pme": [], "priors_km": [[0.0, 0.0, 0.0, 0.0, 0.0], [0.6, 0.6, 0.6, 0.6, 0.6]], "priors_ki": [[0.0], [0.6]], "priors_kcat": [[0.0, 0.0, 0.0], [0.6, 0.6, 0.6]], "priors_dissociation_constant": [[0.0, 0.0], [0.6, 0.6]], "priors_transfer_constant": [[0.0, 0.0], [0.6, 0.6]], "priors_kcat_pme": [[], []], "priors_drain_train": [[[]], [[]]], "priors_conc_enzyme_train": [[[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]], [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]], "priors_conc_unbalanced_train": [[[-2.3, -2.3], [-2.3, -2.3]], [[2.0, 2.0], [2.0, 2.0]]], "priors_conc_pme_train": [[[]], [[]]], "priors_psi_train": [[-0.95, -0.95], [0.2, 0.2]], "prior_loc_dgf": [-1.0, -2.0], "prior_cov_dgf": [[0.05, 0.0], [0.0, 0.05]], "N_experiment_train": 2, "N_flux_measurement_train": 2, "N_enzyme_measurement_train": 6, "N_conc_measurement_train": 7, "N_enzyme_knockout_train": 0, "N_pme_knockout_train": 0, "temperature_train": [299.0, 298.15], "enzyme_knockout_train_long": [], "enzyme_knockout_train_bounds": [[1, 0], [1, 0]], "pme_knockout_train_long": [], "pme_knockout_train_bounds": [[1, 0], [1, 0]], "yconc_train": [0.59, 1.09, 1.05, 0.54, 0.38, 1.12, 1.14], "sigma_yconc_train": [0.1, 0.05, 0.05, 0.1, 0.1, 0.05, 0.05], "experiment_yconc_train": [1, 1, 1, 2, 2, 2, 2], "mic_ix_yconc_train": [2, 1, 4, 2, 3, 1, 4], "yflux_train": [0.19, 0.39], "sigma_yflux_train": [0.1, 0.1], "experiment_yflux_train": [1, 2], "reaction_yflux_train": [3, 3], "yenz_train": [1.5, 1.5, 1.5, 1.5, 1.5, 1.5], "sigma_yenz_train": [0.1, 0.1, 0.1, 0.1, 0.1, 0.1], "experiment_yenz_train": [1, 1, 1, 2, 2, 2], "enzyme_yenz_train": [1, 2, 3, 1, 2, 3], "likelihood": 1, "drain_small_conc_corrector": 1e-06, "reject_non_steady": 1, "steady_state_threshold_abs": 1e-06, "steady_state_threshold_rel": 0.001, "rel_tol": 1e-05, "abs_tol": 1e-05, "timepoint": 1000.0, "max_num_steps": 1000000, "conc_init": [[0.59, 0.38], [0.54, 0.38]]} +{"N_mic": 4, "N_edge_sub": 3, "N_edge_prod": 3, "N_edge": 3, "N_unbalanced": 2, "N_enzyme": 3, "N_phosphorylation": 0, "N_pme": 0, "N_competitive_inhibition": 1, "N_allostery": 2, "N_allosteric_enzyme": 2, "N_drain": 0, "N_km": 5, "N_sub_km": 3, "N_prod_km": 2, "S": [[-1, 0, 0], [1, -1, 0], [0, 1, -1], [0, 0, 1]], "N_reaction": 3, "N_metabolite": 2, "balanced_mic_ix": [2, 3], "unbalanced_mic_ix": [1, 4], "ci_mic_ix": [2], "edge_type": [1, 2, 1], "edge_to_enzyme": [1, 2, 3], "edge_to_tc": [1, 2, 0], "edge_to_drain": [0, 0, 0], "edge_to_reaction": [1, 2, 3], "water_stoichiometry": [0.0, 0.0, 0.0], "transported_charge": [0.0, 0.0, 1.0], "mic_to_met": [1, 1, 2, 2], "subunits": [1, 1, 1], "sub_by_edge_long": [1, 2, 3], "sub_by_edge_bounds": [[1, 1], [2, 2], [3, 3]], "prod_by_edge_long": [2, 3, 4], "prod_by_edge_bounds": [[1, 1], [2, 2], [3, 3]], "sub_km_ix_by_edge_long": [1, 3, 4], "sub_km_ix_by_edge_bounds": [[1, 1], [2, 2], [3, 3]], "prod_km_ix_by_edge_long": [2, 5], "prod_km_ix_by_edge_bounds": [[1, 1], [2, 1], [2, 2]], "ci_ix_long": [1], "ci_ix_bounds": [[1, 0], [1, 1], [2, 1]], "allostery_ix_long": [1, 2], "allostery_ix_bounds": [[1, 1], [2, 2], [3, 2]], "allostery_type": [1, 2], "allostery_mic": [3, 2], "phosphorylation_ix_long": [], "phosphorylation_ix_bounds": [[1, 0], [1, 0], [1, 0]], "phosphorylation_type": [], "phosphorylation_pme": [], "priors_km": [[0.0, 0.0, 0.0, 0.0, 0.0], [0.6, 0.6, 0.6, 0.6, 0.6]], "priors_ki": [[0.0], [0.6]], "priors_kcat": [[0.0, 0.0, 0.0], [0.6, 0.6, 0.6]], "priors_dissociation_constant": [[0.0, 0.0], [0.6, 0.6]], "priors_transfer_constant": [[0.0, 0.0], [0.6, 0.6]], "priors_kcat_pme": [[], []], "priors_drain_train": [[[]], [[]]], "priors_conc_enzyme_train": [[[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]], [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]], "priors_conc_unbalanced_train": [[[-2.3, -2.3], [-2.3, -2.3]], [[2.0, 2.0], [2.0, 2.0]]], "priors_conc_pme_train": [[[]], [[]]], "priors_psi_train": [[-0.95, -0.95], [0.2, 0.2]], "prior_loc_dgf": [-1.0, -2.0], "prior_cov_dgf": [[0.05, 0.0], [0.0, 0.05]], "N_experiment_train": 2, "N_flux_measurement_train": 2, "N_enzyme_measurement_train": 6, "N_conc_measurement_train": 7, "N_enzyme_knockout_train": 0, "N_pme_knockout_train": 0, "temperature_train": [299.0, 298.15], "enzyme_knockout_train_long": [], "enzyme_knockout_train_bounds": [[1, 0], [1, 0]], "pme_knockout_train_long": [], "pme_knockout_train_bounds": [[1, 0], [1, 0]], "yconc_train": [0.59, 1.09, 1.05, 0.54, 0.38, 1.12, 1.14], "sigma_yconc_train": [0.1, 0.05, 0.05, 0.1, 0.1, 0.05, 0.05], "experiment_yconc_train": [1, 1, 1, 2, 2, 2, 2], "mic_ix_yconc_train": [2, 1, 4, 2, 3, 1, 4], "yflux_train": [0.19, 0.39], "sigma_yflux_train": [0.1, 0.1], "experiment_yflux_train": [1, 2], "reaction_yflux_train": [3, 3], "yenz_train": [1.5, 1.5, 1.5, 1.5, 1.5, 1.5], "sigma_yenz_train": [0.1, 0.1, 0.1, 0.1, 0.1, 0.1], "experiment_yenz_train": [1, 1, 1, 2, 2, 2], "enzyme_yenz_train": [1, 2, 3, 1, 2, 3], "likelihood": 1, "drain_small_conc_corrector": 1e-06, "reject_non_steady": 1, "steady_state_threshold_abs": 1e-06, "steady_state_threshold_rel": 0.001, "rel_tol":1e-5,"abs_tol":1e-5,"timepoint":1000.0,"max_num_steps":1000000,"adjoint_solve":0,"algebra_solve":0,"rel_tol_forward":1e-9,"abs_tol_forward":1e-9,"rel_tol_backward":1e-9,"abs_tol_backward":1e-9,"rel_tol_quadrature":1e-9,"abs_tol_quadrature":1e-9,"num_steps_between_checkpoints":100000,"interpolation_polynomial":2,"solver_forward":2,"solver_backward":2,"algebra_func_tol":1e-6,"algebra_rel_tol":1e-10, "conc_init": [[0.59, 0.38], [0.54, 0.38]]} diff --git a/maud/data/example_inputs/methionine/config.toml b/maud/data/example_inputs/methionine/config.toml index a745cb6c..c5e1532d 100644 --- a/maud/data/example_inputs/methionine/config.toml +++ b/maud/data/example_inputs/methionine/config.toml @@ -5,6 +5,8 @@ experiments_file = "experiments.toml" user_inits_file = "inits.toml" likelihood = true reject_non_steady = true +algebra_solve = false +adjoint_solve = false [cmdstanpy_config] iter_warmup = 1000 @@ -19,5 +21,17 @@ adapt_delta = 0.99 [ode_config] rel_tol = 1e-9 abs_tol = 1e-9 -max_num_steps = 1000000.0 +max_num_steps = 1000000 timepoint = 1e+20 +rel_tol_forward = 1e-9 +abs_tol_forward = 1e-9 +rel_tol_backward = 1e-9 +abs_tol_backward = 1e-9 +rel_tol_quadrature = 1e-9 +abs_tol_quadrature = 1e-9 +num_steps_between_checkpoints = 1e5 +interpolation_polynomial = 2 +solver_forward = 2 +solver_backward = 2 +algebra_func_tol = 1e-6 +algebra_rel_tol = 1e-10 diff --git a/maud/data_model/maud_config.py b/maud/data_model/maud_config.py index a8ea4461..d5b62201 100644 --- a/maud/data_model/maud_config.py +++ b/maud/data_model/maud_config.py @@ -12,6 +12,18 @@ class ODEConfig: abs_tol: float = 1e-9 max_num_steps: int = int(1e9) timepoint: float = 500 + rel_tol_forward: float = 1e-9 + abs_tol_forward: float = 1e-9 + rel_tol_backward: float = 1e-9 + abs_tol_backward: float = 1e-9 + rel_tol_quadrature: float = 1e-9 + abs_tol_quadrature: float = 1e-9 + num_steps_between_checkpoints: int = int(100000) + interpolation_polynomial: int = 2 + solver_forward: int = 2 + solver_backward: int = 2 + algebra_func_tol: float = 1e-6 + algebra_rel_tol: float = 1e-10 @dataclass @@ -54,6 +66,8 @@ class MaudConfig: user_inits_file: Optional[str] = None ode_config: ODEConfig = Field(default_factory=ODEConfig) reject_non_steady: bool = True + adjoint_solve: bool = False + algebra_solve: bool = False steady_state_threshold_abs: float = 1e-8 steady_state_threshold_rel: float = 1e-3 default_initial_concentration: float = 0.01 diff --git a/maud/getting_stan_inputs.py b/maud/getting_stan_inputs.py index 1ae34a17..7ba90d21 100644 --- a/maud/getting_stan_inputs.py +++ b/maud/getting_stan_inputs.py @@ -459,16 +459,33 @@ def get_experiments_input( def get_config_input(config: MaudConfig): """Get Stan input related to algorithm configuration.""" + mc = config + oc = mc.ode_config + return { - "likelihood": int(config.likelihood), - "drain_small_conc_corrector": config.drain_small_conc_corrector, - "reject_non_steady": int(config.reject_non_steady), - "steady_state_threshold_abs": config.steady_state_threshold_abs, - "steady_state_threshold_rel": config.steady_state_threshold_rel, - "rel_tol": config.ode_config.rel_tol, - "abs_tol": config.ode_config.abs_tol, - "timepoint": config.ode_config.timepoint, - "max_num_steps": config.ode_config.max_num_steps, + "likelihood": int(mc.likelihood), + "drain_small_conc_corrector": mc.drain_small_conc_corrector, + "reject_non_steady": int(mc.reject_non_steady), + "steady_state_threshold_abs": mc.steady_state_threshold_abs, + "steady_state_threshold_rel": mc.steady_state_threshold_rel, + "rel_tol": oc.rel_tol, + "abs_tol": oc.abs_tol, + "timepoint": oc.timepoint, + "max_num_steps": oc.max_num_steps, + "adjoint_solve": mc.adjoint_solve, + "algebra_solve": mc.algebra_solve, + "rel_tol_forward": oc.rel_tol_forward, + "abs_tol_forward": oc.abs_tol_forward, + "rel_tol_backward": oc.rel_tol_backward, + "abs_tol_backward": oc.abs_tol_backward, + "rel_tol_quadrature": oc.rel_tol_quadrature, + "abs_tol_quadrature": oc.abs_tol_quadrature, + "num_steps_between_checkpoints": oc.num_steps_between_checkpoints, + "interpolation_polynomial": oc.interpolation_polynomial, + "solver_forward": oc.solver_forward, + "solver_backward": oc.solver_backward, + "algebra_func_tol": oc.algebra_func_tol, + "algebra_rel_tol": oc.algebra_rel_tol, } diff --git a/maud/stan/functions.stan b/maud/stan/functions.stan index eb821f14..3228675d 100644 --- a/maud/stan/functions.stan +++ b/maud/stan/functions.stan @@ -444,4 +444,88 @@ functions { phosphorylation_pme); return (S * edge_flux)[balanced_ix]; } + vector ssf(vector current_balanced, + vector unbalanced, + int[] balanced_ix, + int[] unbalanced_ix, + vector enzyme, + vector dgr, + vector kcat, + vector km, + vector ki, + vector tc, + vector dc, + vector kcat_pme, + vector conc_pme, + vector drain, + real temperature, + real drain_small_conc_corrector, + matrix S, + vector subunits, + array[] int edge_type, + array[] int edge_to_enzyme, + array[] int edge_to_drain, + array[] int ci_mic_ix, + array[] int sub_km_ix_by_edge_long, + array[,] int sub_km_ix_by_edge_bounds, + array[] int prod_km_ix_by_edge_long, + array[,] int prod_km_ix_by_edge_bounds, + array[] int sub_by_edge_long, + array[,] int sub_by_edge_bounds, + array[] int prod_by_edge_long, + array[,] int prod_by_edge_bounds, + array[] int ci_ix_long, + array[,] int ci_ix_bounds, + array[] int allostery_ix_long, + array[,] int allostery_ix_bounds, + array[] int allostery_type, + array[] int allostery_mic, + array[] int edge_to_tc, + array[] int phosphorylation_ix_long, + array[,] int phosphorylation_ix_bounds, + array[] int phosphorylation_type, + array[] int phosphorylation_pme){ + vector[rows(current_balanced)+rows(unbalanced)] current_concentration; + current_concentration[balanced_ix] = current_balanced; + current_concentration[unbalanced_ix] = unbalanced; + vector[cols(S)] edge_flux = get_edge_flux(current_concentration, + enzyme, + dgr, + kcat, + km, + ki, + tc, + dc, + kcat_pme, + conc_pme, + drain, + temperature, + drain_small_conc_corrector, + S, + subunits, + edge_type, + edge_to_enzyme, + edge_to_drain, + ci_mic_ix, + sub_km_ix_by_edge_long, + sub_km_ix_by_edge_bounds, + prod_km_ix_by_edge_long, + prod_km_ix_by_edge_bounds, + sub_by_edge_long, + sub_by_edge_bounds, + prod_by_edge_long, + prod_by_edge_bounds, + ci_ix_long, + ci_ix_bounds, + allostery_ix_long, + allostery_ix_bounds, + allostery_type, + allostery_mic, + edge_to_tc, + phosphorylation_ix_long, + phosphorylation_ix_bounds, + phosphorylation_type, + phosphorylation_pme); + return (S * edge_flux)[balanced_ix]; + } } diff --git a/maud/stan/model.stan b/maud/stan/model.stan index d14a2aa6..befe3e9f 100644 --- a/maud/stan/model.stan +++ b/maud/stan/model.stan @@ -90,8 +90,8 @@ data { array[2, N_experiment_train] vector[N_drain] priors_drain_train; // configuration array[N_experiment_train] vector[N_mic-N_unbalanced] conc_init; - real rel_tol; - real abs_tol; + real algebra_func_tol; + real algebra_rel_tol; real steady_state_threshold_abs; real steady_state_threshold_rel; int max_num_steps; @@ -99,6 +99,21 @@ data { real drain_small_conc_corrector; real timepoint; int reject_non_steady; + int algebra_solve; + int adjoint_solve; + // ode configuration + real rel_tol; + real abs_tol; + real rel_tol_forward; + real abs_tol_forward; + real rel_tol_backward; + real abs_tol_backward; + real rel_tol_quadrature; + real abs_tol_quadrature; + int num_steps_between_checkpoints; + int interpolation_polynomial; + int solver_forward; + int solver_backward; } transformed data { real initial_time = 0; @@ -153,54 +168,162 @@ transformed parameters { extract_ragged(pme_knockout_train_long, pme_knockout_train_bounds, e); conc_pme_experiment[pko_experiment] = rep_vector(0, N_pko_experiment); } - conc_balanced_experiment = - ode_bdf_tol(dbalanced_dt, - conc_init[e], - initial_time, - {timepoint}, - rel_tol, - abs_tol, - max_num_steps, - conc_unbalanced_train[e], - balanced_mic_ix, - unbalanced_mic_ix, - conc_enzyme_experiment, - dgr_train[e], - kcat, - km, - ki, - transfer_constant, - dissociation_constant, - kcat_pme, - conc_pme_experiment, - drain_train[e], - temperature_train[e], - drain_small_conc_corrector, - S, - subunits, - edge_type, - edge_to_enzyme, - edge_to_drain, - ci_mic_ix, - sub_km_ix_by_edge_long, - sub_km_ix_by_edge_bounds, - prod_km_ix_by_edge_long, - prod_km_ix_by_edge_bounds, - sub_by_edge_long, - sub_by_edge_bounds, - prod_by_edge_long, - prod_by_edge_bounds, - ci_ix_long, - ci_ix_bounds, - allostery_ix_long, - allostery_ix_bounds, - allostery_type, - allostery_mic, - edge_to_tc, - phosphorylation_ix_long, - phosphorylation_ix_bounds, - phosphorylation_type, - phosphorylation_pme); + if (adjoint_solve == 1){ + conc_balanced_experiment = + ode_adjoint_tol_ctl(dbalanced_dt, + conc_init[e], + initial_time, + {timepoint}, + rel_tol_forward, # adjoint + rep_vector(abs_tol_forward, N_mic - N_unbalanced), # adjoint + rel_tol_backward, # adjoint + rep_vector(abs_tol_backward, N_mic - N_unbalanced), # adjoint + rel_tol_quadrature, # adjoint + abs_tol_quadrature, # adjoint + max_num_steps, + num_steps_between_checkpoints, # adjoint + interpolation_polynomial, # adjoint + solver_forward, # adjoint + solver_backward, # adjoint + conc_unbalanced_train[e], + balanced_mic_ix, + unbalanced_mic_ix, + conc_enzyme_experiment, + dgr_train[e], + kcat, + km, + ki, + transfer_constant, + dissociation_constant, + kcat_pme, + conc_pme_experiment, + drain_train[e], + temperature_train[e], + drain_small_conc_corrector, + S, + subunits, + edge_type, + edge_to_enzyme, + edge_to_drain, + ci_mic_ix, + sub_km_ix_by_edge_long, + sub_km_ix_by_edge_bounds, + prod_km_ix_by_edge_long, + prod_km_ix_by_edge_bounds, + sub_by_edge_long, + sub_by_edge_bounds, + prod_by_edge_long, + prod_by_edge_bounds, + ci_ix_long, + ci_ix_bounds, + allostery_ix_long, + allostery_ix_bounds, + allostery_type, + allostery_mic, + edge_to_tc, + phosphorylation_ix_long, + phosphorylation_ix_bounds, + phosphorylation_type, + phosphorylation_pme); + } else { + conc_balanced_experiment = + ode_bdf_tol(dbalanced_dt, + conc_init[e], + initial_time, + {timepoint}, + rel_tol, + abs_tol, + max_num_steps, + conc_unbalanced_train[e], + balanced_mic_ix, + unbalanced_mic_ix, + conc_enzyme_experiment, + dgr_train[e], + kcat, + km, + ki, + transfer_constant, + dissociation_constant, + kcat_pme, + conc_pme_experiment, + drain_train[e], + temperature_train[e], + drain_small_conc_corrector, + S, + subunits, + edge_type, + edge_to_enzyme, + edge_to_drain, + ci_mic_ix, + sub_km_ix_by_edge_long, + sub_km_ix_by_edge_bounds, + prod_km_ix_by_edge_long, + prod_km_ix_by_edge_bounds, + sub_by_edge_long, + sub_by_edge_bounds, + prod_by_edge_long, + prod_by_edge_bounds, + ci_ix_long, + ci_ix_bounds, + allostery_ix_long, + allostery_ix_bounds, + allostery_type, + allostery_mic, + edge_to_tc, + phosphorylation_ix_long, + phosphorylation_ix_bounds, + phosphorylation_type, + phosphorylation_pme); + + } + + if (algebra_solve == 1){ + conc_balanced_experiment[1] = solve_powell_tol(ssf, + conc_balanced_experiment[1], + algebra_rel_tol, + algebra_func_tol, + max_num_steps, + conc_unbalanced_train[e], + balanced_mic_ix, + unbalanced_mic_ix, + conc_enzyme_experiment, + dgr_train[e], + kcat, + km, + ki, + transfer_constant, + dissociation_constant, + kcat_pme, + conc_pme_experiment, + drain_train[e], + temperature_train[e], + drain_small_conc_corrector, + S, + subunits, + edge_type, + edge_to_enzyme, + edge_to_drain, + ci_mic_ix, + sub_km_ix_by_edge_long, + sub_km_ix_by_edge_bounds, + prod_km_ix_by_edge_long, + prod_km_ix_by_edge_bounds, + sub_by_edge_long, + sub_by_edge_bounds, + prod_by_edge_long, + prod_by_edge_bounds, + ci_ix_long, + ci_ix_bounds, + allostery_ix_long, + allostery_ix_bounds, + allostery_type, + allostery_mic, + edge_to_tc, + phosphorylation_ix_long, + phosphorylation_ix_bounds, + phosphorylation_type, + phosphorylation_pme); + } conc_train[e, balanced_mic_ix] = conc_balanced_experiment[1]; conc_train[e, unbalanced_mic_ix] = conc_unbalanced_train[e]; { diff --git a/tests/test_unit/test_model_ode.py b/tests/test_unit/test_model_ode.py index 6893dfb3..eee3badf 100644 --- a/tests/test_unit/test_model_ode.py +++ b/tests/test_unit/test_model_ode.py @@ -32,6 +32,13 @@ class ODETestCase: expected_flux_train: List[ExpectedValue] +ODE_CONFIG_OPTIONS = [ + "input_data_train_adjoint_algebra.json", + "input_data_train_adjoint.json", + "input_data_train_bdf_algebra.json", + "input_data_train_bdf.json", +] + TEST_CASES = [ ODETestCase( name="example_ode", @@ -39,7 +46,7 @@ class ODETestCase: "inits.json" ), input_data_path=importlib_resources.files(example_ode).joinpath( - "input_data_train.json" + varied_ode_config ), expected_conc_train=[ ExpectedValue("B", (0, 0), 5.0), @@ -51,6 +58,7 @@ class ODETestCase: ExpectedValue("r4", (0, 3), 2.11674), ], ) + for varied_ode_config in ODE_CONFIG_OPTIONS ]