<jupyter_start><jupyter_text>Multi-scale model: Tumor spheroid growth======================<jupyter_code>Download this notebook here: :download:`Multi-scale model: Tumor spheroid growth <multiscale_agent_based.ipynb>`.<jupyter_output><empty_output><jupyter_text>Installation prerequisites------------------------------ To run this example, install the tumor2d package available here:[Agent-based 2D Tumor growth model](https://github.com/ICB-DCM/tumor2d).For details on the model, see<NAME>, <NAME> (2017).(Note that the tumor2d package is not part of the paper by Jagiella, Rickert, <NAME> 2017, but was specifically developed and adapted for pyABC, however based on the original source code). Model description-------------------- Briefly, the model is a multi-scale model describing the growth of a 2D tumor spheroid (see animations below). It is a hybrid discrete-continuum model which exploits an agent-based description for individual cells and a PDE-based description for the extracellular matrix. The intra-cellular mechanism of cell-division is described by continuous-time Markov chains and decision rules. In particular the initial phase of the growth is substantially stochastic. During the later phases, with higher cell numbers, averaging effects occur. The proliferating cells are depicted in orange![Proliferation](t2danimation/dividing_bg_transparent.gif) Extracellular matrix intensity is coded in the intensity of the green color![Extracellular matrix](t2danimation/ecm_bg_transparent.gif) To simulate a growing spheroid, we employ the simulate function provided by the tumor2d package.<jupyter_code>from time import time
from tumor2d import simulate

start_time = time()
observation = simulate(division_rate=4.17e-2,
                       initial_spheroid_radius=1.2e1,
                       initial_quiescent_cell_fraction=7.5e-1,
                       division_depth=100,
                       ecm_production_rate=5e-3,
                       ecm_degradation_rate=8e-4,
                       ecm_division_threshold=1e-2)
print(f"Simulation took {time() - start_time:.2f}s")<jupyter_output>Simulation took 49.06s<jupyter_text>The simulation took a little time.The simulate function does not return each individual cell.Instead, we "measure" the radial density of proliferating cells, the radial extracellular matrix density and the spheroid radius over time.These statistics summarize the underlying data.<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
from string import capwords

fig, axes = plt.subplots(ncols=3)
fig.set_size_inches((16, 5))

color = {"growth_curve":
             "k",
         "extra_cellular_matrix_profile":
             "green",
         "proliferation_profile":
             "orange"}

x_label = {"growth_curve":
               "Time (d)",
           "extra_cellular_matrix_profile":
               "Distance to rim (μm)",
           "proliferation_profile":
               "Distance to rim (μm)"}

y_label = {"growth_curve":
               "Radius (μm)",
           "extra_cellular_matrix_profile":
               "Extracellular matrix intensity",
           "proliferation_profile":
               "Fraction proliferating cells"}

for ax, (key, val) in zip(axes, observation.items()):
    ax.plot(val, color=color[key])
    ax.set_title(capwords(key.replace("_", " ")))
    ax.set_xlabel(x_label[key])
    ax.set_ylabel(y_label[key])
    if key.endswith("profile"):
        ax.set_xlim(0, 600)<jupyter_output><empty_output><jupyter_text>Exemplary pyABC inference run with the model--------------------------------------------------- To perform parameter inference with pyABC, we first define a prior over the seven parameters.We do this on the log-domain.<jupyter_code>from pyabc import Distribution, RV

limits = dict(log_division_rate=(-3, -1),
              log_division_depth=(1, 3),
              log_initial_spheroid_radius=(0, 1.2),
              log_initial_quiescent_cell_fraction=(-5, 0),
              log_ecm_production_rate=(-5, 0),
              log_ecm_degradation_rate=(-5, 0),
              log_ecm_division_threshold=(-5, 0))

prior = Distribution(**{key: RV("uniform", a, b - a)
                        for key, (a,b) in limits.items()})<jupyter_output><empty_output><jupyter_text>Note that we define the ``limits`` dictionary for later visualization usages and then initialize the prior indirectly to not to repeat ourselves.Each spheroid simulation runs already in its own subprocess, which is forked off from the main process by the simulate function.We therefore do not need to parallelize using multiprocessing, but will instead use multithreading.pyABC ships with a flexible sampler which accepts any backend implementing the ``concurrent.futures.Executor`` interface.For instance, we can use the ``ThreadPoolExecutor``.For distributed execution the IPython parallel client would be an alternative, but the ``ThreadPoolExecutor`` is good enough for illustrative purposes.Since we've defined the priors on the log domain, we also use a model accepting log-transformed parameters. This model is defined as ``tumor2d.log_model``.It is a thin wrapper around the ``tumor2d.simulate`` function and transforms the parameters accordingly before executing the simulate function.The tumor2d package also provides a distance function and default data.These data were obtained from 100 samples from the so called "reference parameters", which we also used above to simulate one growing spheroid.We takte the mean from these 100 simulations as the observed data.The distance function calculates $L_2$ norms for each of the three summary statistics and adds them up.(These $L_2$ norms are normalized by the pointwise variances.)<jupyter_code>from tumor2d import log_model, distance, load_default
from pyabc import ABCSMC
from pyabc.sampler import ConcurrentFutureSampler
from concurrent.futures import ThreadPoolExecutor

data_mean = load_default()[1]  # (raw, mean, var)

pool = ThreadPoolExecutor(max_workers=2)
sampler = ConcurrentFutureSampler(pool)

abc = ABCSMC(log_model, prior, distance,
             population_size=3,
             sampler=sampler)<jupyter_output><empty_output><jupyter_text>As usually, we initialize a new ABC inference run with the ``new`` method.<jupyter_code>abc.new("sqlite:////tmp/test.db", data_mean)<jupyter_output>INFO:Epsilon:initial epsilon is 22450777.905396923
INFO:History:Start <ABCSMC(id=6, start_time=2018-04-11 08:43:27.549224, end_time=None)><jupyter_text>Since the inference would run very long on a single local machine, we sample only a single population for illustration purposes.Note, that we've also set the number of particles to a very small number.This is much too low for a real inference run, but serves here for illustration purposes and saves sampling time.<jupyter_code>history = abc.run(max_nr_populations=1, minimum_epsilon=0)<jupyter_output>INFO:ABC:t:0 eps:22450777.905396923
INFO:History:Done <ABCSMC(id=6, start_time=2018-04-11 08:43:27.549224, end_time=2018-04-11 08:46:15.690193)><jupyter_text>We visualize the first populatoin with pyABC's ``plot_kde_matrix`` function.<jupyter_code>from pyabc.visualization import plot_kde_matrix

df, w = history.get_distribution(m=0)
plot_kde_matrix(df, w, limits=limits);<jupyter_output><empty_output><jupyter_text>We see kernel density estimates on the lower diagonal and scatter plots of the weighted particles on the upper diagonal.Note that the weight of the particles is not encoded in the scatter plots.On the diagonal we see the marginal distributions. Results of a stored, distributedly executed pyABC inference run------------------------------------------------------------------------- We've run the inference described above in a distributed fashion on one of our clusters on about 800 cores simultaneously.The stored data is provided as part of the tumor2d package.(Note, that the summary statistics were actually also stored in the database. However, to not to distribute a 200MB database, we deleted them afterwards. So they are not contained in the databases distributed with the tumor2d package. If you're interested in the full database, don't hesitate to contact us)We load the stored database and visualize generation 5.<jupyter_code>from pyabc import History
from tumor2d import stored_data_db

h_loaded = History("sqlite:///" + stored_data_db)

df, w = h_loaded.get_distribution(m=0, t=5)
plot_kde_matrix(df, w, limits=limits);<jupyter_output><empty_output><jupyter_text>The posterior is still quite broad. At the last generation the posterior is notably sharper:<jupyter_code>df, w = h_loaded.get_distribution(m=0)
plot_kde_matrix(df, w, limits=limits);<jupyter_output><empty_output><jupyter_text>Adaptive population sizes----------------------------- For this run, we employed one of pyABC's unique features: Adaptive population sizes.We initialized with population size 500. pyABC tuned the necessary population size automatically.<jupyter_code>populations = h_loaded.get_all_populations()
(populations[populations.t >= 0]
 .plot("t", "particles", marker="o"));<jupyter_output><empty_output><jupyter_text>In this example, the population size was roughly constant, however, this is not always the case.The full executio of this notebook takes a little due to the tumor growth simulations performed:<jupyter_code>print(f"Execution time: {(time() - start_time)/60:.1f}m")<jupyter_output>Execution time: 8.8m
{'fraction_non_alphanumeric': 0.0997344662772172, 'fraction_numerical': 0.02134891131173659, 'mean_word_length': 4.219512195121951, 'pattern_counts': {'":': 9, '<': 51, '<?xml version=': 0, '>': 52, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '361730', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2788, 'n_tokens_neox': 2532, 'n_words': 926, 'path': 'doc/examples/multiscale_agent_based.ipynb', 'repo_name': 'Pat-Laub/pyABC', 'size': '9446'}