Reading and writing mmt files¶
This page is about reading and writing the mmt
format used by Myokit.
For information about using Myokit with other formats, please check out the
API :: Formats section.
Myokit models can be loaded form disk using load()
and stored using
save()
. Both these functions assume you’re working with a full
(model, protocol, script)
tuple. Support for partial loading and saving
is provided through functions like load_model()
and split()
.
Basic commands¶
-
myokit.
load
(filename)¶ Reads an
mmt
file and returns a tuple(model, protocol, embedded script)
.If the file specified by
filename
doesn’t contain one of these parts the corresponding entry in the tuple will beNone
.
-
myokit.
save
(filename=None, model=None, protocol=None, script=None)¶ Saves a model, protocol, and embedded script to an
mmt
file.The
model
argument can be given as plain text or amyokit.Model
object. Similarly,protocol
can be either amyokit.Protocol
or its textual represenation.If no filename is given the
mmt
code is returned as a string.
-
myokit.
parse
(source)¶ Parses strings in
mmt
format and returns a tuple(model, protocol, embedded script)
.None
may be returned for any segment not appearing in the source.The source to parse can be given as a plain string, a sequence of lines or a stream of lines.
Using parts of mmt files¶
-
myokit.
load_model
(filename)¶ Loads the model section from an
mmt
file.Raises a
SectionNotFoundError
if no model section is found.
-
myokit.
load_protocol
(filename)¶ Loads the protocol section from an
mmt
file.Raises a
SectionNotFoundError
if no protocol section is found.
-
myokit.
load_script
(filename)¶ Loads the script section from an
mmt
file.Raises a
SectionNotFoundError
if no script section is found.
-
myokit.
split
(source)¶ Attempts to split the
source
into model, protocol and script segments. Any content before the [[model]] tag is discarded.
-
myokit.
save_model
(filename, model)¶ Saves a model to a file
-
myokit.
save_protocol
(filename, protocol)¶ Saves a protocol to a file
-
myokit.
save_script
(filename, script)¶ Saves an embedded script to a file
Parsing parts mmt files¶
-
myokit.
parse_model
(source)¶ Parses a model in
mmt
format.
-
myokit.
parse_protocol
(source)¶ Parses a protocol in
mmt
format.
Parsing expressions¶
-
myokit.
parse_expression
(string, context=None)¶ Parses string data into a
myokit.Expression
.A
myokit.Variable
object can be given ascontext
to resolve any references against.
Parsing units¶
-
myokit.
parse_unit
(string)¶ Parses string data into a
myokit.Unit
.
Parse errors¶
Errors with the underlying file system (file not found, file corrupt etc) will
result in ordinary Python IO errors. Any other errors occurring during parsing
should be of the type ParseError
.
-
myokit.
format_parse_error
(ex, source=None)¶ Turns a ParseError
ex
into a detailed error message.If a filename or input stream is passed in as
source
, this will be used to show the line on which an error occurred. If a stream is passed, it should be rewinded to the same point the original parsing started.
A note about the format¶
The mmt
format consists of several sections (each with their own syntax)
stored sequentially in a single, string-based format. In theory, this means
mmt
files containing syntax errors cannot be reliably separated into parts.
For example if a string isn’t terminated in one section, it is impossible to
tell if a subsequent section header indicates a new section or is part of the
string.
These problems could be avoided in a more complicated format, for example a zip
file containing separate files for each section. However, using a plain-text
format makes mmt
files human readable, loadable in any text editor and
directly suitable for storage in versioning systems.