示例#1
0
def build_cmdstan_model(target_dir):
    """
    Rebuild cmdstan in the build environment, then use this installation to compile the stan model.
    The stan model is copied to {target_dir}/prophet_model.bin
    The cmdstan files required to run cmdstanpy commands are copied to {target_dir}/cmdstan-{version}.

    Parameters
    ----------
    target_dir: Directory to copy the compiled model executable and core cmdstan files to.
    """
    import cmdstanpy

    cmdstan_cache = get_cmdstan_cache()
    download_cmdstan(cmdstan_cache)

    cmdstan_dir = os.path.join(target_dir, f"cmdstan-{CMDSTAN_VERSION}")
    if os.path.isdir(cmdstan_dir):
        rmtree(cmdstan_dir)
    copytree(cmdstan_cache, cmdstan_dir)
    with cmdstanpy.utils.pushd(cmdstan_dir):
        clean_all_cmdstan()
        build_cmdstan()
    cmdstanpy.set_cmdstan_path(cmdstan_dir)

    model_name = "prophet.stan"
    target_name = "prophet_model.bin"
    sm = cmdstanpy.CmdStanModel(stan_file=os.path.join(MODEL_DIR, model_name))
    copy(sm.exe_file, os.path.join(target_dir, target_name))
    # Clean up
    for f in Path(MODEL_DIR).iterdir():
        if f.is_file() and f.name != model_name:
            os.remove(f)
    prune_cmdstan(cmdstan_dir)
示例#2
0
def variational(mi: MaudInput, output_dir: str) -> CmdStanVB:
    """Do variational inference for the posterior defined by mi.

    :param mi: a MaudInput object
    :param output_dir: a string specifying where to save the output.
    """
    mi_options = (
        {}
        if mi.config.variational_options is None
        else mi.config.variational_options
    )
    model = cmdstanpy.CmdStanModel(
        stan_file=os.path.join(HERE, STAN_PROGRAM_RELATIVE_PATH),
        cpp_options=mi.config.cpp_options,
        stanc_options=mi.config.stanc_options,
    )
    set_up_output_dir(output_dir, mi)
    return model.variational(
        data=os.path.join(output_dir, "input_data_train.json"),
        inits=os.path.join(output_dir, "inits.json"),
        **{
            **DEFAULT_VARIATIONAL_CONFIG,
            **mi_options,
            **{"output_dir": output_dir},
        },
    )
示例#3
0
def fit_original_model():

    # There are a few places where I fit this model.
    # Easiest if I just make it a function.  Less copy pasta

    concentration_data = pd.read_csv("data/generated_data/experiment.csv")

    subject_data = concentration_data.drop_duplicates(['subjectids'])

    model_data = dict(sex=subject_data.sex.tolist(),
                      weight=subject_data.weight.tolist(),
                      age=subject_data.age.tolist(),
                      creatinine=subject_data.creatinine.tolist(),
                      n_subjectids=subject_data.shape[0],
                      D=subject_data.D.tolist(),
                      subjectids=concentration_data.subjectids.tolist(),
                      time=concentration_data.time.tolist(),
                      yobs=concentration_data.yobs.tolist(),
                      n=concentration_data.shape[0])

    model = cmdstanpy.CmdStanModel(
        stan_file='experiment_models/original_model.stan')

    fit = model.sample(model_data,
                       chains=12,
                       parallel_chains=4,
                       seed=19920908,
                       show_progress=True)

    return fit, model_data
示例#4
0
def setup_model(*, cmdstan_dir, model_dir, name, model, data):
    """Compile Stan model."""
    cmdstanpy.set_cmdstan_path(cmdstan_dir)

    model = model.replace("<-", "=")

    with tempfile.NamedTemporaryFile("w",
                                     prefix=f"{name}_",
                                     suffix=".stan",
                                     dir=model_dir,
                                     delete=False) as f:
        print(model, file=f)
        model_file = f.name

    with tempfile.NamedTemporaryFile("w",
                                     prefix=f"{name}_",
                                     suffix=".json",
                                     dir=model_dir,
                                     delete=False) as f:
        json.dump(data, f, indent=2, sort_keys=True)
        data_file = f.name

    model_object = cmdstanpy.CmdStanModel(stan_file=model_file)
    exe_file = model_object.exe_file
    return model_file, data_file, exe_file
