def test_against_cvode_v_independent(self): # Test with v-independent states model = myokit.parse_model(MODEL) model.get('membrane.V').set_rhs(-80) model.get('membrane.V').demote() model.binding('pace').set_binding(None) model.get('membrane.V').set_binding('pace') # Create a protocol vs = [-30, -20, -10] p = myokit.pacing.steptrain(vsteps=vs, vhold=-120, tpre=8, tstep=2, tpost=0) t = p.characteristic_time() # Run an analytical simulation dt = 0.01 m = hh.HHModel.from_component(model.get('binding')) s1 = hh.AnalyticalSimulation(m, p) d1 = s1.run(t, log_interval=dt).npview() s2 = myokit.Simulation(model, p) s2.set_tolerance(1e-8, 1e-8) d2 = s2.run(t, log_interval=dt).npview() # Test protocol output is the same e = np.abs(d1['membrane.V'] - d2['membrane.V']) self.assertEqual(np.max(e), 0) # Test current output is very similar e = np.abs(d1['binding.I'] - d2['binding.I']) self.assertLess(np.max(e), 2e-4)
def test_against_cvode(self): # Validate against a cvode sim. # Get a model fname = os.path.join(DIR_DATA, 'lr-1991-fitting.mmt') model = myokit.load_model(fname) model.get('membrane.V').set_binding('pace') # Create a protocol vs = [-30, -20, -10] p = myokit.pacing.steptrain(vsteps=vs, vhold=-120, tpre=8, tstep=2, tpost=0) t = p.characteristic_time() # Run an analytical simulation dt = 0.01 m = hh.HHModel.from_component(model.get('ina')) s1 = hh.AnalyticalSimulation(m, p) d1 = s1.run(t, log_interval=dt).npview() s2 = myokit.Simulation(model, p) s2.set_tolerance(1e-8, 1e-8) d2 = s2.run(t, log_interval=dt).npview() # Test protocol output is the same e = np.abs(d1['membrane.V'] - d2['membrane.V']) self.assertEqual(np.max(e), 0) # Test current output is very similar e = np.abs(d1['ina.INa'] - d2['ina.INa']) self.assertLess(np.max(e), 2e-4)
def test_analytical_simulation_properties(self): # Test basic get/set methods of analytical simulation. # Create a simulation fname = os.path.join(DIR_DATA, 'lr-1991-fitting.mmt') model = myokit.load_model(fname) m = hh.HHModel.from_component(model.get('ina')) s = hh.AnalyticalSimulation(m) # membrane potential self.assertEqual(s.membrane_potential(), model.get('membrane.V').eval()) s.set_membrane_potential(10) self.assertEqual(s.membrane_potential(), 10) # Parameter values p = list(range(len(s.parameters()))) self.assertNotEqual(p, s.parameters()) s.set_parameters(p) self.assertEqual(p, s.parameters()) self.assertRaises(ValueError, s.set_parameters, p[:-1]) # Change parameter with set_constant p[3] += 1 self.assertNotEqual(p, s.parameters()) s.set_constant(m.parameters()[3], p[3]) self.assertEqual(p, s.parameters()) # State state = np.zeros(len(s.state())) state[0] = 0.3 state[1] = 0.5 self.assertNotEqual(list(state), list(s.state())) s.set_state(state) self.assertEqual(list(state), list(s.state())) self.assertRaisesRegex(ValueError, 'Wrong size', s.set_state, state[:-1]) state[0] = -.1 self.assertRaisesRegex(ValueError, 'must be in the range', s.set_state, state) state[0] = 0 state[1] = 1.1 self.assertRaisesRegex(ValueError, 'must be in the range', s.set_state, state) # Default state dstate = np.zeros(len(s.default_state())) dstate[0] = 0.3 dstate[1] = 0.5 self.assertNotEqual(list(dstate), list(s.default_state())) s.set_default_state(dstate) self.assertEqual(list(dstate), list(s.default_state())) self.assertRaisesRegex(ValueError, 'Wrong size', s.set_default_state, dstate[:-1]) dstate[0] = -.1 self.assertRaisesRegex(ValueError, 'must be in the range', s.set_default_state, dstate) state[0] = 0 state[1] = 1.1 self.assertRaisesRegex(ValueError, 'must be in the range', s.set_default_state, dstate)
def test_create_and_run(self): # Test creating and running a simulation # Create a simulation fname = os.path.join(DIR_DATA, 'lr-1991-fitting.mmt') model = myokit.load_model(fname) m = hh.HHModel.from_component(model.get('ina')) # Bad constructors self.assertRaisesRegex(ValueError, 'HHModel', hh.AnalyticalSimulation, 1) self.assertRaisesRegex(ValueError, 'Protocol', hh.AnalyticalSimulation, m, 1) # Create properly s = hh.AnalyticalSimulation(m) # Times to evaluate at times = np.linspace(0, 100, 5) # Voltages to test at voltages = np.arange(-70, 0, 30) # Generate traces with "solve" method state = s.state() dstate = s.default_state() for v in voltages: s.set_membrane_potential(v) x, i = s.solve(times) # Solve shouldn't change the state self.assertEqual(state, s.state()) self.assertEqual(dstate, s.default_state()) # Run for a bit self.assertIsInstance(s.run(10), myokit.DataLog) # Run can append to log d = s.run(10) n = len(d['engine.time']) e = s.run(1, log=d) self.assertIs(d, e) self.assertTrue(len(d['engine.time']) > n) # Run for zero duration d = s.run(0) self.assertEqual(len(d.time()), 0) d = s.run(0) self.assertEqual(len(d.time()), 0) # No current variable? Then current can't be calculated model2 = model.clone() model2.get('ina').remove_variable(model2.get('ina.INa')) m2 = hh.HHModel.from_component(model2.get('ina')) s2 = hh.AnalyticalSimulation(m2) # But simulation still works self.assertIsInstance(s2.run(10), myokit.DataLog) del (model2, m2, s2) # Create protocol # Protocol times: prep, step, post, full tprep = 2800 tstep = 15 tpost = 0 # Step voltages vhold = -80 vlo = -140 vhi = 100 res = 50 v = np.arange(vlo, vhi + res, res) p = myokit.pacing.steptrain(v, vhold, tprep, tstep, tpost) t = p.characteristic_time() # Create simulation with protocol (set_protocol is not supported) s = hh.AnalyticalSimulation(m, p) # Membrane potential and protocol can't be used simultaneously self.assertRaisesRegex(Exception, 'cannot be set if', s.set_membrane_potential, -80) # Pre should change the state and default state state = s.state() dstate = s.default_state() s.pre(tprep + tstep) self.assertNotEqual(state, s.state()) self.assertNotEqual(dstate, s.default_state()) self.assertRaises(ValueError, s.pre, -1) # Run should change the state, not the default state state = s.state() dstate = s.default_state() d = s.run(t) self.assertNotEqual(state, s.state()) self.assertEqual(dstate, s.default_state()) self.assertRaisesRegex(ValueError, 'Duration', s.run, -1) self.assertRaisesRegex(ValueError, 'Log interval', s.run, 1, log_interval=-1) d['hello'] = [1, 2, 3] self.assertRaisesRegex(ValueError, 'extra keys', s.run, 1, log=d) del (d['hello']) del (d[next(iter(d.keys()))]) self.assertRaisesRegex(ValueError, 'missing', s.run, 1, log=d) # Reset should reset the state s.reset() self.assertEqual(state, s.state()) # Run can append to log d = s.run(10) n = len(d['engine.time']) e = s.run(1, log=d) self.assertIs(d, e) self.assertTrue(len(d['engine.time']) > n) # Run for zero duration d = s.run(0) self.assertEqual(len(d.time()), 0) d = s.run(0) self.assertEqual(len(d.time()), 0)