Skip to content

Commit

Permalink
intro tutorial: Remove unique_id from Agent init
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutH authored and tpike3 committed Sep 26, 2024
1 parent 7a4ce36 commit b316a41
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions docs/tutorials/intro_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
"\n",
"First, there are the required attributes for any GeoAgent in Mesa-Geo\n",
"\n",
"- **unique_id**: Some unique identifier, often an int, this ensure Mesa can keep track of each agent without confusion\n",
"- **model**: Model object class that we will build later, this is a pointer to the model instance so the agent can get information from the model as it behaves\n",
"- **geometry**: GIS geometric object in this case a GIS Point\n",
"- **crs**: A string describing the coordinate reference system the agent is using\n",
Expand Down Expand Up @@ -160,7 +159,6 @@
"\n",
" def __init__(\n",
" self,\n",
" unique_id,\n",
" model,\n",
" geometry,\n",
" crs,\n",
Expand All @@ -170,7 +168,7 @@
" recovery_rate,\n",
" death_risk\n",
" ):\n",
" super().__init__(unique_id, model, geometry, crs)\n",
" super().__init__(model, geometry, crs)\n",
" # Agent attributes\n",
" self.atype = agent_type\n",
" self.mobility_range = mobility_range\n",
Expand All @@ -179,7 +177,7 @@
" self.death_risk = death_risk\n",
"\n",
" def __repr__(self):\n",
" return \"Person \" + str(self.unique_id)\n",
" return f\"Person {self.unique_id}\"\n",
"\n",
" def step(self): \n",
" print (repr(self))\n",
Expand Down Expand Up @@ -213,7 +211,6 @@
"\n",
"The required attributes for any GeoAgent in Mesa-Geo:\n",
"\n",
"- **unique_id**: For geographic agents such as a neighborhood mesa-geo will assign a very large integer as the agent id, if desired users can specify their own. \n",
"- **model**: Model object class that we will build later, this is a pointer to the model instance so the agent can get information from the model as it behaves\n",
"- **geometry**: GIS geometric object in this case a polygon form the geojson defining the perimeter of the neighborhood\n",
"- **crs**: A string describing the coordinate reference system the agent is using\n",
Expand Down Expand Up @@ -243,16 +240,16 @@
" \"\"\"Neighbourhood agent. Changes color according to number of infected inside it.\"\"\"\n",
"\n",
" def __init__(\n",
" self, unique_id, model, geometry, crs, agent_type=\"safe\", hotspot_threshold=1\n",
" self, model, geometry, crs, agent_type=\"safe\", hotspot_threshold=1\n",
" ):\n",
" super().__init__(unique_id, model, geometry, crs)\n",
" super().__init__(model, geometry, crs)\n",
" self.atype = agent_type\n",
" self.hotspot_threshold = (\n",
" hotspot_threshold # When a neighborhood is considered a hot-spot\n",
" )\n",
"\n",
" def __repr__(self):\n",
" return \"Neighbourhood \" + str(self.unique_id)\n",
" return f\"Neighbourhood {self.unique_id}\"\n",
" \n",
" def step(self):\n",
" \"\"\"Advance agent one step.\"\"\"\n",
Expand Down Expand Up @@ -281,7 +278,7 @@
},
"source": [
"\n",
"First, we name our class in this case GeoSIR and we inherit the model class from Mesa. We store the path to our GeoJSON file in the object geojson regions. As JSONs mirror Pythons dictionary structure, we store the key for the neighbourhood id (\"HOODNUM\") in the variable unique_id. \n",
"First, we name our class in this case GeoSIR and we inherit the model class from Mesa. We store the path to our GeoJSON file in the object geojson regions. As JSONs mirror Pythons dictionary structure, we store the key for the neighbourhood id (\"HOODNUM\") in the variable unique_id (TODO: Update).\n",
"\n",
"Second, we set up the python initializer to initiate our model class. To do this we will, set up key word arguments or kwargs of the parameters we want for our model. In this case we will use: \n",
"- population size (pop_size): An integer that determines the number of person agents\n",
Expand Down Expand Up @@ -335,7 +332,6 @@
"\n",
" # Geographical parameters for desired map\n",
" geojson_regions = \"data/TorontoNeighbourhoods.geojson\"\n",
" unique_id = \"HOODNUM\"\n",
"\n",
" def __init__(\n",
" self, pop_size=30, mobility_range=500, init_infection=0.2, exposure_dist=500, max_infection_risk=0.2,\n",
Expand All @@ -355,9 +351,8 @@
"\n",
" # Set up the Neighbourhood patches for every region in file\n",
" ac = mg.AgentCreator(NeighbourhoodAgent, model=self)\n",
" neighbourhood_agents = ac.from_file(\n",
" self.geojson_regions, unique_id=self.unique_id\n",
" )\n",
" neighbourhood_agents = ac.from_file(self.geojson_regions)\n",
" # TODO: Check if the HOODNUM still needed, and if so, is read correctly\n",
" \n",
" #Add neighbourhood agents to space\n",
" self.space.add_agents(neighbourhood_agents)\n",
Expand Down Expand Up @@ -399,9 +394,7 @@
" \n",
" x_home, y_home = self.find_home(neighbourhood_agents)\n",
" \n",
" this_person = unique_person.create_agent(\n",
" Point(x_home, y_home), \"P\" + str(i), \n",
" )\n",
" this_person = unique_person.create_agent(Point(x_home, y_home))\n",
" self.space.add_agents(this_person)\n",
" self.schedule.add(this_person)\n",
" \n",
Expand Down Expand Up @@ -537,7 +530,6 @@
"\n",
" def __init__(\n",
" self,\n",
" unique_id,\n",
" model,\n",
" geometry,\n",
" crs,\n",
Expand All @@ -547,7 +539,7 @@
" recovery_rate,\n",
" death_risk\n",
" ):\n",
" super().__init__(unique_id, model, geometry, crs)\n",
" super().__init__(model, geometry, crs)\n",
" # Agent attributes\n",
" self.atype = agent_type\n",
" self.mobility_range = mobility_range\n",
Expand All @@ -558,7 +550,7 @@
" self.steps_recovered = 0\n",
"\n",
" def __repr__(self):\n",
" return \"Person \" + str(self.unique_id)\n",
" return f\"Person {self.unique_id}\"\n",
"\n",
" #Helper function for moving agent\n",
" def move_point(self, dx, dy):\n",
Expand Down Expand Up @@ -646,16 +638,16 @@
" \"\"\"Neighbourhood agent. Changes color according to number of infected inside it.\"\"\"\n",
"\n",
" def __init__(\n",
" self, unique_id, model, geometry, crs, agent_type=\"safe\", hotspot_threshold=1\n",
" self, model, geometry, crs, agent_type=\"safe\", hotspot_threshold=1\n",
" ):\n",
" super().__init__(unique_id, model, geometry, crs)\n",
" super().__init__(model, geometry, crs)\n",
" self.atype = agent_type\n",
" self.hotspot_threshold = (\n",
" hotspot_threshold # When a neighborhood is considered a hot-spot\n",
" )\n",
"\n",
" def __repr__(self):\n",
" return \"Neighbourhood \" + str(self.unique_id)\n",
" return f\"Neighbourhood {self.unique_id}\"\n",
" \n",
" def color_hotspot(self):\n",
" # Decide if this region agent is a hot-spot\n",
Expand Down Expand Up @@ -766,7 +758,7 @@
"\n",
" # Geographical parameters for desired map\n",
" geojson_regions = \"data/TorontoNeighbourhoods.geojson\"\n",
" unique_id = \"HOODNUM\"\n",
" unique_id = \"HOODNUM\" # TODO: Either use or remove\n",
"\n",
" def __init__(\n",
" self, pop_size=30, mobility_range=500, init_infection=0.2, exposure_dist=500, max_infection_risk=0.2,\n",
Expand Down Expand Up @@ -803,9 +795,7 @@
" \n",
" # Set up the Neighbourhood patches for every region in file\n",
" ac = mg.AgentCreator(NeighbourhoodAgent, model=self)\n",
" neighbourhood_agents = ac.from_file(\n",
" self.geojson_regions, unique_id=self.unique_id\n",
" )\n",
" neighbourhood_agents = ac.from_file(self.geojson_regions)\n",
" \n",
" #Add neighbourhood agents to space\n",
" self.space.add_agents(neighbourhood_agents)\n",
Expand Down Expand Up @@ -847,9 +837,7 @@
" \n",
" x_home, y_home = self.find_home(neighbourhood_agents)\n",
" \n",
" this_person = unique_person.create_agent(\n",
" Point(x_home, y_home), \"P\" + str(i), \n",
" )\n",
" this_person = unique_person.create_agent(Point(x_home, y_home))\n",
" self.space.add_agents(this_person)\n",
" self.schedule.add(this_person)\n",
" \n",
Expand Down Expand Up @@ -896,7 +884,9 @@
" # Run until no one is infected\n",
" if self.counts[\"infected\"] == 0 :\n",
" self.running = False\n"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand Down

0 comments on commit b316a41

Please sign in to comment.