def test_prior_predictive():

    fit, model_data = fit_original_model()
    t_unique = np.unique(model_data['time'])

    c_check = fit.stan_variable('c_check')
    posterior_from_original = np.quantile(c_check, [0.025,  0.5, 0.975], axis = 0)

    # Fit the prior predictive
    with open('data/param_summary.pkl','rb') as file:
        params = pickle.load(file)

    #The params should be 1 on the z-standardized scale.
    params['sex'] = 1
    params['weight'] = params['weight_mean'] + params['weight_sd']
    params['creatinine'] = params['creatinine_mean'] + params['weight_sd']
    params['age'] = params['age_mean'] + params['age_sd']

    params['nt'] = 8
    params['prediction_times'] = t_unique.tolist()
    params['n_doses'] = 1
    params['dose_times'] = [0]
    params['doses'] = [1.0]

    prior_model = cmdstanpy.CmdStanModel(stan_file = 'experiment_models/prior_predictive.stan')
    prior_predictive = prior_model.sample(params, fixed_param=True, chains=1, iter_sampling = 2000, seed = 19920908) 
    c_check_2 = prior_predictive.stan_variable('C')
    posterior_from_summary= np.quantile(c_check_2, [0.025, 0.5, 0.975], axis = 0)

    np.testing.assert_allclose(posterior_from_original, posterior_from_summary, atol = 0.01)
示例#6
0
 def load_model(self):
     import cmdstanpy
     model_file = pkg_resources.resource_filename(
         'fbprophet',
         'stan_model/prophet_model.bin',
     )
     return cmdstanpy.CmdStanModel(exe_file=model_file)
示例#7
0
def _sample_given_config(mi: MaudInput, output_dir: str,
                         config: dict) -> cmdstanpy.CmdStanMCMC:
    """Call CmdStanModel.sample, having already specified all arguments.

    :param mi: a MaudInput object
    :param output_dir: a string specifying where to save the output.
    :param config: a dictionary of keyword arguments to CmdStanModel.sample.
    """

    input_filepath = os.path.join(output_dir, "input_data.json")
    input_data = get_input_data(mi)
    cmdstanpy.utils.jsondump(input_filepath, input_data)
    stan_program_filepath = os.path.join(HERE, STAN_PROGRAM_RELATIVE_PATH)
    include_path = os.path.join(HERE, INCLUDE_PATH)
    cpp_options = {}
    stanc_options = {"include_paths": [include_path]}
    if config["threads_per_chain"] != 1:
        cpp_options["STAN_THREADS"] = True
        os.environ["STAN_NUM_THREADS"] = str(config["threads_per_chain"])
    model = cmdstanpy.CmdStanModel(
        stan_file=stan_program_filepath,
        stanc_options=stanc_options,
        cpp_options=cpp_options,
    )
    return model.sample(data=input_filepath, **config)
示例#8
0
文件: models.py 项目: yarenty/prophet
 def load_model(self):
     import cmdstanpy
     self._add_tbb_to_path()
     model_file = pkg_resources.resource_filename(
         'prophet',
         'stan_model/prophet_model.bin',
     )
     return cmdstanpy.CmdStanModel(exe_file=model_file)
示例#9
0
def compile(file, log=None):
    if log is None:
        log = _log
    _log.info('preparing Stan model %s', file)
    mod = cmdstanpy.CmdStanModel(file.stem,
                                 file,
                                 logger=log,
                                 cpp_options={'STAN_THREADS': 'yes'})
    return mod
示例#10
0
    def build_model(target_dir, model_dir):
        from shutil import copy
        import cmdstanpy
        model_name = 'prophet.stan'
        target_name = 'prophet_model.bin'

        sm = cmdstanpy.CmdStanModel(
            stan_file=os.path.join(model_dir, model_name))
        sm.compile()
        copy(sm.exe_file, os.path.join(target_dir, target_name))
