Exemplo n.º 1
0
    def test_benchmarker(self):
        # Test the benchmarker.

        b = myokit.Benchmarker()
        x = [0] * 1000
        t0 = b.time()
        self.assertTrue(t0 >= 0)
        x = [0] * 1000
        t1 = b.time()
        self.assertTrue(t1 >= t0)
        x = [0] * 1000
        t2 = b.time()
        self.assertTrue(t2 >= t1)
        for i in range(1000):
            x = [0] * 1000
        t3 = b.time()
        self.assertTrue(t3 >= t2)
        b.reset()
        t4 = b.time()
        self.assertTrue(t4 < t3)

        self.assertEqual(b.format(1), '1 second')
        self.assertEqual(b.format(61), '1 minute, 1 second')
        self.assertEqual(b.format(60), '1 minute, 0 seconds')
        self.assertEqual(b.format(180), '3 minutes, 0 seconds')
        self.assertEqual(b.format(3600), '1 hour, 0 minutes, 0 seconds')
        self.assertEqual(b.format(3661), '1 hour, 1 minute, 1 second')
        self.assertEqual(b.format(3600 * 24),
                         '1 day, 0 hours, 0 minutes, 0 seconds')
        self.assertEqual(b.format(3600 * 24 * 7),
                         '1 week, 0 days, 0 hours, 0 minutes, 0 seconds')
Exemplo n.º 2
0
 def enter(self, msg=None):
     self.b = myokit.Benchmarker()
Exemplo n.º 3
0
 def test_benchmarker(self):
     # Deprecated alias of myokit.tools.Benchmarker
     with WarningCollector() as c:
         b = myokit.Benchmarker()
     self.assertIn('`myokit.Benchmarker` is deprecated', c.text())
     self.assertIsInstance(b, myokit.tools.Benchmarker)
Exemplo n.º 4
0
 def __init__(self, digits=1):
     super(ProgressPrinter, self).__init__()
     self._b = myokit.Benchmarker()
     self._f = None
     self._d = int(digits)
Exemplo n.º 5
0
    def fixed_form_pacing_ansic(self):
        """
        Tests the Ansi-C fixed-form pacing system.
        """
        if False:
            # Graphical test, just for playing with the pacing system
            m, p, x = myokit.load('example')
            s = myokit.Simulation(m, p)
            d = s.run(500).npview()
            # Get time and voltage
            t = d.time()
            v = d['membrane.V']
            # Plot trace
            import matplotlib.pyplot as pl
            pl.figure()
            pl.plot(t, v, 'x')
            # Get some points halfway, 1/4 of the way, and 3/4 of the way
            # between the known points
            t2 = t[:-1] + 0.25 * (t[1:] - t[:-1])
            t3 = t[:-1] + 0.5 * (t[1:] - t[:-1])
            t4 = t[:-1] + 0.75 * (t[1:] - t[:-1])
            t2 = np.concatenate((t2, t3, t4))
            del (t3, t4)
            # Get the pacing value at the points, measure how long it takes
            pacing = myotest.AnsicFixedFormPacing(list(t), list(v))
            b = myokit.Benchmarker()
            v2 = [pacing.pace(x) for x in t2]
            print(b.time())
            # Plot the new points
            pl.plot(t2, v2, '.', color='green')
            pl.show()
            # Quite
            import sys
            sys.exit(1)

        # Test input checking
        times = 1
        values = [1, 2]
        self.assertRaises(Exception, myotest.AnsicFixedFormPacing)
        self.assertRaises(Exception, myotest.AnsicFixedFormPacing, 1)
        self.assertRaises(Exception, myotest.AnsicFixedFormPacing, 1, 2)
        self.assertRaises(Exception, myotest.AnsicFixedFormPacing, [1], [2])
        self.assertRaises(Exception, myotest.AnsicFixedFormPacing, [1, 2], [2])
        self.assertRaises(Exception, myotest.AnsicFixedFormPacing, [2, 1],
                          [2, 2])
        myotest.AnsicFixedFormPacing([1, 2], [1, 2])

        # Test with small lists
        values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
        times = [0, 0, 1, 1, 1, 2, 2, 2, 3, 4, 5, 7]
        values = range(len(times))
        pacing = myotest.AnsicFixedFormPacing(times, values)

        def test(value, index):
            self.assertEquals(pacing.pace(value), index)

        test(-1, 0)
        test(0, 1)
        test(1, 2)
        test(2, 5)
        test(3, 8)
        test(4, 9)
        test(5, 10)
        test(7, 11)
        test(8, 11)
        test(1.5, 4.5)
        test(1.75, 4.75)
        test(6, 10.5)
        test(5.5, 10.25)
def run(source, debug, debugfile):
    """
    Runs an mmt file script.
    """
    import sys
    import myokit

    # Debug?
    myokit.DEBUG = myokit.DEBUG or debug or debugfile

    # Read mmt file
    try:
        print('Reading model from ' + source)
        b = myokit.Benchmarker()
        (model, protocol, script) = myokit.load(source)
        print('File loaded in ' + str(b.time()) + ' seconds')
        if model is None:
            print('No model definition found')
        else:
            print('Model read successfully')
            print(model.format_warnings())
            model.solvable_order()
    except myokit.ParseError as ex:
        print(myokit.format_parse_error(ex, source))
        sys.exit(1)

    # Set up pacing protocol
    if protocol is None:
        print('No protocol definition found')
        print('Preparing default pacing protocol (1ms stimulus, 1bpm)')
        protocol = myokit.pacing.blocktrain(1000, 1)

    # Set up script
    if script is None:
        if model is None:
            print('No script or model found, terminating')
            sys.exit(1)
        else:
            print('No embedded script found, using default.')
            script = myokit.default_script()
    else:
        print('Using embedded script')

    # Run, capture output and write to file
    if debugfile:
        debugfile = debugfile[0]
        with open(debugfile, 'w') as f:
            stdout = sys.stdout
            try:
                sys.stdout = f
                line_numbers = myokit.DEBUG_LINE_NUMBERS
                myokit.DEBUG_LINE_NUMBERS = False
                myokit.run(model, protocol, script)
            except SystemExit:
                pass
            finally:
                sys.stdout = stdout
                myokit.DEBUG_LINE_NUMBERS = line_numbers
            print('Output written to ' + str(debugfile))

    else:

        # Show script
        printline()
        lines = script.splitlines()
        template = '{:>3d} {:s}'
        i = 0
        for line in lines:
            i += 1
            print(template.format(i, line))
        printline()

        # Run!
        myokit.run(model, protocol, script)