forked from matthiaskoenig/sbmlutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathml.py
193 lines (151 loc) · 5.46 KB
/
mathml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""Rendering of formulas and Content MathML.
A common problem in rendering MathML is that the content MathML is difficult to read.
The presentation MathML has a much better rendering and improves understandability.
This module uses stylesheets for the conversion of content MathMl -> presentation
MathML.
see also: https://docs.sympy.org/dev/modules/printing.html#module-sympy.printing.mathml
"""
import re
from functools import lru_cache
from typing import Optional, Set
import libsbml
import lxml.etree as ET
from sbmlutils import RESOURCES_DIR, log
logger = log.get_logger(__name__)
xslt_cmml2pmml = ET.parse(str(RESOURCES_DIR / "xslt" / "ctopff.xsl"))
xslt_pmml2tex = ET.parse(str(RESOURCES_DIR / "xslt" / "xsltml" / "mmltex.xsl"))
def formula_to_astnode(
formula: str, model: Optional[libsbml.Model] = None
) -> libsbml.ASTNode:
"""Convert formula string to ASTNode.
:param formula: SBML formula string
:param model: libsbml.Model
:return: libsbml.ASTNode
"""
if model:
astnode = libsbml.parseL3FormulaWithModel(formula, model)
else:
astnode = libsbml.parseL3Formula(formula)
if not astnode:
logger.error(f"Formula could not be parsed: '{formula}'")
logger.error(libsbml.getLastParseL3Error())
raise ValueError(
f"Formula could not be parsed: '{formula}'.\n"
f"{libsbml.getLastParseL3Error()}"
)
return astnode
def formula_to_latex(formula: str, model: Optional[libsbml.Model] = None) -> str:
"""Convert formula string to latex."""
astnode = formula_to_astnode(formula, model)
return astnode_to_latex(astnode)
def cmathml_to_astnode(cmathml: str) -> libsbml.ASTNode:
"""Convert Content MathML string to ASTNode.
:param cmathml: SBML Content MathML string
:return: libsbml.ASTNode
"""
return libsbml.readMathMLFromString(cmathml)
def astnode_to_latex(astnode: libsbml.ASTNode) -> str:
"""Convert ASTNode to Latex using XSLT transformation."""
cmml_str: str = libsbml.writeMathMLToString(astnode)
cmml_str = cmml_str.replace('<?xml version="1.0" encoding="UTF-8"?>', "")
return cmathml_to_latex(cmml_str)
@lru_cache(maxsize=10000)
def cmathml_to_latex(cmml_str: str) -> str:
"""Content MathML to latex conversion using XSLT transformation."""
# content MathML -> presentation MathML
cmml_dom = ET.fromstring(cmml_str)
transform1 = ET.XSLT(xslt_cmml2pmml)
pmml_dom = transform1(cmml_dom)
# content MathML -> latex
transform2 = ET.XSLT(xslt_pmml2tex)
tex_str = str(transform2(pmml_dom))
# remove equation symbols
tex_str = tex_str.replace("$", "")
# fix piecewise
tex_str = tex_str.replace(r"\hfill", "")
tex_str = tex_str.replace(r"\multicolumn{2}{c}", "")
tex_str = tex_str.replace(r"\left(\{\begin{array}{ccc}", r"\begin{cases} ")
tex_str = tex_str.replace(r"\end{array}\right)", r"\end{cases}")
tex_str = tex_str.replace(r"\{\begin{array}{ccc}", r"\begin{cases} ")
tex_str = tex_str.replace(r"\end{array}", r"\end{cases}")
# fix lambda function
tex_str = tex_str.replace(r"}\mathit", r"}, \mathit")
tex_str = tex_str.replace(r"\lambda ", r"\lambda(")
tex_str = tex_str.replace(r"}.", "}) =")
# cleanup symbols
tex_str = _fix_mathit_symbols(tex_str)
# print(tex_str)
# pmml_bytes = ET.tostring(pmml_dom, pretty_print=True)
# pmml_str = pmml_bytes.decode("UTF-8")
return tex_str
# symbols replaced in latex
greek_symbols = [
"alpha",
"beta",
"gamma",
"Gamma" "delta",
"Delta",
"epsilon",
"zeta",
"eta",
"theta",
"iota",
"kappa",
"Lambda", # no lowercase due to function definition
"mu",
"nu",
"omicron",
"pi" "rho",
"sigma",
"tau",
"upsilon",
"Upsilon",
"phi",
"Phi",
"chi",
"psi",
"Psi",
"omega",
"Omega",
]
def symbol_to_latex(symbol: str) -> str:
"""Convert symbol to latex by packing in mathit and escaping underscores."""
symbol = symbol.replace(r"_", r"\_")
symbol = r"\mathit{" + symbol + "}"
return _fix_mathit_symbols(symbol)
def _fix_mathit_symbols(tex_str: str) -> str:
"""Heuristic replacements for better latex rendering.
Single underscores are set down.
Greek symbols are rendered (with exception of small lambda).
"""
# fix single underscores in variable names
# \mathit{group1\_group2} -> \mathit{group1_{group2}}
matches = re.findall(r"\\mathit{([a-zA-Z0-9]+)\\_([a-zA-Z0-9]+)}", tex_str)
if matches:
for m in matches:
tex_str = tex_str.replace(
r"\mathit{" + m[0] + r"\_" + m[1] + "}",
r"\mathit{" + m[0] + r"_{" + m[1] + "}}",
)
# replace greek symbols
for symbol in greek_symbols:
tex_str = tex_str.replace(
r"\mathit{" + symbol + "}", r"\mathit{" + f"\{symbol}" + "}" # noqa: W605
)
return tex_str
def _get_variables(
astnode: libsbml.ASTNode, variables: Optional[Set[str]] = None
) -> Set[str]:
"""Get variables from ASTNode."""
if variables is None:
variables: Set[str] = set() # type: ignore
num_children = astnode.getNumChildren()
if num_children == 0:
if astnode.isName():
name = astnode.getName()
variables.add(name) # type: ignore
else:
for k in range(num_children):
child: libsbml.ASTNode = astnode.getChild(k)
_get_variables(child, variables=variables)
return variables # type: ignore