Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GIS models testable and test in CI #171

Merged
merged 6 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
name: build
name: Test example models

on:
push:
branches:
- main
- release**
paths-ignore:
- '**.md'
- '**.rst'
paths:
- 'examples/**/*.py' # If an example model is modified
- 'test_examples.py' # If the test script is modified
- '.github/workflows/test_examples.yml' # If this workflow is modified
pull_request:
paths-ignore:
- '**.md'
- '**.rst'
paths:
- 'examples/**/*.py'
- 'test_examples.py'
- '.github/workflows/test_examples.yml'
workflow_dispatch:
schedule:
- cron: '0 6 * * 1' # Monday at 6:00 UTC

# This will cancel previous run if a newer job that obsoletes the said previous
# run, is started.
# Based on https://github.com/zulip/zulip/commit/4a11642cee3c8aec976d305d51a86e60e5d70522
concurrency:
group: "${{ github.workflow }}-${{ github.head_ref || github.run_id }}"
cancel-in-progress: true

jobs:
# build-stable:
# runs-on: ubuntu-latest
Expand All @@ -48,7 +40,7 @@ jobs:
- name: Install dependencies
run: |
pip install mesa --pre
pip install pytest
pip install .[test]
- name: Test with pytest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder to use uv later in a separate PR.

run: pytest -rA -Werror test_examples.py

Expand All @@ -62,7 +54,7 @@ jobs:
python-version: "3.12"
- name: Install dependencies
run: |
pip install pytest
pip install .[test]
pip install -U git+https://github.com/projectmesa/mesa@main#egg=mesa
- name: Test with pytest
run: pytest -rA -Werror test_examples.py
60 changes: 60 additions & 0 deletions .github/workflows/test_gis_examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Test GIS models

on:
push:
paths:
- 'gis/**/*.py' # If a gis model is modified
- 'test_gis_examples.py' # If the gis test script is modified
- '.github/workflows/test_gis_examples.yml' # If this workflow is modified
pull_request:
paths:
- 'gis/**/*.py'
- 'test_gis_examples.py'
- '.github/workflows/test_gis_examples.yml'
workflow_dispatch:
schedule:
- cron: '0 6 * * 1' # Monday at 6:00 UTC

jobs:
# build-stable:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will this be enabled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I think we will want to make a backport to the mesa-2.x branch with examples, since Mesa-Geo is meant to work with Mesa 2.3.

So then we will make te split of testing pre-releases + git on the main branch, and stable on the Mesa-Geo 0.8.x branch.

# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - name: Set up Python
# uses: actions/setup-python@v5
# with:
# python-version: "3.12"
# - name: Install dependencies
# run: pip install mesa pytest
# - name: Test with pytest
# run: pytest -rA -Werror test_examples.py

build-pre:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: |
pip install mesa-geo --pre
pip install .[test_gis]
- name: Test with pytest
run: pytest -rA -Werror test_gis_examples.py

build-main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: |
pip install -U git+https://github.com/projectmesa/mesa-geo@main#egg=mesa-geo
pip install .[test_gis]
- name: Test with pytest
run: pytest -rA -Werror test_gis_examples.py
4 changes: 2 additions & 2 deletions gis/agents_and_networks/src/agent/commuter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import pyproj
from shapely.geometry import LineString, Point

from src.agent.building import Building
from src.space.utils import UnitTransformer, redistribute_vertices
from ..space.utils import UnitTransformer, redistribute_vertices
from .building import Building


class Commuter(mg.GeoAgent):
Expand Down
10 changes: 5 additions & 5 deletions gis/agents_and_networks/src/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import pandas as pd
from shapely.geometry import Point

from src.agent.building import Building
from src.agent.commuter import Commuter
from src.agent.geo_agents import Driveway, LakeAndRiver, Walkway
from src.space.campus import Campus
from src.space.road_network import CampusWalkway
from ..agent.building import Building
from ..agent.commuter import Commuter
from ..agent.geo_agents import Driveway, LakeAndRiver, Walkway
from ..space.campus import Campus
from ..space.road_network import CampusWalkway


def get_time(model) -> pd.Timedelta:
Expand Down
4 changes: 2 additions & 2 deletions gis/agents_and_networks/src/space/campus.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import mesa_geo as mg
from shapely.geometry import Point

from src.agent.building import Building
from src.agent.commuter import Commuter
from ..agent.building import Building
from ..agent.commuter import Commuter


class Campus(mg.GeoSpace):
Expand Down
2 changes: 1 addition & 1 deletion gis/agents_and_networks/src/space/road_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pyproj
from sklearn.neighbors import KDTree

from src.space.utils import segmented
from .utils import segmented


class RoadNetwork:
Expand Down
6 changes: 3 additions & 3 deletions gis/agents_and_networks/src/visualization/server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import mesa

from src.agent.building import Building
from src.agent.commuter import Commuter
from src.agent.geo_agents import Driveway, LakeAndRiver, Walkway
from ..agent.building import Building
from ..agent.commuter import Commuter
from ..agent.geo_agents import Driveway, LakeAndRiver, Walkway


class ClockElement(mesa.visualization.TextElement):
Expand Down
3 changes: 2 additions & 1 deletion gis/geo_sir/model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import mesa
import mesa_geo as mg
from agents import NeighbourhoodAgent, PersonAgent
from shapely.geometry import Point

from .agents import NeighbourhoodAgent, PersonAgent


class GeoSir(mesa.Model):
"""Model class for a simplistic infection model."""
Expand Down
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ license = {file = "LICENSE"}
readme = "README.rst"
requires-python = ">=3.8"

[project.optional-dependencies]
test = [
"pytest",
]
test_gis = [
"pytest",
"momepy",
]

[build-system]
requires = [
"setuptools",
Expand Down
34 changes: 34 additions & 0 deletions test_gis_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import importlib
import os

import pytest
from mesa import Model


def get_models(directory):
models = []
for root, dirs, files in os.walk(directory):
for file in files:
if file == "model.py":
module_name = os.path.relpath(os.path.join(root, file[:-3])).replace(
os.sep, "."
)

module = importlib.import_module(module_name)
for item in dir(module):
obj = getattr(module, item)
if (
isinstance(obj, type)
and issubclass(obj, Model)
and obj is not Model
):
models.append(obj)

return models


@pytest.mark.parametrize("model_class", get_models("gis"))
def test_model_steps(model_class):
model = model_class() # Assume no arguments are needed
for _ in range(10):
model.step()
Loading