Skip to content

Commit

Permalink
Blocked on gyorilab/mira#189 but tests pass by making compile_rate_la…
Browse files Browse the repository at this point in the history
…w_p False by default
  • Loading branch information
djinnome committed Jul 6, 2023
1 parent 60b18b0 commit 1592253
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "8d9914c4",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -61,15 +61,15 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 10,
"id": "6f281623-e5dd-4a5f-84b9-c2a12b68dc92",
"metadata": {},
"outputs": [],
"source": [
"beta, gamma, S, I, R, total_population = sympy.symbols('beta, gamma, susceptible_population, infected_population, recovered_population, total_population')\n",
"beta, gamma, S, I, R, total_population = sympy.symbols('beta, gamma, susceptible_population, I, recovered_population, total_population')\n",
"\n",
"susceptible = Concept(name=\"susceptible_population\", identifiers={\"ido\": \"0000514\"})\n",
"infected = Concept(name=\"infected_population\", identifiers={\"ido\": \"0000573\"}) # http://purl.obolibrary.org/obo/IDO_0000573\n",
"infected = Concept(name=\"I\", identifiers={\"ido\": \"0000573\"}) # http://purl.obolibrary.org/obo/IDO_0000573\n",
"recovered = Concept(name=\"recovered_population\", identifiers={\"ido\": \"0000592\"})\n",
"total_pop = 100000\n",
"\n",
Expand All @@ -92,7 +92,7 @@
" },\n",
" initials={\n",
" 'susceptible_population': (Initial(concept=susceptible, value=(total_pop - 1))), \n",
" 'infected_population': (Initial(concept=infected, value=1)),\n",
" 'I': (Initial(concept=infected, value=1)),\n",
" 'recovered_population': (Initial(concept=recovered, value=0)),\n",
" }\n",
")\n",
Expand All @@ -110,7 +110,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 11,
"id": "52336ec5-589e-4694-8d23-6c2a0f460471",
"metadata": {},
"outputs": [
Expand All @@ -124,7 +124,7 @@
")"
]
},
"execution_count": 4,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -143,7 +143,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 12,
"id": "e6ba1615-df53-49f3-9156-fd1dd5281075",
"metadata": {},
"outputs": [],
Expand All @@ -153,7 +153,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 13,
"id": "0df72036-a0e7-469b-a693-c9a002095ed0",
"metadata": {},
"outputs": [],
Expand All @@ -171,7 +171,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 14,
"id": "4ddd5b20-ac09-4cd4-990a-1ac740e4f017",
"metadata": {},
"outputs": [],
Expand All @@ -185,29 +185,31 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "bf9ec456-0909-41f9-bdbf-9ba44a202717",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'beta': <mira.modeling.ModelParameter at 0x14dd4f880>,\n",
" 'gamma': <mira.modeling.ModelParameter at 0x14dd4f8e0>}"
"OrderedDict([('I', <mira.modeling.Variable at 0x10b5c61d0>),\n",
" ('recovered_population', <mira.modeling.Variable at 0x10b5c6320>),\n",
" ('susceptible_population',\n",
" <mira.modeling.Variable at 0x10b5c7e80>)])"
]
},
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sir.G.parameters"
"sir.var_order"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 15,
"id": "c19bd34d-3bad-4443-8f81-ed89ab891130",
"metadata": {},
"outputs": [],
Expand All @@ -231,17 +233,17 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 18,
"id": "3da6b243-792f-4cc1-bb53-30502aff8d87",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([0.5241, 0.1811])"
"tensor([0.5315, 0.2107])"
]
},
"execution_count": 10,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -255,7 +257,7 @@
"fluxes = flux(beta=getattr(sir, 'beta'),\n",
" gamma=getattr(sir,'gamma'),\n",
" susceptible_population=states['susceptible_population'],\n",
" infected_population=states['infected_population'],\n",
" I=states['I'],\n",
" recovered_population=states['recovered_population'])\n",
"fluxes"
]
Expand All @@ -270,17 +272,17 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 21,
"id": "b6f2d2a1-8d4a-4af2-8cac-29f2b673f7ff",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([-0.5241, 0.3430, 0.1811])"
"tensor([-0.5315, 0.3208, 0.2107])"
]
},
"execution_count": 11,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -291,15 +293,15 @@
"\n",
"\n",
"symbolic_derivs['susceptible_population'] = -beta*S*I/(S + I + R)\n",
"symbolic_derivs['infected_population'] = beta*S*I/(S + I + R) - gamma*I\n",
"symbolic_derivs['I'] = beta*S*I/(S + I + R) - gamma*I\n",
"symbolic_derivs['recovered_population'] = gamma*I\n",
"\n",
"\n",
"numeric_deriv = SymPyModule(expressions=list(symbolic_derivs.values()))\n",
"numeric_deriv(beta=getattr(sir, 'beta'),\n",
" gamma=getattr(sir,'gamma'),\n",
" susceptible_population=states['susceptible_population'],\n",
" infected_population=states['infected_population'],\n",
" I=states['I'],\n",
" recovered_population=states['recovered_population'])"
]
},
Expand All @@ -313,17 +315,17 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 23,
"id": "5496ada3-3d65-4219-a298-5ff160d90c58",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([-0.5241, 0.3430, 0.1811])"
"tensor([-0.5315, 0.3208, 0.2107])"
]
},
"execution_count": 12,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -334,15 +336,15 @@
"\n",
"\n",
"symbolic_derivs['susceptible_population'] = -extract_sympy(S_to_I.rate_law)\n",
"symbolic_derivs['infected_population'] = extract_sympy(S_to_I.rate_law) - extract_sympy(I_to_R.rate_law)\n",
"symbolic_derivs['I'] = extract_sympy(S_to_I.rate_law) - extract_sympy(I_to_R.rate_law)\n",
"symbolic_derivs['recovered_population'] = extract_sympy(I_to_R.rate_law)\n",
"\n",
"\n",
"numeric_deriv = SymPyModule(expressions=list(symbolic_derivs.values()))\n",
"numeric_deriv(beta=getattr(sir, 'beta'),\n",
" gamma=getattr(sir,'gamma'),\n",
" susceptible_population=states['susceptible_population'],\n",
" infected_population=states['infected_population'],\n",
" I=states['I'],\n",
" recovered_population=states['recovered_population'])"
]
},
Expand All @@ -356,17 +358,17 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 25,
"id": "9cf7dee8-a5a6-475c-a61e-7e4f508e1545",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([-0.5241, 0.3430, 0.1811])"
"tensor([-0.5315, 0.3208, 0.2107])"
]
},
"execution_count": 13,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -386,7 +388,7 @@
"numeric_deriv(beta=getattr(sir, 'beta'),\n",
" gamma=getattr(sir,'gamma'),\n",
" susceptible_population=states['susceptible_population'],\n",
" infected_population=states['infected_population'],\n",
" I=states['I'],\n",
" recovered_population=states['recovered_population'])"
]
},
Expand All @@ -400,17 +402,17 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 26,
"id": "ac4b97e0-3ee8-486e-8e53-d124459feea8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([-0.5241, 0.3430, 0.1811])"
"tensor([-0.5315, 0.3208, 0.2107])"
]
},
"execution_count": 14,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -446,17 +448,17 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 27,
"id": "4a66ec23-593c-4230-9407-89870c8be020",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([-0.5241, 0.3430, 0.1811])"
"tensor([-0.5315, 0.3208, 0.2107])"
]
},
"execution_count": 15,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -479,27 +481,76 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"id": "3fb6cd74",
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/plain": [
"OrderedDict([('Infected', <mira.modeling.Variable at 0x149af24a0>),\n",
" ('Recovered', <mira.modeling.Variable at 0x149af06d0>),\n",
" ('Susceptible', <mira.modeling.Variable at 0x149993460>)])"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sir = load_petri_model('https://raw.githubusercontent.com/DARPA-ASKEM/Model-Representations/main/petrinet/examples/sir_typed.json', compile_rate_law_p=True)\n",
"sir.var_order"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 31,
"id": "7db690a1",
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\operatorname{SympyExprStr}\\left(I S \\beta\\right)$"
],
"text/plain": [
"I*S*beta"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sir.G.template_model.templates[0].rate_law"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 44,
"id": "874647f5",
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/plain": [
"{'name': 'Susceptible',\n",
" 'identifiers': [('identity', 'ido:0000514')],\n",
" 'context': [],\n",
" 'initial_value': 1000.0}"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sir.G.variables[('Susceptible',\n",
" ('identity', 'ido:0000514'))].data\n"
]
}
],
"metadata": {
Expand Down
2 changes: 1 addition & 1 deletion src/pyciemss/PetriNetODE/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def load_petri_model(
petri_model_or_path: Union[str, mira.metamodel.TemplateModel, mira.modeling.Model],
add_uncertainty=True,
pseudocount=1.0,
compile_rate_law_p=False,
compile_rate_law_p=False
) -> PetriNetODESystem:
"""
Load a petri net from a file and compile it into a probabilistic program.
Expand Down

0 comments on commit 1592253

Please sign in to comment.