Skip to content

Commit

Permalink
Added minimal unit tests for 4 interface methods and unit test for re…
Browse files Browse the repository at this point in the history
…move_state
  • Loading branch information
nanglo123 committed Aug 29, 2023
1 parent d07011b commit 2484668
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 15 deletions.
21 changes: 8 additions & 13 deletions mira/modeling/askenet/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ 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 in new_amr['semantics']['ode']['observables'] to observable.display_name
@amr_to_mira
def replace_observable_id(tm, old_id, new_id):
"""Replace the ID of an observable."""
Expand All @@ -67,8 +64,6 @@ def replace_observable_id(tm, old_id, new_id):


@amr_to_mira
# current bug is that it doesn't return the changed parameter in new_amr['semantics']['ode']['parameters']
# expected 2 returned parameters in list of parameters, only got 1 (the 1 that wasn't changed)
def replace_parameter_id(tm, old_id, new_id):
"""Replace the ID of a parameter."""
for template in tm.templates:
Expand Down Expand Up @@ -138,20 +133,20 @@ def replace_rate_law_sympy(tm, transition_id, new_rate_law):


@amr_to_mira
def stratify(**kwargs):
return tmops.stratify(**kwargs)
def stratify(*args, **kwargs):
return tmops.stratify(*args, **kwargs)


@amr_to_mira
def simplify_rate_laws(**kwargs):
return tmops.simplify_rate_laws(**kwargs)
def simplify_rate_laws(*args, **kwargs):
return tmops.simplify_rate_laws(*args, **kwargs)


@amr_to_mira
def aggregate_parameters(**kwargs):
return tmops.aggregate_parameters(**kwargs)
def aggregate_parameters(*args, **kwargs):
return tmops.aggregate_parameters(*args, **kwargs)


@amr_to_mira
def counts_to_dimensionless(**kwargs):
return tmops.counts_to_dimensionless(**kwargs)
def counts_to_dimensionless(*args, **kwargs):
return tmops.counts_to_dimensionless(*args, **kwargs)
75 changes: 73 additions & 2 deletions tests/test_modeling/test_askenet_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def test_replace_transition_id(self):
if old_transitions['id'] == old_id:
self.assertEqual(new_transition['id'], new_id)

# checks for updated id and name field of an observable - suggested change in method definition
# such that we can use a different value for name rather than reusing new_id
# checks for updated id and name field of an observable - suggested change
# such that we can use a different value for name field rather than reusing new_id
def test_replace_observable_id(self):
old_id = 'noninf'
new_id = 'testinf'
Expand All @@ -149,6 +149,8 @@ def test_replace_observable_id(self):
if old_observable['id'] == old_id:
self.assertEqual(new_observable['id'], new_id) and self.assertEqual(new_observable['name'], new_id)

# current bug is that it doesn't return the changed parameter in new_amr['semantics']['ode']['parameters']
# expected 2 returned parameters in list of parameters, only got 1 (the 1 that wasn't changed)
def test_replace_parameter_id(self):
old_id = 'beta'
new_id = 'zeta'
Expand Down Expand Up @@ -192,3 +194,72 @@ def test_replace_parameter_id(self):
mathml_flag = (new_value_in_observable_mathml and old_value_out_observable_mathml)

self.assertTrue(expression_flag and mathml_flag)

def test_remove_state(self):
removed_state = 'S'
amr = _d(self.sir_amr)

new_amr = remove_state(amr, removed_state)

new_model = new_amr['model']
new_model_states = new_model['states']
new_model_transitions = new_model['transitions']

new_semantics_ode = new_amr['semantics']['ode']
new_semantics_ode_rates = new_semantics_ode['rates']
new_semantics_ode_initials = new_semantics_ode['initials']
new_semantics_ode_parameters = new_semantics_ode['parameters']
new_semantics_ode_observables = new_semantics_ode['observables']

for new_state in new_model_states:
self.assertTrue(removed_state != new_state['id'])

for new_transition in new_model_transitions:
self.assertNotIn(removed_state, new_transition['input'])
self.assertNotIn(removed_state, new_transition['output'])

# output rates that originally contained targeted state are removed
for new_rate in new_semantics_ode_rates:
self.assertNotIn(removed_state, new_rate['expression'])
self.assertNotIn(removed_state, new_rate['expression_mathml'])

# initials are bugged, all states removed rather than just targeted removed state in output amr
for new_initial in new_semantics_ode_initials:
self.assertNotIn(removed_state, new_initial['target'])

# parameters that are associated in an expression with a removed state are not present in output amr
for new_parameter in new_semantics_ode_parameters:
self.assertNotIn(removed_state + '0', new_parameter['id'])

# output observables that originally contained targeted state still exist with targeted state removed
# (e.g. 'S+R' -> 'R') if 'S' is the removed state
for new_observable in new_semantics_ode_observables:
self.assertNotIn(removed_state, new_observable['expression'])
self.assertNotIn(removed_state, new_observable['expression_mathml'])

def test_stratify(self):
amr = _d(self.sir_amr)
new_amr = stratify(amr, key='city', strata=['boston', 'nyc'])

self.assertIsInstance(amr, dict)
self.assertIsInstance(new_amr, dict)

def test_simplify_rate_laws(self):
amr = _d(self.sir_amr)
new_amr = simplify_rate_laws(amr)

self.assertIsInstance(amr, dict)
self.assertIsInstance(new_amr, dict)

def test_aggregate_parameters(self):
amr = _d(self.sir_amr)
new_amr = aggregate_parameters(amr)

self.assertIsInstance(amr, dict)
self.assertIsInstance(new_amr, dict)

def test_counts_to_dimensionless(self):
amr = _d(self.sir_amr)
new_amr = counts_to_dimensionless(amr, 'ml', .8)
self.assertIsInstance(amr, dict)
self.assertIsInstance(new_amr, dict)

0 comments on commit 2484668

Please sign in to comment.