示例#11
0
def sample(dir, model_file, data_file, exe_file, args=None):
    """Run sample."""
    if args is None:
        args = {}
    model_object = cmdstanpy.CmdStanModel(
        stan_file=model_file,
        exe_file=exe_file,
    )
    fit = model_object.sample(data=data_file, **args)
    fit.save_csvfiles(dir=dir)

    return fit.runset.csv_files
示例#12
0
    def build_model(self):
        """
        build the stan model

        :returns:
        :rtype:

        """

        cpp_options = dict(STAN_THREADS=True)

        self._model = cmdstanpy.CmdStanModel(stan_file=self._stan_file,
                                             model_name=self._name,
                                             cpp_options=cpp_options)
示例#13
0
def test_model_ode(folder, csv_ixs, meaningful_ixs, expected_values):
    """Test that the function get_input_data behaves as expected."""
    input_path = os.path.join(data_path, folder)
    init_data = os.path.join(input_path, "inits.json")
    input_data = os.path.join(input_path, "input_data_train.json")
    SIM_CONFIG["inits"] = init_data
    model = cmdstanpy.CmdStanModel(stan_file=model_path)
    sim_values = (model.sample(data=input_data,
                               **SIM_CONFIG).draws_pd().loc[0,
                                                            csv_ixs].to_list())
    for meaningful_ix, expected_value, sim_value in zip(
            meaningful_ixs, expected_values, sim_values):
        var = meaningful_ix.split("|")[0]
        ix = " ".join(meaningful_ix.split("|")[1:])
        msg = (f"\nExpected value of {var} {ix}: {expected_value}"
               f"\nSimulated value:\n {sim_value}")
        assert isclose(expected_value, sim_value), msg
示例#14
0
def simulate(mi: MaudInput, output_dir: str, n: int) -> CmdStanMCMC:
    """Generate simulations from the prior mean.

    :param mi: a MaudInput object
    :param output_dir: a string specifying where to save the output.
    """
    model = cmdstanpy.CmdStanModel(
        stan_file=os.path.join(HERE, STAN_PROGRAM_RELATIVE_PATH),
        cpp_options=mi.config.cpp_options,
        stanc_options=mi.config.stanc_options,
    )
    set_up_output_dir(output_dir, mi)
    return model.sample(
        output_dir=output_dir,
        iter_sampling=n,
        data=os.path.join(output_dir, "input_data_train.json"),
        inits=os.path.join(output_dir, "inits.json"),
        **SIM_CONFIG,
    )
示例#15
0
def cached_stan_model(source):
  """Compiles and caches a Stan model.

  Args:
    source: Stan model source code.

  Returns:
    model: Compiled Stan model.
  """
  filename = FLAGS.stan_model_filename_template.format(
      hashlib.md5(source.encode('ascii')).hexdigest()) + '.stan'
  # We rely on CmdStanPy's internal caching, based on mtimes of the source and
  # binary files. CmdStanPy will not recompile the model if the binary is newer
  # than the source file.
  if not os.path.exists(filename):
    with open(filename, 'w') as f:
      f.write(source)
  model = cmdstanpy.CmdStanModel(stan_file=filename)
  model.compile()
  return model
示例#16
0
def sample(mi: MaudInput, output_dir: str) -> CmdStanMCMC:
    """Sample from the posterior defined by mi.

    :param mi: a MaudInput object
    :param output_dir: a string specifying where to save the output.
    """
    model = cmdstanpy.CmdStanModel(
        stan_file=os.path.join(HERE, STAN_PROGRAM_RELATIVE_PATH),
        cpp_options=mi.config.cpp_options,
        stanc_options=mi.config.stanc_options,
    )
    set_up_output_dir(output_dir, mi)
    sample_args: dict = {
        "data": os.path.join(output_dir, "input_data_train.json"),
        "inits": os.path.join(output_dir, "inits.json"),
        "output_dir": output_dir,
    }
    sample_args = {**sample_args, **DEFAULT_SAMPLE_CONFIG}
    if mi.config.cmdstanpy_config is not None:
        sample_args = {**sample_args, **mi.config.cmdstanpy_config}
    return model.sample(**sample_args)
