Add Cell.xy for RasterLayer cell coordinates and clarify pos/indices semantics#299
Add Cell.xy for RasterLayer cell coordinates and clarify pos/indices semantics#299
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
WalkthroughRefactors Cell and RasterLayer to add explicit Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
for more information, see https://pre-commit.ci
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
tests/test_RasterLayer.py (1)
9-125: Consider adding tests for new Cell attributes and deprecation warnings.The tests are correctly updated for the API changes, but coverage for the new functionality is incomplete:
- No tests verify that
cell.xycontains correct real-world coordinates- No tests verify that
cell.rowcolcontains correct row/column indices- No tests verify that accessing deprecated
cell.posandcell.indicesemitFutureWarningExample test for deprecation warning:
def test_deprecated_pos_warning(self): cell = self.raster_layer.cells[0][0] with self.assertWarns(FutureWarning): _ = cell.pos
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
mesa_geo/raster_layers.pytests/test_RasterLayer.py
🧰 Additional context used
🧬 Code graph analysis (1)
tests/test_RasterLayer.py (1)
mesa_geo/raster_layers.py (3)
apply_raster(394-414)attributes(313-320)get_raster(416-442)
🔇 Additional comments (10)
mesa_geo/raster_layers.py (6)
11-11: LGTM!The
warningsimport is correctly added to support the deprecation warnings implementation.
165-167: LGTM!Clean class-level type annotations for the new coordinate attributes.
169-206: LGTM!The
__init__method correctly implements backward compatibility by accepting both deprecated (pos,indices) and new (grid_pos,rowcol,xy) parameters. The fallback logic prioritizes new parameters while maintaining support for legacy code.
208-242: LGTM!Well-implemented deprecation pattern:
- Uses
FutureWarning(appropriate for user-facing deprecation)stacklevel=2correctly points to the caller's code- Both getters and setters emit warnings and delegate properly
300-309: LGTM!Correct implementation of cell initialization:
- Real-world coordinates (
xy) correctly computed using pixel center offsets (+0.5)- Affine transform applied with proper
(col, row)orderinggrid_posandrowcolare correctly derived from the loop variables and index calculations
604-604: LGTM!Correctly updated to use
cell.rowcolinstead of the deprecatedcell.indices, avoiding deprecation warnings in internal code paths.tests/test_RasterLayer.py (4)
28-47: LGTM!Test correctly updated to use named attributes (
val,elevation) instead of auto-generated attribute names, and assertions properly reflect the expected attribute set.
52-77: LGTM!Test properly exercises both named and auto-generated attributes, with clear comments explaining the expected 3 layers.
81-113: LGTM!Test correctly updated to use
grid_posinstead of the deprecatedposattribute for cell position assertions.
115-125: LGTM!Test correctly updated to use
grid_posfor the maximum cell position assertion.
for more information, see https://pre-commit.ci
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
tests/test_RasterLayer.py (1)
28-28: Typo in test method name.The method is named
test_apple_rasterbut should likely betest_apply_rasterbased on what it tests.✏️ Suggested fix
- def test_apple_raster(self): + def test_apply_raster(self):
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
tests/test_RasterLayer.py
🧰 Additional context used
🧬 Code graph analysis (1)
tests/test_RasterLayer.py (1)
mesa_geo/raster_layers.py (8)
apply_raster(394-414)attributes(313-320)get_raster(416-442)pos(209-215)pos(218-224)indices(227-233)indices(236-242)transform(117-125)
🔇 Additional comments (5)
tests/test_RasterLayer.py (5)
71-77: Potential order-sensitivity issue with set iteration.The comment on line 72-73 states "order doesn't matter for equality check," but
np.testing.assert_array_equalis order-sensitive. According to the relevant code snippet,get_raster()iterates overself.attributeswhich is aset[str]. Since all three raster layers have identical data in this test, the assertion passes regardless of order, but this could be fragile if the test data changes.Consider whether this test should be more explicit about expected behavior, or if you intentionally rely on identical data to avoid order issues.
90-90: LGTM!The updates from
postogrid_poscorrectly align with the new API semantics. The test logic remains valid.Also applies to: 99-99, 111-111
124-125: LGTM!Property update to
grid_posis consistent with other test methods.
127-152: Good deprecation test coverage.The test correctly validates that:
- Deprecated properties emit
FutureWarning- Getters delegate to the new property names
- Setters also work with deprecation warnings
This aligns well with the implementation in
raster_layers.pylines 208-241.
154-175: Well-structured coordinate transformation test.The test effectively validates:
grid_postorowcolmapping follows the expected formula(height - 1 - y, x)- Cell center coordinates (
xy) are correctly computed using the affine transform with(col + 0.5, row + 0.5)The use of
assertAlmostEqualis appropriate for floating-point coordinate comparisons.
|
Please review @wang-boyu |
|
Thank you for your contribution. I put the issues as an |
|
There's an ongoing discussion about multi-space suppport in Mesa and I'd like to see how they name similar concepts for a Cell in Mesa's future discrete space, to see whether we should follow Rasterio's name convention or not: mesa/mesa#2585 (reply in thread) |
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
|
I spent a few days thinking about Cell's property names, mostly about generic
I'll keep this PR open for some more time in case there's anything else to fix before merging. |
|
We might want to update our GIS examples based on this PR. I created a tracking issue: @wang-boyu since this is (apparently) a breaking change:
|
Summary
This PR introduces explicit coordinate attributes (
cell.xy,cell.grid_pos,cell.rowcol) toCellobjects inRasterLayer. It disambiguates real-world geographic coordinates from internal grid indices and deprecates the ambiguouscell.posandcell.indicesproperties.Motive
Closes #298
Implementation
mesa_geo/raster_layers.py:RasterLayerInitialization:Usage Examples
Old Way (Ambiguous & Manual Calculation):
New Way:
Summary by CodeRabbit
New Features
apply_raster/get_rasteracceptattr_namefor named raster layers.xyproperty exposing real-world cell center coordinates.Deprecations
posandindicesdeprecated in favor ofgrid_posandrowcol; deprecated accessors emit warnings.Other
val; tests added/updated for deprecation warnings and coordinate transforms.✏️ Tip: You can customize this high-level summary in your review settings.