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

Alternate sbmlmath testing #241

Merged
merged 5 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ reverse_relative = true
[tool.pytest.ini_options]
markers = [
"slow: marks tests as slow for pytest (deselect with '-m \"not slow\"')",
"sbmlmath: marks tests that import sbmlmath pytest (deselect with '-m \"not sbmlmath\"')"
]
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ install_requires =
typing_extensions
networkx
tqdm
sbmlmath; python_version >= "3.10"

zip_safe = false
include_package_data = True
Expand Down
21 changes: 14 additions & 7 deletions tests/test_metamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
template_model_from_askenet_json
from tests import expression_yielder, remove_all_sympy, sorted_json_str

try:
import sbmlmath
except ImportError:
sbmlmath_available = False
else:
sbmlmath_available = True

SBMLMATH_REQUIRED = unittest.skipUnless(sbmlmath_available, reason="SBMLMath package is not available")


class TestMetaModel(unittest.TestCase):
"""A test case for the metamodel."""
Expand All @@ -31,7 +40,6 @@ def setUp(self) -> None:
self.asymptomatic = Concept(name="asymptomatic infected population", identifiers={"ido": "0000511"})
self.immune = Concept(name="immune population", identifiers={"ido": "0000592"})


def test_schema(self):
"""Test that the schema is up to date."""
self.assertTrue(SCHEMA_PATH.is_file())
Expand Down Expand Up @@ -105,7 +113,7 @@ def test_rate_law_to_mathml():
'<ci>b1</ci></apply>')


