Miscellaneous¶
Model comparison¶
-
class
myokit.
ModelComparison
(model1, model2, live=False)¶ Compares two models.
The resulting diff text can be iterated over:
for line in ModelComparison(m1, m2): print(line)
Differences can be printed directly to
stdout
by settinglive=True
.-
equal
()¶ Returns
True
if the two models were equal.
-
text
()¶ Returns the full difference text.
-
Benchmarking¶
-
class
myokit.
Benchmarker
(output=None)¶ Allows benchmarking using the with statement.
Example:
m,p,x = myokit.load('example') s = myokit.Simulation(m, p) b = myokit.Benchmarker() s.run() print(b.time()) b.reset() s.run() print(b.time())
-
format
(time)¶ Formats a (non-integer) number of seconds, returns a string like “5 weeks, 3 days, 1 hour, 4 minutes, 9 seconds”, or “0.0019 seconds”.
-
reset
()¶ Resets this timer’s start time.
-
time
()¶ Returns the time since benchmarking started.
-
State i/o¶
-
myokit.
load_state
(filename, model=None)¶ Loads an initial state from a file in one of the formats specified by
myokit.parse_state()
.If a
Model
is provided the state will be run throughModel.map_to_state()
and returned as a list of floating point numbers.
-
myokit.
load_state_bin
(filename)¶ Loads an initial state from a file in the binary format used by myokit. See
save_state_bin()
for details.
-
myokit.
parse_state
(state)¶ Parses an initial state declaration given in one of two formats:
<var_name_1> = <value> <var_name_1> = <value> ...
Blank lines, whitespace and indentation are ignored. All variable names must be given fully qualified. Parsed data from this format is returned as a dictionary mapping variable names to values.
Alternatively, the input can be given as a list of floating point numbers separated by spaces, commas, semi-colons, line breaks etc:
<value> <value> ...
Parsed data from this input format is returned as a list of floating point numbers.
-
myokit.
save_state
(filename, state, model=None)¶ Stores the given state in the file at
filename
.If no
model
is specifiedstate
should be given as a list of floating point numbers and will be stored by simply placing each number on a new line.If a
Model
is provided the state can be in any format accepted byModel.map_to_state()
and will be stored in the format returned byModel.format_state()
.
-
myokit.
save_state_bin
(filename, state, precision=64)¶ Stores the given state (or any list of floating point numbers) in the file at
filename
, using a binary format.The used format is a zip file, containing a single entry:
state_x_y
, wherex
is the used data type (d
orf
) andy
is the number of entries. All entries are stored little-endian.
String functions¶
-
myokit.
date
()¶ Returns the current date and time, formatted as specified in
myokit.settings
.
-
myokit.
format_float_dict
(d)¶ Takes a dictionary of
string : float
mappings and returns a formatted string.
-
myokit.
format_path
(path, root='.')¶ Formats a path for use in user messages. If the given path is a subdirectory of the current directory this part is chopped off.
Alternatively, a
root
directory may be given explicitly: any subdirectory of this path will be formatted relative toroot
.This function differs from os.path.relpath() in the way it handles paths outside the root: In these cases relpath returns a relative path such as ‘../../’ while this function returns an absolute path.
-
myokit.
strfloat
(number, full=False, precision=64)¶ Turns the given number into a string.
-
myokit.
time
()¶ Returns the current time, formatted as specified in
myokit.settings
.
-
myokit.
version
(raw=False)¶ Returns the current Myokit version.
By default, a formatted multi-line string is returned. To get a simpler one-line string set the optional argument
raw=True
.The same version info can be accessed using
myokit.__version__
. Unformatted info is available inmyokit.__version_tuple__
, which contains the major, minor, and revision number respectively, all asint
objects. For development versions of Myokit, it may contain a 4th element"dev"
.
Other functions¶
-
myokit.
default_protocol
(model=None)¶ Returns a default protocol to use when no embedded one is available.
-
myokit.
default_script
(model=None)¶ Returns a default script to use when no embedded script is available.
-
myokit.
pack_snapshot
(path, overwrite=True)¶ Packs a snapshot of the current myokit module into a zipfile at the given path.
-
myokit.
numpy_writer
()¶ Returns a globally shared numpy expression writer.
LhsExpressions are converted as follows:
- Derivatives are indicated as “_d_” + var.uname()
- Other names are indicated as var.uname()
This convention ensures a unique mapping of a model’s lhs expressions to acceptable python variable names.
-
myokit.
python_writer
()¶ Returns a globally shared python expression writer.
LhsExpressions are converted as follows:
- Derivatives are indicated as “_d_” + var.uname()
- Other names are indicated as var.uname()
This convention ensures a unique mapping of a model’s lhs expressions to acceptable python variable names.
-
myokit.
run
(model, protocol, script, stdout=None, stderr=None, progress=None)¶ Runs a python
script
using the givenmodel
andprotocol
.The following functions are provided to the script:
get_model()
- This returns the model passed to
run()
. When using the GUI, this returns the model currently loaded into the editor. get_protocol()
- This returns the protocol passed to
run()
. When using the GUI, this returns the protocol currently loaded into the editor.
Ordinary and error output can be re-routed by providing objects with a file-like interface (
x.write(text)
,x.flush()
) asstdout
andstderr
respectively. Note that output is only re-routed on the python level so that any output generated by C-code will still go to the main process’s output and error streams.An object implementing the
ProgressReporter
interface can be passed in. This will be made available globally to any simulations (or other methods) that provide progress update information.
-
myokit.
strip_expression_units
(model_text, skip_literals=True)¶ Takes the text of a valid model as input and returns a version stripped of any expression units. Variable units defined with
in
are preserved. Only the model part should be passed in, no script or protocol segments.By default, constants defined for variables whose RHS is a single number will keep their units. To disable this, set
skip_literals=False
.This method will raise a
myokit.ParseError
if the given code cannot be parsed to a valid model.