示例#17
0
def get_stan_model(stan_model, mpi=False, threads=True):

    assert (
        stan_model in _available_models
    ), f"{stan_model} is not in {','.join(_available_models)}"

    stan_file = pkg_resources.resource_filename(
        "pyipn", os.path.join("stan_models", stan_model)
    )

    cpp_options = {}

    if mpi:
        cpp_options["STAN_MPI"] = True

    if threads:

        cpp_options["STAN_THREADS"] = True

    model = cmdstanpy.CmdStanModel(stan_file=stan_file, cpp_options=cpp_options)

    return model
示例#18
0
def test_model_ode():
    """Test that the function get_input_data behaves as expected."""
    input_path = os.path.join(data_path, "example_ode")
    init_data = os.path.join(input_path, "inits.json")
    input_data = os.path.join(input_path, "input_data.json")
    SIM_CONFIG["inits"] = init_data
    cpp_options = {}
    model = cmdstanpy.CmdStanModel(
        stan_file=model_path,
        stanc_options=stanc_options,
        cpp_options=cpp_options,
    )
    remap = {
        "conc[1,1]": "A",
        "conc[1,2]": "B",
        "conc[1,3]": "C",
        "conc[1,4]": "D",
        "flux[1,1]": "r1",
        "flux[1,4]": "r4",
    }
    true_values = {
        "A": 5.0,
        "B": 0.323117,
        "C": 3.02187,
        "D": 0.5,
        "r1": 0.421816,
        "r4": 2.11674,
    }
    sim_values = (
        model.sample(data=input_data, **SIM_CONFIG)
        .draws_pd()
        .rename(columns=remap)
        .T.loc[true_values.keys(), 0]
        .to_dict()
    )
    msg = f"\nTrue values:\n {true_values}\nSimulated values:\n {sim_values}"
    for true_value, sim_value in zip(true_values.values(), sim_values.values()):
        assert isclose(true_value, sim_value), msg
示例#19
0
def fit(
    formula: str,
    data: typing.Union[dict, pd.DataFrame],
    priors: list = [],
    family: str = "gaussian",
    sample_prior: str = "no",
    sample: bool = True,
    name: str = None,
    outdir: str = ".",
    stan_exe_file: str = None,
    **stan_args,
):
    data = _convert_python_to_R(data)

    # no executable passed -- compile model
    if stan_exe_file is None:
        formula = brms.bf(formula)

        if len(priors) > 0:
            brms_prior = brms.prior_string(*priors[0])
            for p in priors[1:]:
                brms_prior = brms_prior + brms.prior_string(*p)
            assert brms.is_brmsprior(brms_prior)
        else:
            brms_prior = []

        model_code = get_stan_code(
            formula=formula,
            data=data,
            family=family,
            priors=brms_prior,
            sample_prior=sample_prior,
        )

        fname = name if name is not None else 'model'
        stan_file = f'{outdir}/{fname}.stan'
        with open(os.path.abspath(stan_file), 'w') as f:
            f.write(model_code)

        kwargs = {'stan_file': stan_file}

    # load precompiled executable
    else:
        with open(os.path.abspath(f'{stan_exe_file}.stan'), 'r') as f:
            model_code = f.read()

        # allow model name to differ from exe name --
        # this is essential if using same exe to sample given multiple datasets in parallel
        # but depends on removing this check from `cmdstanpy`
        name = name if name is not None else stan_exe_file
        kwargs = {'exe_file': stan_exe_file, 'compile': False}

    model_data = _convert_R_to_python(formula, data, family)
    model_data = _coerce_types(model_code, model_data)

    sm = cmdstanpy.CmdStanModel(model_name=name, **kwargs)
    if not sample:
        return sm
    else:
        fit = sm.sample(data=model_data, output_dir=outdir, **stan_args)
        return fit
from bebi103.stan import disable_logging as be_quiet_stan
from bebi103.stan import check_all_diagnostics
from srep.utils import load_FISH_by_promoter

