Python API

The MUSCLE Python API closely resembles the C api but is slightly more brief. Currently, only Python 2.x is supported. It is enabled by default on the latest version of MUSCLE, and can be forced to be enabled by setting

$ cd [MUSCLE_SRC]/build
$ export MUSCLE_BUILD_OPTIONS="-DBUILD_PYTHON=ON"
$ ./build.sh [MUSCLE_INSTALL_DIR]

It will then be built as the Python module. By doing

$ source [MUSCLE_INSTALL_DIR]/etc/muscle.profile

it gets included in the Python module path. The latest information about the module can always be found by doing

$ python
import muscle
help(muscle)

The API includes the following functions:

muscle.init(sys.argv)
muscle.finalize()

muscle.send(portName, data, [muscle.datatype])
muscle.receive(portName, [muscle.datatype])

muscle.will_stop()
muscle.kernel_name()
muscle.tmp_path()
muscle.has_property(propName)
muscle.get_property(propName)

muscle.log(msg)
muscle.fine(msg)
muscle.warning(msg)
muscle.severe(msg)

These functions behave just like their C++ counterparts, except init, send and receive. For init, first run import sys and then muscle.init(sys.argv). Send will accept a sequence of objects, given a MUSCLE datatype. Receive will create a list of objects. The following datatypes are recognized:

Object Python data type C data type
muscle.int sequence of int 32-bit int
muscle.long sequence of int 64-bit long
muscle.float sequence of double 32-bit float
muscle.double sequence of double 64-bit double
muscle.boolean sequence of bool bool
muscle.raw bytearray char *
muscle.string str const char *

The first five data types take any sequence, as long as the datatype that it contains matches the canonical Python data type. In some cases, like with "NumPy?", it may be necessary to recreate the with the canonical Python datatype. For example, with int:

import muscle, numpy as np

data = np.zeros(100, np.int)
muscle.send([int(d) for d in data], 'out', muscle.int)

<< Back to Documentation