-
-
Notifications
You must be signed in to change notification settings - Fork 5
fix(compatibility): 🐛 Introduce mesa-geo compatibility handling and regression tests #148
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """Compatibility helpers for mesa-geo raster APIs across versions. | ||
|
|
||
| Mesa-geo's ``RasterLayer`` initialization and ``_update_transform`` behavior | ||
| varies between releases. ABSESpy uses ``PatchModule`` with ``_cells`` and a | ||
| ``cached_property`` for ``cells``; upstream probes like ``getattr(self, | ||
| "cells", None)`` during early construction can recurse through ``__getattr__``. | ||
| This module centralizes safe, capability-based handling. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
|
|
||
| def raster_base_update_transform(instance: Any) -> None: | ||
| """Apply ``RasterBase._update_transform`` without ``RasterLayer`` side effects. | ||
|
|
||
| Args: | ||
| instance: A ``RasterLayer`` subclass instance (e.g. ``PatchModule``). | ||
| """ | ||
| from mesa_geo.raster_layers import RasterBase | ||
|
|
||
| RasterBase._update_transform(instance) | ||
|
|
||
|
|
||
| def maybe_sync_cell_xy(instance: Any) -> None: | ||
| """Call ``_sync_cell_xy`` when the upstream class provides it and cells exist. | ||
|
|
||
| Older mesa-geo releases do not define ``_sync_cell_xy``. Newer releases | ||
| sync cell centers after transform updates when cell storage is ready. | ||
|
|
||
| Args: | ||
| instance: A ``RasterLayer`` subclass instance (e.g. ``PatchModule``). | ||
| """ | ||
| if instance.__dict__.get("_cells") is None: | ||
| return | ||
| sync = getattr(type(instance), "_sync_cell_xy", None) | ||
| if sync is None: | ||
| return | ||
| sync(instance) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #!/usr/bin/env python3 | ||
| # -*-coding:utf-8 -*- | ||
| """Regression tests for PatchModule vs mesa-geo raster initialization order.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from abses.core.model import MainModel | ||
| from abses.space.mesa_raster_compat import maybe_sync_cell_xy | ||
| from abses.space.patch import PatchModule | ||
|
|
||
|
|
||
| def test_create_patch_module_no_recursion(model: MainModel) -> None: | ||
| """Creating a grid must complete without RecursionError (mesa-geo 0.9.3+).""" | ||
| module = model.nature.create_module(shape=(4, 4), resolution=1.0) | ||
| assert isinstance(module, PatchModule) | ||
| assert module.array_cells.shape == (4, 4) | ||
| assert len(module.cells) == 4 | ||
|
|
||
|
|
||
| def test_update_transform_after_init_is_safe(model: MainModel) -> None: | ||
| """Transform updates after cells exist must remain safe.""" | ||
| module = model.nature.create_module(shape=(2, 3), resolution=1.0) | ||
| module._update_transform() | ||
| module._update_transform() | ||
| assert module.transform is not None | ||
|
|
||
|
|
||
| def test_maybe_sync_cell_xy_skips_without_cells() -> None: | ||
| """Helper must not fail when ``_cells`` is absent or sync is undefined.""" | ||
|
|
||
| class _NoSync: | ||
| pass | ||
|
|
||
| layer = _NoSync() | ||
| maybe_sync_cell_xy(layer) | ||
|
|
||
| layer.__dict__["_cells"] = None | ||
| maybe_sync_cell_xy(layer) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("shape", [(1, 1), (5, 7)]) | ||
| def test_create_module_various_shapes(model: MainModel, shape: tuple[int, int]) -> None: | ||
| """Smoke test multiple raster dimensions.""" | ||
| module = model.nature.create_module(shape=shape, resolution=1.0) | ||
| assert module.width == shape[1] | ||
| assert module.height == shape[0] | ||
| module.apply_raster(np.ones(module.shape3d), attr_name="ones") | ||
| assert module.get_raster("ones").sum() == shape[0] * shape[1] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix punctuation in the dependency table description.
Line 12 should use
etc.instead ofetcfor correct abbreviation punctuation.Suggested edit
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[style] ~12-~12: In American English, abbreviations like “etc.” require a period.
Context: ...ng and writing vector data (shapefiles, etc) | | rioxarray | ">=0.13" ...
(ETC_PERIOD)
🤖 Prompt for AI Agents