Пример #1
0
def load_model_from_cellml(cellml_filename, mmt_filename):
    """Load a model into Myokit from cellml file format.

    Parameters
    ----------
    cellml_filename : str
        Path to the CellML model
    mmt_filename : str
        Path to location to save the MMT model

    Returns
    -------
    None

    """
    if 'cellml' not in myokit.formats.importers():
        raise Exception('cellml support not detected in your Myokit')

    importer = myokit.formats.importer('cellml')
    model = importer.model(cellml_filename)

    # Try to parse the stimulus protocol from the CellML model
    try:
        model, prot = convert_protocol(model)
        myokit.save_model(mmt_filename, model)

    # If the names of variables do not match, just load the model as is
    except KeyError:
        myokit.save_model(mmt_filename, model)

    sim = load_simulation_from_mmt(mmt_filename)
    return sim
Пример #2
0
 def save_model(self):
     """
     Tests if the correct parts are saved/loaded from disk using the
     ``save_model()`` method.
     """
     ipath = os.path.join(myotest.DIR_DATA, 'lr-1991.mmt')
     opath = os.path.join(myotest.DIR_OUT, 'loadsavetest.mmt')
     if os.path.exists(opath):
         os.remove(opath)
     # Test example loading
     m = myokit.load_model('example')
     self.assertIsInstance(m, myokit.Model)
     # Test file loading
     m = myokit.load_model(ipath)
     self.assertIsInstance(m, myokit.Model)
     if os.path.exists(opath):
         os.remove(opath)
     try:
         myokit.save_model(opath, m)
         # Test no other parts were written
         with open(opath, 'r') as f:
             text = f.read()
         self.assertTrue('[[model]]' in text)
         self.assertFalse('[[protocol]]' in text)
         self.assertFalse('[[script]]' in text)
         # Test reloading
         mm = myokit.load_model(opath)
         self.assertIsInstance(mm, myokit.Model)
         self.assertEqual(mm.code(), m.code())
     finally:
         os.remove(opath)
Пример #3
0
 def test_save_model(self):
     """
     Test if the correct parts are saved/loaded from disk using the
     ``save_model()`` method.
     """
     ipath = os.path.join(DIR_DATA, 'lr-1991.mmt')
     # Test example loading
     m = myokit.load_model('example')
     self.assertIsInstance(m, myokit.Model)
     # Test file loading
     m = myokit.load_model(ipath)
     self.assertIsInstance(m, myokit.Model)
     with TemporaryDirectory() as d:
         opath = d.path('loadsave.mmt')
         myokit.save_model(opath, m)
         # Test no other parts were written
         with open(opath, 'r') as f:
             text = f.read()
         self.assertTrue('[[model]]' in text)
         self.assertFalse('[[protocol]]' in text)
         self.assertFalse('[[script]]' in text)
         # Test reloading
         mm = myokit.load_model(opath)
         self.assertIsInstance(mm, myokit.Model)
         self.assertEqual(mm.code(), m.code())
Пример #4
0
def save_steady_state_to_mmt(s, steady_state: list, list_of_models_names: list,
                             save_location: str):
    """
    Saves the steady states as new mmt models. The structure of the (if
    necessary created) folder is: save_location\\one folder per model\\one
    .mmt model per experimental condition.

    :param s:
    myokit.Simulation or list of myokit.Simulation. Contains the model(s) for
    which the steady-state will be computed.

    :param steady_state:
    list.  List of length the number of models, and each index contains the
    number of different experimental conditions provided with data_exp. Refer
    to steady_state[i][j] for the i-th model steady-state under j-th
    experimental conditions

    :param list_of_models_names:
    list of strings. List of the names of the models. If not specified, the
    models will be named model #.

    :param save_location:
    string. Link to the folder where to save the new model produced. If more
    than one model is provided, sub-directories matching the models names in
    list_of_models_names will be created, and a .mmt file created for each
    steady-state of the model (generated previously with different experimental
    conditions).

    :return: None
    """

    # Check the inputs in the function
    if len(steady_state) != len(list_of_models_names):
        raise ValueError('Steady_state and list_of_models_names should have '
                         'the same length.')

    # Create the folder of save if not existing yet
    if not os.path.exists(save_location):
        os.makedirs(save_location)
    # Write the models into the folder, with one folder per model
    for i in range(len(list_of_models_names)):
        folder = os.path.join(save_location, list_of_models_names[i])
        if not os.path.exists(folder):
            os.makedirs(folder)

        # Create a sub-folder for each experimental condition desired
        for j in range(len(steady_state[0])):
            save_filename = os.path.join(
                folder,
                list_of_models_names[i] + '_exp_cond_' + str(j) + '.mmt')
            s[i].set_default_state(steady_state[i][j])
            model_to_save = s[i]._model
            myokit.save_model(save_filename, model_to_save)

    return None
