Skip to content

Commit

Permalink
Added replace_rate_law_mathml and unit tests and modified unit_test f…
Browse files Browse the repository at this point in the history
…or test_replace_rate_law_sympy to test both output amr expression and expression_mathml
  • Loading branch information
nanglo123 committed Aug 30, 2023
1 parent a7b51c0 commit 27fc460
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
12 changes: 5 additions & 7 deletions mira/modeling/askenet/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import mira.metamodel.ops as tmops
from mira.sources.askenet.petrinet import template_model_from_askenet_json
from .petrinet import template_model_to_petrinet_json
from mira.metamodel.io import mathml_to_expression


def amr_to_mira(func):
Expand Down Expand Up @@ -113,20 +114,17 @@ def remove_transition(tm, transition_id):


@amr_to_mira
# rate law is of type Sympy Expression
def replace_rate_law_sympy(tm, transition_id, new_rate_law):
for template in tm.templates:
if template.name == transition_id:
template.rate_law = SympyExprStr(new_rate_law)
return tm


# Replace expression with new Content MathML
# TODO: we need MathML->sympy conversion for this
# def replace_rate_law_mathml(tm, transition_id, new_rate_law):
# for template in tm.templates:
# if template.name == transition_id:
# template.rate_law = SympyExprStr(new_rate_law)
# return tm
def replace_rate_law_mathml(tm, transition_id, new_rate_law):
new_rate_law_sympy = mathml_to_expression(new_rate_law)
return replace_rate_law_sympy(tm, transition_id, new_rate_law_sympy)


@amr_to_mira
Expand Down
35 changes: 27 additions & 8 deletions tests/test_modeling/test_askenet_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import requests
from copy import deepcopy as _d
from mira.modeling.askenet.ops import *
from sympy.parsing.sympy_parser import parse_expr
# from sympy.parsing.sympy_parser import parse_expr
from sympy import *
from mira.metamodel.io import mathml_to_expression


class TestAskenetOperations(unittest.TestCase):
Expand Down Expand Up @@ -273,18 +275,35 @@ def test_remove_transition(self):
def test_replace_rate_law_sympy(self):

transition_id = 'inf'
new_expression_str = 'TEST'
new_expression_sympy = parse_expr(new_expression_str)
amr = _d(self.sir_amr)
target_expression_str = '8+X'
target_expression_mathml_str = '<apply><plus/><ci>X</ci><cn>8</cn></apply>'

# Convert new_expression string into Sympy expression
new_expression_sympy = parse_expr(target_expression_str)

# what type does new_rate_law need to be?
amr = _d(self.sir_amr)
new_amr = replace_rate_law_sympy(amr, transition_id, new_expression_sympy)
new_semantics_ode_rates = new_amr['semantics']['ode']['rates']

for new_rate in new_semantics_ode_rates:
if new_rate['target'] == transition_id:
self.assertEqual(sstr(new_expression_sympy), new_rate['expression'])
self.assertEqual(sstr(target_expression_mathml_str), new_rate['expression_mathml'])

def test_replace_rate_law_mathml(self):
amr = _d(self.sir_amr)
transition_id = 'inf'
xml_str = "<apply><times/><ci>E</ci><ci>delta</ci></apply>"
sympy_expression = mathml_to_expression(xml_str)

new_amr = replace_rate_law_mathml(amr, transition_id, xml_str)

new_semantics_ode_rates = new_amr['semantics']['ode']['rates']

for new_rates in new_semantics_ode_rates:
if new_rates['target'] == transition_id:
self.assertEqual(new_rates['expression'], new_expression_str)
for new_rate in new_semantics_ode_rates:
if new_rate['target'] == transition_id:
self.assertEqual(new_rate['expression_mathml'], xml_str)
self.assertEqual(new_rate['expression'], sstr(sympy_expression))

def test_stratify(self):
amr = _d(self.sir_amr)
Expand Down

0 comments on commit 27fc460

Please sign in to comment.