Skip to content

Commit

Permalink
maintain max_life_stage attr at the cell level rather than recalculat…
Browse files Browse the repository at this point in the history
…ing it wihtin the viz at
  • Loading branch information
GondekNP committed Jan 11, 2025
1 parent 09eac52 commit 721f187
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
15 changes: 8 additions & 7 deletions vegetation/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from ipyleaflet.leaflet import GeomanDrawControl

from mesa.visualization import Slider, SolaraViz, make_plot_component
from mesa_geo.visualization import make_geospace_component
from vegetation.patch.model import Vegetation, JoshuaTreeAgent
from vegetation.patch.space import VegCell
from vegetation.viz.simple_raster_map import make_simple_raster_geospace_component
from vegetation.viz.log_window import make_log_window_component

# Log window tabled for now... not working as expected at the moment
# from vegetation.viz.log_window import make_log_window_component

# from patch.management import init_tree_management_control
from vegetation.config.stages import LIFE_STAGE_RGB_VIZ_MAP
Expand Down Expand Up @@ -51,12 +52,12 @@ def cell_portrayal(agent):
# life stage of any Joshua Tree agent in the cell. If there are no agents,
# we color based on elevation.

patch_life_stages = [agent.life_stage for agent in agent.jotr_agents]
# patch_life_stages = [agent.life_stage for agent in agent.jotr_agents]

if len(patch_life_stages) > 0:
if agent.jotr_max_life_stage and agent.jotr_max_life_stage > 0:

max_stage = max(patch_life_stages)
rgba = LIFE_STAGE_RGB_VIZ_MAP[max_stage]
# max_stage = max(patch_life_stages)
rgba = LIFE_STAGE_RGB_VIZ_MAP[agent.jotr_max_life_stage]

else:
if not agent.refugia_status:
Expand Down Expand Up @@ -112,7 +113,7 @@ def cell_portrayal(agent):
make_plot_component(
["% Refugia Cells Occupied"],
),
make_log_window_component(),
# make_log_window_component(),
],
model_params=model_params,
)
Expand Down
8 changes: 2 additions & 6 deletions vegetation/patch/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ def __init__(self, model, geometry, crs, age=None, parent_id=None):

def step(self):

logger.info("Agent Step")

# Save initial life stage for logging
initial_life_stage = self.life_stage

Expand Down Expand Up @@ -140,7 +138,7 @@ def step(self):
)
n_seeds = poisson.rvs(jotr_breeding_poisson_lambda)

self.disperse_seeds(n_seeds)
self._disperse_seeds(n_seeds)

def _update_life_stage(self):

Expand All @@ -167,7 +165,7 @@ def _update_life_stage(self):
else:
return False

def disperse_seeds(
def _disperse_seeds(
self, n_seeds, max_dispersal_distance=JOTR_SEED_DISPERSAL_DISTANCE
):
if self.life_stage != LifeStage.BREEDING:
Expand Down Expand Up @@ -350,8 +348,6 @@ def update_metrics(self):

def step(self):

logger.info("Model Step")

# Print timestep header
timestep_str = f"# {STD_INDENT*0}🕰️ Time passes. It is the year {self.steps}. #"
nchar_timestep_str = len(timestep_str)
Expand Down
16 changes: 12 additions & 4 deletions vegetation/patch/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class VegCell(mg.Cell):
elevation: int | None
aridity: int | None
refugia_status: bool = False
jotr_max_life_stage: int | None

def __init__(
self,
Expand All @@ -47,17 +48,24 @@ def __init__(
# is that this will either not work or be very slow, but itll get us started
self.jotr_agents = []
self.occupied_by_jotr_agents = False
self.jotr_max_life_stage = 0

def step(self):
self.update_occupancy()
pass

def update_occupancy(self):
# Very clunky way to exclude dead agents
alive_jotr_agents = [
agent for agent in self.jotr_agents if agent.life_stage != LifeStage.DEAD
alive_patch_life_stages = [
agent.life_stage
for agent in self.jotr_agents
if agent.life_stage != LifeStage.DEAD
]
self.occupied_by_jotr_agents = True if len(alive_jotr_agents) > 0 else False
if alive_patch_life_stages:
self.jotr_max_life_stage = max(alive_patch_life_stages)
self.occupied_by_jotr_agents = True
else:
self.jotr_max_life_stage = None
self.occupied_by_jotr_agents = False

def add_agent_link(self, jotr_agent):
if jotr_agent.life_stage and jotr_agent not in self.jotr_agents:
Expand Down

0 comments on commit 721f187

Please sign in to comment.