From 4872a9ac969a12cad542293983a666c152b8cecc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 23 Nov 2024 15:24:50 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../experimental/cell_space/property_layer.py | 20 +++++++------- tests/test_cell_space.py | 27 +++++++++++++------ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/mesa/experimental/cell_space/property_layer.py b/mesa/experimental/cell_space/property_layer.py index fb9c3705823..e7426d4f485 100644 --- a/mesa/experimental/cell_space/property_layer.py +++ b/mesa/experimental/cell_space/property_layer.py @@ -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. @@ -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: diff --git a/tests/test_cell_space.py b/tests/test_cell_space.py index bd7213ee173..32b66357aa2 100644 --- a/tests/test_cell_space.py +++ b/tests/test_cell_space.py @@ -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