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

Eval sc1 initials #324

Merged
merged 3 commits into from
Apr 9, 2024
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
151 changes: 135 additions & 16 deletions notebooks/evaluation_2024.03/epi_scenario1/epi_eval_sc1_strains.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "fe970922-3aa4-40b5-9caf-4afc08cef04d",
"id": "b1d20fb5-6925-4bda-b652-f9281d8c5ba4",
"metadata": {},
"outputs": [
{
Expand All @@ -30,14 +30,37 @@
"source": [
"# Get an ready-made example SIR model that has parameters and initial values already set\n",
"# (note that the model is missing units)\n",
"from mira.examples.sir import sir_parameterized_init\n",
"from mira.examples.sir import sir_parameterized_init, sir_init_val_norm\n",
"from mira.metamodel import *\n",
"\n",
"susceptible = sir_parameterized_init.get_concepts_name_map()['susceptible_population']\n",
"infected = sir_parameterized_init.get_concepts_name_map()['infected_population']\n",
"recovered = sir_parameterized_init.get_concepts_name_map()['immune_population']\n",
"\n",
"sir_parameterized_init.draw_jupyter()\n"
"sir_parameterized_init.draw_jupyter()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "def6891b-4dac-4216-842f-705b8efdf36f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'susceptible_population': Initial(concept=Concept(name='susceptible_population', display_name=None, description=None, identifiers={'ido': '0000514'}, context={}, units=Unit(expression=person)), expression=99999.0),\n",
" 'infected_population': Initial(concept=Concept(name='infected_population', display_name=None, description=None, identifiers={'ido': '0000511'}, context={}, units=Unit(expression=person)), expression=1),\n",
" 'immune_population': Initial(concept=Concept(name='immune_population', display_name=None, description=None, identifiers={'ido': '0000592'}, context={}, units=Unit(expression=person)), expression=0)}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sir_parameterized_init.initials"
]
},
{
Expand All @@ -51,7 +74,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "745c1649-8e91-42f8-a931-766c51ed1f5e",
"metadata": {},
"outputs": [
Expand All @@ -62,7 +85,7 @@
"<IPython.core.display.Image object>"
]
},
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -77,11 +100,36 @@
" key='strain', \n",
" strata=strains, \n",
" concepts_to_preserve=['susceptible_population'], \n",
" structure=[]\n",
" structure=[],\n",
")\n",
"strain_model.draw_jupyter(\"sir.png\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "32077ecc-7cf4-4c49-a449-44b80844beaa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"susceptible_population 99999.0\n",
"infected_population_alpha 1/3\n",
"infected_population_delta 1/3\n",
"infected_population_omicron 1/3\n",
"immune_population_alpha 0\n",
"immune_population_delta 0\n",
"immune_population_omicron 0\n"
]
}
],
"source": [
"for name, init in strain_model.initials.items():\n",
" print(name, init.expression)"
]
},
{
"cell_type": "markdown",
"id": "e1a19ab7-6ba5-4d2f-9dd8-bc50532deb8c",
Expand All @@ -93,61 +141,93 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"id": "237f6485-1960-4127-bdb1-9e160bac74b6",
"metadata": {},
"outputs": [],
"source": [
"initials = {}\n",
"\n",
"# Reset the infected populations to 1 (they're set to 1/3 when stratified)\n",
"initials['susceptible_population'] = Initial(concept=Concept(name='susceptible_population'), expression=sir_init_val_norm - 3.0)\n",
"\n",
"name_map = strain_model.get_concepts_name_map()\n",
"\n",
"infected_alpha = strain_model.get_concepts_name_map()['infected_population_alpha']\n",
"initials[infected_alpha.name] = Initial(concept=Concept(name=infected_alpha.name), expression=1.0)\n",
"\n",
"infected_delta = strain_model.get_concepts_name_map()['infected_population_delta']\n",
"initials[infected_delta.name] = Initial(concept=Concept(name=infected_delta.name), expression=1.0)\n",
"\n",
"infected_omicron = strain_model.get_concepts_name_map()['infected_population_omicron']\n",
"initials[infected_omicron.name] = Initial(concept=Concept(name=infected_omicron.name), expression=1.0)\n",
"\n",
"recovered_alpha = strain_model.get_concepts_name_map()['immune_population_alpha']\n",
"recovered_delta = strain_model.get_concepts_name_map()['immune_population_delta']\n",
"recovered_omicron = strain_model.get_concepts_name_map()['immune_population_omicron']\n",
"\n",
"# Get reinfected states and reinfected recovered states\n",
"# Get reinfected states and reinfected recovered states, set all initials to zero\n",
"# alpha delta/delta alpha\n",
"infected_alpha_delta = infected.with_context(history='alpha', strain='delta')\n",
"infected_alpha_delta.name = 'infected_population_alpha_delta'\n",
"initials[infected_alpha_delta.name] = Initial(concept=Concept(name=infected_alpha_delta.name), expression=0.0)\n",
"\n",
"infected_delta_alpha = infected.with_context(history='delta', strain='alpha')\n",
"infected_delta_alpha.name = 'infected_population_delta_alpha'\n",
"initials[infected_delta_alpha.name] = Initial(concept=Concept(name=infected_delta_alpha.name), expression=0.0)\n",
"\n",
"recovered_alpha_delta = recovered.with_context(history='alpha_delta')\n",
"recovered_alpha_delta.name = 'immune_population_alpha_delta'\n",
"initials[recovered_alpha_delta.name] = Initial(concept=Concept(name=recovered_alpha_delta.name), expression=0.0)\n",
"\n",
"# alpha omicron/omicron alpha\n",
"infected_alpha_omicron = infected.with_context(history='alpha', strain='omicron')\n",
"infected_alpha_omicron.name = 'infected_population_alpha_omicron'\n",
"initials[infected_alpha_omicron.name] = Initial(concept=Concept(name=infected_alpha_omicron.name), expression=0.0)\n",
"\n",
"infected_omicron_alpha = infected.with_context(history='omicron', strain='alpha')\n",
"infected_omicron_alpha.name = 'infected_population_omicron_alpha'\n",
"initials[infected_omicron_alpha.name] = Initial(concept=Concept(name=infected_omicron_alpha.name), expression=0.0)\n",
"\n",
"recovered_alpha_omicron = recovered.with_context(history='alpha_omicron')\n",
"recovered_alpha_omicron.name = 'immune_population_alpha_omicron'\n",
"initials[recovered_alpha_omicron.name] = Initial(concept=Concept(name=recovered_alpha_omicron.name), expression=0.0)\n",
"\n",
"# delta omicron/omicron delta\n",
"infected_delta_omicron = infected.with_context(history='delta', strain='omicron')\n",
"infected_delta_omicron.name = 'infected_population_delta_omicron'\n",
"initials[infected_delta_omicron.name] = Initial(concept=Concept(name=infected_delta_omicron.name), expression=0.0)\n",
"\n",
"infected_omicron_delta = infected.with_context(history='omicron', strain='delta')\n",
"infected_omicron_delta.name = 'infected_population_omicron_delta'\n",
"initials[infected_omicron_delta.name] = Initial(concept=Concept(name=infected_omicron_delta.name), expression=0.0)\n",
"\n",
"recovered_delta_omicron = recovered.with_context(strain='delta_omicron')\n",
"recovered_delta_omicron.name = 'immune_population_delta_omicron'\n",
"initials[recovered_delta_omicron.name] = Initial(concept=Concept(name=recovered_delta_omicron.name), expression=0.0)\n",
"\n",
"# alpha delta omicron (third and final infection for any branch)\n",
"infected_alpha_delta_omicron = infected.with_context(history='alpha_delta', strain='omicron')\n",
"infected_alpha_delta_omicron.name = 'infected_population_alpha_delta_omicron'\n",
"initials[infected_alpha_delta_omicron.name] = Initial(concept=Concept(name=infected_alpha_delta_omicron.name), expression=0.0)\n",
"\n",
"infected_alpha_omicron_delta = infected.with_context(history='alpha_omicron', strain='delta')\n",
"infected_alpha_omicron_delta.name = 'infected_population_alpha_omicron_delta'\n",
"initials[infected_alpha_omicron_delta.name] = Initial(concept=Concept(name=infected_alpha_omicron_delta.name), expression=0.0)\n",
"\n",
"infected_delta_omicron_alpha = infected.with_context(history='delta_omicron', strain='alpha')\n",
"infected_delta_omicron_alpha.name = 'infected_population_delta_omicron_alpha'\n",
"initials[infected_delta_omicron_alpha.name] = Initial(concept=Concept(name=infected_delta_omicron_alpha.name), expression=0.0)\n",
"\n",
"recovered_alpha_delta_omicron = recovered.with_context(history='alpha_delta_omicron')\n",
"recovered_alpha_delta_omicron.name = 'immune_population_alpha_delta_omicron'"
"recovered_alpha_delta_omicron.name = 'immune_population_alpha_delta_omicron'\n",
"initials[recovered_alpha_delta_omicron.name] = Initial(concept=Concept(name=recovered_alpha_delta_omicron.name), expression=0.0)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "29221f5e-3b2b-425e-8186-1bb63f8aa54d",
"execution_count": 6,
"id": "2944a12c-f3ad-48a7-a5df-af4030fee31e",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -252,14 +332,53 @@
" new_parameters[param_name_rec] = Parameter(name=param_name, value=1)\n",
" new_templates.append(nc)\n",
"\n",
"extenstion_tm = TemplateModel(templates=new_templates, parameters=new_parameters)\n",
"extenstion_tm = TemplateModel(templates=new_templates, parameters=new_parameters, initials=initials)\n",
"\n",
"total_tm = strain_model.extend(extenstion_tm, parameter_mapping=new_parameters)"
"total_tm = strain_model.extend(extenstion_tm, parameter_mapping=new_parameters, initial_mapping=initials)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"id": "98642a20-9e02-4272-990b-5c3424713d75",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"susceptible_population 99997.0\n",
"infected_population_alpha 1.0\n",
"infected_population_delta 1.0\n",
"infected_population_omicron 1.0\n",
"immune_population_alpha 0\n",
"immune_population_delta 0\n",
"immune_population_omicron 0\n",
"infected_population_alpha_delta 0.0\n",
"immune_population_alpha_delta 0.0\n",
"infected_population_omicron_delta 0.0\n",
"infected_population_alpha_omicron_delta 0.0\n",
"immune_population_delta_omicron 0.0\n",
"immune_population_alpha_omicron 0.0\n",
"immune_population_alpha_delta_omicron 0.0\n",
"infected_population_delta_alpha 0.0\n",
"infected_population_omicron_alpha 0.0\n",
"infected_population_delta_omicron_alpha 0.0\n",
"infected_population_alpha_omicron 0.0\n",
"infected_population_delta_omicron 0.0\n",
"infected_population_alpha_delta_omicron 0.0\n"
]
}
],
"source": [
"for name in total_tm.get_concepts_name_map():\n",
" assert name in total_tm.initials\n",
" print(name, total_tm.initials[name].expression)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8e82cfe8-a12c-43fa-b62c-93e0c1b4afc1",
"metadata": {},
"outputs": [
Expand All @@ -270,7 +389,7 @@
"<IPython.core.display.Image object>"
]
},
"execution_count": 5,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -284,7 +403,7 @@
" ('petrinet', template_model_to_petrinet_json)\n",
"]:\n",
" with open(f\"sir_3strain_{out_type}.json\", 'w') as f:\n",
" json.dump(out_fun(total_tm), f, indent=2)\n",
" json.dump(out_fun(total_tm), f, indent=1)\n",
"total_tm.draw_jupyter(\"sir_3strain.png\")"
]
}
Expand Down
Loading
Loading