def test_updating(self): m = equilibrium_model() con1 = InitialValueExperiment(equilibrium_dose_variant()) con2 = InitialValueExperiment(Model().add('A0 = 15', 'kf = 0.3')) active_parameters = ActiveParameters.from_model_experiments( m, [con1, con2], ['kr'], [[], ['A0', 'kf']]) updated_parameters = active_parameters.update_parameters( [4.0, 1.3, 2.0]) self.assertEqual( updated_parameters, ActiveParameters(OrderedDict([ ('kr', 4.0) ]), [OrderedDict(), OrderedDict([('A0', 1.3), ('kf', 2.0)])])) active_parameters = ActiveParameters.from_model_experiments( m, [con2, con2], ['kr'], [['kf'], ['A0', 'kf']]) updated_parameters = active_parameters.update_parameters( [4.0, 3.4, 1.3, 2.0]) self.assertEqual( updated_parameters, ActiveParameters(OrderedDict([('kr', 4.0)]), [ OrderedDict([('kf', 3.4)]), OrderedDict([('A0', 1.3), ('kf', 2.0)]) ]))
def test_extraction(self): m = equilibrium_model() con1 = InitialValueExperiment(equilibrium_dose_variant()) con2 = InitialValueExperiment(Model().add('A0 = 15', 'kf = 0.3')) active_parameters = ActiveParameters.from_model_experiments( m, [con1], ['kf', 'kr'], [[]]) self.assertEqual( active_parameters, ActiveParameters(OrderedDict([('kf', 0.5), ('kr', 0.2)]), [OrderedDict()])) active_parameters = ActiveParameters.from_model_experiments( m, [con1, con2], ['kf', 'kr'], []) self.assertEqual( active_parameters, ActiveParameters(OrderedDict([('kf', 0.5), ('kr', 0.2)]), [OrderedDict(), OrderedDict()])) active_parameters = ActiveParameters.from_model_experiments( m, [con1, con2], ['kr', 'kf'], []) self.assertEqual( active_parameters, ActiveParameters(OrderedDict([('kr', 0.2), ('kf', 0.5)]), [OrderedDict(), OrderedDict()])) active_parameters = ActiveParameters.from_model_experiments( m, [con1, con2], ['kr', 'kf'], []) self.assertEqual( active_parameters, ActiveParameters(OrderedDict([('kr', 0.2), ('kf', 0.5)]), [OrderedDict(), OrderedDict()])) active_parameters = ActiveParameters.from_model_experiments( m, [con1, con2], [], [[], ['A0']]) self.assertEqual( active_parameters, ActiveParameters( OrderedDict(), [OrderedDict(), OrderedDict([('A0', 15)])])) active_parameters = ActiveParameters.from_model_experiments( m, [con1, con2], [], [[], ['A0', 'kf']]) self.assertEqual( active_parameters, ActiveParameters( OrderedDict(), [OrderedDict(), OrderedDict([('A0', 15), ('kf', 0.3)])]))
def test_update_events(self): m = equilibrium_model() con = InitialValueExperiment(Model().add('@(B < 2) B = 0')) sim = simulate(m, con) self.assertTrue(sim.matrix_values(0.7, 'B') < 1)
def test_empty(self): m = Model() con = InitialValueExperiment() sim = simulate(m, con) self.assertEqual(sim.matrix_values(10).shape, (0, )) self.assertEqual(sim.matrix_values([10, 20]).shape, (2, 0))
def test_simulate_system2(self): m = equilibrium_model() con = InitialValueExperiment(equilibrium_dose_variant()) sim = simulate(m, con) self.assertEqual(sim.matrix_values(0, 'kf'), 0.5) self.assertEqual(sim.matrix_values(0, 'A'), 10) self.assertLess(sim.matrix_values(1, 'A'), 10)
def test_basic_fitting(self): m = Model().add('k = 10') con = InitialValueExperiment() obj = LinearWeightedSumOfSquaresObservation( [0], ['k'], AffineMeasurementUncertainty(1, 0)).objective([20]) fitted_parameters = fit_parameters(m, [con], [obj], model_parameters='k') self.assertAlmostEqual(fitted_parameters.model_parameters['k'], 20)
def test_events(self): m = Model() m = m.add("A* = 0", "A' = 1", "@(A > 1) A = 0") con = InitialValueExperiment() sim = simulate(m, con) self.assertTrue(0.9 < sim.matrix_values(0.95, 'A') < 1) self.assertTrue(0 < sim.matrix_values(1.05, 'A') < 0.1) self.assertTrue(0.9 < sim.matrix_values(1.95, 'A') < 1) self.assertTrue(0 < sim.matrix_values(2.05, 'A') < 0.1)
def test_log_probability_dk(self): m = equilibrium_model() con = InitialValueExperiment() unc = AffineMeasurementUncertainty(2, 0) obs = LinearWeightedSumOfSquaresObservation([0], ['A'], unc) assert_log_probability_dk(m, con, obs, m['A0'].value, atol=1e-6) assert_log_probability_dk(m, con, obs, m['A0'].value + 2) obs = LinearWeightedSumOfSquaresObservation([0, 0, 0], ['A', 'B', 'C'], unc) assert_log_probability_dk( m, con, obs, [m['A0'].value + 0.1, m['B0'].value + 0.2, 0.3])
def test_simulate_dose(self): m = dose_step() con = InitialValueExperiment() sim = simulate(m, con) self.assertEqual(sim.matrix_values(0, 'A'), 0.0) self.assertEqual(sim.matrix_values(1, 'A'), 2.0) self.assertEqual(sim.matrix_values(1.5, 'A'), 2.0) self.assertEqual(sim.matrix_values(2, 'A'), 3.0) self.assertEqual(sim.matrix_values(0, 'B'), 0.0) self.assertAlmostEqual(sim.matrix_values(0.5, 'B'), 2.0) self.assertAlmostEqual(sim.matrix_values(1, 'B'), 3.0) self.assertAlmostEqual(sim.matrix_values(2, 'B'), 7.0) self.assertAlmostEqual(sim.matrix_values(3, 'B'), 16.0)
def test_simulate_system(self): m = equilibrium_model() con = InitialValueExperiment() sim = simulate(m, con) self.assertEqual(sim.vector_values(0, 'A'), m['A0'].value) assert_allclose(sim.vector_values(0, ['A', 'A0']), [m['A0'].value, m['A0'].value]) self.assertEqual(sim.matrix_values(0, 'kf'), 0.5) self.assertEqual(sim.matrix_values(0, 'A'), 10) self.assertLess(sim.matrix_values(1, 'A'), 10) assert_allclose(sim.matrix_values(0, ['A', 'B']), np.asarray([10, 5])) assert_allclose(sim.matrix_values([0, 1], 'kf'), np.asarray([0.5, 0.5])) assert_allclose(sim.matrix_values([0, 1, 2], ['kr', 'kf']), np.asarray([[0.2, 0.5], [0.2, 0.5], [0.2, 0.5]]))
def test_simulate_sensitivity(self): m = equilibrium_model() con = InitialValueExperiment() sim = simulate(m, con, parameters=['A0', 'kr']) assert_allclose(sim.matrix_sensitivities(0.0, 'A'), [1.0, 0.0]) self.assertEqual(sim.matrix_sensitivities(0.0, 'A').shape, (2, )) assert_allclose(sim.matrix_sensitivities([0.0, 10.0], 'A0'), [[1.0, 0.0], [1.0, 0.0]]) self.assertEqual( sim.matrix_sensitivities([0.0, 10.0], 'A0').shape, (2, 2)) assert_allclose(sim.matrix_sensitivities(0.0, ['A', 'A0']), [[1.0, 0.0], [1.0, 0.0]]) self.assertEqual( sim.matrix_sensitivities(0.0, ['A', 'A0']).shape, (2, 2)) assert_allclose(sim.matrix_sensitivities([0.0, 10.0], ['A0', 'B0']), [[[1.0, 0.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 0.0]]]) self.assertEqual( sim.matrix_sensitivities([0.0, 10.0], ['A', 'A0']).shape, (2, 2, 2))
def test_log_probability(self): m = equilibrium_model() con = InitialValueExperiment() sim = simulate(m, con) unc = AffineMeasurementUncertainty(2, 0) obs = LinearWeightedSumOfSquaresObservation([0], ['A'], unc) self.assertAlmostEqual(obs.probability(sim, m['A0'].value), norm.pdf(m['A0'].value, m['A0'].value, 2)) self.assertAlmostEqual(obs.log_probability(sim, m['A0'].value), norm.logpdf(m['A0'].value, m['A0'].value, 2)) obs = LinearWeightedSumOfSquaresObservation([0], ['A'], unc) self.assertAlmostEqual(obs.probability(sim, m['A0'].value + 2), norm.pdf(m['A0'].value + 2, m['A0'].value, 2)) self.assertAlmostEqual( obs.log_probability(sim, m['A0'].value + 2), norm.logpdf(m['A0'].value + 2, m['A0'].value, 2)) self.assertEqual(obs.sample(sim).size, 1) obs = LinearWeightedSumOfSquaresObservation([0, 0, 0], ['A', 'B', 'C'], unc) ics = [m['A0'].value, m['B0'].value, 0] self.assertAlmostEqual( obs.probability(sim, ics), multivariate_normal.pdf([0, 0, 0], [0, 0, 0], 2**2)) self.assertAlmostEqual( obs.log_probability(sim, ics), multivariate_normal.logpdf([0, 0, 0], [0, 0, 0], 2**2)) self.assertAlmostEqual( obs.log_probability(sim, ics), multivariate_normal.logpdf([0, 0, 0], [0, 0, 0], np.asarray([2, 2, 2])**2)) self.assertEqual(obs.sample(sim).size, 3)
from biolucia.parser import * from biolucia.experiment import InitialValueExperiment from biolucia.simulation import simulate import matplotlib.pyplot as plt import numpy as np filename = '../models/discontinuity.txt' m = read_model(filename) con = InitialValueExperiment() sim = simulate(m, con) times = np.linspace(0, 10, 1000) values = sim.matrix_values(times) lines = plt.plot(times, values) plt.show()