In [1]:
import os, tempfile
import logging
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
In [2]:
from ionchannelABC import theoretical_population_size
from ionchannelABC import IonChannelDistance, EfficientMultivariateNormalTransition, IonChannelAcceptor
from ionchannelABC.experiment import setup
from ionchannelABC.visualization import plot_sim_results, plot_kde_matrix_custom
import myokit
In [3]:
from pyabc import Distribution, RV, History, ABCSMC
from pyabc.epsilon import MedianEpsilon
from pyabc.sampler import MulticoreEvalParallelSampler, SingleCoreSampler
from pyabc.populationstrategy import ConstantPopulationSize
Load experiments used for unified dataset calibration:
In [4]:
from experiments.ina_sakakibara import (sakakibara_act,
sakakibara_inact,
sakakibara_inact_kin,
sakakibara_rec)
from experiments.ina_schneider import schneider_taum
Load the myokit modelfile for this channel.
In [5]:
modelfile = 'models/nygren_ina.mmt'
Combine model and experiments to produce:
In [6]:
observations, model, summary_statistics = setup(modelfile,
sakakibara_act,
schneider_taum,
sakakibara_inact,
sakakibara_inact_kin,
sakakibara_rec)
assert len(observations)==len(summary_statistics(model({})))
In [7]:
g = plot_sim_results(modelfile,
sakakibara_act,
schneider_taum,
sakakibara_inact,
sakakibara_inact_kin,
sakakibara_rec)
In [8]:
limits = {'ina.s1': (0, 1),
'ina.r1': (0, 100),
'ina.r2': (0, 20),
'ina.q1': (0, 200),
'ina.q2': (0, 20),
'log_ina.r3': (-6, -3),
'ina.r4': (0, 100),
'ina.r5': (0, 20),
'log_ina.r6': (-6, -3),
'log_ina.q3': (-3., 0.),
'ina.q4': (0, 100),
'ina.q5': (0, 20),
'log_ina.q6': (-5, -2),
'log_ina.q7': (-3., 0.),
'log_ina.q8': (-4, -1)}
prior = Distribution(**{key: RV("uniform", a, b - a)
for key, (a,b) in limits.items()})
In [9]:
# Test this works correctly with set-up functions
assert len(observations) == len(summary_statistics(model(prior.rvs())))
Set-up path to results database.
In [10]:
db_path = ("sqlite:///" + os.path.join(tempfile.gettempdir(), "nygren_ina_unified.db"))
In [11]:
# Add logging for additional information during run.
logging.basicConfig()
abc_logger = logging.getLogger('ABC')
abc_logger.setLevel(logging.DEBUG)
eps_logger = logging.getLogger('Epsilon')
eps_logger.setLevel(logging.DEBUG)
Test theoretical number of particles for approximately 2 particles per dimension in the initial sampling of the parameter hyperspace.
In [12]:
pop_size = theoretical_population_size(2, len(limits))
print("Theoretical minimum population size is {} particles".format(pop_size))
Initialise ABCSMC (see pyABC documentation for further details).
IonChannelDistance calculates the weighting applied to each datapoint based on the experimental variance.
In [13]:
abc = ABCSMC(models=model,
parameter_priors=prior,
distance_function=IonChannelDistance(
exp_id=list(observations.exp_id),
variance=list(observations.variance),
delta=0.05),
population_size=ConstantPopulationSize(10000),
summary_statistics=summary_statistics,
transitions=EfficientMultivariateNormalTransition(),
eps=MedianEpsilon(initial_epsilon=20),
sampler=MulticoreEvalParallelSampler(n_procs=16),
acceptor=IonChannelAcceptor())
In [14]:
# Convert observations to dictionary format for calibration
obs = observations.to_dict()['y']
obs = {str(k): v for k, v in obs.items()}
In [15]:
abc_id = abc.new(db_path, obs)
Run calibration with stopping criterion of particle 1\% acceptance rate.
In [ ]:
history = abc.run(minimum_epsilon=0., max_nr_populations=100, min_acceptance_rate=0.01)
In [17]:
history = History(db_path)
In [18]:
history.all_runs()
Out[18]:
In [19]:
df, w = history.get_distribution(m=0)
In [20]:
df.describe()
Out[20]:
In [21]:
sns.set_context('poster')
mpl.rcParams['font.size'] = 14
mpl.rcParams['legend.fontsize'] = 14
g = plot_sim_results(modelfile,
sakakibara_act,
schneider_taum,
sakakibara_inact,
sakakibara_inact_kin,
sakakibara_rec,
df=df, w=w)
plt.tight_layout()
In [22]:
#g.savefig('figures/ina/nyg_unified_sum_stats.pdf')
In [23]:
m,_,_ = myokit.load(modelfile)
In [24]:
originals = {}
for name in limits.keys():
if name.startswith("log"):
name_ = name[4:]
else:
name_ = name
val = m.value(name_)
if name.startswith("log"):
val_ = np.log10(val)
else:
val_ = val
originals[name] = val_
In [25]:
limits.keys()
Out[25]:
In [26]:
sns.set_context('paper')
g = plot_kde_matrix_custom(df, w, limits=limits, refval=originals)
In [ ]: