From ce0d29a2d220bb47d0be931f8c599824f7d4c103 Mon Sep 17 00:00:00 2001 From: Ben Gyori Date: Thu, 5 Sep 2024 10:07:47 -0400 Subject: [PATCH 1/8] Use expressions for distribution parameters --- mira/metamodel/template_model.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mira/metamodel/template_model.py b/mira/metamodel/template_model.py index 67558e760..c5fe5f0ae 100644 --- a/mira/metamodel/template_model.py +++ b/mira/metamodel/template_model.py @@ -104,10 +104,14 @@ class Distribution(BaseModel): """A distribution of values for a parameter.""" type: str = Field( - description="The type of distribution, e.g. 'uniform', 'normal', etc." + description="The type of distribution as provided by ProbOnto " + "e.g. 'StandardUniform1', 'Beta1', etc." ) - parameters: Dict[str, float] = Field( - description="The parameters of the distribution." + parameters: Dict[str, SympyExprStr] = Field( + description="The parameters of the distribution keyed by parameter names " + "controlled by ProbOnto and values that are expressions " + "and contain numerical values or expressions over other " + "parameters." ) From 3a4091b241e266f03567bf1bb05a0120abcf46e2 Mon Sep 17 00:00:00 2001 From: Ben Gyori Date: Thu, 5 Sep 2024 10:20:22 -0400 Subject: [PATCH 2/8] Add a test for distributions with expressions --- tests/test_distributions.py | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/test_distributions.py diff --git a/tests/test_distributions.py b/tests/test_distributions.py new file mode 100644 index 000000000..28d83175c --- /dev/null +++ b/tests/test_distributions.py @@ -0,0 +1,51 @@ +import sympy + +from mira.metamodel import * +from mira.modeling import Model +from mira.modeling.amr.petrinet import template_model_to_petrinet_json + + +def test_distribution_expressions(): + beta_mean = Parameter(name='beta_mean', + distribution=Distribution(type="Beta1", + parameters={'alpha': sympy.Integer(1), + 'beta': sympy.Integer(10)})) + gamma_mean = Parameter(name='gamma_mean', + distribution=Distribution(type="Beta1", + parameters={'alpha': sympy.Integer(10), + 'beta': sympy.Integer(10)})) + beta = Parameter(name='beta', + distribution=Distribution(type="InverseGamma1", + parameters={'shape': sympy.Symbol('beta_mean'), + 'scale': sympy.Float(0.01)})) + gamma = Parameter(name='gamma', + distribution=Distribution(type="InverseGamma1", + parameters={'shape': sympy.Symbol('gamma_mean'), + 'scale': sympy.Float(0.01)})) + + # Make an SIR model with beta and gamma in rate laws + sir_model = TemplateModel( + templates=[ + ControlledConversion( + subject=Concept(name='S'), + outcome=Concept(name='I'), + controller=Concept(name='I'), + rate_law=sympy.Symbol('S') * sympy.Symbol('I') * sympy.Symbol('beta') + ), + NaturalConversion( + subject=Concept(name='I'), + outcome=Concept(name='R'), + rate_law=sympy.Symbol('I') * sympy.Symbol('gamma') + ), + ], + parameters={ + 'beta': beta, + 'gamma': gamma, + 'beta_mean': beta_mean, + 'gamma_mean': gamma_mean, + } + ) + + model = Model(sir_model) + pn_json = template_model_to_petrinet_json(sir_model) + print(pn_json) From 69f54f171e4e6dd5ba3ba8852c04671dae26bb38 Mon Sep 17 00:00:00 2001 From: Ben Gyori Date: Tue, 10 Sep 2024 12:26:05 -0400 Subject: [PATCH 3/8] Process distribution expressions from sources --- mira/metamodel/template_model.py | 6 +++--- mira/sources/amr/petrinet.py | 13 +++++++------ mira/sources/amr/regnet.py | 18 ++++++------------ mira/sources/amr/stockflow.py | 10 +++++++--- mira/sources/util.py | 28 ++++++++++++++++++++++------ 5 files changed, 45 insertions(+), 30 deletions(-) diff --git a/mira/metamodel/template_model.py b/mira/metamodel/template_model.py index c5fe5f0ae..a6fa55bd9 100644 --- a/mira/metamodel/template_model.py +++ b/mira/metamodel/template_model.py @@ -107,10 +107,10 @@ class Distribution(BaseModel): description="The type of distribution as provided by ProbOnto " "e.g. 'StandardUniform1', 'Beta1', etc." ) - parameters: Dict[str, SympyExprStr] = Field( + parameters: Dict[str, Union[float, SympyExprStr]] = Field( description="The parameters of the distribution keyed by parameter names " - "controlled by ProbOnto and values that are expressions " - "and contain numerical values or expressions over other " + "controlled by ProbOnto and values that are either floating" + "point values or symbolic expressions over other " "parameters." ) diff --git a/mira/sources/amr/petrinet.py b/mira/sources/amr/petrinet.py index 970da47b2..2c4c3d244 100644 --- a/mira/sources/amr/petrinet.py +++ b/mira/sources/amr/petrinet.py @@ -109,15 +109,16 @@ def template_model_from_amr_json(model_json) -> TemplateModel: # } ode_semantics = model_json.get("semantics", {}).get("ode", {}) symbols = {state_id: sympy.Symbol(state_id) for state_id in concepts} - mira_parameters = {} + + # We first make symbols for all the parameters for parameter in ode_semantics.get('parameters', []): - mira_parameters[parameter['id']] = parameter_to_mira(parameter) symbols[parameter['id']] = sympy.Symbol(parameter['id']) - param_values = { - p['id']: p['value'] for p in ode_semantics.get('parameters', []) - if p.get('value') is not None - } + # We then process the parameters into MIRA Parameter objects + mira_parameters = {} + for parameter in ode_semantics.get('parameters', []): + mira_parameters[parameter['id']] = \ + parameter_to_mira(parameter, param_symbols=symbols) # Next we process initial conditions initials = {} diff --git a/mira/sources/amr/regnet.py b/mira/sources/amr/regnet.py index ed6646823..60f05919d 100644 --- a/mira/sources/amr/regnet.py +++ b/mira/sources/amr/regnet.py @@ -10,7 +10,7 @@ import requests from mira.metamodel import * -from mira.sources.util import get_sympy +from mira.sources.util import get_sympy, parameter_to_mira def model_from_url(url: str) -> TemplateModel: @@ -83,11 +83,14 @@ def template_model_from_amr_json(model_json) -> TemplateModel: # Next, we capture all symbols in the model, including states and # parameters. We also extract parameters at this point. symbols = {state_id: sympy.Symbol(state_id) for state_id in concepts} - mira_parameters = {} for parameter in model.get('parameters', []): - mira_parameters[parameter['id']] = parameter_to_mira(parameter) symbols[parameter['id']] = sympy.Symbol(parameter['id']) + mira_parameters = {} + for parameter in model.get('parameters', []): + mira_parameters[parameter['id']] = \ + parameter_to_mira(parameter, param_symbols=symbols) + # Next we process any intrinsic positive/negative growth # at the vertex level into templates templates = [] @@ -213,12 +216,3 @@ def vertex_to_template(vertex, concept): template = NaturalDegradation(subject=concept) template.set_mass_action_rate_law(rate_constant) return template - - -def parameter_to_mira(parameter): - """Return a MIRA parameter from a parameter""" - distr = Distribution(**parameter['distribution']) \ - if parameter.get('distribution') else None - return Parameter(name=parameter['id'], - value=parameter.get('value'), - distribution=distr) diff --git a/mira/sources/amr/stockflow.py b/mira/sources/amr/stockflow.py index 169b428c2..c3ca7cb4b 100644 --- a/mira/sources/amr/stockflow.py +++ b/mira/sources/amr/stockflow.py @@ -42,13 +42,17 @@ def template_model_from_amr_json(model_json) -> TemplateModel: all_stocks.add(stock['id']) symbols[stock['id']] = sympy.Symbol(stock['id']) - # Process parameters + # Process parameters, first to get all symbols, then + # processing the parameters to get the MIRA parameters ode_semantics = model_json.get("semantics", {}).get("ode", {}) - mira_parameters = {} for parameter in ode_semantics.get('parameters', []): - mira_parameters[parameter['id']] = parameter_to_mira(parameter) symbols[parameter['id']] = sympy.Symbol(parameter['id']) + mira_parameters = {} + for parameter in ode_semantics.get('parameters', []): + mira_parameters[parameter['id']] = \ + parameter_to_mira(parameter, param_symbols=symbols) + # Process auxiliaries aux_expressions = {} for auxiliary in auxiliaries: diff --git a/mira/sources/util.py b/mira/sources/util.py index 221d93b59..6be5a3cf2 100644 --- a/mira/sources/util.py +++ b/mira/sources/util.py @@ -161,7 +161,7 @@ def transition_to_templates( ) -def parameter_to_mira(parameter) -> Parameter: +def parameter_to_mira(parameter, param_symbols=None) -> Parameter: """ Return a MIRA parameter from a mapping of MIRA Parameter attributes to values. @@ -170,17 +170,33 @@ def parameter_to_mira(parameter) -> Parameter: ---------- parameter : Dict[str,Any] A mapping containing MIRA Parameter attributes to values. + param_symbols : Optional[Dict] + An optional dict of all parameter symbols that are needed if expressions + are used in parameter distributions so that these can be + recognized as symbols. Returns ------- : The corresponding MIRA Parameter. """ - distr = ( - Distribution(**parameter["distribution"]) - if parameter.get("distribution") - else None - ) + distr_json = parameter.get("distribution") + if distr_json: + distr_type = distr_json.get("type") if distr_json else None + # We need to check for symbolic expressions in parameters + processed_distr_parameters = {} + for param_key, param_value in distr_json.get("parameters", {}).items(): + if isinstance(param_value, float): + processed_distr_parameters[param_key] = param_value + else: + processed_distr_parameters[param_key] = \ + safe_parse_expr(param_value) + distr = Distribution( + type=distr_type, + parameters=processed_distr_parameters, + ) + else: + distr = None data = { "name": parameter["id"], "display_name": parameter.get("name"), From 34581fd01730329f9e2db34fabe54b6c6179c0a5 Mon Sep 17 00:00:00 2001 From: Ben Gyori Date: Tue, 10 Sep 2024 12:31:34 -0400 Subject: [PATCH 4/8] Implement distribution serialization in Petrinet AMR --- mira/modeling/amr/petrinet.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mira/modeling/amr/petrinet.py b/mira/modeling/amr/petrinet.py index 71d3812f7..55244e84e 100644 --- a/mira/modeling/amr/petrinet.py +++ b/mira/modeling/amr/petrinet.py @@ -12,7 +12,7 @@ from pydantic import BaseModel, Field -from mira.metamodel import expression_to_mathml, TemplateModel +from mira.metamodel import expression_to_mathml, TemplateModel, SympyExprStr from mira.sources.amr import sanity_check_amr from .. import Model @@ -188,9 +188,16 @@ def __init__(self, model: Model): elif param.distribution.type is None: logger.warning("can not add distribution without type: %s", param.distribution) else: + serialized_distr_parameters = {} + for param_key, param_value in param.distribution.parameters.items(): + if isinstance(param_value, SympyExprStr): + serialized_distr_parameters[param_key] = \ + str(param_value.args[0]) + else: + serialized_distr_parameters[param_key] = param_value param_dict['distribution'] = { 'type': param.distribution.type, - 'parameters': param.distribution.parameters, + 'parameters': serialized_distr_parameters, } if param.concept and param.concept.units: param_dict['units'] = { From fd41e51fd8aed8e7467c3a11ea2222315fce7fbd Mon Sep 17 00:00:00 2001 From: Ben Gyori Date: Tue, 10 Sep 2024 12:40:24 -0400 Subject: [PATCH 5/8] Add serialization and deserialization test --- tests/test_distributions.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_distributions.py b/tests/test_distributions.py index 28d83175c..8a26400c0 100644 --- a/tests/test_distributions.py +++ b/tests/test_distributions.py @@ -3,6 +3,7 @@ from mira.metamodel import * from mira.modeling import Model from mira.modeling.amr.petrinet import template_model_to_petrinet_json +from mira.sources.amr import model_from_json def test_distribution_expressions(): @@ -48,4 +49,17 @@ def test_distribution_expressions(): model = Model(sir_model) pn_json = template_model_to_petrinet_json(sir_model) - print(pn_json) + params = pn_json['semantics']['ode']['parameters'] + assert {p['id'] for p in params} == \ + {'beta_mean', 'gamma_mean', 'beta', 'gamma'} + beta = [p for p in params if p['id'] == 'beta'][0] + assert beta['distribution']['type'] == 'InverseGamma1' + assert beta['distribution']['parameters']['shape'] == 'beta_mean' + + # Now read the model back and check if it is deserialized + tm = model_from_json(pn_json) + assert tm.parameters['beta'].distribution.type == 'InverseGamma1' + assert isinstance(tm.parameters['beta'].distribution.parameters['shape'], + SympyExprStr) + assert tm.parameters['beta'].distribution.parameters['shape'].args[0] == \ + sympy.Symbol('beta_mean') From ca87a351c0654d4f9eb463d923e47fb501b7e60f Mon Sep 17 00:00:00 2001 From: Ben Gyori Date: Tue, 10 Sep 2024 12:50:05 -0400 Subject: [PATCH 6/8] Update metamodel schema --- mira/metamodel/schema.json | 14 +++++++++++--- mira/metamodel/template_model.py | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/mira/metamodel/schema.json b/mira/metamodel/schema.json index e07d1a209..ec696d034 100644 --- a/mira/metamodel/schema.json +++ b/mira/metamodel/schema.json @@ -946,15 +946,23 @@ "properties": { "type": { "title": "Type", - "description": "The type of distribution, e.g. 'uniform', 'normal', etc.", + "description": "The type of distribution as provided by ProbOnto e.g. 'StandardUniform1', 'Beta1', etc.", "type": "string" }, "parameters": { "title": "Parameters", - "description": "The parameters of the distribution.", + "description": "The parameters of the distribution keyed by parameter names controlled by ProbOnto and values that are either floatingpoint values or symbolic expressions over other parameters.", "type": "object", "additionalProperties": { - "type": "number" + "anyOf": [ + { + "type": "number" + }, + { + "type": "string", + "example": "2*x" + } + ] } } }, diff --git a/mira/metamodel/template_model.py b/mira/metamodel/template_model.py index a6fa55bd9..3ceb852b8 100644 --- a/mira/metamodel/template_model.py +++ b/mira/metamodel/template_model.py @@ -109,7 +109,7 @@ class Distribution(BaseModel): ) parameters: Dict[str, Union[float, SympyExprStr]] = Field( description="The parameters of the distribution keyed by parameter names " - "controlled by ProbOnto and values that are either floating" + "controlled by ProbOnto and values that are either floating " "point values or symbolic expressions over other " "parameters." ) From a110e3c80a1c348c7ade779734c23901f7a51e90 Mon Sep 17 00:00:00 2001 From: Ben Gyori Date: Tue, 10 Sep 2024 13:10:32 -0400 Subject: [PATCH 7/8] Update schema description --- mira/metamodel/schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mira/metamodel/schema.json b/mira/metamodel/schema.json index ec696d034..e15eb25df 100644 --- a/mira/metamodel/schema.json +++ b/mira/metamodel/schema.json @@ -951,7 +951,7 @@ }, "parameters": { "title": "Parameters", - "description": "The parameters of the distribution keyed by parameter names controlled by ProbOnto and values that are either floatingpoint values or symbolic expressions over other parameters.", + "description": "The parameters of the distribution keyed by parameter names controlled by ProbOnto and values that are either floating point values or symbolic expressions over other parameters.", "type": "object", "additionalProperties": { "anyOf": [ From fa8e73080f612c9ffb5326d2eabaf3b5ff6d8b1e Mon Sep 17 00:00:00 2001 From: Ben Gyori Date: Tue, 10 Sep 2024 13:14:26 -0400 Subject: [PATCH 8/8] Update REST URL --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4eaa3470c..6766ab2f5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -22,7 +22,7 @@ jobs: pip install "tox<4.0.0" - name: Test with pytest run: | - export MIRA_REST_URL=http://34.230.33.149:8771 + export MIRA_REST_URL=http://mira-epi-dkg-lb-c7b58edea41524e6.elb.us-east-1.amazonaws.com:8771 tox -e py # - name: Upload coverage report to codecov # uses: codecov/codecov-action@v1