From 15922532dbc45b605acba267ce39cf283c5efb7c Mon Sep 17 00:00:00 2001 From: Jeremy Zucker Date: Wed, 5 Jul 2023 23:57:55 -0400 Subject: [PATCH] Blocked on https://github.com/indralab/mira/issues/189 but tests pass by making compile_rate_law_p False by default --- .../Symbolic_Deriv_Experiments.ipynb | 149 ++++++++++++------ src/pyciemss/PetriNetODE/interfaces.py | 2 +- 2 files changed, 101 insertions(+), 50 deletions(-) diff --git a/notebook/Examples_for_TA2_Model_Representation/Symbolic_Deriv_Experiments.ipynb b/notebook/Examples_for_TA2_Model_Representation/Symbolic_Deriv_Experiments.ipynb index 994176c0a..5688aa362 100644 --- a/notebook/Examples_for_TA2_Model_Representation/Symbolic_Deriv_Experiments.ipynb +++ b/notebook/Examples_for_TA2_Model_Representation/Symbolic_Deriv_Experiments.ipynb @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "8d9914c4", "metadata": {}, "outputs": [], @@ -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", @@ -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", @@ -110,7 +110,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 11, "id": "52336ec5-589e-4694-8d23-6c2a0f460471", "metadata": {}, "outputs": [ @@ -124,7 +124,7 @@ ")" ] }, - "execution_count": 4, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -143,7 +143,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 12, "id": "e6ba1615-df53-49f3-9156-fd1dd5281075", "metadata": {}, "outputs": [], @@ -153,7 +153,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 13, "id": "0df72036-a0e7-469b-a693-c9a002095ed0", "metadata": {}, "outputs": [], @@ -171,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 14, "id": "4ddd5b20-ac09-4cd4-990a-1ac740e4f017", "metadata": {}, "outputs": [], @@ -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': ,\n", - " 'gamma': }" + "OrderedDict([('I', ),\n", + " ('recovered_population', ),\n", + " ('susceptible_population',\n", + " )])" ] }, - "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": [], @@ -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" } @@ -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" ] @@ -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" } @@ -291,7 +293,7 @@ "\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", @@ -299,7 +301,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'])" ] }, @@ -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" } @@ -334,7 +336,7 @@ "\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", @@ -342,7 +344,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'])" ] }, @@ -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" } @@ -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'])" ] }, @@ -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" } @@ -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" } @@ -479,27 +481,76 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "3fb6cd74", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "OrderedDict([('Infected', ),\n", + " ('Recovered', ),\n", + " ('Susceptible', )])" + ] + }, + "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": { diff --git a/src/pyciemss/PetriNetODE/interfaces.py b/src/pyciemss/PetriNetODE/interfaces.py index ec0aa1ea0..e43261d10 100644 --- a/src/pyciemss/PetriNetODE/interfaces.py +++ b/src/pyciemss/PetriNetODE/interfaces.py @@ -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.