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

Import and Export Stock and Flow amrs into and from Mira template models. Refactor askenet -> amr #246

Merged
merged 15 commits into from
Oct 15, 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
29 changes: 21 additions & 8 deletions docs/source/modeling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ Modeling
:members:
:show-inheritance:

ASKEM AMR Petri net generation (:py:mod:`mira.modeling.askenet.petrinet`)
ASKEM AMR Petri net generation (:py:mod:`mira.modeling.amr.petrinet`)
-------------------------------------------------------------------------
.. automodule:: mira.modeling.askenet.petrinet
.. automodule:: mira.modeling.amr.petrinet
:members:
:show-inheritance:

ASKEM AMR operations (:py:mod:`mira.modeling.askenet.ops`)
ASKEM AMR Stockflow generation (:py:mod:`mira.modeling.amr.stockflow`)
-------------------------------------------------------------------------
.. automodule:: mira.modeling.amr.stockflow
:members:
:show-inheritance:

ASKEM AMR operations (:py:mod:`mira.modeling.amr.ops`)
----------------------------------------------------------
.. automodule:: mira.modeling.askenet.ops
.. automodule:: mira.modeling.amr.ops
:members:
:show-inheritance:

ASKEM AMR Regulatory net generation (:py:mod:`mira.modeling.askenet.regnet`)
ASKEM AMR Regulatory net generation (:py:mod:`mira.modeling.amr.regnet`)
----------------------------------------------------------------------------
.. automodule:: mira.modeling.askenet.regnet
.. automodule:: mira.modeling.amr.regnet
:members:
:show-inheritance:

Expand All @@ -34,12 +40,19 @@ ODE model generation and simulation (:py:mod:`mira.modeling.ode`)
:members:
:show-inheritance:

ACSets Petri net model generation (:py:mod:`mira.modeling.petri`)
ACSets Petri net model generation (:py:mod:`mira.modeling.acsets.petri`)
-----------------------------------------------------------------
.. automodule:: mira.modeling.petri
.. automodule:: mira.modeling.acsets.petri
:members:
:show-inheritance:

ACSets Stockflow model generation (:py:mod:`mira.modeling.acsets.stockflow`)
-----------------------------------------------------------------
.. automodule:: mira.modeling.acsets.stockflow
:members:
:show-inheritance:


Bilayer model generation (:py:mod:`mira.modeling.bilayer`)
----------------------------------------------------------
.. automodule:: mira.modeling.bilayer
Expand Down
33 changes: 23 additions & 10 deletions docs/source/sources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,34 @@ Sources of model content
:members:
:show-inheritance:

ASKEM AMR (:py:mod:`mira.sources.askenet`)
ASKEM AMR (:py:mod:`mira.sources.amr`)
------------------------------------------
.. automodule:: mira.sources.askenet
.. automodule:: mira.sources.amr
:members:
:show-inheritance:

ASKEM AMR Petri nets (:py:mod:`mira.sources.askenet.petrinet`)
ASKEM AMR Petri nets (:py:mod:`mira.sources.amr.petrinet`)
--------------------------------------------------------------
.. automodule:: mira.sources.askenet.petrinet
.. automodule:: mira.sources.amr.petrinet
:members:
:show-inheritance:

ASKEM AMR Regulatory nets (:py:mod:`mira.sources.askenet.regnet`)
ASKEM AMR Stockflow (:py:mod:`mira.sources.amr.stockflow`)
--------------------------------------------------------------
.. automodule:: mira.sources.amr.stockflow
:members:
:show-inheritance:


ASKEM AMR Regulatory nets (:py:mod:`mira.sources.amr.regnet`)
-----------------------------------------------------------------
.. automodule:: mira.sources.askenet.regnet
.. automodule:: mira.sources.amr.regnet
:members:
:show-inherita

Reconstruct ODE semantics (:py:mod:`mira.sources.askenet.flux_span`)
Reconstruct ODE semantics (:py:mod:`mira.sources.amr.flux_span`)
--------------------------------------------------------------------
.. automodule:: mira.sources.askenet.flux_span
.. automodule:: mira.sources.amr.flux_span
:members:
:show-inheritance:

