def test_version_selection(self):
        # Test choosing between CellML versions

        e = formats.exporter('cellml')
        model = myokit.Model('hello')
        t = model.add_component('env').add_variable('time')
        t.set_binding('time')
        t.set_rhs(0)

        # Write to 1.0 model
        with TemporaryDirectory() as d:
            path = d.path('test.cellml')
            e.model(path, model, version='1.0')
            with open(path, 'r') as f:
                self.assertIn('cellml/1.0#', f.read())

        # Write to 1.1 model
        with TemporaryDirectory() as d:
            path = d.path('test.cellml')
            e.model(path, model, version='1.1')
            with open(path, 'r') as f:
                self.assertIn('cellml/1.1#', f.read())

        # Write to 2.0 model
        with TemporaryDirectory() as d:
            path = d.path('test.cellml')
            e.model(path, model, version='2.0')
            with open(path, 'r') as f:
                self.assertIn('cellml/2.0#', f.read())
Exemple #2
0
 def test(self):
     """
     Test the export functions for this exporter type.
     """
     # Load model, protocol
     m, p, x = myokit.load('example')
     # Get exporter
     e = formats.exporter(self._exporter)
     # Ok? Then update class name for nice error message (!)
     self.__class__.__name__ = 'ExportTest' + e.__class__.__name__
     # Create empty output directory as subdirectory of DIR_OUT
     path = os.path.join(myotest.DIR_OUT, self._exporter)
     if os.path.exists(path):
         shutil.rmtree(path)
     os.makedirs(path)
     try:
         # Try exports
         exports = 0
         # Try model export
         if e.supports_model():
             exports += 1
             fpath = os.path.join(path, 'model.txt')
             ret = e.model(fpath, m)
             self.assertIsNone(ret)
             self.assertTrue(os.path.isfile(fpath))
         else:
             self.assertRaises(NotImplementedError, e.model, path, m)
         # Try runnable export
         if e.supports_runnable():
             exports += 1
             # Check without protocol
             dpath = os.path.join(path, 'runnable1')
             ret = e.runnable(dpath, m)
             self.assertIsNone(ret)
             self.assertTrue(os.path.isdir(dpath))
             self.assertTrue(len(os.listdir(dpath)) > 0)
             # Check with protocol
             dpath = os.path.join(path, 'runnable2')
             ret = e.runnable(dpath, m, p)
             self.assertIsNone(ret)
             self.assertTrue(os.path.isdir(dpath))
             self.assertTrue(len(os.listdir(dpath)) > 0)
         else:
             self.assertRaises(NotImplementedError, e.runnable, path, m, p)
         # Test if any exports were available
         if exports == 0:
             raise Exception(
                 'No types of export supported by: ' + self._exporter)
     finally:
         if os.path.exists(path) and os.path.isdir(path):
             if not myotest.DEBUG:
                 shutil.rmtree(path)
    def test_version_specific_exporters(self):
        # Test the aliased exporters for specific versions

        model = myokit.Model('hello')
        t = model.add_component('env').add_variable('time')
        t.set_binding('time')
        t.set_rhs(0)

        # Write to 1.0 model
        e = formats.exporter('cellml1')
        with TemporaryDirectory() as d:
            path = d.path('test.cellml')
            e.model(path, model)
            with open(path, 'r') as f:
                self.assertIn('cellml/1.0#', f.read())

        # Write to 2.0 model
        e = formats.exporter('cellml2')
        with TemporaryDirectory() as d:
            path = d.path('test.cellml')
            e.model(path, model)
            with open(path, 'r') as f:
                self.assertIn('cellml/2.0#', f.read())
    def test_stimulus_generation(self):
        # Tests if protocols allow a stimulus current to be added

        e = formats.exporter('cellml')
        i = formats.importer('cellml')

        # Load input model
        m1, p1, _ = myokit.load('example')
        org_code = m1.code()

        # 1. Export without a protocol
        with TemporaryDirectory() as d:
            path = d.path('model.cellml')
            with WarningCollector() as w:
                e.model(path, m1)
            m2 = i.model(path)
        self.assertFalse(w.has_warnings())
        self.assertTrue(isinstance(m2.get('engine.pace').rhs(), myokit.Number))

        # 2. Export with protocol, but without variable bound to pacing
        m1.get('engine.pace').set_binding(None)
        with TemporaryDirectory() as d:
            path = d.path('model.cellml')
            with WarningCollector() as w:
                e.model(path, m1, p1)
            m2 = i.model(path)
        self.assertTrue(w.has_warnings())
        self.assertTrue(isinstance(m2.get('engine.pace').rhs(), myokit.Number))

        # 3. Export with protocol and variable bound to pacing
        m1.get('engine.pace').set_binding('pace')
        with TemporaryDirectory() as d:
            path = d.path('model.cellml')
            with WarningCollector() as w:
                e.model(path, m1, p1)
            m2 = i.model(path)
        self.assertFalse(w.has_warnings())
        rhs = m2.get('membrane.i_stim').rhs()
        self.assertTrue(rhs, myokit.Multiply)
        self.assertTrue(isinstance(rhs[0], myokit.Piecewise))

        # Check original model is unchanged
        self.assertEqual(org_code, m1.code())
 def test_capability_reporting(self):
     # Test if the right capabilities are reported.
     e = formats.exporter('cellml')
     self.assertTrue(e.supports_model())
     self.assertFalse(e.supports_runnable())