pfr

ckbit.pfr

ckbit.pfr.MAP(filename, model_name='pfr', pH=False, R_units='kJ/mol/K', priors=None, A0_up_lim=35, Ea_up_lim=350, init_random=False, seed=None, verbose=True, rel_tol=0.0005, abs_tol=0.0005, max_num_steps=100, A0_init=10, Ea_init=80, sigma_init=1)

MAP estimation for PFR parameter estimation

Parameters
  • filename (str) – Filename of Excel input file that contains the appropriately formated PFR data (see examples)

  • model_name (str, optional) – Name of model, used for saving/loading compilied Stan code, Default is ‘pfr’

  • pH (bool, optional) – Indicates whether pH is in the reaction equations and dataset, Default is false

  • R_units (str, optional) – Ideal gas constant units, Default is ‘kJ/mol/K’, note: if changing these units, the prior for Ea should be updated accordingly as well as Ea_up_lim

  • priors (list of str, optional) – User defined prior distributions, Must have appropriate format (see examples), Default is None

  • A0_up_lim (float, optional) – Upper limit of A0 terms to be estimated, Default is 35

  • Ea_up_lim (str, optional) – Upper limit of Ea terms to be estimated, Default is 350

  • init_random (bool, optional) – Flag to signal whether the initialization should be random or if it should use user specified values, Default is False

  • seed (int, optional) – A positive integer used to seed the random number generation, Default is np.random.randint(0, 1E9)

  • verbose (bool, optional) – Flag to signal whether Stan intermediate output should be piped to terminal, Default is True

  • rel_tol (float, optional) – Relative tolerance for Stan’s ODE solver, Default is 5E-4

  • abs_tol (float, optional) – Absolute tolerance for Stan’s ODE solver, Default is 5E-4

  • max_num_steps (int, optional) – Maximum number of steps for Stan’s ODE solver, Default is 100

  • A0_init (float, optional) – Initialization point for the sampler for A0, Default is 10

  • Ea_init (float, optional) – Initialization point for the sampler for Ea, Default is 80

  • sigma_init (float, optional) – Initialization point for the sampler for sigma, Default is 1

Returns

point_estimates – Dictionary containing values corresponding to modes of posterior

Return type

dict

ckbit.pfr.MCMC(filename, model_name='pfr', pH=False, R_units='kJ/mol/K', priors=None, A0_up_lim=35, Ea_up_lim=350, warmup=None, iters=2000, chains=2, n_jobs=1, verbose=True, trace=True, init_random=False, seed=None, control={'adapt_delta': 0.8, 'max_treedepth': 20}, rel_tol=0.0005, abs_tol=0.0005, max_num_steps=100, A0_init=10, Ea_init=80, sigma_init=1)

Bayesian inference using MCMC sampling for PFR parameter estimation

