-
Notifications
You must be signed in to change notification settings - Fork 52
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
Implement RasterLayer #75
Conversation
Codecov Report
@@ Coverage Diff @@
## master #75 +/- ##
==========================================
- Coverage 85.67% 78.09% -7.59%
==========================================
Files 5 7 +2
Lines 363 566 +203
Branches 57 99 +42
==========================================
+ Hits 311 442 +131
- Misses 37 108 +71
- Partials 15 16 +1
Continue to review full report at Codecov.
|
69f94f0
to
2622e87
Compare
I think the 2 examples can be in separate PRs. Imagine the changelog for the release associated with this PR: "Implement RasterLayer and 2 examples", that would be putting too much in. |
2622e87
to
0648e22
Compare
Sure. The examples are in a separate branch now: https://github.com/wang-boyu/mesa-geo/tree/examples |
This might be an opportunity for designing a powerful API like you'd see in Pandas DF col/row aggregation. E.g. |
Looking forward to reviewing this! For me it would have been beneficial to have the examples in this pr to immediately see how it is being used, but I am also fine with a separate branch |
Thanks! And sorry you'll have to check the examples from the separate branch now : ) |
This is a good point! But I think I'll leave it to the next PR - to start with something like a For this PR I'll remove the |
def __getitem__(self, index: Sequence[Coordinate]) -> List[Cell]: | ||
... | ||
|
||
def __getitem__( |
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.
Is the code in this method copied almost verbatim from mesa.space.Grid
? If so, we should inform in the docstring about it. But there are potential new changes to it in projectmesa/mesa#815. Having to manually sync between them would not be ideal.
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.
You're right! Initially I planned to make RasterLayer derived from Grid:
class RasterLayer(GeoBase, mesa.space.Grid):
...
But then I don't want it to be able to hold agents, since we want our users to add agents directly into GeoSpace.
I'm also aware of the new changes, but they are not yet released. I hope such changes won't happen too often in the future so that manual sync remains feasible.
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.
We could define a common class in Mesa that has this method. Is __getitem__
the only one that is copied over so far?
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.
I'm afraid not. What's copied over so far:
__getitem__
__iter__
coord_iter
iter_neighborhood
get_neighborhood
iter_neighbors
get_neighbors # copied and renamed to `get_neighboring_cells`
out_of_bounds # not yet copied but could be useful
iter_cell_list_contents
get_cell_list_contents
Methods that are not copied over:
torus_adj
neighbor_iter
move_agent
place_agent
_place_agent
remove_agent
is_cell_empty
move_to_empty
find_empty
exists_empty_cells
Another difference is that Grid
has self.grid: List[List[Agent | None]]
, whereas it is self.cells: List[List[Cell]]
in RasterLayer
.
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.
That's a lot of methods. We should definitely replace the copying into inheritance very soon. I'm fine with it for now though.
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.
That's indeed a lot of methods. Inheritance would make it much better, but I would not want to mess up the classes in Mesa either.
Edit: Sorry by saying "mess up" I meant to affect the class design in Mesa due to some needs/requirements from Mesa-Geo.
return x < 0 or x >= self.width or y < 0 or y >= self.height | ||
|
||
|
||
class Cell(Agent): |
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.
What is the definition of Cell
?
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.
Cells are building blocks of RasterLayer, and can have multiple attributes, depending on specific models. Some examples I wrote from the separated branch:
- Rainfall example: https://github.com/wang-boyu/mesa-geo/blob/2622e870784aa06b55d52cb9697f8caea65de558/examples/rainfall/rainfall/space.py#L11-L23
- Urban growth example: https://github.com/wang-boyu/mesa-geo/blob/2622e870784aa06b55d52cb9697f8caea65de558/examples/urban_growth/urban_growth/space.py#L10-L43
In theory, they can act like agents if the step()
method is implemented, or stay static. But one thing I'm not sure (that I forgot to mention in the PR description) is how to make them work with the schedulers, which needs a reference/pointer to the model
within the agent/cell.
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.
Ah, so it is a container for attributes. It seems that it is going to be slow for huge raster grid size. Makes me wonder that the raster layer can be reimplemented in Cython for faster computation. But this can be done in future iterations.
0648e22
to
c81ce7d
Compare
The |
My remaining concerns are just documentation issue: you need to put into |
c81ce7d
to
9962032
Compare
Updated the docstrings. |
LGTM. If @Corvince doesn't have the time to review, I will merge in 2 days from now. |
Deal. I might or might not have the time, so a 2 days window sounds good. |
|
This PR is not yet ready, but I'd like to gather some feedbacks from you @rht @Corvince
The classes now look like this:
The main pending works are:
Visualization of
RasterLayer
Currently
RasterLayer
uses a portrayal method to map aCell
to a color, i.e., (r, g, b, a). But it would be better if the users can define the portrayal method to return a dict (similar to agent portrayal). And I'm not sure how this works with a range of colors. Example from NetLogo: scale-color .Theget_min_cell
andget_max_cell
methodsInstead of the current implementation, I was thinking to have something more generic such asso that the users can passmin
andmax
as theaggfunc
parameter. In addition, users can also define their ownby
parameter to be any function instead of cell attributes. But this API seems a bit too complicated.The
apply_raster
methodThis is essentially the gis:apply-raster from NetLogo. But currently it works only if the input matrix has exactly the same dimension as the Raster Layer. Shall I make it more flexible so that the input matrix gets automatically resized to that of the Raster Layer?
Please feel free to suggest how to improve this PR. Thanks a lot!