Skip to content

Instantly share code, notes, and snippets.

@hsorby
Created April 21, 2021 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsorby/63c39830391a789c57133fda4608c5d6 to your computer and use it in GitHub Desktop.
Save hsorby/63c39830391a789c57133fda4608c5d6 to your computer and use it in GitHub Desktop.
2021 CellML Workshop - libCellML Python scripts
import libcellml
p = libcellml.Parser()
with open('New_Modular_Model.cellml') as f:
contents = f.read()
m = p.parseModel(contents)
print(m.componentCount())
v = libcellml.Validator()
v.validateModel(m)
print(v.errorCount())
print(m.hasImports())
i = libcellml.Importer()
i.resolveImports(m, '/Users/hsor001/tut/')
v.validateModel(m)
print(v.errorCount())
with open('NaK_flux.cellml') as f:
contents = f.read()
m_flux = p.parseModel(contents)
v.validateModel(m_flux)
print(v.errorCount())
for index in range(v.errorCount()):
print(v.error(index).description())
imp = libcellml.ImportSource()
imp.setUrl('Units.cellml')
for index in range(v.errorCount()):
if v.error(index).variable() is not None:
name = v.error(index).variable().units().name()
if not m_flux.hasUnits(name):
u = libcellml.Units(name)
u.setImportSource(imp)
u.setImportReference(name)
print(name, m_flux.addUnits(u))
v.validateModel(m_flux)
print(v.errorCount())
cn_units_names = ['per_umol', 'per_volt4_per_second', 'per_volt3_per_second', 'per_volt2_per_second', 'per_volt_per_second']
for cn_units_name in cn_units_names:
u = libcellml.Units(cn_units_name)
u.setImportSource(imp)
u.setImportReference(cn_units_name)
print(cn_units_name, m_flux.addUnits(u))
v.validateModel(m_flux)
print(v.errorCount())
pr = libcellml.Printer()
flux_content = pr.printModel(m_flux)
print(flux_content)
with open('NaK_flux_with_units.cellml', 'w') as f:
f.write(flux_content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment