Tools

Myokit contains a module myokit.tools with functions and classes that are used throughout Myokit, but are not particularly Myokit-specific. This module is imported automatically when import myokit is run.

Benchmarking

class myokit.tools.Benchmarker

Allows benchmarking using the with statement.

Example:

m,p,x = myokit.load('example')
s = myokit.Simulation(m, p)
b = myokit.tools.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.

Capturing printed output

class myokit.tools.capture(fd=False)

Context manager that temporarily redirects the current standard output and error streams, and captures anything that’s written to them.

Example:

with myokit.tools.capture() as a:
    print('This will be captured')

Within a single thread, captures can be nested, for example:

with myokit.tools.capture() as a:
    print('This will be captured by a')
    with myokit.tools.capture() as b:
        print('This will be captured by b, not a')
    print('This will be captured by a again')

Capturing is thread-safe: a lock is used to ensure only a single thread is capturing at any time. For example, if we have a function:

def f(i):
    with myokit.tools.capture() as a:
        print(i)
        ...

    return a.text()

and this is called from several threads, the capture acts as a lock (a threading.RLock) so that one thread will need to finish executing the code within the with statement before a second thread can start capturing.

In multiprocessing, no locks are used, and no memory or streams are shared so that this should also be safe.

By default, this method works by simply redirecting sys.stdout and sys.stderr. This captures any output written by the Python interpreter, but does not catch output from C/C++ extensions or subrocesses. To also catch that output start the capture with the optional argument fd=True, which enables a file descriptor duplication method of redirection.

err()

Returns the text captured from stderr, or an empty string if nothing was captured or capturing is still active.

out()

Returns the text captured from stdout, or an empty string if nothing was captured or capturing is still active.

text()

Returns the combined text captured from output and error text, if any (output first, then error text).

File system

myokit.tools.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 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.tools.rmtree(path)

Version of shutil.rmtree that handles Windows “access denied” errors (when the user is lacking write permissions, but is allowed to set them).

String comparison

myokit.tools.lvsd(s, t)

Returns the Levenshtein distance between two strings s and t.

myokit.tools.natural_sort_key(s)

Function to use as key in a sort, to get natural sorting of strings (e.g. “2” before “10”).

Usage examples:

names.sort(key=myokit.tools.natural_sort_key)

variables.sort(key=lambda v: myokit.tools.natural_sort_key(v.qname()))

Deprecated functions

Some of the functions in tools have deprecated aliases, that will be removed in future Myokit releases.

class myokit.Benchmarker

Deprecated alias of myokit.tools.Benchmarker.

myokit.format_float_dict(d)

Takes a dictionary of string : float mappings and returns a single multi-line string, where each line is of the form key = value, where key is padded with spaces to make the equals signs align.

This method is deprecated and will be removed in future versions of Myokit.

myokit.format_path(path, root='.')

Deprecated alias of myokit.tools.format_path.

myokit.strfloat(number, full=False, precision=64)

Deprecated alias of myokit.float.str.