Skip to content
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

fix_cls #1833

Merged
merged 3 commits into from
Jul 20, 2022
Merged

fix_cls #1833

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions python/amici/pysb_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
ODEModel, Observable, Parameter, SigmaY, State)

CL_Prototype = Dict[str, Dict[str, Any]]
ConservationLaw = Dict[str, Union[str, sp.Basic]]
ConservationLaw = Dict[str, Union[Dict, str, sp.Basic]]

logger = get_logger(__name__, logging.ERROR)

Expand Down Expand Up @@ -1195,11 +1195,10 @@ def _apply_conseration_law_sub(cl: ConservationLaw,

:return: boolean flag indicating whether the substitution was applied
"""
coeff = cl['coefficients'].get(sub[0], 0.0)
if coeff == 0.0 or cl['state'] == sub[0]:
if not _state_in_cl_formula(sub[0], cl):
return False

del cl['coefficients'][sub[0]]
coeff = cl['coefficients'].pop(sub[0], 0.0)
# x_j = T/b_j - sum_{i≠j}(x_i * b_i) / b_j
# don't need to account for totals here as we can simply
# absorb that into the new total
Expand All @@ -1216,6 +1215,27 @@ def _apply_conseration_law_sub(cl: ConservationLaw,
return True


def _state_in_cl_formula(
state: sp.Symbol, cl: ConservationLaw
) -> bool:
"""
Checks whether state appears in the formula the provided cl

:param state:
state

:param cl:
conservation law

:return:
boolean indicator
"""
if cl['state'] == state:
return False

return cl['coefficients'].get(state, 0.0) != 0.0


def _get_conservation_law_subs(
conservation_laws: List[ConservationLaw]
) -> List[Tuple[sp.Symbol, Dict[sp.Symbol, sp.Expr]]]:
Expand All @@ -1233,9 +1253,8 @@ def _get_conservation_law_subs(
return [
(cl['state'], cl['coefficients']) for cl in conservation_laws
if any(
cl['state'] in other_cl['coefficients']
_state_in_cl_formula(cl['state'], other_cl)
for other_cl in conservation_laws
if other_cl != cl
)
]

Expand Down