repo = Repo("./", search_parent_directories=True)
# repo_rootdir holds the absolute path to the top-level of our repo
repo_rootdir = repo.working_tree_dir

# first load data using module util
df_unreg, = load_FISH_by_promoter(("unreg", ))
# pull out one specific promoter for convenience for prior pred check & SBC
df_UV5 = df_unreg[df_unreg["experiment"] == "UV5"]

sm = cmdstanpy.CmdStanModel(
    stan_file=f"{repo_rootdir}/code/stan/constit_post_inf.stan",
    compile=True,
)

all_samples = {}
for gene in df_unreg['experiment'].unique():
    temp_df = df_unreg[df_unreg['experiment'] == gene]
    stan_data = dict(
        N=len(temp_df),
        mRNA_counts=temp_df["mRNA_cell"].values.astype(int),
        ppc=0  # if you produce ppc samples, the InferenceData obj is HUGE
    )
    with be_quiet_stan():
        posterior_samples = sm.sample(data=stan_data, chains=6)
    all_samples[gene] = az.from_cmdstanpy(
        posterior_samples, posterior_predictive=["mRNA_counts_ppc"])
    print(f"For promoter {gene}...")
示例#21
0
with open("data/generated_data/param_summary.pkl", "rb") as file:
    params = pickle.load(file)

# Now that we have drawn covariates via LHS, we can simulate some outcomes from subjects with those covariates.
# I will generate some more PK params, simulate possible outcomes, and compute rewards under the sampled doses.
domain_df = pd.read_csv('data/generated_data/hypercube_sampled_covariates.csv')
sampled_covars_dict = domain_df.to_dict(orient="list")

# Combine the LH sampled covars and the model parameters into a single dictionary.
# Set n so Stan can make all the covars in one go.
params = {**sampled_covars_dict, **params}
params["n_subjects"] = domain_df.shape[0]

# This model is capable of generating PK parameters for subjects given the posterior from the
# original model in step 01.
generative_model = cmdstanpy.CmdStanModel(
    exe_file="experiment_models/draw_pk_parameters")

# Now sample to get the PK params
fit = generative_model.sample(params,
                              fixed_param=True,
                              iter_sampling=1,
                              seed=19920908)

# Append the pk params.  These are all I need to generate observations and pk curves
domain_df["cl"] = fit.stan_variable("cl").squeeze()
domain_df["ke"] = fit.stan_variable("ke").squeeze()
domain_df["ka"] = fit.stan_variable("ka").squeeze()
domain_df["alpha"] = fit.stan_variable("alpha").squeeze()

