From 49d5d3c2c1fc177420e939c5709e12d90c8528ed Mon Sep 17 00:00:00 2001 From: nanglo123 Date: Thu, 27 Jun 2024 14:03:53 -0400 Subject: [PATCH] Add regression test for serialization of template model into different frameworks --- tests/test_amr_source.py | 90 ++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 32 deletions(-) diff --git a/tests/test_amr_source.py b/tests/test_amr_source.py index 6b6db472..d5af156d 100644 --- a/tests/test_amr_source.py +++ b/tests/test_amr_source.py @@ -1,18 +1,51 @@ import requests +import json + import sympy + from mira.metamodel import * from mira.sources.amr import model_from_url from mira.sources.amr import petrinet from mira.sources.amr import regnet from mira.sources.amr import stockflow from mira.modeling.amr.regnet import template_model_to_regnet_json +from mira.modeling.amr.petrinet import template_model_to_petrinet_json +from mira.modeling.amr.stockflow import template_model_to_stockflow_json petrinet_example = 'https://raw.githubusercontent.com/DARPA-ASKEM/' \ - 'Model-Representations/main/petrinet/examples/sir.json' + 'Model-Representations/main/petrinet/examples/sir.json' regnet_example = 'https://raw.githubusercontent.com/DARPA-ASKEM/' \ - 'Model-Representations/main/regnet/examples/lotka_volterra.json' + 'Model-Representations/main/regnet/examples/lotka_volterra.json' stockflow_example = 'https://raw.githubusercontent.com/DARPA-ASKEM/' \ - 'Model-Representations/7f5e377225675259baa6486c64102f559edfd79f/stockflow/examples/sir.json' + 'Model-Representations/7f5e377225675259baa6486c64102f559edfd79f/stockflow/examples/sir.json' + +template_model = TemplateModel( + templates=[ + ControlledReplication( + name='replication', + controller=Concept(name='A'), + subject=Concept(name='B'), + rate_law=SympyExprStr(sympy.sympify('k * A * B / (1 + B)')) + ), + NaturalDegradation( + name='degradation', + subject=Concept(name='B'), + rate_law=SympyExprStr(sympy.sympify('k * B')) + ) + ], + observables={ + 'obs1': Observable( + name='obs1', + expression=SympyExprStr(sympy.sympify('A + B')), + display_name='obs1' + ) + }, + annotations=Annotations( + time_start="2020-03-01T00:00:00", + time_end="2020-08-01T00:00:00" + ), + time=Time(name='timexx'), +) def stockflow_set_up_file(): @@ -76,21 +109,25 @@ def test_stockflow_flow_to_template(): def test_stockflow_parameter_to_mira(): sfamr = stockflow_set_up_file() tm = stockflow.model_from_url(stockflow_example) - for amr_param, tm_param in zip(sfamr['semantics']['ode']['parameters'], tm.parameters.values()): + for amr_param, tm_param in zip(sfamr['semantics']['ode']['parameters'], + tm.parameters.values()): assert amr_param['id'] == tm_param.name assert amr_param['name'] == tm_param.display_name assert amr_param['description'] == tm_param.description assert amr_param['value'] == tm_param.value - assert amr_param.get('units', {}) == (tm_param.units if tm_param.units else {}) + assert amr_param.get('units', {}) == ( + tm_param.units if tm_param.units else {}) def test_stockflow_initial_to_mira(): sfamr = stockflow_set_up_file() tm = stockflow.model_from_url(stockflow_example) - for amr_initial, tm_initial in zip(sfamr['semantics']['ode']['initials'], tm.initials.values()): + for amr_initial, tm_initial in zip(sfamr['semantics']['ode']['initials'], + tm.initials.values()): assert amr_initial['target'] == tm_initial.concept.name assert amr_initial['expression'] == str(tm_initial.expression) - assert amr_initial['expression_mathml'] == expression_to_mathml(tm_initial.expression) + assert amr_initial['expression_mathml'] == expression_to_mathml( + tm_initial.expression) def test_stockflow_stock_to_concept(): @@ -118,33 +155,22 @@ def test_regnet_rate_laws(): # Make a simple template model with rate laws, then export # into AMR, ingest, then make sure we get proper rate laws back # out. - template_model = TemplateModel( - templates=[ - ControlledReplication( - name='replication', - controller=Concept(name='A'), - subject=Concept(name='B'), - rate_law=SympyExprStr(sympy.sympify('k * A * B / (1 + B)')) - ), - NaturalDegradation( - name='degradation', - subject=Concept(name='B'), - rate_law=SympyExprStr(sympy.sympify('k * B')) - ) - ], - observables={ - 'obs1': Observable( - name='obs1', - expression=SympyExprStr(sympy.sympify('A + B')), - display_name='obs1' - ) - }, - time=Time(name='timexx') - ) amr_json = template_model_to_regnet_json(template_model) tm = regnet.template_model_from_amr_json(amr_json) assert isinstance(tm.templates[0].rate_law, SympyExprStr) assert isinstance(tm.templates[1].rate_law, SympyExprStr) - assert tm.templates[1].rate_law.args[0].equals(sympy.sympify('k * A * B / (1 + B)')) + assert tm.templates[1].rate_law.args[0].equals( + sympy.sympify('k * A * B / (1 + B)')) assert tm.time.name == 'timexx' - assert isinstance(tm.observables['obs1'].expression, SympyExprStr) \ No newline at end of file + assert isinstance(tm.observables['obs1'].expression, SympyExprStr) + + +def test_serialization(): + """Test to see if we can serialize template models that contain + datetime in their annotations into different frameworks""" + amrs = [template_model_to_regnet_json(template_model), + template_model_to_petrinet_json(template_model), + template_model_to_stockflow_json(template_model) + ] + for amr in amrs: + json.dumps(amr)