CellML API
Working with CellML models using Python
Setting up cgrspy
The CellML API comes with a module called CGRS (the CellML Generics and Reflection Service). A module called cgrspy allows you to call API services from Python through the CGRS.
Firstly, make sure the CellML SDK is in your dynamic linker library path and include paths, as discussed on the Getting Started page. Then you can run something like:
Getting started with the API in Python
The only Python module you need to import to use the CellML API is cgrspy.bootstrap.
The CellML API is divided up into services. Each service has a CGRS module which needs to be loaded before you can have any interaction with that part of the API. You load a CGRS module by calling cgrspy.botostrap.loadGenericModule("nameOfModuleHere"). The CGRS module should be in the dynamic library loader path. For example, this program simply loads the CGRS module for the core CellML API:
The CellML API is object-orientated, and you generally obtain objects from other objects. However, to get started, you need to obtain a special object called a bootstrap object (there is typically only one bootstrap object per API service). You can use cgrspy.bootstrap.fetch to fetch a CellML API bootstrap. For example, to obtain the CellML Bootstrap:
The Python object cellmlBootstrap is of Python type cgrspy.Object, as all objects obtained from cgrspy are. In the CellML API, one object can implement multiple interfaces; you can access attributes and methods on any of those interfaces directly from a Python object that supports them. You can see the list of interfaces that are supported by an object using the XPCOM::IObject::supported_interfaces attribute:
All attributes defined on the API can be accessed directly as a Python attribute. Operations defined on the CellML API become methods in Python; `in' parameters are Python parameters, and the return value and any `out' parameters get put together as a Python tuple in the return value.
For example, the below example uses cellml_api::CellMLBootstrap::createModel, cellml_api::CellMLBootstrap::createComponent, cellml_api::NamedCellMLElement::name, cellml_api::CellMLElement::addElement, cellml_api::CellMLBootstrap::createCellMLVariable, cellml_services::TeLICeMService::parseMaths, cellml_api::CellMLDOMElement::domElement, dom::Document::ownerDocument, cellml_services::TeLICeMMathResult::mathResult, cellml_api::MathContainer::addMath and cellml_api::Model::serialisedText to create a simple document. Referring to the documentation for each of these operations will help you understand the Python code and how it relates to the API documentation.
One feature of the Python bindings is that you can use iterators conveniently using the Python iterator protocol to write code that looks more natural. The Python binding can be used anywhere where an interface defines an iterate function, and the return value defines a next function. For example, you can use it to iterate through all components in a model: