libcellml
libcellml copied to clipboard
How to create MathML from formula
Hi all, I want to write formulas for my equation using the library. In the example it seems that I have to write the MathML by hand !?
See for instance https://libcellml.org/generated/v0.2.0/user/_downloads/9d787b885b55c361cc7ab95fe2a26858/tutorial3_complete.py
# Create the MathML2 string representing the governing equations.
equation1 = \
" <apply><eq/>"\
" <ci>c</ci>"\
" <apply><plus/>"\
" <ci>a</ci>"\
" <cn>2.0</cn>"\
" </apply>"\
" </apply>"
libsbml has simple helper functions such as parseFormula
or parseFormulaWithModel
which allows to provide a formula string such as a + S1/5.0 + sin(x)
and converts it to MathML. In the process the MathML is checked and in the functions with the model the model symbols are resolved.
Where can I find the helper functions to create MathML in the cellml library? Can I use the libsbml functions or are there cases where the MathML is incompatible between SBML and CellML (talking about core Math, not extended math symbols in SBML).
Best Matthias
P.S. @aditya-ml here the issue
At the moment we are simply treating the MathML as strings. We have done this to concentrate on the validation and code generation aspects of the library.
We will probably be looking at dealing with importing resources (components/units) from remote resources when we have sufficiently progressed the validation and code generation.
You will be able to tell when we have progressed the validation and code generation because we will release the library as version 1.0.0.
So, you won't find helper functions for dealing with MathML as such in the library. The best we have is available through the Analyser which will look at the math a little and determine if it is suitable for code generation.
We haven't considered comparing SBML math with CellML math and trying to work with it. Something for us to discuss.
Thanks. Sounds good. I would try to work with the libsbml helpers for now and see where issues arise in the generated MathML strings.
I got most of this working many years ago with the old CellML API, and I just poke around in the old code looking for 'math':
https://github.com/sys-bio/antimony/blob/develop/src/module-cellml.cpp
It looks like there used to be formula-to-CellML-math functionality in the 'TELICIMS' module, which the new API doesn't have any more, but there was still a lot of stuff that Just Worked using SBML stuff in CellML contexts and visa versa. Here's what I remember about the differences:
- CellML and SBML have different ways of declaring units for 'cn' elements, but it shouldn't be that hard to do a regexp search/replace of one with the other.
- CellML uses 'eq' a lot in the form of 'x = sqrt(3)', while SBML would just have 'sqrt(3)' as the math of an assignment rule for x.
- Similarly, SBML uses 'eq' for tests of equality, but in CellML you have to rely on context to tell if you're using it in a Boolean context or not.
- CellML can theoretically have derivatives of stuff with respect to non-time variables, but in practice this happens very rarely.
- Similarly, CellML in theory allows all of MathML, but in practice uses the same subset that SBML uses.
- Obviously, SBML-specific csymbols won't work in CellML. I haven't seen any CellML-specific csymbols.
- Notwithstanding the fact that CellML allows 'all' of MathML, the lone CellML simulator does not support all of MathML. As such, there are (I believe) parts of of the SBML-allowed subset of MathML that OpenCor doesn't handle. Again, in practice, this is a very small percentage of models that are out there, but it's something to know just in case.
Overall, I would say that 95% of the time, if you fix the units, you can use the MathML as-is in both formats, which means you can probably just use libsbml's formula-to-MathML translator as-is. I would work on the edge cases as they came up, unless you really need to be thorough for some reason (like you're writing a translator).
(Also, for the CellML library going forward: I wrote the bison parser SBML currently uses to parse infix, and I would be more than happy for you to just steal that code and rework it for your own purposes.)
@luciansmith Amazing. Thanks. This confirms what I thought and should work for my use cases which are mainly SBML (core) MathML -> CellML MathML translations.
I think with libCellML its even closer, as CellML 2.0 no longer allows "all of MathML" :)
Hi Matthias,
The only way, for now, to add some math to a model, using libCellML, is to add its corresponding (raw) MathML. So, yes, libCellML doesn't currently have a helper function like libSBML. I am not sure any of us has ever tried to add some math using libSBML's helper functio, but it might be worth giving it a try.
Cheers, Alan.
From: Matthias König @.> Sent: Wednesday, 17 August 2022 6:56 pm To: cellml/libcellml @.> Cc: Subscribed @.***> Subject: [cellml/libcellml] How to create MathML from formula (Issue #1017)
Hi all, I want to write formulas for my equation using the library. In the example it seems that I have to write the MathML by hand !?
See for instance https://libcellml.org/generated/v0.2.0/user/_downloads/9d787b885b55c361cc7ab95fe2a26858/tutorial3_complete.pyhttps://libcellml.org/generated/v0.2.0/user/_downloads/9d787b885b55c361cc7ab95fe2a26858/tutorial3_complete.py
Create the MathML2 string representing the governing equations.
equation1 =
"
"
"
"
"
"
"
libsbml has simple helper functions such as parseFormula or parseFormulaWithModel which allows to provide a formula string such as a + S1/5.0 + sin(x) and converts it to MathML. In the process the MathML is checked and in the functions with the model the model symbols are resolved. Where can I find the helper functions to create MathML in the cellml library? Can I use the libsbml functions or are there cases where the MathML is incompatible between SBML and CellML (talking about core Math, not extended math symbols in SBML).
Best Matthias
P.S. @aditya-mlhttps://github.com/aditya-ml here the issue
— Reply to this email directly, view it on GitHubhttps://github.com/cellml/libcellml/issues/1017, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAETBGMI3BN7EMP5BGCL7P3VZSEJ7ANCNFSM56YLDMDQ. You are receiving this because you are subscribed to this thread.Message ID: @.***>
Hi all, we used the libsbml MathML helpers to create the MathML for CellML version 2. This worked very nicely. Only thing to take care of was to handle the namespaces correctly. In the end this was as easy as the following
https://github.com/combine-org/combine-notebooks/blob/132f0a4af8589429513f882062f592932138b02d/src/combine_notebooks/examples/example_cellml.py#L209
Hope this helps others. Best Matthias