Пример #5
0
 def test_multiline_string_indent(self):
     """
     Test what happens when you load save a string that gets auto-indented.
     """
     # Create model with multi-line meta-data property
     d1 = 'First line\n\nSecond line'
     m1 = myokit.Model()
     m1.meta['desc'] = d1
     e = m1.add_component('engine')
     v = e.add_variable('time')
     v.set_binding('time')
     v.set_rhs(0)
     # Store to disk
     with TemporaryDirectory() as d:
         opath = d.path('multiline.mmt')
         myokit.save_model(opath, m1)
         # Load and compare the meta-data string
         m2 = myokit.load_model(opath)
         d2 = m2.meta['desc']
         self.assertEqual(d1, d2)
     # Create model with indented multi-line meta-data property
     d1 = '  First line\n\n  Second line'
     dr = 'First line\n\nSecond line'
     m1 = myokit.Model()
     m1.meta['desc'] = d1
     e = m1.add_component('engine')
     v = e.add_variable('time')
     v.set_binding('time')
     v.set_rhs(0)
     # Store to disk
     with TemporaryDirectory() as d:
         opath = d.path('multiline.mmt')
         myokit.save_model(opath, m1)
         # Load and compare the meta-data string
         m2 = myokit.load_model(opath)
         d2 = m2.meta['desc']
         self.assertEqual(d2, dr)
     # Create model with strangely indented multi-line meta-data property
     d1 = '  First line\n\n   Second line'
     dr = 'First line\n\n Second line'
     m1 = myokit.Model()
     m1.meta['desc'] = d1
     e = m1.add_component('engine')
     v = e.add_variable('time')
     v.set_binding('time')
     v.set_rhs(0)
     # Store to disk
     with TemporaryDirectory() as d:
         opath = d.path('multiline.mmt')
         myokit.save_model(opath, m1)
         # Load and compare the meta-data string
         m2 = myokit.load_model(opath)
         d2 = m2.meta['desc']
         self.assertEqual(d2, dr)
Пример #6
0
 def multiline_string_indent(self):
     """
     Tests what happens when you load save a string that gets auto-indented.
     """
     # Create model with multi-line meta-data property
     d1 = 'First line\n\nSecond line'
     m1 = myokit.Model()
     m1.meta['desc'] = d1
     e = m1.add_component('engine')
     v = e.add_variable('time')
     v.set_binding('time')
     v.set_rhs(0)
     # Store to disk
     opath = os.path.join(myotest.DIR_OUT, 'multiline.mmt')
     if os.path.exists(opath):
         os.remove(opath)
     try:
         myokit.save_model(opath, m1)
         # Load and compare the meta-data string
         m2 = myokit.load_model(opath)
         d2 = m2.meta['desc']
         self.assertEqual(d1, d2)
     finally:
         # Tidy up
         if os.path.exists(opath):
             os.remove(opath)
     # Create model with indented multi-line meta-data property
     d1 = '  First line\n\n  Second line'
     dr = 'First line\n\nSecond line'
     m1 = myokit.Model()
     m1.meta['desc'] = d1
     e = m1.add_component('engine')
     v = e.add_variable('time')
     v.set_binding('time')
     v.set_rhs(0)
     # Store to disk
     opath = os.path.join(myotest.DIR_OUT, 'multiline.mmt')
     if os.path.exists(opath):
         os.remove(opath)
     try:
         myokit.save_model(opath, m1)
         # Load and compare the meta-data string
         m2 = myokit.load_model(opath)
         d2 = m2.meta['desc']
         self.assertEqual(d2, dr)
     finally:
         # Tidy up
         if os.path.exists(opath):
             os.remove(opath)
     # Create model with strangely indented multi-line meta-data property
     d1 = '  First line\n\n   Second line'
     dr = 'First line\n\n Second line'
     m1 = myokit.Model()
     m1.meta['desc'] = d1
     e = m1.add_component('engine')
     v = e.add_variable('time')
     v.set_binding('time')
     v.set_rhs(0)
     # Store to disk
     opath = os.path.join(myotest.DIR_OUT, 'multiline.mmt')
     if os.path.exists(opath):
         os.remove(opath)
     try:
         myokit.save_model(opath, m1)
         # Load and compare the meta-data string
         m2 = myokit.load_model(opath)
         d2 = m2.meta['desc']
         self.assertEqual(d2, dr)
     finally:
         # Tidy up
         if os.path.exists(opath):
             os.remove(opath)