# Here is where we sample possible outcomes for each simulated subject.
# Because I know their PK parameters, I can ask myself "what could I resonably expect to see under my model at t=tobs"
示例#22
0
def _create_test_data():
    """Create test data to local folder.

    This function is needed when test data needs to be updated.
    """
    import platform
    import shutil
    from pathlib import Path

    import cmdstanpy

    model_code = """
        data {
            int<lower=0> J;
            real y[J];
            real<lower=0> sigma[J];
        }

        parameters {
            real mu;
            real<lower=0> tau;
            real eta[J];
        }

        transformed parameters {
            real theta[J];
            for (j in 1:J)
                theta[j] = mu + tau * eta[j];
        }

        model {
            mu ~ normal(0, 5);
            tau ~ cauchy(0, 5);
            eta ~ normal(0, 1);
            y ~ normal(theta, sigma);
        }

        generated quantities {
            vector[J] log_lik;
            vector[J] y_hat;
            for (j in 1:J) {
                log_lik[j] = normal_lpdf(y[j] | theta[j], sigma[j]);
                y_hat[j] = normal_rng(theta[j], sigma[j]);
            }
        }
    """
    stan_file = "stan_test_data.stan"
    with open(stan_file, "w") as file_handle:
        print(model_code, file=file_handle)
    model = cmdstanpy.CmdStanModel(stan_file=stan_file)
    os.remove(stan_file)
    stan_data = {
        "J": 8,
        "y": np.array([28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0]),
        "sigma": np.array([15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0]),
    }
    fit_no_warmup = model.sample(data=stan_data,
                                 iter_sampling=100,
                                 iter_warmup=1000,
                                 save_warmup=False)
    fit_no_warmup.save_csvfiles(dir=".")
    fit_files = {
        "cmdstanpy_eight_schools_nowarmup": [],
        "cmdstanpy_eight_schools_warmup": [],
    }
    for path in fit_no_warmup.runset.csv_files:
        path = Path(path)
        _, num = path.stem.rsplit("-", 1)
        new_path = path.parent / ("cmdstanpy_eight_schools_nowarmup-" + num +
                                  path.suffix)
        shutil.move(path, new_path)
        fit_files["cmdstanpy_eight_schools_nowarmup"].append(new_path)
    fit_warmup = model.sample(data=stan_data,
                              iter_sampling=100,
                              iter_warmup=500,
                              save_warmup=True)
    fit_warmup.save_csvfiles(dir=".")
    for path in fit_no_warmup.runset.csv_files:
        path = Path(path)
        _, num = path.stem.rsplit("-", 1)
        new_path = path.parent / ("cmdstanpy_eight_schools_warmup-" + num +
                                  path.suffix)
        shutil.move(path, new_path)
        fit_files["cmdstanpy_eight_schools_warmup"].append(new_path)
    path = Path(stan_file)
    os.remove(
        str(path.parent /
            (path.stem + (".exe" if platform.system() == "Windows" else ""))))
    os.remove(str(path.parent / (path.stem + ".hpp")))
    return fit_files
glucose_data = glucose_data[glucose_data['compound'].isin(
    ['glucose', 'acetate'])][[
        'replicate', 'od_600nm', 'compound', 'rel_area_phosphate', 'date',
        'carbon_source'
    ]]
acetate_data = acetate_data[acetate_data['compound'] == 'acetate'][[
    'replicate', 'od_600nm', 'compound', 'rel_area_phosphate', 'date',
    'carbon_source'
]]

# Merge the turnover measurements and save
turnover = pd.concat([glucose_data, acetate_data], sort=False)
turnover.to_csv('../../data/collated_turnover_measurements.csv', index=False)
# %%
# Load and compile the inferrential model
model = cmdstanpy.CmdStanModel(
    stan_file='../stan/hierarchical_yield_coefficient.stan')

# %%

# Define the percentiles to compute
percs = [(2.5, 97.5), (12.5, 87.5), (25, 75), (37.5, 62.5), (47.5, 52.5)]
perc_labels = [95, 75, 50, 25, 5]

# Group by strain, carbon source, and then compound
param_samples, conc_samples = [], []
param_summary, conc_summary = pd.DataFrame([]), pd.DataFrame([])
yield_fits, calib_fits = [], []
for g, d in turnover.groupby(['carbon_source', 'compound']):
    # Get the correct calibration data
    calib = calib_data[calib_data['compound'] == g[1]]
