cellml / libcellml

Repository for libCellML development.
https://libcellml.org
Apache License 2.0
16 stars 21 forks source link

How to create MathML from formula #1017

Open matthiaskoenig opened 2 years ago

matthiaskoenig commented 2 years ago

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

hsorby commented 2 years ago

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.

matthiaskoenig commented 2 years ago

Thanks. Sounds good. I would try to work with the libsbml helpers for now and see where issues arise in the generated MathML strings.

luciansmith commented 2 years ago

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:

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).

luciansmith commented 2 years ago

(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.)

matthiaskoenig commented 2 years ago

@luciansmith Amazing. Thanks. This confirms what I thought and should work for my use cases which are mainly SBML (core) MathML -> CellML MathML translations.

nickerso commented 2 years ago

I think with libCellML its even closer, as CellML 2.0 no longer allows "all of MathML" :)

agarny commented 1 year ago

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 = \ " "\ " c"\ " "\ " a"\ " 2.0"\ " "\ " "

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: @.***>

matthiaskoenig commented 1 year ago

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