You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# to the rasterlayer. This seems like very foundational mesa / mesa-geo stuff,
# which should be handled by the GeoAgent or GeoBase, but the examples are
# inconsistent. For now, invert the affine transformation to get the indices,
# converting from geographic (lat, lon) to raster (col, row) coordinates
importmesa_geoasmgimportnumpyasnpimportshapely.geometryassgimportrandomfromscipy.statsimportpoissonimportloggingfromvegetation.config.stagesimportLifeStagefromvegetation.utils.spatialimporttransform_point_wgs84_utm, generate_point_in_utmfromvegetation.config.transitionsimport (
JOTR_JUVENILE_AGE,
JOTR_REPRODUCTIVE_AGE,
JOTR_ADULT_AGE,
JOTR_SEED_DISPERSAL_DISTANCE,
get_jotr_emergence_rate,
get_jotr_survival_rate,
get_jotr_breeding_poisson_lambda,
)
fromvegetation.config.loggingimport (
AgentLogger,
AgentEventType,
)
classJoshuaTreeAgent(mg.GeoAgent):
@propertydefagent_logger(self):
ifnothasattr(self, "_agent_logger"):
self._agent_logger=AgentLogger()
returnself._agent_loggerdef__init__(self, model, geometry, crs, age=None, parent_id=None, log_level=None):
super().__init__(
model=model,
geometry=geometry,
crs=crs,
)
self.age=ageself.parent_id=parent_idself.life_stage=None# self.log_level = log_level# To get this set up, assume all agents have logging.INFO levelself.log_level=logging.INFO# TODO: When we create the agent, we need to know its own indices relative# Issue URL: https://github.com/SchmidtDSE/mesa_abm_poc/issues/6# to the rasterlayer. This seems like very foundational mesa / mesa-geo stuff,# which should be handled by the GeoAgent or GeoBase, but the examples are# inconsistent. For now, invert the affine transformation to get the indices,# converting from geographic (lat, lon) to raster (col, row) coordinatesself.float_indices=~self.model.space.raster_layer._transform* (
np.float64(geometry.x),
np.float64(geometry.y),
)
# According to wang-boyu, mesa-geo maintainer:# pos = (x, y), with an origin at the lower left corner of the raster grid# indices = (row, col) format with an origin at the upper left corner of the raster grid# See https://github.com/projectmesa/mesa-geo/issues/267# pos = (np.float64(geometry.x), np.float64(geometry.y))# self._pos = posself.indices= (
int(self.float_indices[0]),
self.model.space.raster_layer.height-int(self.float_indices[1]),
)
self._pos= (
int(self.float_indices[0]),
int(self.float_indices[1]),
)
self.agent_logger.log_agent_event(self, AgentEventType.ON_CREATE)
# TODO: Figure out how to set the life stage on init# Issue URL: https://github.com/SchmidtDSE/mesa_abm_poc/issues/3# Seems natural to set the life stage on init, but in# see lines 181-190 in mesa_geo/geoagent.py, the agents are instantiated before the# GeoAgent gets the attributes within the geojson, so we need to call _update_life_stage# after init when the age is known to the agent# self._update_life_stage()defstep(self):
# Check if agent is dead - if yes, skipifself.life_stage==LifeStage.DEAD:
return# Find the underlying cell - it must exist, else raise an errorintersecting_cell_filter=self.model.space.raster_layer.iter_neighbors(
self.indices, moore=False, include_center=True, radius=0
)
intersecting_cell=next(intersecting_cell_filter)
ifnotintersecting_cell:
raiseValueError("No intersecting cell found")
# If seed, get emergence rate, if not, get survival rateifself.life_stage==LifeStage.SEED:
survival_rate=get_jotr_emergence_rate(intersecting_cell.aridity)
else:
survival_rate=get_jotr_survival_rate(
self.life_stage,
intersecting_cell.aridity,
0, # Assume no nurse plants for now
)
# Roll the dice to see if the agent survivesdice_roll_zero_to_one=random.random()
# Check survival, comparing dice roll to survival rateifdice_roll_zero_to_one<survival_rate:
self.agent_logger.log_agent_event(
self,
AgentEventType.ON_SURVIVE,
context={"survival_rate": survival_rate},
)
else:
self.agent_logger.log_agent_event(
self,
AgentEventType.ON_DEATH,
context={"survival_rate": survival_rate},
)
self.life_stage=LifeStage.DEAD# Increment ageself.age+=1life_stage_promotion=self._update_life_stage()
iflife_stage_promotion:
self.agent_logger.log_agent_event(self, AgentEventType.ON_TRANSITION)
# Update underlying patchintersecting_cell.add_agent_link(self)
# Disperseifself.life_stage==LifeStage.BREEDING:
jotr_breeding_poisson_lambda=get_jotr_breeding_poisson_lambda(
intersecting_cell.aridity
)
n_seeds=poisson.rvs(jotr_breeding_poisson_lambda)
self.agent_logger.log_agent_event(
self, AgentEventType.ON_DISPERSE, context={"n_seeds": n_seeds}
)
self._disperse_seeds(n_seeds)
def_update_life_stage(self):
initial_life_stage=self.life_stageifself.life_stage==LifeStage.DEAD:
returnage=self.ageifself.ageelse0ifage==0:
life_stage=LifeStage.SEEDelifage>0andage<=JOTR_JUVENILE_AGE:
life_stage=LifeStage.SEEDLINGelifage>=JOTR_JUVENILE_AGEandage<=JOTR_ADULT_AGE:
life_stage=LifeStage.JUVENILEelifage>JOTR_ADULT_AGEandage<JOTR_REPRODUCTIVE_AGE:
life_stage=LifeStage.ADULTelse:
life_stage=LifeStage.BREEDINGself.life_stage=life_stageifinitial_life_stage!=self.life_stage:
returnTrueelse:
returnFalsedef_disperse_seeds(
self, n_seeds, max_dispersal_distance=JOTR_SEED_DISPERSAL_DISTANCE
):
ifself.life_stage!=LifeStage.BREEDING:
raiseValueError(
f"Agent {self.unique_id} is not breeding and cannot disperse seeds"
)
wgs84_to_utm, utm_to_wgs84=transform_point_wgs84_utm(
self.geometry.x, self.geometry.y
)
x_utm, y_utm=wgs84_to_utm.transform(self.geometry.x, self.geometry.y)
for__seed_idxinnp.arange(0, n_seeds):
seed_x_utm, seed_y_utm=generate_point_in_utm(
x_utm, y_utm, max_dispersal_distance
)
seed_x_wgs84, seed_y_wgs84=utm_to_wgs84.transform(seed_x_utm, seed_y_utm)
seed_agent=JoshuaTreeAgent(
model=self.model,
geometry=sg.Point(seed_x_wgs84, seed_y_wgs84),
crs=self.crs,
age=0,
parent_id=self.unique_id,
)
seed_agent._update_life_stage()
self.model.space.add_agents(seed_agent)
The text was updated successfully, but these errors were encountered:
to the rasterlayer. This seems like very foundational mesa / mesa-geo stuff,
which should be handled by the GeoAgent or GeoBase, but the examples are
inconsistent. For now, invert the affine transformation to get the indices,
converting from geographic (lat, lon) to raster (col, row) coordinates
mesa_abm_poc/vegetation/model/joshua_tree_agent.py
Lines 48 to 53 in ba08316
The text was updated successfully, but these errors were encountered: