Skip to content
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

space: Implement PropertyLayer and _PropertyGrid #1898

Merged
merged 37 commits into from
Jan 6, 2024

Commits on Dec 25, 2023

  1. Configuration menu
    Copy the full SHA
    be494d3 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    6820b12 View commit details
    Browse the repository at this point in the history
  3. _PropertyGrid: Implement multi-property cell selection and enhanced a…

    …gent movement
    
    This commit introduces three significant enhancements for grids based on the _PropertyGrid in the mesa.space module:
    1. `select_cells_multi_properties`: Allows for the selection of cells based on multiple property conditions, using a combination of NumPy operations. This method returns a list of coordinates satisfying the specified conditions.
    2. `move_agent_to_random_cell`: Enables moving an agent to a random cell that meets specified property conditions, enhancing the flexibility in agent movements.
    3. `move_agent_to_extreme_value_cell`: Facilitates moving an agent to a cell with the highest, lowest, or closest property value, based on a specified mode. This method extends agent movement capabilities, allowing for more dynamic and condition-based relocations.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    f3ad411 View commit details
    Browse the repository at this point in the history
  4. _PropertyGrid: Add optional neighborhood filtering to spatial methods

    - Updated `select_cells_multi_properties`, `move_agent_to_random_cell`, and `move_agent_to_extreme_value_cell` methods in the `_PropertyGrid` class to include an optional neighborhood filtering feature.
    - Added `only_neighborhood` parameter to these methods to allow for conditional operations within a specified neighborhood around an agent's position.
    - Introduced `get_neighborhood_mask` as a helper function to create a boolean mask for neighborhood-based selections, enhancing performance and readability.
    - Modified methods to utilize NumPy for efficient array operations, improving the overall performance of grid-based spatial calculations.
    - Ensured backward compatibility by setting `only_neighborhood` to `False` by default, allowing existing code to function without modification.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    38faff5 View commit details
    Browse the repository at this point in the history
  5. PropertyLayer: Check dimensions and dtype on init

    Checks on initialization are cheap, and it helps users make the right decision for (NumPy) data type.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    ddeb91c View commit details
    Browse the repository at this point in the history
  6. Add docstring to PropertyLayer

    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    1216d51 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    f978f58 View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    900cd62 View commit details
    Browse the repository at this point in the history
  9. _PropertyGrid: Make _get_neighborhood_mask private

    Mark the _get_neighborhood_mask method as private, since it's intended as a helper function for select_cells_multi_properties() and move_agent_to_random_cell()
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    04beb82 View commit details
    Browse the repository at this point in the history
  10. move_agent_to_extreme_value_cell: remove closest option

    closest is more difficult than highest or lowest, since closest needs a target to compare against. For now too complex without proven need, so I removed it.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    64ee7d3 View commit details
    Browse the repository at this point in the history
  11. tests: Add tests for _PropertyGrid

    SingleGrid inherits from _PropertyGrid, so testing though SingleGrid.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    db9779c View commit details
    Browse the repository at this point in the history
  12. PropertyLayer: Fix handling of 'condition' callable in set_cells

    Resolved an inconsistency in the set_cells method of our grid class. The method's documentation stated that the 'condition' argument should be a callable (such as a lambda function or a NumPy ufunc), but the implementation incorrectly treated 'condition' as a NumPy array.
    
    This update rectifies the implementation to align with the documented behavior. Now, the 'condition' argument is correctly called with the grid data as input, and its output (expected to be a boolean array) is used for conditional in-place updates of the grid. This change ensures that the function operates correctly when provided with callable conditions, fulfilling the intended functionality.
    
    Includes:
    - Calling 'condition' with self.data and using its output for conditional updates.
    - Adjusted error handling to check the output of the callable, ensuring it's a NumPy array with the correct shape.
    - Updated comments within the method for clarity.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    5be029c View commit details
    Browse the repository at this point in the history
  13. PropertyLayer: Remove unnecessary check in aggregate_property

    Whether it's a Lambda function or NumPy ufunc, they can be called the same way.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    2ac72a5 View commit details
    Browse the repository at this point in the history
  14. _PropertyGrid: Take mask as input for selection functions

    Take mask as input for selection functions. This way, it isn't needed to parse all neighbourhood elements. It also allows to use custom masks like for empty cells, etc.
    
    get_neighborhood_mask is now a public (not private) method again.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    6ead379 View commit details
    Browse the repository at this point in the history
  15. _PropertyGrid: Give option to return list or mask

    Give the select_cells_multi_properties method an option to return a mask instead of a list of cells. list is still default.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    f570272 View commit details
    Browse the repository at this point in the history
  16. _PropertyGrid: Split move_agent_to_extreme_value_cell into two methods

    Split the move_agent_to_extreme_value_cell into two functions:
    - select_extreme_value_cells, which selects target cells
    - move_agent_to_extreme_value_cell, which moves the agent to that cell
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    250678b View commit details
    Browse the repository at this point in the history
  17. Configuration menu
    Copy the full SHA
    24102c0 View commit details
    Browse the repository at this point in the history
  18. Configuration menu
    Copy the full SHA
    6b80a2a View commit details
    Browse the repository at this point in the history
  19. _PropertyGrid: Rename functions to select and move by multiple proper…

    …ties
    
    Hopefully these names are a little clearer
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    2197bd3 View commit details
    Browse the repository at this point in the history
  20. get_empty_mask: Use faster np.zeros

    np.zeros is faster than np.full
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    8afbc99 View commit details
    Browse the repository at this point in the history
  21. Optimize select_extreme_value_cells method for performance

    This commit introduces several optimizations to the select_extreme_value_cells method of the _PropertyGrid class. These changes are aimed at enhancing the performance, especially for larger datasets, by reducing computational complexity and leveraging efficient numpy array operations.
    
    Key Changes:
    - Condensed the mask application process to a single conditional statement, reducing unnecessary operations when no mask is provided.
    - Streamlined the calculation of extreme values (maximum or minimum) using direct numpy functions without separate branching.
    - Optimized the creation of the target mask by utilizing numpy's inherent functions for array comparison, thereby minimizing the computational overhead.
    - Improved the efficiency of converting the mask to a list of coordinates using np.argwhere and tolist(), which is more suited for numpy arrays.
    
    These enhancements ensure that the method is more efficient and performant, particularly when handling large grids or property layers.
    
    Fixes:
    - The method now correctly returns a mask instead of an ndarray of coordinates when return_list is set to False. This fix aligns the method's behavior with its intended functionality and improves its usability in grid operations.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    b0358f9 View commit details
    Browse the repository at this point in the history
  22. Update PropertyGrid tests

    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    eedbec7 View commit details
    Browse the repository at this point in the history
  23. Update test_space.py

    Fix remaining TestPropertyLayer
    
    A condition always is a function, never a mask (for now).
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    0351d14 View commit details
    Browse the repository at this point in the history
  24. Add a test if the coordinate systems are identical

    Checks if the property layer and the property grid use coordinates in the same way.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    50d9602 View commit details
    Browse the repository at this point in the history
  25. PropertyLayer: Improve ufunc handling and lambda vectorization

    This update significantly improves the PropertyLayer's efficiency and robustness, particularly in handling lambda functions and NumPy universal functions (ufuncs). It addresses a critical bug where lambda conditions were incorrectly returning single boolean values instead of boolean arrays (masks) required for grid operations. The enhancements include checks to optimize the use of ufuncs and ensure accurate application based on their argument requirements.
    
    Motivation:
    - The primary motivation for these changes was to fix a bug in the `set_cells` method where lambda functions used as conditions were returning single boolean values instead of boolean arrays. This behavior led to errors and inefficient grid updates.
    - To address this, we optimized the method to correctly handle lambda functions and vectorize them only when necessary, ensuring they return a mask as required.
    - Furthermore, the implementation was fine-tuned to better handle NumPy ufuncs, avoiding redundant vectorization and ensuring performance optimization.
    
    Details:
    - Enhanced `set_cells` to properly handle lambda conditions by vectorizing them to return a boolean array that matches the grid's shape.
    - Added functionality to detect and directly apply NumPy ufuncs in both `set_cells` and `modify_cells`, bypassing unnecessary vectorization.
    - Implemented a shape check in `set_cells` to ensure the condition results align with the grid's dimensions.
    - Introduced `ufunc_requires_additional_input`, a utility function that determines if a ufunc needs an extra input, enhancing the accuracy of operations in `modify_cells`.
    - Ensured that non-ufunc conditions and lambda functions are correctly handled with appropriate vectorization, maintaining backward compatibility and flexibility.
    - The changes result in a more robust and efficient PropertyLayer, capable of handling a variety of functions with improved performance.
    
    Impact:
    These updates resolve the lambda function bug and significantly boost the PropertyLayer's performance and reliability, especially when dealing with different types of functions. The codebase is now better equipped to handle complex grid operations efficiently and accurately.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    04718e7 View commit details
    Browse the repository at this point in the history
  26. Remove move functions from PropertGrid

    As can be seen in the tests, movement can be done by selecting cells (with select_cells_by_properties and select_extreme_value_cells) and by then moving to them with the new move_agent_to_one_of.
    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    a119cd9 View commit details
    Browse the repository at this point in the history
  27. Black formatting, ruff fixes

    EwoutH committed Dec 25, 2023
    Configuration menu
    Copy the full SHA
    b1e6a9b View commit details
    Browse the repository at this point in the history

