-
Notifications
You must be signed in to change notification settings - Fork 7
Fix SbmlModel.get_free_parameter_ids_with_values to handle InitialA…
#248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |||||
| from typing import Iterable, Optional, Tuple | ||||||
|
|
||||||
| import libsbml | ||||||
| import sympy as sp | ||||||
|
|
||||||
| from ..sbml import ( | ||||||
| get_sbml_model, | ||||||
|
|
@@ -103,10 +104,41 @@ def get_free_parameter_ids_with_values( | |||||
| ar.getVariable() for ar in self.sbml_model.getListOfRules() | ||||||
| } | ||||||
|
|
||||||
| parser_settings = libsbml.L3ParserSettings( | ||||||
| self.sbml_model, | ||||||
| libsbml.L3P_PARSE_LOG_AS_LOG10, | ||||||
| libsbml.L3P_EXPAND_UNARY_MINUS, | ||||||
| libsbml.L3P_NO_UNITS, | ||||||
| libsbml.L3P_AVOGADRO_IS_CSYMBOL, | ||||||
| libsbml.L3P_COMPARE_BUILTINS_CASE_INSENSITIVE, | ||||||
| None, | ||||||
| libsbml.L3P_MODULO_IS_PIECEWISE, | ||||||
| ) | ||||||
|
|
||||||
| def get_initial(p): | ||||||
| # return the initial assignment value if there is one, and it is a | ||||||
| # number; `None`, if there is a non-numeric initial assignment; | ||||||
| # otherwise, the parameter value | ||||||
| if ia := self.sbml_model.getInitialAssignmentBySymbol(p.getId()): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's implied.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 was just a PEP 8 recommendation |
||||||
| formula_str = libsbml.formulaToL3StringWithSettings( | ||||||
| ia.getMath(), parser_settings | ||||||
| ) | ||||||
| try: | ||||||
| return float(formula_str) | ||||||
| except ValueError: | ||||||
| sym_expr = sp.sympify(formula_str) | ||||||
| return ( | ||||||
| float(sym_expr.evalf()) | ||||||
| if sym_expr.evalf().is_Number | ||||||
| else None | ||||||
| ) | ||||||
| return p.getValue() | ||||||
|
|
||||||
| return ( | ||||||
| (p.getId(), p.getValue()) | ||||||
| (p.getId(), initial) | ||||||
| for p in self.sbml_model.getListOfParameters() | ||||||
| if p.getId() not in rule_targets | ||||||
| and (initial := get_initial(p)) is not None | ||||||
| ) | ||||||
|
|
||||||
| def get_parameter_ids(self) -> Iterable[str]: | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty unintuitive, to me it looks like this will convert
logtolog10, and I'm not sure if the libsbml docs make sense. e.g. this forL3P_PARSE_LOG_AS_ERRORhttps://synonym.caltech.edu/software/libsbml/5.18.0/docs/formatted/python-api/namespacelibsbml.html#a9ef0398726fb45bd1cb24c4a5ed671f0
The docstring for
L3P_PARSE_LOG_AS_LOG10looks like it actually parseslogaslog, not parsinglogaslog10, but I'm not sure I trust the docs given the docstring forL3P_PARSE_LOG_AS_ERROR. Should we instead useL3P_PARSE_LOG_AS_LN?I think most users would expect
logto beln, and thatlog10is only when explicitly specified withlog(10, x)orlog10(x).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are the libsbml default settings. The only relevant thing here is ignoring units.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, if it actually keeps
logaslog, notlog10, then no problemUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this is about parsing MathML, not any observableFormula or the like. In MathML
logwithout explicit base meanslog10. In plain text formulae,logwill be interpreted asln.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh! Good to know that
logmeans different things in PEtab and the model 😬 Then I think we could be more careful about a math spec in the next version of PEtab, I added a comment to the draft timecourse spec.