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 setting live=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.

Typical use case (one-shot use):

m,p,x = myokit.load('example')
s = myokit.Simulation(m, p)
with myokit.Benchmarker() as b:
    s.run()
    print(b.time())

Alternative use:

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.

Logging

A number of Myokit functions (for example importers and exporters) maintain a log of their actions using the TextLogger class described below. If anything goes wrong, these logs should contain a more detailed error message than a simple exception.

class myokit.TextLogger

Abstract class

Base class for any object that needs to keep a text log of its operations.

clear_warnings()

Clears any currently set warnings.

has_warnings()

Returns True if this logger has any warnings.

is_live()

Returns True if live logging is enabled and any logged messages are directly printed to stdout.

log(*text)

Writes text to the log. Text fragments given as separate arguments will be written to the log as separate lines.

log_flair(text)

Logs a single text fragment centered on the line, surrounded by lines of hyphens:

------------------------------------------------------------
                         Like this!
------------------------------------------------------------
log_line()

Writes a horizontal line (——) to the log.

log_warnings()

Writes all the logged warnings to the log and clears the warnings buffer.

set_live(enabled=True)

When live logging is enabled, all log data will be written to stdout immediatly.

set_log_line_width(width)

Changes the line width used by this text logger.

text()

Returns the logged text.

warn(message)

Logs a warning to the warnings list.

warnings()

Returns an iterator over all warnings currently set

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 through Model.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 specified state 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 by Model.map_to_state() and will be stored in the format returned by Model.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, where x is the used data type (d or f) and y 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=None)

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 to root.

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.lvsd(s1, s2)

Calculates a Levenshtein distance, as found on wikibooks

Parameters:
  • s1 – The first string to compare
  • s2 – The second string to compare
Returns:

int The distance between s1 and s2

myokit.strfloat(number)

Turns the given number into a string, without losing accuracy.

myokit.time()

Returns the current time, formatted as specified in myokit.settings.

myokit.version(raw=False)

Returns the current Myokit version.

Other functions

myokit.default_protocol()

Provides a default protocol to use when no embedded one is available.

myokit.default_script()

Provides 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.natural_sort_key(s)

Natural sort key, from: http://stackoverflow.com/questions/4836710/

Usage example:

values.sort(key=lambda x: myokit.natural_sort_key(x))
myokit.numpywriter()

Returns a globally shared numpy expression writer.

LhsExpressions are converted as follows:

  1. Derivatives are indicated as “_d_” + var.uname()
  2. 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.pywriter()

Returns a globally shared python expression writer.

LhsExpressions are converted as follows:

  1. Derivatives are indicated as “_d_” + var.uname()
  2. 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 given model and protocol.

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()) as stdout and stderr 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.