Expand All @@ -46,8 +53,14 @@ Bilayer extraction (:py:mod:`mira.sources.bilayer`)
:members:
:show-inheritance:

ACSets Petri Net extraction (:py:mod:`mira.sources.petri`)
ACSets Petri Net extraction (:py:mod:`mira.sources.acsets.petri`)
----------------------------------------------------------
.. automodule:: mira.sources.acsets.petri
:members:
:show-inheritance:

ACSets Stockflow extraction (:py:mod:`mira.sources.acsets.stockflow`)
----------------------------------------------------------
.. automodule:: mira.sources.petri
.. automodule:: mira.sources.acsets.stockflow
:members:
:show-inheritance:
40 changes: 20 additions & 20 deletions mira/dkg/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
counts_to_dimensionless
)
from mira.modeling import Model
from mira.modeling.askenet.petrinet import AskeNetPetriNetModel, ModelSpecification
from mira.modeling.amr.petrinet import AMRPetriNetModel, ModelSpecification
from mira.modeling.bilayer import BilayerModel
from mira.modeling.acsets.petri import PetriNetModel, PetriNetResponse
from mira.modeling.viz import GraphicalModel
from mira.sources.askenet.flux_span import reproduce_ode_semantics, \
from mira.sources.amr.flux_span import reproduce_ode_semantics, \
test_file_path, docker_test_file_path
from mira.sources.askenet.petrinet import template_model_from_askenet_json
from mira.sources.amr.petrinet import template_model_from_amr_json
from mira.sources.bilayer import template_model_from_bilayer
from mira.sources.biomodels import get_sbml_model
from mira.sources.acsets.petri import template_model_from_petri_json
Expand Down Expand Up @@ -89,8 +89,8 @@

#: PetriNetModel json example
petrinet_json = PetriNetModel(Model(sir)).to_pydantic()
askenet_petrinet_json = AskeNetPetriNetModel(Model(sir)).to_pydantic()
askenet_petrinet_json_units_values = AskeNetPetriNetModel(
amr_petrinet_json = AMRPetriNetModel(Model(sir)).to_pydantic()
amr_petrinet_json_units_values = AMRPetriNetModel(
Model(sir_parameterized_init)
).to_pydantic()

Expand Down Expand Up @@ -148,12 +148,12 @@ def petri_to_model(petri_json: Dict[str, Any] = Body(..., example=petrinet_json)
consumed by other project members that implement this standard.
""".rstrip()),
)
def model_to_askenet(template_model: Dict[str, Any] = Body(..., example=template_model_example)):
"""Create an AskeNet Petri model from a TemplateModel."""
def model_to_amr(template_model: Dict[str, Any] = Body(..., example=template_model_example)):
"""Create an AMR Petri model from a TemplateModel."""
tm = TemplateModel.from_json(template_model)
model = Model(tm)
askenet_petrinet_model = AskeNetPetriNetModel(model)
return askenet_petrinet_model.to_pydantic()
amr_petrinet_model = AMRPetriNetModel(model)
return amr_petrinet_model.to_pydantic()


@model_blueprint.post(
Expand All @@ -167,9 +167,9 @@ def model_to_askenet(template_model: Dict[str, Any] = Body(..., example=template
with the MIRA ecosystem to do model extension, stratification, and comparison.
""".rstrip()),
)
def askenet_to_model(askenet_json: Dict[str, Any] = Body(..., example=askenet_petrinet_json)):
"""Create a TemplateModel from an AskeNet model."""
return template_model_from_askenet_json(askenet_json)
def amr_to_model(amr_json: Dict[str, Any] = Body(..., example=amr_petrinet_json)):
"""Create a TemplateModel from an AMR model."""
return template_model_from_amr_json(amr_json)


