def test_convert_hh_states_to_inf_tau_form(self): # Tests conversion to inf-tau form m1 = myokit.parse_model(MODEL) self.assertTrue(hh.has_inf_tau_form(m1.get('ikr.a'))) self.assertFalse(hh.has_inf_tau_form(m1.get('ikr.r'))) # Rewrite model m2 = hh.convert_hh_states_to_inf_tau_form(m1) self.assertNotEqual(m1.code(), m2.code()) self.assertTrue(hh.has_inf_tau_form(m1.get('ikr.a'))) self.assertFalse(hh.has_inf_tau_form(m1.get('ikr.r'))) self.assertTrue(hh.has_inf_tau_form(m2.get('ikr.a'))) self.assertTrue(hh.has_inf_tau_form(m2.get('ikr.r'))) # Test only r was affected self.assertEqual(m1.get('ikr.a').code(), m2.get('ikr.a').code()) self.assertNotEqual(m1.get('ikr.r').code(), m2.get('ikr.r').code()) # Test form self.assertEqual(m2.get('ikr.r').rhs().code(), '(inf - ikr.r) / tau') a = m2.get('ikr.r.alpha').eval() b = m2.get('ikr.r.beta').eval() inf = m2.get('ikr.r.inf').eval() tau = m2.get('ikr.r.tau').eval() self.assertAlmostEqual(inf, a / (a + b)) self.assertAlmostEqual(tau, 1 / (a + b)) # Test state values are similar self.assertAlmostEqual(m1.get('ikr.r').eval(), m2.get('ikr.r').eval()) # Second rewrite isn't necessary m3 = hh.convert_hh_states_to_inf_tau_form(m2) self.assertEqual(m2.code(), m3.code()) # First argument must be a myokit model self.assertRaisesRegex(ValueError, 'must be a myokit.Model', hh.convert_hh_states_to_inf_tau_form, []) # Membrane potential given explicitly (not as label) m2 = m1.clone() v = m2.get('membrane.V') v.set_label(None) m3 = hh.convert_hh_states_to_inf_tau_form(m2, v) self.assertNotEqual(m2.code(), m3.code()) # Note: the next methods are called with v from m2, not v from m3! But # this should still work as variables are .get() from the model. self.assertTrue(hh.has_inf_tau_form(m3.get('ikr.a'), v)) self.assertTrue(hh.has_inf_tau_form(m3.get('ikr.r'), v)) # Unknown membrane potential self.assertRaisesRegex(ValueError, 'Membrane potential must be given', hh.convert_hh_states_to_inf_tau_form, m2)
def bad(e): a.set_rhs(e) self.assertFalse(hh.has_inf_tau_form(a, v)) a.set_rhs('(inf - a) / tau') self.assertTrue(hh.has_inf_tau_form(a, v))
def good(e): a.set_rhs(e) self.assertTrue(hh.has_inf_tau_form(a, v))
def test_inf_tau_form(self): # Test methods for working with inf-tau form m = myokit.parse_model(MODEL) self.assertIsInstance(m, myokit.Model) v = m.get('membrane.V') a = m.get('ikr.a') r = m.get('ikr.r') # Test with v detected from label self.assertTrue(hh.has_inf_tau_form(a)) inf, tau = hh.get_inf_and_tau(a) self.assertEqual(inf, m.get('ikr.a.inf')) self.assertEqual(tau, m.get('ikr.a.tau')) self.assertFalse(hh.has_inf_tau_form(r)) self.assertIsNone(hh.get_inf_and_tau(r)) # Test with v argument, no label v.set_label(None) self.assertRaisesRegex(ValueError, 'Membrane potential must be given', hh.has_inf_tau_form, a) self.assertTrue(hh.has_inf_tau_form(a, v)) inf, tau = hh.get_inf_and_tau(a, v) self.assertEqual(inf, m.get('ikr.a.inf')) self.assertEqual(tau, m.get('ikr.a.tau')) self.assertFalse(hh.has_inf_tau_form(r, v)) self.assertIsNone(hh.get_inf_and_tau(r, v)) # Test with v as a constant v.demote() v.set_rhs(-80) self.assertTrue(hh.has_inf_tau_form(a, v)) inf, tau = hh.get_inf_and_tau(a, v) self.assertEqual(inf, m.get('ikr.a.inf')) self.assertEqual(tau, m.get('ikr.a.tau')) self.assertFalse(hh.has_inf_tau_form(r, v)) self.assertIsNone(hh.get_inf_and_tau(r, v)) del (r) # a is not a state self.assertTrue(hh.has_inf_tau_form(a, v)) a.demote() self.assertFalse(hh.has_inf_tau_form(a, v)) a.promote(0) self.assertTrue(hh.has_inf_tau_form(a, v)) # Almost correct forms / different ways to fail def bad(e): a.set_rhs(e) self.assertFalse(hh.has_inf_tau_form(a, v)) a.set_rhs('(inf - a) / tau') self.assertTrue(hh.has_inf_tau_form(a, v)) def good(e): a.set_rhs(e) self.assertTrue(hh.has_inf_tau_form(a, v)) bad('(inf - a) / (1 + tau)') bad('(inf + a) / tau') bad('(inf - 1) / tau') bad('(1 - inf) / tau') bad('(inf - r) / tau') # Inf and tau can't be states m.get('ikr.a').move_variable(m.get('ikr.a.inf'), m.get('ikr'), 'ainf') m.get('ikr.a').move_variable(m.get('ikr.a.tau'), m.get('ikr'), 'atau') self.assertTrue(hh.has_inf_tau_form(a, v)) m.get('ikr.ainf').promote(1) self.assertFalse(hh.has_inf_tau_form(a, v)) m.get('ikr.ainf').demote() self.assertTrue(hh.has_inf_tau_form(a, v)) m.get('ikr.atau').promote(1) self.assertFalse(hh.has_inf_tau_form(a, v)) m.get('ikr.atau').demote() self.assertTrue(hh.has_inf_tau_form(a, v)) # Inf and tau can't depend on other states than v c = m.add_component('ccc') x = c.add_variable('vvv') x.set_rhs(1) x.promote(0) ainf = m.get('ikr.ainf').rhs() m.get('ikr.ainf').set_rhs('2 + ccc.vvv * V') self.assertFalse(hh.has_inf_tau_form(a, v)) m.get('ikr.ainf').set_rhs(ainf) self.assertTrue(hh.has_inf_tau_form(a, v)) atau = m.get('ikr.atau').rhs() m.get('ikr.atau').set_rhs('3 * ccc.vvv * V') self.assertFalse(hh.has_inf_tau_form(a, v)) m.get('ikr.atau').set_rhs(atau) self.assertTrue(hh.has_inf_tau_form(a, v))