@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
def test_mathml_to_sympy():
# 1
xml_str = """<apply>
Expand Down Expand Up @@ -181,11 +189,10 @@ def test_mathml_to_sympy():
assert expression_to_mathml(sympy_expr) == expression_mathml



@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
def test_from_askenet_petri():
source_url = "https://raw.githubusercontent.com/DARPA-ASKEM/Model" \
"-Representations/main/petrinet/examples/sir.json"
"-Representations/main/petrinet/examples/sir.json"
resp = requests.get(source_url)
assert resp.status_code == 200
model_json = resp.json()
Expand All @@ -208,11 +215,11 @@ def test_from_askenet_petri():
sympy.parse_expr(expression_str, local_dict=local_dict)


@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
def test_from_askenet_petri_mathml():
# Get model
source_url = "https://raw.githubusercontent.com/DARPA-ASKEM/Model" \
"-Representations/main/petrinet/examples/sir.json"
"-Representations/main/petrinet/examples/sir.json"
resp = requests.get(source_url)
assert resp.status_code == 200
model_json_mathml = resp.json()
Expand Down
22 changes: 15 additions & 7 deletions tests/test_model_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
from tests import sorted_json_str, remove_all_sympy
import requests

try:
import sbmlmath
except ImportError:
sbmlmath_available = False
else:
sbmlmath_available = True

SBMLMATH_REQUIRED = unittest.skipUnless(sbmlmath_available, reason="SBMLMath package is not available")


class RequestsWrapper:
base_url = "http://localhost:8771"
Expand Down Expand Up @@ -77,9 +86,9 @@ def _get_sir_templatemodel() -> TemplateModel:
class MockNeo4jClient:
@staticmethod
def query_relations(
source_curie: str,
relation_type: Union[str, List[str]],
target_curie: str,
source_curie: str,
relation_type: Union[str, List[str]],
target_curie: str,
) -> List:
rq = RelationQuery(
source_curie=source_curie,
Expand Down Expand Up @@ -151,7 +160,7 @@ def test_petri_distribution(self):
pm = response.json()
assert pm['T'][0]['tprop']['parameter_distribution'] == distr.json()
assert json.loads(pm['T'][0]['tprop']['mira_parameter_distributions']) == \
{'beta': distr.dict()}
{'beta': distr.dict()}
self.assertEqual(200, response.status_code, msg=response.content)

def test_petri_to_template_model(self):
Expand Down Expand Up @@ -179,7 +188,7 @@ def test_askenet_to_template_model(self):
template_model = TemplateModel.from_json(response.json())
self.assertIsInstance(template_model, TemplateModel)

@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
def test_askenet_to_template_model_no_sympy(self):
askenet_json = AskeNetPetriNetModel(Model(
sir_parameterized_init)).to_json()
Expand Down Expand Up @@ -248,7 +257,7 @@ def test_stratify_observable_api(self):
expr = sympy.Add(*[sympy.Symbol(s) for s in symbols])
tm.observables = {'half_population': Observable(
name='half_population',
expression=SympyExprStr(expr/2))
expression=SympyExprStr(expr / 2))
}

strata_options = dict(key='age',
Expand Down Expand Up @@ -665,4 +674,3 @@ def test_reconstruct_ode_semantics_endpoint(self):
assert len(flux_span_tm.templates) == 10
assert len(flux_span_tm.parameters) == 11
assert all(t.rate_law for t in flux_span_tm.templates)

25 changes: 17 additions & 8 deletions tests/test_modeling/test_askenet_ops.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import unittest
import requests
import pytest
from copy import deepcopy as _d
from mira.modeling.askenet.ops import *
from sympy import *
from mira.metamodel.io import mathml_to_expression
from mira.metamodel.templates import Concept

try:
import sbmlmath
except ImportError:
sbmlmath_available = False
else:
sbmlmath_available = True


SBMLMATH_REQUIRED = unittest.skipUnless(sbmlmath_available, reason="SBMLMath package is not available")


class TestAskenetOperations(unittest.TestCase):
"""A test case for operations on template models."""
Expand Down Expand Up @@ -188,7 +197,7 @@ def test_remove_observable_or_parameter(self):

self.assertEqual(old_obs['id'], new_obs['id'])

@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
def test_add_observable(self):
amr = _d(self.sir_amr)
new_id = 'testinf'
Expand All @@ -207,7 +216,7 @@ def test_add_observable(self):
self.assertEqual(xml_expression, new_observable_dict[new_id]['expression_mathml'])
self.assertEqual(sstr(mathml_to_expression(xml_expression)), new_observable_dict[new_id]['expression'])

@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
def test_replace_parameter_id(self):
old_id = 'beta'
new_id = 'TEST'
Expand Down Expand Up @@ -311,7 +320,7 @@ def test_remove_transition(self):
for new_transition in new_model_transition:
self.assertNotEquals(removed_transition, new_transition['id'])

@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
def test_add_transition(self):
test_subject = Concept(name="test_subject", identifiers={"ido": "0000511"})
test_outcome = Concept(name="test_outcome", identifiers={"ido": "0000592"})
Expand Down Expand Up @@ -404,7 +413,7 @@ def test_add_transition(self):
natural_degradation_rates_dict[new_transition_id]['expression'])
self.assertIn(test_subject.name, natural_degradation_states_dict)

@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
def test_replace_rate_law_sympy(self):
transition_id = 'inf'
target_expression_xml_str = '<apply><plus/><ci>X</ci><cn>8</cn></apply>'
Expand All @@ -419,7 +428,7 @@ def test_replace_rate_law_sympy(self):
self.assertEqual(sstr(target_expression_sympy), new_rate['expression'])
self.assertEqual(target_expression_xml_str, new_rate['expression_mathml'])

@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
def test_replace_rate_law_mathml(self):
amr = _d(self.sir_amr)
transition_id = 'inf'
Expand All @@ -435,7 +444,7 @@ def test_replace_rate_law_mathml(self):
self.assertEqual(sstr(target_expression_sympy), new_rate['expression'])
self.assertEqual(target_expression_xml_str, new_rate['expression_mathml'])

@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
# Following 2 unit tests only test for replacing expressions in observables, not initials
def test_replace_observable_expression_sympy(self):
object_id = 'noninf'
Expand All @@ -449,7 +458,7 @@ def test_replace_observable_expression_sympy(self):
self.assertEqual(sstr(target_expression_sympy), new_obs['expression'])
self.assertEqual(target_expression_xml_str, new_obs['expression_mathml'])

@pytest.mark.sbmlmath
@SBMLMATH_REQUIRED
def test_replace_observable_expression_mathml(self):
object_id = 'noninf'
amr = _d(self.sir_amr)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extras =
deps =
anyio<4
commands =
coverage run -p -m pytest --durations=20 {posargs:tests} -m "not slow and not sbmlmath"
coverage run -p -m pytest --durations=20 {posargs:tests} -m "not slow"
; coverage combine
; coverage xml
[testenv:docs]
Expand Down
Loading