Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 23, 2024
1 parent 6966d00 commit 4872a9a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
20 changes: 11 additions & 9 deletions mesa/experimental/cell_space/property_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,24 @@ def __init__(
)
self.__class__.propertylayer_experimental_warning_given = True


@classmethod
def from_data(cls, name:str, data:np.ndarray):
"""create a property layer from a NumPy array.
def from_data(cls, name: str, data: np.ndarray):
"""Create a property layer from a NumPy array.
Args:
name: The name of the property layer.
data: A NumPy array representing the grid data.
"""

layer = cls(name, data.shape, default_value=data[*[0 for _ in range(len(data.shape))]], dtype=data.dtype.type)
layer = cls(
name,
data.shape,
default_value=data[*[0 for _ in range(len(data.shape))]],
dtype=data.dtype.type,
)
layer.set_cells(data)
return layer




def set_cells(self, value, condition: Callable | None = None):
"""Perform a batch update either on the entire grid or conditionally, in-place.
Expand Down Expand Up @@ -353,7 +353,9 @@ def select_cells(

# Apply the empty mask if only_empty is True
if only_empty:
combined_mask = np.logical_and(combined_mask, self._mesa_property_layers["empty"])
combined_mask = np.logical_and(
combined_mask, self._mesa_property_layers["empty"]
)

# Apply conditions
if conditions:
Expand Down
27 changes: 19 additions & 8 deletions tests/test_cell_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,32 +716,43 @@ def test_select_cells():
dimensions = (5, 5)
grid = OrthogonalMooreGrid(dimensions, torus=False, random=random.Random(42))

data = np.random.default_rng(12456).random((5,5))
data = np.random.default_rng(12456).random((5, 5))
grid.add_property_layer(PropertyLayer.from_data("elevation", data))

# fixme, add an agent and update the np.all test accordingly
mask = grid.select_cells(conditions={"elevation": lambda x: x > 0.5}, return_list=False, only_empty=True)
mask = grid.select_cells(
conditions={"elevation": lambda x: x > 0.5}, return_list=False, only_empty=True
)
assert mask.shape == (5, 5)
assert np.all(mask == (data > 0.5))

mask = grid.select_cells(conditions={"elevation": lambda x: x > 0.5}, return_list=False, only_empty=False)
mask = grid.select_cells(
conditions={"elevation": lambda x: x > 0.5}, return_list=False, only_empty=False
)
assert mask.shape == (5, 5)
assert np.all(mask == (data > 0.5))

# fixme add extreme_values heighest and lowest
mask = grid.select_cells(extreme_values={'elevation' : 'highest'}, return_list=False, only_empty=False)
mask = grid.select_cells(
extreme_values={"elevation": "highest"}, return_list=False, only_empty=False
)
assert mask.shape == (5, 5)
assert np.all(mask == (data==data.max()))
assert np.all(mask == (data == data.max()))

mask = grid.select_cells(extreme_values={'elevation' : 'lowest'}, return_list=False, only_empty=False)
mask = grid.select_cells(
extreme_values={"elevation": "lowest"}, return_list=False, only_empty=False
)
assert mask.shape == (5, 5)
assert np.all(mask == (data==data.min()))
assert np.all(mask == (data == data.min()))

with pytest.raises(ValueError):
grid.select_cells(extreme_values={'elevation': 'weird'}, return_list=False, only_empty=False)
grid.select_cells(
extreme_values={"elevation": "weird"}, return_list=False, only_empty=False
)

# fixme add pre-specified mask to any other option


def test_property_layer_errors():
"""Test error handling for PropertyLayers."""
dimensions = 5, 5
Expand Down

0 comments on commit 4872a9a

Please sign in to comment.