CellML 1.0 API¶
In addition to the CellML importers and exporters, Myokit contains an API to represent CellML models, along with methods to read and write CellML 1.0 and 1.1 documents. These methods are used under the hood by the importers and exporters.
In most cases, it’s easier to avoid these methods and use the CellML Importer and Exporter instead.
CellML Model API¶
- class myokit.formats.cellml.v1.Model(name, version='1.1')¶
Represents a CellML 1.0 or 1.1 model.
Support notes for 1.0 and 1.1:
Units with offsets are not supported, including the base units “celsius”.
Defining new base units is not supported.
Reactions are not supported.
Models that take derivatives with respect to more than one variable are not supported.
Models written as a DAE (e.g.
1 + x = 2 + y) are not supported.cmeta:id support is limited to models, components, and variables.
Support notes for 1.1:
Imports (CellML 1.1) are not supported.
Initial values can be local variable names, as long as those variables are constants (or depend only on constants).
Support notes for 1.0:
The stricter 1.1 rule for identifiers is used for both CellML 1.0 and 1.1: a valid identifier may not start with a number, and must contain at least one letter.
Arguments:
nameA valid CellML identifier string.
versionA string representing the CellML version this model is in (must be ‘1.0’ or ‘1.1’).
- add_component(name)¶
Adds an empty component with the given
name.
- add_connection(variable_1, variable_2)¶
Adds a connection between
variable_1andvariable_2.
- add_units(name, myokit_unit)¶
Add a model-level units definition to this model.
Arguments:
nameA valid CellML identifier to use as the name
myokit_unitA
myokit.Unitobject.
- components()¶
Returns an iterator over this model’s components.
- element_with_cmeta_id(cmeta_id)¶
Returns the model, component, or variable with the given
cmeta_idor raises a KeyError if no such element is found.
- find_units(name)¶
Looks up and returns a
Unitsobject with the givenname.Searches first in this model, then in the list of predefined units.
Raises a
CellMLErroris no unit is found.
- find_units_name(myokit_unit)¶
Attempts to find a string name for the given
myokit.Unit.Searches first in this model, then in the list of predefined units. If multiple units definitions have the same
myokit.Unit, the last added name is returned.Raises a
CellMLErroris no appropriate unit is found.
- free_variable()¶
Returns the free variable set with
set_free_variable().
- static from_myokit_model(model, version='1.1')¶
Creates a CellML
Modelfrom amyokit.Model.The CellML version to use can be set with
version, which must be either “1.0” or “1.1”.
- myokit_model()¶
Returns this model’s
myokit.Modelequivalent.
- name()¶
Returns this model’s name.
- set_free_variable(variable)¶
Tells this model which variable to regard as the free variable (with respect to which derivatives are taken).
- validate()¶
Validates this model, raising a
CellMLErrorif an errors are found.
- version()¶
Returns the CellML version this model is in.
- class myokit.formats.cellml.v1.Component(model, name)¶
Represents a model component, should not be created directly but only via
Model.add_component().- add_units(name, myokit_unit)¶
Add a component-level units definition to this component.
Arguments:
nameA valid CellML identifier to use as the name
myokit_unitA
myokit.Unitobject.
- add_variable(name, units, public_interface='none', private_interface='none')¶
Adds a variable with the given
nameandunits.Arguments
nameA valid CellML identifier (string).
unitsThe name of a units definition known to this component or its parent model.
public_interfaceThe variable’s public interface.
private_interfaceThe variable’s private interface.
- children()¶
Returns an iterator over any encapsulated child components.
- find_units(name)¶
Looks up and returns a
Unitsobject with the givenname.Searches first in this component, then in its model, then in the list of predefined units.
Raises a
CellMLErroris no unit is found.
- find_units_name(myokit_unit)¶
Attempts to find a string name for the given
myokit.Unit.Searches first in this component, then in its model, then in the list of predefined units. If multiple units definitions have the same
myokit.Unit, the last added name is returned.Raises a
CellMLErroris no appropriate unit is found.
- has_children()¶
Checks if this component has any encapsulated child components.
- model()¶
Return this component’s model.
- name()¶
Returns this component’s name.
- parent()¶
Returns this component’s parent component (or None).
- set_parent(parent)¶
Sets this component as the encapsulated child of the component
parent.
- variable(name)¶
Returns the variable with the given
name.
- variables()¶
Returns an iterator over this component’s variables.
- class myokit.formats.cellml.v1.Variable(component, name, units, public_interface='none', private_interface='none')¶
Represents a model variable, should not be created directly but only via
Component.add_variable().Arguments
componentThis variable’s parent
Component.nameThis variable’s name (a valid CellML identifier string).
unitsThe string name of a units object known to this variable’s component.
public_interfaceThe variable’s public interface.
private_interfaceThe variable’s private interface.
- component()¶
Return this variable’s component.
- initial_value()¶
Returns this variable’s initial value, or
Noneif it is not set.
- is_free()¶
Checks if this variable has been marked as the free variable.
- is_local()¶
Checks if this variable defines its own value.
- is_state()¶
Checks if this variable has been marked as a state variable.
- model()¶
Return this variable’s model.
- name()¶
Returns this variable’s name.
- private_interface()¶
Returns this variable’s private interface.
- public_interface()¶
Returns this variable’s public interface.
- rhs()¶
Returns this variable’s right-hand side expression, if set.
- rhs_or_initial_value()¶
For non-states, returns this variable’s RHS or its initial value if no RHS is set. For states always returns the RHS.
- set_initial_value(value)¶
Sets this variable’s intial value.
In CellML 1.0, this must be a
myokit.NumberorNone. In CellML 1.1 it can also be amyokit.Namereferencing a variable in the same component.To stay close to the specification, numbers are stored without units.
- set_is_state(state)¶
Set whether this variable is a state variable or not.
- set_rhs(rhs)¶
Sets a right-hand side expression for this variable.
The given
rhsmust be amyokit.Expressionwhere anymyokit.Nameobjects have a CellMLVariableas their value.
- source()¶
If this
Variablehas an “in” interface and is connected to another variable, this method returns that variable.If not, it returns
None.
- value_source()¶
Returns the
Variablethat this variable derives its value from.If the variable has a defining equation or an initial value, this will return the variable itself. If it is connected to another variable, it will follow the chain of dependencies until a variable with an equation or value is found.
- class myokit.formats.cellml.v1.Units(name, myokit_unit, predefined=False)¶
Represents a CellML units definition, should not be created directly but only via
Model.add_units()orComponent.add_units().Arguments:
nameA string name, used to refer to these units in the model.
myokit_unitA
myokit.Unitrepresenting these units.predefinedSet to
Truewhen creating an object for a predefined name. This is only used internally.
- classmethod find_units(name)¶
Searches for a predefined unit with the given
nameand returns aUnitsobject representing it.If no such unit is found a
CellMLErroris raised.
- classmethod find_units_name(myokit_unit)¶
Attempts to find a string name for the given
myokit.Unit.Raises a
CellMLErroris no appropriate unit is found.
- myokit_unit()¶
Returns the
Myokit.Unitequivalent for this units definition.
- name()¶
Returns the string name for this units definition.
- classmethod parse_unit_row(units, prefix=None, exponent=None, multiplier=None, context=None)¶
Creates a
myokit.Unitusing the information found in a single CellMLunitelement.Arguments
unitsThe name of the units to start from.
prefixAn optional argument that can be either a string from one of the predefined units prefixes, or an integer. If an integer is given the unit is scaled by
10**prefix. Used to make e.g.cm.exponentAn optional exponent (power) for this unit, used to make e.g.
m**2orcm**2.multiplierAn optional multiplier for this unit, used to make e.g.
inches. Note that exponentiation affects prefixes, but not multipliers.contextAn optional
ModelorComponentto use when looking upunitsnames. If not given, only the SI units will be supported.
- classmethod si_unit_names()¶
Returns an iterator over the predefined unit names.
- class myokit.formats.cellml.v1.AnnotatableElement(model)¶
Represents a CellML 1.0 or 1.1 element that can have a cmeta:id.
Each
AnnotatableElementalso has a public dictmetathat can be used to story key:value (string:string) pairs, for storing meta data.- cmeta_id()¶
Returns this element’s cmeta:id if set, or
Noneotherwise.
- set_cmeta_id(cmeta_id)¶
Sets this element’s cmeta id (must be a non-empty string or
None).
- class myokit.formats.cellml.v1.CellMLError(message)¶
Raised when an invalid CellML model is created or detected, or when a model uses CellML features that Myokit does not support.
- class myokit.formats.cellml.v1.UnitsError(message)¶
Raised when unsupported unit features are used.
- class myokit.formats.cellml.v1.UnsupportedBaseUnitsError(units)¶
Raised when unsupported base units are used.
- class myokit.formats.cellml.v1.UnsupportedUnitOffsetError¶
Raised when units with non-zero offsets are used.
- myokit.formats.cellml.v1.is_identifier(name)¶
Tests if the given
nameis a valid CellML 1.1 identifier.This method returns True if (and only if) the identifier
contains only alphanumeric characters (from the basic latin set) or underscores
contains at least one letter
does not begin with a numerical character.
The rules for 1.0 are slightly more lenient, and allow silly things like
1e2,123or_123as identifiers. Because this creates issues distinguishing identifiers from numbers, Myokit always follows the 1.1 rule, even for 1.0 documents.
- myokit.formats.cellml.v1.clean_identifier(name)¶
Checks if
nameis a valid CellML identifier and if not attempts to make it into one.Raises a
ValueErrorif it can’t create a valid identifier.
- myokit.formats.cellml.v1.create_unit_name(unit)¶
Creates an almost readable name for a Myokit
unit.
CellML Parsing¶
- myokit.formats.cellml.v1.parse_file(path)¶
Parses a CellML 1.0 or 1.1 model at the given path and returns a
myokit.formats.cellml.v1.Model.Raises a
CellMLParsingErrorif anything goes wrong.For notes about CellML 1.0/1.1 support, see
myokit.formats.cellml.v1.Model.
- myokit.formats.cellml.v1.parse_string(text)¶
Parses a CellML 1.0 or 1.1 model from the given string and returns a
myokit.formats.cellml.v1.Model.Raises a
CellMLParsingErrorif anything goes wrong.For notes about CellML 1.0/1.1 support, see
myokit.formats.cellml.v1.Model.
- class myokit.formats.cellml.v1.CellMLParser¶
Parses CellML 1.0 and 1.1 documents, and performs (partial) validation of a CellML document.
For notes about CellML 1.0/1.1 support, see
myokit.formats.cellml.v1.Model.- flatten(element)¶
Converts an element tree to a plain text string.
- parse(root)¶
Parses and validates a CellML document rooted in the given elementtree element.
- parse_file(path)¶
Parses and validates the CellML file at
pathand returns a CellML Model.
- parse_string(text)¶
Parses and validates the CellML XML in the string
textand returns a CellML model.
- class myokit.formats.cellml.v1.CellMLParsingError(message, element=None)¶
Raised if an error occurs during CellML parsing.
The argument
elementcan be used to pass in an element that caused the error.
CellML Writing¶
- myokit.formats.cellml.v1.write_file(path, model)¶
Writes a CellML 1.0 or 1.1 model to the given path.
- myokit.formats.cellml.v1.write_string(model)¶
Writes a CellML 1.0 or 1.1 model to a string and returns it.
- class myokit.formats.cellml.v1.CellMLWriter¶
Writes CellML 1.0 or 1.1 documents.
- write(model)¶
Takes a
myokit.formats.cellml.v1.Modelas input, and creates an ElementTree that represents it.If the model contains any variables that have an oxmeta meta data property, this will be annotated with RDF tags suitable for use with the Cardiac Electrophysiology Web Lab.
- write_file(path, model)¶
Takes a
myokit.formats.cellml.v1.Modelas input, and writes it to the givenpath.See
write()for details.
- write_string(model)¶
Takes a
myokit.formats.cellml.v1.Modelas input, and converts it to an XML string.See
write()for details.