Commits on Dec 29, 2023

  1. _Property_grid: Implement single select_cells method

    Implement a single select_cells method that incorperates the functionality of both select_cells_by_properties and select_extreme_value_cells.
    
    It now also allows multiple extreme values and multiple masks. Further, it adds a boolean to only include empty cells.
    EwoutH committed Dec 29, 2023
    Configuration menu
    Copy the full SHA
    3d752a4 View commit details
    Browse the repository at this point in the history
  2. _PropertyGrid: Keep empty_mask property, fix extreme_value bug

    Keeps a property empty_mask, that's updated together with the empties property. But because it's a mask it's incredibly fast for other mask-based operations.
    
    Also fix a bug in the extreme_value part of the select() method: It now only considers values from the mask.
    EwoutH committed Dec 29, 2023
    Configuration menu
    Copy the full SHA
    26d2c80 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    598bdea View commit details
    Browse the repository at this point in the history

Commits on Jan 6, 2024

  1. Configuration menu
    Copy the full SHA
    6e12379 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    627eccb View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    f5f5065 View commit details
    Browse the repository at this point in the history
  4. Remove stray print function

    rht committed Jan 6, 2024
    Configuration menu
    Copy the full SHA
    f96a9ef View commit details
    Browse the repository at this point in the history
  5. Remove unused PyLint directive

    rht committed Jan 6, 2024
    Configuration menu
    Copy the full SHA
    3e7bd7b View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    c71c9f5 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    363fb0b View commit details
    Browse the repository at this point in the history