<jupyter_start><jupyter_text>Parameter identification exampleHere is a simple toy model that we use to demonstrate the working of the inference package$\emptyset \xrightarrow[]{k_1} X \; \; \; \; X \xrightarrow[]{d_1} \emptyset$ Run the MCMC algorithm to identify parameters from the experimental data In this demonstration, we will try to use multiple trajectories of data taken under multiple initial conditions and different length of time points?<jupyter_code>%matplotlib inline
%config InlineBackend.figure_format = "retina"

from matplotlib import rcParams
rcParams["savefig.dpi"] = 100
rcParams["figure.dpi"] = 100
rcParams["font.size"] = 20<jupyter_output><empty_output><jupyter_text>Using Gaussian prior for `k1`<jupyter_code>%matplotlib inline
import bioscrape as bs
from bioscrape.types import Model
from bioscrape.inference import py_inference
import numpy as np
import pylab as plt
import pandas as pd
# Import a bioscrape/SBML model
M = Model(sbml_filename = 'toy_sbml_model.xml')
# Import data from CSV
# Import a CSV file for each experiment run
df = pd.read_csv('test_data.csv', delimiter = '\t', names = ['X','time'], skiprows = 1)
M.set_species({'X':df['X'][0]})

# Create prior for parameters
prior = {'d1' : ['gaussian', 0.2, 200]}
sampler, pid = py_inference(Model = M, exp_data = df, measurements = ['X'], time_column = ['time'],
            nwalkers = 5, init_seed = 0.15, nsteps = 1500, sim_type = 'deterministic',
            params_to_estimate = ['d1'], prior = prior)<jupyter_output>100%|█████████████████████████████████████████████████████████████████████████████| 1500/1500 [00:01<00:00, 807.07it/s]<jupyter_text>Using uniform priors and estimating both `k1` and `d1` and use the pid => parameter inference object directly.<jupyter_code>%matplotlib inline
import bioscrape as bs
from bioscrape.types import Model
from bioscrape.inference import py_inference

import numpy as np
import pylab as plt
import pandas as pd
# Import a bioscrape/SBML model
M = Model(sbml_filename = 'toy_sbml_model.xml')
# Import data from CSV
# Import a CSV file for each experiment run
df = pd.read_csv('test_data.csv', delimiter = '\t', names = ['X','time'], skiprows = 1)
M.set_species({'X':df['X'][0]})

prior = {'d1' : ['uniform', 0, 10], 'k1' : ['uniform', 0, 100]}

sampler, pid = py_inference(Model = M, exp_data = df, measurements = ['X'], time_column = ['time'],
            nwalkers = 20, init_seed = 0.15, nsteps = 5500, sim_type = 'deterministic',
            params_to_estimate = ['d1', 'k1'], prior = prior)<jupyter_output>100%|█████████████████████████████████████████████████████████████████████████████| 5500/5500 [00:23<00:00, 238.18it/s]<jupyter_text>Check mcmc_results.csv for the results of the MCMC procedure and perform your own analysis. You can also plot the results as follows<jupyter_code>from bioscrape.simulator import py_simulate_model

M_fit = Model(sbml_filename = 'toy_sbml_model.xml')
M_fit.set_species({'X':df['X'][0]})
timepoints = pid.timepoints
flat_samples = sampler.get_chain(discard=200, thin=15, flat=True)
inds = np.random.randint(len(flat_samples), size=200)
for ind in inds:
    sample = flat_samples[ind]
    for pi, pi_val in zip(pid.params_to_estimate, sample):
        M_fit.set_parameter(pi, pi_val)
    plt.plot(timepoints, py_simulate_model(timepoints, Model= M_fit)['X'], "C1", alpha=0.1)
# plt.errorbar(, y, yerr=yerr, fmt=".k", capsize=0)
# plt.plot(timepoints, list(pid.exp_data['X']), label = 'data')
plt.plot(timepoints, py_simulate_model(timepoints, Model = M)['X'], "k", label="original model")
plt.legend(fontsize=14)
plt.xlabel("Time")
plt.ylabel("[X]");
flat_samples = sampler.get_chain(discard = 200, thin = 15,flat = True)
flat_samples<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16925133689839572, 'fraction_numerical': 0.03208556149732621, 'mean_word_length': 5.76491862567812, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '62700', 'license': 'MIT', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1308, 'n_words': 350, 'path': 'inference examples/Gaussian prior example.ipynb', 'repo_name': 'zoltuz/bioscrape', 'size': '3740'}