示例#24
0
def test_stan_concentration_function():

    # Test if my stan model and my analytical calculation fo the concentration function match up
    model_code='''
    functions{
    vector heaviside(vector t){
        
        vector[size(t)] y;
        for(i in 1:size(t)){
        y[i] = t[i]<=0 ? 0 : 1;
        }
        return y;
    }
    
    
    vector conc(real D, vector t, real Cl, real ka, real ke){
        return heaviside(t) .* (exp(-ka*t) - exp(-ke*t)) * (0.5 * D * ke * ka ) / (Cl *(ke - ka));
    }
    
    }
    data{
        int nt;
        vector[nt] t;
        real cl;
        real ka;
        real ke;
        
        int n_doses;
        vector[n_doses] dose_times;
        vector[n_doses] doses;
    }
    model{}
    generated quantities{
        vector[nt] C = rep_vector(0.0, nt);
        for(i in 1:n_doses){
        C += conc(doses[i], t - dose_times[i], cl, ka, ke);
    }
    }
    '''


    model_dir = '/tmp/test_model.stan'
    with open(model_dir, 'w') as f:
        f.write(model_code)
        

    t = np.arange(0, 48.05, 0.05).tolist()
    nt = len(t)
    cl = 3.3
    ka = 1.0
    ke = 0.2
    dose_times = np.arange(0, 48+12, 12).tolist()
    doses = np.tile(10, len(dose_times)).tolist()
    n_doses = len(dose_times)

    model = cmdstanpy.CmdStanModel(stan_file = model_dir)
    model_data = dict(t=t, cl=cl, ka=ka, ke=ke, dose_times=dose_times, doses=doses, nt=nt, n_doses=n_doses)

    conc_from_stan = model.sample(model_data, fixed_param=True, iter_sampling=1).stan_variable("C").ravel()

    repeat_dose_conc = repeated_dose_concentration(cl, ke, ka)
    conc_from_analytical = repeat_dose_conc(np.array(t), np.array(dose_times), np.array(doses)) 

    np.testing.assert_allclose(conc_from_analytical, conc_from_stan, rtol = 1e-4, atol = 1e-4)
示例#25
0
import cmdstanpy
import numpy as np
import pickle
import warnings

from scipy.stats import gamma, norm
from typing import List, Dict

# Dictionary of parameters to be used in models.  Prevents me from having to
# Manually enter prior params. See step 01
with open('data/generated_data/param_summary.pkl', 'rb') as file:
    _params = pickle.load(file)

# # This model is the prior predictive check.  Give it covariates and it will give you predictions
_prior_model = cmdstanpy.CmdStanModel(
    exe_file='experiment_models/prior_predictive')
_prior_tdm_model = cmdstanpy.CmdStanModel(
    exe_file='experiment_models/tdm_prior_predictive')

# # This model fits to simulated data.  Give it covariates and observed data and it will give you predictions.
_conditioning_model = cmdstanpy.CmdStanModel(
    exe_file='experiment_models/condition_on_patients')
_tdm_model = cmdstanpy.CmdStanModel(
    exe_file='experiment_models/bayesian_tdm_single_subject')


def validate_input(x) -> np.ndarray:
    '''
    Helper function to turn lists into arrays when I pass arguments to functions.
    Just cuts down on me writing np.array.
    '''
import cmdstanpy
import os
# Compile models to be used
model_files = ('experiment_models/' + file
               for file in os.listdir('experiment_models')
               if file.endswith('.stan'))

for model in model_files:
    print(f'\n\ncompiling {model}\n\n')
    cmdstanpy.CmdStanModel(stan_file=model)
示例#27
0
def predict(
    mi: MaudInput,
    output_dir: str,
    idata_train: az.InferenceData,
) -> az.InferenceData:
    """Call CmdStanModel.sample for out of sample predictions.

    :param mi: a MaudInput object
    :param output_dir: directory where output will be saved
    :param idata_train: InferenceData object with posterior draws
    """
    model = cmdstanpy.CmdStanModel(
        stan_file=os.path.join(HERE, STAN_PROGRAM_RELATIVE_PATH_PREDICT),
        cpp_options=mi.config.cpp_options,
        stanc_options=mi.config.stanc_options,
    )
    set_up_output_dir(output_dir, mi)
    kinetic_parameters = [
        "keq",
        "km",
        "kcat",
        "dissociation_constant",
        "transfer_constant",
        "kcat_phos",
        "ki",
    ]
    posterior = idata_train.get("posterior")
    sample_stats = idata_train.get("sample_stats")
    assert posterior is not None
    assert sample_stats is not None
    chains = sample_stats["chain"]
    draws = sample_stats["draw"]
    dims = {
        "conc": ["experiment", "mic"],
        "conc_enzyme": ["experiment", "enzyme"],
        "flux": ["experiment", "reaction"],
    }
    for chain in chains:
        for draw in draws:
            inits = {
                par: (
                    posterior[par]
                    .sel(chain=chain, draw=draw)
                    .to_series()
                    .values
                )
                for par in kinetic_parameters
                if par in posterior.keys()
            }
            sample_args: dict = {
                "data": os.path.join(output_dir, "input_data_test.json"),
                "inits": inits,
                "output_dir": output_dir,
                "iter_warmup": 0,
                "iter_sampling": 1,
                "fixed_param": True,
                "show_progress": False,
            }
            if mi.config.cmdstanpy_config_predict is not None:
                sample_args = {
                    **sample_args,
                    **mi.config.cmdstanpy_config_predict,
                }
            mcmc_draw = model.sample(**sample_args)
            idata_draw = az.from_cmdstan(
                mcmc_draw.runset.csv_files,
                coords={
                    "experiment": [
                        e.id for e in mi.measurements.experiments if e.is_test
                    ],
                    "mic": [m.id for m in mi.kinetic_model.mics],
                    "enzyme": [e.id for e in mi.kinetic_model.enzymes],
                    "reaction": [r.id for r in mi.kinetic_model.reactions],
                },
                dims=dims,
            ).assign_coords(
                coords={"chain": [chain], "draw": [draw]},
                groups="posterior_groups",
            )
            if draw == 0:
                idata_chain = idata_draw.copy()
            else:
                idata_chain = az.concat(
                    [idata_chain, idata_draw], dim="draw", reset_dim=False
                )
        if chain == 0:
            out = idata_chain.copy()
        else:
            out = az.concat([out, idata_chain], dim="chain", reset_dim=False)
    return out
