Skip to content

Commit

Permalink
Added a unit test for replacing observable id and fixed bug concernin…
Browse files Browse the repository at this point in the history
…g picking dict item objects in repplace observable id method
  • Loading branch information
nanglo123 committed Aug 17, 2023
1 parent e0d8a54 commit c652f39
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 10 additions & 1 deletion mira/modeling/askenet/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,38 @@ def replace_transition_id(tm, old_id, new_id):
return tm


# As of now, replace_observable_id replaces both 'id' and 'name' field of an observable in output amr
# can possibly add a new argument for display name and set observable.display_name to new argument and then change
# tm to json method to set 'name' field of output amr to tm.display_name
@amr_to_mira
def replace_observable_id(tm, old_id, new_id):
"""Replace the ID of an observable."""
for obs, observable in copy.deepcopy(tm.observables.items()):
for obs, observable in copy.deepcopy(tm.observables).items():
if obs == old_id:
observable.name = new_id

tm.observables[new_id] = observable
tm.observables.pop(old_id)
return tm


@amr_to_mira
# current bug is that it doesn't return the changed parameter
# expected 2 returned parameters, only got 1 (1 that wasnt changed)
def replace_parameter_id(tm, old_id, new_id):
"""Replace the ID of a parameter."""

for template in tm.templates:
if template.rate_law:
template.rate_law = SympyExprStr(
template.rate_law.args[0].subs(sympy.Symbol(old_id),
sympy.Symbol(new_id)))

for observable in tm.observables.values():
observable.expression = SympyExprStr(
observable.expression.args[0].subs(sympy.Symbol(old_id),
sympy.Symbol(new_id)))

return tm


Expand Down
18 changes: 17 additions & 1 deletion tests/test_modeling/test_askenet_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@ def setUpClass(cls):
'https://raw.githubusercontent.com/DARPA-ASKEM/'
'Model-Representations/main/petrinet/examples/sir.json').json()



# checks for updated id and name field of an observable - suggested change such that we can use a different value
# for name rather than reusing new_id
def test_replace_observable_id(self):
pass
old_id = 'noninf'
new_id = 'testinf'
amr = _d(self.sir_amr)
new_amr = replace_observable_id(amr, old_id, new_id)

old_semantics_observables = amr['semantics']['ode']['observables']
new_semantics_observables = new_amr['semantics']['ode']['observables']

self.assertEqual(len(old_semantics_observables), len(new_semantics_observables))

for old_observable, new_observable in zip(old_semantics_observables, new_semantics_observables):
if old_observable['id'] == old_id:
self.assertEqual(new_observable['id'], new_id) and self.assertEQual(new_observable['name'], new_id)

def test_replace_transition_id(self):
old_id = 'inf'
Expand Down

0 comments on commit c652f39

Please sign in to comment.