Skip to content

Commit

Permalink
hacky trackign of agents at patch level, will probably break or blow …
Browse files Browse the repository at this point in the history
…up but related to previous issues with raster layer cell not having geometry yet
  • Loading branch information
GondekNP committed Dec 18, 2024
1 parent 170e6d7 commit e63dfd5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 42 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/todos_to_issues.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
### NOTE: Disabled for now, since we are not paying for the MongoDB Atlas cluster -
### probably should just self-host it but not really necessary at the moment for just this repo

name: "Run TODO to Issue"
on: ["push"]
jobs:
build:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v4"
- name: "TODO to Issue"
uses: "alstr/todo-to-issue-action@v5"
with:
INSERT_ISSUE_URLS: "true"
CLOSE_ISSUES: "true"

- name: Set Git user
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Commit and Push Changes
run: |
git add -A
if [[ `git status --porcelain` ]]; then
git commit -m "Automatically added GitHub issue links to TODOs"
git push
else
echo "No changes to commit"
fi
2 changes: 1 addition & 1 deletion vegetation/config/transitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def get_jotr_survival_rate(life_stage, aridity, nurse_indicator):
if nurse_indicator:
rate = rate + 0.2

return rate
return rate
76 changes: 36 additions & 40 deletions vegetation/patch/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self, model, geometry, crs, age=None):
)

self.age = age
self.life_stage = None

pos = (np.float64(geometry.x), np.float64(geometry.y))
self._pos = pos
Expand All @@ -51,6 +52,9 @@ def __init__(self, model, geometry, crs, age=None):

def step(self):

if self.life_stage == 'dead':
return

intersecting_cell_filter = self.model.space.raster_layer.iter_neighbors(
self.indices,
moore=False,
Expand Down Expand Up @@ -94,11 +98,18 @@ def step(self):
self.age += 1
self._update_life_stage()

# Update underlying patch
intersecting_cell.update_occupancy(self)

# Disperse
if self.life_stage == 'breeding':
self.disperse_seeds()

def _update_life_stage(self):

if self.life_stage == 'dead':
return

age = self.age if self.age else 0
if age == 0:
life_stage = 'seed'
Expand Down Expand Up @@ -141,57 +152,42 @@ def __init__(self, bounds, export_data=False, num_steps=20, epsg=4326):
self.agents.select(agent_type=JoshuaTreeAgent).do('_update_life_stage')

self.space.add_agents(agents)
self.update_metrics()

self.datacollector = mesa.DataCollector(
{
"Mean Age": self.mean_age,
"N Agents": self.n_agents,
"N Seeds": self.n_seeds,
"N Seedlings": self.n_seedlings,
"N Juveniles": self.n_juveniles,
"N Adults": self.n_adults,
"N Breeding": self.n_breeding
"Mean Age": 'mean_age',
"N Agents": 'n_agents',
"N Seeds": 'n_seeds',
"N Seedlings": 'n_seedlings',
"N Juveniles": 'n_juveniles',
"N Adults": 'n_adults',
"N Breeding": 'n_breeding'
}
)

@property
def mean_age(self):
return self.agents.select(agent_type=JoshuaTreeAgent) \
# @property
def update_metrics(self):
# Mean age
mean_age = self.agents.select(agent_type=JoshuaTreeAgent) \
.agg('age', np.mean)
self.mean_age = mean_age

@property
def n_agents(self):
return len(self.agents.select(agent_type=JoshuaTreeAgent))
# Number of agents (JoshuaTreeAgent)
n_agents = len(self.agents.select(agent_type=JoshuaTreeAgent))
self.n_agents = n_agents

@property
def n_seeds(self):
# Number of agents by life stage
count_dict = self.agents.select(agent_type=JoshuaTreeAgent) \
.groupby('life_stage').count()
return count_dict['seed'] if 'seed' in count_dict else 0

@property
def n_seedlings(self):
count_dict = self.agents.select(agent_type=JoshuaTreeAgent) \
.groupby('life_stage').count()
return count_dict['seedling'] if 'seedling' in count_dict else 0

@property
def n_juveniles(self):
count_dict = self.agents.select(agent_type=JoshuaTreeAgent) \
.groupby('life_stage').count()
return count_dict['juvenile'] if 'juvenile' in count_dict else 0

@property
def n_adults(self):
count_dict = self.agents.select(agent_type=JoshuaTreeAgent) \
.groupby('life_stage').count()
return count_dict['adult'] if 'adult' in count_dict else 0

@property
def n_breeding(self):
count_dict = self.agents.select(agent_type=JoshuaTreeAgent) \
.groupby('life_stage').count()
return count_dict['breeding'] if 'breeding' in count_dict else 0
self.n_seeds = count_dict.get('seed', 0)
self.n_seedlings = count_dict.get('seedling', 0)
self.n_juveniles = count_dict.get('juvenile', 0)
self.n_adults = count_dict.get('adult', 0)
self.n_breeding = count_dict.get('breeding', 0)

def step(self):
self.agents.shuffle_do("step")
self.update_metrics()

self.datacollector.collect(self)
20 changes: 19 additions & 1 deletion vegetation/patch/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import hashlib
import logging
import time
# from patch.model import JoshuaTreeAgent
# import rioxarray as rxr

DEM_STAC_PATH = "https://planetarycomputer.microsoft.com/api/stac/v1/"
Expand All @@ -30,10 +31,27 @@ def __init__(
super().__init__(model, pos, indices)
self.elevation = None
self.aridity = None
self.joshua_tree_occupancy = 0


## TODO: Improve patch level tracking of JOTR agents
## For now, this is somewhat of a hack to track which agents are present within a patch cell
## This is something I suspect is an offshoot of my question posed to the mesa-geo team
## (https://github.com/projectmesa/mesa-geo/issues/267), where the cell does not have a geometry
## and thus I can't use the various geometry based intersection methods to find agents. My guess
## is that this will either not work or be very slow, but itll get us started
self.joshua_tree_agents = []

def step(self):
pass

def update_occupancy(self, jotr_agent):
if jotr_agent.life_stage and jotr_agent.life_stage != 'dead':
self.joshua_tree_agents.append(jotr_agent)
if jotr_agent not in self.joshua_tree_agents:
self.model.joshua_tree_agents.append(jotr_agent.unique_id)
self.joshua_tree_occupancy += 1


class StudyArea(mg.GeoSpace):
def __init__(self, bounds, epsg, model):
Expand Down Expand Up @@ -176,4 +194,4 @@ def is_at_boundary(self, row_idx, col_idx):
or row_idx == self.raster_layer.height
or col_idx == 0
or col_idx == self.raster_layer.width
)
)

0 comments on commit e63dfd5

Please sign in to comment.