示例#28
0
from git import Repo #for directory convenience

import numpy as np

import cmdstanpy
import arviz as az

repo = Repo("./", search_parent_directories=True)
# repo_rootdir holds the absolute path to the top-level of our repo                 
repo_rootdir = repo.working_tree_dir

sm_gaussF = cmdstanpy.CmdStanModel(
    stan_file=f"{repo_rootdir}/code/stan/test_gaussF.stan",
    compile=True,)

# stan needs to know how many data points to generate,
# so pick a representative promoter
stan_data = dict(
    a=22.35,
    b=-17.3,
    c=12.5,
    z=-1.59,
    )

stan_output = sm_gaussF.sample(
    data=stan_data,
    fixed_param=True,
    iter_sampling=1,
)

# Convert to ArviZ InferenceData object
示例#29
0
repo = Repo("./", search_parent_directories=True)
# repo_rootdir holds the absolute path to the top-level of our repo
repo_rootdir = repo.working_tree_dir

# first load data using module util
df_unreg, = load_FISH_by_promoter(("unreg", ))
# pull out one specific promoter for convenience for prior pred check & SBC
df_UV5 = df_unreg[df_unreg["experiment"] == "UV5"]

# ############################################################################
# PRIOR PREDICTIVE CHECK
# ############################################################################

sm_prior_pred = cmdstanpy.CmdStanModel(
    stan_file=f"{repo_rootdir}/code/stan/constit_prior_pred.stan",
    compile=True,
)

# stan needs to know how many data points to generate,
# so pick a representative promoter
data_prior_pred = dict(N=len(df_UV5))

prior_pred_samples = sm_prior_pred.sample(
    data=data_prior_pred,
    fixed_param=True,
    iter_sampling=1000,
)

# Convert to ArviZ InferenceData object
prior_pred_samples = az.from_cmdstanpy(
    posterior=prior_pred_samples,  # this line b/c of arviz bug, PR#979
示例#30
0
#%%
fig, ax = plt.subplots(1, 1)

x = np.linspace(0, 200)

for sample in range(samples):

    prior_dist = stats.lognorm(prior_scale[sample], prior_shape[sample])
    ax.plot(x, prior_dist.pdf(x), 'r-', alpha=0.05)

#%%
# Build Model

file_path = os.path.join(os.getcwd(), 'model_1.stan')

model = cmdstanpy.CmdStanModel(stan_file=file_path)
model.name
model.stan_file
model.exe_file
print(model.code())

#%%
# Sample
stan_data = {'N': claims.shape[0], 'claims': claims.tolist()}

fit = model.sample(data=stan_data)  #, output_dir='./model_output')

print(fit)

print(fit.sample.shape)