Parameters
  • filename (str) – Filename of Excel input file that contains the appropriately formated PFR data (see examples)

  • model_name (str, optional) – Name of model, used for saving/loading compilied Stan code, Default is ‘pfr’

  • pH (bool, optional) – Flag indicating whether pH is in the reaction equations and dataset, Default is false

  • R_units (str, optional) – Ideal gas constant units, Default is ‘kJ/mol/K’, note: if changing these units, the prior for Ea should be updated accordingly as well as Ea_up_lim

  • priors (list of str, optional) – User defined prior distributions, Must have appropriate format (see examples) in accordance with Stan, Default is None

  • A0_up_lim (float, optional) – Upper limit of A0 terms to be estimated, Default is 35

  • Ea_up_lim (str, optional) – Upper limit of Ea terms to be estimated, Default is 350

  • warmup (int, optional) – Number of warmup iterations for MCMC sampler for each chain, Must be less than the number of total iterations, Default is None, which sets warmup equal to half of iters (the Stan default)

  • iters (int, optional) – Number of total interations for MCMC sampler for each chain, Must be greater than the warmup, total number of samples useable for MCMC inference will equal (chains*(iters-warmup)), Default is 2000

  • chains (int, optional) – Number of chains for MCMC sampler, Default is 2

  • n_jobs (int, optional) – Number of jobs to run in parallel for MCMC sampler, maximum is number of cores the computer has, Default is 1

  • verbose (bool, optional) – Flag to signal whether Stan intermediate output should be piped to terminal, Default is True

  • trace (bool, optional) – Flag to signal whether traceplots should be generated upon the run’s completion, Default is True

  • init_random (bool, optional) – Flag to signal whether the initialization should be random or if it should use user specified values, Default is False

  • seed (int, optional) – A positive integer used to seed the random number generation, use one seed even when multiple chains are used since the other chain’s seeds are generated from the first chain’s to avoid dependency among random number streams, set this seed for repeatable inference sampling runs, Default is np.random.randint(0, 1E9)

  • control (dict, optional) – Dictionary of settings for MCMC sampler, Default is {‘adapt_delta’:0.9999, ‘max_treedepth’:100}, more information at: https://mc-stan.org/docs/2_23/reference-manual/hmc-algorithm-parameters.html

  • int_tol (float, optional) – Relative tolerance for Stan’s ODE solver, Default is 5E-4

  • abs_tol (float, optional) – Absolute tolerance for Stan’s ODE solver, Default is 5E-4

  • max_num_steps (int, optional) – Maximum number of steps for Stan’s ODE solver, Default is 100

  • A0_init (float, optional) – Initialization point for the sampler for A0, Default is 10

  • Ea_init (float, optional) – Initialization point for the sampler for Ea, Default is 80

  • sigma_init (float, optional) – Initialization point for the sampler for sigma, Default is 1

Returns

  • fit (Stan object) – Stan object containing results of the MCMC run

  • sample_vals (dict) – Dictionary of values collected by the MCMC sampler

ckbit.pfr.StanModel_cache(model_code, model_name, **kwargs)

Function for saving/loading compiled Stan code to avoid recompilation

Parameters
  • model_code (str) – Stan code written in proper format

  • model_name (str, optional) – Name of model, used for saving/loading compilied Stan code

Returns

sm – Stan object from pystan function StanModel

Return type

Stan model

ckbit.pfr.VI(filename, model_name='pfr', pH=False, R_units='kJ/mol/K', priors=None, A0_up_lim=35, Ea_up_lim=350, iters=2000000, algorithm='fullrank', verbose=True, rel_tol=0.0005, abs_tol=0.0005, max_num_steps=100, seed=None, sample_file='./samples.csv', diagnostic_file='./diagnostics.csv', grad_samples=1, elbo_samples=100, tol_rel_obj=0.01, adapt_iter=50, eval_elbo=100, output_samples=10000, eta=0.2, adapt_engaged=False, trace=True, init_random=False, A0_init=10, Ea_init=80, sigma_init=1)

Bayesian inference using VI for PFR parameter estimation

