Beispiel #1
0
 def good(e):
     r.set_rhs(e)
     self.assertTrue(hh.has_alpha_beta_form(r, v))
Beispiel #2
0
    def test_alpha_beta_form(self):
        # Test methods for working with alpha-beta 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 without v (detected from label)
        self.assertTrue(hh.has_alpha_beta_form(r))
        alph, beta = hh.get_alpha_and_beta(r)
        self.assertEqual(alph, m.get('ikr.r.alpha'))
        self.assertEqual(beta, m.get('ikr.r.beta'))
        self.assertFalse(hh.has_alpha_beta_form(a))
        self.assertIsNone(hh.get_alpha_and_beta(a))

        # Test with v as a state, without a label
        v.set_label(None)
        self.assertRaisesRegex(ValueError, 'Membrane potential must be given',
                               hh.has_alpha_beta_form, r)
        self.assertTrue(hh.has_alpha_beta_form(r, v))
        alph, beta = hh.get_alpha_and_beta(r, v)
        self.assertEqual(alph, m.get('ikr.r.alpha'))
        self.assertEqual(beta, m.get('ikr.r.beta'))
        self.assertFalse(hh.has_alpha_beta_form(a, v))
        self.assertIsNone(hh.get_alpha_and_beta(a, v))

        # Test with v as a constant
        v.demote()
        v.set_rhs(-80)
        self.assertTrue(hh.has_alpha_beta_form(r, v))
        alph, beta = hh.get_alpha_and_beta(r, v)
        self.assertEqual(alph, m.get('ikr.r.alpha'))
        self.assertEqual(beta, m.get('ikr.r.beta'))
        self.assertFalse(hh.has_alpha_beta_form(a, v))
        self.assertIsNone(hh.get_alpha_and_beta(a, v))

        # Almost correct forms / different ways to fail
        def bad(e):
            r.set_rhs(e)
            self.assertFalse(hh.has_alpha_beta_form(r, v))
            r.set_rhs('alpha * (1 - r) - beta * r')
            self.assertTrue(hh.has_alpha_beta_form(r, v))

        def good(e):
            r.set_rhs(e)
            self.assertTrue(hh.has_alpha_beta_form(r, v))

        # r is not a state
        self.assertTrue(hh.has_alpha_beta_form(r, v))
        r.demote()
        self.assertFalse(hh.has_alpha_beta_form(r, v))
        r.promote(0)
        self.assertTrue(hh.has_alpha_beta_form(r, v))

        # Not a minus
        bad('alpha * (1 - r) + beta * r')

        # Minus, but terms aren't multiplies
        bad('alpha / (1 - r) - beta * r')
        bad('alpha * (1 - r) - beta / r')

        # Terms in multiplications can be switched
        good('(1 - r) * alpha - beta * r')
        good('(1 - r) * alpha - r * beta')
        good('alpha * (1 - r) - beta * r')

        # But the correct terms are required
        bad('alpha * (2 - r) - beta * r')
        bad('alpha^2 * (1 - r) - beta * r')
        bad('alpha * (1 - r) - beta * r^2')
        bad('alpha * (1 - r) - beta^2 * r')

        # Alpha and beta can't be states
        m2 = m.clone()
        m2.get('membrane.V').set_label('membrane_potential')
        m2.get('ikr.r').move_variable(m2.get('ikr.r.alpha'), m2.get('ikr'),
                                      'ralpha')
        self.assertTrue(hh.has_alpha_beta_form(m2.get('ikr.r')))
        m2.get('ikr.ralpha').promote(1)
        self.assertFalse(hh.has_alpha_beta_form(m2.get('ikr.r')))
        m2.get('ikr.ralpha').demote()
        m2.get('ikr.r').move_variable(m2.get('ikr.r.beta'), m2.get('ikr'),
                                      'rbeta')
        self.assertTrue(hh.has_alpha_beta_form(m2.get('ikr.r')))
        m2.get('ikr.rbeta').promote(1)

        # Alpha and beta 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)
        ralph = m.get('ikr.r.alpha').rhs()
        self.assertTrue(hh.has_alpha_beta_form(r, v))
        m.get('ikr.r.alpha').set_rhs('3 * ccc.vvv + V')
        self.assertFalse(hh.has_alpha_beta_form(r, v))
        m.get('ikr.r.alpha').set_rhs(ralph)
        self.assertTrue(hh.has_alpha_beta_form(r, v))
        ralph = m.get('ikr.r.beta').rhs()
        m.get('ikr.r.beta').set_rhs('2 + ccc.vvv - V')
        self.assertFalse(hh.has_alpha_beta_form(r, v))
        m.get('ikr.r.beta').set_rhs(ralph)
        self.assertTrue(hh.has_alpha_beta_form(r, v))
Beispiel #3
0
 def bad(e):
     r.set_rhs(e)
     self.assertFalse(hh.has_alpha_beta_form(r, v))
     r.set_rhs('alpha * (1 - r) - beta * r')
     self.assertTrue(hh.has_alpha_beta_form(r, v))