# Model stratification
Expand Down Expand Up @@ -330,7 +330,7 @@ def dimension_transform(
query: Dict[str, Any] = Body(
...,
example={
"model": askenet_petrinet_json_units_values,
"model": amr_petrinet_json_units_values,
"counts_units": "persons",
"norm_factor": 1e5,
},
Expand All @@ -339,13 +339,13 @@ def dimension_transform(
"""Convert all entity concentrations to dimensionless units"""
# convert to template model
amr_json = query.pop("model")
tm = template_model_from_askenet_json(amr_json)
tm = template_model_from_amr_json(amr_json)

# Create a dimensionless model
dimless_model = counts_to_dimensionless(tm=tm, **query)

# Transform back to askenet model
return AskeNetPetriNetModel(Model(dimless_model)).to_pydantic()
# Transform back to amr model
return AMRPetriNetModel(Model(dimless_model)).to_pydantic()


@model_blueprint.get(
Expand Down Expand Up @@ -668,15 +668,15 @@ def model_comparison(
class AMRComparisonQuery(BaseModel):
petrinet_models: List[Dict[str, Any]] = Field(
..., example=[ # fixme: create more examples
askenet_petrinet_json,
amr_petrinet_json,
]
)


@model_blueprint.post("/askenet_model_comparison",
response_model=ModelComparisonResponse,
tags=["modeling"],
description="This endpoint consumes a list of askenet "
description="This endpoint consumes a list of amr "
"petrinet JSON objects and returns "
"similarity scores and the data comparing "
"the models")
Expand All @@ -686,7 +686,7 @@ def askepetrinet_model_comparison(
):
"""Compare a list of models to each other"""
template_models = [
template_model_from_askenet_json(m) for m in query.petrinet_models
template_model_from_amr_json(m) for m in query.petrinet_models
]
graph_comparison_data = ModelComparisonGraphdata.from_template_models(
template_models,
Expand Down Expand Up @@ -724,5 +724,5 @@ def reproduce_ode_semantics_endpoint(
):
"""Reproduce ODE semantics from a stratified model (flux span)."""
tm = reproduce_ode_semantics(query.model)
am = AskeNetPetriNetModel(Model(tm))
am = AMRPetriNetModel(Model(tm))
return am.to_pydantic()
4 changes: 2 additions & 2 deletions mira/metamodel/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Config:

@classmethod
def from_json(cls, data: Dict[str, Any]) -> "Unit":
# Use get_sympy from askenet.petrinet, but avoid circular import
from mira.sources.askenet.petrinet import get_sympy
# Use get_sympy from amr.petrinet, but avoid circular import
from mira.sources.amr.petrinet import get_sympy
data["expression"] = get_sympy(data, local_dict=UNIT_SYMBOLS)
assert data.get('expression') is None or not isinstance(
data['expression'], str
Expand Down
4 changes: 2 additions & 2 deletions mira/modeling/acsets/stockflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from mira.metamodel import *


class AskeNetStockFlowModel:
class ACSetsStockFlowModel:

def __init__(self, model: Model):
self.properties = {}
Expand Down Expand Up @@ -81,4 +81,4 @@ def to_json(self):


def template_model_to_stockflow_ascet_json(tm: TemplateModel):
return AskeNetStockFlowModel(Model(tm)).to_json()
return ACSetsStockFlowModel(Model(tm)).to_json()
File renamed without changes.
19 changes: 9 additions & 10 deletions mira/modeling/askenet/ops.py → mira/modeling/amr/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sympy
from mira.metamodel import SympyExprStr, Unit
import mira.metamodel.ops as tmops
from mira.sources.askenet.petrinet import template_model_from_askenet_json
from mira.sources.amr.petrinet import template_model_from_amr_json
from .petrinet import template_model_to_petrinet_json
from mira.metamodel.io import mathml_to_expression
from mira.metamodel.template_model import Parameter, Distribution, Observable, \
Expand All @@ -14,7 +14,7 @@

def amr_to_mira(func):
def wrapper(amr, *args, **kwargs):
tm = template_model_from_askenet_json(amr)
tm = template_model_from_amr_json(amr)
result = func(tm, *args, **kwargs)
amr = template_model_to_petrinet_json(result)
return amr
Expand Down Expand Up @@ -60,7 +60,7 @@ def replace_transition_id(tm, old_id, new_id):

@amr_to_mira
def replace_observable_id(tm, old_id, new_id, name=None):
"""Replace the ID of an observable"""
"""Replace the ID and display name (optional) of an observable"""
for obs, observable in copy.deepcopy(tm.observables).items():
if obs == old_id:
observable.name = new_id
Expand All @@ -82,9 +82,8 @@ def remove_observable(tm, removed_id):
@amr_to_mira
def remove_parameter(tm, removed_id, replacement_value=None):
"""
If a replacement_value is supplied, substitute every instance of the parameter
in all expressions with the given replacement_value. If replacement_value is none,
substitute the parameter with 0.
Substitute every instance of the parameter with the given replacement_value.
If replacement_value is none, substitute the parameter with 0.
"""
if replacement_value:
tm.substitute_parameter(removed_id, replacement_value)
Expand Down Expand Up @@ -230,7 +229,7 @@ def add_transition(tm, new_transition_id, src_id=None, tgt_id=None,
Returns
-------
:
The updated template model
The updated template model object
"""
if src_id is None and tgt_id is None:
ValueError("You must pass in at least one of source and target id")
Expand Down Expand Up @@ -264,7 +263,7 @@ def replace_rate_law_sympy(tm, transition_id, new_rate_law: sympy.Expr):
# This function isn't wrapped because it calls a wrapped function and just
# passes the AMR through
def replace_rate_law_mathml(amr, transition_id, new_rate_law):
"""Replace the rate law of a transition. THe new rate law passed in will be a MathML str object"""
"""Replace the rate law of a transition. The new rate law passed in will be a MathML str object"""
new_rate_law_sympy = mathml_to_expression(new_rate_law)
return replace_rate_law_sympy(amr, transition_id, new_rate_law_sympy)

Expand All @@ -282,7 +281,7 @@ def replace_observable_expression_sympy(tm, obs_id,
@amr_to_mira
def replace_initial_expression_sympy(tm, initial_id,
new_expression_sympy: sympy.Expr):
"""Replace the expression of an initial. THe new rate law passed in will be a sympy.Expr object"""
"""Replace the expression of an initial. The new rate law passed in will be a sympy.Expr object"""
for init, initial in tm.initials.items():
if init == initial_id:
initial.expression = SympyExprStr(new_expression_sympy)
Expand All @@ -301,7 +300,7 @@ def replace_observable_expression_mathml(amr, obs_id, new_expression_mathml):
# This function isn't wrapped because it calls a wrapped function and just
# passes the AMR through
def replace_initial_expression_mathml(amr, initial_id, new_expression_mathml):
"""Replace the expression of an initial. THe new rate law passed in will be a MathML str object"""
"""Replace the expression of an initial. The new rate law passed in will be a MathML str object"""
new_expression_sympy = mathml_to_expression(new_expression_mathml)
return replace_initial_expression_sympy(amr, initial_id,
new_expression_sympy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
at https://github.com/DARPA-ASKEM/Model-Representations/tree/main/petrinet.
"""

__all__ = ["AskeNetPetriNetModel", "ModelSpecification",
__all__ = ["AMRPetriNetModel", "ModelSpecification",
"template_model_to_petrinet_json"]

import json
Expand All @@ -26,7 +26,7 @@
'petrinet_schema.json') % SCHEMA_VERSION


class AskeNetPetriNetModel:
class AMRPetriNetModel:
"""A class representing a PetriNet model."""

def __init__(self, model: Model):
Expand Down Expand Up @@ -291,7 +291,7 @@ def template_model_to_petrinet_json(tm: TemplateModel):
-------
A JSON dict representing the PetriNet model.
"""
return AskeNetPetriNetModel(Model(tm)).to_json()
return AMRPetriNetModel(Model(tm)).to_json()


class Initial(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
at https://github.com/DARPA-ASKEM/Model-Representations/tree/main/petrinet.
"""

__all__ = ["AskeNetRegNetModel", "ModelSpecification"]
__all__ = ["AMRRegNetModel", "ModelSpecification"]


import json
Expand All @@ -26,7 +26,7 @@
'regnet_schema.json') % SCHEMA_VERSION


class AskeNetRegNetModel:
class AMRRegNetModel:
"""A class representing a PetriNet model."""

def __init__(self, model: Model):
Expand Down
Loading
Loading