Skip to content

Commit

Permalink
Add demo notebook on observable patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
bgyori committed Sep 18, 2024
1 parent 8bdf70b commit 83dd6f3
Showing 1 changed file with 178 additions and 0 deletions.
178 changes: 178 additions & 0 deletions notebooks/observable_patterns.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "99eee62b",
"metadata": {},
"source": [
"# Adding model observables by pattern\n",
"\n",
"This notebook shows how we can construct model observables using patterns of concepts to include in an observable."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b660af76",
"metadata": {},
"outputs": [],
"source": [
"from mira.metamodel import *\n",
"from mira.modeling import Model\n",
"from mira.modeling.viz import GraphicalModel"
]
},
{
"cell_type": "markdown",
"id": "3badc26e",
"metadata": {},
"source": [
"We start with a standard SIR model using standard identifiers for the three compartments."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9bc1a327",
"metadata": {},
"outputs": [],
"source": [
"infected = Concept(name='infected population', identifiers={'ido': '0000511'})\n",
"susceptible = Concept(name='susceptible population', identifiers={'ido': '0000514'})\n",
"immune = Concept(name='immune population', identifiers={'ido': '0000592'})\n",
"\n",
"t1 = ControlledConversion(\n",
" controller=infected,\n",
" subject=susceptible,\n",
" outcome=infected,\n",
")\n",
"t2 = NaturalConversion(subject=infected, outcome=immune)\n",
"template_model = TemplateModel(templates=[t1, t2])"
]
},
{
"cell_type": "markdown",
"id": "948a2d99",
"metadata": {},
"source": [
"We now stratify the model into two cities."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "89ee6980",
"metadata": {},
"outputs": [],
"source": [
"model_2_city = stratify(\n",
" template_model,\n",
" key=\"city\",\n",
" strata=[\n",
" \"geonames:5128581\", # NYC\n",
" \"geonames:4930956\", # Boston\n",
" ],\n",
")\n",
"\n",
"#GraphicalModel.for_jupyter(model_2_city)"
]
},
{
"cell_type": "markdown",
"id": "baa72e45",
"metadata": {},
"source": [
"First, let's create an observable for all susceptible people. Note that an alternative to achieving this would have been to add a susceptible observable before stratification. The stratification would then correctly rewrite the observable expression to reflect changes upon stratification."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c8e84dc8",
"metadata": {},
"outputs": [],
"source": [
"add_observable_pattern(model_2_city,\n",
" 'susceptible',\n",
" identifiers={'ido': '0000511'})"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2acb7d2b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"infected population_geonames_4930956 + infected population_geonames_5128581\n"
]
}
],
"source": [
"print(model_2_city.observables['susceptible'].expression.args[0])"
]
},
{
"cell_type": "markdown",
"id": "b72dfc50",
"metadata": {},
"source": [
"We can also refer to a pattern just based on context to identify compartments to constructn an obserable. For instance, we can add an observable for all people in Boston irrespective of disease state, as follows."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "62fa02f0",
"metadata": {},
"outputs": [],
"source": [
"add_observable_pattern(model_2_city,\n",
" 'Boston',\n",
" context={'city': 'geonames:4930956'})"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "756e6fdf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"immune population_geonames_4930956 + infected population_geonames_4930956 + susceptible population_geonames_4930956\n"
]
}
],
"source": [
"print(model_2_city.observables['Boston'].expression.args[0])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 83dd6f3

Please sign in to comment.