Parameters
  • filename (str) – Filename of Excel input file that contains the appropriately formated PFR data (see examples)

  • model_name (str, optional) – Name of model, used for saving/loading compilied Stan code, Default is ‘pfr’

  • pH (bool, optional) – Indicates whether pH is in the reaction equations and dataset, Default is false

  • R_units (str, optional) – Ideal gas constant units, Default is ‘kJ/mol/K’, note: if changing these units, the prior for Ea should be updated accordingly as well as Ea_up_lim

  • priors (list of str, optional) – User defined prior distributions, Must have appropriate format (see examples) in accordance with Stan, Default is None

  • A0_up_lim (float, optional) – Upper limit of A0 terms to be estimated, Default is 35

  • Ea_up_lim (str, optional) – Upper limit of Ea terms to be estimated, Default is 350

  • iters (int, optional) – Maximum number of interations for VI sampler, Default is 2,000,000

  • algorithm (str, optional) – Algorithm to use for VI, either ‘meanfield’ (for uncorrelated posteriors) or ‘fullrank’ (for correlated posteriors), Default is ‘fullrank’

  • verbose (bool, optional) – Flag to signal whether Stan intermediate output should be piped to terminal, Default is True

  • rel_tol (float, optional) – Relative tolerance for Stan’s ODE solver, Default is 5E-4

  • abs_tol (float, optional) – Absolute tolerance for Stan’s ODE solver, Default is 5E-4

  • max_num_steps (int, optional) – Maximum number of steps for Stan’s ODE solver, Default is 100

  • seed (int, optional) – A positive integer used to seed the random number generation, Default is np.random.randint(0, 1E9)

  • sample_file (str, optional) – Filename where the VI samples are saved to, Default is ‘./samples. csv’

  • diagnostic_file (str, optional) – Filename where the VI diagonstics are saved to, Default is ‘./ diagnostics.csv’

  • grad_samples (int, optional) – Number of gradient evaluations to make to estimate gradient for VI solver, Default is 1

  • elbo_samples (int, optional) – Number of elbo evaluations to make to estimate ELBO for VI solver, Default is 100

  • tol_rel_obj (float, optional) – Relative tolerance for VI solver, Default is 0.01

  • adapt_iter (int, optional) – Number of iterations for adaptive tuning of eta, Default is 50

  • eval_elbo (int, optional) – Number of iterations between ELBO evaluations for VI solver, Default is 100

  • output_samples (int, optional) – Number of samples to draw from final approximation of posterior from VI solver, Default is 10,000

  • eta (float, optional) – Positive, stepsize weighing parameter for VI solver, Ignored if adapt_iter is True, Default is 0.2

  • adapt_engaged – Flag to signal whether eta should be automatically tuned, Default is False

  • trace (bool, optional) – Flag to signal whether traceplots should be generated upon the run’s completion, Default is True

  • init_random (bool, optional) – Flag to signal whether the initialization should be random or if it should use user specified values, Default is False

  • A0_init (float, optional) – Initialization point for the sampler for A0, Default is 10

  • Ea_init (float, optional) – Initialization point for the sampler for Ea, Default is 80

  • sigma_init (float, optional) – Initialization point for the sampler for sigma, Default is 1

Returns

  • fit (Stan object) – Stan object containing results of the VI run

  • sample_vals (dict) – Dictionary of values collected by the VI sampler

ckbit.pfr.pfr_exp_data(filename, pH=False)

Processes Excel file with PFR data that can be used for PFR estimation

Parameters
  • filename (str) – Filename of Excel input file that contains the appropriately formated PFR data (see examples)

  • pH (bool, optional) – Indicates whether pH is in the reaction equations and dataset, Default is false

Returns

  • n_runs (int) – Number of experimental runs in dataset

  • rxns (int) – Number of reactions in network

  • sp (int) – Number of species in dataset

  • rxn_eqs (str) – Reaction equations

  • pfr_data (dict) – Dictionary containing PFR data inputs for Stan code

ckbit.pfr.write_pfr_stan_code(runs, rxns, species, rxn_eqs, R=0.0083144598, A0_up_lim=35, Ea_up_lim=350, pH=False, priors=None, rel_tol=0.0005, abs_tol=0.0005, max_num_steps=100)

Writes Stan code that can be used for PFR estimation

Parameters
  • runs (int) – Number of experimental runs in dataset

  • rxns (int) – Number of reactions in network

  • species (int) – Number of species in dataset

  • rxn_eqs (str) – Reaction equations

  • R (float, optional) – Ideal gas constant for user selected units, Default is 0.0083144598 which corresponds to kJ/mol/K, altering this value will necessitate altering Ea_up_lim and the priors for Ea accordingly

  • A0_up_lim (float, optional) – Upper limit of A0 terms to be estimated, Default is 35

  • Ea_up_lim (str, optional) – Upper limit of Ea terms to be estimated, Default is 350

  • pH (bool, optional) – Indicates whether pH is in the reaction equations and dataset, Default is false

  • priors (list of str, optional) – User defined prior distributions, Must have appropriate format (see examples) in accordance with Stan, Default is None

  • rel_tol (float, optional) – Relative tolerance for Stan’s ODE solver, Default is 5E-4

  • abs_tol (float, optional) – Absolute tolerance for Stan’s ODE solver, Default is 5E-4

  • max_num_steps (int, optional) – Maximum number of steps for Stan’s ODE solver, Default is 100

Returns

code_pfr – Code written in Stan syntax used for PFR estimation

Return type

str