From e1cd30a991f206a1835d7b0fc76591f7dbcbcfdd Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 29 Aug 2025 18:23:32 +0200 Subject: [PATCH 001/329] Add abstract name property and rename method to AgentSetDF for enhanced agent set management --- mesa_frames/abstract/agents.py | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/mesa_frames/abstract/agents.py b/mesa_frames/abstract/agents.py index f4243558..76a34de5 100644 --- a/mesa_frames/abstract/agents.py +++ b/mesa_frames/abstract/agents.py @@ -1098,6 +1098,44 @@ def __str__(self) -> str: def __reversed__(self) -> Iterator: return reversed(self._df) + @property + @abstractmethod + def name(self) -> str | None: + """Human-friendly name of this agent set. + + Returns + ------- + str | None + The explicit name if set; otherwise None. Names are owned by the + agent set itself and are not mutated by `AgentsDF`. + + Notes + ----- + - Names are optional. When not set, accessors like `agents.sets` may + display fallback keys derived from the class name for convenience. + - Use :meth:`rename` to change the name; direct assignment is not + supported. + """ + ... + + @abstractmethod + def rename(self, new_name: str) -> None: + """Rename this agent set. + + Parameters + ---------- + new_name : str + Desired new name. Implementations should ensure uniqueness within + the owning model's agents, typically by applying a numeric suffix + when a collision occurs (e.g., ``Sheep`` -> ``Sheep_1``). + + Notes + ----- + - Implementations must not mutate other agent sets' names. + - This method replaces direct name assignment for clarity and safety. + """ + ... + @property def df(self) -> DataFrame: return self._df From b385314b204defc58c8713ebf6e673bca8266d30 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 29 Aug 2025 18:25:02 +0200 Subject: [PATCH 002/329] Refactor agent retrieval in ModelDF to use dictionary access for improved performance and clarity --- mesa_frames/concrete/model.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index befc1812..7b627c87 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -112,10 +112,12 @@ def get_agents_of_type(self, agent_type: type) -> AgentSetDF: AgentSetDF The AgentSetDF of the specified type. """ - for agentset in self._agents._agentsets: - if isinstance(agentset, agent_type): - return agentset - raise ValueError(f"No agents of type {agent_type} found in the model.") + try: + return self.agents.sets[agent_type] + except KeyError as e: + raise ValueError( + f"No agents of type {agent_type} found in the model." + ) from e def reset_randomizer(self, seed: int | Sequence[int] | None) -> None: """Reset the model random number generator. @@ -196,7 +198,7 @@ def agent_types(self) -> list[type]: list[type] A list of the different agent types present in the model. """ - return [agent.__class__ for agent in self._agents._agentsets] + return [agent.__class__ for agent in self.agents.sets] @property def space(self) -> SpaceDF: From 69b56c17568406d9a66e9f8ed9af92adc00df133 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 29 Aug 2025 18:25:44 +0200 Subject: [PATCH 003/329] Enhance AgentSetPolars with unique naming and renaming capabilities --- mesa_frames/concrete/agentset.py | 76 +++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 81759b19..376285f1 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -65,7 +65,7 @@ def step(self): import numpy as np import polars as pl -from mesa_frames.concrete.agents import AgentSetDF +from mesa_frames.abstract.agents import AgentSetDF from mesa_frames.concrete.mixin import PolarsMixin from mesa_frames.concrete.model import ModelDF from mesa_frames.types_ import AgentPolarsMask, IntoExpr, PolarsIdsLike @@ -83,7 +83,9 @@ class AgentSetPolars(AgentSetDF, PolarsMixin): _copy_only_reference: list[str] = ["_model", "_mask"] _mask: pl.Expr | pl.Series - def __init__(self, model: mesa_frames.concrete.model.ModelDF) -> None: + def __init__( + self, model: mesa_frames.concrete.model.ModelDF, name: str | None = None + ) -> None: """Initialize a new AgentSetPolars. Parameters @@ -91,11 +93,81 @@ def __init__(self, model: mesa_frames.concrete.model.ModelDF) -> None: model : "mesa_frames.concrete.model.ModelDF" The model that the agent set belongs to. """ + # Model reference self._model = model + # Assign unique, human-friendly name (consider only explicitly named sets) + base = name if name is not None else self.__class__.__name__ + existing = {s.name for s in self.model.agents.sets if getattr(s, "name", None)} + unique = self._make_unique_name(base, existing) + if unique != base and name is not None: + import warnings + + warnings.warn( + f"AgentSetPolars with name '{base}' already exists; renamed to '{unique}'.", + UserWarning, + stacklevel=2, + ) + self._name = unique + # No definition of schema with unique_id, as it becomes hard to add new agents self._df = pl.DataFrame() self._mask = pl.repeat(True, len(self._df), dtype=pl.Boolean, eager=True) + @property + def name(self) -> str | None: + return getattr(self, "_name", None) + + def rename(self, new_name: str) -> None: + """Rename this agent set with collision-safe behavior. + + Parameters + ---------- + new_name : str + Desired new name. If it collides with an existing explicit name, + a numeric suffix is added (e.g., 'Sheep' -> 'Sheep_1'). + """ + if not isinstance(new_name, str): + raise TypeError("rename() expects a string name") + # Consider only explicitly named sets and exclude self's current name + existing = {s.name for s in self.model.agents.sets if getattr(s, "name", None)} + if self.name in existing: + existing.discard(self.name) + base = new_name + unique = self._make_unique_name(base, existing) + if unique != base: + import warnings + + warnings.warn( + f"AgentSetPolars with name '{base}' already exists; renamed to '{unique}'.", + UserWarning, + stacklevel=2, + ) + self._name = unique + + @staticmethod + def _make_unique_name(base: str, existing: set[str]) -> str: + if base not in existing: + return base + # If ends with _, increment; else append _1 + import re + + m = re.match(r"^(.*?)(?:_(\d+))$", base) + if m: + prefix, num = m.group(1), int(m.group(2)) + nxt = num + 1 + candidate = f"{prefix}_{nxt}" + while candidate in existing: + nxt += 1 + candidate = f"{prefix}_{nxt}" + return candidate + else: + candidate = f"{base}_1" + i = 1 + while candidate in existing: + i += 1 + candidate = f"{base}_{i}" + return candidate + def add( self, agents: pl.DataFrame | Sequence[Any] | dict[str, Any], From f04cfcf1ae17f6a98a199b215d76a806ae620dfa Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 29 Aug 2025 19:40:22 +0200 Subject: [PATCH 004/329] Add abstract base class for agent sets accessors with comprehensive API --- mesa_frames/abstract/accessors.py | 264 ++++++++++++++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100644 mesa_frames/abstract/accessors.py diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py new file mode 100644 index 00000000..d15beb2a --- /dev/null +++ b/mesa_frames/abstract/accessors.py @@ -0,0 +1,264 @@ +from __future__ import annotations + +from abc import ABC, abstractmethod +from collections.abc import Iterable, Iterator, Mapping +from typing import Any + +from mesa_frames.abstract.agents import AgentSetDF +from mesa_frames.types_ import KeyBy + + +class AgentSetsAccessorBase(ABC): + """Abstract accessor for collections of agent sets. + + This interface defines a flexible, user-friendly API to access agent sets + by name, positional index, or class/type, and to iterate or view the + collection under different key domains. + + Notes + ----- + Concrete implementations should: + - Support ``__getitem__`` with ``int`` | ``str`` | ``type[AgentSetDF]``. + - Return a list for type-based queries (even when there is one match). + - Provide keyed iteration via ``keys/items/iter/mapping`` with ``key_by``. + - Expose read-only snapshots ``by_name`` and ``by_type``. + + Examples + -------- + Assuming ``agents`` is an :class:`~mesa_frames.concrete.agents.AgentsDF`: + + >>> sheep = agents.sets["Sheep"] # name lookup + >>> first = agents.sets[0] # index lookup + >>> wolves = agents.sets[Wolf] # type lookup → list + >>> len(wolves) >= 0 + True + + Choose a key view when iterating: + + >>> for k, aset in agents.sets.items(key_by="index"): + ... print(k, aset.name) + 0 Sheep + 1 Wolf + """ + + @abstractmethod + def __getitem__(self, key: int | str | type[AgentSetDF]) -> AgentSetDF | list[AgentSetDF]: + """Retrieve agent set(s) by index, name, or type. + + Parameters + ---------- + key : int | str | type[AgentSetDF] + - ``int``: positional index (supports negative indices). + - ``str``: agent set name. + - ``type``: class or subclass of :class:`AgentSetDF`. + + Returns + ------- + AgentSetDF | list[AgentSetDF] + A single agent set for ``int``/``str`` keys; a list of matching + agent sets for ``type`` keys (possibly empty). + + Raises + ------ + IndexError + If an index is out of range. + KeyError + If a name is missing. + TypeError + If the key type is unsupported. + """ + + @abstractmethod + def get(self, key: int | str | type[AgentSetDF], default: Any | None = None) -> Any: + """Safe lookup variant that returns a default on miss. + + Parameters + ---------- + key : int | str | type[AgentSetDF] + Lookup key; see :meth:`__getitem__`. + default : Any, optional + Value to return when the lookup fails. If ``key`` is a type and no + matches are found, implementers may prefer returning ``[]`` when + ``default`` is ``None`` to keep list shape stable. + + Returns + ------- + Any + The resolved value or ``default``. + """ + + @abstractmethod + def first(self, t: type[AgentSetDF]) -> AgentSetDF: + """Return the first agent set matching a type. + + Parameters + ---------- + t : type[AgentSetDF] + The concrete class (or base class) to match. + + Returns + ------- + AgentSetDF + The first matching agent set in iteration order. + + Raises + ------ + KeyError + If no agent set matches ``t``. + + Examples + -------- + >>> agents.sets.first(Wolf) # doctest: +SKIP + + """ + + @abstractmethod + def all(self, t: type[AgentSetDF]) -> list[AgentSetDF]: + """Return all agent sets matching a type. + + Parameters + ---------- + t : type[AgentSetDF] + The concrete class (or base class) to match. + + Returns + ------- + list[AgentSetDF] + A list of all matching agent sets (possibly empty). + + Examples + -------- + >>> agents.sets.all(Wolf) # doctest: +SKIP + [, ] + """ + + @abstractmethod + def at(self, index: int) -> AgentSetDF: + """Return the agent set at a positional index. + + Parameters + ---------- + index : int + Positional index; negative indices are supported. + + Returns + ------- + AgentSetDF + The agent set at the given position. + + Raises + ------ + IndexError + If ``index`` is out of range. + + Examples + -------- + >>> agents.sets.at(0) is agents.sets[0] + True + """ + + @abstractmethod + def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: + """Iterate keys under a chosen key domain. + + Parameters + ---------- + key_by : {"name", "index", "object", "type"}, default "name" + - ``"name"`` → agent set names. + - ``"index"`` → positional indices. + - ``"object"`` → the :class:`AgentSetDF` objects. + - ``"type"`` → the concrete classes of each set. + + Returns + ------- + Iterable[Any] + An iterable of keys corresponding to the selected domain. + """ + + @abstractmethod + def items(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: + """Iterate ``(key, AgentSetDF)`` pairs under a chosen key domain. + + See :meth:`keys` for the meaning of ``key_by``. + """ + + @abstractmethod + def values(self) -> Iterable[AgentSetDF]: + """Iterate over agent set values only (no keys).""" + + @abstractmethod + def iter(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: + """Alias for :meth:`items` for convenience.""" + + @abstractmethod + def mapping(self, *, key_by: KeyBy = "name") -> dict[Any, AgentSetDF]: + """Return a dictionary view keyed by the chosen domain. + + Notes + ----- + ``key_by="type"`` will keep the last set per type. For one-to-many + grouping, prefer the read-only :attr:`by_type` snapshot. + """ + + @property + @abstractmethod + def by_name(self) -> Mapping[str, AgentSetDF]: + """Read-only mapping of names to agent sets. + + Returns + ------- + Mapping[str, AgentSetDF] + An immutable snapshot that maps each agent set name to its object. + + Notes + ----- + Implementations should return a read-only mapping such as + ``types.MappingProxyType`` over an internal dict to avoid accidental + mutation. + + Examples + -------- + >>> sheep = agents.sets.by_name["Sheep"] # doctest: +SKIP + >>> sheep is agents.sets["Sheep"] # doctest: +SKIP + True + """ + + @property + @abstractmethod + def by_type(self) -> Mapping[type, list[AgentSetDF]]: + """Read-only mapping of types to lists of agent sets. + + Returns + ------- + Mapping[type, list[AgentSetDF]] + An immutable snapshot grouping agent sets by their concrete class. + + Notes + ----- + This supports one-to-many relationships where multiple sets share the + same type. Prefer this over ``mapping(key_by="type")`` when you need + grouping instead of last-write-wins semantics. + """ + + @abstractmethod + def __contains__(self, x: str | AgentSetDF) -> bool: + """Return ``True`` if a name or object is present. + + Parameters + ---------- + x : str | AgentSetDF + A name to test by equality, or an object to test by identity. + + Returns + ------- + bool + ``True`` if present, else ``False``. + """ + + @abstractmethod + def __len__(self) -> int: + """Return number of agent sets in the collection.""" + + @abstractmethod + def __iter__(self) -> Iterator[AgentSetDF]: + """Iterate over agent set values in insertion order.""" From af0f27079e32eebc4662a3830b577c3e08401002 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 29 Aug 2025 19:46:54 +0200 Subject: [PATCH 005/329] Implement AgentSetsAccessor class for enhanced agent set management and access --- mesa_frames/concrete/accessors.py | 116 ++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 mesa_frames/concrete/accessors.py diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py new file mode 100644 index 00000000..c880973b --- /dev/null +++ b/mesa_frames/concrete/accessors.py @@ -0,0 +1,116 @@ +from collections import defaultdict +from collections.abc import Iterable, Iterator, Mapping +from types import MappingProxyType +from typing import Any, cast + +from types_ import KeyBy + +from mesa_frames.abstract.agents import AgentSetDF +from mesa_frames.concrete.agents import AgentsDF + + +class AgentSetsAccessor(AgentSetsAccessorBase): + def __init__(self, parent: "AgentsDF") -> None: + self._parent = parent + + def __getitem__( + self, key: int | str | type[AgentSetDF] + ) -> AgentSetDF | list[AgentSetDF]: + p = self._parent + if isinstance(key, int): + try: + return p._agentsets[key] + except IndexError as e: + raise IndexError( + f"Index {key} out of range for {len(p._agentsets)} agent sets" + ) from e + if isinstance(key, str): + for s in p._agentsets: + if s.name == key: + return s + available = [getattr(s, "name", None) for s in p._agentsets] + raise KeyError(f"No agent set named '{key}'. Available: {available}") + if isinstance(key, type): + return [s for s in p._agentsets if isinstance(s, key)] + raise TypeError("Key must be int | str | type[AgentSetDF]") + + def get( + self, key: int | str | type[AgentSetDF], default: Any | None = None + ) -> AgentSetDF | list[AgentSetDF] | Any | None: + try: + val = self[key] + if isinstance(key, type) and val == [] and default is None: + return [] + return val + except (KeyError, IndexError, TypeError): + # For type keys, preserve list shape by default + if isinstance(key, type) and default is None: + return [] + return default + + def first(self, t: type[AgentSetDF]) -> AgentSetDF: + matches = [s for s in self._parent._agentsets if isinstance(s, t)] + if not matches: + raise KeyError(f"No agent set of type {getattr(t, '__name__', t)} found.") + return matches[0] + + def all(self, t: type[AgentSetDF]) -> list[AgentSetDF]: + return [s for s in self._parent._agentsets if isinstance(s, t)] + + def at(self, index: int) -> AgentSetDF: + return self[index] # type: ignore[return-value] + + # ---------- key generation and views ---------- + def _gen_key(self, aset: AgentSetDF, idx: int, mode: str) -> Any: + if mode == "name": + return aset.name + if mode == "index": + return idx + if mode == "object": + return aset + if mode == "type": + return type(aset) + raise ValueError("key_by must be 'name'|'index'|'object'|'type'") + + def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: + for i, s in enumerate(self._parent._agentsets): + yield self._gen_key(s, i, key_by) + + def items(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: + for i, s in enumerate(self._parent._agentsets): + yield self._gen_key(s, i, key_by), s + + def values(self) -> Iterable[AgentSetDF]: + return iter(self._parent._agentsets) + + def iter(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: + return self.items(key_by=key_by) + + def mapping(self, *, key_by: KeyBy = "name") -> dict[Any, AgentSetDF]: + return {k: v for k, v in self.items(key_by=key_by)} + + # ---------- read-only snapshots ---------- + @property + def by_name(self) -> Mapping[str, AgentSetDF]: + return MappingProxyType({cast(str, s.name): s for s in self._parent._agentsets}) + + @property + def by_type(self) -> Mapping[type, list[AgentSetDF]]: + d: dict[type, list[AgentSetDF]] = defaultdict(list) + for s in self._parent._agentsets: + d[type(s)].append(s) + return MappingProxyType(dict(d)) + + # ---------- membership & iteration ---------- + def __contains__(self, x: str | AgentSetDF) -> bool: + if isinstance(x, str): + return any(s.name == x for s in self._parent._agentsets) + if isinstance(x, AgentSetDF): + return any(s is x for s in self._parent._agentsets) + return False + + def __len__(self) -> int: + return len(self._parent._agentsets) + + def __iter__(self) -> Iterator[AgentSetDF]: + return iter(self._parent._agentsets) From 4c60083e9b9d8455ffdfec2b10aee43583a3ee69 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 29 Aug 2025 20:02:32 +0200 Subject: [PATCH 006/329] Add KeyBy literal for common option types in type definitions --- mesa_frames/types_.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mesa_frames/types_.py b/mesa_frames/types_.py index 05ab1b3f..f0c515ca 100644 --- a/mesa_frames/types_.py +++ b/mesa_frames/types_.py @@ -83,6 +83,9 @@ ArrayLike = ndarray | Series | Sequence Infinity = Annotated[float, IsEqual[math.inf]] # Only accepts math.inf +# Common option types +KeyBy = Literal["name", "index", "object", "type"] + ###----- Time ------### TimeT = float | int From 637f56028e03f96c1ffd26af383f4391bf78b62c Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 29 Aug 2025 20:17:57 +0200 Subject: [PATCH 007/329] Refactor AgentSetsAccessor to use direct access to agent sets for improved clarity and performance --- mesa_frames/abstract/mixin.py | 10 +++++ mesa_frames/concrete/accessors.py | 24 +++++----- mesa_frames/concrete/agents.py | 73 +++++++++++++++++++------------ 3 files changed, 68 insertions(+), 39 deletions(-) diff --git a/mesa_frames/abstract/mixin.py b/mesa_frames/abstract/mixin.py index 84b4ec7b..5c311ef7 100644 --- a/mesa_frames/abstract/mixin.py +++ b/mesa_frames/abstract/mixin.py @@ -66,6 +66,10 @@ class CopyMixin(ABC): _copy_only_reference: list[str] = [ "_model", ] + # Attributes listed here are not copied at all and will not be set + # on the copied object. Useful for lazily re-creating cyclic or + # parent-bound helpers (e.g., accessors) after copy/deepcopy. + _skip_copy: list[str] = [] @abstractmethod def __init__(self): ... @@ -113,6 +117,7 @@ def copy( for k, v in attributes.items() if k not in self._copy_with_method and k not in self._copy_only_reference + and k not in self._skip_copy and k not in skip ] else: @@ -121,15 +126,20 @@ def copy( for k, v in self.__dict__.items() if k not in self._copy_with_method and k not in self._copy_only_reference + and k not in self._skip_copy and k not in skip ] # Copy attributes with a reference only for attr in self._copy_only_reference: + if attr in self._skip_copy or attr in skip: + continue setattr(obj, attr, getattr(self, attr)) # Copy attributes with a specified method for attr in self._copy_with_method: + if attr in self._skip_copy or attr in skip: + continue attr_obj = getattr(self, attr) attr_copy_method, attr_copy_args = self._copy_with_method[attr] setattr(obj, attr, getattr(attr_obj, attr_copy_method)(*attr_copy_args)) diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index c880973b..0c3364b1 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -1,12 +1,13 @@ +from __future__ import annotations + from collections import defaultdict from collections.abc import Iterable, Iterator, Mapping from types import MappingProxyType from typing import Any, cast -from types_ import KeyBy - +from mesa_frames.types_ import KeyBy from mesa_frames.abstract.agents import AgentSetDF -from mesa_frames.concrete.agents import AgentsDF +from mesa_frames.abstract.accessors import AgentSetsAccessorBase class AgentSetsAccessor(AgentSetsAccessorBase): @@ -16,22 +17,22 @@ def __init__(self, parent: "AgentsDF") -> None: def __getitem__( self, key: int | str | type[AgentSetDF] ) -> AgentSetDF | list[AgentSetDF]: - p = self._parent + sets = self._parent._agentsets if isinstance(key, int): try: - return p._agentsets[key] + return sets[key] except IndexError as e: raise IndexError( - f"Index {key} out of range for {len(p._agentsets)} agent sets" + f"Index {key} out of range for {len(sets)} agent sets" ) from e if isinstance(key, str): - for s in p._agentsets: + for s in sets: if s.name == key: return s - available = [getattr(s, "name", None) for s in p._agentsets] + available = [getattr(s, "name", None) for s in sets] raise KeyError(f"No agent set named '{key}'. Available: {available}") if isinstance(key, type): - return [s for s in p._agentsets if isinstance(s, key)] + return [s for s in sets if isinstance(s, key)] raise TypeError("Key must be int | str | type[AgentSetDF]") def get( @@ -103,10 +104,11 @@ def by_type(self) -> Mapping[type, list[AgentSetDF]]: # ---------- membership & iteration ---------- def __contains__(self, x: str | AgentSetDF) -> bool: + sets = self._parent._agentsets if isinstance(x, str): - return any(s.name == x for s in self._parent._agentsets) + return any(s.name == x for s in sets) if isinstance(x, AgentSetDF): - return any(s is x for s in self._parent._agentsets) + return any(s is x for s in sets) return False def __len__(self) -> int: diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index 799a7b33..b6c305c5 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -46,7 +46,6 @@ def step(self): from __future__ import annotations # For forward references -from collections import defaultdict from collections.abc import Callable, Collection, Iterable, Iterator, Sequence from typing import Any, Literal, Self, cast, overload @@ -54,6 +53,7 @@ def step(self): import polars as pl from mesa_frames.abstract.agents import AgentContainer, AgentSetDF +from mesa_frames.concrete.accessors import AgentSetsAccessor from mesa_frames.types_ import ( AgentMask, AgnosticAgentMask, @@ -61,6 +61,7 @@ def step(self): DataFrame, IdsLike, Index, + KeyBy, Series, ) @@ -68,6 +69,9 @@ def step(self): class AgentsDF(AgentContainer): """A collection of AgentSetDFs. All agents of the model are stored here.""" + # Do not copy the accessor; it holds a reference to this instance and is + # cheaply re-created on demand via the `sets` property. + _skip_copy: list[str] = ["_sets_accessor"] _agentsets: list[AgentSetDF] _ids: pl.Series @@ -80,11 +84,29 @@ def __init__(self, model: mesa_frames.concrete.model.ModelDF) -> None: The model associated with the AgentsDF. """ self._model = model - self._agentsets = [] + self._agentsets = [] # internal storage; used by AgentSetsAccessor self._ids = pl.Series(name="unique_id", dtype=pl.UInt64) + # Accessor is created lazily in the property to survive copy/deepcopy + self._sets_accessor = AgentSetsAccessor(self) + + @property + def sets(self) -> AgentSetsAccessor: + """Accessor for agentset lookup by index/name/type. + + Does not conflict with AgentsDF's existing __getitem__ column API. + """ + # Ensure accessor always points to this instance (robust to copy/deepcopy) + acc = getattr(self, "_sets_accessor", None) + if acc is None or getattr(acc, "_parent", None) is not self: + acc = AgentSetsAccessor(self) + self._sets_accessor = acc + return acc + def add( - self, agents: AgentSetDF | Iterable[AgentSetDF], inplace: bool = True + self, + agents: AgentSetDF | Iterable[AgentSetDF], + inplace: bool = True, ) -> Self: """Add an AgentSetDF to the AgentsDF. @@ -205,9 +227,16 @@ def get( self, attr_names: str | Collection[str] | None = None, mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, - ) -> dict[AgentSetDF, Series] | dict[AgentSetDF, DataFrame]: + key_by: KeyBy = "object", + ) -> ( + dict[AgentSetDF, Series] + | dict[AgentSetDF, DataFrame] + | dict[str, Any] + | dict[int, Any] + | dict[type, Any] + ): agentsets_masks = self._get_bool_masks(mask) - result = {} + result: dict[AgentSetDF, Any] = {} # Convert attr_names to list for consistent checking if attr_names is None: @@ -228,7 +257,17 @@ def get( ): result[agentset] = agentset.get(attr_names, mask) - return result + if key_by == "object": + return result + elif key_by == "name": + return {cast(AgentSetDF, a).name: v for a, v in result.items()} # type: ignore[return-value] + elif key_by == "index": + index_map = {agentset: i for i, agentset in enumerate(self._agentsets)} + return {index_map[a]: v for a, v in result.items()} # type: ignore[return-value] + elif key_by == "type": + return {type(a): v for a, v in result.items()} # type: ignore[return-value] + else: + raise ValueError("key_by must be one of 'object', 'name', 'index', or 'type'") def remove( self, @@ -601,28 +640,6 @@ def active_agents( ) -> None: self.select(agents, inplace=True) - @property - def agentsets_by_type(self) -> dict[type[AgentSetDF], Self]: - """Get the agent sets in the AgentsDF grouped by type. - - Returns - ------- - dict[type[AgentSetDF], Self] - A dictionary mapping agent set types to the corresponding AgentsDF. - """ - - def copy_without_agentsets() -> Self: - return self.copy(deep=False, skip=["_agentsets"]) - - dictionary = defaultdict(copy_without_agentsets) - - for agentset in self._agentsets: - agents_df = dictionary[agentset.__class__] - agents_df._agentsets = [] - agents_df._agentsets = agents_df._agentsets + [agentset] - dictionary[agentset.__class__] = agents_df - return dictionary - @property def inactive_agents(self) -> dict[AgentSetDF, DataFrame]: return {agentset: agentset.inactive_agents for agentset in self._agentsets} From f823400a1cb1a3fae55b6d0e924003e56b4031ce Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 07:19:03 +0200 Subject: [PATCH 008/329] Fix type hint in constructor and improve default handling in get method for AgentSetsAccessor --- mesa_frames/concrete/accessors.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index 0c3364b1..b0961993 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -11,7 +11,7 @@ class AgentSetsAccessor(AgentSetsAccessorBase): - def __init__(self, parent: "AgentsDF") -> None: + def __init__(self, parent: mesa_frames.concrete.agents.AgentsDF) -> None: self._parent = parent def __getitem__( @@ -40,11 +40,13 @@ def get( ) -> AgentSetDF | list[AgentSetDF] | Any | None: try: val = self[key] - if isinstance(key, type) and val == [] and default is None: - return [] + # For type keys: if no matches and a default was provided, return the default; + # if no default, preserve list shape and return []. + if isinstance(key, type) and isinstance(val, list) and len(val) == 0: + return [] if default is None else default return val except (KeyError, IndexError, TypeError): - # For type keys, preserve list shape by default + # For type keys, preserve list shape by default when default is None if isinstance(key, type) and default is None: return [] return default From f190d86c98cf2b5943ee14282e60beca4c9da953 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 08:10:37 +0200 Subject: [PATCH 009/329] Remove redundant test for agent sets by type in Test_AgentsDF --- tests/test_agents.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/test_agents.py b/tests/test_agents.py index 414bb632..8151fe8e 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -1002,18 +1002,6 @@ def test_active_agents(self, fix_AgentsDF: AgentsDF): ) ) - def test_agentsets_by_type(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF - - result = agents.agentsets_by_type - assert isinstance(result, dict) - assert isinstance(result[ExampleAgentSetPolars], AgentsDF) - - assert ( - result[ExampleAgentSetPolars]._agentsets[0].df.rows() - == agents._agentsets[1].df.rows() - ) - def test_inactive_agents(self, fix_AgentsDF: AgentsDF): agents = fix_AgentsDF From 85effbc2bfc1d1e8e0b160c2ad45f3c6b5da8c1d Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 09:48:39 +0200 Subject: [PATCH 010/329] Add rename method to AgentSetsAccessor for agent set renaming with conflict handling --- mesa_frames/concrete/accessors.py | 45 ++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index b0961993..dc8e3bc3 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -3,7 +3,7 @@ from collections import defaultdict from collections.abc import Iterable, Iterator, Mapping from types import MappingProxyType -from typing import Any, cast +from typing import Any, Literal, cast from mesa_frames.types_ import KeyBy from mesa_frames.abstract.agents import AgentSetDF @@ -105,6 +105,49 @@ def by_type(self) -> Mapping[type, list[AgentSetDF]]: return MappingProxyType(dict(d)) # ---------- membership & iteration ---------- + def rename( + self, + target: AgentSetDF | str | dict[AgentSetDF | str, str] | list[tuple[AgentSetDF | str, str]], + new_name: str | None = None, + *, + on_conflict: Literal["canonicalize", "raise"] = "canonicalize", + mode: Literal["atomic", "best_effort"] = "atomic", + ) -> str | dict[AgentSetDF, str]: + """ + Rename agent sets. Supports single and batch renaming with deterministic conflict handling. + + Parameters + ---------- + target : AgentSetDF | str | dict[AgentSetDF | str, str] | list[tuple[AgentSetDF | str, str]] + Either: + - Single: AgentSet or name string (must provide new_name) + - Batch: {target: new_name} dict or [(target, new_name), ...] list + new_name : str | None, optional + New name (only used for single renames) + on_conflict : "canonicalize" | "raise", default "canonicalize" + Conflict resolution: "canonicalize" appends suffixes, "raise" raises ValueError + mode : "atomic" | "best_effort", default "atomic" + Rename mode: "atomic" applies all or none, "best_effort" skips failed renames + + Returns + ------- + str | dict[AgentSetDF, str] + Single rename: final name string + Batch: {agentset: final_name} mapping + + Examples + -------- + Single rename: + >>> agents.sets.rename("old_name", "new_name") + + Batch rename (dict): + >>> agents.sets.rename({"set1": "new_name", "set2": "another_name"}) + + Batch rename (list): + >>> agents.sets.rename([("set1", "new_name"), ("set2", "another_name")]) + """ + return self._parent._rename_set(target, new_name, on_conflict=on_conflict, mode=mode) + def __contains__(self, x: str | AgentSetDF) -> bool: sets = self._parent._agentsets if isinstance(x, str): From 53cd1d2d9322f60e60b3e2c873062e808143b529 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 09:57:51 +0200 Subject: [PATCH 011/329] Refactor name handling in AgentSetPolars to simplify uniqueness management and enhance rename method for better delegation to AgentsDF. --- mesa_frames/concrete/agentset.py | 84 ++++++++++---------------------- 1 file changed, 26 insertions(+), 58 deletions(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 376285f1..e0afedca 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -92,22 +92,14 @@ def __init__( ---------- model : "mesa_frames.concrete.model.ModelDF" The model that the agent set belongs to. + name : str | None, optional + Proposed name for this agent set. Uniqueness is not guaranteed here + and will be validated only when added to AgentsDF. """ # Model reference self._model = model - # Assign unique, human-friendly name (consider only explicitly named sets) - base = name if name is not None else self.__class__.__name__ - existing = {s.name for s in self.model.agents.sets if getattr(s, "name", None)} - unique = self._make_unique_name(base, existing) - if unique != base and name is not None: - import warnings - - warnings.warn( - f"AgentSetPolars with name '{base}' already exists; renamed to '{unique}'.", - UserWarning, - stacklevel=2, - ) - self._name = unique + # Set proposed name (no uniqueness guarantees here) + self._name = name if name is not None else self.__class__.__name__ # No definition of schema with unique_id, as it becomes hard to add new agents self._df = pl.DataFrame() @@ -117,56 +109,32 @@ def __init__( def name(self) -> str | None: return getattr(self, "_name", None) - def rename(self, new_name: str) -> None: - """Rename this agent set with collision-safe behavior. + def rename(self, new_name: str) -> str: + """Rename this agent set. If attached to AgentsDF, delegate for uniqueness enforcement. Parameters ---------- new_name : str - Desired new name. If it collides with an existing explicit name, - a numeric suffix is added (e.g., 'Sheep' -> 'Sheep_1'). + Desired new name. + + Returns + ------- + str + The final name used (may be canonicalized if duplicates exist). + + Raises + ------ + ValueError + If name conflicts occur and delegate encounters errors. """ - if not isinstance(new_name, str): - raise TypeError("rename() expects a string name") - # Consider only explicitly named sets and exclude self's current name - existing = {s.name for s in self.model.agents.sets if getattr(s, "name", None)} - if self.name in existing: - existing.discard(self.name) - base = new_name - unique = self._make_unique_name(base, existing) - if unique != base: - import warnings - - warnings.warn( - f"AgentSetPolars with name '{base}' already exists; renamed to '{unique}'.", - UserWarning, - stacklevel=2, - ) - self._name = unique - - @staticmethod - def _make_unique_name(base: str, existing: set[str]) -> str: - if base not in existing: - return base - # If ends with _, increment; else append _1 - import re - - m = re.match(r"^(.*?)(?:_(\d+))$", base) - if m: - prefix, num = m.group(1), int(m.group(2)) - nxt = num + 1 - candidate = f"{prefix}_{nxt}" - while candidate in existing: - nxt += 1 - candidate = f"{prefix}_{nxt}" - return candidate - else: - candidate = f"{base}_1" - i = 1 - while candidate in existing: - i += 1 - candidate = f"{base}_{i}" - return candidate + # Always delegate to the container's accessor if available through the model's agents + # Check if we have a model and can find the AgentsDF that contains this set + if self in self.model.agents.sets: + return self.model.agents.sets.rename(self._name, new_name) + + # Set name locally if no container found + self._name = new_name + return new_name def add( self, From 3cf2c067b416ae05c0c09a386759a841b009eb56 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 10:02:29 +0200 Subject: [PATCH 012/329] Implement unique name generation and canonicalization for agent sets in AgentsDF --- mesa_frames/concrete/agents.py | 109 +++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 4 deletions(-) diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index b6c305c5..cb055475 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -103,12 +103,103 @@ def sets(self) -> AgentSetsAccessor: return acc + @staticmethod + def _make_unique_name(base: str, existing: set[str]) -> str: + """Generate a unique name by appending numeric suffix if needed.""" + if base not in existing: + return base + # If ends with _, increment; else append _1 + import re + + m = re.match(r"^(.*?)(?:_(\d+))$", base) + if m: + prefix, num = m.group(1), int(m.group(2)) + nxt = num + 1 + candidate = f"{prefix}_{nxt}" + while candidate in existing: + nxt += 1 + candidate = f"{prefix}_{nxt}" + return candidate + else: + candidate = f"{base}_1" + i = 1 + while candidate in existing: + i += 1 + candidate = f"{base}_{i}" + return candidate + + def _canonicalize_names(self, new_agentsets: list[AgentSetDF]) -> None: + """Canonicalize names across existing + new agent sets, ensuring uniqueness.""" + existing_names = {s.name for s in self._agentsets} + + # Process each new agent set in batch to handle potential conflicts + for aset in new_agentsets: + # Use the static method to generate unique name + unique_name = self._make_unique_name(aset.name, existing_names) + if unique_name != aset.name: + # Directly set the name instead of calling rename + import warnings + warnings.warn( + f"AgentSet with name '{aset.name}' already exists; renamed to '{unique_name}'.", + UserWarning, + stacklevel=2, + ) + aset._name = unique_name + existing_names.add(unique_name) + + def _rename_set(self, target: AgentSetDF, new_name: str, + on_conflict: Literal['error', 'skip', 'overwrite'] = 'error', + mode: Literal['atomic'] = 'atomic') -> str: + """Internal rename method for handling delegations from accessor. + + Parameters + ---------- + target : AgentSetDF + The agent set to rename + new_name : str + The new name for the agent set + on_conflict : {'error', 'skip', 'overwrite'}, optional + How to handle naming conflicts, by default 'error' + mode : {'atomic'}, optional + Rename mode, by default 'atomic' + + Returns + ------- + str + The final name assigned to the agent set + + Raises + ------ + ValueError + If target is not in this container or other validation errors + KeyError + If on_conflict='error' and new_name conflicts with existing set + """ + # Validate target is in this container + if target not in self._agentsets: + raise ValueError(f"AgentSet {target} is not in this container") + + # Check for conflicts with existing names (excluding current target) + existing_names = {s.name for s in self._agentsets if s is not target} + if new_name in existing_names: + if on_conflict == 'error': + raise KeyError(f"AgentSet name '{new_name}' already exists") + elif on_conflict == 'skip': + # Return existing name without changes + return target._name + # on_conflict == 'overwrite' - proceed with rename + + # Apply name canonicalization if needed + final_name = self._make_unique_name(new_name, existing_names) + target._name = final_name + return final_name + def add( self, agents: AgentSetDF | Iterable[AgentSetDF], inplace: bool = True, ) -> Self: - """Add an AgentSetDF to the AgentsDF. + """Add an AgentSetDF to the AgentsDF (only gate for name validation). Parameters ---------- @@ -131,13 +222,23 @@ def add( other_list = obj._return_agentsets_list(agents) if obj._check_agentsets_presence(other_list).any(): raise ValueError("Some agentsets are already present in the AgentsDF.") - new_ids = pl.concat( - [obj._ids] + [pl.Series(agentset["unique_id"]) for agentset in other_list] - ) + + # Validate and canonicalize names across existing + batch before mutating + obj._canonicalize_names(other_list) + + # Collect unique_ids from agent sets that have them (may be empty at this point) + new_ids_list = [obj._ids] + for agentset in other_list: + if len(agentset) > 0: # Only include if there are agents in the set + new_ids_list.append(agentset["unique_id"]) + + new_ids = pl.concat(new_ids_list) if new_ids.is_duplicated().any(): raise ValueError("Some of the agent IDs are not unique.") + obj._agentsets.extend(other_list) obj._ids = new_ids + return obj @overload From a6e92ab3be1d09e50ab40b0202fd25cde1cd1009 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 10:13:25 +0200 Subject: [PATCH 013/329] Enhance type handling in AgentSetsAccessor to provide detailed error messages for key lookups --- mesa_frames/concrete/accessors.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index dc8e3bc3..0a5f6ecb 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -32,7 +32,20 @@ def __getitem__( available = [getattr(s, "name", None) for s in sets] raise KeyError(f"No agent set named '{key}'. Available: {available}") if isinstance(key, type): - return [s for s in sets if isinstance(s, key)] + matches = [s for s in sets if isinstance(s, key)] + if len(matches) == 0: + # No matches - list available agent set types + available_types = list(set(type(s).__name__ for s in sets)) + raise KeyError(f"No agent set of type {getattr(key, '__name__', key)} found. " + f"Available agent set types: {available_types}") + elif len(matches) == 1: + # Single match - return it directly + return matches[0] + else: + # Multiple matches - list all matching agent sets + match_names = [s.name for s in matches] + raise ValueError(f"Multiple agent sets ({len(matches)}) of type {getattr(key, '__name__', key)} found. " + f"Matching agent sets: {matches}") raise TypeError("Key must be int | str | type[AgentSetDF]") def get( From 4aaaf4728081083b8aedf195de66dda69fdb50ab Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 10:13:55 +0200 Subject: [PATCH 014/329] Enhance error handling in AgentsDF by providing available agent set names in ValueError for better debugging --- mesa_frames/concrete/agents.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index cb055475..be6035ee 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -177,7 +177,9 @@ def _rename_set(self, target: AgentSetDF, new_name: str, """ # Validate target is in this container if target not in self._agentsets: - raise ValueError(f"AgentSet {target} is not in this container") + available_names = [s.name for s in self._agentsets] + raise ValueError(f"AgentSet {target} is not in this container. " + f"Available agent sets: {available_names}") # Check for conflicts with existing names (excluding current target) existing_names = {s.name for s in self._agentsets if s is not target} From d6493019f0b711c12d17d635807aada6aadcace6 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 10:24:46 +0200 Subject: [PATCH 015/329] Add mesa package to development dependencies in uv.lock --- uv.lock | 2 ++ 1 file changed, 2 insertions(+) diff --git a/uv.lock b/uv.lock index f09db044..a72164c0 100644 --- a/uv.lock +++ b/uv.lock @@ -1258,6 +1258,7 @@ dev = [ docs = [ { name = "autodocsumm" }, { name = "beartype" }, + { name = "mesa" }, { name = "mkdocs-git-revision-date-localized-plugin" }, { name = "mkdocs-include-markdown-plugin" }, { name = "mkdocs-jupyter" }, @@ -1319,6 +1320,7 @@ dev = [ docs = [ { name = "autodocsumm", specifier = ">=0.2.14" }, { name = "beartype", specifier = ">=0.21.0" }, + { name = "mesa", specifier = ">=3.2.0" }, { name = "mkdocs-git-revision-date-localized-plugin", specifier = ">=1.4.7" }, { name = "mkdocs-include-markdown-plugin", specifier = ">=7.1.5" }, { name = "mkdocs-jupyter", specifier = ">=0.25.1" }, From 951d5b6a8a27ff99be7e69e8432721805e396593 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 10:24:57 +0200 Subject: [PATCH 016/329] Refactor __getitem__ method in AgentSetsAccessor to return matching agent sets as a list for multiple matches and improve error messaging for better clarity. --- mesa_frames/concrete/accessors.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index 0a5f6ecb..27d15c73 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -37,15 +37,13 @@ def __getitem__( # No matches - list available agent set types available_types = list(set(type(s).__name__ for s in sets)) raise KeyError(f"No agent set of type {getattr(key, '__name__', key)} found. " - f"Available agent set types: {available_types}") + f"Available agent set types: {available_types}") elif len(matches) == 1: # Single match - return it directly return matches[0] else: - # Multiple matches - list all matching agent sets - match_names = [s.name for s in matches] - raise ValueError(f"Multiple agent sets ({len(matches)}) of type {getattr(key, '__name__', key)} found. " - f"Matching agent sets: {matches}") + # Multiple matches - return all matching agent sets as list + return matches raise TypeError("Key must be int | str | type[AgentSetDF]") def get( From c5c8430ee289ac1cd46ecedcfe874be659332910 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 10:25:15 +0200 Subject: [PATCH 017/329] Add comprehensive tests for AgentSetsAccessor methods to ensure correct functionality and error handling --- tests/test_sets_accessor.py | 145 ++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 tests/test_sets_accessor.py diff --git a/tests/test_sets_accessor.py b/tests/test_sets_accessor.py new file mode 100644 index 00000000..34ab8d96 --- /dev/null +++ b/tests/test_sets_accessor.py @@ -0,0 +1,145 @@ +from copy import copy, deepcopy + +import pytest + +from mesa_frames import AgentsDF, ModelDF +from tests.test_agentset import ExampleAgentSetPolars, fix1_AgentSetPolars, fix2_AgentSetPolars +from tests.test_agents import fix_AgentsDF + + +class TestAgentSetsAccessor: + def test___getitem__(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + # int + assert agents.sets[0] is s1 + assert agents.sets[1] is s2 + with pytest.raises(IndexError): + _ = agents.sets[2] + # str + assert agents.sets[s1.name] is s1 + assert agents.sets[s2.name] is s2 + with pytest.raises(KeyError): + _ = agents.sets["__missing__"] + # type → always list + lst = agents.sets[ExampleAgentSetPolars] + assert isinstance(lst, list) + assert s1 in lst and s2 in lst and len(lst) == 2 + + def test_get(self, fix_AgentsDF): + agents = fix_AgentsDF + assert agents.sets.get("__missing__") is None + assert agents.sets.get(999, default="x") == "x" + + class Temp(ExampleAgentSetPolars): + pass + + assert agents.sets.get(Temp) == [] + assert agents.sets.get(Temp, default=None) == [] + assert agents.sets.get(Temp, default=["fallback"]) == ["fallback"] + + def test_first(self, fix_AgentsDF): + agents = fix_AgentsDF + assert agents.sets.first(ExampleAgentSetPolars) is agents.sets[0] + class Temp(ExampleAgentSetPolars): + pass + with pytest.raises(KeyError): + agents.sets.first(Temp) + + def test_all(self, fix_AgentsDF): + agents = fix_AgentsDF + assert agents.sets.all(ExampleAgentSetPolars) == [agents.sets[0], agents.sets[1]] + class Temp(ExampleAgentSetPolars): + pass + assert agents.sets.all(Temp) == [] + + def test_at(self, fix_AgentsDF): + agents = fix_AgentsDF + assert agents.sets.at(0) is agents.sets[0] + assert agents.sets.at(1) is agents.sets[1] + + def test_keys(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + assert list(agents.sets.keys(key_by="index")) == [0, 1] + assert list(agents.sets.keys(key_by="object")) == [s1, s2] + assert list(agents.sets.keys(key_by="name")) == [s1.name, s2.name] + assert list(agents.sets.keys(key_by="type")) == [type(s1), type(s2)] + + def test_items(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + assert list(agents.sets.items(key_by="index")) == [(0, s1), (1, s2)] + assert list(agents.sets.items(key_by="object")) == [(s1, s1), (s2, s2)] + + def test_values(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + assert list(agents.sets.values()) == [s1, s2] + + def test_iter(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + assert list(agents.sets.iter(key_by="name")) == [(s1.name, s1), (s2.name, s2)] + + def test_mapping(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + by_type_map = agents.sets.mapping(key_by="type") + assert list(by_type_map.keys()) == [type(s1)] + assert by_type_map[type(s1)] is s2 + + def test_by_name(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + name_map = agents.sets.by_name + assert name_map[s1.name] is s1 + assert name_map[s2.name] is s2 + with pytest.raises(TypeError): + name_map["X"] = s1 # type: ignore[index] + + def test_by_type(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + grouped = agents.sets.by_type + assert list(grouped.keys()) == [type(s1)] + assert grouped[type(s1)] == [s1, s2] + + def test___contains__(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + assert s1.name in agents.sets + assert s2.name in agents.sets + assert s1 in agents.sets and s2 in agents.sets + + def test___len__(self, fix_AgentsDF): + agents = fix_AgentsDF + assert len(agents.sets) == 2 + + def test___iter__(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + assert list(iter(agents.sets)) == [s1, s2] + + def test_copy_and_deepcopy_rebinds_accessor(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + a2 = copy(agents) + acc2 = a2.sets # lazily created + assert acc2._parent is a2 + assert acc2 is not agents.sets + a3 = deepcopy(agents) + acc3 = a3.sets # lazily created + assert acc3._parent is a3 + assert acc3 is not agents.sets and acc3 is not acc2 From d0a592a2c07e6cff12ba1252842075c4814e6364 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 10:49:09 +0200 Subject: [PATCH 018/329] Rename AgentSetsAccessorBase to AbstractAgentSetsAccessor for consistency and clarity in the abstract class naming. --- mesa_frames/abstract/accessors.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index d15beb2a..83c1392e 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -8,7 +8,7 @@ from mesa_frames.types_ import KeyBy -class AgentSetsAccessorBase(ABC): +class AbstractAgentSetsAccessor(ABC): """Abstract accessor for collections of agent sets. This interface defines a flexible, user-friendly API to access agent sets @@ -42,7 +42,9 @@ class AgentSetsAccessorBase(ABC): """ @abstractmethod - def __getitem__(self, key: int | str | type[AgentSetDF]) -> AgentSetDF | list[AgentSetDF]: + def __getitem__( + self, key: int | str | type[AgentSetDF] + ) -> AgentSetDF | list[AgentSetDF]: """Retrieve agent set(s) by index, name, or type. Parameters From cf16fb63b2fe83ecb45e55e6eb33b26a4e18ee0c Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 10:51:23 +0200 Subject: [PATCH 019/329] Refactor AgentSetsAccessor to extend AbstractAgentSetsAccessor for improved consistency and clarity; enhance error messaging in __getitem__ and rename methods for better readability. --- mesa_frames/concrete/accessors.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index 27d15c73..b87ef19a 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -5,12 +5,12 @@ from types import MappingProxyType from typing import Any, Literal, cast -from mesa_frames.types_ import KeyBy +from mesa_frames.abstract.accessors import AbstractAgentSetsAccessor from mesa_frames.abstract.agents import AgentSetDF -from mesa_frames.abstract.accessors import AgentSetsAccessorBase +from mesa_frames.types_ import KeyBy -class AgentSetsAccessor(AgentSetsAccessorBase): +class AgentSetsAccessor(AbstractAgentSetsAccessor): def __init__(self, parent: mesa_frames.concrete.agents.AgentsDF) -> None: self._parent = parent @@ -36,8 +36,10 @@ def __getitem__( if len(matches) == 0: # No matches - list available agent set types available_types = list(set(type(s).__name__ for s in sets)) - raise KeyError(f"No agent set of type {getattr(key, '__name__', key)} found. " - f"Available agent set types: {available_types}") + raise KeyError( + f"No agent set of type {getattr(key, '__name__', key)} found. " + f"Available agent set types: {available_types}" + ) elif len(matches) == 1: # Single match - return it directly return matches[0] @@ -118,7 +120,10 @@ def by_type(self) -> Mapping[type, list[AgentSetDF]]: # ---------- membership & iteration ---------- def rename( self, - target: AgentSetDF | str | dict[AgentSetDF | str, str] | list[tuple[AgentSetDF | str, str]], + target: AgentSetDF + | str + | dict[AgentSetDF | str, str] + | list[tuple[AgentSetDF | str, str]], new_name: str | None = None, *, on_conflict: Literal["canonicalize", "raise"] = "canonicalize", @@ -157,7 +162,9 @@ def rename( Batch rename (list): >>> agents.sets.rename([("set1", "new_name"), ("set2", "another_name")]) """ - return self._parent._rename_set(target, new_name, on_conflict=on_conflict, mode=mode) + return self._parent._rename_set( + target, new_name, on_conflict=on_conflict, mode=mode + ) def __contains__(self, x: str | AgentSetDF) -> bool: sets = self._parent._agentsets From 95bb9af14faaaf8708962fe42203521ece8be876 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 30 Aug 2025 08:52:23 +0000 Subject: [PATCH 020/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- AGENTS.md | 6 ++++++ mesa_frames/concrete/accessors.py | 2 +- mesa_frames/concrete/agents.py | 26 +++++++++++++++++--------- mesa_frames/concrete/agentset.py | 2 +- tests/test_sets_accessor.py | 15 +++++++++++++-- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 9bc4999c..19b3caa8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,6 +1,7 @@ # Repository Guidelines ## Project Structure & Module Organization + - `mesa_frames/`: Source package. - `abstract/` and `concrete/`: Core APIs and implementations. - Key modules: `agents.py`, `agentset.py`, `space.py`, `datacollector.py`, `types_.py`. @@ -9,6 +10,7 @@ - `examples/`: Reproducible demo models and performance scripts. ## Build, Test, and Development Commands + - Install (dev stack): `uv sync` (always use uv) - Lint & format: `uv run ruff check . --fix && uv run ruff format .` - Tests (quiet + coverage): `export MESA_FRAMES_RUNTIME_TYPECHECKING = 1 && uv run pytest -q --cov=mesa_frames --cov-report=term-missing` @@ -18,23 +20,27 @@ Always run tools via uv: `uv run `. ## Coding Style & Naming Conventions + - Python 3.11+, 4-space indent, type hints required for public APIs. - Docstrings: NumPy style (validated by Ruff/pydoclint). - Formatting/linting: Ruff (formatter + lints). Fix on save if your IDE supports it. - Names: `CamelCase` for classes, `snake_case` for functions/attributes, tests as `test_.py` with `Test` groups. ## Testing Guidelines + - Framework: Pytest; place tests under `tests/` mirroring module paths. - Conventions: One test module per feature; name tests `test_`. - Coverage: Aim to exercise new branches and error paths; keep `--cov=mesa_frames` green. - Run fast locally: `pytest -q` or `uv run pytest -q`. ## Commit & Pull Request Guidelines + - Commits: Imperative mood, concise subject, meaningful body when needed. Example: `Fix AgentsDF.sets copy binding and tests`. - PRs: Link issues, summarize changes, note API impacts, add/adjust tests and docs. - CI hygiene: Run `ruff`, `pytest`, and `pre-commit` locally before pushing. ## Security & Configuration Tips + - Never commit secrets; use env vars. Example: `MESA_FRAMES_RUNTIME_TYPECHECKING=1` for stricter dev runs. - Treat underscored attributes as internal. diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index b87ef19a..cb30e8c2 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -35,7 +35,7 @@ def __getitem__( matches = [s for s in sets if isinstance(s, key)] if len(matches) == 0: # No matches - list available agent set types - available_types = list(set(type(s).__name__ for s in sets)) + available_types = list({type(s).__name__ for s in sets}) raise KeyError( f"No agent set of type {getattr(key, '__name__', key)} found. " f"Available agent set types: {available_types}" diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index be6035ee..5ff7902c 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -102,7 +102,6 @@ def sets(self) -> AgentSetsAccessor: self._sets_accessor = acc return acc - @staticmethod def _make_unique_name(base: str, existing: set[str]) -> str: """Generate a unique name by appending numeric suffix if needed.""" @@ -139,6 +138,7 @@ def _canonicalize_names(self, new_agentsets: list[AgentSetDF]) -> None: if unique_name != aset.name: # Directly set the name instead of calling rename import warnings + warnings.warn( f"AgentSet with name '{aset.name}' already exists; renamed to '{unique_name}'.", UserWarning, @@ -147,9 +147,13 @@ def _canonicalize_names(self, new_agentsets: list[AgentSetDF]) -> None: aset._name = unique_name existing_names.add(unique_name) - def _rename_set(self, target: AgentSetDF, new_name: str, - on_conflict: Literal['error', 'skip', 'overwrite'] = 'error', - mode: Literal['atomic'] = 'atomic') -> str: + def _rename_set( + self, + target: AgentSetDF, + new_name: str, + on_conflict: Literal["error", "skip", "overwrite"] = "error", + mode: Literal["atomic"] = "atomic", + ) -> str: """Internal rename method for handling delegations from accessor. Parameters @@ -178,15 +182,17 @@ def _rename_set(self, target: AgentSetDF, new_name: str, # Validate target is in this container if target not in self._agentsets: available_names = [s.name for s in self._agentsets] - raise ValueError(f"AgentSet {target} is not in this container. " - f"Available agent sets: {available_names}") + raise ValueError( + f"AgentSet {target} is not in this container. " + f"Available agent sets: {available_names}" + ) # Check for conflicts with existing names (excluding current target) existing_names = {s.name for s in self._agentsets if s is not target} if new_name in existing_names: - if on_conflict == 'error': + if on_conflict == "error": raise KeyError(f"AgentSet name '{new_name}' already exists") - elif on_conflict == 'skip': + elif on_conflict == "skip": # Return existing name without changes return target._name # on_conflict == 'overwrite' - proceed with rename @@ -370,7 +376,9 @@ def get( elif key_by == "type": return {type(a): v for a, v in result.items()} # type: ignore[return-value] else: - raise ValueError("key_by must be one of 'object', 'name', 'index', or 'type'") + raise ValueError( + "key_by must be one of 'object', 'name', 'index', or 'type'" + ) def remove( self, diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index e0afedca..552d371d 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -131,7 +131,7 @@ def rename(self, new_name: str) -> str: # Check if we have a model and can find the AgentsDF that contains this set if self in self.model.agents.sets: return self.model.agents.sets.rename(self._name, new_name) - + # Set name locally if no container found self._name = new_name return new_name diff --git a/tests/test_sets_accessor.py b/tests/test_sets_accessor.py index 34ab8d96..f0bd12e1 100644 --- a/tests/test_sets_accessor.py +++ b/tests/test_sets_accessor.py @@ -3,7 +3,11 @@ import pytest from mesa_frames import AgentsDF, ModelDF -from tests.test_agentset import ExampleAgentSetPolars, fix1_AgentSetPolars, fix2_AgentSetPolars +from tests.test_agentset import ( + ExampleAgentSetPolars, + fix1_AgentSetPolars, + fix2_AgentSetPolars, +) from tests.test_agents import fix_AgentsDF @@ -42,16 +46,23 @@ class Temp(ExampleAgentSetPolars): def test_first(self, fix_AgentsDF): agents = fix_AgentsDF assert agents.sets.first(ExampleAgentSetPolars) is agents.sets[0] + class Temp(ExampleAgentSetPolars): pass + with pytest.raises(KeyError): agents.sets.first(Temp) def test_all(self, fix_AgentsDF): agents = fix_AgentsDF - assert agents.sets.all(ExampleAgentSetPolars) == [agents.sets[0], agents.sets[1]] + assert agents.sets.all(ExampleAgentSetPolars) == [ + agents.sets[0], + agents.sets[1], + ] + class Temp(ExampleAgentSetPolars): pass + assert agents.sets.all(Temp) == [] def test_at(self, fix_AgentsDF): From 823732b2d344fb198fb96fa3daaca6b89149580f Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 10:55:59 +0200 Subject: [PATCH 021/329] Refactor error handling in __getitem__ to use a set for available agent set types, improving performance and clarity in KeyError messages. --- mesa_frames/concrete/accessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index b87ef19a..cb30e8c2 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -35,7 +35,7 @@ def __getitem__( matches = [s for s in sets if isinstance(s, key)] if len(matches) == 0: # No matches - list available agent set types - available_types = list(set(type(s).__name__ for s in sets)) + available_types = list({type(s).__name__ for s in sets}) raise KeyError( f"No agent set of type {getattr(key, '__name__', key)} found. " f"Available agent set types: {available_types}" From ebbbf6b61e40fc7781acb6eeef809b22d257f7a6 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 10:57:12 +0200 Subject: [PATCH 022/329] Enhance code readability and organization by adding whitespace for clarity in AGENTS.md, agents.py, agentset.py, and test_sets_accessor.py; improve formatting in test cases. --- AGENTS.md | 6 ++++++ mesa_frames/concrete/agents.py | 28 ++++++++++++++++++---------- mesa_frames/concrete/agentset.py | 2 +- tests/test_sets_accessor.py | 15 +++++++++++++-- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 9bc4999c..19b3caa8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,6 +1,7 @@ # Repository Guidelines ## Project Structure & Module Organization + - `mesa_frames/`: Source package. - `abstract/` and `concrete/`: Core APIs and implementations. - Key modules: `agents.py`, `agentset.py`, `space.py`, `datacollector.py`, `types_.py`. @@ -9,6 +10,7 @@ - `examples/`: Reproducible demo models and performance scripts. ## Build, Test, and Development Commands + - Install (dev stack): `uv sync` (always use uv) - Lint & format: `uv run ruff check . --fix && uv run ruff format .` - Tests (quiet + coverage): `export MESA_FRAMES_RUNTIME_TYPECHECKING = 1 && uv run pytest -q --cov=mesa_frames --cov-report=term-missing` @@ -18,23 +20,27 @@ Always run tools via uv: `uv run `. ## Coding Style & Naming Conventions + - Python 3.11+, 4-space indent, type hints required for public APIs. - Docstrings: NumPy style (validated by Ruff/pydoclint). - Formatting/linting: Ruff (formatter + lints). Fix on save if your IDE supports it. - Names: `CamelCase` for classes, `snake_case` for functions/attributes, tests as `test_.py` with `Test` groups. ## Testing Guidelines + - Framework: Pytest; place tests under `tests/` mirroring module paths. - Conventions: One test module per feature; name tests `test_`. - Coverage: Aim to exercise new branches and error paths; keep `--cov=mesa_frames` green. - Run fast locally: `pytest -q` or `uv run pytest -q`. ## Commit & Pull Request Guidelines + - Commits: Imperative mood, concise subject, meaningful body when needed. Example: `Fix AgentsDF.sets copy binding and tests`. - PRs: Link issues, summarize changes, note API impacts, add/adjust tests and docs. - CI hygiene: Run `ruff`, `pytest`, and `pre-commit` locally before pushing. ## Security & Configuration Tips + - Never commit secrets; use env vars. Example: `MESA_FRAMES_RUNTIME_TYPECHECKING=1` for stricter dev runs. - Treat underscored attributes as internal. diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index be6035ee..87a707da 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -102,7 +102,6 @@ def sets(self) -> AgentSetsAccessor: self._sets_accessor = acc return acc - @staticmethod def _make_unique_name(base: str, existing: set[str]) -> str: """Generate a unique name by appending numeric suffix if needed.""" @@ -139,6 +138,7 @@ def _canonicalize_names(self, new_agentsets: list[AgentSetDF]) -> None: if unique_name != aset.name: # Directly set the name instead of calling rename import warnings + warnings.warn( f"AgentSet with name '{aset.name}' already exists; renamed to '{unique_name}'.", UserWarning, @@ -147,9 +147,13 @@ def _canonicalize_names(self, new_agentsets: list[AgentSetDF]) -> None: aset._name = unique_name existing_names.add(unique_name) - def _rename_set(self, target: AgentSetDF, new_name: str, - on_conflict: Literal['error', 'skip', 'overwrite'] = 'error', - mode: Literal['atomic'] = 'atomic') -> str: + def _rename_set( + self, + target: AgentSetDF, + new_name: str, + on_conflict: Literal["error", "skip", "overwrite"] = "error", + mode: Literal["atomic"] = "atomic", + ) -> str: """Internal rename method for handling delegations from accessor. Parameters @@ -178,15 +182,17 @@ def _rename_set(self, target: AgentSetDF, new_name: str, # Validate target is in this container if target not in self._agentsets: available_names = [s.name for s in self._agentsets] - raise ValueError(f"AgentSet {target} is not in this container. " - f"Available agent sets: {available_names}") + raise ValueError( + f"AgentSet {target} is not in this container. " + f"Available agent sets: {available_names}" + ) # Check for conflicts with existing names (excluding current target) existing_names = {s.name for s in self._agentsets if s is not target} if new_name in existing_names: - if on_conflict == 'error': + if on_conflict == "error": raise KeyError(f"AgentSet name '{new_name}' already exists") - elif on_conflict == 'skip': + elif on_conflict == "skip": # Return existing name without changes return target._name # on_conflict == 'overwrite' - proceed with rename @@ -370,7 +376,9 @@ def get( elif key_by == "type": return {type(a): v for a, v in result.items()} # type: ignore[return-value] else: - raise ValueError("key_by must be one of 'object', 'name', 'index', or 'type'") + raise ValueError( + "key_by must be one of 'object', 'name', 'index', or 'type'" + ) def remove( self, @@ -602,7 +610,7 @@ def __add__(self, other: AgentSetDF | Iterable[AgentSetDF]) -> Self: """ return super().__add__(other) - def __getattr__(self, name: str) -> dict[AgentSetDF, Any]: + def __getattr__(self, name: str) -> dict[str, Any]: # Avoids infinite recursion of private attributes if __debug__: # Only execute in non-optimized mode if name.startswith("_"): diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index e0afedca..552d371d 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -131,7 +131,7 @@ def rename(self, new_name: str) -> str: # Check if we have a model and can find the AgentsDF that contains this set if self in self.model.agents.sets: return self.model.agents.sets.rename(self._name, new_name) - + # Set name locally if no container found self._name = new_name return new_name diff --git a/tests/test_sets_accessor.py b/tests/test_sets_accessor.py index 34ab8d96..f0bd12e1 100644 --- a/tests/test_sets_accessor.py +++ b/tests/test_sets_accessor.py @@ -3,7 +3,11 @@ import pytest from mesa_frames import AgentsDF, ModelDF -from tests.test_agentset import ExampleAgentSetPolars, fix1_AgentSetPolars, fix2_AgentSetPolars +from tests.test_agentset import ( + ExampleAgentSetPolars, + fix1_AgentSetPolars, + fix2_AgentSetPolars, +) from tests.test_agents import fix_AgentsDF @@ -42,16 +46,23 @@ class Temp(ExampleAgentSetPolars): def test_first(self, fix_AgentsDF): agents = fix_AgentsDF assert agents.sets.first(ExampleAgentSetPolars) is agents.sets[0] + class Temp(ExampleAgentSetPolars): pass + with pytest.raises(KeyError): agents.sets.first(Temp) def test_all(self, fix_AgentsDF): agents = fix_AgentsDF - assert agents.sets.all(ExampleAgentSetPolars) == [agents.sets[0], agents.sets[1]] + assert agents.sets.all(ExampleAgentSetPolars) == [ + agents.sets[0], + agents.sets[1], + ] + class Temp(ExampleAgentSetPolars): pass + assert agents.sets.all(Temp) == [] def test_at(self, fix_AgentsDF): From 7f5844a8557711692c65f46c54584379addf82e5 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 11:04:19 +0200 Subject: [PATCH 023/329] Enhance docstring clarity and type annotations in AbstractAgentSetsAccessor; update parameter descriptions for improved understanding. --- mesa_frames/abstract/accessors.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index 83c1392e..b5225661 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -1,3 +1,9 @@ +"""Abstract accessors for agent sets collections. + +This module provides abstract base classes for accessors that enable +flexible querying and manipulation of collections of agent sets. +""" + from __future__ import annotations from abc import ABC, abstractmethod @@ -78,7 +84,7 @@ def get(self, key: int | str | type[AgentSetDF], default: Any | None = None) -> ---------- key : int | str | type[AgentSetDF] Lookup key; see :meth:`__getitem__`. - default : Any, optional + default : Any | None, optional Value to return when the lookup fails. If ``key`` is a type and no matches are found, implementers may prefer returning ``[]`` when ``default`` is ``None`` to keep list shape stable. @@ -165,7 +171,7 @@ def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: Parameters ---------- - key_by : {"name", "index", "object", "type"}, default "name" + key_by : KeyBy, default "name" - ``"name"`` → agent set names. - ``"index"`` → positional indices. - ``"object"`` → the :class:`AgentSetDF` objects. From 930cd775aea413aca37462d442e80e2e45bddf95 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 11:05:04 +0200 Subject: [PATCH 024/329] Enhance docstring clarity and type annotations in AgentSetsAccessor; update conflict resolution and mode descriptions for improved understanding. --- mesa_frames/concrete/accessors.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index cb30e8c2..88d3c26b 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -1,3 +1,11 @@ +"""Concrete implementations of agent set accessors. + +This module contains the concrete implementation of the AgentSetsAccessor, +which provides a user-friendly interface for accessing and manipulating +collections of agent sets within the mesa-frames library. +""" + +from __future__ import annotations from __future__ import annotations from collections import defaultdict @@ -140,9 +148,9 @@ def rename( - Batch: {target: new_name} dict or [(target, new_name), ...] list new_name : str | None, optional New name (only used for single renames) - on_conflict : "canonicalize" | "raise", default "canonicalize" + on_conflict : "Literal['canonicalize', 'raise']", default "canonicalize" Conflict resolution: "canonicalize" appends suffixes, "raise" raises ValueError - mode : "atomic" | "best_effort", default "atomic" + mode : "Literal['atomic', 'best_effort']", default "atomic" Rename mode: "atomic" applies all or none, "best_effort" skips failed renames Returns From bf5786a6af0e2fdae3547fe7f0f7f0728a79b8e7 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 11:05:19 +0200 Subject: [PATCH 025/329] Refactor docstring in AgentsDF.rename to clarify purpose and improve type annotations for parameters. --- mesa_frames/concrete/agents.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index 87a707da..9e0a8afb 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -154,7 +154,7 @@ def _rename_set( on_conflict: Literal["error", "skip", "overwrite"] = "error", mode: Literal["atomic"] = "atomic", ) -> str: - """Internal rename method for handling delegations from accessor. + """Handle agent set renaming delegations from accessor. Parameters ---------- @@ -162,9 +162,9 @@ def _rename_set( The agent set to rename new_name : str The new name for the agent set - on_conflict : {'error', 'skip', 'overwrite'}, optional + on_conflict : Literal["error", "skip", "overwrite"], optional How to handle naming conflicts, by default 'error' - mode : {'atomic'}, optional + mode : Literal["atomic"], optional Rename mode, by default 'atomic' Returns From 66b70546fd03482752a632de289e186a44335fd7 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 11:22:07 +0200 Subject: [PATCH 026/329] Enhance docstring clarity and type annotations in AbstractAgentSetsAccessor and AgentSetsAccessor; update default values and descriptions for parameters. --- mesa_frames/abstract/accessors.py | 4 ++-- mesa_frames/concrete/accessors.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index b5225661..c9b028e5 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -171,8 +171,8 @@ def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: Parameters ---------- - key_by : KeyBy, default "name" - - ``"name"`` → agent set names. + key_by : KeyBy + - ``"name"`` → agent set names. (Default) - ``"index"`` → positional indices. - ``"object"`` → the :class:`AgentSetDF` objects. - ``"type"`` → the concrete classes of each set. diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index 88d3c26b..c8b6f0ec 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -148,10 +148,10 @@ def rename( - Batch: {target: new_name} dict or [(target, new_name), ...] list new_name : str | None, optional New name (only used for single renames) - on_conflict : "Literal['canonicalize', 'raise']", default "canonicalize" - Conflict resolution: "canonicalize" appends suffixes, "raise" raises ValueError - mode : "Literal['atomic', 'best_effort']", default "atomic" - Rename mode: "atomic" applies all or none, "best_effort" skips failed renames + on_conflict : "Literal['canonicalize', 'raise']" + Conflict resolution: "canonicalize" (default) appends suffixes, "raise" raises ValueError + mode : "Literal['atomic', 'best_effort']" + Rename mode: "atomic" applies all or none (default), "best_effort" skips failed renames Returns ------- From cf56a7dc1c682a268bb16cbc3dea71ccfbf04063 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 30 Aug 2025 09:22:26 +0000 Subject: [PATCH 027/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa_frames/abstract/accessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index c9b028e5..3d41d451 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -171,7 +171,7 @@ def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: Parameters ---------- - key_by : KeyBy + key_by : KeyBy - ``"name"`` → agent set names. (Default) - ``"index"`` → positional indices. - ``"object"`` → the :class:`AgentSetDF` objects. From 7c2afacbbb312de8bceb24c924495ca74bac0171 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 11:40:02 +0200 Subject: [PATCH 028/329] Enhance type annotations and overloads in AbstractAgentSetsAccessor; improve clarity for __getitem__, get, keys, items, and mapping methods. --- mesa_frames/abstract/accessors.py | 147 ++++++++++++++++++++++++++---- 1 file changed, 127 insertions(+), 20 deletions(-) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index c9b028e5..cd1bb625 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -8,11 +8,13 @@ from abc import ABC, abstractmethod from collections.abc import Iterable, Iterator, Mapping -from typing import Any +from typing import Any, Literal, overload, TypeVar from mesa_frames.abstract.agents import AgentSetDF from mesa_frames.types_ import KeyBy +TSet = TypeVar("TSet", bound=AgentSetDF) + class AbstractAgentSetsAccessor(ABC): """Abstract accessor for collections of agent sets. @@ -47,22 +49,33 @@ class AbstractAgentSetsAccessor(ABC): 1 Wolf """ + # __getitem__ — exact shapes per key kind + @overload + @abstractmethod + def __getitem__(self, key: int) -> AgentSetDF: ... + + @overload @abstractmethod - def __getitem__( - self, key: int | str | type[AgentSetDF] - ) -> AgentSetDF | list[AgentSetDF]: + def __getitem__(self, key: str) -> AgentSetDF: ... + + @overload + @abstractmethod + def __getitem__(self, key: type[TSet]) -> list[TSet]: ... + + @abstractmethod + def __getitem__(self, key: int | str | type[TSet]) -> AgentSetDF | list[TSet]: """Retrieve agent set(s) by index, name, or type. Parameters ---------- - key : int | str | type[AgentSetDF] + key : int | str | type[TSet] - ``int``: positional index (supports negative indices). - ``str``: agent set name. - ``type``: class or subclass of :class:`AgentSetDF`. Returns ------- - AgentSetDF | list[AgentSetDF] + AgentSetDF | list[TSet] A single agent set for ``int``/``str`` keys; a list of matching agent sets for ``type`` keys (possibly empty). @@ -76,23 +89,55 @@ def __getitem__( If the key type is unsupported. """ + # get — mirrors dict.get, but preserves list shape for type keys + @overload + @abstractmethod + def get(self, key: int, default: None = ...) -> AgentSetDF | None: ... + + @overload + @abstractmethod + def get(self, key: str, default: None = ...) -> AgentSetDF | None: ... + + @overload @abstractmethod - def get(self, key: int | str | type[AgentSetDF], default: Any | None = None) -> Any: - """Safe lookup variant that returns a default on miss. + def get(self, key: type[TSet], default: None = ...) -> list[TSet]: ... + + @overload + @abstractmethod + def get(self, key: int, default: AgentSetDF) -> AgentSetDF: ... + + @overload + @abstractmethod + def get(self, key: str, default: AgentSetDF) -> AgentSetDF: ... + + @overload + @abstractmethod + def get(self, key: type[TSet], default: list[TSet]) -> list[TSet]: ... + + @abstractmethod + def get( + self, + key: int | str | type[TSet], + default: AgentSetDF | list[TSet] | None = None, + ) -> AgentSetDF | list[TSet] | None: + """ + Safe lookup variant that returns a default on miss. Parameters ---------- - key : int | str | type[AgentSetDF] + key : int | str | type[TSet] Lookup key; see :meth:`__getitem__`. - default : Any | None, optional - Value to return when the lookup fails. If ``key`` is a type and no - matches are found, implementers may prefer returning ``[]`` when - ``default`` is ``None`` to keep list shape stable. + default : AgentSetDF | list[TSet] | None, optional + Value to return when the lookup fails. For type keys, if no matches + are found and default is None, implementers should return [] to keep + list shape stable. Returns ------- - Any - The resolved value or ``default``. + AgentSetDF | list[TSet] | None + - int/str keys: return the set or default/None if missing + - type keys: return list of matching sets; if none and default is None, + return [] (stable list shape) """ @abstractmethod @@ -165,13 +210,31 @@ def at(self, index: int) -> AgentSetDF: True """ + @overload + @abstractmethod + def keys(self, *, key_by: Literal["name"]) -> Iterable[str]: ... + + @overload @abstractmethod - def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: + def keys(self, *, key_by: Literal["index"]) -> Iterable[int]: ... + + @overload + @abstractmethod + def keys(self, *, key_by: Literal["object"]) -> Iterable[AgentSetDF]: ... + + @overload + @abstractmethod + def keys(self, *, key_by: Literal["type"]) -> Iterable[type[AgentSetDF]]: ... + + @abstractmethod + def keys( + self, *, key_by: KeyBy = "name" + ) -> Iterable[str | int | AgentSetDF | type[AgentSetDF]]: """Iterate keys under a chosen key domain. Parameters ---------- - key_by : KeyBy + key_by : KeyBy - ``"name"`` → agent set names. (Default) - ``"index"`` → positional indices. - ``"object"`` → the :class:`AgentSetDF` objects. @@ -179,12 +242,36 @@ def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: Returns ------- - Iterable[Any] + Iterable[str | int | AgentSetDF | type[AgentSetDF]] An iterable of keys corresponding to the selected domain. """ + @overload + @abstractmethod + def items(self, *, key_by: Literal["name"]) -> Iterable[tuple[str, AgentSetDF]]: ... + + @overload + @abstractmethod + def items( + self, *, key_by: Literal["index"] + ) -> Iterable[tuple[int, AgentSetDF]]: ... + + @overload + @abstractmethod + def items( + self, *, key_by: Literal["object"] + ) -> Iterable[tuple[AgentSetDF, AgentSetDF]]: ... + + @overload + @abstractmethod + def items( + self, *, key_by: Literal["type"] + ) -> Iterable[tuple[type[AgentSetDF], AgentSetDF]]: ... + @abstractmethod - def items(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: + def items( + self, *, key_by: KeyBy = "name" + ) -> Iterable[tuple[str | int | AgentSetDF | type[AgentSetDF], AgentSetDF]]: """Iterate ``(key, AgentSetDF)`` pairs under a chosen key domain. See :meth:`keys` for the meaning of ``key_by``. @@ -198,8 +285,28 @@ def values(self) -> Iterable[AgentSetDF]: def iter(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: """Alias for :meth:`items` for convenience.""" + @overload + @abstractmethod + def mapping(self, *, key_by: Literal["name"]) -> dict[str, AgentSetDF]: ... + + @overload + @abstractmethod + def mapping(self, *, key_by: Literal["index"]) -> dict[int, AgentSetDF]: ... + + @overload + @abstractmethod + def mapping(self, *, key_by: Literal["object"]) -> dict[AgentSetDF, AgentSetDF]: ... + + @overload + @abstractmethod + def mapping( + self, *, key_by: Literal["type"] + ) -> dict[type[AgentSetDF], AgentSetDF]: ... + @abstractmethod - def mapping(self, *, key_by: KeyBy = "name") -> dict[Any, AgentSetDF]: + def mapping( + self, *, key_by: KeyBy = "name" + ) -> dict[str | int | AgentSetDF | type[AgentSetDF], AgentSetDF]: """Return a dictionary view keyed by the chosen domain. Notes From 686dfa520ea90be8f3e570e87cf7b7e83c00f905 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 11:51:50 +0200 Subject: [PATCH 029/329] Refactor type annotations in AbstractAgentSetsAccessor; replace AgentSetDF with generic TSet for improved flexibility in first and all methods, and rename mapping methods to dict for clarity. --- mesa_frames/abstract/accessors.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index cd1bb625..25d8d56a 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -141,17 +141,17 @@ def get( """ @abstractmethod - def first(self, t: type[AgentSetDF]) -> AgentSetDF: + def first(self, t: type[TSet]) -> TSet: """Return the first agent set matching a type. Parameters ---------- - t : type[AgentSetDF] + t : type[TSet] The concrete class (or base class) to match. Returns ------- - AgentSetDF + TSet The first matching agent set in iteration order. Raises @@ -166,17 +166,17 @@ def first(self, t: type[AgentSetDF]) -> AgentSetDF: """ @abstractmethod - def all(self, t: type[AgentSetDF]) -> list[AgentSetDF]: + def all(self, t: type[TSet]) -> list[TSet]: """Return all agent sets matching a type. Parameters ---------- - t : type[AgentSetDF] + t : type[TSet] The concrete class (or base class) to match. Returns ------- - list[AgentSetDF] + list[TSet] A list of all matching agent sets (possibly empty). Examples @@ -287,24 +287,24 @@ def iter(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: @overload @abstractmethod - def mapping(self, *, key_by: Literal["name"]) -> dict[str, AgentSetDF]: ... + def dict(self, *, key_by: Literal["name"]) -> dict[str, AgentSetDF]: ... @overload @abstractmethod - def mapping(self, *, key_by: Literal["index"]) -> dict[int, AgentSetDF]: ... + def dict(self, *, key_by: Literal["index"]) -> dict[int, AgentSetDF]: ... @overload @abstractmethod - def mapping(self, *, key_by: Literal["object"]) -> dict[AgentSetDF, AgentSetDF]: ... + def dict(self, *, key_by: Literal["object"]) -> dict[AgentSetDF, AgentSetDF]: ... @overload @abstractmethod - def mapping( + def dict( self, *, key_by: Literal["type"] ) -> dict[type[AgentSetDF], AgentSetDF]: ... @abstractmethod - def mapping( + def dict( self, *, key_by: KeyBy = "name" ) -> dict[str | int | AgentSetDF | type[AgentSetDF], AgentSetDF]: """Return a dictionary view keyed by the chosen domain. From 92ff76e77203eef39bfb3bb17dda662f707a4193 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:02:42 +0200 Subject: [PATCH 030/329] Refactor AgentSetsAccessor methods; replace mapping method with dict for consistency and update test cases accordingly. --- mesa_frames/concrete/accessors.py | 57 ++++++++++++++----------------- tests/test_sets_accessor.py | 4 +-- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index c8b6f0ec..8e9b60ff 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -5,18 +5,19 @@ collections of agent sets within the mesa-frames library. """ -from __future__ import annotations from __future__ import annotations from collections import defaultdict from collections.abc import Iterable, Iterator, Mapping from types import MappingProxyType -from typing import Any, Literal, cast +from typing import Any, Literal, TypeVar, cast from mesa_frames.abstract.accessors import AbstractAgentSetsAccessor from mesa_frames.abstract.agents import AgentSetDF from mesa_frames.types_ import KeyBy +TSet = TypeVar("TSet", bound=AgentSetDF) + class AgentSetsAccessor(AbstractAgentSetsAccessor): def __init__(self, parent: mesa_frames.concrete.agents.AgentsDF) -> None: @@ -41,45 +42,37 @@ def __getitem__( raise KeyError(f"No agent set named '{key}'. Available: {available}") if isinstance(key, type): matches = [s for s in sets if isinstance(s, key)] - if len(matches) == 0: - # No matches - list available agent set types - available_types = list({type(s).__name__ for s in sets}) - raise KeyError( - f"No agent set of type {getattr(key, '__name__', key)} found. " - f"Available agent set types: {available_types}" - ) - elif len(matches) == 1: - # Single match - return it directly - return matches[0] - else: - # Multiple matches - return all matching agent sets as list - return matches + # Always return list for type keys to maintain consistent shape + return matches # type: ignore[return-value] raise TypeError("Key must be int | str | type[AgentSetDF]") def get( - self, key: int | str | type[AgentSetDF], default: Any | None = None - ) -> AgentSetDF | list[AgentSetDF] | Any | None: + self, + key: int | str | type[TSet], + default: AgentSetDF | list[TSet] | None = None, + ) -> AgentSetDF | list[TSet] | None: try: - val = self[key] - # For type keys: if no matches and a default was provided, return the default; - # if no default, preserve list shape and return []. - if isinstance(key, type) and isinstance(val, list) and len(val) == 0: - return [] if default is None else default + val = self[key] # type: ignore[return-value] + # For type keys, if no matches and a default was provided, return default + if ( + isinstance(key, type) + and isinstance(val, list) + and len(val) == 0 + and default is not None + ): + return default return val except (KeyError, IndexError, TypeError): - # For type keys, preserve list shape by default when default is None - if isinstance(key, type) and default is None: - return [] return default - def first(self, t: type[AgentSetDF]) -> AgentSetDF: - matches = [s for s in self._parent._agentsets if isinstance(s, t)] - if not matches: + def first(self, t: type[TSet]) -> TSet: + match = next((s for s in self._parent._agentsets if isinstance(s, t)), None) + if not match: raise KeyError(f"No agent set of type {getattr(t, '__name__', t)} found.") - return matches[0] + return match - def all(self, t: type[AgentSetDF]) -> list[AgentSetDF]: - return [s for s in self._parent._agentsets if isinstance(s, t)] + def all(self, t: type[TSet]) -> list[TSet]: + return [s for s in self._parent._agentsets if isinstance(s, t)] # type: ignore[return-value] def at(self, index: int) -> AgentSetDF: return self[index] # type: ignore[return-value] @@ -110,7 +103,7 @@ def values(self) -> Iterable[AgentSetDF]: def iter(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: return self.items(key_by=key_by) - def mapping(self, *, key_by: KeyBy = "name") -> dict[Any, AgentSetDF]: + def dict(self, *, key_by: KeyBy = "name") -> dict[Any, AgentSetDF]: return {k: v for k, v in self.items(key_by=key_by)} # ---------- read-only snapshots ---------- diff --git a/tests/test_sets_accessor.py b/tests/test_sets_accessor.py index f0bd12e1..c350963c 100644 --- a/tests/test_sets_accessor.py +++ b/tests/test_sets_accessor.py @@ -98,11 +98,11 @@ def test_iter(self, fix_AgentsDF): s2 = agents.sets[1] assert list(agents.sets.iter(key_by="name")) == [(s1.name, s1), (s2.name, s2)] - def test_mapping(self, fix_AgentsDF): + def test_dict(self, fix_AgentsDF): agents = fix_AgentsDF s1 = agents.sets[0] s2 = agents.sets[1] - by_type_map = agents.sets.mapping(key_by="type") + by_type_map = agents.sets.dict(key_by="type") assert list(by_type_map.keys()) == [type(s1)] assert by_type_map[type(s1)] is s2 From bba59cc2e17cc0211c4fe5d5a964e26f4e0bcc91 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:26:41 +0200 Subject: [PATCH 031/329] Add rename method to AbstractAgentSetsAccessor for agent set renaming with conflict handling --- mesa_frames/abstract/accessors.py | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index 25d8d56a..4cfe337d 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -355,6 +355,49 @@ def by_type(self) -> Mapping[type, list[AgentSetDF]]: grouping instead of last-write-wins semantics. """ + @abstractmethod + def rename( + self, + target: AgentSetDF | str | dict[AgentSetDF | str, str] | list[tuple[AgentSetDF | str, str]], + new_name: str | None = None, + *, + on_conflict: Literal["canonicalize", "raise"] = "canonicalize", + mode: Literal["atomic", "best_effort"] = "atomic", + ) -> str | dict[AgentSetDF, str]: + """ + Rename agent sets. Supports single and batch renaming with deterministic conflict handling. + + Parameters + ---------- + target : AgentSetDF | str | dict[AgentSetDF | str, str] | list[tuple[AgentSetDF | str, str]] + Either: + - Single: AgentSet or name string (must provide new_name) + - Batch: {target: new_name} dict or [(target, new_name), ...] list + new_name : str | None, optional + New name (only used for single renames) + on_conflict : "Literal['canonicalize', 'raise']" + Conflict resolution: "canonicalize" (default) appends suffixes, "raise" raises ValueError + mode : "Literal['atomic', 'best_effort']" + Rename mode: "atomic" applies all or none (default), "best_effort" skips failed renames + + Returns + ------- + str | dict[AgentSetDF, str] + Single rename: final name string + Batch: {agentset: final_name} mapping + + Examples + -------- + Single rename: + >>> agents.sets.rename("old_name", "new_name") + + Batch rename (dict): + >>> agents.sets.rename({"set1": "new_name", "set2": "another_name"}) + + Batch rename (list): + >>> agents.sets.rename([("set1", "new_name"), ("set2", "another_name")]) + """ + @abstractmethod def __contains__(self, x: str | AgentSetDF) -> bool: """Return ``True`` if a name or object is present. From c54f9d92dc498e0b87fef7363ccc117f21bd4274 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:27:09 +0200 Subject: [PATCH 032/329] Refactor rename method in AgentSetsAccessor; streamline docstring and update call to _rename_sets for batch renaming support. --- mesa_frames/concrete/accessors.py | 35 +------------------------------ 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index 8e9b60ff..184e281f 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -130,40 +130,7 @@ def rename( on_conflict: Literal["canonicalize", "raise"] = "canonicalize", mode: Literal["atomic", "best_effort"] = "atomic", ) -> str | dict[AgentSetDF, str]: - """ - Rename agent sets. Supports single and batch renaming with deterministic conflict handling. - - Parameters - ---------- - target : AgentSetDF | str | dict[AgentSetDF | str, str] | list[tuple[AgentSetDF | str, str]] - Either: - - Single: AgentSet or name string (must provide new_name) - - Batch: {target: new_name} dict or [(target, new_name), ...] list - new_name : str | None, optional - New name (only used for single renames) - on_conflict : "Literal['canonicalize', 'raise']" - Conflict resolution: "canonicalize" (default) appends suffixes, "raise" raises ValueError - mode : "Literal['atomic', 'best_effort']" - Rename mode: "atomic" applies all or none (default), "best_effort" skips failed renames - - Returns - ------- - str | dict[AgentSetDF, str] - Single rename: final name string - Batch: {agentset: final_name} mapping - - Examples - -------- - Single rename: - >>> agents.sets.rename("old_name", "new_name") - - Batch rename (dict): - >>> agents.sets.rename({"set1": "new_name", "set2": "another_name"}) - - Batch rename (list): - >>> agents.sets.rename([("set1", "new_name"), ("set2", "another_name")]) - """ - return self._parent._rename_set( + return self._parent._rename_sets( target, new_name, on_conflict=on_conflict, mode=mode ) From c83e9e513547958e9aa72073317610925e83b7fd Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:33:47 +0200 Subject: [PATCH 033/329] Refactor rename method in AbstractAgentSetsAccessor; improve type annotations for target parameter to enhance clarity and flexibility. --- mesa_frames/abstract/accessors.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index 4cfe337d..3599ce1e 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -358,7 +358,10 @@ def by_type(self) -> Mapping[type, list[AgentSetDF]]: @abstractmethod def rename( self, - target: AgentSetDF | str | dict[AgentSetDF | str, str] | list[tuple[AgentSetDF | str, str]], + target: AgentSetDF + | str + | dict[AgentSetDF | str, str] + | list[tuple[AgentSetDF | str, str]], new_name: str | None = None, *, on_conflict: Literal["canonicalize", "raise"] = "canonicalize", From 0f640fbd7c442b35754b48e6d23324453ac0b874 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:34:24 +0200 Subject: [PATCH 034/329] Refactor _rename_set method in AgentsDF; enhance functionality for single and batch renaming with improved conflict handling and parsing logic. --- mesa_frames/concrete/agents.py | 137 ++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 3 deletions(-) diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index 9e0a8afb..a253b7f6 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -147,14 +147,144 @@ def _canonicalize_names(self, new_agentsets: list[AgentSetDF]) -> None: aset._name = unique_name existing_names.add(unique_name) - def _rename_set( + def _rename_sets( + self, + target: AgentSetDF + | str + | dict[AgentSetDF | str, str] + | list[tuple[AgentSetDF | str, str]], + new_name: str | None = None, + *, + on_conflict: Literal["canonicalize", "raise"] = "canonicalize", + mode: Literal["atomic", "best_effort"] = "atomic", + ) -> str | dict[AgentSetDF, str]: + """Handle agent set renaming delegations from accessor. + + Parameters + ---------- + target : AgentSetDF | str | dict[AgentSetDF | str, str] | list[tuple[AgentSetDF | str, str]] + Either: + - Single: AgentSet or name string (must provide new_name) + - Batch: {target: new_name} dict or [(target, new_name), ...] list + new_name : str | None, optional + New name (only used for single renames) + on_conflict : Literal["canonicalize", "raise"] + Conflict resolution: "canonicalize" (default) appends suffixes, "raise" raises ValueError + mode : Literal["atomic", "best_effort"] + Rename mode: "atomic" applies all or none (default), "best_effort" skips failed renames + + Returns + ------- + str | dict[AgentSetDF, str] + Single rename: final name string + Batch: {agentset: final_name} mapping + + Raises + ------ + ValueError + If target format is invalid or single rename missing new_name + KeyError + If agent set name not found or naming conflicts with raise mode + """ + # Parse different target formats and build rename operations + rename_ops = self._parse_rename_target(target, new_name) + + # Map on_conflict values to _rename_single_set expected values + mapped_on_conflict = "error" if on_conflict == "raise" else "overwrite" + + # Determine if this is single or batch based on the input format + if isinstance(target, (str, AgentSetDF)): + # Single rename - return the final name + target_set, new_name = rename_ops[0] + return self._rename_single_set( + target_set, new_name, on_conflict=mapped_on_conflict, mode="atomic" + ) + else: + # Batch rename (dict or list) - return mapping of original sets to final names + result = {} + for target_set, new_name in rename_ops: + final_name = self._rename_single_set( + target_set, new_name, on_conflict=mapped_on_conflict, mode="atomic" + ) + result[target_set] = final_name + return result + + def _parse_rename_target( + self, + target: AgentSetDF + | str + | dict[AgentSetDF | str, str] + | list[tuple[AgentSetDF | str, str]], + new_name: str | None = None, + ) -> list[tuple[AgentSetDF, str]]: + """Parse the target parameter into a list of (agentset, new_name) pairs.""" + rename_ops = [] + # Get available names for error messages + available_names = [getattr(s, "name", None) for s in self._agentsets] + + if isinstance(target, dict): + # target is a dict mapping agent sets/names to new names + for k, v in target.items(): + if isinstance(k, str): + # k is a name, find the agent set + target_set = None + for aset in self._agentsets: + if aset.name == k: + target_set = aset + break + if target_set is None: + raise KeyError(f"No agent set named '{k}'. Available: {available_names}") + else: + # k is an AgentSetDF + target_set = k + rename_ops.append((target_set, v)) + + elif isinstance(target, list): + # target is a list of (agent_set/name, new_name) tuples + for k, v in target: + if isinstance(k, str): + # k is a name, find the agent set + target_set = None + for aset in self._agentsets: + if aset.name == k: + target_set = aset + break + if target_set is None: + raise KeyError(f"No agent set named '{k}'. Available: {available_names}") + else: + # k is an AgentSetDF + target_set = k + rename_ops.append((target_set, v)) + + else: + # target is single AgentSetDF or name, new_name must be provided + if isinstance(target, str): + # target is a name, find the agent set + target_set = None + for aset in self._agentsets: + if aset.name == target: + target_set = aset + break + if target_set is None: + raise KeyError(f"No agent set named '{target}'. Available: {available_names}") + else: + # target is an AgentSetDF + target_set = target + + if new_name is None: + raise ValueError("new_name must be provided for single rename") + rename_ops.append((target_set, new_name)) + + return rename_ops + + def _rename_single_set( self, target: AgentSetDF, new_name: str, on_conflict: Literal["error", "skip", "overwrite"] = "error", mode: Literal["atomic"] = "atomic", ) -> str: - """Handle agent set renaming delegations from accessor. + """Handle single agent set renaming. Parameters ---------- @@ -191,7 +321,8 @@ def _rename_set( existing_names = {s.name for s in self._agentsets if s is not target} if new_name in existing_names: if on_conflict == "error": - raise KeyError(f"AgentSet name '{new_name}' already exists") + available_names = [s.name for s in self._agentsets if s.name != target.name] + raise KeyError(f"AgentSet name '{new_name}' already exists. Available names: {available_names}") elif on_conflict == "skip": # Return existing name without changes return target._name From cab89a2d6feace26bfee8034f07de4b53b209563 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:41:07 +0200 Subject: [PATCH 035/329] Refactor AbstractAgentSetsAccessor and AgentSetsAccessor; remove 'object' key option from keys and items methods, and update related logic for consistency. Update KeyBy type alias to reflect changes. --- mesa_frames/abstract/accessors.py | 21 +++------------------ mesa_frames/concrete/accessors.py | 4 +--- mesa_frames/concrete/agents.py | 8 +++----- mesa_frames/types_.py | 2 +- 4 files changed, 8 insertions(+), 27 deletions(-) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index 3599ce1e..a9d6efd0 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -218,10 +218,6 @@ def keys(self, *, key_by: Literal["name"]) -> Iterable[str]: ... @abstractmethod def keys(self, *, key_by: Literal["index"]) -> Iterable[int]: ... - @overload - @abstractmethod - def keys(self, *, key_by: Literal["object"]) -> Iterable[AgentSetDF]: ... - @overload @abstractmethod def keys(self, *, key_by: Literal["type"]) -> Iterable[type[AgentSetDF]]: ... @@ -229,7 +225,7 @@ def keys(self, *, key_by: Literal["type"]) -> Iterable[type[AgentSetDF]]: ... @abstractmethod def keys( self, *, key_by: KeyBy = "name" - ) -> Iterable[str | int | AgentSetDF | type[AgentSetDF]]: + ) -> Iterable[str | int | type[AgentSetDF]]: """Iterate keys under a chosen key domain. Parameters @@ -237,7 +233,6 @@ def keys( key_by : KeyBy - ``"name"`` → agent set names. (Default) - ``"index"`` → positional indices. - - ``"object"`` → the :class:`AgentSetDF` objects. - ``"type"`` → the concrete classes of each set. Returns @@ -256,12 +251,6 @@ def items( self, *, key_by: Literal["index"] ) -> Iterable[tuple[int, AgentSetDF]]: ... - @overload - @abstractmethod - def items( - self, *, key_by: Literal["object"] - ) -> Iterable[tuple[AgentSetDF, AgentSetDF]]: ... - @overload @abstractmethod def items( @@ -271,7 +260,7 @@ def items( @abstractmethod def items( self, *, key_by: KeyBy = "name" - ) -> Iterable[tuple[str | int | AgentSetDF | type[AgentSetDF], AgentSetDF]]: + ) -> Iterable[tuple[str | int | type[AgentSetDF], AgentSetDF]]: """Iterate ``(key, AgentSetDF)`` pairs under a chosen key domain. See :meth:`keys` for the meaning of ``key_by``. @@ -293,10 +282,6 @@ def dict(self, *, key_by: Literal["name"]) -> dict[str, AgentSetDF]: ... @abstractmethod def dict(self, *, key_by: Literal["index"]) -> dict[int, AgentSetDF]: ... - @overload - @abstractmethod - def dict(self, *, key_by: Literal["object"]) -> dict[AgentSetDF, AgentSetDF]: ... - @overload @abstractmethod def dict( @@ -306,7 +291,7 @@ def dict( @abstractmethod def dict( self, *, key_by: KeyBy = "name" - ) -> dict[str | int | AgentSetDF | type[AgentSetDF], AgentSetDF]: + ) -> dict[str | int | type[AgentSetDF], AgentSetDF]: """Return a dictionary view keyed by the chosen domain. Notes diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py index 184e281f..71c2097d 100644 --- a/mesa_frames/concrete/accessors.py +++ b/mesa_frames/concrete/accessors.py @@ -83,11 +83,9 @@ def _gen_key(self, aset: AgentSetDF, idx: int, mode: str) -> Any: return aset.name if mode == "index": return idx - if mode == "object": - return aset if mode == "type": return type(aset) - raise ValueError("key_by must be 'name'|'index'|'object'|'type'") + raise ValueError("key_by must be 'name'|'index'|'type'") def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: for i, s in enumerate(self._parent._agentsets): diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index a253b7f6..681140d0 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -467,7 +467,7 @@ def get( self, attr_names: str | Collection[str] | None = None, mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, - key_by: KeyBy = "object", + key_by: KeyBy = "name", ) -> ( dict[AgentSetDF, Series] | dict[AgentSetDF, DataFrame] @@ -497,9 +497,7 @@ def get( ): result[agentset] = agentset.get(attr_names, mask) - if key_by == "object": - return result - elif key_by == "name": + if key_by == "name": return {cast(AgentSetDF, a).name: v for a, v in result.items()} # type: ignore[return-value] elif key_by == "index": index_map = {agentset: i for i, agentset in enumerate(self._agentsets)} @@ -508,7 +506,7 @@ def get( return {type(a): v for a, v in result.items()} # type: ignore[return-value] else: raise ValueError( - "key_by must be one of 'object', 'name', 'index', or 'type'" + "key_by must be one of 'name', 'index', or 'type'" ) def remove( diff --git a/mesa_frames/types_.py b/mesa_frames/types_.py index f0c515ca..34d5996e 100644 --- a/mesa_frames/types_.py +++ b/mesa_frames/types_.py @@ -84,7 +84,7 @@ Infinity = Annotated[float, IsEqual[math.inf]] # Only accepts math.inf # Common option types -KeyBy = Literal["name", "index", "object", "type"] +KeyBy = Literal["name", "index", "type"] ###----- Time ------### TimeT = float | int From a652892f1043cc75f8642951db91fb7b71485fc9 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:41:16 +0200 Subject: [PATCH 036/329] Add tests for AgentsDF's contains and remove methods; handle empty iterable and None cases --- tests/test_agents.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_agents.py b/tests/test_agents.py index 8151fe8e..f3e4fd11 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -91,6 +91,9 @@ def test_contains( False, ] + # Test with empty iterable - returns True + assert agents.contains([]) + # Test with single id assert agents.contains(agentset_polars1["unique_id"][0]) @@ -390,6 +393,16 @@ def test_remove( with pytest.raises(KeyError): result = agents.remove(0, inplace=False) + # Test with None (should return same agents) + result = agents.remove(None, inplace=False) + assert result is not agents # new object + assert len(result._agentsets) == len(agents._agentsets) + + # Test with empty list + result = agents.remove([], inplace=False) + assert result is not agents + assert len(result._agentsets) == len(agents._agentsets) + def test_select(self, fix_AgentsDF: AgentsDF): agents = fix_AgentsDF From 6ed2419434b2f7f0f95187dcca6c5ccd2037d2b1 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:41:47 +0200 Subject: [PATCH 037/329] Enhance tests for AgentSetsAccessor; add validation for key retrieval, improve rename functionality with single and batch rename tests, and handle invalid key scenarios. --- tests/test_sets_accessor.py | 53 ++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/tests/test_sets_accessor.py b/tests/test_sets_accessor.py index c350963c..c68af1b5 100644 --- a/tests/test_sets_accessor.py +++ b/tests/test_sets_accessor.py @@ -30,11 +30,20 @@ def test___getitem__(self, fix_AgentsDF): lst = agents.sets[ExampleAgentSetPolars] assert isinstance(lst, list) assert s1 in lst and s2 in lst and len(lst) == 2 + # invalid key type → TypeError + # with pytest.raises(TypeError, match="Key must be int \\| str \\| type\\[AgentSetDF\\]"): + # _ = agents.sets[int] # int type not supported as key + # Temporary skip due to beartype issues def test_get(self, fix_AgentsDF): agents = fix_AgentsDF assert agents.sets.get("__missing__") is None - assert agents.sets.get(999, default="x") == "x" + # Test get with int key and invalid index should return default + assert agents.sets.get(999) is None + # + # %# Fix the default type mismatch - for int key, default should be AgentSetDF or None + s1 = agents.sets[0] + assert agents.sets.get(999, default=s1) == s1 class Temp(ExampleAgentSetPolars): pass @@ -75,16 +84,20 @@ def test_keys(self, fix_AgentsDF): s1 = agents.sets[0] s2 = agents.sets[1] assert list(agents.sets.keys(key_by="index")) == [0, 1] - assert list(agents.sets.keys(key_by="object")) == [s1, s2] assert list(agents.sets.keys(key_by="name")) == [s1.name, s2.name] assert list(agents.sets.keys(key_by="type")) == [type(s1), type(s2)] + # Invalid key_by + with pytest.raises( + ValueError, match="key_by must be 'name'\\|'index'\\|'type'" + ): + list(agents.sets.keys(key_by="invalid")) def test_items(self, fix_AgentsDF): agents = fix_AgentsDF s1 = agents.sets[0] s2 = agents.sets[1] assert list(agents.sets.items(key_by="index")) == [(0, s1), (1, s2)] - assert list(agents.sets.items(key_by="object")) == [(s1, s1), (s2, s2)] + def test_values(self, fix_AgentsDF): agents = fix_AgentsDF @@ -131,6 +144,7 @@ def test___contains__(self, fix_AgentsDF): assert s1.name in agents.sets assert s2.name in agents.sets assert s1 in agents.sets and s2 in agents.sets + # Invalid type returns False (simulate by testing the code path manually if needed) def test___len__(self, fix_AgentsDF): agents = fix_AgentsDF @@ -142,6 +156,39 @@ def test___iter__(self, fix_AgentsDF): s2 = agents.sets[1] assert list(iter(agents.sets)) == [s1, s2] + def test_rename(self, fix_AgentsDF): + agents = fix_AgentsDF + s1 = agents.sets[0] + s2 = agents.sets[1] + original_name_1 = s1.name + original_name_2 = s2.name + + # Test single rename by name + new_name_1 = original_name_1 + "_renamed" + result = agents.sets.rename(original_name_1, new_name_1) + assert result == new_name_1 + assert s1.name == new_name_1 + + # Test single rename by object + new_name_2 = original_name_2 + "_modified" + result = agents.sets.rename(s2, new_name_2) + assert result == new_name_2 + assert s2.name == new_name_2 + + # Test batch rename (dict) + s3 = agents.sets[0] # Should be s1 after rename above + new_name_3 = "batch_test" + batch_result = agents.sets.rename({s2: new_name_3}) + assert batch_result[s2] == new_name_3 + assert s2.name == new_name_3 + + # Test batch rename (list) + s4 = agents.sets[0] + new_name_4 = "list_test" + list_result = agents.sets.rename([(s4, new_name_4)]) + assert list_result[s4] == new_name_4 + assert s4.name == new_name_4 + def test_copy_and_deepcopy_rebinds_accessor(self, fix_AgentsDF): agents = fix_AgentsDF s1 = agents.sets[0] From f53b464749ab87bf6b34ceb189f930960125f56e Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:42:28 +0200 Subject: [PATCH 038/329] Refactor keys method in AbstractAgentSetsAccessor for consistency; improve KeyError messages in AgentsDF for better clarity; remove unnecessary blank line in test_sets_accessor. --- mesa_frames/abstract/accessors.py | 4 +--- mesa_frames/concrete/agents.py | 24 ++++++++++++++++-------- tests/test_sets_accessor.py | 1 - 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index a9d6efd0..ae844141 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -223,9 +223,7 @@ def keys(self, *, key_by: Literal["index"]) -> Iterable[int]: ... def keys(self, *, key_by: Literal["type"]) -> Iterable[type[AgentSetDF]]: ... @abstractmethod - def keys( - self, *, key_by: KeyBy = "name" - ) -> Iterable[str | int | type[AgentSetDF]]: + def keys(self, *, key_by: KeyBy = "name") -> Iterable[str | int | type[AgentSetDF]]: """Iterate keys under a chosen key domain. Parameters diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index 681140d0..ea662736 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -233,7 +233,9 @@ def _parse_rename_target( target_set = aset break if target_set is None: - raise KeyError(f"No agent set named '{k}'. Available: {available_names}") + raise KeyError( + f"No agent set named '{k}'. Available: {available_names}" + ) else: # k is an AgentSetDF target_set = k @@ -250,7 +252,9 @@ def _parse_rename_target( target_set = aset break if target_set is None: - raise KeyError(f"No agent set named '{k}'. Available: {available_names}") + raise KeyError( + f"No agent set named '{k}'. Available: {available_names}" + ) else: # k is an AgentSetDF target_set = k @@ -266,7 +270,9 @@ def _parse_rename_target( target_set = aset break if target_set is None: - raise KeyError(f"No agent set named '{target}'. Available: {available_names}") + raise KeyError( + f"No agent set named '{target}'. Available: {available_names}" + ) else: # target is an AgentSetDF target_set = target @@ -321,8 +327,12 @@ def _rename_single_set( existing_names = {s.name for s in self._agentsets if s is not target} if new_name in existing_names: if on_conflict == "error": - available_names = [s.name for s in self._agentsets if s.name != target.name] - raise KeyError(f"AgentSet name '{new_name}' already exists. Available names: {available_names}") + available_names = [ + s.name for s in self._agentsets if s.name != target.name + ] + raise KeyError( + f"AgentSet name '{new_name}' already exists. Available names: {available_names}" + ) elif on_conflict == "skip": # Return existing name without changes return target._name @@ -505,9 +515,7 @@ def get( elif key_by == "type": return {type(a): v for a, v in result.items()} # type: ignore[return-value] else: - raise ValueError( - "key_by must be one of 'name', 'index', or 'type'" - ) + raise ValueError("key_by must be one of 'name', 'index', or 'type'") def remove( self, diff --git a/tests/test_sets_accessor.py b/tests/test_sets_accessor.py index c68af1b5..70ab0f64 100644 --- a/tests/test_sets_accessor.py +++ b/tests/test_sets_accessor.py @@ -98,7 +98,6 @@ def test_items(self, fix_AgentsDF): s2 = agents.sets[1] assert list(agents.sets.items(key_by="index")) == [(0, s1), (1, s2)] - def test_values(self, fix_AgentsDF): agents = fix_AgentsDF s1 = agents.sets[0] From d8661710bcdde3eb0766951f50cf61324c5fc084 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:43:20 +0200 Subject: [PATCH 039/329] Update return type of keys method in AbstractAgentSetsAccessor to exclude AgentSetDF for clarity --- mesa_frames/abstract/accessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py index ae844141..a33ddcab 100644 --- a/mesa_frames/abstract/accessors.py +++ b/mesa_frames/abstract/accessors.py @@ -235,7 +235,7 @@ def keys(self, *, key_by: KeyBy = "name") -> Iterable[str | int | type[AgentSetD Returns ------- - Iterable[str | int | AgentSetDF | type[AgentSetDF]] + Iterable[str | int | type[AgentSetDF]] An iterable of keys corresponding to the selected domain. """ From 006c1abddf960f7abd4a1133e32cbca9ce98adbb Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 16:29:53 +0200 Subject: [PATCH 040/329] Enhance _make_unique_name method in AgentsDF with detailed docstring; ensure name conversion to snake_case and improve uniqueness handling in _canonicalize_names method. --- mesa_frames/concrete/agents.py | 37 +++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index ea662736..a8bd9b7c 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -104,7 +104,32 @@ def sets(self) -> AgentSetsAccessor: @staticmethod def _make_unique_name(base: str, existing: set[str]) -> str: - """Generate a unique name by appending numeric suffix if needed.""" + """Generate a unique name by appending numeric suffix if needed. + + AgentSetPolars constructor ensures names are never None: + `self._name = name if name is not None else self.__class__.__name__` + + Parameters + ---------- + base : str + The base name to make unique. Always a valid string. + existing : set[str] + Set of existing names to avoid conflicts. All items are strings. + + Returns + ------- + str + A unique name in snake_case format. + """ + + # Convert CamelCase to snake_case + def _camel_to_snake(name: str) -> str: + import re + s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) + return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() + + base = _camel_to_snake(base) + if base not in existing: return base # If ends with _, increment; else append _1 @@ -129,18 +154,20 @@ def _make_unique_name(base: str, existing: set[str]) -> str: def _canonicalize_names(self, new_agentsets: list[AgentSetDF]) -> None: """Canonicalize names across existing + new agent sets, ensuring uniqueness.""" - existing_names = {s.name for s in self._agentsets} + existing_names = {str(s.name) for s in self._agentsets} # Process each new agent set in batch to handle potential conflicts for aset in new_agentsets: + # AgentSetPolars guarantees name is always a string + name_str = str(aset.name) # Use the static method to generate unique name - unique_name = self._make_unique_name(aset.name, existing_names) - if unique_name != aset.name: + unique_name = self._make_unique_name(name_str, existing_names) + if unique_name != name_str: # Directly set the name instead of calling rename import warnings warnings.warn( - f"AgentSet with name '{aset.name}' already exists; renamed to '{unique_name}'.", + f"AgentSet with name '{name_str}' already exists; renamed to '{unique_name}'.", UserWarning, stacklevel=2, ) From f7ef41206916affd04fe4a8bf78570cca01ac088 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 18:04:04 +0200 Subject: [PATCH 041/329] Implement camel_case_to_snake_case function for converting camelCase strings to snake_case; include detailed docstring with parameters, return values, and examples. --- mesa_frames/utils.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/mesa_frames/utils.py b/mesa_frames/utils.py index 58b0c85b..8f853bb1 100644 --- a/mesa_frames/utils.py +++ b/mesa_frames/utils.py @@ -16,3 +16,28 @@ def _decorator(func): return func return _decorator + + +def camel_case_to_snake_case(name: str) -> str: + """Convert camelCase to snake_case. + + Parameters + ---------- + name : str + The camelCase string to convert. + + Returns + ------- + str + The converted snake_case string. + + Examples + -------- + >>> camel_case_to_snake_case("ExampleAgentSetPolars") + 'example_agent_set_polars' + >>> camel_case_to_snake_case("getAgentData") + 'get_agent_data' + """ + import re + s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) + return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() From c209c1baafe60aa8b5093652f7aff5a8a5e9a42c Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 18:05:09 +0200 Subject: [PATCH 042/329] Refactor AgentSetPolars to convert proposed name to snake_case if in camelCase; update docstring for clarity on name handling. --- mesa_frames/concrete/agentset.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 552d371d..a2caee9f 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -69,7 +69,7 @@ def step(self): from mesa_frames.concrete.mixin import PolarsMixin from mesa_frames.concrete.model import ModelDF from mesa_frames.types_ import AgentPolarsMask, IntoExpr, PolarsIdsLike -from mesa_frames.utils import copydoc +from mesa_frames.utils import camel_case_to_snake_case, copydoc @copydoc(AgentSetDF) @@ -93,14 +93,13 @@ def __init__( model : "mesa_frames.concrete.model.ModelDF" The model that the agent set belongs to. name : str | None, optional - Proposed name for this agent set. Uniqueness is not guaranteed here - and will be validated only when added to AgentsDF. + Name for this agent set. If None, class name is used. + Will be converted to snake_case if in camelCase. """ # Model reference self._model = model # Set proposed name (no uniqueness guarantees here) - self._name = name if name is not None else self.__class__.__name__ - + self._name = name if name is not None else camel_case_to_snake_case(self.__class__.__name__) # No definition of schema with unique_id, as it becomes hard to add new agents self._df = pl.DataFrame() self._mask = pl.repeat(True, len(self._df), dtype=pl.Boolean, eager=True) From e603996f0c9f7e86a784fe933abb3f796eb1a48d Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 20:26:21 +0200 Subject: [PATCH 043/329] Refactor camel_case_to_snake_case function for consistency in regex string delimiters; improve readability. --- mesa_frames/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mesa_frames/utils.py b/mesa_frames/utils.py index 8f853bb1..fb3e65ff 100644 --- a/mesa_frames/utils.py +++ b/mesa_frames/utils.py @@ -39,5 +39,6 @@ def camel_case_to_snake_case(name: str) -> str: 'get_agent_data' """ import re - s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) - return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() + + s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name) + return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() From d38351e0bec02148199b4f18134106358a08c644 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 20:39:50 +0200 Subject: [PATCH 044/329] Refactor _camel_to_snake function for consistent regex string delimiters; update return types in __getitem__ methods for clarity. --- mesa_frames/concrete/agents.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index a8bd9b7c..3ccbd710 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -125,8 +125,9 @@ def _make_unique_name(base: str, existing: set[str]) -> str: # Convert CamelCase to snake_case def _camel_to_snake(name: str) -> str: import re - s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) - return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() + + s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name) + return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() base = _camel_to_snake(base) @@ -781,12 +782,12 @@ def __getattr__(self, name: str) -> dict[str, Any]: raise AttributeError( f"'{self.__class__.__name__}' object has no attribute '{name}'" ) - return {agentset: getattr(agentset, name) for agentset in self._agentsets} + return {agentset.name: getattr(agentset, name) for agentset in self._agentsets} @overload def __getitem__( self, key: str | tuple[dict[AgentSetDF, AgentMask], str] - ) -> dict[AgentSetDF, Series | pl.Expr]: ... + ) -> dict[str, Series | pl.Expr]: ... @overload def __getitem__( @@ -797,7 +798,7 @@ def __getitem__( | IdsLike | tuple[dict[AgentSetDF, AgentMask], Collection[str]] ), - ) -> dict[AgentSetDF, DataFrame]: ... + ) -> dict[str, DataFrame]: ... def __getitem__( self, @@ -809,7 +810,7 @@ def __getitem__( | tuple[dict[AgentSetDF, AgentMask], str] | tuple[dict[AgentSetDF, AgentMask], Collection[str]] ), - ) -> dict[AgentSetDF, Series | pl.Expr] | dict[AgentSetDF, DataFrame]: + ) -> dict[str, Series | pl.Expr] | dict[str, DataFrame]: return super().__getitem__(key) def __iadd__(self, agents: AgentSetDF | Iterable[AgentSetDF]) -> Self: From 8f9fa542d0a0b0a4db3bb2876e1c56e9772a9b94 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 20:39:57 +0200 Subject: [PATCH 045/329] Enhance AgentContainer type hints to support string and collection of strings; improve method signatures for clarity. --- mesa_frames/abstract/agents.py | 58 ++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/mesa_frames/abstract/agents.py b/mesa_frames/abstract/agents.py index 76a34de5..f74c5513 100644 --- a/mesa_frames/abstract/agents.py +++ b/mesa_frames/abstract/agents.py @@ -77,7 +77,9 @@ def discard( agents: IdsLike | AgentMask | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF], + | Collection[mesa_frames.concrete.agents.AgentSetDF] + | str + | Collection[str], inplace: bool = True, ) -> Self: """Remove agents from the AgentContainer. Does not raise an error if the agent is not found. @@ -130,12 +132,20 @@ def contains(self, agents: int) -> bool: ... @overload @abstractmethod def contains( - self, agents: mesa_frames.concrete.agents.AgentSetDF | IdsLike + self, + agents: mesa_frames.concrete.agents.AgentSetDF + | IdsLike + | str + | Collection[str], ) -> BoolSeries: ... @abstractmethod def contains( - self, agents: mesa_frames.concrete.agents.AgentSetDF | IdsLike + self, + agents: mesa_frames.concrete.agents.AgentSetDF + | IdsLike + | str + | Collection[str], ) -> bool | BoolSeries: """Check if agents with the specified IDs are in the AgentContainer. @@ -172,7 +182,7 @@ def do( return_results: Literal[True], inplace: bool = True, **kwargs: Any, - ) -> Any | dict[mesa_frames.concrete.agents.AgentSetDF, Any]: ... + ) -> Any | dict[str, Any]: ... @abstractmethod def do( @@ -183,7 +193,7 @@ def do( return_results: bool = False, inplace: bool = True, **kwargs: Any, - ) -> Self | Any | dict[mesa_frames.concrete.agents.AgentSetDF, Any]: + ) -> Self | Any | dict[str, Any]: """Invoke a method on the AgentContainer. Parameters @@ -248,6 +258,8 @@ def remove( | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + | str + | Collection[str] ), inplace: bool = True, ) -> Self: @@ -413,12 +425,12 @@ def __add__( """ return self.add(agents=other, inplace=False) - def __contains__(self, agents: int | AgentSetDF) -> bool: + def __contains__(self, agents: int | AgentSetDF | str) -> bool: """Check if an agent is in the AgentContainer. Parameters ---------- - agents : int | AgentSetDF + agents : int | AgentSetDF | str The ID(s) or AgentSetDF to check for. Returns @@ -431,13 +443,13 @@ def __contains__(self, agents: int | AgentSetDF) -> bool: @overload def __getitem__( self, key: str | tuple[AgentMask, str] - ) -> Series | dict[AgentSetDF, Series]: ... + ) -> Series | dict[str, Series]: ... @overload def __getitem__( self, key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], - ) -> DataFrame | dict[AgentSetDF, DataFrame]: ... + ) -> DataFrame | dict[str, DataFrame]: ... def __getitem__( self, @@ -447,10 +459,10 @@ def __getitem__( | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] - | tuple[dict[AgentSetDF, AgentMask], str] - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] + | tuple[dict[AgentSetDF | str, AgentMask], str] + | tuple[dict[AgentSetDF | str, AgentMask], Collection[str]] ), - ) -> Series | DataFrame | dict[AgentSetDF, Series] | dict[AgentSetDF, DataFrame]: + ) -> Series | DataFrame | dict[str, Series] | dict[str, DataFrame]: """Implement the [] operator for the AgentContainer. The key can be: @@ -488,6 +500,8 @@ def __iadd__( | DataFrameInput | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + | str + | Collection[str] ), ) -> Self: """Add agents to the AgentContainer through the += operator. @@ -511,6 +525,8 @@ def __isub__( | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + | str + | Collection[str] ), ) -> Self: """Remove agents from the AgentContainer through the -= operator. @@ -534,6 +550,8 @@ def __sub__( | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + | str + | Collection[str] ), ) -> Self: """Remove agents from a new AgentContainer through the - operator. @@ -557,8 +575,8 @@ def __setitem__( | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] - | tuple[dict[AgentSetDF, AgentMask], str] - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] + | tuple[dict[AgentSetDF | str, AgentMask], str] + | tuple[dict[AgentSetDF | str, AgentMask], Collection[str]] ), values: Any, ) -> None: @@ -744,24 +762,24 @@ def active_agents( @abstractmethod def inactive_agents( self, - ) -> DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame]: + ) -> DataFrame | dict[str, DataFrame]: """The inactive agents in the AgentContainer. Returns ------- - DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame] + DataFrame | dict[str, DataFrame] """ @property @abstractmethod def index( self, - ) -> Index | dict[mesa_frames.concrete.agents.AgentSetDF, Index]: + ) -> Index | dict[str, Index]: """The ids in the AgentContainer. Returns ------- - Index | dict[mesa_frames.concrete.agents.AgentSetDF, Index] + Index | dict[str, Index] """ ... @@ -769,12 +787,12 @@ def index( @abstractmethod def pos( self, - ) -> DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame]: + ) -> DataFrame | dict[str, DataFrame]: """The position of the agents in the AgentContainer. Returns ------- - DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame] + DataFrame | dict[str, DataFrame] """ ... From f80083c1abcd3194f2c33ae7f4c795c544437592 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 30 Aug 2025 20:40:23 +0200 Subject: [PATCH 046/329] Refactor AgentSetPolars to improve readability of name assignment; format multiline expression for clarity. --- mesa_frames/concrete/agentset.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index a2caee9f..0ab0056e 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -99,7 +99,11 @@ def __init__( # Model reference self._model = model # Set proposed name (no uniqueness guarantees here) - self._name = name if name is not None else camel_case_to_snake_case(self.__class__.__name__) + self._name = ( + name + if name is not None + else camel_case_to_snake_case(self.__class__.__name__) + ) # No definition of schema with unique_id, as it becomes hard to add new agents self._df = pl.DataFrame() self._mask = pl.repeat(True, len(self._df), dtype=pl.Boolean, eager=True) From 231e3bd9082cd919499c8343dea016ef49fa81e6 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 31 Aug 2025 21:13:56 +0200 Subject: [PATCH 047/329] Refactor tests to use updated Model and AgentSet classes - Updated test_datacollector.py to replace ModelDF and AgentSetPolars with Model and AgentSet. - Modified ExampleModel and ExampleModelWithMultipleCollects to use AgentSetRegistry. - Adjusted fixtures to reflect changes in agent set classes. - Updated test_grid.py to use new Model and AgentSet classes, ensuring compatibility with the refactored code. - Changed test_modeldf.py to utilize the new Model class. - Updated dependencies in uv.lock to include mesa version 3.2.0. --- AGENTS.md | 2 +- README.md | 20 +- ROADMAP.md | 2 +- docs/api/reference/agents/index.rst | 6 +- docs/api/reference/model.rst | 2 +- docs/general/index.md | 12 +- docs/general/user-guide/0_getting-started.md | 16 +- docs/general/user-guide/1_classes.md | 38 +- .../user-guide/2_introductory-tutorial.ipynb | 42 +- docs/general/user-guide/4_datacollector.ipynb | 14 +- examples/boltzmann_wealth/performance_plot.py | 32 +- examples/sugarscape_ig/ss_polars/agents.py | 10 +- examples/sugarscape_ig/ss_polars/model.py | 12 +- mesa_frames/__init__.py | 18 +- mesa_frames/abstract/__init__.py | 8 +- mesa_frames/abstract/agents.py | 293 +++++----- mesa_frames/abstract/datacollector.py | 8 +- mesa_frames/abstract/mixin.py | 14 +- mesa_frames/abstract/space.py | 222 +++++--- mesa_frames/concrete/__init__.py | 30 +- mesa_frames/concrete/agents.py | 240 +++++---- mesa_frames/concrete/agentset.py | 56 +- mesa_frames/concrete/datacollector.py | 20 +- mesa_frames/concrete/mixin.py | 10 +- mesa_frames/concrete/model.py | 72 +-- mesa_frames/concrete/space.py | 14 +- tests/test_agents.py | 500 +++++++++--------- tests/test_agentset.py | 225 ++++---- tests/test_datacollector.py | 98 ++-- tests/test_grid.py | 168 +++--- tests/test_modeldf.py | 6 +- uv.lock | 2 + 32 files changed, 1138 insertions(+), 1074 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 19b3caa8..cd78226f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -36,7 +36,7 @@ Always run tools via uv: `uv run `. ## Commit & Pull Request Guidelines - Commits: Imperative mood, concise subject, meaningful body when needed. - Example: `Fix AgentsDF.sets copy binding and tests`. + Example: `Fix AgentSetRegistry.sets copy binding and tests`. - PRs: Link issues, summarize changes, note API impacts, add/adjust tests and docs. - CI hygiene: Run `ruff`, `pytest`, and `pre-commit` locally before pushing. diff --git a/README.md b/README.md index 986b9b22..938eb95c 100644 --- a/README.md +++ b/README.md @@ -88,13 +88,13 @@ pip install -e . ### Creation of an Agent -The agent implementation differs from base mesa. Agents are only defined at the AgentSet level. You can import `AgentSetPolars`. As in mesa, you subclass and make sure to call `super().__init__(model)`. You can use the `add` method or the `+=` operator to add agents to the AgentSet. Most methods mirror the functionality of `mesa.AgentSet`. Additionally, `mesa-frames.AgentSet` implements many dunder methods such as `AgentSet[mask, attr]` to get and set items intuitively. All operations are by default inplace, but if you'd like to use functional programming, mesa-frames implements a fast copy method which aims to reduce memory usage, relying on reference-only and native copy methods. +The agent implementation differs from base mesa. Agents are only defined at the AgentSet level. You can import `AgentSet`. As in mesa, you subclass and make sure to call `super().__init__(model)`. You can use the `add` method or the `+=` operator to add agents to the AgentSet. Most methods mirror the functionality of `mesa.AgentSet`. Additionally, `mesa-frames.AgentSet` implements many dunder methods such as `AgentSet[mask, attr]` to get and set items intuitively. All operations are by default inplace, but if you'd like to use functional programming, mesa-frames implements a fast copy method which aims to reduce memory usage, relying on reference-only and native copy methods. ```python -from mesa-frames import AgentSetPolars +from mesa-frames import AgentSet -class MoneyAgentPolars(AgentSetPolars): - def __init__(self, n: int, model: ModelDF): +class MoneyAgentDF(AgentSet): + def __init__(self, n: int, model: Model): super().__init__(model) # Adding the agents to the agent set self += pl.DataFrame( @@ -126,20 +126,20 @@ class MoneyAgentPolars(AgentSetPolars): ### Creation of the Model -Creation of the model is fairly similar to the process in mesa. You subclass `ModelDF` and call `super().__init__()`. The `model.agents` attribute has the same interface as `mesa-frames.AgentSet`. You can use `+=` or `self.agents.add` with a `mesa-frames.AgentSet` (or a list of `AgentSet`) to add agents to the model. +Creation of the model is fairly similar to the process in mesa. You subclass `Model` and call `super().__init__()`. The `model.sets` attribute has the same interface as `mesa-frames.AgentSet`. You can use `+=` or `self.sets.add` with a `mesa-frames.AgentSet` (or a list of `AgentSet`) to add agents to the model. ```python -from mesa-frames import ModelDF +from mesa-frames import Model -class MoneyModelDF(ModelDF): +class MoneyModelDF(Model): def __init__(self, N: int, agents_cls): super().__init__() self.n_agents = N - self.agents += MoneyAgentPolars(N, self) + self.sets += MoneyAgentDF(N, self) def step(self): - # Executes the step method for every agentset in self.agents - self.agents.do("step") + # Executes the step method for every agentset in self.sets + self.sets.do("step") def run_model(self, n): for _ in range(n): diff --git a/ROADMAP.md b/ROADMAP.md index b42b9901..03f3040c 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -49,7 +49,7 @@ The Sugarscape example demonstrates the need for this abstraction, as multiple a #### Progress and Next Steps -- Create utility functions in `DiscreteSpaceDF` and `AgentContainer` to move agents optimally based on specified attributes +- Create utility functions in `DiscreteSpaceDF` and `AbstractAgentSetRegistry` to move agents optimally based on specified attributes - Provide built-in resolution strategies for common concurrency scenarios - Ensure the implementation works efficiently with the vectorized approach of mesa-frames diff --git a/docs/api/reference/agents/index.rst b/docs/api/reference/agents/index.rst index 5d725f02..a1c03126 100644 --- a/docs/api/reference/agents/index.rst +++ b/docs/api/reference/agents/index.rst @@ -4,14 +4,14 @@ Agents .. currentmodule:: mesa_frames -.. autoclass:: AgentSetPolars +.. autoclass:: AgentSet :members: :inherited-members: :autosummary: :autosummary-nosignatures: -.. autoclass:: AgentsDF +.. autoclass:: AgentSetRegistry :members: :inherited-members: :autosummary: - :autosummary-nosignatures: \ No newline at end of file + :autosummary-nosignatures: diff --git a/docs/api/reference/model.rst b/docs/api/reference/model.rst index 0e05d8d7..099e601b 100644 --- a/docs/api/reference/model.rst +++ b/docs/api/reference/model.rst @@ -3,7 +3,7 @@ Model .. currentmodule:: mesa_frames -.. autoclass:: ModelDF +.. autoclass:: Model :members: :inherited-members: :autosummary: diff --git a/docs/general/index.md b/docs/general/index.md index ea3a52d7..d8255260 100644 --- a/docs/general/index.md +++ b/docs/general/index.md @@ -41,11 +41,11 @@ pip install -e . Here's a quick example of how to create a model using mesa-frames: ```python -from mesa_frames import AgentSetPolars, ModelDF +from mesa_frames import AgentSet, Model import polars as pl -class MoneyAgentPolars(AgentSetPolars): - def __init__(self, n: int, model: ModelDF): +class MoneyAgentDF(AgentSet): + def __init__(self, n: int, model: Model): super().__init__(model) self += pl.DataFrame( {"wealth": pl.ones(n, eager=True)} @@ -57,13 +57,13 @@ class MoneyAgentPolars(AgentSetPolars): def give_money(self): # ... (implementation details) -class MoneyModelDF(ModelDF): +class MoneyModelDF(Model): def __init__(self, N: int): super().__init__() - self.agents += MoneyAgentPolars(N, self) + self.sets += MoneyAgentDF(N, self) def step(self): - self.agents.do("step") + self.sets.do("step") def run_model(self, n): for _ in range(n): diff --git a/docs/general/user-guide/0_getting-started.md b/docs/general/user-guide/0_getting-started.md index b2917576..5d2b4cd2 100644 --- a/docs/general/user-guide/0_getting-started.md +++ b/docs/general/user-guide/0_getting-started.md @@ -35,14 +35,14 @@ Here's a comparison between mesa-frames and mesa: === "mesa-frames" ```python - class MoneyAgentPolarsConcise(AgentSetPolars): + class MoneyAgentDFConcise(AgentSet): # initialization... def give_money(self): # Active agents are changed to wealthy agents self.select(self.wealth > 0) # Receiving agents are sampled (only native expressions currently supported) - other_agents = self.agents.sample( + other_agents = self.sets.sample( n=len(self.active_agents), with_replacement=True ) @@ -64,7 +64,7 @@ Here's a comparison between mesa-frames and mesa: def give_money(self): # Verify agent has some wealth if self.wealth > 0: - other_agent = self.random.choice(self.model.agents) + other_agent = self.random.choice(self.model.sets) if other_agent is not None: other_agent.wealth += 1 self.wealth -= 1 @@ -84,7 +84,7 @@ If you're familiar with mesa, this guide will help you understand the key differ === "mesa-frames" ```python - class MoneyAgentSet(AgentSetPolars): + class MoneyAgentSet(AgentSet): def __init__(self, n, model): super().__init__(model) self += pl.DataFrame({ @@ -92,7 +92,7 @@ If you're familiar with mesa, this guide will help you understand the key differ }) def step(self): givers = self.wealth > 0 - receivers = self.agents.sample(n=len(self.active_agents)) + receivers = self.sets.sample(n=len(self.active_agents)) self[givers, "wealth"] -= 1 new_wealth = receivers.groupby("unique_id").count() self[new_wealth["unique_id"], "wealth"] += new_wealth["count"] @@ -121,13 +121,13 @@ If you're familiar with mesa, this guide will help you understand the key differ === "mesa-frames" ```python - class MoneyModel(ModelDF): + class MoneyModel(Model): def __init__(self, N): super().__init__() - self.agents += MoneyAgentSet(N, self) + self.sets += MoneyAgentSet(N, self) def step(self): - self.agents.do("step") + self.sets.do("step") ``` diff --git a/docs/general/user-guide/1_classes.md b/docs/general/user-guide/1_classes.md index ac696731..f2b53b8e 100644 --- a/docs/general/user-guide/1_classes.md +++ b/docs/general/user-guide/1_classes.md @@ -1,18 +1,18 @@ # Classes 📚 -## AgentSetDF 👥 +## AgentSet 👥 -To create your own AgentSetDF class, you need to subclass the AgentSetPolars class and make sure to call `super().__init__(model)`. +To create your own AgentSet class, you need to subclass the AgentSet class and make sure to call `super().__init__(model)`. -Typically, the next step would be to populate the class with your agents. To do that, you need to add a DataFrame to the AgentSetDF. You can do `self += agents` or `self.add(agents)`, where `agents` is a DataFrame or something that could be passed to a DataFrame constructor, like a dictionary or lists of lists. You need to make sure your DataFrame doesn't have a 'unique_id' column because IDs are generated automatically, otherwise you will get an error raised. In the DataFrame, you should also put any attribute of the agent you are using. +Typically, the next step would be to populate the class with your agents. To do that, you need to add a DataFrame to the AgentSet. You can do `self += agents` or `self.add(agents)`, where `agents` is a DataFrame or something that could be passed to a DataFrame constructor, like a dictionary or lists of lists. You need to make sure your DataFrame doesn't have a 'unique_id' column because IDs are generated automatically, otherwise you will get an error raised. In the DataFrame, you should also put any attribute of the agent you are using. How can you choose which agents should be in the same AgentSet? The idea is that you should minimize the missing values in the DataFrame (so they should have similar/same attributes) and mostly everybody should do the same actions. Example: ```python -class MoneyAgent(AgentSetPolars): - def __init__(self, n: int, model: ModelDF): +class MoneyAgent(AgentSet): + def __init__(self, n: int, model: Model): super().__init__(model) self.initial_wealth = pl.ones(n) self += pl.DataFrame({ @@ -25,24 +25,24 @@ class MoneyAgent(AgentSetPolars): You can access the underlying DataFrame where agents are stored with `self.df`. This allows you to use DataFrame methods like `self.df.sample` or `self.df.group_by("wealth")` and more. -## ModelDF 🏗️ +## Model 🏗️ -To add your AgentSetDF to your ModelDF, you should also add it to the agents with `+=` or `add`. +To add your AgentSet to your Model, you should also add it to the sets with `+=` or `add`. -NOTE: ModelDF.agents are stored in a class which is entirely similar to AgentSetDF called AgentsDF. The API of the two are the same. If you try accessing AgentsDF.df, you will get a dictionary of `[AgentSetDF, DataFrame]`. +NOTE: Model.sets are stored in a class which is entirely similar to AgentSet called AgentSetRegistry. The API of the two are the same. If you try accessing AgentSetRegistry.df, you will get a dictionary of `[AgentSet, DataFrame]`. Example: ```python -class EcosystemModel(ModelDF): +class EcosystemModel(Model): def __init__(self, n_prey, n_predators): super().__init__() - self.agents += Preys(n_prey, self) - self.agents += Predators(n_predators, self) + self.sets += Preys(n_prey, self) + self.sets += Predators(n_predators, self) def step(self): - self.agents.do("move") - self.agents.do("hunt") + self.sets.do("move") + self.sets.do("hunt") self.prey.do("reproduce") ``` @@ -55,12 +55,12 @@ mesa-frames provides efficient implementations of spatial environments: Example: ```python -class GridWorld(ModelDF): +class GridWorld(Model): def __init__(self, width, height): super().__init__() self.space = GridPolars(self, (width, height)) - self.agents += AgentSet(100, self) - self.space.place_to_empty(self.agents) + self.sets += AgentSet(100, self) + self.space.place_to_empty(self.sets) ``` A continuous GeoSpace, NetworkSpace, and a collection to have multiple spaces in the models are in the works! 🚧 @@ -73,10 +73,10 @@ You configure what to collect, how to store it, and when to trigger collection. Example: ```python -class ExampleModel(ModelDF): +class ExampleModel(Model): def __init__(self): super().__init__() - self.agents = MoneyAgent(self) + self.sets = MoneyAgent(self) self.datacollector = DataCollector( model=self, model_reporters={"total_wealth": lambda m: m.agents["wealth"].sum()}, @@ -87,7 +87,7 @@ class ExampleModel(ModelDF): ) def step(self): - self.agents.step() + self.sets.step() self.datacollector.conditional_collect() self.datacollector.flush() ``` diff --git a/docs/general/user-guide/2_introductory-tutorial.ipynb b/docs/general/user-guide/2_introductory-tutorial.ipynb index 24742f80..327a32b2 100644 --- a/docs/general/user-guide/2_introductory-tutorial.ipynb +++ b/docs/general/user-guide/2_introductory-tutorial.ipynb @@ -49,14 +49,14 @@ "metadata": {}, "outputs": [], "source": [ - "from mesa_frames import ModelDF, AgentSetPolars, DataCollector\n", + "from mesa_frames import Model, AgentSet, DataCollector\n", "\n", "\n", - "class MoneyModelDF(ModelDF):\n", + "class MoneyModelDF(Model):\n", " def __init__(self, N: int, agents_cls):\n", " super().__init__()\n", " self.n_agents = N\n", - " self.agents += agents_cls(N, self)\n", + " self.sets += agents_cls(N, self)\n", " self.datacollector = DataCollector(\n", " model=self,\n", " model_reporters={\"total_wealth\": lambda m: m.agents[\"wealth\"].sum()},\n", @@ -67,8 +67,8 @@ " )\n", "\n", " def step(self):\n", - " # Executes the step method for every agentset in self.agents\n", - " self.agents.do(\"step\")\n", + " # Executes the step method for every agentset in self.sets\n", + " self.sets.do(\"step\")\n", "\n", " def run_model(self, n):\n", " for _ in range(n):\n", @@ -97,8 +97,8 @@ "import polars as pl\n", "\n", "\n", - "class MoneyAgentPolars(AgentSetPolars):\n", - " def __init__(self, n: int, model: ModelDF):\n", + "class MoneyAgentDF(AgentSet):\n", + " def __init__(self, n: int, model: Model):\n", " super().__init__(model)\n", " self += pl.DataFrame({\"wealth\": pl.ones(n, eager=True)})\n", "\n", @@ -154,14 +154,14 @@ } ], "source": [ - "# Choose either MoneyAgentPandas or MoneyAgentPolars\n", - "agent_class = MoneyAgentPolars\n", + "# Choose either MoneyAgentPandas or MoneyAgentDF\n", + "agent_class = MoneyAgentDF\n", "\n", "# Create and run the model\n", "model = MoneyModelDF(1000, agent_class)\n", "model.run_model(100)\n", "\n", - "wealth_dist = list(model.agents.df.values())[0]\n", + "wealth_dist = list(model.sets.df.values())[0]\n", "\n", "# Print the final wealth distribution\n", "print(wealth_dist.select(pl.col(\"wealth\")).describe())" @@ -187,8 +187,8 @@ "metadata": {}, "outputs": [], "source": [ - "class MoneyAgentPolarsConcise(AgentSetPolars):\n", - " def __init__(self, n: int, model: ModelDF):\n", + "class MoneyAgentDFConcise(AgentSet):\n", + " def __init__(self, n: int, model: Model):\n", " super().__init__(model)\n", " ## Adding the agents to the agent set\n", " # 1. Changing the df attribute directly (not recommended, if other agents were added before, they will be lost)\n", @@ -242,8 +242,8 @@ " self[new_wealth, \"wealth\"] += new_wealth[\"len\"]\n", "\n", "\n", - "class MoneyAgentPolarsNative(AgentSetPolars):\n", - " def __init__(self, n: int, model: ModelDF):\n", + "class MoneyAgentDFNative(AgentSet):\n", + " def __init__(self, n: int, model: Model):\n", " super().__init__(model)\n", " self += pl.DataFrame({\"wealth\": pl.ones(n, eager=True)})\n", "\n", @@ -307,7 +307,7 @@ " def step(self):\n", " # Verify agent has some wealth\n", " if self.wealth > 0:\n", - " other_agent: MoneyAgent = self.model.random.choice(self.model.agents)\n", + " other_agent: MoneyAgent = self.model.random.choice(self.model.sets)\n", " if other_agent is not None:\n", " other_agent.wealth += 1\n", " self.wealth -= 1\n", @@ -320,11 +320,11 @@ " super().__init__()\n", " self.num_agents = N\n", " for _ in range(N):\n", - " self.agents.add(MoneyAgent(self))\n", + " self.sets.add(MoneyAgent(self))\n", "\n", " def step(self):\n", " \"\"\"Advance the model by one step.\"\"\"\n", - " self.agents.shuffle_do(\"step\")\n", + " self.sets.shuffle_do(\"step\")\n", "\n", " def run_model(self, n_steps) -> None:\n", " for _ in range(n_steps):\n", @@ -388,13 +388,9 @@ " if implementation == \"mesa\":\n", " ntime = run_simulation(MoneyModel(n_agents), n_steps)\n", " elif implementation == \"mesa-frames (pl concise)\":\n", - " ntime = run_simulation(\n", - " MoneyModelDF(n_agents, MoneyAgentPolarsConcise), n_steps\n", - " )\n", + " ntime = run_simulation(MoneyModelDF(n_agents, MoneyAgentDFConcise), n_steps)\n", " elif implementation == \"mesa-frames (pl native)\":\n", - " ntime = run_simulation(\n", - " MoneyModelDF(n_agents, MoneyAgentPolarsNative), n_steps\n", - " )\n", + " ntime = run_simulation(MoneyModelDF(n_agents, MoneyAgentDFNative), n_steps)\n", "\n", " print(f\" Number of agents: {n_agents}, Time: {ntime:.2f} seconds\")\n", " print(\"---------------\")" diff --git a/docs/general/user-guide/4_datacollector.ipynb b/docs/general/user-guide/4_datacollector.ipynb index 247dbf70..1fdc114f 100644 --- a/docs/general/user-guide/4_datacollector.ipynb +++ b/docs/general/user-guide/4_datacollector.ipynb @@ -43,7 +43,7 @@ "source": [ "## Minimal Example Model\n", "\n", - "We create a tiny model using the `ModelDF` and an `AgentSetPolars`-style agent container. This is just to demonstrate collection APIs.\n" + "We create a tiny model using the `Model` and an `AgentSet`-style agent container. This is just to demonstrate collection APIs.\n" ] }, { @@ -55,12 +55,12 @@ }, "outputs": [], "source": [ - "from mesa_frames import ModelDF, AgentSetPolars, DataCollector\n", + "from mesa_frames import Model, AgentSet, DataCollector\n", "import polars as pl\n", "\n", "\n", - "class MoneyAgents(AgentSetPolars):\n", - " def __init__(self, n: int, model: ModelDF):\n", + "class MoneyAgents(AgentSet):\n", + " def __init__(self, n: int, model: Model):\n", " super().__init__(model)\n", " # one column, one unit of wealth each\n", " self += pl.DataFrame({\"wealth\": pl.ones(n, eager=True)})\n", @@ -73,10 +73,10 @@ " self[income[\"unique_id\"], \"wealth\"] += income[\"len\"]\n", "\n", "\n", - "class MoneyModel(ModelDF):\n", + "class MoneyModel(Model):\n", " def __init__(self, n: int):\n", " super().__init__()\n", - " self.agents = MoneyAgents(n, self)\n", + " self.sets = MoneyAgents(n, self)\n", " self.dc = DataCollector(\n", " model=self,\n", " model_reporters={\n", @@ -94,7 +94,7 @@ " )\n", "\n", " def step(self):\n", - " self.agents.do(\"step\")\n", + " self.sets.do(\"step\")\n", "\n", " def run(self, steps: int, conditional: bool = True):\n", " for _ in range(steps):\n", diff --git a/examples/boltzmann_wealth/performance_plot.py b/examples/boltzmann_wealth/performance_plot.py index 625c6c56..e5b0ad47 100644 --- a/examples/boltzmann_wealth/performance_plot.py +++ b/examples/boltzmann_wealth/performance_plot.py @@ -8,7 +8,7 @@ import seaborn as sns from packaging import version -from mesa_frames import AgentSetPolars, ModelDF +from mesa_frames import AgentSet, Model ### ---------- Mesa implementation ---------- ### @@ -30,7 +30,7 @@ def __init__(self, model): def step(self): # Verify agent has some wealth if self.wealth > 0: - other_agent = self.random.choice(self.model.agents) + other_agent = self.random.choice(self.model.sets) if other_agent is not None: other_agent.wealth += 1 self.wealth -= 1 @@ -43,11 +43,11 @@ def __init__(self, N): super().__init__() self.num_agents = N for _ in range(self.num_agents): - self.agents.add(MoneyAgent(self)) + self.sets.add(MoneyAgent(self)) def step(self): """Advance the model by one step.""" - self.agents.shuffle_do("step") + self.sets.shuffle_do("step") def run_model(self, n_steps) -> None: for _ in range(n_steps): @@ -55,7 +55,7 @@ def run_model(self, n_steps) -> None: """def compute_gini(model): - agent_wealths = model.agents.get("wealth") + agent_wealths = model.sets.get("wealth") x = sorted(agent_wealths) N = model.num_agents B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x)) @@ -65,12 +65,12 @@ def run_model(self, n_steps) -> None: ### ---------- Mesa-frames implementation ---------- ### -class MoneyAgentPolarsConcise(AgentSetPolars): - def __init__(self, n: int, model: ModelDF): +class MoneyAgentDFConcise(AgentSet): + def __init__(self, n: int, model: Model): super().__init__(model) ## Adding the agents to the agent set # 1. Changing the agents attribute directly (not recommended, if other agents were added before, they will be lost) - """self.agents = pl.DataFrame( + """self.sets = pl.DataFrame( "wealth": pl.ones(n, eager=True)} )""" # 2. Adding the dataframe with add @@ -120,8 +120,8 @@ def give_money(self): self[new_wealth, "wealth"] += new_wealth["len"] -class MoneyAgentPolarsNative(AgentSetPolars): - def __init__(self, n: int, model: ModelDF): +class MoneyAgentDFNative(AgentSet): + def __init__(self, n: int, model: Model): super().__init__(model) self += pl.DataFrame({"wealth": pl.ones(n, eager=True)}) @@ -154,15 +154,15 @@ def give_money(self): ) -class MoneyModelDF(ModelDF): +class MoneyModelDF(Model): def __init__(self, N: int, agents_cls): super().__init__() self.n_agents = N - self.agents += agents_cls(N, self) + self.sets += agents_cls(N, self) def step(self): - # Executes the step method for every agentset in self.agents - self.agents.do("step") + # Executes the step method for every agentset in self.sets + self.sets.do("step") def run_model(self, n): for _ in range(n): @@ -170,12 +170,12 @@ def run_model(self, n): def mesa_frames_polars_concise(n_agents: int) -> None: - model = MoneyModelDF(n_agents, MoneyAgentPolarsConcise) + model = MoneyModelDF(n_agents, MoneyAgentDFConcise) model.run_model(100) def mesa_frames_polars_native(n_agents: int) -> None: - model = MoneyModelDF(n_agents, MoneyAgentPolarsNative) + model = MoneyModelDF(n_agents, MoneyAgentDFNative) model.run_model(100) diff --git a/examples/sugarscape_ig/ss_polars/agents.py b/examples/sugarscape_ig/ss_polars/agents.py index 2d921761..b0ecbe90 100644 --- a/examples/sugarscape_ig/ss_polars/agents.py +++ b/examples/sugarscape_ig/ss_polars/agents.py @@ -4,13 +4,13 @@ import polars as pl from numba import b1, guvectorize, int32 -from mesa_frames import AgentSetPolars, ModelDF +from mesa_frames import AgentSet, Model -class AntPolarsBase(AgentSetPolars): +class AntDFBase(AgentSet): def __init__( self, - model: ModelDF, + model: Model, n_agents: int, initial_sugar: np.ndarray | None = None, metabolism: np.ndarray | None = None, @@ -169,7 +169,7 @@ def get_best_moves(self, neighborhood: pl.DataFrame) -> pl.DataFrame: raise NotImplementedError("Subclasses must implement this method") -class AntPolarsLoopDF(AntPolarsBase): +class AntPolarsLoopDF(AntDFBase): def get_best_moves(self, neighborhood: pl.DataFrame): best_moves = pl.DataFrame() @@ -224,7 +224,7 @@ def get_best_moves(self, neighborhood: pl.DataFrame): return best_moves.sort("agent_order").select(["dim_0", "dim_1"]) -class AntPolarsLoop(AntPolarsBase): +class AntPolarsLoop(AntDFBase): numba_target = None def get_best_moves(self, neighborhood: pl.DataFrame): diff --git a/examples/sugarscape_ig/ss_polars/model.py b/examples/sugarscape_ig/ss_polars/model.py index be9768c1..fe2c5425 100644 --- a/examples/sugarscape_ig/ss_polars/model.py +++ b/examples/sugarscape_ig/ss_polars/model.py @@ -1,12 +1,12 @@ import numpy as np import polars as pl -from mesa_frames import GridPolars, ModelDF +from mesa_frames import GridPolars, Model from .agents import AntPolarsBase -class SugarscapePolars(ModelDF): +class SugarscapePolars(Model): def __init__( self, agent_type: type[AntPolarsBase], @@ -33,15 +33,15 @@ def __init__( sugar=sugar_grid.flatten(), max_sugar=sugar_grid.flatten() ) self.space.set_cells(sugar_grid) - self.agents += agent_type(self, n_agents, initial_sugar, metabolism, vision) + self.sets += agent_type(self, n_agents, initial_sugar, metabolism, vision) if initial_positions is not None: - self.space.place_agents(self.agents, initial_positions) + self.space.place_agents(self.sets, initial_positions) else: - self.space.place_to_empty(self.agents) + self.space.place_to_empty(self.sets) def run_model(self, steps: int) -> list[int]: for _ in range(steps): - if len(self.agents) == 0: + if len(self.sets) == 0: return empty_cells = self.space.empty_cells full_cells = self.space.full_cells diff --git a/mesa_frames/__init__.py b/mesa_frames/__init__.py index d47087d1..4bca420e 100644 --- a/mesa_frames/__init__.py +++ b/mesa_frames/__init__.py @@ -14,19 +14,19 @@ - Includes GridDF for efficient grid-based spatial modeling Main Components: -- AgentSetPolars: Agent set implementation using Polars backend -- ModelDF: Base model class for mesa-frames +- AgentSet: Agent set implementation using Polars backend +- Model: Base model class for mesa-frames - GridDF: Grid space implementation for spatial modeling Usage: To use mesa-frames, import the necessary components and subclass them as needed: - from mesa_frames import AgentSetPolars, ModelDF, GridDF + from mesa_frames import AgentSet, Model, GridDF - class MyAgent(AgentSetPolars): + class MyAgent(AgentSet): # Define your agent logic here - class MyModel(ModelDF): + class MyModel(Model): def __init__(self, width, height): super().__init__() self.grid = GridDF(width, height, self) @@ -60,12 +60,12 @@ def __init__(self, width, height): stacklevel=2, ) -from mesa_frames.concrete.agents import AgentsDF -from mesa_frames.concrete.agentset import AgentSetPolars -from mesa_frames.concrete.model import ModelDF +from mesa_frames.concrete.agents import AgentSetRegistry +from mesa_frames.concrete.agentset import AgentSet +from mesa_frames.concrete.model import Model from mesa_frames.concrete.space import GridPolars from mesa_frames.concrete.datacollector import DataCollector -__all__ = ["AgentsDF", "AgentSetPolars", "ModelDF", "GridPolars", "DataCollector"] +__all__ = ["AgentSetRegistry", "AgentSet", "Model", "GridPolars", "DataCollector"] __version__ = "0.1.1.dev0" diff --git a/mesa_frames/abstract/__init__.py b/mesa_frames/abstract/__init__.py index b61914db..4bc87315 100644 --- a/mesa_frames/abstract/__init__.py +++ b/mesa_frames/abstract/__init__.py @@ -6,8 +6,8 @@ Classes: agents.py: - - AgentContainer: Abstract base class for agent containers. - - AgentSetDF: Abstract base class for agent sets using DataFrames. + - AbstractAgentSetRegistry: Abstract base class for agent containers. + - AbstractAgentSet: Abstract base class for agent sets using DataFrames. mixin.py: - CopyMixin: Mixin class providing fast copy functionality. @@ -28,9 +28,9 @@ For example: - from mesa_frames.abstract import AgentSetDF, DataFrameMixin + from mesa_frames.abstract import AbstractAgentSet, DataFrameMixin - class ConcreteAgentSet(AgentSetDF): + class ConcreteAgentSet(AbstractAgentSet): # Implement abstract methods here ... diff --git a/mesa_frames/abstract/agents.py b/mesa_frames/abstract/agents.py index f4243558..3f746b9f 100644 --- a/mesa_frames/abstract/agents.py +++ b/mesa_frames/abstract/agents.py @@ -6,14 +6,14 @@ manipulation using DataFrame-based approaches. Classes: - AgentContainer(CopyMixin): + AbstractAgentSetRegistry(CopyMixin): An abstract base class that defines the common interface for all agent containers in mesa-frames. It inherits from CopyMixin to provide fast copying functionality. - AgentSetDF(AgentContainer, DataFrameMixin): + AbstractAgentSet(AbstractAgentSetRegistry, DataFrameMixin): An abstract base class for agent sets that use DataFrames as the underlying - storage mechanism. It inherits from both AgentContainer and DataFrameMixin + storage mechanism. It inherits from both AbstractAgentSetRegistry and DataFrameMixin to combine agent container functionality with DataFrame operations. These abstract classes are designed to be subclassed by concrete implementations @@ -23,12 +23,12 @@ These classes should not be instantiated directly. Instead, they should be subclassed to create concrete implementations: - from mesa_frames.abstract.agents import AgentSetDF + from mesa_frames.abstract.agents import AbstractAgentSet - class AgentSetPolars(AgentSetDF): + class AgentSet(AbstractAgentSet): def __init__(self, model): super().__init__(model) - # Implementation using polars DataFrame + # Implementation using a DataFrame backend ... # Implement other abstract methods @@ -61,13 +61,13 @@ def __init__(self, model): ) -class AgentContainer(CopyMixin): - """An abstract class for containing agents. Defines the common interface for AgentSetDF and AgentsDF.""" +class AbstractAgentSetRegistry(CopyMixin): + """An abstract class for containing agents. Defines the common interface for AbstractAgentSet and AgentSetRegistry.""" _copy_only_reference: list[str] = [ "_model", ] - _model: mesa_frames.concrete.model.ModelDF + _model: mesa_frames.concrete.model.Model @abstractmethod def __init__(self) -> None: ... @@ -76,15 +76,15 @@ def discard( self, agents: IdsLike | AgentMask - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF], + | mesa_frames.concrete.agents.AbstractAgentSet + | Collection[mesa_frames.concrete.agents.AbstractAgentSet], inplace: bool = True, ) -> Self: - """Remove agents from the AgentContainer. Does not raise an error if the agent is not found. + """Remove agents from the AbstractAgentSetRegistry. Does not raise an error if the agent is not found. Parameters ---------- - agents : IdsLike | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + agents : IdsLike | AgentMask | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] The agents to remove inplace : bool Whether to remove the agent in place. Defaults to True. @@ -92,7 +92,7 @@ def discard( Returns ------- Self - The updated AgentContainer. + The updated AbstractAgentSetRegistry. """ with suppress(KeyError, ValueError): return self.remove(agents, inplace=inplace) @@ -103,15 +103,15 @@ def add( self, agents: DataFrame | DataFrameInput - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF], + | mesa_frames.concrete.agents.AbstractAgentSet + | Collection[mesa_frames.concrete.agents.AbstractAgentSet], inplace: bool = True, ) -> Self: - """Add agents to the AgentContainer. + """Add agents to the AbstractAgentSetRegistry. Parameters ---------- - agents : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + agents : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] The agents to add. inplace : bool Whether to add the agents in place. Defaults to True. @@ -119,7 +119,7 @@ def add( Returns ------- Self - The updated AgentContainer. + The updated AbstractAgentSetRegistry. """ ... @@ -130,24 +130,24 @@ def contains(self, agents: int) -> bool: ... @overload @abstractmethod def contains( - self, agents: mesa_frames.concrete.agents.AgentSetDF | IdsLike + self, agents: mesa_frames.concrete.agents.AbstractAgentSet | IdsLike ) -> BoolSeries: ... @abstractmethod def contains( - self, agents: mesa_frames.concrete.agents.AgentSetDF | IdsLike + self, agents: mesa_frames.concrete.agents.AbstractAgentSet | IdsLike ) -> bool | BoolSeries: - """Check if agents with the specified IDs are in the AgentContainer. + """Check if agents with the specified IDs are in the AbstractAgentSetRegistry. Parameters ---------- - agents : mesa_frames.concrete.agents.AgentSetDF | IdsLike + agents : mesa_frames.concrete.agents.AbstractAgentSet | IdsLike The ID(s) to check for. Returns ------- bool | BoolSeries - True if the agent is in the AgentContainer, False otherwise. + True if the agent is in the AbstractAgentSetRegistry, False otherwise. """ @overload @@ -172,7 +172,7 @@ def do( return_results: Literal[True], inplace: bool = True, **kwargs: Any, - ) -> Any | dict[mesa_frames.concrete.agents.AgentSetDF, Any]: ... + ) -> Any | dict[mesa_frames.concrete.agents.AbstractAgentSet, Any]: ... @abstractmethod def do( @@ -183,8 +183,8 @@ def do( return_results: bool = False, inplace: bool = True, **kwargs: Any, - ) -> Self | Any | dict[mesa_frames.concrete.agents.AgentSetDF, Any]: - """Invoke a method on the AgentContainer. + ) -> Self | Any | dict[mesa_frames.concrete.agents.AbstractAgentSet, Any]: + """Invoke a method on the AbstractAgentSetRegistry. Parameters ---------- @@ -203,8 +203,8 @@ def do( Returns ------- - Self | Any | dict[mesa_frames.concrete.agents.AgentSetDF, Any] - The updated AgentContainer or the result of the method. + Self | Any | dict[mesa_frames.concrete.agents.AbstractAgentSet, Any] + The updated AbstractAgentSetRegistry or the result of the method. """ ... @@ -224,7 +224,7 @@ def get( attr_names: str | Collection[str] | None = None, mask: AgentMask | None = None, ) -> Series | dict[str, Series] | DataFrame | dict[str, DataFrame]: - """Retrieve the value of a specified attribute for each agent in the AgentContainer. + """Retrieve the value of a specified attribute for each agent in the AbstractAgentSetRegistry. Parameters ---------- @@ -246,16 +246,16 @@ def remove( agents: ( IdsLike | AgentMask - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF] + | mesa_frames.concrete.agents.AbstractAgentSet + | Collection[mesa_frames.concrete.agents.AbstractAgentSet] ), inplace: bool = True, ) -> Self: - """Remove the agents from the AgentContainer. + """Remove the agents from the AbstractAgentSetRegistry. Parameters ---------- - agents : IdsLike | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + agents : IdsLike | AgentMask | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] The agents to remove. inplace : bool, optional Whether to remove the agent in place. @@ -263,7 +263,7 @@ def remove( Returns ------- Self - The updated AgentContainer. + The updated AbstractAgentSetRegistry. """ ... @@ -276,14 +276,14 @@ def select( negate: bool = False, inplace: bool = True, ) -> Self: - """Select agents in the AgentContainer based on the given criteria. + """Select agents in the AbstractAgentSetRegistry based on the given criteria. Parameters ---------- mask : AgentMask | None, optional The AgentMask of agents to be selected, by default None filter_func : Callable[[Self], AgentMask] | None, optional - A function which takes as input the AgentContainer and returns a AgentMask, by default None + A function which takes as input the AbstractAgentSetRegistry and returns a AgentMask, by default None n : int | None, optional The maximum number of agents to be selected, by default None negate : bool, optional @@ -294,7 +294,7 @@ def select( Returns ------- Self - A new or updated AgentContainer. + A new or updated AbstractAgentSetRegistry. """ ... @@ -326,14 +326,14 @@ def set( mask: AgentMask | None = None, inplace: bool = True, ) -> Self: - """Set the value of a specified attribute or attributes for each agent in the mask in AgentContainer. + """Set the value of a specified attribute or attributes for each agent in the mask in AbstractAgentSetRegistry. Parameters ---------- attr_names : DataFrameInput | str | Collection[str] The key can be: - - A string: sets the specified column of the agents in the AgentContainer. - - A collection of strings: sets the specified columns of the agents in the AgentContainer. + - A string: sets the specified column of the agents in the AbstractAgentSetRegistry. + - A collection of strings: sets the specified columns of the agents in the AbstractAgentSetRegistry. - A dictionary: keys should be attributes and values should be the values to set. Value should be None. values : Any | None The value to set the attribute to. If None, attr_names must be a dictionary. @@ -351,7 +351,7 @@ def set( @abstractmethod def shuffle(self, inplace: bool = False) -> Self: - """Shuffles the order of agents in the AgentContainer. + """Shuffles the order of agents in the AbstractAgentSetRegistry. Parameters ---------- @@ -361,7 +361,7 @@ def shuffle(self, inplace: bool = False) -> Self: Returns ------- Self - A new or updated AgentContainer. + A new or updated AbstractAgentSetRegistry. """ @abstractmethod @@ -389,55 +389,55 @@ def sort( Returns ------- Self - A new or updated AgentContainer. + A new or updated AbstractAgentSetRegistry. """ def __add__( self, other: DataFrame | DataFrameInput - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF], + | mesa_frames.concrete.agents.AbstractAgentSet + | Collection[mesa_frames.concrete.agents.AbstractAgentSet], ) -> Self: - """Add agents to a new AgentContainer through the + operator. + """Add agents to a new AbstractAgentSetRegistry through the + operator. Parameters ---------- - other : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + other : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] The agents to add. Returns ------- Self - A new AgentContainer with the added agents. + A new AbstractAgentSetRegistry with the added agents. """ return self.add(agents=other, inplace=False) - def __contains__(self, agents: int | AgentSetDF) -> bool: - """Check if an agent is in the AgentContainer. + def __contains__(self, agents: int | AbstractAgentSet) -> bool: + """Check if an agent is in the AbstractAgentSetRegistry. Parameters ---------- - agents : int | AgentSetDF - The ID(s) or AgentSetDF to check for. + agents : int | AbstractAgentSet + The ID(s) or AbstractAgentSet to check for. Returns ------- bool - True if the agent is in the AgentContainer, False otherwise. + True if the agent is in the AbstractAgentSetRegistry, False otherwise. """ return self.contains(agents=agents) @overload def __getitem__( self, key: str | tuple[AgentMask, str] - ) -> Series | dict[AgentSetDF, Series]: ... + ) -> Series | dict[AbstractAgentSet, Series]: ... @overload def __getitem__( self, key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], - ) -> DataFrame | dict[AgentSetDF, DataFrame]: ... + ) -> DataFrame | dict[AbstractAgentSet, DataFrame]: ... def __getitem__( self, @@ -447,27 +447,32 @@ def __getitem__( | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] - | tuple[dict[AgentSetDF, AgentMask], str] - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] + | tuple[dict[AbstractAgentSet, AgentMask], str] + | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] ), - ) -> Series | DataFrame | dict[AgentSetDF, Series] | dict[AgentSetDF, DataFrame]: - """Implement the [] operator for the AgentContainer. + ) -> ( + Series + | DataFrame + | dict[AbstractAgentSet, Series] + | dict[AbstractAgentSet, DataFrame] + ): + """Implement the [] operator for the AbstractAgentSetRegistry. The key can be: - - An attribute or collection of attributes (eg. AgentContainer["str"], AgentContainer[["str1", "str2"]]): returns the specified column(s) of the agents in the AgentContainer. - - An AgentMask (eg. AgentContainer[AgentMask]): returns the agents in the AgentContainer that satisfy the AgentMask. - - A tuple (eg. AgentContainer[AgentMask, "str"]): returns the specified column of the agents in the AgentContainer that satisfy the AgentMask. - - A tuple with a dictionary (eg. AgentContainer[{AgentSetDF: AgentMask}, "str"]): returns the specified column of the agents in the AgentContainer that satisfy the AgentMask from the dictionary. - - A tuple with a dictionary (eg. AgentContainer[{AgentSetDF: AgentMask}, Collection[str]]): returns the specified columns of the agents in the AgentContainer that satisfy the AgentMask from the dictionary. + - An attribute or collection of attributes (eg. AbstractAgentSetRegistry["str"], AbstractAgentSetRegistry[["str1", "str2"]]): returns the specified column(s) of the agents in the AbstractAgentSetRegistry. + - An AgentMask (eg. AbstractAgentSetRegistry[AgentMask]): returns the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. + - A tuple (eg. AbstractAgentSetRegistry[AgentMask, "str"]): returns the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. + - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, "str"]): returns the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. + - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, Collection[str]]): returns the specified columns of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. Parameters ---------- - key : str | Collection[str] | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] | tuple[dict[AgentSetDF, AgentMask], str] | tuple[dict[AgentSetDF, AgentMask], Collection[str]] + key : str | Collection[str] | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] | tuple[dict[AbstractAgentSet, AgentMask], str] | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] The key to retrieve. Returns ------- - Series | DataFrame | dict[AgentSetDF, Series] | dict[AgentSetDF, DataFrame] + Series | DataFrame | dict[AbstractAgentSet, Series] | dict[AbstractAgentSet, DataFrame] The attribute values. """ # TODO: fix types @@ -486,21 +491,21 @@ def __iadd__( other: ( DataFrame | DataFrameInput - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF] + | mesa_frames.concrete.agents.AbstractAgentSet + | Collection[mesa_frames.concrete.agents.AbstractAgentSet] ), ) -> Self: - """Add agents to the AgentContainer through the += operator. + """Add agents to the AbstractAgentSetRegistry through the += operator. Parameters ---------- - other : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + other : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] The agents to add. Returns ------- Self - The updated AgentContainer. + The updated AbstractAgentSetRegistry. """ return self.add(agents=other, inplace=True) @@ -509,21 +514,21 @@ def __isub__( other: ( IdsLike | AgentMask - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF] + | mesa_frames.concrete.agents.AbstractAgentSet + | Collection[mesa_frames.concrete.agents.AbstractAgentSet] ), ) -> Self: - """Remove agents from the AgentContainer through the -= operator. + """Remove agents from the AbstractAgentSetRegistry through the -= operator. Parameters ---------- - other : IdsLike | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + other : IdsLike | AgentMask | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] The agents to remove. Returns ------- Self - The updated AgentContainer. + The updated AbstractAgentSetRegistry. """ return self.discard(other, inplace=True) @@ -532,21 +537,21 @@ def __sub__( other: ( IdsLike | AgentMask - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF] + | mesa_frames.concrete.agents.AbstractAgentSet + | Collection[mesa_frames.concrete.agents.AbstractAgentSet] ), ) -> Self: - """Remove agents from a new AgentContainer through the - operator. + """Remove agents from a new AbstractAgentSetRegistry through the - operator. Parameters ---------- - other : IdsLike | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] + other : IdsLike | AgentMask | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] The agents to remove. Returns ------- Self - A new AgentContainer with the removed agents. + A new AbstractAgentSetRegistry with the removed agents. """ return self.discard(other, inplace=False) @@ -557,24 +562,24 @@ def __setitem__( | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] - | tuple[dict[AgentSetDF, AgentMask], str] - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] + | tuple[dict[AbstractAgentSet, AgentMask], str] + | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] ), values: Any, ) -> None: - """Implement the [] operator for setting values in the AgentContainer. + """Implement the [] operator for setting values in the AbstractAgentSetRegistry. The key can be: - - A string (eg. AgentContainer["str"]): sets the specified column of the agents in the AgentContainer. - - A list of strings(eg. AgentContainer[["str1", "str2"]]): sets the specified columns of the agents in the AgentContainer. - - A tuple (eg. AgentContainer[AgentMask, "str"]): sets the specified column of the agents in the AgentContainer that satisfy the AgentMask. - - A AgentMask (eg. AgentContainer[AgentMask]): sets the attributes of the agents in the AgentContainer that satisfy the AgentMask. - - A tuple with a dictionary (eg. AgentContainer[{AgentSetDF: AgentMask}, "str"]): sets the specified column of the agents in the AgentContainer that satisfy the AgentMask from the dictionary. - - A tuple with a dictionary (eg. AgentContainer[{AgentSetDF: AgentMask}, Collection[str]]): sets the specified columns of the agents in the AgentContainer that satisfy the AgentMask from the dictionary. + - A string (eg. AbstractAgentSetRegistry["str"]): sets the specified column of the agents in the AbstractAgentSetRegistry. + - A list of strings(eg. AbstractAgentSetRegistry[["str1", "str2"]]): sets the specified columns of the agents in the AbstractAgentSetRegistry. + - A tuple (eg. AbstractAgentSetRegistry[AgentMask, "str"]): sets the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. + - A AgentMask (eg. AbstractAgentSetRegistry[AgentMask]): sets the attributes of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. + - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, "str"]): sets the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. + - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, Collection[str]]): sets the specified columns of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. Parameters ---------- - key : str | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] | tuple[dict[AgentSetDF, AgentMask], str] | tuple[dict[AgentSetDF, AgentMask], Collection[str]] + key : str | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] | tuple[dict[AbstractAgentSet, AgentMask], str] | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] The key to set. values : Any The values to set for the specified key. @@ -595,7 +600,7 @@ def __setitem__( @abstractmethod def __getattr__(self, name: str) -> Any | dict[str, Any]: - """Fallback for retrieving attributes of the AgentContainer. Retrieve an attribute of the underlying DataFrame(s). + """Fallback for retrieving attributes of the AbstractAgentSetRegistry. Retrieve an attribute of the underlying DataFrame(s). Parameters ---------- @@ -610,7 +615,7 @@ def __getattr__(self, name: str) -> Any | dict[str, Any]: @abstractmethod def __iter__(self) -> Iterator[dict[str, Any]]: - """Iterate over the agents in the AgentContainer. + """Iterate over the agents in the AbstractAgentSetRegistry. Returns ------- @@ -621,29 +626,29 @@ def __iter__(self) -> Iterator[dict[str, Any]]: @abstractmethod def __len__(self) -> int: - """Get the number of agents in the AgentContainer. + """Get the number of agents in the AbstractAgentSetRegistry. Returns ------- int - The number of agents in the AgentContainer. + The number of agents in the AbstractAgentSetRegistry. """ ... @abstractmethod def __repr__(self) -> str: - """Get a string representation of the DataFrame in the AgentContainer. + """Get a string representation of the DataFrame in the AbstractAgentSetRegistry. Returns ------- str - A string representation of the DataFrame in the AgentContainer. + A string representation of the DataFrame in the AbstractAgentSetRegistry. """ pass @abstractmethod def __reversed__(self) -> Iterator: - """Iterate over the agents in the AgentContainer in reverse order. + """Iterate over the agents in the AbstractAgentSetRegistry in reverse order. Returns ------- @@ -654,22 +659,22 @@ def __reversed__(self) -> Iterator: @abstractmethod def __str__(self) -> str: - """Get a string representation of the agents in the AgentContainer. + """Get a string representation of the agents in the AbstractAgentSetRegistry. Returns ------- str - A string representation of the agents in the AgentContainer. + A string representation of the agents in the AbstractAgentSetRegistry. """ ... @property - def model(self) -> mesa_frames.concrete.model.ModelDF: - """The model that the AgentContainer belongs to. + def model(self) -> mesa_frames.concrete.model.Model: + """The model that the AbstractAgentSetRegistry belongs to. Returns ------- - mesa_frames.concrete.model.ModelDF + mesa_frames.concrete.model.Model """ return self._model @@ -696,7 +701,7 @@ def space(self) -> mesa_frames.abstract.space.SpaceDF | None: @property @abstractmethod def df(self) -> DataFrame | dict[str, DataFrame]: - """The agents in the AgentContainer. + """The agents in the AbstractAgentSetRegistry. Returns ------- @@ -706,19 +711,19 @@ def df(self) -> DataFrame | dict[str, DataFrame]: @df.setter @abstractmethod def df( - self, agents: DataFrame | list[mesa_frames.concrete.agents.AgentSetDF] + self, agents: DataFrame | list[mesa_frames.concrete.agents.AbstractAgentSet] ) -> None: - """Set the agents in the AgentContainer. + """Set the agents in the AbstractAgentSetRegistry. Parameters ---------- - agents : DataFrame | list[mesa_frames.concrete.agents.AgentSetDF] + agents : DataFrame | list[mesa_frames.concrete.agents.AbstractAgentSet] """ @property @abstractmethod def active_agents(self) -> DataFrame | dict[str, DataFrame]: - """The active agents in the AgentContainer. + """The active agents in the AbstractAgentSetRegistry. Returns ------- @@ -731,7 +736,7 @@ def active_agents( self, mask: AgentMask, ) -> None: - """Set the active agents in the AgentContainer. + """Set the active agents in the AbstractAgentSetRegistry. Parameters ---------- @@ -744,24 +749,24 @@ def active_agents( @abstractmethod def inactive_agents( self, - ) -> DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame]: - """The inactive agents in the AgentContainer. + ) -> DataFrame | dict[mesa_frames.concrete.agents.AbstractAgentSet, DataFrame]: + """The inactive agents in the AbstractAgentSetRegistry. Returns ------- - DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame] + DataFrame | dict[mesa_frames.concrete.agents.AbstractAgentSet, DataFrame] """ @property @abstractmethod def index( self, - ) -> Index | dict[mesa_frames.concrete.agents.AgentSetDF, Index]: - """The ids in the AgentContainer. + ) -> Index | dict[mesa_frames.concrete.agents.AbstractAgentSet, Index]: + """The ids in the AbstractAgentSetRegistry. Returns ------- - Index | dict[mesa_frames.concrete.agents.AgentSetDF, Index] + Index | dict[mesa_frames.concrete.agents.AbstractAgentSet, Index] """ ... @@ -769,35 +774,33 @@ def index( @abstractmethod def pos( self, - ) -> DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame]: - """The position of the agents in the AgentContainer. + ) -> DataFrame | dict[mesa_frames.concrete.agents.AbstractAgentSet, DataFrame]: + """The position of the agents in the AbstractAgentSetRegistry. Returns ------- - DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame] + DataFrame | dict[mesa_frames.concrete.agents.AbstractAgentSet, DataFrame] """ ... -class AgentSetDF(AgentContainer, DataFrameMixin): - """The AgentSetDF class is a container for agents of the same type. +class AbstractAgentSet(AbstractAgentSetRegistry, DataFrameMixin): + """The AbstractAgentSet class is a container for agents of the same type. Parameters ---------- - model : mesa_frames.concrete.model.ModelDF + model : mesa_frames.concrete.model.Model The model that the agent set belongs to. """ - _df: DataFrame # The agents in the AgentSetDF - _mask: ( - AgentMask # The underlying mask used for the active agents in the AgentSetDF. - ) + _df: DataFrame # The agents in the AbstractAgentSet + _mask: AgentMask # The underlying mask used for the active agents in the AbstractAgentSet. _model: ( - mesa_frames.concrete.model.ModelDF - ) # The model that the AgentSetDF belongs to. + mesa_frames.concrete.model.Model + ) # The model that the AbstractAgentSet belongs to. @abstractmethod - def __init__(self, model: mesa_frames.concrete.model.ModelDF) -> None: ... + def __init__(self, model: mesa_frames.concrete.model.Model) -> None: ... @abstractmethod def add( @@ -805,7 +808,7 @@ def add( agents: DataFrame | DataFrameInput, inplace: bool = True, ) -> Self: - """Add agents to the AgentSetDF. + """Add agents to the AbstractAgentSet. Agents can be the input to the DataFrame constructor. So, the input can be: - A DataFrame: adds the agents from the DataFrame. @@ -821,12 +824,12 @@ def add( Returns ------- Self - A new AgentContainer with the added agents. + A new AbstractAgentSetRegistry with the added agents. """ ... def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: - """Remove an agent from the AgentSetDF. Does not raise an error if the agent is not found. + """Remove an agent from the AbstractAgentSet. Does not raise an error if the agent is not found. Parameters ---------- @@ -838,7 +841,7 @@ def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: Returns ------- Self - The updated AgentSetDF. + The updated AbstractAgentSet. """ return super().discard(agents, inplace) @@ -879,7 +882,7 @@ def do( obj = self._get_obj(inplace) method = getattr(obj, method_name) result = method(*args, **kwargs) - else: # If the mask is not empty, we need to create a new masked AgentSetDF and concatenate the AgentSetDFs at the end + else: # If the mask is not empty, we need to create a new masked AbstractAgentSet and concatenate the AbstractAgentSets at the end obj = self._get_obj(inplace=False) obj._df = masked_df original_masked_index = obj._get_obj_copy(obj.index) @@ -925,7 +928,7 @@ def get( @abstractmethod def step(self) -> None: - """Run a single step of the AgentSetDF. This method should be overridden by subclasses.""" + """Run a single step of the AbstractAgentSet. This method should be overridden by subclasses.""" ... def remove(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: @@ -934,10 +937,10 @@ def remove(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): return self._get_obj(inplace) agents = self._df_index(self._get_masked_df(agents), "unique_id") - agentsdf = self.model.agents.remove(agents, inplace=inplace) - # TODO: Refactor AgentsDF to return dict[str, AgentSetDF] instead of dict[AgentSetDF, DataFrame] - # And assign a name to AgentSetDF? This has to be replaced by a nicer API of AgentsDF - for agentset in agentsdf.df.keys(): + sets = self.model.sets.remove(agents, inplace=inplace) + # TODO: Refactor AgentSetRegistry to return dict[str, AbstractAgentSet] instead of dict[AbstractAgentSet, DataFrame] + # And assign a name to AbstractAgentSet? This has to be replaced by a nicer API of AgentSetRegistry + for agentset in sets.df.keys(): if isinstance(agentset, self.__class__): return agentset return self @@ -997,7 +1000,7 @@ def _get_obj_copy( @abstractmethod def _discard(self, ids: IdsLike) -> Self: - """Remove an agent from the DataFrame of the AgentSetDF. Gets called by self.model.agents.remove and self.model.agents.discard. + """Remove an agent from the DataFrame of the AbstractAgentSet. Gets called by self.model.sets.remove and self.model.sets.discard. Parameters ---------- @@ -1017,7 +1020,7 @@ def _update_mask( ) -> None: ... def __add__(self, other: DataFrame | DataFrameInput) -> Self: - """Add agents to a new AgentSetDF through the + operator. + """Add agents to a new AbstractAgentSet through the + operator. Other can be: - A DataFrame: adds the agents from the DataFrame. @@ -1031,13 +1034,13 @@ def __add__(self, other: DataFrame | DataFrameInput) -> Self: Returns ------- Self - A new AgentContainer with the added agents. + A new AbstractAgentSetRegistry with the added agents. """ return super().__add__(other) def __iadd__(self, other: DataFrame | DataFrameInput) -> Self: """ - Add agents to the AgentSetDF through the += operator. + Add agents to the AbstractAgentSet through the += operator. Other can be: - A DataFrame: adds the agents from the DataFrame. @@ -1051,7 +1054,7 @@ def __iadd__(self, other: DataFrame | DataFrameInput) -> Self: Returns ------- Self - The updated AgentContainer. + The updated AbstractAgentSetRegistry. """ return super().__iadd__(other) @@ -1104,7 +1107,7 @@ def df(self) -> DataFrame: @df.setter def df(self, agents: DataFrame) -> None: - """Set the agents in the AgentSetDF. + """Set the agents in the AbstractAgentSet. Parameters ---------- diff --git a/mesa_frames/abstract/datacollector.py b/mesa_frames/abstract/datacollector.py index d93f661d..edbfb11f 100644 --- a/mesa_frames/abstract/datacollector.py +++ b/mesa_frames/abstract/datacollector.py @@ -47,7 +47,7 @@ def flush(self): from abc import ABC, abstractmethod from typing import Any, Literal from collections.abc import Callable -from mesa_frames import ModelDF +from mesa_frames import Model import polars as pl import threading from concurrent.futures import ThreadPoolExecutor @@ -61,7 +61,7 @@ class AbstractDataCollector(ABC): Sub classes must implement logic for the methods """ - _model: ModelDF + _model: Model _model_reporters: dict[str, Callable] | None _agent_reporters: dict[str, str | Callable] | None _trigger: Callable[..., bool] | None @@ -71,7 +71,7 @@ class AbstractDataCollector(ABC): def __init__( self, - model: ModelDF, + model: Model, model_reporters: dict[str, Callable] | None, agent_reporters: dict[str, str | Callable] | None, trigger: Callable[[Any], bool] | None, @@ -86,7 +86,7 @@ def __init__( Parameters ---------- - model : ModelDF + model : Model The model object from which data is collected. model_reporters : dict[str, Callable] | None Functions to collect data at the model level. diff --git a/mesa_frames/abstract/mixin.py b/mesa_frames/abstract/mixin.py index 84b4ec7b..96904eba 100644 --- a/mesa_frames/abstract/mixin.py +++ b/mesa_frames/abstract/mixin.py @@ -81,8 +81,8 @@ def copy( Parameters ---------- deep : bool, optional - Flag indicating whether to perform a deep copy of the AgentContainer. - If True, all attributes of the AgentContainer will be recursively copied (except attributes in self._copy_reference_only). + Flag indicating whether to perform a deep copy of the AbstractAgentSetRegistry. + If True, all attributes of the AbstractAgentSetRegistry will be recursively copied (except attributes in self._copy_reference_only). If False, only the top-level attributes will be copied. Defaults to False. memo : dict | None, optional @@ -95,7 +95,7 @@ def copy( Returns ------- Self - A new instance of the AgentContainer class that is a copy of the original instance. + A new instance of the AbstractAgentSetRegistry class that is a copy of the original instance. """ cls = self.__class__ obj = cls.__new__(cls) @@ -155,17 +155,17 @@ def _get_obj(self, inplace: bool) -> Self: return deepcopy(self) def __copy__(self) -> Self: - """Create a shallow copy of the AgentContainer. + """Create a shallow copy of the AbstractAgentSetRegistry. Returns ------- Self - A shallow copy of the AgentContainer. + A shallow copy of the AbstractAgentSetRegistry. """ return self.copy(deep=False) def __deepcopy__(self, memo: dict) -> Self: - """Create a deep copy of the AgentContainer. + """Create a deep copy of the AbstractAgentSetRegistry. Parameters ---------- @@ -175,7 +175,7 @@ def __deepcopy__(self, memo: dict) -> Self: Returns ------- Self - A deep copy of the AgentContainer. + A deep copy of the AbstractAgentSetRegistry. """ return self.copy(deep=True, memo=memo) diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index dab5f7b0..ab9f6878 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -59,9 +59,9 @@ def __init__(self, model, dimensions, torus, capacity, neighborhood_type): import polars as pl from numpy.random import Generator -from mesa_frames.abstract.agents import AgentContainer, AgentSetDF +from mesa_frames.abstract.agents import AbstractAgentSetRegistry, AbstractAgentSet from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin -from mesa_frames.concrete.agents import AgentsDF +from mesa_frames.concrete.agents import AgentSetRegistry from mesa_frames.types_ import ( ArrayLike, BoolSeries, @@ -94,18 +94,20 @@ class SpaceDF(CopyMixin, DataFrameMixin): str ] # The column names of the positions in the _agents dataframe (eg. ['dim_0', 'dim_1', ...] in Grids, ['node_id', 'edge_id'] in Networks) - def __init__(self, model: mesa_frames.concrete.model.ModelDF) -> None: + def __init__(self, model: mesa_frames.concrete.model.Model) -> None: """Create a new SpaceDF. Parameters ---------- - model : mesa_frames.concrete.model.ModelDF + model : mesa_frames.concrete.model.Model """ self._model = model def move_agents( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], pos: SpaceCoordinate | SpaceCoordinates, inplace: bool = True, ) -> Self: @@ -115,7 +117,7 @@ def move_agents( Parameters ---------- - agents : IdsLike | AgentContainer | Collection[AgentContainer] + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] The agents to move pos : SpaceCoordinate | SpaceCoordinates The coordinates for each agents. The length of the coordinates must match the number of agents. @@ -139,7 +141,9 @@ def move_agents( def place_agents( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], pos: SpaceCoordinate | SpaceCoordinates, inplace: bool = True, ) -> Self: @@ -147,7 +151,7 @@ def place_agents( Parameters ---------- - agents : IdsLike | AgentContainer | Collection[AgentContainer] + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] The agents to place in the space pos : SpaceCoordinate | SpaceCoordinates The coordinates for each agents. The length of the coordinates must match the number of agents. @@ -190,8 +194,12 @@ def random_agents( def swap_agents( self, - agents0: IdsLike | AgentContainer | Collection[AgentContainer], - agents1: IdsLike | AgentContainer | Collection[AgentContainer], + agents0: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], + agents1: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: """Swap the positions of the agents in the space. @@ -200,9 +208,9 @@ def swap_agents( Parameters ---------- - agents0 : IdsLike | AgentContainer | Collection[AgentContainer] + agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] The first set of agents to swap - agents1 : IdsLike | AgentContainer | Collection[AgentContainer] + agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] The second set of agents to swap inplace : bool, optional Whether to perform the operation inplace, by default True @@ -245,8 +253,14 @@ def get_directions( self, pos0: SpaceCoordinate | SpaceCoordinates | None = None, pos1: SpaceCoordinate | SpaceCoordinates | None = None, - agents0: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, - agents1: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, + agents0: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, + agents1: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, normalize: bool = False, ) -> DataFrame: """Return the directions from pos0 to pos1 or agents0 and agents1. @@ -261,9 +275,9 @@ def get_directions( The starting positions pos1 : SpaceCoordinate | SpaceCoordinates | None, optional The ending positions - agents0 : IdsLike | AgentContainer | Collection[AgentContainer] | None, optional + agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional The starting agents - agents1 : IdsLike | AgentContainer | Collection[AgentContainer] | None, optional + agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional The ending agents normalize : bool, optional Whether to normalize the vectors to unit norm. By default False @@ -280,8 +294,14 @@ def get_distances( self, pos0: SpaceCoordinate | SpaceCoordinates | None = None, pos1: SpaceCoordinate | SpaceCoordinates | None = None, - agents0: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, - agents1: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, + agents0: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, + agents1: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, ) -> DataFrame: """Return the distances from pos0 to pos1 or agents0 and agents1. @@ -295,9 +315,9 @@ def get_distances( The starting positions pos1 : SpaceCoordinate | SpaceCoordinates | None, optional The ending positions - agents0 : IdsLike | AgentContainer | Collection[AgentContainer] | None, optional + agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional The starting agents - agents1 : IdsLike | AgentContainer | Collection[AgentContainer] | None, optional + agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional The ending agents Returns @@ -312,7 +332,10 @@ def get_neighbors( self, radius: int | float | Sequence[int] | Sequence[float] | ArrayLike, pos: SpaceCoordinate | SpaceCoordinates | None = None, - agents: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, include_center: bool = False, ) -> DataFrame: """Get the neighboring agents from given positions or agents according to the specified radiuses. @@ -325,7 +348,7 @@ def get_neighbors( The radius(es) of the neighborhood pos : SpaceCoordinate | SpaceCoordinates | None, optional The coordinates of the cell to get the neighborhood from, by default None - agents : IdsLike | AgentContainer | Collection[AgentContainer] | None, optional + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional The id of the agents to get the neighborhood from, by default None include_center : bool, optional If the center cells or agents should be included in the result, by default False @@ -346,14 +369,16 @@ def get_neighbors( @abstractmethod def move_to_empty( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: """Move agents to empty cells/positions in the space (cells/positions where there isn't any single agent). Parameters ---------- - agents : IdsLike | AgentContainer | Collection[AgentContainer] + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] The agents to move to empty cells/positions inplace : bool, optional Whether to perform the operation inplace, by default True @@ -367,14 +392,16 @@ def move_to_empty( @abstractmethod def place_to_empty( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: """Place agents in empty cells/positions in the space (cells/positions where there isn't any single agent). Parameters ---------- - agents : IdsLike | AgentContainer | Collection[AgentContainer] + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] The agents to place in empty cells/positions inplace : bool, optional Whether to perform the operation inplace, by default True @@ -407,7 +434,9 @@ def random_pos( @abstractmethod def remove_agents( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: """Remove agents from the space. @@ -416,7 +445,7 @@ def remove_agents( Parameters ---------- - agents : IdsLike | AgentContainer | Collection[AgentContainer] + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] The agents to remove from the space inplace : bool, optional Whether to perform the operation inplace, by default True @@ -433,22 +462,27 @@ def remove_agents( return ... def _get_ids_srs( - self, agents: IdsLike | AgentContainer | Collection[AgentContainer] + self, + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], ) -> Series: if isinstance(agents, Sized) and len(agents) == 0: return self._srs_constructor([], name="agent_id", dtype="uint64") - if isinstance(agents, AgentSetDF): + if isinstance(agents, AbstractAgentSet): return self._srs_constructor( self._df_index(agents.df, "unique_id"), name="agent_id", dtype="uint64", ) - elif isinstance(agents, AgentsDF): + elif isinstance(agents, AgentSetRegistry): return self._srs_constructor(agents._ids, name="agent_id", dtype="uint64") - elif isinstance(agents, Collection) and (isinstance(agents[0], AgentContainer)): + elif isinstance(agents, Collection) and ( + isinstance(agents[0], AbstractAgentSetRegistry) + ): ids = [] for a in agents: - if isinstance(a, AgentSetDF): + if isinstance(a, AbstractAgentSet): ids.append( self._srs_constructor( self._df_index(a.df, "unique_id"), @@ -456,7 +490,7 @@ def _get_ids_srs( dtype="uint64", ) ) - elif isinstance(a, AgentsDF): + elif isinstance(a, AgentSetRegistry): ids.append( self._srs_constructor(a._ids, name="agent_id", dtype="uint64") ) @@ -469,7 +503,9 @@ def _get_ids_srs( @abstractmethod def _place_or_move_agents( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], pos: SpaceCoordinate | SpaceCoordinates, is_move: bool, ) -> Self: @@ -479,7 +515,7 @@ def _place_or_move_agents( Parameters ---------- - agents : IdsLike | AgentContainer | Collection[AgentContainer] + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] The agents to move/place pos : SpaceCoordinate | SpaceCoordinates The position to move/place agents to @@ -522,12 +558,12 @@ def agents(self) -> DataFrame: # | GeoDataFrame: return self._agents @property - def model(self) -> mesa_frames.concrete.model.ModelDF: + def model(self) -> mesa_frames.concrete.model.Model: """The model to which the space belongs. Returns ------- - 'mesa_frames.concrete.model.ModelDF' + 'mesa_frames.concrete.model.Model' """ return self._model @@ -554,14 +590,14 @@ class DiscreteSpaceDF(SpaceDF): def __init__( self, - model: mesa_frames.concrete.model.ModelDF, + model: mesa_frames.concrete.model.Model, capacity: int | None = None, ): """Create a new DiscreteSpaceDF. Parameters ---------- - model : mesa_frames.concrete.model.ModelDF + model : mesa_frames.concrete.model.Model The model to which the space belongs capacity : int | None, optional The maximum capacity for cells (default is infinite), by default None @@ -616,7 +652,9 @@ def is_full(self, pos: DiscreteCoordinate | DiscreteCoordinates) -> DataFrame: def move_to_empty( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) @@ -626,14 +664,16 @@ def move_to_empty( def move_to_available( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: """Move agents to available cells/positions in the space (cells/positions where there is at least one spot available). Parameters ---------- - agents : IdsLike | AgentContainer | Collection[AgentContainer] + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] The agents to move to available cells/positions inplace : bool, optional Whether to perform the operation inplace, by default True @@ -649,7 +689,9 @@ def move_to_available( def place_to_empty( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) @@ -659,7 +701,9 @@ def place_to_empty( def place_to_available( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) @@ -775,7 +819,9 @@ def get_neighborhood( self, radius: int | float | Sequence[int] | Sequence[float] | ArrayLike, pos: DiscreteCoordinate | DiscreteCoordinates | None = None, - agents: IdsLike | AgentContainer | Collection[AgentContainer] = None, + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] = None, include_center: bool = False, ) -> DataFrame: """Get the neighborhood cells from the given positions (pos) or agents according to the specified radiuses. @@ -788,7 +834,7 @@ def get_neighborhood( The radius(es) of the neighborhoods pos : DiscreteCoordinate | DiscreteCoordinates | None, optional The coordinates of the cell(s) to get the neighborhood from - agents : IdsLike | AgentContainer | Collection[AgentContainer], optional + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry], optional The agent(s) to get the neighborhood from include_center : bool, optional If the cell in the center of the neighborhood should be included in the result, by default False @@ -883,7 +929,9 @@ def _check_cells( def _place_or_move_agents_to_cells( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], cell_type: Literal["any", "empty", "available"], is_move: bool, ) -> Self: @@ -892,7 +940,7 @@ def _place_or_move_agents_to_cells( if __debug__: # Check ids presence in model - b_contained = self.model.agents.contains(agents) + b_contained = self.model.sets.contains(agents) if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): @@ -912,7 +960,10 @@ def _place_or_move_agents_to_cells( def _get_df_coords( self, pos: DiscreteCoordinate | DiscreteCoordinates | None = None, - agents: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, ) -> DataFrame: """Get the DataFrame of coordinates from the specified positions or agents. @@ -920,7 +971,7 @@ def _get_df_coords( ---------- pos : DiscreteCoordinate | DiscreteCoordinates | None, optional The positions to get the DataFrame from, by default None - agents : IdsLike | AgentContainer | Collection[AgentContainer] | None, optional + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional The agents to get the DataFrame from, by default None Returns @@ -1155,7 +1206,7 @@ class GridDF(DiscreteSpaceDF): def __init__( self, - model: mesa_frames.concrete.model.ModelDF, + model: mesa_frames.concrete.model.Model, dimensions: Sequence[int], torus: bool = False, capacity: int | None = None, @@ -1165,7 +1216,7 @@ def __init__( Parameters ---------- - model : mesa_frames.concrete.model.ModelDF + model : mesa_frames.concrete.model.Model The model to which the space belongs dimensions : Sequence[int] The dimensions of the grid @@ -1204,8 +1255,14 @@ def get_directions( self, pos0: GridCoordinate | GridCoordinates | None = None, pos1: GridCoordinate | GridCoordinates | None = None, - agents0: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, - agents1: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, + agents0: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, + agents1: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, normalize: bool = False, ) -> DataFrame: result = self._calculate_differences(pos0, pos1, agents0, agents1) @@ -1217,8 +1274,14 @@ def get_distances( self, pos0: GridCoordinate | GridCoordinates | None = None, pos1: GridCoordinate | GridCoordinates | None = None, - agents0: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, - agents1: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, + agents0: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, + agents1: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, ) -> DataFrame: result = self._calculate_differences(pos0, pos1, agents0, agents1) return self._df_norm(result, "distance", True) @@ -1227,7 +1290,10 @@ def get_neighbors( self, radius: int | Sequence[int], pos: GridCoordinate | GridCoordinates | None = None, - agents: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, include_center: bool = False, ) -> DataFrame: neighborhood_df = self.get_neighborhood( @@ -1243,7 +1309,10 @@ def get_neighborhood( self, radius: int | Sequence[int] | ArrayLike, pos: GridCoordinate | GridCoordinates | None = None, - agents: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, include_center: bool = False, ) -> DataFrame: pos_df = self._get_df_coords(pos, agents) @@ -1476,7 +1545,9 @@ def out_of_bounds(self, pos: GridCoordinate | GridCoordinates) -> DataFrame: def remove_agents( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) @@ -1485,7 +1556,7 @@ def remove_agents( if __debug__: # Check ids presence in model - b_contained = obj.model.agents.contains(agents) + b_contained = obj.model.sets.contains(agents) if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): @@ -1519,8 +1590,14 @@ def _calculate_differences( self, pos0: GridCoordinate | GridCoordinates | None, pos1: GridCoordinate | GridCoordinates | None, - agents0: IdsLike | AgentContainer | Collection[AgentContainer] | None, - agents1: IdsLike | AgentContainer | Collection[AgentContainer] | None, + agents0: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None, + agents1: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None, ) -> DataFrame: """Calculate the differences between two positions or agents. @@ -1530,9 +1607,9 @@ def _calculate_differences( The starting positions pos1 : GridCoordinate | GridCoordinates | None The ending positions - agents0 : IdsLike | AgentContainer | Collection[AgentContainer] | None + agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None The starting agents - agents1 : IdsLike | AgentContainer | Collection[AgentContainer] | None + agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None The ending agents Returns @@ -1613,7 +1690,10 @@ def _compute_offsets(self, neighborhood_type: str) -> DataFrame: def _get_df_coords( self, pos: GridCoordinate | GridCoordinates | None = None, - agents: IdsLike | AgentContainer | Collection[AgentContainer] | None = None, + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry] + | None = None, check_bounds: bool = True, ) -> DataFrame: """Get the DataFrame of coordinates from the specified positions or agents. @@ -1622,7 +1702,7 @@ def _get_df_coords( ---------- pos : GridCoordinate | GridCoordinates | None, optional The positions to get the DataFrame from, by default None - agents : IdsLike | AgentContainer | Collection[AgentContainer] | None, optional + agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional The agents to get the DataFrame from, by default None check_bounds: bool, optional If the positions should be checked for out-of-bounds in non-toroidal grids, by default True @@ -1652,7 +1732,7 @@ def _get_df_coords( if agents is not None: agents = self._get_ids_srs(agents) # Check ids presence in model - b_contained = self.model.agents.contains(agents) + b_contained = self.model.sets.contains(agents) if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): @@ -1712,7 +1792,9 @@ def _get_df_coords( def _place_or_move_agents( self, - agents: IdsLike | AgentContainer | Collection[AgentContainer], + agents: IdsLike + | AbstractAgentSetRegistry + | Collection[AbstractAgentSetRegistry], pos: GridCoordinate | GridCoordinates, is_move: bool, ) -> Self: @@ -1728,7 +1810,7 @@ def _place_or_move_agents( warn("Some agents are already present in the grid", RuntimeWarning) # Check if agents are present in the model - b_contained = self.model.agents.contains(agents) + b_contained = self.model.sets.contains(agents) if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): diff --git a/mesa_frames/concrete/__init__.py b/mesa_frames/concrete/__init__.py index ebccc9e8..550d6dc2 100644 --- a/mesa_frames/concrete/__init__.py +++ b/mesa_frames/concrete/__init__.py @@ -13,15 +13,15 @@ polars: Contains Polars-based implementations of agent sets, mixins, and spatial structures. Modules: - agents: Defines the AgentsDF class, a collection of AgentSetDFs. - model: Provides the ModelDF class, the base class for models in mesa-frames. - agentset: Defines the AgentSetPolars class, a Polars-based implementation of AgentSet. + agents: Defines the AgentSetRegistry class, a collection of AgentSets. + model: Provides the Model class, the base class for models in mesa-frames. + agentset: Defines the AgentSet class, a Polars-based implementation of AgentSet. mixin: Provides the PolarsMixin class, implementing DataFrame operations using Polars. space: Contains the GridPolars class, a Polars-based implementation of Grid. Classes: from agentset: - AgentSetPolars(AgentSetDF, PolarsMixin): + AgentSet(AbstractAgentSet, PolarsMixin): A Polars-based implementation of the AgentSet, using Polars DataFrames for efficient agent storage and manipulation. @@ -35,37 +35,37 @@ efficient spatial operations and agent positioning. From agents: - AgentsDF(AgentContainer): A collection of AgentSetDFs. All agents of the model are stored here. + AgentSetRegistry(AbstractAgentSetRegistry): A collection of AbstractAgentSets. All agents of the model are stored here. From model: - ModelDF: Base class for models in the mesa-frames library. + Model: Base class for models in the mesa-frames library. Usage: Users can import the concrete implementations directly from this package: - from mesa_frames.concrete import ModelDF, AgentsDF + from mesa_frames.concrete import Model, AgentSetRegistry # For Polars-based implementations - from mesa_frames.concrete import AgentSetPolars, GridPolars - from mesa_frames.concrete.model import ModelDF + from mesa_frames.concrete import AgentSet, GridPolars + from mesa_frames.concrete.model import Model - class MyModel(ModelDF): + class MyModel(Model): def __init__(self): super().__init__() - self.agents.add(AgentSetPolars(self)) + self.sets.add(AgentSet(self)) self.space = GridPolars(self, dimensions=[10, 10]) # ... other initialization code - from mesa_frames.concrete import AgentSetPolars, GridPolars + from mesa_frames.concrete import AgentSet, GridPolars - class MyAgents(AgentSetPolars): + class MyAgents(AgentSet): def __init__(self, model): super().__init__(model) # Initialize agents - class MyModel(ModelDF): + class MyModel(Model): def __init__(self, width, height): super().__init__() - self.agents = MyAgents(self) + self.sets = MyAgents(self) self.grid = GridPolars(width, height, self) Features: - High-performance DataFrame operations using Polars diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index 799a7b33..ad0e3ff9 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -2,45 +2,45 @@ Concrete implementation of the agents collection for mesa-frames. This module provides the concrete implementation of the agents collection class -for the mesa-frames library. It defines the AgentsDF class, which serves as a +for the mesa-frames library. It defines the AgentSetRegistry class, which serves as a container for all agent sets in a model, leveraging DataFrame-based storage for improved performance. Classes: - AgentsDF(AgentContainer): - A collection of AgentSetDFs. This class acts as a container for all - agents in the model, organizing them into separate AgentSetDF instances + AgentSetRegistry(AbstractAgentSetRegistry): + A collection of AbstractAgentSets. This class acts as a container for all + agents in the model, organizing them into separate AbstractAgentSet instances based on their types. -The AgentsDF class is designed to be used within ModelDF instances to manage +The AgentSetRegistry class is designed to be used within Model instances to manage all agents in the simulation. It provides methods for adding, removing, and accessing agents and agent sets, while taking advantage of the performance benefits of DataFrame-based agent storage. Usage: - The AgentsDF class is typically instantiated and used within a ModelDF subclass: + The AgentSetRegistry class is typically instantiated and used within a Model subclass: - from mesa_frames.concrete.model import ModelDF - from mesa_frames.concrete.agents import AgentsDF - from mesa_frames.concrete import AgentSetPolars + from mesa_frames.concrete.model import Model + from mesa_frames.concrete.agents import AgentSetRegistry + from mesa_frames.concrete import AgentSet - class MyCustomModel(ModelDF): + class MyCustomModel(Model): def __init__(self): super().__init__() # Adding agent sets to the collection - self.agents += AgentSetPolars(self) - self.agents += AnotherAgentSetPolars(self) + self.sets += AgentSet(self) + self.sets += AnotherAgentSet(self) def step(self): # Step all agent sets - self.agents.do("step") + self.sets.do("step") Note: - This concrete implementation builds upon the abstract AgentContainer class + This concrete implementation builds upon the abstract AbstractAgentSetRegistry class defined in the mesa_frames.abstract package, providing a ready-to-use agents collection that integrates with the DataFrame-based agent storage system. -For more detailed information on the AgentsDF class and its methods, refer to +For more detailed information on the AgentSetRegistry class and its methods, refer to the class docstring. """ @@ -53,7 +53,7 @@ def step(self): import numpy as np import polars as pl -from mesa_frames.abstract.agents import AgentContainer, AgentSetDF +from mesa_frames.abstract.agents import AbstractAgentSetRegistry, AbstractAgentSet from mesa_frames.types_ import ( AgentMask, AgnosticAgentMask, @@ -65,50 +65,54 @@ def step(self): ) -class AgentsDF(AgentContainer): - """A collection of AgentSetDFs. All agents of the model are stored here.""" +class AgentSetRegistry(AbstractAgentSetRegistry): + """A collection of AbstractAgentSets. All agents of the model are stored here.""" - _agentsets: list[AgentSetDF] + _agentsets: list[AbstractAgentSet] _ids: pl.Series - def __init__(self, model: mesa_frames.concrete.model.ModelDF) -> None: - """Initialize a new AgentsDF. + def __init__(self, model: mesa_frames.concrete.model.Model) -> None: + """Initialize a new AgentSetRegistry. Parameters ---------- - model : mesa_frames.concrete.model.ModelDF - The model associated with the AgentsDF. + model : mesa_frames.concrete.model.Model + The model associated with the AgentSetRegistry. """ self._model = model self._agentsets = [] self._ids = pl.Series(name="unique_id", dtype=pl.UInt64) def add( - self, agents: AgentSetDF | Iterable[AgentSetDF], inplace: bool = True + self, + agents: AbstractAgentSet | Iterable[AbstractAgentSet], + inplace: bool = True, ) -> Self: - """Add an AgentSetDF to the AgentsDF. + """Add an AbstractAgentSet to the AgentSetRegistry. Parameters ---------- - agents : AgentSetDF | Iterable[AgentSetDF] - The AgentSetDFs to add. + agents : AbstractAgentSet | Iterable[AbstractAgentSet] + The AbstractAgentSets to add. inplace : bool, optional - Whether to add the AgentSetDFs in place. Defaults to True. + Whether to add the AbstractAgentSets in place. Defaults to True. Returns ------- Self - The updated AgentsDF. + The updated AgentSetRegistry. Raises ------ ValueError - If any AgentSetDFs are already present or if IDs are not unique. + If any AbstractAgentSets are already present or if IDs are not unique. """ obj = self._get_obj(inplace) other_list = obj._return_agentsets_list(agents) if obj._check_agentsets_presence(other_list).any(): - raise ValueError("Some agentsets are already present in the AgentsDF.") + raise ValueError( + "Some agentsets are already present in the AgentSetRegistry." + ) new_ids = pl.concat( [obj._ids] + [pl.Series(agentset["unique_id"]) for agentset in other_list] ) @@ -119,23 +123,23 @@ def add( return obj @overload - def contains(self, agents: int | AgentSetDF) -> bool: ... + def contains(self, agents: int | AbstractAgentSet) -> bool: ... @overload - def contains(self, agents: IdsLike | Iterable[AgentSetDF]) -> pl.Series: ... + def contains(self, agents: IdsLike | Iterable[AbstractAgentSet]) -> pl.Series: ... def contains( - self, agents: IdsLike | AgentSetDF | Iterable[AgentSetDF] + self, agents: IdsLike | AbstractAgentSet | Iterable[AbstractAgentSet] ) -> bool | pl.Series: if isinstance(agents, int): return agents in self._ids - elif isinstance(agents, AgentSetDF): + elif isinstance(agents, AbstractAgentSet): return self._check_agentsets_presence([agents]).any() elif isinstance(agents, Iterable): if len(agents) == 0: return True - elif isinstance(next(iter(agents)), AgentSetDF): - agents = cast(Iterable[AgentSetDF], agents) + elif isinstance(next(iter(agents)), AbstractAgentSet): + agents = cast(Iterable[AbstractAgentSet], agents) return self._check_agentsets_presence(list(agents)) else: # IdsLike agents = cast(IdsLike, agents) @@ -147,7 +151,7 @@ def do( self, method_name: str, *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, return_results: Literal[False] = False, inplace: bool = True, **kwargs, @@ -158,17 +162,17 @@ def do( self, method_name: str, *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, return_results: Literal[True], inplace: bool = True, **kwargs, - ) -> dict[AgentSetDF, Any]: ... + ) -> dict[AbstractAgentSet, Any]: ... def do( self, method_name: str, *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, return_results: bool = False, inplace: bool = True, **kwargs, @@ -204,8 +208,8 @@ def do( def get( self, attr_names: str | Collection[str] | None = None, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, - ) -> dict[AgentSetDF, Series] | dict[AgentSetDF, DataFrame]: + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + ) -> dict[AbstractAgentSet, Series] | dict[AbstractAgentSet, DataFrame]: agentsets_masks = self._get_bool_masks(mask) result = {} @@ -232,16 +236,18 @@ def get( def remove( self, - agents: AgentSetDF | Iterable[AgentSetDF] | IdsLike, + agents: AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike, inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): return obj - if isinstance(agents, AgentSetDF): + if isinstance(agents, AbstractAgentSet): agents = [agents] - if isinstance(agents, Iterable) and isinstance(next(iter(agents)), AgentSetDF): - # We have to get the index of the original AgentSetDF because the copy made AgentSetDFs with different hash + if isinstance(agents, Iterable) and isinstance( + next(iter(agents)), AbstractAgentSet + ): + # We have to get the index of the original AbstractAgentSet because the copy made AbstractAgentSets with different hash ids = [self._agentsets.index(agentset) for agentset in iter(agents)] ids.sort(reverse=True) removed_ids = pl.Series(dtype=pl.UInt64) @@ -281,8 +287,8 @@ def remove( def select( self, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, - filter_func: Callable[[AgentSetDF], AgentMask] | None = None, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + filter_func: Callable[[AbstractAgentSet], AgentMask] | None = None, n: int | None = None, inplace: bool = True, negate: bool = False, @@ -301,9 +307,9 @@ def select( def set( self, - attr_names: str | dict[AgentSetDF, Any] | Collection[str], + attr_names: str | dict[AbstractAgentSet, Any] | Collection[str], values: Any | None = None, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) @@ -311,7 +317,7 @@ def set( if isinstance(attr_names, dict): for agentset, values in attr_names.items(): if not inplace: - # We have to get the index of the original AgentSetDF because the copy made AgentSetDFs with different hash + # We have to get the index of the original AbstractAgentSet because the copy made AbstractAgentSets with different hash id = self._agentsets.index(agentset) agentset = obj._agentsets[id] agentset.set( @@ -346,12 +352,12 @@ def sort( return obj def step(self, inplace: bool = True) -> Self: - """Advance the state of the agents in the AgentsDF by one step. + """Advance the state of the agents in the AgentSetRegistry by one step. Parameters ---------- inplace : bool, optional - Whether to update the AgentsDF in place, by default True + Whether to update the AgentSetRegistry in place, by default True Returns ------- @@ -362,13 +368,13 @@ def step(self, inplace: bool = True) -> Self: agentset.step() return obj - def _check_ids_presence(self, other: list[AgentSetDF]) -> pl.DataFrame: + def _check_ids_presence(self, other: list[AbstractAgentSet]) -> pl.DataFrame: """Check if the IDs of the agents to be added are unique. Parameters ---------- - other : list[AgentSetDF] - The AgentSetDFs to check. + other : list[AbstractAgentSet] + The AbstractAgentSets to check. Returns ------- @@ -395,13 +401,13 @@ def _check_ids_presence(self, other: list[AgentSetDF]) -> pl.DataFrame: presence_df = presence_df.slice(self._ids.len()) return presence_df - def _check_agentsets_presence(self, other: list[AgentSetDF]) -> pl.Series: - """Check if the agent sets to be added are already present in the AgentsDF. + def _check_agentsets_presence(self, other: list[AbstractAgentSet]) -> pl.Series: + """Check if the agent sets to be added are already present in the AgentSetRegistry. Parameters ---------- - other : list[AgentSetDF] - The AgentSetDFs to check. + other : list[AbstractAgentSet] + The AbstractAgentSets to check. Returns ------- @@ -411,7 +417,7 @@ def _check_agentsets_presence(self, other: list[AgentSetDF]) -> pl.Series: Raises ------ ValueError - If the agent sets are already present in the AgentsDF. + If the agent sets are already present in the AgentSetRegistry. """ other_set = set(other) return pl.Series( @@ -420,8 +426,8 @@ def _check_agentsets_presence(self, other: list[AgentSetDF]) -> pl.Series: def _get_bool_masks( self, - mask: (AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask]) = None, - ) -> dict[AgentSetDF, BoolSeries]: + mask: (AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask]) = None, + ) -> dict[AbstractAgentSet, BoolSeries]: return_dictionary = {} if not isinstance(mask, dict): # No need to convert numpy integers - let polars handle them directly @@ -431,36 +437,38 @@ def _get_bool_masks( return return_dictionary def _return_agentsets_list( - self, agentsets: AgentSetDF | Iterable[AgentSetDF] - ) -> list[AgentSetDF]: - """Convert the agentsets to a list of AgentSetDF. + self, agentsets: AbstractAgentSet | Iterable[AbstractAgentSet] + ) -> list[AbstractAgentSet]: + """Convert the agentsets to a list of AbstractAgentSet. Parameters ---------- - agentsets : AgentSetDF | Iterable[AgentSetDF] + agentsets : AbstractAgentSet | Iterable[AbstractAgentSet] Returns ------- - list[AgentSetDF] + list[AbstractAgentSet] """ - return [agentsets] if isinstance(agentsets, AgentSetDF) else list(agentsets) + return ( + [agentsets] if isinstance(agentsets, AbstractAgentSet) else list(agentsets) + ) - def __add__(self, other: AgentSetDF | Iterable[AgentSetDF]) -> Self: - """Add AgentSetDFs to a new AgentsDF through the + operator. + def __add__(self, other: AbstractAgentSet | Iterable[AbstractAgentSet]) -> Self: + """Add AbstractAgentSets to a new AgentSetRegistry through the + operator. Parameters ---------- - other : AgentSetDF | Iterable[AgentSetDF] - The AgentSetDFs to add. + other : AbstractAgentSet | Iterable[AbstractAgentSet] + The AbstractAgentSets to add. Returns ------- Self - A new AgentsDF with the added AgentSetDFs. + A new AgentSetRegistry with the added AbstractAgentSets. """ return super().__add__(other) - def __getattr__(self, name: str) -> dict[AgentSetDF, Any]: + def __getattr__(self, name: str) -> dict[AbstractAgentSet, Any]: # Avoids infinite recursion of private attributes if __debug__: # Only execute in non-optimized mode if name.startswith("_"): @@ -471,8 +479,8 @@ def __getattr__(self, name: str) -> dict[AgentSetDF, Any]: @overload def __getitem__( - self, key: str | tuple[dict[AgentSetDF, AgentMask], str] - ) -> dict[AgentSetDF, Series | pl.Expr]: ... + self, key: str | tuple[dict[AbstractAgentSet, AgentMask], str] + ) -> dict[AbstractAgentSet, Series | pl.Expr]: ... @overload def __getitem__( @@ -481,9 +489,9 @@ def __getitem__( Collection[str] | AgnosticAgentMask | IdsLike - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] + | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] ), - ) -> dict[AgentSetDF, DataFrame]: ... + ) -> dict[AbstractAgentSet, DataFrame]: ... def __getitem__( self, @@ -492,42 +500,44 @@ def __getitem__( | Collection[str] | AgnosticAgentMask | IdsLike - | tuple[dict[AgentSetDF, AgentMask], str] - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] + | tuple[dict[AbstractAgentSet, AgentMask], str] + | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] ), - ) -> dict[AgentSetDF, Series | pl.Expr] | dict[AgentSetDF, DataFrame]: + ) -> dict[AbstractAgentSet, Series | pl.Expr] | dict[AbstractAgentSet, DataFrame]: return super().__getitem__(key) - def __iadd__(self, agents: AgentSetDF | Iterable[AgentSetDF]) -> Self: - """Add AgentSetDFs to the AgentsDF through the += operator. + def __iadd__(self, agents: AbstractAgentSet | Iterable[AbstractAgentSet]) -> Self: + """Add AbstractAgentSets to the AgentSetRegistry through the += operator. Parameters ---------- - agents : AgentSetDF | Iterable[AgentSetDF] - The AgentSetDFs to add. + agents : AbstractAgentSet | Iterable[AbstractAgentSet] + The AbstractAgentSets to add. Returns ------- Self - The updated AgentsDF. + The updated AgentSetRegistry. """ return super().__iadd__(agents) def __iter__(self) -> Iterator[dict[str, Any]]: return (agent for agentset in self._agentsets for agent in iter(agentset)) - def __isub__(self, agents: AgentSetDF | Iterable[AgentSetDF] | IdsLike) -> Self: - """Remove AgentSetDFs from the AgentsDF through the -= operator. + def __isub__( + self, agents: AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike + ) -> Self: + """Remove AbstractAgentSets from the AgentSetRegistry through the -= operator. Parameters ---------- - agents : AgentSetDF | Iterable[AgentSetDF] | IdsLike - The AgentSetDFs or agent IDs to remove. + agents : AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike + The AbstractAgentSets or agent IDs to remove. Returns ------- Self - The updated AgentsDF. + The updated AgentSetRegistry. """ return super().__isub__(agents) @@ -551,8 +561,8 @@ def __setitem__( | Collection[str] | AgnosticAgentMask | IdsLike - | tuple[dict[AgentSetDF, AgentMask], str] - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] + | tuple[dict[AbstractAgentSet, AgentMask], str] + | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] ), values: Any, ) -> None: @@ -561,54 +571,56 @@ def __setitem__( def __str__(self) -> str: return "\n".join([str(agentset) for agentset in self._agentsets]) - def __sub__(self, agents: AgentSetDF | Iterable[AgentSetDF] | IdsLike) -> Self: - """Remove AgentSetDFs from a new AgentsDF through the - operator. + def __sub__( + self, agents: AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike + ) -> Self: + """Remove AbstractAgentSets from a new AgentSetRegistry through the - operator. Parameters ---------- - agents : AgentSetDF | Iterable[AgentSetDF] | IdsLike - The AgentSetDFs or agent IDs to remove. Supports NumPy integer types. + agents : AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike + The AbstractAgentSets or agent IDs to remove. Supports NumPy integer types. Returns ------- Self - A new AgentsDF with the removed AgentSetDFs. + A new AgentSetRegistry with the removed AbstractAgentSets. """ return super().__sub__(agents) @property - def df(self) -> dict[AgentSetDF, DataFrame]: + def df(self) -> dict[AbstractAgentSet, DataFrame]: return {agentset: agentset.df for agentset in self._agentsets} @df.setter - def df(self, other: Iterable[AgentSetDF]) -> None: - """Set the agents in the AgentsDF. + def df(self, other: Iterable[AbstractAgentSet]) -> None: + """Set the agents in the AgentSetRegistry. Parameters ---------- - other : Iterable[AgentSetDF] - The AgentSetDFs to set. + other : Iterable[AbstractAgentSet] + The AbstractAgentSets to set. """ self._agentsets = list(other) @property - def active_agents(self) -> dict[AgentSetDF, DataFrame]: + def active_agents(self) -> dict[AbstractAgentSet, DataFrame]: return {agentset: agentset.active_agents for agentset in self._agentsets} @active_agents.setter def active_agents( - self, agents: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] + self, agents: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] ) -> None: self.select(agents, inplace=True) @property - def agentsets_by_type(self) -> dict[type[AgentSetDF], Self]: - """Get the agent sets in the AgentsDF grouped by type. + def agentsets_by_type(self) -> dict[type[AbstractAgentSet], Self]: + """Get the agent sets in the AgentSetRegistry grouped by type. Returns ------- - dict[type[AgentSetDF], Self] - A dictionary mapping agent set types to the corresponding AgentsDF. + dict[type[AbstractAgentSet], Self] + A dictionary mapping agent set types to the corresponding AgentSetRegistry. """ def copy_without_agentsets() -> Self: @@ -624,13 +636,13 @@ def copy_without_agentsets() -> Self: return dictionary @property - def inactive_agents(self) -> dict[AgentSetDF, DataFrame]: + def inactive_agents(self) -> dict[AbstractAgentSet, DataFrame]: return {agentset: agentset.inactive_agents for agentset in self._agentsets} @property - def index(self) -> dict[AgentSetDF, Index]: + def index(self) -> dict[AbstractAgentSet, Index]: return {agentset: agentset.index for agentset in self._agentsets} @property - def pos(self) -> dict[AgentSetDF, DataFrame]: + def pos(self) -> dict[AbstractAgentSet, DataFrame]: return {agentset: agentset.pos for agentset in self._agentsets} diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 81759b19..7341f066 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -2,29 +2,29 @@ Polars-based implementation of AgentSet for mesa-frames. This module provides a concrete implementation of the AgentSet class using Polars -as the backend for DataFrame operations. It defines the AgentSetPolars class, -which combines the abstract AgentSetDF functionality with Polars-specific +as the backend for DataFrame operations. It defines the AgentSet class, +which combines the abstract AbstractAgentSet functionality with Polars-specific operations for efficient agent management and manipulation. Classes: - AgentSetPolars(AgentSetDF, PolarsMixin): + AgentSet(AbstractAgentSet, PolarsMixin): A Polars-based implementation of the AgentSet. This class uses Polars DataFrames to store and manipulate agent data, providing high-performance operations for large numbers of agents. -The AgentSetPolars class is designed to be used within ModelDF instances or as -part of an AgentsDF collection. It leverages the power of Polars for fast and +The AgentSet class is designed to be used within Model instances or as +part of an AgentSetRegistry collection. It leverages the power of Polars for fast and efficient data operations on agent attributes and behaviors. Usage: - The AgentSetPolars class can be used directly in a model or as part of an - AgentsDF collection: + The AgentSet class can be used directly in a model or as part of an + AgentSetRegistry collection: - from mesa_frames.concrete.model import ModelDF - from mesa_frames.concrete.agentset import AgentSetPolars + from mesa_frames.concrete.model import Model + from mesa_frames.concrete.agentset import AgentSet import polars as pl - class MyAgents(AgentSetPolars): + class MyAgents(AgentSet): def __init__(self, model): super().__init__(model) # Initialize with some agents @@ -32,15 +32,15 @@ def __init__(self, model): def step(self): # Implement step behavior using Polars operations - self.agents = self.agents.with_columns(new_wealth = pl.col('wealth') + 1) + self.sets = self.sets.with_columns(new_wealth = pl.col('wealth') + 1) - class MyModel(ModelDF): + class MyModel(Model): def __init__(self): super().__init__() - self.agents += MyAgents(self) + self.sets += MyAgents(self) def step(self): - self.agents.step() + self.sets.step() Features: - Efficient storage and manipulation of large agent populations @@ -53,7 +53,7 @@ def step(self): is installed and imported. The performance characteristics of this class will depend on the Polars version and the specific operations used. -For more detailed information on the AgentSetPolars class and its methods, +For more detailed information on the AgentSet class and its methods, refer to the class docstring. """ @@ -65,16 +65,16 @@ def step(self): import numpy as np import polars as pl -from mesa_frames.concrete.agents import AgentSetDF +from mesa_frames.concrete.agents import AbstractAgentSet from mesa_frames.concrete.mixin import PolarsMixin -from mesa_frames.concrete.model import ModelDF +from mesa_frames.concrete.model import Model from mesa_frames.types_ import AgentPolarsMask, IntoExpr, PolarsIdsLike from mesa_frames.utils import copydoc -@copydoc(AgentSetDF) -class AgentSetPolars(AgentSetDF, PolarsMixin): - """Polars-based implementation of AgentSetDF.""" +@copydoc(AbstractAgentSet) +class AgentSet(AbstractAgentSet, PolarsMixin): + """Polars-based implementation of AgentSet.""" _df: pl.DataFrame _copy_with_method: dict[str, tuple[str, list[str]]] = { @@ -83,12 +83,12 @@ class AgentSetPolars(AgentSetDF, PolarsMixin): _copy_only_reference: list[str] = ["_model", "_mask"] _mask: pl.Expr | pl.Series - def __init__(self, model: mesa_frames.concrete.model.ModelDF) -> None: - """Initialize a new AgentSetPolars. + def __init__(self, model: mesa_frames.concrete.model.Model) -> None: + """Initialize a new AgentSet. Parameters ---------- - model : "mesa_frames.concrete.model.ModelDF" + model : "mesa_frames.concrete.model.Model" The model that the agent set belongs to. """ self._model = model @@ -101,7 +101,7 @@ def add( agents: pl.DataFrame | Sequence[Any] | dict[str, Any], inplace: bool = True, ) -> Self: - """Add agents to the AgentSetPolars. + """Add agents to the AgentSet. Parameters ---------- @@ -113,12 +113,12 @@ def add( Returns ------- Self - The updated AgentSetPolars. + The updated AgentSet. """ obj = self._get_obj(inplace) - if isinstance(agents, AgentSetDF): + if isinstance(agents, AbstractAgentSet): raise TypeError( - "AgentSetPolars.add() does not accept AgentSetDF objects. " + "AgentSet.add() does not accept AbstractAgentSet objects. " "Extract the DataFrame with agents.agents.drop('unique_id') first." ) elif isinstance(agents, pl.DataFrame): @@ -314,7 +314,7 @@ def _concatenate_agentsets( all_indices = pl.concat(indices_list) if all_indices.is_duplicated().any(): raise ValueError( - "Some ids are duplicated in the AgentSetDFs that are trying to be concatenated" + "Some ids are duplicated in the AbstractAgentSets that are trying to be concatenated" ) if duplicates_allowed & keep_first_only: # Find the original_index list (ie longest index list), to sort correctly the rows after concatenation diff --git a/mesa_frames/concrete/datacollector.py b/mesa_frames/concrete/datacollector.py index 02e40423..2b50c76d 100644 --- a/mesa_frames/concrete/datacollector.py +++ b/mesa_frames/concrete/datacollector.py @@ -26,18 +26,18 @@ If true, data is collected during `conditional_collect()`. Usage: - The `DataCollector` class is designed to be used within a `ModelDF` instance + The `DataCollector` class is designed to be used within a `Model` instance to collect model-level and/or agent-level data. Example: -------- - from mesa_frames.concrete.model import ModelDF + from mesa_frames.concrete.model import Model from mesa_frames.concrete.datacollector import DataCollector - class ExampleModel(ModelDF): - def __init__(self, agents: AgentsDF): + class ExampleModel(Model): + def __init__(self, agents: AgentSetRegistry): super().__init__() - self.agents = agents + self.sets = agents self.dc = DataCollector( model=self, # other required arguments @@ -62,14 +62,14 @@ def step(self): from mesa_frames.abstract.datacollector import AbstractDataCollector from typing import Any, Literal from collections.abc import Callable -from mesa_frames import ModelDF +from mesa_frames import Model from psycopg2.extensions import connection class DataCollector(AbstractDataCollector): def __init__( self, - model: ModelDF, + model: Model, model_reporters: dict[str, Callable] | None = None, agent_reporters: dict[str, str | Callable] | None = None, trigger: Callable[[Any], bool] | None = None, @@ -86,7 +86,7 @@ def __init__( Parameters ---------- - model : ModelDF + model : Model The model object from which data is collected. model_reporters : dict[str, Callable] | None Functions to collect data at the model level. @@ -180,7 +180,7 @@ def _collect_agent_reporters(self, current_model_step: int, batch_id: int): agent_data_dict = {} for col_name, reporter in self._agent_reporters.items(): if isinstance(reporter, str): - for k, v in self._model.agents[reporter].items(): + for k, v in self._model.sets[reporter].items(): agent_data_dict[col_name + "_" + str(k.__class__.__name__)] = v else: agent_data_dict[col_name] = reporter(self._model) @@ -463,7 +463,7 @@ def _validate_reporter_table_columns( expected_columns = set() for col_name, required_column in reporter.items(): if isinstance(required_column, str): - for k, v in self._model.agents[required_column].items(): + for k, v in self._model.sets[required_column].items(): expected_columns.add( (col_name + "_" + str(k.__class__.__name__)).lower() ) diff --git a/mesa_frames/concrete/mixin.py b/mesa_frames/concrete/mixin.py index eba00ae6..0f2f9eca 100644 --- a/mesa_frames/concrete/mixin.py +++ b/mesa_frames/concrete/mixin.py @@ -10,7 +10,7 @@ PolarsMixin(DataFrameMixin): A Polars-based implementation of DataFrame operations. This class provides methods for manipulating and analyzing data stored in Polars DataFrames, - tailored for use in mesa-frames components like AgentSetPolars and GridPolars. + tailored for use in mesa-frames components like AgentSet and GridPolars. The PolarsMixin class is designed to be used as a mixin with other mesa-frames classes, providing them with Polars-specific DataFrame functionality. It implements @@ -20,17 +20,17 @@ Usage: The PolarsMixin is typically used in combination with other base classes: - from mesa_frames.abstract import AgentSetDF + from mesa_frames.abstract import AbstractAgentSet from mesa_frames.concrete.mixin import PolarsMixin - class AgentSetPolars(AgentSetDF, PolarsMixin): + class AgentSet(AbstractAgentSet, PolarsMixin): def __init__(self, model): super().__init__(model) - self.agents = pl.DataFrame() # Initialize empty DataFrame + self.sets = pl.DataFrame() # Initialize empty DataFrame def some_method(self): # Use Polars operations provided by the mixin - result = self._df_groupby(self.agents, 'some_column') + result = self._df_groupby(self.sets, 'some_column') # ... further processing ... Features: diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index befc1812..2703c0e6 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -2,31 +2,31 @@ Concrete implementation of the model class for mesa-frames. This module provides the concrete implementation of the base model class for -the mesa-frames library. It defines the ModelDF class, which serves as the +the mesa-frames library. It defines the Model class, which serves as the foundation for creating agent-based models using DataFrame-based agent storage. Classes: - ModelDF: + Model: The base class for models in the mesa-frames library. This class provides the core functionality for initializing and running agent-based simulations using DataFrame-backed agent sets. -The ModelDF class is designed to be subclassed by users to create specific +The Model class is designed to be subclassed by users to create specific model implementations. It provides the basic structure and methods necessary for setting up and running simulations, while leveraging the performance benefits of DataFrame-based agent storage. Usage: - To create a custom model, subclass ModelDF and implement the necessary + To create a custom model, subclass Model and implement the necessary methods: - from mesa_frames.concrete.model import ModelDF - from mesa_frames.concrete.agents import AgentSetPolars + from mesa_frames.concrete.model import Model + from mesa_frames.concrete.agentset import AgentSet - class MyCustomModel(ModelDF): + class MyCustomModel(Model): def __init__(self, num_agents): super().__init__() - self.agents += AgentSetPolars(self) + self.sets += AgentSet(self) # Initialize your model-specific attributes and agent sets def run_model(self): @@ -36,7 +36,7 @@ def run_model(self): # Add any other custom methods for your model -For more detailed information on the ModelDF class and its methods, refer to +For more detailed information on the Model class and its methods, refer to the class docstring. """ @@ -46,12 +46,12 @@ def run_model(self): import numpy as np -from mesa_frames.abstract.agents import AgentSetDF +from mesa_frames.abstract.agents import AbstractAgentSet from mesa_frames.abstract.space import SpaceDF -from mesa_frames.concrete.agents import AgentsDF +from mesa_frames.concrete.agents import AgentSetRegistry -class ModelDF: +class Model: """Base class for models in the mesa-frames library. This class serves as a foundational structure for creating agent-based models. @@ -63,7 +63,7 @@ class ModelDF: random: np.random.Generator running: bool _seed: int | Sequence[int] - _agents: AgentsDF # Where the agents are stored + _sets: AgentSetRegistry # Where the agent sets are stored _space: SpaceDF | None # This will be a MultiSpaceDF object def __init__(self, seed: int | Sequence[int] | None = None) -> None: @@ -82,7 +82,7 @@ def __init__(self, seed: int | Sequence[int] | None = None) -> None: self.reset_randomizer(seed) self.running = True self.current_id = 0 - self._agents = AgentsDF(self) + self._sets = AgentSetRegistry(self) self._space = None self._steps = 0 @@ -99,23 +99,23 @@ def steps(self) -> int: """Get the current step count.""" return self._steps - def get_agents_of_type(self, agent_type: type) -> AgentSetDF: - """Retrieve the AgentSetDF of a specified type. + def get_sets_of_type(self, agent_type: type) -> AbstractAgentSet: + """Retrieve the AbstractAgentSet of a specified type. Parameters ---------- agent_type : type - The type of AgentSetDF to retrieve. + The type of AbstractAgentSet to retrieve. Returns ------- - AgentSetDF - The AgentSetDF of the specified type. + AbstractAgentSet + The AbstractAgentSet of the specified type. """ - for agentset in self._agents._agentsets: + for agentset in self._sets._agentsets: if isinstance(agentset, agent_type): return agentset - raise ValueError(f"No agents of type {agent_type} found in the model.") + raise ValueError(f"No agent sets of type {agent_type} found in the model.") def reset_randomizer(self, seed: int | Sequence[int] | None) -> None: """Reset the model random number generator. @@ -144,7 +144,7 @@ def step(self) -> None: The default method calls the step() method of all agents. Overload as needed. """ - self.agents.step() + self.sets.step() @property def steps(self) -> int: @@ -158,13 +158,13 @@ def steps(self) -> int: return self._steps @property - def agents(self) -> AgentsDF: - """Get the AgentsDF object containing all agents in the model. + def sets(self) -> AgentSetRegistry: + """Get the AgentSetRegistry object containing all agent sets in the model. Returns ------- - AgentsDF - The AgentsDF object containing all agents in the model. + AgentSetRegistry + The AgentSetRegistry object containing all agent sets in the model. Raises ------ @@ -172,31 +172,31 @@ def agents(self) -> AgentsDF: If the model has not been initialized properly with super().__init__(). """ try: - return self._agents + return self._sets except AttributeError: if __debug__: # Only execute in non-optimized mode raise RuntimeError( "You haven't called super().__init__() in your model. Make sure to call it in your __init__ method." ) - @agents.setter - def agents(self, agents: AgentsDF) -> None: + @sets.setter + def sets(self, sets: AgentSetRegistry) -> None: if __debug__: # Only execute in non-optimized mode - if not isinstance(agents, AgentsDF): - raise TypeError("agents must be an instance of AgentsDF") + if not isinstance(sets, AgentSetRegistry): + raise TypeError("sets must be an instance of AgentSetRegistry") - self._agents = agents + self._sets = sets @property - def agent_types(self) -> list[type]: - """Get a list of different agent types present in the model. + def set_types(self) -> list[type]: + """Get a list of different agent set types present in the model. Returns ------- list[type] - A list of the different agent types present in the model. + A list of the different agent set types present in the model. """ - return [agent.__class__ for agent in self._agents._agentsets] + return [agent.__class__ for agent in self._sets._agentsets] @property def space(self) -> SpaceDF: diff --git a/mesa_frames/concrete/space.py b/mesa_frames/concrete/space.py index 55a00589..20f87b0c 100644 --- a/mesa_frames/concrete/space.py +++ b/mesa_frames/concrete/space.py @@ -12,7 +12,7 @@ DataFrames to store and manipulate spatial data, providing high-performance operations for large-scale spatial simulations. -The GridPolars class is designed to be used within ModelDF instances to represent +The GridPolars class is designed to be used within Model instances to represent the spatial environment of the simulation. It leverages the power of Polars for fast and efficient data operations on spatial attributes and agent positions. @@ -20,22 +20,22 @@ The GridPolars class can be used directly in a model to represent the spatial environment: - from mesa_frames.concrete.model import ModelDF + from mesa_frames.concrete.model import Model from mesa_frames.concrete.space import GridPolars - from mesa_frames.concrete.agentset import AgentSetPolars + from mesa_frames.concrete.agentset import AgentSet - class MyAgents(AgentSetPolars): + class MyAgents(AgentSet): # ... agent implementation ... - class MyModel(ModelDF): + class MyModel(Model): def __init__(self, width, height): super().__init__() self.space = GridPolars(self, [width, height]) - self.agents += MyAgents(self) + self.sets += MyAgents(self) def step(self): # Move agents - self.space.move_agents(self.agents) + self.space.move_agents(self.sets) # ... other model logic ... For more detailed information on the GridPolars class and its methods, diff --git a/tests/test_agents.py b/tests/test_agents.py index 414bb632..f43d94f6 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -3,34 +3,34 @@ import polars as pl import pytest -from mesa_frames import AgentsDF, ModelDF -from mesa_frames.abstract.agents import AgentSetDF +from mesa_frames import AgentSetRegistry, Model +from mesa_frames import AgentSet from mesa_frames.types_ import AgentMask from tests.test_agentset import ( - ExampleAgentSetPolars, - ExampleAgentSetPolarsNoWealth, - fix1_AgentSetPolars_no_wealth, - fix1_AgentSetPolars, - fix2_AgentSetPolars, - fix3_AgentSetPolars, + ExampleAgentSet, + ExampleAgentSetNoWealth, + fix1_AgentSet_no_wealth, + fix1_AgentSet, + fix2_AgentSet, + fix3_AgentSet, ) @pytest.fixture -def fix_AgentsDF( - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, -) -> AgentsDF: - model = ModelDF() - agents = AgentsDF(model) - agents.add([fix1_AgentSetPolars, fix2_AgentSetPolars]) +def fix_AgentSetRegistry( + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, +) -> AgentSetRegistry: + model = Model() + agents = AgentSetRegistry(model) + agents.add([fix1_AgentSet, fix2_AgentSet]) return agents -class Test_AgentsDF: +class Test_AgentSetRegistry: def test___init__(self): - model = ModelDF() - agents = AgentsDF(model) + model = Model() + agents = AgentSetRegistry(model) assert agents.model == model assert isinstance(agents._agentsets, list) assert len(agents._agentsets) == 0 @@ -40,20 +40,20 @@ def test___init__(self): def test_add( self, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - model = ModelDF() - agents = AgentsDF(model) - agentset_polars1 = fix1_AgentSetPolars - agentset_polars2 = fix2_AgentSetPolars + model = Model() + agents = AgentSetRegistry(model) + agentset_polars1 = fix1_AgentSet + agentset_polars2 = fix2_AgentSet - # Test with a single AgentSetPolars + # Test with a single AgentSet result = agents.add(agentset_polars1, inplace=False) assert result._agentsets[0] is agentset_polars1 assert result._ids.to_list() == agentset_polars1._df["unique_id"].to_list() - # Test with a list of AgentSetDFs + # Test with a list of AgentSets result = agents.add([agentset_polars1, agentset_polars2], inplace=True) assert result._agentsets[0] is agentset_polars1 assert result._agentsets[1] is agentset_polars2 @@ -63,30 +63,30 @@ def test_add( + agentset_polars2._df["unique_id"].to_list() ) - # Test if adding the same AgentSetDF raises ValueError + # Test if adding the same AgentSet raises ValueError with pytest.raises(ValueError): agents.add(agentset_polars1, inplace=False) def test_contains( self, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, - fix3_AgentSetPolars: ExampleAgentSetPolars, - fix_AgentsDF: AgentsDF, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, + fix3_AgentSet: ExampleAgentSet, + fix_AgentSetRegistry: AgentSetRegistry, ): - agents = fix_AgentsDF + agents = fix_AgentSetRegistry agentset_polars1 = agents._agentsets[0] - # Test with an AgentSetDF + # Test with an AgentSet assert agents.contains(agentset_polars1) - assert agents.contains(fix1_AgentSetPolars) - assert agents.contains(fix2_AgentSetPolars) + assert agents.contains(fix1_AgentSet) + assert agents.contains(fix2_AgentSet) - # Test with an AgentSetDF not present - assert not agents.contains(fix3_AgentSetPolars) + # Test with an AgentSet not present + assert not agents.contains(fix3_AgentSet) - # Test with an iterable of AgentSetDFs - assert agents.contains([agentset_polars1, fix3_AgentSetPolars]).to_list() == [ + # Test with an iterable of AgentSets + assert agents.contains([agentset_polars1, fix3_AgentSet]).to_list() == [ True, False, ] @@ -100,8 +100,8 @@ def test_contains( False, ] - def test_copy(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test_copy(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry agents.test_list = [[1, 2, 3]] # Test with deep=False @@ -113,7 +113,7 @@ def test_copy(self, fix_AgentsDF: AgentsDF): assert (agents._ids == agents2._ids).all() # Test with deep=True - agents2 = fix_AgentsDF.copy(deep=True) + agents2 = fix_AgentSetRegistry.copy(deep=True) agents2.test_list[0].append(4) assert agents.test_list[-1] != agents2.test_list[-1] assert agents.model == agents2.model @@ -121,16 +121,16 @@ def test_copy(self, fix_AgentsDF: AgentsDF): assert (agents._ids == agents2._ids).all() def test_discard( - self, fix_AgentsDF: AgentsDF, fix2_AgentSetPolars: ExampleAgentSetPolars + self, fix_AgentSetRegistry: AgentSetRegistry, fix2_AgentSet: ExampleAgentSet ): - agents = fix_AgentsDF - # Test with a single AgentSetDF + agents = fix_AgentSetRegistry + # Test with a single AgentSet agentset_polars2 = agents._agentsets[1] result = agents.discard(agents._agentsets[0], inplace=False) - assert isinstance(result._agentsets[0], ExampleAgentSetPolars) + assert isinstance(result._agentsets[0], ExampleAgentSet) assert len(result._agentsets) == 1 - # Test with a list of AgentSetDFs + # Test with a list of AgentSets result = agents.discard(agents._agentsets.copy(), inplace=False) assert len(result._agentsets) == 0 @@ -151,15 +151,15 @@ def test_discard( == agentset_polars2._df["unique_id"][1] ) - # Test if removing an AgentSetDF not present raises ValueError - result = agents.discard(fix2_AgentSetPolars, inplace=False) + # Test if removing an AgentSet not present raises ValueError + result = agents.discard(fix2_AgentSet, inplace=False) # Test if removing an ID not present raises KeyError assert 0 not in agents._ids result = agents.discard(0, inplace=False) - def test_do(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test_do(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry expected_result_0 = agents._agentsets[0].df["wealth"] expected_result_0 += 1 @@ -212,88 +212,84 @@ def test_do(self, fix_AgentsDF: AgentsDF): def test_get( self, - fix_AgentsDF: AgentsDF, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, - fix1_AgentSetPolars_no_wealth: ExampleAgentSetPolarsNoWealth, + fix_AgentSetRegistry: AgentSetRegistry, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, + fix1_AgentSet_no_wealth: ExampleAgentSetNoWealth, ): - agents = fix_AgentsDF + agents = fix_AgentSetRegistry # Test with a single attribute assert ( - agents.get("wealth")[fix1_AgentSetPolars].to_list() - == fix1_AgentSetPolars._df["wealth"].to_list() + agents.get("wealth")[fix1_AgentSet].to_list() + == fix1_AgentSet._df["wealth"].to_list() ) assert ( - agents.get("wealth")[fix2_AgentSetPolars].to_list() - == fix2_AgentSetPolars._df["wealth"].to_list() + agents.get("wealth")[fix2_AgentSet].to_list() + == fix2_AgentSet._df["wealth"].to_list() ) # Test with a list of attributes result = agents.get(["wealth", "age"]) - assert result[fix1_AgentSetPolars].columns == ["wealth", "age"] + assert result[fix1_AgentSet].columns == ["wealth", "age"] assert ( - result[fix1_AgentSetPolars]["wealth"].to_list() - == fix1_AgentSetPolars._df["wealth"].to_list() + result[fix1_AgentSet]["wealth"].to_list() + == fix1_AgentSet._df["wealth"].to_list() ) assert ( - result[fix1_AgentSetPolars]["age"].to_list() - == fix1_AgentSetPolars._df["age"].to_list() + result[fix1_AgentSet]["age"].to_list() == fix1_AgentSet._df["age"].to_list() ) - assert result[fix2_AgentSetPolars].columns == ["wealth", "age"] + assert result[fix2_AgentSet].columns == ["wealth", "age"] assert ( - result[fix2_AgentSetPolars]["wealth"].to_list() - == fix2_AgentSetPolars._df["wealth"].to_list() + result[fix2_AgentSet]["wealth"].to_list() + == fix2_AgentSet._df["wealth"].to_list() ) assert ( - result[fix2_AgentSetPolars]["age"].to_list() - == fix2_AgentSetPolars._df["age"].to_list() + result[fix2_AgentSet]["age"].to_list() == fix2_AgentSet._df["age"].to_list() ) # Test with a single attribute and a mask - mask0 = fix1_AgentSetPolars._df["wealth"] > fix1_AgentSetPolars._df["wealth"][0] - mask1 = fix2_AgentSetPolars._df["wealth"] > fix2_AgentSetPolars._df["wealth"][0] - mask_dictionary = {fix1_AgentSetPolars: mask0, fix2_AgentSetPolars: mask1} + mask0 = fix1_AgentSet._df["wealth"] > fix1_AgentSet._df["wealth"][0] + mask1 = fix2_AgentSet._df["wealth"] > fix2_AgentSet._df["wealth"][0] + mask_dictionary = {fix1_AgentSet: mask0, fix2_AgentSet: mask1} result = agents.get("wealth", mask=mask_dictionary) assert ( - result[fix1_AgentSetPolars].to_list() - == fix1_AgentSetPolars._df["wealth"].to_list()[1:] + result[fix1_AgentSet].to_list() == fix1_AgentSet._df["wealth"].to_list()[1:] ) assert ( - result[fix2_AgentSetPolars].to_list() - == fix2_AgentSetPolars._df["wealth"].to_list()[1:] + result[fix2_AgentSet].to_list() == fix2_AgentSet._df["wealth"].to_list()[1:] ) # Test heterogeneous agent sets (different columns) # This tests the fix for the bug where agents_df["column"] would raise # ColumnNotFoundError when some agent sets didn't have that column. - # Create a new AgentsDF with heterogeneous agent sets - model = ModelDF() - hetero_agents = AgentsDF(model) - hetero_agents.add([fix1_AgentSetPolars, fix1_AgentSetPolars_no_wealth]) + # Create a new AgentSetRegistry with heterogeneous agent sets + model = Model() + hetero_agents = AgentSetRegistry(model) + hetero_agents.add([fix1_AgentSet, fix1_AgentSet_no_wealth]) # Test 1: Access column that exists in only one agent set result_wealth = hetero_agents.get("wealth") assert len(result_wealth) == 1, ( "Should only return agent sets that have 'wealth'" ) - assert fix1_AgentSetPolars in result_wealth, ( + assert fix1_AgentSet in result_wealth, ( "Should include the agent set with wealth" ) - assert fix1_AgentSetPolars_no_wealth not in result_wealth, ( + assert fix1_AgentSet_no_wealth not in result_wealth, ( "Should not include agent set without wealth" ) - assert result_wealth[fix1_AgentSetPolars].to_list() == [1, 2, 3, 4] + assert result_wealth[fix1_AgentSet].to_list() == [1, 2, 3, 4] # Test 2: Access column that exists in all agent sets result_age = hetero_agents.get("age") assert len(result_age) == 2, "Should return both agent sets that have 'age'" - assert fix1_AgentSetPolars in result_age - assert fix1_AgentSetPolars_no_wealth in result_age - assert result_age[fix1_AgentSetPolars].to_list() == [10, 20, 30, 40] - assert result_age[fix1_AgentSetPolars_no_wealth].to_list() == [1, 2, 3, 4] + assert fix1_AgentSet in result_age + assert fix1_AgentSet_no_wealth in result_age + assert result_age[fix1_AgentSet].to_list() == [10, 20, 30, 40] + assert result_age[fix1_AgentSet_no_wealth].to_list() == [1, 2, 3, 4] # Test 3: Access column that exists in no agent sets result_nonexistent = hetero_agents.get("nonexistent_column") @@ -306,41 +302,41 @@ def test_get( assert len(result_multi) == 1, ( "Should only include agent sets that have ALL requested columns" ) - assert fix1_AgentSetPolars in result_multi - assert fix1_AgentSetPolars_no_wealth not in result_multi - assert result_multi[fix1_AgentSetPolars].columns == ["wealth", "age"] + assert fix1_AgentSet in result_multi + assert fix1_AgentSet_no_wealth not in result_multi + assert result_multi[fix1_AgentSet].columns == ["wealth", "age"] # Test 5: Access multiple columns where some exist in different sets result_mixed = hetero_agents.get(["age", "income"]) assert len(result_mixed) == 1, ( "Should only include agent set that has both 'age' and 'income'" ) - assert fix1_AgentSetPolars_no_wealth in result_mixed - assert fix1_AgentSetPolars not in result_mixed + assert fix1_AgentSet_no_wealth in result_mixed + assert fix1_AgentSet not in result_mixed # Test 6: Test via __getitem__ syntax (the original bug report case) wealth_via_getitem = hetero_agents["wealth"] assert len(wealth_via_getitem) == 1 - assert fix1_AgentSetPolars in wealth_via_getitem - assert wealth_via_getitem[fix1_AgentSetPolars].to_list() == [1, 2, 3, 4] + assert fix1_AgentSet in wealth_via_getitem + assert wealth_via_getitem[fix1_AgentSet].to_list() == [1, 2, 3, 4] # Test 7: Test get(None) - should return all columns for all agent sets result_none = hetero_agents.get(None) assert len(result_none) == 2, ( "Should return both agent sets when attr_names=None" ) - assert fix1_AgentSetPolars in result_none - assert fix1_AgentSetPolars_no_wealth in result_none + assert fix1_AgentSet in result_none + assert fix1_AgentSet_no_wealth in result_none # Verify each agent set returns all its columns (excluding unique_id) - wealth_set_result = result_none[fix1_AgentSetPolars] + wealth_set_result = result_none[fix1_AgentSet] assert isinstance(wealth_set_result, pl.DataFrame), ( "Should return DataFrame when attr_names=None" ) expected_wealth_cols = {"wealth", "age"} # unique_id should be excluded assert set(wealth_set_result.columns) == expected_wealth_cols - no_wealth_set_result = result_none[fix1_AgentSetPolars_no_wealth] + no_wealth_set_result = result_none[fix1_AgentSet_no_wealth] assert isinstance(no_wealth_set_result, pl.DataFrame), ( "Should return DataFrame when attr_names=None" ) @@ -349,18 +345,18 @@ def test_get( def test_remove( self, - fix_AgentsDF: AgentsDF, - fix3_AgentSetPolars: ExampleAgentSetPolars, + fix_AgentSetRegistry: AgentSetRegistry, + fix3_AgentSet: ExampleAgentSet, ): - agents = fix_AgentsDF + agents = fix_AgentSetRegistry - # Test with a single AgentSetDF + # Test with a single AgentSet agentset_polars = agents._agentsets[1] result = agents.remove(agents._agentsets[0], inplace=False) - assert isinstance(result._agentsets[0], ExampleAgentSetPolars) + assert isinstance(result._agentsets[0], ExampleAgentSet) assert len(result._agentsets) == 1 - # Test with a list of AgentSetDFs + # Test with a list of AgentSets result = agents.remove(agents._agentsets.copy(), inplace=False) assert len(result._agentsets) == 0 @@ -381,17 +377,17 @@ def test_remove( == agentset_polars2._df["unique_id"][1] ) - # Test if removing an AgentSetDF not present raises ValueError + # Test if removing an AgentSet not present raises ValueError with pytest.raises(ValueError): - result = agents.remove(fix3_AgentSetPolars, inplace=False) + result = agents.remove(fix3_AgentSet, inplace=False) # Test if removing an ID not present raises KeyError assert 0 not in agents._ids with pytest.raises(KeyError): result = agents.remove(0, inplace=False) - def test_select(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test_select(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry # Test with default arguments. Should select all agents selected = agents.select(inplace=False) @@ -437,7 +433,7 @@ def test_select(self, fix_AgentsDF: AgentsDF): # Test with filter_func - def filter_func(agentset: AgentSetDF) -> pl.Series: + def filter_func(agentset: AgentSet) -> pl.Series: return agentset.df["wealth"] > agentset.df["wealth"].to_list()[0] selected = agents.select(filter_func=filter_func, inplace=False) @@ -472,8 +468,8 @@ def filter_func(agentset: AgentSetDF) -> pl.Series: ] ) - def test_set(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test_set(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry # Test with a single attribute result = agents.set("wealth", 0, inplace=False) @@ -521,8 +517,8 @@ def test_set(self, fix_AgentsDF: AgentsDF): agents._agentsets[1] ) - def test_shuffle(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test_shuffle(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry for _ in range(100): original_order_0 = agents._agentsets[0].df["unique_id"].to_list() original_order_1 = agents._agentsets[1].df["unique_id"].to_list() @@ -534,22 +530,22 @@ def test_shuffle(self, fix_AgentsDF: AgentsDF): return assert False - def test_sort(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test_sort(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry agents.sort("wealth", ascending=False, inplace=True) assert pl.Series(agents._agentsets[0].df["wealth"]).is_sorted(descending=True) assert pl.Series(agents._agentsets[1].df["wealth"]).is_sorted(descending=True) def test_step( self, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, - fix_AgentsDF: AgentsDF, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, + fix_AgentSetRegistry: AgentSetRegistry, ): - previous_wealth_0 = fix1_AgentSetPolars._df["wealth"].clone() - previous_wealth_1 = fix2_AgentSetPolars._df["wealth"].clone() + previous_wealth_0 = fix1_AgentSet._df["wealth"].clone() + previous_wealth_1 = fix2_AgentSet._df["wealth"].clone() - agents = fix_AgentsDF + agents = fix_AgentSetRegistry agents.step() assert ( @@ -563,16 +559,16 @@ def test_step( def test__check_ids_presence( self, - fix_AgentsDF: AgentsDF, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix_AgentSetRegistry: AgentSetRegistry, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - agents = fix_AgentsDF.remove(fix2_AgentSetPolars, inplace=False) - agents_different_index = deepcopy(fix2_AgentSetPolars) - result = agents._check_ids_presence([fix1_AgentSetPolars]) - assert result.filter( - pl.col("unique_id").is_in(fix1_AgentSetPolars._df["unique_id"]) - )["present"].all() + agents = fix_AgentSetRegistry.remove(fix2_AgentSet, inplace=False) + agents_different_index = deepcopy(fix2_AgentSet) + result = agents._check_ids_presence([fix1_AgentSet]) + assert result.filter(pl.col("unique_id").is_in(fix1_AgentSet._df["unique_id"]))[ + "present" + ].all() assert not result.filter( pl.col("unique_id").is_in(agents_different_index._df["unique_id"]) @@ -580,19 +576,17 @@ def test__check_ids_presence( def test__check_agentsets_presence( self, - fix_AgentsDF: AgentsDF, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix3_AgentSetPolars: ExampleAgentSetPolars, + fix_AgentSetRegistry: AgentSetRegistry, + fix1_AgentSet: ExampleAgentSet, + fix3_AgentSet: ExampleAgentSet, ): - agents = fix_AgentsDF - result = agents._check_agentsets_presence( - [fix1_AgentSetPolars, fix3_AgentSetPolars] - ) + agents = fix_AgentSetRegistry + result = agents._check_agentsets_presence([fix1_AgentSet, fix3_AgentSet]) assert result[0] assert not result[1] - def test__get_bool_masks(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test__get_bool_masks(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry # Test with mask = None result = agents._get_bool_masks(mask=None) truth_value = True @@ -637,51 +631,49 @@ def test__get_bool_masks(self, fix_AgentsDF: AgentsDF): len(agents._agentsets[1]) - 1 ) - # Test with mask = dict[AgentSetDF, AgentMask] + # Test with mask = dict[AgentSet, AgentMask] result = agents._get_bool_masks(mask=mask_dictionary) assert result[agents._agentsets[0]].to_list() == mask0.to_list() assert result[agents._agentsets[1]].to_list() == mask1.to_list() - def test__get_obj(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test__get_obj(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry assert agents._get_obj(inplace=True) is agents assert agents._get_obj(inplace=False) is not agents def test__return_agentsets_list( self, - fix_AgentsDF: AgentsDF, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix_AgentSetRegistry: AgentSetRegistry, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - agents = fix_AgentsDF - result = agents._return_agentsets_list(fix1_AgentSetPolars) - assert result == [fix1_AgentSetPolars] - result = agents._return_agentsets_list( - [fix1_AgentSetPolars, fix2_AgentSetPolars] - ) - assert result == [fix1_AgentSetPolars, fix2_AgentSetPolars] + agents = fix_AgentSetRegistry + result = agents._return_agentsets_list(fix1_AgentSet) + assert result == [fix1_AgentSet] + result = agents._return_agentsets_list([fix1_AgentSet, fix2_AgentSet]) + assert result == [fix1_AgentSet, fix2_AgentSet] def test___add__( self, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - model = ModelDF() - agents = AgentsDF(model) - agentset_polars1 = fix1_AgentSetPolars - agentset_polars2 = fix2_AgentSetPolars + model = Model() + agents = AgentSetRegistry(model) + agentset_polars1 = fix1_AgentSet + agentset_polars2 = fix2_AgentSet - # Test with a single AgentSetPolars + # Test with a single AgentSet result = agents + agentset_polars1 assert result._agentsets[0] is agentset_polars1 assert result._ids.to_list() == agentset_polars1._df["unique_id"].to_list() - # Test with a single AgentSetPolars same as above + # Test with a single AgentSet same as above result = agents + agentset_polars2 assert result._agentsets[0] is agentset_polars2 assert result._ids.to_list() == agentset_polars2._df["unique_id"].to_list() - # Test with a list of AgentSetDFs + # Test with a list of AgentSets result = agents + [agentset_polars1, agentset_polars2] assert result._agentsets[0] is agentset_polars1 assert result._agentsets[1] is agentset_polars2 @@ -691,21 +683,21 @@ def test___add__( + agentset_polars2._df["unique_id"].to_list() ) - # Test if adding the same AgentSetDF raises ValueError + # Test if adding the same AgentSet raises ValueError with pytest.raises(ValueError): result + agentset_polars1 def test___contains__( - self, fix_AgentsDF: AgentsDF, fix3_AgentSetPolars: ExampleAgentSetPolars + self, fix_AgentSetRegistry: AgentSetRegistry, fix3_AgentSet: ExampleAgentSet ): # Test with a single value - agents = fix_AgentsDF + agents = fix_AgentSetRegistry agentset_polars1 = agents._agentsets[0] - # Test with an AgentSetDF + # Test with an AgentSet assert agentset_polars1 in agents - # Test with an AgentSetDF not present - assert fix3_AgentSetPolars not in agents + # Test with an AgentSet not present + assert fix3_AgentSet not in agents # Test with single id present assert agentset_polars1["unique_id"][0] in agents @@ -713,8 +705,8 @@ def test___contains__( # Test with single id not present assert 0 not in agents - def test___copy__(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test___copy__(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry agents.test_list = [[1, 2, 3]] # Test with deep=False @@ -725,8 +717,8 @@ def test___copy__(self, fix_AgentsDF: AgentsDF): assert agents._agentsets[0] == agents2._agentsets[0] assert (agents._ids == agents2._ids).all() - def test___deepcopy__(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test___deepcopy__(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry agents.test_list = [[1, 2, 3]] agents2 = deepcopy(agents) @@ -736,9 +728,9 @@ def test___deepcopy__(self, fix_AgentsDF: AgentsDF): assert agents._agentsets[0] != agents2._agentsets[0] assert (agents._ids == agents2._ids).all() - def test___getattr__(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF - assert isinstance(agents.model, ModelDF) + def test___getattr__(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry + assert isinstance(agents.model, Model) result = agents.wealth assert ( result[agents._agentsets[0]].to_list() @@ -751,77 +743,73 @@ def test___getattr__(self, fix_AgentsDF: AgentsDF): def test___getitem__( self, - fix_AgentsDF: AgentsDF, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix_AgentSetRegistry: AgentSetRegistry, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - agents = fix_AgentsDF + agents = fix_AgentSetRegistry # Test with a single attribute assert ( - agents["wealth"][fix1_AgentSetPolars].to_list() - == fix1_AgentSetPolars._df["wealth"].to_list() + agents["wealth"][fix1_AgentSet].to_list() + == fix1_AgentSet._df["wealth"].to_list() ) assert ( - agents["wealth"][fix2_AgentSetPolars].to_list() - == fix2_AgentSetPolars._df["wealth"].to_list() + agents["wealth"][fix2_AgentSet].to_list() + == fix2_AgentSet._df["wealth"].to_list() ) # Test with a list of attributes result = agents[["wealth", "age"]] - assert result[fix1_AgentSetPolars].columns == ["wealth", "age"] + assert result[fix1_AgentSet].columns == ["wealth", "age"] assert ( - result[fix1_AgentSetPolars]["wealth"].to_list() - == fix1_AgentSetPolars._df["wealth"].to_list() + result[fix1_AgentSet]["wealth"].to_list() + == fix1_AgentSet._df["wealth"].to_list() ) assert ( - result[fix1_AgentSetPolars]["age"].to_list() - == fix1_AgentSetPolars._df["age"].to_list() + result[fix1_AgentSet]["age"].to_list() == fix1_AgentSet._df["age"].to_list() ) - assert result[fix2_AgentSetPolars].columns == ["wealth", "age"] + assert result[fix2_AgentSet].columns == ["wealth", "age"] assert ( - result[fix2_AgentSetPolars]["wealth"].to_list() - == fix2_AgentSetPolars._df["wealth"].to_list() + result[fix2_AgentSet]["wealth"].to_list() + == fix2_AgentSet._df["wealth"].to_list() ) assert ( - result[fix2_AgentSetPolars]["age"].to_list() - == fix2_AgentSetPolars._df["age"].to_list() + result[fix2_AgentSet]["age"].to_list() == fix2_AgentSet._df["age"].to_list() ) # Test with a single attribute and a mask - mask0 = fix1_AgentSetPolars._df["wealth"] > fix1_AgentSetPolars._df["wealth"][0] - mask1 = fix2_AgentSetPolars._df["wealth"] > fix2_AgentSetPolars._df["wealth"][0] - mask_dictionary: dict[AgentSetDF, AgentMask] = { - fix1_AgentSetPolars: mask0, - fix2_AgentSetPolars: mask1, + mask0 = fix1_AgentSet._df["wealth"] > fix1_AgentSet._df["wealth"][0] + mask1 = fix2_AgentSet._df["wealth"] > fix2_AgentSet._df["wealth"][0] + mask_dictionary: dict[AgentSet, AgentMask] = { + fix1_AgentSet: mask0, + fix2_AgentSet: mask1, } result = agents[mask_dictionary, "wealth"] assert ( - result[fix1_AgentSetPolars].to_list() - == fix1_AgentSetPolars.df["wealth"].to_list()[1:] + result[fix1_AgentSet].to_list() == fix1_AgentSet.df["wealth"].to_list()[1:] ) assert ( - result[fix2_AgentSetPolars].to_list() - == fix2_AgentSetPolars.df["wealth"].to_list()[1:] + result[fix2_AgentSet].to_list() == fix2_AgentSet.df["wealth"].to_list()[1:] ) def test___iadd__( self, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - model = ModelDF() - agents = AgentsDF(model) - agentset_polars1 = fix1_AgentSetPolars - agentset_polars = fix2_AgentSetPolars + model = Model() + agents = AgentSetRegistry(model) + agentset_polars1 = fix1_AgentSet + agentset_polars = fix2_AgentSet - # Test with a single AgentSetPolars + # Test with a single AgentSet agents_copy = deepcopy(agents) agents_copy += agentset_polars assert agents_copy._agentsets[0] is agentset_polars assert agents_copy._ids.to_list() == agentset_polars._df["unique_id"].to_list() - # Test with a list of AgentSetDFs + # Test with a list of AgentSets agents_copy = deepcopy(agents) agents_copy += [agentset_polars1, agentset_polars] assert agents_copy._agentsets[0] is agentset_polars1 @@ -832,12 +820,12 @@ def test___iadd__( + agentset_polars._df["unique_id"].to_list() ) - # Test if adding the same AgentSetDF raises ValueError + # Test if adding the same AgentSet raises ValueError with pytest.raises(ValueError): agents_copy += agentset_polars1 - def test___iter__(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test___iter__(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry len_agentset0 = len(agents._agentsets[0]) len_agentset1 = len(agents._agentsets[1]) for i, agent in enumerate(agents): @@ -853,36 +841,36 @@ def test___iter__(self, fix_AgentsDF: AgentsDF): def test___isub__( self, - fix_AgentsDF: AgentsDF, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix_AgentSetRegistry: AgentSetRegistry, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - # Test with an AgentSetPolars and a DataFrame - agents = fix_AgentsDF - agents -= fix1_AgentSetPolars - assert agents._agentsets[0] == fix2_AgentSetPolars + # Test with an AgentSet and a DataFrame + agents = fix_AgentSetRegistry + agents -= fix1_AgentSet + assert agents._agentsets[0] == fix2_AgentSet assert len(agents._agentsets) == 1 def test___len__( self, - fix_AgentsDF: AgentsDF, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix_AgentSetRegistry: AgentSetRegistry, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - assert len(fix_AgentsDF) == len(fix1_AgentSetPolars) + len(fix2_AgentSetPolars) + assert len(fix_AgentSetRegistry) == len(fix1_AgentSet) + len(fix2_AgentSet) - def test___repr__(self, fix_AgentsDF: AgentsDF): - repr(fix_AgentsDF) + def test___repr__(self, fix_AgentSetRegistry: AgentSetRegistry): + repr(fix_AgentSetRegistry) - def test___reversed__(self, fix2_AgentSetPolars: AgentsDF): - agents = fix2_AgentSetPolars + def test___reversed__(self, fix2_AgentSet: AgentSetRegistry): + agents = fix2_AgentSet reversed_wealth = [] for agent in reversed(list(agents)): reversed_wealth.append(agent["wealth"]) assert reversed_wealth == list(reversed(agents["wealth"])) - def test___setitem__(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test___setitem__(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry # Test with a single attribute agents["wealth"] = 0 @@ -918,38 +906,38 @@ def test___setitem__(self, fix_AgentsDF: AgentsDF): len(agents._agentsets[1]) - 1 ) - def test___str__(self, fix_AgentsDF: AgentsDF): - str(fix_AgentsDF) + def test___str__(self, fix_AgentSetRegistry: AgentSetRegistry): + str(fix_AgentSetRegistry) def test___sub__( self, - fix_AgentsDF: AgentsDF, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix_AgentSetRegistry: AgentSetRegistry, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - # Test with an AgentSetPolars and a DataFrame - result = fix_AgentsDF - fix1_AgentSetPolars - assert isinstance(result._agentsets[0], ExampleAgentSetPolars) + # Test with an AgentSet and a DataFrame + result = fix_AgentSetRegistry - fix1_AgentSet + assert isinstance(result._agentsets[0], ExampleAgentSet) assert len(result._agentsets) == 1 def test_agents( self, - fix_AgentsDF: AgentsDF, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix_AgentSetRegistry: AgentSetRegistry, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - assert isinstance(fix_AgentsDF.df, dict) - assert len(fix_AgentsDF.df) == 2 - assert fix_AgentsDF.df[fix1_AgentSetPolars] is fix1_AgentSetPolars._df - assert fix_AgentsDF.df[fix2_AgentSetPolars] is fix2_AgentSetPolars._df + assert isinstance(fix_AgentSetRegistry.df, dict) + assert len(fix_AgentSetRegistry.df) == 2 + assert fix_AgentSetRegistry.df[fix1_AgentSet] is fix1_AgentSet._df + assert fix_AgentSetRegistry.df[fix2_AgentSet] is fix2_AgentSet._df # Test agents.setter - fix_AgentsDF.df = [fix1_AgentSetPolars, fix2_AgentSetPolars] - assert fix_AgentsDF._agentsets[0] == fix1_AgentSetPolars - assert fix_AgentsDF._agentsets[1] == fix2_AgentSetPolars + fix_AgentSetRegistry.df = [fix1_AgentSet, fix2_AgentSet] + assert fix_AgentSetRegistry._agentsets[0] == fix1_AgentSet + assert fix_AgentSetRegistry._agentsets[1] == fix2_AgentSet - def test_active_agents(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test_active_agents(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry # Test with select mask0 = ( @@ -1002,20 +990,20 @@ def test_active_agents(self, fix_AgentsDF: AgentsDF): ) ) - def test_agentsets_by_type(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test_agentsets_by_type(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry result = agents.agentsets_by_type assert isinstance(result, dict) - assert isinstance(result[ExampleAgentSetPolars], AgentsDF) + assert isinstance(result[ExampleAgentSet], AgentSetRegistry) assert ( - result[ExampleAgentSetPolars]._agentsets[0].df.rows() + result[ExampleAgentSet]._agentsets[0].df.rows() == agents._agentsets[1].df.rows() ) - def test_inactive_agents(self, fix_AgentsDF: AgentsDF): - agents = fix_AgentsDF + def test_inactive_agents(self, fix_AgentSetRegistry: AgentSetRegistry): + agents = fix_AgentSetRegistry # Test with select mask0 = ( diff --git a/tests/test_agentset.py b/tests/test_agentset.py index 0c849abe..66eca478 100644 --- a/tests/test_agentset.py +++ b/tests/test_agentset.py @@ -4,11 +4,11 @@ import pytest from numpy.random import Generator -from mesa_frames import AgentSetPolars, GridPolars, ModelDF +from mesa_frames import AgentSet, GridPolars, Model -class ExampleAgentSetPolars(AgentSetPolars): - def __init__(self, model: ModelDF): +class ExampleAgentSet(AgentSet): + def __init__(self, model: Model): super().__init__(model) self.starting_wealth = pl.Series("wealth", [1, 2, 3, 4]) @@ -19,8 +19,8 @@ def step(self) -> None: self.add_wealth(1) -class ExampleAgentSetPolarsNoWealth(AgentSetPolars): - def __init__(self, model: ModelDF): +class ExampleAgentSetNoWealth(AgentSet): + def __init__(self, model: Model): super().__init__(model) self.starting_income = pl.Series("income", [1000, 2000, 3000, 4000]) @@ -32,23 +32,23 @@ def step(self) -> None: @pytest.fixture -def fix1_AgentSetPolars() -> ExampleAgentSetPolars: - model = ModelDF() - agents = ExampleAgentSetPolars(model) +def fix1_AgentSet() -> ExampleAgentSet: + model = Model() + agents = ExampleAgentSet(model) agents["wealth"] = agents.starting_wealth agents["age"] = [10, 20, 30, 40] - model.agents.add(agents) + model.sets.add(agents) return agents @pytest.fixture -def fix2_AgentSetPolars() -> ExampleAgentSetPolars: - model = ModelDF() - agents = ExampleAgentSetPolars(model) +def fix2_AgentSet() -> ExampleAgentSet: + model = Model() + agents = ExampleAgentSet(model) agents["wealth"] = agents.starting_wealth + 10 agents["age"] = [100, 200, 300, 400] - model.agents.add(agents) + model.sets.add(agents) space = GridPolars(model, dimensions=[3, 3], capacity=2) model.space = space space.place_agents(agents=agents["unique_id"][[0, 1]], pos=[[2, 1], [1, 2]]) @@ -56,40 +56,38 @@ def fix2_AgentSetPolars() -> ExampleAgentSetPolars: @pytest.fixture -def fix3_AgentSetPolars() -> ExampleAgentSetPolars: - model = ModelDF() - agents = ExampleAgentSetPolars(model) +def fix3_AgentSet() -> ExampleAgentSet: + model = Model() + agents = ExampleAgentSet(model) agents["wealth"] = agents.starting_wealth + 7 agents["age"] = [12, 13, 14, 116] return agents @pytest.fixture -def fix1_AgentSetPolars_with_pos( - fix1_AgentSetPolars: ExampleAgentSetPolars, -) -> ExampleAgentSetPolars: - space = GridPolars(fix1_AgentSetPolars.model, dimensions=[3, 3], capacity=2) - fix1_AgentSetPolars.model.space = space - space.place_agents( - agents=fix1_AgentSetPolars["unique_id"][[0, 1]], pos=[[0, 0], [1, 1]] - ) - return fix1_AgentSetPolars +def fix1_AgentSet_with_pos( + fix1_AgentSet: ExampleAgentSet, +) -> ExampleAgentSet: + space = GridPolars(fix1_AgentSet.model, dimensions=[3, 3], capacity=2) + fix1_AgentSet.model.space = space + space.place_agents(agents=fix1_AgentSet["unique_id"][[0, 1]], pos=[[0, 0], [1, 1]]) + return fix1_AgentSet @pytest.fixture -def fix1_AgentSetPolars_no_wealth() -> ExampleAgentSetPolarsNoWealth: - model = ModelDF() - agents = ExampleAgentSetPolarsNoWealth(model) +def fix1_AgentSet_no_wealth() -> ExampleAgentSetNoWealth: + model = Model() + agents = ExampleAgentSetNoWealth(model) agents["income"] = agents.starting_income agents["age"] = [1, 2, 3, 4] - model.agents.add(agents) + model.sets.add(agents) return agents -class Test_AgentSetPolars: +class Test_AgentSet: def test__init__(self): - model = ModelDF() - agents = ExampleAgentSetPolars(model) + model = Model() + agents = ExampleAgentSet(model) agents.add({"age": [0, 1, 2, 3]}) assert agents.model == model assert isinstance(agents.df, pl.DataFrame) @@ -100,9 +98,9 @@ def test__init__(self): def test_add( self, - fix1_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, ): - agents = fix1_AgentSetPolars + agents = fix1_AgentSet # Test with a pl.Dataframe result = agents.add( @@ -140,14 +138,14 @@ def test_add( agents.add([10, 20, 30]) # Three values but agents has 2 columns # Test adding sequence to empty AgentSet - should raise ValueError - empty_agents = ExampleAgentSetPolars(agents.model) + empty_agents = ExampleAgentSet(agents.model) with pytest.raises( ValueError, match="Cannot add a sequence to an empty AgentSet" ): empty_agents.add([1, 2]) # Should raise error for empty AgentSet - def test_contains(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_contains(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet # Test with a single value assert agents.contains(agents["unique_id"][0]) @@ -161,8 +159,8 @@ def test_contains(self, fix1_AgentSetPolars: ExampleAgentSetPolars): result = agents.contains(unique_ids[:2]) assert all(result == [True, True]) - def test_copy(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_copy(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet agents.test_list = [[1, 2, 3]] # Test with deep=False @@ -171,12 +169,12 @@ def test_copy(self, fix1_AgentSetPolars: ExampleAgentSetPolars): assert agents.test_list[0][-1] == agents2.test_list[0][-1] # Test with deep=True - agents2 = fix1_AgentSetPolars.copy(deep=True) + agents2 = fix1_AgentSet.copy(deep=True) agents2.test_list[0].append(4) assert agents.test_list[-1] != agents2.test_list[-1] - def test_discard(self, fix1_AgentSetPolars_with_pos: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars_with_pos + def test_discard(self, fix1_AgentSet_with_pos: ExampleAgentSet): + agents = fix1_AgentSet_with_pos # Test with a single value result = agents.discard(agents["unique_id"][0], inplace=False) @@ -214,8 +212,8 @@ def test_discard(self, fix1_AgentSetPolars_with_pos: ExampleAgentSetPolars): result = agents.discard([], inplace=False) assert all(result.df["unique_id"] == agents["unique_id"]) - def test_do(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_do(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet # Test with no return_results, no mask agents.do("add_wealth", 1) @@ -229,8 +227,8 @@ def test_do(self, fix1_AgentSetPolars: ExampleAgentSetPolars): agents.do("add_wealth", 1, mask=agents["wealth"] > 3) assert agents.df["wealth"].to_list() == [3, 5, 6, 7] - def test_get(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_get(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet # Test with a single attribute assert agents.get("wealth").to_list() == [1, 2, 3, 4] @@ -245,16 +243,16 @@ def test_get(self, fix1_AgentSetPolars: ExampleAgentSetPolars): selected = agents.select(agents.df["wealth"] > 1, inplace=False) assert selected.get("wealth", mask="active").to_list() == [2, 3, 4] - def test_remove(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_remove(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet remaining_agents_id = agents["unique_id"][2, 3] agents.remove(agents["unique_id"][0, 1]) assert all(agents.df["unique_id"] == remaining_agents_id) with pytest.raises(KeyError): agents.remove([0]) - def test_select(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_select(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet # Test with default arguments. Should select all agents selected = agents.select(inplace=False) @@ -278,7 +276,7 @@ def test_select(self, fix1_AgentSetPolars: ExampleAgentSetPolars): assert all(selected.active_agents["unique_id"] == agents["unique_id"][0, 1]) # Test with filter_func - def filter_func(agentset: AgentSetPolars) -> pl.Series: + def filter_func(agentset: AgentSet) -> pl.Series: return agentset.df["wealth"] > 1 selected = agents.select(filter_func=filter_func, inplace=False) @@ -296,8 +294,8 @@ def filter_func(agentset: AgentSetPolars) -> pl.Series: for id in agents["unique_id"][2, 3] ) - def test_set(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_set(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet # Test with a single attribute result = agents.set("wealth", 0, inplace=False) @@ -322,8 +320,8 @@ def test_set(self, fix1_AgentSetPolars: ExampleAgentSetPolars): result = agents.set("wealth", [100, 200, 300, 400], inplace=False) assert result.df["wealth"].to_list() == [100, 200, 300, 400] - def test_shuffle(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_shuffle(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet for _ in range(10): original_order = agents.df["unique_id"].to_list() agents.shuffle() @@ -331,41 +329,41 @@ def test_shuffle(self, fix1_AgentSetPolars: ExampleAgentSetPolars): return assert False - def test_sort(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_sort(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet agents.sort("wealth", ascending=False) assert agents.df["wealth"].to_list() == [4, 3, 2, 1] def test__add__( self, - fix1_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, ): - agents = fix1_AgentSetPolars + agents = fix1_AgentSet - # Test with an AgentSetPolars and a DataFrame + # Test with an AgentSet and a DataFrame agents3 = agents + pl.DataFrame({"wealth": [5, 6], "age": [50, 60]}) assert agents3.df["wealth"].to_list() == [1, 2, 3, 4, 5, 6] assert agents3.df["age"].to_list() == [10, 20, 30, 40, 50, 60] - # Test with an AgentSetPolars and a list (Sequence[Any]) + # Test with an AgentSet and a list (Sequence[Any]) agents3 = agents + [5, 5] # unique_id, wealth, age assert all(agents3.df["unique_id"].to_list()[:-1] == agents["unique_id"]) assert len(agents3.df) == 5 assert agents3.df["wealth"].to_list() == [1, 2, 3, 4, 5] assert agents3.df["age"].to_list() == [10, 20, 30, 40, 5] - # Test with an AgentSetPolars and a dict + # Test with an AgentSet and a dict agents3 = agents + {"age": 10, "wealth": 5} assert agents3.df["wealth"].to_list() == [1, 2, 3, 4, 5] - def test__contains__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): + def test__contains__(self, fix1_AgentSet: ExampleAgentSet): # Test with a single value - agents = fix1_AgentSetPolars + agents = fix1_AgentSet assert agents["unique_id"][0] in agents assert 0 not in agents - def test__copy__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test__copy__(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet agents.test_list = [[1, 2, 3]] # Test with deep=False @@ -373,21 +371,21 @@ def test__copy__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): agents2.test_list[0].append(4) assert agents.test_list[0][-1] == agents2.test_list[0][-1] - def test__deepcopy__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test__deepcopy__(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet agents.test_list = [[1, 2, 3]] agents2 = deepcopy(agents) agents2.test_list[0].append(4) assert agents.test_list[-1] != agents2.test_list[-1] - def test__getattr__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars - assert isinstance(agents.model, ModelDF) + def test__getattr__(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet + assert isinstance(agents.model, Model) assert agents.wealth.to_list() == [1, 2, 3, 4] - def test__getitem__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test__getitem__(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet # Testing with a string assert agents["wealth"].to_list() == [1, 2, 3, 4] @@ -405,59 +403,58 @@ def test__getitem__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): def test__iadd__( self, - fix1_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, ): - # Test with an AgentSetPolars and a DataFrame - agents = deepcopy(fix1_AgentSetPolars) + # Test with an AgentSet and a DataFrame + agents = deepcopy(fix1_AgentSet) agents += pl.DataFrame({"wealth": [5, 6], "age": [50, 60]}) assert agents.df["wealth"].to_list() == [1, 2, 3, 4, 5, 6] assert agents.df["age"].to_list() == [10, 20, 30, 40, 50, 60] - # Test with an AgentSetPolars and a list - agents = deepcopy(fix1_AgentSetPolars) + # Test with an AgentSet and a list + agents = deepcopy(fix1_AgentSet) agents += [5, 5] # unique_id, wealth, age assert all( - agents["unique_id"].to_list()[:-1] - == fix1_AgentSetPolars["unique_id"][0, 1, 2, 3] + agents["unique_id"].to_list()[:-1] == fix1_AgentSet["unique_id"][0, 1, 2, 3] ) assert len(agents.df) == 5 assert agents.df["wealth"].to_list() == [1, 2, 3, 4, 5] assert agents.df["age"].to_list() == [10, 20, 30, 40, 5] - # Test with an AgentSetPolars and a dict - agents = deepcopy(fix1_AgentSetPolars) + # Test with an AgentSet and a dict + agents = deepcopy(fix1_AgentSet) agents += {"age": 10, "wealth": 5} assert agents.df["wealth"].to_list() == [1, 2, 3, 4, 5] - def test__iter__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test__iter__(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet for i, agent in enumerate(agents): assert isinstance(agent, dict) assert agent["wealth"] == i + 1 - def test__isub__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - # Test with an AgentSetPolars and a DataFrame - agents = deepcopy(fix1_AgentSetPolars) + def test__isub__(self, fix1_AgentSet: ExampleAgentSet): + # Test with an AgentSet and a DataFrame + agents = deepcopy(fix1_AgentSet) agents -= agents.df assert agents.df.is_empty() - def test__len__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test__len__(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet assert len(agents) == 4 - def test__repr__(self, fix1_AgentSetPolars): - agents: ExampleAgentSetPolars = fix1_AgentSetPolars + def test__repr__(self, fix1_AgentSet): + agents: ExampleAgentSet = fix1_AgentSet repr(agents) - def test__reversed__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test__reversed__(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet reversed_wealth = [] for i, agent in reversed(list(enumerate(agents))): reversed_wealth.append(agent["wealth"]) assert reversed_wealth == [4, 3, 2, 1] - def test__setitem__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test__setitem__(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet agents = deepcopy(agents) # To test passing through a df later @@ -479,36 +476,36 @@ def test__setitem__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): assert agents.df.item(0, "wealth") == 9 assert agents.df.item(0, "age") == 99 - def test__str__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents: ExampleAgentSetPolars = fix1_AgentSetPolars + def test__str__(self, fix1_AgentSet: ExampleAgentSet): + agents: ExampleAgentSet = fix1_AgentSet str(agents) - def test__sub__(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents: ExampleAgentSetPolars = fix1_AgentSetPolars - agents2: ExampleAgentSetPolars = agents - agents.df + def test__sub__(self, fix1_AgentSet: ExampleAgentSet): + agents: ExampleAgentSet = fix1_AgentSet + agents2: ExampleAgentSet = agents - agents.df assert agents2.df.is_empty() assert agents.df["wealth"].to_list() == [1, 2, 3, 4] - def test_get_obj(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_get_obj(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet assert agents._get_obj(inplace=True) is agents assert agents._get_obj(inplace=False) is not agents def test_agents( self, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): - agents = fix1_AgentSetPolars - agents2 = fix2_AgentSetPolars + agents = fix1_AgentSet + agents2 = fix2_AgentSet assert isinstance(agents.df, pl.DataFrame) # Test agents.setter agents.df = agents2.df assert all(agents["unique_id"] == agents2["unique_id"][0, 1, 2, 3]) - def test_active_agents(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_active_agents(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet # Test with select agents.select(agents.df["wealth"] > 2, inplace=True) @@ -518,18 +515,16 @@ def test_active_agents(self, fix1_AgentSetPolars: ExampleAgentSetPolars): agents.active_agents = agents.df["wealth"] > 2 assert all(agents.active_agents["unique_id"] == agents["unique_id"][2, 3]) - def test_inactive_agents(self, fix1_AgentSetPolars: ExampleAgentSetPolars): - agents = fix1_AgentSetPolars + def test_inactive_agents(self, fix1_AgentSet: ExampleAgentSet): + agents = fix1_AgentSet agents.select(agents.df["wealth"] > 2, inplace=True) assert all(agents.inactive_agents["unique_id"] == agents["unique_id"][0, 1]) - def test_pos(self, fix1_AgentSetPolars_with_pos: ExampleAgentSetPolars): - pos = fix1_AgentSetPolars_with_pos.pos + def test_pos(self, fix1_AgentSet_with_pos: ExampleAgentSet): + pos = fix1_AgentSet_with_pos.pos assert isinstance(pos, pl.DataFrame) - assert all( - pos["unique_id"] == fix1_AgentSetPolars_with_pos["unique_id"][0, 1, 2, 3] - ) + assert all(pos["unique_id"] == fix1_AgentSet_with_pos["unique_id"][0, 1, 2, 3]) assert pos.columns == ["unique_id", "dim_0", "dim_1"] assert pos["dim_0"].to_list() == [0, 1, None, None] assert pos["dim_1"].to_list() == [0, 1, None, None] diff --git a/tests/test_datacollector.py b/tests/test_datacollector.py index beb96632..8141f749 100644 --- a/tests/test_datacollector.py +++ b/tests/test_datacollector.py @@ -1,5 +1,5 @@ from mesa_frames.concrete.datacollector import DataCollector -from mesa_frames import ModelDF, AgentSetPolars, AgentsDF +from mesa_frames import Model, AgentSet, AgentSetRegistry import pytest import polars as pl import beartype @@ -12,8 +12,8 @@ def custom_trigger(model): return model._steps % 2 == 0 -class ExampleAgentSet1(AgentSetPolars): - def __init__(self, model: ModelDF): +class ExampleAgentSet1(AgentSet): + def __init__(self, model: Model): super().__init__(model) self["wealth"] = pl.Series("wealth", [1, 2, 3, 4]) self["age"] = pl.Series("age", [10, 20, 30, 40]) @@ -25,8 +25,8 @@ def step(self) -> None: self.add_wealth(1) -class ExampleAgentSet2(AgentSetPolars): - def __init__(self, model: ModelDF): +class ExampleAgentSet2(AgentSet): + def __init__(self, model: Model): super().__init__(model) self["wealth"] = pl.Series("wealth", [10, 20, 30, 40]) self["age"] = pl.Series("age", [11, 22, 33, 44]) @@ -38,8 +38,8 @@ def step(self) -> None: self.add_wealth(2) -class ExampleAgentSet3(AgentSetPolars): - def __init__(self, model: ModelDF): +class ExampleAgentSet3(AgentSet): + def __init__(self, model: Model): super().__init__(model) self["age"] = pl.Series("age", [1, 2, 3, 4]) self["wealth"] = pl.Series("wealth", [1, 2, 3, 4]) @@ -51,13 +51,13 @@ def step(self) -> None: self.age_agents(1) -class ExampleModel(ModelDF): - def __init__(self, agents: AgentsDF): +class ExampleModel(Model): + def __init__(self, agents: AgentSetRegistry): super().__init__() - self.agents = agents + self.sets = agents def step(self): - self.agents.do("step") + self.sets.do("step") def run_model(self, n): for _ in range(n): @@ -74,14 +74,14 @@ def run_model_with_conditional_collect(self, n): self.dc.conditional_collect() -class ExampleModelWithMultipleCollects(ModelDF): - def __init__(self, agents: AgentsDF): +class ExampleModelWithMultipleCollects(Model): + def __init__(self, agents: AgentSetRegistry): super().__init__() - self.agents = agents + self.sets = agents def step(self): self.dc.conditional_collect() - self.agents.do("step") + self.sets.do("step") self.dc.conditional_collect() def run_model_with_conditional_collect_multiple_batch(self, n): @@ -95,40 +95,40 @@ def postgres_uri(): @pytest.fixture -def fix1_AgentSetPolars() -> ExampleAgentSet1: - return ExampleAgentSet1(ModelDF()) +def fix1_AgentSet() -> ExampleAgentSet1: + return ExampleAgentSet1(Model()) @pytest.fixture -def fix2_AgentSetPolars() -> ExampleAgentSet2: - return ExampleAgentSet2(ModelDF()) +def fix2_AgentSet() -> ExampleAgentSet2: + return ExampleAgentSet2(Model()) @pytest.fixture -def fix3_AgentSetPolars() -> ExampleAgentSet3: - return ExampleAgentSet3(ModelDF()) +def fix3_AgentSet() -> ExampleAgentSet3: + return ExampleAgentSet3(Model()) @pytest.fixture -def fix_AgentsDF( - fix1_AgentSetPolars: ExampleAgentSet1, - fix2_AgentSetPolars: ExampleAgentSet2, - fix3_AgentSetPolars: ExampleAgentSet3, -) -> AgentsDF: - model = ModelDF() - agents = AgentsDF(model) - agents.add([fix1_AgentSetPolars, fix2_AgentSetPolars, fix3_AgentSetPolars]) +def fix_AgentSetRegistry( + fix1_AgentSet: ExampleAgentSet1, + fix2_AgentSet: ExampleAgentSet2, + fix3_AgentSet: ExampleAgentSet3, +) -> AgentSetRegistry: + model = Model() + agents = AgentSetRegistry(model) + agents.add([fix1_AgentSet, fix2_AgentSet, fix3_AgentSet]) return agents @pytest.fixture -def fix1_model(fix_AgentsDF: AgentsDF) -> ExampleModel: - return ExampleModel(fix_AgentsDF) +def fix1_model(fix_AgentSetRegistry: AgentSetRegistry) -> ExampleModel: + return ExampleModel(fix_AgentSetRegistry) @pytest.fixture -def fix2_model(fix_AgentsDF: AgentsDF) -> ExampleModel: - return ExampleModelWithMultipleCollects(fix_AgentsDF) +def fix2_model(fix_AgentSetRegistry: AgentSetRegistry) -> ExampleModel: + return ExampleModelWithMultipleCollects(fix_AgentSetRegistry) class TestDataCollector: @@ -160,11 +160,11 @@ def test_collect(self, fix1_model): model=model, model_reporters={ "total_agents": lambda model: sum( - len(agentset) for agentset in model.agents._agentsets + len(agentset) for agentset in model.sets._agentsets ) }, agent_reporters={ - "wealth": lambda model: model.agents._agentsets[0]["wealth"], + "wealth": lambda model: model.sets._agentsets[0]["wealth"], "age": "age", }, ) @@ -219,11 +219,11 @@ def test_collect_step(self, fix1_model): model=model, model_reporters={ "total_agents": lambda model: sum( - len(agentset) for agentset in model.agents._agentsets + len(agentset) for agentset in model.sets._agentsets ) }, agent_reporters={ - "wealth": lambda model: model.agents._agentsets[0]["wealth"], + "wealth": lambda model: model.sets._agentsets[0]["wealth"], "age": "age", }, ) @@ -275,11 +275,11 @@ def test_conditional_collect(self, fix1_model): trigger=custom_trigger, model_reporters={ "total_agents": lambda model: sum( - len(agentset) for agentset in model.agents._agentsets + len(agentset) for agentset in model.sets._agentsets ) }, agent_reporters={ - "wealth": lambda model: model.agents._agentsets[0]["wealth"], + "wealth": lambda model: model.sets._agentsets[0]["wealth"], "age": "age", }, ) @@ -357,11 +357,11 @@ def test_flush_local_csv(self, fix1_model): trigger=custom_trigger, model_reporters={ "total_agents": lambda model: sum( - len(agentset) for agentset in model.agents._agentsets + len(agentset) for agentset in model.sets._agentsets ) }, agent_reporters={ - "wealth": lambda model: model.agents._agentsets[0]["wealth"], + "wealth": lambda model: model.sets._agentsets[0]["wealth"], "age": "age", }, storage="csv", @@ -433,11 +433,11 @@ def test_flush_local_parquet(self, fix1_model): trigger=custom_trigger, model_reporters={ "total_agents": lambda model: sum( - len(agentset) for agentset in model.agents._agentsets + len(agentset) for agentset in model.sets._agentsets ) }, agent_reporters={ - "wealth": lambda model: model.agents._agentsets[0]["wealth"], + "wealth": lambda model: model.sets._agentsets[0]["wealth"], }, storage="parquet", storage_uri=tmpdir, @@ -509,11 +509,11 @@ def test_postgress(self, fix1_model, postgres_uri): trigger=custom_trigger, model_reporters={ "total_agents": lambda model: sum( - len(agentset) for agentset in model.agents._agentsets + len(agentset) for agentset in model.sets._agentsets ) }, agent_reporters={ - "wealth": lambda model: model.agents._agentsets[0]["wealth"], + "wealth": lambda model: model.sets._agentsets[0]["wealth"], "age": "age", }, storage="postgresql", @@ -558,11 +558,11 @@ def test_batch_memory(self, fix2_model): trigger=custom_trigger, model_reporters={ "total_agents": lambda model: sum( - len(agentset) for agentset in model.agents._agentsets + len(agentset) for agentset in model.sets._agentsets ) }, agent_reporters={ - "wealth": lambda model: model.agents._agentsets[0]["wealth"], + "wealth": lambda model: model.sets._agentsets[0]["wealth"], "age": "age", }, ) @@ -703,11 +703,11 @@ def test_batch_save(self, fix2_model): trigger=custom_trigger, model_reporters={ "total_agents": lambda model: sum( - len(agentset) for agentset in model.agents._agentsets + len(agentset) for agentset in model.sets._agentsets ) }, agent_reporters={ - "wealth": lambda model: model.agents._agentsets[0]["wealth"], + "wealth": lambda model: model.sets._agentsets[0]["wealth"], "age": "age", }, storage="csv", diff --git a/tests/test_grid.py b/tests/test_grid.py index 5d8cafa6..2fe17aea 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -3,18 +3,18 @@ import pytest from polars.testing import assert_frame_equal -from mesa_frames import GridPolars, ModelDF +from mesa_frames import GridPolars, Model from tests.test_agentset import ( - ExampleAgentSetPolars, - fix1_AgentSetPolars, - fix2_AgentSetPolars, + ExampleAgentSet, + fix1_AgentSet, + fix2_AgentSet, ) -def get_unique_ids(model: ModelDF) -> pl.Series: - # return model.get_agents_of_type(model.agent_types[0])["unique_id"] +def get_unique_ids(model: Model) -> pl.Series: + # return model.get_sets_of_type(model.set_types[0])["unique_id"] series_list = [ - agent_set["unique_id"].cast(pl.UInt64) for agent_set in model.agents.df.values() + agent_set["unique_id"].cast(pl.UInt64) for agent_set in model.sets.df.values() ] return pl.concat(series_list) @@ -23,15 +23,15 @@ class TestGridPolars: @pytest.fixture def model( self, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, - ) -> ModelDF: - model = ModelDF() - model.agents.add([fix1_AgentSetPolars, fix2_AgentSetPolars]) + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, + ) -> Model: + model = Model() + model.sets.add([fix1_AgentSet, fix2_AgentSet]) return model @pytest.fixture - def grid_moore(self, model: ModelDF) -> GridPolars: + def grid_moore(self, model: Model) -> GridPolars: space = GridPolars(model, dimensions=[3, 3], capacity=2) unique_ids = get_unique_ids(model) space.place_agents(agents=unique_ids[[0, 1]], pos=[[0, 0], [1, 1]]) @@ -41,7 +41,7 @@ def grid_moore(self, model: ModelDF) -> GridPolars: return space @pytest.fixture - def grid_moore_torus(self, model: ModelDF) -> GridPolars: + def grid_moore_torus(self, model: Model) -> GridPolars: space = GridPolars(model, dimensions=[3, 3], capacity=2, torus=True) unique_ids = get_unique_ids(model) space.place_agents(agents=unique_ids[[0, 1]], pos=[[0, 0], [1, 1]]) @@ -51,20 +51,20 @@ def grid_moore_torus(self, model: ModelDF) -> GridPolars: return space @pytest.fixture - def grid_von_neumann(self, model: ModelDF) -> GridPolars: + def grid_von_neumann(self, model: Model) -> GridPolars: space = GridPolars(model, dimensions=[3, 3], neighborhood_type="von_neumann") unique_ids = get_unique_ids(model) space.place_agents(agents=unique_ids[[0, 1]], pos=[[0, 0], [1, 1]]) return space @pytest.fixture - def grid_hexagonal(self, model: ModelDF) -> GridPolars: + def grid_hexagonal(self, model: Model) -> GridPolars: space = GridPolars(model, dimensions=[10, 10], neighborhood_type="hexagonal") unique_ids = get_unique_ids(model) space.place_agents(agents=unique_ids[[0, 1]], pos=[[0, 0], [1, 1]]) return space - def test___init__(self, model: ModelDF): + def test___init__(self, model: Model): # Test with default parameters grid1 = GridPolars(model, dimensions=[3, 3]) assert isinstance(grid1, GridPolars) @@ -133,8 +133,8 @@ def test_get_cells(self, grid_moore: GridPolars): def test_get_directions( self, grid_moore: GridPolars, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): unique_ids = get_unique_ids(grid_moore.model) # Test with GridCoordinate @@ -151,12 +151,10 @@ def test_get_directions( # Test with missing agents (raises ValueError) with pytest.raises(ValueError): - grid_moore.get_directions( - agents0=fix1_AgentSetPolars, agents1=fix2_AgentSetPolars - ) + grid_moore.get_directions(agents0=fix1_AgentSet, agents1=fix2_AgentSet) # Test with IdsLike - grid_moore.place_agents(fix2_AgentSetPolars, [[0, 1], [0, 2], [1, 0], [1, 2]]) + grid_moore.place_agents(fix2_AgentSet, [[0, 1], [0, 2], [1, 0], [1, 2]]) assert_frame_equal( grid_moore.agents, pl.DataFrame( @@ -177,18 +175,16 @@ def test_get_directions( assert dir.select(pl.col("dim_0")).to_series().to_list() == [0, -1] assert dir.select(pl.col("dim_1")).to_series().to_list() == [1, 1] - # Test with two AgentSetDFs + # Test with two AgentSets grid_moore.place_agents(unique_ids[[2, 3]], [[1, 1], [2, 2]]) - dir = grid_moore.get_directions( - agents0=fix1_AgentSetPolars, agents1=fix2_AgentSetPolars - ) + dir = grid_moore.get_directions(agents0=fix1_AgentSet, agents1=fix2_AgentSet) assert isinstance(dir, pl.DataFrame) assert dir.select(pl.col("dim_0")).to_series().to_list() == [0, -1, 0, -1] assert dir.select(pl.col("dim_1")).to_series().to_list() == [1, 1, -1, 0] - # Test with AgentsDF + # Test with AgentSetRegistry dir = grid_moore.get_directions( - agents0=grid_moore.model.agents, agents1=grid_moore.model.agents + agents0=grid_moore.model.sets, agents1=grid_moore.model.sets ) assert isinstance(dir, pl.DataFrame) assert grid_moore._df_all(dir == 0).all() @@ -216,8 +212,8 @@ def test_get_directions( def test_get_distances( self, grid_moore: GridPolars, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): # Test with GridCoordinate dist = grid_moore.get_distances(pos0=[1, 1], pos1=[2, 2]) @@ -236,12 +232,10 @@ def test_get_distances( # Test with missing agents (raises ValueError) with pytest.raises(ValueError): - grid_moore.get_distances( - agents0=fix1_AgentSetPolars, agents1=fix2_AgentSetPolars - ) + grid_moore.get_distances(agents0=fix1_AgentSet, agents1=fix2_AgentSet) # Test with IdsLike - grid_moore.place_agents(fix2_AgentSetPolars, [[0, 1], [0, 2], [1, 0], [1, 2]]) + grid_moore.place_agents(fix2_AgentSet, [[0, 1], [0, 2], [1, 0], [1, 2]]) unique_ids = get_unique_ids(grid_moore.model) dist = grid_moore.get_distances( agents0=unique_ids[[0, 1]], agents1=unique_ids[[4, 5]] @@ -251,20 +245,18 @@ def test_get_distances( dist.select(pl.col("distance")).to_series().to_list(), [1.0, np.sqrt(2)] ) - # Test with two AgentSetDFs + # Test with two AgentSets grid_moore.place_agents(unique_ids[[2, 3]], [[1, 1], [2, 2]]) - dist = grid_moore.get_distances( - agents0=fix1_AgentSetPolars, agents1=fix2_AgentSetPolars - ) + dist = grid_moore.get_distances(agents0=fix1_AgentSet, agents1=fix2_AgentSet) assert isinstance(dist, pl.DataFrame) assert np.allclose( dist.select(pl.col("distance")).to_series().to_list(), [1.0, np.sqrt(2), 1.0, 1.0], ) - # Test with AgentsDF + # Test with AgentSetRegistry dist = grid_moore.get_distances( - agents0=grid_moore.model.agents, agents1=grid_moore.model.agents + agents0=grid_moore.model.sets, agents1=grid_moore.model.sets ) assert grid_moore._df_all(dist == 0).all() @@ -621,7 +613,7 @@ def test_get_neighborhood( def test_get_neighbors( self, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix2_AgentSet: ExampleAgentSet, grid_moore: GridPolars, grid_hexagonal: GridPolars, grid_von_neumann: GridPolars, @@ -798,8 +790,8 @@ def test_is_full(self, grid_moore: GridPolars): def test_move_agents( self, grid_moore: GridPolars, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): # Test with IdsLike unique_ids = get_unique_ids(grid_moore.model) @@ -813,10 +805,10 @@ def test_move_agents( check_row_order=False, ) - # Test with AgentSetDF + # Test with AgentSet with pytest.warns(RuntimeWarning): space = grid_moore.move_agents( - agents=fix2_AgentSetPolars, + agents=fix2_AgentSet, pos=[[0, 0], [1, 0], [2, 0], [0, 1]], inplace=False, ) @@ -833,10 +825,10 @@ def test_move_agents( check_row_order=False, ) - # Test with Collection[AgentSetDF] + # Test with Collection[AgentSet] with pytest.warns(RuntimeWarning): space = grid_moore.move_agents( - agents=[fix1_AgentSetPolars, fix2_AgentSetPolars], + agents=[fix1_AgentSet, fix2_AgentSet], pos=[[0, 2], [1, 2], [2, 2], [0, 1], [1, 1], [2, 1], [0, 0], [1, 0]], inplace=False, ) @@ -859,7 +851,7 @@ def test_move_agents( agents=unique_ids[[0, 1]], pos=[[0, 0], [1, 1], [2, 2]], inplace=False ) - # Test with AgentsDF, pos=DataFrame + # Test with AgentSetRegistry, pos=DataFrame pos = pl.DataFrame( { "dim_0": [0, 1, 2, 0, 1, 2, 0, 1], @@ -869,7 +861,7 @@ def test_move_agents( with pytest.warns(RuntimeWarning): space = grid_moore.move_agents( - agents=grid_moore.model.agents, + agents=grid_moore.model.sets, pos=pos, inplace=False, ) @@ -939,12 +931,12 @@ def test_move_to_available(self, grid_moore: GridPolars): last = space.agents.select(pl.col("dim_0", "dim_1")).to_numpy() assert different - # Test with AgentSetDF + # Test with AgentSet last = None different = False for _ in range(10): available_cells = grid_moore.available_cells - space = grid_moore.move_to_available(grid_moore.model.agents, inplace=False) + space = grid_moore.move_to_available(grid_moore.model.sets, inplace=False) if last is not None and not different: if (space.agents.select(pl.col("dim_0")).to_numpy() != last).any(): different = True @@ -999,12 +991,12 @@ def test_move_to_empty(self, grid_moore: GridPolars): last = space.agents.select(pl.col("dim_0", "dim_1")).to_numpy() assert different - # Test with AgentSetDF + # Test with AgentSet last = None different = False for _ in range(10): empty_cells = grid_moore.empty_cells - space = grid_moore.move_to_empty(grid_moore.model.agents, inplace=False) + space = grid_moore.move_to_empty(grid_moore.model.sets, inplace=False) if last is not None and not different: if (space.agents.select(pl.col("dim_0")).to_numpy() != last).any(): different = True @@ -1037,8 +1029,8 @@ def test_out_of_bounds(self, grid_moore: GridPolars): def test_place_agents( self, grid_moore: GridPolars, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): # Test with IdsLike unique_ids = get_unique_ids(grid_moore.model) @@ -1069,9 +1061,9 @@ def test_place_agents( inplace=False, ) - # Test with AgentSetDF + # Test with AgentSet space = grid_moore.place_agents( - agents=fix2_AgentSetPolars, + agents=fix2_AgentSet, pos=[[0, 0], [1, 0], [2, 0], [0, 1]], inplace=False, ) @@ -1113,10 +1105,10 @@ def test_place_agents( check_row_order=False, ) - # Test with Collection[AgentSetDF] + # Test with Collection[AgentSet] with pytest.warns(RuntimeWarning): space = grid_moore.place_agents( - agents=[fix1_AgentSetPolars, fix2_AgentSetPolars], + agents=[fix1_AgentSet, fix2_AgentSet], pos=[[0, 2], [1, 2], [2, 2], [0, 1], [1, 1], [2, 1], [0, 0], [1, 0]], inplace=False, ) @@ -1163,7 +1155,7 @@ def test_place_agents( check_row_order=False, ) - # Test with AgentsDF, pos=DataFrame + # Test with AgentSetRegistry, pos=DataFrame pos = pl.DataFrame( { "dim_0": [0, 1, 2, 0, 1, 2, 0, 1], @@ -1172,7 +1164,7 @@ def test_place_agents( ) with pytest.warns(RuntimeWarning): space = grid_moore.place_agents( - agents=grid_moore.model.agents, + agents=grid_moore.model.sets, pos=pos, inplace=False, ) @@ -1274,14 +1266,12 @@ def test_place_to_available(self, grid_moore: GridPolars): last = space.agents.select(pl.col("dim_0", "dim_1")).to_numpy() assert different - # Test with AgentSetDF + # Test with AgentSet last = None different = False for _ in range(10): available_cells = grid_moore.available_cells - space = grid_moore.place_to_available( - grid_moore.model.agents, inplace=False - ) + space = grid_moore.place_to_available(grid_moore.model.sets, inplace=False) if last is not None and not different: if (space.agents.select(pl.col("dim_0")).to_numpy() != last).any(): different = True @@ -1336,12 +1326,12 @@ def test_place_to_empty(self, grid_moore: GridPolars): last = space.agents.select(pl.col("dim_0", "dim_1")).to_numpy() assert different - # Test with AgentSetDF + # Test with AgentSet last = None different = False for _ in range(10): empty_cells = grid_moore.empty_cells - space = grid_moore.place_to_empty(grid_moore.model.agents, inplace=False) + space = grid_moore.place_to_empty(grid_moore.model.sets, inplace=False) if last is not None and not different: if (space.agents.select(pl.col("dim_0")).to_numpy() != last).any(): different = True @@ -1389,8 +1379,8 @@ def test_random_pos(self, grid_moore: GridPolars): def test_remove_agents( self, grid_moore: GridPolars, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): unique_ids = get_unique_ids(grid_moore.model) grid_moore.move_agents( @@ -1416,11 +1406,11 @@ def test_remove_agents( ].to_list() ) assert [ - x for id in space.model.agents.index.values() for x in id.to_list() + x for id in space.model.sets.index.values() for x in id.to_list() ] == unique_ids[:8].to_list() - # Test with AgentSetDF - space = grid_moore.remove_agents(fix1_AgentSetPolars, inplace=False) + # Test with AgentSet + space = grid_moore.remove_agents(fix1_AgentSet, inplace=False) assert space.agents.shape == (4, 3) assert space.remaining_capacity == capacity + 4 assert ( @@ -1435,24 +1425,22 @@ def test_remove_agents( ].to_list() ) assert [ - x for id in space.model.agents.index.values() for x in id.to_list() + x for id in space.model.sets.index.values() for x in id.to_list() ] == unique_ids[:8].to_list() - # Test with Collection[AgentSetDF] - space = grid_moore.remove_agents( - [fix1_AgentSetPolars, fix2_AgentSetPolars], inplace=False - ) + # Test with Collection[AgentSet] + space = grid_moore.remove_agents([fix1_AgentSet, fix2_AgentSet], inplace=False) assert [ - x for id in space.model.agents.index.values() for x in id.to_list() + x for id in space.model.sets.index.values() for x in id.to_list() ] == unique_ids[:8].to_list() assert space.agents.is_empty() assert space.remaining_capacity == capacity + 8 - # Test with AgentsDF - space = grid_moore.remove_agents(grid_moore.model.agents, inplace=False) + # Test with AgentSetRegistry + space = grid_moore.remove_agents(grid_moore.model.sets, inplace=False) assert space.remaining_capacity == capacity + 8 assert space.agents.is_empty() assert [ - x for id in space.model.agents.index.values() for x in id.to_list() + x for id in space.model.sets.index.values() for x in id.to_list() ] == unique_ids[:8].to_list() def test_sample_cells(self, grid_moore: GridPolars): @@ -1532,7 +1520,7 @@ def test_sample_cells(self, grid_moore: GridPolars): with pytest.raises(AssertionError): grid_moore.sample_cells(3, cell_type="full", with_replacement=False) - def test_set_cells(self, model: ModelDF): + def test_set_cells(self, model: Model): # Initialize GridPolars grid_moore = GridPolars(model, dimensions=[3, 3], capacity=2) @@ -1584,8 +1572,8 @@ def test_set_cells(self, model: ModelDF): def test_swap_agents( self, grid_moore: GridPolars, - fix1_AgentSetPolars: ExampleAgentSetPolars, - fix2_AgentSetPolars: ExampleAgentSetPolars, + fix1_AgentSet: ExampleAgentSet, + fix2_AgentSet: ExampleAgentSet, ): unique_ids = get_unique_ids(grid_moore.model) grid_moore.move_agents( @@ -1612,10 +1600,8 @@ def test_swap_agents( space.agents.filter(pl.col("agent_id") == unique_ids[3]).row(0)[1:] == grid_moore.agents.filter(pl.col("agent_id") == unique_ids[1]).row(0)[1:] ) - # Test with AgentSetDFs - space = grid_moore.swap_agents( - fix1_AgentSetPolars, fix2_AgentSetPolars, inplace=False - ) + # Test with AgentSets + space = grid_moore.swap_agents(fix1_AgentSet, fix2_AgentSet, inplace=False) assert ( space.agents.filter(pl.col("agent_id") == unique_ids[0]).row(0)[1:] == grid_moore.agents.filter(pl.col("agent_id") == unique_ids[4]).row(0)[1:] @@ -1765,7 +1751,7 @@ def test_full_cells(self, grid_moore: GridPolars): ) ).all() - def test_model(self, grid_moore: GridPolars, model: ModelDF): + def test_model(self, grid_moore: GridPolars, model: Model): assert grid_moore.model == model def test_neighborhood_type( @@ -1784,7 +1770,7 @@ def test_random(self, grid_moore: GridPolars): def test_remaining_capacity(self, grid_moore: GridPolars): assert grid_moore.remaining_capacity == (3 * 3 * 2 - 2) - def test_torus(self, model: ModelDF, grid_moore: GridPolars): + def test_torus(self, model: Model, grid_moore: GridPolars): assert not grid_moore.torus grid_2 = GridPolars(model, [3, 3], torus=True) diff --git a/tests/test_modeldf.py b/tests/test_modeldf.py index afc45405..82ff430d 100644 --- a/tests/test_modeldf.py +++ b/tests/test_modeldf.py @@ -1,7 +1,7 @@ -from mesa_frames import ModelDF +from mesa_frames import Model -class CustomModel(ModelDF): +class CustomModel(Model): def __init__(self): super().__init__() self.custom_step_count = 0 @@ -12,7 +12,7 @@ def step(self): class Test_ModelDF: def test_steps(self): - model = ModelDF() + model = Model() assert model.steps == 0 diff --git a/uv.lock b/uv.lock index f09db044..a72164c0 100644 --- a/uv.lock +++ b/uv.lock @@ -1258,6 +1258,7 @@ dev = [ docs = [ { name = "autodocsumm" }, { name = "beartype" }, + { name = "mesa" }, { name = "mkdocs-git-revision-date-localized-plugin" }, { name = "mkdocs-include-markdown-plugin" }, { name = "mkdocs-jupyter" }, @@ -1319,6 +1320,7 @@ dev = [ docs = [ { name = "autodocsumm", specifier = ">=0.2.14" }, { name = "beartype", specifier = ">=0.21.0" }, + { name = "mesa", specifier = ">=3.2.0" }, { name = "mkdocs-git-revision-date-localized-plugin", specifier = ">=1.4.7" }, { name = "mkdocs-include-markdown-plugin", specifier = ">=7.1.5" }, { name = "mkdocs-jupyter", specifier = ">=0.25.1" }, From 8d7fe146f7fd39ee2d5a21339c181c4619286c7c Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 31 Aug 2025 21:18:09 +0200 Subject: [PATCH 048/329] Refactor agent set imports and introduce AgentSetRegistry - Updated import paths for AbstractAgentSet and AgentSetRegistry to reflect new module structure. - Created a new concrete implementation of AgentSetRegistry in `agentsetregistry.py`, providing a collection for managing agent sets with DataFrame-based storage. - Modified existing files to utilize the new AgentSetRegistry class, ensuring consistent usage across the codebase. --- mesa_frames/__init__.py | 2 +- mesa_frames/abstract/{agents.py => agentsetregistry.py} | 0 mesa_frames/abstract/space.py | 4 ++-- mesa_frames/concrete/agentset.py | 2 +- mesa_frames/concrete/{agents.py => agentsetregistry.py} | 2 +- mesa_frames/concrete/model.py | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) rename mesa_frames/abstract/{agents.py => agentsetregistry.py} (100%) rename mesa_frames/concrete/{agents.py => agentsetregistry.py} (99%) diff --git a/mesa_frames/__init__.py b/mesa_frames/__init__.py index 4bca420e..ae16b4a0 100644 --- a/mesa_frames/__init__.py +++ b/mesa_frames/__init__.py @@ -60,7 +60,7 @@ def __init__(self, width, height): stacklevel=2, ) -from mesa_frames.concrete.agents import AgentSetRegistry +from mesa_frames.concrete.agentsetregistry import AgentSetRegistry from mesa_frames.concrete.agentset import AgentSet from mesa_frames.concrete.model import Model from mesa_frames.concrete.space import GridPolars diff --git a/mesa_frames/abstract/agents.py b/mesa_frames/abstract/agentsetregistry.py similarity index 100% rename from mesa_frames/abstract/agents.py rename to mesa_frames/abstract/agentsetregistry.py diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index ab9f6878..73ddac8c 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -59,9 +59,9 @@ def __init__(self, model, dimensions, torus, capacity, neighborhood_type): import polars as pl from numpy.random import Generator -from mesa_frames.abstract.agents import AbstractAgentSetRegistry, AbstractAgentSet +from mesa_frames.abstract.agentsetregistry import AbstractAgentSetRegistry, AbstractAgentSet from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin -from mesa_frames.concrete.agents import AgentSetRegistry +from mesa_frames.concrete.agentsetregistry import AgentSetRegistry from mesa_frames.types_ import ( ArrayLike, BoolSeries, diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 7341f066..3d5fb4f6 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -65,7 +65,7 @@ def step(self): import numpy as np import polars as pl -from mesa_frames.concrete.agents import AbstractAgentSet +from mesa_frames.concrete.agentsetregistry import AbstractAgentSet from mesa_frames.concrete.mixin import PolarsMixin from mesa_frames.concrete.model import Model from mesa_frames.types_ import AgentPolarsMask, IntoExpr, PolarsIdsLike diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agentsetregistry.py similarity index 99% rename from mesa_frames/concrete/agents.py rename to mesa_frames/concrete/agentsetregistry.py index ad0e3ff9..26b247e6 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -53,7 +53,7 @@ def step(self): import numpy as np import polars as pl -from mesa_frames.abstract.agents import AbstractAgentSetRegistry, AbstractAgentSet +from mesa_frames.abstract.agentsetregistry import AbstractAgentSetRegistry, AbstractAgentSet from mesa_frames.types_ import ( AgentMask, AgnosticAgentMask, diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index 2703c0e6..e1aeea4b 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -46,9 +46,9 @@ def run_model(self): import numpy as np -from mesa_frames.abstract.agents import AbstractAgentSet +from mesa_frames.abstract.agentsetregistry import AbstractAgentSet from mesa_frames.abstract.space import SpaceDF -from mesa_frames.concrete.agents import AgentSetRegistry +from mesa_frames.concrete.agentsetregistry import AgentSetRegistry class Model: From a814dd84da82d9b780d82c51fa4431e322f81a71 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 31 Aug 2025 21:20:31 +0200 Subject: [PATCH 049/329] Refactor import statements for better readability in space.py and agentsetregistry.py --- mesa_frames/abstract/space.py | 5 ++++- mesa_frames/concrete/agentsetregistry.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index 73ddac8c..bef1ec57 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -59,7 +59,10 @@ def __init__(self, model, dimensions, torus, capacity, neighborhood_type): import polars as pl from numpy.random import Generator -from mesa_frames.abstract.agentsetregistry import AbstractAgentSetRegistry, AbstractAgentSet +from mesa_frames.abstract.agentsetregistry import ( + AbstractAgentSetRegistry, + AbstractAgentSet, +) from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin from mesa_frames.concrete.agentsetregistry import AgentSetRegistry from mesa_frames.types_ import ( diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 26b247e6..7f43e987 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -53,7 +53,10 @@ def step(self): import numpy as np import polars as pl -from mesa_frames.abstract.agentsetregistry import AbstractAgentSetRegistry, AbstractAgentSet +from mesa_frames.abstract.agentsetregistry import ( + AbstractAgentSetRegistry, + AbstractAgentSet, +) from mesa_frames.types_ import ( AgentMask, AgnosticAgentMask, From 5dbe6f5011d8c4f56193059b9e85df584c584b05 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 31 Aug 2025 21:21:36 +0200 Subject: [PATCH 050/329] Fix formatting in AGENTS.md for MESA_FRAMES_RUNTIME_TYPECHECKING variable --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index cd78226f..90ccd30b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,7 +13,7 @@ - Install (dev stack): `uv sync` (always use uv) - Lint & format: `uv run ruff check . --fix && uv run ruff format .` -- Tests (quiet + coverage): `export MESA_FRAMES_RUNTIME_TYPECHECKING = 1 && uv run pytest -q --cov=mesa_frames --cov-report=term-missing` +- Tests (quiet + coverage): `export MESA_FRAMES_RUNTIME_TYPECHECKING=1 && uv run pytest -q --cov=mesa_frames --cov-report=term-missing` - Pre-commit (all files): `uv run pre-commit run -a` - Docs preview: `uv run mkdocs serve` From 79e94e5c080c3af81581c7ec4cc862307565000d Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 31 Aug 2025 21:22:36 +0200 Subject: [PATCH 051/329] Update type hints in AbstractAgentSetRegistry to reference abstract agents --- mesa_frames/abstract/agentsetregistry.py | 70 ++++++++++++------------ 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index 3f746b9f..5f9f9699 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -76,15 +76,15 @@ def discard( self, agents: IdsLike | AgentMask - | mesa_frames.concrete.agents.AbstractAgentSet - | Collection[mesa_frames.concrete.agents.AbstractAgentSet], + | mesa_frames.abstract.agents.AbstractAgentSet + | Collection[mesa_frames.abstract.agents.AbstractAgentSet], inplace: bool = True, ) -> Self: """Remove agents from the AbstractAgentSetRegistry. Does not raise an error if the agent is not found. Parameters ---------- - agents : IdsLike | AgentMask | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + agents : IdsLike | AgentMask | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] The agents to remove inplace : bool Whether to remove the agent in place. Defaults to True. @@ -103,15 +103,15 @@ def add( self, agents: DataFrame | DataFrameInput - | mesa_frames.concrete.agents.AbstractAgentSet - | Collection[mesa_frames.concrete.agents.AbstractAgentSet], + | mesa_frames.abstract.agents.AbstractAgentSet + | Collection[mesa_frames.abstract.agents.AbstractAgentSet], inplace: bool = True, ) -> Self: """Add agents to the AbstractAgentSetRegistry. Parameters ---------- - agents : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + agents : DataFrame | DataFrameInput | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] The agents to add. inplace : bool Whether to add the agents in place. Defaults to True. @@ -130,18 +130,18 @@ def contains(self, agents: int) -> bool: ... @overload @abstractmethod def contains( - self, agents: mesa_frames.concrete.agents.AbstractAgentSet | IdsLike + self, agents: mesa_frames.abstract.agents.AbstractAgentSet | IdsLike ) -> BoolSeries: ... @abstractmethod def contains( - self, agents: mesa_frames.concrete.agents.AbstractAgentSet | IdsLike + self, agents: mesa_frames.abstract.agents.AbstractAgentSet | IdsLike ) -> bool | BoolSeries: """Check if agents with the specified IDs are in the AbstractAgentSetRegistry. Parameters ---------- - agents : mesa_frames.concrete.agents.AbstractAgentSet | IdsLike + agents : mesa_frames.abstract.agents.AbstractAgentSet | IdsLike The ID(s) to check for. Returns @@ -172,7 +172,7 @@ def do( return_results: Literal[True], inplace: bool = True, **kwargs: Any, - ) -> Any | dict[mesa_frames.concrete.agents.AbstractAgentSet, Any]: ... + ) -> Any | dict[mesa_frames.abstract.agents.AbstractAgentSet, Any]: ... @abstractmethod def do( @@ -183,7 +183,7 @@ def do( return_results: bool = False, inplace: bool = True, **kwargs: Any, - ) -> Self | Any | dict[mesa_frames.concrete.agents.AbstractAgentSet, Any]: + ) -> Self | Any | dict[mesa_frames.abstract.agents.AbstractAgentSet, Any]: """Invoke a method on the AbstractAgentSetRegistry. Parameters @@ -203,7 +203,7 @@ def do( Returns ------- - Self | Any | dict[mesa_frames.concrete.agents.AbstractAgentSet, Any] + Self | Any | dict[mesa_frames.abstract.agents.AbstractAgentSet, Any] The updated AbstractAgentSetRegistry or the result of the method. """ ... @@ -246,8 +246,8 @@ def remove( agents: ( IdsLike | AgentMask - | mesa_frames.concrete.agents.AbstractAgentSet - | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + | mesa_frames.abstract.agents.AbstractAgentSet + | Collection[mesa_frames.abstract.agents.AbstractAgentSet] ), inplace: bool = True, ) -> Self: @@ -255,7 +255,7 @@ def remove( Parameters ---------- - agents : IdsLike | AgentMask | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + agents : IdsLike | AgentMask | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] The agents to remove. inplace : bool, optional Whether to remove the agent in place. @@ -396,14 +396,14 @@ def __add__( self, other: DataFrame | DataFrameInput - | mesa_frames.concrete.agents.AbstractAgentSet - | Collection[mesa_frames.concrete.agents.AbstractAgentSet], + | mesa_frames.abstract.agents.AbstractAgentSet + | Collection[mesa_frames.abstract.agents.AbstractAgentSet], ) -> Self: """Add agents to a new AbstractAgentSetRegistry through the + operator. Parameters ---------- - other : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + other : DataFrame | DataFrameInput | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] The agents to add. Returns @@ -491,15 +491,15 @@ def __iadd__( other: ( DataFrame | DataFrameInput - | mesa_frames.concrete.agents.AbstractAgentSet - | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + | mesa_frames.abstract.agents.AbstractAgentSet + | Collection[mesa_frames.abstract.agents.AbstractAgentSet] ), ) -> Self: """Add agents to the AbstractAgentSetRegistry through the += operator. Parameters ---------- - other : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + other : DataFrame | DataFrameInput | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] The agents to add. Returns @@ -514,15 +514,15 @@ def __isub__( other: ( IdsLike | AgentMask - | mesa_frames.concrete.agents.AbstractAgentSet - | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + | mesa_frames.abstract.agents.AbstractAgentSet + | Collection[mesa_frames.abstract.agents.AbstractAgentSet] ), ) -> Self: """Remove agents from the AbstractAgentSetRegistry through the -= operator. Parameters ---------- - other : IdsLike | AgentMask | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + other : IdsLike | AgentMask | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] The agents to remove. Returns @@ -537,15 +537,15 @@ def __sub__( other: ( IdsLike | AgentMask - | mesa_frames.concrete.agents.AbstractAgentSet - | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + | mesa_frames.abstract.agents.AbstractAgentSet + | Collection[mesa_frames.abstract.agents.AbstractAgentSet] ), ) -> Self: """Remove agents from a new AbstractAgentSetRegistry through the - operator. Parameters ---------- - other : IdsLike | AgentMask | mesa_frames.concrete.agents.AbstractAgentSet | Collection[mesa_frames.concrete.agents.AbstractAgentSet] + other : IdsLike | AgentMask | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] The agents to remove. Returns @@ -711,13 +711,13 @@ def df(self) -> DataFrame | dict[str, DataFrame]: @df.setter @abstractmethod def df( - self, agents: DataFrame | list[mesa_frames.concrete.agents.AbstractAgentSet] + self, agents: DataFrame | list[mesa_frames.abstract.agents.AbstractAgentSet] ) -> None: """Set the agents in the AbstractAgentSetRegistry. Parameters ---------- - agents : DataFrame | list[mesa_frames.concrete.agents.AbstractAgentSet] + agents : DataFrame | list[mesa_frames.abstract.agents.AbstractAgentSet] """ @property @@ -749,24 +749,24 @@ def active_agents( @abstractmethod def inactive_agents( self, - ) -> DataFrame | dict[mesa_frames.concrete.agents.AbstractAgentSet, DataFrame]: + ) -> DataFrame | dict[mesa_frames.abstract.agents.AbstractAgentSet, DataFrame]: """The inactive agents in the AbstractAgentSetRegistry. Returns ------- - DataFrame | dict[mesa_frames.concrete.agents.AbstractAgentSet, DataFrame] + DataFrame | dict[mesa_frames.abstract.agents.AbstractAgentSet, DataFrame] """ @property @abstractmethod def index( self, - ) -> Index | dict[mesa_frames.concrete.agents.AbstractAgentSet, Index]: + ) -> Index | dict[mesa_frames.abstract.agents.AbstractAgentSet, Index]: """The ids in the AbstractAgentSetRegistry. Returns ------- - Index | dict[mesa_frames.concrete.agents.AbstractAgentSet, Index] + Index | dict[mesa_frames.abstract.agents.AbstractAgentSet, Index] """ ... @@ -774,12 +774,12 @@ def index( @abstractmethod def pos( self, - ) -> DataFrame | dict[mesa_frames.concrete.agents.AbstractAgentSet, DataFrame]: + ) -> DataFrame | dict[mesa_frames.abstract.agents.AbstractAgentSet, DataFrame]: """The position of the agents in the AbstractAgentSetRegistry. Returns ------- - DataFrame | dict[mesa_frames.concrete.agents.AbstractAgentSet, DataFrame] + DataFrame | dict[mesa_frames.abstract.agents.AbstractAgentSet, DataFrame] """ ... From 09cb3361ec97fd18c972ace727a1ea55e097ea0b Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 31 Aug 2025 21:31:05 +0200 Subject: [PATCH 052/329] Introduce AbstractAgentSet class and refactor imports for consistency --- mesa_frames/abstract/agentset.py | 365 +++++++++++++++++++ mesa_frames/abstract/agentsetregistry.py | 433 ++--------------------- mesa_frames/abstract/space.py | 2 +- mesa_frames/concrete/agentset.py | 2 +- mesa_frames/concrete/agentsetregistry.py | 2 +- mesa_frames/concrete/model.py | 2 +- 6 files changed, 407 insertions(+), 399 deletions(-) create mode 100644 mesa_frames/abstract/agentset.py diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py new file mode 100644 index 00000000..ea57021a --- /dev/null +++ b/mesa_frames/abstract/agentset.py @@ -0,0 +1,365 @@ +from abc import abstractmethod +from collections.abc import Collection, Iterable, Iterator +from typing import Any, Literal, Self, overload + +from mesa_frames.abstract.agentsetregistry import AbstractAgentSetRegistry +from mesa_frames.abstract.mixin import DataFrameMixin +from mesa_frames.types_ import DataFrame, Series, AgentMask, IdsLike, DataFrameInput, Index, BoolSeries + + +class AbstractAgentSet(AbstractAgentSetRegistry, DataFrameMixin): + """The AbstractAgentSet class is a container for agents of the same type. + + Parameters + ---------- + model : mesa_frames.concrete.model.Model + The model that the agent set belongs to. + """ + + _df: DataFrame # The agents in the AbstractAgentSet + _mask: AgentMask # The underlying mask used for the active agents in the AbstractAgentSet. + _model: ( + mesa_frames.concrete.model.Model + ) # The model that the AbstractAgentSet belongs to. + + @abstractmethod + def __init__(self, model: mesa_frames.concrete.model.Model) -> None: ... + + @abstractmethod + def add( + self, + agents: DataFrame | DataFrameInput, + inplace: bool = True, + ) -> Self: + """Add agents to the AbstractAgentSet. + + Agents can be the input to the DataFrame constructor. So, the input can be: + - A DataFrame: adds the agents from the DataFrame. + - A DataFrameInput: passes the input to the DataFrame constructor. + + Parameters + ---------- + agents : DataFrame | DataFrameInput + The agents to add. + inplace : bool, optional + If True, perform the operation in place, by default True + + Returns + ------- + Self + A new AbstractAgentSetRegistry with the added agents. + """ + ... + + def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: + """Remove an agent from the AbstractAgentSet. Does not raise an error if the agent is not found. + + Parameters + ---------- + agents : IdsLike | AgentMask + The ids to remove + inplace : bool, optional + Whether to remove the agent in place, by default True + + Returns + ------- + Self + The updated AbstractAgentSet. + """ + return super().discard(agents, inplace) + + @overload + def do( + self, + method_name: str, + *args, + mask: AgentMask | None = None, + return_results: Literal[False] = False, + inplace: bool = True, + **kwargs, + ) -> Self: ... + + @overload + def do( + self, + method_name: str, + *args, + mask: AgentMask | None = None, + return_results: Literal[True], + inplace: bool = True, + **kwargs, + ) -> Any: ... + + def do( + self, + method_name: str, + *args, + mask: AgentMask | None = None, + return_results: bool = False, + inplace: bool = True, + **kwargs, + ) -> Self | Any: + masked_df = self._get_masked_df(mask) + # If the mask is empty, we can use the object as is + if len(masked_df) == len(self._df): + obj = self._get_obj(inplace) + method = getattr(obj, method_name) + result = method(*args, **kwargs) + else: # If the mask is not empty, we need to create a new masked AbstractAgentSet and concatenate the AbstractAgentSets at the end + obj = self._get_obj(inplace=False) + obj._df = masked_df + original_masked_index = obj._get_obj_copy(obj.index) + method = getattr(obj, method_name) + result = method(*args, **kwargs) + obj._concatenate_agentsets( + [self], + duplicates_allowed=True, + keep_first_only=True, + original_masked_index=original_masked_index, + ) + if inplace: + for key, value in obj.__dict__.items(): + setattr(self, key, value) + obj = self + if return_results: + return result + else: + return obj + + @abstractmethod + @overload + def get( + self, + attr_names: str, + mask: AgentMask | None = None, + ) -> Series: ... + + @abstractmethod + @overload + def get( + self, + attr_names: Collection[str] | None = None, + mask: AgentMask | None = None, + ) -> DataFrame: ... + + @abstractmethod + def get( + self, + attr_names: str | Collection[str] | None = None, + mask: AgentMask | None = None, + ) -> Series | DataFrame: ... + + @abstractmethod + def step(self) -> None: + """Run a single step of the AbstractAgentSet. This method should be overridden by subclasses.""" + ... + + def remove(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: + if isinstance(agents, str) and agents == "active": + agents = self.active_agents + if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): + return self._get_obj(inplace) + agents = self._df_index(self._get_masked_df(agents), "unique_id") + sets = self.model.sets.remove(agents, inplace=inplace) + # TODO: Refactor AgentSetRegistry to return dict[str, AbstractAgentSet] instead of dict[AbstractAgentSet, DataFrame] + # And assign a name to AbstractAgentSet? This has to be replaced by a nicer API of AgentSetRegistry + for agentset in sets.df.keys(): + if isinstance(agentset, self.__class__): + return agentset + return self + + @abstractmethod + def _concatenate_agentsets( + self, + objs: Iterable[Self], + duplicates_allowed: bool = True, + keep_first_only: bool = True, + original_masked_index: Index | None = None, + ) -> Self: ... + + @abstractmethod + def _get_bool_mask(self, mask: AgentMask) -> BoolSeries: + """Get the equivalent boolean mask based on the input mask. + + Parameters + ---------- + mask : AgentMask + + Returns + ------- + BoolSeries + """ + ... + + @abstractmethod + def _get_masked_df(self, mask: AgentMask) -> DataFrame: + """Get the df filtered by the input mask. + + Parameters + ---------- + mask : AgentMask + + Returns + ------- + DataFrame + """ + + @overload + @abstractmethod + def _get_obj_copy(self, obj: DataFrame) -> DataFrame: ... + + @overload + @abstractmethod + def _get_obj_copy(self, obj: Series) -> Series: ... + + @overload + @abstractmethod + def _get_obj_copy(self, obj: Index) -> Index: ... + + @abstractmethod + def _get_obj_copy( + self, obj: DataFrame | Series | Index + ) -> DataFrame | Series | Index: ... + + @abstractmethod + def _discard(self, ids: IdsLike) -> Self: + """Remove an agent from the DataFrame of the AbstractAgentSet. Gets called by self.model.sets.remove and self.model.sets.discard. + + Parameters + ---------- + ids : IdsLike + + The ids to remove + + Returns + ------- + Self + """ + ... + + @abstractmethod + def _update_mask( + self, original_active_indices: Index, new_active_indices: Index | None = None + ) -> None: ... + + def __add__(self, other: DataFrame | DataFrameInput) -> Self: + """Add agents to a new AbstractAgentSet through the + operator. + + Other can be: + - A DataFrame: adds the agents from the DataFrame. + - A DataFrameInput: passes the input to the DataFrame constructor. + + Parameters + ---------- + other : DataFrame | DataFrameInput + The agents to add. + + Returns + ------- + Self + A new AbstractAgentSetRegistry with the added agents. + """ + return super().__add__(other) + + def __iadd__(self, other: DataFrame | DataFrameInput) -> Self: + """ + Add agents to the AbstractAgentSet through the += operator. + + Other can be: + - A DataFrame: adds the agents from the DataFrame. + - A DataFrameInput: passes the input to the DataFrame constructor. + + Parameters + ---------- + other : DataFrame | DataFrameInput + The agents to add. + + Returns + ------- + Self + The updated AbstractAgentSetRegistry. + """ + return super().__iadd__(other) + + @abstractmethod + def __getattr__(self, name: str) -> Any: + if __debug__: # Only execute in non-optimized mode + if name == "_df": + raise AttributeError( + "The _df attribute is not set. You probably forgot to call super().__init__ in the __init__ method." + ) + + @overload + def __getitem__(self, key: str | tuple[AgentMask, str]) -> Series | DataFrame: ... + + @overload + def __getitem__( + self, + key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], + ) -> DataFrame: ... + + def __getitem__( + self, + key: ( + str + | Collection[str] + | AgentMask + | tuple[AgentMask, str] + | tuple[AgentMask, Collection[str]] + ), + ) -> Series | DataFrame: + attr = super().__getitem__(key) + assert isinstance(attr, (Series, DataFrame, Index)) + return attr + + def __len__(self) -> int: + return len(self._df) + + def __repr__(self) -> str: + return f"{self.__class__.__name__}\n {str(self._df)}" + + def __str__(self) -> str: + return f"{self.__class__.__name__}\n {str(self._df)}" + + def __reversed__(self) -> Iterator: + return reversed(self._df) + + @property + def df(self) -> DataFrame: + return self._df + + @df.setter + def df(self, agents: DataFrame) -> None: + """Set the agents in the AbstractAgentSet. + + Parameters + ---------- + agents : DataFrame + The agents to set. + """ + self._df = agents + + @property + @abstractmethod + def active_agents(self) -> DataFrame: ... + + @property + @abstractmethod + def inactive_agents(self) -> DataFrame: ... + + @property + def index(self) -> Index: ... + + @property + def pos(self) -> DataFrame: + if self.space is None: + raise AttributeError( + "Attempted to access `pos`, but the model has no space attached." + ) + pos = self._df_get_masked_df( + df=self.space.agents, index_cols="agent_id", mask=self.index + ) + pos = self._df_reindex( + pos, self.index, new_index_cols="unique_id", original_index_cols="agent_id" + ) + return pos diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index 5f9f9699..2c11cf30 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -43,13 +43,14 @@ def __init__(self, model): from __future__ import annotations # PEP 563: postponed evaluation of type annotations from abc import abstractmethod -from collections.abc import Callable, Collection, Iterable, Iterator, Sequence +from collections.abc import Callable, Collection, Iterator, Sequence from contextlib import suppress from typing import Any, Literal, Self, overload from numpy.random import Generator -from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin +from mesa_frames.abstract.agentset import AbstractAgentSet +from mesa_frames.abstract.mixin import CopyMixin from mesa_frames.types_ import ( AgentMask, BoolSeries, @@ -76,15 +77,15 @@ def discard( self, agents: IdsLike | AgentMask - | mesa_frames.abstract.agents.AbstractAgentSet - | Collection[mesa_frames.abstract.agents.AbstractAgentSet], + | mesa_frames.abstract.agentset.AbstractAgentSet + | Collection[mesa_frames.abstract.agentset.AbstractAgentSet], inplace: bool = True, ) -> Self: """Remove agents from the AbstractAgentSetRegistry. Does not raise an error if the agent is not found. Parameters ---------- - agents : IdsLike | AgentMask | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + agents : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The agents to remove inplace : bool Whether to remove the agent in place. Defaults to True. @@ -103,15 +104,15 @@ def add( self, agents: DataFrame | DataFrameInput - | mesa_frames.abstract.agents.AbstractAgentSet - | Collection[mesa_frames.abstract.agents.AbstractAgentSet], + | mesa_frames.abstract.agentset.AbstractAgentSet + | Collection[mesa_frames.abstract.agentset.AbstractAgentSet], inplace: bool = True, ) -> Self: """Add agents to the AbstractAgentSetRegistry. Parameters ---------- - agents : DataFrame | DataFrameInput | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + agents : DataFrame | DataFrameInput | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The agents to add. inplace : bool Whether to add the agents in place. Defaults to True. @@ -130,18 +131,18 @@ def contains(self, agents: int) -> bool: ... @overload @abstractmethod def contains( - self, agents: mesa_frames.abstract.agents.AbstractAgentSet | IdsLike + self, agents: mesa_frames.abstract.agentset.AbstractAgentSet | IdsLike ) -> BoolSeries: ... @abstractmethod def contains( - self, agents: mesa_frames.abstract.agents.AbstractAgentSet | IdsLike + self, agents: mesa_frames.abstract.agentset.AbstractAgentSet | IdsLike ) -> bool | BoolSeries: """Check if agents with the specified IDs are in the AbstractAgentSetRegistry. Parameters ---------- - agents : mesa_frames.abstract.agents.AbstractAgentSet | IdsLike + agents : mesa_frames.abstract.agentset.AbstractAgentSet | IdsLike The ID(s) to check for. Returns @@ -172,7 +173,7 @@ def do( return_results: Literal[True], inplace: bool = True, **kwargs: Any, - ) -> Any | dict[mesa_frames.abstract.agents.AbstractAgentSet, Any]: ... + ) -> Any | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Any]: ... @abstractmethod def do( @@ -183,7 +184,7 @@ def do( return_results: bool = False, inplace: bool = True, **kwargs: Any, - ) -> Self | Any | dict[mesa_frames.abstract.agents.AbstractAgentSet, Any]: + ) -> Self | Any | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Any]: """Invoke a method on the AbstractAgentSetRegistry. Parameters @@ -203,7 +204,7 @@ def do( Returns ------- - Self | Any | dict[mesa_frames.abstract.agents.AbstractAgentSet, Any] + Self | Any | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Any] The updated AbstractAgentSetRegistry or the result of the method. """ ... @@ -246,8 +247,8 @@ def remove( agents: ( IdsLike | AgentMask - | mesa_frames.abstract.agents.AbstractAgentSet - | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + | mesa_frames.abstract.agentset.AbstractAgentSet + | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] ), inplace: bool = True, ) -> Self: @@ -255,7 +256,7 @@ def remove( Parameters ---------- - agents : IdsLike | AgentMask | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + agents : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The agents to remove. inplace : bool, optional Whether to remove the agent in place. @@ -396,14 +397,14 @@ def __add__( self, other: DataFrame | DataFrameInput - | mesa_frames.abstract.agents.AbstractAgentSet - | Collection[mesa_frames.abstract.agents.AbstractAgentSet], + | mesa_frames.abstract.agentset.AbstractAgentSet + | Collection[mesa_frames.abstract.agentset.AbstractAgentSet], ) -> Self: """Add agents to a new AbstractAgentSetRegistry through the + operator. Parameters ---------- - other : DataFrame | DataFrameInput | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + other : DataFrame | DataFrameInput | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The agents to add. Returns @@ -491,15 +492,15 @@ def __iadd__( other: ( DataFrame | DataFrameInput - | mesa_frames.abstract.agents.AbstractAgentSet - | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + | mesa_frames.abstract.agentset.AbstractAgentSet + | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] ), ) -> Self: """Add agents to the AbstractAgentSetRegistry through the += operator. Parameters ---------- - other : DataFrame | DataFrameInput | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + other : DataFrame | DataFrameInput | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The agents to add. Returns @@ -514,15 +515,15 @@ def __isub__( other: ( IdsLike | AgentMask - | mesa_frames.abstract.agents.AbstractAgentSet - | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + | mesa_frames.abstract.agentset.AbstractAgentSet + | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] ), ) -> Self: """Remove agents from the AbstractAgentSetRegistry through the -= operator. Parameters ---------- - other : IdsLike | AgentMask | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + other : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The agents to remove. Returns @@ -537,15 +538,15 @@ def __sub__( other: ( IdsLike | AgentMask - | mesa_frames.abstract.agents.AbstractAgentSet - | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + | mesa_frames.abstract.agentset.AbstractAgentSet + | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] ), ) -> Self: """Remove agents from a new AbstractAgentSetRegistry through the - operator. Parameters ---------- - other : IdsLike | AgentMask | mesa_frames.abstract.agents.AbstractAgentSet | Collection[mesa_frames.abstract.agents.AbstractAgentSet] + other : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The agents to remove. Returns @@ -711,13 +712,13 @@ def df(self) -> DataFrame | dict[str, DataFrame]: @df.setter @abstractmethod def df( - self, agents: DataFrame | list[mesa_frames.abstract.agents.AbstractAgentSet] + self, agents: DataFrame | list[mesa_frames.abstract.agentset.AbstractAgentSet] ) -> None: """Set the agents in the AbstractAgentSetRegistry. Parameters ---------- - agents : DataFrame | list[mesa_frames.abstract.agents.AbstractAgentSet] + agents : DataFrame | list[mesa_frames.abstract.agentset.AbstractAgentSet] """ @property @@ -749,24 +750,24 @@ def active_agents( @abstractmethod def inactive_agents( self, - ) -> DataFrame | dict[mesa_frames.abstract.agents.AbstractAgentSet, DataFrame]: + ) -> DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame]: """The inactive agents in the AbstractAgentSetRegistry. Returns ------- - DataFrame | dict[mesa_frames.abstract.agents.AbstractAgentSet, DataFrame] + DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] """ @property @abstractmethod def index( self, - ) -> Index | dict[mesa_frames.abstract.agents.AbstractAgentSet, Index]: + ) -> Index | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Index]: """The ids in the AbstractAgentSetRegistry. Returns ------- - Index | dict[mesa_frames.abstract.agents.AbstractAgentSet, Index] + Index | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Index] """ ... @@ -774,369 +775,11 @@ def index( @abstractmethod def pos( self, - ) -> DataFrame | dict[mesa_frames.abstract.agents.AbstractAgentSet, DataFrame]: + ) -> DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame]: """The position of the agents in the AbstractAgentSetRegistry. Returns ------- - DataFrame | dict[mesa_frames.abstract.agents.AbstractAgentSet, DataFrame] + DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] """ ... - - -class AbstractAgentSet(AbstractAgentSetRegistry, DataFrameMixin): - """The AbstractAgentSet class is a container for agents of the same type. - - Parameters - ---------- - model : mesa_frames.concrete.model.Model - The model that the agent set belongs to. - """ - - _df: DataFrame # The agents in the AbstractAgentSet - _mask: AgentMask # The underlying mask used for the active agents in the AbstractAgentSet. - _model: ( - mesa_frames.concrete.model.Model - ) # The model that the AbstractAgentSet belongs to. - - @abstractmethod - def __init__(self, model: mesa_frames.concrete.model.Model) -> None: ... - - @abstractmethod - def add( - self, - agents: DataFrame | DataFrameInput, - inplace: bool = True, - ) -> Self: - """Add agents to the AbstractAgentSet. - - Agents can be the input to the DataFrame constructor. So, the input can be: - - A DataFrame: adds the agents from the DataFrame. - - A DataFrameInput: passes the input to the DataFrame constructor. - - Parameters - ---------- - agents : DataFrame | DataFrameInput - The agents to add. - inplace : bool, optional - If True, perform the operation in place, by default True - - Returns - ------- - Self - A new AbstractAgentSetRegistry with the added agents. - """ - ... - - def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: - """Remove an agent from the AbstractAgentSet. Does not raise an error if the agent is not found. - - Parameters - ---------- - agents : IdsLike | AgentMask - The ids to remove - inplace : bool, optional - Whether to remove the agent in place, by default True - - Returns - ------- - Self - The updated AbstractAgentSet. - """ - return super().discard(agents, inplace) - - @overload - def do( - self, - method_name: str, - *args, - mask: AgentMask | None = None, - return_results: Literal[False] = False, - inplace: bool = True, - **kwargs, - ) -> Self: ... - - @overload - def do( - self, - method_name: str, - *args, - mask: AgentMask | None = None, - return_results: Literal[True], - inplace: bool = True, - **kwargs, - ) -> Any: ... - - def do( - self, - method_name: str, - *args, - mask: AgentMask | None = None, - return_results: bool = False, - inplace: bool = True, - **kwargs, - ) -> Self | Any: - masked_df = self._get_masked_df(mask) - # If the mask is empty, we can use the object as is - if len(masked_df) == len(self._df): - obj = self._get_obj(inplace) - method = getattr(obj, method_name) - result = method(*args, **kwargs) - else: # If the mask is not empty, we need to create a new masked AbstractAgentSet and concatenate the AbstractAgentSets at the end - obj = self._get_obj(inplace=False) - obj._df = masked_df - original_masked_index = obj._get_obj_copy(obj.index) - method = getattr(obj, method_name) - result = method(*args, **kwargs) - obj._concatenate_agentsets( - [self], - duplicates_allowed=True, - keep_first_only=True, - original_masked_index=original_masked_index, - ) - if inplace: - for key, value in obj.__dict__.items(): - setattr(self, key, value) - obj = self - if return_results: - return result - else: - return obj - - @abstractmethod - @overload - def get( - self, - attr_names: str, - mask: AgentMask | None = None, - ) -> Series: ... - - @abstractmethod - @overload - def get( - self, - attr_names: Collection[str] | None = None, - mask: AgentMask | None = None, - ) -> DataFrame: ... - - @abstractmethod - def get( - self, - attr_names: str | Collection[str] | None = None, - mask: AgentMask | None = None, - ) -> Series | DataFrame: ... - - @abstractmethod - def step(self) -> None: - """Run a single step of the AbstractAgentSet. This method should be overridden by subclasses.""" - ... - - def remove(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: - if isinstance(agents, str) and agents == "active": - agents = self.active_agents - if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): - return self._get_obj(inplace) - agents = self._df_index(self._get_masked_df(agents), "unique_id") - sets = self.model.sets.remove(agents, inplace=inplace) - # TODO: Refactor AgentSetRegistry to return dict[str, AbstractAgentSet] instead of dict[AbstractAgentSet, DataFrame] - # And assign a name to AbstractAgentSet? This has to be replaced by a nicer API of AgentSetRegistry - for agentset in sets.df.keys(): - if isinstance(agentset, self.__class__): - return agentset - return self - - @abstractmethod - def _concatenate_agentsets( - self, - objs: Iterable[Self], - duplicates_allowed: bool = True, - keep_first_only: bool = True, - original_masked_index: Index | None = None, - ) -> Self: ... - - @abstractmethod - def _get_bool_mask(self, mask: AgentMask) -> BoolSeries: - """Get the equivalent boolean mask based on the input mask. - - Parameters - ---------- - mask : AgentMask - - Returns - ------- - BoolSeries - """ - ... - - @abstractmethod - def _get_masked_df(self, mask: AgentMask) -> DataFrame: - """Get the df filtered by the input mask. - - Parameters - ---------- - mask : AgentMask - - Returns - ------- - DataFrame - """ - - @overload - @abstractmethod - def _get_obj_copy(self, obj: DataFrame) -> DataFrame: ... - - @overload - @abstractmethod - def _get_obj_copy(self, obj: Series) -> Series: ... - - @overload - @abstractmethod - def _get_obj_copy(self, obj: Index) -> Index: ... - - @abstractmethod - def _get_obj_copy( - self, obj: DataFrame | Series | Index - ) -> DataFrame | Series | Index: ... - - @abstractmethod - def _discard(self, ids: IdsLike) -> Self: - """Remove an agent from the DataFrame of the AbstractAgentSet. Gets called by self.model.sets.remove and self.model.sets.discard. - - Parameters - ---------- - ids : IdsLike - - The ids to remove - - Returns - ------- - Self - """ - ... - - @abstractmethod - def _update_mask( - self, original_active_indices: Index, new_active_indices: Index | None = None - ) -> None: ... - - def __add__(self, other: DataFrame | DataFrameInput) -> Self: - """Add agents to a new AbstractAgentSet through the + operator. - - Other can be: - - A DataFrame: adds the agents from the DataFrame. - - A DataFrameInput: passes the input to the DataFrame constructor. - - Parameters - ---------- - other : DataFrame | DataFrameInput - The agents to add. - - Returns - ------- - Self - A new AbstractAgentSetRegistry with the added agents. - """ - return super().__add__(other) - - def __iadd__(self, other: DataFrame | DataFrameInput) -> Self: - """ - Add agents to the AbstractAgentSet through the += operator. - - Other can be: - - A DataFrame: adds the agents from the DataFrame. - - A DataFrameInput: passes the input to the DataFrame constructor. - - Parameters - ---------- - other : DataFrame | DataFrameInput - The agents to add. - - Returns - ------- - Self - The updated AbstractAgentSetRegistry. - """ - return super().__iadd__(other) - - @abstractmethod - def __getattr__(self, name: str) -> Any: - if __debug__: # Only execute in non-optimized mode - if name == "_df": - raise AttributeError( - "The _df attribute is not set. You probably forgot to call super().__init__ in the __init__ method." - ) - - @overload - def __getitem__(self, key: str | tuple[AgentMask, str]) -> Series | DataFrame: ... - - @overload - def __getitem__( - self, - key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], - ) -> DataFrame: ... - - def __getitem__( - self, - key: ( - str - | Collection[str] - | AgentMask - | tuple[AgentMask, str] - | tuple[AgentMask, Collection[str]] - ), - ) -> Series | DataFrame: - attr = super().__getitem__(key) - assert isinstance(attr, (Series, DataFrame, Index)) - return attr - - def __len__(self) -> int: - return len(self._df) - - def __repr__(self) -> str: - return f"{self.__class__.__name__}\n {str(self._df)}" - - def __str__(self) -> str: - return f"{self.__class__.__name__}\n {str(self._df)}" - - def __reversed__(self) -> Iterator: - return reversed(self._df) - - @property - def df(self) -> DataFrame: - return self._df - - @df.setter - def df(self, agents: DataFrame) -> None: - """Set the agents in the AbstractAgentSet. - - Parameters - ---------- - agents : DataFrame - The agents to set. - """ - self._df = agents - - @property - @abstractmethod - def active_agents(self) -> DataFrame: ... - - @property - @abstractmethod - def inactive_agents(self) -> DataFrame: ... - - @property - def index(self) -> Index: ... - - @property - def pos(self) -> DataFrame: - if self.space is None: - raise AttributeError( - "Attempted to access `pos`, but the model has no space attached." - ) - pos = self._df_get_masked_df( - df=self.space.agents, index_cols="agent_id", mask=self.index - ) - pos = self._df_reindex( - pos, self.index, new_index_cols="unique_id", original_index_cols="agent_id" - ) - return pos diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index bef1ec57..a1f855e9 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -59,9 +59,9 @@ def __init__(self, model, dimensions, torus, capacity, neighborhood_type): import polars as pl from numpy.random import Generator +from mesa_frames.abstract.agentset import AbstractAgentSet from mesa_frames.abstract.agentsetregistry import ( AbstractAgentSetRegistry, - AbstractAgentSet, ) from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin from mesa_frames.concrete.agentsetregistry import AgentSetRegistry diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 3d5fb4f6..3b60c565 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -65,7 +65,7 @@ def step(self): import numpy as np import polars as pl -from mesa_frames.concrete.agentsetregistry import AbstractAgentSet +from mesa_frames.abstract.agentset import AbstractAgentSet from mesa_frames.concrete.mixin import PolarsMixin from mesa_frames.concrete.model import Model from mesa_frames.types_ import AgentPolarsMask, IntoExpr, PolarsIdsLike diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 7f43e987..9169919a 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -53,9 +53,9 @@ def step(self): import numpy as np import polars as pl +from mesa_frames.abstract.agentset import AbstractAgentSet from mesa_frames.abstract.agentsetregistry import ( AbstractAgentSetRegistry, - AbstractAgentSet, ) from mesa_frames.types_ import ( AgentMask, diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index e1aeea4b..a1ad66e1 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -46,7 +46,7 @@ def run_model(self): import numpy as np -from mesa_frames.abstract.agentsetregistry import AbstractAgentSet +from mesa_frames.abstract.agentset import AbstractAgentSet from mesa_frames.abstract.space import SpaceDF from mesa_frames.concrete.agentsetregistry import AgentSetRegistry From ab80df033bd5d9d08c6d918068b281df0e293754 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 31 Aug 2025 21:33:50 +0200 Subject: [PATCH 053/329] Update type hints in AbstractAgentSetRegistry to reference concrete AbstractAgentSet --- mesa_frames/abstract/agentsetregistry.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index 2c11cf30..4075185e 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -49,7 +49,6 @@ def __init__(self, model): from numpy.random import Generator -from mesa_frames.abstract.agentset import AbstractAgentSet from mesa_frames.abstract.mixin import CopyMixin from mesa_frames.types_ import ( AgentMask, @@ -414,7 +413,7 @@ def __add__( """ return self.add(agents=other, inplace=False) - def __contains__(self, agents: int | AbstractAgentSet) -> bool: + def __contains__(self, agents: int | mesa_frames.abstract.agentset.AbstractAgentSet) -> bool: """Check if an agent is in the AbstractAgentSetRegistry. Parameters @@ -432,13 +431,13 @@ def __contains__(self, agents: int | AbstractAgentSet) -> bool: @overload def __getitem__( self, key: str | tuple[AgentMask, str] - ) -> Series | dict[AbstractAgentSet, Series]: ... + ) -> Series | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Series]: ... @overload def __getitem__( self, key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], - ) -> DataFrame | dict[AbstractAgentSet, DataFrame]: ... + ) -> DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame]: ... def __getitem__( self, @@ -448,14 +447,14 @@ def __getitem__( | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] - | tuple[dict[AbstractAgentSet, AgentMask], str] - | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] + | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] + | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] ), ) -> ( Series | DataFrame - | dict[AbstractAgentSet, Series] - | dict[AbstractAgentSet, DataFrame] + | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Series] + | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] ): """Implement the [] operator for the AbstractAgentSetRegistry. @@ -563,8 +562,8 @@ def __setitem__( | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] - | tuple[dict[AbstractAgentSet, AgentMask], str] - | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] + | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] + | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] ), values: Any, ) -> None: From 7878392b7892c36cf1a1f862909ee75b75fd68ae Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 1 Sep 2025 07:51:53 +0200 Subject: [PATCH 054/329] Refactor import statements in agentset.py for improved readability --- mesa_frames/abstract/agentset.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index ea57021a..cfe6cab3 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -1,10 +1,20 @@ +from __future__ import annotations + from abc import abstractmethod from collections.abc import Collection, Iterable, Iterator from typing import Any, Literal, Self, overload from mesa_frames.abstract.agentsetregistry import AbstractAgentSetRegistry from mesa_frames.abstract.mixin import DataFrameMixin -from mesa_frames.types_ import DataFrame, Series, AgentMask, IdsLike, DataFrameInput, Index, BoolSeries +from mesa_frames.types_ import ( + AgentMask, + BoolSeries, + DataFrame, + DataFrameInput, + IdsLike, + Index, + Series, +) class AbstractAgentSet(AbstractAgentSetRegistry, DataFrameMixin): From 47a5413c733bd53bce8ed93ad0eda38f03b3b4db Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Sep 2025 06:24:27 +0000 Subject: [PATCH 055/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa_frames/abstract/agentsetregistry.py | 26 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index 4075185e..2b7d2c99 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -413,7 +413,9 @@ def __add__( """ return self.add(agents=other, inplace=False) - def __contains__(self, agents: int | mesa_frames.abstract.agentset.AbstractAgentSet) -> bool: + def __contains__( + self, agents: int | mesa_frames.abstract.agentset.AbstractAgentSet + ) -> bool: """Check if an agent is in the AbstractAgentSetRegistry. Parameters @@ -437,7 +439,9 @@ def __getitem__( def __getitem__( self, key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], - ) -> DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame]: ... + ) -> ( + DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] + ): ... def __getitem__( self, @@ -447,8 +451,13 @@ def __getitem__( | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] - | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] - | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] + | tuple[ + dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str + ] + | tuple[ + dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], + Collection[str], + ] ), ) -> ( Series @@ -562,8 +571,13 @@ def __setitem__( | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] - | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] - | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] + | tuple[ + dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str + ] + | tuple[ + dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], + Collection[str], + ] ), values: Any, ) -> None: From dfa22874440db54a805db77c982a515f91880115 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 1 Sep 2025 08:32:44 +0200 Subject: [PATCH 056/329] Update docstring in AbstractAgentSet and improve type hints in AbstractAgentSetRegistry --- mesa_frames/abstract/agentset.py | 18 +++++++++++++ mesa_frames/abstract/agentsetregistry.py | 34 +++++++++++++++++------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index cfe6cab3..a7da9097 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -1,3 +1,21 @@ +""" +Abstract base classes for agent sets in mesa-frames. + +This module defines the core abstractions for agent sets in the mesa-frames +extension. It provides the foundation for implementing agent set storage and +manipulation. + +Classes: + AbstractAgentSet: + An abstract base class for agent sets that combines agent container + functionality with DataFrame operations. It inherits from both + AbstractAgentSetRegistry and DataFrameMixin to provide comprehensive + agent management capabilities. + +This abstract class is designed to be subclassed to create concrete +implementations that use specific DataFrame backends. +""" + from __future__ import annotations from abc import abstractmethod diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index 4075185e..abebe7a2 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -413,12 +413,14 @@ def __add__( """ return self.add(agents=other, inplace=False) - def __contains__(self, agents: int | mesa_frames.abstract.agentset.AbstractAgentSet) -> bool: + def __contains__( + self, agents: int | mesa_frames.abstract.agentset.AbstractAgentSet + ) -> bool: """Check if an agent is in the AbstractAgentSetRegistry. Parameters ---------- - agents : int | AbstractAgentSet + agents : int | mesa_frames.abstract.agentset.AbstractAgentSet The ID(s) or AbstractAgentSet to check for. Returns @@ -437,7 +439,9 @@ def __getitem__( def __getitem__( self, key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], - ) -> DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame]: ... + ) -> ( + DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] + ): ... def __getitem__( self, @@ -447,8 +451,13 @@ def __getitem__( | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] - | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] - | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] + | tuple[ + dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str + ] + | tuple[ + dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], + Collection[str], + ] ), ) -> ( Series @@ -467,12 +476,12 @@ def __getitem__( Parameters ---------- - key : str | Collection[str] | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] | tuple[dict[AbstractAgentSet, AgentMask], str] | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] + key : str | Collection[str] | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] The key to retrieve. Returns ------- - Series | DataFrame | dict[AbstractAgentSet, Series] | dict[AbstractAgentSet, DataFrame] + Series | DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Series] | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] The attribute values. """ # TODO: fix types @@ -562,8 +571,13 @@ def __setitem__( | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] - | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] - | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] + | tuple[ + dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str + ] + | tuple[ + dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], + Collection[str], + ] ), values: Any, ) -> None: @@ -579,7 +593,7 @@ def __setitem__( Parameters ---------- - key : str | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] | tuple[dict[AbstractAgentSet, AgentMask], str] | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] + key : str | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] The key to set. values : Any The values to set for the specified key. From c67292467762d4733fd8b20e831337e11d926522 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 1 Sep 2025 08:40:12 +0200 Subject: [PATCH 057/329] Remove AbstractAgentSetsAccessor class and its associated methods from accessors.py --- mesa_frames/abstract/accessors.py | 408 ------------------------------ 1 file changed, 408 deletions(-) delete mode 100644 mesa_frames/abstract/accessors.py diff --git a/mesa_frames/abstract/accessors.py b/mesa_frames/abstract/accessors.py deleted file mode 100644 index a33ddcab..00000000 --- a/mesa_frames/abstract/accessors.py +++ /dev/null @@ -1,408 +0,0 @@ -"""Abstract accessors for agent sets collections. - -This module provides abstract base classes for accessors that enable -flexible querying and manipulation of collections of agent sets. -""" - -from __future__ import annotations - -from abc import ABC, abstractmethod -from collections.abc import Iterable, Iterator, Mapping -from typing import Any, Literal, overload, TypeVar - -from mesa_frames.abstract.agents import AgentSetDF -from mesa_frames.types_ import KeyBy - -TSet = TypeVar("TSet", bound=AgentSetDF) - - -class AbstractAgentSetsAccessor(ABC): - """Abstract accessor for collections of agent sets. - - This interface defines a flexible, user-friendly API to access agent sets - by name, positional index, or class/type, and to iterate or view the - collection under different key domains. - - Notes - ----- - Concrete implementations should: - - Support ``__getitem__`` with ``int`` | ``str`` | ``type[AgentSetDF]``. - - Return a list for type-based queries (even when there is one match). - - Provide keyed iteration via ``keys/items/iter/mapping`` with ``key_by``. - - Expose read-only snapshots ``by_name`` and ``by_type``. - - Examples - -------- - Assuming ``agents`` is an :class:`~mesa_frames.concrete.agents.AgentsDF`: - - >>> sheep = agents.sets["Sheep"] # name lookup - >>> first = agents.sets[0] # index lookup - >>> wolves = agents.sets[Wolf] # type lookup → list - >>> len(wolves) >= 0 - True - - Choose a key view when iterating: - - >>> for k, aset in agents.sets.items(key_by="index"): - ... print(k, aset.name) - 0 Sheep - 1 Wolf - """ - - # __getitem__ — exact shapes per key kind - @overload - @abstractmethod - def __getitem__(self, key: int) -> AgentSetDF: ... - - @overload - @abstractmethod - def __getitem__(self, key: str) -> AgentSetDF: ... - - @overload - @abstractmethod - def __getitem__(self, key: type[TSet]) -> list[TSet]: ... - - @abstractmethod - def __getitem__(self, key: int | str | type[TSet]) -> AgentSetDF | list[TSet]: - """Retrieve agent set(s) by index, name, or type. - - Parameters - ---------- - key : int | str | type[TSet] - - ``int``: positional index (supports negative indices). - - ``str``: agent set name. - - ``type``: class or subclass of :class:`AgentSetDF`. - - Returns - ------- - AgentSetDF | list[TSet] - A single agent set for ``int``/``str`` keys; a list of matching - agent sets for ``type`` keys (possibly empty). - - Raises - ------ - IndexError - If an index is out of range. - KeyError - If a name is missing. - TypeError - If the key type is unsupported. - """ - - # get — mirrors dict.get, but preserves list shape for type keys - @overload - @abstractmethod - def get(self, key: int, default: None = ...) -> AgentSetDF | None: ... - - @overload - @abstractmethod - def get(self, key: str, default: None = ...) -> AgentSetDF | None: ... - - @overload - @abstractmethod - def get(self, key: type[TSet], default: None = ...) -> list[TSet]: ... - - @overload - @abstractmethod - def get(self, key: int, default: AgentSetDF) -> AgentSetDF: ... - - @overload - @abstractmethod - def get(self, key: str, default: AgentSetDF) -> AgentSetDF: ... - - @overload - @abstractmethod - def get(self, key: type[TSet], default: list[TSet]) -> list[TSet]: ... - - @abstractmethod - def get( - self, - key: int | str | type[TSet], - default: AgentSetDF | list[TSet] | None = None, - ) -> AgentSetDF | list[TSet] | None: - """ - Safe lookup variant that returns a default on miss. - - Parameters - ---------- - key : int | str | type[TSet] - Lookup key; see :meth:`__getitem__`. - default : AgentSetDF | list[TSet] | None, optional - Value to return when the lookup fails. For type keys, if no matches - are found and default is None, implementers should return [] to keep - list shape stable. - - Returns - ------- - AgentSetDF | list[TSet] | None - - int/str keys: return the set or default/None if missing - - type keys: return list of matching sets; if none and default is None, - return [] (stable list shape) - """ - - @abstractmethod - def first(self, t: type[TSet]) -> TSet: - """Return the first agent set matching a type. - - Parameters - ---------- - t : type[TSet] - The concrete class (or base class) to match. - - Returns - ------- - TSet - The first matching agent set in iteration order. - - Raises - ------ - KeyError - If no agent set matches ``t``. - - Examples - -------- - >>> agents.sets.first(Wolf) # doctest: +SKIP - - """ - - @abstractmethod - def all(self, t: type[TSet]) -> list[TSet]: - """Return all agent sets matching a type. - - Parameters - ---------- - t : type[TSet] - The concrete class (or base class) to match. - - Returns - ------- - list[TSet] - A list of all matching agent sets (possibly empty). - - Examples - -------- - >>> agents.sets.all(Wolf) # doctest: +SKIP - [, ] - """ - - @abstractmethod - def at(self, index: int) -> AgentSetDF: - """Return the agent set at a positional index. - - Parameters - ---------- - index : int - Positional index; negative indices are supported. - - Returns - ------- - AgentSetDF - The agent set at the given position. - - Raises - ------ - IndexError - If ``index`` is out of range. - - Examples - -------- - >>> agents.sets.at(0) is agents.sets[0] - True - """ - - @overload - @abstractmethod - def keys(self, *, key_by: Literal["name"]) -> Iterable[str]: ... - - @overload - @abstractmethod - def keys(self, *, key_by: Literal["index"]) -> Iterable[int]: ... - - @overload - @abstractmethod - def keys(self, *, key_by: Literal["type"]) -> Iterable[type[AgentSetDF]]: ... - - @abstractmethod - def keys(self, *, key_by: KeyBy = "name") -> Iterable[str | int | type[AgentSetDF]]: - """Iterate keys under a chosen key domain. - - Parameters - ---------- - key_by : KeyBy - - ``"name"`` → agent set names. (Default) - - ``"index"`` → positional indices. - - ``"type"`` → the concrete classes of each set. - - Returns - ------- - Iterable[str | int | type[AgentSetDF]] - An iterable of keys corresponding to the selected domain. - """ - - @overload - @abstractmethod - def items(self, *, key_by: Literal["name"]) -> Iterable[tuple[str, AgentSetDF]]: ... - - @overload - @abstractmethod - def items( - self, *, key_by: Literal["index"] - ) -> Iterable[tuple[int, AgentSetDF]]: ... - - @overload - @abstractmethod - def items( - self, *, key_by: Literal["type"] - ) -> Iterable[tuple[type[AgentSetDF], AgentSetDF]]: ... - - @abstractmethod - def items( - self, *, key_by: KeyBy = "name" - ) -> Iterable[tuple[str | int | type[AgentSetDF], AgentSetDF]]: - """Iterate ``(key, AgentSetDF)`` pairs under a chosen key domain. - - See :meth:`keys` for the meaning of ``key_by``. - """ - - @abstractmethod - def values(self) -> Iterable[AgentSetDF]: - """Iterate over agent set values only (no keys).""" - - @abstractmethod - def iter(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: - """Alias for :meth:`items` for convenience.""" - - @overload - @abstractmethod - def dict(self, *, key_by: Literal["name"]) -> dict[str, AgentSetDF]: ... - - @overload - @abstractmethod - def dict(self, *, key_by: Literal["index"]) -> dict[int, AgentSetDF]: ... - - @overload - @abstractmethod - def dict( - self, *, key_by: Literal["type"] - ) -> dict[type[AgentSetDF], AgentSetDF]: ... - - @abstractmethod - def dict( - self, *, key_by: KeyBy = "name" - ) -> dict[str | int | type[AgentSetDF], AgentSetDF]: - """Return a dictionary view keyed by the chosen domain. - - Notes - ----- - ``key_by="type"`` will keep the last set per type. For one-to-many - grouping, prefer the read-only :attr:`by_type` snapshot. - """ - - @property - @abstractmethod - def by_name(self) -> Mapping[str, AgentSetDF]: - """Read-only mapping of names to agent sets. - - Returns - ------- - Mapping[str, AgentSetDF] - An immutable snapshot that maps each agent set name to its object. - - Notes - ----- - Implementations should return a read-only mapping such as - ``types.MappingProxyType`` over an internal dict to avoid accidental - mutation. - - Examples - -------- - >>> sheep = agents.sets.by_name["Sheep"] # doctest: +SKIP - >>> sheep is agents.sets["Sheep"] # doctest: +SKIP - True - """ - - @property - @abstractmethod - def by_type(self) -> Mapping[type, list[AgentSetDF]]: - """Read-only mapping of types to lists of agent sets. - - Returns - ------- - Mapping[type, list[AgentSetDF]] - An immutable snapshot grouping agent sets by their concrete class. - - Notes - ----- - This supports one-to-many relationships where multiple sets share the - same type. Prefer this over ``mapping(key_by="type")`` when you need - grouping instead of last-write-wins semantics. - """ - - @abstractmethod - def rename( - self, - target: AgentSetDF - | str - | dict[AgentSetDF | str, str] - | list[tuple[AgentSetDF | str, str]], - new_name: str | None = None, - *, - on_conflict: Literal["canonicalize", "raise"] = "canonicalize", - mode: Literal["atomic", "best_effort"] = "atomic", - ) -> str | dict[AgentSetDF, str]: - """ - Rename agent sets. Supports single and batch renaming with deterministic conflict handling. - - Parameters - ---------- - target : AgentSetDF | str | dict[AgentSetDF | str, str] | list[tuple[AgentSetDF | str, str]] - Either: - - Single: AgentSet or name string (must provide new_name) - - Batch: {target: new_name} dict or [(target, new_name), ...] list - new_name : str | None, optional - New name (only used for single renames) - on_conflict : "Literal['canonicalize', 'raise']" - Conflict resolution: "canonicalize" (default) appends suffixes, "raise" raises ValueError - mode : "Literal['atomic', 'best_effort']" - Rename mode: "atomic" applies all or none (default), "best_effort" skips failed renames - - Returns - ------- - str | dict[AgentSetDF, str] - Single rename: final name string - Batch: {agentset: final_name} mapping - - Examples - -------- - Single rename: - >>> agents.sets.rename("old_name", "new_name") - - Batch rename (dict): - >>> agents.sets.rename({"set1": "new_name", "set2": "another_name"}) - - Batch rename (list): - >>> agents.sets.rename([("set1", "new_name"), ("set2", "another_name")]) - """ - - @abstractmethod - def __contains__(self, x: str | AgentSetDF) -> bool: - """Return ``True`` if a name or object is present. - - Parameters - ---------- - x : str | AgentSetDF - A name to test by equality, or an object to test by identity. - - Returns - ------- - bool - ``True`` if present, else ``False``. - """ - - @abstractmethod - def __len__(self) -> int: - """Return number of agent sets in the collection.""" - - @abstractmethod - def __iter__(self) -> Iterator[AgentSetDF]: - """Iterate over agent set values in insertion order.""" From 5f2f0b3bb3347098cd1618cf0b9082198923baa4 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 1 Sep 2025 08:40:19 +0200 Subject: [PATCH 058/329] Remove TestAgentSetsAccessor class and its associated tests from test_sets_accessor.py --- tests/test_sets_accessor.py | 202 ------------------------------------ 1 file changed, 202 deletions(-) delete mode 100644 tests/test_sets_accessor.py diff --git a/tests/test_sets_accessor.py b/tests/test_sets_accessor.py deleted file mode 100644 index 70ab0f64..00000000 --- a/tests/test_sets_accessor.py +++ /dev/null @@ -1,202 +0,0 @@ -from copy import copy, deepcopy - -import pytest - -from mesa_frames import AgentsDF, ModelDF -from tests.test_agentset import ( - ExampleAgentSetPolars, - fix1_AgentSetPolars, - fix2_AgentSetPolars, -) -from tests.test_agents import fix_AgentsDF - - -class TestAgentSetsAccessor: - def test___getitem__(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - # int - assert agents.sets[0] is s1 - assert agents.sets[1] is s2 - with pytest.raises(IndexError): - _ = agents.sets[2] - # str - assert agents.sets[s1.name] is s1 - assert agents.sets[s2.name] is s2 - with pytest.raises(KeyError): - _ = agents.sets["__missing__"] - # type → always list - lst = agents.sets[ExampleAgentSetPolars] - assert isinstance(lst, list) - assert s1 in lst and s2 in lst and len(lst) == 2 - # invalid key type → TypeError - # with pytest.raises(TypeError, match="Key must be int \\| str \\| type\\[AgentSetDF\\]"): - # _ = agents.sets[int] # int type not supported as key - # Temporary skip due to beartype issues - - def test_get(self, fix_AgentsDF): - agents = fix_AgentsDF - assert agents.sets.get("__missing__") is None - # Test get with int key and invalid index should return default - assert agents.sets.get(999) is None - # - # %# Fix the default type mismatch - for int key, default should be AgentSetDF or None - s1 = agents.sets[0] - assert agents.sets.get(999, default=s1) == s1 - - class Temp(ExampleAgentSetPolars): - pass - - assert agents.sets.get(Temp) == [] - assert agents.sets.get(Temp, default=None) == [] - assert agents.sets.get(Temp, default=["fallback"]) == ["fallback"] - - def test_first(self, fix_AgentsDF): - agents = fix_AgentsDF - assert agents.sets.first(ExampleAgentSetPolars) is agents.sets[0] - - class Temp(ExampleAgentSetPolars): - pass - - with pytest.raises(KeyError): - agents.sets.first(Temp) - - def test_all(self, fix_AgentsDF): - agents = fix_AgentsDF - assert agents.sets.all(ExampleAgentSetPolars) == [ - agents.sets[0], - agents.sets[1], - ] - - class Temp(ExampleAgentSetPolars): - pass - - assert agents.sets.all(Temp) == [] - - def test_at(self, fix_AgentsDF): - agents = fix_AgentsDF - assert agents.sets.at(0) is agents.sets[0] - assert agents.sets.at(1) is agents.sets[1] - - def test_keys(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - assert list(agents.sets.keys(key_by="index")) == [0, 1] - assert list(agents.sets.keys(key_by="name")) == [s1.name, s2.name] - assert list(agents.sets.keys(key_by="type")) == [type(s1), type(s2)] - # Invalid key_by - with pytest.raises( - ValueError, match="key_by must be 'name'\\|'index'\\|'type'" - ): - list(agents.sets.keys(key_by="invalid")) - - def test_items(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - assert list(agents.sets.items(key_by="index")) == [(0, s1), (1, s2)] - - def test_values(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - assert list(agents.sets.values()) == [s1, s2] - - def test_iter(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - assert list(agents.sets.iter(key_by="name")) == [(s1.name, s1), (s2.name, s2)] - - def test_dict(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - by_type_map = agents.sets.dict(key_by="type") - assert list(by_type_map.keys()) == [type(s1)] - assert by_type_map[type(s1)] is s2 - - def test_by_name(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - name_map = agents.sets.by_name - assert name_map[s1.name] is s1 - assert name_map[s2.name] is s2 - with pytest.raises(TypeError): - name_map["X"] = s1 # type: ignore[index] - - def test_by_type(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - grouped = agents.sets.by_type - assert list(grouped.keys()) == [type(s1)] - assert grouped[type(s1)] == [s1, s2] - - def test___contains__(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - assert s1.name in agents.sets - assert s2.name in agents.sets - assert s1 in agents.sets and s2 in agents.sets - # Invalid type returns False (simulate by testing the code path manually if needed) - - def test___len__(self, fix_AgentsDF): - agents = fix_AgentsDF - assert len(agents.sets) == 2 - - def test___iter__(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - assert list(iter(agents.sets)) == [s1, s2] - - def test_rename(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - original_name_1 = s1.name - original_name_2 = s2.name - - # Test single rename by name - new_name_1 = original_name_1 + "_renamed" - result = agents.sets.rename(original_name_1, new_name_1) - assert result == new_name_1 - assert s1.name == new_name_1 - - # Test single rename by object - new_name_2 = original_name_2 + "_modified" - result = agents.sets.rename(s2, new_name_2) - assert result == new_name_2 - assert s2.name == new_name_2 - - # Test batch rename (dict) - s3 = agents.sets[0] # Should be s1 after rename above - new_name_3 = "batch_test" - batch_result = agents.sets.rename({s2: new_name_3}) - assert batch_result[s2] == new_name_3 - assert s2.name == new_name_3 - - # Test batch rename (list) - s4 = agents.sets[0] - new_name_4 = "list_test" - list_result = agents.sets.rename([(s4, new_name_4)]) - assert list_result[s4] == new_name_4 - assert s4.name == new_name_4 - - def test_copy_and_deepcopy_rebinds_accessor(self, fix_AgentsDF): - agents = fix_AgentsDF - s1 = agents.sets[0] - s2 = agents.sets[1] - a2 = copy(agents) - acc2 = a2.sets # lazily created - assert acc2._parent is a2 - assert acc2 is not agents.sets - a3 = deepcopy(agents) - acc3 = a3.sets # lazily created - assert acc3._parent is a3 - assert acc3 is not agents.sets and acc3 is not acc2 From 4dff1d8d110bb5037e6795405e86d9636642ecfe Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 1 Sep 2025 10:55:10 +0200 Subject: [PATCH 059/329] Rename test class from Test_ModelDF to Test_Model for consistency --- tests/test_modeldf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_modeldf.py b/tests/test_modeldf.py index 82ff430d..34a7862b 100644 --- a/tests/test_modeldf.py +++ b/tests/test_modeldf.py @@ -10,7 +10,7 @@ def step(self): self.custom_step_count += 2 -class Test_ModelDF: +class Test_Model: def test_steps(self): model = Model() From 927014cf855e7059201cd5f149e4a4200b3a817e Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 1 Sep 2025 11:02:33 +0200 Subject: [PATCH 060/329] Add abstract agent set classes and concrete agent set registry implementation - Introduced AbstractAgentSet class for agent management with DataFrame operations. - Implemented AgentSetRegistry for managing collections of AbstractAgentSets. - Refactored AgentSetPolars to improve name handling and added name property. - Removed deprecated methods from ModelDF related to agent retrieval and types. --- mesa_frames/abstract/agentset.py | 415 +++++++++++++ mesa_frames/concrete/agentset.py | 16 +- mesa_frames/concrete/agentsetregistry.py | 710 +++++++++++++++++++++++ mesa_frames/concrete/model.py | 30 - 4 files changed, 1139 insertions(+), 32 deletions(-) create mode 100644 mesa_frames/abstract/agentset.py create mode 100644 mesa_frames/concrete/agentsetregistry.py diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py new file mode 100644 index 00000000..e7453801 --- /dev/null +++ b/mesa_frames/abstract/agentset.py @@ -0,0 +1,415 @@ +""" +Abstract base classes for agent sets in mesa-frames. + +This module defines the core abstractions for agent sets in the mesa-frames +extension. It provides the foundation for implementing agent set storage and +manipulation. + +Classes: + AbstractAgentSet: + An abstract base class for agent sets that combines agent container + functionality with DataFrame operations. It inherits from both + AbstractAgentSetRegistry and DataFrameMixin to provide comprehensive + agent management capabilities. + +This abstract class is designed to be subclassed to create concrete +implementations that use specific DataFrame backends. +""" + +from __future__ import annotations + +from abc import abstractmethod +from collections.abc import Collection, Iterable, Iterator +from typing import Any, Literal, Self, overload + +from mesa_frames.abstract.agentsetregistry import AbstractAgentSetRegistry +from mesa_frames.abstract.mixin import DataFrameMixin +from mesa_frames.types_ import ( + AgentMask, + BoolSeries, + DataFrame, + DataFrameInput, + IdsLike, + Index, + Series, +) + + +class AbstractAgentSet(AbstractAgentSetRegistry, DataFrameMixin): + """The AbstractAgentSet class is a container for agents of the same type. + + Parameters + ---------- + model : mesa_frames.concrete.model.Model + The model that the agent set belongs to. + """ + + _df: DataFrame # The agents in the AbstractAgentSet + _mask: AgentMask # The underlying mask used for the active agents in the AbstractAgentSet. + _model: ( + mesa_frames.concrete.model.Model + ) # The model that the AbstractAgentSet belongs to. + + @abstractmethod + def __init__(self, model: mesa_frames.concrete.model.Model) -> None: ... + + @abstractmethod + def add( + self, + agents: DataFrame | DataFrameInput, + inplace: bool = True, + ) -> Self: + """Add agents to the AbstractAgentSet. + + Agents can be the input to the DataFrame constructor. So, the input can be: + - A DataFrame: adds the agents from the DataFrame. + - A DataFrameInput: passes the input to the DataFrame constructor. + + Parameters + ---------- + agents : DataFrame | DataFrameInput + The agents to add. + inplace : bool, optional + If True, perform the operation in place, by default True + + Returns + ------- + Self + A new AbstractAgentSetRegistry with the added agents. + """ + ... + + def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: + """Remove an agent from the AbstractAgentSet. Does not raise an error if the agent is not found. + + Parameters + ---------- + agents : IdsLike | AgentMask + The ids to remove + inplace : bool, optional + Whether to remove the agent in place, by default True + + Returns + ------- + Self + The updated AbstractAgentSet. + """ + return super().discard(agents, inplace) + + @overload + def do( + self, + method_name: str, + *args, + mask: AgentMask | None = None, + return_results: Literal[False] = False, + inplace: bool = True, + **kwargs, + ) -> Self: ... + + @overload + def do( + self, + method_name: str, + *args, + mask: AgentMask | None = None, + return_results: Literal[True], + inplace: bool = True, + **kwargs, + ) -> Any: ... + + def do( + self, + method_name: str, + *args, + mask: AgentMask | None = None, + return_results: bool = False, + inplace: bool = True, + **kwargs, + ) -> Self | Any: + masked_df = self._get_masked_df(mask) + # If the mask is empty, we can use the object as is + if len(masked_df) == len(self._df): + obj = self._get_obj(inplace) + method = getattr(obj, method_name) + result = method(*args, **kwargs) + else: # If the mask is not empty, we need to create a new masked AbstractAgentSet and concatenate the AbstractAgentSets at the end + obj = self._get_obj(inplace=False) + obj._df = masked_df + original_masked_index = obj._get_obj_copy(obj.index) + method = getattr(obj, method_name) + result = method(*args, **kwargs) + obj._concatenate_agentsets( + [self], + duplicates_allowed=True, + keep_first_only=True, + original_masked_index=original_masked_index, + ) + if inplace: + for key, value in obj.__dict__.items(): + setattr(self, key, value) + obj = self + if return_results: + return result + else: + return obj + + @abstractmethod + @overload + def get( + self, + attr_names: str, + mask: AgentMask | None = None, + ) -> Series: ... + + @abstractmethod + @overload + def get( + self, + attr_names: Collection[str] | None = None, + mask: AgentMask | None = None, + ) -> DataFrame: ... + + @abstractmethod + def get( + self, + attr_names: str | Collection[str] | None = None, + mask: AgentMask | None = None, + ) -> Series | DataFrame: ... + + @abstractmethod + def step(self) -> None: + """Run a single step of the AbstractAgentSet. This method should be overridden by subclasses.""" + ... + + def remove(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: + if isinstance(agents, str) and agents == "active": + agents = self.active_agents + if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): + return self._get_obj(inplace) + agents = self._df_index(self._get_masked_df(agents), "unique_id") + sets = self.model.sets.remove(agents, inplace=inplace) + # TODO: Refactor AgentSetRegistry to return dict[str, AbstractAgentSet] instead of dict[AbstractAgentSet, DataFrame] + # And assign a name to AbstractAgentSet? This has to be replaced by a nicer API of AgentSetRegistry + for agentset in sets.df.keys(): + if isinstance(agentset, self.__class__): + return agentset + return self + + @abstractmethod + def _concatenate_agentsets( + self, + objs: Iterable[Self], + duplicates_allowed: bool = True, + keep_first_only: bool = True, + original_masked_index: Index | None = None, + ) -> Self: ... + + @abstractmethod + def _get_bool_mask(self, mask: AgentMask) -> BoolSeries: + """Get the equivalent boolean mask based on the input mask. + + Parameters + ---------- + mask : AgentMask + + Returns + ------- + BoolSeries + """ + ... + + @abstractmethod + def _get_masked_df(self, mask: AgentMask) -> DataFrame: + """Get the df filtered by the input mask. + + Parameters + ---------- + mask : AgentMask + + Returns + ------- + DataFrame + """ + + @overload + @abstractmethod + def _get_obj_copy(self, obj: DataFrame) -> DataFrame: ... + + @overload + @abstractmethod + def _get_obj_copy(self, obj: Series) -> Series: ... + + @overload + @abstractmethod + def _get_obj_copy(self, obj: Index) -> Index: ... + + @abstractmethod + def _get_obj_copy( + self, obj: DataFrame | Series | Index + ) -> DataFrame | Series | Index: ... + + @abstractmethod + def _discard(self, ids: IdsLike) -> Self: + """Remove an agent from the DataFrame of the AbstractAgentSet. Gets called by self.model.sets.remove and self.model.sets.discard. + + Parameters + ---------- + ids : IdsLike + + The ids to remove + + Returns + ------- + Self + """ + ... + + @abstractmethod + def _update_mask( + self, original_active_indices: Index, new_active_indices: Index | None = None + ) -> None: ... + + def __add__(self, other: DataFrame | DataFrameInput) -> Self: + """Add agents to a new AbstractAgentSet through the + operator. + + Other can be: + - A DataFrame: adds the agents from the DataFrame. + - A DataFrameInput: passes the input to the DataFrame constructor. + + Parameters + ---------- + other : DataFrame | DataFrameInput + The agents to add. + + Returns + ------- + Self + A new AbstractAgentSetRegistry with the added agents. + """ + return super().__add__(other) + + def __iadd__(self, other: DataFrame | DataFrameInput) -> Self: + """ + Add agents to the AbstractAgentSet through the += operator. + + Other can be: + - A DataFrame: adds the agents from the DataFrame. + - A DataFrameInput: passes the input to the DataFrame constructor. + + Parameters + ---------- + other : DataFrame | DataFrameInput + The agents to add. + + Returns + ------- + Self + The updated AbstractAgentSetRegistry. + """ + return super().__iadd__(other) + + @abstractmethod + def __getattr__(self, name: str) -> Any: + if __debug__: # Only execute in non-optimized mode + if name == "_df": + raise AttributeError( + "The _df attribute is not set. You probably forgot to call super().__init__ in the __init__ method." + ) + + @overload + def __getitem__(self, key: str | tuple[AgentMask, str]) -> Series | DataFrame: ... + + @overload + def __getitem__( + self, + key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], + ) -> DataFrame: ... + + def __getitem__( + self, + key: ( + str + | Collection[str] + | AgentMask + | tuple[AgentMask, str] + | tuple[AgentMask, Collection[str]] + ), + ) -> Series | DataFrame: + attr = super().__getitem__(key) + assert isinstance(attr, (Series, DataFrame, Index)) + return attr + + def __len__(self) -> int: + return len(self._df) + + def __repr__(self) -> str: + return f"{self.__class__.__name__}\n {str(self._df)}" + + def __str__(self) -> str: + return f"{self.__class__.__name__}\n {str(self._df)}" + + def __reversed__(self) -> Iterator: + return reversed(self._df) + + @property + def df(self) -> DataFrame: + return self._df + + @df.setter + def df(self, agents: DataFrame) -> None: + """Set the agents in the AbstractAgentSet. + + Parameters + ---------- + agents : DataFrame + The agents to set. + """ + self._df = agents + + @property + @abstractmethod + def active_agents(self) -> DataFrame: ... + + @property + @abstractmethod + def inactive_agents(self) -> DataFrame: ... + + @property + def index(self) -> Index: ... + + @property + def pos(self) -> DataFrame: + if self.space is None: + raise AttributeError( + "Attempted to access `pos`, but the model has no space attached." + ) + pos = self._df_get_masked_df( + df=self.space.agents, index_cols="agent_id", mask=self.index + ) + pos = self._df_reindex( + pos, self.index, new_index_cols="unique_id", original_index_cols="agent_id" + ) + return pos + + @property + def name(self) -> str | None: + """The name of the agent set. + + Returns + ------- + str | None + The name of the agent set, or None if not set. + """ + return getattr(self, '_name', None) + + @name.setter + def name(self, value: str) -> None: + """Set the name of the agent set. + + Parameters + ---------- + value : str + The name to set. + """ + self._name = value diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 0ab0056e..0fbaa899 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -102,7 +102,7 @@ def __init__( self._name = ( name if name is not None - else camel_case_to_snake_case(self.__class__.__name__) + else self.__class__.__name__ ) # No definition of schema with unique_id, as it becomes hard to add new agents self._df = pl.DataFrame() @@ -507,7 +507,9 @@ def _update_mask( else: self._mask = self._df["unique_id"].is_in(original_active_indices) - def __getattr__(self, key: str) -> pl.Series: + def __getattr__(self, key: str) -> Any: + if key == "name": + return self.name super().__getattr__(key) return self._df[key] @@ -590,3 +592,13 @@ def index(self) -> pl.Series: @property def pos(self) -> pl.DataFrame: return super().pos + + @property + def name(self) -> str | None: + """Return the name of the AgentSet.""" + return self._name + + @name.setter + def name(self, value: str) -> None: + """Set the name of the AgentSet.""" + self._name = value diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py new file mode 100644 index 00000000..a74ba0d2 --- /dev/null +++ b/mesa_frames/concrete/agentsetregistry.py @@ -0,0 +1,710 @@ +""" +Concrete implementation of the agents collection for mesa-frames. + +This module provides the concrete implementation of the agents collection class +for the mesa-frames library. It defines the AgentSetRegistry class, which serves as a +container for all agent sets in a model, leveraging DataFrame-based storage for +improved performance. + +Classes: + AgentSetRegistry(AbstractAgentSetRegistry): + A collection of AbstractAgentSets. This class acts as a container for all + agents in the model, organizing them into separate AbstractAgentSet instances + based on their types. + +The AgentSetRegistry class is designed to be used within Model instances to manage +all agents in the simulation. It provides methods for adding, removing, and +accessing agents and agent sets, while taking advantage of the performance +benefits of DataFrame-based agent storage. + +Usage: + The AgentSetRegistry class is typically instantiated and used within a Model subclass: + + from mesa_frames.concrete.model import Model + from mesa_frames.concrete.agents import AgentSetRegistry + from mesa_frames.concrete import AgentSet + + class MyCustomModel(Model): + def __init__(self): + super().__init__() + # Adding agent sets to the collection + self.sets += AgentSet(self) + self.sets += AnotherAgentSet(self) + + def step(self): + # Step all agent sets + self.sets.do("step") + +Note: + This concrete implementation builds upon the abstract AbstractAgentSetRegistry class + defined in the mesa_frames.abstract package, providing a ready-to-use + agents collection that integrates with the DataFrame-based agent storage system. + +For more detailed information on the AgentSetRegistry class and its methods, refer to +the class docstring. +""" + +from __future__ import annotations # For forward references + +from collections import defaultdict +from collections.abc import Callable, Collection, Iterable, Iterator, Sequence +from typing import Any, Literal, Self, cast, overload + +import numpy as np +import polars as pl + +from mesa_frames.abstract.agentset import AbstractAgentSet +from mesa_frames.abstract.agentsetregistry import ( + AbstractAgentSetRegistry, +) +from mesa_frames.types_ import ( + AgentMask, + AgnosticAgentMask, + BoolSeries, + DataFrame, + IdsLike, + Index, + Series, +) + + +class AgentSetRegistry(AbstractAgentSetRegistry): + """A collection of AbstractAgentSets. All agents of the model are stored here.""" + + _agentsets: list[AbstractAgentSet] + _ids: pl.Series + + def __init__(self, model: mesa_frames.concrete.model.Model) -> None: + """Initialize a new AgentSetRegistry. + + Parameters + ---------- + model : mesa_frames.concrete.model.Model + The model associated with the AgentSetRegistry. + """ + self._model = model + self._agentsets = [] + self._ids = pl.Series(name="unique_id", dtype=pl.UInt64) + + def add( + self, + agents: AbstractAgentSet | Iterable[AbstractAgentSet], + inplace: bool = True, + ) -> Self: + """Add an AbstractAgentSet to the AgentSetRegistry. + + Parameters + ---------- + agents : AbstractAgentSet | Iterable[AbstractAgentSet] + The AbstractAgentSets to add. + inplace : bool, optional + Whether to add the AbstractAgentSets in place. Defaults to True. + + Returns + ------- + Self + The updated AgentSetRegistry. + + Raises + ------ + ValueError + If any AbstractAgentSets are already present or if IDs are not unique. + """ + obj = self._get_obj(inplace) + other_list = obj._return_agentsets_list(agents) + if obj._check_agentsets_presence(other_list).any(): + raise ValueError( + "Some agentsets are already present in the AgentSetRegistry." + ) + for agentset in other_list: + # Set name if not already set, using class name + if agentset.name is None: + base_name = agentset.__class__.__name__ + name = obj._generate_name(base_name) + agentset.name = name + new_ids = pl.concat( + [obj._ids] + [pl.Series(agentset["unique_id"]) for agentset in other_list] + ) + if new_ids.is_duplicated().any(): + raise ValueError("Some of the agent IDs are not unique.") + obj._agentsets.extend(other_list) + obj._ids = new_ids + return obj + + @overload + def contains(self, agents: int | AbstractAgentSet) -> bool: ... + + @overload + def contains(self, agents: IdsLike | Iterable[AbstractAgentSet]) -> pl.Series: ... + + def contains( + self, agents: IdsLike | AbstractAgentSet | Iterable[AbstractAgentSet] + ) -> bool | pl.Series: + if isinstance(agents, int): + return agents in self._ids + elif isinstance(agents, AbstractAgentSet): + return self._check_agentsets_presence([agents]).any() + elif isinstance(agents, Iterable): + if len(agents) == 0: + return True + elif isinstance(next(iter(agents)), AbstractAgentSet): + agents = cast(Iterable[AbstractAgentSet], agents) + return self._check_agentsets_presence(list(agents)) + else: # IdsLike + agents = cast(IdsLike, agents) + + return pl.Series(agents, dtype=pl.UInt64).is_in(self._ids) + + @overload + def do( + self, + method_name: str, + *args, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + return_results: Literal[False] = False, + inplace: bool = True, + **kwargs, + ) -> Self: ... + + @overload + def do( + self, + method_name: str, + *args, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + return_results: Literal[True], + inplace: bool = True, + **kwargs, + ) -> dict[AbstractAgentSet, Any]: ... + + def do( + self, + method_name: str, + *args, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + return_results: bool = False, + inplace: bool = True, + **kwargs, + ) -> Self | Any: + obj = self._get_obj(inplace) + agentsets_masks = obj._get_bool_masks(mask) + if return_results: + return { + agentset: agentset.do( + method_name, + *args, + mask=mask, + return_results=return_results, + **kwargs, + inplace=inplace, + ) + for agentset, mask in agentsets_masks.items() + } + else: + obj._agentsets = [ + agentset.do( + method_name, + *args, + mask=mask, + return_results=return_results, + **kwargs, + inplace=inplace, + ) + for agentset, mask in agentsets_masks.items() + ] + return obj + + def get( + self, + attr_names: str | Collection[str] | None = None, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + ) -> dict[AbstractAgentSet, Series] | dict[AbstractAgentSet, DataFrame]: + agentsets_masks = self._get_bool_masks(mask) + result = {} + + # Convert attr_names to list for consistent checking + if attr_names is None: + # None means get all data - no column filtering needed + required_columns = [] + elif isinstance(attr_names, str): + required_columns = [attr_names] + else: + required_columns = list(attr_names) + + for agentset, mask in agentsets_masks.items(): + # Fast column existence check - no data processing, just property access + agentset_columns = agentset.df.columns + + # Check if all required columns exist in this agent set + if not required_columns or all( + col in agentset_columns for col in required_columns + ): + result[agentset] = agentset.get(attr_names, mask) + + return result + + def remove( + self, + agents: AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike, + inplace: bool = True, + ) -> Self: + obj = self._get_obj(inplace) + if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): + return obj + if isinstance(agents, AbstractAgentSet): + agents = [agents] + if isinstance(agents, Iterable) and isinstance( + next(iter(agents)), AbstractAgentSet + ): + # We have to get the index of the original AbstractAgentSet because the copy made AbstractAgentSets with different hash + ids = [self._agentsets.index(agentset) for agentset in iter(agents)] + ids.sort(reverse=True) + removed_ids = pl.Series(dtype=pl.UInt64) + for id in ids: + removed_ids = pl.concat( + [ + removed_ids, + pl.Series(obj._agentsets[id]["unique_id"], dtype=pl.UInt64), + ] + ) + obj._agentsets.pop(id) + + else: # IDsLike + if isinstance(agents, (int, np.uint64)): + agents = [agents] + elif isinstance(agents, DataFrame): + agents = agents["unique_id"] + removed_ids = pl.Series(agents, dtype=pl.UInt64) + deleted = 0 + + for agentset in obj._agentsets: + initial_len = len(agentset) + agentset._discard(removed_ids) + deleted += initial_len - len(agentset) + if deleted == len(removed_ids): + break + if deleted < len(removed_ids): # TODO: fix type hint + raise KeyError( + "There exist some IDs which are not present in any agentset" + ) + try: + obj.space.remove_agents(removed_ids, inplace=True) + except ValueError: + pass + obj._ids = obj._ids.filter(obj._ids.is_in(removed_ids).not_()) + return obj + + def select( + self, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + filter_func: Callable[[AbstractAgentSet], AgentMask] | None = None, + n: int | None = None, + inplace: bool = True, + negate: bool = False, + ) -> Self: + obj = self._get_obj(inplace) + agentsets_masks = obj._get_bool_masks(mask) + if n is not None: + n = n // len(agentsets_masks) + obj._agentsets = [ + agentset.select( + mask=mask, filter_func=filter_func, n=n, negate=negate, inplace=inplace + ) + for agentset, mask in agentsets_masks.items() + ] + return obj + + def set( + self, + attr_names: str | dict[AbstractAgentSet, Any] | Collection[str], + values: Any | None = None, + mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + inplace: bool = True, + ) -> Self: + obj = self._get_obj(inplace) + agentsets_masks = obj._get_bool_masks(mask) + if isinstance(attr_names, dict): + for agentset, values in attr_names.items(): + if not inplace: + # We have to get the index of the original AbstractAgentSet because the copy made AbstractAgentSets with different hash + id = self._agentsets.index(agentset) + agentset = obj._agentsets[id] + agentset.set( + attr_names=values, mask=agentsets_masks[agentset], inplace=True + ) + else: + obj._agentsets = [ + agentset.set( + attr_names=attr_names, values=values, mask=mask, inplace=True + ) + for agentset, mask in agentsets_masks.items() + ] + return obj + + def shuffle(self, inplace: bool = True) -> Self: + obj = self._get_obj(inplace) + obj._agentsets = [agentset.shuffle(inplace=True) for agentset in obj._agentsets] + return obj + + def sort( + self, + by: str | Sequence[str], + ascending: bool | Sequence[bool] = True, + inplace: bool = True, + **kwargs, + ) -> Self: + obj = self._get_obj(inplace) + obj._agentsets = [ + agentset.sort(by=by, ascending=ascending, inplace=inplace, **kwargs) + for agentset in obj._agentsets + ] + return obj + + def step(self, inplace: bool = True) -> Self: + """Advance the state of the agents in the AgentSetRegistry by one step. + + Parameters + ---------- + inplace : bool, optional + Whether to update the AgentSetRegistry in place, by default True + + Returns + ------- + Self + """ + obj = self._get_obj(inplace) + for agentset in obj._agentsets: + agentset.step() + return obj + + def _check_ids_presence(self, other: list[AbstractAgentSet]) -> pl.DataFrame: + """Check if the IDs of the agents to be added are unique. + + Parameters + ---------- + other : list[AbstractAgentSet] + The AbstractAgentSets to check. + + Returns + ------- + pl.DataFrame + A DataFrame with the unique IDs and a boolean column indicating if they are present. + """ + presence_df = pl.DataFrame( + data={"unique_id": self._ids, "present": True}, + schema={"unique_id": pl.UInt64, "present": pl.Boolean}, + ) + for agentset in other: + new_ids = pl.Series(agentset.index, dtype=pl.UInt64) + presence_df = pl.concat( + [ + presence_df, + ( + new_ids.is_in(presence_df["unique_id"]) + .to_frame("present") + .with_columns(unique_id=new_ids) + .select(["unique_id", "present"]) + ), + ] + ) + presence_df = presence_df.slice(self._ids.len()) + return presence_df + + def _check_agentsets_presence(self, other: list[AbstractAgentSet]) -> pl.Series: + """Check if the agent sets to be added are already present in the AgentSetRegistry. + + Parameters + ---------- + other : list[AbstractAgentSet] + The AbstractAgentSets to check. + + Returns + ------- + pl.Series + A boolean Series indicating if the agent sets are present. + + Raises + ------ + ValueError + If the agent sets are already present in the AgentSetRegistry. + """ + other_set = set(other) + return pl.Series( + [agentset in other_set for agentset in self._agentsets], dtype=pl.Boolean + ) + + def _get_bool_masks( + self, + mask: (AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask]) = None, + ) -> dict[AbstractAgentSet, BoolSeries]: + return_dictionary = {} + if not isinstance(mask, dict): + # No need to convert numpy integers - let polars handle them directly + mask = {agentset: mask for agentset in self._agentsets} + for agentset, mask_value in mask.items(): + return_dictionary[agentset] = agentset._get_bool_mask(mask_value) + return return_dictionary + + def _return_agentsets_list( + self, agentsets: AbstractAgentSet | Iterable[AbstractAgentSet] + ) -> list[AbstractAgentSet]: + """Convert the agentsets to a list of AbstractAgentSet. + + Parameters + ---------- + agentsets : AbstractAgentSet | Iterable[AbstractAgentSet] + + Returns + ------- + list[AbstractAgentSet] + """ + return ( + [agentsets] if isinstance(agentsets, AbstractAgentSet) else list(agentsets) + ) + + def __add__(self, other: AbstractAgentSet | Iterable[AbstractAgentSet]) -> Self: + """Add AbstractAgentSets to a new AgentSetRegistry through the + operator. + + Parameters + ---------- + other : AbstractAgentSet | Iterable[AbstractAgentSet] + The AbstractAgentSets to add. + + Returns + ------- + Self + A new AgentSetRegistry with the added AbstractAgentSets. + """ + return super().__add__(other) + + def keys(self) -> Iterator[str]: + """Return an iterator over the names of the agent sets.""" + for agentset in self._agentsets: + if agentset.name is not None: + yield agentset.name + + def names(self) -> list[str]: + """Return a list of the names of the agent sets.""" + return list(self.keys()) + + def items(self) -> Iterator[tuple[str, AbstractAgentSet]]: + """Return an iterator over (name, agentset) pairs.""" + for agentset in self._agentsets: + if agentset.name is not None: + yield agentset.name, agentset + + def __contains__(self, name: object) -> bool: + """Check if a name is in the registry.""" + if not isinstance(name, str): + return False + return name in [agentset.name for agentset in self._agentsets if agentset.name is not None] + + def __getitem__(self, key: str) -> AbstractAgentSet: + """Get an agent set by name.""" + if isinstance(key, str): + for agentset in self._agentsets: + if agentset.name == key: + return agentset + raise KeyError(f"Agent set '{key}' not found") + return super().__getitem__(key) + + def _generate_name(self, base_name: str) -> str: + """Generate a unique name for an agent set.""" + existing_names = [agentset.name for agentset in self._agentsets if agentset.name is not None] + if base_name not in existing_names: + return base_name + counter = 1 + candidate = f"{base_name}_{counter}" + while candidate in existing_names: + counter += 1 + candidate = f"{base_name}_{counter}" + return candidate + + def __getattr__(self, name: str) -> dict[AbstractAgentSet, Any]: + # Handle special mapping methods + if name in ("keys", "items", "values"): + raise AttributeError( + f"'{self.__class__.__name__}' object has no attribute '{name}'" + ) + # Avoid delegating container-level attributes to agentsets + if name in ("df", "active_agents", "inactive_agents", "index", "pos"): + raise AttributeError( + f"'{self.__class__.__name__}' object has no attribute '{name}'" + ) + # Avoids infinite recursion of private attributes + if __debug__: # Only execute in non-optimized mode + if name.startswith("_"): + raise AttributeError( + f"'{self.__class__.__name__}' object has no attribute '{name}'" + ) + return {agentset: getattr(agentset, name) for agentset in self._agentsets} + + @overload + def __getitem__( + self, key: str | tuple[dict[AbstractAgentSet, AgentMask], str] + ) -> dict[AbstractAgentSet, Series | pl.Expr]: ... + + @overload + def __getitem__( + self, + key: ( + Collection[str] + | AgnosticAgentMask + | IdsLike + | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] + ), + ) -> dict[AbstractAgentSet, DataFrame]: ... + + def __getitem__( + self, + key: ( + str + | Collection[str] + | AgnosticAgentMask + | IdsLike + | tuple[dict[AbstractAgentSet, AgentMask], str] + | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] + ), + ) -> dict[AbstractAgentSet, Series | pl.Expr] | dict[AbstractAgentSet, DataFrame]: + return super().__getitem__(key) + + def __iadd__(self, agents: AbstractAgentSet | Iterable[AbstractAgentSet]) -> Self: + """Add AbstractAgentSets to the AgentSetRegistry through the += operator. + + Parameters + ---------- + agents : AbstractAgentSet | Iterable[AbstractAgentSet] + The AbstractAgentSets to add. + + Returns + ------- + Self + The updated AgentSetRegistry. + """ + return super().__iadd__(agents) + + def __iter__(self) -> Iterator[dict[str, Any]]: + return (agent for agentset in self._agentsets for agent in iter(agentset)) + + def __isub__( + self, agents: AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike + ) -> Self: + """Remove AbstractAgentSets from the AgentSetRegistry through the -= operator. + + Parameters + ---------- + agents : AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike + The AbstractAgentSets or agent IDs to remove. + + Returns + ------- + Self + The updated AgentSetRegistry. + """ + return super().__isub__(agents) + + def __len__(self) -> int: + return sum(len(agentset._df) for agentset in self._agentsets) + + def __repr__(self) -> str: + return "\n".join([repr(agentset) for agentset in self._agentsets]) + + def __reversed__(self) -> Iterator: + return ( + agent + for agentset in self._agentsets + for agent in reversed(agentset._backend) + ) + + def __setitem__( + self, + key: ( + str + | Collection[str] + | AgnosticAgentMask + | IdsLike + | tuple[dict[AbstractAgentSet, AgentMask], str] + | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] + ), + values: Any, + ) -> None: + super().__setitem__(key, values) + + def __str__(self) -> str: + return "\n".join([str(agentset) for agentset in self._agentsets]) + + def __sub__( + self, agents: AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike + ) -> Self: + """Remove AbstractAgentSets from a new AgentSetRegistry through the - operator. + + Parameters + ---------- + agents : AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike + The AbstractAgentSets or agent IDs to remove. Supports NumPy integer types. + + Returns + ------- + Self + A new AgentSetRegistry with the removed AbstractAgentSets. + """ + return super().__sub__(agents) + + @property + def df(self) -> dict[AbstractAgentSet, DataFrame]: + return {agentset: agentset.df for agentset in self._agentsets} + + @df.setter + def df(self, other: Iterable[AbstractAgentSet]) -> None: + """Set the agents in the AgentSetRegistry. + + Parameters + ---------- + other : Iterable[AbstractAgentSet] + The AbstractAgentSets to set. + """ + self._agentsets = list(other) + + @property + def active_agents(self) -> dict[AbstractAgentSet, DataFrame]: + return {agentset: agentset.active_agents for agentset in self._agentsets} + + @active_agents.setter + def active_agents( + self, agents: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] + ) -> None: + self.select(agents, inplace=True) + + @property + def agentsets_by_type(self) -> dict[type[AbstractAgentSet], Self]: + """Get the agent sets in the AgentSetRegistry grouped by type. + + Returns + ------- + dict[type[AbstractAgentSet], Self] + A dictionary mapping agent set types to the corresponding AgentSetRegistry. + """ + + def copy_without_agentsets() -> Self: + return self.copy(deep=False, skip=["_agentsets"]) + + dictionary = defaultdict(copy_without_agentsets) + + for agentset in self._agentsets: + agents_df = dictionary[agentset.__class__] + agents_df._agentsets = [] + agents_df._agentsets = agents_df._agentsets + [agentset] + dictionary[agentset.__class__] = agents_df + return dictionary + + @property + def inactive_agents(self) -> dict[AbstractAgentSet, DataFrame]: + return {agentset: agentset.inactive_agents for agentset in self._agentsets} + + @property + def index(self) -> dict[AbstractAgentSet, Index]: + return {agentset: agentset.index for agentset in self._agentsets} + + @property + def pos(self) -> dict[AbstractAgentSet, DataFrame]: + return {agentset: agentset.pos for agentset in self._agentsets} diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index 7b627c87..a3b6200f 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -99,26 +99,6 @@ def steps(self) -> int: """Get the current step count.""" return self._steps - def get_agents_of_type(self, agent_type: type) -> AgentSetDF: - """Retrieve the AgentSetDF of a specified type. - - Parameters - ---------- - agent_type : type - The type of AgentSetDF to retrieve. - - Returns - ------- - AgentSetDF - The AgentSetDF of the specified type. - """ - try: - return self.agents.sets[agent_type] - except KeyError as e: - raise ValueError( - f"No agents of type {agent_type} found in the model." - ) from e - def reset_randomizer(self, seed: int | Sequence[int] | None) -> None: """Reset the model random number generator. @@ -189,16 +169,6 @@ def agents(self, agents: AgentsDF) -> None: self._agents = agents - @property - def agent_types(self) -> list[type]: - """Get a list of different agent types present in the model. - - Returns - ------- - list[type] - A list of the different agent types present in the model. - """ - return [agent.__class__ for agent in self.agents.sets] @property def space(self) -> SpaceDF: From 1396bc0d22f07e0ca2b0b328cf32adb8bff33d25 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Sep 2025 10:49:42 +0000 Subject: [PATCH 061/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa_frames/abstract/agentset.py | 2 +- mesa_frames/concrete/agentset.py | 6 +----- mesa_frames/concrete/agentsetregistry.py | 8 ++++++-- mesa_frames/concrete/model.py | 1 - 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index e7453801..032e448d 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -401,7 +401,7 @@ def name(self) -> str | None: str | None The name of the agent set, or None if not set. """ - return getattr(self, '_name', None) + return getattr(self, "_name", None) @name.setter def name(self, value: str) -> None: diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 0fbaa899..5a15c423 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -99,11 +99,7 @@ def __init__( # Model reference self._model = model # Set proposed name (no uniqueness guarantees here) - self._name = ( - name - if name is not None - else self.__class__.__name__ - ) + self._name = name if name is not None else self.__class__.__name__ # No definition of schema with unique_id, as it becomes hard to add new agents self._df = pl.DataFrame() self._mask = pl.repeat(True, len(self._df), dtype=pl.Boolean, eager=True) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index a74ba0d2..4879a446 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -497,7 +497,9 @@ def __contains__(self, name: object) -> bool: """Check if a name is in the registry.""" if not isinstance(name, str): return False - return name in [agentset.name for agentset in self._agentsets if agentset.name is not None] + return name in [ + agentset.name for agentset in self._agentsets if agentset.name is not None + ] def __getitem__(self, key: str) -> AbstractAgentSet: """Get an agent set by name.""" @@ -510,7 +512,9 @@ def __getitem__(self, key: str) -> AbstractAgentSet: def _generate_name(self, base_name: str) -> str: """Generate a unique name for an agent set.""" - existing_names = [agentset.name for agentset in self._agentsets if agentset.name is not None] + existing_names = [ + agentset.name for agentset in self._agentsets if agentset.name is not None + ] if base_name not in existing_names: return base_name counter = 1 diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index a3b6200f..0c16adb3 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -169,7 +169,6 @@ def agents(self, agents: AgentsDF) -> None: self._agents = agents - @property def space(self) -> SpaceDF: """Get the space object associated with the model. From fccf344567ab40caaf727bca3bf952cc9ee618d1 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 1 Sep 2025 15:54:57 +0200 Subject: [PATCH 062/329] Refactor GridPolars to Grid and update related references across the codebase for consistency and clarity --- ROADMAP.md | 2 +- docs/api/reference/space/index.rst | 2 +- docs/general/user-guide/1_classes.md | 4 +- examples/sugarscape_ig/ss_polars/model.py | 4 +- mesa_frames/__init__.py | 16 +-- mesa_frames/abstract/__init__.py | 6 +- mesa_frames/abstract/space.py | 32 +++--- mesa_frames/concrete/__init__.py | 12 +-- mesa_frames/concrete/mixin.py | 2 +- mesa_frames/concrete/model.py | 10 +- mesa_frames/concrete/space.py | 22 ++-- tests/test_agentset.py | 6 +- tests/test_grid.py | 124 +++++++++++----------- 13 files changed, 121 insertions(+), 121 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 03f3040c..c8447773 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -49,7 +49,7 @@ The Sugarscape example demonstrates the need for this abstraction, as multiple a #### Progress and Next Steps -- Create utility functions in `DiscreteSpaceDF` and `AbstractAgentSetRegistry` to move agents optimally based on specified attributes +- Create utility functions in `AbstractDiscreteSpace` and `AbstractAgentSetRegistry` to move agents optimally based on specified attributes - Provide built-in resolution strategies for common concurrency scenarios - Ensure the implementation works efficiently with the vectorized approach of mesa-frames diff --git a/docs/api/reference/space/index.rst b/docs/api/reference/space/index.rst index e2afa319..8741b6b6 100644 --- a/docs/api/reference/space/index.rst +++ b/docs/api/reference/space/index.rst @@ -4,7 +4,7 @@ This page provides a high-level overview of possible space objects for mesa-fram .. currentmodule:: mesa_frames -.. autoclass:: GridPolars +.. autoclass:: Grid :members: :inherited-members: :autosummary: diff --git a/docs/general/user-guide/1_classes.md b/docs/general/user-guide/1_classes.md index f2b53b8e..b772e248 100644 --- a/docs/general/user-guide/1_classes.md +++ b/docs/general/user-guide/1_classes.md @@ -46,7 +46,7 @@ class EcosystemModel(Model): self.prey.do("reproduce") ``` -## Space: GridDF 🌐 +## Space: Grid 🌐 mesa-frames provides efficient implementations of spatial environments: @@ -58,7 +58,7 @@ Example: class GridWorld(Model): def __init__(self, width, height): super().__init__() - self.space = GridPolars(self, (width, height)) + self.space = Grid(self, (width, height)) self.sets += AgentSet(100, self) self.space.place_to_empty(self.sets) ``` diff --git a/examples/sugarscape_ig/ss_polars/model.py b/examples/sugarscape_ig/ss_polars/model.py index fe2c5425..61029582 100644 --- a/examples/sugarscape_ig/ss_polars/model.py +++ b/examples/sugarscape_ig/ss_polars/model.py @@ -1,7 +1,7 @@ import numpy as np import polars as pl -from mesa_frames import GridPolars, Model +from mesa_frames import Grid, Model from .agents import AntPolarsBase @@ -24,7 +24,7 @@ def __init__( if sugar_grid is None: sugar_grid = self.random.integers(0, 4, (width, height)) grid_dimensions = sugar_grid.shape - self.space = GridPolars( + self.space = Grid( self, grid_dimensions, neighborhood_type="von_neumann", capacity=1 ) dim_0 = pl.Series("dim_0", pl.arange(grid_dimensions[0], eager=True)).to_frame() diff --git a/mesa_frames/__init__.py b/mesa_frames/__init__.py index ae16b4a0..79a89ba8 100644 --- a/mesa_frames/__init__.py +++ b/mesa_frames/__init__.py @@ -11,17 +11,17 @@ - Provides similar syntax to Mesa for ease of transition - Allows for vectorized functions when simultaneous activation of agents is possible - Implements SIMD processing for optimized simultaneous operations -- Includes GridDF for efficient grid-based spatial modeling +- Includes Grid for efficient grid-based spatial modeling Main Components: - AgentSet: Agent set implementation using Polars backend - Model: Base model class for mesa-frames -- GridDF: Grid space implementation for spatial modeling +- Grid: Grid space implementation for spatial modeling Usage: To use mesa-frames, import the necessary components and subclass them as needed: - from mesa_frames import AgentSet, Model, GridDF + from mesa_frames import AgentSet, Model, Grid class MyAgent(AgentSet): # Define your agent logic here @@ -29,7 +29,7 @@ class MyAgent(AgentSet): class MyModel(Model): def __init__(self, width, height): super().__init__() - self.grid = GridDF(width, height, self) + self.grid = Grid(self, [width, height]) # Define your model logic here Note: mesa-frames is in early development. API and usage patterns may change. @@ -60,12 +60,12 @@ def __init__(self, width, height): stacklevel=2, ) -from mesa_frames.concrete.agentsetregistry import AgentSetRegistry from mesa_frames.concrete.agentset import AgentSet -from mesa_frames.concrete.model import Model -from mesa_frames.concrete.space import GridPolars +from mesa_frames.concrete.agentsetregistry import AgentSetRegistry from mesa_frames.concrete.datacollector import DataCollector +from mesa_frames.concrete.model import Model +from mesa_frames.concrete.space import Grid -__all__ = ["AgentSetRegistry", "AgentSet", "Model", "GridPolars", "DataCollector"] +__all__ = ["AgentSetRegistry", "AgentSet", "Model", "Grid", "DataCollector"] __version__ = "0.1.1.dev0" diff --git a/mesa_frames/abstract/__init__.py b/mesa_frames/abstract/__init__.py index 4bc87315..127c1784 100644 --- a/mesa_frames/abstract/__init__.py +++ b/mesa_frames/abstract/__init__.py @@ -14,9 +14,9 @@ - DataFrameMixin: Mixin class defining the interface for DataFrame operations. space.py: - - SpaceDF: Abstract base class for all space classes. - - DiscreteSpaceDF: Abstract base class for discrete space classes (Grids and Networks). - - GridDF: Abstract base class for grid classes. + - Space: Abstract base class for all space classes. + - AbstractDiscreteSpace: Abstract base class for discrete space classes (Grids and Networks). + - AbstractGrid: Abstract base class for grid classes. These abstract classes and mixins provide the foundation for the concrete implementations in mesa-frames, ensuring consistent interfaces and shared diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index a1f855e9..74df16e8 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -12,13 +12,13 @@ classes in mesa-frames. It combines fast copying functionality with DataFrame operations. - DiscreteSpaceDF(SpaceDF): + AbstractDiscreteSpace(SpaceDF): An abstract base class for discrete space implementations, such as grids and networks. It extends SpaceDF with methods specific to discrete spaces. - GridDF(DiscreteSpaceDF): + AbstractGrid(AbstractDiscreteSpace): An abstract base class for grid-based spaces. It inherits from - DiscreteSpaceDF and adds grid-specific functionality. + AbstractDiscreteSpace and adds grid-specific functionality. These abstract classes are designed to be subclassed by concrete implementations that use Polars library as their backend. @@ -29,9 +29,9 @@ These classes should not be instantiated directly. Instead, they should be subclassed to create concrete implementations: - from mesa_frames.abstract.space import GridDF + from mesa_frames.abstract.space import AbstractGrid - class GridPolars(GridDF): + class Grid(AbstractGrid): def __init__(self, model, dimensions, torus, capacity, neighborhood_type): super().__init__(model, dimensions, torus, capacity, neighborhood_type) # Implementation using polars DataFrame @@ -86,8 +86,8 @@ def __init__(self, model, dimensions, torus, capacity, neighborhood_type): ESPG = int -class SpaceDF(CopyMixin, DataFrameMixin): - """The SpaceDF class is an abstract class that defines the interface for all space classes in mesa_frames.""" +class Space(CopyMixin, DataFrameMixin): + """The Space class is an abstract class that defines the interface for all space classes in mesa_frames.""" _agents: DataFrame # | GeoDataFrame # Stores the agents placed in the space _center_col_names: list[ @@ -532,7 +532,7 @@ def _place_or_move_agents( @abstractmethod def __repr__(self) -> str: - """Return a string representation of the SpaceDF. + """Return a string representation of the Space. Returns ------- @@ -542,7 +542,7 @@ def __repr__(self) -> str: @abstractmethod def __str__(self) -> str: - """Return a string representation of the SpaceDF. + """Return a string representation of the Space. Returns ------- @@ -581,8 +581,8 @@ def random(self) -> Generator: return self.model.random -class DiscreteSpaceDF(SpaceDF): - """The DiscreteSpaceDF class is an abstract class that defines the interface for all discrete space classes (Grids and Networks) in mesa_frames.""" +class AbstractDiscreteSpace(Space): + """The AbstractDiscreteSpace class is an abstract class that defines the interface for all discrete space classes (Grids and Networks) in mesa_frames.""" _agents: DataFrame _capacity: int | None # The maximum capacity for cells (default is infinite) @@ -596,7 +596,7 @@ def __init__( model: mesa_frames.concrete.model.Model, capacity: int | None = None, ): - """Create a new DiscreteSpaceDF. + """Create a new AbstractDiscreteSpace. Parameters ---------- @@ -1173,10 +1173,10 @@ def remaining_capacity(self) -> int | Infinity: ... -class GridDF(DiscreteSpaceDF): - """The GridDF class is an abstract class that defines the interface for all grid classes in mesa-frames. +class AbstractGrid(AbstractDiscreteSpace): + """The AbstractGrid class is an abstract class that defines the interface for all grid classes in mesa-frames. - Inherits from DiscreteSpaceDF. + Inherits from AbstractDiscreteSpace. Warning ------- @@ -1215,7 +1215,7 @@ def __init__( capacity: int | None = None, neighborhood_type: str = "moore", ): - """Create a new GridDF. + """Create a new AbstractGrid. Parameters ---------- diff --git a/mesa_frames/concrete/__init__.py b/mesa_frames/concrete/__init__.py index 550d6dc2..069fcf4b 100644 --- a/mesa_frames/concrete/__init__.py +++ b/mesa_frames/concrete/__init__.py @@ -17,7 +17,7 @@ model: Provides the Model class, the base class for models in mesa-frames. agentset: Defines the AgentSet class, a Polars-based implementation of AgentSet. mixin: Provides the PolarsMixin class, implementing DataFrame operations using Polars. - space: Contains the GridPolars class, a Polars-based implementation of Grid. + space: Contains the Grid class, a Polars-based implementation of Grid. Classes: from agentset: @@ -30,7 +30,7 @@ A mixin class that implements DataFrame operations using Polars, providing methods for data manipulation and analysis. from space: - GridPolars(GridDF, PolarsMixin): + Grid(AbstractGrid, PolarsMixin): A Polars-based implementation of Grid, using Polars DataFrames for efficient spatial operations and agent positioning. @@ -45,17 +45,17 @@ from mesa_frames.concrete import Model, AgentSetRegistry # For Polars-based implementations - from mesa_frames.concrete import AgentSet, GridPolars + from mesa_frames.concrete import AgentSet, Grid from mesa_frames.concrete.model import Model class MyModel(Model): def __init__(self): super().__init__() self.sets.add(AgentSet(self)) - self.space = GridPolars(self, dimensions=[10, 10]) + self.space = Grid(self, dimensions=[10, 10]) # ... other initialization code - from mesa_frames.concrete import AgentSet, GridPolars + from mesa_frames.concrete import AgentSet, Grid class MyAgents(AgentSet): def __init__(self, model): @@ -66,7 +66,7 @@ class MyModel(Model): def __init__(self, width, height): super().__init__() self.sets = MyAgents(self) - self.grid = GridPolars(width, height, self) + self.grid = Grid(width, height, self) Features: - High-performance DataFrame operations using Polars - Efficient memory usage and fast computation diff --git a/mesa_frames/concrete/mixin.py b/mesa_frames/concrete/mixin.py index 0f2f9eca..341d558b 100644 --- a/mesa_frames/concrete/mixin.py +++ b/mesa_frames/concrete/mixin.py @@ -10,7 +10,7 @@ PolarsMixin(DataFrameMixin): A Polars-based implementation of DataFrame operations. This class provides methods for manipulating and analyzing data stored in Polars DataFrames, - tailored for use in mesa-frames components like AgentSet and GridPolars. + tailored for use in mesa-frames components like AgentSet and Grid. The PolarsMixin class is designed to be used as a mixin with other mesa-frames classes, providing them with Polars-specific DataFrame functionality. It implements diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index a1ad66e1..773cae73 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -47,7 +47,7 @@ def run_model(self): import numpy as np from mesa_frames.abstract.agentset import AbstractAgentSet -from mesa_frames.abstract.space import SpaceDF +from mesa_frames.abstract.space import Space from mesa_frames.concrete.agentsetregistry import AgentSetRegistry @@ -64,7 +64,7 @@ class Model: running: bool _seed: int | Sequence[int] _sets: AgentSetRegistry # Where the agent sets are stored - _space: SpaceDF | None # This will be a MultiSpaceDF object + _space: Space | None # This will be a MultiSpaceDF object def __init__(self, seed: int | Sequence[int] | None = None) -> None: """Create a new model. @@ -199,12 +199,12 @@ def set_types(self) -> list[type]: return [agent.__class__ for agent in self._sets._agentsets] @property - def space(self) -> SpaceDF: + def space(self) -> Space: """Get the space object associated with the model. Returns ------- - SpaceDF + Space The space object associated with the model. Raises @@ -219,7 +219,7 @@ def space(self) -> SpaceDF: return self._space @space.setter - def space(self, space: SpaceDF) -> None: + def space(self, space: Space) -> None: """Set the space of the model. Parameters diff --git a/mesa_frames/concrete/space.py b/mesa_frames/concrete/space.py index 20f87b0c..4f55a680 100644 --- a/mesa_frames/concrete/space.py +++ b/mesa_frames/concrete/space.py @@ -2,26 +2,26 @@ Polars-based implementation of spatial structures for mesa-frames. This module provides concrete implementations of spatial structures using Polars -as the backend for DataFrame operations. It defines the GridPolars class, which +as the backend for DataFrame operations. It defines the Grid class, which implements a 2D grid structure using Polars DataFrames for efficient spatial operations and agent positioning. Classes: - GridPolars(GridDF, PolarsMixin): + Grid(AbstractGrid, PolarsMixin): A Polars-based implementation of a 2D grid. This class uses Polars DataFrames to store and manipulate spatial data, providing high-performance operations for large-scale spatial simulations. -The GridPolars class is designed to be used within Model instances to represent +The Grid class is designed to be used within Model instances to represent the spatial environment of the simulation. It leverages the power of Polars for fast and efficient data operations on spatial attributes and agent positions. Usage: - The GridPolars class can be used directly in a model to represent the + The Grid class can be used directly in a model to represent the spatial environment: from mesa_frames.concrete.model import Model - from mesa_frames.concrete.space import GridPolars + from mesa_frames.concrete.space import Grid from mesa_frames.concrete.agentset import AgentSet class MyAgents(AgentSet): @@ -30,7 +30,7 @@ class MyAgents(AgentSet): class MyModel(Model): def __init__(self, width, height): super().__init__() - self.space = GridPolars(self, [width, height]) + self.space = Grid(self, [width, height]) self.sets += MyAgents(self) def step(self): @@ -38,7 +38,7 @@ def step(self): self.space.move_agents(self.sets) # ... other model logic ... -For more detailed information on the GridPolars class and its methods, +For more detailed information on the Grid class and its methods, refer to the class docstring. """ @@ -49,15 +49,15 @@ def step(self): import numpy as np import polars as pl -from mesa_frames.abstract.space import GridDF +from mesa_frames.abstract.space import AbstractGrid from mesa_frames.concrete.mixin import PolarsMixin from mesa_frames.types_ import Infinity from mesa_frames.utils import copydoc -@copydoc(GridDF) -class GridPolars(GridDF, PolarsMixin): - """Polars-based implementation of GridDF.""" +@copydoc(AbstractGrid) +class Grid(AbstractGrid, PolarsMixin): + """Polars-based implementation of AbstractGrid.""" _agents: pl.DataFrame _copy_with_method: dict[str, tuple[str, list[str]]] = { diff --git a/tests/test_agentset.py b/tests/test_agentset.py index 66eca478..d475a4fc 100644 --- a/tests/test_agentset.py +++ b/tests/test_agentset.py @@ -4,7 +4,7 @@ import pytest from numpy.random import Generator -from mesa_frames import AgentSet, GridPolars, Model +from mesa_frames import AgentSet, Grid, Model class ExampleAgentSet(AgentSet): @@ -49,7 +49,7 @@ def fix2_AgentSet() -> ExampleAgentSet: agents["age"] = [100, 200, 300, 400] model.sets.add(agents) - space = GridPolars(model, dimensions=[3, 3], capacity=2) + space = Grid(model, dimensions=[3, 3], capacity=2) model.space = space space.place_agents(agents=agents["unique_id"][[0, 1]], pos=[[2, 1], [1, 2]]) return agents @@ -68,7 +68,7 @@ def fix3_AgentSet() -> ExampleAgentSet: def fix1_AgentSet_with_pos( fix1_AgentSet: ExampleAgentSet, ) -> ExampleAgentSet: - space = GridPolars(fix1_AgentSet.model, dimensions=[3, 3], capacity=2) + space = Grid(fix1_AgentSet.model, dimensions=[3, 3], capacity=2) fix1_AgentSet.model.space = space space.place_agents(agents=fix1_AgentSet["unique_id"][[0, 1]], pos=[[0, 0], [1, 1]]) return fix1_AgentSet diff --git a/tests/test_grid.py b/tests/test_grid.py index 2fe17aea..6d75f3cc 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -3,7 +3,7 @@ import pytest from polars.testing import assert_frame_equal -from mesa_frames import GridPolars, Model +from mesa_frames import Grid, Model from tests.test_agentset import ( ExampleAgentSet, fix1_AgentSet, @@ -19,7 +19,7 @@ def get_unique_ids(model: Model) -> pl.Series: return pl.concat(series_list) -class TestGridPolars: +class TestGrid: @pytest.fixture def model( self, @@ -31,8 +31,8 @@ def model( return model @pytest.fixture - def grid_moore(self, model: Model) -> GridPolars: - space = GridPolars(model, dimensions=[3, 3], capacity=2) + def grid_moore(self, model: Model) -> Grid: + space = Grid(model, dimensions=[3, 3], capacity=2) unique_ids = get_unique_ids(model) space.place_agents(agents=unique_ids[[0, 1]], pos=[[0, 0], [1, 1]]) space.set_cells( @@ -41,8 +41,8 @@ def grid_moore(self, model: Model) -> GridPolars: return space @pytest.fixture - def grid_moore_torus(self, model: Model) -> GridPolars: - space = GridPolars(model, dimensions=[3, 3], capacity=2, torus=True) + def grid_moore_torus(self, model: Model) -> Grid: + space = Grid(model, dimensions=[3, 3], capacity=2, torus=True) unique_ids = get_unique_ids(model) space.place_agents(agents=unique_ids[[0, 1]], pos=[[0, 0], [1, 1]]) space.set_cells( @@ -51,23 +51,23 @@ def grid_moore_torus(self, model: Model) -> GridPolars: return space @pytest.fixture - def grid_von_neumann(self, model: Model) -> GridPolars: - space = GridPolars(model, dimensions=[3, 3], neighborhood_type="von_neumann") + def grid_von_neumann(self, model: Model) -> Grid: + space = Grid(model, dimensions=[3, 3], neighborhood_type="von_neumann") unique_ids = get_unique_ids(model) space.place_agents(agents=unique_ids[[0, 1]], pos=[[0, 0], [1, 1]]) return space @pytest.fixture - def grid_hexagonal(self, model: Model) -> GridPolars: - space = GridPolars(model, dimensions=[10, 10], neighborhood_type="hexagonal") + def grid_hexagonal(self, model: Model) -> Grid: + space = Grid(model, dimensions=[10, 10], neighborhood_type="hexagonal") unique_ids = get_unique_ids(model) space.place_agents(agents=unique_ids[[0, 1]], pos=[[0, 0], [1, 1]]) return space def test___init__(self, model: Model): # Test with default parameters - grid1 = GridPolars(model, dimensions=[3, 3]) - assert isinstance(grid1, GridPolars) + grid1 = Grid(model, dimensions=[3, 3]) + assert isinstance(grid1, Grid) assert isinstance(grid1.agents, pl.DataFrame) assert grid1.agents.is_empty() assert isinstance(grid1.cells, pl.DataFrame) @@ -80,26 +80,26 @@ def test___init__(self, model: Model): assert grid1.model == model # Test with capacity = 10 - grid2 = GridPolars(model, dimensions=[3, 3], capacity=10) + grid2 = Grid(model, dimensions=[3, 3], capacity=10) assert grid2.remaining_capacity == (10 * 3 * 3) # Test with torus = True - grid3 = GridPolars(model, dimensions=[3, 3], torus=True) + grid3 = Grid(model, dimensions=[3, 3], torus=True) assert grid3.torus # Test with neighborhood_type = "von_neumann" - grid4 = GridPolars(model, dimensions=[3, 3], neighborhood_type="von_neumann") + grid4 = Grid(model, dimensions=[3, 3], neighborhood_type="von_neumann") assert grid4.neighborhood_type == "von_neumann" # Test with neighborhood_type = "moore" - grid5 = GridPolars(model, dimensions=[3, 3], neighborhood_type="moore") + grid5 = Grid(model, dimensions=[3, 3], neighborhood_type="moore") assert grid5.neighborhood_type == "moore" # Test with neighborhood_type = "hexagonal" - grid6 = GridPolars(model, dimensions=[3, 3], neighborhood_type="hexagonal") + grid6 = Grid(model, dimensions=[3, 3], neighborhood_type="hexagonal") assert grid6.neighborhood_type == "hexagonal" - def test_get_cells(self, grid_moore: GridPolars): + def test_get_cells(self, grid_moore: Grid): # Test with None (all cells) result = grid_moore.get_cells() assert isinstance(result, pl.DataFrame) @@ -132,7 +132,7 @@ def test_get_cells(self, grid_moore: GridPolars): def test_get_directions( self, - grid_moore: GridPolars, + grid_moore: Grid, fix1_AgentSet: ExampleAgentSet, fix2_AgentSet: ExampleAgentSet, ): @@ -211,7 +211,7 @@ def test_get_directions( def test_get_distances( self, - grid_moore: GridPolars, + grid_moore: Grid, fix1_AgentSet: ExampleAgentSet, fix2_AgentSet: ExampleAgentSet, ): @@ -262,10 +262,10 @@ def test_get_distances( def test_get_neighborhood( self, - grid_moore: GridPolars, - grid_hexagonal: GridPolars, - grid_von_neumann: GridPolars, - grid_moore_torus: GridPolars, + grid_moore: Grid, + grid_hexagonal: Grid, + grid_von_neumann: Grid, + grid_moore_torus: Grid, ): # Test with radius = int, pos=GridCoordinate neighborhood = grid_moore.get_neighborhood(radius=1, pos=[1, 1]) @@ -614,10 +614,10 @@ def test_get_neighborhood( def test_get_neighbors( self, fix2_AgentSet: ExampleAgentSet, - grid_moore: GridPolars, - grid_hexagonal: GridPolars, - grid_von_neumann: GridPolars, - grid_moore_torus: GridPolars, + grid_moore: Grid, + grid_hexagonal: Grid, + grid_von_neumann: Grid, + grid_moore_torus: Grid, ): # Place agents in the grid unique_ids = get_unique_ids(grid_moore.model) @@ -751,7 +751,7 @@ def test_get_neighbors( check_column_order=False, ) - def test_is_available(self, grid_moore: GridPolars): + def test_is_available(self, grid_moore: Grid): # Test with GridCoordinate result = grid_moore.is_available([0, 0]) assert isinstance(result, pl.DataFrame) @@ -763,7 +763,7 @@ def test_is_available(self, grid_moore: GridPolars): result = grid_moore.is_available([[0, 0], [1, 1]]) assert result.select(pl.col("available")).to_series().to_list() == [False, True] - def test_is_empty(self, grid_moore: GridPolars): + def test_is_empty(self, grid_moore: Grid): # Test with GridCoordinate result = grid_moore.is_empty([0, 0]) assert isinstance(result, pl.DataFrame) @@ -775,7 +775,7 @@ def test_is_empty(self, grid_moore: GridPolars): result = grid_moore.is_empty([[0, 0], [1, 1]]) assert result.select(pl.col("empty")).to_series().to_list() == [False, False] - def test_is_full(self, grid_moore: GridPolars): + def test_is_full(self, grid_moore: Grid): # Test with GridCoordinate result = grid_moore.is_full([0, 0]) assert isinstance(result, pl.DataFrame) @@ -789,7 +789,7 @@ def test_is_full(self, grid_moore: GridPolars): def test_move_agents( self, - grid_moore: GridPolars, + grid_moore: Grid, fix1_AgentSet: ExampleAgentSet, fix2_AgentSet: ExampleAgentSet, ): @@ -890,7 +890,7 @@ def test_move_agents( check_row_order=False, ) - def test_move_to_available(self, grid_moore: GridPolars): + def test_move_to_available(self, grid_moore: Grid): # Test with GridCoordinate unique_ids = get_unique_ids(grid_moore.model) last = None @@ -950,7 +950,7 @@ def test_move_to_available(self, grid_moore: GridPolars): last = space.agents.select(pl.col("dim_0")).to_numpy() assert different - def test_move_to_empty(self, grid_moore: GridPolars): + def test_move_to_empty(self, grid_moore: Grid): # Test with GridCoordinate unique_ids = get_unique_ids(grid_moore.model) last = None @@ -1010,7 +1010,7 @@ def test_move_to_empty(self, grid_moore: GridPolars): last = space.agents.select(pl.col("dim_0")).to_numpy() assert different - def test_out_of_bounds(self, grid_moore: GridPolars): + def test_out_of_bounds(self, grid_moore: Grid): # Test with GridCoordinate out_of_bounds = grid_moore.out_of_bounds([11, 11]) assert isinstance(out_of_bounds, pl.DataFrame) @@ -1028,7 +1028,7 @@ def test_out_of_bounds(self, grid_moore: GridPolars): def test_place_agents( self, - grid_moore: GridPolars, + grid_moore: Grid, fix1_AgentSet: ExampleAgentSet, fix2_AgentSet: ExampleAgentSet, ): @@ -1225,7 +1225,7 @@ def test_place_agents( check_row_order=False, ) - def test_place_to_available(self, grid_moore: GridPolars): + def test_place_to_available(self, grid_moore: Grid): # Test with GridCoordinate unique_ids = get_unique_ids(grid_moore.model) last = None @@ -1285,7 +1285,7 @@ def test_place_to_available(self, grid_moore: GridPolars): last = space.agents.select(pl.col("dim_0")).to_numpy() assert different - def test_place_to_empty(self, grid_moore: GridPolars): + def test_place_to_empty(self, grid_moore: Grid): # Test with GridCoordinate unique_ids = get_unique_ids(grid_moore.model) last = None @@ -1345,7 +1345,7 @@ def test_place_to_empty(self, grid_moore: GridPolars): last = space.agents.select(pl.col("dim_0")).to_numpy() assert different - def test_random_agents(self, grid_moore: GridPolars): + def test_random_agents(self, grid_moore: Grid): different = False agents0 = grid_moore.random_agents(1) for _ in range(100): @@ -1355,7 +1355,7 @@ def test_random_agents(self, grid_moore: GridPolars): break assert different - def test_random_pos(self, grid_moore: GridPolars): + def test_random_pos(self, grid_moore: Grid): different = False last = None for _ in range(10): @@ -1378,7 +1378,7 @@ def test_random_pos(self, grid_moore: GridPolars): def test_remove_agents( self, - grid_moore: GridPolars, + grid_moore: Grid, fix1_AgentSet: ExampleAgentSet, fix2_AgentSet: ExampleAgentSet, ): @@ -1443,7 +1443,7 @@ def test_remove_agents( x for id in space.model.sets.index.values() for x in id.to_list() ] == unique_ids[:8].to_list() - def test_sample_cells(self, grid_moore: GridPolars): + def test_sample_cells(self, grid_moore: Grid): # Test with default parameters replacement = False same = True @@ -1521,8 +1521,8 @@ def test_sample_cells(self, grid_moore: GridPolars): grid_moore.sample_cells(3, cell_type="full", with_replacement=False) def test_set_cells(self, model: Model): - # Initialize GridPolars - grid_moore = GridPolars(model, dimensions=[3, 3], capacity=2) + # Initialize Grid + grid_moore = Grid(model, dimensions=[3, 3], capacity=2) # Test with GridCoordinate grid_moore.set_cells( @@ -1571,7 +1571,7 @@ def test_set_cells(self, model: Model): def test_swap_agents( self, - grid_moore: GridPolars, + grid_moore: Grid, fix1_AgentSet: ExampleAgentSet, fix2_AgentSet: ExampleAgentSet, ): @@ -1619,7 +1619,7 @@ def test_swap_agents( == grid_moore.agents.filter(pl.col("agent_id") == unique_ids[7]).row(0)[1:] ) - def test_torus_adj(self, grid_moore: GridPolars, grid_moore_torus: GridPolars): + def test_torus_adj(self, grid_moore: Grid, grid_moore_torus: Grid): # Test with non-toroidal grid with pytest.raises(ValueError): grid_moore.torus_adj([10, 10]) @@ -1639,7 +1639,7 @@ def test_torus_adj(self, grid_moore: GridPolars, grid_moore_torus: GridPolars): assert adj_df.row(0) == (1, 2) assert adj_df.row(1) == (0, 2) - def test___getitem__(self, grid_moore: GridPolars): + def test___getitem__(self, grid_moore: Grid): # Test out of bounds with pytest.raises(ValueError): grid_moore[[5, 5]] @@ -1677,7 +1677,7 @@ def test___getitem__(self, grid_moore: GridPolars): check_dtypes=False, ) - def test___setitem__(self, grid_moore: GridPolars): + def test___setitem__(self, grid_moore: Grid): # Test with out-of-bounds with pytest.raises(ValueError): grid_moore[[5, 5]] = {"capacity": 10} @@ -1692,7 +1692,7 @@ def test___setitem__(self, grid_moore: GridPolars): ).to_series().to_list() == [20, 20] # Property tests - def test_agents(self, grid_moore: GridPolars): + def test_agents(self, grid_moore: Grid): unique_ids = get_unique_ids(grid_moore.model) assert_frame_equal( grid_moore.agents, @@ -1701,13 +1701,13 @@ def test_agents(self, grid_moore: GridPolars): ), ) - def test_available_cells(self, grid_moore: GridPolars): + def test_available_cells(self, grid_moore: Grid): result = grid_moore.available_cells assert len(result) == 8 assert isinstance(result, pl.DataFrame) assert result.columns == ["dim_0", "dim_1"] - def test_cells(self, grid_moore: GridPolars): + def test_cells(self, grid_moore: Grid): result = grid_moore.cells unique_ids = get_unique_ids(grid_moore.model) assert_frame_equal( @@ -1724,17 +1724,17 @@ def test_cells(self, grid_moore: GridPolars): check_dtypes=False, ) - def test_dimensions(self, grid_moore: GridPolars): + def test_dimensions(self, grid_moore: Grid): assert isinstance(grid_moore.dimensions, list) assert len(grid_moore.dimensions) == 2 - def test_empty_cells(self, grid_moore: GridPolars): + def test_empty_cells(self, grid_moore: Grid): result = grid_moore.empty_cells assert len(result) == 7 assert isinstance(result, pl.DataFrame) assert result.columns == ["dim_0", "dim_1"] - def test_full_cells(self, grid_moore: GridPolars): + def test_full_cells(self, grid_moore: Grid): grid_moore.set_cells([[0, 0], [1, 1]], {"capacity": 1}) result = grid_moore.full_cells assert len(result) == 2 @@ -1751,27 +1751,27 @@ def test_full_cells(self, grid_moore: GridPolars): ) ).all() - def test_model(self, grid_moore: GridPolars, model: Model): + def test_model(self, grid_moore: Grid, model: Model): assert grid_moore.model == model def test_neighborhood_type( self, - grid_moore: GridPolars, - grid_von_neumann: GridPolars, - grid_hexagonal: GridPolars, + grid_moore: Grid, + grid_von_neumann: Grid, + grid_hexagonal: Grid, ): assert grid_moore.neighborhood_type == "moore" assert grid_von_neumann.neighborhood_type == "von_neumann" assert grid_hexagonal.neighborhood_type == "hexagonal" - def test_random(self, grid_moore: GridPolars): + def test_random(self, grid_moore: Grid): assert grid_moore.random == grid_moore.model.random - def test_remaining_capacity(self, grid_moore: GridPolars): + def test_remaining_capacity(self, grid_moore: Grid): assert grid_moore.remaining_capacity == (3 * 3 * 2 - 2) - def test_torus(self, model: Model, grid_moore: GridPolars): + def test_torus(self, model: Model, grid_moore: Grid): assert not grid_moore.torus - grid_2 = GridPolars(model, [3, 3], torus=True) + grid_2 = Grid(model, [3, 3], torus=True) assert grid_2.torus From c8d77e2bf70a2cdf089968fc8377a759cecb7f6a Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 1 Sep 2025 19:34:40 +0200 Subject: [PATCH 063/329] Remove concrete implementation of AgentSetsAccessor for codebase cleanup --- mesa_frames/concrete/accessors.py | 147 ------------------------------ 1 file changed, 147 deletions(-) delete mode 100644 mesa_frames/concrete/accessors.py diff --git a/mesa_frames/concrete/accessors.py b/mesa_frames/concrete/accessors.py deleted file mode 100644 index 71c2097d..00000000 --- a/mesa_frames/concrete/accessors.py +++ /dev/null @@ -1,147 +0,0 @@ -"""Concrete implementations of agent set accessors. - -This module contains the concrete implementation of the AgentSetsAccessor, -which provides a user-friendly interface for accessing and manipulating -collections of agent sets within the mesa-frames library. -""" - -from __future__ import annotations - -from collections import defaultdict -from collections.abc import Iterable, Iterator, Mapping -from types import MappingProxyType -from typing import Any, Literal, TypeVar, cast - -from mesa_frames.abstract.accessors import AbstractAgentSetsAccessor -from mesa_frames.abstract.agents import AgentSetDF -from mesa_frames.types_ import KeyBy - -TSet = TypeVar("TSet", bound=AgentSetDF) - - -class AgentSetsAccessor(AbstractAgentSetsAccessor): - def __init__(self, parent: mesa_frames.concrete.agents.AgentsDF) -> None: - self._parent = parent - - def __getitem__( - self, key: int | str | type[AgentSetDF] - ) -> AgentSetDF | list[AgentSetDF]: - sets = self._parent._agentsets - if isinstance(key, int): - try: - return sets[key] - except IndexError as e: - raise IndexError( - f"Index {key} out of range for {len(sets)} agent sets" - ) from e - if isinstance(key, str): - for s in sets: - if s.name == key: - return s - available = [getattr(s, "name", None) for s in sets] - raise KeyError(f"No agent set named '{key}'. Available: {available}") - if isinstance(key, type): - matches = [s for s in sets if isinstance(s, key)] - # Always return list for type keys to maintain consistent shape - return matches # type: ignore[return-value] - raise TypeError("Key must be int | str | type[AgentSetDF]") - - def get( - self, - key: int | str | type[TSet], - default: AgentSetDF | list[TSet] | None = None, - ) -> AgentSetDF | list[TSet] | None: - try: - val = self[key] # type: ignore[return-value] - # For type keys, if no matches and a default was provided, return default - if ( - isinstance(key, type) - and isinstance(val, list) - and len(val) == 0 - and default is not None - ): - return default - return val - except (KeyError, IndexError, TypeError): - return default - - def first(self, t: type[TSet]) -> TSet: - match = next((s for s in self._parent._agentsets if isinstance(s, t)), None) - if not match: - raise KeyError(f"No agent set of type {getattr(t, '__name__', t)} found.") - return match - - def all(self, t: type[TSet]) -> list[TSet]: - return [s for s in self._parent._agentsets if isinstance(s, t)] # type: ignore[return-value] - - def at(self, index: int) -> AgentSetDF: - return self[index] # type: ignore[return-value] - - # ---------- key generation and views ---------- - def _gen_key(self, aset: AgentSetDF, idx: int, mode: str) -> Any: - if mode == "name": - return aset.name - if mode == "index": - return idx - if mode == "type": - return type(aset) - raise ValueError("key_by must be 'name'|'index'|'type'") - - def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: - for i, s in enumerate(self._parent._agentsets): - yield self._gen_key(s, i, key_by) - - def items(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: - for i, s in enumerate(self._parent._agentsets): - yield self._gen_key(s, i, key_by), s - - def values(self) -> Iterable[AgentSetDF]: - return iter(self._parent._agentsets) - - def iter(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: - return self.items(key_by=key_by) - - def dict(self, *, key_by: KeyBy = "name") -> dict[Any, AgentSetDF]: - return {k: v for k, v in self.items(key_by=key_by)} - - # ---------- read-only snapshots ---------- - @property - def by_name(self) -> Mapping[str, AgentSetDF]: - return MappingProxyType({cast(str, s.name): s for s in self._parent._agentsets}) - - @property - def by_type(self) -> Mapping[type, list[AgentSetDF]]: - d: dict[type, list[AgentSetDF]] = defaultdict(list) - for s in self._parent._agentsets: - d[type(s)].append(s) - return MappingProxyType(dict(d)) - - # ---------- membership & iteration ---------- - def rename( - self, - target: AgentSetDF - | str - | dict[AgentSetDF | str, str] - | list[tuple[AgentSetDF | str, str]], - new_name: str | None = None, - *, - on_conflict: Literal["canonicalize", "raise"] = "canonicalize", - mode: Literal["atomic", "best_effort"] = "atomic", - ) -> str | dict[AgentSetDF, str]: - return self._parent._rename_sets( - target, new_name, on_conflict=on_conflict, mode=mode - ) - - def __contains__(self, x: str | AgentSetDF) -> bool: - sets = self._parent._agentsets - if isinstance(x, str): - return any(s.name == x for s in sets) - if isinstance(x, AgentSetDF): - return any(s is x for s in sets) - return False - - def __len__(self) -> int: - return len(self._parent._agentsets) - - def __iter__(self) -> Iterator[AgentSetDF]: - return iter(self._parent._agentsets) From eaec185f202c76e9b3d7a47e0fd59e22dc1f6247 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 1 Sep 2025 19:35:13 +0200 Subject: [PATCH 064/329] Remove camel_case_to_snake_case function for codebase cleanup --- mesa_frames/utils.py | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/mesa_frames/utils.py b/mesa_frames/utils.py index fb3e65ff..4c092384 100644 --- a/mesa_frames/utils.py +++ b/mesa_frames/utils.py @@ -17,28 +17,3 @@ def _decorator(func): return _decorator - -def camel_case_to_snake_case(name: str) -> str: - """Convert camelCase to snake_case. - - Parameters - ---------- - name : str - The camelCase string to convert. - - Returns - ------- - str - The converted snake_case string. - - Examples - -------- - >>> camel_case_to_snake_case("ExampleAgentSetPolars") - 'example_agent_set_polars' - >>> camel_case_to_snake_case("getAgentData") - 'get_agent_data' - """ - import re - - s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name) - return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() From 6b1f3ad75626323e77925d24425cfe5381a259a4 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 1 Sep 2025 19:36:30 +0200 Subject: [PATCH 065/329] Rename SpaceDF to Space and update related references for consistency --- mesa_frames/abstract/space.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index 74df16e8..f5982154 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -7,14 +7,14 @@ performance and scalability. Classes: - SpaceDF(CopyMixin, DataFrameMixin): + Space(CopyMixin, DataFrameMixin): An abstract base class that defines the common interface for all space classes in mesa-frames. It combines fast copying functionality with DataFrame operations. - AbstractDiscreteSpace(SpaceDF): + AbstractDiscreteSpace(Space): An abstract base class for discrete space implementations, such as grids - and networks. It extends SpaceDF with methods specific to discrete spaces. + and networks. It extends Space with methods specific to discrete spaces. AbstractGrid(AbstractDiscreteSpace): An abstract base class for grid-based spaces. It inherits from @@ -98,7 +98,7 @@ class Space(CopyMixin, DataFrameMixin): ] # The column names of the positions in the _agents dataframe (eg. ['dim_0', 'dim_1', ...] in Grids, ['node_id', 'edge_id'] in Networks) def __init__(self, model: mesa_frames.concrete.model.Model) -> None: - """Create a new SpaceDF. + """Create a new Space. Parameters ---------- From f46fbb9999d0bd5531b6ab75d78ed0e1c665a591 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:32:40 +0200 Subject: [PATCH 066/329] Rename MoneyAgentDFConcise to MoneyAgentConcise and MoneyAgentDFNative to MoneyAgentNative for clarity; update MoneyModelDF to MoneyModel and adjust related references. --- examples/boltzmann_wealth/performance_plot.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/boltzmann_wealth/performance_plot.py b/examples/boltzmann_wealth/performance_plot.py index e5b0ad47..e378018b 100644 --- a/examples/boltzmann_wealth/performance_plot.py +++ b/examples/boltzmann_wealth/performance_plot.py @@ -65,7 +65,7 @@ def run_model(self, n_steps) -> None: ### ---------- Mesa-frames implementation ---------- ### -class MoneyAgentDFConcise(AgentSet): +class MoneyAgentConcise(AgentSet): def __init__(self, n: int, model: Model): super().__init__(model) ## Adding the agents to the agent set @@ -120,7 +120,7 @@ def give_money(self): self[new_wealth, "wealth"] += new_wealth["len"] -class MoneyAgentDFNative(AgentSet): +class MoneyAgentNative(AgentSet): def __init__(self, n: int, model: Model): super().__init__(model) self += pl.DataFrame({"wealth": pl.ones(n, eager=True)}) @@ -154,7 +154,7 @@ def give_money(self): ) -class MoneyModelDF(Model): +class MoneyModel(Model): def __init__(self, N: int, agents_cls): super().__init__() self.n_agents = N @@ -170,12 +170,12 @@ def run_model(self, n): def mesa_frames_polars_concise(n_agents: int) -> None: - model = MoneyModelDF(n_agents, MoneyAgentDFConcise) + model = MoneyModel(n_agents, MoneyAgentConcise) model.run_model(100) def mesa_frames_polars_native(n_agents: int) -> None: - model = MoneyModelDF(n_agents, MoneyAgentDFNative) + model = MoneyModel(n_agents, MoneyAgentNative) model.run_model(100) From 3cdd5c1968d094a3931be4585a59da7f17f1fd62 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:34:14 +0200 Subject: [PATCH 067/329] Update rename method documentation to reflect delegation to AgentSetRegistry instead of AgentsDF --- mesa_frames/concrete/agentset.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 8dcc841d..fcf5f963 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -113,7 +113,7 @@ def name(self) -> str | None: return getattr(self, "_name", None) def rename(self, new_name: str) -> str: - """Rename this agent set. If attached to AgentsDF, delegate for uniqueness enforcement. + """Rename this agent set. If attached to AgentSetRegistry, delegate for uniqueness enforcement. Parameters ---------- @@ -130,10 +130,10 @@ def rename(self, new_name: str) -> str: ValueError If name conflicts occur and delegate encounters errors. """ - # Always delegate to the container's accessor if available through the model's agents - # Check if we have a model and can find the AgentsDF that contains this set - if self in self.model.agents.sets: - return self.model.agents.sets.rename(self._name, new_name) + # Always delegate to the container's accessor if available through the model's sets + # Check if we have a model and can find the AgentSetRegistry that contains this set + if self in self.model.sets: + return self.model.sets.rename(self._name, new_name) # Set name locally if no container found self._name = new_name From 5f217b06239b09b65dbf8f6dd330bf1ea00260a5 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:35:00 +0200 Subject: [PATCH 068/329] Remove unused properties from AgentSetRegistry for codebase cleanup --- mesa_frames/concrete/agentsetregistry.py | 35 ------------------------ 1 file changed, 35 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index a74ba0d2..9c65c324 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -650,30 +650,6 @@ def __sub__( """ return super().__sub__(agents) - @property - def df(self) -> dict[AbstractAgentSet, DataFrame]: - return {agentset: agentset.df for agentset in self._agentsets} - - @df.setter - def df(self, other: Iterable[AbstractAgentSet]) -> None: - """Set the agents in the AgentSetRegistry. - - Parameters - ---------- - other : Iterable[AbstractAgentSet] - The AbstractAgentSets to set. - """ - self._agentsets = list(other) - - @property - def active_agents(self) -> dict[AbstractAgentSet, DataFrame]: - return {agentset: agentset.active_agents for agentset in self._agentsets} - - @active_agents.setter - def active_agents( - self, agents: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] - ) -> None: - self.select(agents, inplace=True) @property def agentsets_by_type(self) -> dict[type[AbstractAgentSet], Self]: @@ -697,14 +673,3 @@ def copy_without_agentsets() -> Self: dictionary[agentset.__class__] = agents_df return dictionary - @property - def inactive_agents(self) -> dict[AbstractAgentSet, DataFrame]: - return {agentset: agentset.inactive_agents for agentset in self._agentsets} - - @property - def index(self) -> dict[AbstractAgentSet, Index]: - return {agentset: agentset.index for agentset in self._agentsets} - - @property - def pos(self) -> dict[AbstractAgentSet, DataFrame]: - return {agentset: agentset.pos for agentset in self._agentsets} From ca54b408ab068e54ce615107e6b4187cb556f6cf Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:35:39 +0200 Subject: [PATCH 069/329] Update space type annotations to reflect Space object instead of MultiSpaceDF --- mesa_frames/concrete/model.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index e4493aef..e3c4cda3 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -64,7 +64,7 @@ class Model: running: bool _seed: int | Sequence[int] _sets: AgentSetRegistry # Where the agent sets are stored - _space: Space | None # This will be a MultiSpaceDF object + _space: Space | None # This will be a Space object def __init__(self, seed: int | Sequence[int] | None = None) -> None: """Create a new model. @@ -170,7 +170,7 @@ def sets(self, sets: AgentSetRegistry) -> None: self._sets = sets @property - def space(self) -> SpaceDF: + def space(self) -> Space: """Get the space object associated with the model. Returns @@ -195,6 +195,6 @@ def space(self, space: Space) -> None: Parameters ---------- - space : SpaceDF + space : Space """ self._space = space From cc1f1338d00e3ce61a3fd09cb184e8eb5682f1a5 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:36:15 +0200 Subject: [PATCH 070/329] Fix get_unique_ids function to correctly cast unique_id series from model sets --- tests/test_grid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_grid.py b/tests/test_grid.py index 6d75f3cc..231f929e 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -14,7 +14,7 @@ def get_unique_ids(model: Model) -> pl.Series: # return model.get_sets_of_type(model.set_types[0])["unique_id"] series_list = [ - agent_set["unique_id"].cast(pl.UInt64) for agent_set in model.sets.df.values() + series.cast(pl.UInt64) for series in model.sets.get("unique_id").values() ] return pl.concat(series_list) From 9cb79c2110ef343db006e53bade7eee24289a32c Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:37:18 +0200 Subject: [PATCH 071/329] Refactor space property type annotation to use Space instead of SpaceDF; remove unused abstract properties for cleaner interface. --- mesa_frames/abstract/agentsetregistry.py | 88 +----------------------- 1 file changed, 2 insertions(+), 86 deletions(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index abebe7a2..eba8097d 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -703,96 +703,12 @@ def random(self) -> Generator: return self.model.random @property - def space(self) -> mesa_frames.abstract.space.SpaceDF | None: + def space(self) -> mesa_frames.abstract.space.Space | None: """The space of the model. Returns ------- - mesa_frames.abstract.space.SpaceDF | None + mesa_frames.abstract.space.Space | None """ return self.model.space - @property - @abstractmethod - def df(self) -> DataFrame | dict[str, DataFrame]: - """The agents in the AbstractAgentSetRegistry. - - Returns - ------- - DataFrame | dict[str, DataFrame] - """ - - @df.setter - @abstractmethod - def df( - self, agents: DataFrame | list[mesa_frames.abstract.agentset.AbstractAgentSet] - ) -> None: - """Set the agents in the AbstractAgentSetRegistry. - - Parameters - ---------- - agents : DataFrame | list[mesa_frames.abstract.agentset.AbstractAgentSet] - """ - - @property - @abstractmethod - def active_agents(self) -> DataFrame | dict[str, DataFrame]: - """The active agents in the AbstractAgentSetRegistry. - - Returns - ------- - DataFrame | dict[str, DataFrame] - """ - - @active_agents.setter - @abstractmethod - def active_agents( - self, - mask: AgentMask, - ) -> None: - """Set the active agents in the AbstractAgentSetRegistry. - - Parameters - ---------- - mask : AgentMask - The mask to apply. - """ - self.select(mask=mask, inplace=True) - - @property - @abstractmethod - def inactive_agents( - self, - ) -> DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame]: - """The inactive agents in the AbstractAgentSetRegistry. - - Returns - ------- - DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] - """ - - @property - @abstractmethod - def index( - self, - ) -> Index | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Index]: - """The ids in the AbstractAgentSetRegistry. - - Returns - ------- - Index | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Index] - """ - ... - - @property - @abstractmethod - def pos( - self, - ) -> DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame]: - """The position of the agents in the AbstractAgentSetRegistry. - - Returns - ------- - DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] - """ - ... From 89454e20cb85a39fcfc69cf4af8372a4d9461789 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:41:25 +0200 Subject: [PATCH 072/329] Update copyright year in conf.py to use current year dynamically --- docs/api/conf.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/api/conf.py b/docs/api/conf.py index 0dcdded8..43098ec2 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -4,6 +4,7 @@ # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. import sys +from datetime import datetime from pathlib import Path sys.path.insert(0, str(Path("..").resolve())) @@ -11,7 +12,7 @@ # -- Project information ----------------------------------------------------- project = "mesa-frames" author = "Project Mesa, Adam Amer" -copyright = f"2023, {author}" +copyright = f"{datetime.now().year}, {author}" # -- General configuration --------------------------------------------------- extensions = [ From 36f132accd0b51f33cb3fa7251140822887e7f04 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:49:30 +0200 Subject: [PATCH 073/329] Rename MoneyAgentDF and MoneyModelDF classes to MoneyAgents and MoneyModel for consistency across the codebase --- docs/general/index.md | 4 +- docs/general/user-guide/0_getting-started.md | 6 +-- docs/general/user-guide/1_classes.md | 2 +- .../user-guide/2_introductory-tutorial.ipynb | 47 ++++++++++++------- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/docs/general/index.md b/docs/general/index.md index d8255260..f0f437e5 100644 --- a/docs/general/index.md +++ b/docs/general/index.md @@ -44,7 +44,7 @@ Here's a quick example of how to create a model using mesa-frames: from mesa_frames import AgentSet, Model import polars as pl -class MoneyAgentDF(AgentSet): +class MoneyAgents(AgentSet): def __init__(self, n: int, model: Model): super().__init__(model) self += pl.DataFrame( @@ -57,7 +57,7 @@ class MoneyAgentDF(AgentSet): def give_money(self): # ... (implementation details) -class MoneyModelDF(Model): +class MoneyModel(Model): def __init__(self, N: int): super().__init__() self.sets += MoneyAgentDF(N, self) diff --git a/docs/general/user-guide/0_getting-started.md b/docs/general/user-guide/0_getting-started.md index 5d2b4cd2..1edc1587 100644 --- a/docs/general/user-guide/0_getting-started.md +++ b/docs/general/user-guide/0_getting-started.md @@ -35,7 +35,7 @@ Here's a comparison between mesa-frames and mesa: === "mesa-frames" ```python - class MoneyAgentDFConcise(AgentSet): + class MoneyAgents(AgentSet): # initialization... def give_money(self): # Active agents are changed to wealthy agents @@ -84,7 +84,7 @@ If you're familiar with mesa, this guide will help you understand the key differ === "mesa-frames" ```python - class MoneyAgentSet(AgentSet): + class MoneyAgents(AgentSet): def __init__(self, n, model): super().__init__(model) self += pl.DataFrame({ @@ -124,7 +124,7 @@ If you're familiar with mesa, this guide will help you understand the key differ class MoneyModel(Model): def __init__(self, N): super().__init__() - self.sets += MoneyAgentSet(N, self) + self.sets += MoneyAgents(N, self) def step(self): self.sets.do("step") diff --git a/docs/general/user-guide/1_classes.md b/docs/general/user-guide/1_classes.md index b772e248..d5d55c5c 100644 --- a/docs/general/user-guide/1_classes.md +++ b/docs/general/user-guide/1_classes.md @@ -11,7 +11,7 @@ How can you choose which agents should be in the same AgentSet? The idea is that Example: ```python -class MoneyAgent(AgentSet): +class MoneyAgents(AgentSet): def __init__(self, n: int, model: Model): super().__init__(model) self.initial_wealth = pl.ones(n) diff --git a/docs/general/user-guide/2_introductory-tutorial.ipynb b/docs/general/user-guide/2_introductory-tutorial.ipynb index 327a32b2..64106483 100644 --- a/docs/general/user-guide/2_introductory-tutorial.ipynb +++ b/docs/general/user-guide/2_introductory-tutorial.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 1, "id": "df4d8623", "metadata": {}, "outputs": [], @@ -44,10 +44,25 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 2, "id": "fc0ee981", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "cannot import name 'Model' from partially initialized module 'mesa_frames' (most likely due to a circular import) (/home/adam/projects/mesa-frames/mesa_frames/__init__.py)", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mImportError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Model, AgentSet, DataCollector\n\u001b[32m 4\u001b[39m \u001b[38;5;28;01mclass\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mMoneyModelDF\u001b[39;00m(Model):\n\u001b[32m 5\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, N: \u001b[38;5;28mint\u001b[39m, agents_cls):\n", + "\u001b[36mFile \u001b[39m\u001b[32m~/projects/mesa-frames/mesa_frames/__init__.py:65\u001b[39m\n\u001b[32m 63\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mconcrete\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01magentset\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m AgentSet\n\u001b[32m 64\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mconcrete\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01magentsetregistry\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m AgentSetRegistry\n\u001b[32m---> \u001b[39m\u001b[32m65\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mconcrete\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mdatacollector\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m DataCollector\n\u001b[32m 66\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mconcrete\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mmodel\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Model\n\u001b[32m 67\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mconcrete\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mspace\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Grid\n", + "\u001b[36mFile \u001b[39m\u001b[32m~/projects/mesa-frames/mesa_frames/concrete/datacollector.py:62\u001b[39m\n\u001b[32m 60\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtempfile\u001b[39;00m\n\u001b[32m 61\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpsycopg2\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m62\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mabstract\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mdatacollector\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m AbstractDataCollector\n\u001b[32m 63\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtyping\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Any, Literal\n\u001b[32m 64\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mcollections\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mabc\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Callable\n", + "\u001b[36mFile \u001b[39m\u001b[32m~/projects/mesa-frames/mesa_frames/abstract/datacollector.py:50\u001b[39m\n\u001b[32m 48\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtyping\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Any, Literal\n\u001b[32m 49\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mcollections\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mabc\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Callable\n\u001b[32m---> \u001b[39m\u001b[32m50\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Model\n\u001b[32m 51\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpolars\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpl\u001b[39;00m\n\u001b[32m 52\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mthreading\u001b[39;00m\n", + "\u001b[31mImportError\u001b[39m: cannot import name 'Model' from partially initialized module 'mesa_frames' (most likely due to a circular import) (/home/adam/projects/mesa-frames/mesa_frames/__init__.py)" + ] + } + ], "source": [ "from mesa_frames import Model, AgentSet, DataCollector\n", "\n", @@ -89,7 +104,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "2bac0126", "metadata": {}, "outputs": [], @@ -97,7 +112,7 @@ "import polars as pl\n", "\n", "\n", - "class MoneyAgentDF(AgentSet):\n", + "class MoneyAgentsConcise(AgentSet):\n", " def __init__(self, n: int, model: Model):\n", " super().__init__(model)\n", " self += pl.DataFrame({\"wealth\": pl.ones(n, eager=True)})\n", @@ -126,7 +141,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "65da4e6f", "metadata": {}, "outputs": [ @@ -155,7 +170,7 @@ ], "source": [ "# Choose either MoneyAgentPandas or MoneyAgentDF\n", - "agent_class = MoneyAgentDF\n", + "agent_class = MoneyAgentsConcise\n", "\n", "# Create and run the model\n", "model = MoneyModelDF(1000, agent_class)\n", @@ -182,12 +197,12 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "id": "fbdb540810924de8", "metadata": {}, "outputs": [], "source": [ - "class MoneyAgentDFConcise(AgentSet):\n", + "class MoneyAgentsConcise(AgentSet):\n", " def __init__(self, n: int, model: Model):\n", " super().__init__(model)\n", " ## Adding the agents to the agent set\n", @@ -242,7 +257,7 @@ " self[new_wealth, \"wealth\"] += new_wealth[\"len\"]\n", "\n", "\n", - "class MoneyAgentDFNative(AgentSet):\n", + "class MoneyAgentsNative(AgentSet):\n", " def __init__(self, n: int, model: Model):\n", " super().__init__(model)\n", " self += pl.DataFrame({\"wealth\": pl.ones(n, eager=True)})\n", @@ -286,7 +301,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "id": "9dbe761af964af5b", "metadata": {}, "outputs": [], @@ -333,7 +348,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "id": "2d864cd3", "metadata": {}, "outputs": [ @@ -367,7 +382,7 @@ "import time\n", "\n", "\n", - "def run_simulation(model: MoneyModel | MoneyModelDF, n_steps: int):\n", + "def run_simulation(model: MoneyModelDF | MoneyModel, n_steps: int):\n", " start_time = time.time()\n", " model.run_model(n_steps)\n", " end_time = time.time()\n", @@ -388,9 +403,9 @@ " if implementation == \"mesa\":\n", " ntime = run_simulation(MoneyModel(n_agents), n_steps)\n", " elif implementation == \"mesa-frames (pl concise)\":\n", - " ntime = run_simulation(MoneyModelDF(n_agents, MoneyAgentDFConcise), n_steps)\n", + " ntime = run_simulation(MoneyModelDF(n_agents, MoneyAgentsConcise), n_steps)\n", " elif implementation == \"mesa-frames (pl native)\":\n", - " ntime = run_simulation(MoneyModelDF(n_agents, MoneyAgentDFNative), n_steps)\n", + " ntime = run_simulation(MoneyModelDF(n_agents, MoneyAgentsNative), n_steps)\n", "\n", " print(f\" Number of agents: {n_agents}, Time: {ntime:.2f} seconds\")\n", " print(\"---------------\")" @@ -413,7 +428,7 @@ ], "metadata": { "kernelspec": { - "display_name": "mesa-frames", + "display_name": ".venv", "language": "python", "name": "python3" }, From 0771ef30572919a2467a8b3fed2fbeae37f0cfbf Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:58:54 +0200 Subject: [PATCH 074/329] Add tests for CustomModel and its step functionality --- tests/{test_modeldf.py => test_model.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{test_modeldf.py => test_model.py} (100%) diff --git a/tests/test_modeldf.py b/tests/test_model.py similarity index 100% rename from tests/test_modeldf.py rename to tests/test_model.py From a234bc86931bb165f5e4fc170c57fe5dc13ff654 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 17:56:22 +0200 Subject: [PATCH 075/329] Update space property type hint to use Space instead of SpaceDF for clarity --- mesa_frames/abstract/agentsetregistry.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index abebe7a2..c8fa6c60 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -703,12 +703,12 @@ def random(self) -> Generator: return self.model.random @property - def space(self) -> mesa_frames.abstract.space.SpaceDF | None: + def space(self) -> mesa_frames.abstract.space.Space | None: """The space of the model. Returns ------- - mesa_frames.abstract.space.SpaceDF | None + mesa_frames.abstract.space.Space | None """ return self.model.space From 028c91f7dc58ca72af82c776eb73abe74a3b7db1 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 18:03:07 +0200 Subject: [PATCH 076/329] Format list comprehensions for improved readability in AgentSetRegistry methods --- mesa_frames/concrete/agentsetregistry.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 9c65c324..e7ffcf16 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -497,7 +497,9 @@ def __contains__(self, name: object) -> bool: """Check if a name is in the registry.""" if not isinstance(name, str): return False - return name in [agentset.name for agentset in self._agentsets if agentset.name is not None] + return name in [ + agentset.name for agentset in self._agentsets if agentset.name is not None + ] def __getitem__(self, key: str) -> AbstractAgentSet: """Get an agent set by name.""" @@ -510,7 +512,9 @@ def __getitem__(self, key: str) -> AbstractAgentSet: def _generate_name(self, base_name: str) -> str: """Generate a unique name for an agent set.""" - existing_names = [agentset.name for agentset in self._agentsets if agentset.name is not None] + existing_names = [ + agentset.name for agentset in self._agentsets if agentset.name is not None + ] if base_name not in existing_names: return base_name counter = 1 From e4737d98719637561a3aa894d8459812d64a74c1 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 18:10:08 +0200 Subject: [PATCH 077/329] Rename parameter in ExampleModel constructor from 'agents' to 'sets' for clarity --- tests/test_datacollector.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_datacollector.py b/tests/test_datacollector.py index 8141f749..b7407711 100644 --- a/tests/test_datacollector.py +++ b/tests/test_datacollector.py @@ -52,9 +52,9 @@ def step(self) -> None: class ExampleModel(Model): - def __init__(self, agents: AgentSetRegistry): + def __init__(self, sets: AgentSetRegistry): super().__init__() - self.sets = agents + self.sets = sets def step(self): self.sets.do("step") From ec1a3579653ff0ab0754e337609607e80689be95 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 18:23:59 +0200 Subject: [PATCH 078/329] Reorder DataCollector import to avoid circular import error --- mesa_frames/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mesa_frames/__init__.py b/mesa_frames/__init__.py index 79a89ba8..1e932cb0 100644 --- a/mesa_frames/__init__.py +++ b/mesa_frames/__init__.py @@ -62,8 +62,9 @@ def __init__(self, width, height): from mesa_frames.concrete.agentset import AgentSet from mesa_frames.concrete.agentsetregistry import AgentSetRegistry -from mesa_frames.concrete.datacollector import DataCollector from mesa_frames.concrete.model import Model +# DataCollector has to be imported after Model or a circular import error will occur +from mesa_frames.concrete.datacollector import DataCollector from mesa_frames.concrete.space import Grid __all__ = ["AgentSetRegistry", "AgentSet", "Model", "Grid", "DataCollector"] From a3e2c56244bb40ad51b7338f698f3682773eb386 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 3 Sep 2025 16:24:51 +0000 Subject: [PATCH 079/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa_frames/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mesa_frames/__init__.py b/mesa_frames/__init__.py index 1e932cb0..20fcbeef 100644 --- a/mesa_frames/__init__.py +++ b/mesa_frames/__init__.py @@ -63,6 +63,7 @@ def __init__(self, width, height): from mesa_frames.concrete.agentset import AgentSet from mesa_frames.concrete.agentsetregistry import AgentSetRegistry from mesa_frames.concrete.model import Model + # DataCollector has to be imported after Model or a circular import error will occur from mesa_frames.concrete.datacollector import DataCollector from mesa_frames.concrete.space import Grid From 040e00c9dea541fb0d2afb34ab7fcc944d67b194 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 3 Sep 2025 16:25:16 +0000 Subject: [PATCH 080/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa_frames/abstract/agentset.py | 1 - mesa_frames/abstract/agentsetregistry.py | 1 - mesa_frames/concrete/agentsetregistry.py | 2 -- mesa_frames/utils.py | 1 - 4 files changed, 5 deletions(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index b08534d8..2bc92c54 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -402,4 +402,3 @@ def name(self) -> str: The name of the agent set """ return self._name - diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index eba8097d..529e09ba 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -711,4 +711,3 @@ def space(self) -> mesa_frames.abstract.space.Space | None: mesa_frames.abstract.space.Space | None """ return self.model.space - diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index e7ffcf16..3ecb9140 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -654,7 +654,6 @@ def __sub__( """ return super().__sub__(agents) - @property def agentsets_by_type(self) -> dict[type[AbstractAgentSet], Self]: """Get the agent sets in the AgentSetRegistry grouped by type. @@ -676,4 +675,3 @@ def copy_without_agentsets() -> Self: agents_df._agentsets = agents_df._agentsets + [agentset] dictionary[agentset.__class__] = agents_df return dictionary - diff --git a/mesa_frames/utils.py b/mesa_frames/utils.py index 4c092384..58b0c85b 100644 --- a/mesa_frames/utils.py +++ b/mesa_frames/utils.py @@ -16,4 +16,3 @@ def _decorator(func): return func return _decorator - From 9afda447c0a08e9baee566432e5479ececf494b4 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 3 Sep 2025 18:26:29 +0200 Subject: [PATCH 081/329] Remove unused import of camel_case_to_snake_case in agentset.py --- mesa_frames/concrete/agentset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index cf5c5aff..9b5c8ff2 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -69,7 +69,7 @@ def step(self): from mesa_frames.concrete.mixin import PolarsMixin from mesa_frames.concrete.model import Model from mesa_frames.types_ import AgentPolarsMask, IntoExpr, PolarsIdsLike -from mesa_frames.utils import camel_case_to_snake_case, copydoc +from mesa_frames.utils import copydoc @copydoc(AbstractAgentSet) From 5750a4f6b380af00792b2a903b118671ac1d2d83 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 3 Sep 2025 16:43:48 +0000 Subject: [PATCH 082/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa_frames/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mesa_frames/__init__.py b/mesa_frames/__init__.py index 1e932cb0..20fcbeef 100644 --- a/mesa_frames/__init__.py +++ b/mesa_frames/__init__.py @@ -63,6 +63,7 @@ def __init__(self, width, height): from mesa_frames.concrete.agentset import AgentSet from mesa_frames.concrete.agentsetregistry import AgentSetRegistry from mesa_frames.concrete.model import Model + # DataCollector has to be imported after Model or a circular import error will occur from mesa_frames.concrete.datacollector import DataCollector from mesa_frames.concrete.space import Grid From ae1390b7a0f3256b065b4ec36ab907354d854c7d Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 4 Sep 2025 23:31:46 +0200 Subject: [PATCH 083/329] Add conftest.py to enable beartype runtime checking for tests --- tests/conftest.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..fd84a7ac --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,11 @@ +"""Conftest for tests. + +Ensure beartype runtime checking is enabled before importing the package. + +This module sets MESA_FRAMES_RUNTIME_TYPECHECKING=1 at import time so tests that +assert beartype failures at import or construct time behave deterministically. +""" + +import os + +os.environ.setdefault("MESA_FRAMES_RUNTIME_TYPECHECKING", "1") From fd6f13bf34684cf99d6ef718af0837a744ab980a Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 4 Sep 2025 23:31:53 +0200 Subject: [PATCH 084/329] Fix import order by adding a newline for clarity in __init__.py --- mesa_frames/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mesa_frames/__init__.py b/mesa_frames/__init__.py index 1e932cb0..20fcbeef 100644 --- a/mesa_frames/__init__.py +++ b/mesa_frames/__init__.py @@ -63,6 +63,7 @@ def __init__(self, width, height): from mesa_frames.concrete.agentset import AgentSet from mesa_frames.concrete.agentsetregistry import AgentSetRegistry from mesa_frames.concrete.model import Model + # DataCollector has to be imported after Model or a circular import error will occur from mesa_frames.concrete.datacollector import DataCollector from mesa_frames.concrete.space import Grid From ed8dc6190f6de8615e08fac714e18e4a6edc411d Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 4 Sep 2025 23:43:43 +0200 Subject: [PATCH 085/329] Enhance type hinting for agent parameters in Space and AbstractDiscreteSpace classes --- mesa_frames/abstract/space.py | 61 +++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index f5982154..6273ed3a 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -52,7 +52,7 @@ def __init__(self, model, dimensions, torus, capacity, neighborhood_type): from abc import abstractmethod from collections.abc import Callable, Collection, Sequence, Sized from itertools import product -from typing import Any, Literal, Self +from typing import Any, Literal, Self, cast from warnings import warn import numpy as np @@ -64,7 +64,6 @@ def __init__(self, model, dimensions, torus, capacity, neighborhood_type): AbstractAgentSetRegistry, ) from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin -from mesa_frames.concrete.agentsetregistry import AgentSetRegistry from mesa_frames.types_ import ( ArrayLike, BoolSeries, @@ -109,7 +108,9 @@ def __init__(self, model: mesa_frames.concrete.model.Model) -> None: def move_agents( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], pos: SpaceCoordinate | SpaceCoordinates, inplace: bool = True, @@ -145,7 +146,9 @@ def move_agents( def place_agents( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], pos: SpaceCoordinate | SpaceCoordinates, inplace: bool = True, @@ -198,10 +201,14 @@ def random_agents( def swap_agents( self, agents0: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], agents1: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -222,8 +229,6 @@ def swap_agents( ------- Self """ - agents0 = self._get_ids_srs(agents0) - agents1 = self._get_ids_srs(agents1) if __debug__: if len(agents0) != len(agents1): raise ValueError("The two sets of agents must have the same length") @@ -257,11 +262,15 @@ def get_directions( pos0: SpaceCoordinate | SpaceCoordinates | None = None, pos1: SpaceCoordinate | SpaceCoordinates | None = None, agents0: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None = None, agents1: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None = None, normalize: bool = False, @@ -298,11 +307,15 @@ def get_distances( pos0: SpaceCoordinate | SpaceCoordinates | None = None, pos1: SpaceCoordinate | SpaceCoordinates | None = None, agents0: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None = None, agents1: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None = None, ) -> DataFrame: @@ -336,7 +349,9 @@ def get_neighbors( radius: int | float | Sequence[int] | Sequence[float] | ArrayLike, pos: SpaceCoordinate | SpaceCoordinates | None = None, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None = None, include_center: bool = False, @@ -438,7 +453,9 @@ def random_pos( def remove_agents( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -467,7 +484,9 @@ def remove_agents( def _get_ids_srs( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], ) -> Series: if isinstance(agents, Sized) and len(agents) == 0: @@ -657,7 +676,9 @@ def move_to_empty( self, agents: IdsLike | AbstractAgentSetRegistry - | Collection[AbstractAgentSetRegistry], + | Collection[AbstractAgentSetRegistry] + | AbstractAgentSet + | Collection[AbstractAgentSet], inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) @@ -668,7 +689,9 @@ def move_to_empty( def move_to_available( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -686,6 +709,7 @@ def move_to_available( Self """ obj = self._get_obj(inplace) + return obj._place_or_move_agents_to_cells( agents, cell_type="available", is_move=True ) @@ -693,11 +717,14 @@ def move_to_available( def place_to_empty( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) + return obj._place_or_move_agents_to_cells( agents, cell_type="empty", is_move=False ) @@ -705,7 +732,9 @@ def place_to_empty( def place_to_available( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -933,7 +962,9 @@ def _check_cells( def _place_or_move_agents_to_cells( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], cell_type: Literal["any", "empty", "available"], is_move: bool, @@ -994,7 +1025,7 @@ def _sample_cells( self, n: int | None, with_replacement: bool, - condition: Callable[[DiscreteSpaceCapacity], BoolSeries], + condition: Callable[[DiscreteSpaceCapacity], BoolSeries | np.ndarray], respect_capacity: bool = True, ) -> DataFrame: """Sample cells from the grid according to a condition on the capacity. @@ -1259,11 +1290,15 @@ def get_directions( pos0: GridCoordinate | GridCoordinates | None = None, pos1: GridCoordinate | GridCoordinates | None = None, agents0: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None = None, agents1: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None = None, normalize: bool = False, @@ -1278,11 +1313,15 @@ def get_distances( pos0: GridCoordinate | GridCoordinates | None = None, pos1: GridCoordinate | GridCoordinates | None = None, agents0: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None = None, agents1: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None = None, ) -> DataFrame: @@ -1311,7 +1350,7 @@ def get_neighbors( def get_neighborhood( self, radius: int | Sequence[int] | ArrayLike, - pos: GridCoordinate | GridCoordinates | None = None, + pos: DiscreteCoordinate | DiscreteCoordinates | None = None, agents: IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] @@ -1594,11 +1633,15 @@ def _calculate_differences( pos0: GridCoordinate | GridCoordinates | None, pos1: GridCoordinate | GridCoordinates | None, agents0: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, agents1: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, ) -> DataFrame: @@ -1694,7 +1737,9 @@ def _get_df_coords( self, pos: GridCoordinate | GridCoordinates | None = None, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None = None, check_bounds: bool = True, @@ -1796,7 +1841,9 @@ def _get_df_coords( def _place_or_move_agents( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], pos: GridCoordinate | GridCoordinates, is_move: bool, From d9dc746e69fee1a9f50e35734e8a640f8f360e0c Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 4 Sep 2025 23:44:50 +0200 Subject: [PATCH 086/329] Refactor agent type checks to use AbstractAgentSetRegistry for improved clarity and consistency --- mesa_frames/abstract/space.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index 6273ed3a..a5e2deed 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -497,7 +497,7 @@ def _get_ids_srs( name="agent_id", dtype="uint64", ) - elif isinstance(agents, AgentSetRegistry): + elif isinstance(agents, AbstractAgentSetRegistry): return self._srs_constructor(agents._ids, name="agent_id", dtype="uint64") elif isinstance(agents, Collection) and ( isinstance(agents[0], AbstractAgentSetRegistry) @@ -512,7 +512,7 @@ def _get_ids_srs( dtype="uint64", ) ) - elif isinstance(a, AgentSetRegistry): + elif isinstance(a, AbstractAgentSetRegistry): ids.append( self._srs_constructor(a._ids, name="agent_id", dtype="uint64") ) From b72e34bb04d8cd5ef311618d79cc3b4319168d17 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 4 Sep 2025 23:52:26 +0200 Subject: [PATCH 087/329] Refactor AgentSet constructor and name property for improved clarity and type consistency --- mesa_frames/concrete/agentset.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 9b5c8ff2..35a714fe 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -83,9 +83,7 @@ class AgentSet(AbstractAgentSet, PolarsMixin): _copy_only_reference: list[str] = ["_model", "_mask"] _mask: pl.Expr | pl.Series - def __init__( - self, model: mesa_frames.concrete.model.Model, name: str | None = None - ) -> None: + def __init__(self, model: Model, name: str | None = None) -> None: """Initialize a new AgentSet. Parameters @@ -104,10 +102,6 @@ def __init__( self._df = pl.DataFrame() self._mask = pl.repeat(True, len(self._df), dtype=pl.Boolean, eager=True) - @property - def name(self) -> str | None: - return getattr(self, "_name", None) - def rename(self, new_name: str) -> str: """Rename this agent set. If attached to AgentSetRegistry, delegate for uniqueness enforcement. @@ -590,7 +584,7 @@ def pos(self) -> pl.DataFrame: return super().pos @property - def name(self) -> str | None: + def name(self) -> str: """Return the name of the AgentSet.""" return self._name From 84f186fdb2fcdb2f0e2b366b613113c36168c638 Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 6 Sep 2025 00:45:46 +0530 Subject: [PATCH 088/329] precommit --- mesa_frames/concrete/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index 773cae73..dbeac5b0 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -224,6 +224,6 @@ def space(self, space: Space) -> None: Parameters ---------- - space : SpaceDF + space : Space """ self._space = space From d50b00f2fdd34ba39bde78290be765e28dae3bd6 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 13:35:40 +0200 Subject: [PATCH 089/329] Replace MoneyAgentDF with MoneyAgents in MoneyModel constructor for consistency --- docs/general/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/index.md b/docs/general/index.md index f0f437e5..9859d2ee 100644 --- a/docs/general/index.md +++ b/docs/general/index.md @@ -60,7 +60,7 @@ class MoneyAgents(AgentSet): class MoneyModel(Model): def __init__(self, N: int): super().__init__() - self.sets += MoneyAgentDF(N, self) + self.sets += MoneyAgents(N, self) def step(self): self.sets.do("step") From 98f4859cdcd17438b1e02275495bcb04dafbcf26 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 13:35:44 +0200 Subject: [PATCH 090/329] Rename MoneyAgentDF to MoneyAgents for consistency in agent set implementation --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 938eb95c..6a16baad 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ The agent implementation differs from base mesa. Agents are only defined at the ```python from mesa-frames import AgentSet -class MoneyAgentDF(AgentSet): +class MoneyAgents(AgentSet): def __init__(self, n: int, model: Model): super().__init__(model) # Adding the agents to the agent set @@ -135,7 +135,7 @@ class MoneyModelDF(Model): def __init__(self, N: int, agents_cls): super().__init__() self.n_agents = N - self.sets += MoneyAgentDF(N, self) + self.sets += MoneyAgents(N, self) def step(self): # Executes the step method for every agentset in self.sets From d3402ee0e7b0c15f2b1d793ccc5ed546628268fc Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 13:35:51 +0200 Subject: [PATCH 091/329] Update tutorial to reflect renaming of agent classes from MoneyAgentPandas and MoneyAgentDF to MoneyAgentsConcise and MoneyAgentsNative --- docs/general/user-guide/2_introductory-tutorial.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/user-guide/2_introductory-tutorial.ipynb b/docs/general/user-guide/2_introductory-tutorial.ipynb index 64106483..8c7ede66 100644 --- a/docs/general/user-guide/2_introductory-tutorial.ipynb +++ b/docs/general/user-guide/2_introductory-tutorial.ipynb @@ -169,7 +169,7 @@ } ], "source": [ - "# Choose either MoneyAgentPandas or MoneyAgentDF\n", + "# Choose either MoneyAgentsConcise or MoneyAgentsNative\n", "agent_class = MoneyAgentsConcise\n", "\n", "# Create and run the model\n", From 2d4854f6aa1fdbad878a0bde6cfd773e6b8736ff Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 13:48:31 +0200 Subject: [PATCH 092/329] Refactor MoneyModel and MoneyAgents classes for consistency and clarity in naming --- .../user-guide/2_introductory-tutorial.ipynb | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/docs/general/user-guide/2_introductory-tutorial.ipynb b/docs/general/user-guide/2_introductory-tutorial.ipynb index 8c7ede66..ec1165da 100644 --- a/docs/general/user-guide/2_introductory-tutorial.ipynb +++ b/docs/general/user-guide/2_introductory-tutorial.ipynb @@ -44,7 +44,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "fc0ee981", "metadata": {}, "outputs": [ @@ -67,7 +67,7 @@ "from mesa_frames import Model, AgentSet, DataCollector\n", "\n", "\n", - "class MoneyModelDF(Model):\n", + "class MoneyModel(Model):\n", " def __init__(self, N: int, agents_cls):\n", " super().__init__()\n", " self.n_agents = N\n", @@ -99,7 +99,7 @@ "source": [ "## Implementing the AgentSet 👥\n", "\n", - "Now, let's implement our `MoneyAgentSet` using polars backends." + "Now, let's implement our `MoneyAgents` using polars backends." ] }, { @@ -112,7 +112,7 @@ "import polars as pl\n", "\n", "\n", - "class MoneyAgentsConcise(AgentSet):\n", + "class MoneyAgents(AgentSet):\n", " def __init__(self, n: int, model: Model):\n", " super().__init__(model)\n", " self += pl.DataFrame({\"wealth\": pl.ones(n, eager=True)})\n", @@ -169,11 +169,8 @@ } ], "source": [ - "# Choose either MoneyAgentsConcise or MoneyAgentsNative\n", - "agent_class = MoneyAgentsConcise\n", - "\n", "# Create and run the model\n", - "model = MoneyModelDF(1000, agent_class)\n", + "model = MoneyModel(1000, MoneyAgents)\n", "model.run_model(100)\n", "\n", "wealth_dist = list(model.sets.df.values())[0]\n", @@ -309,7 +306,7 @@ "import mesa\n", "\n", "\n", - "class MoneyAgent(mesa.Agent):\n", + "class MesaMoneyAgent(mesa.Agent):\n", " \"\"\"An agent with fixed initial wealth.\"\"\"\n", "\n", " def __init__(self, model):\n", @@ -322,24 +319,24 @@ " def step(self):\n", " # Verify agent has some wealth\n", " if self.wealth > 0:\n", - " other_agent: MoneyAgent = self.model.random.choice(self.model.sets)\n", + " other_agent: MesaMoneyAgent = self.model.random.choice(self.model.agents)\n", " if other_agent is not None:\n", " other_agent.wealth += 1\n", " self.wealth -= 1\n", "\n", "\n", - "class MoneyModel(mesa.Model):\n", + "class MesaMoneyModel(mesa.Model):\n", " \"\"\"A model with some number of agents.\"\"\"\n", "\n", " def __init__(self, N: int):\n", " super().__init__()\n", " self.num_agents = N\n", " for _ in range(N):\n", - " self.sets.add(MoneyAgent(self))\n", + " self.agents.add(MesaMoneyAgent(self))\n", "\n", " def step(self):\n", " \"\"\"Advance the model by one step.\"\"\"\n", - " self.sets.shuffle_do(\"step\")\n", + " self.agents.shuffle_do(\"step\")\n", "\n", " def run_model(self, n_steps) -> None:\n", " for _ in range(n_steps):\n", @@ -382,7 +379,7 @@ "import time\n", "\n", "\n", - "def run_simulation(model: MoneyModelDF | MoneyModel, n_steps: int):\n", + "def run_simulation(model: MesaMoneyModel | MoneyModel, n_steps: int):\n", " start_time = time.time()\n", " model.run_model(n_steps)\n", " end_time = time.time()\n", @@ -401,11 +398,11 @@ " print(f\"---------------\\n{implementation}:\")\n", " for n_agents in n_agents_list:\n", " if implementation == \"mesa\":\n", - " ntime = run_simulation(MoneyModel(n_agents), n_steps)\n", + " ntime = run_simulation(MesaMoneyModel(n_agents), n_steps)\n", " elif implementation == \"mesa-frames (pl concise)\":\n", - " ntime = run_simulation(MoneyModelDF(n_agents, MoneyAgentsConcise), n_steps)\n", + " ntime = run_simulation(MoneyModel(n_agents, MoneyAgentsConcise), n_steps)\n", " elif implementation == \"mesa-frames (pl native)\":\n", - " ntime = run_simulation(MoneyModelDF(n_agents, MoneyAgentsNative), n_steps)\n", + " ntime = run_simulation(MoneyModel(n_agents, MoneyAgentsNative), n_steps)\n", "\n", " print(f\" Number of agents: {n_agents}, Time: {ntime:.2f} seconds\")\n", " print(\"---------------\")" From dcee916ace9ecd378eee9c0ef0bb959b3738be5b Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 14:01:35 +0200 Subject: [PATCH 093/329] Update DataCollector tutorial with execution results and fix agent wealth calculations --- docs/general/user-guide/4_datacollector.ipynb | 174 +++++++++++++++--- 1 file changed, 150 insertions(+), 24 deletions(-) diff --git a/docs/general/user-guide/4_datacollector.ipynb b/docs/general/user-guide/4_datacollector.ipynb index 1fdc114f..3fa16b49 100644 --- a/docs/general/user-guide/4_datacollector.ipynb +++ b/docs/general/user-guide/4_datacollector.ipynb @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "9a63283cbaf04dbcab1f6479b197f3a8", "metadata": { "editable": true @@ -53,7 +53,47 @@ "metadata": { "editable": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'model': shape: (5, 5)\n", + " ┌──────┬─────────────────────────────────┬───────┬──────────────┬──────────┐\n", + " │ step ┆ seed ┆ batch ┆ total_wealth ┆ n_agents │\n", + " │ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", + " │ i64 ┆ str ┆ i64 ┆ f64 ┆ i64 │\n", + " ╞══════╪═════════════════════════════════╪═══════╪══════════════╪══════════╡\n", + " │ 2 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", + " │ 4 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", + " │ 6 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", + " │ 8 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", + " │ 10 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", + " └──────┴─────────────────────────────────┴───────┴──────────────┴──────────┘,\n", + " 'agent': shape: (5_000, 4)\n", + " ┌────────────────────┬──────┬─────────────────────────────────┬───────┐\n", + " │ wealth_MoneyAgents ┆ step ┆ seed ┆ batch │\n", + " │ --- ┆ --- ┆ --- ┆ --- │\n", + " │ f64 ┆ i32 ┆ str ┆ i32 │\n", + " ╞════════════════════╪══════╪═════════════════════════════════╪═══════╡\n", + " │ 0.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ 3.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ 1.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ 3.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ 6.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ … ┆ … ┆ … ┆ … │\n", + " │ 4.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ 1.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " └────────────────────┴──────┴─────────────────────────────────┴───────┘}" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from mesa_frames import Model, AgentSet, DataCollector\n", "import polars as pl\n", @@ -76,19 +116,19 @@ "class MoneyModel(Model):\n", " def __init__(self, n: int):\n", " super().__init__()\n", - " self.sets = MoneyAgents(n, self)\n", + " self.sets.add(MoneyAgents(n, self))\n", " self.dc = DataCollector(\n", " model=self,\n", " model_reporters={\n", - " \"total_wealth\": lambda m: m.agents[\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(m.agents),\n", + " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", + " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\", # pull existing column\n", " },\n", " storage=\"memory\", # we'll switch this per example\n", " storage_uri=None,\n", - " trigger=lambda m: m._steps % 2\n", + " trigger=lambda m: m.steps % 2\n", " == 0, # collect every 2 steps via conditional_collect\n", " reset_memory=True,\n", " )\n", @@ -135,10 +175,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "5f14f38c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import os\n", "\n", @@ -147,8 +198,8 @@ "model_csv.dc = DataCollector(\n", " model=model_csv,\n", " model_reporters={\n", - " \"total_wealth\": lambda m: m.agents[\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(m.agents),\n", + " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", + " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -175,20 +226,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "8763a12b2bbd4a93a75aff182afb95dc", "metadata": { "editable": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "os.makedirs(\"./data_parquet\", exist_ok=True)\n", "model_parq = MoneyModel(1000)\n", "model_parq.dc = DataCollector(\n", " model=model_parq,\n", " model_reporters={\n", - " \"total_wealth\": lambda m: m.agents[\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(m.agents),\n", + " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", + " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -217,7 +279,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "7cdc8c89c7104fffa095e18ddfef8986", "metadata": { "editable": true @@ -228,8 +290,8 @@ "model_s3.dc = DataCollector(\n", " model=model_s3,\n", " model_reporters={\n", - " \"total_wealth\": lambda m: m.agents[\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(m.agents),\n", + " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", + " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -257,12 +319,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "938c804e27f84196a10c8828c723f798", "metadata": { "editable": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "CREATE SCHEMA IF NOT EXISTS public;\n", + "CREATE TABLE IF NOT EXISTS public.model_data (\n", + " step INTEGER,\n", + " seed VARCHAR,\n", + " total_wealth BIGINT,\n", + " n_agents INTEGER\n", + ");\n", + "\n", + "\n", + "CREATE TABLE IF NOT EXISTS public.agent_data (\n", + " step INTEGER,\n", + " seed VARCHAR,\n", + " unique_id BIGINT,\n", + " wealth BIGINT\n", + ");\n", + "\n" + ] + } + ], "source": [ "DDL_MODEL = r\"\"\"\n", "CREATE SCHEMA IF NOT EXISTS public;\n", @@ -295,7 +381,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "id": "59bbdb311c014d738909a11f9e486628", "metadata": { "editable": true @@ -324,12 +410,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "id": "8a65eabff63a45729fe45fb5ade58bdc", "metadata": { "editable": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "shape: (5, 5)
stepseedbatchtotal_wealthn_agents
i64stri64f64i64
2"732054881101029867447298951813…0100.0100
4"732054881101029867447298951813…0100.0100
6"732054881101029867447298951813…0100.0100
8"732054881101029867447298951813…0100.0100
10"732054881101029867447298951813…0100.0100
" + ], + "text/plain": [ + "shape: (5, 5)\n", + "┌──────┬─────────────────────────────────┬───────┬──────────────┬──────────┐\n", + "│ step ┆ seed ┆ batch ┆ total_wealth ┆ n_agents │\n", + "│ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", + "│ i64 ┆ str ┆ i64 ┆ f64 ┆ i64 │\n", + "╞══════╪═════════════════════════════════╪═══════╪══════════════╪══════════╡\n", + "│ 2 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", + "│ 4 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", + "│ 6 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", + "│ 8 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", + "│ 10 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", + "└──────┴─────────────────────────────────┴───────┴──────────────┴──────────┘" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "m = MoneyModel(100)\n", "m.dc.trigger = lambda model: model._steps % 3 == 0 # every 3rd step\n", @@ -361,13 +479,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "mesa-frames (3.12.3)", "language": "python", "name": "python3" }, "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", "name": "python", - "version": "3.x" + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.3" } }, "nbformat": 4, From 08288329c9475fbeaf48daeebd165b53d2109a4e Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 14:15:03 +0200 Subject: [PATCH 094/329] Refactor agent and model classes for consistency: rename MoneyModel to MesaMoneyModel and MoneyAgent to MesaMoneyAgent; update agent sets to MoneyAgentsConcise and MoneyAgentsNative. --- examples/boltzmann_wealth/performance_plot.py | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/boltzmann_wealth/performance_plot.py b/examples/boltzmann_wealth/performance_plot.py index e5b0ad47..e565bda3 100644 --- a/examples/boltzmann_wealth/performance_plot.py +++ b/examples/boltzmann_wealth/performance_plot.py @@ -13,11 +13,11 @@ ### ---------- Mesa implementation ---------- ### def mesa_implementation(n_agents: int) -> None: - model = MoneyModel(n_agents) + model = MesaMoneyModel(n_agents) model.run_model(100) -class MoneyAgent(mesa.Agent): +class MesaMoneyAgent(mesa.Agent): """An agent with fixed initial wealth.""" def __init__(self, model): @@ -30,24 +30,24 @@ def __init__(self, model): def step(self): # Verify agent has some wealth if self.wealth > 0: - other_agent = self.random.choice(self.model.sets) + other_agent = self.random.choice(self.model.agents) if other_agent is not None: other_agent.wealth += 1 self.wealth -= 1 -class MoneyModel(mesa.Model): +class MesaMoneyModel(mesa.Model): """A model with some number of agents.""" def __init__(self, N): super().__init__() self.num_agents = N for _ in range(self.num_agents): - self.sets.add(MoneyAgent(self)) + self.agents.add(MesaMoneyAgent(self)) def step(self): """Advance the model by one step.""" - self.sets.shuffle_do("step") + self.agents.shuffle_do("step") def run_model(self, n_steps) -> None: for _ in range(n_steps): @@ -65,7 +65,7 @@ def run_model(self, n_steps) -> None: ### ---------- Mesa-frames implementation ---------- ### -class MoneyAgentDFConcise(AgentSet): +class MoneyAgentsConcise(AgentSet): def __init__(self, n: int, model: Model): super().__init__(model) ## Adding the agents to the agent set @@ -120,7 +120,7 @@ def give_money(self): self[new_wealth, "wealth"] += new_wealth["len"] -class MoneyAgentDFNative(AgentSet): +class MoneyAgentsNative(AgentSet): def __init__(self, n: int, model: Model): super().__init__(model) self += pl.DataFrame({"wealth": pl.ones(n, eager=True)}) @@ -154,7 +154,7 @@ def give_money(self): ) -class MoneyModelDF(Model): +class MoneyModel(Model): def __init__(self, N: int, agents_cls): super().__init__() self.n_agents = N @@ -170,12 +170,12 @@ def run_model(self, n): def mesa_frames_polars_concise(n_agents: int) -> None: - model = MoneyModelDF(n_agents, MoneyAgentDFConcise) + model = MoneyModel(n_agents, MoneyAgentsConcise) model.run_model(100) def mesa_frames_polars_native(n_agents: int) -> None: - model = MoneyModelDF(n_agents, MoneyAgentDFNative) + model = MoneyModel(n_agents, MoneyAgentsNative) model.run_model(100) From 2cd4e00efbb12ed47cc0e03ed0328ef5ec4a6f09 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 15:07:01 +0200 Subject: [PATCH 095/329] Fix agent type reference in SugarscapePolars model: update from AntPolarsBase to AntDFBase for consistency --- examples/sugarscape_ig/ss_polars/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/sugarscape_ig/ss_polars/model.py b/examples/sugarscape_ig/ss_polars/model.py index 61029582..56e76b17 100644 --- a/examples/sugarscape_ig/ss_polars/model.py +++ b/examples/sugarscape_ig/ss_polars/model.py @@ -3,13 +3,13 @@ from mesa_frames import Grid, Model -from .agents import AntPolarsBase +from .agents import AntDFBase class SugarscapePolars(Model): def __init__( self, - agent_type: type[AntPolarsBase], + agent_type: type[AntDFBase], n_agents: int, sugar_grid: np.ndarray | None = None, initial_sugar: np.ndarray | None = None, From 73fa761f5c808457bb2476b0b57982d869279b08 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 15:18:53 +0200 Subject: [PATCH 096/329] Fix model_reporters lambda function in ExampleModel to correctly sum agent wealth --- docs/general/user-guide/1_classes.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/general/user-guide/1_classes.md b/docs/general/user-guide/1_classes.md index d5d55c5c..4fe43e98 100644 --- a/docs/general/user-guide/1_classes.md +++ b/docs/general/user-guide/1_classes.md @@ -79,7 +79,7 @@ class ExampleModel(Model): self.sets = MoneyAgent(self) self.datacollector = DataCollector( model=self, - model_reporters={"total_wealth": lambda m: m.agents["wealth"].sum()}, + model_reporters={"total_wealth": lambda m: lambda m: list(m.sets.df.values())[0]["wealth"].sum()}, agent_reporters={"wealth": "wealth"}, storage="csv", storage_uri="./data", @@ -90,4 +90,4 @@ class ExampleModel(Model): self.sets.step() self.datacollector.conditional_collect() self.datacollector.flush() -``` +``` \ No newline at end of file From 4e02ffc10cac567184a5c77987fe95f49fed4ac3 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 15:32:24 +0200 Subject: [PATCH 097/329] Refactor agent and model classes for consistency: update references from AbstractAgentSet to AgentSet and adjust related documentation. --- ROADMAP.md | 2 +- examples/sugarscape_ig/ss_polars/model.py | 2 +- mesa_frames/concrete/agentset.py | 4 +- mesa_frames/concrete/agentsetregistry.py | 180 +++++++++++----------- mesa_frames/concrete/mixin.py | 2 +- mesa_frames/concrete/model.py | 12 +- 6 files changed, 97 insertions(+), 105 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index c8447773..7dd953f5 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -49,7 +49,7 @@ The Sugarscape example demonstrates the need for this abstraction, as multiple a #### Progress and Next Steps -- Create utility functions in `AbstractDiscreteSpace` and `AbstractAgentSetRegistry` to move agents optimally based on specified attributes +- Create utility functions in `DiscreteSpace` and `AgentSetRegistry` to move agents optimally based on specified attributes - Provide built-in resolution strategies for common concurrency scenarios - Ensure the implementation works efficiently with the vectorized approach of mesa-frames diff --git a/examples/sugarscape_ig/ss_polars/model.py b/examples/sugarscape_ig/ss_polars/model.py index 56e76b17..56a3a83b 100644 --- a/examples/sugarscape_ig/ss_polars/model.py +++ b/examples/sugarscape_ig/ss_polars/model.py @@ -41,7 +41,7 @@ def __init__( def run_model(self, steps: int) -> list[int]: for _ in range(steps): - if len(self.sets) == 0: + if len(list(self.sets.df.values())[0]) == 0: return empty_cells = self.space.empty_cells full_cells = self.space.full_cells diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 3b60c565..55002e9a 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -118,7 +118,7 @@ def add( obj = self._get_obj(inplace) if isinstance(agents, AbstractAgentSet): raise TypeError( - "AgentSet.add() does not accept AbstractAgentSet objects. " + "AgentSet.add() does not accept AgentSet objects. " "Extract the DataFrame with agents.agents.drop('unique_id') first." ) elif isinstance(agents, pl.DataFrame): @@ -314,7 +314,7 @@ def _concatenate_agentsets( all_indices = pl.concat(indices_list) if all_indices.is_duplicated().any(): raise ValueError( - "Some ids are duplicated in the AbstractAgentSets that are trying to be concatenated" + "Some ids are duplicated in the AgentSets that are trying to be concatenated" ) if duplicates_allowed & keep_first_only: # Find the original_index list (ie longest index list), to sort correctly the rows after concatenation diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 9169919a..b9ed1563 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -8,8 +8,8 @@ Classes: AgentSetRegistry(AbstractAgentSetRegistry): - A collection of AbstractAgentSets. This class acts as a container for all - agents in the model, organizing them into separate AbstractAgentSet instances + A collection of AgentSets. This class acts as a container for all + agents in the model, organizing them into separate AgentSet instances based on their types. The AgentSetRegistry class is designed to be used within Model instances to manage @@ -36,7 +36,7 @@ def step(self): self.sets.do("step") Note: - This concrete implementation builds upon the abstract AbstractAgentSetRegistry class + This concrete implementation builds upon the abstract AgentSetRegistry class defined in the mesa_frames.abstract package, providing a ready-to-use agents collection that integrates with the DataFrame-based agent storage system. @@ -53,10 +53,10 @@ def step(self): import numpy as np import polars as pl -from mesa_frames.abstract.agentset import AbstractAgentSet from mesa_frames.abstract.agentsetregistry import ( AbstractAgentSetRegistry, ) +from mesa_frames.concrete.agentset import AgentSet from mesa_frames.types_ import ( AgentMask, AgnosticAgentMask, @@ -69,9 +69,9 @@ def step(self): class AgentSetRegistry(AbstractAgentSetRegistry): - """A collection of AbstractAgentSets. All agents of the model are stored here.""" + """A collection of AgentSets. All agents of the model are stored here.""" - _agentsets: list[AbstractAgentSet] + _agentsets: list[AgentSet] _ids: pl.Series def __init__(self, model: mesa_frames.concrete.model.Model) -> None: @@ -88,17 +88,17 @@ def __init__(self, model: mesa_frames.concrete.model.Model) -> None: def add( self, - agents: AbstractAgentSet | Iterable[AbstractAgentSet], + agents: AgentSet | Iterable[AgentSet], inplace: bool = True, ) -> Self: - """Add an AbstractAgentSet to the AgentSetRegistry. + """Add an AgentSet to the AgentSetRegistry. Parameters ---------- - agents : AbstractAgentSet | Iterable[AbstractAgentSet] - The AbstractAgentSets to add. + agents : AgentSet | Iterable[AgentSet] + The AgentSets to add. inplace : bool, optional - Whether to add the AbstractAgentSets in place. Defaults to True. + Whether to add the AgentSets in place. Defaults to True. Returns ------- @@ -108,7 +108,7 @@ def add( Raises ------ ValueError - If any AbstractAgentSets are already present or if IDs are not unique. + If any AgentSets are already present or if IDs are not unique. """ obj = self._get_obj(inplace) other_list = obj._return_agentsets_list(agents) @@ -126,23 +126,23 @@ def add( return obj @overload - def contains(self, agents: int | AbstractAgentSet) -> bool: ... + def contains(self, agents: int | AgentSet) -> bool: ... @overload - def contains(self, agents: IdsLike | Iterable[AbstractAgentSet]) -> pl.Series: ... + def contains(self, agents: IdsLike | Iterable[AgentSet]) -> pl.Series: ... def contains( - self, agents: IdsLike | AbstractAgentSet | Iterable[AbstractAgentSet] + self, agents: IdsLike | AgentSet | Iterable[AgentSet] ) -> bool | pl.Series: if isinstance(agents, int): return agents in self._ids - elif isinstance(agents, AbstractAgentSet): + elif isinstance(agents, AgentSet): return self._check_agentsets_presence([agents]).any() elif isinstance(agents, Iterable): if len(agents) == 0: return True - elif isinstance(next(iter(agents)), AbstractAgentSet): - agents = cast(Iterable[AbstractAgentSet], agents) + elif isinstance(next(iter(agents)), AgentSet): + agents = cast(Iterable[AgentSet], agents) return self._check_agentsets_presence(list(agents)) else: # IdsLike agents = cast(IdsLike, agents) @@ -154,7 +154,7 @@ def do( self, method_name: str, *args, - mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, return_results: Literal[False] = False, inplace: bool = True, **kwargs, @@ -165,17 +165,17 @@ def do( self, method_name: str, *args, - mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, return_results: Literal[True], inplace: bool = True, **kwargs, - ) -> dict[AbstractAgentSet, Any]: ... + ) -> dict[AgentSet, Any]: ... def do( self, method_name: str, *args, - mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, return_results: bool = False, inplace: bool = True, **kwargs, @@ -211,8 +211,8 @@ def do( def get( self, attr_names: str | Collection[str] | None = None, - mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, - ) -> dict[AbstractAgentSet, Series] | dict[AbstractAgentSet, DataFrame]: + mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, + ) -> dict[AgentSet, Series] | dict[AgentSet, DataFrame]: agentsets_masks = self._get_bool_masks(mask) result = {} @@ -239,18 +239,16 @@ def get( def remove( self, - agents: AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike, + agents: AgentSet | Iterable[AgentSet] | IdsLike, inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): return obj - if isinstance(agents, AbstractAgentSet): + if isinstance(agents, AgentSet): agents = [agents] - if isinstance(agents, Iterable) and isinstance( - next(iter(agents)), AbstractAgentSet - ): - # We have to get the index of the original AbstractAgentSet because the copy made AbstractAgentSets with different hash + if isinstance(agents, Iterable) and isinstance(next(iter(agents)), AgentSet): + # We have to get the index of the original AgentSet because the copy made AgentSets with different hash ids = [self._agentsets.index(agentset) for agentset in iter(agents)] ids.sort(reverse=True) removed_ids = pl.Series(dtype=pl.UInt64) @@ -290,8 +288,8 @@ def remove( def select( self, - mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, - filter_func: Callable[[AbstractAgentSet], AgentMask] | None = None, + mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, + filter_func: Callable[[AgentSet], AgentMask] | None = None, n: int | None = None, inplace: bool = True, negate: bool = False, @@ -310,9 +308,9 @@ def select( def set( self, - attr_names: str | dict[AbstractAgentSet, Any] | Collection[str], + attr_names: str | dict[AgentSet, Any] | Collection[str], values: Any | None = None, - mask: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] = None, + mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) @@ -320,7 +318,7 @@ def set( if isinstance(attr_names, dict): for agentset, values in attr_names.items(): if not inplace: - # We have to get the index of the original AbstractAgentSet because the copy made AbstractAgentSets with different hash + # We have to get the index of the original AgentSet because the copy made AgentSets with different hash id = self._agentsets.index(agentset) agentset = obj._agentsets[id] agentset.set( @@ -371,13 +369,13 @@ def step(self, inplace: bool = True) -> Self: agentset.step() return obj - def _check_ids_presence(self, other: list[AbstractAgentSet]) -> pl.DataFrame: + def _check_ids_presence(self, other: list[AgentSet]) -> pl.DataFrame: """Check if the IDs of the agents to be added are unique. Parameters ---------- - other : list[AbstractAgentSet] - The AbstractAgentSets to check. + other : list[AgentSet] + The AgentSets to check. Returns ------- @@ -404,13 +402,13 @@ def _check_ids_presence(self, other: list[AbstractAgentSet]) -> pl.DataFrame: presence_df = presence_df.slice(self._ids.len()) return presence_df - def _check_agentsets_presence(self, other: list[AbstractAgentSet]) -> pl.Series: + def _check_agentsets_presence(self, other: list[AgentSet]) -> pl.Series: """Check if the agent sets to be added are already present in the AgentSetRegistry. Parameters ---------- - other : list[AbstractAgentSet] - The AbstractAgentSets to check. + other : list[AgentSet] + The AgentSets to check. Returns ------- @@ -429,8 +427,8 @@ def _check_agentsets_presence(self, other: list[AbstractAgentSet]) -> pl.Series: def _get_bool_masks( self, - mask: (AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask]) = None, - ) -> dict[AbstractAgentSet, BoolSeries]: + mask: (AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask]) = None, + ) -> dict[AgentSet, BoolSeries]: return_dictionary = {} if not isinstance(mask, dict): # No need to convert numpy integers - let polars handle them directly @@ -440,38 +438,36 @@ def _get_bool_masks( return return_dictionary def _return_agentsets_list( - self, agentsets: AbstractAgentSet | Iterable[AbstractAgentSet] - ) -> list[AbstractAgentSet]: - """Convert the agentsets to a list of AbstractAgentSet. + self, agentsets: AgentSet | Iterable[AgentSet] + ) -> list[AgentSet]: + """Convert the agentsets to a list of AgentSet. Parameters ---------- - agentsets : AbstractAgentSet | Iterable[AbstractAgentSet] + agentsets : AgentSet | Iterable[AgentSet] Returns ------- - list[AbstractAgentSet] + list[AgentSet] """ - return ( - [agentsets] if isinstance(agentsets, AbstractAgentSet) else list(agentsets) - ) + return [agentsets] if isinstance(agentsets, AgentSet) else list(agentsets) - def __add__(self, other: AbstractAgentSet | Iterable[AbstractAgentSet]) -> Self: - """Add AbstractAgentSets to a new AgentSetRegistry through the + operator. + def __add__(self, other: AgentSet | Iterable[AgentSet]) -> Self: + """Add AgentSets to a new AgentSetRegistry through the + operator. Parameters ---------- - other : AbstractAgentSet | Iterable[AbstractAgentSet] - The AbstractAgentSets to add. + other : AgentSet | Iterable[AgentSet] + The AgentSets to add. Returns ------- Self - A new AgentSetRegistry with the added AbstractAgentSets. + A new AgentSetRegistry with the added AgentSets. """ return super().__add__(other) - def __getattr__(self, name: str) -> dict[AbstractAgentSet, Any]: + def __getattr__(self, name: str) -> dict[AgentSet, Any]: # Avoids infinite recursion of private attributes if __debug__: # Only execute in non-optimized mode if name.startswith("_"): @@ -482,8 +478,8 @@ def __getattr__(self, name: str) -> dict[AbstractAgentSet, Any]: @overload def __getitem__( - self, key: str | tuple[dict[AbstractAgentSet, AgentMask], str] - ) -> dict[AbstractAgentSet, Series | pl.Expr]: ... + self, key: str | tuple[dict[AgentSet, AgentMask], str] + ) -> dict[AgentSet, Series | pl.Expr]: ... @overload def __getitem__( @@ -492,9 +488,9 @@ def __getitem__( Collection[str] | AgnosticAgentMask | IdsLike - | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] + | tuple[dict[AgentSet, AgentMask], Collection[str]] ), - ) -> dict[AbstractAgentSet, DataFrame]: ... + ) -> dict[AgentSet, DataFrame]: ... def __getitem__( self, @@ -503,19 +499,19 @@ def __getitem__( | Collection[str] | AgnosticAgentMask | IdsLike - | tuple[dict[AbstractAgentSet, AgentMask], str] - | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] + | tuple[dict[AgentSet, AgentMask], str] + | tuple[dict[AgentSet, AgentMask], Collection[str]] ), - ) -> dict[AbstractAgentSet, Series | pl.Expr] | dict[AbstractAgentSet, DataFrame]: + ) -> dict[AgentSet, Series | pl.Expr] | dict[AgentSet, DataFrame]: return super().__getitem__(key) - def __iadd__(self, agents: AbstractAgentSet | Iterable[AbstractAgentSet]) -> Self: - """Add AbstractAgentSets to the AgentSetRegistry through the += operator. + def __iadd__(self, agents: AgentSet | Iterable[AgentSet]) -> Self: + """Add AgentSets to the AgentSetRegistry through the += operator. Parameters ---------- - agents : AbstractAgentSet | Iterable[AbstractAgentSet] - The AbstractAgentSets to add. + agents : AgentSet | Iterable[AgentSet] + The AgentSets to add. Returns ------- @@ -527,15 +523,13 @@ def __iadd__(self, agents: AbstractAgentSet | Iterable[AbstractAgentSet]) -> Sel def __iter__(self) -> Iterator[dict[str, Any]]: return (agent for agentset in self._agentsets for agent in iter(agentset)) - def __isub__( - self, agents: AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike - ) -> Self: - """Remove AbstractAgentSets from the AgentSetRegistry through the -= operator. + def __isub__(self, agents: AgentSet | Iterable[AgentSet] | IdsLike) -> Self: + """Remove AgentSets from the AgentSetRegistry through the -= operator. Parameters ---------- - agents : AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike - The AbstractAgentSets or agent IDs to remove. + agents : AgentSet | Iterable[AgentSet] | IdsLike + The AgentSets or agent IDs to remove. Returns ------- @@ -564,8 +558,8 @@ def __setitem__( | Collection[str] | AgnosticAgentMask | IdsLike - | tuple[dict[AbstractAgentSet, AgentMask], str] - | tuple[dict[AbstractAgentSet, AgentMask], Collection[str]] + | tuple[dict[AgentSet, AgentMask], str] + | tuple[dict[AgentSet, AgentMask], Collection[str]] ), values: Any, ) -> None: @@ -574,55 +568,53 @@ def __setitem__( def __str__(self) -> str: return "\n".join([str(agentset) for agentset in self._agentsets]) - def __sub__( - self, agents: AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike - ) -> Self: - """Remove AbstractAgentSets from a new AgentSetRegistry through the - operator. + def __sub__(self, agents: AgentSet | Iterable[AgentSet] | IdsLike) -> Self: + """Remove AgentSets from a new AgentSetRegistry through the - operator. Parameters ---------- - agents : AbstractAgentSet | Iterable[AbstractAgentSet] | IdsLike - The AbstractAgentSets or agent IDs to remove. Supports NumPy integer types. + agents : AgentSet | Iterable[AgentSet] | IdsLike + The AgentSets or agent IDs to remove. Supports NumPy integer types. Returns ------- Self - A new AgentSetRegistry with the removed AbstractAgentSets. + A new AgentSetRegistry with the removed AgentSets. """ return super().__sub__(agents) @property - def df(self) -> dict[AbstractAgentSet, DataFrame]: + def df(self) -> dict[AgentSet, DataFrame]: return {agentset: agentset.df for agentset in self._agentsets} @df.setter - def df(self, other: Iterable[AbstractAgentSet]) -> None: + def df(self, other: Iterable[AgentSet]) -> None: """Set the agents in the AgentSetRegistry. Parameters ---------- - other : Iterable[AbstractAgentSet] - The AbstractAgentSets to set. + other : Iterable[AgentSet] + The AgentSets to set. """ self._agentsets = list(other) @property - def active_agents(self) -> dict[AbstractAgentSet, DataFrame]: + def active_agents(self) -> dict[AgentSet, DataFrame]: return {agentset: agentset.active_agents for agentset in self._agentsets} @active_agents.setter def active_agents( - self, agents: AgnosticAgentMask | IdsLike | dict[AbstractAgentSet, AgentMask] + self, agents: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] ) -> None: self.select(agents, inplace=True) @property - def agentsets_by_type(self) -> dict[type[AbstractAgentSet], Self]: + def agentsets_by_type(self) -> dict[type[AgentSet], Self]: """Get the agent sets in the AgentSetRegistry grouped by type. Returns ------- - dict[type[AbstractAgentSet], Self] + dict[type[AgentSet], Self] A dictionary mapping agent set types to the corresponding AgentSetRegistry. """ @@ -639,13 +631,13 @@ def copy_without_agentsets() -> Self: return dictionary @property - def inactive_agents(self) -> dict[AbstractAgentSet, DataFrame]: + def inactive_agents(self) -> dict[AgentSet, DataFrame]: return {agentset: agentset.inactive_agents for agentset in self._agentsets} @property - def index(self) -> dict[AbstractAgentSet, Index]: + def index(self) -> dict[AgentSet, Index]: return {agentset: agentset.index for agentset in self._agentsets} @property - def pos(self) -> dict[AbstractAgentSet, DataFrame]: + def pos(self) -> dict[AgentSet, DataFrame]: return {agentset: agentset.pos for agentset in self._agentsets} diff --git a/mesa_frames/concrete/mixin.py b/mesa_frames/concrete/mixin.py index 341d558b..4900536e 100644 --- a/mesa_frames/concrete/mixin.py +++ b/mesa_frames/concrete/mixin.py @@ -23,7 +23,7 @@ from mesa_frames.abstract import AbstractAgentSet from mesa_frames.concrete.mixin import PolarsMixin - class AgentSet(AbstractAgentSet, PolarsMixin): + class AgentSet(AgentSet, PolarsMixin): def __init__(self, model): super().__init__(model) self.sets = pl.DataFrame() # Initialize empty DataFrame diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index dbeac5b0..a10ce240 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -46,7 +46,7 @@ def run_model(self): import numpy as np -from mesa_frames.abstract.agentset import AbstractAgentSet +from mesa_frames.concrete.agentset import AgentSet from mesa_frames.abstract.space import Space from mesa_frames.concrete.agentsetregistry import AgentSetRegistry @@ -99,18 +99,18 @@ def steps(self) -> int: """Get the current step count.""" return self._steps - def get_sets_of_type(self, agent_type: type) -> AbstractAgentSet: - """Retrieve the AbstractAgentSet of a specified type. + def get_sets_of_type(self, agent_type: type) -> AgentSet: + """Retrieve the AgentSet of a specified type. Parameters ---------- agent_type : type - The type of AbstractAgentSet to retrieve. + The type of AgentSet to retrieve. Returns ------- - AbstractAgentSet - The AbstractAgentSet of the specified type. + AgentSet + The AgentSet of the specified type. """ for agentset in self._sets._agentsets: if isinstance(agentset, agent_type): From 3637a35abdbc957047a7a8708b9088b1708b10c6 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 15:33:30 +0200 Subject: [PATCH 098/329] Fix missing newline at end of file in ExampleModel documentation --- docs/general/user-guide/1_classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/user-guide/1_classes.md b/docs/general/user-guide/1_classes.md index 4fe43e98..f85c062d 100644 --- a/docs/general/user-guide/1_classes.md +++ b/docs/general/user-guide/1_classes.md @@ -90,4 +90,4 @@ class ExampleModel(Model): self.sets.step() self.datacollector.conditional_collect() self.datacollector.flush() -``` \ No newline at end of file +``` From 2826c5cf74e3d16bab874316e68ebb6c20d92f5f Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 15:43:20 +0200 Subject: [PATCH 099/329] Remove unused import of Model in agentset.py --- mesa_frames/concrete/agentset.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 55002e9a..5c64aef6 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -67,7 +67,6 @@ def step(self): from mesa_frames.abstract.agentset import AbstractAgentSet from mesa_frames.concrete.mixin import PolarsMixin -from mesa_frames.concrete.model import Model from mesa_frames.types_ import AgentPolarsMask, IntoExpr, PolarsIdsLike from mesa_frames.utils import copydoc From 475c4cbe4f8e5f1786665dcdab4d5bf44bb628c7 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 15:50:00 +0200 Subject: [PATCH 100/329] Fix class name in documentation: update Space to AbstractSpace for clarity --- mesa_frames/abstract/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa_frames/abstract/__init__.py b/mesa_frames/abstract/__init__.py index 127c1784..bfa358d0 100644 --- a/mesa_frames/abstract/__init__.py +++ b/mesa_frames/abstract/__init__.py @@ -14,7 +14,7 @@ - DataFrameMixin: Mixin class defining the interface for DataFrame operations. space.py: - - Space: Abstract base class for all space classes. + - AbstractSpace: Abstract base class for all space classes. - AbstractDiscreteSpace: Abstract base class for discrete space classes (Grids and Networks). - AbstractGrid: Abstract base class for grid classes. From 4ef6dfc25e14d73d568bcf9f1b6370251dae3790 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 16:18:59 +0200 Subject: [PATCH 101/329] Refactor AbstractAgentSet class: remove inheritance from AbstractAgentSetRegistry and add contains method overloads --- mesa_frames/abstract/agentset.py | 107 +++++++++++++++++-------------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index 2bc92c54..4dffc9de 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -35,7 +35,7 @@ ) -class AbstractAgentSet(AbstractAgentSetRegistry, DataFrameMixin): +class AbstractAgentSet(DataFrameMixin): """The AbstractAgentSet class is a container for agents of the same type. Parameters @@ -44,6 +44,7 @@ class AbstractAgentSet(AbstractAgentSetRegistry, DataFrameMixin): The model that the agent set belongs to. """ + _copy_only_reference: list[str] = ["_model"] _df: DataFrame # The agents in the AbstractAgentSet _mask: AgentMask # The underlying mask used for the active agents in the AbstractAgentSet. _model: ( @@ -79,6 +80,31 @@ def add( """ ... + @overload + @abstractmethod + def contains(self, agents: int) -> bool: ... + + @overload + @abstractmethod + def contains(self, agents: IdsLike) -> BoolSeries: ... + + @abstractmethod + def contains(self, agents: IdsLike) -> bool | BoolSeries: + """Check if agents with the specified IDs are in the AgentSet. + + Parameters + ---------- + agents : mesa_frames.concrete.agents.AgentSetDF | IdsLike + The ID(s) to check for. + + Returns + ------- + bool | BoolSeries + True if the agent is in the AgentSet, False otherwise. + """ + ... + + @abstractmethod def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: """Remove an agent from the AbstractAgentSet. Does not raise an error if the agent is not found. @@ -94,65 +120,64 @@ def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: Self The updated AbstractAgentSet. """ - return super().discard(agents, inplace) @overload + @abstractmethod def do( self, method_name: str, - *args, + *args: Any, mask: AgentMask | None = None, return_results: Literal[False] = False, inplace: bool = True, - **kwargs, + **kwargs: Any, ) -> Self: ... @overload + @abstractmethod def do( self, method_name: str, - *args, + *args: Any, mask: AgentMask | None = None, return_results: Literal[True], inplace: bool = True, - **kwargs, + **kwargs: Any, ) -> Any: ... + @abstractmethod def do( self, method_name: str, - *args, + *args: Any, mask: AgentMask | None = None, return_results: bool = False, inplace: bool = True, - **kwargs, + **kwargs: Any, ) -> Self | Any: - masked_df = self._get_masked_df(mask) - # If the mask is empty, we can use the object as is - if len(masked_df) == len(self._df): - obj = self._get_obj(inplace) - method = getattr(obj, method_name) - result = method(*args, **kwargs) - else: # If the mask is not empty, we need to create a new masked AbstractAgentSet and concatenate the AbstractAgentSets at the end - obj = self._get_obj(inplace=False) - obj._df = masked_df - original_masked_index = obj._get_obj_copy(obj.index) - method = getattr(obj, method_name) - result = method(*args, **kwargs) - obj._concatenate_agentsets( - [self], - duplicates_allowed=True, - keep_first_only=True, - original_masked_index=original_masked_index, - ) - if inplace: - for key, value in obj.__dict__.items(): - setattr(self, key, value) - obj = self - if return_results: - return result - else: - return obj + """Invoke a method on the AgentSet. + + Parameters + ---------- + method_name : str + The name of the method to invoke. + *args : Any + Positional arguments to pass to the method + mask : AgentMask | None, optional + The subset of agents on which to apply the method + return_results : bool, optional + Whether to return the result of the method, by default False + inplace : bool, optional + Whether the operation should be done inplace, by default False + **kwargs : Any + Keyword arguments to pass to the method + + Returns + ------- + Self | Any + The updated AgentSet or the result of the method. + """ + ... @abstractmethod @overload @@ -182,20 +207,6 @@ def step(self) -> None: """Run a single step of the AbstractAgentSet. This method should be overridden by subclasses.""" ... - def remove(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: - if isinstance(agents, str) and agents == "active": - agents = self.active_agents - if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): - return self._get_obj(inplace) - agents = self._df_index(self._get_masked_df(agents), "unique_id") - sets = self.model.sets.remove(agents, inplace=inplace) - # TODO: Refactor AgentSetRegistry to return dict[str, AbstractAgentSet] instead of dict[AbstractAgentSet, DataFrame] - # And assign a name to AbstractAgentSet? This has to be replaced by a nicer API of AgentSetRegistry - for agentset in sets.df.keys(): - if isinstance(agentset, self.__class__): - return agentset - return self - @abstractmethod def _concatenate_agentsets( self, From 50548de63f238b0d1f38eb8a66587af5c6fe7bc3 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 16:19:07 +0200 Subject: [PATCH 102/329] Add method overloads for do and implement remove method in AgentSet class --- mesa_frames/concrete/agentset.py | 72 +++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 968c664c..7b91a0c8 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -67,7 +67,7 @@ def step(self): from mesa_frames.abstract.agentset import AbstractAgentSet from mesa_frames.concrete.mixin import PolarsMixin -from mesa_frames.types_ import AgentPolarsMask, IntoExpr, PolarsIdsLike +from mesa_frames.types_ import AgentMask, AgentPolarsMask, IntoExpr, PolarsIdsLike from mesa_frames.utils import copydoc @@ -214,6 +214,64 @@ def contains( else: return agents in self._df["unique_id"] + @overload + def do( + self, + method_name: str, + *args, + mask: AgentMask | None = None, + return_results: Literal[False] = False, + inplace: bool = True, + **kwargs, + ) -> Self: ... + + @overload + def do( + self, + method_name: str, + *args, + mask: AgentMask | None = None, + return_results: Literal[True], + inplace: bool = True, + **kwargs, + ) -> Any: ... + + def do( + self, + method_name: str, + *args, + mask: AgentMask | None = None, + return_results: bool = False, + inplace: bool = True, + **kwargs, + ) -> Self | Any: + masked_df = self._get_masked_df(mask) + # If the mask is empty, we can use the object as is + if len(masked_df) == len(self._df): + obj = self._get_obj(inplace) + method = getattr(obj, method_name) + result = method(*args, **kwargs) + else: # If the mask is not empty, we need to create a new masked AbstractAgentSet and concatenate the AbstractAgentSets at the end + obj = self._get_obj(inplace=False) + obj._df = masked_df + original_masked_index = obj._get_obj_copy(obj.index) + method = getattr(obj, method_name) + result = method(*args, **kwargs) + obj._concatenate_agentsets( + [self], + duplicates_allowed=True, + keep_first_only=True, + original_masked_index=original_masked_index, + ) + if inplace: + for key, value in obj.__dict__.items(): + setattr(self, key, value) + obj = self + if return_results: + return result + else: + return obj + def get( self, attr_names: IntoExpr | Iterable[IntoExpr] | None, @@ -231,6 +289,18 @@ def get( return masked_df[masked_df.columns[0]] return masked_df + def remove(self, agents: PolarsIdsLike | AgentMask, inplace: bool = True) -> Self: + if isinstance(agents, str) and agents == "active": + agents = self.active_agents + if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): + return self._get_obj(inplace) + agents = self._df_index(self._get_masked_df(agents), "unique_id") + sets = self.model.sets.remove(agents, inplace=inplace) + for agentset in sets.df.keys(): + if isinstance(agentset, self.__class__): + return agentset + return self + def set( self, attr_names: str | Collection[str] | dict[str, Any] | None = None, From e08c9286f7017fad37e68334ede2a8c32b38b3e2 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Wed, 10 Sep 2025 16:59:15 +0200 Subject: [PATCH 103/329] Refactor AbstractAgentSet class: add remove method and improve agent management functionality --- mesa_frames/abstract/agentset.py | 100 +++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 12 deletions(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index 4dffc9de..c7bf2224 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -20,10 +20,12 @@ from abc import abstractmethod from collections.abc import Collection, Iterable, Iterator +from contextlib import suppress from typing import Any, Literal, Self, overload -from mesa_frames.abstract.agentsetregistry import AbstractAgentSetRegistry -from mesa_frames.abstract.mixin import DataFrameMixin +from numpy.random import Generator + +from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin from mesa_frames.types_ import ( AgentMask, BoolSeries, @@ -35,7 +37,7 @@ ) -class AbstractAgentSet(DataFrameMixin): +class AbstractAgentSet(CopyMixin, DataFrameMixin): """The AbstractAgentSet class is a container for agents of the same type. Parameters @@ -76,7 +78,7 @@ def add( Returns ------- Self - A new AbstractAgentSetRegistry with the added agents. + A new AbstractAgentSet with the added agents. """ ... @@ -104,7 +106,6 @@ def contains(self, agents: IdsLike) -> bool | BoolSeries: """ ... - @abstractmethod def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: """Remove an agent from the AbstractAgentSet. Does not raise an error if the agent is not found. @@ -120,6 +121,27 @@ def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: Self The updated AbstractAgentSet. """ + with suppress(KeyError, ValueError): + return self.remove(agents, inplace=inplace) + return self._get_obj(inplace) + + @abstractmethod + def remove(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: + """Remove agents from this AbstractAgentSet. + + Parameters + ---------- + agents : IdsLike | AgentMask + The agents or mask to remove. + inplace : bool, optional + Whether to remove in place, by default True. + + Returns + ------- + Self + The updated agent set. + """ + ... @overload @abstractmethod @@ -296,9 +318,9 @@ def __add__(self, other: DataFrame | DataFrameInput) -> Self: Returns ------- Self - A new AbstractAgentSetRegistry with the added agents. + A new AbstractAgentSet with the added agents. """ - return super().__add__(other) + return self.add(other, inplace=False) def __iadd__(self, other: DataFrame | DataFrameInput) -> Self: """ @@ -316,9 +338,17 @@ def __iadd__(self, other: DataFrame | DataFrameInput) -> Self: Returns ------- Self - The updated AbstractAgentSetRegistry. + The updated AbstractAgentSet. """ - return super().__iadd__(other) + return self.add(other, inplace=True) + + def __isub__(self, other: IdsLike | AgentMask | DataFrame) -> Self: + """Remove agents via -= operator.""" + return self.discard(other, inplace=True) + + def __sub__(self, other: IdsLike | AgentMask | DataFrame) -> Self: + """Return a new set with agents removed via - operator.""" + return self.discard(other, inplace=False) @abstractmethod def __getattr__(self, name: str) -> Any: @@ -347,9 +377,20 @@ def __getitem__( | tuple[AgentMask, Collection[str]] ), ) -> Series | DataFrame: - attr = super().__getitem__(key) - assert isinstance(attr, (Series, DataFrame, Index)) - return attr + # Mirror registry/old container behavior: delegate to get() + if isinstance(key, tuple): + return self.get(mask=key[0], attr_names=key[1]) + else: + if isinstance(key, str) or ( + isinstance(key, Collection) and all(isinstance(k, str) for k in key) + ): + return self.get(attr_names=key) + else: + return self.get(mask=key) + + def __contains__(self, agents: int) -> bool: + """Membership test for an agent id in this set.""" + return bool(self.contains(agents)) def __len__(self) -> int: return len(self._df) @@ -387,6 +428,7 @@ def active_agents(self) -> DataFrame: ... def inactive_agents(self) -> DataFrame: ... @property + @abstractmethod def index(self) -> Index: ... @property @@ -413,3 +455,37 @@ def name(self) -> str: The name of the agent set """ return self._name + + @property + def model(self) -> mesa_frames.concrete.model.Model: + return self._model + + @property + def random(self) -> Generator: + return self.model.random + + @property + def space(self) -> mesa_frames.abstract.space.Space | None: + return self.model.space + + def __setitem__( + self, + key: str + | Collection[str] + | AgentMask + | tuple[AgentMask, str | Collection[str]], + values: Any, + ) -> None: + """Set values using [] syntax, delegating to set().""" + if isinstance(key, tuple): + self.set(mask=key[0], attr_names=key[1], values=values) + else: + if isinstance(key, str) or ( + isinstance(key, Collection) and all(isinstance(k, str) for k in key) + ): + try: + self.set(attr_names=key, values=values) + except KeyError: # key may actually be a mask + self.set(attr_names=None, mask=key, values=values) + else: + self.set(attr_names=None, mask=key, values=values) From 809570d572b0fb3d794fb7a5757d53645a248be1 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 14:25:31 +0200 Subject: [PATCH 104/329] Refactor AbstractAgentSetRegistry: update discard, add, and contains methods to use AgentSetSelector; enhance type annotations for clarity --- mesa_frames/abstract/agentsetregistry.py | 508 ++++++++--------------- mesa_frames/types_.py | 12 +- 2 files changed, 185 insertions(+), 335 deletions(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index 529e09ba..2fdc3c28 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -51,12 +51,10 @@ def __init__(self, model): from mesa_frames.abstract.mixin import CopyMixin from mesa_frames.types_ import ( - AgentMask, BoolSeries, - DataFrame, - DataFrameInput, - IdsLike, Index, + KeyBy, + AgentSetSelector, Series, ) @@ -74,20 +72,17 @@ def __init__(self) -> None: ... def discard( self, - agents: IdsLike - | AgentMask - | mesa_frames.abstract.agentset.AbstractAgentSet - | Collection[mesa_frames.abstract.agentset.AbstractAgentSet], + sets: AgentSetSelector, inplace: bool = True, ) -> Self: - """Remove agents from the AbstractAgentSetRegistry. Does not raise an error if the agent is not found. + """Remove AgentSets selected by ``sets``. Ignores missing. Parameters ---------- - agents : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to remove + sets : AgentSetSelector + Which AgentSets to remove (instance, type, name, or collection thereof). inplace : bool - Whether to remove the agent in place. Defaults to True. + Whether to remove in place. Defaults to True. Returns ------- @@ -95,26 +90,26 @@ def discard( The updated AbstractAgentSetRegistry. """ with suppress(KeyError, ValueError): - return self.remove(agents, inplace=inplace) + return self.remove(sets, inplace=inplace) return self._get_obj(inplace) @abstractmethod def add( self, - agents: DataFrame - | DataFrameInput - | mesa_frames.abstract.agentset.AbstractAgentSet - | Collection[mesa_frames.abstract.agentset.AbstractAgentSet], + sets: ( + mesa_frames.abstract.agentset.AbstractAgentSet + | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] + ), inplace: bool = True, ) -> Self: - """Add agents to the AbstractAgentSetRegistry. + """Add AgentSets to the AbstractAgentSetRegistry. Parameters ---------- - agents : DataFrame | DataFrameInput | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to add. + agents : mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] + The AgentSet(s) to add. inplace : bool - Whether to add the agents in place. Defaults to True. + Whether to add in place. Defaults to True. Returns ------- @@ -125,29 +120,40 @@ def add( @overload @abstractmethod - def contains(self, agents: int) -> bool: ... + def contains( + self, + sets: ( + mesa_frames.abstract.agentset.AbstractAgentSet + | type[mesa_frames.abstract.agentset.AbstractAgentSet] + | str + ), + ) -> bool: ... @overload @abstractmethod def contains( - self, agents: mesa_frames.abstract.agentset.AbstractAgentSet | IdsLike + self, + sets: Collection[ + mesa_frames.abstract.agentset.AbstractAgentSet + | type[mesa_frames.abstract.agentset.AbstractAgentSet] + | str + ], ) -> BoolSeries: ... @abstractmethod - def contains( - self, agents: mesa_frames.abstract.agentset.AbstractAgentSet | IdsLike - ) -> bool | BoolSeries: - """Check if agents with the specified IDs are in the AbstractAgentSetRegistry. + def contains(self, sets: AgentSetSelector) -> bool | BoolSeries: + """Check if selected AgentSets are present in the registry. Parameters ---------- - agents : mesa_frames.abstract.agentset.AbstractAgentSet | IdsLike - The ID(s) to check for. + sets : AgentSetSelector + An AgentSet instance, class/type, name string, or a collection of + those. For collections, returns a BoolSeries aligned with input order. Returns ------- bool | BoolSeries - True if the agent is in the AbstractAgentSetRegistry, False otherwise. + Boolean for single selector values; BoolSeries for collections. """ @overload @@ -156,9 +162,10 @@ def do( self, method_name: str, *args: Any, - mask: AgentMask | None = None, + sets: AgentSetSelector | None = None, return_results: Literal[False] = False, inplace: bool = True, + key_by: KeyBy = "name", **kwargs: Any, ) -> Self: ... @@ -168,22 +175,35 @@ def do( self, method_name: str, *args: Any, - mask: AgentMask | None = None, + sets: AgentSetSelector, return_results: Literal[True], inplace: bool = True, + key_by: KeyBy = "name", **kwargs: Any, - ) -> Any | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Any]: ... + ) -> ( + Any + | dict[str, Any] + | dict[int, Any] + | dict[type[mesa_frames.abstract.agentset.AbstractAgentSet], Any] + ): ... @abstractmethod def do( self, method_name: str, *args: Any, - mask: AgentMask | None = None, + sets: AgentSetSelector = None, return_results: bool = False, inplace: bool = True, + key_by: KeyBy = "name", **kwargs: Any, - ) -> Self | Any | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Any]: + ) -> ( + Self + | Any + | dict[str, Any] + | dict[int, Any] + | dict[type[mesa_frames.abstract.agentset.AbstractAgentSet], Any] + ): """Invoke a method on the AbstractAgentSetRegistry. Parameters @@ -192,71 +212,88 @@ def do( The name of the method to invoke. *args : Any Positional arguments to pass to the method - mask : AgentMask | None, optional - The subset of agents on which to apply the method + sets : AgentSetSelector, optional + Which AgentSets to target (instance, type, name, or collection thereof). Defaults to all. return_results : bool, optional - Whether to return the result of the method, by default False + Whether to return per-set results as a dictionary, by default False. inplace : bool, optional Whether the operation should be done inplace, by default False + key_by : KeyBy, optional + Key domain for the returned mapping when ``return_results`` is True. + - "name" (default) → keys are set names (str) + - "index" → keys are positional indices (int) + - "type" → keys are concrete set classes (type) **kwargs : Any Keyword arguments to pass to the method Returns ------- - Self | Any | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Any] - The updated AbstractAgentSetRegistry or the result of the method. + Self | Any | dict[str, Any] | dict[int, Any] | dict[type[AbstractAgentSet], Any] + The updated registry, or the method result(s). When ``return_results`` + is True, returns a dictionary keyed per ``key_by``. """ ... - @abstractmethod @overload - def get(self, attr_names: str) -> Series | dict[str, Series]: ... - @abstractmethod + def get( + self, key: int, default: None = ... + ) -> mesa_frames.abstract.agentset.AbstractAgentSet | None: ... + @overload + @abstractmethod def get( - self, attr_names: Collection[str] | None = None - ) -> DataFrame | dict[str, DataFrame]: ... + self, key: str, default: None = ... + ) -> mesa_frames.abstract.agentset.AbstractAgentSet | None: ... + @overload @abstractmethod def get( self, - attr_names: str | Collection[str] | None = None, - mask: AgentMask | None = None, - ) -> Series | dict[str, Series] | DataFrame | dict[str, DataFrame]: - """Retrieve the value of a specified attribute for each agent in the AbstractAgentSetRegistry. + key: type[mesa_frames.abstract.agentset.AbstractAgentSet], + default: None = ..., + ) -> list[mesa_frames.abstract.agentset.AbstractAgentSet]: ... - Parameters - ---------- - attr_names : str | Collection[str] | None, optional - The attributes to retrieve. If None, all attributes are retrieved. Defaults to None. - mask : AgentMask | None, optional - The AgentMask of agents to retrieve the attribute for. If None, attributes of all agents are returned. Defaults to None. + @overload + @abstractmethod + def get( + self, + key: int | str | type[mesa_frames.abstract.agentset.AbstractAgentSet], + default: mesa_frames.abstract.agentset.AbstractAgentSet + | list[mesa_frames.abstract.agentset.AbstractAgentSet] + | None, + ) -> ( + mesa_frames.abstract.agentset.AbstractAgentSet + | list[mesa_frames.abstract.agentset.AbstractAgentSet] + | None + ): ... - Returns - ------- - Series | dict[str, Series] | DataFrame | dict[str, DataFrame] - The attribute values. - """ - ... + @abstractmethod + def get( + self, + key: int | str | type[mesa_frames.abstract.agentset.AbstractAgentSet], + default: mesa_frames.abstract.agentset.AbstractAgentSet + | list[mesa_frames.abstract.agentset.AbstractAgentSet] + | None = None, + ) -> ( + mesa_frames.abstract.agentset.AbstractAgentSet + | list[mesa_frames.abstract.agentset.AbstractAgentSet] + | None + ): + """Safe lookup for AgentSet(s) by index, name, or type.""" @abstractmethod def remove( self, - agents: ( - IdsLike - | AgentMask - | mesa_frames.abstract.agentset.AbstractAgentSet - | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - ), + sets: AgentSetSelector, inplace: bool = True, ) -> Self: - """Remove the agents from the AbstractAgentSetRegistry. + """Remove AgentSets from the AbstractAgentSetRegistry. Parameters ---------- - agents : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to remove. + sets : AgentSetSelector + Which AgentSets to remove (instance, type, name, or collection thereof). inplace : bool, optional Whether to remove the agent in place. @@ -267,96 +304,46 @@ def remove( """ ... - @abstractmethod - def select( - self, - mask: AgentMask | None = None, - filter_func: Callable[[Self], AgentMask] | None = None, - n: int | None = None, - negate: bool = False, - inplace: bool = True, - ) -> Self: - """Select agents in the AbstractAgentSetRegistry based on the given criteria. - - Parameters - ---------- - mask : AgentMask | None, optional - The AgentMask of agents to be selected, by default None - filter_func : Callable[[Self], AgentMask] | None, optional - A function which takes as input the AbstractAgentSetRegistry and returns a AgentMask, by default None - n : int | None, optional - The maximum number of agents to be selected, by default None - negate : bool, optional - If the selection should be negated, by default False - inplace : bool, optional - If the operation should be performed on the same object, by default True - - Returns - ------- - Self - A new or updated AbstractAgentSetRegistry. - """ - ... - - @abstractmethod - @overload - def set( - self, - attr_names: dict[str, Any], - values: None, - mask: AgentMask | None = None, - inplace: bool = True, - ) -> Self: ... + # select() intentionally removed from the abstract API. @abstractmethod - @overload - def set( + def replace( self, - attr_names: str | Collection[str], - values: Any, - mask: AgentMask | None = None, - inplace: bool = True, - ) -> Self: ... - - @abstractmethod - def set( - self, - attr_names: DataFrameInput | str | Collection[str], - values: Any | None = None, - mask: AgentMask | None = None, + mapping: ( + dict[int | str, mesa_frames.abstract.agentset.AbstractAgentSet] + | list[tuple[int | str, mesa_frames.abstract.agentset.AbstractAgentSet]] + ), + *, inplace: bool = True, + atomic: bool = True, ) -> Self: - """Set the value of a specified attribute or attributes for each agent in the mask in AbstractAgentSetRegistry. + """Batch assign/replace AgentSets by index or name. Parameters ---------- - attr_names : DataFrameInput | str | Collection[str] - The key can be: - - A string: sets the specified column of the agents in the AbstractAgentSetRegistry. - - A collection of strings: sets the specified columns of the agents in the AbstractAgentSetRegistry. - - A dictionary: keys should be attributes and values should be the values to set. Value should be None. - values : Any | None - The value to set the attribute to. If None, attr_names must be a dictionary. - mask : AgentMask | None - The AgentMask of agents to set the attribute for. - inplace : bool - Whether to set the attribute in place. + mapping : dict[int | str, AbstractAgentSet] | list[tuple[int | str, AbstractAgentSet]] + Keys are indices or names to assign; values are AgentSets bound to the same model. + inplace : bool, optional + Whether to apply on this registry or return a copy, by default True. + atomic : bool, optional + When True, validates all keys and name invariants before applying any + change; either all assignments succeed or none are applied. Returns ------- Self - The updated agent set. + Updated registry. """ ... @abstractmethod def shuffle(self, inplace: bool = False) -> Self: - """Shuffles the order of agents in the AbstractAgentSetRegistry. + """Shuffle the order of AgentSets in the registry. Parameters ---------- inplace : bool - Whether to shuffle the agents in place. + Whether to shuffle in place. Returns ------- @@ -373,7 +360,7 @@ def sort( **kwargs, ) -> Self: """ - Sorts the agents in the agent set based on the given criteria. + Sort the AgentSets in the registry based on the given criteria. Parameters ---------- @@ -394,145 +381,75 @@ def sort( def __add__( self, - other: DataFrame - | DataFrameInput - | mesa_frames.abstract.agentset.AbstractAgentSet + other: mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet], ) -> Self: - """Add agents to a new AbstractAgentSetRegistry through the + operator. - - Parameters - ---------- - other : DataFrame | DataFrameInput | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to add. - - Returns - ------- - Self - A new AbstractAgentSetRegistry with the added agents. - """ - return self.add(agents=other, inplace=False) + """Add AgentSets to a new AbstractAgentSetRegistry through the + operator.""" + return self.add(sets=other, inplace=False) def __contains__( - self, agents: int | mesa_frames.abstract.agentset.AbstractAgentSet + self, sets: mesa_frames.abstract.agentset.AbstractAgentSet ) -> bool: - """Check if an agent is in the AbstractAgentSetRegistry. + """Check if an AgentSet is in the AbstractAgentSetRegistry.""" + return bool(self.contains(sets=sets)) - Parameters - ---------- - agents : int | mesa_frames.abstract.agentset.AbstractAgentSet - The ID(s) or AbstractAgentSet to check for. - - Returns - ------- - bool - True if the agent is in the AbstractAgentSetRegistry, False otherwise. - """ - return self.contains(agents=agents) + @overload + def __getitem__( + self, key: int + ) -> mesa_frames.abstract.agentset.AbstractAgentSet: ... @overload def __getitem__( - self, key: str | tuple[AgentMask, str] - ) -> Series | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Series]: ... + self, key: str + ) -> mesa_frames.abstract.agentset.AbstractAgentSet: ... @overload def __getitem__( - self, - key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], - ) -> ( - DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] - ): ... + self, key: type[mesa_frames.abstract.agentset.AbstractAgentSet] + ) -> list[mesa_frames.abstract.agentset.AbstractAgentSet]: ... def __getitem__( - self, - key: ( - str - | Collection[str] - | AgentMask - | tuple[AgentMask, str] - | tuple[AgentMask, Collection[str]] - | tuple[ - dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str - ] - | tuple[ - dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], - Collection[str], - ] - ), + self, key: int | str | type[mesa_frames.abstract.agentset.AbstractAgentSet] ) -> ( - Series - | DataFrame - | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Series] - | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] + mesa_frames.abstract.agentset.AbstractAgentSet + | list[mesa_frames.abstract.agentset.AbstractAgentSet] ): - """Implement the [] operator for the AbstractAgentSetRegistry. - - The key can be: - - An attribute or collection of attributes (eg. AbstractAgentSetRegistry["str"], AbstractAgentSetRegistry[["str1", "str2"]]): returns the specified column(s) of the agents in the AbstractAgentSetRegistry. - - An AgentMask (eg. AbstractAgentSetRegistry[AgentMask]): returns the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. - - A tuple (eg. AbstractAgentSetRegistry[AgentMask, "str"]): returns the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. - - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, "str"]): returns the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. - - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, Collection[str]]): returns the specified columns of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. - - Parameters - ---------- - key : str | Collection[str] | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] - The key to retrieve. - - Returns - ------- - Series | DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Series] | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] - The attribute values. - """ - # TODO: fix types - if isinstance(key, tuple): - return self.get(mask=key[0], attr_names=key[1]) - else: - if isinstance(key, str) or ( - isinstance(key, Collection) and all(isinstance(k, str) for k in key) - ): - return self.get(attr_names=key) - else: - return self.get(mask=key) + """Retrieve AgentSet(s) by index, name, or type.""" def __iadd__( self, other: ( - DataFrame - | DataFrameInput - | mesa_frames.abstract.agentset.AbstractAgentSet + mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] ), ) -> Self: - """Add agents to the AbstractAgentSetRegistry through the += operator. + """Add AgentSets to the registry through the += operator. Parameters ---------- - other : DataFrame | DataFrameInput | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to add. + other : mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] + The AgentSets to add. Returns ------- Self The updated AbstractAgentSetRegistry. """ - return self.add(agents=other, inplace=True) + return self.add(sets=other, inplace=True) def __isub__( self, other: ( - IdsLike - | AgentMask - | mesa_frames.abstract.agentset.AbstractAgentSet + mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] ), ) -> Self: - """Remove agents from the AbstractAgentSetRegistry through the -= operator. + """Remove AgentSets from the registry through the -= operator. Parameters ---------- - other : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to remove. + other : mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] + The AgentSets to remove. Returns ------- @@ -544,142 +461,65 @@ def __isub__( def __sub__( self, other: ( - IdsLike - | AgentMask - | mesa_frames.abstract.agentset.AbstractAgentSet + mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] ), ) -> Self: - """Remove agents from a new AbstractAgentSetRegistry through the - operator. + """Remove AgentSets from a new registry through the - operator. Parameters ---------- - other : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to remove. + other : mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] + The AgentSets to remove. Returns ------- Self - A new AbstractAgentSetRegistry with the removed agents. + A new AbstractAgentSetRegistry with the removed AgentSets. """ return self.discard(other, inplace=False) def __setitem__( self, - key: ( - str - | Collection[str] - | AgentMask - | tuple[AgentMask, str | Collection[str]] - | tuple[ - dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str - ] - | tuple[ - dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], - Collection[str], - ] - ), - values: Any, + key: int | str, + value: mesa_frames.abstract.agentset.AbstractAgentSet, ) -> None: - """Implement the [] operator for setting values in the AbstractAgentSetRegistry. + """Assign/replace a single AgentSet at an index or name. - The key can be: - - A string (eg. AbstractAgentSetRegistry["str"]): sets the specified column of the agents in the AbstractAgentSetRegistry. - - A list of strings(eg. AbstractAgentSetRegistry[["str1", "str2"]]): sets the specified columns of the agents in the AbstractAgentSetRegistry. - - A tuple (eg. AbstractAgentSetRegistry[AgentMask, "str"]): sets the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. - - A AgentMask (eg. AbstractAgentSetRegistry[AgentMask]): sets the attributes of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. - - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, "str"]): sets the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. - - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, Collection[str]]): sets the specified columns of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. - - Parameters - ---------- - key : str | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] - The key to set. - values : Any - The values to set for the specified key. + Mirrors the invariants of ``replace`` for single-key assignment: + - Names remain unique across the registry + - ``value.model is self.model`` + - For name keys, the key is authoritative for the assigned set's name + - For index keys, collisions on a different entry's name must raise """ - # TODO: fix types as in __getitem__ - if isinstance(key, tuple): - self.set(mask=key[0], attr_names=key[1], values=values) - else: - if isinstance(key, str) or ( - isinstance(key, Collection) and all(isinstance(k, str) for k in key) - ): - try: - self.set(attr_names=key, values=values) - except KeyError: # key=AgentMask - self.set(attr_names=None, mask=key, values=values) - else: - self.set(attr_names=None, mask=key, values=values) @abstractmethod def __getattr__(self, name: str) -> Any | dict[str, Any]: - """Fallback for retrieving attributes of the AbstractAgentSetRegistry. Retrieve an attribute of the underlying DataFrame(s). - - Parameters - ---------- - name : str - The name of the attribute to retrieve. - - Returns - ------- - Any | dict[str, Any] - The attribute value - """ + """Fallback for retrieving attributes of the AgentSetRegistry.""" @abstractmethod - def __iter__(self) -> Iterator[dict[str, Any]]: - """Iterate over the agents in the AbstractAgentSetRegistry. - - Returns - ------- - Iterator[dict[str, Any]] - An iterator over the agents. - """ + def __iter__(self) -> Iterator[mesa_frames.abstract.agentset.AbstractAgentSet]: + """Iterate over AgentSets in the registry.""" ... @abstractmethod def __len__(self) -> int: - """Get the number of agents in the AbstractAgentSetRegistry. - - Returns - ------- - int - The number of agents in the AbstractAgentSetRegistry. - """ + """Get the number of AgentSets in the registry.""" ... @abstractmethod def __repr__(self) -> str: - """Get a string representation of the DataFrame in the AbstractAgentSetRegistry. - - Returns - ------- - str - A string representation of the DataFrame in the AbstractAgentSetRegistry. - """ + """Get a string representation of the AgentSets in the registry.""" pass @abstractmethod def __reversed__(self) -> Iterator: - """Iterate over the agents in the AbstractAgentSetRegistry in reverse order. - - Returns - ------- - Iterator - An iterator over the agents in reverse order. - """ + """Iterate over AgentSets in reverse order.""" ... @abstractmethod def __str__(self) -> str: - """Get a string representation of the agents in the AbstractAgentSetRegistry. - - Returns - ------- - str - A string representation of the agents in the AbstractAgentSetRegistry. - """ + """Get a string representation of the AgentSets in the registry.""" ... @property diff --git a/mesa_frames/types_.py b/mesa_frames/types_.py index 34d5996e..86afbe2f 100644 --- a/mesa_frames/types_.py +++ b/mesa_frames/types_.py @@ -3,7 +3,7 @@ from __future__ import annotations from collections.abc import Collection, Sequence from datetime import date, datetime, time, timedelta -from typing import Literal, Annotated, Union, Any +from typing import Literal, Annotated, Union, Any, TYPE_CHECKING from collections.abc import Mapping from beartype.vale import IsEqual import math @@ -86,6 +86,16 @@ # Common option types KeyBy = Literal["name", "index", "type"] +# Selector for choosing AgentSets at the registry level +if TYPE_CHECKING: + from mesa_frames.abstract.agentset import AbstractAgentSet as _AAS + + AgentSetSelector = ( + _AAS | type[_AAS] | str | Collection[_AAS | type[_AAS] | str] | None + ) +else: + AgentSetSelector = Any # runtime fallback to avoid import cycles + ###----- Time ------### TimeT = float | int From 9ced3308c248555cacc692ab1968be0e3290dfe6 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 15:17:59 +0200 Subject: [PATCH 105/329] Refactor type aliases in types_.py: reorganize imports, enhance AgentSetSelector definitions, and add __all__ for better module export --- mesa_frames/types_.py | 45 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/mesa_frames/types_.py b/mesa_frames/types_.py index 86afbe2f..5873e034 100644 --- a/mesa_frames/types_.py +++ b/mesa_frames/types_.py @@ -1,15 +1,17 @@ """Type aliases for the mesa_frames package.""" from __future__ import annotations -from collections.abc import Collection, Sequence -from datetime import date, datetime, time, timedelta -from typing import Literal, Annotated, Union, Any, TYPE_CHECKING -from collections.abc import Mapping -from beartype.vale import IsEqual + import math +from collections.abc import Collection, Mapping, Sequence +from datetime import date, datetime, time, timedelta +from typing import TYPE_CHECKING, Annotated, Any, Literal, Union + +import numpy as np import polars as pl +from beartype.vale import IsEqual from numpy import ndarray -import numpy as np + # import geopolars as gpl # TODO: Uncomment when geopolars is available ###----- Optional Types -----### @@ -86,16 +88,43 @@ # Common option types KeyBy = Literal["name", "index", "type"] -# Selector for choosing AgentSets at the registry level +# Selectors for choosing AgentSets at the registry level +# Abstract (for abstract layer APIs) if TYPE_CHECKING: from mesa_frames.abstract.agentset import AbstractAgentSet as _AAS - AgentSetSelector = ( + AbstractAgentSetSelector = ( _AAS | type[_AAS] | str | Collection[_AAS | type[_AAS] | str] | None ) +else: + AbstractAgentSetSelector = Any # runtime fallback to avoid import cycles + +# Concrete (for concrete layer APIs) +if TYPE_CHECKING: + from mesa_frames.concrete.agentset import AgentSet as _CAS + + AgentSetSelector = ( + _CAS | type[_CAS] | str | Collection[_CAS | type[_CAS] | str] | None + ) else: AgentSetSelector = Any # runtime fallback to avoid import cycles +__all__ = [ + # common + "DataFrame", + "Series", + "Index", + "BoolSeries", + "Mask", + "AgentMask", + "IdsLike", + "ArrayLike", + "KeyBy", + # selectors + "AbstractAgentSetSelector", + "AgentSetSelector", +] + ###----- Time ------### TimeT = float | int From 5b79c35fb7334b54a46f4ffdc89429f5755a96b0 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 15:35:18 +0200 Subject: [PATCH 106/329] Refactor import statement in agentsetregistry.py: rename AbstractAgentSetSelector to AgentSetSelector for clarity --- mesa_frames/abstract/agentsetregistry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index 2fdc3c28..03a277d6 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -54,7 +54,7 @@ def __init__(self, model): BoolSeries, Index, KeyBy, - AgentSetSelector, + AbstractAgentSetSelector as AgentSetSelector, Series, ) From 6baec28d98bf4dcaed051d962a4c2600a3f5a613 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:03:09 +0200 Subject: [PATCH 107/329] Refactor AgentSetRegistry: streamline imports, rename parameters for clarity, and enhance type annotations --- mesa_frames/concrete/agentsetregistry.py | 363 ++++++++--------------- 1 file changed, 120 insertions(+), 243 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index b85b72bc..7fcda742 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -46,26 +46,16 @@ def step(self): from __future__ import annotations # For forward references -from collections import defaultdict -from collections.abc import Callable, Collection, Iterable, Iterator, Sequence -from typing import Any, Literal, Self, cast, overload +from collections.abc import Collection, Iterable, Iterator, Sequence +from typing import Any, Literal, Self, overload, cast -import numpy as np import polars as pl from mesa_frames.abstract.agentsetregistry import ( AbstractAgentSetRegistry, ) from mesa_frames.concrete.agentset import AgentSet -from mesa_frames.types_ import ( - AgentMask, - AgnosticAgentMask, - BoolSeries, - DataFrame, - IdsLike, - Index, - Series, -) +from mesa_frames.types_ import BoolSeries, KeyBy, AgentSetSelector class AgentSetRegistry(AbstractAgentSetRegistry): @@ -88,30 +78,11 @@ def __init__(self, model: mesa_frames.concrete.model.Model) -> None: def add( self, - agents: AgentSet | Iterable[AgentSet], + sets: AgentSet | Iterable[AgentSet], inplace: bool = True, ) -> Self: - """Add an AgentSet to the AgentSetRegistry. - - Parameters - ---------- - agents : AgentSet | Iterable[AgentSet] - The AgentSets to add. - inplace : bool, optional - Whether to add the AgentSets in place. Defaults to True. - - Returns - ------- - Self - The updated AgentSetRegistry. - - Raises - ------ - ValueError - If any AgentSets are already present or if IDs are not unique. - """ obj = self._get_obj(inplace) - other_list = obj._return_agentsets_list(agents) + other_list = obj._return_agentsets_list(sets) if obj._check_agentsets_presence(other_list).any(): raise ValueError( "Some agentsets are already present in the AgentSetRegistry." @@ -132,13 +103,22 @@ def add( return obj @overload - def contains(self, agents: int | AgentSet) -> bool: ... + def contains(self, sets: AgentSet | type[AgentSet] | str) -> bool: ... @overload - def contains(self, agents: IdsLike | Iterable[AgentSet]) -> pl.Series: ... + def contains( + self, + sets: Iterable[AgentSet] | Iterable[type[AgentSet]] | Iterable[str], + ) -> pl.Series: ... def contains( - self, agents: IdsLike | AgentSet | Iterable[AgentSet] + self, + sets: AgentSet + | type[AgentSet] + | str + | Iterable[AgentSet] + | Iterable[type[AgentSet]] + | Iterable[str], ) -> bool | pl.Series: if isinstance(agents, int): return agents in self._ids @@ -159,32 +139,35 @@ def contains( def do( self, method_name: str, - *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, + *args: Any, + sets: AgentSetSelector | None = None, return_results: Literal[False] = False, inplace: bool = True, - **kwargs, + key_by: KeyBy = "name", + **kwargs: Any, ) -> Self: ... @overload def do( self, method_name: str, - *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, + *args: Any, + sets: AgentSetSelector, return_results: Literal[True], inplace: bool = True, - **kwargs, - ) -> dict[AgentSet, Any]: ... + key_by: KeyBy = "name", + **kwargs: Any, + ) -> dict[str, Any] | dict[int, Any] | dict[type[AgentSet], Any]: ... def do( self, method_name: str, - *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, + *args: Any, + sets: AgentSetSelector = None, return_results: bool = False, inplace: bool = True, - **kwargs, + key_by: KeyBy = "name", + **kwargs: Any, ) -> Self | Any: obj = self._get_obj(inplace) agentsets_masks = obj._get_bool_masks(mask) @@ -214,8 +197,27 @@ def do( ] return obj + @overload + def get(self, key: int, default: None = ...) -> AgentSet | None: ... + + @overload + def get(self, key: str, default: None = ...) -> AgentSet | None: ... + + @overload + def get(self, key: type[AgentSet], default: None = ...) -> list[AgentSet]: ... + + @overload + def get( + self, + key: int | str | type[AgentSet], + default: AgentSet | list[AgentSet] | None, + ) -> AgentSet | list[AgentSet] | None: ... + def get( self, + key: int | str | type[AgentSet], + default: AgentSet | list[AgentSet] | None = None, + ) -> AgentSet | list[AgentSet] | None: attr_names: str | Collection[str] | None = None, mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, ) -> dict[AgentSet, Series] | dict[AgentSet, DataFrame]: @@ -245,7 +247,7 @@ def get( def remove( self, - agents: AgentSet | Iterable[AgentSet] | IdsLike, + sets: AgentSetSelector, inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) @@ -340,6 +342,7 @@ def set( return obj def shuffle(self, inplace: bool = True) -> Self: + def shuffle(self, inplace: bool = False) -> Self: obj = self._get_obj(inplace) obj._agentsets = [agentset.shuffle(inplace=True) for agentset in obj._agentsets] return obj @@ -349,7 +352,7 @@ def sort( by: str | Sequence[str], ascending: bool | Sequence[bool] = True, inplace: bool = True, - **kwargs, + **kwargs: Any, ) -> Self: obj = self._get_obj(inplace) obj._agentsets = [ @@ -358,23 +361,6 @@ def sort( ] return obj - def step(self, inplace: bool = True) -> Self: - """Advance the state of the agents in the AgentSetRegistry by one step. - - Parameters - ---------- - inplace : bool, optional - Whether to update the AgentSetRegistry in place, by default True - - Returns - ------- - Self - """ - obj = self._get_obj(inplace) - for agentset in obj._agentsets: - agentset.step() - return obj - def _check_ids_presence(self, other: list[AgentSet]) -> pl.DataFrame: """Check if the IDs of the agents to be added are unique. @@ -458,54 +444,6 @@ def _return_agentsets_list( """ return [agentsets] if isinstance(agentsets, AgentSet) else list(agentsets) - def __add__(self, other: AgentSet | Iterable[AgentSet]) -> Self: - """Add AgentSets to a new AgentSetRegistry through the + operator. - - Parameters - ---------- - other : AgentSet | Iterable[AgentSet] - The AgentSets to add. - - Returns - ------- - Self - A new AgentSetRegistry with the added AgentSets. - """ - return super().__add__(other) - - def keys(self) -> Iterator[str]: - """Return an iterator over the names of the agent sets.""" - for agentset in self._agentsets: - if agentset.name is not None: - yield agentset.name - - def names(self) -> list[str]: - """Return a list of the names of the agent sets.""" - return list(self.keys()) - - def items(self) -> Iterator[tuple[str, AbstractAgentSet]]: - """Return an iterator over (name, agentset) pairs.""" - for agentset in self._agentsets: - if agentset.name is not None: - yield agentset.name, agentset - - def __contains__(self, name: object) -> bool: - """Check if a name is in the registry.""" - if not isinstance(name, str): - return False - return name in [ - agentset.name for agentset in self._agentsets if agentset.name is not None - ] - - def __getitem__(self, key: str) -> AbstractAgentSet: - """Get an agent set by name.""" - if isinstance(key, str): - for agentset in self._agentsets: - if agentset.name == key: - return agentset - raise KeyError(f"Agent set '{key}' not found") - return super().__getitem__(key) - def _generate_name(self, base_name: str) -> str: """Generate a unique name for an agent set.""" existing_names = [ @@ -520,150 +458,89 @@ def _generate_name(self, base_name: str) -> str: candidate = f"{base_name}_{counter}" return candidate - def __getattr__(self, name: str) -> dict[AbstractAgentSet, Any]: - # Handle special mapping methods - if name in ("keys", "items", "values"): - raise AttributeError( - f"'{self.__class__.__name__}' object has no attribute '{name}'" - ) - # Avoid delegating container-level attributes to agentsets - if name in ("df", "active_agents", "inactive_agents", "index", "pos"): + def __getattr__(self, name: str) -> Any | dict[str, Any]: + # Avoids infinite recursion of private attributes + if name.startswith("_"): raise AttributeError( f"'{self.__class__.__name__}' object has no attribute '{name}'" ) - # Avoids infinite recursion of private attributes - if __debug__: # Only execute in non-optimized mode - if name.startswith("_"): - raise AttributeError( - f"'{self.__class__.__name__}' object has no attribute '{name}'" - ) - return {agentset: getattr(agentset, name) for agentset in self._agentsets} - - @overload - def __getitem__( - self, key: str | tuple[dict[AgentSet, AgentMask], str] - ) -> dict[AgentSet, Series | pl.Expr]: ... - - @overload - def __getitem__( - self, - key: ( - Collection[str] - | AgnosticAgentMask - | IdsLike - | tuple[dict[AgentSet, AgentMask], Collection[str]] - ), - ) -> dict[AgentSet, DataFrame]: ... - - def __getitem__( - self, - key: ( - str - | Collection[str] - | AgnosticAgentMask - | IdsLike - | tuple[dict[AgentSet, AgentMask], str] - | tuple[dict[AgentSet, AgentMask], Collection[str]] - ), - ) -> dict[AgentSet, Series | pl.Expr] | dict[AgentSet, DataFrame]: - return super().__getitem__(key) - - def __iadd__(self, agents: AgentSet | Iterable[AgentSet]) -> Self: - """Add AgentSets to the AgentSetRegistry through the += operator. - - Parameters - ---------- - agents : AgentSet | Iterable[AgentSet] - The AgentSets to add. - - Returns - ------- - Self - The updated AgentSetRegistry. - """ - return super().__iadd__(agents) - - def __iter__(self) -> Iterator[dict[str, Any]]: - return (agent for agentset in self._agentsets for agent in iter(agentset)) - - def __isub__(self, agents: AgentSet | Iterable[AgentSet] | IdsLike) -> Self: - """Remove AgentSets from the AgentSetRegistry through the -= operator. + # Delegate attribute access to sets; map results by set name + return {cast(str, s.name): getattr(s, name) for s in self._agentsets} - Parameters - ---------- - agents : AgentSet | Iterable[AgentSet] | IdsLike - The AgentSets or agent IDs to remove. - - Returns - ------- - Self - The updated AgentSetRegistry. - """ - return super().__isub__(agents) + def __iter__(self) -> Iterator[AgentSet]: + return iter(self._agentsets) def __len__(self) -> int: - return sum(len(agentset._df) for agentset in self._agentsets) + return len(self._agentsets) def __repr__(self) -> str: return "\n".join([repr(agentset) for agentset in self._agentsets]) - def __reversed__(self) -> Iterator: - return ( - agent - for agentset in self._agentsets - for agent in reversed(agentset._backend) - ) + def __reversed__(self) -> Iterator[AgentSet]: + return reversed(self._agentsets) - def __setitem__( - self, - key: ( - str - | Collection[str] - | AgnosticAgentMask - | IdsLike - | tuple[dict[AgentSet, AgentMask], str] - | tuple[dict[AgentSet, AgentMask], Collection[str]] - ), - values: Any, - ) -> None: - super().__setitem__(key, values) + def __setitem__(self, key: int | str, value: AgentSet) -> None: + """Assign/replace a single AgentSet at an index or name. - def __str__(self) -> str: - return "\n".join([str(agentset) for agentset in self._agentsets]) - - def __sub__(self, agents: AgentSet | Iterable[AgentSet] | IdsLike) -> Self: - """Remove AgentSets from a new AgentSetRegistry through the - operator. - - Parameters - ---------- - agents : AgentSet | Iterable[AgentSet] | IdsLike - The AgentSets or agent IDs to remove. Supports NumPy integer types. - - Returns - ------- - Self - A new AgentSetRegistry with the removed AgentSets. + Enforces name uniqueness and model consistency. """ - return super().__sub__(agents) + if value.model is not self.model: + raise TypeError("Assigned AgentSet must belong to the same model") + if isinstance(key, int): + if value.name is not None: + for i, s in enumerate(self._agentsets): + if i != key and s.name == value.name: + raise ValueError( + f"Duplicate agent set name disallowed: {value.name}" + ) + self._agentsets[key] = value + elif isinstance(key, str): + try: + value.rename(key) + except Exception: + if hasattr(value, "_name"): + setattr(value, "_name", key) + idx = None + for i, s in enumerate(self._agentsets): + if s.name == key: + idx = i + break + if idx is None: + self._agentsets.append(value) + else: + self._agentsets[idx] = value + else: + raise TypeError("Key must be int index or str name") + # Recompute ids cache + if self._agentsets: + self._ids = pl.concat( + [pl.Series(name="unique_id", dtype=pl.UInt64)] + + [pl.Series(s["unique_id"]) for s in self._agentsets] + ) + else: + self._ids = pl.Series(name="unique_id", dtype=pl.UInt64) - @property - def agentsets_by_type(self) -> dict[type[AbstractAgentSet], Self]: - """Get the agent sets in the AgentSetRegistry grouped by type. + def __str__(self) -> str: + return "\n".join([str(agentset) for agentset in self._agentsets]) - Returns - ------- - dict[type[AgentSet], Self] - A dictionary mapping agent set types to the corresponding AgentSetRegistry. - """ + @overload + def __getitem__(self, key: int) -> AgentSet: ... - def copy_without_agentsets() -> Self: - return self.copy(deep=False, skip=["_agentsets"]) + @overload + def __getitem__(self, key: str) -> AgentSet: ... - dictionary = defaultdict(copy_without_agentsets) + @overload + def __getitem__(self, key: type[AgentSet]) -> list[AgentSet]: ... - for agentset in self._agentsets: - agents_df = dictionary[agentset.__class__] - agents_df._agentsets = [] - agents_df._agentsets = agents_df._agentsets + [agentset] - dictionary[agentset.__class__] = agents_df - return dictionary + def __getitem__(self, key: int | str | type[AgentSet]) -> AgentSet | list[AgentSet]: + """Retrieve AgentSet(s) by index, name, or type.""" + if isinstance(key, int): + return self._agentsets[key] + if isinstance(key, str): + for s in self._agentsets: + if s.name == key: + return s + raise KeyError(f"Agent set '{key}' not found") + if isinstance(key, type) and issubclass(key, AgentSet): + return [s for s in self._agentsets if isinstance(s, key)] + raise TypeError("Key must be int, str (name), or AgentSet type") From 7f78887595073fe5fe45369de29ee02cc1edbf8b Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:16:14 +0200 Subject: [PATCH 108/329] Refactor AbstractAgentSetRegistry: add abstract methods keys, items, and values for improved agent set iteration --- mesa_frames/abstract/agentsetregistry.py | 26 +++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index 03a277d6..a5a6e6bd 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -43,7 +43,7 @@ def __init__(self, model): from __future__ import annotations # PEP 563: postponed evaluation of type annotations from abc import abstractmethod -from collections.abc import Callable, Collection, Iterator, Sequence +from collections.abc import Callable, Collection, Iterator, Sequence, Iterable from contextlib import suppress from typing import Any, Literal, Self, overload @@ -522,6 +522,30 @@ def __str__(self) -> str: """Get a string representation of the AgentSets in the registry.""" ... + @abstractmethod + def keys( + self, *, key_by: KeyBy = "name" + ) -> Iterable[str | int | type[mesa_frames.abstract.agentset.AbstractAgentSet]]: + """Iterate keys for contained AgentSets (by name|index|type).""" + ... + + @abstractmethod + def items( + self, *, key_by: KeyBy = "name" + ) -> Iterable[ + tuple[ + str | int | type[mesa_frames.abstract.agentset.AbstractAgentSet], + mesa_frames.abstract.agentset.AbstractAgentSet, + ] + ]: + """Iterate (key, AgentSet) pairs for contained sets.""" + ... + + @abstractmethod + def values(self) -> Iterable[mesa_frames.abstract.agentset.AbstractAgentSet]: + """Iterate contained AgentSets (values view).""" + ... + @property def model(self) -> mesa_frames.concrete.model.Model: """The model that the AbstractAgentSetRegistry belongs to. From 37d892283455bd05c3d81e04b8d5a94f7183a109 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:17:13 +0200 Subject: [PATCH 109/329] Refactor AgentSetRegistry: add keys, items, and values methods for enhanced agent set iteration --- mesa_frames/concrete/agentsetregistry.py | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 7fcda742..b6628f4c 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -523,6 +523,41 @@ def __setitem__(self, key: int | str, value: AgentSet) -> None: def __str__(self) -> str: return "\n".join([str(agentset) for agentset in self._agentsets]) + def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: + if key_by not in ("name", "index", "type"): + raise ValueError("key_by must be 'name'|'index'|'type'") + if key_by == "index": + for i in range(len(self._agentsets)): + yield i + return + if key_by == "type": + for s in self._agentsets: + yield type(s) + return + # name + for s in self._agentsets: + if s.name is not None: + yield s.name + + def items(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSet]]: + if key_by not in ("name", "index", "type"): + raise ValueError("key_by must be 'name'|'index'|'type'") + if key_by == "index": + for i, s in enumerate(self._agentsets): + yield i, s + return + if key_by == "type": + for s in self._agentsets: + yield type(s), s + return + # name + for s in self._agentsets: + if s.name is not None: + yield s.name, s + + def values(self) -> Iterable[AgentSet]: + return iter(self._agentsets) + @overload def __getitem__(self, key: int) -> AgentSet: ... From 33aa5365b77fb00e8be9bbe3baf11ea9fc1506c1 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:23:06 +0200 Subject: [PATCH 110/329] Refactor contains method in AgentSetRegistry: optimize type checks and improve handling of single values and iterables --- mesa_frames/concrete/agentsetregistry.py | 51 +++++++++++++++++------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index b6628f4c..e1223ed7 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -120,20 +120,43 @@ def contains( | Iterable[type[AgentSet]] | Iterable[str], ) -> bool | pl.Series: - if isinstance(agents, int): - return agents in self._ids - elif isinstance(agents, AgentSet): - return self._check_agentsets_presence([agents]).any() - elif isinstance(agents, Iterable): - if len(agents) == 0: - return True - elif isinstance(next(iter(agents)), AgentSet): - agents = cast(Iterable[AgentSet], agents) - return self._check_agentsets_presence(list(agents)) - else: # IdsLike - agents = cast(IdsLike, agents) - - return pl.Series(agents, dtype=pl.UInt64).is_in(self._ids) + # Single value fast paths + if isinstance(sets, AgentSet): + return self._check_agentsets_presence([sets]).any() + if isinstance(sets, type) and issubclass(sets, AgentSet): + return any(isinstance(s, sets) for s in self._agentsets) + if isinstance(sets, str): + return any(s.name == sets for s in self._agentsets) + + # Iterable paths without materializing unnecessarily + + if isinstance(sets, Sized) and len(sets) == 0: # type: ignore[arg-type] + return True + it = iter(sets) # type: ignore[arg-type] + try: + first = next(it) + except StopIteration: + return True + + if isinstance(first, AgentSet): + lst = [first, *it] + return self._check_agentsets_presence(lst) + + if isinstance(first, type) and issubclass(first, AgentSet): + present_types = {type(s) for s in self._agentsets} + + def has_type(t: type[AgentSet]) -> bool: + return any(issubclass(pt, t) for pt in present_types) + + return pl.Series( + (has_type(t) for t in chain([first], it)), dtype=pl.Boolean + ) + + if isinstance(first, str): + names = {s.name for s in self._agentsets if s.name is not None} + return pl.Series((x in names for x in chain([first], it)), dtype=pl.Boolean) + + raise TypeError("Unsupported type for contains()") @overload def do( From e641f123b6b27dfdf21c411300e08165f305372a Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:23:23 +0200 Subject: [PATCH 111/329] Refactor AgentSetRegistry: streamline method for resolving agent sets and improve key generation logic --- mesa_frames/concrete/agentsetregistry.py | 41 +++++++++++------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index e1223ed7..2df0b5e5 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -193,32 +193,29 @@ def do( **kwargs: Any, ) -> Self | Any: obj = self._get_obj(inplace) - agentsets_masks = obj._get_bool_masks(mask) + target_sets = obj._resolve_selector(sets) if return_results: + + def make_key(i: int, s: AgentSet) -> Any: + if key_by == "name": + return s.name + if key_by == "index": + return i + if key_by == "type": + return type(s) + return s # backward-compatible: key by object + return { - agentset: agentset.do( - method_name, - *args, - mask=mask, - return_results=return_results, - **kwargs, - inplace=inplace, + make_key(i, s): s.do( + method_name, *args, return_results=True, inplace=inplace, **kwargs ) - for agentset, mask in agentsets_masks.items() + for i, s in enumerate(target_sets) } - else: - obj._agentsets = [ - agentset.do( - method_name, - *args, - mask=mask, - return_results=return_results, - **kwargs, - inplace=inplace, - ) - for agentset, mask in agentsets_masks.items() - ] - return obj + obj._agentsets = [ + s.do(method_name, *args, return_results=False, inplace=inplace, **kwargs) + for s in target_sets + ] + return obj @overload def get(self, key: int, default: None = ...) -> AgentSet | None: ... From f847a57866d943e04a9fefe5a142abdcf2b7abba Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:23:59 +0200 Subject: [PATCH 112/329] Refactor AgentSetRegistry: simplify key retrieval logic and enhance error handling in the get method --- mesa_frames/concrete/agentsetregistry.py | 39 ++++++++---------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 2df0b5e5..7084ca60 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -238,32 +238,19 @@ def get( key: int | str | type[AgentSet], default: AgentSet | list[AgentSet] | None = None, ) -> AgentSet | list[AgentSet] | None: - attr_names: str | Collection[str] | None = None, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, - ) -> dict[AgentSet, Series] | dict[AgentSet, DataFrame]: - agentsets_masks = self._get_bool_masks(mask) - result = {} - - # Convert attr_names to list for consistent checking - if attr_names is None: - # None means get all data - no column filtering needed - required_columns = [] - elif isinstance(attr_names, str): - required_columns = [attr_names] - else: - required_columns = list(attr_names) - - for agentset, mask in agentsets_masks.items(): - # Fast column existence check - no data processing, just property access - agentset_columns = agentset.df.columns - - # Check if all required columns exist in this agent set - if not required_columns or all( - col in agentset_columns for col in required_columns - ): - result[agentset] = agentset.get(attr_names, mask) - - return result + try: + if isinstance(key, int): + return self._agentsets[key] + if isinstance(key, str): + for s in self._agentsets: + if s.name == key: + return s + return default + if isinstance(key, type) and issubclass(key, AgentSet): + return [s for s in self._agentsets if isinstance(s, key)] + except (IndexError, KeyError, TypeError): + return default + return default def remove( self, From 7588966367067fb71c6f8d863e61e501b05b51d0 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:28:29 +0200 Subject: [PATCH 113/329] Refactor AgentSetRegistry: implement _resolve_selector method for improved agent set selection and deduplication --- mesa_frames/concrete/agentsetregistry.py | 44 ++++++++++++++++++------ 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 7084ca60..91e7971b 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -424,17 +424,39 @@ def _check_agentsets_presence(self, other: list[AgentSet]) -> pl.Series: [agentset in other_set for agentset in self._agentsets], dtype=pl.Boolean ) - def _get_bool_masks( - self, - mask: (AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask]) = None, - ) -> dict[AgentSet, BoolSeries]: - return_dictionary = {} - if not isinstance(mask, dict): - # No need to convert numpy integers - let polars handle them directly - mask = {agentset: mask for agentset in self._agentsets} - for agentset, mask_value in mask.items(): - return_dictionary[agentset] = agentset._get_bool_mask(mask_value) - return return_dictionary + def _resolve_selector(self, selector: AgentSetSelector = None) -> list[AgentSet]: + """Resolve a selector (instance/type/name or collection) to a list of AgentSets.""" + if selector is None: + return list(self._agentsets) + # Single instance + if isinstance(selector, AgentSet): + return [selector] if selector in self._agentsets else [] + # Single type + if isinstance(selector, type) and issubclass(selector, AgentSet): + return [s for s in self._agentsets if isinstance(s, selector)] + # Single name + if isinstance(selector, str): + return [s for s in self._agentsets if s.name == selector] + # Collection of mixed selectors + selected: list[AgentSet] = [] + for item in selector: # type: ignore[assignment] + if isinstance(item, AgentSet): + if item in self._agentsets: + selected.append(item) + elif isinstance(item, type) and issubclass(item, AgentSet): + selected.extend([s for s in self._agentsets if isinstance(s, item)]) + elif isinstance(item, str): + selected.extend([s for s in self._agentsets if s.name == item]) + else: + raise TypeError("Unsupported selector element type") + # Deduplicate while preserving order + seen = set() + result = [] + for s in selected: + if s not in seen: + seen.add(s) + result.append(s) + return result def _return_agentsets_list( self, agentsets: AgentSet | Iterable[AgentSet] From 6b7be9dbec3ecab64e5f3f389869c6669d454dff Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:31:28 +0200 Subject: [PATCH 114/329] Refactor AgentSetRegistry: optimize agent removal logic and normalize selection using _resolve_selector method --- mesa_frames/concrete/agentsetregistry.py | 103 ++++------------------- 1 file changed, 15 insertions(+), 88 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 91e7971b..731b3922 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -48,7 +48,8 @@ def step(self): from collections.abc import Collection, Iterable, Iterator, Sequence from typing import Any, Literal, Self, overload, cast - +from collections.abc import Sized +from itertools import chain import polars as pl from mesa_frames.abstract.agentsetregistry import ( @@ -258,97 +259,23 @@ def remove( inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) - if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): - return obj - if isinstance(agents, AgentSet): - agents = [agents] - if isinstance(agents, Iterable) and isinstance(next(iter(agents)), AgentSet): - # We have to get the index of the original AgentSet because the copy made AgentSets with different hash - ids = [self._agentsets.index(agentset) for agentset in iter(agents)] - ids.sort(reverse=True) - removed_ids = pl.Series(dtype=pl.UInt64) - for id in ids: - removed_ids = pl.concat( - [ - removed_ids, - pl.Series(obj._agentsets[id]["unique_id"], dtype=pl.UInt64), - ] - ) - obj._agentsets.pop(id) - - else: # IDsLike - if isinstance(agents, (int, np.uint64)): - agents = [agents] - elif isinstance(agents, DataFrame): - agents = agents["unique_id"] - removed_ids = pl.Series(agents, dtype=pl.UInt64) - deleted = 0 - - for agentset in obj._agentsets: - initial_len = len(agentset) - agentset._discard(removed_ids) - deleted += initial_len - len(agentset) - if deleted == len(removed_ids): - break - if deleted < len(removed_ids): # TODO: fix type hint - raise KeyError( - "There exist some IDs which are not present in any agentset" - ) - try: - obj.space.remove_agents(removed_ids, inplace=True) - except ValueError: - pass - obj._ids = obj._ids.filter(obj._ids.is_in(removed_ids).not_()) - return obj - - def select( - self, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, - filter_func: Callable[[AgentSet], AgentMask] | None = None, - n: int | None = None, - inplace: bool = True, - negate: bool = False, - ) -> Self: - obj = self._get_obj(inplace) - agentsets_masks = obj._get_bool_masks(mask) - if n is not None: - n = n // len(agentsets_masks) - obj._agentsets = [ - agentset.select( - mask=mask, filter_func=filter_func, n=n, negate=negate, inplace=inplace + # Normalize to a list of AgentSet instances using _resolve_selector + selected = obj._resolve_selector(sets) # type: ignore[arg-type] + # Remove in reverse positional order + indices = [i for i, s in enumerate(obj._agentsets) if s in selected] + indices.sort(reverse=True) + for idx in indices: + obj._agentsets.pop(idx) + # Recompute ids cache + if obj._agentsets: + obj._ids = pl.concat( + [pl.Series(name="unique_id", dtype=pl.UInt64)] + + [pl.Series(s["unique_id"]) for s in obj._agentsets] ) - for agentset, mask in agentsets_masks.items() - ] - return obj - - def set( - self, - attr_names: str | dict[AgentSet, Any] | Collection[str], - values: Any | None = None, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, - inplace: bool = True, - ) -> Self: - obj = self._get_obj(inplace) - agentsets_masks = obj._get_bool_masks(mask) - if isinstance(attr_names, dict): - for agentset, values in attr_names.items(): - if not inplace: - # We have to get the index of the original AgentSet because the copy made AgentSets with different hash - id = self._agentsets.index(agentset) - agentset = obj._agentsets[id] - agentset.set( - attr_names=values, mask=agentsets_masks[agentset], inplace=True - ) else: - obj._agentsets = [ - agentset.set( - attr_names=attr_names, values=values, mask=mask, inplace=True - ) - for agentset, mask in agentsets_masks.items() - ] + obj._ids = pl.Series(name="unique_id", dtype=pl.UInt64) return obj - def shuffle(self, inplace: bool = True) -> Self: def shuffle(self, inplace: bool = False) -> Self: obj = self._get_obj(inplace) obj._agentsets = [agentset.shuffle(inplace=True) for agentset in obj._agentsets] From e45efbe7fc547605917861f681929cc0b67905f3 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:34:26 +0200 Subject: [PATCH 115/329] Refactor AgentSetRegistry: add replace method for bulk updating of agent sets and improve id recomputation logic --- mesa_frames/concrete/agentsetregistry.py | 141 ++++++++++++++++++++--- 1 file changed, 127 insertions(+), 14 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 731b3922..70ef01d7 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -103,6 +103,116 @@ def add( obj._ids = new_ids return obj + def replace( + self, + mapping: (dict[int | str, AgentSet] | list[tuple[int | str, AgentSet]]), + *, + inplace: bool = True, + atomic: bool = True, + ) -> Self: + # Normalize to list of (key, value) + items: list[tuple[int | str, AgentSet]] + if isinstance(mapping, dict): + items = list(mapping.items()) + else: + items = list(mapping) + + obj = self._get_obj(inplace) + + # Helpers (build name->idx map only if needed) + has_str_keys = any(isinstance(k, str) for k, _ in items) + if has_str_keys: + name_to_idx = { + s.name: i for i, s in enumerate(obj._agentsets) if s.name is not None + } + + def _find_index_by_name(name: str) -> int: + try: + return name_to_idx[name] + except KeyError: + raise KeyError(f"Agent set '{name}' not found") + else: + + def _find_index_by_name(name: str) -> int: + for i, s in enumerate(obj._agentsets): + if s.name == name: + return i + + raise KeyError(f"Agent set '{name}' not found") + + if atomic: + n = len(obj._agentsets) + # Map existing object identity -> index (for aliasing checks) + id_to_idx = {id(s): i for i, s in enumerate(obj._agentsets)} + + for k, v in items: + if not isinstance(v, AgentSet): + raise TypeError("Values must be AgentSet instances") + if v.model is not obj.model: + raise TypeError( + "All AgentSets must belong to the same model as the registry" + ) + + v_idx_existing = id_to_idx.get(id(v)) + + if isinstance(k, int): + if not (0 <= k < n): + raise IndexError( + f"Index {k} out of range for AgentSetRegistry of size {n}" + ) + + # Prevent aliasing: the same object cannot appear in two positions + if v_idx_existing is not None and v_idx_existing != k: + raise ValueError( + f"This AgentSet instance already exists at index {v_idx_existing}; cannot also place it at {k}." + ) + + # Preserve name uniqueness when assigning by index + vname = v.name + if vname is not None: + try: + other_idx = _find_index_by_name(vname) + if other_idx != k: + raise ValueError( + f"Duplicate agent set name disallowed: '{vname}' already at index {other_idx}" + ) + except KeyError: + # name not present elsewhere -> OK + pass + + elif isinstance(k, str): + # Locate the slot by name; replacing that slot preserves uniqueness + idx = _find_index_by_name(k) + + # Prevent aliasing: if the same object already exists at a different slot, forbid + if v_idx_existing is not None and v_idx_existing != idx: + raise ValueError( + f"This AgentSet instance already exists at index {v_idx_existing}; cannot also place it at {idx}." + ) + + else: + raise TypeError("Keys must be int indices or str names") + + # Apply + target = obj if inplace else obj.copy(deep=False) + if not inplace: + target._agentsets = list(obj._agentsets) + + for k, v in items: + if isinstance(k, int): + target._agentsets[k] = v # keep v.name as-is (validated above) + else: + idx = _find_index_by_name(k) + # Force the authoritative name without triggering external uniqueness checks + if hasattr(v, "_name"): + v._name = k # type: ignore[attr-defined] + target._agentsets[idx] = v + + # Recompute ids cache + target._recompute_ids() + + return target + @overload def contains(self, sets: AgentSet | type[AgentSet] | str) -> bool: ... @@ -267,13 +377,7 @@ def remove( for idx in indices: obj._agentsets.pop(idx) # Recompute ids cache - if obj._agentsets: - obj._ids = pl.concat( - [pl.Series(name="unique_id", dtype=pl.UInt64)] - + [pl.Series(s["unique_id"]) for s in obj._agentsets] - ) - else: - obj._ids = pl.Series(name="unique_id", dtype=pl.UInt64) + obj._recompute_ids() return obj def shuffle(self, inplace: bool = False) -> Self: @@ -351,6 +455,21 @@ def _check_agentsets_presence(self, other: list[AgentSet]) -> pl.Series: [agentset in other_set for agentset in self._agentsets], dtype=pl.Boolean ) + def _recompute_ids(self) -> None: + """Rebuild the registry-level `unique_id` cache from current AgentSets. + + Ensures `self._ids` stays a `pl.UInt64` Series and empty when no sets. + """ + if self._agentsets: + cols = [pl.Series(s["unique_id"]) for s in self._agentsets] + self._ids = ( + pl.concat(cols) + if cols + else pl.Series(name="unique_id", dtype=pl.UInt64) + ) + else: + self._ids = pl.Series(name="unique_id", dtype=pl.UInt64) + def _resolve_selector(self, selector: AgentSetSelector = None) -> list[AgentSet]: """Resolve a selector (instance/type/name or collection) to a list of AgentSets.""" if selector is None: @@ -468,13 +587,7 @@ def __setitem__(self, key: int | str, value: AgentSet) -> None: else: raise TypeError("Key must be int index or str name") # Recompute ids cache - if self._agentsets: - self._ids = pl.concat( - [pl.Series(name="unique_id", dtype=pl.UInt64)] - + [pl.Series(s["unique_id"]) for s in self._agentsets] - ) - else: - self._ids = pl.Series(name="unique_id", dtype=pl.UInt64) + self._recompute_ids() def __str__(self) -> str: return "\n".join([str(agentset) for agentset in self._agentsets]) From 267e64b9e01554497a3bb00a2a4874ac4874827d Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:35:21 +0200 Subject: [PATCH 116/329] Refactor AgentSetRegistry: simplify index key generation logic using yield from --- mesa_frames/concrete/agentsetregistry.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 70ef01d7..5fbc3ad3 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -596,8 +596,7 @@ def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: if key_by not in ("name", "index", "type"): raise ValueError("key_by must be 'name'|'index'|'type'") if key_by == "index": - for i in range(len(self._agentsets)): - yield i + yield from range(len(self._agentsets)) return if key_by == "type": for s in self._agentsets: From 963f949dc070e0c0102431a7cc4fbf419beaa7df Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 18:38:02 +0200 Subject: [PATCH 117/329] Refactor AbstractAgentSetRegistry: update parameter names and types for clarity and consistency --- mesa_frames/abstract/agentsetregistry.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index a5a6e6bd..c3d0356d 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -106,7 +106,7 @@ def add( Parameters ---------- - agents : mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] + sets : mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The AgentSet(s) to add. inplace : bool Whether to add in place. Defaults to True. @@ -217,7 +217,7 @@ def do( return_results : bool, optional Whether to return per-set results as a dictionary, by default False. inplace : bool, optional - Whether the operation should be done inplace, by default False + Whether the operation should be done inplace, by default True key_by : KeyBy, optional Key domain for the returned mapping when ``return_results`` is True. - "name" (default) → keys are set names (str) @@ -228,7 +228,7 @@ def do( Returns ------- - Self | Any | dict[str, Any] | dict[int, Any] | dict[type[AbstractAgentSet], Any] + Self | Any | dict[str, Any] | dict[int, Any] | dict[type[mesa_frames.abstract.agentset.AbstractAgentSet], Any] The updated registry, or the method result(s). When ``return_results`` is True, returns a dictionary keyed per ``key_by``. """ @@ -321,7 +321,7 @@ def replace( Parameters ---------- - mapping : dict[int | str, AbstractAgentSet] | list[tuple[int | str, AbstractAgentSet]] + mapping : dict[int | str, mesa_frames.abstract.agentset.AbstractAgentSet] | list[tuple[int | str, mesa_frames.abstract.agentset.AbstractAgentSet]] Keys are indices or names to assign; values are AgentSets bound to the same model. inplace : bool, optional Whether to apply on this registry or return a copy, by default True. From 073b6dbfe1989955cfcf1ff91192ecb319d2a6df Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 20:09:13 +0200 Subject: [PATCH 118/329] Refactor AgentSet: update model parameter type for improved clarity --- mesa_frames/concrete/agentset.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 7b91a0c8..2dcdec85 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -82,7 +82,9 @@ class AgentSet(AbstractAgentSet, PolarsMixin): _copy_only_reference: list[str] = ["_model", "_mask"] _mask: pl.Expr | pl.Series - def __init__(self, model: Model, name: str | None = None) -> None: + def __init__( + self, model: mesa_frames.concrete.model.Model, name: str | None = None + ) -> None: """Initialize a new AgentSet. Parameters From a8b615e907d3430638e261b20056eee7d1e82127 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 12 Sep 2025 20:09:36 +0200 Subject: [PATCH 119/329] Refactor get_unique_ids: update implementation for clarity and correctness --- tests/test_grid.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_grid.py b/tests/test_grid.py index 231f929e..904efdb0 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -12,10 +12,8 @@ def get_unique_ids(model: Model) -> pl.Series: - # return model.get_sets_of_type(model.set_types[0])["unique_id"] - series_list = [ - series.cast(pl.UInt64) for series in model.sets.get("unique_id").values() - ] + # Collect unique_id across all concrete AgentSets in the registry + series_list = [aset["unique_id"].cast(pl.UInt64) for aset in model.sets] return pl.concat(series_list) From 26eaefc4fd8054c84a15ea4d96047657a747259d Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 14 Sep 2025 11:27:01 +0200 Subject: [PATCH 120/329] Refactor AgentSet: enhance agent removal logic with validation for unique_ids --- mesa_frames/concrete/agentset.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 2dcdec85..f62d608f 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -296,12 +296,14 @@ def remove(self, agents: PolarsIdsLike | AgentMask, inplace: bool = True) -> Sel agents = self.active_agents if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): return self._get_obj(inplace) - agents = self._df_index(self._get_masked_df(agents), "unique_id") - sets = self.model.sets.remove(agents, inplace=inplace) - for agentset in sets.df.keys(): - if isinstance(agentset, self.__class__): - return agentset - return self + obj = self._get_obj(inplace) + # Normalize to Series of unique_ids + ids = obj._df_index(obj._get_masked_df(agents), "unique_id") + # Validate presence + if not ids.is_in(obj._df["unique_id"]).all(): + raise KeyError("Some 'unique_id' of mask are not present in this AgentSet.") + # Remove by ids + return obj._discard(ids) def set( self, From 8afca27efa06ef61c95377917b06dc7b3ac06439 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 14 Sep 2025 11:39:04 +0200 Subject: [PATCH 121/329] Refactor AgentSetRegistry: improve agent set name assignment logic for uniqueness --- mesa_frames/concrete/agentsetregistry.py | 25 ++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 5fbc3ad3..91a5cc54 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -88,12 +88,22 @@ def add( raise ValueError( "Some agentsets are already present in the AgentSetRegistry." ) + # Ensure unique names across existing and to-be-added sets + existing_names = {s.name for s in obj._agentsets} for agentset in other_list: - # Set name if not already set, using class name - if agentset.name is None: - base_name = agentset.__class__.__name__ - name = obj._generate_name(base_name) + base_name = agentset.name or agentset.__class__.__name__ + name = base_name + if name in existing_names: + counter = 1 + candidate = f"{base_name}_{counter}" + while candidate in existing_names: + counter += 1 + candidate = f"{base_name}_{counter}" + name = candidate + # Assign back if changed or was None + if name != (agentset.name or base_name): agentset.name = name + existing_names.add(name) new_ids = pl.concat( [obj._ids] + [pl.Series(agentset["unique_id"]) for agentset in other_list] ) @@ -224,12 +234,7 @@ def contains( def contains( self, - sets: AgentSet - | type[AgentSet] - | str - | Iterable[AgentSet] - | Iterable[type[AgentSet]] - | Iterable[str], + sets: AgentSet | type[AgentSet] | str | Iterable[AgentSet] | Iterable[type[AgentSet]] | Iterable[str], ) -> bool | pl.Series: # Single value fast paths if isinstance(sets, AgentSet): From a43da1a3816079f45f90e9e5c087359b07ee4068 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 14 Sep 2025 11:39:23 +0200 Subject: [PATCH 122/329] Refactor Model: update step method to use public registry API for invoking agent steps --- mesa_frames/concrete/model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index 61a1db44..b91db207 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -126,7 +126,8 @@ def step(self) -> None: The default method calls the step() method of all agents. Overload as needed. """ - self.sets.step() + # Invoke step on all contained AgentSets via the public registry API + self.sets.do("step") @property def steps(self) -> int: From ccbd8a0fced95f4979cc43aa09a8df017be8c241 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 14 Sep 2025 18:12:09 +0200 Subject: [PATCH 123/329] Refactor Space: improve agent ID validation and handling using public API --- mesa_frames/abstract/agentsetregistry.py | 12 +++++++ mesa_frames/abstract/space.py | 42 ++++++++++++++---------- mesa_frames/concrete/agentsetregistry.py | 5 +++ 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index c3d0356d..6c43505b 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -575,3 +575,15 @@ def space(self) -> mesa_frames.abstract.space.Space | None: mesa_frames.abstract.space.Space | None """ return self.model.space + + @property + @abstractmethod + def ids(self) -> Series: + """Public view of all agent unique_id values across contained sets. + + Returns + ------- + Series + Concatenated unique_id Series for all AgentSets. + """ + ... diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index a5e2deed..808eb450 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -229,24 +229,27 @@ def swap_agents( ------- Self """ + # Normalize inputs to Series of ids for validation and operations + ids0 = self._get_ids_srs(agents0) + ids1 = self._get_ids_srs(agents1) if __debug__: - if len(agents0) != len(agents1): + if len(ids0) != len(ids1): raise ValueError("The two sets of agents must have the same length") - if not self._df_contains(self._agents, "agent_id", agents0).all(): + if not self._df_contains(self._agents, "agent_id", ids0).all(): raise ValueError("Some agents in agents0 are not in the space") - if not self._df_contains(self._agents, "agent_id", agents1).all(): + if not self._df_contains(self._agents, "agent_id", ids1).all(): raise ValueError("Some agents in agents1 are not in the space") - if self._srs_contains(agents0, agents1).any(): + if self._srs_contains(ids0, ids1).any(): raise ValueError("Some agents are present in both agents0 and agents1") obj = self._get_obj(inplace) agents0_df = obj._df_get_masked_df( - obj._agents, index_cols="agent_id", mask=agents0 + obj._agents, index_cols="agent_id", mask=ids0 ) agents1_df = obj._df_get_masked_df( - obj._agents, index_cols="agent_id", mask=agents1 + obj._agents, index_cols="agent_id", mask=ids1 ) - agents0_df = obj._df_set_index(agents0_df, "agent_id", agents1) - agents1_df = obj._df_set_index(agents1_df, "agent_id", agents0) + agents0_df = obj._df_set_index(agents0_df, "agent_id", ids1) + agents1_df = obj._df_set_index(agents1_df, "agent_id", ids0) obj._agents = obj._df_combine_first( agents0_df, obj._agents, index_cols="agent_id" ) @@ -498,9 +501,10 @@ def _get_ids_srs( dtype="uint64", ) elif isinstance(agents, AbstractAgentSetRegistry): - return self._srs_constructor(agents._ids, name="agent_id", dtype="uint64") + return self._srs_constructor(agents.ids, name="agent_id", dtype="uint64") elif isinstance(agents, Collection) and ( - isinstance(agents[0], AbstractAgentSetRegistry) + isinstance(agents[0], AbstractAgentSet) + or isinstance(agents[0], AbstractAgentSetRegistry) ): ids = [] for a in agents: @@ -514,7 +518,7 @@ def _get_ids_srs( ) elif isinstance(a, AbstractAgentSetRegistry): ids.append( - self._srs_constructor(a._ids, name="agent_id", dtype="uint64") + self._srs_constructor(a.ids, name="agent_id", dtype="uint64") ) return self._df_concat(ids, ignore_index=True) elif isinstance(agents, int): @@ -973,8 +977,8 @@ def _place_or_move_agents_to_cells( agents = self._get_ids_srs(agents) if __debug__: - # Check ids presence in model - b_contained = self.model.sets.contains(agents) + # Check ids presence in model using public API + b_contained = agents.is_in(self.model.sets.ids) if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): @@ -1588,7 +1592,9 @@ def out_of_bounds(self, pos: GridCoordinate | GridCoordinates) -> DataFrame: def remove_agents( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -1597,8 +1603,8 @@ def remove_agents( agents = obj._get_ids_srs(agents) if __debug__: - # Check ids presence in model - b_contained = obj.model.sets.contains(agents) + # Check ids presence in model via public ids + b_contained = agents.is_in(obj.model.sets.ids) if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): @@ -1780,7 +1786,7 @@ def _get_df_coords( if agents is not None: agents = self._get_ids_srs(agents) # Check ids presence in model - b_contained = self.model.sets.contains(agents) + b_contained = agents.is_in(self.model.sets.ids) if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): @@ -1859,8 +1865,8 @@ def _place_or_move_agents( if self._df_contains(self._agents, "agent_id", agents).any(): warn("Some agents are already present in the grid", RuntimeWarning) - # Check if agents are present in the model - b_contained = self.model.sets.contains(agents) + # Check if agents are present in the model using the public ids + b_contained = agents.is_in(self.model.sets.ids) if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 91a5cc54..4a29f5f1 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -631,6 +631,11 @@ def items(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSet]]: def values(self) -> Iterable[AgentSet]: return iter(self._agentsets) + @property + def ids(self) -> pl.Series: + """Public view of all agent unique_id values across contained sets.""" + return self._ids + @overload def __getitem__(self, key: int) -> AgentSet: ... From 4b832e1ebf3977ec82eaaf2c99e18b5418df0c59 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 14 Sep 2025 18:44:33 +0200 Subject: [PATCH 124/329] Add comprehensive tests for AgentSetRegistry functionality - Implemented unit tests for AgentSetRegistry, covering initialization, addition, removal, and retrieval of agent sets. - Created example agent sets (ExampleAgentSetA and ExampleAgentSetB) to facilitate testing. - Verified behavior for methods such as add, remove, contains, do, get, and various dunder methods. - Ensured proper handling of edge cases, including duplicate names and model mismatches. - Utilized pytest fixtures for consistent test setup and teardown. --- mesa_frames/concrete/agentsetregistry.py | 7 +- tests/test_agents.py | 1039 ---------------------- tests/test_agentsetregistry.py | 382 ++++++++ 3 files changed, 388 insertions(+), 1040 deletions(-) delete mode 100644 tests/test_agents.py create mode 100644 tests/test_agentsetregistry.py diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 4a29f5f1..d64644ef 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -234,7 +234,12 @@ def contains( def contains( self, - sets: AgentSet | type[AgentSet] | str | Iterable[AgentSet] | Iterable[type[AgentSet]] | Iterable[str], + sets: AgentSet + | type[AgentSet] + | str + | Iterable[AgentSet] + | Iterable[type[AgentSet]] + | Iterable[str], ) -> bool | pl.Series: # Single value fast paths if isinstance(sets, AgentSet): diff --git a/tests/test_agents.py b/tests/test_agents.py deleted file mode 100644 index 9de45dd3..00000000 --- a/tests/test_agents.py +++ /dev/null @@ -1,1039 +0,0 @@ -from copy import copy, deepcopy - -import polars as pl -import pytest - -from mesa_frames import AgentSetRegistry, Model -from mesa_frames import AgentSet -from mesa_frames.types_ import AgentMask -from tests.test_agentset import ( - ExampleAgentSet, - ExampleAgentSetNoWealth, - fix1_AgentSet_no_wealth, - fix1_AgentSet, - fix2_AgentSet, - fix3_AgentSet, -) - - -@pytest.fixture -def fix_AgentSetRegistry( - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, -) -> AgentSetRegistry: - model = Model() - agents = AgentSetRegistry(model) - agents.add([fix1_AgentSet, fix2_AgentSet]) - return agents - - -class Test_AgentSetRegistry: - def test___init__(self): - model = Model() - agents = AgentSetRegistry(model) - assert agents.model == model - assert isinstance(agents._agentsets, list) - assert len(agents._agentsets) == 0 - assert isinstance(agents._ids, pl.Series) - assert agents._ids.is_empty() - assert agents._ids.name == "unique_id" - - def test_add( - self, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - model = Model() - agents = AgentSetRegistry(model) - agentset_polars1 = fix1_AgentSet - agentset_polars2 = fix2_AgentSet - - # Test with a single AgentSet - result = agents.add(agentset_polars1, inplace=False) - assert result._agentsets[0] is agentset_polars1 - assert result._ids.to_list() == agentset_polars1._df["unique_id"].to_list() - - # Test with a list of AgentSets - result = agents.add([agentset_polars1, agentset_polars2], inplace=True) - assert result._agentsets[0] is agentset_polars1 - assert result._agentsets[1] is agentset_polars2 - assert ( - result._ids.to_list() - == agentset_polars1._df["unique_id"].to_list() - + agentset_polars2._df["unique_id"].to_list() - ) - - # Test if adding the same AgentSet raises ValueError - with pytest.raises(ValueError): - agents.add(agentset_polars1, inplace=False) - - def test_contains( - self, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - fix3_AgentSet: ExampleAgentSet, - fix_AgentSetRegistry: AgentSetRegistry, - ): - agents = fix_AgentSetRegistry - agentset_polars1 = agents._agentsets[0] - - # Test with an AgentSet - assert agents.contains(agentset_polars1) - assert agents.contains(fix1_AgentSet) - assert agents.contains(fix2_AgentSet) - - # Test with an AgentSet not present - assert not agents.contains(fix3_AgentSet) - - # Test with an iterable of AgentSets - assert agents.contains([agentset_polars1, fix3_AgentSet]).to_list() == [ - True, - False, - ] - - # Test with empty iterable - returns True - assert agents.contains([]) - - # Test with single id - assert agents.contains(agentset_polars1["unique_id"][0]) - - # Test with a list of ids - assert agents.contains([agentset_polars1["unique_id"][0], 0]).to_list() == [ - True, - False, - ] - - def test_copy(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - agents.test_list = [[1, 2, 3]] - - # Test with deep=False - agents2 = agents.copy(deep=False) - agents2.test_list[0].append(4) - assert agents.test_list[0][-1] == agents2.test_list[0][-1] - assert agents.model == agents2.model - assert agents._agentsets[0] == agents2._agentsets[0] - assert (agents._ids == agents2._ids).all() - - # Test with deep=True - agents2 = fix_AgentSetRegistry.copy(deep=True) - agents2.test_list[0].append(4) - assert agents.test_list[-1] != agents2.test_list[-1] - assert agents.model == agents2.model - assert agents._agentsets[0] != agents2._agentsets[0] - assert (agents._ids == agents2._ids).all() - - def test_discard( - self, fix_AgentSetRegistry: AgentSetRegistry, fix2_AgentSet: ExampleAgentSet - ): - agents = fix_AgentSetRegistry - # Test with a single AgentSet - agentset_polars2 = agents._agentsets[1] - result = agents.discard(agents._agentsets[0], inplace=False) - assert isinstance(result._agentsets[0], ExampleAgentSet) - assert len(result._agentsets) == 1 - - # Test with a list of AgentSets - result = agents.discard(agents._agentsets.copy(), inplace=False) - assert len(result._agentsets) == 0 - - # Test with IDs - ids = [ - agents._agentsets[0]._df["unique_id"][0], - agents._agentsets[1]._df["unique_id"][0], - ] - agentset_polars1 = agents._agentsets[0] - agentset_polars2 = agents._agentsets[1] - result = agents.discard(ids, inplace=False) - assert ( - result._agentsets[0]["unique_id"][0] - == agentset_polars1._df.select("unique_id").row(1)[0] - ) - assert ( - result._agentsets[1].df["unique_id"][0] - == agentset_polars2._df["unique_id"][1] - ) - - # Test if removing an AgentSet not present raises ValueError - result = agents.discard(fix2_AgentSet, inplace=False) - - # Test if removing an ID not present raises KeyError - assert 0 not in agents._ids - result = agents.discard(0, inplace=False) - - def test_do(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - expected_result_0 = agents._agentsets[0].df["wealth"] - expected_result_0 += 1 - - expected_result_1 = agents._agentsets[1].df["wealth"] - expected_result_1 += 1 - - # Test with no return_results, no mask, inplace - agents.do("add_wealth", 1) - assert ( - agents._agentsets[0].df["wealth"].to_list() == expected_result_0.to_list() - ) - assert ( - agents._agentsets[1].df["wealth"].to_list() == expected_result_1.to_list() - ) - - # Test with return_results=True, no mask, inplace - expected_result_0 = agents._agentsets[0].df["wealth"] - expected_result_0 += 1 - - expected_result_1 = agents._agentsets[1].df["wealth"] - expected_result_1 += 1 - assert agents.do("add_wealth", 1, return_results=True) == { - agents._agentsets[0]: None, - agents._agentsets[1]: None, - } - assert ( - agents._agentsets[0].df["wealth"].to_list() == expected_result_0.to_list() - ) - assert ( - agents._agentsets[1].df["wealth"].to_list() == expected_result_1.to_list() - ) - - # Test with a mask, inplace - mask0 = agents._agentsets[0].df["wealth"] > 10 # No agent should be selected - mask1 = agents._agentsets[1].df["wealth"] > 10 # All agents should be selected - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - - expected_result_0 = agents._agentsets[0].df["wealth"] - expected_result_1 = agents._agentsets[1].df["wealth"] - expected_result_1 += 1 - - agents.do("add_wealth", 1, mask=mask_dictionary) - assert ( - agents._agentsets[0].df["wealth"].to_list() == expected_result_0.to_list() - ) - assert ( - agents._agentsets[1].df["wealth"].to_list() == expected_result_1.to_list() - ) - - def test_get( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - fix1_AgentSet_no_wealth: ExampleAgentSetNoWealth, - ): - agents = fix_AgentSetRegistry - - # Test with a single attribute - assert ( - agents.get("wealth")[fix1_AgentSet].to_list() - == fix1_AgentSet._df["wealth"].to_list() - ) - assert ( - agents.get("wealth")[fix2_AgentSet].to_list() - == fix2_AgentSet._df["wealth"].to_list() - ) - - # Test with a list of attributes - result = agents.get(["wealth", "age"]) - assert result[fix1_AgentSet].columns == ["wealth", "age"] - assert ( - result[fix1_AgentSet]["wealth"].to_list() - == fix1_AgentSet._df["wealth"].to_list() - ) - assert ( - result[fix1_AgentSet]["age"].to_list() == fix1_AgentSet._df["age"].to_list() - ) - - assert result[fix2_AgentSet].columns == ["wealth", "age"] - assert ( - result[fix2_AgentSet]["wealth"].to_list() - == fix2_AgentSet._df["wealth"].to_list() - ) - assert ( - result[fix2_AgentSet]["age"].to_list() == fix2_AgentSet._df["age"].to_list() - ) - - # Test with a single attribute and a mask - mask0 = fix1_AgentSet._df["wealth"] > fix1_AgentSet._df["wealth"][0] - mask1 = fix2_AgentSet._df["wealth"] > fix2_AgentSet._df["wealth"][0] - mask_dictionary = {fix1_AgentSet: mask0, fix2_AgentSet: mask1} - result = agents.get("wealth", mask=mask_dictionary) - assert ( - result[fix1_AgentSet].to_list() == fix1_AgentSet._df["wealth"].to_list()[1:] - ) - assert ( - result[fix2_AgentSet].to_list() == fix2_AgentSet._df["wealth"].to_list()[1:] - ) - - # Test heterogeneous agent sets (different columns) - # This tests the fix for the bug where agents_df["column"] would raise - # ColumnNotFoundError when some agent sets didn't have that column. - - # Create a new AgentSetRegistry with heterogeneous agent sets - model = Model() - hetero_agents = AgentSetRegistry(model) - hetero_agents.add([fix1_AgentSet, fix1_AgentSet_no_wealth]) - - # Test 1: Access column that exists in only one agent set - result_wealth = hetero_agents.get("wealth") - assert len(result_wealth) == 1, ( - "Should only return agent sets that have 'wealth'" - ) - assert fix1_AgentSet in result_wealth, ( - "Should include the agent set with wealth" - ) - assert fix1_AgentSet_no_wealth not in result_wealth, ( - "Should not include agent set without wealth" - ) - assert result_wealth[fix1_AgentSet].to_list() == [1, 2, 3, 4] - - # Test 2: Access column that exists in all agent sets - result_age = hetero_agents.get("age") - assert len(result_age) == 2, "Should return both agent sets that have 'age'" - assert fix1_AgentSet in result_age - assert fix1_AgentSet_no_wealth in result_age - assert result_age[fix1_AgentSet].to_list() == [10, 20, 30, 40] - assert result_age[fix1_AgentSet_no_wealth].to_list() == [1, 2, 3, 4] - - # Test 3: Access column that exists in no agent sets - result_nonexistent = hetero_agents.get("nonexistent_column") - assert len(result_nonexistent) == 0, ( - "Should return empty dict for non-existent column" - ) - - # Test 4: Access multiple columns (mixed availability) - result_multi = hetero_agents.get(["wealth", "age"]) - assert len(result_multi) == 1, ( - "Should only include agent sets that have ALL requested columns" - ) - assert fix1_AgentSet in result_multi - assert fix1_AgentSet_no_wealth not in result_multi - assert result_multi[fix1_AgentSet].columns == ["wealth", "age"] - - # Test 5: Access multiple columns where some exist in different sets - result_mixed = hetero_agents.get(["age", "income"]) - assert len(result_mixed) == 1, ( - "Should only include agent set that has both 'age' and 'income'" - ) - assert fix1_AgentSet_no_wealth in result_mixed - assert fix1_AgentSet not in result_mixed - - # Test 6: Test via __getitem__ syntax (the original bug report case) - wealth_via_getitem = hetero_agents["wealth"] - assert len(wealth_via_getitem) == 1 - assert fix1_AgentSet in wealth_via_getitem - assert wealth_via_getitem[fix1_AgentSet].to_list() == [1, 2, 3, 4] - - # Test 7: Test get(None) - should return all columns for all agent sets - result_none = hetero_agents.get(None) - assert len(result_none) == 2, ( - "Should return both agent sets when attr_names=None" - ) - assert fix1_AgentSet in result_none - assert fix1_AgentSet_no_wealth in result_none - - # Verify each agent set returns all its columns (excluding unique_id) - wealth_set_result = result_none[fix1_AgentSet] - assert isinstance(wealth_set_result, pl.DataFrame), ( - "Should return DataFrame when attr_names=None" - ) - expected_wealth_cols = {"wealth", "age"} # unique_id should be excluded - assert set(wealth_set_result.columns) == expected_wealth_cols - - no_wealth_set_result = result_none[fix1_AgentSet_no_wealth] - assert isinstance(no_wealth_set_result, pl.DataFrame), ( - "Should return DataFrame when attr_names=None" - ) - expected_no_wealth_cols = {"income", "age"} # unique_id should be excluded - assert set(no_wealth_set_result.columns) == expected_no_wealth_cols - - def test_remove( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix3_AgentSet: ExampleAgentSet, - ): - agents = fix_AgentSetRegistry - - # Test with a single AgentSet - agentset_polars = agents._agentsets[1] - result = agents.remove(agents._agentsets[0], inplace=False) - assert isinstance(result._agentsets[0], ExampleAgentSet) - assert len(result._agentsets) == 1 - - # Test with a list of AgentSets - result = agents.remove(agents._agentsets.copy(), inplace=False) - assert len(result._agentsets) == 0 - - # Test with IDs - ids = [ - agents._agentsets[0]._df["unique_id"][0], - agents._agentsets[1]._df["unique_id"][0], - ] - agentset_polars1 = agents._agentsets[0] - agentset_polars2 = agents._agentsets[1] - result = agents.remove(ids, inplace=False) - assert ( - result._agentsets[0]["unique_id"][0] - == agentset_polars1._df.select("unique_id").row(1)[0] - ) - assert ( - result._agentsets[1].df["unique_id"][0] - == agentset_polars2._df["unique_id"][1] - ) - - # Test if removing an AgentSet not present raises ValueError - with pytest.raises(ValueError): - result = agents.remove(fix3_AgentSet, inplace=False) - - # Test if removing an ID not present raises KeyError - assert 0 not in agents._ids - with pytest.raises(KeyError): - result = agents.remove(0, inplace=False) - - def test_select(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - # Test with default arguments. Should select all agents - selected = agents.select(inplace=False) - active_agents_dict = selected.active_agents - agents_dict = selected.df - assert active_agents_dict.keys() == agents_dict.keys() - # Using assert to compare all DataFrames in the dictionaries - - assert ( - list(active_agents_dict.values())[0].rows() - == list(agents_dict.values())[0].rows() - ) - - assert all( - series.all() - for series in ( - list(active_agents_dict.values())[1] == list(agents_dict.values())[1] - ) - ) - - # Test with a mask - mask0 = pl.Series("mask", [True, False, True, True], dtype=pl.Boolean) - mask1 = pl.Series("mask", [True, False, True, True], dtype=pl.Boolean) - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - selected = agents.select(mask_dictionary, inplace=False) - assert ( - selected.active_agents[selected._agentsets[0]]["wealth"].to_list()[0] - == agents._agentsets[0]["wealth"].to_list()[0] - ) - assert ( - selected.active_agents[selected._agentsets[0]]["wealth"].to_list()[-1] - == agents._agentsets[0]["wealth"].to_list()[-1] - ) - - assert ( - selected.active_agents[selected._agentsets[1]]["wealth"].to_list()[0] - == agents._agentsets[1]["wealth"].to_list()[0] - ) - assert ( - selected.active_agents[selected._agentsets[1]]["wealth"].to_list()[-1] - == agents._agentsets[1]["wealth"].to_list()[-1] - ) - - # Test with filter_func - - def filter_func(agentset: AgentSet) -> pl.Series: - return agentset.df["wealth"] > agentset.df["wealth"].to_list()[0] - - selected = agents.select(filter_func=filter_func, inplace=False) - assert ( - selected.active_agents[selected._agentsets[0]]["wealth"].to_list() - == agents._agentsets[0]["wealth"].to_list()[1:] - ) - assert ( - selected.active_agents[selected._agentsets[1]]["wealth"].to_list() - == agents._agentsets[1]["wealth"].to_list()[1:] - ) - - # Test with n - selected = agents.select(n=3, inplace=False) - assert sum(len(df) for df in selected.active_agents.values()) in [2, 3] - - # Test with n, filter_func and mask - selected = agents.select( - mask_dictionary, filter_func=filter_func, n=2, inplace=False - ) - assert any( - el in selected.active_agents[selected._agentsets[0]]["wealth"].to_list() - for el in agents.active_agents[agents._agentsets[0]]["wealth"].to_list()[ - 2:4 - ] - ) - - assert any( - el in selected.active_agents[selected._agentsets[1]]["wealth"].to_list() - for el in agents.active_agents[agents._agentsets[1]]["wealth"].to_list()[ - 2:4 - ] - ) - - def test_set(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - # Test with a single attribute - result = agents.set("wealth", 0, inplace=False) - assert result._agentsets[0].df["wealth"].to_list() == [0] * len( - agents._agentsets[0] - ) - assert result._agentsets[1].df["wealth"].to_list() == [0] * len( - agents._agentsets[1] - ) - - # Test with a list of attributes - agents.set(["wealth", "age"], 1, inplace=True) - assert agents._agentsets[0].df["wealth"].to_list() == [1] * len( - agents._agentsets[0] - ) - assert agents._agentsets[0].df["age"].to_list() == [1] * len( - agents._agentsets[0] - ) - - # Test with a single attribute and a mask - mask0 = pl.Series( - "mask", [True] + [False] * (len(agents._agentsets[0]) - 1), dtype=pl.Boolean - ) - mask1 = pl.Series( - "mask", [True] + [False] * (len(agents._agentsets[1]) - 1), dtype=pl.Boolean - ) - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - result = agents.set("wealth", 0, mask=mask_dictionary, inplace=False) - assert result._agentsets[0].df["wealth"].to_list() == [0] + [1] * ( - len(agents._agentsets[0]) - 1 - ) - assert result._agentsets[1].df["wealth"].to_list() == [0] + [1] * ( - len(agents._agentsets[1]) - 1 - ) - - # Test with a dictionary - agents.set( - {agents._agentsets[0]: {"wealth": 0}, agents._agentsets[1]: {"wealth": 1}}, - inplace=True, - ) - assert agents._agentsets[0].df["wealth"].to_list() == [0] * len( - agents._agentsets[0] - ) - assert agents._agentsets[1].df["wealth"].to_list() == [1] * len( - agents._agentsets[1] - ) - - def test_shuffle(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - for _ in range(100): - original_order_0 = agents._agentsets[0].df["unique_id"].to_list() - original_order_1 = agents._agentsets[1].df["unique_id"].to_list() - agents.shuffle(inplace=True) - if ( - original_order_0 != agents._agentsets[0].df["unique_id"].to_list() - and original_order_1 != agents._agentsets[1].df["unique_id"].to_list() - ): - return - assert False - - def test_sort(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - agents.sort("wealth", ascending=False, inplace=True) - assert pl.Series(agents._agentsets[0].df["wealth"]).is_sorted(descending=True) - assert pl.Series(agents._agentsets[1].df["wealth"]).is_sorted(descending=True) - - def test_step( - self, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - fix_AgentSetRegistry: AgentSetRegistry, - ): - previous_wealth_0 = fix1_AgentSet._df["wealth"].clone() - previous_wealth_1 = fix2_AgentSet._df["wealth"].clone() - - agents = fix_AgentSetRegistry - agents.step() - - assert ( - agents._agentsets[0].df["wealth"].to_list() - == (previous_wealth_0 + 1).to_list() - ) - assert ( - agents._agentsets[1].df["wealth"].to_list() - == (previous_wealth_1 + 1).to_list() - ) - - def test__check_ids_presence( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - agents = fix_AgentSetRegistry.remove(fix2_AgentSet, inplace=False) - agents_different_index = deepcopy(fix2_AgentSet) - result = agents._check_ids_presence([fix1_AgentSet]) - assert result.filter(pl.col("unique_id").is_in(fix1_AgentSet._df["unique_id"]))[ - "present" - ].all() - - assert not result.filter( - pl.col("unique_id").is_in(agents_different_index._df["unique_id"]) - )["present"].any() - - def test__check_agentsets_presence( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix3_AgentSet: ExampleAgentSet, - ): - agents = fix_AgentSetRegistry - result = agents._check_agentsets_presence([fix1_AgentSet, fix3_AgentSet]) - assert result[0] - assert not result[1] - - def test__get_bool_masks(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - # Test with mask = None - result = agents._get_bool_masks(mask=None) - truth_value = True - for i, mask in enumerate(result.values()): - if isinstance(mask, pl.Expr): - mask = agents._agentsets[i]._df.select(mask).to_series() - truth_value &= mask.all() - assert truth_value - - # Test with mask = "all" - result = agents._get_bool_masks(mask="all") - truth_value = True - for i, mask in enumerate(result.values()): - if isinstance(mask, pl.Expr): - mask = agents._agentsets[i]._df.select(mask).to_series() - truth_value &= mask.all() - assert truth_value - - # Test with mask = "active" - mask0 = ( - agents._agentsets[0].df["wealth"] - > agents._agentsets[0].df["wealth"].to_list()[0] - ) - mask1 = agents._agentsets[1].df["wealth"] > agents._agentsets[1].df["wealth"][0] - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - agents.select(mask=mask_dictionary) - result = agents._get_bool_masks(mask="active") - assert result[agents._agentsets[0]].to_list() == mask0.to_list() - assert result[agents._agentsets[1]].to_list() == mask1.to_list() - - # Test with mask = IdsLike - result = agents._get_bool_masks( - mask=[ - agents._agentsets[0]["unique_id"][0], - agents._agentsets[1].df["unique_id"][0], - ] - ) - assert result[agents._agentsets[0]].to_list() == [True] + [False] * ( - len(agents._agentsets[0]) - 1 - ) - assert result[agents._agentsets[1]].to_list() == [True] + [False] * ( - len(agents._agentsets[1]) - 1 - ) - - # Test with mask = dict[AgentSet, AgentMask] - result = agents._get_bool_masks(mask=mask_dictionary) - assert result[agents._agentsets[0]].to_list() == mask0.to_list() - assert result[agents._agentsets[1]].to_list() == mask1.to_list() - - def test__get_obj(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - assert agents._get_obj(inplace=True) is agents - assert agents._get_obj(inplace=False) is not agents - - def test__return_agentsets_list( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - agents = fix_AgentSetRegistry - result = agents._return_agentsets_list(fix1_AgentSet) - assert result == [fix1_AgentSet] - result = agents._return_agentsets_list([fix1_AgentSet, fix2_AgentSet]) - assert result == [fix1_AgentSet, fix2_AgentSet] - - def test___add__( - self, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - model = Model() - agents = AgentSetRegistry(model) - agentset_polars1 = fix1_AgentSet - agentset_polars2 = fix2_AgentSet - - # Test with a single AgentSet - result = agents + agentset_polars1 - assert result._agentsets[0] is agentset_polars1 - assert result._ids.to_list() == agentset_polars1._df["unique_id"].to_list() - - # Test with a single AgentSet same as above - result = agents + agentset_polars2 - assert result._agentsets[0] is agentset_polars2 - assert result._ids.to_list() == agentset_polars2._df["unique_id"].to_list() - - # Test with a list of AgentSets - result = agents + [agentset_polars1, agentset_polars2] - assert result._agentsets[0] is agentset_polars1 - assert result._agentsets[1] is agentset_polars2 - assert ( - result._ids.to_list() - == agentset_polars1._df["unique_id"].to_list() - + agentset_polars2._df["unique_id"].to_list() - ) - - # Test if adding the same AgentSet raises ValueError - with pytest.raises(ValueError): - result + agentset_polars1 - - def test___contains__( - self, fix_AgentSetRegistry: AgentSetRegistry, fix3_AgentSet: ExampleAgentSet - ): - # Test with a single value - agents = fix_AgentSetRegistry - agentset_polars1 = agents._agentsets[0] - - # Test with an AgentSet - assert agentset_polars1 in agents - # Test with an AgentSet not present - assert fix3_AgentSet not in agents - - # Test with single id present - assert agentset_polars1["unique_id"][0] in agents - - # Test with single id not present - assert 0 not in agents - - def test___copy__(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - agents.test_list = [[1, 2, 3]] - - # Test with deep=False - agents2 = copy(agents) - agents2.test_list[0].append(4) - assert agents.test_list[0][-1] == agents2.test_list[0][-1] - assert agents.model == agents2.model - assert agents._agentsets[0] == agents2._agentsets[0] - assert (agents._ids == agents2._ids).all() - - def test___deepcopy__(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - agents.test_list = [[1, 2, 3]] - - agents2 = deepcopy(agents) - agents2.test_list[0].append(4) - assert agents.test_list[-1] != agents2.test_list[-1] - assert agents.model == agents2.model - assert agents._agentsets[0] != agents2._agentsets[0] - assert (agents._ids == agents2._ids).all() - - def test___getattr__(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - assert isinstance(agents.model, Model) - result = agents.wealth - assert ( - result[agents._agentsets[0]].to_list() - == agents._agentsets[0].df["wealth"].to_list() - ) - assert ( - result[agents._agentsets[1]].to_list() - == agents._agentsets[1].df["wealth"].to_list() - ) - - def test___getitem__( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - agents = fix_AgentSetRegistry - - # Test with a single attribute - assert ( - agents["wealth"][fix1_AgentSet].to_list() - == fix1_AgentSet._df["wealth"].to_list() - ) - assert ( - agents["wealth"][fix2_AgentSet].to_list() - == fix2_AgentSet._df["wealth"].to_list() - ) - - # Test with a list of attributes - result = agents[["wealth", "age"]] - assert result[fix1_AgentSet].columns == ["wealth", "age"] - assert ( - result[fix1_AgentSet]["wealth"].to_list() - == fix1_AgentSet._df["wealth"].to_list() - ) - assert ( - result[fix1_AgentSet]["age"].to_list() == fix1_AgentSet._df["age"].to_list() - ) - assert result[fix2_AgentSet].columns == ["wealth", "age"] - assert ( - result[fix2_AgentSet]["wealth"].to_list() - == fix2_AgentSet._df["wealth"].to_list() - ) - assert ( - result[fix2_AgentSet]["age"].to_list() == fix2_AgentSet._df["age"].to_list() - ) - - # Test with a single attribute and a mask - mask0 = fix1_AgentSet._df["wealth"] > fix1_AgentSet._df["wealth"][0] - mask1 = fix2_AgentSet._df["wealth"] > fix2_AgentSet._df["wealth"][0] - mask_dictionary: dict[AgentSet, AgentMask] = { - fix1_AgentSet: mask0, - fix2_AgentSet: mask1, - } - result = agents[mask_dictionary, "wealth"] - assert ( - result[fix1_AgentSet].to_list() == fix1_AgentSet.df["wealth"].to_list()[1:] - ) - assert ( - result[fix2_AgentSet].to_list() == fix2_AgentSet.df["wealth"].to_list()[1:] - ) - - def test___iadd__( - self, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - model = Model() - agents = AgentSetRegistry(model) - agentset_polars1 = fix1_AgentSet - agentset_polars = fix2_AgentSet - - # Test with a single AgentSet - agents_copy = deepcopy(agents) - agents_copy += agentset_polars - assert agents_copy._agentsets[0] is agentset_polars - assert agents_copy._ids.to_list() == agentset_polars._df["unique_id"].to_list() - - # Test with a list of AgentSets - agents_copy = deepcopy(agents) - agents_copy += [agentset_polars1, agentset_polars] - assert agents_copy._agentsets[0] is agentset_polars1 - assert agents_copy._agentsets[1] is agentset_polars - assert ( - agents_copy._ids.to_list() - == agentset_polars1._df["unique_id"].to_list() - + agentset_polars._df["unique_id"].to_list() - ) - - # Test if adding the same AgentSet raises ValueError - with pytest.raises(ValueError): - agents_copy += agentset_polars1 - - def test___iter__(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - len_agentset0 = len(agents._agentsets[0]) - len_agentset1 = len(agents._agentsets[1]) - for i, agent in enumerate(agents): - assert isinstance(agent, dict) - if i < len_agentset0: - assert agent["unique_id"] == agents._agentsets[0].df["unique_id"][i] - else: - assert ( - agent["unique_id"] - == agents._agentsets[1].df["unique_id"][i - len_agentset0] - ) - assert i == len_agentset0 + len_agentset1 - 1 - - def test___isub__( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - # Test with an AgentSet and a DataFrame - agents = fix_AgentSetRegistry - agents -= fix1_AgentSet - assert agents._agentsets[0] == fix2_AgentSet - assert len(agents._agentsets) == 1 - - def test___len__( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - assert len(fix_AgentSetRegistry) == len(fix1_AgentSet) + len(fix2_AgentSet) - - def test___repr__(self, fix_AgentSetRegistry: AgentSetRegistry): - repr(fix_AgentSetRegistry) - - def test___reversed__(self, fix2_AgentSet: AgentSetRegistry): - agents = fix2_AgentSet - reversed_wealth = [] - for agent in reversed(list(agents)): - reversed_wealth.append(agent["wealth"]) - assert reversed_wealth == list(reversed(agents["wealth"])) - - def test___setitem__(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - # Test with a single attribute - agents["wealth"] = 0 - assert agents._agentsets[0].df["wealth"].to_list() == [0] * len( - agents._agentsets[0] - ) - assert agents._agentsets[1].df["wealth"].to_list() == [0] * len( - agents._agentsets[1] - ) - - # Test with a list of attributes - agents[["wealth", "age"]] = 1 - assert agents._agentsets[0].df["wealth"].to_list() == [1] * len( - agents._agentsets[0] - ) - assert agents._agentsets[0].df["age"].to_list() == [1] * len( - agents._agentsets[0] - ) - - # Test with a single attribute and a mask - mask0 = pl.Series( - "mask", [True] + [False] * (len(agents._agentsets[0]) - 1), dtype=pl.Boolean - ) - mask1 = pl.Series( - "mask", [True] + [False] * (len(agents._agentsets[1]) - 1), dtype=pl.Boolean - ) - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - agents[mask_dictionary, "wealth"] = 0 - assert agents._agentsets[0].df["wealth"].to_list() == [0] + [1] * ( - len(agents._agentsets[0]) - 1 - ) - assert agents._agentsets[1].df["wealth"].to_list() == [0] + [1] * ( - len(agents._agentsets[1]) - 1 - ) - - def test___str__(self, fix_AgentSetRegistry: AgentSetRegistry): - str(fix_AgentSetRegistry) - - def test___sub__( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - # Test with an AgentSet and a DataFrame - result = fix_AgentSetRegistry - fix1_AgentSet - assert isinstance(result._agentsets[0], ExampleAgentSet) - assert len(result._agentsets) == 1 - - def test_agents( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - assert isinstance(fix_AgentSetRegistry.df, dict) - assert len(fix_AgentSetRegistry.df) == 2 - assert fix_AgentSetRegistry.df[fix1_AgentSet] is fix1_AgentSet._df - assert fix_AgentSetRegistry.df[fix2_AgentSet] is fix2_AgentSet._df - - # Test agents.setter - fix_AgentSetRegistry.df = [fix1_AgentSet, fix2_AgentSet] - assert fix_AgentSetRegistry._agentsets[0] == fix1_AgentSet - assert fix_AgentSetRegistry._agentsets[1] == fix2_AgentSet - - def test_active_agents(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - # Test with select - mask0 = ( - agents._agentsets[0].df["wealth"] - > agents._agentsets[0].df["wealth"].to_list()[0] - ) - mask1 = ( - agents._agentsets[1].df["wealth"] - > agents._agentsets[1].df["wealth"].to_list()[0] - ) - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - - agents1 = agents.select(mask=mask_dictionary, inplace=False) - - result = agents1.active_agents - assert isinstance(result, dict) - assert isinstance(result[agents1._agentsets[0]], pl.DataFrame) - assert isinstance(result[agents1._agentsets[1]], pl.DataFrame) - - assert all( - series.all() - for series in ( - result[agents1._agentsets[0]] == agents1._agentsets[0]._df.filter(mask0) - ) - ) - - assert all( - series.all() - for series in ( - result[agents1._agentsets[1]] == agents1._agentsets[1]._df.filter(mask1) - ) - ) - - # Test with active_agents.setter - agents1.active_agents = mask_dictionary - result = agents1.active_agents - assert isinstance(result, dict) - assert isinstance(result[agents1._agentsets[0]], pl.DataFrame) - assert isinstance(result[agents1._agentsets[1]], pl.DataFrame) - assert all( - series.all() - for series in ( - result[agents1._agentsets[0]] == agents1._agentsets[0]._df.filter(mask0) - ) - ) - assert all( - series.all() - for series in ( - result[agents1._agentsets[1]] == agents1._agentsets[1]._df.filter(mask1) - ) - ) - - def test_agentsets_by_type(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - result = agents.agentsets_by_type - assert isinstance(result, dict) - assert isinstance(result[ExampleAgentSet], AgentSetRegistry) - - assert ( - result[ExampleAgentSet]._agentsets[0].df.rows() - == agents._agentsets[1].df.rows() - ) - - def test_inactive_agents(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - # Test with select - mask0 = ( - agents._agentsets[0].df["wealth"] - > agents._agentsets[0].df["wealth"].to_list()[0] - ) - mask1 = ( - agents._agentsets[1].df["wealth"] - > agents._agentsets[1].df["wealth"].to_list()[0] - ) - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - agents1 = agents.select(mask=mask_dictionary, inplace=False) - result = agents1.inactive_agents - assert isinstance(result, dict) - assert isinstance(result[agents1._agentsets[0]], pl.DataFrame) - assert isinstance(result[agents1._agentsets[1]], pl.DataFrame) - assert all( - series.all() - for series in ( - result[agents1._agentsets[0]] - == agents1._agentsets[0].select(mask0, negate=True).active_agents - ) - ) - assert all( - series.all() - for series in ( - result[agents1._agentsets[1]] - == agents1._agentsets[1].select(mask1, negate=True).active_agents - ) - ) diff --git a/tests/test_agentsetregistry.py b/tests/test_agentsetregistry.py new file mode 100644 index 00000000..32ac00bc --- /dev/null +++ b/tests/test_agentsetregistry.py @@ -0,0 +1,382 @@ +import polars as pl +import pytest +import beartype.roar as bear_roar + +from mesa_frames import AgentSet, AgentSetRegistry, Model + + +class ExampleAgentSetA(AgentSet): + def __init__(self, model: Model): + super().__init__(model) + self["wealth"] = pl.Series("wealth", [1, 2, 3, 4]) + self["age"] = pl.Series("age", [10, 20, 30, 40]) + + def add_wealth(self, amount: int) -> None: + self["wealth"] += amount + + def step(self) -> None: + self.add_wealth(1) + + def count(self) -> int: + return len(self) + + +class ExampleAgentSetB(AgentSet): + def __init__(self, model: Model): + super().__init__(model) + self["wealth"] = pl.Series("wealth", [10, 20, 30, 40]) + self["age"] = pl.Series("age", [11, 22, 33, 44]) + + def add_wealth(self, amount: int) -> None: + self["wealth"] += amount + + def step(self) -> None: + self.add_wealth(2) + + def count(self) -> int: + return len(self) + + +@pytest.fixture +def fix_model() -> Model: + return Model() + + +@pytest.fixture +def fix_set_a(fix_model: Model) -> ExampleAgentSetA: + return ExampleAgentSetA(fix_model) + + +@pytest.fixture +def fix_set_b(fix_model: Model) -> ExampleAgentSetB: + return ExampleAgentSetB(fix_model) + + +@pytest.fixture +def fix_registry_with_two( + fix_model: Model, fix_set_a: ExampleAgentSetA, fix_set_b: ExampleAgentSetB +) -> AgentSetRegistry: + reg = AgentSetRegistry(fix_model) + reg.add([fix_set_a, fix_set_b]) + return reg + + +class TestAgentSetRegistry: + # Dunder: __init__ + def test__init__(self): + model = Model() + reg = AgentSetRegistry(model) + assert reg.model is model + assert len(reg) == 0 + assert reg.ids.len() == 0 + + # Public: add + def test_add(self, fix_model: Model) -> None: + reg = AgentSetRegistry(fix_model) + a1 = ExampleAgentSetA(fix_model) + a2 = ExampleAgentSetA(fix_model) + # Add single + reg.add(a1) + assert len(reg) == 1 + assert a1 in reg + # Add list; second should be auto-renamed with suffix + reg.add([a2]) + assert len(reg) == 2 + names = [s.name for s in reg] + assert names[0] == "ExampleAgentSetA" + assert names[1] in ("ExampleAgentSetA_1", "ExampleAgentSetA_2") + # ids concatenated + assert reg.ids.len() == len(a1) + len(a2) + # Duplicate instance rejected + with pytest.raises( + ValueError, match="already present in the AgentSetRegistry" + ): + reg.add([a1]) + # Duplicate unique_id space rejected + a3 = ExampleAgentSetB(fix_model) + a3.df = a1.df + with pytest.raises(ValueError, match="agent IDs are not unique"): + reg.add(a3) + + # Public: contains + def test_contains(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + a_name = next(iter(reg)).name + # Single instance + assert reg.contains(reg[0]) is True + # Single type + assert reg.contains(ExampleAgentSetA) is True + # Single name + assert reg.contains(a_name) is True + # Iterable: instances + assert reg.contains([reg[0], reg[1]]).to_list() == [True, True] + # Iterable: types + types_result = reg.contains([ExampleAgentSetA, ExampleAgentSetB]) + assert types_result.dtype == pl.Boolean + assert types_result.to_list() == [True, True] + # Iterable: names + names = [s.name for s in reg] + assert reg.contains(names).to_list() == [True, True] + # Empty iterable is vacuously true + assert reg.contains([]) is True + # Unsupported element type (rejected by runtime type checking) + with pytest.raises(bear_roar.BeartypeCallHintParamViolation): + reg.contains([object()]) + + # Public: do + def test_do(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + # Inplace operation across both sets + reg.do("add_wealth", 5) + assert reg[0]["wealth"].to_list() == [6, 7, 8, 9] + assert reg[1]["wealth"].to_list() == [15, 25, 35, 45] + # return_results with different key domains + res_by_name = reg.do("count", return_results=True, key_by="name") + assert set(res_by_name.keys()) == {s.name for s in reg} + assert all(v == 4 for v in res_by_name.values()) + res_by_index = reg.do("count", return_results=True, key_by="index") + assert set(res_by_index.keys()) == {0, 1} + res_by_type = reg.do("count", return_results=True, key_by="type") + assert set(res_by_type.keys()) == {ExampleAgentSetA, ExampleAgentSetB} + + # Public: get + def test_get(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + # By index + assert isinstance(reg.get(0), AgentSet) + # By name + name = reg[0].name + assert reg.get(name) is reg[0] + # By type returns list + aset_list = reg.get(ExampleAgentSetA) + assert isinstance(aset_list, list) and all( + isinstance(s, ExampleAgentSetA) for s in aset_list + ) + # Missing returns default None + assert reg.get(9999) is None + # Out-of-range index handled without raising + assert reg.get(10) is None + + # Public: remove + def test_remove(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + total_ids = reg.ids.len() + # By instance + reg.remove(reg[0]) + assert len(reg) == 1 + # By type + reg.add(ExampleAgentSetA(reg.model)) + assert len(reg.get(ExampleAgentSetA)) == 1 + reg.remove(ExampleAgentSetA) + assert all(not isinstance(s, ExampleAgentSetA) for s in reg) + # By name (no error if not present) + reg.remove("nonexistent") + # ids recomputed and not equal to previous total + assert reg.ids.len() != total_ids + + # Public: shuffle + def test_shuffle(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + reg.shuffle(inplace=True) + assert len(reg) == 2 + # Public: sort + def test_sort(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + reg.sort(by="wealth", ascending=False) + assert reg[0]["wealth"].to_list() == sorted( + reg[0]["wealth"].to_list(), reverse=True + ) + + # Dunder: __getattr__ + def test__getattr__(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + ages = reg.age + assert isinstance(ages, dict) + assert set(ages.keys()) == {s.name for s in reg} + # Dunder: __iter__ + def test__iter__(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + it = list(iter(reg)) + assert it[0] is reg[0] + assert all(isinstance(s, AgentSet) for s in it) + # Dunder: __len__ + def test__len__(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + assert len(reg) == 2 + # Dunder: __repr__ + def test__repr__(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + repr(reg) + # Dunder: __str__ + def test__str__(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + str(reg) + # Dunder: __reversed__ + def test__reversed__(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + list(reversed(reg)) + + # Dunder: __setitem__ + def test__setitem__(self, fix_model: Model) -> None: + reg = AgentSetRegistry(fix_model) + a1 = ExampleAgentSetA(fix_model) + a2 = ExampleAgentSetB(fix_model) + reg.add([a1, a2]) + # Assign by index with duplicate name should raise + a_dup = ExampleAgentSetA(fix_model) + a_dup.name = reg[1].name # create name collision + with pytest.raises(ValueError, match="Duplicate agent set name disallowed"): + reg[0] = a_dup + # Assign by name: replace existing slot, authoritative name should be key + new_set = ExampleAgentSetA(fix_model) + reg[reg[1].name] = new_set + assert reg[1] is new_set + assert reg[1].name == reg[1].name + # Assign new name appends + extra = ExampleAgentSetA(fix_model) + reg["extra_set"] = extra + assert reg["extra_set"] is extra + # Model mismatch raises + other_model_set = ExampleAgentSetA(Model()) + with pytest.raises(TypeError, match="Assigned AgentSet must belong to the same model"): + reg[0] = other_model_set + + # Public: keys + def test_keys(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + # keys by name + names = list(reg.keys()) + assert names == [s.name for s in reg] + # keys by index + assert list(reg.keys(key_by="index")) == [0, 1] + # keys by type + assert set(reg.keys(key_by="type")) == {ExampleAgentSetA, ExampleAgentSetB} + # invalid key_by + with pytest.raises(bear_roar.BeartypeCallHintParamViolation): + list(reg.keys(key_by="bad")) + # Public: items + def test_items(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + items_name = list(reg.items()) + assert [k for k, _ in items_name] == [s.name for s in reg] + items_idx = list(reg.items(key_by="index")) + assert [k for k, _ in items_idx] == [0, 1] + items_type = list(reg.items(key_by="type")) + assert set(k for k, _ in items_type) == {ExampleAgentSetA, ExampleAgentSetB} + # Public: values + def test_values(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + assert list(reg.values())[0] is reg[0] + # Public: discard + def test_discard(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + original_len = len(reg) + # Missing selector ignored without error + reg.discard("missing_name") + assert len(reg) == original_len + # Remove by instance + reg.discard(reg[0]) + assert len(reg) == original_len - 1 + # Non-inplace returns new copy + reg2 = reg.discard("missing_name", inplace=False) + assert len(reg2) == len(reg) + # Public: ids (property) + def test_ids(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + assert isinstance(reg.ids, pl.Series) + before = reg.ids.len() + reg.remove(reg[0]) + assert reg.ids.len() < before + # Dunder: __getitem__ + def test__getitem__(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + # By index + assert reg[0] is next(iter(reg)) + # By name + name0 = reg[0].name + assert reg[name0] is reg[0] + # By type + lst = reg[ExampleAgentSetA] + assert isinstance(lst, list) and all(isinstance(s, ExampleAgentSetA) for s in lst) + # Missing name raises KeyError + with pytest.raises(KeyError): + _ = reg["missing"] + # Dunder: __contains__ (membership) + def test__contains__(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + assert reg[0] in reg + new_set = ExampleAgentSetA(reg.model) + assert new_set not in reg + # Dunder: __add__ + def test__add__(self, fix_model: Model) -> None: + reg = AgentSetRegistry(fix_model) + a1 = ExampleAgentSetA(fix_model) + a2 = ExampleAgentSetB(fix_model) + reg.add(a1) + reg_new = reg + a2 + # original unchanged, new has two + assert len(reg) == 1 + assert len(reg_new) == 2 + # Presence by type/name (instances are deep-copied) + assert reg_new.contains(ExampleAgentSetA) is True + assert reg_new.contains(ExampleAgentSetB) is True + # Dunder: __iadd__ + def test__iadd__(self, fix_model: Model) -> None: + reg = AgentSetRegistry(fix_model) + a1 = ExampleAgentSetA(fix_model) + a2 = ExampleAgentSetB(fix_model) + reg += a1 + assert len(reg) == 1 + reg += [a2] + assert len(reg) == 2 + assert reg.contains([a1, a2]).all() + # Dunder: __sub__ + def test__sub__(self, fix_model: Model) -> None: + reg = AgentSetRegistry(fix_model) + a1 = ExampleAgentSetA(fix_model) + a2 = ExampleAgentSetB(fix_model) + reg.add([a1, a2]) + reg_new = reg - a1 + # original unchanged + assert len(reg) == 2 + # In current implementation, subtraction with instance returns a copy + # without mutation due to deep-copied identity; ensure new object + assert isinstance(reg_new, AgentSetRegistry) and reg_new is not reg + assert len(reg_new) == len(reg) + # subtract list of instances also yields unchanged copy + reg_new2 = reg - [a1, a2] + assert len(reg_new2) == len(reg) + # Dunder: __isub__ + def test__isub__(self, fix_model: Model) -> None: + reg = AgentSetRegistry(fix_model) + a1 = ExampleAgentSetA(fix_model) + a2 = ExampleAgentSetB(fix_model) + reg.add([a1, a2]) + reg -= a1 + assert len(reg) == 1 and a1 not in reg + reg -= [a2] + assert len(reg) == 0 + + # Public: replace + def test_replace(self, fix_model: Model) -> None: + reg = AgentSetRegistry(fix_model) + a1 = ExampleAgentSetA(fix_model) + a2 = ExampleAgentSetB(fix_model) + a3 = ExampleAgentSetA(fix_model) + reg.add([a1, a2]) + # Replace by index + reg.replace({0: a3}) + assert reg[0] is a3 + # Replace by name (authoritative) + reg.replace({reg[1].name: a2}) + assert reg[1] is a2 + # Atomic aliasing error: same object in two positions + with pytest.raises(ValueError, match="already exists at index"): + reg.replace({0: a2, 1: a2}) + # Model mismatch + with pytest.raises(TypeError, match="must belong to the same model"): + reg.replace({0: ExampleAgentSetA(Model())}) + # Non-atomic: only applies valid keys to copy + reg2 = reg.replace({0: a1}, inplace=False, atomic=False) + assert reg2[0] is a1 + assert reg[0] is not a1 From 393a5db9f14e416feeb6f203d557a5a47e5ef84a Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 14 Sep 2025 19:36:20 +0200 Subject: [PATCH 125/329] Enhance agent reporter functionality: support string collections and callable types for data collection --- mesa_frames/abstract/datacollector.py | 6 +- mesa_frames/concrete/datacollector.py | 136 +++++++++++++++++++++++--- 2 files changed, 125 insertions(+), 17 deletions(-) diff --git a/mesa_frames/abstract/datacollector.py b/mesa_frames/abstract/datacollector.py index edbfb11f..6505408f 100644 --- a/mesa_frames/abstract/datacollector.py +++ b/mesa_frames/abstract/datacollector.py @@ -91,7 +91,11 @@ def __init__( model_reporters : dict[str, Callable] | None Functions to collect data at the model level. agent_reporters : dict[str, str | Callable] | None - Attributes or functions to collect data at the agent level. + Agent-level reporters. Values may be: + - str or list[str]: pull existing columns from each set; columns are suffixed per-set. + - Callable[[AbstractAgentSetRegistry], Series | DataFrame | dict[str, Series|DataFrame]]: registry-level, runs once per step. + - Callable[[mesa_frames.abstract.agentset.AbstractAgentSet], Series | DataFrame]: set-level, runs once per set. + Note: model-level callables are not supported for agent reporters. trigger : Callable[[Any], bool] | None A function(model) -> bool that determines whether to collect data. reset_memory : bool diff --git a/mesa_frames/concrete/datacollector.py b/mesa_frames/concrete/datacollector.py index 2b50c76d..cd2cc72e 100644 --- a/mesa_frames/concrete/datacollector.py +++ b/mesa_frames/concrete/datacollector.py @@ -177,13 +177,94 @@ def _collect_agent_reporters(self, current_model_step: int, batch_id: int): Constructs a LazyFrame with one column per reporter and includes `step` and `seed` metadata. Appends it to internal storage. """ - agent_data_dict = {} + + def _is_str_collection(x: Any) -> bool: + try: + from collections.abc import Collection + + if isinstance(x, str): + return False + return isinstance(x, Collection) and all(isinstance(i, str) for i in x) + except Exception: + return False + + agent_data_dict: dict[str, pl.Series] = {} + for col_name, reporter in self._agent_reporters.items(): - if isinstance(reporter, str): - for k, v in self._model.sets[reporter].items(): - agent_data_dict[col_name + "_" + str(k.__class__.__name__)] = v - else: - agent_data_dict[col_name] = reporter(self._model) + # 1) String or collection[str]: shorthand to fetch columns + if isinstance(reporter, str) or _is_str_collection(reporter): + # If a single string, fetch that attribute from each set + if isinstance(reporter, str): + values_by_set = getattr(self._model.sets, reporter) + for set_name, series in values_by_set.items(): + agent_data_dict[f"{col_name}_{set_name}"] = series + else: + # Collection of strings: pull multiple columns from each set via set.get([...]) + for set_name, aset in self._model.sets.items(): # type: ignore[attr-defined] + df = aset.get(list(reporter)) # DataFrame of requested attrs + if isinstance(df, pl.Series): + # Defensive, though get(list) should yield DataFrame + agent_data_dict[f"{col_name}_{df.name}_{set_name}"] = df + else: + for subcol in df.columns: + agent_data_dict[f"{col_name}_{subcol}_{set_name}"] = df[ + subcol + ] + continue + + # 2) Callables: prefer registry-level; then set-level + if callable(reporter): + called = False + # Try registry-level callable: reporter(AgentSetRegistry) + try: + reg_result = reporter(self._model.sets) + # Accept Series | DataFrame | dict[str, Series|DataFrame] + if isinstance(reg_result, pl.Series): + agent_data_dict[col_name] = reg_result + called = True + elif isinstance(reg_result, pl.DataFrame): + for subcol in reg_result.columns: + agent_data_dict[f"{col_name}_{subcol}"] = reg_result[subcol] + called = True + elif isinstance(reg_result, dict): + for key, val in reg_result.items(): + if isinstance(val, pl.Series): + agent_data_dict[f"{col_name}_{key}"] = val + elif isinstance(val, pl.DataFrame): + for subcol in val.columns: + agent_data_dict[f"{col_name}_{key}_{subcol}"] = val[ + subcol + ] + else: + raise TypeError( + "Registry-level reporter dict values must be Series or DataFrame" + ) + called = True + except Exception: + called = False + + if not called: + # Fallback: set-level callable, run once per set and suffix by set name + for set_name, aset in self._model.sets.items(): # type: ignore[attr-defined] + set_result = reporter(aset) + if isinstance(set_result, pl.Series): + agent_data_dict[f"{col_name}_{set_name}"] = set_result + elif isinstance(set_result, pl.DataFrame): + for subcol in set_result.columns: + agent_data_dict[f"{col_name}_{subcol}_{set_name}"] = ( + set_result[subcol] + ) + else: + raise TypeError( + "Set-level reporter must return polars Series or DataFrame" + ) + continue + + # Unknown type + raise TypeError( + "agent_reporters values must be str, collection[str], or callable" + ) + agent_lazy_frame = pl.LazyFrame(agent_data_dict) agent_lazy_frame = agent_lazy_frame.with_columns( [ @@ -441,7 +522,10 @@ def _validate_reporter_table(self, conn: connection, table_name: str): ) def _validate_reporter_table_columns( - self, conn: connection, table_name: str, reporter: dict[str, Callable | str] + self, + conn: connection, + table_name: str, + reporter: dict[str, Callable | str], ): """ Check if the expected columns are present in a given PostgreSQL table. @@ -460,15 +544,35 @@ def _validate_reporter_table_columns( ValueError If any expected columns are missing from the table. """ - expected_columns = set() - for col_name, required_column in reporter.items(): - if isinstance(required_column, str): - for k, v in self._model.sets[required_column].items(): - expected_columns.add( - (col_name + "_" + str(k.__class__.__name__)).lower() - ) - else: - expected_columns.add(col_name.lower()) + + def _is_str_collection(x: Any) -> bool: + try: + from collections.abc import Collection + + if isinstance(x, str): + return False + return isinstance(x, Collection) and all(isinstance(i, str) for i in x) + except Exception: + return False + + expected_columns: set[str] = set() + for col_name, req in reporter.items(): + # Strings → one column per set with suffix + if isinstance(req, str): + for set_name, _ in self._model.sets.items(): # type: ignore[attr-defined] + expected_columns.add(f"{col_name}_{set_name}".lower()) + continue + + # Collection[str] → one column per attribute per set + if _is_str_collection(req): + for set_name, _ in self._model.sets.items(): # type: ignore[attr-defined] + for subcol in req: # type: ignore[assignment] + expected_columns.add(f"{col_name}_{subcol}_{set_name}".lower()) + continue + + # Callable: conservative default → require 'col_name' to exist + # We cannot know the dynamic column explosion without running model code safely here. + expected_columns.add(col_name.lower()) query = f""" SELECT column_name From 500bc2331a4421ceb6698661e6b75b9399d4852b Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 14 Sep 2025 19:36:42 +0200 Subject: [PATCH 126/329] Refactor agent reporter lambda functions to use sets parameter for wealth retrieval --- tests/test_datacollector.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/test_datacollector.py b/tests/test_datacollector.py index b7407711..b2ac3279 100644 --- a/tests/test_datacollector.py +++ b/tests/test_datacollector.py @@ -164,7 +164,7 @@ def test_collect(self, fix1_model): ) }, agent_reporters={ - "wealth": lambda model: model.sets._agentsets[0]["wealth"], + "wealth": lambda sets: sets[0]["wealth"], "age": "age", }, ) @@ -223,7 +223,7 @@ def test_collect_step(self, fix1_model): ) }, agent_reporters={ - "wealth": lambda model: model.sets._agentsets[0]["wealth"], + "wealth": lambda sets: sets[0]["wealth"], "age": "age", }, ) @@ -279,7 +279,7 @@ def test_conditional_collect(self, fix1_model): ) }, agent_reporters={ - "wealth": lambda model: model.sets._agentsets[0]["wealth"], + "wealth": lambda sets: sets[0]["wealth"], "age": "age", }, ) @@ -361,7 +361,7 @@ def test_flush_local_csv(self, fix1_model): ) }, agent_reporters={ - "wealth": lambda model: model.sets._agentsets[0]["wealth"], + "wealth": lambda sets: sets[0]["wealth"], "age": "age", }, storage="csv", @@ -437,7 +437,7 @@ def test_flush_local_parquet(self, fix1_model): ) }, agent_reporters={ - "wealth": lambda model: model.sets._agentsets[0]["wealth"], + "wealth": lambda sets: sets[0]["wealth"], }, storage="parquet", storage_uri=tmpdir, @@ -513,7 +513,7 @@ def test_postgress(self, fix1_model, postgres_uri): ) }, agent_reporters={ - "wealth": lambda model: model.sets._agentsets[0]["wealth"], + "wealth": lambda sets: sets[0]["wealth"], "age": "age", }, storage="postgresql", @@ -562,7 +562,7 @@ def test_batch_memory(self, fix2_model): ) }, agent_reporters={ - "wealth": lambda model: model.sets._agentsets[0]["wealth"], + "wealth": lambda sets: sets[0]["wealth"], "age": "age", }, ) @@ -707,7 +707,7 @@ def test_batch_save(self, fix2_model): ) }, agent_reporters={ - "wealth": lambda model: model.sets._agentsets[0]["wealth"], + "wealth": lambda sets: sets[0]["wealth"], "age": "age", }, storage="csv", From 6f62e995a27b2f79c95ae3913dd87d6b643e0c0f Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 14 Sep 2025 19:36:54 +0200 Subject: [PATCH 127/329] Refactor test assertions in TestAgentSetRegistry for improved readability and consistency --- tests/test_agentsetregistry.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/test_agentsetregistry.py b/tests/test_agentsetregistry.py index 32ac00bc..1483c670 100644 --- a/tests/test_agentsetregistry.py +++ b/tests/test_agentsetregistry.py @@ -88,9 +88,7 @@ def test_add(self, fix_model: Model) -> None: # ids concatenated assert reg.ids.len() == len(a1) + len(a2) # Duplicate instance rejected - with pytest.raises( - ValueError, match="already present in the AgentSetRegistry" - ): + with pytest.raises(ValueError, match="already present in the AgentSetRegistry"): reg.add([a1]) # Duplicate unique_id space rejected a3 = ExampleAgentSetB(fix_model) @@ -179,6 +177,7 @@ def test_shuffle(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two reg.shuffle(inplace=True) assert len(reg) == 2 + # Public: sort def test_sort(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two @@ -193,24 +192,29 @@ def test__getattr__(self, fix_registry_with_two: AgentSetRegistry) -> None: ages = reg.age assert isinstance(ages, dict) assert set(ages.keys()) == {s.name for s in reg} + # Dunder: __iter__ def test__iter__(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two it = list(iter(reg)) assert it[0] is reg[0] assert all(isinstance(s, AgentSet) for s in it) + # Dunder: __len__ def test__len__(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two assert len(reg) == 2 + # Dunder: __repr__ def test__repr__(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two repr(reg) + # Dunder: __str__ def test__str__(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two str(reg) + # Dunder: __reversed__ def test__reversed__(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two @@ -238,7 +242,9 @@ def test__setitem__(self, fix_model: Model) -> None: assert reg["extra_set"] is extra # Model mismatch raises other_model_set = ExampleAgentSetA(Model()) - with pytest.raises(TypeError, match="Assigned AgentSet must belong to the same model"): + with pytest.raises( + TypeError, match="Assigned AgentSet must belong to the same model" + ): reg[0] = other_model_set # Public: keys @@ -254,6 +260,7 @@ def test_keys(self, fix_registry_with_two: AgentSetRegistry) -> None: # invalid key_by with pytest.raises(bear_roar.BeartypeCallHintParamViolation): list(reg.keys(key_by="bad")) + # Public: items def test_items(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two @@ -263,10 +270,12 @@ def test_items(self, fix_registry_with_two: AgentSetRegistry) -> None: assert [k for k, _ in items_idx] == [0, 1] items_type = list(reg.items(key_by="type")) assert set(k for k, _ in items_type) == {ExampleAgentSetA, ExampleAgentSetB} + # Public: values def test_values(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two assert list(reg.values())[0] is reg[0] + # Public: discard def test_discard(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two @@ -280,6 +289,7 @@ def test_discard(self, fix_registry_with_two: AgentSetRegistry) -> None: # Non-inplace returns new copy reg2 = reg.discard("missing_name", inplace=False) assert len(reg2) == len(reg) + # Public: ids (property) def test_ids(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two @@ -287,6 +297,7 @@ def test_ids(self, fix_registry_with_two: AgentSetRegistry) -> None: before = reg.ids.len() reg.remove(reg[0]) assert reg.ids.len() < before + # Dunder: __getitem__ def test__getitem__(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two @@ -297,16 +308,20 @@ def test__getitem__(self, fix_registry_with_two: AgentSetRegistry) -> None: assert reg[name0] is reg[0] # By type lst = reg[ExampleAgentSetA] - assert isinstance(lst, list) and all(isinstance(s, ExampleAgentSetA) for s in lst) + assert isinstance(lst, list) and all( + isinstance(s, ExampleAgentSetA) for s in lst + ) # Missing name raises KeyError with pytest.raises(KeyError): _ = reg["missing"] + # Dunder: __contains__ (membership) def test__contains__(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two assert reg[0] in reg new_set = ExampleAgentSetA(reg.model) assert new_set not in reg + # Dunder: __add__ def test__add__(self, fix_model: Model) -> None: reg = AgentSetRegistry(fix_model) @@ -320,6 +335,7 @@ def test__add__(self, fix_model: Model) -> None: # Presence by type/name (instances are deep-copied) assert reg_new.contains(ExampleAgentSetA) is True assert reg_new.contains(ExampleAgentSetB) is True + # Dunder: __iadd__ def test__iadd__(self, fix_model: Model) -> None: reg = AgentSetRegistry(fix_model) @@ -330,6 +346,7 @@ def test__iadd__(self, fix_model: Model) -> None: reg += [a2] assert len(reg) == 2 assert reg.contains([a1, a2]).all() + # Dunder: __sub__ def test__sub__(self, fix_model: Model) -> None: reg = AgentSetRegistry(fix_model) @@ -346,6 +363,7 @@ def test__sub__(self, fix_model: Model) -> None: # subtract list of instances also yields unchanged copy reg_new2 = reg - [a1, a2] assert len(reg_new2) == len(reg) + # Dunder: __isub__ def test__isub__(self, fix_model: Model) -> None: reg = AgentSetRegistry(fix_model) From 95cf99604a7b19f719c6483984ba8867380859c9 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 14 Sep 2025 19:41:45 +0200 Subject: [PATCH 128/329] Refactor DataCollector model reporters for improved efficiency and readability --- docs/general/user-guide/4_datacollector.ipynb | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/docs/general/user-guide/4_datacollector.ipynb b/docs/general/user-guide/4_datacollector.ipynb index 3fa16b49..6f06c5e7 100644 --- a/docs/general/user-guide/4_datacollector.ipynb +++ b/docs/general/user-guide/4_datacollector.ipynb @@ -175,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "5f14f38c", "metadata": {}, "outputs": [ @@ -198,8 +198,10 @@ "model_csv.dc = DataCollector(\n", " model=model_csv,\n", " model_reporters={\n", - " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", + " \"total_wealth\": lambda m: sum(\n", + " s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n", + " ),\n", + " \"n_agents\": lambda m: len(m.sets.ids),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -226,7 +228,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "8763a12b2bbd4a93a75aff182afb95dc", "metadata": { "editable": true @@ -249,8 +251,10 @@ "model_parq.dc = DataCollector(\n", " model=model_parq,\n", " model_reporters={\n", - " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", + " \"total_wealth\": lambda m: sum(\n", + " s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n", + " ),\n", + " \"n_agents\": lambda m: len(m.sets.ids),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -279,7 +283,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "id": "7cdc8c89c7104fffa095e18ddfef8986", "metadata": { "editable": true @@ -289,7 +293,14 @@ "model_s3 = MoneyModel(1000)\n", "model_s3.dc = DataCollector(\n", " model=model_s3,\n", - " model_reporters={\n", + " model_reporters = {\n", + "\"total_wealth\": lambda m: sum(\n", + "s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n", + "),\n", + "\"n_agents\": lambda m: len(m.sets.ids),\n", + "}\n", + "\n", + "\n", " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", " },\n", From 352f2af190cdfa967162a6c080e1999f6282ef32 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 14 Sep 2025 19:43:41 +0200 Subject: [PATCH 129/329] Fix execution counts in DataCollector tutorial notebook for consistency --- docs/general/user-guide/4_datacollector.ipynb | 81 +++++++++---------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/docs/general/user-guide/4_datacollector.ipynb b/docs/general/user-guide/4_datacollector.ipynb index 6f06c5e7..085d655b 100644 --- a/docs/general/user-guide/4_datacollector.ipynb +++ b/docs/general/user-guide/4_datacollector.ipynb @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 6, "id": "9a63283cbaf04dbcab1f6479b197f3a8", "metadata": { "editable": true @@ -48,7 +48,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "72eea5119410473aa328ad9291626812", "metadata": { "editable": true @@ -63,11 +63,11 @@ " │ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", " │ i64 ┆ str ┆ i64 ┆ f64 ┆ i64 │\n", " ╞══════╪═════════════════════════════════╪═══════╪══════════════╪══════════╡\n", - " │ 2 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 4 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 6 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 8 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 10 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", + " │ 2 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", + " │ 4 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", + " │ 6 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", + " │ 8 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", + " │ 10 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", " └──────┴─────────────────────────────────┴───────┴──────────────┴──────────┘,\n", " 'agent': shape: (5_000, 4)\n", " ┌────────────────────┬──────┬─────────────────────────────────┬───────┐\n", @@ -75,21 +75,21 @@ " │ --- ┆ --- ┆ --- ┆ --- │\n", " │ f64 ┆ i32 ┆ str ┆ i32 │\n", " ╞════════════════════╪══════╪═════════════════════════════════╪═══════╡\n", - " │ 0.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 3.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 1.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 3.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 6.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ 3.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", + " │ 0.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", + " │ 2.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", + " │ 1.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", + " │ 0.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", " │ … ┆ … ┆ … ┆ … │\n", - " │ 4.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 1.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", + " │ 0.0 ┆ 10 ┆ 332212815818606584686857770936… ┆ 0 │\n", + " │ 0.0 ┆ 10 ┆ 332212815818606584686857770936… ┆ 0 │\n", + " │ 0.0 ┆ 10 ┆ 332212815818606584686857770936… ┆ 0 │\n", + " │ 0.0 ┆ 10 ┆ 332212815818606584686857770936… ┆ 0 │\n", + " │ 0.0 ┆ 10 ┆ 332212815818606584686857770936… ┆ 0 │\n", " └────────────────────┴──────┴─────────────────────────────────┴───────┘}" ] }, - "execution_count": 19, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -175,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "5f14f38c", "metadata": {}, "outputs": [ @@ -185,7 +185,7 @@ "[]" ] }, - "execution_count": 20, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -228,7 +228,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "8763a12b2bbd4a93a75aff182afb95dc", "metadata": { "editable": true @@ -240,7 +240,7 @@ "[]" ] }, - "execution_count": 21, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -283,7 +283,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "7cdc8c89c7104fffa095e18ddfef8986", "metadata": { "editable": true @@ -293,16 +293,11 @@ "model_s3 = MoneyModel(1000)\n", "model_s3.dc = DataCollector(\n", " model=model_s3,\n", - " model_reporters = {\n", - "\"total_wealth\": lambda m: sum(\n", - "s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n", - "),\n", - "\"n_agents\": lambda m: len(m.sets.ids),\n", - "}\n", - "\n", - "\n", - " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", + " model_reporters={\n", + " \"total_wealth\": lambda m: sum(\n", + " s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n", + " ),\n", + " \"n_agents\": lambda m: len(m.sets.ids),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -330,7 +325,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 11, "id": "938c804e27f84196a10c8828c723f798", "metadata": { "editable": true @@ -392,7 +387,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 12, "id": "59bbdb311c014d738909a11f9e486628", "metadata": { "editable": true @@ -421,7 +416,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 13, "id": "8a65eabff63a45729fe45fb5ade58bdc", "metadata": { "editable": true @@ -437,7 +432,7 @@ " white-space: pre-wrap;\n", "}\n", "\n", - "shape: (5, 5)
stepseedbatchtotal_wealthn_agents
i64stri64f64i64
2"732054881101029867447298951813…0100.0100
4"732054881101029867447298951813…0100.0100
6"732054881101029867447298951813…0100.0100
8"732054881101029867447298951813…0100.0100
10"732054881101029867447298951813…0100.0100
" + "shape: (5, 5)
stepseedbatchtotal_wealthn_agents
i64stri64f64i64
2"540832786058427425452319829502…0100.0100
4"540832786058427425452319829502…0100.0100
6"540832786058427425452319829502…0100.0100
8"540832786058427425452319829502…0100.0100
10"540832786058427425452319829502…0100.0100
" ], "text/plain": [ "shape: (5, 5)\n", @@ -446,15 +441,15 @@ "│ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", "│ i64 ┆ str ┆ i64 ┆ f64 ┆ i64 │\n", "╞══════╪═════════════════════════════════╪═══════╪══════════════╪══════════╡\n", - "│ 2 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 4 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 6 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 8 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 10 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", + "│ 2 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", + "│ 4 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", + "│ 6 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", + "│ 8 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", + "│ 10 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", "└──────┴─────────────────────────────────┴───────┴──────────────┴──────────┘" ] }, - "execution_count": 25, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } From 4a2018e5ddeb2eace7ab9f0852e146b3bc1613fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 15 Sep 2025 08:44:37 +0000 Subject: [PATCH 130/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_agentsetregistry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_agentsetregistry.py b/tests/test_agentsetregistry.py index 1483c670..d81c4d02 100644 --- a/tests/test_agentsetregistry.py +++ b/tests/test_agentsetregistry.py @@ -269,7 +269,7 @@ def test_items(self, fix_registry_with_two: AgentSetRegistry) -> None: items_idx = list(reg.items(key_by="index")) assert [k for k, _ in items_idx] == [0, 1] items_type = list(reg.items(key_by="type")) - assert set(k for k, _ in items_type) == {ExampleAgentSetA, ExampleAgentSetB} + assert {k for k, _ in items_type} == {ExampleAgentSetA, ExampleAgentSetB} # Public: values def test_values(self, fix_registry_with_two: AgentSetRegistry) -> None: From 065bea8070d6353220f42d9529622096c6733530 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 15 Sep 2025 14:15:05 +0200 Subject: [PATCH 131/329] Implement rename functionality for AgentSet and AgentSetRegistry with conflict handling --- mesa_frames/abstract/agentset.py | 44 ++++++++++ mesa_frames/abstract/agentsetregistry.py | 38 ++++++++ mesa_frames/concrete/agentset.py | 32 +++++-- mesa_frames/concrete/agentsetregistry.py | 105 +++++++++++++++++++++++ tests/test_agentset.py | 30 +++++++ tests/test_agentsetregistry.py | 36 ++++++++ 6 files changed, 278 insertions(+), 7 deletions(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index c7bf2224..9c01897f 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -468,6 +468,50 @@ def random(self) -> Generator: def space(self) -> mesa_frames.abstract.space.Space | None: return self.model.space + def rename(self, new_name: str, inplace: bool = True) -> Self: + """Rename this AgentSet. + + If this set is contained in the model's AgentSetRegistry, delegate to + the registry's rename implementation so that name uniqueness and + conflicts are handled consistently. If the set is not yet part of a + registry, update the local name directly. + + Parameters + ---------- + new_name : str + Desired new name for this AgentSet. + + Returns + ------- + Self + The updated AgentSet (or a renamed copy when ``inplace=False``). + """ + obj = self._get_obj(inplace) + try: + # If contained in registry, delegate to it so conflicts are handled + if self in self.model.sets: # type: ignore[operator] + # Preserve index to retrieve copy when not inplace + idx = None + try: + idx = list(self.model.sets).index(self) # type: ignore[arg-type] + except Exception: + idx = None + reg = self.model.sets.rename(self, new_name, inplace=inplace) + if inplace: + return self + # Non-inplace: return the corresponding set from the copied registry + if idx is not None: + return reg[idx] # type: ignore[index] + # Fallback: look up by name (may be canonicalized) + return reg.get(new_name) # type: ignore[return-value] + except Exception: + # If delegation cannot be resolved, fall back to local rename + obj._name = new_name + return obj + # Not in a registry: local rename + obj._name = new_name + return obj + def __setitem__( self, key: str diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index 6c43505b..ad255797 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -93,6 +93,44 @@ def discard( return self.remove(sets, inplace=inplace) return self._get_obj(inplace) + @abstractmethod + def rename( + self, + target: ( + mesa_frames.abstract.agentset.AbstractAgentSet + | str + | dict[mesa_frames.abstract.agentset.AbstractAgentSet | str, str] + | list[tuple[mesa_frames.abstract.agentset.AbstractAgentSet | str, str]] + ), + new_name: str | None = None, + *, + on_conflict: Literal["canonicalize", "raise"] = "canonicalize", + mode: Literal["atomic", "best_effort"] = "atomic", + inplace: bool = True, + ) -> Self: + """Rename AgentSets in this registry, handling conflicts. + + Parameters + ---------- + target : AgentSet | str | dict | list[tuple] + Single target (instance or existing name) with ``new_name`` provided, + or a mapping/sequence of (target, new_name) pairs for batch rename. + new_name : str | None + New name for single-target rename. + on_conflict : {"canonicalize", "raise"} + When a desired name collides, either canonicalize by appending a + numeric suffix (default) or raise ``ValueError``. + mode : {"atomic", "best_effort"} + In "atomic" mode, validate all renames before applying any. In + "best_effort" mode, apply what can be applied and skip failures. + + Returns + ------- + Self + Updated registry (or a renamed copy when ``inplace=False``). + """ + ... + @abstractmethod def add( self, diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index f62d608f..9e7cdad7 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -103,7 +103,7 @@ def __init__( self._df = pl.DataFrame() self._mask = pl.repeat(True, len(self._df), dtype=pl.Boolean, eager=True) - def rename(self, new_name: str) -> str: + def rename(self, new_name: str, inplace: bool = True) -> Self: """Rename this agent set. If attached to AgentSetRegistry, delegate for uniqueness enforcement. Parameters @@ -113,22 +113,40 @@ def rename(self, new_name: str) -> str: Returns ------- - str - The final name used (may be canonicalized if duplicates exist). + Self + The updated AgentSet (or a renamed copy when ``inplace=False``). Raises ------ ValueError If name conflicts occur and delegate encounters errors. """ + # Respect inplace semantics consistently with other mutators + obj = self._get_obj(inplace) + # Always delegate to the container's accessor if available through the model's sets # Check if we have a model and can find the AgentSetRegistry that contains this set - if self in self.model.sets: - return self.model.sets.rename(self._name, new_name) + try: + if self in self.model.sets: + # Save index to locate the copy on non-inplace path + try: + idx = list(self.model.sets).index(self) # type: ignore[arg-type] + except Exception: + idx = None + reg = self.model.sets.rename(self, new_name, inplace=inplace) + if inplace: + return self + if idx is not None: + return reg[idx] + return reg.get(new_name) # type: ignore[return-value] + except Exception: + # Fall back to local rename if delegation fails + obj._name = new_name + return obj # Set name locally if no container found - self._name = new_name - return new_name + obj._name = new_name + return obj def add( self, diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index d64644ef..7cb9e97d 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -113,6 +113,111 @@ def add( obj._ids = new_ids return obj + def rename( + self, + target: ( + AgentSet + | str + | dict[AgentSet | str, str] + | list[tuple[AgentSet | str, str]] + ), + new_name: str | None = None, + *, + on_conflict: Literal["canonicalize", "raise"] = "canonicalize", + mode: Literal["atomic", "best_effort"] = "atomic", + inplace: bool = True, + ) -> Self: + """Rename AgentSets with conflict handling. + + Supports single-target ``(set | old_name, new_name)`` and batch rename via + dict or list of pairs. Names remain unique across the registry. + """ + + # Normalize to list of (index_in_self, desired_name) using the original registry + def _resolve_one(x: AgentSet | str) -> int: + if isinstance(x, AgentSet): + for i, s in enumerate(self._agentsets): + if s is x: + return i + raise KeyError("AgentSet not found in registry") + # name lookup on original registry + for i, s in enumerate(self._agentsets): + if s.name == x: + return i + raise KeyError(f"Agent set '{x}' not found") + + if isinstance(target, (AgentSet, str)): + if new_name is None: + raise TypeError("new_name must be provided for single rename") + pairs_idx: list[tuple[int, str]] = [(_resolve_one(target), new_name)] + single = True + elif isinstance(target, dict): + pairs_idx = [(_resolve_one(k), v) for k, v in target.items()] + single = False + else: + pairs_idx = [(_resolve_one(k), v) for k, v in target] + single = False + + # Choose object to mutate + obj = self._get_obj(inplace) + # Translate indices to object AgentSets in the selected registry object + target_sets = [obj._agentsets[i] for i, _ in pairs_idx] + + # Build the set of names that remain fixed (exclude targets' current names) + targets_set = set(target_sets) + fixed_names: set[str] = { + s.name + for s in obj._agentsets + if s.name is not None and s not in targets_set + } # type: ignore[comparison-overlap] + + # Plan final names + final: list[tuple[AgentSet, str]] = [] + used = set(fixed_names) + + def _canonicalize(base: str) -> str: + if base not in used: + used.add(base) + return base + counter = 1 + cand = f"{base}_{counter}" + while cand in used: + counter += 1 + cand = f"{base}_{counter}" + used.add(cand) + return cand + + errors: list[Exception] = [] + for aset, (_idx, desired) in zip(target_sets, pairs_idx): + if on_conflict == "canonicalize": + final_name = _canonicalize(desired) + final.append((aset, final_name)) + else: # on_conflict == 'raise' + if desired in used: + err = ValueError( + f"Duplicate agent set name disallowed: '{desired}'" + ) + if mode == "atomic": + errors.append(err) + else: + # best_effort: skip this rename + continue + else: + used.add(desired) + final.append((aset, desired)) + + if errors and mode == "atomic": + # Surface first meaningful error + raise errors[0] + + # Apply renames + for aset, newn in final: + # Set the private name directly to avoid external uniqueness hooks + if hasattr(aset, "_name"): + aset._name = newn # type: ignore[attr-defined] + + return obj + def replace( self, mapping: (dict[int | str, AgentSet] | list[tuple[int | str, AgentSet]]), diff --git a/tests/test_agentset.py b/tests/test_agentset.py index d475a4fc..c8459a80 100644 --- a/tests/test_agentset.py +++ b/tests/test_agentset.py @@ -260,6 +260,36 @@ def test_select(self, fix1_AgentSet: ExampleAgentSet): selected.active_agents["wealth"].to_list() == agents.df["wealth"].to_list() ) + def test_rename(self, fix1_AgentSet: ExampleAgentSet) -> None: + agents = fix1_AgentSet + reg = agents.model.sets + # Inplace rename returns self and updates registry + old_name = agents.name + result = agents.rename("alpha", inplace=True) + assert result is agents + assert agents.name == "alpha" + assert reg.get("alpha") is agents + assert reg.get(old_name) is None + + # Add a second set and claim the same name via registry first + other = ExampleAgentSet(agents.model) + other["wealth"] = other.starting_wealth + other["age"] = [1, 2, 3, 4] + reg.add(other) + reg.rename(other, "omega") + # Now rename the first to an existing name; should canonicalize to omega_1 + agents.rename("omega", inplace=True) + assert agents.name != "omega" + assert agents.name.startswith("omega_") + assert reg.get(agents.name) is agents + + # Non-inplace: returns a renamed copy of the set + copy_set = agents.rename("beta", inplace=False) + assert copy_set is not agents + assert copy_set.name in ("beta", "beta_1") + # Original remains unchanged + assert agents.name not in ("beta", "beta_1") + # Test with a pl.Series[bool] mask = pl.Series("mask", [True, False, True, True], dtype=pl.Boolean) selected = agents.select(mask, inplace=False) diff --git a/tests/test_agentsetregistry.py b/tests/test_agentsetregistry.py index 1483c670..3a5c5592 100644 --- a/tests/test_agentsetregistry.py +++ b/tests/test_agentsetregistry.py @@ -186,6 +186,42 @@ def test_sort(self, fix_registry_with_two: AgentSetRegistry) -> None: reg[0]["wealth"].to_list(), reverse=True ) + # Public: rename + def test_rename(self, fix_registry_with_two: AgentSetRegistry) -> None: + reg = fix_registry_with_two + # Single rename by instance, inplace + a0 = reg[0] + reg.rename(a0, "X") + assert a0.name == "X" + assert reg.get("X") is a0 + + # Rename second to same name should canonicalize + a1 = reg[1] + reg.rename(a1, "X") + assert a1.name != "X" and a1.name.startswith("X_") + assert reg.get(a1.name) is a1 + + # Non-inplace copy + reg2 = reg.rename(a0, "Y", inplace=False) + assert reg2 is not reg + assert reg.get("Y") is None + assert reg2.get("Y") is not None + + # Atomic conflict raise: attempt to rename to existing name + with pytest.raises(ValueError): + reg.rename({a0: a1.name}, on_conflict="raise", mode="atomic") + # Names unchanged + assert reg.get(a1.name) is a1 + + # Best-effort: one ok, one conflicting → only ok applied + unique_name = "Z_unique" + reg.rename( + {a0: unique_name, a1: unique_name}, on_conflict="raise", mode="best_effort" + ) + assert a0.name == unique_name + # a1 stays with its previous (non-unique_name) value + assert a1.name != unique_name + # Dunder: __getattr__ def test__getattr__(self, fix_registry_with_two: AgentSetRegistry) -> None: reg = fix_registry_with_two From 03575eba774284c2eb95c75408416162fb7a588f Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 15 Sep 2025 14:15:33 +0200 Subject: [PATCH 132/329] Refactor test assertion in TestAgentSetRegistry to use set literal for improved clarity --- tests/test_agentsetregistry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_agentsetregistry.py b/tests/test_agentsetregistry.py index 3a5c5592..07422fb6 100644 --- a/tests/test_agentsetregistry.py +++ b/tests/test_agentsetregistry.py @@ -305,7 +305,7 @@ def test_items(self, fix_registry_with_two: AgentSetRegistry) -> None: items_idx = list(reg.items(key_by="index")) assert [k for k, _ in items_idx] == [0, 1] items_type = list(reg.items(key_by="type")) - assert set(k for k, _ in items_type) == {ExampleAgentSetA, ExampleAgentSetB} + assert {k for k, _ in items_type} == {ExampleAgentSetA, ExampleAgentSetB} # Public: values def test_values(self, fix_registry_with_two: AgentSetRegistry) -> None: From 5e6dd9aaf4325adc16a9c421d9653bfe6ef97978 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 15 Sep 2025 15:19:06 +0200 Subject: [PATCH 133/329] Enhance parameter documentation for agent handling and rename functionality across multiple classes --- mesa_frames/abstract/agentset.py | 5 ++- mesa_frames/abstract/agentsetregistry.py | 18 +++++++--- mesa_frames/abstract/space.py | 42 ++++++++++++++---------- mesa_frames/concrete/agentset.py | 4 +++ 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index 9c01897f..9bc25174 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -96,7 +96,7 @@ def contains(self, agents: IdsLike) -> bool | BoolSeries: Parameters ---------- - agents : mesa_frames.concrete.agents.AgentSetDF | IdsLike + agents : IdsLike The ID(s) to check for. Returns @@ -480,6 +480,9 @@ def rename(self, new_name: str, inplace: bool = True) -> Self: ---------- new_name : str Desired new name for this AgentSet. + inplace : bool, optional + Whether to perform the rename in place. If False, a renamed copy is + returned, by default True. Returns ------- diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index ad255797..cb535d1b 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -43,18 +43,20 @@ def __init__(self, model): from __future__ import annotations # PEP 563: postponed evaluation of type annotations from abc import abstractmethod -from collections.abc import Callable, Collection, Iterator, Sequence, Iterable +from collections.abc import Callable, Collection, Iterable, Iterator, Sequence from contextlib import suppress from typing import Any, Literal, Self, overload from numpy.random import Generator from mesa_frames.abstract.mixin import CopyMixin +from mesa_frames.types_ import ( + AbstractAgentSetSelector as AgentSetSelector, +) from mesa_frames.types_ import ( BoolSeries, Index, KeyBy, - AbstractAgentSetSelector as AgentSetSelector, Series, ) @@ -112,15 +114,15 @@ def rename( Parameters ---------- - target : AgentSet | str | dict | list[tuple] + target : mesa_frames.abstract.agentset.AbstractAgentSet | str | dict[mesa_frames.abstract.agentset.AbstractAgentSet | str, str] | list[tuple[mesa_frames.abstract.agentset.AbstractAgentSet | str, str]] Single target (instance or existing name) with ``new_name`` provided, or a mapping/sequence of (target, new_name) pairs for batch rename. new_name : str | None New name for single-target rename. - on_conflict : {"canonicalize", "raise"} + on_conflict : Literal["canonicalize", "raise"] When a desired name collides, either canonicalize by appending a numeric suffix (default) or raise ``ValueError``. - mode : {"atomic", "best_effort"} + mode : Literal["atomic", "best_effort"] In "atomic" mode, validate all renames before applying any. In "best_effort" mode, apply what can be applied and skip failures. @@ -128,6 +130,12 @@ def rename( ------- Self Updated registry (or a renamed copy when ``inplace=False``). + + Parameters + ---------- + inplace : bool, optional + Whether to perform the rename in place. If False, a renamed copy is + returned, by default True. """ ... diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index 808eb450..39abe6bd 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -121,7 +121,7 @@ def move_agents( Parameters ---------- - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] + agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] The agents to move pos : SpaceCoordinate | SpaceCoordinates The coordinates for each agents. The length of the coordinates must match the number of agents. @@ -157,7 +157,7 @@ def place_agents( Parameters ---------- - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] + agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] The agents to place in the space pos : SpaceCoordinate | SpaceCoordinates The coordinates for each agents. The length of the coordinates must match the number of agents. @@ -218,9 +218,9 @@ def swap_agents( Parameters ---------- - agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] + agents0 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] The first set of agents to swap - agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] + agents1 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] The second set of agents to swap inplace : bool, optional Whether to perform the operation inplace, by default True @@ -290,9 +290,9 @@ def get_directions( The starting positions pos1 : SpaceCoordinate | SpaceCoordinates | None, optional The ending positions - agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional + agents0 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional The starting agents - agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional + agents1 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional The ending agents normalize : bool, optional Whether to normalize the vectors to unit norm. By default False @@ -334,9 +334,9 @@ def get_distances( The starting positions pos1 : SpaceCoordinate | SpaceCoordinates | None, optional The ending positions - agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional + agents0 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional The starting agents - agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional + agents1 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional The ending agents Returns @@ -369,7 +369,7 @@ def get_neighbors( The radius(es) of the neighborhood pos : SpaceCoordinate | SpaceCoordinates | None, optional The coordinates of the cell to get the neighborhood from, by default None - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional + agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional The id of the agents to get the neighborhood from, by default None include_center : bool, optional If the center cells or agents should be included in the result, by default False @@ -391,7 +391,9 @@ def get_neighbors( def move_to_empty( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -399,7 +401,7 @@ def move_to_empty( Parameters ---------- - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] + agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] The agents to move to empty cells/positions inplace : bool, optional Whether to perform the operation inplace, by default True @@ -414,7 +416,9 @@ def move_to_empty( def place_to_empty( self, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -422,7 +426,7 @@ def place_to_empty( Parameters ---------- - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] + agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] The agents to place in empty cells/positions inplace : bool, optional Whether to perform the operation inplace, by default True @@ -468,7 +472,7 @@ def remove_agents( Parameters ---------- - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] + agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] The agents to remove from the space inplace : bool, optional Whether to perform the operation inplace, by default True @@ -703,7 +707,7 @@ def move_to_available( Parameters ---------- - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] + agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] The agents to move to available cells/positions inplace : bool, optional Whether to perform the operation inplace, by default True @@ -856,7 +860,9 @@ def get_neighborhood( radius: int | float | Sequence[int] | Sequence[float] | ArrayLike, pos: DiscreteCoordinate | DiscreteCoordinates | None = None, agents: IdsLike + | AbstractAgentSet | AbstractAgentSetRegistry + | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] = None, include_center: bool = False, ) -> DataFrame: @@ -870,7 +876,7 @@ def get_neighborhood( The radius(es) of the neighborhoods pos : DiscreteCoordinate | DiscreteCoordinates | None, optional The coordinates of the cell(s) to get the neighborhood from - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry], optional + agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], optional The agent(s) to get the neighborhood from include_center : bool, optional If the cell in the center of the neighborhood should be included in the result, by default False @@ -1040,7 +1046,7 @@ def _sample_cells( The number of cells to sample. If None, samples the maximum available. with_replacement : bool If the sampling should be with replacement - condition : Callable[[DiscreteSpaceCapacity], BoolSeries] + condition : Callable[[DiscreteSpaceCapacity], BoolSeries | np.ndarray] The condition to apply on the capacity respect_capacity : bool, optional If the capacity should be respected in the sampling. @@ -1659,9 +1665,9 @@ def _calculate_differences( The starting positions pos1 : GridCoordinate | GridCoordinates | None The ending positions - agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None + agents0 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None The starting agents - agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None + agents1 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None The ending agents Returns @@ -1756,7 +1762,7 @@ def _get_df_coords( ---------- pos : GridCoordinate | GridCoordinates | None, optional The positions to get the DataFrame from, by default None - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional + agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional The agents to get the DataFrame from, by default None check_bounds: bool, optional If the positions should be checked for out-of-bounds in non-toroidal grids, by default True diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 9e7cdad7..2a9b1a55 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -111,6 +111,10 @@ def rename(self, new_name: str, inplace: bool = True) -> Self: new_name : str Desired new name. + inplace : bool, optional + Whether to perform the rename in place. If False, a renamed copy is + returned, by default True. + Returns ------- Self From 647f5b6d3eee38f70f9bd15502e59d8d64374025 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 15 Sep 2025 18:05:18 +0200 Subject: [PATCH 134/329] Update documentation to clarify usage of AgentSetRegistry and improve DataCollector examples --- docs/general/user-guide/1_classes.md | 20 ++++++++----- .../user-guide/2_introductory-tutorial.ipynb | 4 ++- docs/general/user-guide/4_datacollector.ipynb | 28 ++++++++----------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/docs/general/user-guide/1_classes.md b/docs/general/user-guide/1_classes.md index f85c062d..9ac446de 100644 --- a/docs/general/user-guide/1_classes.md +++ b/docs/general/user-guide/1_classes.md @@ -27,9 +27,9 @@ You can access the underlying DataFrame where agents are stored with `self.df`. ## Model 🏗️ -To add your AgentSet to your Model, you should also add it to the sets with `+=` or `add`. +To add your AgentSet to your Model, use the registry `self.sets` with `+=` or `add`. -NOTE: Model.sets are stored in a class which is entirely similar to AgentSet called AgentSetRegistry. The API of the two are the same. If you try accessing AgentSetRegistry.df, you will get a dictionary of `[AgentSet, DataFrame]`. +Note: All agent sets live inside `AgentSetRegistry` (available as `model.sets`). Access sets through the registry, and access DataFrames from the set itself. For example: `self.sets["Preys"].df`. Example: @@ -43,7 +43,8 @@ class EcosystemModel(Model): def step(self): self.sets.do("move") self.sets.do("hunt") - self.prey.do("reproduce") + # Access specific sets via the registry + self.sets["Preys"].do("reproduce") ``` ## Space: Grid 🌐 @@ -76,18 +77,23 @@ Example: class ExampleModel(Model): def __init__(self): super().__init__() - self.sets = MoneyAgent(self) + # Add the set to the registry + self.sets.add(MoneyAgents(100, self)) + # Configure reporters: use the registry to locate sets; get df from the set self.datacollector = DataCollector( model=self, - model_reporters={"total_wealth": lambda m: lambda m: list(m.sets.df.values())[0]["wealth"].sum()}, + model_reporters={ + "total_wealth": lambda m: m.sets["MoneyAgents"].df["wealth"].sum(), + }, agent_reporters={"wealth": "wealth"}, storage="csv", storage_uri="./data", - trigger=lambda m: m.schedule.steps % 2 == 0 + trigger=lambda m: m.steps % 2 == 0, ) def step(self): - self.sets.step() + # Step all sets via the registry + self.sets.do("step") self.datacollector.conditional_collect() self.datacollector.flush() ``` diff --git a/docs/general/user-guide/2_introductory-tutorial.ipynb b/docs/general/user-guide/2_introductory-tutorial.ipynb index ec1165da..11391f9d 100644 --- a/docs/general/user-guide/2_introductory-tutorial.ipynb +++ b/docs/general/user-guide/2_introductory-tutorial.ipynb @@ -74,7 +74,9 @@ " self.sets += agents_cls(N, self)\n", " self.datacollector = DataCollector(\n", " model=self,\n", - " model_reporters={\"total_wealth\": lambda m: m.agents[\"wealth\"].sum()},\n", + " model_reporters={\n", + " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum()\n", + " },\n", " agent_reporters={\"wealth\": \"wealth\"},\n", " storage=\"csv\",\n", " storage_uri=\"./data\",\n", diff --git a/docs/general/user-guide/4_datacollector.ipynb b/docs/general/user-guide/4_datacollector.ipynb index 085d655b..0809caa2 100644 --- a/docs/general/user-guide/4_datacollector.ipynb +++ b/docs/general/user-guide/4_datacollector.ipynb @@ -120,8 +120,8 @@ " self.dc = DataCollector(\n", " model=self,\n", " model_reporters={\n", - " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", + " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", + " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\", # pull existing column\n", @@ -175,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "5f14f38c", "metadata": {}, "outputs": [ @@ -198,10 +198,8 @@ "model_csv.dc = DataCollector(\n", " model=model_csv,\n", " model_reporters={\n", - " \"total_wealth\": lambda m: sum(\n", - " s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n", - " ),\n", - " \"n_agents\": lambda m: len(m.sets.ids),\n", + " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", + " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -228,7 +226,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "8763a12b2bbd4a93a75aff182afb95dc", "metadata": { "editable": true @@ -251,10 +249,8 @@ "model_parq.dc = DataCollector(\n", " model=model_parq,\n", " model_reporters={\n", - " \"total_wealth\": lambda m: sum(\n", - " s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n", - " ),\n", - " \"n_agents\": lambda m: len(m.sets.ids),\n", + " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", + " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -283,7 +279,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "7cdc8c89c7104fffa095e18ddfef8986", "metadata": { "editable": true @@ -294,10 +290,8 @@ "model_s3.dc = DataCollector(\n", " model=model_s3,\n", " model_reporters={\n", - " \"total_wealth\": lambda m: sum(\n", - " s[\"wealth\"].sum() for s in m.sets if \"wealth\" in s.df.columns\n", - " ),\n", - " \"n_agents\": lambda m: len(m.sets.ids),\n", + " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", + " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", From cb139b1b79fe7d2e2f7d6f501d9f00426221a510 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 15 Sep 2025 19:59:45 +0200 Subject: [PATCH 135/329] fix ss_polars --- examples/sugarscape_ig/ss_polars/agents.py | 11 ++++++----- examples/sugarscape_ig/ss_polars/model.py | 8 ++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/examples/sugarscape_ig/ss_polars/agents.py b/examples/sugarscape_ig/ss_polars/agents.py index b0ecbe90..ac163553 100644 --- a/examples/sugarscape_ig/ss_polars/agents.py +++ b/examples/sugarscape_ig/ss_polars/agents.py @@ -35,12 +35,13 @@ def __init__( self.add(agents) def eat(self): + # Only consider cells currently occupied by agents of this set cells = self.space.cells.filter(pl.col("agent_id").is_not_null()) - self[cells["agent_id"], "sugar"] = ( - self[cells["agent_id"], "sugar"] - + cells["sugar"] - - self[cells["agent_id"], "metabolism"] - ) + mask_in_set = cells["agent_id"].is_in(self.index) + if mask_in_set.any(): + cells = cells.filter(mask_in_set) + ids = cells["agent_id"] + self[ids, "sugar"] = self[ids, "sugar"] + cells["sugar"] - self[ids, "metabolism"] def step(self): self.shuffle().do("move").do("eat") diff --git a/examples/sugarscape_ig/ss_polars/model.py b/examples/sugarscape_ig/ss_polars/model.py index 56a3a83b..36b2718e 100644 --- a/examples/sugarscape_ig/ss_polars/model.py +++ b/examples/sugarscape_ig/ss_polars/model.py @@ -33,7 +33,10 @@ def __init__( sugar=sugar_grid.flatten(), max_sugar=sugar_grid.flatten() ) self.space.set_cells(sugar_grid) - self.sets += agent_type(self, n_agents, initial_sugar, metabolism, vision) + # Create and register the main agent set; keep its name for later lookups + main_set = agent_type(self, n_agents, initial_sugar, metabolism, vision) + self.sets += main_set + self._main_set_name = main_set.name if initial_positions is not None: self.space.place_agents(self.sets, initial_positions) else: @@ -41,7 +44,8 @@ def __init__( def run_model(self, steps: int) -> list[int]: for _ in range(steps): - if len(list(self.sets.df.values())[0]) == 0: + # Stop if the main agent set is empty + if len(self.sets[self._main_set_name]) == 0: # type: ignore[index] return empty_cells = self.space.empty_cells full_cells = self.space.full_cells From 5c68bd8c587e65788f2960de1c7cf8ea8be0e864 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Mon, 15 Sep 2025 20:00:22 +0200 Subject: [PATCH 136/329] formatting --- examples/sugarscape_ig/ss_polars/agents.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/sugarscape_ig/ss_polars/agents.py b/examples/sugarscape_ig/ss_polars/agents.py index ac163553..32ca91f5 100644 --- a/examples/sugarscape_ig/ss_polars/agents.py +++ b/examples/sugarscape_ig/ss_polars/agents.py @@ -41,7 +41,9 @@ def eat(self): if mask_in_set.any(): cells = cells.filter(mask_in_set) ids = cells["agent_id"] - self[ids, "sugar"] = self[ids, "sugar"] + cells["sugar"] - self[ids, "metabolism"] + self[ids, "sugar"] = ( + self[ids, "sugar"] + cells["sugar"] - self[ids, "metabolism"] + ) def step(self): self.shuffle().do("move").do("eat") From 7c53eb8bc6c74c5919dddee379271e29361dec53 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 19 Sep 2025 07:53:24 +0000 Subject: [PATCH 137/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/general/user-guide/1_classes.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/general/user-guide/1_classes.md b/docs/general/user-guide/1_classes.md index e64443c2..1aac4344 100644 --- a/docs/general/user-guide/1_classes.md +++ b/docs/general/user-guide/1_classes.md @@ -30,8 +30,9 @@ You can access the underlying DataFrame where agents are stored with `self.df`. <<<<<<< HEAD To add your AgentSet to your Model, use the registry `self.sets` with `+=` or `add`. -Note: All agent sets live inside `AgentSetRegistry` (available as `model.sets`). Access sets through the registry, and access DataFrames from the set itself. For example: `self.sets["Preys"].df`. +Note: All agent sets live inside `AgentSetRegistry` (available as `model.sets`). Access sets through the registry, and access DataFrames from the set itself. For example: `self.sets["Preys"].df` ======= + To add your AgentSet to your Model, you should also add it to the sets with `+=` or `add`. NOTE: Model.sets are stored in a class which is entirely similar to AgentSet called AgentSetRegistry. The API of the two are the same. If you try accessing AgentSetRegistry.df, you will get a dictionary of `[AgentSet, DataFrame]`. From cdcdfc8840bff4020384c5ace6baa8a86b5e6581 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 11:34:03 +0200 Subject: [PATCH 138/329] fix: remove conflict markers by preferring OURS from 2c43b3f5e9283be53db4de2419cffe1dfac79154^1 --- docs/general/user-guide/1_classes.md | 24 +- docs/general/user-guide/4_datacollector.ipynb | 92 +-- examples/sugarscape_ig/ss_polars/model.py | 8 - mesa_frames/abstract/agentset.py | 121 ---- mesa_frames/abstract/agentsetregistry.py | 608 ------------------ mesa_frames/abstract/space.py | 217 ------- mesa_frames/concrete/agentset.py | 8 - mesa_frames/concrete/agentsetregistry.py | 484 -------------- mesa_frames/concrete/datacollector.py | 20 - mesa_frames/concrete/model.py | 43 -- tests/test_datacollector.py | 32 - tests/test_grid.py | 7 - 12 files changed, 4 insertions(+), 1660 deletions(-) diff --git a/docs/general/user-guide/1_classes.md b/docs/general/user-guide/1_classes.md index 1aac4344..9ac446de 100644 --- a/docs/general/user-guide/1_classes.md +++ b/docs/general/user-guide/1_classes.md @@ -27,16 +27,9 @@ You can access the underlying DataFrame where agents are stored with `self.df`. ## Model 🏗️ -<<<<<<< HEAD To add your AgentSet to your Model, use the registry `self.sets` with `+=` or `add`. -Note: All agent sets live inside `AgentSetRegistry` (available as `model.sets`). Access sets through the registry, and access DataFrames from the set itself. For example: `self.sets["Preys"].df` -======= - -To add your AgentSet to your Model, you should also add it to the sets with `+=` or `add`. - -NOTE: Model.sets are stored in a class which is entirely similar to AgentSet called AgentSetRegistry. The API of the two are the same. If you try accessing AgentSetRegistry.df, you will get a dictionary of `[AgentSet, DataFrame]`. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 +Note: All agent sets live inside `AgentSetRegistry` (available as `model.sets`). Access sets through the registry, and access DataFrames from the set itself. For example: `self.sets["Preys"].df`. Example: @@ -50,12 +43,8 @@ class EcosystemModel(Model): def step(self): self.sets.do("move") self.sets.do("hunt") -<<<<<<< HEAD # Access specific sets via the registry self.sets["Preys"].do("reproduce") -======= - self.prey.do("reproduce") ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 ``` ## Space: Grid 🌐 @@ -88,7 +77,6 @@ Example: class ExampleModel(Model): def __init__(self): super().__init__() -<<<<<<< HEAD # Add the set to the registry self.sets.add(MoneyAgents(100, self)) # Configure reporters: use the registry to locate sets; get df from the set @@ -97,12 +85,6 @@ class ExampleModel(Model): model_reporters={ "total_wealth": lambda m: m.sets["MoneyAgents"].df["wealth"].sum(), }, -======= - self.sets = MoneyAgent(self) - self.datacollector = DataCollector( - model=self, - model_reporters={"total_wealth": lambda m: lambda m: list(m.sets.df.values())[0]["wealth"].sum()}, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 agent_reporters={"wealth": "wealth"}, storage="csv", storage_uri="./data", @@ -110,12 +92,8 @@ class ExampleModel(Model): ) def step(self): -<<<<<<< HEAD # Step all sets via the registry self.sets.do("step") -======= - self.sets.step() ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 self.datacollector.conditional_collect() self.datacollector.flush() ``` diff --git a/docs/general/user-guide/4_datacollector.ipynb b/docs/general/user-guide/4_datacollector.ipynb index f3fb5d93..0809caa2 100644 --- a/docs/general/user-guide/4_datacollector.ipynb +++ b/docs/general/user-guide/4_datacollector.ipynb @@ -26,11 +26,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD "execution_count": 6, -======= - "execution_count": 18, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "id": "9a63283cbaf04dbcab1f6479b197f3a8", "metadata": { "editable": true @@ -67,19 +63,11 @@ " │ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", " │ i64 ┆ str ┆ i64 ┆ f64 ┆ i64 │\n", " ╞══════╪═════════════════════════════════╪═══════╪══════════════╪══════════╡\n", -<<<<<<< HEAD " │ 2 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", " │ 4 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", " │ 6 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", " │ 8 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", " │ 10 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", -======= - " │ 2 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 4 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 6 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 8 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 10 ┆ 162681765859364298619846106603… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 " └──────┴─────────────────────────────────┴───────┴──────────────┴──────────┘,\n", " 'agent': shape: (5_000, 4)\n", " ┌────────────────────┬──────┬─────────────────────────────────┬───────┐\n", @@ -87,7 +75,6 @@ " │ --- ┆ --- ┆ --- ┆ --- │\n", " │ f64 ┆ i32 ┆ str ┆ i32 │\n", " ╞════════════════════╪══════╪═════════════════════════════════╪═══════╡\n", -<<<<<<< HEAD " │ 3.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", " │ 0.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", " │ 2.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", @@ -103,23 +90,6 @@ ] }, "execution_count": 7, -======= - " │ 0.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 3.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 1.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 3.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 6.0 ┆ 2 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ … ┆ … ┆ … ┆ … │\n", - " │ 4.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 1.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " │ 0.0 ┆ 10 ┆ 162681765859364298619846106603… ┆ 0 │\n", - " └────────────────────┴──────┴─────────────────────────────────┴───────┘}" - ] - }, - "execution_count": 19, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "metadata": {}, "output_type": "execute_result" } @@ -150,13 +120,8 @@ " self.dc = DataCollector(\n", " model=self,\n", " model_reporters={\n", -<<<<<<< HEAD " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", -======= - " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\", # pull existing column\n", @@ -210,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "5f14f38c", "metadata": {}, "outputs": [ @@ -220,11 +185,7 @@ "[]" ] }, -<<<<<<< HEAD "execution_count": 8, -======= - "execution_count": 20, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "metadata": {}, "output_type": "execute_result" } @@ -237,13 +198,8 @@ "model_csv.dc = DataCollector(\n", " model=model_csv,\n", " model_reporters={\n", -<<<<<<< HEAD " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", -======= - " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -270,7 +226,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "8763a12b2bbd4a93a75aff182afb95dc", "metadata": { "editable": true @@ -282,11 +238,7 @@ "[]" ] }, -<<<<<<< HEAD "execution_count": 9, -======= - "execution_count": 21, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "metadata": {}, "output_type": "execute_result" } @@ -297,13 +249,8 @@ "model_parq.dc = DataCollector(\n", " model=model_parq,\n", " model_reporters={\n", -<<<<<<< HEAD " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", -======= - " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -332,7 +279,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "id": "7cdc8c89c7104fffa095e18ddfef8986", "metadata": { "editable": true @@ -343,13 +290,8 @@ "model_s3.dc = DataCollector(\n", " model=model_s3,\n", " model_reporters={\n", -<<<<<<< HEAD " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", -======= - " \"total_wealth\": lambda m: list(m.sets.df.values())[0][\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(list(m.sets.df.values())[0]),\n", ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 " },\n", " agent_reporters={\n", " \"wealth\": \"wealth\",\n", @@ -377,11 +319,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD "execution_count": 11, -======= - "execution_count": 23, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "id": "938c804e27f84196a10c8828c723f798", "metadata": { "editable": true @@ -443,11 +381,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD "execution_count": 12, -======= - "execution_count": 24, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "id": "59bbdb311c014d738909a11f9e486628", "metadata": { "editable": true @@ -476,11 +410,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD "execution_count": 13, -======= - "execution_count": 25, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "id": "8a65eabff63a45729fe45fb5ade58bdc", "metadata": { "editable": true @@ -496,11 +426,7 @@ " white-space: pre-wrap;\n", "}\n", "\n", -<<<<<<< HEAD "shape: (5, 5)
stepseedbatchtotal_wealthn_agents
i64stri64f64i64
2"540832786058427425452319829502…0100.0100
4"540832786058427425452319829502…0100.0100
6"540832786058427425452319829502…0100.0100
8"540832786058427425452319829502…0100.0100
10"540832786058427425452319829502…0100.0100
" -======= - "shape: (5, 5)
stepseedbatchtotal_wealthn_agents
i64stri64f64i64
2"732054881101029867447298951813…0100.0100
4"732054881101029867447298951813…0100.0100
6"732054881101029867447298951813…0100.0100
8"732054881101029867447298951813…0100.0100
10"732054881101029867447298951813…0100.0100
" ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 ], "text/plain": [ "shape: (5, 5)\n", @@ -509,7 +435,6 @@ "│ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", "│ i64 ┆ str ┆ i64 ┆ f64 ┆ i64 │\n", "╞══════╪═════════════════════════════════╪═══════╪══════════════╪══════════╡\n", -<<<<<<< HEAD "│ 2 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", "│ 4 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", "│ 6 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", @@ -519,17 +444,6 @@ ] }, "execution_count": 13, -======= - "│ 2 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 4 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 6 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 8 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 10 ┆ 732054881101029867447298951813… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "└──────┴─────────────────────────────────┴───────┴──────────────┴──────────┘" - ] - }, - "execution_count": 25, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "metadata": {}, "output_type": "execute_result" } diff --git a/examples/sugarscape_ig/ss_polars/model.py b/examples/sugarscape_ig/ss_polars/model.py index 359291bd..36b2718e 100644 --- a/examples/sugarscape_ig/ss_polars/model.py +++ b/examples/sugarscape_ig/ss_polars/model.py @@ -33,14 +33,10 @@ def __init__( sugar=sugar_grid.flatten(), max_sugar=sugar_grid.flatten() ) self.space.set_cells(sugar_grid) -<<<<<<< HEAD # Create and register the main agent set; keep its name for later lookups main_set = agent_type(self, n_agents, initial_sugar, metabolism, vision) self.sets += main_set self._main_set_name = main_set.name -======= - self.sets += agent_type(self, n_agents, initial_sugar, metabolism, vision) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 if initial_positions is not None: self.space.place_agents(self.sets, initial_positions) else: @@ -48,12 +44,8 @@ def __init__( def run_model(self, steps: int) -> list[int]: for _ in range(steps): -<<<<<<< HEAD # Stop if the main agent set is empty if len(self.sets[self._main_set_name]) == 0: # type: ignore[index] -======= - if len(list(self.sets.df.values())[0]) == 0: ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 return empty_cells = self.space.empty_cells full_cells = self.space.full_cells diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index d3974967..9bc25174 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -20,19 +20,12 @@ from abc import abstractmethod from collections.abc import Collection, Iterable, Iterator -<<<<<<< HEAD from contextlib import suppress from typing import Any, Literal, Self, overload from numpy.random import Generator from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin -======= -from typing import Any, Literal, Self, overload - -from mesa_frames.abstract.agentsetregistry import AbstractAgentSetRegistry -from mesa_frames.abstract.mixin import DataFrameMixin ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 from mesa_frames.types_ import ( AgentMask, BoolSeries, @@ -44,11 +37,7 @@ ) -<<<<<<< HEAD class AbstractAgentSet(CopyMixin, DataFrameMixin): -======= -class AbstractAgentSet(AbstractAgentSetRegistry, DataFrameMixin): ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 """The AbstractAgentSet class is a container for agents of the same type. Parameters @@ -57,10 +46,7 @@ class AbstractAgentSet(AbstractAgentSetRegistry, DataFrameMixin): The model that the agent set belongs to. """ -<<<<<<< HEAD _copy_only_reference: list[str] = ["_model"] -======= ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 _df: DataFrame # The agents in the AbstractAgentSet _mask: AgentMask # The underlying mask used for the active agents in the AbstractAgentSet. _model: ( @@ -92,7 +78,6 @@ def add( Returns ------- Self -<<<<<<< HEAD A new AbstractAgentSet with the added agents. """ ... @@ -118,9 +103,6 @@ def contains(self, agents: IdsLike) -> bool | BoolSeries: ------- bool | BoolSeries True if the agent is in the AgentSet, False otherwise. -======= - A new AbstractAgentSetRegistry with the added agents. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 """ ... @@ -139,7 +121,6 @@ def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: Self The updated AbstractAgentSet. """ -<<<<<<< HEAD with suppress(KeyError, ValueError): return self.remove(agents, inplace=inplace) return self._get_obj(inplace) @@ -219,67 +200,6 @@ def do( The updated AgentSet or the result of the method. """ ... -======= - return super().discard(agents, inplace) - - @overload - def do( - self, - method_name: str, - *args, - mask: AgentMask | None = None, - return_results: Literal[False] = False, - inplace: bool = True, - **kwargs, - ) -> Self: ... - - @overload - def do( - self, - method_name: str, - *args, - mask: AgentMask | None = None, - return_results: Literal[True], - inplace: bool = True, - **kwargs, - ) -> Any: ... - - def do( - self, - method_name: str, - *args, - mask: AgentMask | None = None, - return_results: bool = False, - inplace: bool = True, - **kwargs, - ) -> Self | Any: - masked_df = self._get_masked_df(mask) - # If the mask is empty, we can use the object as is - if len(masked_df) == len(self._df): - obj = self._get_obj(inplace) - method = getattr(obj, method_name) - result = method(*args, **kwargs) - else: # If the mask is not empty, we need to create a new masked AbstractAgentSet and concatenate the AbstractAgentSets at the end - obj = self._get_obj(inplace=False) - obj._df = masked_df - original_masked_index = obj._get_obj_copy(obj.index) - method = getattr(obj, method_name) - result = method(*args, **kwargs) - obj._concatenate_agentsets( - [self], - duplicates_allowed=True, - keep_first_only=True, - original_masked_index=original_masked_index, - ) - if inplace: - for key, value in obj.__dict__.items(): - setattr(self, key, value) - obj = self - if return_results: - return result - else: - return obj ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 @abstractmethod @overload @@ -309,23 +229,6 @@ def step(self) -> None: """Run a single step of the AbstractAgentSet. This method should be overridden by subclasses.""" ... -<<<<<<< HEAD -======= - def remove(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: - if isinstance(agents, str) and agents == "active": - agents = self.active_agents - if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): - return self._get_obj(inplace) - agents = self._df_index(self._get_masked_df(agents), "unique_id") - sets = self.model.sets.remove(agents, inplace=inplace) - # TODO: Refactor AgentSetRegistry to return dict[str, AbstractAgentSet] instead of dict[AbstractAgentSet, DataFrame] - # And assign a name to AbstractAgentSet? This has to be replaced by a nicer API of AgentSetRegistry - for agentset in sets.df.keys(): - if isinstance(agentset, self.__class__): - return agentset - return self - ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 @abstractmethod def _concatenate_agentsets( self, @@ -415,15 +318,9 @@ def __add__(self, other: DataFrame | DataFrameInput) -> Self: Returns ------- Self -<<<<<<< HEAD A new AbstractAgentSet with the added agents. """ return self.add(other, inplace=False) -======= - A new AbstractAgentSetRegistry with the added agents. - """ - return super().__add__(other) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def __iadd__(self, other: DataFrame | DataFrameInput) -> Self: """ @@ -441,7 +338,6 @@ def __iadd__(self, other: DataFrame | DataFrameInput) -> Self: Returns ------- Self -<<<<<<< HEAD The updated AbstractAgentSet. """ return self.add(other, inplace=True) @@ -453,11 +349,6 @@ def __isub__(self, other: IdsLike | AgentMask | DataFrame) -> Self: def __sub__(self, other: IdsLike | AgentMask | DataFrame) -> Self: """Return a new set with agents removed via - operator.""" return self.discard(other, inplace=False) -======= - The updated AbstractAgentSetRegistry. - """ - return super().__iadd__(other) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 @abstractmethod def __getattr__(self, name: str) -> Any: @@ -486,7 +377,6 @@ def __getitem__( | tuple[AgentMask, Collection[str]] ), ) -> Series | DataFrame: -<<<<<<< HEAD # Mirror registry/old container behavior: delegate to get() if isinstance(key, tuple): return self.get(mask=key[0], attr_names=key[1]) @@ -501,11 +391,6 @@ def __getitem__( def __contains__(self, agents: int) -> bool: """Membership test for an agent id in this set.""" return bool(self.contains(agents)) -======= - attr = super().__getitem__(key) - assert isinstance(attr, (Series, DataFrame, Index)) - return attr ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def __len__(self) -> int: return len(self._df) @@ -543,10 +428,7 @@ def active_agents(self) -> DataFrame: ... def inactive_agents(self) -> DataFrame: ... @property -<<<<<<< HEAD @abstractmethod -======= ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def index(self) -> Index: ... @property @@ -562,7 +444,6 @@ def pos(self) -> DataFrame: pos, self.index, new_index_cols="unique_id", original_index_cols="agent_id" ) return pos -<<<<<<< HEAD @property def name(self) -> str: @@ -655,5 +536,3 @@ def __setitem__( self.set(attr_names=None, mask=key, values=values) else: self.set(attr_names=None, mask=key, values=values) -======= ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index fe9ae732..cb535d1b 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -43,11 +43,7 @@ def __init__(self, model): from __future__ import annotations # PEP 563: postponed evaluation of type annotations from abc import abstractmethod -<<<<<<< HEAD from collections.abc import Callable, Collection, Iterable, Iterator, Sequence -======= -from collections.abc import Callable, Collection, Iterator, Sequence ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 from contextlib import suppress from typing import Any, Literal, Self, overload @@ -55,21 +51,12 @@ def __init__(self, model): from mesa_frames.abstract.mixin import CopyMixin from mesa_frames.types_ import ( -<<<<<<< HEAD AbstractAgentSetSelector as AgentSetSelector, ) from mesa_frames.types_ import ( BoolSeries, Index, KeyBy, -======= - AgentMask, - BoolSeries, - DataFrame, - DataFrameInput, - IdsLike, - Index, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Series, ) @@ -87,7 +74,6 @@ def __init__(self) -> None: ... def discard( self, -<<<<<<< HEAD sets: AgentSetSelector, inplace: bool = True, ) -> Self: @@ -99,22 +85,6 @@ def discard( Which AgentSets to remove (instance, type, name, or collection thereof). inplace : bool Whether to remove in place. Defaults to True. -======= - agents: IdsLike - | AgentMask - | mesa_frames.abstract.agentset.AbstractAgentSet - | Collection[mesa_frames.abstract.agentset.AbstractAgentSet], - inplace: bool = True, - ) -> Self: - """Remove agents from the AbstractAgentSetRegistry. Does not raise an error if the agent is not found. - - Parameters - ---------- - agents : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to remove - inplace : bool - Whether to remove the agent in place. Defaults to True. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Returns ------- @@ -122,7 +92,6 @@ def discard( The updated AbstractAgentSetRegistry. """ with suppress(KeyError, ValueError): -<<<<<<< HEAD return self.remove(sets, inplace=inplace) return self._get_obj(inplace) @@ -187,28 +156,6 @@ def add( The AgentSet(s) to add. inplace : bool Whether to add in place. Defaults to True. -======= - return self.remove(agents, inplace=inplace) - return self._get_obj(inplace) - - @abstractmethod - def add( - self, - agents: DataFrame - | DataFrameInput - | mesa_frames.abstract.agentset.AbstractAgentSet - | Collection[mesa_frames.abstract.agentset.AbstractAgentSet], - inplace: bool = True, - ) -> Self: - """Add agents to the AbstractAgentSetRegistry. - - Parameters - ---------- - agents : DataFrame | DataFrameInput | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to add. - inplace : bool - Whether to add the agents in place. Defaults to True. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Returns ------- @@ -219,7 +166,6 @@ def add( @overload @abstractmethod -<<<<<<< HEAD def contains( self, sets: ( @@ -228,14 +174,10 @@ def contains( | str ), ) -> bool: ... -======= - def contains(self, agents: int) -> bool: ... ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 @overload @abstractmethod def contains( -<<<<<<< HEAD self, sets: Collection[ mesa_frames.abstract.agentset.AbstractAgentSet @@ -253,30 +195,11 @@ def contains(self, sets: AgentSetSelector) -> bool | BoolSeries: sets : AgentSetSelector An AgentSet instance, class/type, name string, or a collection of those. For collections, returns a BoolSeries aligned with input order. -======= - self, agents: mesa_frames.abstract.agentset.AbstractAgentSet | IdsLike - ) -> BoolSeries: ... - - @abstractmethod - def contains( - self, agents: mesa_frames.abstract.agentset.AbstractAgentSet | IdsLike - ) -> bool | BoolSeries: - """Check if agents with the specified IDs are in the AbstractAgentSetRegistry. - - Parameters - ---------- - agents : mesa_frames.abstract.agentset.AbstractAgentSet | IdsLike - The ID(s) to check for. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Returns ------- bool | BoolSeries -<<<<<<< HEAD Boolean for single selector values; BoolSeries for collections. -======= - True if the agent is in the AbstractAgentSetRegistry, False otherwise. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 """ @overload @@ -285,16 +208,10 @@ def do( self, method_name: str, *args: Any, -<<<<<<< HEAD sets: AgentSetSelector | None = None, return_results: Literal[False] = False, inplace: bool = True, key_by: KeyBy = "name", -======= - mask: AgentMask | None = None, - return_results: Literal[False] = False, - inplace: bool = True, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 **kwargs: Any, ) -> Self: ... @@ -304,7 +221,6 @@ def do( self, method_name: str, *args: Any, -<<<<<<< HEAD sets: AgentSetSelector, return_results: Literal[True], inplace: bool = True, @@ -316,20 +232,12 @@ def do( | dict[int, Any] | dict[type[mesa_frames.abstract.agentset.AbstractAgentSet], Any] ): ... -======= - mask: AgentMask | None = None, - return_results: Literal[True], - inplace: bool = True, - **kwargs: Any, - ) -> Any | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Any]: ... ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 @abstractmethod def do( self, method_name: str, *args: Any, -<<<<<<< HEAD sets: AgentSetSelector = None, return_results: bool = False, inplace: bool = True, @@ -342,13 +250,6 @@ def do( | dict[int, Any] | dict[type[mesa_frames.abstract.agentset.AbstractAgentSet], Any] ): -======= - mask: AgentMask | None = None, - return_results: bool = False, - inplace: bool = True, - **kwargs: Any, - ) -> Self | Any | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Any]: ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 """Invoke a method on the AbstractAgentSetRegistry. Parameters @@ -357,7 +258,6 @@ def do( The name of the method to invoke. *args : Any Positional arguments to pass to the method -<<<<<<< HEAD sets : AgentSetSelector, optional Which AgentSets to target (instance, type, name, or collection thereof). Defaults to all. return_results : bool, optional @@ -369,20 +269,11 @@ def do( - "name" (default) → keys are set names (str) - "index" → keys are positional indices (int) - "type" → keys are concrete set classes (type) -======= - mask : AgentMask | None, optional - The subset of agents on which to apply the method - return_results : bool, optional - Whether to return the result of the method, by default False - inplace : bool, optional - Whether the operation should be done inplace, by default False ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 **kwargs : Any Keyword arguments to pass to the method Returns ------- -<<<<<<< HEAD Self | Any | dict[str, Any] | dict[int, Any] | dict[type[mesa_frames.abstract.agentset.AbstractAgentSet], Any] The updated registry, or the method result(s). When ``return_results`` is True, returns a dictionary keyed per ``key_by``. @@ -422,27 +313,10 @@ def get( | list[mesa_frames.abstract.agentset.AbstractAgentSet] | None ): ... -======= - Self | Any | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Any] - The updated AbstractAgentSetRegistry or the result of the method. - """ - ... - - @abstractmethod - @overload - def get(self, attr_names: str) -> Series | dict[str, Series]: ... - - @abstractmethod - @overload - def get( - self, attr_names: Collection[str] | None = None - ) -> DataFrame | dict[str, DataFrame]: ... ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 @abstractmethod def get( self, -<<<<<<< HEAD key: int | str | type[mesa_frames.abstract.agentset.AbstractAgentSet], default: mesa_frames.abstract.agentset.AbstractAgentSet | list[mesa_frames.abstract.agentset.AbstractAgentSet] @@ -453,31 +327,10 @@ def get( | None ): """Safe lookup for AgentSet(s) by index, name, or type.""" -======= - attr_names: str | Collection[str] | None = None, - mask: AgentMask | None = None, - ) -> Series | dict[str, Series] | DataFrame | dict[str, DataFrame]: - """Retrieve the value of a specified attribute for each agent in the AbstractAgentSetRegistry. - - Parameters - ---------- - attr_names : str | Collection[str] | None, optional - The attributes to retrieve. If None, all attributes are retrieved. Defaults to None. - mask : AgentMask | None, optional - The AgentMask of agents to retrieve the attribute for. If None, attributes of all agents are returned. Defaults to None. - - Returns - ------- - Series | dict[str, Series] | DataFrame | dict[str, DataFrame] - The attribute values. - """ - ... ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 @abstractmethod def remove( self, -<<<<<<< HEAD sets: AgentSetSelector, inplace: bool = True, ) -> Self: @@ -487,22 +340,6 @@ def remove( ---------- sets : AgentSetSelector Which AgentSets to remove (instance, type, name, or collection thereof). -======= - agents: ( - IdsLike - | AgentMask - | mesa_frames.abstract.agentset.AbstractAgentSet - | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - ), - inplace: bool = True, - ) -> Self: - """Remove the agents from the AbstractAgentSetRegistry. - - Parameters - ---------- - agents : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to remove. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 inplace : bool, optional Whether to remove the agent in place. @@ -513,7 +350,6 @@ def remove( """ ... -<<<<<<< HEAD # select() intentionally removed from the abstract API. @abstractmethod @@ -538,110 +374,22 @@ def replace( atomic : bool, optional When True, validates all keys and name invariants before applying any change; either all assignments succeed or none are applied. -======= - @abstractmethod - def select( - self, - mask: AgentMask | None = None, - filter_func: Callable[[Self], AgentMask] | None = None, - n: int | None = None, - negate: bool = False, - inplace: bool = True, - ) -> Self: - """Select agents in the AbstractAgentSetRegistry based on the given criteria. - - Parameters - ---------- - mask : AgentMask | None, optional - The AgentMask of agents to be selected, by default None - filter_func : Callable[[Self], AgentMask] | None, optional - A function which takes as input the AbstractAgentSetRegistry and returns a AgentMask, by default None - n : int | None, optional - The maximum number of agents to be selected, by default None - negate : bool, optional - If the selection should be negated, by default False - inplace : bool, optional - If the operation should be performed on the same object, by default True ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Returns ------- Self -<<<<<<< HEAD Updated registry. -======= - A new or updated AbstractAgentSetRegistry. - """ - ... - - @abstractmethod - @overload - def set( - self, - attr_names: dict[str, Any], - values: None, - mask: AgentMask | None = None, - inplace: bool = True, - ) -> Self: ... - - @abstractmethod - @overload - def set( - self, - attr_names: str | Collection[str], - values: Any, - mask: AgentMask | None = None, - inplace: bool = True, - ) -> Self: ... - - @abstractmethod - def set( - self, - attr_names: DataFrameInput | str | Collection[str], - values: Any | None = None, - mask: AgentMask | None = None, - inplace: bool = True, - ) -> Self: - """Set the value of a specified attribute or attributes for each agent in the mask in AbstractAgentSetRegistry. - - Parameters - ---------- - attr_names : DataFrameInput | str | Collection[str] - The key can be: - - A string: sets the specified column of the agents in the AbstractAgentSetRegistry. - - A collection of strings: sets the specified columns of the agents in the AbstractAgentSetRegistry. - - A dictionary: keys should be attributes and values should be the values to set. Value should be None. - values : Any | None - The value to set the attribute to. If None, attr_names must be a dictionary. - mask : AgentMask | None - The AgentMask of agents to set the attribute for. - inplace : bool - Whether to set the attribute in place. - - Returns - ------- - Self - The updated agent set. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 """ ... @abstractmethod def shuffle(self, inplace: bool = False) -> Self: -<<<<<<< HEAD """Shuffle the order of AgentSets in the registry. -======= - """Shuffles the order of agents in the AbstractAgentSetRegistry. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Parameters ---------- inplace : bool -<<<<<<< HEAD Whether to shuffle in place. -======= - Whether to shuffle the agents in place. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Returns ------- @@ -658,11 +406,7 @@ def sort( **kwargs, ) -> Self: """ -<<<<<<< HEAD Sort the AgentSets in the registry based on the given criteria. -======= - Sorts the agents in the agent set based on the given criteria. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Parameters ---------- @@ -683,7 +427,6 @@ def sort( def __add__( self, -<<<<<<< HEAD other: mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet], ) -> Self: @@ -718,113 +461,10 @@ def __getitem__( | list[mesa_frames.abstract.agentset.AbstractAgentSet] ): """Retrieve AgentSet(s) by index, name, or type.""" -======= - other: DataFrame - | DataFrameInput - | mesa_frames.abstract.agentset.AbstractAgentSet - | Collection[mesa_frames.abstract.agentset.AbstractAgentSet], - ) -> Self: - """Add agents to a new AbstractAgentSetRegistry through the + operator. - - Parameters - ---------- - other : DataFrame | DataFrameInput | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to add. - - Returns - ------- - Self - A new AbstractAgentSetRegistry with the added agents. - """ - return self.add(agents=other, inplace=False) - - def __contains__( - self, agents: int | mesa_frames.abstract.agentset.AbstractAgentSet - ) -> bool: - """Check if an agent is in the AbstractAgentSetRegistry. - - Parameters - ---------- - agents : int | mesa_frames.abstract.agentset.AbstractAgentSet - The ID(s) or AbstractAgentSet to check for. - - Returns - ------- - bool - True if the agent is in the AbstractAgentSetRegistry, False otherwise. - """ - return self.contains(agents=agents) - - @overload - def __getitem__( - self, key: str | tuple[AgentMask, str] - ) -> Series | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Series]: ... - - @overload - def __getitem__( - self, - key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], - ) -> ( - DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] - ): ... - - def __getitem__( - self, - key: ( - str - | Collection[str] - | AgentMask - | tuple[AgentMask, str] - | tuple[AgentMask, Collection[str]] - | tuple[ - dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str - ] - | tuple[ - dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], - Collection[str], - ] - ), - ) -> ( - Series - | DataFrame - | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Series] - | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] - ): - """Implement the [] operator for the AbstractAgentSetRegistry. - - The key can be: - - An attribute or collection of attributes (eg. AbstractAgentSetRegistry["str"], AbstractAgentSetRegistry[["str1", "str2"]]): returns the specified column(s) of the agents in the AbstractAgentSetRegistry. - - An AgentMask (eg. AbstractAgentSetRegistry[AgentMask]): returns the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. - - A tuple (eg. AbstractAgentSetRegistry[AgentMask, "str"]): returns the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. - - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, "str"]): returns the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. - - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, Collection[str]]): returns the specified columns of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. - - Parameters - ---------- - key : str | Collection[str] | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] - The key to retrieve. - - Returns - ------- - Series | DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Series] | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] - The attribute values. - """ - # TODO: fix types - if isinstance(key, tuple): - return self.get(mask=key[0], attr_names=key[1]) - else: - if isinstance(key, str) or ( - isinstance(key, Collection) and all(isinstance(k, str) for k in key) - ): - return self.get(attr_names=key) - else: - return self.get(mask=key) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def __iadd__( self, other: ( -<<<<<<< HEAD mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] ), @@ -835,36 +475,17 @@ def __iadd__( ---------- other : mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The AgentSets to add. -======= - DataFrame - | DataFrameInput - | mesa_frames.abstract.agentset.AbstractAgentSet - | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - ), - ) -> Self: - """Add agents to the AbstractAgentSetRegistry through the += operator. - - Parameters - ---------- - other : DataFrame | DataFrameInput | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to add. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Returns ------- Self The updated AbstractAgentSetRegistry. """ -<<<<<<< HEAD return self.add(sets=other, inplace=True) -======= - return self.add(agents=other, inplace=True) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def __isub__( self, other: ( -<<<<<<< HEAD mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] ), @@ -875,20 +496,6 @@ def __isub__( ---------- other : mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The AgentSets to remove. -======= - IdsLike - | AgentMask - | mesa_frames.abstract.agentset.AbstractAgentSet - | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - ), - ) -> Self: - """Remove agents from the AbstractAgentSetRegistry through the -= operator. - - Parameters - ---------- - other : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to remove. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Returns ------- @@ -900,7 +507,6 @@ def __isub__( def __sub__( self, other: ( -<<<<<<< HEAD mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] ), @@ -911,35 +517,16 @@ def __sub__( ---------- other : mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] The AgentSets to remove. -======= - IdsLike - | AgentMask - | mesa_frames.abstract.agentset.AbstractAgentSet - | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - ), - ) -> Self: - """Remove agents from a new AbstractAgentSetRegistry through the - operator. - - Parameters - ---------- - other : IdsLike | AgentMask | mesa_frames.abstract.agentset.AbstractAgentSet | Collection[mesa_frames.abstract.agentset.AbstractAgentSet] - The agents to remove. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Returns ------- Self -<<<<<<< HEAD A new AbstractAgentSetRegistry with the removed AgentSets. -======= - A new AbstractAgentSetRegistry with the removed agents. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 """ return self.discard(other, inplace=False) def __setitem__( self, -<<<<<<< HEAD key: int | str, value: mesa_frames.abstract.agentset.AbstractAgentSet, ) -> None: @@ -959,128 +546,25 @@ def __getattr__(self, name: str) -> Any | dict[str, Any]: @abstractmethod def __iter__(self) -> Iterator[mesa_frames.abstract.agentset.AbstractAgentSet]: """Iterate over AgentSets in the registry.""" -======= - key: ( - str - | Collection[str] - | AgentMask - | tuple[AgentMask, str | Collection[str]] - | tuple[ - dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str - ] - | tuple[ - dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], - Collection[str], - ] - ), - values: Any, - ) -> None: - """Implement the [] operator for setting values in the AbstractAgentSetRegistry. - - The key can be: - - A string (eg. AbstractAgentSetRegistry["str"]): sets the specified column of the agents in the AbstractAgentSetRegistry. - - A list of strings(eg. AbstractAgentSetRegistry[["str1", "str2"]]): sets the specified columns of the agents in the AbstractAgentSetRegistry. - - A tuple (eg. AbstractAgentSetRegistry[AgentMask, "str"]): sets the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. - - A AgentMask (eg. AbstractAgentSetRegistry[AgentMask]): sets the attributes of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask. - - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, "str"]): sets the specified column of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. - - A tuple with a dictionary (eg. AbstractAgentSetRegistry[{AbstractAgentSet: AgentMask}, Collection[str]]): sets the specified columns of the agents in the AbstractAgentSetRegistry that satisfy the AgentMask from the dictionary. - - Parameters - ---------- - key : str | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], str] | tuple[dict[mesa_frames.abstract.agentset.AbstractAgentSet, AgentMask], Collection[str]] - The key to set. - values : Any - The values to set for the specified key. - """ - # TODO: fix types as in __getitem__ - if isinstance(key, tuple): - self.set(mask=key[0], attr_names=key[1], values=values) - else: - if isinstance(key, str) or ( - isinstance(key, Collection) and all(isinstance(k, str) for k in key) - ): - try: - self.set(attr_names=key, values=values) - except KeyError: # key=AgentMask - self.set(attr_names=None, mask=key, values=values) - else: - self.set(attr_names=None, mask=key, values=values) - - @abstractmethod - def __getattr__(self, name: str) -> Any | dict[str, Any]: - """Fallback for retrieving attributes of the AbstractAgentSetRegistry. Retrieve an attribute of the underlying DataFrame(s). - - Parameters - ---------- - name : str - The name of the attribute to retrieve. - - Returns - ------- - Any | dict[str, Any] - The attribute value - """ - - @abstractmethod - def __iter__(self) -> Iterator[dict[str, Any]]: - """Iterate over the agents in the AbstractAgentSetRegistry. - - Returns - ------- - Iterator[dict[str, Any]] - An iterator over the agents. - """ ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 ... @abstractmethod def __len__(self) -> int: -<<<<<<< HEAD """Get the number of AgentSets in the registry.""" -======= - """Get the number of agents in the AbstractAgentSetRegistry. - - Returns - ------- - int - The number of agents in the AbstractAgentSetRegistry. - """ ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 ... @abstractmethod def __repr__(self) -> str: -<<<<<<< HEAD """Get a string representation of the AgentSets in the registry.""" -======= - """Get a string representation of the DataFrame in the AbstractAgentSetRegistry. - - Returns - ------- - str - A string representation of the DataFrame in the AbstractAgentSetRegistry. - """ ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 pass @abstractmethod def __reversed__(self) -> Iterator: -<<<<<<< HEAD """Iterate over AgentSets in reverse order.""" -======= - """Iterate over the agents in the AbstractAgentSetRegistry in reverse order. - - Returns - ------- - Iterator - An iterator over the agents in reverse order. - """ ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 ... @abstractmethod def __str__(self) -> str: -<<<<<<< HEAD """Get a string representation of the AgentSets in the registry.""" ... @@ -1106,15 +590,6 @@ def items( @abstractmethod def values(self) -> Iterable[mesa_frames.abstract.agentset.AbstractAgentSet]: """Iterate contained AgentSets (values view).""" -======= - """Get a string representation of the agents in the AbstractAgentSetRegistry. - - Returns - ------- - str - A string representation of the agents in the AbstractAgentSetRegistry. - """ ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 ... @property @@ -1149,7 +624,6 @@ def space(self) -> mesa_frames.abstract.space.Space | None: @property @abstractmethod -<<<<<<< HEAD def ids(self) -> Series: """Public view of all agent unique_id values across contained sets. @@ -1157,87 +631,5 @@ def ids(self) -> Series: ------- Series Concatenated unique_id Series for all AgentSets. -======= - def df(self) -> DataFrame | dict[str, DataFrame]: - """The agents in the AbstractAgentSetRegistry. - - Returns - ------- - DataFrame | dict[str, DataFrame] - """ - - @df.setter - @abstractmethod - def df( - self, agents: DataFrame | list[mesa_frames.abstract.agentset.AbstractAgentSet] - ) -> None: - """Set the agents in the AbstractAgentSetRegistry. - - Parameters - ---------- - agents : DataFrame | list[mesa_frames.abstract.agentset.AbstractAgentSet] - """ - - @property - @abstractmethod - def active_agents(self) -> DataFrame | dict[str, DataFrame]: - """The active agents in the AbstractAgentSetRegistry. - - Returns - ------- - DataFrame | dict[str, DataFrame] - """ - - @active_agents.setter - @abstractmethod - def active_agents( - self, - mask: AgentMask, - ) -> None: - """Set the active agents in the AbstractAgentSetRegistry. - - Parameters - ---------- - mask : AgentMask - The mask to apply. - """ - self.select(mask=mask, inplace=True) - - @property - @abstractmethod - def inactive_agents( - self, - ) -> DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame]: - """The inactive agents in the AbstractAgentSetRegistry. - - Returns - ------- - DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] - """ - - @property - @abstractmethod - def index( - self, - ) -> Index | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Index]: - """The ids in the AbstractAgentSetRegistry. - - Returns - ------- - Index | dict[mesa_frames.abstract.agentset.AbstractAgentSet, Index] - """ - ... - - @property - @abstractmethod - def pos( - self, - ) -> DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame]: - """The position of the agents in the AbstractAgentSetRegistry. - - Returns - ------- - DataFrame | dict[mesa_frames.abstract.agentset.AbstractAgentSet, DataFrame] ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 """ ... diff --git a/mesa_frames/abstract/space.py b/mesa_frames/abstract/space.py index 3f6bab3a..39abe6bd 100644 --- a/mesa_frames/abstract/space.py +++ b/mesa_frames/abstract/space.py @@ -12,11 +12,7 @@ classes in mesa-frames. It combines fast copying functionality with DataFrame operations. -<<<<<<< HEAD AbstractDiscreteSpace(Space): -======= - AbstractDiscreteSpace(SpaceDF): ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 An abstract base class for discrete space implementations, such as grids and networks. It extends Space with methods specific to discrete spaces. @@ -68,10 +64,6 @@ def __init__(self, model, dimensions, torus, capacity, neighborhood_type): AbstractAgentSetRegistry, ) from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin -<<<<<<< HEAD -======= -from mesa_frames.concrete.agentsetregistry import AgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 from mesa_frames.types_ import ( ArrayLike, BoolSeries, @@ -105,11 +97,7 @@ class Space(CopyMixin, DataFrameMixin): ] # The column names of the positions in the _agents dataframe (eg. ['dim_0', 'dim_1', ...] in Grids, ['node_id', 'edge_id'] in Networks) def __init__(self, model: mesa_frames.concrete.model.Model) -> None: -<<<<<<< HEAD """Create a new Space. -======= - """Create a new SpaceDF. ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 Parameters ---------- @@ -120,13 +108,9 @@ def __init__(self, model: mesa_frames.concrete.model.Model) -> None: def move_agents( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], pos: SpaceCoordinate | SpaceCoordinates, inplace: bool = True, @@ -137,11 +121,7 @@ def move_agents( Parameters ---------- -<<<<<<< HEAD agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] -======= - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The agents to move pos : SpaceCoordinate | SpaceCoordinates The coordinates for each agents. The length of the coordinates must match the number of agents. @@ -166,13 +146,9 @@ def move_agents( def place_agents( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], pos: SpaceCoordinate | SpaceCoordinates, inplace: bool = True, @@ -181,11 +157,7 @@ def place_agents( Parameters ---------- -<<<<<<< HEAD agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] -======= - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The agents to place in the space pos : SpaceCoordinate | SpaceCoordinates The coordinates for each agents. The length of the coordinates must match the number of agents. @@ -229,7 +201,6 @@ def random_agents( def swap_agents( self, agents0: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] @@ -238,12 +209,6 @@ def swap_agents( | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry - | Collection[AbstractAgentSetRegistry], - agents1: IdsLike - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -253,15 +218,9 @@ def swap_agents( Parameters ---------- -<<<<<<< HEAD agents0 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] The first set of agents to swap agents1 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] -======= - agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] - The first set of agents to swap - agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The second set of agents to swap inplace : bool, optional Whether to perform the operation inplace, by default True @@ -306,7 +265,6 @@ def get_directions( pos0: SpaceCoordinate | SpaceCoordinates | None = None, pos1: SpaceCoordinate | SpaceCoordinates | None = None, agents0: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] @@ -316,13 +274,6 @@ def get_directions( | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry - | Collection[AbstractAgentSetRegistry] - | None = None, - agents1: IdsLike - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry] | None = None, normalize: bool = False, @@ -339,15 +290,9 @@ def get_directions( The starting positions pos1 : SpaceCoordinate | SpaceCoordinates | None, optional The ending positions -<<<<<<< HEAD agents0 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional The starting agents agents1 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional -======= - agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional - The starting agents - agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The ending agents normalize : bool, optional Whether to normalize the vectors to unit norm. By default False @@ -365,7 +310,6 @@ def get_distances( pos0: SpaceCoordinate | SpaceCoordinates | None = None, pos1: SpaceCoordinate | SpaceCoordinates | None = None, agents0: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] @@ -375,13 +319,6 @@ def get_distances( | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry - | Collection[AbstractAgentSetRegistry] - | None = None, - agents1: IdsLike - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry] | None = None, ) -> DataFrame: @@ -397,15 +334,9 @@ def get_distances( The starting positions pos1 : SpaceCoordinate | SpaceCoordinates | None, optional The ending positions -<<<<<<< HEAD agents0 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional The starting agents agents1 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional -======= - agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional - The starting agents - agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The ending agents Returns @@ -421,13 +352,9 @@ def get_neighbors( radius: int | float | Sequence[int] | Sequence[float] | ArrayLike, pos: SpaceCoordinate | SpaceCoordinates | None = None, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry] | None = None, include_center: bool = False, @@ -442,11 +369,7 @@ def get_neighbors( The radius(es) of the neighborhood pos : SpaceCoordinate | SpaceCoordinates | None, optional The coordinates of the cell to get the neighborhood from, by default None -<<<<<<< HEAD agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional -======= - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The id of the agents to get the neighborhood from, by default None include_center : bool, optional If the center cells or agents should be included in the result, by default False @@ -468,13 +391,9 @@ def get_neighbors( def move_to_empty( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -482,11 +401,7 @@ def move_to_empty( Parameters ---------- -<<<<<<< HEAD agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] -======= - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The agents to move to empty cells/positions inplace : bool, optional Whether to perform the operation inplace, by default True @@ -501,13 +416,9 @@ def move_to_empty( def place_to_empty( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -515,11 +426,7 @@ def place_to_empty( Parameters ---------- -<<<<<<< HEAD agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] -======= - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The agents to place in empty cells/positions inplace : bool, optional Whether to perform the operation inplace, by default True @@ -553,13 +460,9 @@ def random_pos( def remove_agents( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -569,11 +472,7 @@ def remove_agents( Parameters ---------- -<<<<<<< HEAD agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] -======= - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The agents to remove from the space inplace : bool, optional Whether to perform the operation inplace, by default True @@ -592,13 +491,9 @@ def remove_agents( def _get_ids_srs( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], ) -> Series: if isinstance(agents, Sized) and len(agents) == 0: @@ -609,18 +504,11 @@ def _get_ids_srs( name="agent_id", dtype="uint64", ) -<<<<<<< HEAD elif isinstance(agents, AbstractAgentSetRegistry): return self._srs_constructor(agents.ids, name="agent_id", dtype="uint64") elif isinstance(agents, Collection) and ( isinstance(agents[0], AbstractAgentSet) or isinstance(agents[0], AbstractAgentSetRegistry) -======= - elif isinstance(agents, AgentSetRegistry): - return self._srs_constructor(agents._ids, name="agent_id", dtype="uint64") - elif isinstance(agents, Collection) and ( - isinstance(agents[0], AbstractAgentSetRegistry) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 ): ids = [] for a in agents: @@ -632,11 +520,7 @@ def _get_ids_srs( dtype="uint64", ) ) -<<<<<<< HEAD elif isinstance(a, AbstractAgentSetRegistry): -======= - elif isinstance(a, AgentSetRegistry): ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 ids.append( self._srs_constructor(a.ids, name="agent_id", dtype="uint64") ) @@ -800,13 +684,9 @@ def move_to_empty( self, agents: IdsLike | AbstractAgentSetRegistry -<<<<<<< HEAD | Collection[AbstractAgentSetRegistry] | AbstractAgentSet | Collection[AbstractAgentSet], -======= - | Collection[AbstractAgentSetRegistry], ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) @@ -817,13 +697,9 @@ def move_to_empty( def move_to_available( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -831,11 +707,7 @@ def move_to_available( Parameters ---------- -<<<<<<< HEAD agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] -======= - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The agents to move to available cells/positions inplace : bool, optional Whether to perform the operation inplace, by default True @@ -853,13 +725,9 @@ def move_to_available( def place_to_empty( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -872,13 +740,9 @@ def place_to_empty( def place_to_available( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -996,13 +860,9 @@ def get_neighborhood( radius: int | float | Sequence[int] | Sequence[float] | ArrayLike, pos: DiscreteCoordinate | DiscreteCoordinates | None = None, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry] = None, include_center: bool = False, ) -> DataFrame: @@ -1016,11 +876,7 @@ def get_neighborhood( The radius(es) of the neighborhoods pos : DiscreteCoordinate | DiscreteCoordinates | None, optional The coordinates of the cell(s) to get the neighborhood from -<<<<<<< HEAD agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry], optional -======= - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry], optional ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The agent(s) to get the neighborhood from include_center : bool, optional If the cell in the center of the neighborhood should be included in the result, by default False @@ -1116,13 +972,9 @@ def _check_cells( def _place_or_move_agents_to_cells( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], cell_type: Literal["any", "empty", "available"], is_move: bool, @@ -1131,13 +983,8 @@ def _place_or_move_agents_to_cells( agents = self._get_ids_srs(agents) if __debug__: -<<<<<<< HEAD # Check ids presence in model using public API b_contained = agents.is_in(self.model.sets.ids) -======= - # Check ids presence in model - b_contained = self.model.sets.contains(agents) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): @@ -1453,7 +1300,6 @@ def get_directions( pos0: GridCoordinate | GridCoordinates | None = None, pos1: GridCoordinate | GridCoordinates | None = None, agents0: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] @@ -1463,13 +1309,6 @@ def get_directions( | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry - | Collection[AbstractAgentSetRegistry] - | None = None, - agents1: IdsLike - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry] | None = None, normalize: bool = False, @@ -1484,7 +1323,6 @@ def get_distances( pos0: GridCoordinate | GridCoordinates | None = None, pos1: GridCoordinate | GridCoordinates | None = None, agents0: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] @@ -1494,13 +1332,6 @@ def get_distances( | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry - | Collection[AbstractAgentSetRegistry] - | None = None, - agents1: IdsLike - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry] | None = None, ) -> DataFrame: @@ -1529,11 +1360,7 @@ def get_neighbors( def get_neighborhood( self, radius: int | Sequence[int] | ArrayLike, -<<<<<<< HEAD pos: DiscreteCoordinate | DiscreteCoordinates | None = None, -======= - pos: GridCoordinate | GridCoordinates | None = None, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 agents: IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] @@ -1771,13 +1598,9 @@ def out_of_bounds(self, pos: GridCoordinate | GridCoordinates) -> DataFrame: def remove_agents( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], inplace: bool = True, ) -> Self: @@ -1786,13 +1609,8 @@ def remove_agents( agents = obj._get_ids_srs(agents) if __debug__: -<<<<<<< HEAD # Check ids presence in model via public ids b_contained = agents.is_in(obj.model.sets.ids) -======= - # Check ids presence in model - b_contained = obj.model.sets.contains(agents) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): @@ -1827,7 +1645,6 @@ def _calculate_differences( pos0: GridCoordinate | GridCoordinates | None, pos1: GridCoordinate | GridCoordinates | None, agents0: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] @@ -1837,13 +1654,6 @@ def _calculate_differences( | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry - | Collection[AbstractAgentSetRegistry] - | None, - agents1: IdsLike - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry] | None, ) -> DataFrame: @@ -1855,15 +1665,9 @@ def _calculate_differences( The starting positions pos1 : GridCoordinate | GridCoordinates | None The ending positions -<<<<<<< HEAD agents0 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None The starting agents agents1 : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None -======= - agents0 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None - The starting agents - agents1 : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The ending agents Returns @@ -1945,13 +1749,9 @@ def _get_df_coords( self, pos: GridCoordinate | GridCoordinates | None = None, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry] | None = None, check_bounds: bool = True, @@ -1962,11 +1762,7 @@ def _get_df_coords( ---------- pos : GridCoordinate | GridCoordinates | None, optional The positions to get the DataFrame from, by default None -<<<<<<< HEAD agents : IdsLike | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] | Collection[AbstractAgentSetRegistry] | None, optional -======= - agents : IdsLike | AbstractAgentSetRegistry | Collection[AbstractAgentSetRegistry] | None, optional ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 The agents to get the DataFrame from, by default None check_bounds: bool, optional If the positions should be checked for out-of-bounds in non-toroidal grids, by default True @@ -1996,11 +1792,7 @@ def _get_df_coords( if agents is not None: agents = self._get_ids_srs(agents) # Check ids presence in model -<<<<<<< HEAD b_contained = agents.is_in(self.model.sets.ids) -======= - b_contained = self.model.sets.contains(agents) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): @@ -2061,13 +1853,9 @@ def _get_df_coords( def _place_or_move_agents( self, agents: IdsLike -<<<<<<< HEAD | AbstractAgentSet | AbstractAgentSetRegistry | Collection[AbstractAgentSet] -======= - | AbstractAgentSetRegistry ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 | Collection[AbstractAgentSetRegistry], pos: GridCoordinate | GridCoordinates, is_move: bool, @@ -2083,13 +1871,8 @@ def _place_or_move_agents( if self._df_contains(self._agents, "agent_id", agents).any(): warn("Some agents are already present in the grid", RuntimeWarning) -<<<<<<< HEAD # Check if agents are present in the model using the public ids b_contained = agents.is_in(self.model.sets.ids) -======= - # Check if agents are present in the model - b_contained = self.model.sets.contains(agents) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 if (isinstance(b_contained, Series) and not b_contained.all()) or ( isinstance(b_contained, bool) and not b_contained ): diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index c44ee149..2a9b1a55 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -67,11 +67,7 @@ def step(self): from mesa_frames.abstract.agentset import AbstractAgentSet from mesa_frames.concrete.mixin import PolarsMixin -<<<<<<< HEAD from mesa_frames.types_ import AgentMask, AgentPolarsMask, IntoExpr, PolarsIdsLike -======= -from mesa_frames.types_ import AgentPolarsMask, IntoExpr, PolarsIdsLike ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 from mesa_frames.utils import copydoc @@ -86,13 +82,9 @@ class AgentSet(AbstractAgentSet, PolarsMixin): _copy_only_reference: list[str] = ["_model", "_mask"] _mask: pl.Expr | pl.Series -<<<<<<< HEAD def __init__( self, model: mesa_frames.concrete.model.Model, name: str | None = None ) -> None: -======= - def __init__(self, model: mesa_frames.concrete.model.Model) -> None: ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 """Initialize a new AgentSet. Parameters diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 7b7583b1..7cb9e97d 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -46,37 +46,17 @@ def step(self): from __future__ import annotations # For forward references -<<<<<<< HEAD from collections.abc import Collection, Iterable, Iterator, Sequence from typing import Any, Literal, Self, overload, cast from collections.abc import Sized from itertools import chain -======= -from collections import defaultdict -from collections.abc import Callable, Collection, Iterable, Iterator, Sequence -from typing import Any, Literal, Self, cast, overload - -import numpy as np ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 import polars as pl from mesa_frames.abstract.agentsetregistry import ( AbstractAgentSetRegistry, ) from mesa_frames.concrete.agentset import AgentSet -<<<<<<< HEAD from mesa_frames.types_ import BoolSeries, KeyBy, AgentSetSelector -======= -from mesa_frames.types_ import ( - AgentMask, - AgnosticAgentMask, - BoolSeries, - DataFrame, - IdsLike, - Index, - Series, -) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 class AgentSetRegistry(AbstractAgentSetRegistry): @@ -99,43 +79,15 @@ def __init__(self, model: mesa_frames.concrete.model.Model) -> None: def add( self, -<<<<<<< HEAD sets: AgentSet | Iterable[AgentSet], inplace: bool = True, ) -> Self: obj = self._get_obj(inplace) other_list = obj._return_agentsets_list(sets) -======= - agents: AgentSet | Iterable[AgentSet], - inplace: bool = True, - ) -> Self: - """Add an AgentSet to the AgentSetRegistry. - - Parameters - ---------- - agents : AgentSet | Iterable[AgentSet] - The AgentSets to add. - inplace : bool, optional - Whether to add the AgentSets in place. Defaults to True. - - Returns - ------- - Self - The updated AgentSetRegistry. - - Raises - ------ - ValueError - If any AgentSets are already present or if IDs are not unique. - """ - obj = self._get_obj(inplace) - other_list = obj._return_agentsets_list(agents) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 if obj._check_agentsets_presence(other_list).any(): raise ValueError( "Some agentsets are already present in the AgentSetRegistry." ) -<<<<<<< HEAD # Ensure unique names across existing and to-be-added sets existing_names = {s.name for s in obj._agentsets} for agentset in other_list: @@ -152,8 +104,6 @@ def add( if name != (agentset.name or base_name): agentset.name = name existing_names.add(name) -======= ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 new_ids = pl.concat( [obj._ids] + [pl.Series(agentset["unique_id"]) for agentset in other_list] ) @@ -163,7 +113,6 @@ def add( obj._ids = new_ids return obj -<<<<<<< HEAD def rename( self, target: ( @@ -434,57 +383,23 @@ def has_type(t: type[AgentSet]) -> bool: return pl.Series((x in names for x in chain([first], it)), dtype=pl.Boolean) raise TypeError("Unsupported type for contains()") -======= - @overload - def contains(self, agents: int | AgentSet) -> bool: ... - - @overload - def contains(self, agents: IdsLike | Iterable[AgentSet]) -> pl.Series: ... - - def contains( - self, agents: IdsLike | AgentSet | Iterable[AgentSet] - ) -> bool | pl.Series: - if isinstance(agents, int): - return agents in self._ids - elif isinstance(agents, AgentSet): - return self._check_agentsets_presence([agents]).any() - elif isinstance(agents, Iterable): - if len(agents) == 0: - return True - elif isinstance(next(iter(agents)), AgentSet): - agents = cast(Iterable[AgentSet], agents) - return self._check_agentsets_presence(list(agents)) - else: # IdsLike - agents = cast(IdsLike, agents) - - return pl.Series(agents, dtype=pl.UInt64).is_in(self._ids) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 @overload def do( self, method_name: str, -<<<<<<< HEAD *args: Any, sets: AgentSetSelector | None = None, return_results: Literal[False] = False, inplace: bool = True, key_by: KeyBy = "name", **kwargs: Any, -======= - *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, - return_results: Literal[False] = False, - inplace: bool = True, - **kwargs, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 ) -> Self: ... @overload def do( self, method_name: str, -<<<<<<< HEAD *args: Any, sets: AgentSetSelector, return_results: Literal[True], @@ -492,19 +407,10 @@ def do( key_by: KeyBy = "name", **kwargs: Any, ) -> dict[str, Any] | dict[int, Any] | dict[type[AgentSet], Any]: ... -======= - *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, - return_results: Literal[True], - inplace: bool = True, - **kwargs, - ) -> dict[AgentSet, Any]: ... ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def do( self, method_name: str, -<<<<<<< HEAD *args: Any, sets: AgentSetSelector = None, return_results: bool = False, @@ -590,168 +496,6 @@ def remove( return obj def shuffle(self, inplace: bool = False) -> Self: -======= - *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, - return_results: bool = False, - inplace: bool = True, - **kwargs, - ) -> Self | Any: - obj = self._get_obj(inplace) - agentsets_masks = obj._get_bool_masks(mask) - if return_results: - return { - agentset: agentset.do( - method_name, - *args, - mask=mask, - return_results=return_results, - **kwargs, - inplace=inplace, - ) - for agentset, mask in agentsets_masks.items() - } - else: - obj._agentsets = [ - agentset.do( - method_name, - *args, - mask=mask, - return_results=return_results, - **kwargs, - inplace=inplace, - ) - for agentset, mask in agentsets_masks.items() - ] - return obj - - def get( - self, - attr_names: str | Collection[str] | None = None, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, - ) -> dict[AgentSet, Series] | dict[AgentSet, DataFrame]: - agentsets_masks = self._get_bool_masks(mask) - result = {} - - # Convert attr_names to list for consistent checking - if attr_names is None: - # None means get all data - no column filtering needed - required_columns = [] - elif isinstance(attr_names, str): - required_columns = [attr_names] - else: - required_columns = list(attr_names) - - for agentset, mask in agentsets_masks.items(): - # Fast column existence check - no data processing, just property access - agentset_columns = agentset.df.columns - - # Check if all required columns exist in this agent set - if not required_columns or all( - col in agentset_columns for col in required_columns - ): - result[agentset] = agentset.get(attr_names, mask) - - return result - - def remove( - self, - agents: AgentSet | Iterable[AgentSet] | IdsLike, - inplace: bool = True, - ) -> Self: - obj = self._get_obj(inplace) - if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): - return obj - if isinstance(agents, AgentSet): - agents = [agents] - if isinstance(agents, Iterable) and isinstance(next(iter(agents)), AgentSet): - # We have to get the index of the original AgentSet because the copy made AgentSets with different hash - ids = [self._agentsets.index(agentset) for agentset in iter(agents)] - ids.sort(reverse=True) - removed_ids = pl.Series(dtype=pl.UInt64) - for id in ids: - removed_ids = pl.concat( - [ - removed_ids, - pl.Series(obj._agentsets[id]["unique_id"], dtype=pl.UInt64), - ] - ) - obj._agentsets.pop(id) - - else: # IDsLike - if isinstance(agents, (int, np.uint64)): - agents = [agents] - elif isinstance(agents, DataFrame): - agents = agents["unique_id"] - removed_ids = pl.Series(agents, dtype=pl.UInt64) - deleted = 0 - - for agentset in obj._agentsets: - initial_len = len(agentset) - agentset._discard(removed_ids) - deleted += initial_len - len(agentset) - if deleted == len(removed_ids): - break - if deleted < len(removed_ids): # TODO: fix type hint - raise KeyError( - "There exist some IDs which are not present in any agentset" - ) - try: - obj.space.remove_agents(removed_ids, inplace=True) - except ValueError: - pass - obj._ids = obj._ids.filter(obj._ids.is_in(removed_ids).not_()) - return obj - - def select( - self, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, - filter_func: Callable[[AgentSet], AgentMask] | None = None, - n: int | None = None, - inplace: bool = True, - negate: bool = False, - ) -> Self: - obj = self._get_obj(inplace) - agentsets_masks = obj._get_bool_masks(mask) - if n is not None: - n = n // len(agentsets_masks) - obj._agentsets = [ - agentset.select( - mask=mask, filter_func=filter_func, n=n, negate=negate, inplace=inplace - ) - for agentset, mask in agentsets_masks.items() - ] - return obj - - def set( - self, - attr_names: str | dict[AgentSet, Any] | Collection[str], - values: Any | None = None, - mask: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] = None, - inplace: bool = True, - ) -> Self: - obj = self._get_obj(inplace) - agentsets_masks = obj._get_bool_masks(mask) - if isinstance(attr_names, dict): - for agentset, values in attr_names.items(): - if not inplace: - # We have to get the index of the original AgentSet because the copy made AgentSets with different hash - id = self._agentsets.index(agentset) - agentset = obj._agentsets[id] - agentset.set( - attr_names=values, mask=agentsets_masks[agentset], inplace=True - ) - else: - obj._agentsets = [ - agentset.set( - attr_names=attr_names, values=values, mask=mask, inplace=True - ) - for agentset, mask in agentsets_masks.items() - ] - return obj - - def shuffle(self, inplace: bool = True) -> Self: ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 obj = self._get_obj(inplace) obj._agentsets = [agentset.shuffle(inplace=True) for agentset in obj._agentsets] return obj @@ -761,11 +505,7 @@ def sort( by: str | Sequence[str], ascending: bool | Sequence[bool] = True, inplace: bool = True, -<<<<<<< HEAD **kwargs: Any, -======= - **kwargs, ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 ) -> Self: obj = self._get_obj(inplace) obj._agentsets = [ @@ -774,26 +514,6 @@ def sort( ] return obj -<<<<<<< HEAD -======= - def step(self, inplace: bool = True) -> Self: - """Advance the state of the agents in the AgentSetRegistry by one step. - - Parameters - ---------- - inplace : bool, optional - Whether to update the AgentSetRegistry in place, by default True - - Returns - ------- - Self - """ - obj = self._get_obj(inplace) - for agentset in obj._agentsets: - agentset.step() - return obj - ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def _check_ids_presence(self, other: list[AgentSet]) -> pl.DataFrame: """Check if the IDs of the agents to be added are unique. @@ -850,7 +570,6 @@ def _check_agentsets_presence(self, other: list[AgentSet]) -> pl.Series: [agentset in other_set for agentset in self._agentsets], dtype=pl.Boolean ) -<<<<<<< HEAD def _recompute_ids(self) -> None: """Rebuild the registry-level `unique_id` cache from current AgentSets. @@ -899,19 +618,6 @@ def _resolve_selector(self, selector: AgentSetSelector = None) -> list[AgentSet] seen.add(s) result.append(s) return result -======= - def _get_bool_masks( - self, - mask: (AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask]) = None, - ) -> dict[AgentSet, BoolSeries]: - return_dictionary = {} - if not isinstance(mask, dict): - # No need to convert numpy integers - let polars handle them directly - mask = {agentset: mask for agentset in self._agentsets} - for agentset, mask_value in mask.items(): - return_dictionary[agentset] = agentset._get_bool_mask(mask_value) - return return_dictionary ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def _return_agentsets_list( self, agentsets: AgentSet | Iterable[AgentSet] @@ -928,7 +634,6 @@ def _return_agentsets_list( """ return [agentsets] if isinstance(agentsets, AgentSet) else list(agentsets) -<<<<<<< HEAD def _generate_name(self, base_name: str) -> str: """Generate a unique name for an agent set.""" existing_names = [ @@ -957,101 +662,10 @@ def __iter__(self) -> Iterator[AgentSet]: def __len__(self) -> int: return len(self._agentsets) -======= - def __add__(self, other: AgentSet | Iterable[AgentSet]) -> Self: - """Add AgentSets to a new AgentSetRegistry through the + operator. - - Parameters - ---------- - other : AgentSet | Iterable[AgentSet] - The AgentSets to add. - - Returns - ------- - Self - A new AgentSetRegistry with the added AgentSets. - """ - return super().__add__(other) - - def __getattr__(self, name: str) -> dict[AgentSet, Any]: - # Avoids infinite recursion of private attributes - if __debug__: # Only execute in non-optimized mode - if name.startswith("_"): - raise AttributeError( - f"'{self.__class__.__name__}' object has no attribute '{name}'" - ) - return {agentset: getattr(agentset, name) for agentset in self._agentsets} - - @overload - def __getitem__( - self, key: str | tuple[dict[AgentSet, AgentMask], str] - ) -> dict[AgentSet, Series | pl.Expr]: ... - - @overload - def __getitem__( - self, - key: ( - Collection[str] - | AgnosticAgentMask - | IdsLike - | tuple[dict[AgentSet, AgentMask], Collection[str]] - ), - ) -> dict[AgentSet, DataFrame]: ... - - def __getitem__( - self, - key: ( - str - | Collection[str] - | AgnosticAgentMask - | IdsLike - | tuple[dict[AgentSet, AgentMask], str] - | tuple[dict[AgentSet, AgentMask], Collection[str]] - ), - ) -> dict[AgentSet, Series | pl.Expr] | dict[AgentSet, DataFrame]: - return super().__getitem__(key) - - def __iadd__(self, agents: AgentSet | Iterable[AgentSet]) -> Self: - """Add AgentSets to the AgentSetRegistry through the += operator. - - Parameters - ---------- - agents : AgentSet | Iterable[AgentSet] - The AgentSets to add. - - Returns - ------- - Self - The updated AgentSetRegistry. - """ - return super().__iadd__(agents) - - def __iter__(self) -> Iterator[dict[str, Any]]: - return (agent for agentset in self._agentsets for agent in iter(agentset)) - - def __isub__(self, agents: AgentSet | Iterable[AgentSet] | IdsLike) -> Self: - """Remove AgentSets from the AgentSetRegistry through the -= operator. - - Parameters - ---------- - agents : AgentSet | Iterable[AgentSet] | IdsLike - The AgentSets or agent IDs to remove. - - Returns - ------- - Self - The updated AgentSetRegistry. - """ - return super().__isub__(agents) - - def __len__(self) -> int: - return sum(len(agentset._df) for agentset in self._agentsets) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def __repr__(self) -> str: return "\n".join([repr(agentset) for agentset in self._agentsets]) -<<<<<<< HEAD def __reversed__(self) -> Iterator[AgentSet]: return reversed(self._agentsets) @@ -1089,33 +703,10 @@ def __setitem__(self, key: int | str, value: AgentSet) -> None: raise TypeError("Key must be int index or str name") # Recompute ids cache self._recompute_ids() -======= - def __reversed__(self) -> Iterator: - return ( - agent - for agentset in self._agentsets - for agent in reversed(agentset._backend) - ) - - def __setitem__( - self, - key: ( - str - | Collection[str] - | AgnosticAgentMask - | IdsLike - | tuple[dict[AgentSet, AgentMask], str] - | tuple[dict[AgentSet, AgentMask], Collection[str]] - ), - values: Any, - ) -> None: - super().__setitem__(key, values) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def __str__(self) -> str: return "\n".join([str(agentset) for agentset in self._agentsets]) -<<<<<<< HEAD def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: if key_by not in ("name", "index", "type"): raise ValueError("key_by must be 'name'|'index'|'type'") @@ -1176,78 +767,3 @@ def __getitem__(self, key: int | str | type[AgentSet]) -> AgentSet | list[AgentS if isinstance(key, type) and issubclass(key, AgentSet): return [s for s in self._agentsets if isinstance(s, key)] raise TypeError("Key must be int, str (name), or AgentSet type") -======= - def __sub__(self, agents: AgentSet | Iterable[AgentSet] | IdsLike) -> Self: - """Remove AgentSets from a new AgentSetRegistry through the - operator. - - Parameters - ---------- - agents : AgentSet | Iterable[AgentSet] | IdsLike - The AgentSets or agent IDs to remove. Supports NumPy integer types. - - Returns - ------- - Self - A new AgentSetRegistry with the removed AgentSets. - """ - return super().__sub__(agents) - - @property - def df(self) -> dict[AgentSet, DataFrame]: - return {agentset: agentset.df for agentset in self._agentsets} - - @df.setter - def df(self, other: Iterable[AgentSet]) -> None: - """Set the agents in the AgentSetRegistry. - - Parameters - ---------- - other : Iterable[AgentSet] - The AgentSets to set. - """ - self._agentsets = list(other) - - @property - def active_agents(self) -> dict[AgentSet, DataFrame]: - return {agentset: agentset.active_agents for agentset in self._agentsets} - - @active_agents.setter - def active_agents( - self, agents: AgnosticAgentMask | IdsLike | dict[AgentSet, AgentMask] - ) -> None: - self.select(agents, inplace=True) - - @property - def agentsets_by_type(self) -> dict[type[AgentSet], Self]: - """Get the agent sets in the AgentSetRegistry grouped by type. - - Returns - ------- - dict[type[AgentSet], Self] - A dictionary mapping agent set types to the corresponding AgentSetRegistry. - """ - - def copy_without_agentsets() -> Self: - return self.copy(deep=False, skip=["_agentsets"]) - - dictionary = defaultdict(copy_without_agentsets) - - for agentset in self._agentsets: - agents_df = dictionary[agentset.__class__] - agents_df._agentsets = [] - agents_df._agentsets = agents_df._agentsets + [agentset] - dictionary[agentset.__class__] = agents_df - return dictionary - - @property - def inactive_agents(self) -> dict[AgentSet, DataFrame]: - return {agentset: agentset.inactive_agents for agentset in self._agentsets} - - @property - def index(self) -> dict[AgentSet, Index]: - return {agentset: agentset.index for agentset in self._agentsets} - - @property - def pos(self) -> dict[AgentSet, DataFrame]: - return {agentset: agentset.pos for agentset in self._agentsets} ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 diff --git a/mesa_frames/concrete/datacollector.py b/mesa_frames/concrete/datacollector.py index 6306fda6..cd2cc72e 100644 --- a/mesa_frames/concrete/datacollector.py +++ b/mesa_frames/concrete/datacollector.py @@ -191,7 +191,6 @@ def _is_str_collection(x: Any) -> bool: agent_data_dict: dict[str, pl.Series] = {} for col_name, reporter in self._agent_reporters.items(): -<<<<<<< HEAD # 1) String or collection[str]: shorthand to fetch columns if isinstance(reporter, str) or _is_str_collection(reporter): # If a single string, fetch that attribute from each set @@ -266,13 +265,6 @@ def _is_str_collection(x: Any) -> bool: "agent_reporters values must be str, collection[str], or callable" ) -======= - if isinstance(reporter, str): - for k, v in self._model.sets[reporter].items(): - agent_data_dict[col_name + "_" + str(k.__class__.__name__)] = v - else: - agent_data_dict[col_name] = reporter(self._model) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 agent_lazy_frame = pl.LazyFrame(agent_data_dict) agent_lazy_frame = agent_lazy_frame.with_columns( [ @@ -552,7 +544,6 @@ def _validate_reporter_table_columns( ValueError If any expected columns are missing from the table. """ -<<<<<<< HEAD def _is_str_collection(x: Any) -> bool: try: @@ -582,17 +573,6 @@ def _is_str_collection(x: Any) -> bool: # Callable: conservative default → require 'col_name' to exist # We cannot know the dynamic column explosion without running model code safely here. expected_columns.add(col_name.lower()) -======= - expected_columns = set() - for col_name, required_column in reporter.items(): - if isinstance(required_column, str): - for k, v in self._model.sets[required_column].items(): - expected_columns.add( - (col_name + "_" + str(k.__class__.__name__)).lower() - ) - else: - expected_columns.add(col_name.lower()) ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 query = f""" SELECT column_name diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index 704f8681..b91db207 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -64,11 +64,7 @@ class Model: running: bool _seed: int | Sequence[int] _sets: AgentSetRegistry # Where the agent sets are stored -<<<<<<< HEAD _space: Space | None # This will be a Space object -======= - _space: Space | None # This will be a MultiSpaceDF object ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def __init__(self, seed: int | Sequence[int] | None = None) -> None: """Create a new model. @@ -103,27 +99,6 @@ def steps(self) -> int: """Get the current step count.""" return self._steps -<<<<<<< HEAD -======= - def get_sets_of_type(self, agent_type: type) -> AgentSet: - """Retrieve the AgentSet of a specified type. - - Parameters - ---------- - agent_type : type - The type of AgentSet to retrieve. - - Returns - ------- - AgentSet - The AgentSet of the specified type. - """ - for agentset in self._sets._agentsets: - if isinstance(agentset, agent_type): - return agentset - raise ValueError(f"No agent sets of type {agent_type} found in the model.") - ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def reset_randomizer(self, seed: int | Sequence[int] | None) -> None: """Reset the model random number generator. @@ -151,12 +126,8 @@ def step(self) -> None: The default method calls the step() method of all agents. Overload as needed. """ -<<<<<<< HEAD # Invoke step on all contained AgentSets via the public registry API self.sets.do("step") -======= - self.sets.step() ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 @property def steps(self) -> int: @@ -200,20 +171,6 @@ def sets(self, sets: AgentSetRegistry) -> None: self._sets = sets @property -<<<<<<< HEAD -======= - def set_types(self) -> list[type]: - """Get a list of different agent set types present in the model. - - Returns - ------- - list[type] - A list of the different agent set types present in the model. - """ - return [agent.__class__ for agent in self._sets._agentsets] - - @property ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 def space(self) -> Space: """Get the space object associated with the model. diff --git a/tests/test_datacollector.py b/tests/test_datacollector.py index b86a8f08..b2ac3279 100644 --- a/tests/test_datacollector.py +++ b/tests/test_datacollector.py @@ -164,11 +164,7 @@ def test_collect(self, fix1_model): ) }, agent_reporters={ -<<<<<<< HEAD "wealth": lambda sets: sets[0]["wealth"], -======= - "wealth": lambda model: model.sets._agentsets[0]["wealth"], ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "age": "age", }, ) @@ -227,11 +223,7 @@ def test_collect_step(self, fix1_model): ) }, agent_reporters={ -<<<<<<< HEAD "wealth": lambda sets: sets[0]["wealth"], -======= - "wealth": lambda model: model.sets._agentsets[0]["wealth"], ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "age": "age", }, ) @@ -287,11 +279,7 @@ def test_conditional_collect(self, fix1_model): ) }, agent_reporters={ -<<<<<<< HEAD "wealth": lambda sets: sets[0]["wealth"], -======= - "wealth": lambda model: model.sets._agentsets[0]["wealth"], ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "age": "age", }, ) @@ -373,11 +361,7 @@ def test_flush_local_csv(self, fix1_model): ) }, agent_reporters={ -<<<<<<< HEAD "wealth": lambda sets: sets[0]["wealth"], -======= - "wealth": lambda model: model.sets._agentsets[0]["wealth"], ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "age": "age", }, storage="csv", @@ -453,11 +437,7 @@ def test_flush_local_parquet(self, fix1_model): ) }, agent_reporters={ -<<<<<<< HEAD "wealth": lambda sets: sets[0]["wealth"], -======= - "wealth": lambda model: model.sets._agentsets[0]["wealth"], ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 }, storage="parquet", storage_uri=tmpdir, @@ -533,11 +513,7 @@ def test_postgress(self, fix1_model, postgres_uri): ) }, agent_reporters={ -<<<<<<< HEAD "wealth": lambda sets: sets[0]["wealth"], -======= - "wealth": lambda model: model.sets._agentsets[0]["wealth"], ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "age": "age", }, storage="postgresql", @@ -586,11 +562,7 @@ def test_batch_memory(self, fix2_model): ) }, agent_reporters={ -<<<<<<< HEAD "wealth": lambda sets: sets[0]["wealth"], -======= - "wealth": lambda model: model.sets._agentsets[0]["wealth"], ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "age": "age", }, ) @@ -735,11 +707,7 @@ def test_batch_save(self, fix2_model): ) }, agent_reporters={ -<<<<<<< HEAD "wealth": lambda sets: sets[0]["wealth"], -======= - "wealth": lambda model: model.sets._agentsets[0]["wealth"], ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 "age": "age", }, storage="csv", diff --git a/tests/test_grid.py b/tests/test_grid.py index c08910ff..904efdb0 100644 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -12,15 +12,8 @@ def get_unique_ids(model: Model) -> pl.Series: -<<<<<<< HEAD # Collect unique_id across all concrete AgentSets in the registry series_list = [aset["unique_id"].cast(pl.UInt64) for aset in model.sets] -======= - # return model.get_sets_of_type(model.set_types[0])["unique_id"] - series_list = [ - agent_set["unique_id"].cast(pl.UInt64) for agent_set in model.sets.df.values() - ] ->>>>>>> 51c54cd666d876a5debb1b7dd71556ee9c458956 return pl.concat(series_list) From d7d5ca82d4414914194d20c441aaeb5aa59927a1 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 12:22:34 +0200 Subject: [PATCH 139/329] remove old test_agents.py --- tests/test_agents.py | 1036 ------------------------------------------ 1 file changed, 1036 deletions(-) delete mode 100644 tests/test_agents.py diff --git a/tests/test_agents.py b/tests/test_agents.py deleted file mode 100644 index f43d94f6..00000000 --- a/tests/test_agents.py +++ /dev/null @@ -1,1036 +0,0 @@ -from copy import copy, deepcopy - -import polars as pl -import pytest - -from mesa_frames import AgentSetRegistry, Model -from mesa_frames import AgentSet -from mesa_frames.types_ import AgentMask -from tests.test_agentset import ( - ExampleAgentSet, - ExampleAgentSetNoWealth, - fix1_AgentSet_no_wealth, - fix1_AgentSet, - fix2_AgentSet, - fix3_AgentSet, -) - - -@pytest.fixture -def fix_AgentSetRegistry( - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, -) -> AgentSetRegistry: - model = Model() - agents = AgentSetRegistry(model) - agents.add([fix1_AgentSet, fix2_AgentSet]) - return agents - - -class Test_AgentSetRegistry: - def test___init__(self): - model = Model() - agents = AgentSetRegistry(model) - assert agents.model == model - assert isinstance(agents._agentsets, list) - assert len(agents._agentsets) == 0 - assert isinstance(agents._ids, pl.Series) - assert agents._ids.is_empty() - assert agents._ids.name == "unique_id" - - def test_add( - self, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - model = Model() - agents = AgentSetRegistry(model) - agentset_polars1 = fix1_AgentSet - agentset_polars2 = fix2_AgentSet - - # Test with a single AgentSet - result = agents.add(agentset_polars1, inplace=False) - assert result._agentsets[0] is agentset_polars1 - assert result._ids.to_list() == agentset_polars1._df["unique_id"].to_list() - - # Test with a list of AgentSets - result = agents.add([agentset_polars1, agentset_polars2], inplace=True) - assert result._agentsets[0] is agentset_polars1 - assert result._agentsets[1] is agentset_polars2 - assert ( - result._ids.to_list() - == agentset_polars1._df["unique_id"].to_list() - + agentset_polars2._df["unique_id"].to_list() - ) - - # Test if adding the same AgentSet raises ValueError - with pytest.raises(ValueError): - agents.add(agentset_polars1, inplace=False) - - def test_contains( - self, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - fix3_AgentSet: ExampleAgentSet, - fix_AgentSetRegistry: AgentSetRegistry, - ): - agents = fix_AgentSetRegistry - agentset_polars1 = agents._agentsets[0] - - # Test with an AgentSet - assert agents.contains(agentset_polars1) - assert agents.contains(fix1_AgentSet) - assert agents.contains(fix2_AgentSet) - - # Test with an AgentSet not present - assert not agents.contains(fix3_AgentSet) - - # Test with an iterable of AgentSets - assert agents.contains([agentset_polars1, fix3_AgentSet]).to_list() == [ - True, - False, - ] - - # Test with single id - assert agents.contains(agentset_polars1["unique_id"][0]) - - # Test with a list of ids - assert agents.contains([agentset_polars1["unique_id"][0], 0]).to_list() == [ - True, - False, - ] - - def test_copy(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - agents.test_list = [[1, 2, 3]] - - # Test with deep=False - agents2 = agents.copy(deep=False) - agents2.test_list[0].append(4) - assert agents.test_list[0][-1] == agents2.test_list[0][-1] - assert agents.model == agents2.model - assert agents._agentsets[0] == agents2._agentsets[0] - assert (agents._ids == agents2._ids).all() - - # Test with deep=True - agents2 = fix_AgentSetRegistry.copy(deep=True) - agents2.test_list[0].append(4) - assert agents.test_list[-1] != agents2.test_list[-1] - assert agents.model == agents2.model - assert agents._agentsets[0] != agents2._agentsets[0] - assert (agents._ids == agents2._ids).all() - - def test_discard( - self, fix_AgentSetRegistry: AgentSetRegistry, fix2_AgentSet: ExampleAgentSet - ): - agents = fix_AgentSetRegistry - # Test with a single AgentSet - agentset_polars2 = agents._agentsets[1] - result = agents.discard(agents._agentsets[0], inplace=False) - assert isinstance(result._agentsets[0], ExampleAgentSet) - assert len(result._agentsets) == 1 - - # Test with a list of AgentSets - result = agents.discard(agents._agentsets.copy(), inplace=False) - assert len(result._agentsets) == 0 - - # Test with IDs - ids = [ - agents._agentsets[0]._df["unique_id"][0], - agents._agentsets[1]._df["unique_id"][0], - ] - agentset_polars1 = agents._agentsets[0] - agentset_polars2 = agents._agentsets[1] - result = agents.discard(ids, inplace=False) - assert ( - result._agentsets[0]["unique_id"][0] - == agentset_polars1._df.select("unique_id").row(1)[0] - ) - assert ( - result._agentsets[1].df["unique_id"][0] - == agentset_polars2._df["unique_id"][1] - ) - - # Test if removing an AgentSet not present raises ValueError - result = agents.discard(fix2_AgentSet, inplace=False) - - # Test if removing an ID not present raises KeyError - assert 0 not in agents._ids - result = agents.discard(0, inplace=False) - - def test_do(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - expected_result_0 = agents._agentsets[0].df["wealth"] - expected_result_0 += 1 - - expected_result_1 = agents._agentsets[1].df["wealth"] - expected_result_1 += 1 - - # Test with no return_results, no mask, inplace - agents.do("add_wealth", 1) - assert ( - agents._agentsets[0].df["wealth"].to_list() == expected_result_0.to_list() - ) - assert ( - agents._agentsets[1].df["wealth"].to_list() == expected_result_1.to_list() - ) - - # Test with return_results=True, no mask, inplace - expected_result_0 = agents._agentsets[0].df["wealth"] - expected_result_0 += 1 - - expected_result_1 = agents._agentsets[1].df["wealth"] - expected_result_1 += 1 - assert agents.do("add_wealth", 1, return_results=True) == { - agents._agentsets[0]: None, - agents._agentsets[1]: None, - } - assert ( - agents._agentsets[0].df["wealth"].to_list() == expected_result_0.to_list() - ) - assert ( - agents._agentsets[1].df["wealth"].to_list() == expected_result_1.to_list() - ) - - # Test with a mask, inplace - mask0 = agents._agentsets[0].df["wealth"] > 10 # No agent should be selected - mask1 = agents._agentsets[1].df["wealth"] > 10 # All agents should be selected - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - - expected_result_0 = agents._agentsets[0].df["wealth"] - expected_result_1 = agents._agentsets[1].df["wealth"] - expected_result_1 += 1 - - agents.do("add_wealth", 1, mask=mask_dictionary) - assert ( - agents._agentsets[0].df["wealth"].to_list() == expected_result_0.to_list() - ) - assert ( - agents._agentsets[1].df["wealth"].to_list() == expected_result_1.to_list() - ) - - def test_get( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - fix1_AgentSet_no_wealth: ExampleAgentSetNoWealth, - ): - agents = fix_AgentSetRegistry - - # Test with a single attribute - assert ( - agents.get("wealth")[fix1_AgentSet].to_list() - == fix1_AgentSet._df["wealth"].to_list() - ) - assert ( - agents.get("wealth")[fix2_AgentSet].to_list() - == fix2_AgentSet._df["wealth"].to_list() - ) - - # Test with a list of attributes - result = agents.get(["wealth", "age"]) - assert result[fix1_AgentSet].columns == ["wealth", "age"] - assert ( - result[fix1_AgentSet]["wealth"].to_list() - == fix1_AgentSet._df["wealth"].to_list() - ) - assert ( - result[fix1_AgentSet]["age"].to_list() == fix1_AgentSet._df["age"].to_list() - ) - - assert result[fix2_AgentSet].columns == ["wealth", "age"] - assert ( - result[fix2_AgentSet]["wealth"].to_list() - == fix2_AgentSet._df["wealth"].to_list() - ) - assert ( - result[fix2_AgentSet]["age"].to_list() == fix2_AgentSet._df["age"].to_list() - ) - - # Test with a single attribute and a mask - mask0 = fix1_AgentSet._df["wealth"] > fix1_AgentSet._df["wealth"][0] - mask1 = fix2_AgentSet._df["wealth"] > fix2_AgentSet._df["wealth"][0] - mask_dictionary = {fix1_AgentSet: mask0, fix2_AgentSet: mask1} - result = agents.get("wealth", mask=mask_dictionary) - assert ( - result[fix1_AgentSet].to_list() == fix1_AgentSet._df["wealth"].to_list()[1:] - ) - assert ( - result[fix2_AgentSet].to_list() == fix2_AgentSet._df["wealth"].to_list()[1:] - ) - - # Test heterogeneous agent sets (different columns) - # This tests the fix for the bug where agents_df["column"] would raise - # ColumnNotFoundError when some agent sets didn't have that column. - - # Create a new AgentSetRegistry with heterogeneous agent sets - model = Model() - hetero_agents = AgentSetRegistry(model) - hetero_agents.add([fix1_AgentSet, fix1_AgentSet_no_wealth]) - - # Test 1: Access column that exists in only one agent set - result_wealth = hetero_agents.get("wealth") - assert len(result_wealth) == 1, ( - "Should only return agent sets that have 'wealth'" - ) - assert fix1_AgentSet in result_wealth, ( - "Should include the agent set with wealth" - ) - assert fix1_AgentSet_no_wealth not in result_wealth, ( - "Should not include agent set without wealth" - ) - assert result_wealth[fix1_AgentSet].to_list() == [1, 2, 3, 4] - - # Test 2: Access column that exists in all agent sets - result_age = hetero_agents.get("age") - assert len(result_age) == 2, "Should return both agent sets that have 'age'" - assert fix1_AgentSet in result_age - assert fix1_AgentSet_no_wealth in result_age - assert result_age[fix1_AgentSet].to_list() == [10, 20, 30, 40] - assert result_age[fix1_AgentSet_no_wealth].to_list() == [1, 2, 3, 4] - - # Test 3: Access column that exists in no agent sets - result_nonexistent = hetero_agents.get("nonexistent_column") - assert len(result_nonexistent) == 0, ( - "Should return empty dict for non-existent column" - ) - - # Test 4: Access multiple columns (mixed availability) - result_multi = hetero_agents.get(["wealth", "age"]) - assert len(result_multi) == 1, ( - "Should only include agent sets that have ALL requested columns" - ) - assert fix1_AgentSet in result_multi - assert fix1_AgentSet_no_wealth not in result_multi - assert result_multi[fix1_AgentSet].columns == ["wealth", "age"] - - # Test 5: Access multiple columns where some exist in different sets - result_mixed = hetero_agents.get(["age", "income"]) - assert len(result_mixed) == 1, ( - "Should only include agent set that has both 'age' and 'income'" - ) - assert fix1_AgentSet_no_wealth in result_mixed - assert fix1_AgentSet not in result_mixed - - # Test 6: Test via __getitem__ syntax (the original bug report case) - wealth_via_getitem = hetero_agents["wealth"] - assert len(wealth_via_getitem) == 1 - assert fix1_AgentSet in wealth_via_getitem - assert wealth_via_getitem[fix1_AgentSet].to_list() == [1, 2, 3, 4] - - # Test 7: Test get(None) - should return all columns for all agent sets - result_none = hetero_agents.get(None) - assert len(result_none) == 2, ( - "Should return both agent sets when attr_names=None" - ) - assert fix1_AgentSet in result_none - assert fix1_AgentSet_no_wealth in result_none - - # Verify each agent set returns all its columns (excluding unique_id) - wealth_set_result = result_none[fix1_AgentSet] - assert isinstance(wealth_set_result, pl.DataFrame), ( - "Should return DataFrame when attr_names=None" - ) - expected_wealth_cols = {"wealth", "age"} # unique_id should be excluded - assert set(wealth_set_result.columns) == expected_wealth_cols - - no_wealth_set_result = result_none[fix1_AgentSet_no_wealth] - assert isinstance(no_wealth_set_result, pl.DataFrame), ( - "Should return DataFrame when attr_names=None" - ) - expected_no_wealth_cols = {"income", "age"} # unique_id should be excluded - assert set(no_wealth_set_result.columns) == expected_no_wealth_cols - - def test_remove( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix3_AgentSet: ExampleAgentSet, - ): - agents = fix_AgentSetRegistry - - # Test with a single AgentSet - agentset_polars = agents._agentsets[1] - result = agents.remove(agents._agentsets[0], inplace=False) - assert isinstance(result._agentsets[0], ExampleAgentSet) - assert len(result._agentsets) == 1 - - # Test with a list of AgentSets - result = agents.remove(agents._agentsets.copy(), inplace=False) - assert len(result._agentsets) == 0 - - # Test with IDs - ids = [ - agents._agentsets[0]._df["unique_id"][0], - agents._agentsets[1]._df["unique_id"][0], - ] - agentset_polars1 = agents._agentsets[0] - agentset_polars2 = agents._agentsets[1] - result = agents.remove(ids, inplace=False) - assert ( - result._agentsets[0]["unique_id"][0] - == agentset_polars1._df.select("unique_id").row(1)[0] - ) - assert ( - result._agentsets[1].df["unique_id"][0] - == agentset_polars2._df["unique_id"][1] - ) - - # Test if removing an AgentSet not present raises ValueError - with pytest.raises(ValueError): - result = agents.remove(fix3_AgentSet, inplace=False) - - # Test if removing an ID not present raises KeyError - assert 0 not in agents._ids - with pytest.raises(KeyError): - result = agents.remove(0, inplace=False) - - def test_select(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - # Test with default arguments. Should select all agents - selected = agents.select(inplace=False) - active_agents_dict = selected.active_agents - agents_dict = selected.df - assert active_agents_dict.keys() == agents_dict.keys() - # Using assert to compare all DataFrames in the dictionaries - - assert ( - list(active_agents_dict.values())[0].rows() - == list(agents_dict.values())[0].rows() - ) - - assert all( - series.all() - for series in ( - list(active_agents_dict.values())[1] == list(agents_dict.values())[1] - ) - ) - - # Test with a mask - mask0 = pl.Series("mask", [True, False, True, True], dtype=pl.Boolean) - mask1 = pl.Series("mask", [True, False, True, True], dtype=pl.Boolean) - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - selected = agents.select(mask_dictionary, inplace=False) - assert ( - selected.active_agents[selected._agentsets[0]]["wealth"].to_list()[0] - == agents._agentsets[0]["wealth"].to_list()[0] - ) - assert ( - selected.active_agents[selected._agentsets[0]]["wealth"].to_list()[-1] - == agents._agentsets[0]["wealth"].to_list()[-1] - ) - - assert ( - selected.active_agents[selected._agentsets[1]]["wealth"].to_list()[0] - == agents._agentsets[1]["wealth"].to_list()[0] - ) - assert ( - selected.active_agents[selected._agentsets[1]]["wealth"].to_list()[-1] - == agents._agentsets[1]["wealth"].to_list()[-1] - ) - - # Test with filter_func - - def filter_func(agentset: AgentSet) -> pl.Series: - return agentset.df["wealth"] > agentset.df["wealth"].to_list()[0] - - selected = agents.select(filter_func=filter_func, inplace=False) - assert ( - selected.active_agents[selected._agentsets[0]]["wealth"].to_list() - == agents._agentsets[0]["wealth"].to_list()[1:] - ) - assert ( - selected.active_agents[selected._agentsets[1]]["wealth"].to_list() - == agents._agentsets[1]["wealth"].to_list()[1:] - ) - - # Test with n - selected = agents.select(n=3, inplace=False) - assert sum(len(df) for df in selected.active_agents.values()) in [2, 3] - - # Test with n, filter_func and mask - selected = agents.select( - mask_dictionary, filter_func=filter_func, n=2, inplace=False - ) - assert any( - el in selected.active_agents[selected._agentsets[0]]["wealth"].to_list() - for el in agents.active_agents[agents._agentsets[0]]["wealth"].to_list()[ - 2:4 - ] - ) - - assert any( - el in selected.active_agents[selected._agentsets[1]]["wealth"].to_list() - for el in agents.active_agents[agents._agentsets[1]]["wealth"].to_list()[ - 2:4 - ] - ) - - def test_set(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - # Test with a single attribute - result = agents.set("wealth", 0, inplace=False) - assert result._agentsets[0].df["wealth"].to_list() == [0] * len( - agents._agentsets[0] - ) - assert result._agentsets[1].df["wealth"].to_list() == [0] * len( - agents._agentsets[1] - ) - - # Test with a list of attributes - agents.set(["wealth", "age"], 1, inplace=True) - assert agents._agentsets[0].df["wealth"].to_list() == [1] * len( - agents._agentsets[0] - ) - assert agents._agentsets[0].df["age"].to_list() == [1] * len( - agents._agentsets[0] - ) - - # Test with a single attribute and a mask - mask0 = pl.Series( - "mask", [True] + [False] * (len(agents._agentsets[0]) - 1), dtype=pl.Boolean - ) - mask1 = pl.Series( - "mask", [True] + [False] * (len(agents._agentsets[1]) - 1), dtype=pl.Boolean - ) - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - result = agents.set("wealth", 0, mask=mask_dictionary, inplace=False) - assert result._agentsets[0].df["wealth"].to_list() == [0] + [1] * ( - len(agents._agentsets[0]) - 1 - ) - assert result._agentsets[1].df["wealth"].to_list() == [0] + [1] * ( - len(agents._agentsets[1]) - 1 - ) - - # Test with a dictionary - agents.set( - {agents._agentsets[0]: {"wealth": 0}, agents._agentsets[1]: {"wealth": 1}}, - inplace=True, - ) - assert agents._agentsets[0].df["wealth"].to_list() == [0] * len( - agents._agentsets[0] - ) - assert agents._agentsets[1].df["wealth"].to_list() == [1] * len( - agents._agentsets[1] - ) - - def test_shuffle(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - for _ in range(100): - original_order_0 = agents._agentsets[0].df["unique_id"].to_list() - original_order_1 = agents._agentsets[1].df["unique_id"].to_list() - agents.shuffle(inplace=True) - if ( - original_order_0 != agents._agentsets[0].df["unique_id"].to_list() - and original_order_1 != agents._agentsets[1].df["unique_id"].to_list() - ): - return - assert False - - def test_sort(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - agents.sort("wealth", ascending=False, inplace=True) - assert pl.Series(agents._agentsets[0].df["wealth"]).is_sorted(descending=True) - assert pl.Series(agents._agentsets[1].df["wealth"]).is_sorted(descending=True) - - def test_step( - self, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - fix_AgentSetRegistry: AgentSetRegistry, - ): - previous_wealth_0 = fix1_AgentSet._df["wealth"].clone() - previous_wealth_1 = fix2_AgentSet._df["wealth"].clone() - - agents = fix_AgentSetRegistry - agents.step() - - assert ( - agents._agentsets[0].df["wealth"].to_list() - == (previous_wealth_0 + 1).to_list() - ) - assert ( - agents._agentsets[1].df["wealth"].to_list() - == (previous_wealth_1 + 1).to_list() - ) - - def test__check_ids_presence( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - agents = fix_AgentSetRegistry.remove(fix2_AgentSet, inplace=False) - agents_different_index = deepcopy(fix2_AgentSet) - result = agents._check_ids_presence([fix1_AgentSet]) - assert result.filter(pl.col("unique_id").is_in(fix1_AgentSet._df["unique_id"]))[ - "present" - ].all() - - assert not result.filter( - pl.col("unique_id").is_in(agents_different_index._df["unique_id"]) - )["present"].any() - - def test__check_agentsets_presence( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix3_AgentSet: ExampleAgentSet, - ): - agents = fix_AgentSetRegistry - result = agents._check_agentsets_presence([fix1_AgentSet, fix3_AgentSet]) - assert result[0] - assert not result[1] - - def test__get_bool_masks(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - # Test with mask = None - result = agents._get_bool_masks(mask=None) - truth_value = True - for i, mask in enumerate(result.values()): - if isinstance(mask, pl.Expr): - mask = agents._agentsets[i]._df.select(mask).to_series() - truth_value &= mask.all() - assert truth_value - - # Test with mask = "all" - result = agents._get_bool_masks(mask="all") - truth_value = True - for i, mask in enumerate(result.values()): - if isinstance(mask, pl.Expr): - mask = agents._agentsets[i]._df.select(mask).to_series() - truth_value &= mask.all() - assert truth_value - - # Test with mask = "active" - mask0 = ( - agents._agentsets[0].df["wealth"] - > agents._agentsets[0].df["wealth"].to_list()[0] - ) - mask1 = agents._agentsets[1].df["wealth"] > agents._agentsets[1].df["wealth"][0] - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - agents.select(mask=mask_dictionary) - result = agents._get_bool_masks(mask="active") - assert result[agents._agentsets[0]].to_list() == mask0.to_list() - assert result[agents._agentsets[1]].to_list() == mask1.to_list() - - # Test with mask = IdsLike - result = agents._get_bool_masks( - mask=[ - agents._agentsets[0]["unique_id"][0], - agents._agentsets[1].df["unique_id"][0], - ] - ) - assert result[agents._agentsets[0]].to_list() == [True] + [False] * ( - len(agents._agentsets[0]) - 1 - ) - assert result[agents._agentsets[1]].to_list() == [True] + [False] * ( - len(agents._agentsets[1]) - 1 - ) - - # Test with mask = dict[AgentSet, AgentMask] - result = agents._get_bool_masks(mask=mask_dictionary) - assert result[agents._agentsets[0]].to_list() == mask0.to_list() - assert result[agents._agentsets[1]].to_list() == mask1.to_list() - - def test__get_obj(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - assert agents._get_obj(inplace=True) is agents - assert agents._get_obj(inplace=False) is not agents - - def test__return_agentsets_list( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - agents = fix_AgentSetRegistry - result = agents._return_agentsets_list(fix1_AgentSet) - assert result == [fix1_AgentSet] - result = agents._return_agentsets_list([fix1_AgentSet, fix2_AgentSet]) - assert result == [fix1_AgentSet, fix2_AgentSet] - - def test___add__( - self, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - model = Model() - agents = AgentSetRegistry(model) - agentset_polars1 = fix1_AgentSet - agentset_polars2 = fix2_AgentSet - - # Test with a single AgentSet - result = agents + agentset_polars1 - assert result._agentsets[0] is agentset_polars1 - assert result._ids.to_list() == agentset_polars1._df["unique_id"].to_list() - - # Test with a single AgentSet same as above - result = agents + agentset_polars2 - assert result._agentsets[0] is agentset_polars2 - assert result._ids.to_list() == agentset_polars2._df["unique_id"].to_list() - - # Test with a list of AgentSets - result = agents + [agentset_polars1, agentset_polars2] - assert result._agentsets[0] is agentset_polars1 - assert result._agentsets[1] is agentset_polars2 - assert ( - result._ids.to_list() - == agentset_polars1._df["unique_id"].to_list() - + agentset_polars2._df["unique_id"].to_list() - ) - - # Test if adding the same AgentSet raises ValueError - with pytest.raises(ValueError): - result + agentset_polars1 - - def test___contains__( - self, fix_AgentSetRegistry: AgentSetRegistry, fix3_AgentSet: ExampleAgentSet - ): - # Test with a single value - agents = fix_AgentSetRegistry - agentset_polars1 = agents._agentsets[0] - - # Test with an AgentSet - assert agentset_polars1 in agents - # Test with an AgentSet not present - assert fix3_AgentSet not in agents - - # Test with single id present - assert agentset_polars1["unique_id"][0] in agents - - # Test with single id not present - assert 0 not in agents - - def test___copy__(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - agents.test_list = [[1, 2, 3]] - - # Test with deep=False - agents2 = copy(agents) - agents2.test_list[0].append(4) - assert agents.test_list[0][-1] == agents2.test_list[0][-1] - assert agents.model == agents2.model - assert agents._agentsets[0] == agents2._agentsets[0] - assert (agents._ids == agents2._ids).all() - - def test___deepcopy__(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - agents.test_list = [[1, 2, 3]] - - agents2 = deepcopy(agents) - agents2.test_list[0].append(4) - assert agents.test_list[-1] != agents2.test_list[-1] - assert agents.model == agents2.model - assert agents._agentsets[0] != agents2._agentsets[0] - assert (agents._ids == agents2._ids).all() - - def test___getattr__(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - assert isinstance(agents.model, Model) - result = agents.wealth - assert ( - result[agents._agentsets[0]].to_list() - == agents._agentsets[0].df["wealth"].to_list() - ) - assert ( - result[agents._agentsets[1]].to_list() - == agents._agentsets[1].df["wealth"].to_list() - ) - - def test___getitem__( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - agents = fix_AgentSetRegistry - - # Test with a single attribute - assert ( - agents["wealth"][fix1_AgentSet].to_list() - == fix1_AgentSet._df["wealth"].to_list() - ) - assert ( - agents["wealth"][fix2_AgentSet].to_list() - == fix2_AgentSet._df["wealth"].to_list() - ) - - # Test with a list of attributes - result = agents[["wealth", "age"]] - assert result[fix1_AgentSet].columns == ["wealth", "age"] - assert ( - result[fix1_AgentSet]["wealth"].to_list() - == fix1_AgentSet._df["wealth"].to_list() - ) - assert ( - result[fix1_AgentSet]["age"].to_list() == fix1_AgentSet._df["age"].to_list() - ) - assert result[fix2_AgentSet].columns == ["wealth", "age"] - assert ( - result[fix2_AgentSet]["wealth"].to_list() - == fix2_AgentSet._df["wealth"].to_list() - ) - assert ( - result[fix2_AgentSet]["age"].to_list() == fix2_AgentSet._df["age"].to_list() - ) - - # Test with a single attribute and a mask - mask0 = fix1_AgentSet._df["wealth"] > fix1_AgentSet._df["wealth"][0] - mask1 = fix2_AgentSet._df["wealth"] > fix2_AgentSet._df["wealth"][0] - mask_dictionary: dict[AgentSet, AgentMask] = { - fix1_AgentSet: mask0, - fix2_AgentSet: mask1, - } - result = agents[mask_dictionary, "wealth"] - assert ( - result[fix1_AgentSet].to_list() == fix1_AgentSet.df["wealth"].to_list()[1:] - ) - assert ( - result[fix2_AgentSet].to_list() == fix2_AgentSet.df["wealth"].to_list()[1:] - ) - - def test___iadd__( - self, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - model = Model() - agents = AgentSetRegistry(model) - agentset_polars1 = fix1_AgentSet - agentset_polars = fix2_AgentSet - - # Test with a single AgentSet - agents_copy = deepcopy(agents) - agents_copy += agentset_polars - assert agents_copy._agentsets[0] is agentset_polars - assert agents_copy._ids.to_list() == agentset_polars._df["unique_id"].to_list() - - # Test with a list of AgentSets - agents_copy = deepcopy(agents) - agents_copy += [agentset_polars1, agentset_polars] - assert agents_copy._agentsets[0] is agentset_polars1 - assert agents_copy._agentsets[1] is agentset_polars - assert ( - agents_copy._ids.to_list() - == agentset_polars1._df["unique_id"].to_list() - + agentset_polars._df["unique_id"].to_list() - ) - - # Test if adding the same AgentSet raises ValueError - with pytest.raises(ValueError): - agents_copy += agentset_polars1 - - def test___iter__(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - len_agentset0 = len(agents._agentsets[0]) - len_agentset1 = len(agents._agentsets[1]) - for i, agent in enumerate(agents): - assert isinstance(agent, dict) - if i < len_agentset0: - assert agent["unique_id"] == agents._agentsets[0].df["unique_id"][i] - else: - assert ( - agent["unique_id"] - == agents._agentsets[1].df["unique_id"][i - len_agentset0] - ) - assert i == len_agentset0 + len_agentset1 - 1 - - def test___isub__( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - # Test with an AgentSet and a DataFrame - agents = fix_AgentSetRegistry - agents -= fix1_AgentSet - assert agents._agentsets[0] == fix2_AgentSet - assert len(agents._agentsets) == 1 - - def test___len__( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - assert len(fix_AgentSetRegistry) == len(fix1_AgentSet) + len(fix2_AgentSet) - - def test___repr__(self, fix_AgentSetRegistry: AgentSetRegistry): - repr(fix_AgentSetRegistry) - - def test___reversed__(self, fix2_AgentSet: AgentSetRegistry): - agents = fix2_AgentSet - reversed_wealth = [] - for agent in reversed(list(agents)): - reversed_wealth.append(agent["wealth"]) - assert reversed_wealth == list(reversed(agents["wealth"])) - - def test___setitem__(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - # Test with a single attribute - agents["wealth"] = 0 - assert agents._agentsets[0].df["wealth"].to_list() == [0] * len( - agents._agentsets[0] - ) - assert agents._agentsets[1].df["wealth"].to_list() == [0] * len( - agents._agentsets[1] - ) - - # Test with a list of attributes - agents[["wealth", "age"]] = 1 - assert agents._agentsets[0].df["wealth"].to_list() == [1] * len( - agents._agentsets[0] - ) - assert agents._agentsets[0].df["age"].to_list() == [1] * len( - agents._agentsets[0] - ) - - # Test with a single attribute and a mask - mask0 = pl.Series( - "mask", [True] + [False] * (len(agents._agentsets[0]) - 1), dtype=pl.Boolean - ) - mask1 = pl.Series( - "mask", [True] + [False] * (len(agents._agentsets[1]) - 1), dtype=pl.Boolean - ) - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - agents[mask_dictionary, "wealth"] = 0 - assert agents._agentsets[0].df["wealth"].to_list() == [0] + [1] * ( - len(agents._agentsets[0]) - 1 - ) - assert agents._agentsets[1].df["wealth"].to_list() == [0] + [1] * ( - len(agents._agentsets[1]) - 1 - ) - - def test___str__(self, fix_AgentSetRegistry: AgentSetRegistry): - str(fix_AgentSetRegistry) - - def test___sub__( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - # Test with an AgentSet and a DataFrame - result = fix_AgentSetRegistry - fix1_AgentSet - assert isinstance(result._agentsets[0], ExampleAgentSet) - assert len(result._agentsets) == 1 - - def test_agents( - self, - fix_AgentSetRegistry: AgentSetRegistry, - fix1_AgentSet: ExampleAgentSet, - fix2_AgentSet: ExampleAgentSet, - ): - assert isinstance(fix_AgentSetRegistry.df, dict) - assert len(fix_AgentSetRegistry.df) == 2 - assert fix_AgentSetRegistry.df[fix1_AgentSet] is fix1_AgentSet._df - assert fix_AgentSetRegistry.df[fix2_AgentSet] is fix2_AgentSet._df - - # Test agents.setter - fix_AgentSetRegistry.df = [fix1_AgentSet, fix2_AgentSet] - assert fix_AgentSetRegistry._agentsets[0] == fix1_AgentSet - assert fix_AgentSetRegistry._agentsets[1] == fix2_AgentSet - - def test_active_agents(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - # Test with select - mask0 = ( - agents._agentsets[0].df["wealth"] - > agents._agentsets[0].df["wealth"].to_list()[0] - ) - mask1 = ( - agents._agentsets[1].df["wealth"] - > agents._agentsets[1].df["wealth"].to_list()[0] - ) - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - - agents1 = agents.select(mask=mask_dictionary, inplace=False) - - result = agents1.active_agents - assert isinstance(result, dict) - assert isinstance(result[agents1._agentsets[0]], pl.DataFrame) - assert isinstance(result[agents1._agentsets[1]], pl.DataFrame) - - assert all( - series.all() - for series in ( - result[agents1._agentsets[0]] == agents1._agentsets[0]._df.filter(mask0) - ) - ) - - assert all( - series.all() - for series in ( - result[agents1._agentsets[1]] == agents1._agentsets[1]._df.filter(mask1) - ) - ) - - # Test with active_agents.setter - agents1.active_agents = mask_dictionary - result = agents1.active_agents - assert isinstance(result, dict) - assert isinstance(result[agents1._agentsets[0]], pl.DataFrame) - assert isinstance(result[agents1._agentsets[1]], pl.DataFrame) - assert all( - series.all() - for series in ( - result[agents1._agentsets[0]] == agents1._agentsets[0]._df.filter(mask0) - ) - ) - assert all( - series.all() - for series in ( - result[agents1._agentsets[1]] == agents1._agentsets[1]._df.filter(mask1) - ) - ) - - def test_agentsets_by_type(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - result = agents.agentsets_by_type - assert isinstance(result, dict) - assert isinstance(result[ExampleAgentSet], AgentSetRegistry) - - assert ( - result[ExampleAgentSet]._agentsets[0].df.rows() - == agents._agentsets[1].df.rows() - ) - - def test_inactive_agents(self, fix_AgentSetRegistry: AgentSetRegistry): - agents = fix_AgentSetRegistry - - # Test with select - mask0 = ( - agents._agentsets[0].df["wealth"] - > agents._agentsets[0].df["wealth"].to_list()[0] - ) - mask1 = ( - agents._agentsets[1].df["wealth"] - > agents._agentsets[1].df["wealth"].to_list()[0] - ) - mask_dictionary = {agents._agentsets[0]: mask0, agents._agentsets[1]: mask1} - agents1 = agents.select(mask=mask_dictionary, inplace=False) - result = agents1.inactive_agents - assert isinstance(result, dict) - assert isinstance(result[agents1._agentsets[0]], pl.DataFrame) - assert isinstance(result[agents1._agentsets[1]], pl.DataFrame) - assert all( - series.all() - for series in ( - result[agents1._agentsets[0]] - == agents1._agentsets[0].select(mask0, negate=True).active_agents - ) - ) - assert all( - series.all() - for series in ( - result[agents1._agentsets[1]] - == agents1._agentsets[1].select(mask1, negate=True).active_agents - ) - ) From 5f89f0796c8b88d0eb298d46db37f7712c9d891b Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 12:41:00 +0200 Subject: [PATCH 140/329] feat: add abstract rename method to AbstractAgentSet for consistent naming management --- mesa_frames/abstract/agentset.py | 35 ++++++-------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index 9bc25174..f8b5c134 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -468,13 +468,14 @@ def random(self) -> Generator: def space(self) -> mesa_frames.abstract.space.Space | None: return self.model.space + @abstractmethod def rename(self, new_name: str, inplace: bool = True) -> Self: """Rename this AgentSet. - If this set is contained in the model's AgentSetRegistry, delegate to - the registry's rename implementation so that name uniqueness and - conflicts are handled consistently. If the set is not yet part of a - registry, update the local name directly. + Concrete subclasses must implement the mechanics for coordinating with + any containing registry and managing ``inplace`` semantics. The method + should update the set's name (or return a renamed copy when + ``inplace=False``) while preserving registry invariants. Parameters ---------- @@ -489,31 +490,7 @@ def rename(self, new_name: str, inplace: bool = True) -> Self: Self The updated AgentSet (or a renamed copy when ``inplace=False``). """ - obj = self._get_obj(inplace) - try: - # If contained in registry, delegate to it so conflicts are handled - if self in self.model.sets: # type: ignore[operator] - # Preserve index to retrieve copy when not inplace - idx = None - try: - idx = list(self.model.sets).index(self) # type: ignore[arg-type] - except Exception: - idx = None - reg = self.model.sets.rename(self, new_name, inplace=inplace) - if inplace: - return self - # Non-inplace: return the corresponding set from the copied registry - if idx is not None: - return reg[idx] # type: ignore[index] - # Fallback: look up by name (may be canonicalized) - return reg.get(new_name) # type: ignore[return-value] - except Exception: - # If delegation cannot be resolved, fall back to local rename - obj._name = new_name - return obj - # Not in a registry: local rename - obj._name = new_name - return obj + ... def __setitem__( self, From acf9c50ad35948a256f79f763b2a545a594b07c8 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 14:08:09 +0200 Subject: [PATCH 141/329] feat: add abstract set method to AbstractAgentSet for updating agent attributes --- mesa_frames/abstract/agentset.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index f8b5c134..ae5db2db 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -492,6 +492,35 @@ def rename(self, new_name: str, inplace: bool = True) -> Self: """ ... + @abstractmethod + def set( + self, + attr_names: str | Collection[str] | dict[str, Any] | None = None, + values: Any | None = None, + mask: AgentMask | None = None, + inplace: bool = True, + ) -> Self: + """Update agent attributes, optionally on a masked subset. + + Parameters + ---------- + attr_names : str | Collection[str] | dict[str, Any] | None, optional + Attribute(s) to assign. When ``None``, concrete implementations may + derive targets from ``values``. + values : Any | None, optional + Replacement value(s) aligned with ``attr_names``. + mask : AgentMask | None, optional + Subset selector limiting which agents are updated. + inplace : bool, optional + Whether to mutate in place or return an updated copy, by default True. + + Returns + ------- + Self + The updated AgentSet (or a modified copy when ``inplace=False``). + """ + ... + def __setitem__( self, key: str From 2935894df0edccc56ef68375d69ef52232ff032c Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 15:46:48 +0200 Subject: [PATCH 142/329] Remove obsolete OLD_concrete_accessors.py and OLD_concrete_agents.py files These files contained outdated implementations of the AgentSetsAccessor and AgentsDF classes, which are no longer in use. Their removal helps to clean up the codebase and reduce confusion regarding the current architecture of the mesa-frames library. --- mesa_frames/OLD_abstract_accessors.py | 408 --------- mesa_frames/OLD_abstract_agents.py | 1139 ------------------------- mesa_frames/OLD_concrete_accessors.py | 147 ---- mesa_frames/OLD_concrete_agents.py | 636 -------------- 4 files changed, 2330 deletions(-) delete mode 100644 mesa_frames/OLD_abstract_accessors.py delete mode 100644 mesa_frames/OLD_abstract_agents.py delete mode 100644 mesa_frames/OLD_concrete_accessors.py delete mode 100644 mesa_frames/OLD_concrete_agents.py diff --git a/mesa_frames/OLD_abstract_accessors.py b/mesa_frames/OLD_abstract_accessors.py deleted file mode 100644 index a33ddcab..00000000 --- a/mesa_frames/OLD_abstract_accessors.py +++ /dev/null @@ -1,408 +0,0 @@ -"""Abstract accessors for agent sets collections. - -This module provides abstract base classes for accessors that enable -flexible querying and manipulation of collections of agent sets. -""" - -from __future__ import annotations - -from abc import ABC, abstractmethod -from collections.abc import Iterable, Iterator, Mapping -from typing import Any, Literal, overload, TypeVar - -from mesa_frames.abstract.agents import AgentSetDF -from mesa_frames.types_ import KeyBy - -TSet = TypeVar("TSet", bound=AgentSetDF) - - -class AbstractAgentSetsAccessor(ABC): - """Abstract accessor for collections of agent sets. - - This interface defines a flexible, user-friendly API to access agent sets - by name, positional index, or class/type, and to iterate or view the - collection under different key domains. - - Notes - ----- - Concrete implementations should: - - Support ``__getitem__`` with ``int`` | ``str`` | ``type[AgentSetDF]``. - - Return a list for type-based queries (even when there is one match). - - Provide keyed iteration via ``keys/items/iter/mapping`` with ``key_by``. - - Expose read-only snapshots ``by_name`` and ``by_type``. - - Examples - -------- - Assuming ``agents`` is an :class:`~mesa_frames.concrete.agents.AgentsDF`: - - >>> sheep = agents.sets["Sheep"] # name lookup - >>> first = agents.sets[0] # index lookup - >>> wolves = agents.sets[Wolf] # type lookup → list - >>> len(wolves) >= 0 - True - - Choose a key view when iterating: - - >>> for k, aset in agents.sets.items(key_by="index"): - ... print(k, aset.name) - 0 Sheep - 1 Wolf - """ - - # __getitem__ — exact shapes per key kind - @overload - @abstractmethod - def __getitem__(self, key: int) -> AgentSetDF: ... - - @overload - @abstractmethod - def __getitem__(self, key: str) -> AgentSetDF: ... - - @overload - @abstractmethod - def __getitem__(self, key: type[TSet]) -> list[TSet]: ... - - @abstractmethod - def __getitem__(self, key: int | str | type[TSet]) -> AgentSetDF | list[TSet]: - """Retrieve agent set(s) by index, name, or type. - - Parameters - ---------- - key : int | str | type[TSet] - - ``int``: positional index (supports negative indices). - - ``str``: agent set name. - - ``type``: class or subclass of :class:`AgentSetDF`. - - Returns - ------- - AgentSetDF | list[TSet] - A single agent set for ``int``/``str`` keys; a list of matching - agent sets for ``type`` keys (possibly empty). - - Raises - ------ - IndexError - If an index is out of range. - KeyError - If a name is missing. - TypeError - If the key type is unsupported. - """ - - # get — mirrors dict.get, but preserves list shape for type keys - @overload - @abstractmethod - def get(self, key: int, default: None = ...) -> AgentSetDF | None: ... - - @overload - @abstractmethod - def get(self, key: str, default: None = ...) -> AgentSetDF | None: ... - - @overload - @abstractmethod - def get(self, key: type[TSet], default: None = ...) -> list[TSet]: ... - - @overload - @abstractmethod - def get(self, key: int, default: AgentSetDF) -> AgentSetDF: ... - - @overload - @abstractmethod - def get(self, key: str, default: AgentSetDF) -> AgentSetDF: ... - - @overload - @abstractmethod - def get(self, key: type[TSet], default: list[TSet]) -> list[TSet]: ... - - @abstractmethod - def get( - self, - key: int | str | type[TSet], - default: AgentSetDF | list[TSet] | None = None, - ) -> AgentSetDF | list[TSet] | None: - """ - Safe lookup variant that returns a default on miss. - - Parameters - ---------- - key : int | str | type[TSet] - Lookup key; see :meth:`__getitem__`. - default : AgentSetDF | list[TSet] | None, optional - Value to return when the lookup fails. For type keys, if no matches - are found and default is None, implementers should return [] to keep - list shape stable. - - Returns - ------- - AgentSetDF | list[TSet] | None - - int/str keys: return the set or default/None if missing - - type keys: return list of matching sets; if none and default is None, - return [] (stable list shape) - """ - - @abstractmethod - def first(self, t: type[TSet]) -> TSet: - """Return the first agent set matching a type. - - Parameters - ---------- - t : type[TSet] - The concrete class (or base class) to match. - - Returns - ------- - TSet - The first matching agent set in iteration order. - - Raises - ------ - KeyError - If no agent set matches ``t``. - - Examples - -------- - >>> agents.sets.first(Wolf) # doctest: +SKIP - - """ - - @abstractmethod - def all(self, t: type[TSet]) -> list[TSet]: - """Return all agent sets matching a type. - - Parameters - ---------- - t : type[TSet] - The concrete class (or base class) to match. - - Returns - ------- - list[TSet] - A list of all matching agent sets (possibly empty). - - Examples - -------- - >>> agents.sets.all(Wolf) # doctest: +SKIP - [, ] - """ - - @abstractmethod - def at(self, index: int) -> AgentSetDF: - """Return the agent set at a positional index. - - Parameters - ---------- - index : int - Positional index; negative indices are supported. - - Returns - ------- - AgentSetDF - The agent set at the given position. - - Raises - ------ - IndexError - If ``index`` is out of range. - - Examples - -------- - >>> agents.sets.at(0) is agents.sets[0] - True - """ - - @overload - @abstractmethod - def keys(self, *, key_by: Literal["name"]) -> Iterable[str]: ... - - @overload - @abstractmethod - def keys(self, *, key_by: Literal["index"]) -> Iterable[int]: ... - - @overload - @abstractmethod - def keys(self, *, key_by: Literal["type"]) -> Iterable[type[AgentSetDF]]: ... - - @abstractmethod - def keys(self, *, key_by: KeyBy = "name") -> Iterable[str | int | type[AgentSetDF]]: - """Iterate keys under a chosen key domain. - - Parameters - ---------- - key_by : KeyBy - - ``"name"`` → agent set names. (Default) - - ``"index"`` → positional indices. - - ``"type"`` → the concrete classes of each set. - - Returns - ------- - Iterable[str | int | type[AgentSetDF]] - An iterable of keys corresponding to the selected domain. - """ - - @overload - @abstractmethod - def items(self, *, key_by: Literal["name"]) -> Iterable[tuple[str, AgentSetDF]]: ... - - @overload - @abstractmethod - def items( - self, *, key_by: Literal["index"] - ) -> Iterable[tuple[int, AgentSetDF]]: ... - - @overload - @abstractmethod - def items( - self, *, key_by: Literal["type"] - ) -> Iterable[tuple[type[AgentSetDF], AgentSetDF]]: ... - - @abstractmethod - def items( - self, *, key_by: KeyBy = "name" - ) -> Iterable[tuple[str | int | type[AgentSetDF], AgentSetDF]]: - """Iterate ``(key, AgentSetDF)`` pairs under a chosen key domain. - - See :meth:`keys` for the meaning of ``key_by``. - """ - - @abstractmethod - def values(self) -> Iterable[AgentSetDF]: - """Iterate over agent set values only (no keys).""" - - @abstractmethod - def iter(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: - """Alias for :meth:`items` for convenience.""" - - @overload - @abstractmethod - def dict(self, *, key_by: Literal["name"]) -> dict[str, AgentSetDF]: ... - - @overload - @abstractmethod - def dict(self, *, key_by: Literal["index"]) -> dict[int, AgentSetDF]: ... - - @overload - @abstractmethod - def dict( - self, *, key_by: Literal["type"] - ) -> dict[type[AgentSetDF], AgentSetDF]: ... - - @abstractmethod - def dict( - self, *, key_by: KeyBy = "name" - ) -> dict[str | int | type[AgentSetDF], AgentSetDF]: - """Return a dictionary view keyed by the chosen domain. - - Notes - ----- - ``key_by="type"`` will keep the last set per type. For one-to-many - grouping, prefer the read-only :attr:`by_type` snapshot. - """ - - @property - @abstractmethod - def by_name(self) -> Mapping[str, AgentSetDF]: - """Read-only mapping of names to agent sets. - - Returns - ------- - Mapping[str, AgentSetDF] - An immutable snapshot that maps each agent set name to its object. - - Notes - ----- - Implementations should return a read-only mapping such as - ``types.MappingProxyType`` over an internal dict to avoid accidental - mutation. - - Examples - -------- - >>> sheep = agents.sets.by_name["Sheep"] # doctest: +SKIP - >>> sheep is agents.sets["Sheep"] # doctest: +SKIP - True - """ - - @property - @abstractmethod - def by_type(self) -> Mapping[type, list[AgentSetDF]]: - """Read-only mapping of types to lists of agent sets. - - Returns - ------- - Mapping[type, list[AgentSetDF]] - An immutable snapshot grouping agent sets by their concrete class. - - Notes - ----- - This supports one-to-many relationships where multiple sets share the - same type. Prefer this over ``mapping(key_by="type")`` when you need - grouping instead of last-write-wins semantics. - """ - - @abstractmethod - def rename( - self, - target: AgentSetDF - | str - | dict[AgentSetDF | str, str] - | list[tuple[AgentSetDF | str, str]], - new_name: str | None = None, - *, - on_conflict: Literal["canonicalize", "raise"] = "canonicalize", - mode: Literal["atomic", "best_effort"] = "atomic", - ) -> str | dict[AgentSetDF, str]: - """ - Rename agent sets. Supports single and batch renaming with deterministic conflict handling. - - Parameters - ---------- - target : AgentSetDF | str | dict[AgentSetDF | str, str] | list[tuple[AgentSetDF | str, str]] - Either: - - Single: AgentSet or name string (must provide new_name) - - Batch: {target: new_name} dict or [(target, new_name), ...] list - new_name : str | None, optional - New name (only used for single renames) - on_conflict : "Literal['canonicalize', 'raise']" - Conflict resolution: "canonicalize" (default) appends suffixes, "raise" raises ValueError - mode : "Literal['atomic', 'best_effort']" - Rename mode: "atomic" applies all or none (default), "best_effort" skips failed renames - - Returns - ------- - str | dict[AgentSetDF, str] - Single rename: final name string - Batch: {agentset: final_name} mapping - - Examples - -------- - Single rename: - >>> agents.sets.rename("old_name", "new_name") - - Batch rename (dict): - >>> agents.sets.rename({"set1": "new_name", "set2": "another_name"}) - - Batch rename (list): - >>> agents.sets.rename([("set1", "new_name"), ("set2", "another_name")]) - """ - - @abstractmethod - def __contains__(self, x: str | AgentSetDF) -> bool: - """Return ``True`` if a name or object is present. - - Parameters - ---------- - x : str | AgentSetDF - A name to test by equality, or an object to test by identity. - - Returns - ------- - bool - ``True`` if present, else ``False``. - """ - - @abstractmethod - def __len__(self) -> int: - """Return number of agent sets in the collection.""" - - @abstractmethod - def __iter__(self) -> Iterator[AgentSetDF]: - """Iterate over agent set values in insertion order.""" diff --git a/mesa_frames/OLD_abstract_agents.py b/mesa_frames/OLD_abstract_agents.py deleted file mode 100644 index f4243558..00000000 --- a/mesa_frames/OLD_abstract_agents.py +++ /dev/null @@ -1,1139 +0,0 @@ -""" -Abstract base classes for agent containers in mesa-frames. - -This module defines the core abstractions for agent containers in the mesa-frames -extension. It provides the foundation for implementing agent storage and -manipulation using DataFrame-based approaches. - -Classes: - AgentContainer(CopyMixin): - An abstract base class that defines the common interface for all agent - containers in mesa-frames. It inherits from CopyMixin to provide fast - copying functionality. - - AgentSetDF(AgentContainer, DataFrameMixin): - An abstract base class for agent sets that use DataFrames as the underlying - storage mechanism. It inherits from both AgentContainer and DataFrameMixin - to combine agent container functionality with DataFrame operations. - -These abstract classes are designed to be subclassed by concrete implementations -that use Polars library as their backend. - -Usage: - These classes should not be instantiated directly. Instead, they should be - subclassed to create concrete implementations: - - from mesa_frames.abstract.agents import AgentSetDF - - class AgentSetPolars(AgentSetDF): - def __init__(self, model): - super().__init__(model) - # Implementation using polars DataFrame - ... - - # Implement other abstract methods - -Note: - The abstract methods in these classes use Python's @abstractmethod decorator, - ensuring that concrete subclasses must implement these methods. - -Attributes and methods of each class are documented in their respective docstrings. -""" - -from __future__ import annotations # PEP 563: postponed evaluation of type annotations - -from abc import abstractmethod -from collections.abc import Callable, Collection, Iterable, Iterator, Sequence -from contextlib import suppress -from typing import Any, Literal, Self, overload - -from numpy.random import Generator - -from mesa_frames.abstract.mixin import CopyMixin, DataFrameMixin -from mesa_frames.types_ import ( - AgentMask, - BoolSeries, - DataFrame, - DataFrameInput, - IdsLike, - Index, - Series, -) - - -class AgentContainer(CopyMixin): - """An abstract class for containing agents. Defines the common interface for AgentSetDF and AgentsDF.""" - - _copy_only_reference: list[str] = [ - "_model", - ] - _model: mesa_frames.concrete.model.ModelDF - - @abstractmethod - def __init__(self) -> None: ... - - def discard( - self, - agents: IdsLike - | AgentMask - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF], - inplace: bool = True, - ) -> Self: - """Remove agents from the AgentContainer. Does not raise an error if the agent is not found. - - Parameters - ---------- - agents : IdsLike | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] - The agents to remove - inplace : bool - Whether to remove the agent in place. Defaults to True. - - Returns - ------- - Self - The updated AgentContainer. - """ - with suppress(KeyError, ValueError): - return self.remove(agents, inplace=inplace) - return self._get_obj(inplace) - - @abstractmethod - def add( - self, - agents: DataFrame - | DataFrameInput - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF], - inplace: bool = True, - ) -> Self: - """Add agents to the AgentContainer. - - Parameters - ---------- - agents : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] - The agents to add. - inplace : bool - Whether to add the agents in place. Defaults to True. - - Returns - ------- - Self - The updated AgentContainer. - """ - ... - - @overload - @abstractmethod - def contains(self, agents: int) -> bool: ... - - @overload - @abstractmethod - def contains( - self, agents: mesa_frames.concrete.agents.AgentSetDF | IdsLike - ) -> BoolSeries: ... - - @abstractmethod - def contains( - self, agents: mesa_frames.concrete.agents.AgentSetDF | IdsLike - ) -> bool | BoolSeries: - """Check if agents with the specified IDs are in the AgentContainer. - - Parameters - ---------- - agents : mesa_frames.concrete.agents.AgentSetDF | IdsLike - The ID(s) to check for. - - Returns - ------- - bool | BoolSeries - True if the agent is in the AgentContainer, False otherwise. - """ - - @overload - @abstractmethod - def do( - self, - method_name: str, - *args: Any, - mask: AgentMask | None = None, - return_results: Literal[False] = False, - inplace: bool = True, - **kwargs: Any, - ) -> Self: ... - - @overload - @abstractmethod - def do( - self, - method_name: str, - *args: Any, - mask: AgentMask | None = None, - return_results: Literal[True], - inplace: bool = True, - **kwargs: Any, - ) -> Any | dict[mesa_frames.concrete.agents.AgentSetDF, Any]: ... - - @abstractmethod - def do( - self, - method_name: str, - *args: Any, - mask: AgentMask | None = None, - return_results: bool = False, - inplace: bool = True, - **kwargs: Any, - ) -> Self | Any | dict[mesa_frames.concrete.agents.AgentSetDF, Any]: - """Invoke a method on the AgentContainer. - - Parameters - ---------- - method_name : str - The name of the method to invoke. - *args : Any - Positional arguments to pass to the method - mask : AgentMask | None, optional - The subset of agents on which to apply the method - return_results : bool, optional - Whether to return the result of the method, by default False - inplace : bool, optional - Whether the operation should be done inplace, by default False - **kwargs : Any - Keyword arguments to pass to the method - - Returns - ------- - Self | Any | dict[mesa_frames.concrete.agents.AgentSetDF, Any] - The updated AgentContainer or the result of the method. - """ - ... - - @abstractmethod - @overload - def get(self, attr_names: str) -> Series | dict[str, Series]: ... - - @abstractmethod - @overload - def get( - self, attr_names: Collection[str] | None = None - ) -> DataFrame | dict[str, DataFrame]: ... - - @abstractmethod - def get( - self, - attr_names: str | Collection[str] | None = None, - mask: AgentMask | None = None, - ) -> Series | dict[str, Series] | DataFrame | dict[str, DataFrame]: - """Retrieve the value of a specified attribute for each agent in the AgentContainer. - - Parameters - ---------- - attr_names : str | Collection[str] | None, optional - The attributes to retrieve. If None, all attributes are retrieved. Defaults to None. - mask : AgentMask | None, optional - The AgentMask of agents to retrieve the attribute for. If None, attributes of all agents are returned. Defaults to None. - - Returns - ------- - Series | dict[str, Series] | DataFrame | dict[str, DataFrame] - The attribute values. - """ - ... - - @abstractmethod - def remove( - self, - agents: ( - IdsLike - | AgentMask - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF] - ), - inplace: bool = True, - ) -> Self: - """Remove the agents from the AgentContainer. - - Parameters - ---------- - agents : IdsLike | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] - The agents to remove. - inplace : bool, optional - Whether to remove the agent in place. - - Returns - ------- - Self - The updated AgentContainer. - """ - ... - - @abstractmethod - def select( - self, - mask: AgentMask | None = None, - filter_func: Callable[[Self], AgentMask] | None = None, - n: int | None = None, - negate: bool = False, - inplace: bool = True, - ) -> Self: - """Select agents in the AgentContainer based on the given criteria. - - Parameters - ---------- - mask : AgentMask | None, optional - The AgentMask of agents to be selected, by default None - filter_func : Callable[[Self], AgentMask] | None, optional - A function which takes as input the AgentContainer and returns a AgentMask, by default None - n : int | None, optional - The maximum number of agents to be selected, by default None - negate : bool, optional - If the selection should be negated, by default False - inplace : bool, optional - If the operation should be performed on the same object, by default True - - Returns - ------- - Self - A new or updated AgentContainer. - """ - ... - - @abstractmethod - @overload - def set( - self, - attr_names: dict[str, Any], - values: None, - mask: AgentMask | None = None, - inplace: bool = True, - ) -> Self: ... - - @abstractmethod - @overload - def set( - self, - attr_names: str | Collection[str], - values: Any, - mask: AgentMask | None = None, - inplace: bool = True, - ) -> Self: ... - - @abstractmethod - def set( - self, - attr_names: DataFrameInput | str | Collection[str], - values: Any | None = None, - mask: AgentMask | None = None, - inplace: bool = True, - ) -> Self: - """Set the value of a specified attribute or attributes for each agent in the mask in AgentContainer. - - Parameters - ---------- - attr_names : DataFrameInput | str | Collection[str] - The key can be: - - A string: sets the specified column of the agents in the AgentContainer. - - A collection of strings: sets the specified columns of the agents in the AgentContainer. - - A dictionary: keys should be attributes and values should be the values to set. Value should be None. - values : Any | None - The value to set the attribute to. If None, attr_names must be a dictionary. - mask : AgentMask | None - The AgentMask of agents to set the attribute for. - inplace : bool - Whether to set the attribute in place. - - Returns - ------- - Self - The updated agent set. - """ - ... - - @abstractmethod - def shuffle(self, inplace: bool = False) -> Self: - """Shuffles the order of agents in the AgentContainer. - - Parameters - ---------- - inplace : bool - Whether to shuffle the agents in place. - - Returns - ------- - Self - A new or updated AgentContainer. - """ - - @abstractmethod - def sort( - self, - by: str | Sequence[str], - ascending: bool | Sequence[bool] = True, - inplace: bool = True, - **kwargs, - ) -> Self: - """ - Sorts the agents in the agent set based on the given criteria. - - Parameters - ---------- - by : str | Sequence[str] - The attribute(s) to sort by. - ascending : bool | Sequence[bool] - Whether to sort in ascending order. - inplace : bool - Whether to sort the agents in place. - **kwargs - Keyword arguments to pass to the sort - - Returns - ------- - Self - A new or updated AgentContainer. - """ - - def __add__( - self, - other: DataFrame - | DataFrameInput - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF], - ) -> Self: - """Add agents to a new AgentContainer through the + operator. - - Parameters - ---------- - other : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] - The agents to add. - - Returns - ------- - Self - A new AgentContainer with the added agents. - """ - return self.add(agents=other, inplace=False) - - def __contains__(self, agents: int | AgentSetDF) -> bool: - """Check if an agent is in the AgentContainer. - - Parameters - ---------- - agents : int | AgentSetDF - The ID(s) or AgentSetDF to check for. - - Returns - ------- - bool - True if the agent is in the AgentContainer, False otherwise. - """ - return self.contains(agents=agents) - - @overload - def __getitem__( - self, key: str | tuple[AgentMask, str] - ) -> Series | dict[AgentSetDF, Series]: ... - - @overload - def __getitem__( - self, - key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], - ) -> DataFrame | dict[AgentSetDF, DataFrame]: ... - - def __getitem__( - self, - key: ( - str - | Collection[str] - | AgentMask - | tuple[AgentMask, str] - | tuple[AgentMask, Collection[str]] - | tuple[dict[AgentSetDF, AgentMask], str] - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] - ), - ) -> Series | DataFrame | dict[AgentSetDF, Series] | dict[AgentSetDF, DataFrame]: - """Implement the [] operator for the AgentContainer. - - The key can be: - - An attribute or collection of attributes (eg. AgentContainer["str"], AgentContainer[["str1", "str2"]]): returns the specified column(s) of the agents in the AgentContainer. - - An AgentMask (eg. AgentContainer[AgentMask]): returns the agents in the AgentContainer that satisfy the AgentMask. - - A tuple (eg. AgentContainer[AgentMask, "str"]): returns the specified column of the agents in the AgentContainer that satisfy the AgentMask. - - A tuple with a dictionary (eg. AgentContainer[{AgentSetDF: AgentMask}, "str"]): returns the specified column of the agents in the AgentContainer that satisfy the AgentMask from the dictionary. - - A tuple with a dictionary (eg. AgentContainer[{AgentSetDF: AgentMask}, Collection[str]]): returns the specified columns of the agents in the AgentContainer that satisfy the AgentMask from the dictionary. - - Parameters - ---------- - key : str | Collection[str] | AgentMask | tuple[AgentMask, str] | tuple[AgentMask, Collection[str]] | tuple[dict[AgentSetDF, AgentMask], str] | tuple[dict[AgentSetDF, AgentMask], Collection[str]] - The key to retrieve. - - Returns - ------- - Series | DataFrame | dict[AgentSetDF, Series] | dict[AgentSetDF, DataFrame] - The attribute values. - """ - # TODO: fix types - if isinstance(key, tuple): - return self.get(mask=key[0], attr_names=key[1]) - else: - if isinstance(key, str) or ( - isinstance(key, Collection) and all(isinstance(k, str) for k in key) - ): - return self.get(attr_names=key) - else: - return self.get(mask=key) - - def __iadd__( - self, - other: ( - DataFrame - | DataFrameInput - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF] - ), - ) -> Self: - """Add agents to the AgentContainer through the += operator. - - Parameters - ---------- - other : DataFrame | DataFrameInput | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] - The agents to add. - - Returns - ------- - Self - The updated AgentContainer. - """ - return self.add(agents=other, inplace=True) - - def __isub__( - self, - other: ( - IdsLike - | AgentMask - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF] - ), - ) -> Self: - """Remove agents from the AgentContainer through the -= operator. - - Parameters - ---------- - other : IdsLike | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] - The agents to remove. - - Returns - ------- - Self - The updated AgentContainer. - """ - return self.discard(other, inplace=True) - - def __sub__( - self, - other: ( - IdsLike - | AgentMask - | mesa_frames.concrete.agents.AgentSetDF - | Collection[mesa_frames.concrete.agents.AgentSetDF] - ), - ) -> Self: - """Remove agents from a new AgentContainer through the - operator. - - Parameters - ---------- - other : IdsLike | AgentMask | mesa_frames.concrete.agents.AgentSetDF | Collection[mesa_frames.concrete.agents.AgentSetDF] - The agents to remove. - - Returns - ------- - Self - A new AgentContainer with the removed agents. - """ - return self.discard(other, inplace=False) - - def __setitem__( - self, - key: ( - str - | Collection[str] - | AgentMask - | tuple[AgentMask, str | Collection[str]] - | tuple[dict[AgentSetDF, AgentMask], str] - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] - ), - values: Any, - ) -> None: - """Implement the [] operator for setting values in the AgentContainer. - - The key can be: - - A string (eg. AgentContainer["str"]): sets the specified column of the agents in the AgentContainer. - - A list of strings(eg. AgentContainer[["str1", "str2"]]): sets the specified columns of the agents in the AgentContainer. - - A tuple (eg. AgentContainer[AgentMask, "str"]): sets the specified column of the agents in the AgentContainer that satisfy the AgentMask. - - A AgentMask (eg. AgentContainer[AgentMask]): sets the attributes of the agents in the AgentContainer that satisfy the AgentMask. - - A tuple with a dictionary (eg. AgentContainer[{AgentSetDF: AgentMask}, "str"]): sets the specified column of the agents in the AgentContainer that satisfy the AgentMask from the dictionary. - - A tuple with a dictionary (eg. AgentContainer[{AgentSetDF: AgentMask}, Collection[str]]): sets the specified columns of the agents in the AgentContainer that satisfy the AgentMask from the dictionary. - - Parameters - ---------- - key : str | Collection[str] | AgentMask | tuple[AgentMask, str | Collection[str]] | tuple[dict[AgentSetDF, AgentMask], str] | tuple[dict[AgentSetDF, AgentMask], Collection[str]] - The key to set. - values : Any - The values to set for the specified key. - """ - # TODO: fix types as in __getitem__ - if isinstance(key, tuple): - self.set(mask=key[0], attr_names=key[1], values=values) - else: - if isinstance(key, str) or ( - isinstance(key, Collection) and all(isinstance(k, str) for k in key) - ): - try: - self.set(attr_names=key, values=values) - except KeyError: # key=AgentMask - self.set(attr_names=None, mask=key, values=values) - else: - self.set(attr_names=None, mask=key, values=values) - - @abstractmethod - def __getattr__(self, name: str) -> Any | dict[str, Any]: - """Fallback for retrieving attributes of the AgentContainer. Retrieve an attribute of the underlying DataFrame(s). - - Parameters - ---------- - name : str - The name of the attribute to retrieve. - - Returns - ------- - Any | dict[str, Any] - The attribute value - """ - - @abstractmethod - def __iter__(self) -> Iterator[dict[str, Any]]: - """Iterate over the agents in the AgentContainer. - - Returns - ------- - Iterator[dict[str, Any]] - An iterator over the agents. - """ - ... - - @abstractmethod - def __len__(self) -> int: - """Get the number of agents in the AgentContainer. - - Returns - ------- - int - The number of agents in the AgentContainer. - """ - ... - - @abstractmethod - def __repr__(self) -> str: - """Get a string representation of the DataFrame in the AgentContainer. - - Returns - ------- - str - A string representation of the DataFrame in the AgentContainer. - """ - pass - - @abstractmethod - def __reversed__(self) -> Iterator: - """Iterate over the agents in the AgentContainer in reverse order. - - Returns - ------- - Iterator - An iterator over the agents in reverse order. - """ - ... - - @abstractmethod - def __str__(self) -> str: - """Get a string representation of the agents in the AgentContainer. - - Returns - ------- - str - A string representation of the agents in the AgentContainer. - """ - ... - - @property - def model(self) -> mesa_frames.concrete.model.ModelDF: - """The model that the AgentContainer belongs to. - - Returns - ------- - mesa_frames.concrete.model.ModelDF - """ - return self._model - - @property - def random(self) -> Generator: - """The random number generator of the model. - - Returns - ------- - Generator - """ - return self.model.random - - @property - def space(self) -> mesa_frames.abstract.space.SpaceDF | None: - """The space of the model. - - Returns - ------- - mesa_frames.abstract.space.SpaceDF | None - """ - return self.model.space - - @property - @abstractmethod - def df(self) -> DataFrame | dict[str, DataFrame]: - """The agents in the AgentContainer. - - Returns - ------- - DataFrame | dict[str, DataFrame] - """ - - @df.setter - @abstractmethod - def df( - self, agents: DataFrame | list[mesa_frames.concrete.agents.AgentSetDF] - ) -> None: - """Set the agents in the AgentContainer. - - Parameters - ---------- - agents : DataFrame | list[mesa_frames.concrete.agents.AgentSetDF] - """ - - @property - @abstractmethod - def active_agents(self) -> DataFrame | dict[str, DataFrame]: - """The active agents in the AgentContainer. - - Returns - ------- - DataFrame | dict[str, DataFrame] - """ - - @active_agents.setter - @abstractmethod - def active_agents( - self, - mask: AgentMask, - ) -> None: - """Set the active agents in the AgentContainer. - - Parameters - ---------- - mask : AgentMask - The mask to apply. - """ - self.select(mask=mask, inplace=True) - - @property - @abstractmethod - def inactive_agents( - self, - ) -> DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame]: - """The inactive agents in the AgentContainer. - - Returns - ------- - DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame] - """ - - @property - @abstractmethod - def index( - self, - ) -> Index | dict[mesa_frames.concrete.agents.AgentSetDF, Index]: - """The ids in the AgentContainer. - - Returns - ------- - Index | dict[mesa_frames.concrete.agents.AgentSetDF, Index] - """ - ... - - @property - @abstractmethod - def pos( - self, - ) -> DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame]: - """The position of the agents in the AgentContainer. - - Returns - ------- - DataFrame | dict[mesa_frames.concrete.agents.AgentSetDF, DataFrame] - """ - ... - - -class AgentSetDF(AgentContainer, DataFrameMixin): - """The AgentSetDF class is a container for agents of the same type. - - Parameters - ---------- - model : mesa_frames.concrete.model.ModelDF - The model that the agent set belongs to. - """ - - _df: DataFrame # The agents in the AgentSetDF - _mask: ( - AgentMask # The underlying mask used for the active agents in the AgentSetDF. - ) - _model: ( - mesa_frames.concrete.model.ModelDF - ) # The model that the AgentSetDF belongs to. - - @abstractmethod - def __init__(self, model: mesa_frames.concrete.model.ModelDF) -> None: ... - - @abstractmethod - def add( - self, - agents: DataFrame | DataFrameInput, - inplace: bool = True, - ) -> Self: - """Add agents to the AgentSetDF. - - Agents can be the input to the DataFrame constructor. So, the input can be: - - A DataFrame: adds the agents from the DataFrame. - - A DataFrameInput: passes the input to the DataFrame constructor. - - Parameters - ---------- - agents : DataFrame | DataFrameInput - The agents to add. - inplace : bool, optional - If True, perform the operation in place, by default True - - Returns - ------- - Self - A new AgentContainer with the added agents. - """ - ... - - def discard(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: - """Remove an agent from the AgentSetDF. Does not raise an error if the agent is not found. - - Parameters - ---------- - agents : IdsLike | AgentMask - The ids to remove - inplace : bool, optional - Whether to remove the agent in place, by default True - - Returns - ------- - Self - The updated AgentSetDF. - """ - return super().discard(agents, inplace) - - @overload - def do( - self, - method_name: str, - *args, - mask: AgentMask | None = None, - return_results: Literal[False] = False, - inplace: bool = True, - **kwargs, - ) -> Self: ... - - @overload - def do( - self, - method_name: str, - *args, - mask: AgentMask | None = None, - return_results: Literal[True], - inplace: bool = True, - **kwargs, - ) -> Any: ... - - def do( - self, - method_name: str, - *args, - mask: AgentMask | None = None, - return_results: bool = False, - inplace: bool = True, - **kwargs, - ) -> Self | Any: - masked_df = self._get_masked_df(mask) - # If the mask is empty, we can use the object as is - if len(masked_df) == len(self._df): - obj = self._get_obj(inplace) - method = getattr(obj, method_name) - result = method(*args, **kwargs) - else: # If the mask is not empty, we need to create a new masked AgentSetDF and concatenate the AgentSetDFs at the end - obj = self._get_obj(inplace=False) - obj._df = masked_df - original_masked_index = obj._get_obj_copy(obj.index) - method = getattr(obj, method_name) - result = method(*args, **kwargs) - obj._concatenate_agentsets( - [self], - duplicates_allowed=True, - keep_first_only=True, - original_masked_index=original_masked_index, - ) - if inplace: - for key, value in obj.__dict__.items(): - setattr(self, key, value) - obj = self - if return_results: - return result - else: - return obj - - @abstractmethod - @overload - def get( - self, - attr_names: str, - mask: AgentMask | None = None, - ) -> Series: ... - - @abstractmethod - @overload - def get( - self, - attr_names: Collection[str] | None = None, - mask: AgentMask | None = None, - ) -> DataFrame: ... - - @abstractmethod - def get( - self, - attr_names: str | Collection[str] | None = None, - mask: AgentMask | None = None, - ) -> Series | DataFrame: ... - - @abstractmethod - def step(self) -> None: - """Run a single step of the AgentSetDF. This method should be overridden by subclasses.""" - ... - - def remove(self, agents: IdsLike | AgentMask, inplace: bool = True) -> Self: - if isinstance(agents, str) and agents == "active": - agents = self.active_agents - if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): - return self._get_obj(inplace) - agents = self._df_index(self._get_masked_df(agents), "unique_id") - agentsdf = self.model.agents.remove(agents, inplace=inplace) - # TODO: Refactor AgentsDF to return dict[str, AgentSetDF] instead of dict[AgentSetDF, DataFrame] - # And assign a name to AgentSetDF? This has to be replaced by a nicer API of AgentsDF - for agentset in agentsdf.df.keys(): - if isinstance(agentset, self.__class__): - return agentset - return self - - @abstractmethod - def _concatenate_agentsets( - self, - objs: Iterable[Self], - duplicates_allowed: bool = True, - keep_first_only: bool = True, - original_masked_index: Index | None = None, - ) -> Self: ... - - @abstractmethod - def _get_bool_mask(self, mask: AgentMask) -> BoolSeries: - """Get the equivalent boolean mask based on the input mask. - - Parameters - ---------- - mask : AgentMask - - Returns - ------- - BoolSeries - """ - ... - - @abstractmethod - def _get_masked_df(self, mask: AgentMask) -> DataFrame: - """Get the df filtered by the input mask. - - Parameters - ---------- - mask : AgentMask - - Returns - ------- - DataFrame - """ - - @overload - @abstractmethod - def _get_obj_copy(self, obj: DataFrame) -> DataFrame: ... - - @overload - @abstractmethod - def _get_obj_copy(self, obj: Series) -> Series: ... - - @overload - @abstractmethod - def _get_obj_copy(self, obj: Index) -> Index: ... - - @abstractmethod - def _get_obj_copy( - self, obj: DataFrame | Series | Index - ) -> DataFrame | Series | Index: ... - - @abstractmethod - def _discard(self, ids: IdsLike) -> Self: - """Remove an agent from the DataFrame of the AgentSetDF. Gets called by self.model.agents.remove and self.model.agents.discard. - - Parameters - ---------- - ids : IdsLike - - The ids to remove - - Returns - ------- - Self - """ - ... - - @abstractmethod - def _update_mask( - self, original_active_indices: Index, new_active_indices: Index | None = None - ) -> None: ... - - def __add__(self, other: DataFrame | DataFrameInput) -> Self: - """Add agents to a new AgentSetDF through the + operator. - - Other can be: - - A DataFrame: adds the agents from the DataFrame. - - A DataFrameInput: passes the input to the DataFrame constructor. - - Parameters - ---------- - other : DataFrame | DataFrameInput - The agents to add. - - Returns - ------- - Self - A new AgentContainer with the added agents. - """ - return super().__add__(other) - - def __iadd__(self, other: DataFrame | DataFrameInput) -> Self: - """ - Add agents to the AgentSetDF through the += operator. - - Other can be: - - A DataFrame: adds the agents from the DataFrame. - - A DataFrameInput: passes the input to the DataFrame constructor. - - Parameters - ---------- - other : DataFrame | DataFrameInput - The agents to add. - - Returns - ------- - Self - The updated AgentContainer. - """ - return super().__iadd__(other) - - @abstractmethod - def __getattr__(self, name: str) -> Any: - if __debug__: # Only execute in non-optimized mode - if name == "_df": - raise AttributeError( - "The _df attribute is not set. You probably forgot to call super().__init__ in the __init__ method." - ) - - @overload - def __getitem__(self, key: str | tuple[AgentMask, str]) -> Series | DataFrame: ... - - @overload - def __getitem__( - self, - key: AgentMask | Collection[str] | tuple[AgentMask, Collection[str]], - ) -> DataFrame: ... - - def __getitem__( - self, - key: ( - str - | Collection[str] - | AgentMask - | tuple[AgentMask, str] - | tuple[AgentMask, Collection[str]] - ), - ) -> Series | DataFrame: - attr = super().__getitem__(key) - assert isinstance(attr, (Series, DataFrame, Index)) - return attr - - def __len__(self) -> int: - return len(self._df) - - def __repr__(self) -> str: - return f"{self.__class__.__name__}\n {str(self._df)}" - - def __str__(self) -> str: - return f"{self.__class__.__name__}\n {str(self._df)}" - - def __reversed__(self) -> Iterator: - return reversed(self._df) - - @property - def df(self) -> DataFrame: - return self._df - - @df.setter - def df(self, agents: DataFrame) -> None: - """Set the agents in the AgentSetDF. - - Parameters - ---------- - agents : DataFrame - The agents to set. - """ - self._df = agents - - @property - @abstractmethod - def active_agents(self) -> DataFrame: ... - - @property - @abstractmethod - def inactive_agents(self) -> DataFrame: ... - - @property - def index(self) -> Index: ... - - @property - def pos(self) -> DataFrame: - if self.space is None: - raise AttributeError( - "Attempted to access `pos`, but the model has no space attached." - ) - pos = self._df_get_masked_df( - df=self.space.agents, index_cols="agent_id", mask=self.index - ) - pos = self._df_reindex( - pos, self.index, new_index_cols="unique_id", original_index_cols="agent_id" - ) - return pos diff --git a/mesa_frames/OLD_concrete_accessors.py b/mesa_frames/OLD_concrete_accessors.py deleted file mode 100644 index 71c2097d..00000000 --- a/mesa_frames/OLD_concrete_accessors.py +++ /dev/null @@ -1,147 +0,0 @@ -"""Concrete implementations of agent set accessors. - -This module contains the concrete implementation of the AgentSetsAccessor, -which provides a user-friendly interface for accessing and manipulating -collections of agent sets within the mesa-frames library. -""" - -from __future__ import annotations - -from collections import defaultdict -from collections.abc import Iterable, Iterator, Mapping -from types import MappingProxyType -from typing import Any, Literal, TypeVar, cast - -from mesa_frames.abstract.accessors import AbstractAgentSetsAccessor -from mesa_frames.abstract.agents import AgentSetDF -from mesa_frames.types_ import KeyBy - -TSet = TypeVar("TSet", bound=AgentSetDF) - - -class AgentSetsAccessor(AbstractAgentSetsAccessor): - def __init__(self, parent: mesa_frames.concrete.agents.AgentsDF) -> None: - self._parent = parent - - def __getitem__( - self, key: int | str | type[AgentSetDF] - ) -> AgentSetDF | list[AgentSetDF]: - sets = self._parent._agentsets - if isinstance(key, int): - try: - return sets[key] - except IndexError as e: - raise IndexError( - f"Index {key} out of range for {len(sets)} agent sets" - ) from e - if isinstance(key, str): - for s in sets: - if s.name == key: - return s - available = [getattr(s, "name", None) for s in sets] - raise KeyError(f"No agent set named '{key}'. Available: {available}") - if isinstance(key, type): - matches = [s for s in sets if isinstance(s, key)] - # Always return list for type keys to maintain consistent shape - return matches # type: ignore[return-value] - raise TypeError("Key must be int | str | type[AgentSetDF]") - - def get( - self, - key: int | str | type[TSet], - default: AgentSetDF | list[TSet] | None = None, - ) -> AgentSetDF | list[TSet] | None: - try: - val = self[key] # type: ignore[return-value] - # For type keys, if no matches and a default was provided, return default - if ( - isinstance(key, type) - and isinstance(val, list) - and len(val) == 0 - and default is not None - ): - return default - return val - except (KeyError, IndexError, TypeError): - return default - - def first(self, t: type[TSet]) -> TSet: - match = next((s for s in self._parent._agentsets if isinstance(s, t)), None) - if not match: - raise KeyError(f"No agent set of type {getattr(t, '__name__', t)} found.") - return match - - def all(self, t: type[TSet]) -> list[TSet]: - return [s for s in self._parent._agentsets if isinstance(s, t)] # type: ignore[return-value] - - def at(self, index: int) -> AgentSetDF: - return self[index] # type: ignore[return-value] - - # ---------- key generation and views ---------- - def _gen_key(self, aset: AgentSetDF, idx: int, mode: str) -> Any: - if mode == "name": - return aset.name - if mode == "index": - return idx - if mode == "type": - return type(aset) - raise ValueError("key_by must be 'name'|'index'|'type'") - - def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: - for i, s in enumerate(self._parent._agentsets): - yield self._gen_key(s, i, key_by) - - def items(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: - for i, s in enumerate(self._parent._agentsets): - yield self._gen_key(s, i, key_by), s - - def values(self) -> Iterable[AgentSetDF]: - return iter(self._parent._agentsets) - - def iter(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSetDF]]: - return self.items(key_by=key_by) - - def dict(self, *, key_by: KeyBy = "name") -> dict[Any, AgentSetDF]: - return {k: v for k, v in self.items(key_by=key_by)} - - # ---------- read-only snapshots ---------- - @property - def by_name(self) -> Mapping[str, AgentSetDF]: - return MappingProxyType({cast(str, s.name): s for s in self._parent._agentsets}) - - @property - def by_type(self) -> Mapping[type, list[AgentSetDF]]: - d: dict[type, list[AgentSetDF]] = defaultdict(list) - for s in self._parent._agentsets: - d[type(s)].append(s) - return MappingProxyType(dict(d)) - - # ---------- membership & iteration ---------- - def rename( - self, - target: AgentSetDF - | str - | dict[AgentSetDF | str, str] - | list[tuple[AgentSetDF | str, str]], - new_name: str | None = None, - *, - on_conflict: Literal["canonicalize", "raise"] = "canonicalize", - mode: Literal["atomic", "best_effort"] = "atomic", - ) -> str | dict[AgentSetDF, str]: - return self._parent._rename_sets( - target, new_name, on_conflict=on_conflict, mode=mode - ) - - def __contains__(self, x: str | AgentSetDF) -> bool: - sets = self._parent._agentsets - if isinstance(x, str): - return any(s.name == x for s in sets) - if isinstance(x, AgentSetDF): - return any(s is x for s in sets) - return False - - def __len__(self) -> int: - return len(self._parent._agentsets) - - def __iter__(self) -> Iterator[AgentSetDF]: - return iter(self._parent._agentsets) diff --git a/mesa_frames/OLD_concrete_agents.py b/mesa_frames/OLD_concrete_agents.py deleted file mode 100644 index 799a7b33..00000000 --- a/mesa_frames/OLD_concrete_agents.py +++ /dev/null @@ -1,636 +0,0 @@ -""" -Concrete implementation of the agents collection for mesa-frames. - -This module provides the concrete implementation of the agents collection class -for the mesa-frames library. It defines the AgentsDF class, which serves as a -container for all agent sets in a model, leveraging DataFrame-based storage for -improved performance. - -Classes: - AgentsDF(AgentContainer): - A collection of AgentSetDFs. This class acts as a container for all - agents in the model, organizing them into separate AgentSetDF instances - based on their types. - -The AgentsDF class is designed to be used within ModelDF instances to manage -all agents in the simulation. It provides methods for adding, removing, and -accessing agents and agent sets, while taking advantage of the performance -benefits of DataFrame-based agent storage. - -Usage: - The AgentsDF class is typically instantiated and used within a ModelDF subclass: - - from mesa_frames.concrete.model import ModelDF - from mesa_frames.concrete.agents import AgentsDF - from mesa_frames.concrete import AgentSetPolars - - class MyCustomModel(ModelDF): - def __init__(self): - super().__init__() - # Adding agent sets to the collection - self.agents += AgentSetPolars(self) - self.agents += AnotherAgentSetPolars(self) - - def step(self): - # Step all agent sets - self.agents.do("step") - -Note: - This concrete implementation builds upon the abstract AgentContainer class - defined in the mesa_frames.abstract package, providing a ready-to-use - agents collection that integrates with the DataFrame-based agent storage system. - -For more detailed information on the AgentsDF class and its methods, refer to -the class docstring. -""" - -from __future__ import annotations # For forward references - -from collections import defaultdict -from collections.abc import Callable, Collection, Iterable, Iterator, Sequence -from typing import Any, Literal, Self, cast, overload - -import numpy as np -import polars as pl - -from mesa_frames.abstract.agents import AgentContainer, AgentSetDF -from mesa_frames.types_ import ( - AgentMask, - AgnosticAgentMask, - BoolSeries, - DataFrame, - IdsLike, - Index, - Series, -) - - -class AgentsDF(AgentContainer): - """A collection of AgentSetDFs. All agents of the model are stored here.""" - - _agentsets: list[AgentSetDF] - _ids: pl.Series - - def __init__(self, model: mesa_frames.concrete.model.ModelDF) -> None: - """Initialize a new AgentsDF. - - Parameters - ---------- - model : mesa_frames.concrete.model.ModelDF - The model associated with the AgentsDF. - """ - self._model = model - self._agentsets = [] - self._ids = pl.Series(name="unique_id", dtype=pl.UInt64) - - def add( - self, agents: AgentSetDF | Iterable[AgentSetDF], inplace: bool = True - ) -> Self: - """Add an AgentSetDF to the AgentsDF. - - Parameters - ---------- - agents : AgentSetDF | Iterable[AgentSetDF] - The AgentSetDFs to add. - inplace : bool, optional - Whether to add the AgentSetDFs in place. Defaults to True. - - Returns - ------- - Self - The updated AgentsDF. - - Raises - ------ - ValueError - If any AgentSetDFs are already present or if IDs are not unique. - """ - obj = self._get_obj(inplace) - other_list = obj._return_agentsets_list(agents) - if obj._check_agentsets_presence(other_list).any(): - raise ValueError("Some agentsets are already present in the AgentsDF.") - new_ids = pl.concat( - [obj._ids] + [pl.Series(agentset["unique_id"]) for agentset in other_list] - ) - if new_ids.is_duplicated().any(): - raise ValueError("Some of the agent IDs are not unique.") - obj._agentsets.extend(other_list) - obj._ids = new_ids - return obj - - @overload - def contains(self, agents: int | AgentSetDF) -> bool: ... - - @overload - def contains(self, agents: IdsLike | Iterable[AgentSetDF]) -> pl.Series: ... - - def contains( - self, agents: IdsLike | AgentSetDF | Iterable[AgentSetDF] - ) -> bool | pl.Series: - if isinstance(agents, int): - return agents in self._ids - elif isinstance(agents, AgentSetDF): - return self._check_agentsets_presence([agents]).any() - elif isinstance(agents, Iterable): - if len(agents) == 0: - return True - elif isinstance(next(iter(agents)), AgentSetDF): - agents = cast(Iterable[AgentSetDF], agents) - return self._check_agentsets_presence(list(agents)) - else: # IdsLike - agents = cast(IdsLike, agents) - - return pl.Series(agents, dtype=pl.UInt64).is_in(self._ids) - - @overload - def do( - self, - method_name: str, - *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, - return_results: Literal[False] = False, - inplace: bool = True, - **kwargs, - ) -> Self: ... - - @overload - def do( - self, - method_name: str, - *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, - return_results: Literal[True], - inplace: bool = True, - **kwargs, - ) -> dict[AgentSetDF, Any]: ... - - def do( - self, - method_name: str, - *args, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, - return_results: bool = False, - inplace: bool = True, - **kwargs, - ) -> Self | Any: - obj = self._get_obj(inplace) - agentsets_masks = obj._get_bool_masks(mask) - if return_results: - return { - agentset: agentset.do( - method_name, - *args, - mask=mask, - return_results=return_results, - **kwargs, - inplace=inplace, - ) - for agentset, mask in agentsets_masks.items() - } - else: - obj._agentsets = [ - agentset.do( - method_name, - *args, - mask=mask, - return_results=return_results, - **kwargs, - inplace=inplace, - ) - for agentset, mask in agentsets_masks.items() - ] - return obj - - def get( - self, - attr_names: str | Collection[str] | None = None, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, - ) -> dict[AgentSetDF, Series] | dict[AgentSetDF, DataFrame]: - agentsets_masks = self._get_bool_masks(mask) - result = {} - - # Convert attr_names to list for consistent checking - if attr_names is None: - # None means get all data - no column filtering needed - required_columns = [] - elif isinstance(attr_names, str): - required_columns = [attr_names] - else: - required_columns = list(attr_names) - - for agentset, mask in agentsets_masks.items(): - # Fast column existence check - no data processing, just property access - agentset_columns = agentset.df.columns - - # Check if all required columns exist in this agent set - if not required_columns or all( - col in agentset_columns for col in required_columns - ): - result[agentset] = agentset.get(attr_names, mask) - - return result - - def remove( - self, - agents: AgentSetDF | Iterable[AgentSetDF] | IdsLike, - inplace: bool = True, - ) -> Self: - obj = self._get_obj(inplace) - if agents is None or (isinstance(agents, Iterable) and len(agents) == 0): - return obj - if isinstance(agents, AgentSetDF): - agents = [agents] - if isinstance(agents, Iterable) and isinstance(next(iter(agents)), AgentSetDF): - # We have to get the index of the original AgentSetDF because the copy made AgentSetDFs with different hash - ids = [self._agentsets.index(agentset) for agentset in iter(agents)] - ids.sort(reverse=True) - removed_ids = pl.Series(dtype=pl.UInt64) - for id in ids: - removed_ids = pl.concat( - [ - removed_ids, - pl.Series(obj._agentsets[id]["unique_id"], dtype=pl.UInt64), - ] - ) - obj._agentsets.pop(id) - - else: # IDsLike - if isinstance(agents, (int, np.uint64)): - agents = [agents] - elif isinstance(agents, DataFrame): - agents = agents["unique_id"] - removed_ids = pl.Series(agents, dtype=pl.UInt64) - deleted = 0 - - for agentset in obj._agentsets: - initial_len = len(agentset) - agentset._discard(removed_ids) - deleted += initial_len - len(agentset) - if deleted == len(removed_ids): - break - if deleted < len(removed_ids): # TODO: fix type hint - raise KeyError( - "There exist some IDs which are not present in any agentset" - ) - try: - obj.space.remove_agents(removed_ids, inplace=True) - except ValueError: - pass - obj._ids = obj._ids.filter(obj._ids.is_in(removed_ids).not_()) - return obj - - def select( - self, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, - filter_func: Callable[[AgentSetDF], AgentMask] | None = None, - n: int | None = None, - inplace: bool = True, - negate: bool = False, - ) -> Self: - obj = self._get_obj(inplace) - agentsets_masks = obj._get_bool_masks(mask) - if n is not None: - n = n // len(agentsets_masks) - obj._agentsets = [ - agentset.select( - mask=mask, filter_func=filter_func, n=n, negate=negate, inplace=inplace - ) - for agentset, mask in agentsets_masks.items() - ] - return obj - - def set( - self, - attr_names: str | dict[AgentSetDF, Any] | Collection[str], - values: Any | None = None, - mask: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] = None, - inplace: bool = True, - ) -> Self: - obj = self._get_obj(inplace) - agentsets_masks = obj._get_bool_masks(mask) - if isinstance(attr_names, dict): - for agentset, values in attr_names.items(): - if not inplace: - # We have to get the index of the original AgentSetDF because the copy made AgentSetDFs with different hash - id = self._agentsets.index(agentset) - agentset = obj._agentsets[id] - agentset.set( - attr_names=values, mask=agentsets_masks[agentset], inplace=True - ) - else: - obj._agentsets = [ - agentset.set( - attr_names=attr_names, values=values, mask=mask, inplace=True - ) - for agentset, mask in agentsets_masks.items() - ] - return obj - - def shuffle(self, inplace: bool = True) -> Self: - obj = self._get_obj(inplace) - obj._agentsets = [agentset.shuffle(inplace=True) for agentset in obj._agentsets] - return obj - - def sort( - self, - by: str | Sequence[str], - ascending: bool | Sequence[bool] = True, - inplace: bool = True, - **kwargs, - ) -> Self: - obj = self._get_obj(inplace) - obj._agentsets = [ - agentset.sort(by=by, ascending=ascending, inplace=inplace, **kwargs) - for agentset in obj._agentsets - ] - return obj - - def step(self, inplace: bool = True) -> Self: - """Advance the state of the agents in the AgentsDF by one step. - - Parameters - ---------- - inplace : bool, optional - Whether to update the AgentsDF in place, by default True - - Returns - ------- - Self - """ - obj = self._get_obj(inplace) - for agentset in obj._agentsets: - agentset.step() - return obj - - def _check_ids_presence(self, other: list[AgentSetDF]) -> pl.DataFrame: - """Check if the IDs of the agents to be added are unique. - - Parameters - ---------- - other : list[AgentSetDF] - The AgentSetDFs to check. - - Returns - ------- - pl.DataFrame - A DataFrame with the unique IDs and a boolean column indicating if they are present. - """ - presence_df = pl.DataFrame( - data={"unique_id": self._ids, "present": True}, - schema={"unique_id": pl.UInt64, "present": pl.Boolean}, - ) - for agentset in other: - new_ids = pl.Series(agentset.index, dtype=pl.UInt64) - presence_df = pl.concat( - [ - presence_df, - ( - new_ids.is_in(presence_df["unique_id"]) - .to_frame("present") - .with_columns(unique_id=new_ids) - .select(["unique_id", "present"]) - ), - ] - ) - presence_df = presence_df.slice(self._ids.len()) - return presence_df - - def _check_agentsets_presence(self, other: list[AgentSetDF]) -> pl.Series: - """Check if the agent sets to be added are already present in the AgentsDF. - - Parameters - ---------- - other : list[AgentSetDF] - The AgentSetDFs to check. - - Returns - ------- - pl.Series - A boolean Series indicating if the agent sets are present. - - Raises - ------ - ValueError - If the agent sets are already present in the AgentsDF. - """ - other_set = set(other) - return pl.Series( - [agentset in other_set for agentset in self._agentsets], dtype=pl.Boolean - ) - - def _get_bool_masks( - self, - mask: (AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask]) = None, - ) -> dict[AgentSetDF, BoolSeries]: - return_dictionary = {} - if not isinstance(mask, dict): - # No need to convert numpy integers - let polars handle them directly - mask = {agentset: mask for agentset in self._agentsets} - for agentset, mask_value in mask.items(): - return_dictionary[agentset] = agentset._get_bool_mask(mask_value) - return return_dictionary - - def _return_agentsets_list( - self, agentsets: AgentSetDF | Iterable[AgentSetDF] - ) -> list[AgentSetDF]: - """Convert the agentsets to a list of AgentSetDF. - - Parameters - ---------- - agentsets : AgentSetDF | Iterable[AgentSetDF] - - Returns - ------- - list[AgentSetDF] - """ - return [agentsets] if isinstance(agentsets, AgentSetDF) else list(agentsets) - - def __add__(self, other: AgentSetDF | Iterable[AgentSetDF]) -> Self: - """Add AgentSetDFs to a new AgentsDF through the + operator. - - Parameters - ---------- - other : AgentSetDF | Iterable[AgentSetDF] - The AgentSetDFs to add. - - Returns - ------- - Self - A new AgentsDF with the added AgentSetDFs. - """ - return super().__add__(other) - - def __getattr__(self, name: str) -> dict[AgentSetDF, Any]: - # Avoids infinite recursion of private attributes - if __debug__: # Only execute in non-optimized mode - if name.startswith("_"): - raise AttributeError( - f"'{self.__class__.__name__}' object has no attribute '{name}'" - ) - return {agentset: getattr(agentset, name) for agentset in self._agentsets} - - @overload - def __getitem__( - self, key: str | tuple[dict[AgentSetDF, AgentMask], str] - ) -> dict[AgentSetDF, Series | pl.Expr]: ... - - @overload - def __getitem__( - self, - key: ( - Collection[str] - | AgnosticAgentMask - | IdsLike - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] - ), - ) -> dict[AgentSetDF, DataFrame]: ... - - def __getitem__( - self, - key: ( - str - | Collection[str] - | AgnosticAgentMask - | IdsLike - | tuple[dict[AgentSetDF, AgentMask], str] - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] - ), - ) -> dict[AgentSetDF, Series | pl.Expr] | dict[AgentSetDF, DataFrame]: - return super().__getitem__(key) - - def __iadd__(self, agents: AgentSetDF | Iterable[AgentSetDF]) -> Self: - """Add AgentSetDFs to the AgentsDF through the += operator. - - Parameters - ---------- - agents : AgentSetDF | Iterable[AgentSetDF] - The AgentSetDFs to add. - - Returns - ------- - Self - The updated AgentsDF. - """ - return super().__iadd__(agents) - - def __iter__(self) -> Iterator[dict[str, Any]]: - return (agent for agentset in self._agentsets for agent in iter(agentset)) - - def __isub__(self, agents: AgentSetDF | Iterable[AgentSetDF] | IdsLike) -> Self: - """Remove AgentSetDFs from the AgentsDF through the -= operator. - - Parameters - ---------- - agents : AgentSetDF | Iterable[AgentSetDF] | IdsLike - The AgentSetDFs or agent IDs to remove. - - Returns - ------- - Self - The updated AgentsDF. - """ - return super().__isub__(agents) - - def __len__(self) -> int: - return sum(len(agentset._df) for agentset in self._agentsets) - - def __repr__(self) -> str: - return "\n".join([repr(agentset) for agentset in self._agentsets]) - - def __reversed__(self) -> Iterator: - return ( - agent - for agentset in self._agentsets - for agent in reversed(agentset._backend) - ) - - def __setitem__( - self, - key: ( - str - | Collection[str] - | AgnosticAgentMask - | IdsLike - | tuple[dict[AgentSetDF, AgentMask], str] - | tuple[dict[AgentSetDF, AgentMask], Collection[str]] - ), - values: Any, - ) -> None: - super().__setitem__(key, values) - - def __str__(self) -> str: - return "\n".join([str(agentset) for agentset in self._agentsets]) - - def __sub__(self, agents: AgentSetDF | Iterable[AgentSetDF] | IdsLike) -> Self: - """Remove AgentSetDFs from a new AgentsDF through the - operator. - - Parameters - ---------- - agents : AgentSetDF | Iterable[AgentSetDF] | IdsLike - The AgentSetDFs or agent IDs to remove. Supports NumPy integer types. - - Returns - ------- - Self - A new AgentsDF with the removed AgentSetDFs. - """ - return super().__sub__(agents) - - @property - def df(self) -> dict[AgentSetDF, DataFrame]: - return {agentset: agentset.df for agentset in self._agentsets} - - @df.setter - def df(self, other: Iterable[AgentSetDF]) -> None: - """Set the agents in the AgentsDF. - - Parameters - ---------- - other : Iterable[AgentSetDF] - The AgentSetDFs to set. - """ - self._agentsets = list(other) - - @property - def active_agents(self) -> dict[AgentSetDF, DataFrame]: - return {agentset: agentset.active_agents for agentset in self._agentsets} - - @active_agents.setter - def active_agents( - self, agents: AgnosticAgentMask | IdsLike | dict[AgentSetDF, AgentMask] - ) -> None: - self.select(agents, inplace=True) - - @property - def agentsets_by_type(self) -> dict[type[AgentSetDF], Self]: - """Get the agent sets in the AgentsDF grouped by type. - - Returns - ------- - dict[type[AgentSetDF], Self] - A dictionary mapping agent set types to the corresponding AgentsDF. - """ - - def copy_without_agentsets() -> Self: - return self.copy(deep=False, skip=["_agentsets"]) - - dictionary = defaultdict(copy_without_agentsets) - - for agentset in self._agentsets: - agents_df = dictionary[agentset.__class__] - agents_df._agentsets = [] - agents_df._agentsets = agents_df._agentsets + [agentset] - dictionary[agentset.__class__] = agents_df - return dictionary - - @property - def inactive_agents(self) -> dict[AgentSetDF, DataFrame]: - return {agentset: agentset.inactive_agents for agentset in self._agentsets} - - @property - def index(self) -> dict[AgentSetDF, Index]: - return {agentset: agentset.index for agentset in self._agentsets} - - @property - def pos(self) -> dict[AgentSetDF, DataFrame]: - return {agentset: agentset.pos for agentset in self._agentsets} From 0e1044c04a769744aebaf2fe4d13c4a2a07c31e5 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 16:33:58 +0200 Subject: [PATCH 143/329] refactor: streamline key management methods in AgentSetRegistry and enforce model consistency --- mesa_frames/abstract/agentsetregistry.py | 39 +++++++++++--- mesa_frames/concrete/agentsetregistry.py | 69 ------------------------ 2 files changed, 33 insertions(+), 75 deletions(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index cb535d1b..8ac5093a 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -538,6 +538,13 @@ def __setitem__( - For name keys, the key is authoritative for the assigned set's name - For index keys, collisions on a different entry's name must raise """ + if value.model is not self.model: + raise TypeError("Assigned AgentSet must belong to the same model") + if isinstance(key, (int, str)): + # Delegate to replace() so subclasses centralize invariant handling. + self.replace({key: value}, inplace=True, atomic=True) + return + raise TypeError("Key must be int index or str name") @abstractmethod def __getattr__(self, name: str) -> Any | dict[str, Any]: @@ -568,14 +575,23 @@ def __str__(self) -> str: """Get a string representation of the AgentSets in the registry.""" ... - @abstractmethod def keys( self, *, key_by: KeyBy = "name" ) -> Iterable[str | int | type[mesa_frames.abstract.agentset.AbstractAgentSet]]: """Iterate keys for contained AgentSets (by name|index|type).""" - ... + if key_by == "index": + yield from range(len(self)) + return + if key_by == "type": + for agentset in self: + yield type(agentset) + return + if key_by != "name": + raise ValueError("key_by must be 'name'|'index'|'type'") + for agentset in self: + if agentset.name is not None: + yield agentset.name - @abstractmethod def items( self, *, key_by: KeyBy = "name" ) -> Iterable[ @@ -585,12 +601,23 @@ def items( ] ]: """Iterate (key, AgentSet) pairs for contained sets.""" - ... + if key_by == "index": + for idx, agentset in enumerate(self): + yield idx, agentset + return + if key_by == "type": + for agentset in self: + yield type(agentset), agentset + return + if key_by != "name": + raise ValueError("key_by must be 'name'|'index'|'type'") + for agentset in self: + if agentset.name is not None: + yield agentset.name, agentset - @abstractmethod def values(self) -> Iterable[mesa_frames.abstract.agentset.AbstractAgentSet]: """Iterate contained AgentSets (values view).""" - ... + yield from self @property def model(self) -> mesa_frames.concrete.model.Model: diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index 7cb9e97d..eb13e637 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -669,78 +669,9 @@ def __repr__(self) -> str: def __reversed__(self) -> Iterator[AgentSet]: return reversed(self._agentsets) - def __setitem__(self, key: int | str, value: AgentSet) -> None: - """Assign/replace a single AgentSet at an index or name. - - Enforces name uniqueness and model consistency. - """ - if value.model is not self.model: - raise TypeError("Assigned AgentSet must belong to the same model") - if isinstance(key, int): - if value.name is not None: - for i, s in enumerate(self._agentsets): - if i != key and s.name == value.name: - raise ValueError( - f"Duplicate agent set name disallowed: {value.name}" - ) - self._agentsets[key] = value - elif isinstance(key, str): - try: - value.rename(key) - except Exception: - if hasattr(value, "_name"): - setattr(value, "_name", key) - idx = None - for i, s in enumerate(self._agentsets): - if s.name == key: - idx = i - break - if idx is None: - self._agentsets.append(value) - else: - self._agentsets[idx] = value - else: - raise TypeError("Key must be int index or str name") - # Recompute ids cache - self._recompute_ids() - def __str__(self) -> str: return "\n".join([str(agentset) for agentset in self._agentsets]) - def keys(self, *, key_by: KeyBy = "name") -> Iterable[Any]: - if key_by not in ("name", "index", "type"): - raise ValueError("key_by must be 'name'|'index'|'type'") - if key_by == "index": - yield from range(len(self._agentsets)) - return - if key_by == "type": - for s in self._agentsets: - yield type(s) - return - # name - for s in self._agentsets: - if s.name is not None: - yield s.name - - def items(self, *, key_by: KeyBy = "name") -> Iterable[tuple[Any, AgentSet]]: - if key_by not in ("name", "index", "type"): - raise ValueError("key_by must be 'name'|'index'|'type'") - if key_by == "index": - for i, s in enumerate(self._agentsets): - yield i, s - return - if key_by == "type": - for s in self._agentsets: - yield type(s), s - return - # name - for s in self._agentsets: - if s.name is not None: - yield s.name, s - - def values(self) -> Iterable[AgentSet]: - return iter(self._agentsets) - @property def ids(self) -> pl.Series: """Public view of all agent unique_id values across contained sets.""" From 4187380edae8d915ddb0adde96dcd0e83bba1e76 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 16:37:54 +0200 Subject: [PATCH 144/329] feat: enhance key handling in AbstractAgentSetRegistry for string keys --- mesa_frames/abstract/agentsetregistry.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index 8ac5093a..abb0ef69 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -540,10 +540,22 @@ def __setitem__( """ if value.model is not self.model: raise TypeError("Assigned AgentSet must belong to the same model") - if isinstance(key, (int, str)): + if isinstance(key, int): # Delegate to replace() so subclasses centralize invariant handling. self.replace({key: value}, inplace=True, atomic=True) return + if isinstance(key, str): + for existing in self: + if existing.name == key: + self.replace({key: value}, inplace=True, atomic=True) + return + try: + value.rename(key, inplace=True) + except Exception: + if hasattr(value, "_name"): + value._name = key # type: ignore[attr-defined] + self.add(value, inplace=True) + return raise TypeError("Key must be int index or str name") @abstractmethod From 4ef1a254efa9c5a3cf5ffaf67e0c2bcfd4ee5bf8 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 16:39:21 +0200 Subject: [PATCH 145/329] refactor: remove unused _skip_copy attribute from CopyMixin --- mesa_frames/abstract/mixin.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/mesa_frames/abstract/mixin.py b/mesa_frames/abstract/mixin.py index ce72e6ca..96904eba 100644 --- a/mesa_frames/abstract/mixin.py +++ b/mesa_frames/abstract/mixin.py @@ -66,10 +66,6 @@ class CopyMixin(ABC): _copy_only_reference: list[str] = [ "_model", ] - # Attributes listed here are not copied at all and will not be set - # on the copied object. Useful for lazily re-creating cyclic or - # parent-bound helpers (e.g., accessors) after copy/deepcopy. - _skip_copy: list[str] = [] @abstractmethod def __init__(self): ... @@ -117,7 +113,6 @@ def copy( for k, v in attributes.items() if k not in self._copy_with_method and k not in self._copy_only_reference - and k not in self._skip_copy and k not in skip ] else: @@ -126,20 +121,15 @@ def copy( for k, v in self.__dict__.items() if k not in self._copy_with_method and k not in self._copy_only_reference - and k not in self._skip_copy and k not in skip ] # Copy attributes with a reference only for attr in self._copy_only_reference: - if attr in self._skip_copy or attr in skip: - continue setattr(obj, attr, getattr(self, attr)) # Copy attributes with a specified method for attr in self._copy_with_method: - if attr in self._skip_copy or attr in skip: - continue attr_obj = getattr(self, attr) attr_copy_method, attr_copy_args = self._copy_with_method[attr] setattr(obj, attr, getattr(attr_obj, attr_copy_method)(*attr_copy_args)) From 073bb9079700d62d67854c18cf3cc1cbec76bc0f Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 17:17:37 +0200 Subject: [PATCH 146/329] refactor: improve key handling and update logic in AgentSetRegistry --- mesa_frames/concrete/agentsetregistry.py | 68 ++++++++++++++++++------ 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/mesa_frames/concrete/agentsetregistry.py b/mesa_frames/concrete/agentsetregistry.py index eb13e637..4b486ba2 100644 --- a/mesa_frames/concrete/agentsetregistry.py +++ b/mesa_frames/concrete/agentsetregistry.py @@ -420,27 +420,65 @@ def do( ) -> Self | Any: obj = self._get_obj(inplace) target_sets = obj._resolve_selector(sets) + + if not target_sets: + return {} if return_results else obj + + index_lookup = {id(s): idx for idx, s in enumerate(obj._agentsets)} + if return_results: - def make_key(i: int, s: AgentSet) -> Any: + def make_key(agentset: AgentSet) -> Any: if key_by == "name": - return s.name + return agentset.name if key_by == "index": - return i + try: + return index_lookup[id(agentset)] + except KeyError as exc: # pragma: no cover - defensive + raise ValueError( + "AgentSet not found in registry; cannot key by index." + ) from exc if key_by == "type": - return type(s) - return s # backward-compatible: key by object - - return { - make_key(i, s): s.do( - method_name, *args, return_results=True, inplace=inplace, **kwargs + return type(agentset) + return agentset # backward-compatible: key by object + + results: dict[Any, Any] = {} + for agentset in target_sets: + key = make_key(agentset) + if key_by == "type" and key in results: + raise ValueError( + "Multiple agent sets of the same type were selected; " + "use key_by='name' or key_by='index' instead." + ) + results[key] = agentset.do( + method_name, + *args, + return_results=True, + inplace=inplace, + **kwargs, ) - for i, s in enumerate(target_sets) - } - obj._agentsets = [ - s.do(method_name, *args, return_results=False, inplace=inplace, **kwargs) - for s in target_sets - ] + return results + + updates: list[tuple[int, AgentSet]] = [] + for agentset in target_sets: + try: + registry_index = index_lookup[id(agentset)] + except KeyError as exc: # pragma: no cover - defensive + raise ValueError( + "AgentSet not found in registry; cannot apply operation." + ) from exc + updated = agentset.do( + method_name, + *args, + return_results=False, + inplace=inplace, + **kwargs, + ) + updates.append((registry_index, updated)) + + for registry_index, updated in updates: + obj._agentsets[registry_index] = updated + obj._recompute_ids() return obj @overload From dccc8345d2d085177377b292a2f47cf0fdde19e6 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 17:17:45 +0200 Subject: [PATCH 147/329] fix: specify exception type in DataCollector to improve error handling --- mesa_frames/concrete/datacollector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa_frames/concrete/datacollector.py b/mesa_frames/concrete/datacollector.py index cd2cc72e..f7db338e 100644 --- a/mesa_frames/concrete/datacollector.py +++ b/mesa_frames/concrete/datacollector.py @@ -240,7 +240,7 @@ def _is_str_collection(x: Any) -> bool: "Registry-level reporter dict values must be Series or DataFrame" ) called = True - except Exception: + except TypeError: called = False if not called: From 6251de99ea09dfd0c5aafc38eff77699903d79be Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 18:48:13 +0200 Subject: [PATCH 148/329] chore: update index.md to include README content to remove duplication --- docs/general/index.md | 90 +------------------------------------------ 1 file changed, 1 insertion(+), 89 deletions(-) diff --git a/docs/general/index.md b/docs/general/index.md index 9859d2ee..ee967623 100644 --- a/docs/general/index.md +++ b/docs/general/index.md @@ -1,89 +1 @@ -# Welcome to mesa-frames 🚀 - -mesa-frames is an extension of the [mesa](https://github.com/projectmesa/mesa) framework, designed for complex simulations with thousands of agents. By storing agents in a DataFrame, mesa-frames significantly enhances the performance and scalability of mesa, while maintaining a similar syntax. - -You can get a model which is multiple orders of magnitude faster based on the number of agents - the more agents, the faster the relative performance. - -## Why DataFrames? 📊 - -DataFrames are optimized for simultaneous operations through [SIMD processing](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data). Currently, mesa-frames supports the library: - -- [Polars](https://pola.rs/): A new DataFrame library with a Rust backend, offering innovations like Apache Arrow memory format and support for larger-than-memory DataFrames. - -## Performance Boost 🏎️ - -Check out our performance graphs comparing mesa and mesa-frames for the [Boltzmann Wealth model](https://mesa.readthedocs.io/en/stable/tutorials/intro_tutorial.html): - -![Performance Graph with Mesa](https://github.com/projectmesa/mesa-frames/raw/main/examples/boltzmann_wealth/boltzmann_with_mesa.png) - -![Performance Graph without Mesa](https://github.com/projectmesa/mesa-frames/raw/main/examples/boltzmann_wealth/boltzmann_no_mesa.png) - -## Quick Start 🚀 - -### Installation - -#### Installing from PyPI - -```bash -pip install mesa-frames -``` - -#### Installing from Source - -```bash -git clone https://github.com/projectmesa/mesa-frames.git -cd mesa_frames -pip install -e . -``` - -### Basic Usage - -Here's a quick example of how to create a model using mesa-frames: - -```python -from mesa_frames import AgentSet, Model -import polars as pl - -class MoneyAgents(AgentSet): - def __init__(self, n: int, model: Model): - super().__init__(model) - self += pl.DataFrame( - {"wealth": pl.ones(n, eager=True)} - ) - - def step(self) -> None: - self.do("give_money") - - def give_money(self): - # ... (implementation details) - -class MoneyModel(Model): - def __init__(self, N: int): - super().__init__() - self.sets += MoneyAgents(N, self) - - def step(self): - self.sets.do("step") - - def run_model(self, n): - for _ in range(n): - self.step() -``` - -## What's Next? 🔮 - -- API refinement for seamless transition from mesa -- Support for mesa functions -- Multiple other spaces: GeoGrid, ContinuousSpace, Network... -- Additional backends: Dask, cuDF (GPU), Dask-cuDF (GPU)... -- More examples: Schelling model, ... -- Automatic vectorization of existing mesa models -- Backend-agnostic AgentSet class - -## Get Involved! 🤝 - -mesa-frames is in its early stages, and we welcome your feedback and contributions! Check out our [GitHub repository](https://github.com/projectmesa/mesa-frames) to get started. - -## License - -mesa-frames is available under the MIT License. See the [LICENSE](https://github.com/projectmesa/mesa-frames/blob/main/LICENSE) file for full details. +{% include-markdown "../../README.md" %} \ No newline at end of file From 169c61826f9ec691bfb49f7d043ab3b93220e9b7 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 18:48:24 +0200 Subject: [PATCH 149/329] fix: update benchmarks navigation link to correct file path --- mkdocs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs.yml b/mkdocs.yml index 8a462881..0e55fd49 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -113,7 +113,7 @@ nav: - Introductory Tutorial: user-guide/2_introductory-tutorial.ipynb - Data Collector Tutorial: user-guide/4_datacollector.ipynb - Advanced Tutorial: user-guide/3_advanced-tutorial.md - - Benchmarks: user-guide/4_benchmarks.md + - Benchmarks: user-guide/5_benchmarks.md - API Reference: api/index.html - Contributing: - Contribution Guide: contributing.md From 6287a1b39675e92999ed651416d1aad1de03d47a Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 19:09:25 +0200 Subject: [PATCH 150/329] fix: clarify guidance on using vectorized operations and correct sample method reference --- docs/general/user-guide/0_getting-started.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/general/user-guide/0_getting-started.md b/docs/general/user-guide/0_getting-started.md index 1edc1587..93f95269 100644 --- a/docs/general/user-guide/0_getting-started.md +++ b/docs/general/user-guide/0_getting-started.md @@ -21,7 +21,7 @@ mesa-frames leverages the power of vectorized operations provided by DataFrame l - This approach is significantly faster than iterating over individual agents - Complex behaviors can be expressed in fewer lines of code -You should never use loops to iterate through your agents. Instead, use vectorized operations and implemented methods. If you need to loop, loop through vectorized operations (see the advanced tutorial SugarScape IG for more information). +Default to vectorized operations when expressing agent behaviour; that's where mesa-frames gains most of its speed-ups. If your agents must act sequentially (for example, to resolve conflicts or enforce ordering), fall back to loops or staged vectorized passes—mesa-frames will behave more like base mesa in those situations. We'll unpack these trade-offs in the upcoming SugarScape advanced tutorial. It's important to note that in traditional `mesa` models, the order in which agents are activated can significantly impact the results of the model (see [Comer, 2014](http://mars.gmu.edu/bitstream/handle/1920/9070/Comer_gmu_0883E_10539.pdf)). `mesa-frames`, by default, doesn't have this issue as all agents are processed simultaneously. However, this comes with the trade-off of needing to carefully implement conflict resolution mechanisms when sequential processing is required. We'll discuss how to handle these situations later in this guide. @@ -42,7 +42,7 @@ Here's a comparison between mesa-frames and mesa: self.select(self.wealth > 0) # Receiving agents are sampled (only native expressions currently supported) - other_agents = self.sets.sample( + other_agents = self.model.sets.sample( n=len(self.active_agents), with_replacement=True ) @@ -92,7 +92,7 @@ If you're familiar with mesa, this guide will help you understand the key differ }) def step(self): givers = self.wealth > 0 - receivers = self.sets.sample(n=len(self.active_agents)) + receivers = self.model.sets.sample(n=len(self.active_agents)) self[givers, "wealth"] -= 1 new_wealth = receivers.groupby("unique_id").count() self[new_wealth["unique_id"], "wealth"] += new_wealth["count"] From e8624f9d3589e14f535a726793af48896768e750 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 19:10:51 +0200 Subject: [PATCH 151/329] docs: streamline installation instructions for development setup --- README.md | 52 +++++----------------------------------------------- 1 file changed, 5 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 6a16baad..5486a8b7 100644 --- a/README.md +++ b/README.md @@ -24,59 +24,17 @@ The following is a performance graph showing execution time using mesa and mesa- pip install mesa-frames ``` -### Install from Source +### Install from Source (development) -To install the most updated version of mesa-frames, you can clone the repository and install the package in editable mode. - -#### Cloning the Repository - -To get started with mesa-frames, first clone the repository from GitHub: +Clone the repository and install dependencies with [uv](https://docs.astral.sh/uv/): ```bash git clone https://github.com/projectmesa/mesa-frames.git -cd mesa_frames -``` - -#### Installing in a Conda Environment - -If you want to install it into a new environment: - -```bash -conda create -n myenv +cd mesa-frames +uv sync --all-extras ``` -If you want to install it into an existing environment: - -```bash -conda activate myenv -``` - -Then, to install mesa-frames itself: - -```bash -pip install -e . -``` - -#### Installing in a Python Virtual Environment - -If you want to install it into a new environment: - -```bash -python3 -m venv myenv -source myenv/bin/activate # On Windows, use `myenv\Scripts\activate` -``` - -If you want to install it into an existing environment: - -```bash -source myenv/bin/activate # On Windows, use `myenv\Scripts\activate` -``` - -Then, to install mesa-frames itself: - -```bash -pip install -e . -``` +`uv sync` creates a local `.venv/` with mesa-frames and its development extras. ## Usage From 4633c66232e3663a041505a13d05294cac7955ca Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 19:11:40 +0200 Subject: [PATCH 152/329] docs: update dependency installation instructions to streamline setup with uv --- CONTRIBUTING.md | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 147b84d3..bb8b4148 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -58,28 +58,13 @@ Before you begin contributing, ensure that you have the necessary tools installe #### **Step 3: Install Dependencies** 📦 -It is recommended to set up a virtual environment before installing dependencies. +We manage the development environment with [uv](https://docs.astral.sh/uv/): -- **Using UV**: +```sh +uv sync --all-extras +``` - ```sh - uv add --dev .[dev] - ``` - -- **Using Hatch**: - - ```sh - hatch env create dev - ``` - -- **Using Standard Python**: - - ```sh - python3 -m venv myenv - source myenv/bin/activate # macOS/Linux - myenv\Scripts\activate # Windows - pip install -e ".[dev]" - ``` +This creates `.venv/` and installs mesa-frames with the development extras. #### **Step 4: Make and Commit Changes** ✨ @@ -99,21 +84,19 @@ It is recommended to set up a virtual environment before installing dependencies - **Run pre-commit hooks** to enforce code quality standards: ```sh - pre-commit run + uv run pre-commit run -a ``` - **Run tests** to ensure your contribution does not break functionality: ```sh - pytest --cov + uv run pytest -q --cov=mesa_frames --cov-report=term-missing ``` - - If using UV: `uv run pytest --cov` - - **Optional: Enable runtime type checking** during development for enhanced type safety: ```sh - MESA_FRAMES_RUNTIME_TYPECHECKING=1 uv run pytest --cov + MESA_FRAMES_RUNTIME_TYPECHECKING=1 uv run pytest -q --cov=mesa_frames --cov-report=term-missing ``` !!! tip "Automatically Enabled" @@ -135,8 +118,7 @@ It is recommended to set up a virtual environment before installing dependencies - Preview your changes by running: ```sh - mkdocs serve - uv run mkdocs serve #If using uv + uv run mkdocs serve ``` - Open `http://127.0.0.1:8000` in your browser to verify documentation updates. From c3797c195c66558d6081f28d208aa12721ad494e Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 19:11:54 +0200 Subject: [PATCH 153/329] fix: correct minor wording for clarity in vectorized operations section --- docs/general/user-guide/0_getting-started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/user-guide/0_getting-started.md b/docs/general/user-guide/0_getting-started.md index 93f95269..51ebe319 100644 --- a/docs/general/user-guide/0_getting-started.md +++ b/docs/general/user-guide/0_getting-started.md @@ -21,7 +21,7 @@ mesa-frames leverages the power of vectorized operations provided by DataFrame l - This approach is significantly faster than iterating over individual agents - Complex behaviors can be expressed in fewer lines of code -Default to vectorized operations when expressing agent behaviour; that's where mesa-frames gains most of its speed-ups. If your agents must act sequentially (for example, to resolve conflicts or enforce ordering), fall back to loops or staged vectorized passes—mesa-frames will behave more like base mesa in those situations. We'll unpack these trade-offs in the upcoming SugarScape advanced tutorial. +Default to vectorized operations when expressing agent behaviour; that's where mesa-frames gains most of its speed-ups. If your agents must act sequentially (for example, to resolve conflicts or enforce ordering), fall back to loops or staged vectorized passes—mesa-frames will behave more like base mesa in those situations. We'll unpack these trade-offs in the SugarScape advanced tutorial. It's important to note that in traditional `mesa` models, the order in which agents are activated can significantly impact the results of the model (see [Comer, 2014](http://mars.gmu.edu/bitstream/handle/1920/9070/Comer_gmu_0883E_10539.pdf)). `mesa-frames`, by default, doesn't have this issue as all agents are processed simultaneously. However, this comes with the trade-off of needing to carefully implement conflict resolution mechanisms when sequential processing is required. We'll discuss how to handle these situations later in this guide. From 48b7659f73265e209b4f27f98e737fc07959b8b7 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 19:12:01 +0200 Subject: [PATCH 154/329] fix: swap benchmark graph images for Boltzmann Wealth Model comparisons --- docs/general/user-guide/5_benchmarks.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/general/user-guide/5_benchmarks.md b/docs/general/user-guide/5_benchmarks.md index 61fca87b..233c394c 100644 --- a/docs/general/user-guide/5_benchmarks.md +++ b/docs/general/user-guide/5_benchmarks.md @@ -8,11 +8,11 @@ mesa-frames offers significant performance improvements over the original mesa f ### Comparison with mesa -![Performance Graph BW](https://github.com/projectmesa/mesa-frames/raw/main/examples/boltzmann_wealth/boltzmann_no_mesa.png) +![Performance Graph BW](https://github.com/projectmesa/mesa-frames/raw/main/examples/boltzmann_wealth/boltzmann_with_mesa.png) ### Comparison of mesa-frames implementations -![Performance Graph BW without Mesa](https://github.com/projectmesa/mesa-frames/raw/main/examples/boltzmann_wealth/boltzmann_with_mesa.png) +![Performance Graph BW without Mesa](https://github.com/projectmesa/mesa-frames/raw/main/examples/boltzmann_wealth/boltzmann_no_mesa.png) ## SugarScape with Instantaneous Growback 🍬 From 7912b0469b7e30fe8f586b91b8eb7f2b9015cb9c Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 19 Sep 2025 19:26:33 +0200 Subject: [PATCH 155/329] docs: add tooling instructions for running tests and checks in development setup --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5486a8b7..a68823dc 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,13 @@ cd mesa-frames uv sync --all-extras ``` -`uv sync` creates a local `.venv/` with mesa-frames and its development extras. +`uv sync` creates a local `.venv/` with mesa-frames and its development extras. Run tooling through uv to keep the virtual environment isolated: + +```bash +uv run pytest -q --cov=mesa_frames --cov-report=term-missing +uv run ruff check . --fix +uv run pre-commit run -a +``` ## Usage From 30478ff35d31cdc1946a8c6c82c3d50ae327caea Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 12:35:54 +0200 Subject: [PATCH 156/329] docs: add advanced tutorial for Sugarscape model using mesa-frames --- .../general/user-guide/3_advanced_tutorial.py | 950 ++++++++++++++++++ 1 file changed, 950 insertions(+) create mode 100644 docs/general/user-guide/3_advanced_tutorial.py diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py new file mode 100644 index 00000000..0f31f317 --- /dev/null +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -0,0 +1,950 @@ +# --- +# jupyter: +# jupytext: +# formats: py:percent,ipynb +# kernelspec: +# display_name: Python 3 (uv) +# language: python +# name: python3 +# --- + +# %% [markdown] +""" +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/projectmesa/mesa-frames/blob/main/docs/general/user-guide/3_advanced_tutorial.ipynb) + +# Advanced Tutorial — Rebuilding Sugarscape with mesa-frames + +We revisit the classic Sugarscape instant-growback model described in chapter 2 of [Growing Artificial Societies](https://direct.mit.edu/books/monograph/2503/Growing-Artificial-SocietiesSocial-Science-from) (Epstein & Axtell, +1996) and rebuild it step by step using `mesa-frames`. Along the way we highlight why the traditional definition is not ideal for high-performance with mesa-frames and how a simple relaxation can unlock vectorisation and lead to similar macro behaviour. + +## Sugarscape in Plain Terms + +We model a population of *ants* living on a rectangular grid rich in sugar. Each +cell can host at most one ant and holds a fixed amount of sugar. Every time step +unfolds as follows: + +* **Sense:** each ant looks outward along the four cardinal directions up to its + `vision` radius and spots open cells. +* **Move:** the ant chooses the cell with highest sugar (breaking ties by + distance and coordinates). The sugar on cells that are already occupied (including its own) is 0. +* **Eat & survive:** ants harvest the sugar on the cell they occupy. If their + sugar stock falls below their `metabolism` cost, they die. +* **Regrow:** sugar instantly regrows to its maximum level on empty cells. The + landscape is drawn from a uniform distribution, so resources are homogeneous + on average and the interesting dynamics come from agent heterogeneity and + congestion. + +The update schedule matters for micro-behaviour, so we study three variants: + +1. **Sequential loop (asynchronous):** This is the traditional definition. Ants move one at a time in random order. +This cannnot be vectorised easily as the best move for an ant might depend on the moves of earlier ants (for example, if they target the same cell). +2. **Sequential with Numba:** matches the first variant but relies on a compiled + helper for speed. +3. **Parallel (synchronous):** all ants propose moves; conflicts are resolved at + random before applying the winners simultaneously (and the losers get to their second-best cell, etc). + +Our goal is to show that, under instantaneous growback and uniform resources, +the model converges to the *same* macroscopic inequality pattern regardless of +whether agents act sequentially or in parallel and that As long as the random draws do +not push the system into extinction, the long-run Gini coefficient of wealth and +the wealth–trait correlations line up within sampling error — a classic example +of emergent macro regularities in agent-based models. +""" + +# %% [markdown] +# First, let's install and import the necessary packages. + +# %% [markdown] +# If you're running this tutorial on Google Colab or another fresh environment, +# uncomment the cell below to install the required dependencies. + +# %% +# !pip install git+https://github.com/projectmesa/mesa-frames polars numba numpy + +# %% [markdown] +"""## 1. Imports""" + +# %% +from __future__ import annotations + +from collections import defaultdict +from dataclasses import dataclass +from time import perf_counter +from typing import Iterable + +import numpy as np +import polars as pl +from numba import njit + +from mesa_frames import AgentSet, DataCollector, Grid, Model + +# %% [markdown] +"""## 2. Model definition + +In this section we define the model class that wires together the grid and the agents. +Note that we define agent_type as flexible so we can plug in different movement policies later. +Also sugar_grid, initial_sugar, metabolism, vision, and positions are optional parameters so we can reuse the same initial conditions across variants. + +The space is a von Neumann grid (which means agents can only move up, down, left, or right) with capacity 1, meaning each cell can host at most one agent. +The sugar field is stored as part of the cell data frame, with columns for current sugar and maximum sugar (for regrowth). The model also sets up a data collector to track aggregate statistics and agent traits over time. + + +""" + + +# %% + + +class SugarscapeTutorialModel(Model): + """Minimal Sugarscape model used throughout the tutorial.""" + + def __init__( + self, + agent_type: type["SugarscapeAgentsBase"], + n_agents: int, + *, + sugar_grid: np.ndarray | None = None, + initial_sugar: np.ndarray | None = None, + metabolism: np.ndarray | None = None, + vision: np.ndarray | None = None, + positions: pl.DataFrame | None = None, + seed: int | None = None, + width: int | None = None, + height: int | None = None, + max_sugar: int = 4, + ) -> None: + super().__init__(seed) + rng = self.random + + + + if sugar_grid is None: + if width is None or height is None: + raise ValueError( + "When `sugar_grid` is omitted you must provide `width` and `height`." + ) + sugar_grid = self._generate_sugar_grid(rng, width, height, max_sugar) + else: + width, height = sugar_grid.shape + + self.space = Grid( + self, [width, height], neighborhood_type="von_neumann", capacity=1 + ) + dim_0 = pl.Series("dim_0", pl.arange(width, eager=True)).to_frame() + dim_1 = pl.Series("dim_1", pl.arange(height, eager=True)).to_frame() + sugar_df = dim_0.join(dim_1, how="cross").with_columns( + sugar=sugar_grid.flatten(), max_sugar=sugar_grid.flatten() + ) + self.space.set_cells(sugar_df) + self._max_sugar = sugar_df.select(["dim_0", "dim_1", "max_sugar"]) + + if initial_sugar is None: + initial_sugar = rng.integers(6, 25, size=n_agents, dtype=np.int64) + else: + n_agents = len(initial_sugar) + if metabolism is None: + metabolism = rng.integers(2, 5, size=n_agents, dtype=np.int64) + if vision is None: + vision = rng.integers(1, 6, size=n_agents, dtype=np.int64) + + main_set = agent_type( + self, + n_agents, + initial_sugar=initial_sugar, + metabolism=metabolism, + vision=vision, + ) + self.sets += main_set + self.population = main_set + + if positions is None: + positions = self._generate_initial_positions(rng, n_agents, width, height) + self.space.place_agents(self.sets, positions.select(["dim_0", "dim_1"])) + + self.datacollector = DataCollector( + model=self, + model_reporters={ + "mean_sugar": lambda m: 0.0 + if len(m.population) == 0 + else float(m.population.df["sugar"].mean()), + "total_sugar": lambda m: float(m.population.df["sugar"].sum()) + if len(m.population) + else 0.0, + "living_agents": lambda m: len(m.population), + }, + agent_reporters={"traits": ["sugar", "metabolism", "vision"]}, + ) + self.datacollector.collect() + + @staticmethod + def _generate_sugar_grid( + rng: np.random.Generator, width: int, height: int, max_sugar: int + ) -> np.ndarray: + """Generate a random sugar grid with values between 0 and max_sugar (inclusive). + + Parameters + ---------- + rng : np.random.Generator + Random number generator for reproducibility. + width : int + Width of the grid. + height : int + Height of the grid. + max_sugar : int + Maximum sugar level for any cell. + + Returns + ------- + np.ndarray + A 2D array representing the sugar levels on the grid. + """ + return rng.integers(0, max_sugar + 1, size=(width, height), dtype=np.int64) + + @staticmethod + def _generate_initial_positions( + rng: np.random.Generator, n_agents: int, width: int, height: int + ) -> pl.DataFrame: + total_cells = width * height + if n_agents > total_cells: + raise ValueError( + "Cannot place more agents than grid cells when capacity is 1." + ) + indices = rng.choice(total_cells, size=n_agents, replace=False) + return pl.DataFrame( + { + "dim_0": (indices // height).astype(np.int64), + "dim_1": (indices % height).astype(np.int64), + } + ) + + def step(self) -> None: + if len(self.population) == 0: + self.running = False + return + self._advance_sugar_field() + self.population.step() + self.datacollector.collect() + if len(self.population) == 0: + self.running = False + + def run(self, steps: int) -> None: + for _ in range(steps): + if not self.running: + break + self.step() + + def _advance_sugar_field(self) -> None: + empty_cells = self.space.empty_cells + if not empty_cells.is_empty(): + refresh = empty_cells.join(self._max_sugar, on=["dim_0", "dim_1"], how="left") + self.space.set_cells(empty_cells, {"sugar": refresh["max_sugar"]}) + full_cells = self.space.full_cells + if not full_cells.is_empty(): + zeros = pl.Series(np.zeros(len(full_cells), dtype=np.int64)) + self.space.set_cells(full_cells, {"sugar": zeros}) + + + + + + +# %% +GRID_WIDTH = 50 +GRID_HEIGHT = 50 +NUM_AGENTS = 400 +MODEL_STEPS = 60 + +@njit(cache=True) +def _numba_should_replace( + best_sugar: int, + best_distance: int, + best_x: int, + best_y: int, + candidate_sugar: int, + candidate_distance: int, + candidate_x: int, + candidate_y: int, +) -> bool: + if candidate_sugar > best_sugar: + return True + if candidate_sugar == best_sugar: + if candidate_distance < best_distance: + return True + if candidate_distance == best_distance: + if candidate_x < best_x: + return True + if candidate_x == best_x and candidate_y < best_y: + return True + return False + + +@njit(cache=True) +def _numba_find_best_cell( + x0: int, + y0: int, + vision: int, + sugar_array: np.ndarray, + occupied: np.ndarray, +) -> tuple[int, int]: + width, height = sugar_array.shape + best_x = x0 + best_y = y0 + best_sugar = sugar_array[x0, y0] + best_distance = 0 + + for step in range(1, vision + 1): + nx = x0 + step + if nx < width and not occupied[nx, y0]: + sugar_here = sugar_array[nx, y0] + if _numba_should_replace( + best_sugar, best_distance, best_x, best_y, sugar_here, step, nx, y0 + ): + best_x = nx + best_y = y0 + best_sugar = sugar_here + best_distance = step + + nx = x0 - step + if nx >= 0 and not occupied[nx, y0]: + sugar_here = sugar_array[nx, y0] + if _numba_should_replace( + best_sugar, best_distance, best_x, best_y, sugar_here, step, nx, y0 + ): + best_x = nx + best_y = y0 + best_sugar = sugar_here + best_distance = step + + ny = y0 + step + if ny < height and not occupied[x0, ny]: + sugar_here = sugar_array[x0, ny] + if _numba_should_replace( + best_sugar, best_distance, best_x, best_y, sugar_here, step, x0, ny + ): + best_x = x0 + best_y = ny + best_sugar = sugar_here + best_distance = step + + ny = y0 - step + if ny >= 0 and not occupied[x0, ny]: + sugar_here = sugar_array[x0, ny] + if _numba_should_replace( + best_sugar, best_distance, best_x, best_y, sugar_here, step, x0, ny + ): + best_x = x0 + best_y = ny + best_sugar = sugar_here + best_distance = step + + return best_x, best_y + + +@njit(cache=True) +def sequential_move_numba( + dim0: np.ndarray, + dim1: np.ndarray, + vision: np.ndarray, + sugar_array: np.ndarray, +) -> tuple[np.ndarray, np.ndarray]: + n_agents = dim0.shape[0] + width, height = sugar_array.shape + new_dim0 = dim0.copy() + new_dim1 = dim1.copy() + occupied = np.zeros((width, height), dtype=np.bool_) + + for i in range(n_agents): + occupied[new_dim0[i], new_dim1[i]] = True + + for i in range(n_agents): + x0 = new_dim0[i] + y0 = new_dim1[i] + occupied[x0, y0] = False + best_x, best_y = _numba_find_best_cell( + x0, y0, int(vision[i]), sugar_array, occupied + ) + occupied[best_x, best_y] = True + new_dim0[i] = best_x + new_dim1[i] = best_y + + return new_dim0, new_dim1 + + + + +# %% [markdown] +""" +## 2. Agent Scaffolding + +With the space logic in place we can define the agents. The base class stores +traits and implements eating/starvation; concrete subclasses only override +`move`. +""" + + +class SugarscapeAgentsBase(AgentSet): + def __init__( + self, + model: Model, + n_agents: int, + *, + initial_sugar: np.ndarray | None = None, + metabolism: np.ndarray | None = None, + vision: np.ndarray | None = None, + ) -> None: + super().__init__(model) + rng = model.random + if initial_sugar is None: + initial_sugar = rng.integers(6, 25, size=n_agents, dtype=np.int64) + if metabolism is None: + metabolism = rng.integers(2, 5, size=n_agents, dtype=np.int64) + if vision is None: + vision = rng.integers(1, 6, size=n_agents, dtype=np.int64) + self.add( + pl.DataFrame( + { + "sugar": initial_sugar, + "metabolism": metabolism, + "vision": vision, + } + ) + ) + + def step(self) -> None: + self.shuffle(inplace=True) + self.move() + self.eat() + self._remove_starved() + + def move(self) -> None: # pragma: no cover + raise NotImplementedError + + def eat(self) -> None: + occupied_ids = self.index.to_list() + occupied = self.space.cells.filter(pl.col("agent_id").is_in(occupied_ids)) + if occupied.is_empty(): + return + ids = occupied["agent_id"] + self[ids, "sugar"] = ( + self[ids, "sugar"] + occupied["sugar"] - self[ids, "metabolism"] + ) + self.space.set_cells( + occupied.select(["dim_0", "dim_1"]), + {"sugar": pl.Series(np.zeros(len(occupied), dtype=np.int64))}, + ) + + def _remove_starved(self) -> None: + starved = self.df.filter(pl.col("sugar") <= 0) + if not starved.is_empty(): + self.discard(starved) + + def _current_sugar_map(self) -> dict[tuple[int, int], int]: + cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) + return { + (int(x), int(y)): 0 if sugar is None else int(sugar) + for x, y, sugar in cells.iter_rows() + } + + @staticmethod + def _manhattan(a: tuple[int, int], b: tuple[int, int]) -> int: + return abs(a[0] - b[0]) + abs(a[1] - b[1]) + + def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: + x0, y0 = origin + width, height = self.space.dimensions + cells: list[tuple[int, int]] = [origin] + for step in range(1, vision + 1): + if x0 + step < width: + cells.append((x0 + step, y0)) + if x0 - step >= 0: + cells.append((x0 - step, y0)) + if y0 + step < height: + cells.append((x0, y0 + step)) + if y0 - step >= 0: + cells.append((x0, y0 - step)) + return cells + + def _choose_best_cell( + self, + origin: tuple[int, int], + vision: int, + sugar_map: dict[tuple[int, int], int], + blocked: set[tuple[int, int]] | None, + ) -> tuple[int, int]: + best_cell = origin + best_sugar = sugar_map.get(origin, 0) + best_distance = 0 + for candidate in self._visible_cells(origin, vision): + if blocked and candidate != origin and candidate in blocked: + continue + sugar_here = sugar_map.get(candidate, 0) + distance = self._manhattan(origin, candidate) + better = False + if sugar_here > best_sugar: + better = True + elif sugar_here == best_sugar: + if distance < best_distance: + better = True + elif distance == best_distance and candidate < best_cell: + better = True + if better: + best_cell = candidate + best_sugar = sugar_here + best_distance = distance + return best_cell + + +# %% [markdown] +""" +## 3. Sequential Movement +""" + + +class SugarscapeSequentialAgents(SugarscapeAgentsBase): + def move(self) -> None: + sugar_map = self._current_sugar_map() + state = self.df.join(self.pos, on="unique_id", how="left") + positions = { + int(row["unique_id"]): (int(row["dim_0"]), int(row["dim_1"])) + for row in state.iter_rows(named=True) + } + taken: set[tuple[int, int]] = set(positions.values()) + + for row in state.iter_rows(named=True): + agent_id = int(row["unique_id"]) + vision = int(row["vision"]) + current = positions[agent_id] + taken.discard(current) + target = self._choose_best_cell(current, vision, sugar_map, taken) + taken.add(target) + positions[agent_id] = target + if target != current: + self.space.move_agents(agent_id, target) + + +# %% [markdown] +""" +## 4. Speeding Up the Loop with Numba +""" + + +class SugarscapeNumbaAgents(SugarscapeAgentsBase): + def move(self) -> None: + state = self.df.join(self.pos, on="unique_id", how="left") + if state.is_empty(): + return + + agent_ids = state["unique_id"].to_list() + dim0 = state["dim_0"].to_numpy().astype(np.int64) + dim1 = state["dim_1"].to_numpy().astype(np.int64) + vision = state["vision"].to_numpy().astype(np.int64) + + sugar_array = ( + self.space.cells.sort(["dim_0", "dim_1"]) + .with_columns(pl.col("sugar").fill_null(0)) + ["sugar"].to_numpy() + .reshape(self.space.dimensions) + ) + + new_dim0, new_dim1 = sequential_move_numba(dim0, dim1, vision, sugar_array) + coords = pl.DataFrame({"dim_0": new_dim0.tolist(), "dim_1": new_dim1.tolist()}) + self.space.move_agents(agent_ids, coords) + + +# %% [markdown] +""" +## 5. Simultaneous Movement with Conflict Resolution +""" + + +class SugarscapeParallelAgents(SugarscapeAgentsBase): + def move(self) -> None: + if len(self.df) == 0: + return + sugar_map = self._current_sugar_map() + state = self.df.join(self.pos, on="unique_id", how="left") + if state.is_empty(): + return + + origins: dict[int, tuple[int, int]] = {} + choices: dict[int, list[tuple[int, int]]] = {} + choice_idx: dict[int, int] = {} + + for row in state.iter_rows(named=True): + agent_id = int(row["unique_id"]) + origin = (int(row["dim_0"]), int(row["dim_1"])) + vision = int(row["vision"]) + origins[agent_id] = origin + candidate_cells: list[tuple[int, int]] = [] + seen: set[tuple[int, int]] = set() + for cell in self._visible_cells(origin, vision): + if cell not in seen: + seen.add(cell) + candidate_cells.append(cell) + candidate_cells.sort( + key=lambda cell: ( + -sugar_map.get(cell, 0), + self._manhattan(origin, cell), + cell, + ) + ) + if origin not in seen: + candidate_cells.append(origin) + choices[agent_id] = candidate_cells + choice_idx[agent_id] = 0 + + assigned: dict[int, tuple[int, int]] = {} + taken: set[tuple[int, int]] = set() + unresolved: set[int] = set(choices.keys()) + + while unresolved: + cell_to_agents: defaultdict[tuple[int, int], list[int]] = defaultdict(list) + for agent in list(unresolved): + ranked = choices[agent] + idx = choice_idx[agent] + while idx < len(ranked) and ranked[idx] in taken: + idx += 1 + if idx >= len(ranked): + idx = len(ranked) - 1 + choice_idx[agent] = idx + cell_to_agents[ranked[idx]].append(agent) + + progress = False + for cell, agents in cell_to_agents.items(): + if len(agents) == 1: + winner = agents[0] + else: + winner = agents[int(self.random.integers(0, len(agents)))] + assigned[winner] = cell + taken.add(cell) + unresolved.remove(winner) + progress = True + for agent in agents: + if agent != winner: + idx = choice_idx[agent] + 1 + if idx >= len(choices[agent]): + idx = len(choices[agent]) - 1 + choice_idx[agent] = idx + + if not progress: + for agent in list(unresolved): + assigned[agent] = origins[agent] + unresolved.remove(agent) + + move_df = pl.DataFrame( + { + "unique_id": list(assigned.keys()), + "dim_0": [cell[0] for cell in assigned.values()], + "dim_1": [cell[1] for cell in assigned.values()], + } + ) + self.space.move_agents( + move_df["unique_id"].to_list(), move_df.select(["dim_0", "dim_1"]) + ) +@dataclass(slots=True) +class InitialConditions: + sugar_grid: np.ndarray + initial_sugar: np.ndarray + metabolism: np.ndarray + vision: np.ndarray + positions: pl.DataFrame + + +def build_initial_conditions( + width: int, + height: int, + n_agents: int, + *, + seed: int = 7, + peak_height: int = 4, +) -> InitialConditions: + rng = np.random.default_rng(seed) + sugar_grid = SugarscapeTutorialModel._generate_sugar_grid( + rng, width, height, peak_height + ) + initial_sugar = rng.integers(6, 25, size=n_agents, dtype=np.int64) + metabolism = rng.integers(2, 5, size=n_agents, dtype=np.int64) + vision = rng.integers(1, 6, size=n_agents, dtype=np.int64) + positions = SugarscapeTutorialModel._generate_initial_positions( + rng, n_agents, width, height + ) + return InitialConditions( + sugar_grid=sugar_grid, + initial_sugar=initial_sugar, + metabolism=metabolism, + vision=vision, + positions=positions, + ) + + +def run_variant( + agent_cls: type[SugarscapeAgentsBase], + conditions: InitialConditions, + *, + steps: int, + seed: int, +) -> tuple[SugarscapeTutorialModel, float]: + model = SugarscapeTutorialModel( + agent_type=agent_cls, + n_agents=len(conditions.initial_sugar), + sugar_grid=conditions.sugar_grid.copy(), + initial_sugar=conditions.initial_sugar.copy(), + metabolism=conditions.metabolism.copy(), + vision=conditions.vision.copy(), + positions=conditions.positions.clone(), + seed=seed, + ) + start = perf_counter() + model.run(steps) + return model, perf_counter() - start + + +# %% [markdown] +""" +## 6. Shared Model Infrastructure + +`SugarscapeTutorialModel` wires the grid, agent set, regrowth logic, and data +collection. Each variant simply plugs in a different agent class. +""" + + +def gini(values: np.ndarray) -> float: + if values.size == 0: + return float("nan") + sorted_vals = np.sort(values.astype(np.float64)) + n = sorted_vals.size + if n == 0: + return float("nan") + cumulative = np.cumsum(sorted_vals) + total = cumulative[-1] + if total == 0: + return 0.0 + index = np.arange(1, n + 1, dtype=np.float64) + return float((2.0 * np.dot(index, sorted_vals) / (n * total)) - (n + 1) / n) + + +def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: + if x.size < 2 or y.size < 2: + return float("nan") + if np.allclose(x, x[0]) or np.allclose(y, y[0]): + return float("nan") + return float(np.corrcoef(x, y)[0, 1]) + + +def _column_with_prefix(df: pl.DataFrame, prefix: str) -> str: + for col in df.columns: + if col.startswith(prefix): + return col + raise KeyError(f"No column starts with prefix '{prefix}'") + + +def final_agent_snapshot(model: Model) -> pl.DataFrame: + agent_frame = model.datacollector.data["agent"] + if agent_frame.is_empty(): + return agent_frame + last_step = agent_frame["step"].max() + return agent_frame.filter(pl.col("step") == last_step) + + +def summarise_inequality(model: Model) -> dict[str, float]: + snapshot = final_agent_snapshot(model) + if snapshot.is_empty(): + return { + "gini": float("nan"), + "corr_sugar_metabolism": float("nan"), + "corr_sugar_vision": float("nan"), + "agents_alive": 0, + } + + sugar_col = _column_with_prefix(snapshot, "traits_sugar_") + metabolism_col = _column_with_prefix(snapshot, "traits_metabolism_") + vision_col = _column_with_prefix(snapshot, "traits_vision_") + + sugar = snapshot[sugar_col].to_numpy() + metabolism = snapshot[metabolism_col].to_numpy() + vision = snapshot[vision_col].to_numpy() + + return { + "gini": gini(sugar), + "corr_sugar_metabolism": _safe_corr(sugar, metabolism), + "corr_sugar_vision": _safe_corr(sugar, vision), + "agents_alive": float(sugar.size), + } + + +# %% [markdown] +""" +## 7. Run the Sequential Model (Python loop) + +With the scaffolding in place we can simulate the sequential version and inspect +its aggregate behaviour. +""" + +# %% +conditions = build_initial_conditions( + width=GRID_WIDTH, height=GRID_HEIGHT, n_agents=NUM_AGENTS, seed=11 +) + +sequential_model, sequential_time = run_variant( + SugarscapeSequentialAgents, conditions, steps=MODEL_STEPS, seed=11 +) + +seq_model_frame = sequential_model.datacollector.data["model"] +print("Sequential aggregate trajectory (last 5 steps):") +print( + seq_model_frame.select(["step", "mean_sugar", "total_sugar", "living_agents"]).tail(5) +) +print(f"Sequential runtime: {sequential_time:.3f} s") + +# %% [markdown] +""" +## 8. Run the Numba-Accelerated Model + +We reuse the same initial conditions so the only difference is the compiled +movement helper. The trajectory matches the pure Python loop (up to floating- +point noise) while running much faster on larger grids. +""" + +# %% +numba_model, numba_time = run_variant( + SugarscapeNumbaAgents, conditions, steps=MODEL_STEPS, seed=11 +) + +numba_model_frame = numba_model.datacollector.data["model"] +print("Numba sequential aggregate trajectory (last 5 steps):") +print( + numba_model_frame.select(["step", "mean_sugar", "total_sugar", "living_agents"]).tail(5) +) +print(f"Numba sequential runtime: {numba_time:.3f} s") + +# %% [markdown] +""" +## 9. Run the Simultaneous Model + +Next we reuse the **same** initial conditions so that both variants start from a +common state. The only change is the movement policy. +""" + +# %% +parallel_model, parallel_time = run_variant( + SugarscapeParallelAgents, conditions, steps=MODEL_STEPS, seed=11 +) + +par_model_frame = parallel_model.datacollector.data["model"] +print("Parallel aggregate trajectory (last 5 steps):") +print(par_model_frame.select(["step", "mean_sugar", "total_sugar", "living_agents"]).tail(5)) +print(f"Parallel runtime: {parallel_time:.3f} s") + +# %% [markdown] +""" +## 10. Runtime Comparison + +The table below summarises the elapsed time for 60 steps on the 50×50 grid with +400 ants. Parallel scheduling on top of Polars lands in the same performance +band as the Numba-accelerated loop, while both are far faster than the pure +Python baseline. +""" + +# %% +runtime_table = pl.DataFrame( + { + "update_rule": [ + "Sequential (Python loop)", + "Sequential (Numba)", + "Parallel (Polars)", + ], + "runtime_seconds": [sequential_time, numba_time, parallel_time], + } +).with_columns(pl.col("runtime_seconds").round(4)) + +print(runtime_table) + +# %% [markdown] +""" +Polars gives us that performance without any bespoke compiled kernels—the move +logic reads like ordinary DataFrame code. The Numba version is a touch faster, +but only after writing and maintaining `_numba_find_best_cell` and friends. In +practice we get near-identical runtimes, so you can pick the implementation that +is simplest for your team. +""" + +# %% [markdown] +""" +## 11. Comparing the Update Rules + +Even though the micro rules differ, the aggregate trajectories keep the same +overall shape: sugar holdings trend upward while the population tapers off. By +joining the model-level traces we can quantify how conflict resolution +randomness introduces modest deviations (for example, the simultaneous variant +often retires a few more agents when several conflicts pile up in the same +neighbourhood). Crucially, the steady-state inequality metrics line up: the Gini +coefficients differ by roughly 0.0015 and the wealth–trait correlations are +indistinguishable, which validates the relaxed, fully-parallel update scheme. +""" + +# %% +comparison = numba_model_frame.select(["step", "mean_sugar", "total_sugar", "living_agents"]).join( + par_model_frame.select(["step", "mean_sugar", "total_sugar", "living_agents"]), + on="step", + how="inner", + suffix="_parallel", +) +comparison = comparison.with_columns( + (pl.col("mean_sugar") - pl.col("mean_sugar_parallel")).abs().alias("mean_diff"), + (pl.col("total_sugar") - pl.col("total_sugar_parallel")).abs().alias("total_diff"), + (pl.col("living_agents") - pl.col("living_agents_parallel")).abs().alias("count_diff"), +) +print("Step-level absolute differences (first 10 steps):") +print(comparison.select(["step", "mean_diff", "total_diff", "count_diff"]).head(10)) + +metrics_table = pl.DataFrame( + [ + { + "update_rule": "Sequential (Numba)", + **summarise_inequality(numba_model), + }, + { + "update_rule": "Parallel (random tie-break)", + **summarise_inequality(parallel_model), + }, + ] +) + +print("\nSteady-state inequality metrics:") +print( + metrics_table.select( + [ + "update_rule", + pl.col("gini").round(4), + pl.col("corr_sugar_metabolism").round(4), + pl.col("corr_sugar_vision").round(4), + pl.col("agents_alive"), + ] + ) +) + +numba_gini = metrics_table.filter(pl.col("update_rule") == "Sequential (Numba)")["gini"][0] +par_gini = metrics_table.filter(pl.col("update_rule") == "Parallel (random tie-break)")["gini"][0] +print(f"Absolute Gini gap (numba vs parallel): {abs(numba_gini - par_gini):.4f}") + +# %% [markdown] +""" +## 12. Where to Go Next? + +* **Polars + LazyFrames roadmap** – future mesa-frames releases will expose + LazyFrame-powered schedulers (with GPU offloading hooks), so the same Polars + code you wrote here will scale even further without touching Numba. +* **Production reference** – the `examples/sugarscape_ig/ss_polars` package + shows how to take this pattern further with additional vectorisation tricks. +* **Alternative conflict rules** – it is straightforward to swap in other + tie-breakers, such as letting losing agents search for the next-best empty + cell rather than staying put. +* **Macro validation** – wrap the metric collection in a loop over seeds to + quantify how small the Gini gap remains across independent replications. +* **Statistical physics meets ABM** – for a modern take on the macro behaviour + of Sugarscape-like economies, see Axtell (2000) or subsequent statistical + physics treatments of wealth exchange models. + +Because this script doubles as the notebook source, any edits you make here can +be synchronised with a `.ipynb` representation via Jupytext. +""" From d05e00ef592d5f4abc38d3fda5db6ff6fde65969 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 15:19:56 +0200 Subject: [PATCH 157/329] refactor: streamline Sugarscape model initialization and enhance agent frame generation --- .../general/user-guide/3_advanced_tutorial.py | 271 ++++++------------ 1 file changed, 85 insertions(+), 186 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 0f31f317..4748bc71 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -68,9 +68,7 @@ from __future__ import annotations from collections import defaultdict -from dataclasses import dataclass from time import perf_counter -from typing import Iterable import numpy as np import polars as pl @@ -81,21 +79,27 @@ # %% [markdown] """## 2. Model definition -In this section we define the model class that wires together the grid and the agents. -Note that we define agent_type as flexible so we can plug in different movement policies later. -Also sugar_grid, initial_sugar, metabolism, vision, and positions are optional parameters so we can reuse the same initial conditions across variants. - -The space is a von Neumann grid (which means agents can only move up, down, left, or right) with capacity 1, meaning each cell can host at most one agent. -The sugar field is stored as part of the cell data frame, with columns for current sugar and maximum sugar (for regrowth). The model also sets up a data collector to track aggregate statistics and agent traits over time. - - +In this section we define some helpers and the model class that wires +together the grid and the agents. The `agent_type` parameter stays flexible so +we can plug in different movement policies later, but the model now owns the +logic that generates the sugar field and the initial agent frame. Because both +helpers use `self.random`, instantiating each variant with the same seed keeps +the initial conditions identical across the sequential, Numba, and parallel +implementations. + +The space is a von Neumann grid (which means agents can only move up, down, left, +or right) with capacity 1, meaning each cell can host at most one agent. The sugar +field is stored as part of the cell data frame, with columns for current sugar +and maximum sugar (for regrowth). The model also sets up a data collector to +track aggregate statistics and agent traits over time. + +The `step` method advances the sugar field, triggers the agent set's step """ # %% - -class SugarscapeTutorialModel(Model): +class Sugarscape(Model): """Minimal Sugarscape model used throughout the tutorial.""" def __init__( @@ -103,128 +107,81 @@ def __init__( agent_type: type["SugarscapeAgentsBase"], n_agents: int, *, - sugar_grid: np.ndarray | None = None, - initial_sugar: np.ndarray | None = None, - metabolism: np.ndarray | None = None, - vision: np.ndarray | None = None, - positions: pl.DataFrame | None = None, - seed: int | None = None, - width: int | None = None, - height: int | None = None, + width: int, + height: int, max_sugar: int = 4, + seed: int | None = None, ) -> None: + if n_agents > width * height: + raise ValueError( + "Cannot place more agents than grid cells when capacity is 1." + ) super().__init__(seed) - rng = self.random - - - if sugar_grid is None: - if width is None or height is None: - raise ValueError( - "When `sugar_grid` is omitted you must provide `width` and `height`." - ) - sugar_grid = self._generate_sugar_grid(rng, width, height, max_sugar) - else: - width, height = sugar_grid.shape + # 1. Let's create the sugar grid and set up the space + sugar_grid_df = self._generate_sugar_grid(width, height, max_sugar) self.space = Grid( self, [width, height], neighborhood_type="von_neumann", capacity=1 ) - dim_0 = pl.Series("dim_0", pl.arange(width, eager=True)).to_frame() - dim_1 = pl.Series("dim_1", pl.arange(height, eager=True)).to_frame() - sugar_df = dim_0.join(dim_1, how="cross").with_columns( - sugar=sugar_grid.flatten(), max_sugar=sugar_grid.flatten() - ) - self.space.set_cells(sugar_df) - self._max_sugar = sugar_df.select(["dim_0", "dim_1", "max_sugar"]) - - if initial_sugar is None: - initial_sugar = rng.integers(6, 25, size=n_agents, dtype=np.int64) - else: - n_agents = len(initial_sugar) - if metabolism is None: - metabolism = rng.integers(2, 5, size=n_agents, dtype=np.int64) - if vision is None: - vision = rng.integers(1, 6, size=n_agents, dtype=np.int64) - - main_set = agent_type( - self, - n_agents, - initial_sugar=initial_sugar, - metabolism=metabolism, - vision=vision, - ) - self.sets += main_set - self.population = main_set + self.space.set_cells(sugar_grid_df) + self._max_sugar = sugar_grid_df.select(["dim_0", "dim_1", "max_sugar"]) + + # 2. Now we create the agents and place them on the grid - if positions is None: - positions = self._generate_initial_positions(rng, n_agents, width, height) - self.space.place_agents(self.sets, positions.select(["dim_0", "dim_1"])) + agent_frame = self._generate_agent_frame(n_agents) + main_set = agent_type(self, agent_frame) + self.sets += main_set + self.space.place_to_empty(self.sets) + # 3. Finally we set up the data collector self.datacollector = DataCollector( model=self, model_reporters={ "mean_sugar": lambda m: 0.0 - if len(m.population) == 0 - else float(m.population.df["sugar"].mean()), - "total_sugar": lambda m: float(m.population.df["sugar"].sum()) - if len(m.population) + if len(m.sets[0]) == 0 + else float(m.sets[0].df["sugar"].mean()), + "total_sugar": lambda m: float(m.sets[0].df["sugar"].sum()) + if len(m.sets[0]) else 0.0, - "living_agents": lambda m: len(m.population), + "living_agents": lambda m: len(m.sets[0]), }, agent_reporters={"traits": ["sugar", "metabolism", "vision"]}, ) self.datacollector.collect() - @staticmethod def _generate_sugar_grid( - rng: np.random.Generator, width: int, height: int, max_sugar: int - ) -> np.ndarray: - """Generate a random sugar grid with values between 0 and max_sugar (inclusive). - - Parameters - ---------- - rng : np.random.Generator - Random number generator for reproducibility. - width : int - Width of the grid. - height : int - Height of the grid. - max_sugar : int - Maximum sugar level for any cell. - - Returns - ------- - np.ndarray - A 2D array representing the sugar levels on the grid. - """ - return rng.integers(0, max_sugar + 1, size=(width, height), dtype=np.int64) - - @staticmethod - def _generate_initial_positions( - rng: np.random.Generator, n_agents: int, width: int, height: int + self, width: int, height: int, max_sugar: int ) -> pl.DataFrame: - total_cells = width * height - if n_agents > total_cells: - raise ValueError( - "Cannot place more agents than grid cells when capacity is 1." - ) - indices = rng.choice(total_cells, size=n_agents, replace=False) + """Generate a random sugar grid using the model RNG.""" + sugar_vals = self.random.integers( + 0, max_sugar + 1, size=(width, height), dtype=np.int64 + ) + dim_0 = pl.Series("dim_0", pl.arange(width, eager=True)).to_frame() + dim_1 = pl.Series("dim_1", pl.arange(height, eager=True)).to_frame() + return dim_0.join(dim_1, how="cross").with_columns( + sugar=sugar_vals.flatten(), max_sugar=sugar_vals.flatten() + ) + + def _generate_agent_frame(self, n_agents: int) -> pl.DataFrame: + """Create the initial agent frame populated with traits.""" + rng = self.random return pl.DataFrame( { - "dim_0": (indices // height).astype(np.int64), - "dim_1": (indices % height).astype(np.int64), + "sugar": rng.integers(6, 25, size=n_agents, dtype=np.int64), + "metabolism": rng.integers(2, 5, size=n_agents, dtype=np.int64), + "vision": rng.integers(1, 6, size=n_agents, dtype=np.int64), } ) def step(self) -> None: - if len(self.population) == 0: + if len(self.sets[0]) == 0: self.running = False return self._advance_sugar_field() - self.population.step() + self.sets[0].step() self.datacollector.collect() - if len(self.population) == 0: + if len(self.sets[0]) == 0: self.running = False def run(self, steps: int) -> None: @@ -245,14 +202,12 @@ def _advance_sugar_field(self) -> None: - - - # %% GRID_WIDTH = 50 GRID_HEIGHT = 50 NUM_AGENTS = 400 MODEL_STEPS = 60 +MAX_SUGAR = 4 @njit(cache=True) def _numba_should_replace( @@ -383,32 +338,15 @@ def sequential_move_numba( class SugarscapeAgentsBase(AgentSet): - def __init__( - self, - model: Model, - n_agents: int, - *, - initial_sugar: np.ndarray | None = None, - metabolism: np.ndarray | None = None, - vision: np.ndarray | None = None, - ) -> None: + def __init__(self, model: Model, agent_frame: pl.DataFrame) -> None: super().__init__(model) - rng = model.random - if initial_sugar is None: - initial_sugar = rng.integers(6, 25, size=n_agents, dtype=np.int64) - if metabolism is None: - metabolism = rng.integers(2, 5, size=n_agents, dtype=np.int64) - if vision is None: - vision = rng.integers(1, 6, size=n_agents, dtype=np.int64) - self.add( - pl.DataFrame( - { - "sugar": initial_sugar, - "metabolism": metabolism, - "vision": vision, - } + required = {"sugar", "metabolism", "vision"} + missing = required.difference(agent_frame.columns) + if missing: + raise ValueError( + f"Initial agent frame must include columns {sorted(required)}; missing {sorted(missing)}." ) - ) + self.add(agent_frame.clone()) def step(self) -> None: self.shuffle(inplace=True) @@ -641,57 +579,18 @@ def move(self) -> None: self.space.move_agents( move_df["unique_id"].to_list(), move_df.select(["dim_0", "dim_1"]) ) -@dataclass(slots=True) -class InitialConditions: - sugar_grid: np.ndarray - initial_sugar: np.ndarray - metabolism: np.ndarray - vision: np.ndarray - positions: pl.DataFrame - - -def build_initial_conditions( - width: int, - height: int, - n_agents: int, - *, - seed: int = 7, - peak_height: int = 4, -) -> InitialConditions: - rng = np.random.default_rng(seed) - sugar_grid = SugarscapeTutorialModel._generate_sugar_grid( - rng, width, height, peak_height - ) - initial_sugar = rng.integers(6, 25, size=n_agents, dtype=np.int64) - metabolism = rng.integers(2, 5, size=n_agents, dtype=np.int64) - vision = rng.integers(1, 6, size=n_agents, dtype=np.int64) - positions = SugarscapeTutorialModel._generate_initial_positions( - rng, n_agents, width, height - ) - return InitialConditions( - sugar_grid=sugar_grid, - initial_sugar=initial_sugar, - metabolism=metabolism, - vision=vision, - positions=positions, - ) - - def run_variant( agent_cls: type[SugarscapeAgentsBase], - conditions: InitialConditions, *, steps: int, seed: int, -) -> tuple[SugarscapeTutorialModel, float]: - model = SugarscapeTutorialModel( +) -> tuple[Sugarscape, float]: + model = Sugarscape( agent_type=agent_cls, - n_agents=len(conditions.initial_sugar), - sugar_grid=conditions.sugar_grid.copy(), - initial_sugar=conditions.initial_sugar.copy(), - metabolism=conditions.metabolism.copy(), - vision=conditions.vision.copy(), - positions=conditions.positions.clone(), + n_agents=NUM_AGENTS, + width=GRID_WIDTH, + height=GRID_HEIGHT, + max_sugar=MAX_SUGAR, seed=seed, ) start = perf_counter() @@ -777,16 +676,16 @@ def summarise_inequality(model: Model) -> dict[str, float]: ## 7. Run the Sequential Model (Python loop) With the scaffolding in place we can simulate the sequential version and inspect -its aggregate behaviour. +its aggregate behaviour. Because all random draws flow through the model's RNG, +constructing each variant with the same seed reproduces identical initial +conditions across the different movement rules. """ # %% -conditions = build_initial_conditions( - width=GRID_WIDTH, height=GRID_HEIGHT, n_agents=NUM_AGENTS, seed=11 -) +sequential_seed = 11 sequential_model, sequential_time = run_variant( - SugarscapeSequentialAgents, conditions, steps=MODEL_STEPS, seed=11 + SugarscapeSequentialAgents, steps=MODEL_STEPS, seed=sequential_seed ) seq_model_frame = sequential_model.datacollector.data["model"] @@ -800,14 +699,14 @@ def summarise_inequality(model: Model) -> dict[str, float]: """ ## 8. Run the Numba-Accelerated Model -We reuse the same initial conditions so the only difference is the compiled -movement helper. The trajectory matches the pure Python loop (up to floating- -point noise) while running much faster on larger grids. +We reuse the same seed so the only difference is the compiled movement helper. +The trajectory matches the pure Python loop (up to floating-point noise) while +running much faster on larger grids. """ # %% numba_model, numba_time = run_variant( - SugarscapeNumbaAgents, conditions, steps=MODEL_STEPS, seed=11 + SugarscapeNumbaAgents, steps=MODEL_STEPS, seed=sequential_seed ) numba_model_frame = numba_model.datacollector.data["model"] @@ -821,13 +720,13 @@ def summarise_inequality(model: Model) -> dict[str, float]: """ ## 9. Run the Simultaneous Model -Next we reuse the **same** initial conditions so that both variants start from a -common state. The only change is the movement policy. +Next we instantiate the parallel variant with the same seed so every run starts +from the common state generated by the helper methods. """ # %% parallel_model, parallel_time = run_variant( - SugarscapeParallelAgents, conditions, steps=MODEL_STEPS, seed=11 + SugarscapeParallelAgents, steps=MODEL_STEPS, seed=sequential_seed ) par_model_frame = parallel_model.datacollector.data["model"] From 27ea2ecee388b36b9b76887597a8b97e5f24f272 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 15:25:43 +0200 Subject: [PATCH 158/329] docs: enhance Sugarscape model class docstrings for clarity and completeness --- .../general/user-guide/3_advanced_tutorial.py | 96 ++++++++++++++++++- 1 file changed, 93 insertions(+), 3 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 4748bc71..dac90aea 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -100,7 +100,42 @@ # %% class Sugarscape(Model): - """Minimal Sugarscape model used throughout the tutorial.""" + """Minimal Sugarscape model used throughout the tutorial. + + This class wires together a grid that stores ``sugar`` per cell, an + agent set implementation (passed in as ``agent_type``), and a + data collector that records model- and agent-level statistics. + + The model's responsibilities are to: + - create the sugar landscape (cells with current and maximum sugar) + - create and place agents on the grid + - advance the sugar regrowth rule each step + - run the model for a fixed number of steps and collect data + + Parameters + ---------- + agent_type : type + The :class:`AgentSet` subclass implementing the movement rules + (sequential, numba-accelerated, or parallel). + n_agents : int + Number of agents to create and place on the grid. + width : int + Grid width (number of columns). + height : int + Grid height (number of rows). + max_sugar : int, optional + Upper bound for the randomly initialised sugar values on the grid, + by default 4. + seed : int or None, optional + RNG seed to make runs reproducible across variants, by default None. + + Notes + ----- + The grid uses a von Neumann neighbourhood and capacity 1 (at most one + agent per cell). Both the sugar landscape and initial agent traits are + drawn from ``self.random`` so different movement variants can be + instantiated with identical initial conditions by passing the same seed. + """ def __init__( self, @@ -153,7 +188,23 @@ def __init__( def _generate_sugar_grid( self, width: int, height: int, max_sugar: int ) -> pl.DataFrame: - """Generate a random sugar grid using the model RNG.""" + """Generate a random sugar grid. + + Parameters + ---------- + width : int + Grid width (number of columns). + height : int + Grid height (number of rows). + max_sugar : int + Maximum sugar value (inclusive) for each cell. + + Returns + ------- + pl.DataFrame + DataFrame with columns ``dim_0``, ``dim_1``, ``sugar`` (current + amount) and ``max_sugar`` (regrowth target). + """ sugar_vals = self.random.integers( 0, max_sugar + 1, size=(width, height), dtype=np.int64 ) @@ -164,7 +215,19 @@ def _generate_sugar_grid( ) def _generate_agent_frame(self, n_agents: int) -> pl.DataFrame: - """Create the initial agent frame populated with traits.""" + """Create the initial agent frame populated with agent traits. + + Parameters + ---------- + n_agents : int + Number of agents to create. + + Returns + ------- + pl.DataFrame + DataFrame with columns ``sugar``, ``metabolism`` and ``vision`` + (integer values) for each agent. + """ rng = self.random return pl.DataFrame( { @@ -175,6 +238,15 @@ def _generate_agent_frame(self, n_agents: int) -> pl.DataFrame: ) def step(self) -> None: + """Advance the model by one step. + + Notes + ----- + The per-step ordering is important: regrowth happens first (so empty + cells are refilled), then agents move and eat, and finally metrics are + collected. If the agent set becomes empty at any point the model is + marked as not running. + """ if len(self.sets[0]) == 0: self.running = False return @@ -185,18 +257,36 @@ def step(self) -> None: self.running = False def run(self, steps: int) -> None: + """Run the model for a fixed number of steps. + + Parameters + ---------- + steps : int + Maximum number of steps to run. The model may terminate earlier if + ``self.running`` is set to ``False`` (for example, when all agents + have died). + """ for _ in range(steps): if not self.running: break self.step() def _advance_sugar_field(self) -> None: + """Apply the instant-growback sugar regrowth rule. + + Empty cells (no agent present) are refilled to their ``max_sugar`` + value. Cells that are occupied are set to zero because agents harvest + the sugar when they eat. The method uses vectorised DataFrame joins + and writes to keep the operation efficient. + """ empty_cells = self.space.empty_cells if not empty_cells.is_empty(): + # Look up the maximum sugar for each empty cell and restore it. refresh = empty_cells.join(self._max_sugar, on=["dim_0", "dim_1"], how="left") self.space.set_cells(empty_cells, {"sugar": refresh["max_sugar"]}) full_cells = self.space.full_cells if not full_cells.is_empty(): + # Occupied cells have just been harvested; set their sugar to 0. zeros = pl.Series(np.zeros(len(full_cells), dtype=np.int64)) self.space.set_cells(full_cells, {"sugar": zeros}) From 4552a0859d479d527ee545337de4737c4a3098d2 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 15:35:33 +0200 Subject: [PATCH 159/329] docs: add agent definition section and base agent class implementation to advanced tutorial --- .../general/user-guide/3_advanced_tutorial.py | 210 ++++++++++-------- 1 file changed, 117 insertions(+), 93 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index dac90aea..718570c5 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -290,6 +290,123 @@ def _advance_sugar_field(self) -> None: zeros = pl.Series(np.zeros(len(full_cells), dtype=np.int64)) self.space.set_cells(full_cells, {"sugar": zeros}) +# %% [markdown] + +""" +## 3. Agent definition + +### Base agent class + +Now let's define the agent class (the ant class). We start with a base class which implements the common logic for eating and starvation, while leaving the `move` method abstract. +The base class also provides helper methods for sensing visible cells and choosing the best cell based on sugar, distance, and coordinates. +This will allow us to define different movement policies (sequential, Numba-accelerated, and parallel) as subclasses that only need to implement the `move` method. + +""" + +# %% + +class SugarscapeAgentsBase(AgentSet): + def __init__(self, model: Model, agent_frame: pl.DataFrame) -> None: + super().__init__(model) + required = {"sugar", "metabolism", "vision"} + missing = required.difference(agent_frame.columns) + if missing: + raise ValueError( + f"Initial agent frame must include columns {sorted(required)}; missing {sorted(missing)}." + ) + self.add(agent_frame.clone()) + + def step(self) -> None: + self.shuffle(inplace=True) + self.move() + self.eat() + self._remove_starved() + + def move(self) -> None: # pragma: no cover + raise NotImplementedError + + def eat(self) -> None: + occupied_ids = self.index.to_list() + occupied = self.space.cells.filter(pl.col("agent_id").is_in(occupied_ids)) + if occupied.is_empty(): + return + ids = occupied["agent_id"] + self[ids, "sugar"] = ( + self[ids, "sugar"] + occupied["sugar"] - self[ids, "metabolism"] + ) + self.space.set_cells( + occupied.select(["dim_0", "dim_1"]), + {"sugar": pl.Series(np.zeros(len(occupied), dtype=np.int64))}, + ) + + def _remove_starved(self) -> None: + starved = self.df.filter(pl.col("sugar") <= 0) + if not starved.is_empty(): + self.discard(starved) + + def _current_sugar_map(self) -> dict[tuple[int, int], int]: + cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) + return { + (int(x), int(y)): 0 if sugar is None else int(sugar) + for x, y, sugar in cells.iter_rows() + } + + @staticmethod + def _manhattan(a: tuple[int, int], b: tuple[int, int]) -> int: + return abs(a[0] - b[0]) + abs(a[1] - b[1]) + + def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: + x0, y0 = origin + width, height = self.space.dimensions + cells: list[tuple[int, int]] = [origin] + for step in range(1, vision + 1): + if x0 + step < width: + cells.append((x0 + step, y0)) + if x0 - step >= 0: + cells.append((x0 - step, y0)) + if y0 + step < height: + cells.append((x0, y0 + step)) + if y0 - step >= 0: + cells.append((x0, y0 - step)) + return cells + + def _choose_best_cell( + self, + origin: tuple[int, int], + vision: int, + sugar_map: dict[tuple[int, int], int], + blocked: set[tuple[int, int]] | None, + ) -> tuple[int, int]: + best_cell = origin + best_sugar = sugar_map.get(origin, 0) + best_distance = 0 + for candidate in self._visible_cells(origin, vision): + if blocked and candidate != origin and candidate in blocked: + continue + sugar_here = sugar_map.get(candidate, 0) + distance = self._manhattan(origin, candidate) + better = False + if sugar_here > best_sugar: + better = True + elif sugar_here == best_sugar: + if distance < best_distance: + better = True + elif distance == best_distance and candidate < best_cell: + better = True + if better: + best_cell = candidate + best_sugar = sugar_here + best_distance = distance + return best_cell + + + + + + + + + # %% @@ -427,99 +544,6 @@ def sequential_move_numba( """ -class SugarscapeAgentsBase(AgentSet): - def __init__(self, model: Model, agent_frame: pl.DataFrame) -> None: - super().__init__(model) - required = {"sugar", "metabolism", "vision"} - missing = required.difference(agent_frame.columns) - if missing: - raise ValueError( - f"Initial agent frame must include columns {sorted(required)}; missing {sorted(missing)}." - ) - self.add(agent_frame.clone()) - - def step(self) -> None: - self.shuffle(inplace=True) - self.move() - self.eat() - self._remove_starved() - - def move(self) -> None: # pragma: no cover - raise NotImplementedError - - def eat(self) -> None: - occupied_ids = self.index.to_list() - occupied = self.space.cells.filter(pl.col("agent_id").is_in(occupied_ids)) - if occupied.is_empty(): - return - ids = occupied["agent_id"] - self[ids, "sugar"] = ( - self[ids, "sugar"] + occupied["sugar"] - self[ids, "metabolism"] - ) - self.space.set_cells( - occupied.select(["dim_0", "dim_1"]), - {"sugar": pl.Series(np.zeros(len(occupied), dtype=np.int64))}, - ) - - def _remove_starved(self) -> None: - starved = self.df.filter(pl.col("sugar") <= 0) - if not starved.is_empty(): - self.discard(starved) - - def _current_sugar_map(self) -> dict[tuple[int, int], int]: - cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) - return { - (int(x), int(y)): 0 if sugar is None else int(sugar) - for x, y, sugar in cells.iter_rows() - } - - @staticmethod - def _manhattan(a: tuple[int, int], b: tuple[int, int]) -> int: - return abs(a[0] - b[0]) + abs(a[1] - b[1]) - - def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: - x0, y0 = origin - width, height = self.space.dimensions - cells: list[tuple[int, int]] = [origin] - for step in range(1, vision + 1): - if x0 + step < width: - cells.append((x0 + step, y0)) - if x0 - step >= 0: - cells.append((x0 - step, y0)) - if y0 + step < height: - cells.append((x0, y0 + step)) - if y0 - step >= 0: - cells.append((x0, y0 - step)) - return cells - - def _choose_best_cell( - self, - origin: tuple[int, int], - vision: int, - sugar_map: dict[tuple[int, int], int], - blocked: set[tuple[int, int]] | None, - ) -> tuple[int, int]: - best_cell = origin - best_sugar = sugar_map.get(origin, 0) - best_distance = 0 - for candidate in self._visible_cells(origin, vision): - if blocked and candidate != origin and candidate in blocked: - continue - sugar_here = sugar_map.get(candidate, 0) - distance = self._manhattan(origin, candidate) - better = False - if sugar_here > best_sugar: - better = True - elif sugar_here == best_sugar: - if distance < best_distance: - better = True - elif distance == best_distance and candidate < best_cell: - better = True - if better: - best_cell = candidate - best_sugar = sugar_here - best_distance = distance - return best_cell # %% [markdown] From 3c55734bc48411f2c63c4724c349f59e1d076d44 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 15:53:07 +0200 Subject: [PATCH 160/329] docs: update import statements for future annotations in advanced tutorial --- docs/general/user-guide/3_advanced_tutorial.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 718570c5..ff68b80b 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -1,3 +1,5 @@ +from __future__ import annotations + # --- # jupyter: # jupytext: @@ -65,7 +67,6 @@ """## 1. Imports""" # %% -from __future__ import annotations from collections import defaultdict from time import perf_counter From 8e978dac119b9848a4d68dfe1b2503448efdd960 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 15:56:54 +0200 Subject: [PATCH 161/329] refactor: optimize sugar consumption logic in SugarscapeAgentsBase class --- .../general/user-guide/3_advanced_tutorial.py | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index ff68b80b..a3a59749 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -327,17 +327,17 @@ def move(self) -> None: # pragma: no cover raise NotImplementedError def eat(self) -> None: - occupied_ids = self.index.to_list() - occupied = self.space.cells.filter(pl.col("agent_id").is_in(occupied_ids)) - if occupied.is_empty(): + occupied_ids = self.index + occupied_cells = self.space.cells.filter(pl.col("agent_id").is_in(occupied_ids)) + if occupied_cells.is_empty(): return - ids = occupied["agent_id"] - self[ids, "sugar"] = ( - self[ids, "sugar"] + occupied["sugar"] - self[ids, "metabolism"] + agent_ids = occupied_cells["agent_id"] + self[agent_ids, "sugar"] = ( + self[agent_ids, "sugar"] + occupied_cells["sugar"] - self[agent_ids, "metabolism"] ) self.space.set_cells( - occupied.select(["dim_0", "dim_1"]), - {"sugar": pl.Series(np.zeros(len(occupied), dtype=np.int64))}, + occupied_cells.select(["dim_0", "dim_1"]), + {"sugar": pl.Series(np.zeros(len(occupied_cells), dtype=np.int64))}, ) def _remove_starved(self) -> None: @@ -402,14 +402,6 @@ def _choose_best_cell( - - - - - - - - # %% GRID_WIDTH = 50 GRID_HEIGHT = 50 From 894c18176ccc50b20eacd83aa59651d7e9586aa4 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 16:32:30 +0200 Subject: [PATCH 162/329] docs: enhance documentation for Sugarscape agent classes and methods --- .../general/user-guide/3_advanced_tutorial.py | 456 +++++++++++++++--- 1 file changed, 386 insertions(+), 70 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index a3a59749..a301822b 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -301,13 +301,40 @@ def _advance_sugar_field(self) -> None: Now let's define the agent class (the ant class). We start with a base class which implements the common logic for eating and starvation, while leaving the `move` method abstract. The base class also provides helper methods for sensing visible cells and choosing the best cell based on sugar, distance, and coordinates. This will allow us to define different movement policies (sequential, Numba-accelerated, and parallel) as subclasses that only need to implement the `move` method. - +We also add """ # %% class SugarscapeAgentsBase(AgentSet): + """Base agent set for the Sugarscape tutorial. + + This class implements the common behaviour shared by all agent + movement variants (sequential, numba-accelerated and parallel). + + Notes + ----- + - Agents are expected to have integer traits: ``sugar``, ``metabolism`` + and ``vision``. These are validated in :meth:`__init__`. + - Subclasses must implement :meth:`move` which changes agent positions + on the grid (via :meth:`mesa_frames.Grid` helpers). + """ def __init__(self, model: Model, agent_frame: pl.DataFrame) -> None: + """Initialise the agent set and validate required trait columns. + + Parameters + ---------- + model : Model + The parent model which provides RNG and space. + agent_frame : pl.DataFrame + A Polars DataFrame with at least the columns ``sugar``, + ``metabolism`` and ``vision`` for each agent. + + Raises + ------ + ValueError + If required trait columns are missing from ``agent_frame``. + """ super().__init__(model) required = {"sugar", "metabolism", "vision"} missing = required.difference(agent_frame.columns) @@ -318,35 +345,83 @@ def __init__(self, model: Model, agent_frame: pl.DataFrame) -> None: self.add(agent_frame.clone()) def step(self) -> None: + """Advance the agent set by one time step. + + The update order is important: agents are first shuffled to randomise + move order (this is important only for sequential variants), then they move, harvest sugar + from their occupied cells, and finally any agents whose sugar falls + to zero or below are removed. + """ + # Randomise ordering for movement decisions when required by the + # implementation (e.g. sequential update uses this shuffle). self.shuffle(inplace=True) + # Movement policy implemented by subclasses. self.move() + # Agents harvest sugar on their occupied cells. self.eat() + # Remove agents that starved after eating. self._remove_starved() def move(self) -> None: # pragma: no cover + """Abstract movement method. + + Subclasses must override this method to update agent positions on the + grid. Implementations should use :meth:`mesa_frames.Grid.move_agents` + or similar helpers provided by the space API. + """ raise NotImplementedError def eat(self) -> None: + """Agents harvest sugar from the cells they currently occupy. + + Behaviour: + - Look up the set of occupied cells (cells that reference an agent + id). + - For each occupied cell, add the cell sugar to the agent's sugar + stock and subtract the agent's metabolism cost. + - After agents harvest, set the sugar on those cells to zero (they + were consumed). + """ + # Map of currently occupied agent ids on the grid. occupied_ids = self.index occupied_cells = self.space.cells.filter(pl.col("agent_id").is_in(occupied_ids)) if occupied_cells.is_empty(): return + # The agent ordering here uses the agent_id values stored in the + # occupied cells frame; indexing the agent set with that vector updates + # the matching agents' sugar values in one vectorised write. agent_ids = occupied_cells["agent_id"] self[agent_ids, "sugar"] = ( self[agent_ids, "sugar"] + occupied_cells["sugar"] - self[agent_ids, "metabolism"] ) + # After harvesting, occupied cells have zero sugar. self.space.set_cells( occupied_cells.select(["dim_0", "dim_1"]), {"sugar": pl.Series(np.zeros(len(occupied_cells), dtype=np.int64))}, ) def _remove_starved(self) -> None: + """Discard agents whose sugar stock has fallen to zero or below. + + This method performs a vectorised filter on the agent frame and + removes any matching rows from the set. + """ starved = self.df.filter(pl.col("sugar") <= 0) if not starved.is_empty(): + # ``discard`` accepts a DataFrame of agents to remove. self.discard(starved) def _current_sugar_map(self) -> dict[tuple[int, int], int]: + """Return a mapping from grid coordinates to the current sugar value. + + Returns + ------- + dict + Keys are ``(x, y)`` tuples and values are the integer sugar amount + on that cell (zero if missing/None). + """ cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) + # Build a plain Python dict for fast lookups in the movement code. return { (int(x), int(y)): 0 if sugar is None else int(sugar) for x, y, sugar in cells.iter_rows() @@ -354,12 +429,44 @@ def _current_sugar_map(self) -> dict[tuple[int, int], int]: @staticmethod def _manhattan(a: tuple[int, int], b: tuple[int, int]) -> int: + """Compute the Manhattan (L1) distance between two grid cells. + + Parameters + ---------- + a, b : tuple[int, int] + Coordinate pairs ``(x, y)``. + + Returns + ------- + int + The Manhattan distance between ``a`` and ``b``. + """ return abs(a[0] - b[0]) + abs(a[1] - b[1]) def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: + """List cells visible from an origin along the four cardinal axes. + + The visibility set includes the origin cell itself and cells at + Manhattan distances 1..vision along the four cardinal directions + (up, down, left, right), clipped to the grid bounds. + + Parameters + ---------- + origin : tuple[int, int] + The agent's current coordinate ``(x, y)``. + vision : int + Maximum Manhattan radius to consider along each axis. + + Returns + ------- + list[tuple[int, int]] + Ordered list of visible cells (origin first, then increasing + step distance along each axis). + """ x0, y0 = origin width, height = self.space.dimensions cells: list[tuple[int, int]] = [origin] + # Look outward one step at a time in the four cardinal directions. for step in range(1, vision + 1): if x0 + step < width: cells.append((x0 + step, y0)) @@ -378,20 +485,52 @@ def _choose_best_cell( sugar_map: dict[tuple[int, int], int], blocked: set[tuple[int, int]] | None, ) -> tuple[int, int]: + """Select the best visible cell according to the movement rules. + + Tie-break rules (in order): + 1. Prefer cells with strictly greater sugar. + 2. If equal sugar, prefer the cell with smaller Manhattan distance + from the origin. + 3. If still tied, prefer the cell with smaller coordinates (lexicographic + ordering of the ``(x, y)`` tuple). + + Parameters + ---------- + origin : tuple[int, int] + Agent's current coordinate. + vision : int + Maximum vision radius along cardinal axes. + sugar_map : dict + Mapping from ``(x, y)`` to sugar amount. + blocked : set or None + Optional set of coordinates that should be considered occupied and + therefore skipped (except the origin which is always allowed). + + Returns + ------- + tuple[int, int] + Chosen target coordinate (may be the origin if no better cell is + available). + """ best_cell = origin best_sugar = sugar_map.get(origin, 0) best_distance = 0 for candidate in self._visible_cells(origin, vision): + # Skip blocked cells (occupied by other agents) unless it's the + # agent's current cell which we always consider. if blocked and candidate != origin and candidate in blocked: continue sugar_here = sugar_map.get(candidate, 0) distance = self._manhattan(origin, candidate) better = False + # Primary criterion: strictly more sugar. if sugar_here > best_sugar: better = True elif sugar_here == best_sugar: + # Secondary: closer distance. if distance < best_distance: better = True + # Tertiary: lexicographic tie-break on coordinates. elif distance == best_distance and candidate < best_cell: better = True if better: @@ -420,11 +559,34 @@ def _numba_should_replace( candidate_x: int, candidate_y: int, ) -> bool: + """Numba helper: decide whether a candidate cell should replace the + current best cell according to the movement tie-break rules. + + This implements the same ordering used in :meth:`_choose_best_cell` but + in a tightly-typed, compiled form suitable for Numba loops. + + Parameters + ---------- + best_sugar, candidate_sugar : int + Sugar at the current best cell and the candidate cell. + best_distance, candidate_distance : int + Manhattan distances from the origin to the best and candidate cells. + best_x, best_y, candidate_x, candidate_y : int + Coordinates used for the final lexicographic tie-break. + + Returns + ------- + bool + True if the candidate should replace the current best cell. + """ + # Primary criterion: prefer strictly greater sugar. if candidate_sugar > best_sugar: return True + # If sugar ties, prefer the closer cell. if candidate_sugar == best_sugar: if candidate_distance < best_distance: return True + # If distance ties as well, compare coordinates lexicographically. if candidate_distance == best_distance: if candidate_x < best_x: return True @@ -447,6 +609,10 @@ def _numba_find_best_cell( best_sugar = sugar_array[x0, y0] best_distance = 0 + # Examine visible cells along the four cardinal directions, increasing + # step by step. The 'occupied' array marks cells that are currently + # unavailable (True = occupied). The origin cell is allowed as the + # default; callers typically clear the origin before searching. for step in range(1, vision + 1): nx = x0 + step if nx < width and not occupied[nx, y0]: @@ -502,22 +668,55 @@ def sequential_move_numba( vision: np.ndarray, sugar_array: np.ndarray, ) -> tuple[np.ndarray, np.ndarray]: + """Numba-accelerated sequential movement helper. + + This function emulates the traditional asynchronous (sequential) update + where agents move one at a time in the current ordering. It accepts + numpy arrays describing agent positions and vision ranges, and a 2D + sugar array for lookup. + + Parameters + ---------- + dim0, dim1 : np.ndarray + 1D integer arrays of length n_agents containing the x and y + coordinates for each agent. + vision : np.ndarray + 1D integer array of vision radii for each agent. + sugar_array : np.ndarray + 2D array shaped (width, height) containing per-cell sugar values. + + Returns + ------- + tuple[np.ndarray, np.ndarray] + Updated arrays of x and y coordinates after sequential movement. + """ n_agents = dim0.shape[0] width, height = sugar_array.shape + # Copy inputs to avoid mutating caller arrays in-place. new_dim0 = dim0.copy() new_dim1 = dim1.copy() + # Occupancy grid: True when a cell is currently occupied by an agent. occupied = np.zeros((width, height), dtype=np.bool_) + # Mark initial occupancy. for i in range(n_agents): occupied[new_dim0[i], new_dim1[i]] = True + # Process agents in order. For each agent we clear its current cell in + # the occupancy grid (so it can consider moving into it), search for the + # best unoccupied visible cell, and mark the chosen destination as + # occupied. This models agents moving one-by-one. for i in range(n_agents): x0 = new_dim0[i] y0 = new_dim1[i] + # Free the agent's current cell so it is considered available during + # the search (agents may choose to stay, in which case we'll re-mark + # it below). occupied[x0, y0] = False best_x, best_y = _numba_find_best_cell( x0, y0, int(vision[i]), sugar_array, occupied ) + # Claim the chosen destination. occupied[best_x, best_y] = True new_dim0[i] = best_x new_dim1[i] = best_y @@ -578,8 +777,7 @@ def move(self) -> None: state = self.df.join(self.pos, on="unique_id", how="left") if state.is_empty(): return - - agent_ids = state["unique_id"].to_list() + agent_ids = state["unique_id"] dim0 = state["dim_0"].to_numpy().astype(np.int64) dim1 = state["dim_1"].to_numpy().astype(np.int64) vision = state["vision"].to_numpy().astype(np.int64) @@ -604,6 +802,10 @@ def move(self) -> None: class SugarscapeParallelAgents(SugarscapeAgentsBase): def move(self) -> None: + # Parallel movement: each agent proposes a ranked list of visible + # cells (including its own). We resolve conflicts in rounds using + # DataFrame operations so winners can be chosen per-cell at random + # and losers are promoted to their next-ranked choice. if len(self.df) == 0: return sugar_map = self._current_sugar_map() @@ -611,81 +813,195 @@ def move(self) -> None: if state.is_empty(): return - origins: dict[int, tuple[int, int]] = {} - choices: dict[int, list[tuple[int, int]]] = {} - choice_idx: dict[int, int] = {} + # Map the positional frame to a center lookup used when joining + # neighbourhoods produced by the space helper. + center_lookup = self.pos.rename( + { + "unique_id": "agent_id", + "dim_0": "dim_0_center", + "dim_1": "dim_1_center", + } + ) - for row in state.iter_rows(named=True): - agent_id = int(row["unique_id"]) - origin = (int(row["dim_0"]), int(row["dim_1"])) - vision = int(row["vision"]) - origins[agent_id] = origin - candidate_cells: list[tuple[int, int]] = [] - seen: set[tuple[int, int]] = set() - for cell in self._visible_cells(origin, vision): - if cell not in seen: - seen.add(cell) - candidate_cells.append(cell) - candidate_cells.sort( - key=lambda cell: ( - -sugar_map.get(cell, 0), - self._manhattan(origin, cell), - cell, + # Build a neighbourhood frame: for each agent and visible cell we + # attach the cell sugar and the agent_id of the occupant (if any). + neighborhood = ( + self.space.get_neighborhood( + radius=self["vision"], agents=self, include_center=True + ) + .join( + self.space.cells.select(["dim_0", "dim_1", "sugar"]), + on=["dim_0", "dim_1"], + how="left", + ) + .join(center_lookup, on=["dim_0_center", "dim_1_center"], how="left") + .with_columns(pl.col("sugar").fill_null(0)) + ) + + # Normalise occupant column name if present. + if "agent_id" in neighborhood.columns: + neighborhood = neighborhood.rename({"agent_id": "occupant_id"}) + + # Create ranked choices per agent: sort by sugar (desc), radius + # (asc), then coordinates. Keep the first unique entry per cell. + choices = ( + neighborhood.select( + [ + "agent_id", + "dim_0", + "dim_1", + "sugar", + "radius", + "dim_0_center", + "dim_1_center", + ] + ) + .with_columns(pl.col("radius").cast(pl.Int64)) + .sort( + ["agent_id", "sugar", "radius", "dim_0", "dim_1"], + descending=[False, True, False, False, False], + ) + .unique( + subset=["agent_id", "dim_0", "dim_1"], + keep="first", + maintain_order=True, + ) + .with_columns(pl.cum_count().over("agent_id").cast(pl.Int64).alias("rank")) + ) + + if choices.is_empty(): + return + + # Origins for fallback (if an agent exhausts candidates it stays put). + origins = center_lookup.select( + [ + "agent_id", + pl.col("dim_0_center").alias("dim_0"), + pl.col("dim_1_center").alias("dim_1"), + ] + ) + + # Track the maximum available rank per agent to clamp promotions. + max_rank = choices.group_by("agent_id").agg(pl.col("rank").max().alias("max_rank")) + + # Prepare unresolved agents and working tables. + agent_ids = choices["agent_id"].unique(maintain_order=True) + unresolved = pl.DataFrame( + { + "agent_id": agent_ids, + "current_rank": pl.Series(np.zeros(agent_ids.len(), dtype=np.int64)), + } + ) + + assigned = pl.DataFrame( + { + "agent_id": pl.Series(name="agent_id", values=[], dtype=agent_ids.dtype), + "dim_0": pl.Series(name="dim_0", values=[], dtype=pl.Int64), + "dim_1": pl.Series(name="dim_1", values=[], dtype=pl.Int64), + } + ) + + taken = pl.DataFrame( + { + "dim_0": pl.Series(name="dim_0", values=[], dtype=pl.Int64), + "dim_1": pl.Series(name="dim_1", values=[], dtype=pl.Int64), + } + ) + + # Resolve in rounds: each unresolved agent proposes its current-ranked + # candidate; winners per-cell are selected at random and losers are + # promoted to their next choice. + while unresolved.height > 0: + candidate_pool = choices.join(unresolved, on="agent_id") + candidate_pool = candidate_pool.filter(pl.col("rank") >= pl.col("current_rank")) + if not taken.is_empty(): + candidate_pool = candidate_pool.join(taken, on=["dim_0", "dim_1"], how="anti") + + if candidate_pool.is_empty(): + # No available candidates — everyone falls back to origin. + fallback = unresolved.join(origins, on="agent_id", how="left") + assigned = pl.concat( + [assigned, fallback.select(["agent_id", "dim_0", "dim_1"])], + how="vertical", ) + break + + best_candidates = ( + candidate_pool.sort(["agent_id", "rank"]) .group_by("agent_id", maintain_order=True).first() + ) + + # Agents that had no candidate this round fall back to origin. + missing = unresolved.join(best_candidates.select("agent_id"), on="agent_id", how="anti") + if not missing.is_empty(): + fallback = missing.join(origins, on="agent_id", how="left") + assigned = pl.concat( + [assigned, fallback.select(["agent_id", "dim_0", "dim_1"])], + how="vertical", + ) + taken = pl.concat([taken, fallback.select(["dim_0", "dim_1"])], how="vertical") + unresolved = unresolved.join(missing.select("agent_id"), on="agent_id", how="anti") + best_candidates = best_candidates.join(missing.select("agent_id"), on="agent_id", how="anti") + if unresolved.is_empty() or best_candidates.is_empty(): + continue + + # Add a small random lottery to break ties deterministically for + # each candidate set. + lottery = pl.Series("lottery", self.random.random(best_candidates.height)) + best_candidates = best_candidates.with_columns(lottery) + + winners = ( + best_candidates.sort(["dim_0", "dim_1", "lottery"]) .group_by(["dim_0", "dim_1"], maintain_order=True).first() + ) + + assigned = pl.concat( + [assigned, winners.select(["agent_id", "dim_0", "dim_1"])], + how="vertical", ) - if origin not in seen: - candidate_cells.append(origin) - choices[agent_id] = candidate_cells - choice_idx[agent_id] = 0 - - assigned: dict[int, tuple[int, int]] = {} - taken: set[tuple[int, int]] = set() - unresolved: set[int] = set(choices.keys()) - - while unresolved: - cell_to_agents: defaultdict[tuple[int, int], list[int]] = defaultdict(list) - for agent in list(unresolved): - ranked = choices[agent] - idx = choice_idx[agent] - while idx < len(ranked) and ranked[idx] in taken: - idx += 1 - if idx >= len(ranked): - idx = len(ranked) - 1 - choice_idx[agent] = idx - cell_to_agents[ranked[idx]].append(agent) - - progress = False - for cell, agents in cell_to_agents.items(): - if len(agents) == 1: - winner = agents[0] - else: - winner = agents[int(self.random.integers(0, len(agents)))] - assigned[winner] = cell - taken.add(cell) - unresolved.remove(winner) - progress = True - for agent in agents: - if agent != winner: - idx = choice_idx[agent] + 1 - if idx >= len(choices[agent]): - idx = len(choices[agent]) - 1 - choice_idx[agent] = idx - - if not progress: - for agent in list(unresolved): - assigned[agent] = origins[agent] - unresolved.remove(agent) + taken = pl.concat([taken, winners.select(["dim_0", "dim_1"])], how="vertical") + + winner_ids = winners.select("agent_id") + unresolved = unresolved.join(winner_ids, on="agent_id", how="anti") + if unresolved.is_empty(): + break + + losers = best_candidates.join(winner_ids, on="agent_id", how="anti") + if losers.is_empty(): + continue + + loser_updates = ( + losers.select( + "agent_id", + (pl.col("rank") + 1).cast(pl.Int64).alias("next_rank"), + ) + .join(max_rank, on="agent_id", how="left") + .with_columns( + pl.min_horizontal(pl.col("next_rank"), pl.col("max_rank")).alias("next_rank") + ) + .select(["agent_id", "next_rank"]) + ) + + # Promote losers' current_rank (if any) and continue. + unresolved = unresolved.join(loser_updates, on="agent_id", how="left").with_columns( + pl.when(pl.col("next_rank").is_not_null()) + .then(pl.col("next_rank")) + .otherwise(pl.col("current_rank")) + .alias("current_rank") + ).drop("next_rank") + + if assigned.is_empty(): + return move_df = pl.DataFrame( { - "unique_id": list(assigned.keys()), - "dim_0": [cell[0] for cell in assigned.values()], - "dim_1": [cell[1] for cell in assigned.values()], + "unique_id": assigned["agent_id"], + "dim_0": assigned["dim_0"], + "dim_1": assigned["dim_1"], } ) - self.space.move_agents( - move_df["unique_id"].to_list(), move_df.select(["dim_0", "dim_1"]) - ) + # `move_agents` accepts IdsLike and SpaceCoordinates (Polars Series/DataFrame), + # so pass Series/DataFrame directly rather than converting to Python lists. + self.space.move_agents(move_df["unique_id"], move_df.select(["dim_0", "dim_1"])) + def run_variant( agent_cls: type[SugarscapeAgentsBase], *, From b8597391eb173d07bb9887635071675f1dd07fee Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 16:53:28 +0200 Subject: [PATCH 163/329] fix: resolve ambiguity in membership checks for occupied cells in SugarscapeAgentsBase --- docs/general/user-guide/3_advanced_tutorial.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index a301822b..b0a1d5c6 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -384,7 +384,10 @@ def eat(self) -> None: """ # Map of currently occupied agent ids on the grid. occupied_ids = self.index - occupied_cells = self.space.cells.filter(pl.col("agent_id").is_in(occupied_ids)) + # `occupied_ids` is a Polars Series; calling `is_in` with a Series + # of the same datatype is ambiguous in newer Polars. Use `implode` + # to collapse the Series into a list-like value for membership checks. + occupied_cells = self.space.cells.filter(pl.col("agent_id").is_in(occupied_ids.implode())) if occupied_cells.is_empty(): return # The agent ordering here uses the agent_id values stored in the From 0c819c98d8a9014de72030cd454dffe18e8279d3 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 17:03:05 +0200 Subject: [PATCH 164/329] feat: add environment variable support for sequential baseline execution in advanced tutorial --- .../general/user-guide/3_advanced_tutorial.py | 122 +++++++++++++----- 1 file changed, 91 insertions(+), 31 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index b0a1d5c6..2579816a 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -67,7 +67,7 @@ """## 1. Imports""" # %% - +import os from collections import defaultdict from time import perf_counter @@ -551,6 +551,16 @@ def _choose_best_cell( MODEL_STEPS = 60 MAX_SUGAR = 4 +# Allow quick testing by skipping the slow pure-Python sequential baseline. +# Set the environment variable ``MESA_FRAMES_RUN_SEQUENTIAL=0`` (or "false") +# to disable the baseline when running this script. +RUN_SEQUENTIAL = os.getenv("MESA_FRAMES_RUN_SEQUENTIAL", "0").lower() not in { + "0", + "false", + "no", + "off", +} + @njit(cache=True) def _numba_should_replace( best_sugar: int, @@ -817,13 +827,16 @@ def move(self) -> None: return # Map the positional frame to a center lookup used when joining - # neighbourhoods produced by the space helper. - center_lookup = self.pos.rename( - { - "unique_id": "agent_id", - "dim_0": "dim_0_center", - "dim_1": "dim_1_center", - } + # neighbourhoods produced by the space helper. Build the lookup by + # explicitly selecting and aliasing columns so the join creates a + # deterministic `agent_id` column (some internal joins can drop or + # fail to expose renamed columns when types/indices differ). + center_lookup = self.pos.select( + [ + pl.col("unique_id").alias("agent_id"), + pl.col("dim_0").alias("dim_0_center"), + pl.col("dim_1").alias("dim_1_center"), + ] ) # Build a neighbourhood frame: for each agent and visible cell we @@ -837,13 +850,22 @@ def move(self) -> None: on=["dim_0", "dim_1"], how="left", ) - .join(center_lookup, on=["dim_0_center", "dim_1_center"], how="left") .with_columns(pl.col("sugar").fill_null(0)) ) - # Normalise occupant column name if present. + # Normalise occupant column name if present (agent occupying the + # cell). The center lookup join may produce a conflicting + # `agent_id` column (suffix _right) — handle both cases so that + # `agent_id` unambiguously refers to the center agent and + # `occupant_id` refers to any agent already occupying the cell. if "agent_id" in neighborhood.columns: neighborhood = neighborhood.rename({"agent_id": "occupant_id"}) + neighborhood = neighborhood.join( + center_lookup, on=["dim_0_center", "dim_1_center"], how="left" + ) + if "agent_id_right" in neighborhood.columns: + # Rename the joined center lookup's id to the canonical name. + neighborhood = neighborhood.rename({"agent_id_right": "agent_id"}) # Create ranked choices per agent: sort by sugar (desc), radius # (asc), then coordinates. Keep the first unique entry per cell. @@ -869,7 +891,13 @@ def move(self) -> None: keep="first", maintain_order=True, ) - .with_columns(pl.cum_count().over("agent_id").cast(pl.Int64).alias("rank")) + .with_columns( + pl.col("agent_id") + .cum_count() + .over("agent_id") + .cast(pl.Int64) + .alias("rank") + ) ) if choices.is_empty(): @@ -892,7 +920,7 @@ def move(self) -> None: unresolved = pl.DataFrame( { "agent_id": agent_ids, - "current_rank": pl.Series(np.zeros(agent_ids.len(), dtype=np.int64)), + "current_rank": pl.Series(np.zeros(len(agent_ids), dtype=np.int64)), } ) @@ -1110,16 +1138,26 @@ def summarise_inequality(model: Model) -> dict[str, float]: # %% sequential_seed = 11 -sequential_model, sequential_time = run_variant( - SugarscapeSequentialAgents, steps=MODEL_STEPS, seed=sequential_seed -) +if RUN_SEQUENTIAL: + sequential_model, sequential_time = run_variant( + SugarscapeSequentialAgents, steps=MODEL_STEPS, seed=sequential_seed + ) -seq_model_frame = sequential_model.datacollector.data["model"] -print("Sequential aggregate trajectory (last 5 steps):") -print( - seq_model_frame.select(["step", "mean_sugar", "total_sugar", "living_agents"]).tail(5) -) -print(f"Sequential runtime: {sequential_time:.3f} s") + seq_model_frame = sequential_model.datacollector.data["model"] + print("Sequential aggregate trajectory (last 5 steps):") + print( + seq_model_frame.select( + ["step", "mean_sugar", "total_sugar", "living_agents"] + ).tail(5) + ) + print(f"Sequential runtime: {sequential_time:.3f} s") +else: + sequential_model = None + seq_model_frame = pl.DataFrame() + sequential_time = float("nan") + print( + "Skipping sequential baseline; set MESA_FRAMES_RUN_SEQUENTIAL=1 to enable it." + ) # %% [markdown] """ @@ -1171,16 +1209,38 @@ def summarise_inequality(model: Model) -> dict[str, float]: """ # %% -runtime_table = pl.DataFrame( - { - "update_rule": [ - "Sequential (Python loop)", - "Sequential (Numba)", - "Parallel (Polars)", - ], - "runtime_seconds": [sequential_time, numba_time, parallel_time], - } -).with_columns(pl.col("runtime_seconds").round(4)) +runtime_rows: list[dict[str, float | str]] = [] +if RUN_SEQUENTIAL: + runtime_rows.append( + { + "update_rule": "Sequential (Python loop)", + "runtime_seconds": sequential_time, + } + ) +else: + runtime_rows.append( + { + "update_rule": "Sequential (Python loop) [skipped]", + "runtime_seconds": float("nan"), + } + ) + +runtime_rows.extend( + [ + { + "update_rule": "Sequential (Numba)", + "runtime_seconds": numba_time, + }, + { + "update_rule": "Parallel (Polars)", + "runtime_seconds": parallel_time, + }, + ] +) + +runtime_table = pl.DataFrame(runtime_rows).with_columns( + pl.col("runtime_seconds").round(4) +) print(runtime_table) From 1f52845b9f7d5a0ea50e9fb292c90860dfdb6057 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 17:06:22 +0200 Subject: [PATCH 165/329] refactor: move _current_sugar_map method to SugarscapeSequentialAgents class --- .../general/user-guide/3_advanced_tutorial.py | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 2579816a..5a0368d6 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -414,22 +414,6 @@ def _remove_starved(self) -> None: # ``discard`` accepts a DataFrame of agents to remove. self.discard(starved) - def _current_sugar_map(self) -> dict[tuple[int, int], int]: - """Return a mapping from grid coordinates to the current sugar value. - - Returns - ------- - dict - Keys are ``(x, y)`` tuples and values are the integer sugar amount - on that cell (zero if missing/None). - """ - cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) - # Build a plain Python dict for fast lookups in the movement code. - return { - (int(x), int(y)): 0 if sugar is None else int(sugar) - for x, y, sugar in cells.iter_rows() - } - @staticmethod def _manhattan(a: tuple[int, int], b: tuple[int, int]) -> int: """Compute the Manhattan (L1) distance between two grid cells. @@ -758,6 +742,21 @@ def sequential_move_numba( class SugarscapeSequentialAgents(SugarscapeAgentsBase): + def _current_sugar_map(self) -> dict[tuple[int, int], int]: + """Return a mapping from grid coordinates to the current sugar value. + + Returns + ------- + dict + Keys are ``(x, y)`` tuples and values are the integer sugar amount + on that cell (zero if missing/None). + """ + cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) + # Build a plain Python dict for fast lookups in the movement code. + return { + (int(x), int(y)): 0 if sugar is None else int(sugar) + for x, y, sugar in cells.iter_rows() + } def move(self) -> None: sugar_map = self._current_sugar_map() state = self.df.join(self.pos, on="unique_id", how="left") @@ -821,12 +820,11 @@ def move(self) -> None: # and losers are promoted to their next-ranked choice. if len(self.df) == 0: return - sugar_map = self._current_sugar_map() state = self.df.join(self.pos, on="unique_id", how="left") if state.is_empty(): return - # Map the positional frame to a center lookup used when joining + # Map the positional frame to a center lookup used when joining # neighbourhoods produced by the space helper. Build the lookup by # explicitly selecting and aliasing columns so the join creates a # deterministic `agent_id` column (some internal joins can drop or From f78c4c2b5f9ecf24aee0f2f22aa7ff83cd9efb55 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 17:17:22 +0200 Subject: [PATCH 166/329] refactor: replace Manhattan distance calculation with Frobenius norm in SugarscapeAgentsBase --- .../general/user-guide/3_advanced_tutorial.py | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 5a0368d6..c40863ab 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -414,22 +414,6 @@ def _remove_starved(self) -> None: # ``discard`` accepts a DataFrame of agents to remove. self.discard(starved) - @staticmethod - def _manhattan(a: tuple[int, int], b: tuple[int, int]) -> int: - """Compute the Manhattan (L1) distance between two grid cells. - - Parameters - ---------- - a, b : tuple[int, int] - Coordinate pairs ``(x, y)``. - - Returns - ------- - int - The Manhattan distance between ``a`` and ``b``. - """ - return abs(a[0] - b[0]) + abs(a[1] - b[1]) - def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: """List cells visible from an origin along the four cardinal axes. @@ -476,8 +460,9 @@ def _choose_best_cell( Tie-break rules (in order): 1. Prefer cells with strictly greater sugar. - 2. If equal sugar, prefer the cell with smaller Manhattan distance - from the origin. + 2. If equal sugar, prefer the cell with smaller distance from the + origin (measured with the Frobenius norm returned by + ``space.get_distances``). 3. If still tied, prefer the cell with smaller coordinates (lexicographic ordering of the ``(x, y)`` tuple). @@ -508,7 +493,7 @@ def _choose_best_cell( if blocked and candidate != origin and candidate in blocked: continue sugar_here = sugar_map.get(candidate, 0) - distance = self._manhattan(origin, candidate) + distance = self.model.space.get_distances(origin, candidate)["distance"].item() better = False # Primary criterion: strictly more sugar. if sugar_here > best_sugar: From 6e6c5d1e2be8b09e00597409fe6cd49328ec5304 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 17:20:52 +0200 Subject: [PATCH 167/329] refactor: move _visible_cells and _choose_best_cell methods to SugarscapeSequentialAgents class --- .../general/user-guide/3_advanced_tutorial.py | 194 +++++++++--------- 1 file changed, 97 insertions(+), 97 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index c40863ab..93c2000a 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -414,103 +414,6 @@ def _remove_starved(self) -> None: # ``discard`` accepts a DataFrame of agents to remove. self.discard(starved) - def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: - """List cells visible from an origin along the four cardinal axes. - - The visibility set includes the origin cell itself and cells at - Manhattan distances 1..vision along the four cardinal directions - (up, down, left, right), clipped to the grid bounds. - - Parameters - ---------- - origin : tuple[int, int] - The agent's current coordinate ``(x, y)``. - vision : int - Maximum Manhattan radius to consider along each axis. - - Returns - ------- - list[tuple[int, int]] - Ordered list of visible cells (origin first, then increasing - step distance along each axis). - """ - x0, y0 = origin - width, height = self.space.dimensions - cells: list[tuple[int, int]] = [origin] - # Look outward one step at a time in the four cardinal directions. - for step in range(1, vision + 1): - if x0 + step < width: - cells.append((x0 + step, y0)) - if x0 - step >= 0: - cells.append((x0 - step, y0)) - if y0 + step < height: - cells.append((x0, y0 + step)) - if y0 - step >= 0: - cells.append((x0, y0 - step)) - return cells - - def _choose_best_cell( - self, - origin: tuple[int, int], - vision: int, - sugar_map: dict[tuple[int, int], int], - blocked: set[tuple[int, int]] | None, - ) -> tuple[int, int]: - """Select the best visible cell according to the movement rules. - - Tie-break rules (in order): - 1. Prefer cells with strictly greater sugar. - 2. If equal sugar, prefer the cell with smaller distance from the - origin (measured with the Frobenius norm returned by - ``space.get_distances``). - 3. If still tied, prefer the cell with smaller coordinates (lexicographic - ordering of the ``(x, y)`` tuple). - - Parameters - ---------- - origin : tuple[int, int] - Agent's current coordinate. - vision : int - Maximum vision radius along cardinal axes. - sugar_map : dict - Mapping from ``(x, y)`` to sugar amount. - blocked : set or None - Optional set of coordinates that should be considered occupied and - therefore skipped (except the origin which is always allowed). - - Returns - ------- - tuple[int, int] - Chosen target coordinate (may be the origin if no better cell is - available). - """ - best_cell = origin - best_sugar = sugar_map.get(origin, 0) - best_distance = 0 - for candidate in self._visible_cells(origin, vision): - # Skip blocked cells (occupied by other agents) unless it's the - # agent's current cell which we always consider. - if blocked and candidate != origin and candidate in blocked: - continue - sugar_here = sugar_map.get(candidate, 0) - distance = self.model.space.get_distances(origin, candidate)["distance"].item() - better = False - # Primary criterion: strictly more sugar. - if sugar_here > best_sugar: - better = True - elif sugar_here == best_sugar: - # Secondary: closer distance. - if distance < best_distance: - better = True - # Tertiary: lexicographic tie-break on coordinates. - elif distance == best_distance and candidate < best_cell: - better = True - if better: - best_cell = candidate - best_sugar = sugar_here - best_distance = distance - return best_cell - # %% @@ -727,6 +630,103 @@ def sequential_move_numba( class SugarscapeSequentialAgents(SugarscapeAgentsBase): + def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: + """List cells visible from an origin along the four cardinal axes. + + The visibility set includes the origin cell itself and cells at + Manhattan distances 1..vision along the four cardinal directions + (up, down, left, right), clipped to the grid bounds. + + Parameters + ---------- + origin : tuple[int, int] + The agent's current coordinate ``(x, y)``. + vision : int + Maximum Manhattan radius to consider along each axis. + + Returns + ------- + list[tuple[int, int]] + Ordered list of visible cells (origin first, then increasing + step distance along each axis). + """ + x0, y0 = origin + width, height = self.space.dimensions + cells: list[tuple[int, int]] = [origin] + # Look outward one step at a time in the four cardinal directions. + for step in range(1, vision + 1): + if x0 + step < width: + cells.append((x0 + step, y0)) + if x0 - step >= 0: + cells.append((x0 - step, y0)) + if y0 + step < height: + cells.append((x0, y0 + step)) + if y0 - step >= 0: + cells.append((x0, y0 - step)) + return cells + + def _choose_best_cell( + self, + origin: tuple[int, int], + vision: int, + sugar_map: dict[tuple[int, int], int], + blocked: set[tuple[int, int]] | None, + ) -> tuple[int, int]: + """Select the best visible cell according to the movement rules. + + Tie-break rules (in order): + 1. Prefer cells with strictly greater sugar. + 2. If equal sugar, prefer the cell with smaller distance from the + origin (measured with the Frobenius norm returned by + ``space.get_distances``). + 3. If still tied, prefer the cell with smaller coordinates (lexicographic + ordering of the ``(x, y)`` tuple). + + Parameters + ---------- + origin : tuple[int, int] + Agent's current coordinate. + vision : int + Maximum vision radius along cardinal axes. + sugar_map : dict + Mapping from ``(x, y)`` to sugar amount. + blocked : set or None + Optional set of coordinates that should be considered occupied and + therefore skipped (except the origin which is always allowed). + + Returns + ------- + tuple[int, int] + Chosen target coordinate (may be the origin if no better cell is + available). + """ + best_cell = origin + best_sugar = sugar_map.get(origin, 0) + best_distance = 0 + for candidate in self._visible_cells(origin, vision): + # Skip blocked cells (occupied by other agents) unless it's the + # agent's current cell which we always consider. + if blocked and candidate != origin and candidate in blocked: + continue + sugar_here = sugar_map.get(candidate, 0) + distance = self.model.space.get_distances(origin, candidate)["distance"].item() + better = False + # Primary criterion: strictly more sugar. + if sugar_here > best_sugar: + better = True + elif sugar_here == best_sugar: + # Secondary: closer distance. + if distance < best_distance: + better = True + # Tertiary: lexicographic tie-break on coordinates. + elif distance == best_distance and candidate < best_cell: + better = True + if better: + best_cell = candidate + best_sugar = sugar_here + best_distance = distance + return best_cell + def _current_sugar_map(self) -> dict[tuple[int, int], int]: """Return a mapping from grid coordinates to the current sugar value. From 71254476e6fff7cab0c2e5f4cfdcc5193102c2b5 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 17:21:13 +0200 Subject: [PATCH 168/329] chore: remove placeholder advanced tutorial for SugarScape with Instantaneous Growback --- docs/general/user-guide/3_advanced-tutorial.md | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 docs/general/user-guide/3_advanced-tutorial.md diff --git a/docs/general/user-guide/3_advanced-tutorial.md b/docs/general/user-guide/3_advanced-tutorial.md deleted file mode 100644 index 8a2eae55..00000000 --- a/docs/general/user-guide/3_advanced-tutorial.md +++ /dev/null @@ -1,4 +0,0 @@ -# Advanced Tutorial: SugarScape with Instantaneous Growback 🍬🔄 - -!!! warning "Work in Progress 🚧" - This tutorial is coming soon! 🔜✨ In the meantime, you can check out the code in the `examples/sugarscape-ig` directory of the mesa-frames repository. From 0be9d0f8ef2f5462ea686c2880c032c263b41e62 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 18:16:59 +0200 Subject: [PATCH 169/329] feat: add Gini coefficient and correlation metrics for sugar, metabolism, and vision in Sugarscape model --- .../general/user-guide/3_advanced_tutorial.py | 513 +++++++++--------- 1 file changed, 257 insertions(+), 256 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 93c2000a..f578b71e 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -68,7 +68,6 @@ # %% import os -from collections import defaultdict from time import perf_counter import numpy as np @@ -94,12 +93,74 @@ and maximum sugar (for regrowth). The model also sets up a data collector to track aggregate statistics and agent traits over time. -The `step` method advances the sugar field, triggers the agent set's step +The `step` method advances the sugar field, triggers the agent set's step. + +We also define some useful functions to compute metrics like the Gini coefficient and correlations. """ # %% +# Model-level reporters + +def gini(model: Model) -> float: + if len(model.sets) == 0: + return float("nan") + + primary_set = model.sets[0] + if len(primary_set) == 0: + return float("nan") + + sugar = primary_set.df["sugar"].to_numpy().astype(np.float64) + + if sugar.size == 0: + return float("nan") + sorted_vals = np.sort(sugar.astype(np.float64)) + n = sorted_vals.size + if n == 0: + return float("nan") + cumulative = np.cumsum(sorted_vals) + total = cumulative[-1] + if total == 0: + return 0.0 + index = np.arange(1, n + 1, dtype=np.float64) + return float((2.0 * np.dot(index, sorted_vals) / (n * total)) - (n + 1) / n) + +def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: + if x.size < 2 or y.size < 2: + return float("nan") + if np.allclose(x, x[0]) or np.allclose(y, y[0]): + return float("nan") + return float(np.corrcoef(x, y)[0, 1]) + + +def corr_sugar_metabolism(model: Model) -> float: + if len(model.sets) == 0: + return float("nan") + + primary_set = model.sets[0] + if len(primary_set) == 0: + return float("nan") + + agent_df = primary_set.df + sugar = agent_df["sugar"].to_numpy().astype(np.float64) + metabolism = agent_df["metabolism"].to_numpy().astype(np.float64) + return _safe_corr(sugar, metabolism) + + +def corr_sugar_vision(model: Model) -> float: + if len(model.sets) == 0: + return float("nan") + + primary_set = model.sets[0] + if len(primary_set) == 0: + return float("nan") + + agent_df = primary_set.df + sugar = agent_df["sugar"].to_numpy().astype(np.float64) + vision = agent_df["vision"].to_numpy().astype(np.float64) + return _safe_corr(sugar, vision) + class Sugarscape(Model): """Minimal Sugarscape model used throughout the tutorial. @@ -181,6 +242,11 @@ def __init__( if len(m.sets[0]) else 0.0, "living_agents": lambda m: len(m.sets[0]), + # Inequality metrics recorded individually. + "gini": gini, + "corr_sugar_metabolism": corr_sugar_metabolism, + "corr_sugar_vision": corr_sugar_vision, + "agents_alive": lambda m: float(len(m.sets[0])) if len(m.sets) else 0.0, }, agent_reporters={"traits": ["sugar", "metabolism", "vision"]}, ) @@ -296,12 +362,11 @@ def _advance_sugar_field(self) -> None: """ ## 3. Agent definition -### Base agent class +### 3.1 Base agent class Now let's define the agent class (the ant class). We start with a base class which implements the common logic for eating and starvation, while leaving the `move` method abstract. The base class also provides helper methods for sensing visible cells and choosing the best cell based on sugar, distance, and coordinates. This will allow us to define different movement policies (sequential, Numba-accelerated, and parallel) as subclasses that only need to implement the `move` method. -We also add """ # %% @@ -415,23 +480,156 @@ def _remove_starved(self) -> None: self.discard(starved) +# %% [markdown] -# %% -GRID_WIDTH = 50 -GRID_HEIGHT = 50 -NUM_AGENTS = 400 -MODEL_STEPS = 60 -MAX_SUGAR = 4 +"""### 3.2 Sequential movement + +We now implement the simplest movement policy: sequential (asynchronous). Each agent moves one at a time in the current ordering, choosing the best visible cell according to the rules. + +This implementation uses plain Python loops as the logic cannot be easily vectorised. As a result, it is slow for large populations and grids. We will later show how to speed it up with Numba. +""" + +# %% + +class SugarscapeSequentialAgents(SugarscapeAgentsBase): + def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: + """List cells visible from an origin along the four cardinal axes. + + The visibility set includes the origin cell itself and cells at + Manhattan distances 1..vision along the four cardinal directions + (up, down, left, right), clipped to the grid bounds. + + Parameters + ---------- + origin : tuple[int, int] + The agent's current coordinate ``(x, y)``. + vision : int + Maximum Manhattan radius to consider along each axis. + + Returns + ------- + list[tuple[int, int]] + Ordered list of visible cells (origin first, then increasing + step distance along each axis). + """ + x0, y0 = origin + width, height = self.space.dimensions + cells: list[tuple[int, int]] = [origin] + # Look outward one step at a time in the four cardinal directions. + for step in range(1, vision + 1): + if x0 + step < width: + cells.append((x0 + step, y0)) + if x0 - step >= 0: + cells.append((x0 - step, y0)) + if y0 + step < height: + cells.append((x0, y0 + step)) + if y0 - step >= 0: + cells.append((x0, y0 - step)) + return cells + + def _choose_best_cell( + self, + origin: tuple[int, int], + vision: int, + sugar_map: dict[tuple[int, int], int], + blocked: set[tuple[int, int]] | None, + ) -> tuple[int, int]: + """Select the best visible cell according to the movement rules. + + Tie-break rules (in order): + 1. Prefer cells with strictly greater sugar. + 2. If equal sugar, prefer the cell with smaller distance from the + origin (measured with the Frobenius norm returned by + ``space.get_distances``). + 3. If still tied, prefer the cell with smaller coordinates (lexicographic + ordering of the ``(x, y)`` tuple). + + Parameters + ---------- + origin : tuple[int, int] + Agent's current coordinate. + vision : int + Maximum vision radius along cardinal axes. + sugar_map : dict + Mapping from ``(x, y)`` to sugar amount. + blocked : set or None + Optional set of coordinates that should be considered occupied and + therefore skipped (except the origin which is always allowed). + + Returns + ------- + tuple[int, int] + Chosen target coordinate (may be the origin if no better cell is + available). + """ + best_cell = origin + best_sugar = sugar_map.get(origin, 0) + best_distance = 0 + for candidate in self._visible_cells(origin, vision): + # Skip blocked cells (occupied by other agents) unless it's the + # agent's current cell which we always consider. + if blocked and candidate != origin and candidate in blocked: + continue + sugar_here = sugar_map.get(candidate, 0) + distance = self.model.space.get_distances(origin, candidate)["distance"].item() + better = False + # Primary criterion: strictly more sugar. + if sugar_here > best_sugar: + better = True + elif sugar_here == best_sugar: + # Secondary: closer distance. + if distance < best_distance: + better = True + # Tertiary: lexicographic tie-break on coordinates. + elif distance == best_distance and candidate < best_cell: + better = True + if better: + best_cell = candidate + best_sugar = sugar_here + best_distance = distance + return best_cell + + def _current_sugar_map(self) -> dict[tuple[int, int], int]: + """Return a mapping from grid coordinates to the current sugar value. + + Returns + ------- + dict + Keys are ``(x, y)`` tuples and values are the integer sugar amount + on that cell (zero if missing/None). + """ + cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) + # Build a plain Python dict for fast lookups in the movement code. + return { + (int(x), int(y)): 0 if sugar is None else int(sugar) + for x, y, sugar in cells.iter_rows() + } + + def move(self) -> None: + sugar_map = self._current_sugar_map() + state = self.df.join(self.pos, on="unique_id", how="left") + positions = { + int(row["unique_id"]): (int(row["dim_0"]), int(row["dim_1"])) + for row in state.iter_rows(named=True) + } + taken: set[tuple[int, int]] = set(positions.values()) + + for row in state.iter_rows(named=True): + agent_id = int(row["unique_id"]) + vision = int(row["vision"]) + current = positions[agent_id] + taken.discard(current) + target = self._choose_best_cell(current, vision, sugar_map, taken) + taken.add(target) + positions[agent_id] = target + if target != current: + self.space.move_agents(agent_id, target) + +# %% [markdown] +""" +## 3.4 Speeding Up the Loop with Numba +""" -# Allow quick testing by skipping the slow pure-Python sequential baseline. -# Set the environment variable ``MESA_FRAMES_RUN_SEQUENTIAL=0`` (or "false") -# to disable the baseline when running this script. -RUN_SEQUENTIAL = os.getenv("MESA_FRAMES_RUN_SEQUENTIAL", "0").lower() not in { - "0", - "false", - "no", - "off", -} @njit(cache=True) def _numba_should_replace( @@ -608,167 +806,6 @@ def sequential_move_numba( return new_dim0, new_dim1 - - - -# %% [markdown] -""" -## 2. Agent Scaffolding - -With the space logic in place we can define the agents. The base class stores -traits and implements eating/starvation; concrete subclasses only override -`move`. -""" - - - - -# %% [markdown] -""" -## 3. Sequential Movement -""" - - -class SugarscapeSequentialAgents(SugarscapeAgentsBase): - def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: - """List cells visible from an origin along the four cardinal axes. - - The visibility set includes the origin cell itself and cells at - Manhattan distances 1..vision along the four cardinal directions - (up, down, left, right), clipped to the grid bounds. - - Parameters - ---------- - origin : tuple[int, int] - The agent's current coordinate ``(x, y)``. - vision : int - Maximum Manhattan radius to consider along each axis. - - Returns - ------- - list[tuple[int, int]] - Ordered list of visible cells (origin first, then increasing - step distance along each axis). - """ - x0, y0 = origin - width, height = self.space.dimensions - cells: list[tuple[int, int]] = [origin] - # Look outward one step at a time in the four cardinal directions. - for step in range(1, vision + 1): - if x0 + step < width: - cells.append((x0 + step, y0)) - if x0 - step >= 0: - cells.append((x0 - step, y0)) - if y0 + step < height: - cells.append((x0, y0 + step)) - if y0 - step >= 0: - cells.append((x0, y0 - step)) - return cells - - def _choose_best_cell( - self, - origin: tuple[int, int], - vision: int, - sugar_map: dict[tuple[int, int], int], - blocked: set[tuple[int, int]] | None, - ) -> tuple[int, int]: - """Select the best visible cell according to the movement rules. - - Tie-break rules (in order): - 1. Prefer cells with strictly greater sugar. - 2. If equal sugar, prefer the cell with smaller distance from the - origin (measured with the Frobenius norm returned by - ``space.get_distances``). - 3. If still tied, prefer the cell with smaller coordinates (lexicographic - ordering of the ``(x, y)`` tuple). - - Parameters - ---------- - origin : tuple[int, int] - Agent's current coordinate. - vision : int - Maximum vision radius along cardinal axes. - sugar_map : dict - Mapping from ``(x, y)`` to sugar amount. - blocked : set or None - Optional set of coordinates that should be considered occupied and - therefore skipped (except the origin which is always allowed). - - Returns - ------- - tuple[int, int] - Chosen target coordinate (may be the origin if no better cell is - available). - """ - best_cell = origin - best_sugar = sugar_map.get(origin, 0) - best_distance = 0 - for candidate in self._visible_cells(origin, vision): - # Skip blocked cells (occupied by other agents) unless it's the - # agent's current cell which we always consider. - if blocked and candidate != origin and candidate in blocked: - continue - sugar_here = sugar_map.get(candidate, 0) - distance = self.model.space.get_distances(origin, candidate)["distance"].item() - better = False - # Primary criterion: strictly more sugar. - if sugar_here > best_sugar: - better = True - elif sugar_here == best_sugar: - # Secondary: closer distance. - if distance < best_distance: - better = True - # Tertiary: lexicographic tie-break on coordinates. - elif distance == best_distance and candidate < best_cell: - better = True - if better: - best_cell = candidate - best_sugar = sugar_here - best_distance = distance - return best_cell - - def _current_sugar_map(self) -> dict[tuple[int, int], int]: - """Return a mapping from grid coordinates to the current sugar value. - - Returns - ------- - dict - Keys are ``(x, y)`` tuples and values are the integer sugar amount - on that cell (zero if missing/None). - """ - cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) - # Build a plain Python dict for fast lookups in the movement code. - return { - (int(x), int(y)): 0 if sugar is None else int(sugar) - for x, y, sugar in cells.iter_rows() - } - def move(self) -> None: - sugar_map = self._current_sugar_map() - state = self.df.join(self.pos, on="unique_id", how="left") - positions = { - int(row["unique_id"]): (int(row["dim_0"]), int(row["dim_1"])) - for row in state.iter_rows(named=True) - } - taken: set[tuple[int, int]] = set(positions.values()) - - for row in state.iter_rows(named=True): - agent_id = int(row["unique_id"]) - vision = int(row["vision"]) - current = positions[agent_id] - taken.discard(current) - target = self._choose_best_cell(current, vision, sugar_map, taken) - taken.add(target) - positions[agent_id] = target - if target != current: - self.space.move_agents(agent_id, target) - - -# %% [markdown] -""" -## 4. Speeding Up the Loop with Numba -""" - - class SugarscapeNumbaAgents(SugarscapeAgentsBase): def move(self) -> None: state = self.df.join(self.pos, on="unique_id", how="left") @@ -1016,6 +1053,16 @@ def move(self) -> None: # so pass Series/DataFrame directly rather than converting to Python lists. self.space.move_agents(move_df["unique_id"], move_df.select(["dim_0", "dim_1"])) + + +# %% [markdown] +""" +## 6. Shared Model Infrastructure + +`SugarscapeTutorialModel` wires the grid, agent set, regrowth logic, and data +collection. Each variant simply plugs in a different agent class. +""" + def run_variant( agent_cls: type[SugarscapeAgentsBase], *, @@ -1034,80 +1081,6 @@ def run_variant( model.run(steps) return model, perf_counter() - start - -# %% [markdown] -""" -## 6. Shared Model Infrastructure - -`SugarscapeTutorialModel` wires the grid, agent set, regrowth logic, and data -collection. Each variant simply plugs in a different agent class. -""" - - -def gini(values: np.ndarray) -> float: - if values.size == 0: - return float("nan") - sorted_vals = np.sort(values.astype(np.float64)) - n = sorted_vals.size - if n == 0: - return float("nan") - cumulative = np.cumsum(sorted_vals) - total = cumulative[-1] - if total == 0: - return 0.0 - index = np.arange(1, n + 1, dtype=np.float64) - return float((2.0 * np.dot(index, sorted_vals) / (n * total)) - (n + 1) / n) - - -def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: - if x.size < 2 or y.size < 2: - return float("nan") - if np.allclose(x, x[0]) or np.allclose(y, y[0]): - return float("nan") - return float(np.corrcoef(x, y)[0, 1]) - - -def _column_with_prefix(df: pl.DataFrame, prefix: str) -> str: - for col in df.columns: - if col.startswith(prefix): - return col - raise KeyError(f"No column starts with prefix '{prefix}'") - - -def final_agent_snapshot(model: Model) -> pl.DataFrame: - agent_frame = model.datacollector.data["agent"] - if agent_frame.is_empty(): - return agent_frame - last_step = agent_frame["step"].max() - return agent_frame.filter(pl.col("step") == last_step) - - -def summarise_inequality(model: Model) -> dict[str, float]: - snapshot = final_agent_snapshot(model) - if snapshot.is_empty(): - return { - "gini": float("nan"), - "corr_sugar_metabolism": float("nan"), - "corr_sugar_vision": float("nan"), - "agents_alive": 0, - } - - sugar_col = _column_with_prefix(snapshot, "traits_sugar_") - metabolism_col = _column_with_prefix(snapshot, "traits_metabolism_") - vision_col = _column_with_prefix(snapshot, "traits_vision_") - - sugar = snapshot[sugar_col].to_numpy() - metabolism = snapshot[metabolism_col].to_numpy() - vision = snapshot[vision_col].to_numpy() - - return { - "gini": gini(sugar), - "corr_sugar_metabolism": _safe_corr(sugar, metabolism), - "corr_sugar_vision": _safe_corr(sugar, vision), - "agents_alive": float(sugar.size), - } - - # %% [markdown] """ ## 7. Run the Sequential Model (Python loop) @@ -1119,6 +1092,24 @@ def summarise_inequality(model: Model) -> dict[str, float]: """ # %% + +# %% +GRID_WIDTH = 50 +GRID_HEIGHT = 50 +NUM_AGENTS = 400 +MODEL_STEPS = 60 +MAX_SUGAR = 4 + +# Allow quick testing by skipping the slow pure-Python sequential baseline. +# Set the environment variable ``MESA_FRAMES_RUN_SEQUENTIAL=0`` (or "false") +# to disable the baseline when running this script. +RUN_SEQUENTIAL = os.getenv("MESA_FRAMES_RUN_SEQUENTIAL", "0").lower() not in { + "0", + "false", + "no", + "off", +} + sequential_seed = 11 if RUN_SEQUENTIAL: @@ -1269,11 +1260,21 @@ def summarise_inequality(model: Model) -> dict[str, float]: [ { "update_rule": "Sequential (Numba)", - **summarise_inequality(numba_model), + "gini": gini(numba_model), + "corr_sugar_metabolism": corr_sugar_metabolism(numba_model), + "corr_sugar_vision": corr_sugar_vision(numba_model), + "agents_alive": float(len(numba_model.sets[0])) + if len(numba_model.sets) + else 0.0, }, { "update_rule": "Parallel (random tie-break)", - **summarise_inequality(parallel_model), + "gini": gini(parallel_model), + "corr_sugar_metabolism": corr_sugar_metabolism(parallel_model), + "corr_sugar_vision": corr_sugar_vision(parallel_model), + "agents_alive": float(len(parallel_model.sets[0])) + if len(parallel_model.sets) + else 0.0, }, ] ) From 273ba7ce110a8652fb3bfb59e50385f89fef1851 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 18:19:00 +0200 Subject: [PATCH 170/329] refactor: move _safe_corr function to improve code organization in advanced tutorial --- docs/general/user-guide/3_advanced_tutorial.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index f578b71e..14f4c5ac 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -126,14 +126,6 @@ def gini(model: Model) -> float: index = np.arange(1, n + 1, dtype=np.float64) return float((2.0 * np.dot(index, sorted_vals) / (n * total)) - (n + 1) / n) -def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: - if x.size < 2 or y.size < 2: - return float("nan") - if np.allclose(x, x[0]) or np.allclose(y, y[0]): - return float("nan") - return float(np.corrcoef(x, y)[0, 1]) - - def corr_sugar_metabolism(model: Model) -> float: if len(model.sets) == 0: return float("nan") @@ -147,7 +139,6 @@ def corr_sugar_metabolism(model: Model) -> float: metabolism = agent_df["metabolism"].to_numpy().astype(np.float64) return _safe_corr(sugar, metabolism) - def corr_sugar_vision(model: Model) -> float: if len(model.sets) == 0: return float("nan") @@ -161,6 +152,13 @@ def corr_sugar_vision(model: Model) -> float: vision = agent_df["vision"].to_numpy().astype(np.float64) return _safe_corr(sugar, vision) +def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: + if x.size < 2 or y.size < 2: + return float("nan") + if np.allclose(x, x[0]) or np.allclose(y, y[0]): + return float("nan") + return float(np.corrcoef(x, y)[0, 1]) + class Sugarscape(Model): """Minimal Sugarscape model used throughout the tutorial. From 0e5b125d8b3da88ca560a48f2b1b9576205ced57 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 18:20:23 +0200 Subject: [PATCH 171/329] feat: add Gini coefficient and correlation metrics for sugar, metabolism, and vision in advanced tutorial --- .../general/user-guide/3_advanced_tutorial.py | 83 ++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 14f4c5ac..88fb1a40 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -104,6 +104,26 @@ # Model-level reporters def gini(model: Model) -> float: + """Compute the Gini coefficient of agent sugar holdings. + + The function reads the primary agent set from ``model.sets[0]`` and + computes the population Gini coefficient on the ``sugar`` column. The + implementation is robust to empty sets and zero-total sugar. + + Parameters + ---------- + model : Model + The simulation model that contains agent sets. The primary agent set + is expected to be at ``model.sets[0]`` and to expose a Polars DataFrame + under ``.df`` with a ``sugar`` column. + + Returns + ------- + float + Gini coefficient in the range [0, 1] if defined, ``0.0`` when the + total sugar is zero, and ``nan`` when the agent set is empty or too + small to measure. + """ if len(model.sets) == 0: return float("nan") @@ -112,7 +132,7 @@ def gini(model: Model) -> float: return float("nan") sugar = primary_set.df["sugar"].to_numpy().astype(np.float64) - + if sugar.size == 0: return float("nan") sorted_vals = np.sort(sugar.astype(np.float64)) @@ -127,6 +147,27 @@ def gini(model: Model) -> float: return float((2.0 * np.dot(index, sorted_vals) / (n * total)) - (n + 1) / n) def corr_sugar_metabolism(model: Model) -> float: + """Pearson correlation between agent sugar and metabolism. + + This reporter extracts the ``sugar`` and ``metabolism`` columns from the + primary agent set and returns their Pearson correlation coefficient. When + the agent set is empty or contains insufficient variation the function + returns ``nan``. + + Parameters + ---------- + model : Model + The simulation model that contains agent sets. The primary agent set + is expected to be at ``model.sets[0]`` and provide a Polars DataFrame + with ``sugar`` and ``metabolism`` columns. + + Returns + ------- + float + Pearson correlation coefficient between sugar and metabolism, or + ``nan`` when the correlation is undefined (empty set or constant + values). + """ if len(model.sets) == 0: return float("nan") @@ -140,6 +181,26 @@ def corr_sugar_metabolism(model: Model) -> float: return _safe_corr(sugar, metabolism) def corr_sugar_vision(model: Model) -> float: + """Pearson correlation between agent sugar and vision. + + Extracts the ``sugar`` and ``vision`` columns from the primary agent set + and returns their Pearson correlation coefficient. If the reporter cannot + compute a meaningful correlation (for example, when the agent set is + empty or values are constant) it returns ``nan``. + + Parameters + ---------- + model : Model + The simulation model that contains agent sets. The primary agent set + is expected to be at ``model.sets[0]`` and provide a Polars DataFrame + with ``sugar`` and ``vision`` columns. + + Returns + ------- + float + Pearson correlation coefficient between sugar and vision, or ``nan`` + when the correlation is undefined. + """ if len(model.sets) == 0: return float("nan") @@ -153,6 +214,26 @@ def corr_sugar_vision(model: Model) -> float: return _safe_corr(sugar, vision) def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: + """Safely compute Pearson correlation between two 1-D arrays. + + This helper guards against degenerate inputs (too few observations or + constant arrays) which would make the Pearson correlation undefined or + numerically unstable. When a valid correlation can be computed the + function returns a Python float. + + Parameters + ---------- + x, y : np.ndarray + One-dimensional numeric arrays of the same length containing the two + variables to correlate. + + Returns + ------- + float + Pearson correlation coefficient as a Python float, or ``nan`` if the + correlation is undefined (fewer than 2 observations or constant + inputs). + """ if x.size < 2 or y.size < 2: return float("nan") if np.allclose(x, x[0]) or np.allclose(y, y[0]): From 8957020cdb8316f3c1cc5466c459df971f99a198 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 18:21:28 +0200 Subject: [PATCH 172/329] refactor: rename 'living_agents' to 'agents_alive' for clarity in Sugarscape model --- docs/general/user-guide/3_advanced_tutorial.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 88fb1a40..45149e55 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -320,12 +320,10 @@ def __init__( "total_sugar": lambda m: float(m.sets[0].df["sugar"].sum()) if len(m.sets[0]) else 0.0, - "living_agents": lambda m: len(m.sets[0]), - # Inequality metrics recorded individually. + "agents_alive": lambda m: float(len(m.sets[0])) if len(m.sets) else 0.0, "gini": gini, "corr_sugar_metabolism": corr_sugar_metabolism, "corr_sugar_vision": corr_sugar_vision, - "agents_alive": lambda m: float(len(m.sets[0])) if len(m.sets) else 0.0, }, agent_reporters={"traits": ["sugar", "metabolism", "vision"]}, ) From 62812762b25991173aa92fc462db802a03320c98 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 18:31:45 +0200 Subject: [PATCH 173/329] refactor: rename 'SugarscapeAgentsBase' to 'AntsBase' and update related classes for improved clarity --- .../general/user-guide/3_advanced_tutorial.py | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 45149e55..7b49ef23 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -280,7 +280,7 @@ class Sugarscape(Model): def __init__( self, - agent_type: type["SugarscapeAgentsBase"], + agent_type: type["AntsBase"], n_agents: int, *, width: int, @@ -448,7 +448,7 @@ def _advance_sugar_field(self) -> None: # %% -class SugarscapeAgentsBase(AgentSet): +class AntsBase(AgentSet): """Base agent set for the Sugarscape tutorial. This class implements the common behaviour shared by all agent @@ -568,7 +568,7 @@ def _remove_starved(self) -> None: # %% -class SugarscapeSequentialAgents(SugarscapeAgentsBase): +class AntsSequential(AntsBase): def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: """List cells visible from an origin along the four cardinal axes. @@ -705,6 +705,10 @@ def move(self) -> None: # %% [markdown] """ ## 3.4 Speeding Up the Loop with Numba + +As we will see later, the previous sequential implementation is slow for large populations and grids because it relies on plain Python loops. We can speed it up significantly by using Numba to compile the movement logic. + +Numba compiles numerical Python code to fast machine code at runtime. To use Numba, we need to rewrite the movement logic in a way that is compatible with Numba's restrictions (using tightly typed numpy arrays and accessing data indexes directly). """ @@ -883,7 +887,7 @@ def sequential_move_numba( return new_dim0, new_dim1 -class SugarscapeNumbaAgents(SugarscapeAgentsBase): +class AntsNumba(AntsBase): def move(self) -> None: state = self.df.join(self.pos, on="unique_id", how="left") if state.is_empty(): @@ -908,10 +912,12 @@ def move(self) -> None: # %% [markdown] """ ## 5. Simultaneous Movement with Conflict Resolution + +The previous implementation is fast but it requires """ -class SugarscapeParallelAgents(SugarscapeAgentsBase): +class AntsParallel(AntsBase): def move(self) -> None: # Parallel movement: each agent proposes a ranked list of visible # cells (including its own). We resolve conflicts in rounds using @@ -1141,7 +1147,7 @@ def move(self) -> None: """ def run_variant( - agent_cls: type[SugarscapeAgentsBase], + agent_cls: type[AntsBase], *, steps: int, seed: int, @@ -1191,7 +1197,7 @@ def run_variant( if RUN_SEQUENTIAL: sequential_model, sequential_time = run_variant( - SugarscapeSequentialAgents, steps=MODEL_STEPS, seed=sequential_seed + AntsSequential, steps=MODEL_STEPS, seed=sequential_seed ) seq_model_frame = sequential_model.datacollector.data["model"] @@ -1221,7 +1227,7 @@ def run_variant( # %% numba_model, numba_time = run_variant( - SugarscapeNumbaAgents, steps=MODEL_STEPS, seed=sequential_seed + AntsNumba, steps=MODEL_STEPS, seed=sequential_seed ) numba_model_frame = numba_model.datacollector.data["model"] @@ -1241,7 +1247,7 @@ def run_variant( # %% parallel_model, parallel_time = run_variant( - SugarscapeParallelAgents, steps=MODEL_STEPS, seed=sequential_seed + AntsParallel, steps=MODEL_STEPS, seed=sequential_seed ) par_model_frame = parallel_model.datacollector.data["model"] From e03f2da683e7e3d157b8a365fc644aa406f8aab4 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 18:40:23 +0200 Subject: [PATCH 174/329] refactor: update grid dimensions and rename 'living_agents' to 'agents_alive' for clarity in advanced tutorial --- docs/general/user-guide/3_advanced_tutorial.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 7b49ef23..84e0701e 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -1177,9 +1177,9 @@ def run_variant( # %% # %% -GRID_WIDTH = 50 -GRID_HEIGHT = 50 -NUM_AGENTS = 400 +GRID_WIDTH = 250 +GRID_HEIGHT = 250 +NUM_AGENTS = 10000 MODEL_STEPS = 60 MAX_SUGAR = 4 @@ -1204,7 +1204,7 @@ def run_variant( print("Sequential aggregate trajectory (last 5 steps):") print( seq_model_frame.select( - ["step", "mean_sugar", "total_sugar", "living_agents"] + ["step", "mean_sugar", "total_sugar", "agents_alive"] ).tail(5) ) print(f"Sequential runtime: {sequential_time:.3f} s") @@ -1233,7 +1233,7 @@ def run_variant( numba_model_frame = numba_model.datacollector.data["model"] print("Numba sequential aggregate trajectory (last 5 steps):") print( - numba_model_frame.select(["step", "mean_sugar", "total_sugar", "living_agents"]).tail(5) + numba_model_frame.select(["step", "mean_sugar", "total_sugar", "agents_alive"]).tail(5) ) print(f"Numba sequential runtime: {numba_time:.3f} s") @@ -1252,7 +1252,7 @@ def run_variant( par_model_frame = parallel_model.datacollector.data["model"] print("Parallel aggregate trajectory (last 5 steps):") -print(par_model_frame.select(["step", "mean_sugar", "total_sugar", "living_agents"]).tail(5)) +print(par_model_frame.select(["step", "mean_sugar", "total_sugar", "agents_alive"]).tail(5)) print(f"Parallel runtime: {parallel_time:.3f} s") # %% [markdown] @@ -1325,8 +1325,8 @@ def run_variant( """ # %% -comparison = numba_model_frame.select(["step", "mean_sugar", "total_sugar", "living_agents"]).join( - par_model_frame.select(["step", "mean_sugar", "total_sugar", "living_agents"]), +comparison = numba_model_frame.select(["step", "mean_sugar", "total_sugar", "agents_alive"]).join( + par_model_frame.select(["step", "mean_sugar", "total_sugar", "agents_alive"]), on="step", how="inner", suffix="_parallel", @@ -1334,7 +1334,7 @@ def run_variant( comparison = comparison.with_columns( (pl.col("mean_sugar") - pl.col("mean_sugar_parallel")).abs().alias("mean_diff"), (pl.col("total_sugar") - pl.col("total_sugar_parallel")).abs().alias("total_diff"), - (pl.col("living_agents") - pl.col("living_agents_parallel")).abs().alias("count_diff"), + (pl.col("agents_alive") - pl.col("agents_alive_parallel")).abs().alias("count_diff"), ) print("Step-level absolute differences (first 10 steps):") print(comparison.select(["step", "mean_diff", "total_diff", "count_diff"]).head(10)) From 44bbdc2378555298dc226bfcca377308f99ca6e0 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:18:23 +0200 Subject: [PATCH 175/329] refactor: update section headings for clarity and consistency in advanced tutorial --- .../general/user-guide/3_advanced_tutorial.py | 176 ++++++++++++++---- 1 file changed, 136 insertions(+), 40 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 84e0701e..7b50564a 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -704,7 +704,7 @@ def move(self) -> None: # %% [markdown] """ -## 3.4 Speeding Up the Loop with Numba +### 3.3 Speeding Up the Loop with Numba As we will see later, the previous sequential implementation is slow for large populations and grids because it relies on plain Python loops. We can speed it up significantly by using Numba to compile the movement logic. @@ -911,30 +911,32 @@ def move(self) -> None: # %% [markdown] """ -## 5. Simultaneous Movement with Conflict Resolution +### 3.5. Simultaneous Movement with Conflict Resolution (the Polars mesa-frames idiomatic way) -The previous implementation is fast but it requires +The previous implementation is optimal speed-wise but it's a bit low-level. It requires mantaining an occupancy grid and imperative loops and it might become tricky to extend with more complex movement rules or models. +To stay in mesa-frames idiom, we can implement a parallel movement policy that uses Polars DataFrame operations to resolve conflicts when multiple agents target the same cell. +These conflicts are resolved in rounds: in each round, each agent proposes its current best candidate cell; winners per cell are chosen at random, and losers are promoted to their next-ranked choice. This continues until all agents have moved. +This implementation is a tad slower but still efficient and easier to read (for a Polars user). """ class AntsParallel(AntsBase): def move(self) -> None: - # Parallel movement: each agent proposes a ranked list of visible - # cells (including its own). We resolve conflicts in rounds using - # DataFrame operations so winners can be chosen per-cell at random - # and losers are promoted to their next-ranked choice. + """ + Parallel movement: each agent proposes a ranked list of visible cells (including its own). + We resolve conflicts in rounds using DataFrame operations so winners can be chosen per-cell at random and losers are promoted to their next-ranked choice. + """ + # Early exit if there are no agents. if len(self.df) == 0: return - state = self.df.join(self.pos, on="unique_id", how="left") - if state.is_empty(): - return - # Map the positional frame to a center lookup used when joining - # neighbourhoods produced by the space helper. Build the lookup by - # explicitly selecting and aliasing columns so the join creates a - # deterministic `agent_id` column (some internal joins can drop or - # fail to expose renamed columns when types/indices differ). - center_lookup = self.pos.select( + # current_pos columns: + # ┌──────────┬────────────────┬────────────────┐ + # │ agent_id ┆ dim_0_center ┆ dim_1_center │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════════════╪════════════════╡ + current_pos = self.pos.select( [ pl.col("unique_id").alias("agent_id"), pl.col("dim_0").alias("dim_0_center"), @@ -943,35 +945,54 @@ def move(self) -> None: ) # Build a neighbourhood frame: for each agent and visible cell we - # attach the cell sugar and the agent_id of the occupant (if any). + # attach the cell sugar. The raw offsets contain the candidate + # cell coordinates and the center coordinates for the sensing agent. + # Raw neighborhood columns: + # ┌────────────┬────────────┬────────┬────────────────┬────────────────┐ + # │ dim_0 ┆ dim_1 ┆ radius ┆ dim_0_center ┆ dim_1_center │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞════════════╪════════════╪════════╪════════════════╪════════════════╡ + neighborhood = self.space.get_neighborhood( + radius=self["vision"], agents=self, include_center=True + ) + + cell_props = self.space.cells.select(["dim_0", "dim_1", "sugar"]) neighborhood = ( - self.space.get_neighborhood( - radius=self["vision"], agents=self, include_center=True - ) - .join( - self.space.cells.select(["dim_0", "dim_1", "sugar"]), - on=["dim_0", "dim_1"], - how="left", - ) + neighborhood + .join(cell_props, on=["dim_0", "dim_1"], how="left") .with_columns(pl.col("sugar").fill_null(0)) ) - # Normalise occupant column name if present (agent occupying the - # cell). The center lookup join may produce a conflicting - # `agent_id` column (suffix _right) — handle both cases so that - # `agent_id` unambiguously refers to the center agent and - # `occupant_id` refers to any agent already occupying the cell. - if "agent_id" in neighborhood.columns: - neighborhood = neighborhood.rename({"agent_id": "occupant_id"}) + # Neighborhood after sugar join: + # ┌────────────┬────────────┬────────┬────────────────┬────────────────┬────────┐ + # │ dim_0 ┆ dim_1 ┆ radius ┆ dim_0_center ┆ dim_1_center ┆ sugar │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞════════════╪════════════╪════════╪════════════════╪════════════════╪════════╡ + neighborhood = neighborhood.join( - center_lookup, on=["dim_0_center", "dim_1_center"], how="left" + current_pos, + left_on=["dim_0_center", "dim_1_center"], + right_on=["dim_0_center", "dim_1_center"], + how="left", ) - if "agent_id_right" in neighborhood.columns: - # Rename the joined center lookup's id to the canonical name. - neighborhood = neighborhood.rename({"agent_id_right": "agent_id"}) + + # Final neighborhood columns: + # ┌────────────┬────────────┬────────┬────────────────┬────────────────┬────────┬──────────┐ + # │ dim_0 ┆ dim_1 ┆ radius ┆ dim_0_center ┆ dim_1_center ┆ sugar ┆ agent_id │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u64 │ + # ╞════════════╪════════════╪════════╪════════════════╪════════════════╪════════╪══════════╡ # Create ranked choices per agent: sort by sugar (desc), radius # (asc), then coordinates. Keep the first unique entry per cell. + # choices columns (after select): + # ┌──────────┬────────────┬────────────┬────────┬────────┬────────────────┬────────────────┐ + # │ agent_id ┆ dim_0 ┆ dim_1 ┆ sugar ┆ radius ┆ dim_0_center ┆ dim_1_center │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════════╪════════════╪════════╪════════╪════════════════╪════════════════╡ choices = ( neighborhood.select( [ @@ -1007,7 +1028,13 @@ def move(self) -> None: return # Origins for fallback (if an agent exhausts candidates it stays put). - origins = center_lookup.select( + # origins columns: + # ┌──────────┬────────────┬────────────┐ + # │ agent_id ┆ dim_0 ┆ dim_1 │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════════╪════════════╡ + origins = current_pos.select( [ "agent_id", pl.col("dim_0_center").alias("dim_0"), @@ -1016,10 +1043,22 @@ def move(self) -> None: ) # Track the maximum available rank per agent to clamp promotions. + # max_rank columns: + # ┌──────────┬───────────┐ + # │ agent_id ┆ max_rank │ + # │ --- ┆ --- │ + # │ u64 ┆ i64 │ + # ╞══════════╪═══════════╡ max_rank = choices.group_by("agent_id").agg(pl.col("rank").max().alias("max_rank")) # Prepare unresolved agents and working tables. agent_ids = choices["agent_id"].unique(maintain_order=True) + # unresolved columns: + # ┌──────────┬────────────────┐ + # │ agent_id ┆ current_rank │ + # │ --- ┆ --- │ + # │ u64 ┆ i64 │ + # ╞══════════╪════════════════╡ unresolved = pl.DataFrame( { "agent_id": agent_ids, @@ -1027,6 +1066,12 @@ def move(self) -> None: } ) + # assigned columns: + # ┌──────────┬────────────┬────────────┐ + # │ agent_id ┆ dim_0 ┆ dim_1 │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════════╪════════════╡ assigned = pl.DataFrame( { "agent_id": pl.Series(name="agent_id", values=[], dtype=agent_ids.dtype), @@ -1035,6 +1080,12 @@ def move(self) -> None: } ) + # taken columns: + # ┌────────────┬────────────┐ + # │ dim_0 ┆ dim_1 │ + # │ --- ┆ --- │ + # │ i64 ┆ i64 │ + # ╞════════════╪════════════╡ taken = pl.DataFrame( { "dim_0": pl.Series(name="dim_0", values=[], dtype=pl.Int64), @@ -1046,6 +1097,12 @@ def move(self) -> None: # candidate; winners per-cell are selected at random and losers are # promoted to their next choice. while unresolved.height > 0: + # candidate_pool columns (after join with unresolved): + # ┌──────────┬────────────┬────────────┬────────┬────────┬────────────────┬────────────────┬──────────────┐ + # │ agent_id ┆ dim_0 ┆ dim_1 ┆ sugar ┆ radius ┆ dim_0_center ┆ dim_1_center ┆ current_rank │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════════╪════════════╪════════╪════════╪════════════════╪════════════════╪══════════════╡ candidate_pool = choices.join(unresolved, on="agent_id") candidate_pool = candidate_pool.filter(pl.col("rank") >= pl.col("current_rank")) if not taken.is_empty(): @@ -1053,6 +1110,12 @@ def move(self) -> None: if candidate_pool.is_empty(): # No available candidates — everyone falls back to origin. + # fallback columns: + # ┌──────────┬────────────┬────────────┬──────────────┐ + # │ agent_id ┆ dim_0 ┆ dim_1 ┆ current_rank │ + # │ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════════╪════════════╪══════════════╡ fallback = unresolved.join(origins, on="agent_id", how="left") assigned = pl.concat( [assigned, fallback.select(["agent_id", "dim_0", "dim_1"])], @@ -1060,13 +1123,26 @@ def move(self) -> None: ) break + # best_candidates columns (per agent first choice): + # ┌──────────┬────────────┬────────────┬────────┬────────┬────────────────┬────────────────┬──────────────┐ + # │ agent_id ┆ dim_0 ┆ dim_1 ┆ sugar ┆ radius ┆ dim_0_center ┆ dim_1_center ┆ current_rank │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════════╪════════════╪════════╪════════╪════════════════╪════════════════╪══════════════╡ best_candidates = ( candidate_pool.sort(["agent_id", "rank"]) .group_by("agent_id", maintain_order=True).first() ) # Agents that had no candidate this round fall back to origin. + # missing columns: + # ┌──────────┬──────────────┐ + # │ agent_id ┆ current_rank │ + # │ --- ┆ --- │ + # │ u64 ┆ i64 │ + # ╞══════════╪══════════════╡ missing = unresolved.join(best_candidates.select("agent_id"), on="agent_id", how="anti") if not missing.is_empty(): + # fallback (missing) columns match fallback table above. fallback = missing.join(origins, on="agent_id", how="left") assigned = pl.concat( [assigned, fallback.select(["agent_id", "dim_0", "dim_1"])], @@ -1083,6 +1159,12 @@ def move(self) -> None: lottery = pl.Series("lottery", self.random.random(best_candidates.height)) best_candidates = best_candidates.with_columns(lottery) + # winners columns: + # ┌──────────┬────────────┬────────────┬────────┬────────┬────────────────┬────────────────┬──────────────┬─────────┐ + # │ agent_id ┆ dim_0 ┆ dim_1 ┆ sugar ┆ radius ┆ dim_0_center ┆ dim_1_center ┆ current_rank ┆ lottery │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ f64 │ + # ╞══════════╪════════════╪════════════╪════════╪════════╪════════════════╪════════════════╪══════════════╪═════════╡ winners = ( best_candidates.sort(["dim_0", "dim_1", "lottery"]) .group_by(["dim_0", "dim_1"], maintain_order=True).first() ) @@ -1098,10 +1180,17 @@ def move(self) -> None: if unresolved.is_empty(): break + # loser candidates columns mirror best_candidates (minus winners). losers = best_candidates.join(winner_ids, on="agent_id", how="anti") if losers.is_empty(): continue + # loser_updates columns: + # ┌──────────┬───────────┬───────────┐ + # │ agent_id ┆ next_rank ┆ max_rank │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞══════════╪═══════════╪═══════════╡ loser_updates = ( losers.select( "agent_id", @@ -1115,6 +1204,7 @@ def move(self) -> None: ) # Promote losers' current_rank (if any) and continue. + # unresolved (updated) retains columns agent_id/current_rank. unresolved = unresolved.join(loser_updates, on="agent_id", how="left").with_columns( pl.when(pl.col("next_rank").is_not_null()) .then(pl.col("next_rank")) @@ -1125,6 +1215,12 @@ def move(self) -> None: if assigned.is_empty(): return + # move_df columns: + # ┌────────────┬────────────┬────────────┐ + # │ unique_id ┆ dim_0 ┆ dim_1 │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞════════════╪════════════╪════════════╡ move_df = pl.DataFrame( { "unique_id": assigned["agent_id"], @@ -1177,9 +1273,9 @@ def run_variant( # %% # %% -GRID_WIDTH = 250 -GRID_HEIGHT = 250 -NUM_AGENTS = 10000 +GRID_WIDTH = 40 +GRID_HEIGHT = 40 +NUM_AGENTS = 400 MODEL_STEPS = 60 MAX_SUGAR = 4 From 9edbfdb5770f18493c480562451bc3e136503286 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:24:37 +0200 Subject: [PATCH 176/329] refactor: improve agent movement logic and enhance readability in AntsParallel class --- .../general/user-guide/3_advanced_tutorial.py | 82 +++++++++++++------ 1 file changed, 55 insertions(+), 27 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 7b50564a..426fb7da 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -922,9 +922,12 @@ def move(self) -> None: class AntsParallel(AntsBase): def move(self) -> None: - """ - Parallel movement: each agent proposes a ranked list of visible cells (including its own). - We resolve conflicts in rounds using DataFrame operations so winners can be chosen per-cell at random and losers are promoted to their next-ranked choice. + """Move agents in parallel by ranking visible cells and resolving conflicts. + + Returns + ------- + None + Movement updates happen in-place on the underlying space. """ # Early exit if there are no agents. if len(self.df) == 0: @@ -944,6 +947,33 @@ def move(self) -> None: ] ) + neighborhood = self._build_neighborhood_frame(current_pos) + choices, origins, max_rank = self._rank_candidates(neighborhood, current_pos) + if choices.is_empty(): + return + + assigned = self._resolve_conflicts_in_rounds(choices, origins, max_rank) + if assigned.is_empty(): + return + + # move_df columns: + # ┌────────────┬────────────┬────────────┐ + # │ unique_id ┆ dim_0 ┆ dim_1 │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞════════════╪════════════╪════════════╡ + move_df = pl.DataFrame( + { + "unique_id": assigned["agent_id"], + "dim_0": assigned["dim_0"], + "dim_1": assigned["dim_1"], + } + ) + # `move_agents` accepts IdsLike and SpaceCoordinates (Polars Series/DataFrame), + # so pass Series/DataFrame directly rather than converting to Python lists. + self.space.move_agents(move_df["unique_id"], move_df.select(["dim_0", "dim_1"])) + + def _build_neighborhood_frame(self, current_pos: pl.DataFrame) -> pl.DataFrame: # Build a neighbourhood frame: for each agent and visible cell we # attach the cell sugar. The raw offsets contain the candidate # cell coordinates and the center coordinates for the sensing agent. @@ -984,7 +1014,13 @@ def move(self) -> None: # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ # │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u64 │ # ╞════════════╪════════════╪════════╪════════════════╪════════════════╪════════╪══════════╡ + return neighborhood + def _rank_candidates( + self, + neighborhood: pl.DataFrame, + current_pos: pl.DataFrame, + ) -> tuple[pl.DataFrame, pl.DataFrame, pl.DataFrame]: # Create ranked choices per agent: sort by sugar (desc), radius # (asc), then coordinates. Keep the first unique entry per cell. # choices columns (after select): @@ -1024,9 +1060,6 @@ def move(self) -> None: ) ) - if choices.is_empty(): - return - # Origins for fallback (if an agent exhausts candidates it stays put). # origins columns: # ┌──────────┬────────────┬────────────┐ @@ -1050,7 +1083,14 @@ def move(self) -> None: # │ u64 ┆ i64 │ # ╞══════════╪═══════════╡ max_rank = choices.group_by("agent_id").agg(pl.col("rank").max().alias("max_rank")) + return choices, origins, max_rank + def _resolve_conflicts_in_rounds( + self, + choices: pl.DataFrame, + origins: pl.DataFrame, + max_rank: pl.DataFrame, + ) -> pl.DataFrame: # Prepare unresolved agents and working tables. agent_ids = choices["agent_id"].unique(maintain_order=True) # unresolved columns: @@ -1130,7 +1170,10 @@ def move(self) -> None: # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ # ╞══════════╪════════════╪════════════╪════════╪════════╪════════════════╪════════════════╪══════════════╡ best_candidates = ( - candidate_pool.sort(["agent_id", "rank"]) .group_by("agent_id", maintain_order=True).first() + candidate_pool + .sort(["agent_id", "rank"]) + .group_by("agent_id", maintain_order=True) + .first() ) # Agents that had no candidate this round fall back to origin. @@ -1166,7 +1209,10 @@ def move(self) -> None: # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ f64 │ # ╞══════════╪════════════╪════════════╪════════╪════════╪════════════════╪════════════════╪══════════════╪═════════╡ winners = ( - best_candidates.sort(["dim_0", "dim_1", "lottery"]) .group_by(["dim_0", "dim_1"], maintain_order=True).first() + best_candidates + .sort(["dim_0", "dim_1", "lottery"]) + .group_by(["dim_0", "dim_1"], maintain_order=True) + .first() ) assigned = pl.concat( @@ -1212,25 +1258,7 @@ def move(self) -> None: .alias("current_rank") ).drop("next_rank") - if assigned.is_empty(): - return - - # move_df columns: - # ┌────────────┬────────────┬────────────┐ - # │ unique_id ┆ dim_0 ┆ dim_1 │ - # │ --- ┆ --- ┆ --- │ - # │ u64 ┆ i64 ┆ i64 │ - # ╞════════════╪════════════╪════════════╡ - move_df = pl.DataFrame( - { - "unique_id": assigned["agent_id"], - "dim_0": assigned["dim_0"], - "dim_1": assigned["dim_1"], - } - ) - # `move_agents` accepts IdsLike and SpaceCoordinates (Polars Series/DataFrame), - # so pass Series/DataFrame directly rather than converting to Python lists. - self.space.move_agents(move_df["unique_id"], move_df.select(["dim_0", "dim_1"])) + return assigned From 408e04074b139b416488f66dde12c8b346ccf9b5 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 00:03:07 +0200 Subject: [PATCH 177/329] refactor: update candidate dimension names for clarity in AntsParallel class --- .../general/user-guide/3_advanced_tutorial.py | 265 +++++++++++++----- 1 file changed, 194 insertions(+), 71 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 426fb7da..a5b53439 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -965,8 +965,8 @@ def move(self) -> None: move_df = pl.DataFrame( { "unique_id": assigned["agent_id"], - "dim_0": assigned["dim_0"], - "dim_1": assigned["dim_1"], + "dim_0": assigned["dim_0_candidate"], + "dim_1": assigned["dim_1_candidate"], } ) # `move_agents` accepts IdsLike and SpaceCoordinates (Polars Series/DataFrame), @@ -974,6 +974,21 @@ def move(self) -> None: self.space.move_agents(move_df["unique_id"], move_df.select(["dim_0", "dim_1"])) def _build_neighborhood_frame(self, current_pos: pl.DataFrame) -> pl.DataFrame: + """Assemble the sugar-weighted neighbourhood for each sensing agent. + + Parameters + ---------- + current_pos : pl.DataFrame + DataFrame with columns ``agent_id``, ``dim_0_center`` and + ``dim_1_center`` describing the current position of each agent. + + Returns + ------- + pl.DataFrame + DataFrame with columns ``agent_id``, ``radius``, ``dim_0_candidate``, + ``dim_1_candidate`` and ``sugar`` describing the visible cells for + each agent. + """ # Build a neighbourhood frame: for each agent and visible cell we # attach the cell sugar. The raw offsets contain the candidate # cell coordinates and the center coordinates for the sensing agent. @@ -983,25 +998,27 @@ def _build_neighborhood_frame(self, current_pos: pl.DataFrame) -> pl.DataFrame: # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ # │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ # ╞════════════╪════════════╪════════╪════════════════╪════════════════╡ - neighborhood = self.space.get_neighborhood( + neighborhood_cells = self.space.get_neighborhood( radius=self["vision"], agents=self, include_center=True ) - cell_props = self.space.cells.select(["dim_0", "dim_1", "sugar"]) - neighborhood = ( - neighborhood - .join(cell_props, on=["dim_0", "dim_1"], how="left") + # sugar_cells columns: + # ┌────────────┬────────────┬────────┐ + # │ dim_0 ┆ dim_1 ┆ sugar │ + # │ --- ┆ --- ┆ --- │ + # │ i64 ┆ i64 ┆ i64 │ + # ╞════════════╪════════════╪════════╡ + + sugar_cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) + + neighborhood_cells = ( + neighborhood_cells + .join(sugar_cells, on=["dim_0", "dim_1"], how="left") .with_columns(pl.col("sugar").fill_null(0)) + .rename({"dim_0": "dim_0_candidate", "dim_1": "dim_1_candidate"}) ) - # Neighborhood after sugar join: - # ┌────────────┬────────────┬────────┬────────────────┬────────────────┬────────┐ - # │ dim_0 ┆ dim_1 ┆ radius ┆ dim_0_center ┆ dim_1_center ┆ sugar │ - # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ - # │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ - # ╞════════════╪════════════╪════════╪════════════════╪════════════════╪════════╡ - - neighborhood = neighborhood.join( + neighborhood_cells = neighborhood_cells.join( current_pos, left_on=["dim_0_center", "dim_1_center"], right_on=["dim_0_center", "dim_1_center"], @@ -1009,45 +1026,74 @@ def _build_neighborhood_frame(self, current_pos: pl.DataFrame) -> pl.DataFrame: ) # Final neighborhood columns: - # ┌────────────┬────────────┬────────┬────────────────┬────────────────┬────────┬──────────┐ - # │ dim_0 ┆ dim_1 ┆ radius ┆ dim_0_center ┆ dim_1_center ┆ sugar ┆ agent_id │ - # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ - # │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u64 │ - # ╞════════════╪════════════╪════════╪════════════════╪════════════════╪════════╪══════════╡ - return neighborhood + # ┌──────────┬────────┬──────────────────┬──────────────────┬────────┐ + # │ agent_id ┆ radius ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════╪══════════════════╪══════════════════╪════════╡ + neighborhood_cells = ( + neighborhood_cells + .drop(["dim_0_center", "dim_1_center"]) + .select(["agent_id", "radius", "dim_0_candidate", "dim_1_candidate", "sugar"]) + ) + + return neighborhood_cells def _rank_candidates( self, neighborhood: pl.DataFrame, current_pos: pl.DataFrame, ) -> tuple[pl.DataFrame, pl.DataFrame, pl.DataFrame]: + """Rank candidate destination cells for each agent. + + Parameters + ---------- + neighborhood : pl.DataFrame + Output of :meth:`_build_neighborhood_frame` with columns + ``agent_id``, ``radius``, ``dim_0_candidate``, ``dim_1_candidate`` + and ``sugar``. + current_pos : pl.DataFrame + Frame with columns ``agent_id``, ``dim_0_center`` and + ``dim_1_center`` describing where each agent currently stands. + + Returns + ------- + choices : pl.DataFrame + Ranked candidates per agent with columns ``agent_id``, + ``dim_0_candidate``, ``dim_1_candidate``, ``sugar``, ``radius`` and + ``rank``. + origins : pl.DataFrame + Original coordinates per agent with columns ``agent_id``, + ``dim_0`` and ``dim_1``. + max_rank : pl.DataFrame + Maximum available rank per agent with columns ``agent_id`` and + ``max_rank``. + """ # Create ranked choices per agent: sort by sugar (desc), radius # (asc), then coordinates. Keep the first unique entry per cell. # choices columns (after select): - # ┌──────────┬────────────┬────────────┬────────┬────────┬────────────────┬────────────────┐ - # │ agent_id ┆ dim_0 ┆ dim_1 ┆ sugar ┆ radius ┆ dim_0_center ┆ dim_1_center │ - # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ - # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ - # ╞══════════╪════════════╪════════════╪════════╪════════╪════════════════╪════════════════╡ + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╡ choices = ( neighborhood.select( [ "agent_id", - "dim_0", - "dim_1", + "dim_0_candidate", + "dim_1_candidate", "sugar", "radius", - "dim_0_center", - "dim_1_center", ] ) - .with_columns(pl.col("radius").cast(pl.Int64)) + .with_columns(pl.col("radius")) .sort( - ["agent_id", "sugar", "radius", "dim_0", "dim_1"], + ["agent_id", "sugar", "radius", "dim_0_candidate", "dim_1_candidate"], descending=[False, True, False, False, False], ) .unique( - subset=["agent_id", "dim_0", "dim_1"], + subset=["agent_id", "dim_0_candidate", "dim_1_candidate"], keep="first", maintain_order=True, ) @@ -1055,7 +1101,6 @@ def _rank_candidates( pl.col("agent_id") .cum_count() .over("agent_id") - .cast(pl.Int64) .alias("rank") ) ) @@ -1091,8 +1136,30 @@ def _resolve_conflicts_in_rounds( origins: pl.DataFrame, max_rank: pl.DataFrame, ) -> pl.DataFrame: + """Resolve movement conflicts through iterative lottery rounds. + + Parameters + ---------- + choices : pl.DataFrame + Ranked candidate cells per agent with headers matching the + ``choices`` frame returned by :meth:`_rank_candidates`. + origins : pl.DataFrame + Agent origin coordinates with columns ``agent_id``, ``dim_0`` and + ``dim_1``. + max_rank : pl.DataFrame + Maximum rank offset per agent with columns ``agent_id`` and + ``max_rank``. + + Returns + ------- + pl.DataFrame + Allocated movements with columns ``agent_id``, ``dim_0_candidate`` + and ``dim_1_candidate``; each row records the destination assigned + to an agent. + """ # Prepare unresolved agents and working tables. agent_ids = choices["agent_id"].unique(maintain_order=True) + # unresolved columns: # ┌──────────┬────────────────┐ # │ agent_id ┆ current_rank │ @@ -1107,29 +1174,37 @@ def _resolve_conflicts_in_rounds( ) # assigned columns: - # ┌──────────┬────────────┬────────────┐ - # │ agent_id ┆ dim_0 ┆ dim_1 │ - # │ --- ┆ --- ┆ --- │ - # │ u64 ┆ i64 ┆ i64 │ - # ╞══════════╪════════════╪════════════╡ + # ┌──────────┬──────────────────┬──────────────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞══════════╪══════════════════╪══════════════════╡ assigned = pl.DataFrame( { "agent_id": pl.Series(name="agent_id", values=[], dtype=agent_ids.dtype), - "dim_0": pl.Series(name="dim_0", values=[], dtype=pl.Int64), - "dim_1": pl.Series(name="dim_1", values=[], dtype=pl.Int64), + "dim_0_candidate": pl.Series( + name="dim_0_candidate", values=[], dtype=pl.Int64 + ), + "dim_1_candidate": pl.Series( + name="dim_1_candidate", values=[], dtype=pl.Int64 + ), } ) # taken columns: - # ┌────────────┬────────────┐ - # │ dim_0 ┆ dim_1 │ - # │ --- ┆ --- │ - # │ i64 ┆ i64 │ - # ╞════════════╪════════════╡ + # ┌──────────────────┬──────────────────┐ + # │ dim_0_candidate ┆ dim_1_candidate │ + # │ --- ┆ --- │ + # │ i64 ┆ i64 │ + # ╞══════════════════╪══════════════════╡ taken = pl.DataFrame( { - "dim_0": pl.Series(name="dim_0", values=[], dtype=pl.Int64), - "dim_1": pl.Series(name="dim_1", values=[], dtype=pl.Int64), + "dim_0_candidate": pl.Series( + name="dim_0_candidate", values=[], dtype=pl.Int64 + ), + "dim_1_candidate": pl.Series( + name="dim_1_candidate", values=[], dtype=pl.Int64 + ), } ) @@ -1138,15 +1213,19 @@ def _resolve_conflicts_in_rounds( # promoted to their next choice. while unresolved.height > 0: # candidate_pool columns (after join with unresolved): - # ┌──────────┬────────────┬────────────┬────────┬────────┬────────────────┬────────────────┬──────────────┐ - # │ agent_id ┆ dim_0 ┆ dim_1 ┆ sugar ┆ radius ┆ dim_0_center ┆ dim_1_center ┆ current_rank │ - # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ - # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ - # ╞══════════╪════════════╪════════════╪════════╪════════╪════════════════╪════════════════╪══════════════╡ + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ current_rank │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════════════╡ candidate_pool = choices.join(unresolved, on="agent_id") candidate_pool = candidate_pool.filter(pl.col("rank") >= pl.col("current_rank")) if not taken.is_empty(): - candidate_pool = candidate_pool.join(taken, on=["dim_0", "dim_1"], how="anti") + candidate_pool = candidate_pool.join( + taken, + on=["dim_0_candidate", "dim_1_candidate"], + how="anti", + ) if candidate_pool.is_empty(): # No available candidates — everyone falls back to origin. @@ -1158,17 +1237,26 @@ def _resolve_conflicts_in_rounds( # ╞══════════╪════════════╪════════════╪══════════════╡ fallback = unresolved.join(origins, on="agent_id", how="left") assigned = pl.concat( - [assigned, fallback.select(["agent_id", "dim_0", "dim_1"])], + [ + assigned, + fallback.select( + [ + "agent_id", + pl.col("dim_0").alias("dim_0_candidate"), + pl.col("dim_1").alias("dim_1_candidate"), + ] + ), + ], how="vertical", ) break # best_candidates columns (per agent first choice): - # ┌──────────┬────────────┬────────────┬────────┬────────┬────────────────┬────────────────┬──────────────┐ - # │ agent_id ┆ dim_0 ┆ dim_1 ┆ sugar ┆ radius ┆ dim_0_center ┆ dim_1_center ┆ current_rank │ - # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ - # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ - # ╞══════════╪════════════╪════════════╪════════╪════════╪════════════════╪════════════════╪══════════════╡ + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ current_rank │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════════════╡ best_candidates = ( candidate_pool .sort(["agent_id", "rank"]) @@ -1188,10 +1276,30 @@ def _resolve_conflicts_in_rounds( # fallback (missing) columns match fallback table above. fallback = missing.join(origins, on="agent_id", how="left") assigned = pl.concat( - [assigned, fallback.select(["agent_id", "dim_0", "dim_1"])], + [ + assigned, + fallback.select( + [ + "agent_id", + pl.col("dim_0").alias("dim_0_candidate"), + pl.col("dim_1").alias("dim_1_candidate"), + ] + ), + ], + how="vertical", + ) + taken = pl.concat( + [ + taken, + fallback.select( + [ + pl.col("dim_0").alias("dim_0_candidate"), + pl.col("dim_1").alias("dim_1_candidate"), + ] + ), + ], how="vertical", ) - taken = pl.concat([taken, fallback.select(["dim_0", "dim_1"])], how="vertical") unresolved = unresolved.join(missing.select("agent_id"), on="agent_id", how="anti") best_candidates = best_candidates.join(missing.select("agent_id"), on="agent_id", how="anti") if unresolved.is_empty() or best_candidates.is_empty(): @@ -1203,23 +1311,38 @@ def _resolve_conflicts_in_rounds( best_candidates = best_candidates.with_columns(lottery) # winners columns: - # ┌──────────┬────────────┬────────────┬────────┬────────┬────────────────┬────────────────┬──────────────┬─────────┐ - # │ agent_id ┆ dim_0 ┆ dim_1 ┆ sugar ┆ radius ┆ dim_0_center ┆ dim_1_center ┆ current_rank ┆ lottery │ - # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ - # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ f64 │ - # ╞══════════╪════════════╪════════════╪════════╪════════╪════════════════╪════════════════╪══════════════╪═════════╡ + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────────────┬─────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ current_rank │ lottery │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ f64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════════════╪═════════╡ winners = ( best_candidates - .sort(["dim_0", "dim_1", "lottery"]) - .group_by(["dim_0", "dim_1"], maintain_order=True) + .sort(["dim_0_candidate", "dim_1_candidate", "lottery"]) + .group_by(["dim_0_candidate", "dim_1_candidate"], maintain_order=True) .first() ) assigned = pl.concat( - [assigned, winners.select(["agent_id", "dim_0", "dim_1"])], + [ + assigned, + winners.select( + [ + "agent_id", + pl.col("dim_0_candidate"), + pl.col("dim_1_candidate"), + ] + ), + ], + how="vertical", + ) + taken = pl.concat( + [ + taken, + winners.select(["dim_0_candidate", "dim_1_candidate"]), + ], how="vertical", ) - taken = pl.concat([taken, winners.select(["dim_0", "dim_1"])], how="vertical") winner_ids = winners.select("agent_id") unresolved = unresolved.join(winner_ids, on="agent_id", how="anti") From 0f966538e17c8372c66eeca49b01c7fc61978451 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 00:37:07 +0200 Subject: [PATCH 178/329] refactor: enhance comments for clarity and understanding in AntsParallel class --- .../general/user-guide/3_advanced_tutorial.py | 60 ++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index a5b53439..7cf8252c 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -1105,6 +1105,13 @@ def _rank_candidates( ) ) + # Precompute per‑agent candidate rank once so conflict resolution can + # promote losers by incrementing a cheap `current_rank` counter, + # without re-sorting after each round. Alternative: drop taken cells + # and re-rank by sugar every round; simpler conceptually but requires + # repeated sorts and deduplication, which is heavier than filtering by + # `rank >= current_rank`. + # Origins for fallback (if an agent exhausts candidates it stays put). # origins columns: # ┌──────────┬────────────┬────────────┐ @@ -1121,11 +1128,14 @@ def _rank_candidates( ) # Track the maximum available rank per agent to clamp promotions. + # This bounds `current_rank`; once an agent reaches `max_rank` and + # cannot secure a cell, they fall back to origin cleanly instead of + # chasing nonexistent ranks. # max_rank columns: # ┌──────────┬───────────┐ # │ agent_id ┆ max_rank │ # │ --- ┆ --- │ - # │ u64 ┆ i64 │ + # │ u64 ┆ u32 │ # ╞══════════╪═══════════╡ max_rank = choices.group_by("agent_id").agg(pl.col("rank").max().alias("max_rank")) return choices, origins, max_rank @@ -1212,12 +1222,16 @@ def _resolve_conflicts_in_rounds( # candidate; winners per-cell are selected at random and losers are # promoted to their next choice. while unresolved.height > 0: + # Using precomputed `rank` lets us select candidates with + # `rank >= current_rank` and avoid re-ranking after each round. + # Alternative: remove taken cells and re-sort remaining candidates + # by sugar/distance per round (heavier due to repeated sort/dedupe). # candidate_pool columns (after join with unresolved): - # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────────────┐ - # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ current_rank │ - # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ - # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ - # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════════════╡ + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────┬──────────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ rank ┆ current_rank │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u32 ┆ i64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════╪══════════════╡ candidate_pool = choices.join(unresolved, on="agent_id") candidate_pool = candidate_pool.filter(pl.col("rank") >= pl.col("current_rank")) if not taken.is_empty(): @@ -1229,6 +1243,8 @@ def _resolve_conflicts_in_rounds( if candidate_pool.is_empty(): # No available candidates — everyone falls back to origin. + # Note: this covers both agents with no visible cells left and + # the case where all remaining candidates are already taken. # fallback columns: # ┌──────────┬────────────┬────────────┬──────────────┐ # │ agent_id ┆ dim_0 ┆ dim_1 ┆ current_rank │ @@ -1252,11 +1268,11 @@ def _resolve_conflicts_in_rounds( break # best_candidates columns (per agent first choice): - # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────────────┐ - # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ current_rank │ - # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ - # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ - # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════════════╡ + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────┬──────────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ rank ┆ current_rank │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u32 ┆ i64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════╪══════════════╡ best_candidates = ( candidate_pool .sort(["agent_id", "rank"]) @@ -1311,11 +1327,11 @@ def _resolve_conflicts_in_rounds( best_candidates = best_candidates.with_columns(lottery) # winners columns: - # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────────────┬─────────┐ - # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ current_rank │ lottery │ - # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ - # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ f64 │ - # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════════════╪═════════╡ + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────┬──────────────┬─────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ rank ┆ current_rank │ lottery │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u32 ┆ i64 ┆ f64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════╪══════════════╪═════════╡ winners = ( best_candidates .sort(["dim_0_candidate", "dim_1_candidate", "lottery"]) @@ -1354,12 +1370,12 @@ def _resolve_conflicts_in_rounds( if losers.is_empty(): continue - # loser_updates columns: - # ┌──────────┬───────────┬───────────┐ - # │ agent_id ┆ next_rank ┆ max_rank │ - # │ --- ┆ --- ┆ --- │ - # │ u64 ┆ i64 ┆ i64 │ - # ╞══════════╪═══════════╪═══════════╡ + # loser_updates columns (after select): + # ┌──────────┬───────────┐ + # │ agent_id ┆ next_rank │ + # │ --- ┆ --- │ + # │ u64 ┆ i64 │ + # ╞══════════╪═══════════╡ loser_updates = ( losers.select( "agent_id", From 5e2ce8724a2c964c73b5a97e10a84e6a1f99ad55 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:07:49 +0200 Subject: [PATCH 179/329] refactor: streamline model variant execution and improve readability in advanced tutorial --- .../general/user-guide/3_advanced_tutorial.py | 187 +++++++----------- 1 file changed, 72 insertions(+), 115 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 7cf8252c..3ff67723 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -1429,17 +1429,12 @@ def run_variant( # %% [markdown] """ -## 7. Run the Sequential Model (Python loop) +## 7. Run the Model Variants -With the scaffolding in place we can simulate the sequential version and inspect -its aggregate behaviour. Because all random draws flow through the model's RNG, -constructing each variant with the same seed reproduces identical initial -conditions across the different movement rules. +We iterate over each movement policy with a shared helper so all runs reuse the same seed. Set `MESA_FRAMES_RUN_SEQUENTIAL=1` to include the slower pure-Python baseline. """ # %% - -# %% GRID_WIDTH = 40 GRID_HEIGHT = 40 NUM_AGENTS = 400 @@ -1458,112 +1453,59 @@ def run_variant( sequential_seed = 11 -if RUN_SEQUENTIAL: - sequential_model, sequential_time = run_variant( - AntsSequential, steps=MODEL_STEPS, seed=sequential_seed - ) - - seq_model_frame = sequential_model.datacollector.data["model"] - print("Sequential aggregate trajectory (last 5 steps):") - print( - seq_model_frame.select( - ["step", "mean_sugar", "total_sugar", "agents_alive"] - ).tail(5) - ) - print(f"Sequential runtime: {sequential_time:.3f} s") -else: - sequential_model = None - seq_model_frame = pl.DataFrame() - sequential_time = float("nan") - print( - "Skipping sequential baseline; set MESA_FRAMES_RUN_SEQUENTIAL=1 to enable it." - ) - -# %% [markdown] -""" -## 8. Run the Numba-Accelerated Model - -We reuse the same seed so the only difference is the compiled movement helper. -The trajectory matches the pure Python loop (up to floating-point noise) while -running much faster on larger grids. -""" - -# %% -numba_model, numba_time = run_variant( - AntsNumba, steps=MODEL_STEPS, seed=sequential_seed -) - -numba_model_frame = numba_model.datacollector.data["model"] -print("Numba sequential aggregate trajectory (last 5 steps):") -print( - numba_model_frame.select(["step", "mean_sugar", "total_sugar", "agents_alive"]).tail(5) -) -print(f"Numba sequential runtime: {numba_time:.3f} s") - -# %% [markdown] -""" -## 9. Run the Simultaneous Model - -Next we instantiate the parallel variant with the same seed so every run starts -from the common state generated by the helper methods. -""" +variant_specs: dict[str, tuple[type[AntsBase], bool]] = { + "Sequential (Python loop)": (AntsSequential, RUN_SEQUENTIAL), + "Sequential (Numba)": (AntsNumba, True), + "Parallel (Polars)": (AntsParallel, True), +} -# %% -parallel_model, parallel_time = run_variant( - AntsParallel, steps=MODEL_STEPS, seed=sequential_seed -) +models: dict[str, Sugarscape] = {} +frames: dict[str, pl.DataFrame] = {} +runtimes: dict[str, float] = {} -par_model_frame = parallel_model.datacollector.data["model"] -print("Parallel aggregate trajectory (last 5 steps):") -print(par_model_frame.select(["step", "mean_sugar", "total_sugar", "agents_alive"]).tail(5)) -print(f"Parallel runtime: {parallel_time:.3f} s") +for variant_name, (agent_cls, enabled) in variant_specs.items(): + if not enabled: + print( + f"Skipping {variant_name}; set MESA_FRAMES_RUN_SEQUENTIAL=1 to enable it." + ) + runtimes[variant_name] = float("nan") + continue -# %% [markdown] -""" -## 10. Runtime Comparison + model, runtime = run_variant(agent_cls, steps=MODEL_STEPS, seed=sequential_seed) + models[variant_name] = model + frames[variant_name] = model.datacollector.data["model"] + runtimes[variant_name] = runtime -The table below summarises the elapsed time for 60 steps on the 50×50 grid with -400 ants. Parallel scheduling on top of Polars lands in the same performance -band as the Numba-accelerated loop, while both are far faster than the pure -Python baseline. -""" - -# %% -runtime_rows: list[dict[str, float | str]] = [] -if RUN_SEQUENTIAL: - runtime_rows.append( - { - "update_rule": "Sequential (Python loop)", - "runtime_seconds": sequential_time, - } - ) -else: - runtime_rows.append( - { - "update_rule": "Sequential (Python loop) [skipped]", - "runtime_seconds": float("nan"), - } + print(f"{variant_name} aggregate trajectory (last 5 steps):") + print( + frames[variant_name] + .select(["step", "mean_sugar", "total_sugar", "agents_alive"]) + .tail(5) ) + print(f"{variant_name} runtime: {runtime:.3f} s") + print() -runtime_rows.extend( - [ - { - "update_rule": "Sequential (Numba)", - "runtime_seconds": numba_time, - }, - { - "update_rule": "Parallel (Polars)", - "runtime_seconds": parallel_time, - }, - ] -) - -runtime_table = pl.DataFrame(runtime_rows).with_columns( - pl.col("runtime_seconds").round(4) +runtime_table = ( + pl.DataFrame( + [ + { + "update_rule": variant_name if enabled else f"{variant_name} [skipped]", + "runtime_seconds": runtimes.get(variant_name, float("nan")), + } + for variant_name, (_, enabled) in variant_specs.items() + ] + ) + .with_columns(pl.col("runtime_seconds").round(4)) + .sort("runtime_seconds", descending=False, nulls_last=True) ) +print("Runtime comparison (fastest first):") print(runtime_table) +# Access models/frames on demand; keep namespace minimal. +numba_model_frame = frames.get("Sequential (Numba)", pl.DataFrame()) +par_model_frame = frames.get("Parallel (Polars)", pl.DataFrame()) + # %% [markdown] """ Polars gives us that performance without any bespoke compiled kernels—the move @@ -1575,7 +1517,7 @@ def run_variant( # %% [markdown] """ -## 11. Comparing the Update Rules +## 8. Comparing the Update Rules Even though the micro rules differ, the aggregate trajectories keep the same overall shape: sugar holdings trend upward while the population tapers off. By @@ -1606,20 +1548,20 @@ def run_variant( [ { "update_rule": "Sequential (Numba)", - "gini": gini(numba_model), - "corr_sugar_metabolism": corr_sugar_metabolism(numba_model), - "corr_sugar_vision": corr_sugar_vision(numba_model), - "agents_alive": float(len(numba_model.sets[0])) - if len(numba_model.sets) + "gini": gini(models["Sequential (Numba)"]), + "corr_sugar_metabolism": corr_sugar_metabolism(models["Sequential (Numba)"]), + "corr_sugar_vision": corr_sugar_vision(models["Sequential (Numba)"]), + "agents_alive": float(len(models["Sequential (Numba)"].sets[0])) + if len(models["Sequential (Numba)"].sets) else 0.0, }, { "update_rule": "Parallel (random tie-break)", - "gini": gini(parallel_model), - "corr_sugar_metabolism": corr_sugar_metabolism(parallel_model), - "corr_sugar_vision": corr_sugar_vision(parallel_model), - "agents_alive": float(len(parallel_model.sets[0])) - if len(parallel_model.sets) + "gini": gini(models["Parallel (Polars)"]), + "corr_sugar_metabolism": corr_sugar_metabolism(models["Parallel (Polars)"]), + "corr_sugar_vision": corr_sugar_vision(models["Parallel (Polars)"]), + "agents_alive": float(len(models["Parallel (Polars)"].sets[0])) + if len(models["Parallel (Polars)"].sets) else 0.0, }, ] @@ -1644,7 +1586,22 @@ def run_variant( # %% [markdown] """ -## 12. Where to Go Next? +The section above demonstrated how we can iterate across variants inside a single code cell +without sprinkling the global namespace with per‑variant variables like +`sequential_model`, `seq_model_frame`, etc. Instead we retained compact dictionaries: + +``models[name]`` -> Sugarscape instance +``frames[name]`` -> model-level DataFrame trace +``runtimes[name]`` -> wall time in seconds + +This keeps the tutorial easier to skim and copy/paste for users who only want one +variant. The minimal convenience aliases (`numba_model`, `parallel_model`) exist solely +for the comparison section; feel free to inline those if further slimming is desired. +""" + +# %% [markdown] +""" +## 9. Where to Go Next? * **Polars + LazyFrames roadmap** – future mesa-frames releases will expose LazyFrame-powered schedulers (with GPU offloading hooks), so the same Polars From 34c2fd8427c81662e6aeb28803c9fd9e5b2132ce Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:12:07 +0200 Subject: [PATCH 180/329] refactor: update metrics table construction for clarity and consistency in advanced tutorial --- .../general/user-guide/3_advanced_tutorial.py | 86 ++++++++++--------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 3ff67723..5e04172f 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -1544,28 +1544,45 @@ def run_variant( print("Step-level absolute differences (first 10 steps):") print(comparison.select(["step", "mean_diff", "total_diff", "count_diff"]).head(10)) -metrics_table = pl.DataFrame( - [ - { - "update_rule": "Sequential (Numba)", - "gini": gini(models["Sequential (Numba)"]), - "corr_sugar_metabolism": corr_sugar_metabolism(models["Sequential (Numba)"]), - "corr_sugar_vision": corr_sugar_vision(models["Sequential (Numba)"]), - "agents_alive": float(len(models["Sequential (Numba)"].sets[0])) - if len(models["Sequential (Numba)"].sets) - else 0.0, - }, - { - "update_rule": "Parallel (random tie-break)", - "gini": gini(models["Parallel (Polars)"]), - "corr_sugar_metabolism": corr_sugar_metabolism(models["Parallel (Polars)"]), - "corr_sugar_vision": corr_sugar_vision(models["Parallel (Polars)"]), - "agents_alive": float(len(models["Parallel (Polars)"].sets[0])) - if len(models["Parallel (Polars)"].sets) - else 0.0, - }, - ] -) +# Build the steady‑state metrics table from the DataCollector output rather than +# recomputing reporters directly on the model objects. The collector already +# stored the model‑level reporters (gini, correlations, etc.) every step. +def _last_row(df: pl.DataFrame) -> pl.DataFrame: + if df.is_empty(): + return df + # Ensure we take the final time step in case steps < MODEL_STEPS due to extinction. + return df.sort("step").tail(1) + +numba_last = _last_row(frames.get("Sequential (Numba)", pl.DataFrame())) +parallel_last = _last_row(frames.get("Parallel (Polars)", pl.DataFrame())) + +metrics_pieces: list[pl.DataFrame] = [] +if not numba_last.is_empty(): + metrics_pieces.append( + numba_last.select( + [ + pl.lit("Sequential (Numba)").alias("update_rule"), + "gini", + "corr_sugar_metabolism", + "corr_sugar_vision", + pl.col("agents_alive"), + ] + ) + ) +if not parallel_last.is_empty(): + metrics_pieces.append( + parallel_last.select( + [ + pl.lit("Parallel (random tie-break)").alias("update_rule"), + "gini", + "corr_sugar_metabolism", + "corr_sugar_vision", + pl.col("agents_alive"), + ] + ) + ) + +metrics_table = pl.concat(metrics_pieces, how="vertical") if metrics_pieces else pl.DataFrame() print("\nSteady-state inequality metrics:") print( @@ -1580,24 +1597,15 @@ def run_variant( ) ) -numba_gini = metrics_table.filter(pl.col("update_rule") == "Sequential (Numba)")["gini"][0] -par_gini = metrics_table.filter(pl.col("update_rule") == "Parallel (random tie-break)")["gini"][0] -print(f"Absolute Gini gap (numba vs parallel): {abs(numba_gini - par_gini):.4f}") +# Note: The steady-state rows above are extracted directly from the DataCollector's +# model-level frame (last available step for each variant). We avoid recomputing +# metrics on the live model objects to ensure consistency with any user-defined +# reporters that might add transformations or post-processing in future. -# %% [markdown] -""" -The section above demonstrated how we can iterate across variants inside a single code cell -without sprinkling the global namespace with per‑variant variables like -`sequential_model`, `seq_model_frame`, etc. Instead we retained compact dictionaries: - -``models[name]`` -> Sugarscape instance -``frames[name]`` -> model-level DataFrame trace -``runtimes[name]`` -> wall time in seconds - -This keeps the tutorial easier to skim and copy/paste for users who only want one -variant. The minimal convenience aliases (`numba_model`, `parallel_model`) exist solely -for the comparison section; feel free to inline those if further slimming is desired. -""" +if metrics_table.height >= 2: + numba_gini = metrics_table.filter(pl.col("update_rule") == "Sequential (Numba)")["gini"][0] + par_gini = metrics_table.filter(pl.col("update_rule") == "Parallel (random tie-break)")["gini"][0] + print(f"Absolute Gini gap (numba vs parallel): {abs(numba_gini - par_gini):.4f}") # %% [markdown] """ From b5cf869949117ddba0c9bfa45632616dc20fbb29 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:19:00 +0200 Subject: [PATCH 181/329] refactor: update section headings for clarity and consistency in advanced tutorial --- .../general/user-guide/3_advanced_tutorial.py | 63 ++++++------------- 1 file changed, 18 insertions(+), 45 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 5e04172f..fade7bc3 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -911,7 +911,7 @@ def move(self) -> None: # %% [markdown] """ -### 3.5. Simultaneous Movement with Conflict Resolution (the Polars mesa-frames idiomatic way) +### 3.5 Simultaneous Movement with Conflict Resolution (the Polars mesa-frames idiomatic way) The previous implementation is optimal speed-wise but it's a bit low-level. It requires mantaining an occupancy grid and imperative loops and it might become tricky to extend with more complex movement rules or models. To stay in mesa-frames idiom, we can implement a parallel movement policy that uses Polars DataFrame operations to resolve conflicts when multiple agents target the same cell. @@ -1403,12 +1403,19 @@ def _resolve_conflicts_in_rounds( # %% [markdown] """ -## 6. Shared Model Infrastructure +## 4. Run the Model Variants + +We iterate over each movement policy with a shared helper so all runs reuse the same seed. Set `MESA_FRAMES_RUN_SEQUENTIAL=1` to include the slower pure-Python baseline. -`SugarscapeTutorialModel` wires the grid, agent set, regrowth logic, and data -collection. Each variant simply plugs in a different agent class. """ +GRID_WIDTH = 40 +GRID_HEIGHT = 40 +NUM_AGENTS = 400 +MODEL_STEPS = 60 +MAX_SUGAR = 4 +SEED = 42 + def run_variant( agent_cls: type[AntsBase], *, @@ -1427,20 +1434,6 @@ def run_variant( model.run(steps) return model, perf_counter() - start -# %% [markdown] -""" -## 7. Run the Model Variants - -We iterate over each movement policy with a shared helper so all runs reuse the same seed. Set `MESA_FRAMES_RUN_SEQUENTIAL=1` to include the slower pure-Python baseline. -""" - -# %% -GRID_WIDTH = 40 -GRID_HEIGHT = 40 -NUM_AGENTS = 400 -MODEL_STEPS = 60 -MAX_SUGAR = 4 - # Allow quick testing by skipping the slow pure-Python sequential baseline. # Set the environment variable ``MESA_FRAMES_RUN_SEQUENTIAL=0`` (or "false") # to disable the baseline when running this script. @@ -1451,7 +1444,6 @@ def run_variant( "off", } -sequential_seed = 11 variant_specs: dict[str, tuple[type[AntsBase], bool]] = { "Sequential (Python loop)": (AntsSequential, RUN_SEQUENTIAL), @@ -1471,7 +1463,7 @@ def run_variant( runtimes[variant_name] = float("nan") continue - model, runtime = run_variant(agent_cls, steps=MODEL_STEPS, seed=sequential_seed) + model, runtime = run_variant(agent_cls, steps=MODEL_STEPS, seed=SEED) models[variant_name] = model frames[variant_name] = model.datacollector.data["model"] runtimes[variant_name] = runtime @@ -1506,18 +1498,10 @@ def run_variant( numba_model_frame = frames.get("Sequential (Numba)", pl.DataFrame()) par_model_frame = frames.get("Parallel (Polars)", pl.DataFrame()) -# %% [markdown] -""" -Polars gives us that performance without any bespoke compiled kernels—the move -logic reads like ordinary DataFrame code. The Numba version is a touch faster, -but only after writing and maintaining `_numba_find_best_cell` and friends. In -practice we get near-identical runtimes, so you can pick the implementation that -is simplest for your team. -""" # %% [markdown] """ -## 8. Comparing the Update Rules +## 5. Comparing the Update Rules Even though the micro rules differ, the aggregate trajectories keep the same overall shape: sugar holdings trend upward while the population tapers off. By @@ -1609,22 +1593,11 @@ def _last_row(df: pl.DataFrame) -> pl.DataFrame: # %% [markdown] """ -## 9. Where to Go Next? +## 6. Where to Go Next? + +Currently, the Polars implementation spends most of the time in join operations. -* **Polars + LazyFrames roadmap** – future mesa-frames releases will expose - LazyFrame-powered schedulers (with GPU offloading hooks), so the same Polars +**Polars + LazyFrames roadmap** – future mesa-frames releases will expose + LazyFrame-powered sets and spaces (which can also use a GPU cuda accelerated backend which greatly accelerates joins), so the same Polars code you wrote here will scale even further without touching Numba. -* **Production reference** – the `examples/sugarscape_ig/ss_polars` package - shows how to take this pattern further with additional vectorisation tricks. -* **Alternative conflict rules** – it is straightforward to swap in other - tie-breakers, such as letting losing agents search for the next-best empty - cell rather than staying put. -* **Macro validation** – wrap the metric collection in a loop over seeds to - quantify how small the Gini gap remains across independent replications. -* **Statistical physics meets ABM** – for a modern take on the macro behaviour - of Sugarscape-like economies, see Axtell (2000) or subsequent statistical - physics treatments of wealth exchange models. - -Because this script doubles as the notebook source, any edits you make here can -be synchronised with a `.ipynb` representation via Jupytext. """ From df89feaac3f7fef745ec1308e9535cc7a32f59a6 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:28:08 +0200 Subject: [PATCH 182/329] refactor: improve clarity and conciseness in advanced tutorial section on update rules and next steps --- .../general/user-guide/3_advanced_tutorial.py | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index fade7bc3..bcd07aed 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -1503,15 +1503,10 @@ def run_variant( """ ## 5. Comparing the Update Rules -Even though the micro rules differ, the aggregate trajectories keep the same -overall shape: sugar holdings trend upward while the population tapers off. By -joining the model-level traces we can quantify how conflict resolution -randomness introduces modest deviations (for example, the simultaneous variant -often retires a few more agents when several conflicts pile up in the same -neighbourhood). Crucially, the steady-state inequality metrics line up: the Gini -coefficients differ by roughly 0.0015 and the wealth–trait correlations are -indistinguishable, which validates the relaxed, fully-parallel update scheme. -""" +Even though micro rules differ, aggregate trajectories remain qualitatively similar (sugar trends up while population gradually declines). +When we join the traces step-by-step, we see small but noticeable deviations introduced by synchronous conflict resolution (e.g., a few more retirements when conflicts cluster). +In our run (seed=42), the final-step Gini differs by ≈0.005, and wealth–trait correlations match within ~1e-3. +These gaps vary by seed and grid size, but they consistently stay modest, supporting the relaxed parallel update as a faithful macro-level approximation.""" # %% comparison = numba_model_frame.select(["step", "mean_sugar", "total_sugar", "agents_alive"]).join( @@ -1581,11 +1576,6 @@ def _last_row(df: pl.DataFrame) -> pl.DataFrame: ) ) -# Note: The steady-state rows above are extracted directly from the DataCollector's -# model-level frame (last available step for each variant). We avoid recomputing -# metrics on the live model objects to ensure consistency with any user-defined -# reporters that might add transformations or post-processing in future. - if metrics_table.height >= 2: numba_gini = metrics_table.filter(pl.col("update_rule") == "Sequential (Numba)")["gini"][0] par_gini = metrics_table.filter(pl.col("update_rule") == "Parallel (random tie-break)")["gini"][0] @@ -1593,7 +1583,12 @@ def _last_row(df: pl.DataFrame) -> pl.DataFrame: # %% [markdown] """ -## 6. Where to Go Next? +## 6. Takeaways and Next Steps + +Some final notes: +- mesa-frames should preferably be used when you have many agents and operations can be vectorized. +- If your model is not easily vectorizable, consider using Numba or reducing your microscopic rule to a vectorizable form. As we saw, the macroscopic behavior can remain consistent (and be more similar to real-world systems). + Currently, the Polars implementation spends most of the time in join operations. @@ -1601,3 +1596,4 @@ def _last_row(df: pl.DataFrame) -> pl.DataFrame: LazyFrame-powered sets and spaces (which can also use a GPU cuda accelerated backend which greatly accelerates joins), so the same Polars code you wrote here will scale even further without touching Numba. """ + From 9eb2457ee60b0cb85158be0a6f08cf222f77c458 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:28:25 +0200 Subject: [PATCH 183/329] refactor: remove unnecessary newline at the end of the file in advanced tutorial --- docs/general/user-guide/3_advanced_tutorial.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index bcd07aed..a026ed2f 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -1595,5 +1595,4 @@ def _last_row(df: pl.DataFrame) -> pl.DataFrame: **Polars + LazyFrames roadmap** – future mesa-frames releases will expose LazyFrame-powered sets and spaces (which can also use a GPU cuda accelerated backend which greatly accelerates joins), so the same Polars code you wrote here will scale even further without touching Numba. -""" - +""" \ No newline at end of file From 92db3bebcd8066ff12b9ce048beafb2d017bdfa0 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:40:32 +0200 Subject: [PATCH 184/329] fix: update link for Advanced Tutorial to point to the correct notebook file --- mkdocs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs.yml b/mkdocs.yml index 0e55fd49..331165b5 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -112,7 +112,7 @@ nav: - Classes: user-guide/1_classes.md - Introductory Tutorial: user-guide/2_introductory-tutorial.ipynb - Data Collector Tutorial: user-guide/4_datacollector.ipynb - - Advanced Tutorial: user-guide/3_advanced-tutorial.md + - Advanced Tutorial: user-guide/3_advanced-tutorial.ipynb - Benchmarks: user-guide/5_benchmarks.md - API Reference: api/index.html - Contributing: From 7c2645e2713a90dbc13d3d7246252f2363caa993 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:41:07 +0200 Subject: [PATCH 185/329] feat: add jupytext dependency for enhanced notebook support --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 99b15899..8ecbc911 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -80,6 +80,7 @@ docs = [ "seaborn>=0.13.2", "sphinx-autobuild>=2025.8.25", "mesa>=3.2.0", + "jupytext>=1.17.3", ] # dev = test ∪ docs ∪ extra tooling From 511e3030d597900182b1c83282c97549997a4623 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:42:43 +0200 Subject: [PATCH 186/329] refactor: remove Jupyter metadata and clean up markdown cells in advanced tutorial --- docs/general/user-guide/3_advanced_tutorial.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index a026ed2f..59382383 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -1,15 +1,5 @@ from __future__ import annotations -# --- -# jupyter: -# jupytext: -# formats: py:percent,ipynb -# kernelspec: -# display_name: Python 3 (uv) -# language: python -# name: python3 -# --- - # %% [markdown] """ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/projectmesa/mesa-frames/blob/main/docs/general/user-guide/3_advanced_tutorial.ipynb) @@ -711,7 +701,7 @@ def move(self) -> None: Numba compiles numerical Python code to fast machine code at runtime. To use Numba, we need to rewrite the movement logic in a way that is compatible with Numba's restrictions (using tightly typed numpy arrays and accessing data indexes directly). """ - +# %% @njit(cache=True) def _numba_should_replace( best_sugar: int, @@ -919,6 +909,7 @@ def move(self) -> None: This implementation is a tad slower but still efficient and easier to read (for a Polars user). """ +# %% class AntsParallel(AntsBase): def move(self) -> None: @@ -1409,6 +1400,8 @@ def _resolve_conflicts_in_rounds( """ +# %% + GRID_WIDTH = 40 GRID_HEIGHT = 40 NUM_AGENTS = 400 From f37b61bf9fb1b24aed412aee9d7e1049d713ee22 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:44:59 +0200 Subject: [PATCH 187/329] feat: add step to convert tutorial .py scripts to notebooks in CI workflow --- .github/workflows/docs-gh-pages.yml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docs-gh-pages.yml b/.github/workflows/docs-gh-pages.yml index 435af957..ae6974e5 100644 --- a/.github/workflows/docs-gh-pages.yml +++ b/.github/workflows/docs-gh-pages.yml @@ -26,11 +26,20 @@ jobs: uv pip install --system . uv pip install --group docs --system + - name: Convert tutorial .py scripts to notebooks + run: | + set -euxo pipefail + for nb in docs/general/*.ipynb; do + echo "Executing $nb" + uv run jupyter nbconvert --to notebook --execute --inplace "$nb" + done + + - name: Build MkDocs site (general documentation) - run: mkdocs build --config-file mkdocs.yml --site-dir ./site + run: uv run mkdocs build --config-file mkdocs.yml --site-dir ./site - name: Build Sphinx docs (API documentation) - run: sphinx-build -b html docs/api site/api + run: uv run sphinx-build -b html docs/api site/api - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v4 From 284a991b7affc443f9bf39f86a29ea09a14b5e7a Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:45:03 +0200 Subject: [PATCH 188/329] feat: add jupytext dependency for enhanced notebook support in development and documentation environments --- uv.lock | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/uv.lock b/uv.lock index a72164c0..4e4d7e1d 100644 --- a/uv.lock +++ b/uv.lock @@ -1234,6 +1234,7 @@ dependencies = [ dev = [ { name = "autodocsumm" }, { name = "beartype" }, + { name = "jupytext" }, { name = "mesa" }, { name = "mkdocs-git-revision-date-localized-plugin" }, { name = "mkdocs-include-markdown-plugin" }, @@ -1258,6 +1259,7 @@ dev = [ docs = [ { name = "autodocsumm" }, { name = "beartype" }, + { name = "jupytext" }, { name = "mesa" }, { name = "mkdocs-git-revision-date-localized-plugin" }, { name = "mkdocs-include-markdown-plugin" }, @@ -1296,6 +1298,7 @@ requires-dist = [ dev = [ { name = "autodocsumm", specifier = ">=0.2.14" }, { name = "beartype", specifier = ">=0.21.0" }, + { name = "jupytext", specifier = ">=1.17.3" }, { name = "mesa", specifier = ">=3.2.0" }, { name = "mkdocs-git-revision-date-localized-plugin", specifier = ">=1.4.7" }, { name = "mkdocs-include-markdown-plugin", specifier = ">=7.1.5" }, @@ -1320,6 +1323,7 @@ dev = [ docs = [ { name = "autodocsumm", specifier = ">=0.2.14" }, { name = "beartype", specifier = ">=0.21.0" }, + { name = "jupytext", specifier = ">=1.17.3" }, { name = "mesa", specifier = ">=3.2.0" }, { name = "mkdocs-git-revision-date-localized-plugin", specifier = ">=1.4.7" }, { name = "mkdocs-include-markdown-plugin", specifier = ">=7.1.5" }, From b4076c4da7da36f18a420dfdabbe673f07902765 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:48:54 +0200 Subject: [PATCH 189/329] refactor: simplify variant_specs structure and remove unused RUN_SEQUENTIAL logic --- .../general/user-guide/3_advanced_tutorial.py | 33 ++++--------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 59382383..b0fae391 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -57,7 +57,6 @@ """## 1. Imports""" # %% -import os from time import perf_counter import numpy as np @@ -1427,35 +1426,17 @@ def run_variant( model.run(steps) return model, perf_counter() - start -# Allow quick testing by skipping the slow pure-Python sequential baseline. -# Set the environment variable ``MESA_FRAMES_RUN_SEQUENTIAL=0`` (or "false") -# to disable the baseline when running this script. -RUN_SEQUENTIAL = os.getenv("MESA_FRAMES_RUN_SEQUENTIAL", "0").lower() not in { - "0", - "false", - "no", - "off", -} - - -variant_specs: dict[str, tuple[type[AntsBase], bool]] = { - "Sequential (Python loop)": (AntsSequential, RUN_SEQUENTIAL), - "Sequential (Numba)": (AntsNumba, True), - "Parallel (Polars)": (AntsParallel, True), +variant_specs: dict[str, type[AntsBase]] = { + "Sequential (Python loop)": AntsSequential, + "Sequential (Numba)": AntsNumba, + "Parallel (Polars)": AntsParallel, } models: dict[str, Sugarscape] = {} frames: dict[str, pl.DataFrame] = {} runtimes: dict[str, float] = {} -for variant_name, (agent_cls, enabled) in variant_specs.items(): - if not enabled: - print( - f"Skipping {variant_name}; set MESA_FRAMES_RUN_SEQUENTIAL=1 to enable it." - ) - runtimes[variant_name] = float("nan") - continue - +for variant_name, agent_cls in variant_specs.items(): model, runtime = run_variant(agent_cls, steps=MODEL_STEPS, seed=SEED) models[variant_name] = model frames[variant_name] = model.datacollector.data["model"] @@ -1474,10 +1455,10 @@ def run_variant( pl.DataFrame( [ { - "update_rule": variant_name if enabled else f"{variant_name} [skipped]", + "update_rule": variant_name, "runtime_seconds": runtimes.get(variant_name, float("nan")), } - for variant_name, (_, enabled) in variant_specs.items() + for variant_name in variant_specs.keys() ] ) .with_columns(pl.col("runtime_seconds").round(4)) From a36e181594839de0b1177c00a1007b8b57d1c251 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:54:31 +0200 Subject: [PATCH 190/329] refactor: update GitHub Actions workflow for documentation build and preview --- .github/workflows/docs-gh-pages.yml | 82 +++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/.github/workflows/docs-gh-pages.yml b/.github/workflows/docs-gh-pages.yml index ae6974e5..705b0fc7 100644 --- a/.github/workflows/docs-gh-pages.yml +++ b/.github/workflows/docs-gh-pages.yml @@ -1,32 +1,35 @@ -name: Build and Deploy Documentation +name: Docs — Build & Preview on: push: - branches: - - main + branches: [ main ] # regular prod deploy + paths: + - 'mkdocs.yml' + - 'docs/**' + pull_request: # preview only when docs are touched + branches: [ '**' ] + paths: + - 'mkdocs.yml' + - 'docs/**' jobs: - build-and-deploy-docs: + build: runs-on: ubuntu-latest + outputs: + short_sha: ${{ steps.sha.outputs.short }} steps: - uses: actions/checkout@v4 - with: - fetch-depth: 0 # Fetch all history for .git-restore-mtime to work correctly - - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: '3.x' + with: { fetch-depth: 0 } + - uses: actions/setup-python@v5 + with: { python-version: '3.x' } + - uses: astral-sh/setup-uv@v6 - - name: Install uv via GitHub Action - uses: astral-sh/setup-uv@v6 - - - name: Install mesa-frames + docs dependencies + - name: Install mesa-frames + docs deps run: | uv pip install --system . uv pip install --group docs --system - - name: Convert tutorial .py scripts to notebooks + - name: Convert jupytext .py notebooks to .ipynb run: | set -euxo pipefail for nb in docs/general/*.ipynb; do @@ -34,16 +37,53 @@ jobs: uv run jupyter nbconvert --to notebook --execute --inplace "$nb" done - - - name: Build MkDocs site (general documentation) + - name: Build MkDocs site run: uv run mkdocs build --config-file mkdocs.yml --site-dir ./site - - name: Build Sphinx docs (API documentation) + - name: Build Sphinx docs (API) run: uv run sphinx-build -b html docs/api site/api - - name: Deploy to GitHub Pages + - name: Short SHA + id: sha + run: echo "short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" + + - name: Upload site artifact + uses: actions/upload-artifact@v4 + with: + name: site + path: site + + deploy-main: + needs: build + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + steps: + - uses: actions/download-artifact@v4 + with: { name: site, path: site } + - name: Deploy to GitHub Pages (main) + uses: peaceiris/actions-gh-pages@v4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_branch: gh-pages + publish_dir: ./site + force_orphan: true + + deploy-preview: + needs: build + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + steps: + - uses: actions/download-artifact@v4 + with: { name: site, path: site } + - name: Deploy preview under subfolder uses: peaceiris/actions-gh-pages@v4 with: github_token: ${{ secrets.GITHUB_TOKEN }} + publish_branch: gh-pages publish_dir: ./site - force_orphan: true \ No newline at end of file + destination_dir: preview/${{ github.head_ref || github.ref_name }}/${{ needs.build.outputs.short_sha }} + keep_files: true # keep previous previews + # DO NOT set force_orphan here + - name: Print preview URL + run: | + echo "Preview: https://${{ github.repository_owner }}.github.io/$(basename ${{ github.repository }})/preview/${{ github.head_ref || github.ref_name }}/${{ needs.build.outputs.short_sha }}/" From ec98197f9ea0431c770388e2727ccc32c13978e8 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 01:55:53 +0200 Subject: [PATCH 191/329] docs: clarify tutorial instructions for running model variants --- docs/general/user-guide/3_advanced_tutorial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index b0fae391..cee86f9e 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -1395,7 +1395,7 @@ def _resolve_conflicts_in_rounds( """ ## 4. Run the Model Variants -We iterate over each movement policy with a shared helper so all runs reuse the same seed. Set `MESA_FRAMES_RUN_SEQUENTIAL=1` to include the slower pure-Python baseline. +We iterate over each movement policy with a shared helper so all runs reuse the same seed. The tutorial runs all three variants (Python sequential, Numba sequential, and parallel) by default; edit the script if you want to skip the slow pure-Python baseline. """ From 4ef1cf6ad69972a3fe315474bac280e81abb9b58 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 21 Sep 2025 08:21:51 +0000 Subject: [PATCH 192/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/general/index.md | 2 +- .../general/user-guide/3_advanced_tutorial.py | 154 +++++++++++------- 2 files changed, 100 insertions(+), 56 deletions(-) diff --git a/docs/general/index.md b/docs/general/index.md index ee967623..cee3f109 100644 --- a/docs/general/index.md +++ b/docs/general/index.md @@ -1 +1 @@ -{% include-markdown "../../README.md" %} \ No newline at end of file +{% include-markdown "../../README.md" %} diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index cee86f9e..05d9f194 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -28,7 +28,7 @@ The update schedule matters for micro-behaviour, so we study three variants: -1. **Sequential loop (asynchronous):** This is the traditional definition. Ants move one at a time in random order. +1. **Sequential loop (asynchronous):** This is the traditional definition. Ants move one at a time in random order. This cannnot be vectorised easily as the best move for an ant might depend on the moves of earlier ants (for example, if they target the same cell). 2. **Sequential with Numba:** matches the first variant but relies on a compiled helper for speed. @@ -92,6 +92,7 @@ # Model-level reporters + def gini(model: Model) -> float: """Compute the Gini coefficient of agent sugar holdings. @@ -135,6 +136,7 @@ def gini(model: Model) -> float: index = np.arange(1, n + 1, dtype=np.float64) return float((2.0 * np.dot(index, sorted_vals) / (n * total)) - (n + 1) / n) + def corr_sugar_metabolism(model: Model) -> float: """Pearson correlation between agent sugar and metabolism. @@ -169,6 +171,7 @@ def corr_sugar_metabolism(model: Model) -> float: metabolism = agent_df["metabolism"].to_numpy().astype(np.float64) return _safe_corr(sugar, metabolism) + def corr_sugar_vision(model: Model) -> float: """Pearson correlation between agent sugar and vision. @@ -202,6 +205,7 @@ def corr_sugar_vision(model: Model) -> float: vision = agent_df["vision"].to_numpy().astype(np.float64) return _safe_corr(sugar, vision) + def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: """Safely compute Pearson correlation between two 1-D arrays. @@ -229,6 +233,7 @@ def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: return float("nan") return float(np.corrcoef(x, y)[0, 1]) + class Sugarscape(Model): """Minimal Sugarscape model used throughout the tutorial. @@ -269,7 +274,7 @@ class Sugarscape(Model): def __init__( self, - agent_type: type["AntsBase"], + agent_type: type[AntsBase], n_agents: int, *, width: int, @@ -282,7 +287,7 @@ def __init__( "Cannot place more agents than grid cells when capacity is 1." ) super().__init__(seed) - + # 1. Let's create the sugar grid and set up the space sugar_grid_df = self._generate_sugar_grid(width, height, max_sugar) @@ -291,7 +296,7 @@ def __init__( ) self.space.set_cells(sugar_grid_df) self._max_sugar = sugar_grid_df.select(["dim_0", "dim_1", "max_sugar"]) - + # 2. Now we create the agents and place them on the grid agent_frame = self._generate_agent_frame(n_agents) @@ -415,7 +420,9 @@ def _advance_sugar_field(self) -> None: empty_cells = self.space.empty_cells if not empty_cells.is_empty(): # Look up the maximum sugar for each empty cell and restore it. - refresh = empty_cells.join(self._max_sugar, on=["dim_0", "dim_1"], how="left") + refresh = empty_cells.join( + self._max_sugar, on=["dim_0", "dim_1"], how="left" + ) self.space.set_cells(empty_cells, {"sugar": refresh["max_sugar"]}) full_cells = self.space.full_cells if not full_cells.is_empty(): @@ -423,6 +430,7 @@ def _advance_sugar_field(self) -> None: zeros = pl.Series(np.zeros(len(full_cells), dtype=np.int64)) self.space.set_cells(full_cells, {"sugar": zeros}) + # %% [markdown] """ @@ -430,13 +438,14 @@ def _advance_sugar_field(self) -> None: ### 3.1 Base agent class -Now let's define the agent class (the ant class). We start with a base class which implements the common logic for eating and starvation, while leaving the `move` method abstract. +Now let's define the agent class (the ant class). We start with a base class which implements the common logic for eating and starvation, while leaving the `move` method abstract. The base class also provides helper methods for sensing visible cells and choosing the best cell based on sugar, distance, and coordinates. This will allow us to define different movement policies (sequential, Numba-accelerated, and parallel) as subclasses that only need to implement the `move` method. """ # %% + class AntsBase(AgentSet): """Base agent set for the Sugarscape tutorial. @@ -450,6 +459,7 @@ class AntsBase(AgentSet): - Subclasses must implement :meth:`move` which changes agent positions on the grid (via :meth:`mesa_frames.Grid` helpers). """ + def __init__(self, model: Model, agent_frame: pl.DataFrame) -> None: """Initialise the agent set and validate required trait columns. @@ -518,7 +528,9 @@ def eat(self) -> None: # `occupied_ids` is a Polars Series; calling `is_in` with a Series # of the same datatype is ambiguous in newer Polars. Use `implode` # to collapse the Series into a list-like value for membership checks. - occupied_cells = self.space.cells.filter(pl.col("agent_id").is_in(occupied_ids.implode())) + occupied_cells = self.space.cells.filter( + pl.col("agent_id").is_in(occupied_ids.implode()) + ) if occupied_cells.is_empty(): return # The agent ordering here uses the agent_id values stored in the @@ -526,7 +538,9 @@ def eat(self) -> None: # the matching agents' sugar values in one vectorised write. agent_ids = occupied_cells["agent_id"] self[agent_ids, "sugar"] = ( - self[agent_ids, "sugar"] + occupied_cells["sugar"] - self[agent_ids, "metabolism"] + self[agent_ids, "sugar"] + + occupied_cells["sugar"] + - self[agent_ids, "metabolism"] ) # After harvesting, occupied cells have zero sugar. self.space.set_cells( @@ -557,8 +571,11 @@ def _remove_starved(self) -> None: # %% + class AntsSequential(AntsBase): - def _visible_cells(self, origin: tuple[int, int], vision: int) -> list[tuple[int, int]]: + def _visible_cells( + self, origin: tuple[int, int], vision: int + ) -> list[tuple[int, int]]: """List cells visible from an origin along the four cardinal axes. The visibility set includes the origin cell itself and cells at @@ -637,7 +654,9 @@ def _choose_best_cell( if blocked and candidate != origin and candidate in blocked: continue sugar_here = sugar_map.get(candidate, 0) - distance = self.model.space.get_distances(origin, candidate)["distance"].item() + distance = self.model.space.get_distances(origin, candidate)[ + "distance" + ].item() better = False # Primary criterion: strictly more sugar. if sugar_here > best_sugar: @@ -670,7 +689,7 @@ def _current_sugar_map(self) -> dict[tuple[int, int], int]: (int(x), int(y)): 0 if sugar is None else int(sugar) for x, y, sugar in cells.iter_rows() } - + def move(self) -> None: sugar_map = self._current_sugar_map() state = self.df.join(self.pos, on="unique_id", how="left") @@ -691,6 +710,7 @@ def move(self) -> None: if target != current: self.space.move_agents(agent_id, target) + # %% [markdown] """ ### 3.3 Speeding Up the Loop with Numba @@ -700,7 +720,8 @@ def move(self) -> None: Numba compiles numerical Python code to fast machine code at runtime. To use Numba, we need to rewrite the movement logic in a way that is compatible with Numba's restrictions (using tightly typed numpy arrays and accessing data indexes directly). """ -# %% + +# %% @njit(cache=True) def _numba_should_replace( best_sugar: int, @@ -876,6 +897,7 @@ def sequential_move_numba( return new_dim0, new_dim1 + class AntsNumba(AntsBase): def move(self) -> None: state = self.df.join(self.pos, on="unique_id", how="left") @@ -888,8 +910,8 @@ def move(self) -> None: sugar_array = ( self.space.cells.sort(["dim_0", "dim_1"]) - .with_columns(pl.col("sugar").fill_null(0)) - ["sugar"].to_numpy() + .with_columns(pl.col("sugar").fill_null(0))["sugar"] + .to_numpy() .reshape(self.space.dimensions) ) @@ -910,6 +932,7 @@ def move(self) -> None: # %% + class AntsParallel(AntsBase): def move(self) -> None: """Move agents in parallel by ranking visible cells and resolving conflicts. @@ -1002,8 +1025,7 @@ def _build_neighborhood_frame(self, current_pos: pl.DataFrame) -> pl.DataFrame: sugar_cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) neighborhood_cells = ( - neighborhood_cells - .join(sugar_cells, on=["dim_0", "dim_1"], how="left") + neighborhood_cells.join(sugar_cells, on=["dim_0", "dim_1"], how="left") .with_columns(pl.col("sugar").fill_null(0)) .rename({"dim_0": "dim_0_candidate", "dim_1": "dim_1_candidate"}) ) @@ -1021,11 +1043,9 @@ def _build_neighborhood_frame(self, current_pos: pl.DataFrame) -> pl.DataFrame: # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ # ╞══════════╪════════╪══════════════════╪══════════════════╪════════╡ - neighborhood_cells = ( - neighborhood_cells - .drop(["dim_0_center", "dim_1_center"]) - .select(["agent_id", "radius", "dim_0_candidate", "dim_1_candidate", "sugar"]) - ) + neighborhood_cells = neighborhood_cells.drop( + ["dim_0_center", "dim_1_center"] + ).select(["agent_id", "radius", "dim_0_candidate", "dim_1_candidate", "sugar"]) return neighborhood_cells @@ -1087,12 +1107,7 @@ def _rank_candidates( keep="first", maintain_order=True, ) - .with_columns( - pl.col("agent_id") - .cum_count() - .over("agent_id") - .alias("rank") - ) + .with_columns(pl.col("agent_id").cum_count().over("agent_id").alias("rank")) ) # Precompute per‑agent candidate rank once so conflict resolution can @@ -1127,7 +1142,9 @@ def _rank_candidates( # │ --- ┆ --- │ # │ u64 ┆ u32 │ # ╞══════════╪═══════════╡ - max_rank = choices.group_by("agent_id").agg(pl.col("rank").max().alias("max_rank")) + max_rank = choices.group_by("agent_id").agg( + pl.col("rank").max().alias("max_rank") + ) return choices, origins, max_rank def _resolve_conflicts_in_rounds( @@ -1159,7 +1176,7 @@ def _resolve_conflicts_in_rounds( """ # Prepare unresolved agents and working tables. agent_ids = choices["agent_id"].unique(maintain_order=True) - + # unresolved columns: # ┌──────────┬────────────────┐ # │ agent_id ┆ current_rank │ @@ -1181,7 +1198,9 @@ def _resolve_conflicts_in_rounds( # ╞══════════╪══════════════════╪══════════════════╡ assigned = pl.DataFrame( { - "agent_id": pl.Series(name="agent_id", values=[], dtype=agent_ids.dtype), + "agent_id": pl.Series( + name="agent_id", values=[], dtype=agent_ids.dtype + ), "dim_0_candidate": pl.Series( name="dim_0_candidate", values=[], dtype=pl.Int64 ), @@ -1223,7 +1242,9 @@ def _resolve_conflicts_in_rounds( # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u32 ┆ i64 │ # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════╪══════════════╡ candidate_pool = choices.join(unresolved, on="agent_id") - candidate_pool = candidate_pool.filter(pl.col("rank") >= pl.col("current_rank")) + candidate_pool = candidate_pool.filter( + pl.col("rank") >= pl.col("current_rank") + ) if not taken.is_empty(): candidate_pool = candidate_pool.join( taken, @@ -1264,8 +1285,7 @@ def _resolve_conflicts_in_rounds( # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u32 ┆ i64 │ # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════╪══════════════╡ best_candidates = ( - candidate_pool - .sort(["agent_id", "rank"]) + candidate_pool.sort(["agent_id", "rank"]) .group_by("agent_id", maintain_order=True) .first() ) @@ -1277,7 +1297,9 @@ def _resolve_conflicts_in_rounds( # │ --- ┆ --- │ # │ u64 ┆ i64 │ # ╞══════════╪══════════════╡ - missing = unresolved.join(best_candidates.select("agent_id"), on="agent_id", how="anti") + missing = unresolved.join( + best_candidates.select("agent_id"), on="agent_id", how="anti" + ) if not missing.is_empty(): # fallback (missing) columns match fallback table above. fallback = missing.join(origins, on="agent_id", how="left") @@ -1306,8 +1328,12 @@ def _resolve_conflicts_in_rounds( ], how="vertical", ) - unresolved = unresolved.join(missing.select("agent_id"), on="agent_id", how="anti") - best_candidates = best_candidates.join(missing.select("agent_id"), on="agent_id", how="anti") + unresolved = unresolved.join( + missing.select("agent_id"), on="agent_id", how="anti" + ) + best_candidates = best_candidates.join( + missing.select("agent_id"), on="agent_id", how="anti" + ) if unresolved.is_empty() or best_candidates.is_empty(): continue @@ -1323,8 +1349,7 @@ def _resolve_conflicts_in_rounds( # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u32 ┆ i64 ┆ f64 │ # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════╪══════════════╪═════════╡ winners = ( - best_candidates - .sort(["dim_0_candidate", "dim_1_candidate", "lottery"]) + best_candidates.sort(["dim_0_candidate", "dim_1_candidate", "lottery"]) .group_by(["dim_0_candidate", "dim_1_candidate"], maintain_order=True) .first() ) @@ -1373,22 +1398,27 @@ def _resolve_conflicts_in_rounds( ) .join(max_rank, on="agent_id", how="left") .with_columns( - pl.min_horizontal(pl.col("next_rank"), pl.col("max_rank")).alias("next_rank") + pl.min_horizontal(pl.col("next_rank"), pl.col("max_rank")).alias( + "next_rank" + ) ) .select(["agent_id", "next_rank"]) ) # Promote losers' current_rank (if any) and continue. # unresolved (updated) retains columns agent_id/current_rank. - unresolved = unresolved.join(loser_updates, on="agent_id", how="left").with_columns( - pl.when(pl.col("next_rank").is_not_null()) - .then(pl.col("next_rank")) - .otherwise(pl.col("current_rank")) - .alias("current_rank") - ).drop("next_rank") + unresolved = ( + unresolved.join(loser_updates, on="agent_id", how="left") + .with_columns( + pl.when(pl.col("next_rank").is_not_null()) + .then(pl.col("next_rank")) + .otherwise(pl.col("current_rank")) + .alias("current_rank") + ) + .drop("next_rank") + ) return assigned - # %% [markdown] @@ -1408,6 +1438,7 @@ def _resolve_conflicts_in_rounds( MAX_SUGAR = 4 SEED = 42 + def run_variant( agent_cls: type[AntsBase], *, @@ -1426,6 +1457,7 @@ def run_variant( model.run(steps) return model, perf_counter() - start + variant_specs: dict[str, type[AntsBase]] = { "Sequential (Python loop)": AntsSequential, "Sequential (Numba)": AntsNumba, @@ -1477,13 +1509,15 @@ def run_variant( """ ## 5. Comparing the Update Rules -Even though micro rules differ, aggregate trajectories remain qualitatively similar (sugar trends up while population gradually declines). -When we join the traces step-by-step, we see small but noticeable deviations introduced by synchronous conflict resolution (e.g., a few more retirements when conflicts cluster). -In our run (seed=42), the final-step Gini differs by ≈0.005, and wealth–trait correlations match within ~1e-3. +Even though micro rules differ, aggregate trajectories remain qualitatively similar (sugar trends up while population gradually declines). +When we join the traces step-by-step, we see small but noticeable deviations introduced by synchronous conflict resolution (e.g., a few more retirements when conflicts cluster). +In our run (seed=42), the final-step Gini differs by ≈0.005, and wealth–trait correlations match within ~1e-3. These gaps vary by seed and grid size, but they consistently stay modest, supporting the relaxed parallel update as a faithful macro-level approximation.""" # %% -comparison = numba_model_frame.select(["step", "mean_sugar", "total_sugar", "agents_alive"]).join( +comparison = numba_model_frame.select( + ["step", "mean_sugar", "total_sugar", "agents_alive"] +).join( par_model_frame.select(["step", "mean_sugar", "total_sugar", "agents_alive"]), on="step", how="inner", @@ -1492,11 +1526,14 @@ def run_variant( comparison = comparison.with_columns( (pl.col("mean_sugar") - pl.col("mean_sugar_parallel")).abs().alias("mean_diff"), (pl.col("total_sugar") - pl.col("total_sugar_parallel")).abs().alias("total_diff"), - (pl.col("agents_alive") - pl.col("agents_alive_parallel")).abs().alias("count_diff"), + (pl.col("agents_alive") - pl.col("agents_alive_parallel")) + .abs() + .alias("count_diff"), ) print("Step-level absolute differences (first 10 steps):") print(comparison.select(["step", "mean_diff", "total_diff", "count_diff"]).head(10)) + # Build the steady‑state metrics table from the DataCollector output rather than # recomputing reporters directly on the model objects. The collector already # stored the model‑level reporters (gini, correlations, etc.) every step. @@ -1506,6 +1543,7 @@ def _last_row(df: pl.DataFrame) -> pl.DataFrame: # Ensure we take the final time step in case steps < MODEL_STEPS due to extinction. return df.sort("step").tail(1) + numba_last = _last_row(frames.get("Sequential (Numba)", pl.DataFrame())) parallel_last = _last_row(frames.get("Parallel (Polars)", pl.DataFrame())) @@ -1535,7 +1573,9 @@ def _last_row(df: pl.DataFrame) -> pl.DataFrame: ) ) -metrics_table = pl.concat(metrics_pieces, how="vertical") if metrics_pieces else pl.DataFrame() +metrics_table = ( + pl.concat(metrics_pieces, how="vertical") if metrics_pieces else pl.DataFrame() +) print("\nSteady-state inequality metrics:") print( @@ -1551,8 +1591,12 @@ def _last_row(df: pl.DataFrame) -> pl.DataFrame: ) if metrics_table.height >= 2: - numba_gini = metrics_table.filter(pl.col("update_rule") == "Sequential (Numba)")["gini"][0] - par_gini = metrics_table.filter(pl.col("update_rule") == "Parallel (random tie-break)")["gini"][0] + numba_gini = metrics_table.filter(pl.col("update_rule") == "Sequential (Numba)")[ + "gini" + ][0] + par_gini = metrics_table.filter( + pl.col("update_rule") == "Parallel (random tie-break)" + )["gini"][0] print(f"Absolute Gini gap (numba vs parallel): {abs(numba_gini - par_gini):.4f}") # %% [markdown] @@ -1569,4 +1613,4 @@ def _last_row(df: pl.DataFrame) -> pl.DataFrame: **Polars + LazyFrames roadmap** – future mesa-frames releases will expose LazyFrame-powered sets and spaces (which can also use a GPU cuda accelerated backend which greatly accelerates joins), so the same Polars code you wrote here will scale even further without touching Numba. -""" \ No newline at end of file +""" From 6c0deb02810dd652867ac6e17af23b4a50a63b91 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 10:33:50 +0200 Subject: [PATCH 193/329] refactor: enhance jupytext conversion process for .py notebooks in documentation --- .github/workflows/docs-gh-pages.yml | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/.github/workflows/docs-gh-pages.yml b/.github/workflows/docs-gh-pages.yml index 705b0fc7..d60be123 100644 --- a/.github/workflows/docs-gh-pages.yml +++ b/.github/workflows/docs-gh-pages.yml @@ -32,10 +32,22 @@ jobs: - name: Convert jupytext .py notebooks to .ipynb run: | set -euxo pipefail - for nb in docs/general/*.ipynb; do - echo "Executing $nb" - uv run jupyter nbconvert --to notebook --execute --inplace "$nb" - done + # Convert any jupytext .py files to .ipynb without executing them. + # Enable nullglob so the pattern expands to empty when there are no matches + # and globstar so we recurse into subdirectories (e.g., user-guide/). + shopt -s nullglob globstar || true + files=(docs/general/**/*.py) + if [ ${#files[@]} -eq 0 ]; then + echo "No jupytext .py files found under docs/general" + else + for src in "${files[@]}"; do + [ -e "$src" ] || continue + dest="${src%.py}.ipynb" + echo "Converting $src -> $dest" + # jupytext will write the .ipynb alongside the source file + uv run jupytext --to notebook "$src" + done + fi - name: Build MkDocs site run: uv run mkdocs build --config-file mkdocs.yml --site-dir ./site From 60bd49ba7663b8e9ea232adaf027d14040f3e7cf Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 18:06:33 +0200 Subject: [PATCH 194/329] docs: clarify step ordering in Sugarscape model tutorial --- docs/general/user-guide/3_advanced_tutorial.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 05d9f194..af4cd90d 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -378,18 +378,18 @@ def _generate_agent_frame(self, n_agents: int) -> pl.DataFrame: def step(self) -> None: """Advance the model by one step. - Notes - ----- - The per-step ordering is important: regrowth happens first (so empty - cells are refilled), then agents move and eat, and finally metrics are - collected. If the agent set becomes empty at any point the model is - marked as not running. + Notes + ----- + The per-step ordering is important and this tutorial implements the + classic Sugarscape "instant growback": agents move and eat first, + and then empty cells are refilled immediately (move -> eat -> regrow + -> collect). """ if len(self.sets[0]) == 0: self.running = False return - self._advance_sugar_field() self.sets[0].step() + self._advance_sugar_field() self.datacollector.collect() if len(self.sets[0]) == 0: self.running = False From d7263e10642d030777810c6fcd1c3d785d97b337 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 18:11:13 +0200 Subject: [PATCH 195/329] refactor: optimize distance calculation in AntsSequential class using Manhattan distance --- docs/general/user-guide/3_advanced_tutorial.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index af4cd90d..191bdc30 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -648,15 +648,18 @@ def _choose_best_cell( best_cell = origin best_sugar = sugar_map.get(origin, 0) best_distance = 0 + ox, oy = origin for candidate in self._visible_cells(origin, vision): # Skip blocked cells (occupied by other agents) unless it's the # agent's current cell which we always consider. if blocked and candidate != origin and candidate in blocked: continue sugar_here = sugar_map.get(candidate, 0) - distance = self.model.space.get_distances(origin, candidate)[ - "distance" - ].item() + # Use step-based Manhattan distance (number of steps along cardinal + # axes) which is the same metric used by the Numba path. This avoids + # calling the heavier `space.get_distances` per candidate. + cx, cy = candidate + distance = abs(cx - ox) + abs(cy - oy) better = False # Primary criterion: strictly more sugar. if sugar_here > best_sugar: From d62d406a7bc7f869903d343428afd392dbf9465f Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 18:48:08 +0200 Subject: [PATCH 196/329] docs: enhance move method documentation in AntsParallel class with declarative mental model --- docs/general/user-guide/3_advanced_tutorial.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 191bdc30..85ac305d 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -940,6 +940,10 @@ class AntsParallel(AntsBase): def move(self) -> None: """Move agents in parallel by ranking visible cells and resolving conflicts. + Declarative mental model: express *what* each agent wants (ranked candidates), + then use dataframe ops to *allocate* (joins, group_by with a lottery). + Performance is handled by Polars/LazyFrames; avoid premature micro-optimisations. + Returns ------- None From 495dbfb7efda75e7b0e0cfd501f61b82fbb902cf Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 18:50:47 +0200 Subject: [PATCH 197/329] docs: enhance explanation of modeling philosophy in advanced tutorial --- docs/general/user-guide/3_advanced_tutorial.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 85ac305d..9b2757d7 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -35,6 +35,11 @@ 3. **Parallel (synchronous):** all ants propose moves; conflicts are resolved at random before applying the winners simultaneously (and the losers get to their second-best cell, etc). +The first variant (pure Python loops) is a natural starting point, but it is **not** the mesa-frames philosophy. +The latter two are: we aim to **write rules declaratively** and let the dataframe engine worry about performance. +Our guiding principle is to **focus on modelling first and performance second**. Only when a rule is truly +inherently sequential do we fall back to a compiled kernel (Numba or JAX). + Our goal is to show that, under instantaneous growback and uniform resources, the model converges to the *same* macroscopic inequality pattern regardless of whether agents act sequentially or in parallel and that As long as the random draws do From 2f023670e60b59f71bff0600b19fb1c9f276229b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 21 Sep 2025 16:51:11 +0000 Subject: [PATCH 198/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/general/user-guide/3_advanced_tutorial.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 9b2757d7..ef1bfa4c 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -35,8 +35,8 @@ 3. **Parallel (synchronous):** all ants propose moves; conflicts are resolved at random before applying the winners simultaneously (and the losers get to their second-best cell, etc). -The first variant (pure Python loops) is a natural starting point, but it is **not** the mesa-frames philosophy. -The latter two are: we aim to **write rules declaratively** and let the dataframe engine worry about performance. +The first variant (pure Python loops) is a natural starting point, but it is **not** the mesa-frames philosophy. +The latter two are: we aim to **write rules declaratively** and let the dataframe engine worry about performance. Our guiding principle is to **focus on modelling first and performance second**. Only when a rule is truly inherently sequential do we fall back to a compiled kernel (Numba or JAX). @@ -383,12 +383,12 @@ def _generate_agent_frame(self, n_agents: int) -> pl.DataFrame: def step(self) -> None: """Advance the model by one step. - Notes - ----- - The per-step ordering is important and this tutorial implements the - classic Sugarscape "instant growback": agents move and eat first, - and then empty cells are refilled immediately (move -> eat -> regrow - -> collect). + Notes + ----- + The per-step ordering is important and this tutorial implements the + classic Sugarscape "instant growback": agents move and eat first, + and then empty cells are refilled immediately (move -> eat -> regrow + -> collect). """ if len(self.sets[0]) == 0: self.running = False From 82a9ab35500f431625734123370b087831c8a3e0 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Sun, 21 Sep 2025 18:54:42 +0200 Subject: [PATCH 199/329] docs: update user guide link to point to the getting started section --- docs/api/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/conf.py b/docs/api/conf.py index 43098ec2..95f23a38 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -64,7 +64,7 @@ "external_links": [ { "name": "User guide", - "url": f"{web_root}/user-guide/", + "url": f"{web_root}/user-guide/0_getting-started/", }, ], "icon_links": [ From bb7910b079728150b456d3142fd40c90e8b77192 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 19:39:23 +0200 Subject: [PATCH 200/329] docs: remove obsolete Makefile and batch script for Sphinx documentation --- docs/api/Makefile | 20 -------------------- docs/api/make.bat | 35 ----------------------------------- 2 files changed, 55 deletions(-) delete mode 100644 docs/api/Makefile delete mode 100644 docs/api/make.bat diff --git a/docs/api/Makefile b/docs/api/Makefile deleted file mode 100644 index d0c3cbf1..00000000 --- a/docs/api/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -# Minimal makefile for Sphinx documentation -# - -# You can set these variables from the command line, and also -# from the environment for the first two. -SPHINXOPTS ?= -SPHINXBUILD ?= sphinx-build -SOURCEDIR = source -BUILDDIR = build - -# Put it first so that "make" without argument is like "make help". -help: - @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) - -.PHONY: help Makefile - -# Catch-all target: route all unknown targets to Sphinx using the new -# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). -%: Makefile - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/api/make.bat b/docs/api/make.bat deleted file mode 100644 index dc1312ab..00000000 --- a/docs/api/make.bat +++ /dev/null @@ -1,35 +0,0 @@ -@ECHO OFF - -pushd %~dp0 - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set SOURCEDIR=source -set BUILDDIR=build - -%SPHINXBUILD% >NUL 2>NUL -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.https://www.sphinx-doc.org/ - exit /b 1 -) - -if "%1" == "" goto help - -%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% -goto end - -:help -%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% - -:end -popd From bcf9a51cee6b4057a0c0fea90d0e92f94bcc1ea4 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 20:17:09 +0200 Subject: [PATCH 201/329] docs: update overview and mini usage flow in API documentation --- docs/api/index.rst | 47 +++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/docs/api/index.rst b/docs/api/index.rst index 936350d6..f848c6f7 100644 --- a/docs/api/index.rst +++ b/docs/api/index.rst @@ -1,34 +1,43 @@ mesa-frames API =============== -This page provides a high-level overview of all public mesa-frames objects, functions, and methods. All classes and functions exposed in the ``mesa_frames.*`` namespace are public. +Overview +-------- -.. grid:: - - .. grid-item-card:: +mesa-frames provides a DataFrame-first API for agent-based models. Instead of representing each agent as a distinct Python object, agents are stored in AgentSets (backed by DataFrames) and manipulated via vectorised operations. This leads to much lower memory overhead and faster bulk updates while keeping an object-oriented feel for model structure and lifecycle management. - .. toctree:: - :maxdepth: 2 - reference/agents/index +Mini usage flow +--------------- - .. grid-item-card:: +1. Create a Model and register AgentSets on ``model.sets``. +2. Populate AgentSets with agents (rows) and attributes (columns) via adding a DataFrame to the AgentSet. +3. Implement AgentSet methods that operate on DataFrames +4. Use ``model.sets.do("step")`` from the model loop to advance the simulation; datacollectors and reporters can sample model- and agent-level columns at each step. - .. toctree:: - :maxdepth: 2 +.. grid:: + :gutter: 2 - reference/model + .. grid-item-card:: Manage agent collections + :link: reference/agents/index + :link-type: doc - .. grid-item-card:: + Create and operate on ``AgentSets`` and ``AgentSetRegisties``: add/remove agents. - .. toctree:: - :maxdepth: 2 + .. grid-item-card:: Model orchestration + :link: reference/model + :link-type: doc - reference/space/index + ``Model`` API for registering sets, stepping the simulation, and integrating with datacollectors/reporters. + + .. grid-item-card:: Spatial support + :link: reference/space/index + :link-type: doc - .. grid-item-card:: + Placement and neighbourhood utilities for ``Grid`` and space - .. toctree:: - :maxdepth: 2 + .. grid-item-card:: Collect simulation data + :link: reference/datacollector + :link-type: doc - reference/datacollector \ No newline at end of file + Record model- and agent-level metrics over time with ``DataCollector``. Sample columns, run aggregations, and export cleaned frames for analysis. \ No newline at end of file From 2f51c5aa2bd6ac2b5934c2b3205cf934cfaa8be0 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 20:21:04 +0200 Subject: [PATCH 202/329] docs: add docs/site to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4a189d56..41ce8f27 100644 --- a/.gitignore +++ b/.gitignore @@ -154,3 +154,4 @@ cython_debug/ *.code-workspace llm_rules.md .python-version +docs/site \ No newline at end of file From 62ede62999374c9bef711fe21a746baf2dce7750 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 20:36:15 +0200 Subject: [PATCH 203/329] docs: update API reference for clarity and consistency --- docs/api/reference/agents/index.rst | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/api/reference/agents/index.rst b/docs/api/reference/agents/index.rst index a1c03126..549381fa 100644 --- a/docs/api/reference/agents/index.rst +++ b/docs/api/reference/agents/index.rst @@ -3,6 +3,41 @@ Agents .. currentmodule:: mesa_frames +Quick intro +----------- + +- ``AgentSet`` stores agents as rows in a Polars-backed table and provides vectorised operations for high-performance updates. + +- ``AgentSetRegistry`` (available at ``model.sets``) is the container that holds all ``AgentSet`` instances for a model and provides convenience operations (add/remove sets, step all sets, rename). + +- Keep agent logic column-oriented and prefer Polars expressions for updates. + +Minimal example +--------------- + +.. code-block:: python + + from mesa_frames import Model, AgentSet + import polars as pl + + class MySet(AgentSet): + def step(self): + # vectorised update: increase age for all agents + self.df = self.df.with_columns((pl.col("age") + 1).alias("age")) + + class MyModel(Model): + def __init__(self): + super().__init__() + # register an AgentSet on the model's registry + self.sets += MySet(self) + + m = MyModel() + m.sets["MySet"].add(pl.DataFrame({"age": [0, 5, 10]})) + # step all registered sets (delegates to each AgentSet.step) + m.sets.do("step") + +API reference +-------------------------------- .. autoclass:: AgentSet :members: From 056b5b0fa9237b394cac5d9ba2096bed87b97a71 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 20:38:57 +0200 Subject: [PATCH 204/329] docs: refine minimal example for AgentSet initialization --- docs/api/reference/agents/index.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/api/reference/agents/index.rst b/docs/api/reference/agents/index.rst index 549381fa..69287af9 100644 --- a/docs/api/reference/agents/index.rst +++ b/docs/api/reference/agents/index.rst @@ -21,6 +21,10 @@ Minimal example import polars as pl class MySet(AgentSet): + def __init__(self, model): + super().__init__(model) + self.add(pl.DataFrame({"age": [0, 5, 10]})) + def step(self): # vectorised update: increase age for all agents self.df = self.df.with_columns((pl.col("age") + 1).alias("age")) @@ -32,7 +36,6 @@ Minimal example self.sets += MySet(self) m = MyModel() - m.sets["MySet"].add(pl.DataFrame({"age": [0, 5, 10]})) # step all registered sets (delegates to each AgentSet.step) m.sets.do("step") From b7e437e66c01a577551f069a6362fd5f63a1914e Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 20:40:24 +0200 Subject: [PATCH 205/329] docs: enhance minimal example for Model with updated AgentSet usage --- docs/api/reference/model.rst | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/api/reference/model.rst b/docs/api/reference/model.rst index 099e601b..6ba3dc43 100644 --- a/docs/api/reference/model.rst +++ b/docs/api/reference/model.rst @@ -3,6 +3,40 @@ Model .. currentmodule:: mesa_frames +Quick intro +----------- + +`Model` orchestrates the simulation lifecycle: creating and registering `AgentSet`s, stepping the simulation, and integrating with `DataCollector` and spatial `Grid`s. Typical usage: + +- Instantiate `Model`, add `AgentSet` instances to `model.sets`. +- Call `model.sets.do('step')` inside your model loop to trigger set-level updates. +- Use `DataCollector` to sample model- and agent-level columns each step. + +Minimal example +--------------- + +.. code-block:: python + + from mesa_frames import Model, AgentSet, DataCollector + import polars as pl + + class People(AgentSet): + def step(self): + self._df = self._df.with_columns((pl.col("wealth") * 1.01).alias("wealth")) + + class MyModel(Model): + def __init__(self): + super().__init__() + self.sets += People(self) + self.dc = DataCollector(model_reporters={'avg_wealth': lambda m: m.sets.get('People')._df['wealth'].mean()}) + + m = MyModel() + m.sets.get('People').add(pl.DataFrame({'wealth': [100.0, 50.0]})) + m.step() + +API reference +------------- + .. autoclass:: Model :members: :inherited-members: From 631f3633255f06f05d43ee6c7e0295fb50cc929a Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 20:40:57 +0200 Subject: [PATCH 206/329] docs: expand DataCollector documentation with detailed usage examples --- docs/api/reference/datacollector.rst | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/api/reference/datacollector.rst b/docs/api/reference/datacollector.rst index bdf38cfd..017bd18d 100644 --- a/docs/api/reference/datacollector.rst +++ b/docs/api/reference/datacollector.rst @@ -3,6 +3,40 @@ Data Collection .. currentmodule:: mesa_frames +Quick intro +----------- + +``DataCollector`` samples model- and agent-level columns over time and returns cleaned DataFrames suitable for analysis. Typical patterns: + +- Provide ``model_reporters`` (callables producing scalars) and ``agent_reporters`` (column selectors or callables that operate on an AgentSet). +- Call ``collector.collect(model)`` inside the model step or use built-in integration if the model calls the collector automatically. + +Minimal example +--------------- + +.. code-block:: python + + from mesa_frames import DataCollector, Model, AgentSet + import polars as pl + + class P(AgentSet): + def __init__(self, model): + super().__init__(model) + self.add(pl.DataFrame({'x': [1,2]})) + + class M(Model): + def __init__(self): + super().__init__() + self.sets += P(self) + self.dc = DataCollector(model_reporters={'count': lambda m: len(m.sets['P'])}, + agent_reporters='x') + + m = M() + m.dc.collect() + +API reference +------------- + .. autoclass:: DataCollector :members: :inherited-members: From 473e0d880f56d2a3366adef610193ded38f95d67 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 20:42:02 +0200 Subject: [PATCH 207/329] docs: update Model documentation for improved clarity and examples --- docs/api/reference/model.rst | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/docs/api/reference/model.rst b/docs/api/reference/model.rst index 6ba3dc43..2b0b2102 100644 --- a/docs/api/reference/model.rst +++ b/docs/api/reference/model.rst @@ -6,11 +6,11 @@ Model Quick intro ----------- -`Model` orchestrates the simulation lifecycle: creating and registering `AgentSet`s, stepping the simulation, and integrating with `DataCollector` and spatial `Grid`s. Typical usage: +``Model`` orchestrates the simulation lifecycle: creating and registering ``AgentSet``s, stepping the simulation, and integrating with ``DataCollector`` and spatial ``Grid``s. Typical usage: -- Instantiate `Model`, add `AgentSet` instances to `model.sets`. -- Call `model.sets.do('step')` inside your model loop to trigger set-level updates. -- Use `DataCollector` to sample model- and agent-level columns each step. +- Instantiate ``Model``, add ``AgentSet`` instances to ``model.sets``. +- Call ``model.sets.do('step')`` inside your model loop to trigger set-level updates. +- Use ``DataCollector`` to sample model- and agent-level columns each step. Minimal example --------------- @@ -22,16 +22,15 @@ Minimal example class People(AgentSet): def step(self): - self._df = self._df.with_columns((pl.col("wealth") * 1.01).alias("wealth")) + self.add(pl.DataFrame({'wealth': [1, 2, 3]})) class MyModel(Model): def __init__(self): super().__init__() self.sets += People(self) - self.dc = DataCollector(model_reporters={'avg_wealth': lambda m: m.sets.get('People')._df['wealth'].mean()}) + self.dc = DataCollector(model_reporters={'avg_wealth': lambda m: m.sets['People'].df['wealth'].mean()}) m = MyModel() - m.sets.get('People').add(pl.DataFrame({'wealth': [100.0, 50.0]})) m.step() API reference From 439b6a6c40de254956eaec7c441a41136f1f8c27 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 20:46:51 +0200 Subject: [PATCH 208/329] docs: enhance overview and examples for Grid usage in space reference --- docs/api/reference/space/index.rst | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/api/reference/space/index.rst b/docs/api/reference/space/index.rst index 8741b6b6..03763610 100644 --- a/docs/api/reference/space/index.rst +++ b/docs/api/reference/space/index.rst @@ -4,6 +4,41 @@ This page provides a high-level overview of possible space objects for mesa-fram .. currentmodule:: mesa_frames +Quick intro +----------- + + + +Currently we only support the ``Grid``. Typical usage: + +- Construct ``Grid(model, (width, height))`` and use ``place``/ ``move`` helpers to update agent positional columns. +- Use neighbourhood queries to produce masks or index lists and then apply vectorised updates to selected rows. + +Minimal example +--------------- + +.. code-block:: python + + from mesa_frames import Model, Grid, AgentSet + import polars as pl + + class P(AgentSet): + pass + + class M(Model): + def __init__(self): + super().__init__() + self.space = Grid(self, (10, 10)) + self.sets += P(self) + self.space.place_to_empty(self.sets) + + m = M() + m.space.move_to_available(m.sets) + + +API reference +------------- + .. autoclass:: Grid :members: :inherited-members: From 0c82492d4915c2e6344712c14e26c514e2402fd6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 21 Sep 2025 18:58:01 +0000 Subject: [PATCH 209/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/api/index.rst | 2 +- docs/api/reference/agents/index.rst | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/api/index.rst b/docs/api/index.rst index f848c6f7..08b51f97 100644 --- a/docs/api/index.rst +++ b/docs/api/index.rst @@ -29,7 +29,7 @@ Mini usage flow :link-type: doc ``Model`` API for registering sets, stepping the simulation, and integrating with datacollectors/reporters. - + .. grid-item-card:: Spatial support :link: reference/space/index :link-type: doc diff --git a/docs/api/reference/agents/index.rst b/docs/api/reference/agents/index.rst index 69287af9..082be02c 100644 --- a/docs/api/reference/agents/index.rst +++ b/docs/api/reference/agents/index.rst @@ -6,9 +6,9 @@ Agents Quick intro ----------- -- ``AgentSet`` stores agents as rows in a Polars-backed table and provides vectorised operations for high-performance updates. +- ``AgentSet`` stores agents as rows in a Polars-backed table and provides vectorised operations for high-performance updates. -- ``AgentSetRegistry`` (available at ``model.sets``) is the container that holds all ``AgentSet`` instances for a model and provides convenience operations (add/remove sets, step all sets, rename). +- ``AgentSetRegistry`` (available at ``model.sets``) is the container that holds all ``AgentSet`` instances for a model and provides convenience operations (add/remove sets, step all sets, rename). - Keep agent logic column-oriented and prefer Polars expressions for updates. From 82bcc78eb13fcb8421f299635c569ecd5ad5ea10 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 21:06:10 +0200 Subject: [PATCH 210/329] docs: add custom branding CSS and JS files to documentation --- docs/api/_static/mesa_brand.css | 128 ++++++++++++++++++++++++++++++++ docs/api/_static/mesa_brand.js | 43 +++++++++++ docs/api/conf.py | 9 +++ 3 files changed, 180 insertions(+) create mode 100644 docs/api/_static/mesa_brand.css create mode 100644 docs/api/_static/mesa_brand.js diff --git a/docs/api/_static/mesa_brand.css b/docs/api/_static/mesa_brand.css new file mode 100644 index 00000000..0a4d9fc0 --- /dev/null +++ b/docs/api/_static/mesa_brand.css @@ -0,0 +1,128 @@ +/* Mesa Frames branding overrides for pydata_sphinx_theme + - Defines CSS variables for light/dark modes + - Hero gradient, navbar contrast, CTA, code/table, badges, admonition styles +*/ +:root{ + /* Brand colors */ + --mesa-primary: #a6c1dd; /* primary actions */ + --mesa-surface: #c7d9ec; /* background panels */ + --mesa-dark: #060808; /* text / dark accents */ + + /* Derived tokens */ + --mesa-primary-text: var(--mesa-dark); + --mesa-surface-contrast: rgba(6,8,8,0.9); + --mesa-shadow: rgba(6,8,8,0.12); +} + +/* Dark mode variables - applied when document has data-mode="dark" or .theme-dark */ +:root[data-mode="dark"], .theme-dark { + --mesa-background: var(--mesa-dark); + --mesa-surface: #0d1213; /* desaturated charcoal for surfaces in dark mode */ + --mesa-primary: #a6c1dd; /* keep accent */ + --mesa-primary-text: #ffffff; + --mesa-surface-contrast: rgba(166,193,221,0.12); + --mesa-shadow: rgba(0,0,0,0.6); +} + +/* Hero gradient behind top-of-page header */ +.pydata-header { + background: linear-gradient(135deg, var(--mesa-surface) 0%, var(--mesa-primary) 100%); + color: var(--mesa-dark); +} + +/* Navbar contrast and hover states */ +.pydata-navbar, .navbar { + background-color: var(--mesa-dark) !important; + color: var(--mesa-primary) !important; +} +.pydata-navbar a.nav-link, .navbar a.nav-link, .pydata-navbar .navbar-brand { + color: var(--mesa-primary) !important; +} +.pydata-navbar a.nav-link:hover, .navbar a.nav-link:hover { + background-color: rgba(199,217,236,0.07); + color: var(--mesa-surface-contrast) !important; +} + +/* Transparent overlay for nav items on hover */ +.pydata-navbar .nav-link:hover::after, .navbar .nav-link:hover::after{ + content: ""; + position: absolute; + inset: 0; + background: rgba(6,8,8,0.15); + border-radius: 6px; +} + +/* CTA buttons using sphinx-design components */ +.sd-button, .sd-button .sd-button--primary, .sd-btn, .sphinx-button { + border-radius: 10px; + box-shadow: 0 6px 18px var(--mesa-shadow); +} + +/* Primary CTA: dark text on surface */ +.btn-mesa-primary, .sd-button--mesa-primary { + background: var(--mesa-surface) !important; + color: var(--mesa-primary-text) !important; + border: 1px solid rgba(6,8,8,0.06); +} +/* Secondary CTA: inverted */ +.btn-mesa-secondary, .sd-button--mesa-secondary { + background: var(--mesa-dark) !important; + color: #fff !important; + border: 1px solid rgba(166,193,221,0.06); +} + +/* Add small white SVG icon space inside CTA */ +.btn-mesa-primary svg, .btn-mesa-secondary svg { + width: 18px; height: 18px; vertical-align: middle; margin-right: 8px; fill: #fff; +} + +/* Cards and tiles */ +.sd-card, .card, .sphinx-design-card { + border-radius: 12px; + background: var(--mesa-surface); + color: var(--mesa-dark); + box-shadow: 0 8px 20px var(--mesa-shadow); +} + +/* Code block and table legibility */ +.highlight, .literal-block, pre, .py, code { + background-color: rgba(199,217,236,0.18); /* light tint */ + border-radius: 8px; + padding: 0.6rem 0.9rem; + color: var(--mesa-dark); +} +:root[data-mode="dark"] .highlight, .theme-dark .highlight, :root[data-mode="dark"] pre, .theme-dark pre { + background-color: #111516; /* desaturated charcoal */ + color: #e6eef6; +} + +/* Highlight keywords with medium blue to align syntax */ +.highlight .k, .highlight .kn, .highlight .c1, .highlight .gp { color: var(--mesa-primary) !important; } + +/* Badges and pill links */ +.mesa-badge { + display: inline-block; + padding: 0.15rem 0.6rem; + border-radius: 999px; + background: var(--mesa-dark); + color: var(--mesa-primary); + font-weight: 600; + box-shadow: 0 4px 10px rgba(6,8,8,0.12); +} + +/* Admonitions / callouts */ +.admonition { + border-left: 4px solid rgba(6,8,8,0.12); + background: linear-gradient(180deg, rgba(199,217,236,0.06), rgba(166,193,221,0.02)); + border-radius: 8px; + padding: 0.8rem 1rem; +} +.admonition.note { background-color: rgba(199,217,236,0.06); } +.admonition.tip { background-color: rgba(166,193,221,0.04); } +.admonition.warning { background-color: rgba(255,230,120,0.04); border-left-color: rgba(255,170,0,0.8); } + +/* Small responsive tweaks */ +@media (max-width: 720px){ + .pydata-header { padding: 1rem 0; } + .sd-card, .card { margin-bottom: 0.75rem; } +} diff --git a/docs/api/_static/mesa_brand.js b/docs/api/_static/mesa_brand.js new file mode 100644 index 00000000..b1e18f81 --- /dev/null +++ b/docs/api/_static/mesa_brand.js @@ -0,0 +1,43 @@ +// Small script to add a theme toggle to the navbar and integrate with pydata theme +(function(){ + function createToggle(){ + try{ + var btn = document.createElement('button'); + btn.className = 'theme-switch-button btn btn-sm'; + btn.type = 'button'; + btn.title = 'Toggle theme'; + btn.setAttribute('aria-label','Toggle theme'); + btn.innerHTML = ''; + var container = document.querySelector('.navbar-icon-links') || document.querySelector('.bd-navbar-elements') || document.querySelector('.navbar .navbar-nav') || document.querySelector('.pydata-navbar .navbar-nav'); + if(container){ + var li = document.createElement('li'); + li.className = 'nav-item'; + var a = document.createElement('a'); + a.className = 'nav-link'; + a.href = '#'; + a.appendChild(btn); + li.appendChild(a); + // insert at the end of the list so we don't disrupt other items + container.appendChild(li); + + btn.addEventListener('click', function(e){ + e.preventDefault(); + // Try to reuse pydata theme switch if available + try{ + // cycleMode function may be defined by pydata theme; call if present + if(typeof cycleMode === 'function'){ + cycleMode(); + return; + } + // fallback: toggle data-mode between dark and light and persist + var current = document.documentElement.getAttribute('data-mode') || ''; + var next = (current === 'dark') ? 'light' : 'dark'; + document.documentElement.setAttribute('data-mode', next); + document.documentElement.dataset.mode = next; + try{ localStorage.setItem('mode', next); }catch(e){} + }catch(err){ console.warn('Theme toggle failed', err);} + }); + } + }catch(e){console.warn('mesa_brand.js init fail',e);} } + document.addEventListener('DOMContentLoaded', createToggle); +})(); diff --git a/docs/api/conf.py b/docs/api/conf.py index 43098ec2..924b0644 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -35,6 +35,15 @@ html_static_path = ["_static"] html_show_sourcelink = False +# Add custom branding CSS/JS (mesa_brand) to static files +html_css_files = [ + "mesa_brand.css", +] + +html_js_files = [ + "mesa_brand.js", +] + # -- Extension settings ------------------------------------------------------ # intersphinx mapping intersphinx_mapping = { From fb3bdc4b6f364800194cad07c1288b8899747735 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 21:11:53 +0200 Subject: [PATCH 211/329] docs: remove mesa_brand.js and update theme switcher integration in conf.py --- docs/api/_static/mesa_brand.js | 43 ---------------------------------- docs/api/conf.py | 6 +---- 2 files changed, 1 insertion(+), 48 deletions(-) delete mode 100644 docs/api/_static/mesa_brand.js diff --git a/docs/api/_static/mesa_brand.js b/docs/api/_static/mesa_brand.js deleted file mode 100644 index b1e18f81..00000000 --- a/docs/api/_static/mesa_brand.js +++ /dev/null @@ -1,43 +0,0 @@ -// Small script to add a theme toggle to the navbar and integrate with pydata theme -(function(){ - function createToggle(){ - try{ - var btn = document.createElement('button'); - btn.className = 'theme-switch-button btn btn-sm'; - btn.type = 'button'; - btn.title = 'Toggle theme'; - btn.setAttribute('aria-label','Toggle theme'); - btn.innerHTML = ''; - var container = document.querySelector('.navbar-icon-links') || document.querySelector('.bd-navbar-elements') || document.querySelector('.navbar .navbar-nav') || document.querySelector('.pydata-navbar .navbar-nav'); - if(container){ - var li = document.createElement('li'); - li.className = 'nav-item'; - var a = document.createElement('a'); - a.className = 'nav-link'; - a.href = '#'; - a.appendChild(btn); - li.appendChild(a); - // insert at the end of the list so we don't disrupt other items - container.appendChild(li); - - btn.addEventListener('click', function(e){ - e.preventDefault(); - // Try to reuse pydata theme switch if available - try{ - // cycleMode function may be defined by pydata theme; call if present - if(typeof cycleMode === 'function'){ - cycleMode(); - return; - } - // fallback: toggle data-mode between dark and light and persist - var current = document.documentElement.getAttribute('data-mode') || ''; - var next = (current === 'dark') ? 'light' : 'dark'; - document.documentElement.setAttribute('data-mode', next); - document.documentElement.dataset.mode = next; - try{ localStorage.setItem('mode', next); }catch(e){} - }catch(err){ console.warn('Theme toggle failed', err);} - }); - } - }catch(e){console.warn('mesa_brand.js init fail',e);} } - document.addEventListener('DOMContentLoaded', createToggle); -})(); diff --git a/docs/api/conf.py b/docs/api/conf.py index 924b0644..4047d320 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -40,10 +40,6 @@ "mesa_brand.css", ] -html_js_files = [ - "mesa_brand.js", -] - # -- Extension settings ------------------------------------------------------ # intersphinx mapping intersphinx_mapping = { @@ -83,5 +79,5 @@ "icon": "fa-brands fa-github", }, ], - "navbar_end": ["navbar-icon-links"], + "navbar_end": ["theme-switcher", "navbar-icon-links"], } From 46c0fd37124c976fc86e0cdbffee11a447b47619 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 21:12:04 +0200 Subject: [PATCH 212/329] docs: update .gitignore to exclude site and API build directories --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 41ce8f27..198af709 100644 --- a/.gitignore +++ b/.gitignore @@ -154,4 +154,5 @@ cython_debug/ *.code-workspace llm_rules.md .python-version -docs/site \ No newline at end of file +docs/site +docs/api/_build \ No newline at end of file From bcc055f28c397c18b93592f99911635eb54830c8 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 21:42:34 +0200 Subject: [PATCH 213/329] docs: add brand variables and theme adapters for improved styling --- docs/api/_static/brand-core.css | 9 ++ docs/api/_static/brand-pydata.css | 19 +++++ docs/api/_static/mesa_brand.css | 128 ---------------------------- docs/api/conf.py | 4 +- docs/stylesheets/brand-material.css | 18 ++++ mkdocs.yml | 5 ++ 6 files changed, 54 insertions(+), 129 deletions(-) create mode 100644 docs/api/_static/brand-core.css create mode 100644 docs/api/_static/brand-pydata.css delete mode 100644 docs/api/_static/mesa_brand.css create mode 100644 docs/stylesheets/brand-material.css diff --git a/docs/api/_static/brand-core.css b/docs/api/_static/brand-core.css new file mode 100644 index 00000000..32cb4906 --- /dev/null +++ b/docs/api/_static/brand-core.css @@ -0,0 +1,9 @@ +/* Mesa-Frames shared brand variables (core) */ +:root{ + /* Brand palette */ + --mf-primary: #A6C1DD; + --mf-surface: #C7D9EC; + --mf-dark: #060808; + --mf-fg-dark: #0F1113; + --mf-fg-light: #E8EEF6; +} diff --git a/docs/api/_static/brand-pydata.css b/docs/api/_static/brand-pydata.css new file mode 100644 index 00000000..c9039617 --- /dev/null +++ b/docs/api/_static/brand-pydata.css @@ -0,0 +1,19 @@ +/* PyData theme adapter: maps Mesa-Frames brand variables to pydata tokens */ +:root{ + --pst-color-primary: var(--mf-primary); + --pst-content-max-width: 1100px; +} + +:root[data-mode="dark"]{ + --pst-color-background: var(--mf-dark); + --pst-color-on-background: var(--mf-fg-light); + --pst-color-surface: #111516; +} + +/* Optional gentle polish */ +.bd-header{ + background: linear-gradient(135deg, var(--mf-surface), var(--mf-primary)); +} +.card, .sd-card, .sphinx-design-card{ border-radius:12px; box-shadow:0 4px 16px rgba(0,0,0,.06) } +:root[data-mode="dark"] .card, :root[data-mode="dark"] .sd-card{ box-shadow:0 6px 22px rgba(0,0,0,.45) } +pre, .highlight{ border-radius:8px } diff --git a/docs/api/_static/mesa_brand.css b/docs/api/_static/mesa_brand.css deleted file mode 100644 index 0a4d9fc0..00000000 --- a/docs/api/_static/mesa_brand.css +++ /dev/null @@ -1,128 +0,0 @@ -/* Mesa Frames branding overrides for pydata_sphinx_theme - - Defines CSS variables for light/dark modes - - Hero gradient, navbar contrast, CTA, code/table, badges, admonition styles -*/ -:root{ - /* Brand colors */ - --mesa-primary: #a6c1dd; /* primary actions */ - --mesa-surface: #c7d9ec; /* background panels */ - --mesa-dark: #060808; /* text / dark accents */ - - /* Derived tokens */ - --mesa-primary-text: var(--mesa-dark); - --mesa-surface-contrast: rgba(6,8,8,0.9); - --mesa-shadow: rgba(6,8,8,0.12); -} - -/* Dark mode variables - applied when document has data-mode="dark" or .theme-dark */ -:root[data-mode="dark"], .theme-dark { - --mesa-background: var(--mesa-dark); - --mesa-surface: #0d1213; /* desaturated charcoal for surfaces in dark mode */ - --mesa-primary: #a6c1dd; /* keep accent */ - --mesa-primary-text: #ffffff; - --mesa-surface-contrast: rgba(166,193,221,0.12); - --mesa-shadow: rgba(0,0,0,0.6); -} - -/* Hero gradient behind top-of-page header */ -.pydata-header { - background: linear-gradient(135deg, var(--mesa-surface) 0%, var(--mesa-primary) 100%); - color: var(--mesa-dark); -} - -/* Navbar contrast and hover states */ -.pydata-navbar, .navbar { - background-color: var(--mesa-dark) !important; - color: var(--mesa-primary) !important; -} -.pydata-navbar a.nav-link, .navbar a.nav-link, .pydata-navbar .navbar-brand { - color: var(--mesa-primary) !important; -} -.pydata-navbar a.nav-link:hover, .navbar a.nav-link:hover { - background-color: rgba(199,217,236,0.07); - color: var(--mesa-surface-contrast) !important; -} - -/* Transparent overlay for nav items on hover */ -.pydata-navbar .nav-link:hover::after, .navbar .nav-link:hover::after{ - content: ""; - position: absolute; - inset: 0; - background: rgba(6,8,8,0.15); - border-radius: 6px; -} - -/* CTA buttons using sphinx-design components */ -.sd-button, .sd-button .sd-button--primary, .sd-btn, .sphinx-button { - border-radius: 10px; - box-shadow: 0 6px 18px var(--mesa-shadow); -} - -/* Primary CTA: dark text on surface */ -.btn-mesa-primary, .sd-button--mesa-primary { - background: var(--mesa-surface) !important; - color: var(--mesa-primary-text) !important; - border: 1px solid rgba(6,8,8,0.06); -} -/* Secondary CTA: inverted */ -.btn-mesa-secondary, .sd-button--mesa-secondary { - background: var(--mesa-dark) !important; - color: #fff !important; - border: 1px solid rgba(166,193,221,0.06); -} - -/* Add small white SVG icon space inside CTA */ -.btn-mesa-primary svg, .btn-mesa-secondary svg { - width: 18px; height: 18px; vertical-align: middle; margin-right: 8px; fill: #fff; -} - -/* Cards and tiles */ -.sd-card, .card, .sphinx-design-card { - border-radius: 12px; - background: var(--mesa-surface); - color: var(--mesa-dark); - box-shadow: 0 8px 20px var(--mesa-shadow); -} - -/* Code block and table legibility */ -.highlight, .literal-block, pre, .py, code { - background-color: rgba(199,217,236,0.18); /* light tint */ - border-radius: 8px; - padding: 0.6rem 0.9rem; - color: var(--mesa-dark); -} -:root[data-mode="dark"] .highlight, .theme-dark .highlight, :root[data-mode="dark"] pre, .theme-dark pre { - background-color: #111516; /* desaturated charcoal */ - color: #e6eef6; -} - -/* Highlight keywords with medium blue to align syntax */ -.highlight .k, .highlight .kn, .highlight .c1, .highlight .gp { color: var(--mesa-primary) !important; } - -/* Badges and pill links */ -.mesa-badge { - display: inline-block; - padding: 0.15rem 0.6rem; - border-radius: 999px; - background: var(--mesa-dark); - color: var(--mesa-primary); - font-weight: 600; - box-shadow: 0 4px 10px rgba(6,8,8,0.12); -} - -/* Admonitions / callouts */ -.admonition { - border-left: 4px solid rgba(6,8,8,0.12); - background: linear-gradient(180deg, rgba(199,217,236,0.06), rgba(166,193,221,0.02)); - border-radius: 8px; - padding: 0.8rem 1rem; -} -.admonition.note { background-color: rgba(199,217,236,0.06); } -.admonition.tip { background-color: rgba(166,193,221,0.04); } -.admonition.warning { background-color: rgba(255,230,120,0.04); border-left-color: rgba(255,170,0,0.8); } - -/* Small responsive tweaks */ -@media (max-width: 720px){ - .pydata-header { padding: 1rem 0; } - .sd-card, .card { margin-bottom: 0.75rem; } -} diff --git a/docs/api/conf.py b/docs/api/conf.py index 4047d320..418b4d23 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -37,7 +37,9 @@ # Add custom branding CSS/JS (mesa_brand) to static files html_css_files = [ - "mesa_brand.css", + # Shared brand variables then theme adapter for pydata + "brand-core.css", + "brand-pydata.css", ] # -- Extension settings ------------------------------------------------------ diff --git a/docs/stylesheets/brand-material.css b/docs/stylesheets/brand-material.css new file mode 100644 index 00000000..d6623334 --- /dev/null +++ b/docs/stylesheets/brand-material.css @@ -0,0 +1,18 @@ +/* Material theme adapter: maps Mesa-Frames brand variables to Material tokens */ +/* Light scheme */ +:root{ + --md-primary-fg-color: var(--mf-primary); + --md-primary-fg-color--light: #D7E3F2; + --md-primary-fg-color--dark: #6F92B5; +} + +/* Dark scheme (slate) */ +[data-md-color-scheme="slate"]{ + --md-default-bg-color: var(--mf-dark); + --md-default-fg-color: var(--mf-fg-light); + --md-primary-fg-color: var(--mf-primary); + --md-code-bg-color: #111516; +} + +/* Optional: soft hero tint */ +.md-header { background: linear-gradient(135deg, var(--mf-surface), var(--mf-primary)); } diff --git a/mkdocs.yml b/mkdocs.yml index 331165b5..f8ae79dd 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -96,6 +96,11 @@ extra_javascript: - https://polyfill.io/v3/polyfill.min.js?features=es6 - https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js +# Custom CSS for branding (brand-core then material adapter) +extra_css: + - stylesheets/brand-core.css + - stylesheets/brand-material.css + # Customization extra: social: From f0ee97475046314516e2d89e2424cb134bd69864 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 21:57:52 +0200 Subject: [PATCH 214/329] docs: remove obsolete brand CSS files for cleaner styling integration --- docs/api/_static/brand-core.css | 9 --------- docs/api/_static/brand-pydata.css | 19 ------------------- docs/stylesheets/brand-material.css | 18 ------------------ 3 files changed, 46 deletions(-) diff --git a/docs/api/_static/brand-core.css b/docs/api/_static/brand-core.css index 32cb4906..e69de29b 100644 --- a/docs/api/_static/brand-core.css +++ b/docs/api/_static/brand-core.css @@ -1,9 +0,0 @@ -/* Mesa-Frames shared brand variables (core) */ -:root{ - /* Brand palette */ - --mf-primary: #A6C1DD; - --mf-surface: #C7D9EC; - --mf-dark: #060808; - --mf-fg-dark: #0F1113; - --mf-fg-light: #E8EEF6; -} diff --git a/docs/api/_static/brand-pydata.css b/docs/api/_static/brand-pydata.css index c9039617..e69de29b 100644 --- a/docs/api/_static/brand-pydata.css +++ b/docs/api/_static/brand-pydata.css @@ -1,19 +0,0 @@ -/* PyData theme adapter: maps Mesa-Frames brand variables to pydata tokens */ -:root{ - --pst-color-primary: var(--mf-primary); - --pst-content-max-width: 1100px; -} - -:root[data-mode="dark"]{ - --pst-color-background: var(--mf-dark); - --pst-color-on-background: var(--mf-fg-light); - --pst-color-surface: #111516; -} - -/* Optional gentle polish */ -.bd-header{ - background: linear-gradient(135deg, var(--mf-surface), var(--mf-primary)); -} -.card, .sd-card, .sphinx-design-card{ border-radius:12px; box-shadow:0 4px 16px rgba(0,0,0,.06) } -:root[data-mode="dark"] .card, :root[data-mode="dark"] .sd-card{ box-shadow:0 6px 22px rgba(0,0,0,.45) } -pre, .highlight{ border-radius:8px } diff --git a/docs/stylesheets/brand-material.css b/docs/stylesheets/brand-material.css index d6623334..e69de29b 100644 --- a/docs/stylesheets/brand-material.css +++ b/docs/stylesheets/brand-material.css @@ -1,18 +0,0 @@ -/* Material theme adapter: maps Mesa-Frames brand variables to Material tokens */ -/* Light scheme */ -:root{ - --md-primary-fg-color: var(--mf-primary); - --md-primary-fg-color--light: #D7E3F2; - --md-primary-fg-color--dark: #6F92B5; -} - -/* Dark scheme (slate) */ -[data-md-color-scheme="slate"]{ - --md-default-bg-color: var(--mf-dark); - --md-default-fg-color: var(--mf-fg-light); - --md-primary-fg-color: var(--mf-primary); - --md-code-bg-color: #111516; -} - -/* Optional: soft hero tint */ -.md-header { background: linear-gradient(135deg, var(--mf-surface), var(--mf-primary)); } From 03913525ad9d23841e9d973af4b6cd28965af2a9 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 22:13:26 +0200 Subject: [PATCH 215/329] .gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 198af709..ca2da040 100644 --- a/.gitignore +++ b/.gitignore @@ -155,4 +155,6 @@ cython_debug/ llm_rules.md .python-version docs/site -docs/api/_build \ No newline at end of file +docs/api/_build +docs/general/user-guide/data_csv +docs/general/user-guide/data_parquet From f11b62f199ecf161459b176337b77395d959501d Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 22:53:09 +0200 Subject: [PATCH 216/329] docs: update navigation structure in conf.py and index.rst for improved accessibility --- docs/api/conf.py | 4 ++++ docs/api/index.rst | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/docs/api/conf.py b/docs/api/conf.py index 418b4d23..6223a36b 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -81,5 +81,9 @@ "icon": "fa-brands fa-github", }, ], + "navbar_start": ["navbar-logo"] + + , + "navbar_end": ["theme-switcher", "navbar-icon-links"], } diff --git a/docs/api/index.rst b/docs/api/index.rst index f848c6f7..630e6e43 100644 --- a/docs/api/index.rst +++ b/docs/api/index.rst @@ -1,6 +1,18 @@ mesa-frames API =============== +.. toctree:: + :caption: Shortcuts + :maxdepth: 1 + :hidden: + + reference/agents/index + reference/model + reference/space/index + reference/datacollector + + + Overview -------- From e9c982d51753af7206b0a6dc54190ef36ca638ae Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 21 Sep 2025 23:06:08 +0200 Subject: [PATCH 217/329] docs: clean up navbar configuration and fix formatting in API documentation --- docs/api/conf.py | 5 +---- docs/api/index.rst | 2 +- docs/api/reference/agents/index.rst | 4 ++-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/docs/api/conf.py b/docs/api/conf.py index 6223a36b..15512cd6 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -81,9 +81,6 @@ "icon": "fa-brands fa-github", }, ], - "navbar_start": ["navbar-logo"] - - , - + "navbar_start": ["navbar-logo"], "navbar_end": ["theme-switcher", "navbar-icon-links"], } diff --git a/docs/api/index.rst b/docs/api/index.rst index 630e6e43..a7c2ab4c 100644 --- a/docs/api/index.rst +++ b/docs/api/index.rst @@ -41,7 +41,7 @@ Mini usage flow :link-type: doc ``Model`` API for registering sets, stepping the simulation, and integrating with datacollectors/reporters. - + .. grid-item-card:: Spatial support :link: reference/space/index :link-type: doc diff --git a/docs/api/reference/agents/index.rst b/docs/api/reference/agents/index.rst index 69287af9..082be02c 100644 --- a/docs/api/reference/agents/index.rst +++ b/docs/api/reference/agents/index.rst @@ -6,9 +6,9 @@ Agents Quick intro ----------- -- ``AgentSet`` stores agents as rows in a Polars-backed table and provides vectorised operations for high-performance updates. +- ``AgentSet`` stores agents as rows in a Polars-backed table and provides vectorised operations for high-performance updates. -- ``AgentSetRegistry`` (available at ``model.sets``) is the container that holds all ``AgentSet`` instances for a model and provides convenience operations (add/remove sets, step all sets, rename). +- ``AgentSetRegistry`` (available at ``model.sets``) is the container that holds all ``AgentSet`` instances for a model and provides convenience operations (add/remove sets, step all sets, rename). - Keep agent logic column-oriented and prefer Polars expressions for updates. From 620ece950ebd1d76ea31a34572593c7e14f0b53f Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 14:34:44 +0200 Subject: [PATCH 218/329] docs: update TOC settings and enhance autodoc options for better documentation clarity --- docs/api/conf.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/api/conf.py b/docs/api/conf.py index 15512cd6..8162bd67 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -31,6 +31,10 @@ exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] # -- Options for HTML output ------------------------------------------------- +# Hide objects (classes/methods) from the page Table of Contents +toc_object_entries = False # NEW: stop adding class/method entries to the TOC + + html_theme = "pydata_sphinx_theme" html_static_path = ["_static"] html_show_sourcelink = False @@ -59,9 +63,18 @@ copybutton_prompt_is_regexp = True # -- Custom configurations --------------------------------------------------- +add_module_names = False autoclass_content = "class" autodoc_member_order = "bysource" -autodoc_default_options = {"special-members": True, "exclude-members": "__weakref__"} +autodoc_default_options = { + "members": True, + "inherited-members": True, + "undoc-members": True, + "member-order": "bysource", + "special-members": True, + "exclude-members": "__weakref__,__dict__,__module__,__annotations__", +} + # -- GitHub link and user guide settings ------------------------------------- github_root = "https://github.com/projectmesa/mesa-frames" From 0ec7fe44eec67897fb5a5a300475e56c2dcaf190 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 14:35:08 +0200 Subject: [PATCH 219/329] docs: restructure API reference for AgentSet and AgentSetRegistry with detailed autosummary sections --- docs/api/reference/agents/index.rst | 153 +++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 13 deletions(-) diff --git a/docs/api/reference/agents/index.rst b/docs/api/reference/agents/index.rst index 082be02c..9a620dea 100644 --- a/docs/api/reference/agents/index.rst +++ b/docs/api/reference/agents/index.rst @@ -40,16 +40,143 @@ Minimal example m.sets.do("step") API reference --------------------------------- - -.. autoclass:: AgentSet - :members: - :inherited-members: - :autosummary: - :autosummary-nosignatures: - -.. autoclass:: AgentSetRegistry - :members: - :inherited-members: - :autosummary: - :autosummary-nosignatures: +--------------------------------- + +.. tab-set:: + + .. tab-item:: AgentSet + + .. tab-set:: + + .. tab-item:: Overview + + .. rubric:: Lifecycle / Core + + .. autosummary:: + :nosignatures: + :toctree: _autosummary + + AgentSet.__init__ + AgentSet.step + AgentSet.rename + AgentSet.copy + + .. rubric:: Accessors & Views + + .. autosummary:: + :nosignatures: + :toctree: _autosummary + + AgentSet.df + AgentSet.active_agents + AgentSet.inactive_agents + AgentSet.index + AgentSet.pos + AgentSet.name + AgentSet.get + AgentSet.contains + AgentSet.__len__ + AgentSet.__iter__ + AgentSet.__getitem__ + AgentSet.__contains__ + + .. rubric:: Mutators + + .. autosummary:: + :nosignatures: + :toctree: _autosummary + + AgentSet.add + AgentSet.remove + AgentSet.discard + AgentSet.set + AgentSet.select + AgentSet.shuffle + AgentSet.sort + AgentSet.do + + .. rubric:: Operators / Internal helpers + + .. autosummary:: + :nosignatures: + :toctree: _autosummary + + AgentSet.__add__ + AgentSet.__iadd__ + AgentSet.__sub__ + AgentSet.__isub__ + AgentSet.__repr__ + AgentSet.__reversed__ + + .. tab-item:: Full API + + .. autoclass:: AgentSet + + .. tab-item:: AgentSetRegistry + + .. tab-set:: + + .. tab-item:: Overview + + .. rubric:: Lifecycle / Core + + .. autosummary:: + :nosignatures: + :toctree: _autosummary + + AgentSetRegistry.__init__ + AgentSetRegistry.copy + AgentSetRegistry.rename + + .. rubric:: Accessors & Queries + + .. autosummary:: + :nosignatures: + :toctree: _autosummary + + AgentSetRegistry.get + AgentSetRegistry.contains + AgentSetRegistry.ids + AgentSetRegistry.keys + AgentSetRegistry.items + AgentSetRegistry.values + AgentSetRegistry.model + AgentSetRegistry.random + AgentSetRegistry.space + AgentSetRegistry.__len__ + AgentSetRegistry.__iter__ + AgentSetRegistry.__getitem__ + AgentSetRegistry.__contains__ + + .. rubric:: Mutators / Coordination + + .. autosummary:: + :nosignatures: + :toctree: _autosummary + + AgentSetRegistry.add + AgentSetRegistry.remove + AgentSetRegistry.discard + AgentSetRegistry.replace + AgentSetRegistry.shuffle + AgentSetRegistry.sort + AgentSetRegistry.do + AgentSetRegistry.__setitem__ + AgentSetRegistry.__add__ + AgentSetRegistry.__iadd__ + AgentSetRegistry.__sub__ + AgentSetRegistry.__isub__ + + .. rubric:: Representation + + .. autosummary:: + :nosignatures: + :toctree: _autosummary + + AgentSetRegistry.__repr__ + AgentSetRegistry.__str__ + AgentSetRegistry.__reversed__ + + .. tab-item:: Full API + + .. autoclass:: AgentSetRegistry \ No newline at end of file From 5927c609f67629c11da5db637347ffa2b1720b56 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 15:01:05 +0200 Subject: [PATCH 220/329] docs: add mesa_frames RST files to .gitignore to prevent tracking --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ca2da040..ca0ad990 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,4 @@ docs/site docs/api/_build docs/general/user-guide/data_csv docs/general/user-guide/data_parquet +docs/api/reference/**/mesa_frames.*.rst \ No newline at end of file From 66dbd5edd1a1bf92061e84abbb84d60f2465c8ba Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 15:01:18 +0200 Subject: [PATCH 221/329] docs: update autosummary toctree settings for AgentSet and AgentSetRegistry --- docs/api/reference/agents/index.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/api/reference/agents/index.rst b/docs/api/reference/agents/index.rst index 9a620dea..b7ed147c 100644 --- a/docs/api/reference/agents/index.rst +++ b/docs/api/reference/agents/index.rst @@ -54,7 +54,7 @@ API reference .. autosummary:: :nosignatures: - :toctree: _autosummary + :toctree: AgentSet.__init__ AgentSet.step @@ -65,7 +65,7 @@ API reference .. autosummary:: :nosignatures: - :toctree: _autosummary + :toctree: AgentSet.df AgentSet.active_agents @@ -84,7 +84,7 @@ API reference .. autosummary:: :nosignatures: - :toctree: _autosummary + :toctree: AgentSet.add AgentSet.remove @@ -99,7 +99,7 @@ API reference .. autosummary:: :nosignatures: - :toctree: _autosummary + :toctree: AgentSet.__add__ AgentSet.__iadd__ @@ -122,7 +122,7 @@ API reference .. autosummary:: :nosignatures: - :toctree: _autosummary + :toctree: AgentSetRegistry.__init__ AgentSetRegistry.copy @@ -132,7 +132,7 @@ API reference .. autosummary:: :nosignatures: - :toctree: _autosummary + :toctree: AgentSetRegistry.get AgentSetRegistry.contains @@ -152,7 +152,7 @@ API reference .. autosummary:: :nosignatures: - :toctree: _autosummary + :toctree: AgentSetRegistry.add AgentSetRegistry.remove @@ -171,7 +171,7 @@ API reference .. autosummary:: :nosignatures: - :toctree: _autosummary + :toctree: AgentSetRegistry.__repr__ AgentSetRegistry.__str__ From 8373dbebf69d2293668091d053e2c8d851689f2e Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 15:16:17 +0200 Subject: [PATCH 222/329] docs: enhance API documentation structure with tabs and autosummary for better clarity --- docs/api/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/conf.py b/docs/api/conf.py index 8162bd67..61d34eb4 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -72,7 +72,7 @@ "undoc-members": True, "member-order": "bysource", "special-members": True, - "exclude-members": "__weakref__,__dict__,__module__,__annotations__", + "exclude-members": "__weakref__,__dict__,__module__,__annotations__,__firstlineno__,__static_attributes__,__abstractmethods__,__slots__" } From 5b76c0ad05f321c04292b62c9ace9853b5d0c6de Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 15:16:56 +0200 Subject: [PATCH 223/329] docs: enhance API reference structure with tabs and autosummary for improved clarity --- docs/api/reference/agents/index.rst | 6 ++++- docs/api/reference/datacollector.rst | 34 ++++++++++++++++++++++---- docs/api/reference/model.rst | 36 ++++++++++++++++++++++++---- docs/api/reference/space/index.rst | 30 +++++++++++++++++++---- 4 files changed, 90 insertions(+), 16 deletions(-) diff --git a/docs/api/reference/agents/index.rst b/docs/api/reference/agents/index.rst index b7ed147c..8904e6ff 100644 --- a/docs/api/reference/agents/index.rst +++ b/docs/api/reference/agents/index.rst @@ -111,6 +111,8 @@ API reference .. tab-item:: Full API .. autoclass:: AgentSet + :autosummary: + :autosummary-nosignatures: .. tab-item:: AgentSetRegistry @@ -179,4 +181,6 @@ API reference .. tab-item:: Full API - .. autoclass:: AgentSetRegistry \ No newline at end of file + .. autoclass:: AgentSetRegistry + :autosummary: + :autosummary-nosignatures: \ No newline at end of file diff --git a/docs/api/reference/datacollector.rst b/docs/api/reference/datacollector.rst index 017bd18d..f1f2c68e 100644 --- a/docs/api/reference/datacollector.rst +++ b/docs/api/reference/datacollector.rst @@ -37,8 +37,32 @@ Minimal example API reference ------------- -.. autoclass:: DataCollector - :members: - :inherited-members: - :autosummary: - :autosummary-nosignatures: \ No newline at end of file +.. tab-set:: + + .. tab-item:: Overview + + .. rubric:: Lifecycle / Core + + .. autosummary:: + :nosignatures: + :toctree: + + DataCollector.__init__ + DataCollector.collect + DataCollector.conditional_collect + DataCollector.flush + DataCollector.data + + .. rubric:: Reporting / Internals + + .. autosummary:: + :nosignatures: + :toctree: + + DataCollector.seed + + .. tab-item:: Full API + + .. autoclass:: DataCollector + :autosummary: + :autosummary-nosignatures: \ No newline at end of file diff --git a/docs/api/reference/model.rst b/docs/api/reference/model.rst index 2b0b2102..74b7e4e5 100644 --- a/docs/api/reference/model.rst +++ b/docs/api/reference/model.rst @@ -36,8 +36,34 @@ Minimal example API reference ------------- -.. autoclass:: Model - :members: - :inherited-members: - :autosummary: - :autosummary-nosignatures: \ No newline at end of file +.. tab-set:: + + .. tab-item:: Overview + + .. rubric:: Lifecycle / Core + + .. autosummary:: + :nosignatures: + :toctree: + + Model.__init__ + Model.step + Model.run_model + Model.reset_randomizer + + .. rubric:: Accessors / Properties + + .. autosummary:: + :nosignatures: + :toctree: + + Model.steps + Model.sets + Model.space + Model.seed + + .. tab-item:: Full API + + .. autoclass:: Model + :autosummary: + :autosummary-nosignatures: \ No newline at end of file diff --git a/docs/api/reference/space/index.rst b/docs/api/reference/space/index.rst index 03763610..c11b140d 100644 --- a/docs/api/reference/space/index.rst +++ b/docs/api/reference/space/index.rst @@ -39,8 +39,28 @@ Minimal example API reference ------------- -.. autoclass:: Grid - :members: - :inherited-members: - :autosummary: - :autosummary-nosignatures: \ No newline at end of file +.. tab-set:: + + .. tab-item:: Overview + + .. rubric:: Lifecycle / Core + + .. autosummary:: + :nosignatures: + :toctree: + + Grid.__init__ + + .. rubric:: Sampling & Queries + + .. autosummary:: + :nosignatures: + :toctree: + + Grid.remaining_capacity + + .. tab-item:: Full API + + .. autoclass:: Grid + :autosummary: + :autosummary-nosignatures: \ No newline at end of file From eb87385e1d0e76e08aaa3c0c6e4abc9a80d4ac75 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 15:23:16 +0200 Subject: [PATCH 224/329] docs: add logo and favicon settings to HTML output configuration --- docs/api/_static/mesa_logo.png | Bin 0 -> 10958 bytes docs/api/conf.py | 2 ++ 2 files changed, 2 insertions(+) create mode 100644 docs/api/_static/mesa_logo.png diff --git a/docs/api/_static/mesa_logo.png b/docs/api/_static/mesa_logo.png new file mode 100644 index 0000000000000000000000000000000000000000..41994d7e45f355924aa07a8a04d571107568b549 GIT binary patch literal 10958 zcmV;mJ1B8{x& zK78KuusgS|UF3g0oZhtK+!gU~b~Lzc$C>-w?Bm(bciqv>d;QuSuMTwkWNZup-Zb=J zJa6?ffxoRdKl|~^7y_6TlhLf%%VcxEW%Hc_E!(d8m#a^yBx{$HaEY_=J3J|y7_f{JdnNONpCpaYn{uMu9^7Zuvh|PL9A)X$Z}#GEQl3GKw!{- z9h&Du7Y6J$KrGXUF-yDXqMI|Z5@%v{FwtSU>iZ*Bu{rks{Idg~Xg9>_mJo}*fOq$a zg|%SVuOL{q6vXNoVr2q{K`dv0SV2QuC|dA*XfFU_VKBx3xCy$n=ZKYTE@F|J49!15 zEQh7x*B!CwjimIx*hp*ewi;rA{s7!y*b!^>;PC$SS*JqFcY-(-f>>AxYq0?=BbM6U zh{f$1#G?O-fo4 zs?b=GqsEHdBNqG;XsqI?DkB!e`%_DW6%q^Fa!iF?5wSY`iJw%c7FZat0d(1?LK;ld zg{d%DoeDdTPK7Y6R0vPmL3EK{CE{I?ftx^Mm5iY-zp0R1#!p7#B^CA{u(n5I0kn@G z7VYT@@TpMXGKeLLSkK{97&x5@Te67Nmq#pUEarQZ-dziDJB#-ShBcjf{YNaIJYr=A z9wJuV+9-RM;HGtTi&%hSVqtqrBUV&txq&rS_+KK&6|hc)G=`=Fo-|etjRjPru|o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d;b9XePJH5tCvJ2&6v#=GwS{vR5g94%i^rnQJ2m2ORN`X+ z3uq(?JTL=TOP4MUQ{rPe;A6Gac{e}M>+_AR*)X?&k_zP1AXTsnu&6AJ zifa|%<{Hyo zrVdBZu3e=^&VEeQ)?hx4V0Rp?3nX6Wvu976KHX5n zY*=;;oiSrZK|uk1VJD}jr#~wYh|tebp8nFMOI+ifyZ5f&QXthJZVTcDB*H_lF79{2 zJ>Qm}Pqqw1;RvA+edp)rgGiiZZ=rCrQ>&dEfrOz$8DB(pc{k^sjF&s_x=U)8?yL;~ zK8DKeBKRs$HF@&n2%%lx&+v`Mnlfcdlw1bK#DGZdM%s}=q3k)!4y?@1rf-B$I7LAm z{SDo^F$Rm2P$MlZ4Zb}wmb5RyI~_SwR6l2zrDoh}?3!=3ybjZbJ7M0udA*~e*p({J zM|pX9j0@)HTglrUV3Eg zSb}kdOH5zV8Wg_rpSpEZd@3Ygl&l19CIzA&SR`&BXO)KZsgNT2fu(`fMSv9+rh(-Pu+(ELfMwGd%OQ!etR72Y=3ODi zvI6S{KVq!z9UEiW4_LvS2P}GZ3b3$iOJM1tP!EMV#?ryN2G;cotibSC9H$Q6J?Bi)}QrIsE0y56l!2;U}<3K2~r(n=@_ePdMt;nVl0lfQ#=-z*e{O-A1ly+ zWwYSjZ(t><4Tb&y3qIBZ4?NJfUq6VTB8`iSBUNV1m@#k^rF~;!V?pGkNs}nO%&ttM zqhTikEUG3;`%>VUddU!!`}Xb2u2ks-I0nhTFLmuBqoP=R04k<}-9CN$QcQd>b`fFU wL4#ue*4VLQ73^|`@f{>$Q5Ij?H!?Ev{~hlwytuk+-~a#s07*qoM6N<$f(9Z=fB*mh literal 0 HcmV?d00001 diff --git a/docs/api/conf.py b/docs/api/conf.py index 61d34eb4..36f85c4e 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -38,6 +38,8 @@ html_theme = "pydata_sphinx_theme" html_static_path = ["_static"] html_show_sourcelink = False +html_logo = "_static/mesa_logo.png" +html_favicon = "_static/mesa_logo.png" # Add custom branding CSS/JS (mesa_brand) to static files html_css_files = [ From 1abf27ac995fd6702ef845eb5af5ba53f02ddf7c Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 15:31:38 +0200 Subject: [PATCH 225/329] docs: add Matrix link to navigation bar for community support --- docs/api/conf.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/api/conf.py b/docs/api/conf.py index 36f85c4e..f17e9108 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -95,6 +95,11 @@ "url": github_root, "icon": "fa-brands fa-github", }, + { + "name": "Matrix", + "url": "https://matrix.to/#/#project-mesa:matrix.org", + "icon": "fa-solid fa-comments", + }, ], "navbar_start": ["navbar-logo"], "navbar_end": ["theme-switcher", "navbar-icon-links"], From 7632f921b03adceee937b8e0a4a896436728afd5 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 17:15:08 +0200 Subject: [PATCH 226/329] docs: add Meta and Chat sections with relevant badges to README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index a68823dc..358733ef 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,12 @@ # mesa-frames 🚀 +| | | +| ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| CI/CD | [![CI Checks](https://github.com/projectmesa/mesa-frames/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/projectmesa/mesa-frames/actions/workflows/build.yml) [![codecov](https://codecov.io/gh/projectmesa/mesa-frames/branch/main/graph/badge.svg)](https://app.codecov.io/gh/projectmesa/mesa-frames) | +| Package | [![PyPI - Version](https://img.shields.io/pypi/v/mesa-frames.svg?logo=pypi&label=PyPI&logoColor=gold)](https://pypi.org/project/mesa-frames/) [![PyPI - Downloads](https://img.shields.io/pypi/dm/mesa-frames.svg?color=blue&label=Downloads&logo=pypi&logoColor=gold)](https://pypi.org/project/mesa-frames/) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/mesa-frames.svg?logo=python&label=Python&logoColor=gold)](https://pypi.org/project/mesa-frames/) | +| Meta | [![linting - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://docs.astral.sh/ruff/) [![formatter - Ruff](https://img.shields.io/badge/formatter-Ruff-0f172a?logo=ruff&logoColor=white)](https://docs.astral.sh/ruff/formatter/) [![Hatch project](https://img.shields.io/badge/%F0%9F%A5%9A-Hatch-4051b5.svg)](https://github.com/pypa/hatch) [![Managed with uv](https://img.shields.io/badge/managed%20with-uv-5a4fcf?logo=uv&logoColor=white)](https://github.com/astral-sh/uv) | +| Chat | [![chat](https://img.shields.io/matrix/project-mesa:matrix.org?label=chat&logo=Matrix)](https://matrix.to/#/#project-mesa:matrix.org) | + mesa-frames is an extension of the [mesa](https://github.com/projectmesa/mesa) framework, designed for complex simulations with thousands of agents. By storing agents in a DataFrame, mesa-frames significantly enhances the performance and scalability of mesa, while maintaining a similar syntax. mesa-frames allows for the use of [vectorized functions](https://stackoverflow.com/a/1422198) which significantly speeds up operations whenever simultaneous activation of agents is possible. ## Why DataFrames? 📊 From b3bdf73cec3379ddd657649bff5067a3bbb14816 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 17:36:45 +0200 Subject: [PATCH 227/329] docs: update logo and favicon URLs to use remote assets --- docs/api/_static/mesa_logo.png | Bin 10958 -> 0 bytes docs/api/conf.py | 4 ++-- mkdocs.yml | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) delete mode 100644 docs/api/_static/mesa_logo.png diff --git a/docs/api/_static/mesa_logo.png b/docs/api/_static/mesa_logo.png deleted file mode 100644 index 41994d7e45f355924aa07a8a04d571107568b549..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10958 zcmV;mJ1B8{x& zK78KuusgS|UF3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}wmb5RyI~_SwR6l2zrDoh}?3!=3ybjZbJ7M0udA*~e*p({J zM|pX9j0@)HTglrUV3Eg zSb}kdOH5zV8Wg_rpSpEZd@3Ygl&l19CIzA&SR`&BXO)KZsgNT2fu(`fMSv9+rh(-Pu+(ELfMwGd%OQ!etR72Y=3ODi zvI6S{KVq!z9UEiW4_LvS2P}GZ3b3$iOJM1tP!EMV#?ryN2G;cotibSC9H$Q6J?Bi)}QrIsE0y56l!2;U}<3K2~r(n=@_ePdMt;nVl0lfQ#=-z*e{O-A1ly+ zWwYSjZ(t><4Tb&y3qIBZ4?NJfUq6VTB8`iSBUNV1m@#k^rF~;!V?pGkNs}nO%&ttM zqhTikEUG3;`%>VUddU!!`}Xb2u2ks-I0nhTFLmuBqoP=R04k<}-9CN$QcQd>b`fFU wL4#ue*4VLQ73^|`@f{>$Q5Ij?H!?Ev{~hlwytuk+-~a#s07*qoM6N<$f(9Z=fB*mh diff --git a/docs/api/conf.py b/docs/api/conf.py index f17e9108..8701feb4 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -38,8 +38,8 @@ html_theme = "pydata_sphinx_theme" html_static_path = ["_static"] html_show_sourcelink = False -html_logo = "_static/mesa_logo.png" -html_favicon = "_static/mesa_logo.png" +html_logo = "https://raw.githubusercontent.com/projectmesa/mesa/main/docs/images/mesa_logo.png" +html_favicon = "https://raw.githubusercontent.com/projectmesa/mesa/main/docs/images/mesa_logo.ico" # Add custom branding CSS/JS (mesa_brand) to static files html_css_files = [ diff --git a/mkdocs.yml b/mkdocs.yml index f8ae79dd..1e481037 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -40,6 +40,10 @@ theme: code: Roboto Mono icon: repo: fontawesome/brands/github + # Logo (PNG) + logo: https://raw.githubusercontent.com/projectmesa/mesa/main/docs/images/mesa_logo.png + # Favicon (ICO) + favicon: https://raw.githubusercontent.com/projectmesa/mesa/main/docs/images/mesa_logo.ico # Plugins plugins: From 3717ed23160e0f50f8fe5b6980dddad48612993e Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 18:07:35 +0200 Subject: [PATCH 228/329] docs: update site name for clarity in project documentation --- mkdocs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs.yml b/mkdocs.yml index 1e481037..a1caa258 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,5 +1,5 @@ # Project information -site_name: mesa-frames +site_name: mesa-frames documentation site_url: https://projectmesa.github.io/mesa-frames repo_url: https://github.com/projectmesa/mesa-frames repo_name: projectmesa/mesa-frames From 9db75c02982cb2d38519a75e70cce9a9d6d87a91 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 22 Sep 2025 18:08:03 +0200 Subject: [PATCH 229/329] docs: enhance README structure and content for clarity and organization --- README.md | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 358733ef..195d7625 100644 --- a/README.md +++ b/README.md @@ -1,35 +1,40 @@ -# mesa-frames 🚀 +

+ Mesa logo +

+ +

mesa-frames

| | | | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | CI/CD | [![CI Checks](https://github.com/projectmesa/mesa-frames/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/projectmesa/mesa-frames/actions/workflows/build.yml) [![codecov](https://codecov.io/gh/projectmesa/mesa-frames/branch/main/graph/badge.svg)](https://app.codecov.io/gh/projectmesa/mesa-frames) | | Package | [![PyPI - Version](https://img.shields.io/pypi/v/mesa-frames.svg?logo=pypi&label=PyPI&logoColor=gold)](https://pypi.org/project/mesa-frames/) [![PyPI - Downloads](https://img.shields.io/pypi/dm/mesa-frames.svg?color=blue&label=Downloads&logo=pypi&logoColor=gold)](https://pypi.org/project/mesa-frames/) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/mesa-frames.svg?logo=python&label=Python&logoColor=gold)](https://pypi.org/project/mesa-frames/) | | Meta | [![linting - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://docs.astral.sh/ruff/) [![formatter - Ruff](https://img.shields.io/badge/formatter-Ruff-0f172a?logo=ruff&logoColor=white)](https://docs.astral.sh/ruff/formatter/) [![Hatch project](https://img.shields.io/badge/%F0%9F%A5%9A-Hatch-4051b5.svg)](https://github.com/pypa/hatch) [![Managed with uv](https://img.shields.io/badge/managed%20with-uv-5a4fcf?logo=uv&logoColor=white)](https://github.com/astral-sh/uv) | -| Chat | [![chat](https://img.shields.io/matrix/project-mesa:matrix.org?label=chat&logo=Matrix)](https://matrix.to/#/#project-mesa:matrix.org) | - -mesa-frames is an extension of the [mesa](https://github.com/projectmesa/mesa) framework, designed for complex simulations with thousands of agents. By storing agents in a DataFrame, mesa-frames significantly enhances the performance and scalability of mesa, while maintaining a similar syntax. mesa-frames allows for the use of [vectorized functions](https://stackoverflow.com/a/1422198) which significantly speeds up operations whenever simultaneous activation of agents is possible. +| Chat | [![chat](https://img.shields.io/matrix/project-mesa:matrix.org?label=chat&logo=Matrix)](https://matrix.to/#/#project-mesa:matrix.org) | -## Why DataFrames? 📊 +--- -DataFrames are optimized for simultaneous operations through [SIMD processing](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data). At the moment, mesa-frames supports the use of Polars library. +## Scale Mesa beyond its limits -- [Polars](https://pola.rs/) is a new DataFrame library with a syntax similar to pandas but with several innovations, including a backend implemented in Rust, the Apache Arrow memory format, query optimization, and support for larger-than-memory DataFrames. +Classic [Mesa](https://github.com/projectmesa/mesa) stores each agent as a Python object, which quickly becomes a bottleneck at scale. +**mesa-frames** reimagines agent storage using **Polars DataFrames**, so agents live in a columnar store rather than the Python heap. -The following is a performance graph showing execution time using mesa and mesa-frames for the [Boltzmann Wealth model](https://mesa.readthedocs.io/en/stable/tutorials/intro_tutorial.html). +You keep the Mesa-style `Model` / `AgentSet` structure, but updates are vectorized and memory-efficient. -![Performance Graph with Mesa](https://github.com/projectmesa/mesa-frames/blob/main/examples/boltzmann_wealth/boltzmann_with_mesa.png) +### Why it matters +- ⚡ **10× faster** bulk updates on 10k+ agents (see benchmarks) +- 📊 **Columnar execution** via [Polars](https://docs.pola.rs/): [SIMD](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data) ops, multi-core support +- 🔄 **Declarative logic**: agent rules as transformations, not Python loops +- 🚀 **Roadmap**: Lazy queries and GPU support for even faster models -![Performance Graph without Mesa](https://github.com/projectmesa/mesa-frames/blob/main/examples/boltzmann_wealth/boltzmann_no_mesa.png) +--- -([You can check the script used to generate the graph here](https://github.com/projectmesa/mesa-frames/blob/main/examples/boltzmann_wealth/performance_plot.py), but if you want to additionally compare vs Mesa, you have to uncomment `mesa_implementation` and its label) +## Who is it for? -## Installation +- Researchers needing to scale to **tens or hundreds of thousands of agents** +- Users whose agent logic can be written as **vectorized, set-based operations** -### Install from PyPI +❌ **Not a good fit if:** your model depends on strict per-agent sequencing, complex non-vectorizable methods, or fine-grained identity tracking. -```bash -pip install mesa-frames -``` ### Install from Source (development) From 54d0f27ae6b776fee42ae4f2eb96836206c55c25 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 23 Sep 2025 09:06:08 +0200 Subject: [PATCH 230/329] docs: change .ipynb tutorials to jupytext --- .../user-guide/2_introductory-tutorial.ipynb | 449 ---------------- .../user-guide/2_introductory_tutorial.py | 277 ++++++++++ docs/general/user-guide/4_datacollector.ipynb | 501 ------------------ docs/general/user-guide/4_datacollector.py | 229 ++++++++ 4 files changed, 506 insertions(+), 950 deletions(-) delete mode 100644 docs/general/user-guide/2_introductory-tutorial.ipynb create mode 100644 docs/general/user-guide/2_introductory_tutorial.py delete mode 100644 docs/general/user-guide/4_datacollector.ipynb create mode 100644 docs/general/user-guide/4_datacollector.py diff --git a/docs/general/user-guide/2_introductory-tutorial.ipynb b/docs/general/user-guide/2_introductory-tutorial.ipynb deleted file mode 100644 index 11391f9d..00000000 --- a/docs/general/user-guide/2_introductory-tutorial.ipynb +++ /dev/null @@ -1,449 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "7ee055b2", - "metadata": {}, - "source": [ - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/projectmesa/mesa-frames/blob/main/docs/general/user-guide/2_introductory-tutorial.ipynb)" - ] - }, - { - "cell_type": "markdown", - "id": "8bd0381e", - "metadata": {}, - "source": [ - "## Installation (if running in Colab)\n", - "\n", - "Run the following cell to install `mesa-frames` if you are using Google Colab." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "df4d8623", - "metadata": {}, - "outputs": [], - "source": [ - "# !pip install git+https://github.com/projectmesa/mesa-frames mesa" - ] - }, - { - "cell_type": "markdown", - "id": "11515dfc", - "metadata": {}, - "source": [ - " # Introductory Tutorial: Boltzmann Wealth Model with mesa-frames 💰🚀\n", - "\n", - "In this tutorial, we'll implement the Boltzmann Wealth Model using mesa-frames. This model simulates the distribution of wealth among agents, where agents randomly give money to each other.\n", - "\n", - "## Setting Up the Model 🏗️\n", - "\n", - "First, let's import the necessary modules and set up our model class:\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fc0ee981", - "metadata": {}, - "outputs": [ - { - "ename": "ImportError", - "evalue": "cannot import name 'Model' from partially initialized module 'mesa_frames' (most likely due to a circular import) (/home/adam/projects/mesa-frames/mesa_frames/__init__.py)", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mImportError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Model, AgentSet, DataCollector\n\u001b[32m 4\u001b[39m \u001b[38;5;28;01mclass\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mMoneyModelDF\u001b[39;00m(Model):\n\u001b[32m 5\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, N: \u001b[38;5;28mint\u001b[39m, agents_cls):\n", - "\u001b[36mFile \u001b[39m\u001b[32m~/projects/mesa-frames/mesa_frames/__init__.py:65\u001b[39m\n\u001b[32m 63\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mconcrete\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01magentset\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m AgentSet\n\u001b[32m 64\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mconcrete\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01magentsetregistry\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m AgentSetRegistry\n\u001b[32m---> \u001b[39m\u001b[32m65\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mconcrete\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mdatacollector\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m DataCollector\n\u001b[32m 66\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mconcrete\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mmodel\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Model\n\u001b[32m 67\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mconcrete\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mspace\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Grid\n", - "\u001b[36mFile \u001b[39m\u001b[32m~/projects/mesa-frames/mesa_frames/concrete/datacollector.py:62\u001b[39m\n\u001b[32m 60\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtempfile\u001b[39;00m\n\u001b[32m 61\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpsycopg2\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m62\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mabstract\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mdatacollector\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m AbstractDataCollector\n\u001b[32m 63\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtyping\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Any, Literal\n\u001b[32m 64\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mcollections\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mabc\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Callable\n", - "\u001b[36mFile \u001b[39m\u001b[32m~/projects/mesa-frames/mesa_frames/abstract/datacollector.py:50\u001b[39m\n\u001b[32m 48\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtyping\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Any, Literal\n\u001b[32m 49\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mcollections\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mabc\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Callable\n\u001b[32m---> \u001b[39m\u001b[32m50\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmesa_frames\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Model\n\u001b[32m 51\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpolars\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpl\u001b[39;00m\n\u001b[32m 52\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mthreading\u001b[39;00m\n", - "\u001b[31mImportError\u001b[39m: cannot import name 'Model' from partially initialized module 'mesa_frames' (most likely due to a circular import) (/home/adam/projects/mesa-frames/mesa_frames/__init__.py)" - ] - } - ], - "source": [ - "from mesa_frames import Model, AgentSet, DataCollector\n", - "\n", - "\n", - "class MoneyModel(Model):\n", - " def __init__(self, N: int, agents_cls):\n", - " super().__init__()\n", - " self.n_agents = N\n", - " self.sets += agents_cls(N, self)\n", - " self.datacollector = DataCollector(\n", - " model=self,\n", - " model_reporters={\n", - " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum()\n", - " },\n", - " agent_reporters={\"wealth\": \"wealth\"},\n", - " storage=\"csv\",\n", - " storage_uri=\"./data\",\n", - " trigger=lambda m: m.schedule.steps % 2 == 0,\n", - " )\n", - "\n", - " def step(self):\n", - " # Executes the step method for every agentset in self.sets\n", - " self.sets.do(\"step\")\n", - "\n", - " def run_model(self, n):\n", - " for _ in range(n):\n", - " self.step()\n", - " self.datacollector.conditional_collect\n", - " self.datacollector.flush()" - ] - }, - { - "cell_type": "markdown", - "id": "00e092c4", - "metadata": {}, - "source": [ - "## Implementing the AgentSet 👥\n", - "\n", - "Now, let's implement our `MoneyAgents` using polars backends." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2bac0126", - "metadata": {}, - "outputs": [], - "source": [ - "import polars as pl\n", - "\n", - "\n", - "class MoneyAgents(AgentSet):\n", - " def __init__(self, n: int, model: Model):\n", - " super().__init__(model)\n", - " self += pl.DataFrame({\"wealth\": pl.ones(n, eager=True)})\n", - "\n", - " def step(self) -> None:\n", - " self.do(\"give_money\")\n", - "\n", - " def give_money(self):\n", - " self.select(self.wealth > 0)\n", - " other_agents = self.df.sample(n=len(self.active_agents), with_replacement=True)\n", - " self[\"active\", \"wealth\"] -= 1\n", - " new_wealth = other_agents.group_by(\"unique_id\").len()\n", - " self[new_wealth[\"unique_id\"], \"wealth\"] += new_wealth[\"len\"]" - ] - }, - { - "cell_type": "markdown", - "id": "3b141016", - "metadata": {}, - "source": [ - "\n", - "## Running the Model ▶️\n", - "\n", - "Now that we have our model and agent set defined, let's run a simulation:\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "65da4e6f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "shape: (9, 2)\n", - "┌────────────┬──────────┐\n", - "│ statistic ┆ wealth │\n", - "│ --- ┆ --- │\n", - "│ str ┆ f64 │\n", - "╞════════════╪══════════╡\n", - "│ count ┆ 1000.0 │\n", - "│ null_count ┆ 0.0 │\n", - "│ mean ┆ 1.0 │\n", - "│ std ┆ 1.134587 │\n", - "│ min ┆ 0.0 │\n", - "│ 25% ┆ 0.0 │\n", - "│ 50% ┆ 1.0 │\n", - "│ 75% ┆ 2.0 │\n", - "│ max ┆ 8.0 │\n", - "└────────────┴──────────┘\n" - ] - } - ], - "source": [ - "# Create and run the model\n", - "model = MoneyModel(1000, MoneyAgents)\n", - "model.run_model(100)\n", - "\n", - "wealth_dist = list(model.sets.df.values())[0]\n", - "\n", - "# Print the final wealth distribution\n", - "print(wealth_dist.select(pl.col(\"wealth\")).describe())" - ] - }, - { - "cell_type": "markdown", - "id": "812da73b", - "metadata": {}, - "source": [ - "\n", - "This output shows the statistical summary of the wealth distribution after 100 steps of the simulation with 1000 agents.\n", - "\n", - "## Performance Comparison 🏎️💨\n", - "\n", - "One of the key advantages of mesa-frames is its performance with large numbers of agents. Let's compare the performance of mesa and polars:\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fbdb540810924de8", - "metadata": {}, - "outputs": [], - "source": [ - "class MoneyAgentsConcise(AgentSet):\n", - " def __init__(self, n: int, model: Model):\n", - " super().__init__(model)\n", - " ## Adding the agents to the agent set\n", - " # 1. Changing the df attribute directly (not recommended, if other agents were added before, they will be lost)\n", - " \"\"\"self.df = pl.DataFrame(\n", - " {\"wealth\": pl.ones(n, eager=True)}\n", - " )\"\"\"\n", - " # 2. Adding the dataframe with add\n", - " \"\"\"self.add(\n", - " pl.DataFrame(\n", - " {\n", - " \"wealth\": pl.ones(n, eager=True),\n", - " }\n", - " )\n", - " )\"\"\"\n", - " # 3. Adding the dataframe with __iadd__\n", - " self += pl.DataFrame({\"wealth\": pl.ones(n, eager=True)})\n", - "\n", - " def step(self) -> None:\n", - " # The give_money method is called\n", - " # self.give_money()\n", - " self.do(\"give_money\")\n", - "\n", - " def give_money(self):\n", - " ## Active agents are changed to wealthy agents\n", - " # 1. Using the __getitem__ method\n", - " # self.select(self[\"wealth\"] > 0)\n", - " # 2. Using the fallback __getattr__ method\n", - " self.select(self.wealth > 0)\n", - "\n", - " # Receiving agents are sampled (only native expressions currently supported)\n", - " other_agents = self.df.sample(n=len(self.active_agents), with_replacement=True)\n", - "\n", - " # Wealth of wealthy is decreased by 1\n", - " # 1. Using the __setitem__ method with self.active_agents mask\n", - " # self[self.active_agents, \"wealth\"] -= 1\n", - " # 2. Using the __setitem__ method with \"active\" mask\n", - " self[\"active\", \"wealth\"] -= 1\n", - "\n", - " # Compute the income of the other agents (only native expressions currently supported)\n", - " new_wealth = other_agents.group_by(\"unique_id\").len()\n", - "\n", - " # Add the income to the other agents\n", - " # 1. Using the set method\n", - " \"\"\"self.set(\n", - " attr_names=\"wealth\",\n", - " values=pl.col(\"wealth\") + new_wealth[\"len\"],\n", - " mask=new_wealth,\n", - " )\"\"\"\n", - "\n", - " # 2. Using the __setitem__ method\n", - " self[new_wealth, \"wealth\"] += new_wealth[\"len\"]\n", - "\n", - "\n", - "class MoneyAgentsNative(AgentSet):\n", - " def __init__(self, n: int, model: Model):\n", - " super().__init__(model)\n", - " self += pl.DataFrame({\"wealth\": pl.ones(n, eager=True)})\n", - "\n", - " def step(self) -> None:\n", - " self.do(\"give_money\")\n", - "\n", - " def give_money(self):\n", - " ## Active agents are changed to wealthy agents\n", - " self.select(pl.col(\"wealth\") > 0)\n", - "\n", - " other_agents = self.df.sample(n=len(self.active_agents), with_replacement=True)\n", - "\n", - " # Wealth of wealthy is decreased by 1\n", - " self.df = self.df.with_columns(\n", - " wealth=pl.when(\n", - " pl.col(\"unique_id\").is_in(self.active_agents[\"unique_id\"].implode())\n", - " )\n", - " .then(pl.col(\"wealth\") - 1)\n", - " .otherwise(pl.col(\"wealth\"))\n", - " )\n", - "\n", - " new_wealth = other_agents.group_by(\"unique_id\").len()\n", - "\n", - " # Add the income to the other agents\n", - " self.df = (\n", - " self.df.join(new_wealth, on=\"unique_id\", how=\"left\")\n", - " .fill_null(0)\n", - " .with_columns(wealth=pl.col(\"wealth\") + pl.col(\"len\"))\n", - " .drop(\"len\")\n", - " )" - ] - }, - { - "cell_type": "markdown", - "id": "496196d999f18634", - "metadata": {}, - "source": [ - "Add Mesa implementation of MoneyAgent and MoneyModel classes to test Mesa performance" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9dbe761af964af5b", - "metadata": {}, - "outputs": [], - "source": [ - "import mesa\n", - "\n", - "\n", - "class MesaMoneyAgent(mesa.Agent):\n", - " \"\"\"An agent with fixed initial wealth.\"\"\"\n", - "\n", - " def __init__(self, model):\n", - " # Pass the parameters to the parent class.\n", - " super().__init__(model)\n", - "\n", - " # Create the agent's variable and set the initial values.\n", - " self.wealth = 1\n", - "\n", - " def step(self):\n", - " # Verify agent has some wealth\n", - " if self.wealth > 0:\n", - " other_agent: MesaMoneyAgent = self.model.random.choice(self.model.agents)\n", - " if other_agent is not None:\n", - " other_agent.wealth += 1\n", - " self.wealth -= 1\n", - "\n", - "\n", - "class MesaMoneyModel(mesa.Model):\n", - " \"\"\"A model with some number of agents.\"\"\"\n", - "\n", - " def __init__(self, N: int):\n", - " super().__init__()\n", - " self.num_agents = N\n", - " for _ in range(N):\n", - " self.agents.add(MesaMoneyAgent(self))\n", - "\n", - " def step(self):\n", - " \"\"\"Advance the model by one step.\"\"\"\n", - " self.agents.shuffle_do(\"step\")\n", - "\n", - " def run_model(self, n_steps) -> None:\n", - " for _ in range(n_steps):\n", - " self.step()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2d864cd3", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Execution times:\n", - "---------------\n", - "mesa:\n", - " Number of agents: 100, Time: 0.03 seconds\n", - " Number of agents: 1001, Time: 1.45 seconds\n", - " Number of agents: 2000, Time: 5.40 seconds\n", - "---------------\n", - "---------------\n", - "mesa-frames (pl concise):\n", - " Number of agents: 100, Time: 1.60 seconds\n", - " Number of agents: 1001, Time: 2.68 seconds\n", - " Number of agents: 2000, Time: 3.04 seconds\n", - "---------------\n", - "---------------\n", - "mesa-frames (pl native):\n", - " Number of agents: 100, Time: 0.62 seconds\n", - " Number of agents: 1001, Time: 0.80 seconds\n", - " Number of agents: 2000, Time: 1.10 seconds\n", - "---------------\n" - ] - } - ], - "source": [ - "import time\n", - "\n", - "\n", - "def run_simulation(model: MesaMoneyModel | MoneyModel, n_steps: int):\n", - " start_time = time.time()\n", - " model.run_model(n_steps)\n", - " end_time = time.time()\n", - " return end_time - start_time\n", - "\n", - "\n", - "# Compare mesa and mesa-frames implementations\n", - "n_agents_list = [10**2, 10**3 + 1, 2 * 10**3]\n", - "n_steps = 100\n", - "print(\"Execution times:\")\n", - "for implementation in [\n", - " \"mesa\",\n", - " \"mesa-frames (pl concise)\",\n", - " \"mesa-frames (pl native)\",\n", - "]:\n", - " print(f\"---------------\\n{implementation}:\")\n", - " for n_agents in n_agents_list:\n", - " if implementation == \"mesa\":\n", - " ntime = run_simulation(MesaMoneyModel(n_agents), n_steps)\n", - " elif implementation == \"mesa-frames (pl concise)\":\n", - " ntime = run_simulation(MoneyModel(n_agents, MoneyAgentsConcise), n_steps)\n", - " elif implementation == \"mesa-frames (pl native)\":\n", - " ntime = run_simulation(MoneyModel(n_agents, MoneyAgentsNative), n_steps)\n", - "\n", - " print(f\" Number of agents: {n_agents}, Time: {ntime:.2f} seconds\")\n", - " print(\"---------------\")" - ] - }, - { - "cell_type": "markdown", - "id": "6dfc6d34", - "metadata": {}, - "source": [ - "\n", - "## Conclusion 🎉\n", - "\n", - "- All mesa-frames implementations significantly outperform the original mesa implementation. 🏆\n", - "- The native implementation for Polars shows better performance than their concise counterparts. 💪\n", - "- The Polars native implementation shows the most impressive speed-up, ranging from 10.86x to 17.60x faster than mesa! 🚀🚀🚀\n", - "- The performance advantage of mesa-frames becomes more pronounced as the number of agents increases. 📈" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": ".venv", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.3" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/general/user-guide/2_introductory_tutorial.py b/docs/general/user-guide/2_introductory_tutorial.py new file mode 100644 index 00000000..8560034a --- /dev/null +++ b/docs/general/user-guide/2_introductory_tutorial.py @@ -0,0 +1,277 @@ +from __future__ import annotations + +# %% [markdown] +"""[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/projectmesa/mesa-frames/blob/main/docs/general/user-guide/2_introductory-tutorial.ipynb)""" + +# %% [markdown] +"""## Installation (if running in Colab) + +Run the following cell to install `mesa-frames` if you are using Google Colab.""" + +# %% +# !pip install git+https://github.com/projectmesa/mesa-frames mesa + +# %% [markdown] +""" # Introductory Tutorial: Boltzmann Wealth Model with mesa-frames 💰🚀 + +In this tutorial, we'll implement the Boltzmann Wealth Model using mesa-frames. This model simulates the distribution of wealth among agents, where agents randomly give money to each other. + +## Setting Up the Model 🏗️ + +First, let's import the necessary modules and set up our model class:""" + +# %% +from mesa_frames import Model, AgentSet, DataCollector + + +class MoneyModel(Model): + def __init__(self, N: int, agents_cls): + super().__init__() + self.n_agents = N + self.sets += agents_cls(N, self) + self.datacollector = DataCollector( + model=self, + model_reporters={ + "total_wealth": lambda m: m.sets["MoneyAgents"].df["wealth"].sum() + }, + agent_reporters={"wealth": "wealth"}, + storage="csv", + storage_uri="./data", + trigger=lambda m: m.schedule.steps % 2 == 0, + ) + + def step(self): + # Executes the step method for every agentset in self.sets + self.sets.do("step") + + def run_model(self, n): + for _ in range(n): + self.step() + self.datacollector.conditional_collect + self.datacollector.flush() + + +# %% [markdown] +"""## Implementing the AgentSet 👥 + +Now, let's implement our `MoneyAgents` using polars backends.""" + +# %% +import polars as pl + + +class MoneyAgents(AgentSet): + def __init__(self, n: int, model: Model): + super().__init__(model) + self += pl.DataFrame({"wealth": pl.ones(n, eager=True)}) + + def step(self) -> None: + self.do("give_money") + + def give_money(self): + self.select(self.wealth > 0) + other_agents = self.df.sample(n=len(self.active_agents), with_replacement=True) + self["active", "wealth"] -= 1 + new_wealth = other_agents.group_by("unique_id").len() + self[new_wealth["unique_id"], "wealth"] += new_wealth["len"] + + +# %% [markdown] +""" +## Running the Model ▶️ + +Now that we have our model and agent set defined, let's run a simulation:""" + +# %% +# Create and run the model +model = MoneyModel(1000, MoneyAgents) +model.run_model(100) + +wealth_dist = list(model.sets.df.values())[0] + +# Print the final wealth distribution +print(wealth_dist.select(pl.col("wealth")).describe()) + +# %% [markdown] +""" +This output shows the statistical summary of the wealth distribution after 100 steps of the simulation with 1000 agents. + +## Performance Comparison 🏎️💨 + +One of the key advantages of mesa-frames is its performance with large numbers of agents. Let's compare the performance of mesa and polars:""" + + +# %% +class MoneyAgentsConcise(AgentSet): + def __init__(self, n: int, model: Model): + super().__init__(model) + ## Adding the agents to the agent set + # 1. Changing the df attribute directly (not recommended, if other agents were added before, they will be lost) + """self.df = pl.DataFrame( + {"wealth": pl.ones(n, eager=True)} + )""" + # 2. Adding the dataframe with add + """self.add( + pl.DataFrame( + { + "wealth": pl.ones(n, eager=True), + } + ) + )""" + # 3. Adding the dataframe with __iadd__ + self += pl.DataFrame({"wealth": pl.ones(n, eager=True)}) + + def step(self) -> None: + # The give_money method is called + # self.give_money() + self.do("give_money") + + def give_money(self): + ## Active agents are changed to wealthy agents + # 1. Using the __getitem__ method + # self.select(self["wealth"] > 0) + # 2. Using the fallback __getattr__ method + self.select(self.wealth > 0) + + # Receiving agents are sampled (only native expressions currently supported) + other_agents = self.df.sample(n=len(self.active_agents), with_replacement=True) + + # Wealth of wealthy is decreased by 1 + # 1. Using the __setitem__ method with self.active_agents mask + # self[self.active_agents, "wealth"] -= 1 + # 2. Using the __setitem__ method with "active" mask + self["active", "wealth"] -= 1 + + # Compute the income of the other agents (only native expressions currently supported) + new_wealth = other_agents.group_by("unique_id").len() + + # Add the income to the other agents + # 1. Using the set method + """self.set( + attr_names="wealth", + values=pl.col("wealth") + new_wealth["len"], + mask=new_wealth, + )""" + + # 2. Using the __setitem__ method + self[new_wealth, "wealth"] += new_wealth["len"] + + +class MoneyAgentsNative(AgentSet): + def __init__(self, n: int, model: Model): + super().__init__(model) + self += pl.DataFrame({"wealth": pl.ones(n, eager=True)}) + + def step(self) -> None: + self.do("give_money") + + def give_money(self): + ## Active agents are changed to wealthy agents + self.select(pl.col("wealth") > 0) + + other_agents = self.df.sample(n=len(self.active_agents), with_replacement=True) + + # Wealth of wealthy is decreased by 1 + self.df = self.df.with_columns( + wealth=pl.when( + pl.col("unique_id").is_in(self.active_agents["unique_id"].implode()) + ) + .then(pl.col("wealth") - 1) + .otherwise(pl.col("wealth")) + ) + + new_wealth = other_agents.group_by("unique_id").len() + + # Add the income to the other agents + self.df = ( + self.df.join(new_wealth, on="unique_id", how="left") + .fill_null(0) + .with_columns(wealth=pl.col("wealth") + pl.col("len")) + .drop("len") + ) + + +# %% [markdown] +"""Add Mesa implementation of MoneyAgent and MoneyModel classes to test Mesa performance""" + +# %% +import mesa + + +class MesaMoneyAgent(mesa.Agent): + """An agent with fixed initial wealth.""" + + def __init__(self, model): + # Pass the parameters to the parent class. + super().__init__(model) + + # Create the agent's variable and set the initial values. + self.wealth = 1 + + def step(self): + # Verify agent has some wealth + if self.wealth > 0: + other_agent: MesaMoneyAgent = self.model.random.choice(self.model.agents) + if other_agent is not None: + other_agent.wealth += 1 + self.wealth -= 1 + + +class MesaMoneyModel(mesa.Model): + """A model with some number of agents.""" + + def __init__(self, N: int): + super().__init__() + self.num_agents = N + for _ in range(N): + self.agents.add(MesaMoneyAgent(self)) + + def step(self): + """Advance the model by one step.""" + self.agents.shuffle_do("step") + + def run_model(self, n_steps) -> None: + for _ in range(n_steps): + self.step() + + +# %% +import time + + +def run_simulation(model: MesaMoneyModel | MoneyModel, n_steps: int): + start_time = time.time() + model.run_model(n_steps) + end_time = time.time() + return end_time - start_time + + +# Compare mesa and mesa-frames implementations +n_agents_list = [10**2, 10**3 + 1, 2 * 10**3] +n_steps = 100 +print("Execution times:") +for implementation in [ + "mesa", + "mesa-frames (pl concise)", + "mesa-frames (pl native)", +]: + print(f"---------------\n{implementation}:") + for n_agents in n_agents_list: + if implementation == "mesa": + ntime = run_simulation(MesaMoneyModel(n_agents), n_steps) + elif implementation == "mesa-frames (pl concise)": + ntime = run_simulation(MoneyModel(n_agents, MoneyAgentsConcise), n_steps) + elif implementation == "mesa-frames (pl native)": + ntime = run_simulation(MoneyModel(n_agents, MoneyAgentsNative), n_steps) + + print(f" Number of agents: {n_agents}, Time: {ntime:.2f} seconds") + print("---------------") + +# %% [markdown] +""" +## Conclusion 🎉 + +- All mesa-frames implementations significantly outperform the original mesa implementation. 🏆 +- The native implementation for Polars shows better performance than their concise counterparts. 💪 +- The Polars native implementation shows the most impressive speed-up, ranging from 10.86x to 17.60x faster than mesa! 🚀🚀🚀 +- The performance advantage of mesa-frames becomes more pronounced as the number of agents increases. 📈""" diff --git a/docs/general/user-guide/4_datacollector.ipynb b/docs/general/user-guide/4_datacollector.ipynb deleted file mode 100644 index 0809caa2..00000000 --- a/docs/general/user-guide/4_datacollector.ipynb +++ /dev/null @@ -1,501 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "7fb27b941602401d91542211134fc71a", - "metadata": {}, - "source": [ - "# Data Collector Tutorial\n", - "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/projectmesa/mesa-frames/blob/main/docs/general/user-guide/4_datacollector.ipynb)\n", - "\n", - "This notebook walks you through using the concrete `DataCollector` in `mesa-frames` to collect model- and agent-level data and write it to different storage backends: **memory, CSV, Parquet, S3, and PostgreSQL**.\n", - "\n", - "It also shows how to use **conditional triggers** and how the **schema validation** behaves for PostgreSQL.\n" - ] - }, - { - "cell_type": "markdown", - "id": "acae54e37e7d407bbb7b55eff062a284", - "metadata": {}, - "source": [ - "## Installation (Colab or fresh env)\n", - "\n", - "Uncomment and run the next cell if you're in Colab or a clean environment.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "9a63283cbaf04dbcab1f6479b197f3a8", - "metadata": { - "editable": true - }, - "outputs": [], - "source": [ - "# !pip install git+https://github.com/projectmesa/mesa-frames mesa" - ] - }, - { - "cell_type": "markdown", - "id": "8dd0d8092fe74a7c96281538738b07e2", - "metadata": {}, - "source": [ - "## Minimal Example Model\n", - "\n", - "We create a tiny model using the `Model` and an `AgentSet`-style agent container. This is just to demonstrate collection APIs.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "72eea5119410473aa328ad9291626812", - "metadata": { - "editable": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'model': shape: (5, 5)\n", - " ┌──────┬─────────────────────────────────┬───────┬──────────────┬──────────┐\n", - " │ step ┆ seed ┆ batch ┆ total_wealth ┆ n_agents │\n", - " │ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", - " │ i64 ┆ str ┆ i64 ┆ f64 ┆ i64 │\n", - " ╞══════╪═════════════════════════════════╪═══════╪══════════════╪══════════╡\n", - " │ 2 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 4 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 6 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 8 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " │ 10 ┆ 332212815818606584686857770936… ┆ 0 ┆ 1000.0 ┆ 1000 │\n", - " └──────┴─────────────────────────────────┴───────┴──────────────┴──────────┘,\n", - " 'agent': shape: (5_000, 4)\n", - " ┌────────────────────┬──────┬─────────────────────────────────┬───────┐\n", - " │ wealth_MoneyAgents ┆ step ┆ seed ┆ batch │\n", - " │ --- ┆ --- ┆ --- ┆ --- │\n", - " │ f64 ┆ i32 ┆ str ┆ i32 │\n", - " ╞════════════════════╪══════╪═════════════════════════════════╪═══════╡\n", - " │ 3.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", - " │ 0.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", - " │ 2.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", - " │ 1.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", - " │ 0.0 ┆ 2 ┆ 332212815818606584686857770936… ┆ 0 │\n", - " │ … ┆ … ┆ … ┆ … │\n", - " │ 0.0 ┆ 10 ┆ 332212815818606584686857770936… ┆ 0 │\n", - " │ 0.0 ┆ 10 ┆ 332212815818606584686857770936… ┆ 0 │\n", - " │ 0.0 ┆ 10 ┆ 332212815818606584686857770936… ┆ 0 │\n", - " │ 0.0 ┆ 10 ┆ 332212815818606584686857770936… ┆ 0 │\n", - " │ 0.0 ┆ 10 ┆ 332212815818606584686857770936… ┆ 0 │\n", - " └────────────────────┴──────┴─────────────────────────────────┴───────┘}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from mesa_frames import Model, AgentSet, DataCollector\n", - "import polars as pl\n", - "\n", - "\n", - "class MoneyAgents(AgentSet):\n", - " def __init__(self, n: int, model: Model):\n", - " super().__init__(model)\n", - " # one column, one unit of wealth each\n", - " self += pl.DataFrame({\"wealth\": pl.ones(n, eager=True)})\n", - "\n", - " def step(self) -> None:\n", - " self.select(self.wealth > 0)\n", - " receivers = self.df.sample(n=len(self.active_agents), with_replacement=True)\n", - " self[\"active\", \"wealth\"] -= 1\n", - " income = receivers.group_by(\"unique_id\").len()\n", - " self[income[\"unique_id\"], \"wealth\"] += income[\"len\"]\n", - "\n", - "\n", - "class MoneyModel(Model):\n", - " def __init__(self, n: int):\n", - " super().__init__()\n", - " self.sets.add(MoneyAgents(n, self))\n", - " self.dc = DataCollector(\n", - " model=self,\n", - " model_reporters={\n", - " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", - " },\n", - " agent_reporters={\n", - " \"wealth\": \"wealth\", # pull existing column\n", - " },\n", - " storage=\"memory\", # we'll switch this per example\n", - " storage_uri=None,\n", - " trigger=lambda m: m.steps % 2\n", - " == 0, # collect every 2 steps via conditional_collect\n", - " reset_memory=True,\n", - " )\n", - "\n", - " def step(self):\n", - " self.sets.do(\"step\")\n", - "\n", - " def run(self, steps: int, conditional: bool = True):\n", - " for _ in range(steps):\n", - " self.step()\n", - " self.dc.conditional_collect() # or .collect if you want to collect every step regardless of trigger\n", - "\n", - "\n", - "model = MoneyModel(1000)\n", - "model.run(10)\n", - "model.dc.data # peek in-memory dataframes" - ] - }, - { - "cell_type": "markdown", - "id": "3d3ca41d", - "metadata": {}, - "source": [ - "## Saving the data for later use \n", - "\n", - "`DataCollector` supports multiple storage backends. \n", - "Files are saved with **step number** and **batch number** (e.g., `model_step10_batch2.csv`) so multiple collects at the same step don’t overwrite. \n", - " \n", - "- **CSV:** `storage=\"csv\"` → writes `model_step{n}_batch{k}.csv`, easy to open anywhere. \n", - "- **Parquet:** `storage=\"parquet\"` → compressed, efficient for large datasets. \n", - "- **S3:** `storage=\"S3-csv\"`/`storage=\"S3-parquet\"` → saves CSV/Parquet directly to Amazon S3. \n", - "- **PostgreSQL:** `storage=\"postgresql\"` → inserts results into `model_data` and `agent_data` tables for querying. \n" - ] - }, - { - "cell_type": "markdown", - "id": "8edb47106e1a46a883d545849b8ab81b", - "metadata": {}, - "source": [ - "## Writing to Local CSV\n", - "\n", - "Switch the storage to `csv` and provide a folder path. Files are written as `model_step{n}.csv` and `agent_step{n}.csv`.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5f14f38c", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[]" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import os\n", - "\n", - "os.makedirs(\"./data_csv\", exist_ok=True)\n", - "model_csv = MoneyModel(1000)\n", - "model_csv.dc = DataCollector(\n", - " model=model_csv,\n", - " model_reporters={\n", - " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", - " },\n", - " agent_reporters={\n", - " \"wealth\": \"wealth\",\n", - " },\n", - " storage=\"csv\", # saving as csv\n", - " storage_uri=\"./data_csv\",\n", - " trigger=lambda m: m._steps % 2 == 0,\n", - " reset_memory=True,\n", - ")\n", - "model_csv.run(10)\n", - "model_csv.dc.flush()\n", - "os.listdir(\"./data_csv\")" - ] - }, - { - "cell_type": "markdown", - "id": "10185d26023b46108eb7d9f57d49d2b3", - "metadata": {}, - "source": [ - "## Writing to Local Parquet\n", - "\n", - "Use `parquet` for columnar output.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8763a12b2bbd4a93a75aff182afb95dc", - "metadata": { - "editable": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[]" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "os.makedirs(\"./data_parquet\", exist_ok=True)\n", - "model_parq = MoneyModel(1000)\n", - "model_parq.dc = DataCollector(\n", - " model=model_parq,\n", - " model_reporters={\n", - " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", - " },\n", - " agent_reporters={\n", - " \"wealth\": \"wealth\",\n", - " },\n", - " storage=\"parquet\", # save as parquet\n", - " storage_uri=\"data_parquet\",\n", - " trigger=lambda m: m._steps % 2 == 0,\n", - " reset_memory=True,\n", - ")\n", - "model_parq.run(10)\n", - "model_parq.dc.flush()\n", - "os.listdir(\"./data_parquet\")" - ] - }, - { - "cell_type": "markdown", - "id": "7623eae2785240b9bd12b16a66d81610", - "metadata": {}, - "source": [ - "## Writing to Amazon S3 (CSV or Parquet)\n", - "\n", - "Set AWS credentials via environment variables or your usual config. Then choose `S3-csv` or `S3-parquet` and pass an S3 URI (e.g., `s3://my-bucket/experiments/run-1`).\n", - "\n", - "> **Note:** This cell requires network access & credentials when actually run.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7cdc8c89c7104fffa095e18ddfef8986", - "metadata": { - "editable": true - }, - "outputs": [], - "source": [ - "model_s3 = MoneyModel(1000)\n", - "model_s3.dc = DataCollector(\n", - " model=model_s3,\n", - " model_reporters={\n", - " \"total_wealth\": lambda m: m.sets[\"MoneyAgents\"].df[\"wealth\"].sum(),\n", - " \"n_agents\": lambda m: len(m.sets[\"MoneyAgents\"]),\n", - " },\n", - " agent_reporters={\n", - " \"wealth\": \"wealth\",\n", - " },\n", - " storage=\"S3-csv\", # save as csv in S3\n", - " storage_uri=\"s3://my-bucket/experiments/run-1\", # change it to required path\n", - " trigger=lambda m: m._steps % 2 == 0,\n", - " reset_memory=True,\n", - ")\n", - "model_s3.run(10)\n", - "model_s3.dc.flush()" - ] - }, - { - "cell_type": "markdown", - "id": "b118ea5561624da68c537baed56e602f", - "metadata": {}, - "source": [ - "## Writing to PostgreSQL\n", - "\n", - "PostgreSQL requires that the target tables exist and that the expected reporter columns are present. The collector will validate tables/columns up front and raise descriptive errors if something is missing.\n", - "\n", - "Below is a minimal schema example. Adjust columns to your configured reporters.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "938c804e27f84196a10c8828c723f798", - "metadata": { - "editable": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "CREATE SCHEMA IF NOT EXISTS public;\n", - "CREATE TABLE IF NOT EXISTS public.model_data (\n", - " step INTEGER,\n", - " seed VARCHAR,\n", - " total_wealth BIGINT,\n", - " n_agents INTEGER\n", - ");\n", - "\n", - "\n", - "CREATE TABLE IF NOT EXISTS public.agent_data (\n", - " step INTEGER,\n", - " seed VARCHAR,\n", - " unique_id BIGINT,\n", - " wealth BIGINT\n", - ");\n", - "\n" - ] - } - ], - "source": [ - "DDL_MODEL = r\"\"\"\n", - "CREATE SCHEMA IF NOT EXISTS public;\n", - "CREATE TABLE IF NOT EXISTS public.model_data (\n", - " step INTEGER,\n", - " seed VARCHAR,\n", - " total_wealth BIGINT,\n", - " n_agents INTEGER\n", - ");\n", - "\"\"\"\n", - "DDL_AGENT = r\"\"\"\n", - "CREATE TABLE IF NOT EXISTS public.agent_data (\n", - " step INTEGER,\n", - " seed VARCHAR,\n", - " unique_id BIGINT,\n", - " wealth BIGINT\n", - ");\n", - "\"\"\"\n", - "print(DDL_MODEL)\n", - "print(DDL_AGENT)" - ] - }, - { - "cell_type": "markdown", - "id": "504fb2a444614c0babb325280ed9130a", - "metadata": {}, - "source": [ - "After creating the tables (outside this notebook or via a DB connection cell), configure and flush:\n" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "59bbdb311c014d738909a11f9e486628", - "metadata": { - "editable": true - }, - "outputs": [], - "source": [ - "POSTGRES_URI = \"postgresql://user:pass@localhost:5432/mydb\"\n", - "m_pg = MoneyModel(300)\n", - "m_pg.dc._storage = \"postgresql\"\n", - "m_pg.dc._storage_uri = POSTGRES_URI\n", - "m_pg.run(6)\n", - "m_pg.dc.flush()" - ] - }, - { - "cell_type": "markdown", - "id": "b43b363d81ae4b689946ece5c682cd59", - "metadata": {}, - "source": [ - "## Triggers & Conditional Collection\n", - "\n", - "The collector accepts a `trigger: Callable[[Model], bool]`. When using `conditional_collect()`, the collector checks the trigger and collects only if it returns `True`.\n", - "\n", - "You can always call `collect()` to gather data unconditionally.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "8a65eabff63a45729fe45fb5ade58bdc", - "metadata": { - "editable": true - }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "shape: (5, 5)
stepseedbatchtotal_wealthn_agents
i64stri64f64i64
2"540832786058427425452319829502…0100.0100
4"540832786058427425452319829502…0100.0100
6"540832786058427425452319829502…0100.0100
8"540832786058427425452319829502…0100.0100
10"540832786058427425452319829502…0100.0100
" - ], - "text/plain": [ - "shape: (5, 5)\n", - "┌──────┬─────────────────────────────────┬───────┬──────────────┬──────────┐\n", - "│ step ┆ seed ┆ batch ┆ total_wealth ┆ n_agents │\n", - "│ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", - "│ i64 ┆ str ┆ i64 ┆ f64 ┆ i64 │\n", - "╞══════╪═════════════════════════════════╪═══════╪══════════════╪══════════╡\n", - "│ 2 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 4 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 6 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 8 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "│ 10 ┆ 540832786058427425452319829502… ┆ 0 ┆ 100.0 ┆ 100 │\n", - "└──────┴─────────────────────────────────┴───────┴──────────────┴──────────┘" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "m = MoneyModel(100)\n", - "m.dc.trigger = lambda model: model._steps % 3 == 0 # every 3rd step\n", - "m.run(10, conditional=True)\n", - "m.dc.data[\"model\"].head()" - ] - }, - { - "cell_type": "markdown", - "id": "c3933fab20d04ec698c2621248eb3be0", - "metadata": {}, - "source": [ - "## Troubleshooting\n", - "\n", - "- **ValueError: Please define a storage_uri** — for non-memory backends you must set `_storage_uri`.\n", - "- **Missing columns in table** — check the PostgreSQL error text; create/alter the table to include the columns for your configured `model_reporters` and `agent_reporters`, plus required `step` and `seed`.\n", - "- **Permissions/credentials errors** (S3/PostgreSQL) — ensure correct IAM/credentials or database permissions.\n" - ] - }, - { - "cell_type": "markdown", - "id": "4dd4641cc4064e0191573fe9c69df29b", - "metadata": {}, - "source": [ - "---\n", - "*Generated on 2025-08-30.*\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "mesa-frames (3.12.3)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.3" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/general/user-guide/4_datacollector.py b/docs/general/user-guide/4_datacollector.py new file mode 100644 index 00000000..b08e35a2 --- /dev/null +++ b/docs/general/user-guide/4_datacollector.py @@ -0,0 +1,229 @@ +from __future__ import annotations + +# %% [markdown] +"""# Data Collector Tutorial + +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/projectmesa/mesa-frames/blob/main/docs/general/user-guide/4_datacollector.ipynb) + +This notebook walks you through using the concrete `DataCollector` in `mesa-frames` to collect model- and agent-level data and write it to different storage backends: **memory, CSV, Parquet, S3, and PostgreSQL**. + +It also shows how to use **conditional triggers** and how the **schema validation** behaves for PostgreSQL.""" + +# %% [markdown] +"""## Installation (Colab or fresh env) + +Uncomment and run the next cell if you're in Colab or a clean environment.""" + +# %% +# !pip install git+https://github.com/projectmesa/mesa-frames mesa + +# %% [markdown] +"""## Minimal Example Model + +We create a tiny model using the `Model` and an `AgentSet`-style agent container. This is just to demonstrate collection APIs.""" + +# %% +from mesa_frames import Model, AgentSet, DataCollector +import polars as pl + + +class MoneyAgents(AgentSet): + def __init__(self, n: int, model: Model): + super().__init__(model) + # one column, one unit of wealth each + self += pl.DataFrame({"wealth": pl.ones(n, eager=True)}) + + def step(self) -> None: + self.select(self.wealth > 0) + receivers = self.df.sample(n=len(self.active_agents), with_replacement=True) + self["active", "wealth"] -= 1 + income = receivers.group_by("unique_id").len() + self[income["unique_id"], "wealth"] += income["len"] + + +class MoneyModel(Model): + def __init__(self, n: int): + super().__init__() + self.sets.add(MoneyAgents(n, self)) + self.dc = DataCollector( + model=self, + model_reporters={ + "total_wealth": lambda m: m.sets["MoneyAgents"].df["wealth"].sum(), + "n_agents": lambda m: len(m.sets["MoneyAgents"]), + }, + agent_reporters={ + "wealth": "wealth", # pull existing column + }, + storage="memory", # we'll switch this per example + storage_uri=None, + trigger=lambda m: m.steps % 2 + == 0, # collect every 2 steps via conditional_collect + reset_memory=True, + ) + + def step(self): + self.sets.do("step") + + def run(self, steps: int, conditional: bool = True): + for _ in range(steps): + self.step() + self.dc.conditional_collect() # or .collect if you want to collect every step regardless of trigger + + +model = MoneyModel(1000) +model.run(10) +model.dc.data # peek in-memory dataframes + +# %% [markdown] +"""## Saving the data for later use + +`DataCollector` supports multiple storage backends. +Files are saved with **step number** and **batch number** (e.g., `model_step10_batch2.csv`) so multiple collects at the same step don’t overwrite. + +- **CSV:** `storage="csv"` → writes `model_step{n}_batch{k}.csv`, easy to open anywhere. +- **Parquet:** `storage="parquet"` → compressed, efficient for large datasets. +- **S3:** `storage="S3-csv"`/`storage="S3-parquet"` → saves CSV/Parquet directly to Amazon S3. +- **PostgreSQL:** `storage="postgresql"` → inserts results into `model_data` and `agent_data` tables for querying.""" + +# %% [markdown] +"""## Writing to Local CSV + +Switch the storage to `csv` and provide a folder path. Files are written as `model_step{n}.csv` and `agent_step{n}.csv`.""" + +# %% +import os + +os.makedirs("./data_csv", exist_ok=True) +model_csv = MoneyModel(1000) +model_csv.dc = DataCollector( + model=model_csv, + model_reporters={ + "total_wealth": lambda m: m.sets["MoneyAgents"].df["wealth"].sum(), + "n_agents": lambda m: len(m.sets["MoneyAgents"]), + }, + agent_reporters={ + "wealth": "wealth", + }, + storage="csv", # saving as csv + storage_uri="./data_csv", + trigger=lambda m: m._steps % 2 == 0, + reset_memory=True, +) +model_csv.run(10) +model_csv.dc.flush() +os.listdir("./data_csv") + +# %% [markdown] +"""## Writing to Local Parquet + +Use `parquet` for columnar output.""" + +# %% +os.makedirs("./data_parquet", exist_ok=True) +model_parq = MoneyModel(1000) +model_parq.dc = DataCollector( + model=model_parq, + model_reporters={ + "total_wealth": lambda m: m.sets["MoneyAgents"].df["wealth"].sum(), + "n_agents": lambda m: len(m.sets["MoneyAgents"]), + }, + agent_reporters={ + "wealth": "wealth", + }, + storage="parquet", # save as parquet + storage_uri="data_parquet", + trigger=lambda m: m._steps % 2 == 0, + reset_memory=True, +) +model_parq.run(10) +model_parq.dc.flush() +os.listdir("./data_parquet") + +# %% [markdown] +"""## Writing to Amazon S3 (CSV or Parquet) + +Set AWS credentials via environment variables or your usual config. Then choose `S3-csv` or `S3-parquet` and pass an S3 URI (e.g., `s3://my-bucket/experiments/run-1`). + +> **Note:** This cell requires network access & credentials when actually run.""" + +# %% +model_s3 = MoneyModel(1000) +model_s3.dc = DataCollector( + model=model_s3, + model_reporters={ + "total_wealth": lambda m: m.sets["MoneyAgents"].df["wealth"].sum(), + "n_agents": lambda m: len(m.sets["MoneyAgents"]), + }, + agent_reporters={ + "wealth": "wealth", + }, + storage="S3-csv", # save as csv in S3 + storage_uri="s3://my-bucket/experiments/run-1", # change it to required path + trigger=lambda m: m._steps % 2 == 0, + reset_memory=True, +) +model_s3.run(10) +model_s3.dc.flush() + +# %% [markdown] +"""## Writing to PostgreSQL + +PostgreSQL requires that the target tables exist and that the expected reporter columns are present. The collector will validate tables/columns up front and raise descriptive errors if something is missing. + +Below is a minimal schema example. Adjust columns to your configured reporters.""" + +# %% +DDL_MODEL = r""" +CREATE SCHEMA IF NOT EXISTS public; +CREATE TABLE IF NOT EXISTS public.model_data ( + step INTEGER, + seed VARCHAR, + total_wealth BIGINT, + n_agents INTEGER +); +""" +DDL_AGENT = r""" +CREATE TABLE IF NOT EXISTS public.agent_data ( + step INTEGER, + seed VARCHAR, + unique_id BIGINT, + wealth BIGINT +); +""" +print(DDL_MODEL) +print(DDL_AGENT) + +# %% [markdown] +"""After creating the tables (outside this notebook or via a DB connection cell), configure and flush:""" + +# %% +POSTGRES_URI = "postgresql://user:pass@localhost:5432/mydb" +m_pg = MoneyModel(300) +m_pg.dc._storage = "postgresql" +m_pg.dc._storage_uri = POSTGRES_URI +m_pg.run(6) +m_pg.dc.flush() + +# %% [markdown] +"""## Triggers & Conditional Collection + +The collector accepts a `trigger: Callable[[Model], bool]`. When using `conditional_collect()`, the collector checks the trigger and collects only if it returns `True`. + +You can always call `collect()` to gather data unconditionally.""" + +# %% +m = MoneyModel(100) +m.dc.trigger = lambda model: model._steps % 3 == 0 # every 3rd step +m.run(10, conditional=True) +m.dc.data["model"].head() + +# %% [markdown] +"""## Troubleshooting + +- **ValueError: Please define a storage_uri** — for non-memory backends you must set `_storage_uri`. +- **Missing columns in table** — check the PostgreSQL error text; create/alter the table to include the columns for your configured `model_reporters` and `agent_reporters`, plus required `step` and `seed`. +- **Permissions/credentials errors** (S3/PostgreSQL) — ensure correct IAM/credentials or database permissions.""" + +# %% [markdown] +"""--- +*Generated on 2025-08-30.*""" From 978b8745145c3faedf29aa15d4057403cb6e636d Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 23 Sep 2025 12:26:12 +0200 Subject: [PATCH 231/329] docs: update README for clarity and organization, enhance benchmarks section, and improve installation instructions --- README.md | 156 ++++++++++++++++++++++++++---------------------------- 1 file changed, 75 insertions(+), 81 deletions(-) diff --git a/README.md b/README.md index 195d7625..b35537a0 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Classic [Mesa](https://github.com/projectmesa/mesa) stores each agent as a Pytho You keep the Mesa-style `Model` / `AgentSet` structure, but updates are vectorized and memory-efficient. ### Why it matters -- ⚡ **10× faster** bulk updates on 10k+ agents (see benchmarks) +- ⚡ **10× faster** bulk updates on 10k+ agents ([see Benchmarks](#benchmarks)) - 📊 **Columnar execution** via [Polars](https://docs.pola.rs/): [SIMD](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data) ops, multi-core support - 🔄 **Declarative logic**: agent rules as transformations, not Python loops - 🚀 **Roadmap**: Lazy queries and GPU support for even faster models @@ -35,115 +35,109 @@ You keep the Mesa-style `Model` / `AgentSet` structure, but updates are vectoriz ❌ **Not a good fit if:** your model depends on strict per-agent sequencing, complex non-vectorizable methods, or fine-grained identity tracking. +--- -### Install from Source (development) - -Clone the repository and install dependencies with [uv](https://docs.astral.sh/uv/): - -```bash -git clone https://github.com/projectmesa/mesa-frames.git -cd mesa-frames -uv sync --all-extras -``` +## Why DataFrames? -`uv sync` creates a local `.venv/` with mesa-frames and its development extras. Run tooling through uv to keep the virtual environment isolated: +DataFrames enable SIMD and columnar operations that are far more efficient than Python loops. +mesa-frames currently uses **Polars** as its backend. -```bash -uv run pytest -q --cov=mesa_frames --cov-report=term-missing -uv run ruff check . --fix -uv run pre-commit run -a -``` +| Feature | mesa (classic) | mesa-frames | +| ---------------------- | -------------- | ----------- | +| Storage | Python objects | Polars DataFrame | +| Updates | Loops | Vectorized ops | +| Memory overhead | High | Low | +| Max agents (practical) | ~10^3 | ~10^6+ | -## Usage +--- -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/projectmesa/mesa-frames/blob/main/docs/general/user-guide/2_introductory-tutorial.ipynb) +## Benchmarks -**Note:** mesa-frames is currently in its early stages of development. As such, the usage patterns and API are subject to change. Breaking changes may be introduced. Reports of feedback and issues are encouraged. +

+ + Reproduce Benchmarks + +

-[You can find the API documentation here](https://projectmesa.github.io/mesa-frames/api). -### Creation of an Agent +mesa-frames delivers consistent speedups across both toy and canonical ABMs. +At 10k agents, it runs **~10× faster** than classic Mesa, and the gap grows with scale. -The agent implementation differs from base mesa. Agents are only defined at the AgentSet level. You can import `AgentSet`. As in mesa, you subclass and make sure to call `super().__init__(model)`. You can use the `add` method or the `+=` operator to add agents to the AgentSet. Most methods mirror the functionality of `mesa.AgentSet`. Additionally, `mesa-frames.AgentSet` implements many dunder methods such as `AgentSet[mask, attr]` to get and set items intuitively. All operations are by default inplace, but if you'd like to use functional programming, mesa-frames implements a fast copy method which aims to reduce memory usage, relying on reference-only and native copy methods. +

+ Benchmark: Boltzmann Wealth + Benchmark: Sugarscape IG +

-```python -from mesa-frames import AgentSet -class MoneyAgents(AgentSet): - def __init__(self, n: int, model: Model): - super().__init__(model) - # Adding the agents to the agent set - self += pl.DataFrame( - {"wealth": pl.ones(n, eager=True)} - ) +--- - def step(self) -> None: - # The give_money method is called - self.do("give_money") +## Quick Start - def give_money(self): - # Active agents are changed to wealthy agents - self.select(self.wealth > 0) +

+ + Explore the Tutorials + +

- # Receiving agents are sampled (only native expressions currently supported) - other_agents = self.df.sample( - n=len(self.active_agents), with_replacement=True - ) +1. **Install** - # Wealth of wealthy is decreased by 1 - self["active", "wealth"] -= 1 +```bash + pip install mesa-frames +``` - # Compute the income of the other agents (only native expressions currently supported) - new_wealth = other_agents.group_by("unique_id").len() +Or for development: - # Add the income to the other agents - self[new_wealth, "wealth"] += new_wealth["len"] +```bash +git clone https://github.com/projectmesa/mesa-frames.git +cd mesa-frames +uv sync --all-extras ``` -### Creation of the Model +2. **Create a model** -Creation of the model is fairly similar to the process in mesa. You subclass `Model` and call `super().__init__()`. The `model.sets` attribute has the same interface as `mesa-frames.AgentSet`. You can use `+=` or `self.sets.add` with a `mesa-frames.AgentSet` (or a list of `AgentSet`) to add agents to the model. + ```python + from mesa_frames import AgentSet, Model + import polars as pl -```python -from mesa-frames import Model + class MoneyAgents(AgentSet): + def __init__(self, n: int, model: Model): + super().__init__(model) + self += pl.DataFrame({"wealth": pl.ones(n, eager=True)}) -class MoneyModelDF(Model): - def __init__(self, N: int, agents_cls): - super().__init__() - self.n_agents = N - self.sets += MoneyAgents(N, self) + def give_money(self): + self.select(self.wealth > 0) + other_agents = self.df.sample(n=len(self.active_agents), with_replacement=True) + self["active", "wealth"] -= 1 + new_wealth = other_agents.group_by("unique_id").len() + self[new_wealth, "wealth"] += new_wealth["len"] - def step(self): - # Executes the step method for every agentset in self.sets - self.sets.do("step") + def step(self): + self.do("give_money") - def run_model(self, n): - for _ in range(n): - self.step() -``` + class MoneyModelDF(Model): + def __init__(self, N: int): + super().__init__() + self.sets += MoneyAgents(N, self) -## What's Next? 🔮 + def step(self): + self.sets.do("step") + ``` -- Refine the API to make it more understandable for someone who is already familiar with the mesa package. The goal is to provide a seamless experience for users transitioning to or incorporating mesa-frames. -- Adding support for default mesa functions to ensure that the standard mesa functionality is preserved. -- Adding GPU functionality (cuDF and Dask-cuDF). -- Creating a decorator that will automatically vectorize an existing mesa model. This feature will allow users to easily tap into the performance enhancements that mesa-frames offers without significant code alterations. -- Creating a unique class for AgentSet, independent of the backend implementation. +--- -## License +## Roadmap -Copyright 2024 Adam Amer, Project Mesa team and contributors +> Community contributions welcome — see the [full roadmap](https://projectmesa.github.io/mesa-frames/general/roadmap) -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at +* Transition to LazyFrames for optimization and GPU support +* Auto-vectorize existing Mesa models via decorator +* Increase possible Spaces +* Refine the API to align to Mesa - http://www.apache.org/licenses/LICENSE-2.0 +--- + +## License -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. +Copyright © 2025 Adam Amer, Project Mesa team and contributors -For the full license text, see the [LICENSE](https://github.com/projectmesa/mesa-frames/blob/main/LICENSE) file in the GitHub repository. +Licensed under the [Apache License, Version 2.0](https://raw.githubusercontent.com/projectmesa/mesa-frames/refs/heads/main/LICENSE). \ No newline at end of file From 5b9c2e9ff2116615744f0bef01115acb82a13ed1 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 23 Sep 2025 12:29:40 +0200 Subject: [PATCH 232/329] docs: fix typos in advanced tutorial for clarity --- docs/general/user-guide/3_advanced_tutorial.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index ef1bfa4c..6c0c97d0 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -29,7 +29,7 @@ The update schedule matters for micro-behaviour, so we study three variants: 1. **Sequential loop (asynchronous):** This is the traditional definition. Ants move one at a time in random order. -This cannnot be vectorised easily as the best move for an ant might depend on the moves of earlier ants (for example, if they target the same cell). +This cannot be vectorised easily as the best move for an ant might depend on the moves of earlier ants (for example, if they target the same cell). 2. **Sequential with Numba:** matches the first variant but relies on a compiled helper for speed. 3. **Parallel (synchronous):** all ants propose moves; conflicts are resolved at @@ -932,7 +932,7 @@ def move(self) -> None: """ ### 3.5 Simultaneous Movement with Conflict Resolution (the Polars mesa-frames idiomatic way) -The previous implementation is optimal speed-wise but it's a bit low-level. It requires mantaining an occupancy grid and imperative loops and it might become tricky to extend with more complex movement rules or models. +The previous implementation is optimal speed-wise but it's a bit low-level. It requires maintaining an occupancy grid and imperative loops and it might become tricky to extend with more complex movement rules or models. To stay in mesa-frames idiom, we can implement a parallel movement policy that uses Polars DataFrame operations to resolve conflicts when multiple agents target the same cell. These conflicts are resolved in rounds: in each round, each agent proposes its current best candidate cell; winners per cell are chosen at random, and losers are promoted to their next-ranked choice. This continues until all agents have moved. This implementation is a tad slower but still efficient and easier to read (for a Polars user). From 5ac548c1a522364e11bc4d2000717c31918768b8 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 23 Sep 2025 12:29:48 +0200 Subject: [PATCH 233/329] docs: update Data Collector tutorial for clarity and organization --- docs/general/user-guide/4_datacollector.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/general/user-guide/4_datacollector.py b/docs/general/user-guide/4_datacollector.py index b08e35a2..16d9837b 100644 --- a/docs/general/user-guide/4_datacollector.py +++ b/docs/general/user-guide/4_datacollector.py @@ -75,14 +75,14 @@ def run(self, steps: int, conditional: bool = True): model.dc.data # peek in-memory dataframes # %% [markdown] -"""## Saving the data for later use - -`DataCollector` supports multiple storage backends. -Files are saved with **step number** and **batch number** (e.g., `model_step10_batch2.csv`) so multiple collects at the same step don’t overwrite. - -- **CSV:** `storage="csv"` → writes `model_step{n}_batch{k}.csv`, easy to open anywhere. -- **Parquet:** `storage="parquet"` → compressed, efficient for large datasets. -- **S3:** `storage="S3-csv"`/`storage="S3-parquet"` → saves CSV/Parquet directly to Amazon S3. +"""## Saving the data for later use + +`DataCollector` supports multiple storage backends. +Files are saved with **step number** and **batch number** (e.g., `model_step10_batch2.csv`) so multiple collects at the same step don’t overwrite. + +- **CSV:** `storage="csv"` → writes `model_step{n}_batch{k}.csv`, easy to open anywhere. +- **Parquet:** `storage="parquet"` → compressed, efficient for large datasets. +- **S3:** `storage="S3-csv"`/`storage="S3-parquet"` → saves CSV/Parquet directly to Amazon S3. - **PostgreSQL:** `storage="postgresql"` → inserts results into `model_data` and `agent_data` tables for querying.""" # %% [markdown] From 39053e19c31e8f3dad1601323ab9b8f2a1d6f809 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 23 Sep 2025 12:30:13 +0200 Subject: [PATCH 234/329] docs: improve formatting and consistency in README.md --- README.md | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b35537a0..032483d7 100644 --- a/README.md +++ b/README.md @@ -15,23 +15,24 @@ ## Scale Mesa beyond its limits -Classic [Mesa](https://github.com/projectmesa/mesa) stores each agent as a Python object, which quickly becomes a bottleneck at scale. -**mesa-frames** reimagines agent storage using **Polars DataFrames**, so agents live in a columnar store rather than the Python heap. +Classic [Mesa](https://github.com/projectmesa/mesa) stores each agent as a Python object, which quickly becomes a bottleneck at scale. +**mesa-frames** reimagines agent storage using **Polars DataFrames**, so agents live in a columnar store rather than the Python heap. You keep the Mesa-style `Model` / `AgentSet` structure, but updates are vectorized and memory-efficient. ### Why it matters + - ⚡ **10× faster** bulk updates on 10k+ agents ([see Benchmarks](#benchmarks)) -- 📊 **Columnar execution** via [Polars](https://docs.pola.rs/): [SIMD](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data) ops, multi-core support -- 🔄 **Declarative logic**: agent rules as transformations, not Python loops +- 📊 **Columnar execution** via [Polars](https://docs.pola.rs/): [SIMD](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data) ops, multi-core support +- 🔄 **Declarative logic**: agent rules as transformations, not Python loops - 🚀 **Roadmap**: Lazy queries and GPU support for even faster models --- ## Who is it for? -- Researchers needing to scale to **tens or hundreds of thousands of agents** -- Users whose agent logic can be written as **vectorized, set-based operations** +- Researchers needing to scale to **tens or hundreds of thousands of agents** +- Users whose agent logic can be written as **vectorized, set-based operations** ❌ **Not a good fit if:** your model depends on strict per-agent sequencing, complex non-vectorizable methods, or fine-grained identity tracking. @@ -39,7 +40,7 @@ You keep the Mesa-style `Model` / `AgentSet` structure, but updates are vectoriz ## Why DataFrames? -DataFrames enable SIMD and columnar operations that are far more efficient than Python loops. +DataFrames enable SIMD and columnar operations that are far more efficient than Python loops. mesa-frames currently uses **Polars** as its backend. | Feature | mesa (classic) | mesa-frames | @@ -59,8 +60,7 @@ mesa-frames currently uses **Polars** as its backend.

- -mesa-frames delivers consistent speedups across both toy and canonical ABMs. +mesa-frames delivers consistent speedups across both toy and canonical ABMs. At 10k agents, it runs **~10× faster** than classic Mesa, and the gap grows with scale.

@@ -68,7 +68,6 @@ At 10k agents, it runs **~10× faster** than classic Mesa, and the gap grows wit Benchmark: Sugarscape IG

- --- ## Quick Start @@ -79,7 +78,7 @@ At 10k agents, it runs **~10× faster** than classic Mesa, and the gap grows wit

-1. **Install** +1. **Install** ```bash pip install mesa-frames @@ -129,10 +128,10 @@ uv sync --all-extras > Community contributions welcome — see the [full roadmap](https://projectmesa.github.io/mesa-frames/general/roadmap) -* Transition to LazyFrames for optimization and GPU support -* Auto-vectorize existing Mesa models via decorator -* Increase possible Spaces -* Refine the API to align to Mesa +- Transition to LazyFrames for optimization and GPU support +- Auto-vectorize existing Mesa models via decorator +- Increase possible Spaces +- Refine the API to align to Mesa --- @@ -140,4 +139,4 @@ uv sync --all-extras Copyright © 2025 Adam Amer, Project Mesa team and contributors -Licensed under the [Apache License, Version 2.0](https://raw.githubusercontent.com/projectmesa/mesa-frames/refs/heads/main/LICENSE). \ No newline at end of file +Licensed under the [Apache License, Version 2.0](https://raw.githubusercontent.com/projectmesa/mesa-frames/refs/heads/main/LICENSE). From 8d7acbf16b779ffa32b99f4c662d37849ab852ae Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 23 Sep 2025 12:33:54 +0200 Subject: [PATCH 235/329] docs: clarify parameter descriptions in advanced tutorial --- .../general/user-guide/3_advanced_tutorial.py | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/user-guide/3_advanced_tutorial.py index 6c0c97d0..b1009734 100644 --- a/docs/general/user-guide/3_advanced_tutorial.py +++ b/docs/general/user-guide/3_advanced_tutorial.py @@ -221,9 +221,12 @@ def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: Parameters ---------- - x, y : np.ndarray - One-dimensional numeric arrays of the same length containing the two - variables to correlate. + x : np.ndarray + One-dimensional numeric array containing the first variable to + correlate. + y : np.ndarray + One-dimensional numeric array containing the second variable to + correlate. Returns ------- @@ -254,7 +257,7 @@ class Sugarscape(Model): Parameters ---------- - agent_type : type + agent_type : type[AntsBase] The :class:`AgentSet` subclass implementing the movement rules (sequential, numba-accelerated, or parallel). n_agents : int @@ -266,7 +269,7 @@ class Sugarscape(Model): max_sugar : int, optional Upper bound for the randomly initialised sugar values on the grid, by default 4. - seed : int or None, optional + seed : int | None, optional RNG seed to make runs reproducible across variants, by default None. Notes @@ -638,9 +641,9 @@ def _choose_best_cell( Agent's current coordinate. vision : int Maximum vision radius along cardinal axes. - sugar_map : dict + sugar_map : dict[tuple[int, int], int] Mapping from ``(x, y)`` to sugar amount. - blocked : set or None + blocked : set[tuple[int, int]] | None Optional set of coordinates that should be considered occupied and therefore skipped (except the origin which is always allowed). @@ -687,7 +690,7 @@ def _current_sugar_map(self) -> dict[tuple[int, int], int]: Returns ------- - dict + dict[tuple[int, int], int] Keys are ``(x, y)`` tuples and values are the integer sugar amount on that cell (zero if missing/None). """ @@ -749,12 +752,22 @@ def _numba_should_replace( Parameters ---------- - best_sugar, candidate_sugar : int - Sugar at the current best cell and the candidate cell. - best_distance, candidate_distance : int - Manhattan distances from the origin to the best and candidate cells. - best_x, best_y, candidate_x, candidate_y : int - Coordinates used for the final lexicographic tie-break. + best_sugar : int + Sugar at the current best cell. + best_distance : int + Manhattan distance from the origin to the current best cell. + best_x : int + X coordinate of the current best cell. + best_y : int + Y coordinate of the current best cell. + candidate_sugar : int + Sugar at the candidate cell. + candidate_distance : int + Manhattan distance from the origin to the candidate cell. + candidate_x : int + X coordinate of the candidate cell. + candidate_y : int + Y coordinate of the candidate cell. Returns ------- @@ -859,9 +872,12 @@ def sequential_move_numba( Parameters ---------- - dim0, dim1 : np.ndarray - 1D integer arrays of length n_agents containing the x and y - coordinates for each agent. + dim0 : np.ndarray + 1D integer array of length n_agents containing the x coordinates + for each agent. + dim1 : np.ndarray + 1D integer array of length n_agents containing the y coordinates + for each agent. vision : np.ndarray 1D integer array of vision radii for each agent. sugar_array : np.ndarray From 24ba420379eff79d113e999926ffddfd092b43c0 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 23 Sep 2025 12:34:00 +0200 Subject: [PATCH 236/329] docs: improve README formatting and consistency --- README.md | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 032483d7..9ff6205a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ +

- Mesa logo + Mesa logo

mesa-frames

+ | | | | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -54,29 +56,20 @@ mesa-frames currently uses **Polars** as its backend. ## Benchmarks -

- - Reproduce Benchmarks - -

+[![Reproduce Benchmarks](https://img.shields.io/badge/Reproduce%20Benchmarks-📊-orange?style=for-the-badge)](https://projectmesa.github.io/mesa-frames/general/benchmarks/) mesa-frames delivers consistent speedups across both toy and canonical ABMs. At 10k agents, it runs **~10× faster** than classic Mesa, and the gap grows with scale. -

- Benchmark: Boltzmann Wealth - Benchmark: Sugarscape IG -

+![Benchmark: Boltzmann Wealth](examples/boltzmann_wealth/boltzmann_benchmark.png) + +![Benchmark: Sugarscape IG](examples/sugarscape/sugarscape_benchmark.png) --- ## Quick Start -

- - Explore the Tutorials - -

+[![Explore the Tutorials](https://img.shields.io/badge/Explore%20the%20Tutorials-📚-blue?style=for-the-badge)](https://projectmesa.github.io/mesa-frames/general/user-guide/) 1. **Install** @@ -92,7 +85,7 @@ cd mesa-frames uv sync --all-extras ``` -2. **Create a model** +1. **Create a model** ```python from mesa_frames import AgentSet, Model From 0279f6a88e58208ad7382b2edee059b7ebccfdbe Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Wed, 24 Sep 2025 09:16:31 +0200 Subject: [PATCH 237/329] feat: add initial implementation of Boltzmann wealth example --- examples/boltzmann_wealth/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 examples/boltzmann_wealth/__init__.py diff --git a/examples/boltzmann_wealth/__init__.py b/examples/boltzmann_wealth/__init__.py new file mode 100644 index 00000000..e69de29b From ea06c973e5694f6ddbbc3edb44c1c38c306c390a Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Wed, 24 Sep 2025 11:41:13 +0200 Subject: [PATCH 238/329] feat: update docs dependencies to include typer version 0.9.0 --- pyproject.toml | 1 + uv.lock | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 8ecbc911..c130f9e5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,6 +64,7 @@ test = [ docs = [ { include-group = "typechecking" }, + "typer[all]>=0.9.0", "mkdocs-material>=9.6.14", "mkdocs-jupyter>=0.25.1", "mkdocs-git-revision-date-localized-plugin>=1.4.7", diff --git a/uv.lock b/uv.lock index 4e4d7e1d..8095193c 100644 --- a/uv.lock +++ b/uv.lock @@ -1255,6 +1255,7 @@ dev = [ { name = "sphinx-copybutton" }, { name = "sphinx-design" }, { name = "sphinx-rtd-theme" }, + { name = "typer" }, ] docs = [ { name = "autodocsumm" }, @@ -1275,6 +1276,7 @@ docs = [ { name = "sphinx-copybutton" }, { name = "sphinx-design" }, { name = "sphinx-rtd-theme" }, + { name = "typer" }, ] test = [ { name = "beartype" }, @@ -1319,6 +1321,7 @@ dev = [ { name = "sphinx-copybutton", specifier = ">=0.5.2" }, { name = "sphinx-design", specifier = ">=0.6.1" }, { name = "sphinx-rtd-theme", specifier = ">=3.0.2" }, + { name = "typer", extras = ["all"], specifier = ">=0.9.0" }, ] docs = [ { name = "autodocsumm", specifier = ">=0.2.14" }, @@ -1339,6 +1342,7 @@ docs = [ { name = "sphinx-copybutton", specifier = ">=0.5.2" }, { name = "sphinx-design", specifier = ">=0.6.1" }, { name = "sphinx-rtd-theme", specifier = ">=3.0.2" }, + { name = "typer", extras = ["all"], specifier = ">=0.9.0" }, ] test = [ { name = "beartype", specifier = ">=0.21.0" }, @@ -2524,6 +2528,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/83/11/00d3c3dfc25ad54e731d91449895a79e4bf2384dc3ac01809010ba88f6d5/seaborn-0.13.2-py3-none-any.whl", hash = "sha256:636f8336facf092165e27924f223d3c62ca560b1f2bb5dff7ab7fad265361987", size = 294914, upload-time = "2024-01-25T13:21:49.598Z" }, ] +[[package]] +name = "shellingham" +version = "1.5.4" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/58/15/8b3609fd3830ef7b27b655beb4b4e9c62313a4e8da8c676e142cc210d58e/shellingham-1.5.4.tar.gz", hash = "sha256:8dbca0739d487e5bd35ab3ca4b36e11c4078f3a234bfce294b0a0291363404de", size = 10310, upload-time = "2023-10-24T04:13:40.426Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e0/f9/0595336914c5619e5f28a1fb793285925a8cd4b432c9da0a987836c7f822/shellingham-1.5.4-py2.py3-none-any.whl", hash = "sha256:7ecfff8f2fd72616f7481040475a65b2bf8af90a56c89140852d1120324e8686", size = 9755, upload-time = "2023-10-24T04:13:38.866Z" }, +] + [[package]] name = "six" version = "1.17.0" @@ -2836,6 +2849,21 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/00/c0/8f5d070730d7836adc9c9b6408dec68c6ced86b304a9b26a14df072a6e8c/traitlets-5.14.3-py3-none-any.whl", hash = "sha256:b74e89e397b1ed28cc831db7aea759ba6640cb3de13090ca145426688ff1ac4f", size = 85359, upload-time = "2024-04-19T11:11:46.763Z" }, ] +[[package]] +name = "typer" +version = "0.19.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "click" }, + { name = "rich" }, + { name = "shellingham" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/21/ca/950278884e2ca20547ff3eb109478c6baf6b8cf219318e6bc4f666fad8e8/typer-0.19.2.tar.gz", hash = "sha256:9ad824308ded0ad06cc716434705f691d4ee0bfd0fb081839d2e426860e7fdca", size = 104755, upload-time = "2025-09-23T09:47:48.256Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/00/22/35617eee79080a5d071d0f14ad698d325ee6b3bf824fc0467c03b30e7fa8/typer-0.19.2-py3-none-any.whl", hash = "sha256:755e7e19670ffad8283db353267cb81ef252f595aa6834a0d1ca9312d9326cb9", size = 46748, upload-time = "2025-09-23T09:47:46.777Z" }, +] + [[package]] name = "typing-extensions" version = "4.15.0" From bb75630d699e10be5e4c6c4eeba985ed729a761f Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Wed, 24 Sep 2025 19:59:12 +0200 Subject: [PATCH 239/329] feat: add Typer CLI for mesa vs mesa-frames performance benchmarks --- benchmarks/cli.py | 216 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 benchmarks/cli.py diff --git a/benchmarks/cli.py b/benchmarks/cli.py new file mode 100644 index 00000000..c0b9355d --- /dev/null +++ b/benchmarks/cli.py @@ -0,0 +1,216 @@ +"""Typer CLI for running mesa vs mesa-frames performance benchmarks.""" + +from __future__ import annotations + +from dataclasses import dataclass +from datetime import datetime +from pathlib import Path +from time import perf_counter +from typing import Literal, Annotated, Protocol, Optional + +import matplotlib.pyplot as plt +import polars as pl +import seaborn as sns +import typer + +from examples.boltzmann_wealth import backend_frames as boltzmann_frames +from examples.boltzmann_wealth import backend_mesa as boltzmann_mesa +from examples.sugarscape_ig.backend_frames import model as sugarscape_frames +from examples.sugarscape_ig.backend_mesa import model as sugarscape_mesa + +app = typer.Typer(add_completion=False) + +class RunnerP(Protocol): + def __call__(self, agents: int, steps: int, seed: Optional[int] = None) -> None: ... + + +@dataclass(slots=True) +class Backend: + name: Literal['mesa', 'frames'] + runner: RunnerP + + +@dataclass(slots=True) +class ModelConfig: + name: str + backends: list[Backend] + + +MODELS: dict[str, ModelConfig] = { + "boltzmann": ModelConfig( + name="boltzmann", + backends=[ + Backend(name="mesa", runner=boltzmann_mesa.simulate), + Backend(name="frames", runner=boltzmann_frames.simulate), + ], + ), + "sugarscape": ModelConfig( + name="sugarscape", + backends=[ + Backend( + name="mesa", + runner=sugarscape_mesa.simulate, + ), + Backend( + name="frames", + runner=sugarscape_frames.simulate, + ), + ], + ), +} + +def _parse_agents(value: str) -> list[int]: + value = value.strip() + if ":" in value: + parts = value.split(":") + if len(parts) != 3: + raise typer.BadParameter("Ranges must use start:stop:step format") + try: + start, stop, step = (int(part) for part in parts) + except ValueError as exc: + raise typer.BadParameter("Range values must be integers") from exc + if step <= 0: + raise typer.BadParameter("Step must be positive") + if start <= 0 or stop <= 0: + raise typer.BadParameter("Range endpoints must be positive") + if start > stop: + raise typer.BadParameter("Range start must be <= stop") + counts = list(range(start, stop + step, step)) + if counts[-1] > stop: + counts.pop() + return counts + try: + agents = int(value) + except ValueError as exc: # pragma: no cover - defensive + raise typer.BadParameter("Agent count must be an integer") from exc + if agents <= 0: + raise typer.BadParameter("Agent count must be positive") + return [agents] + +def _parse_models(value: str) -> list[str]: + """Parse models option into a list of model keys. + + Accepts: + - "all" -> returns all available model keys + - a single model name -> returns [name] + - a comma-separated list of model names -> returns list + + Validates that each selected model exists in MODELS. + """ + value = value.strip() + if value == "all": + return list(MODELS.keys()) + # support comma-separated lists + parts = [part.strip() for part in value.split(",") if part.strip()] + if not parts: + raise typer.BadParameter("Model selection must not be empty") + unknown = [p for p in parts if p not in MODELS] + if unknown: + raise typer.BadParameter(f"Unknown model selection: {', '.join(unknown)}") + # preserve order and uniqueness + seen = set() + result: list[str] = [] + for p in parts: + if p not in seen: + seen.add(p) + result.append(p) + return result + +def _plot_performance( + df: pl.DataFrame, model_name: str, output_dir: Path, timestamp: str +) -> None: + if df.is_empty(): + return + for theme, style in {"light": "whitegrid", "dark": "darkgrid"}.items(): + sns.set_theme(style=style) + fig, ax = plt.subplots(figsize=(8, 5)) + sns.lineplot( + data=df.to_pandas(), + x="agents", + y="runtime_seconds", + hue="backend", + estimator="mean", + errorbar="sd", + marker="o", + ax=ax, + ) + ax.set_title(f"{model_name.title()} runtime vs agents") + ax.set_xlabel("Agents") + ax.set_ylabel("Runtime (seconds)") + fig.tight_layout() + filename = output_dir / f"{model_name}_runtime_{timestamp}_{theme}.png" + fig.savefig(filename, dpi=300) + plt.close(fig) + + +@app.command() +def run( + models: Annotated[str, typer.Option( + help="Models to benchmark: boltzmann, sugarscape, or all", + callback=_parse_models + )] = "all", + agents: Annotated[list[int], typer.Option( + help="Agent count or range (start:stop:step)", + callback=_parse_agents + )] = "1000:5000:1000", + steps: Annotated[int, typer.Option( + min=0, + help="Number of steps per run.", + )] = 100, + repeats: Annotated[int, typer.Option(help="Repeats per configuration.", min=1)] = 1, + seed: Annotated[int, typer.Option(help="Optional RNG seed.")] = 42, + save: Annotated[bool, typer.Option(help="Persist benchmark CSV results.")] = True, + plot: Annotated[bool, typer.Option(help="Render performance plots.")] = True, + results_dir: Annotated[Path, typer.Option( + help="Directory for benchmark CSV results.", + )] = Path(__file__).resolve().parent / "results", + plots_dir: Annotated[Path, typer.Option( + help="Directory for benchmark plots.", + )] = Path(__file__).resolve().parent / "plots", +) -> None: + """Run performance benchmarks for the models models.""" + rows: list[dict[str, object]] = [] + timestamp = datetime.now(datetime.timezone.utc).strftime("%Y%m%d_%H%M%S") + for model in models: + config = MODELS[model] + typer.echo(f"Benchmarking {model} with agents {agents}") + for agents_count in agents: + for repeat_idx in range(repeats): + run_seed = seed + repeat_idx + for backend in config.backends: + start = perf_counter() + backend.runner(agents_count, steps, run_seed) + runtime = perf_counter() - start + rows.append( + { + "model": model, + "backend": backend.name, + "agents": agents_count, + "steps": steps, + "seed": run_seed, + "repeat_idx": repeat_idx, + "runtime_seconds": runtime, + "timestamp": timestamp, + } + ) + if not rows: + typer.echo("No benchmark data collected.") + return + df = pl.DataFrame(rows) + if save: + results_dir.mkdir(parents=True, exist_ok=True) + for model in models: + model_df = df.filter(pl.col("model") == model) + csv_path = results_dir / f"{model}_perf_{timestamp}.csv" + model_df.write_csv(csv_path) + typer.echo(f"Saved {model} results to {csv_path}") + if plot: + plots_dir.mkdir(parents=True, exist_ok=True) + for model in models: + model_df = df.filter(pl.col("model") == model) + _plot_performance(model_df, model, plots_dir, timestamp) + typer.echo(f"Saved {model} plots under {plots_dir}") + + +if __name__ == "__main__": + app() From 6a6604cd4d825388feb0c8ea2ac299194c6d2703 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Thu, 25 Sep 2025 19:37:43 +0200 Subject: [PATCH 240/329] feat: remove unused images and performance plot for Boltzmann wealth model; add Sugarscape IG backend package --- .../boltzmann_wealth/boltzmann_no_mesa.png | Bin 59194 -> 0 bytes .../boltzmann_wealth/boltzmann_with_mesa.png | Bin 61887 -> 0 bytes examples/boltzmann_wealth/performance_plot.py | 239 ------------------ .../sugarscape_ig/backend_frames/__init__.py | 1 + examples/sugarscape_ig/mesa_comparison.png | Bin 31762 -> 0 bytes examples/sugarscape_ig/polars_comparison.png | Bin 70235 -> 0 bytes 6 files changed, 1 insertion(+), 239 deletions(-) delete mode 100644 examples/boltzmann_wealth/boltzmann_no_mesa.png delete mode 100644 examples/boltzmann_wealth/boltzmann_with_mesa.png delete mode 100644 examples/boltzmann_wealth/performance_plot.py create mode 100644 examples/sugarscape_ig/backend_frames/__init__.py delete mode 100644 examples/sugarscape_ig/mesa_comparison.png delete mode 100644 examples/sugarscape_ig/polars_comparison.png diff --git a/examples/boltzmann_wealth/boltzmann_no_mesa.png b/examples/boltzmann_wealth/boltzmann_no_mesa.png deleted file mode 100644 index 369597e2648d065bcfe3a4b628aad54dcb5dab7d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 59194 zcmeFZXEa=I_%@nEL=6!&(Sk5U8KU>-X^4mtz4zX`=zT;aI-`Z?(Z__*4bi*k3`UGT zQAau3@BhAMt#j7-de-@JEM;utnZ5UO*Xz3OiFv1{a-aAy@vU38?!S7e`2N-{Jl9*d z?r;#^1CCrq<23?5Bs`RKJv5!IJiJZaEN`irdbl_^dpOvdJ@K-1bGLPN666!$=i}#j zV&mcA;w}LOJN}=~;B$7f27h(abpuu*a(VgD{njmV)0?l`KV*w-Z{6A`dZj4$!6$1w z8{dNwdDJ5-AgJFE|0Vqo1o-UC5*ZP)ecvPGI9s&S`n67VtnR+>TL_mG@IfP3dxg$K z2l{?t*RNrp`_j53zW363(O~ z`4lV^pm3Y7A3l7@{OavnMW{^RPs{$4H!_m$8>EP% z3g`4;+{s>|@#U6I@8Dp?zwG8_spiwwI65gGe!t`G7$kEr14uomxk!YXmKJr=dM&*% z3{C9mN@7#t4?Oqq_&bJA2~sORmX!3`ZlhF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mu%`aoSt68$=~@SOPhrCN#*tmDajEwbYq4A!0U(GDSS&DZ1Kb z-gN_+aEVyNzwp`9*su~m(vnnu5FrDpzL+I7oZc`rE&@Hl1*84KOt)#%mSq>B_o62!5n-e9`mQj6w_N zDbGPX>Huos%J(nZ|F9cJQtJ_T(CC6~Vh#+(!d+L=z^JY zzj(3e@#8I+hk>>3L}$3O_0{j=IoAO+XKj&E=G&FBXq9yiF+Y^RqH_I$8bQV#Zt7Qi znmC&b&)&t>QZld$ILoU-B8fH0R|kGAc@`QlWRU3{B{5@K{H2Uv>p252YCbRD-4-)c zz|L6-*LX}6Ai|5gZWb8aZMhjXGpQeSh^Dzdd}|MwnXQUMW+~(q$QDdG2Cgewxh;iJ z{19S8W;*haGVh&H;Ul_7=LV{l{7Kh1L1F!|0bez8|w zUtco;pZ)EApr)@yM1fY3vY#Sw@Ee~n$C^NKA;CWDkGHLwL`^<=7zrD<1H?B3VsqC15Ax~D G_x=ZAXDPw} diff --git a/examples/boltzmann_wealth/boltzmann_with_mesa.png b/examples/boltzmann_wealth/boltzmann_with_mesa.png deleted file mode 100644 index 257d5d184ad412431459160e9653803462247749..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 61887 zcmdRWXHZjJ)UJwxQdLw$svsb}1gW9;iUJ~F0qGrr2%&`%x`=dX(z{BP-bo6 zrG*|KN(+QeAl!rc-CuX+-oN+EWE{ykd!N1b+N(V4SqXotrAkMAi~7u&Gj!@|D$mcH zq4;p-%sKXpl)ztl-NY^c|H!#L)OUN~1ab4UaJ4z3Y2oJV;N<3DXUXYd;|jBLas-Kp zi;0K{alUeMbB4)@ibDVQ9U@MywxS8H`mVqzmz>oMU}w%SSe$;&7Ah6kojJ29sIGEf z2cEGyb-^uRbcAwNdVL+yDF5*Br@Zw%EfdGJ5p2s^X(Oq0hFbVX)UCX?MlQ*-mp%6m zMt$0)pZs_As+vUHo`E0@L?Tso>wvtgh)0Y$NU^B=yp36w zorizE&(*r777=!Osb$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0AlnzB z?+0)uWK>-!X*!7b;W7RT^}U>TUOo5nk6^C+HzR%czhJa9r-lp*8SNGp7Mj>~RhRWe zA3X4^kKJWd%}7u$3vw;|pq{8(lx8deyl`@2aN`TjQ61B0PZ9rKlh1h@4lS})$tKY> z+W{xQM)`5JIr<`2Yt|~hiu#6w!1>A_K!hfeGwd=jF%?7nX1V4uvHqvojNH6!T9D)_ zlQ4UYr^w2@JY|F+3&P)l31>t1v(}QQSUKe2>-kl|ri_q3K+~N604+u+ z-*@LgvVb#~PJRw-!CP>uap#Uwdr;rYCg9(VQTN6ii6gFytd^JC%nu|}bSI!w$IT5G z$GV0_Vu(fR^sZ`FAsjxG#F1wOJtBKQ3qL=@7DI9WSh?t_nwA#EV2CPcBe1DOL>30gXY)U;EXwJ@Lh8UF07-!2)9|n^4T!!*5}!`= z^PI1br`0V0jz>A9K5*KqUQHS(`}stlBTt6qhpdeN6DADHl1Lm6d`b1VO*%WG< z0+UJG4u{#M$rTU5VSuBP-ALYJTejt2uh?}{=DT)AAVIsnKXu0I^hyDBKdVPmoFzgs znJrKlElBD$+0%-XlP%lVd`lAQU9&BS#v;w0A+wKHr2�BTEK(f7;-%LKe=Eb zb*vK3a{O2CD0*96*~n6gyzDapK1I~|Ux!`uyKMi0sQ>$U*8d1>1<9TIKOxuOa?^vv QTmVB&NfTP8@I2su0k*rW6aWAK diff --git a/examples/boltzmann_wealth/performance_plot.py b/examples/boltzmann_wealth/performance_plot.py deleted file mode 100644 index e565bda3..00000000 --- a/examples/boltzmann_wealth/performance_plot.py +++ /dev/null @@ -1,239 +0,0 @@ -import importlib.metadata - -import matplotlib.pyplot as plt -import mesa -import numpy as np -import perfplot -import polars as pl -import seaborn as sns -from packaging import version - -from mesa_frames import AgentSet, Model - - -### ---------- Mesa implementation ---------- ### -def mesa_implementation(n_agents: int) -> None: - model = MesaMoneyModel(n_agents) - model.run_model(100) - - -class MesaMoneyAgent(mesa.Agent): - """An agent with fixed initial wealth.""" - - def __init__(self, model): - # Pass the parameters to the parent class. - super().__init__(model) - - # Create the agent's variable and set the initial values. - self.wealth = 1 - - def step(self): - # Verify agent has some wealth - if self.wealth > 0: - other_agent = self.random.choice(self.model.agents) - if other_agent is not None: - other_agent.wealth += 1 - self.wealth -= 1 - - -class MesaMoneyModel(mesa.Model): - """A model with some number of agents.""" - - def __init__(self, N): - super().__init__() - self.num_agents = N - for _ in range(self.num_agents): - self.agents.add(MesaMoneyAgent(self)) - - def step(self): - """Advance the model by one step.""" - self.agents.shuffle_do("step") - - def run_model(self, n_steps) -> None: - for _ in range(n_steps): - self.step() - - -"""def compute_gini(model): - agent_wealths = model.sets.get("wealth") - x = sorted(agent_wealths) - N = model.num_agents - B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x)) - return 1 + (1 / N) - 2 * B""" - - -### ---------- Mesa-frames implementation ---------- ### - - -class MoneyAgentsConcise(AgentSet): - def __init__(self, n: int, model: Model): - super().__init__(model) - ## Adding the agents to the agent set - # 1. Changing the agents attribute directly (not recommended, if other agents were added before, they will be lost) - """self.sets = pl.DataFrame( - "wealth": pl.ones(n, eager=True)} - )""" - # 2. Adding the dataframe with add - """self.add( - pl.DataFrame( - { - "wealth": pl.ones(n, eager=True), - } - ) - )""" - # 3. Adding the dataframe with __iadd__ - self += pl.DataFrame({"wealth": pl.ones(n, eager=True)}) - - def step(self) -> None: - # The give_money method is called - # self.give_money() - self.do("give_money") - - def give_money(self): - ## Active agents are changed to wealthy agents - # 1. Using the __getitem__ method - # self.select(self["wealth"] > 0) - # 2. Using the fallback __getattr__ method - self.select(self.wealth > 0) - - # Receiving agents are sampled (only native expressions currently supported) - other_agents = self.df.sample(n=len(self.active_agents), with_replacement=True) - - # Wealth of wealthy is decreased by 1 - # 1. Using the __setitem__ method with self.active_agents mask - # self[self.active_agents, "wealth"] -= 1 - # 2. Using the __setitem__ method with "active" mask - self["active", "wealth"] -= 1 - - # Compute the income of the other agents (only native expressions currently supported) - new_wealth = other_agents.group_by("unique_id").len() - - # Add the income to the other agents - # 1. Using the set method - """self.set( - attr_names="wealth", - values=pl.col("wealth") + new_wealth["len"], - mask=new_wealth, - )""" - - # 2. Using the __setitem__ method - self[new_wealth, "wealth"] += new_wealth["len"] - - -class MoneyAgentsNative(AgentSet): - def __init__(self, n: int, model: Model): - super().__init__(model) - self += pl.DataFrame({"wealth": pl.ones(n, eager=True)}) - - def step(self) -> None: - self.do("give_money") - - def give_money(self): - ## Active agents are changed to wealthy agents - self.select(pl.col("wealth") > 0) - - other_agents = self.df.sample(n=len(self.active_agents), with_replacement=True) - - # Wealth of wealthy is decreased by 1 - self.df = self.df.with_columns( - wealth=pl.when( - pl.col("unique_id").is_in(self.active_agents["unique_id"].implode()) - ) - .then(pl.col("wealth") - 1) - .otherwise(pl.col("wealth")) - ) - - new_wealth = other_agents.group_by("unique_id").len() - - # Add the income to the other agents - self.df = ( - self.df.join(new_wealth, on="unique_id", how="left") - .fill_null(0) - .with_columns(wealth=pl.col("wealth") + pl.col("len")) - .drop("len") - ) - - -class MoneyModel(Model): - def __init__(self, N: int, agents_cls): - super().__init__() - self.n_agents = N - self.sets += agents_cls(N, self) - - def step(self): - # Executes the step method for every agentset in self.sets - self.sets.do("step") - - def run_model(self, n): - for _ in range(n): - self.step() - - -def mesa_frames_polars_concise(n_agents: int) -> None: - model = MoneyModel(n_agents, MoneyAgentsConcise) - model.run_model(100) - - -def mesa_frames_polars_native(n_agents: int) -> None: - model = MoneyModel(n_agents, MoneyAgentsNative) - model.run_model(100) - - -def plot_and_print_benchmark(labels, kernels, n_range, title, image_path): - out = perfplot.bench( - setup=lambda n: n, - kernels=kernels, - labels=labels, - n_range=n_range, - xlabel="Number of agents", - equality_check=None, - title=title, - ) - - plt.ylabel("Execution time (s)") - out.save(image_path, transparent=False) - - print("\nExecution times:") - for i, label in enumerate(labels): - print(f"---------------\n{label}:") - for n, t in zip(out.n_range, out.timings_s[i]): - print(f" Number of agents: {n}, Time: {t:.2f} seconds") - print("---------------") - - -def main(): - sns.set_theme(style="whitegrid") - - labels_0 = [ - "mesa", - "mesa-frames (pl concise)", - "mesa-frames (pl native)", - ] - kernels_0 = [ - mesa_implementation, - mesa_frames_polars_concise, - mesa_frames_polars_native, - ] - n_range_0 = [k for k in range(0, 100001, 10000)] - title_0 = "100 steps of the Boltzmann Wealth model:\n" + " vs ".join(labels_0) - image_path_0 = "boltzmann_with_mesa.png" - - plot_and_print_benchmark(labels_0, kernels_0, n_range_0, title_0, image_path_0) - - labels_1 = [ - "mesa-frames (pl concise)", - "mesa-frames (pl native)", - ] - kernels_1 = [ - mesa_frames_polars_concise, - mesa_frames_polars_native, - ] - n_range_1 = [k for k in range(100000, 1000001, 100000)] - title_1 = "100 steps of the Boltzmann Wealth model:\n" + " vs ".join(labels_1) - image_path_1 = "boltzmann_no_mesa.png" - - plot_and_print_benchmark(labels_1, kernels_1, n_range_1, title_1, image_path_1) - - -if __name__ == "__main__": - main() diff --git a/examples/sugarscape_ig/backend_frames/__init__.py b/examples/sugarscape_ig/backend_frames/__init__.py new file mode 100644 index 00000000..614fa64d --- /dev/null +++ b/examples/sugarscape_ig/backend_frames/__init__.py @@ -0,0 +1 @@ +"""mesa-frames backend package for Sugarscape IG examples.""" diff --git a/examples/sugarscape_ig/mesa_comparison.png b/examples/sugarscape_ig/mesa_comparison.png deleted file mode 100644 index c619ae281ba9601f5a1d659c9a1a5348e0a81f44..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 31762 zcmeFZ`9GBH-#mQf5G>M>v3K=&*MB^$7^}M)^qNkw6j79ND07T zFcb!T)By%V-h;sqd-!<3U#{-BoDVLBfyd4Tp7y&M806u91!n6Jh`ZqzcmsP$;r12( z0IZ*{u9l(JfxQaX0t0aYM%vmw|Mh^DpTC#3q8+jyyo4WzJ|6&siFiQ&!C#pdU}3PZ zD9q6#j=?#5lsv-2bG~18#0C)uW##&k)L%y?Wd0w%3H+N@Si_{^Ojmfcc4u z+1ZGI&&EFA-<(GiRWL-o`d=_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fH(o_p$i-HMYbjy#WTEA_E9_(7h2JEtL-w0MS=pFFBAsLpP= zp&>kcD0hxKmejWZWRt;Tc9(T`=ltVo!j>fEG3y;D`xD~AKaPnF-FZhy?#lDfXub8CBCC`pS*PAywmS59ob^^10jKGcw0Ojh zf56L#(_^q)?b%hmYl&N0$Bfv>9n}wK2TL-}iGJOpp6ya#xy}?)+&H_L(9Lg^)A*%U z*0Bn`+QNPPD_SY#>8%GmN1Al9E_6A5YS1E?oE!-f{JQoi*fHwo_n${xpKHBIed$r) zN=^@Nr(_lOnzdb#B04%oWshy&pH8s2AYK3D9Wn&=@A(n$*Ch9ub&Y3XSCK`+9us`v z#X-5M?XcJ2Jx6;7+=)b^PHe2AoYvRYlGhr>ezTJWxvE3IzC?{wt;h_#I3cIGtX3m( z(&bo}`77l|6T$h`jS#msza7 zp!I_!&+!8WjuY`o^bxmSoFPe{B)-?%MIWTB)5L5?3C}7@&CTlY%HB2A4!O%prQSr{ z{M^=JPsgh2C(F&1EA0hW6D;cTl)bMwIXOmsY|!b6es7VUHFnkC@N;=@Vp3pR<>36T zdkq1PIR79Xt8=#qf(exH2@j`V96WD#K0(xiu+cbVzcMFl>&dH3V|qWBPtF^y7mVB3 z9d%TA$eXPlO*pok6<+p2b(dIhYElU23Qy5XS*g_xlL7S{VMlhot(yuub3!-2vEdB$ zFfGvR(TG{BwOCBvev{FD!93+ZdY~ax(@>c)%cz5U+H zBLfO{TLwlOHrFS9#FqM_fB9}3i% zB!9U{p5q0#SINHkykVQKNIQaXWIGyI^4ABmUt?B}CA0_XA16&UQp>qt1aP$>u}{vn seDwOr^`iMdp(_aar^NrG_;H=nI?ZrS;MPP92L3ZRYOMSGu-*0l14n*iqyPW_ From da65b01c1b0226868711743922fef3f1d1b31102 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Thu, 25 Sep 2025 21:00:50 +0200 Subject: [PATCH 241/329] feat: add Boltzmann wealth model implementation with Typer CLI; include utility functions for simulation results and plotting --- .gitignore | 3 +- examples/__init__.py | 6 + examples/boltzmann_wealth/backend_frames.py | 154 ++++++++++++++++++++ examples/utils.py | 106 ++++++++++++++ 4 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 examples/__init__.py create mode 100644 examples/boltzmann_wealth/backend_frames.py create mode 100644 examples/utils.py diff --git a/.gitignore b/.gitignore index ca0ad990..45729158 100644 --- a/.gitignore +++ b/.gitignore @@ -158,4 +158,5 @@ docs/site docs/api/_build docs/general/user-guide/data_csv docs/general/user-guide/data_parquet -docs/api/reference/**/mesa_frames.*.rst \ No newline at end of file +docs/api/reference/**/mesa_frames.*.rst +examples/**/results \ No newline at end of file diff --git a/examples/__init__.py b/examples/__init__.py new file mode 100644 index 00000000..069e9dc5 --- /dev/null +++ b/examples/__init__.py @@ -0,0 +1,6 @@ +"""Examples package for the repository.""" + +__all__ = [ + "boltzmann_wealth", + "sugarscape_ig", +] diff --git a/examples/boltzmann_wealth/backend_frames.py b/examples/boltzmann_wealth/backend_frames.py new file mode 100644 index 00000000..67b803e6 --- /dev/null +++ b/examples/boltzmann_wealth/backend_frames.py @@ -0,0 +1,154 @@ +"""Mesa-frames implementation of the Boltzmann wealth model with Typer CLI.""" + +from __future__ import annotations + +from datetime import datetime, timezone +from pathlib import Path +from typing import Annotated + +import numpy as np +import polars as pl +import typer +from time import perf_counter + +from mesa_frames import AgentSet, DataCollector, Model + + +from examples.utils import SimulationResult, plot_model_metrics + + +# Note: by default we create a timestamped results directory under `results/`. +# The CLI will accept optional `results_dir` and `plots_dir` arguments to override. + + +def gini(frame: pl.DataFrame) -> float: + wealth = frame["wealth"] if "wealth" in frame.columns else pl.Series([]) + if wealth.is_empty(): + return float("nan") + values = wealth.to_numpy().astype(np.float64) + if values.size == 0: + return float("nan") + if np.allclose(values, 0.0): + return 0.0 + if np.allclose(values, values[0]): + return 0.0 + sorted_vals = np.sort(values) + n = sorted_vals.size + cumulative = np.cumsum(sorted_vals) + total = cumulative[-1] + if total == 0: + return 0.0 + index = np.arange(1, n + 1, dtype=np.float64) + return float((2.0 * np.dot(index, sorted_vals) / (n * total)) - (n + 1) / n) + + +class MoneyAgents(AgentSet): + """Vectorised agent set for the Boltzmann wealth exchange model.""" + + def __init__(self, model: Model, agents: int) -> None: + super().__init__(model) + self += pl.DataFrame({"wealth": pl.Series(np.ones(agents, dtype=np.int64))}) + + def step(self) -> None: + self.select(pl.col("wealth") > 0) + if len(self.active_agents) == 0: + return + # Use the model RNG to seed Polars sampling so results are reproducible + recipients = self.df.sample( + n=len(self.active_agents), with_replacement=True, seed=self.random.integers(np.iinfo(np.int32).max) + ) + # donors lose one unit + self["active", "wealth"] -= 1 + gains = recipients.group_by("unique_id").len() + self[gains, "wealth"] += gains["len"] + + +class MoneyModel(Model): + """Mesa-frames model that mirrors the Mesa implementation.""" + + def __init__( + self, agents: int, *, seed: int | None = None, results_dir: Path | None = None + ) -> None: + super().__init__(seed) + self.sets += MoneyAgents(self, agents) + storage_uri = str(results_dir) if results_dir is not None else None + self.datacollector = DataCollector( + model=self, + model_reporters={ + "gini": lambda m: gini(m.sets[0].df), + }, + storage="csv", + storage_uri=storage_uri, + ) + + def step(self) -> None: + self.sets.do("step") + self.datacollector.collect() + + def run(self, steps: int) -> None: + for _ in range(steps): + self.step() + + +def simulate( + agents: int, + steps: int, + seed: int | None = None, + results_dir: Path | None = None, +) -> SimulationResult: + model = MoneyModel(agents, seed=seed, results_dir=results_dir) + model.run(steps) + # collect data from datacollector into memory first + return SimulationResult(datacollector=model.datacollector) + + + +app = typer.Typer(add_completion=False) + +@app.command() +def run( + agents: Annotated[int, typer.Option(help="Number of agents to simulate.")] = 5000, + steps: Annotated[int, typer.Option(help="Number of model steps to run.")] = 100, + seed: Annotated[int | None, typer.Option(help="Optional RNG seed.")] = None, + plot: Annotated[bool, typer.Option(help="Render Seaborn plots.")] = True, + save_results: Annotated[bool, typer.Option(help="Persist metrics as CSV.")] = True, + results_dir: Annotated[Path | None, typer.Option(help="Directory to write CSV results and plots into. If omitted a timestamped subdir under `results/` is used.")] = None, +) -> None: + typer.echo(f"Running Boltzmann wealth model (mesa-frames) with {agents} agents for {steps} steps") + timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") + if results_dir is None: + results_dir = (Path(__file__).resolve().parent / "results" / timestamp).resolve() + results_dir.mkdir(parents=True, exist_ok=True) + start_time = perf_counter() + result = simulate(agents=agents, steps=steps, seed=seed, results_dir=results_dir) + + + typer.echo(f"Simulation complete in {perf_counter() - start_time:.2f} seconds") + + model_metrics = result.datacollector.data["model"].select("step", "gini") + + typer.echo(f"Metrics in the final 5 steps: {model_metrics[-5:]}") + + if save_results: + result.datacollector.flush() + + if plot: + stem = f"gini_{timestamp}" + # write plots into the results directory so outputs are colocated + plot_model_metrics( + model_metrics, + results_dir, + stem, + title="Boltzmann wealth — Gini", + subtitle=f"mesa-frames backend; seed={result.datacollector.seed}", + agents=agents, + steps=steps, + ) + typer.echo(f"Saved plots under {results_dir}") + + # Inform user where CSVs were saved + typer.echo(f"Saved CSV results under {results_dir}") + + +if __name__ == "__main__": + app() diff --git a/examples/utils.py b/examples/utils.py new file mode 100644 index 00000000..ef8c3448 --- /dev/null +++ b/examples/utils.py @@ -0,0 +1,106 @@ +"""Utilities shared by the examples package. + +This module centralises small utilities used across the examples so they +don't have to duplicate simple data containers like SimulationResult. +""" +from __future__ import annotations + +from dataclasses import dataclass +from typing import Optional + +import polars as pl +from pathlib import Path +from typing import Sequence + +import matplotlib.pyplot as plt +import seaborn as sns + + + +@dataclass +class SimulationResult: + """Container for example simulation outputs. + + The dataclass is intentionally permissive: some backends only provide + `metrics`, while others also return `agent_metrics`. + """ + + model_metrics: pl.DataFrame + agent_metrics: Optional[pl.DataFrame] = None + + +def plot_model_metrics( + metrics: pl.DataFrame, + output_dir: Path, + stem: str, + title: str | None = None, + figsize: tuple[int, int] | None = None, +) -> None: + """Plot time-series metrics from a polars DataFrame. + + This helper auto-detects all columns except the `step` column and + plots them as separate series. It writes two theme variants + (light/dark) as PNG files under ``output_dir`` with the provided stem. + """ + if metrics.is_empty(): + return + + if "step" not in metrics.columns: + metrics = metrics.with_row_count("step") + + # melt all non-step columns into long form + value_cols: Sequence[str] = [c for c in metrics.columns if c != "step"] + if not value_cols: + return + long = metrics.select(["step", *value_cols]).melt( + id_vars="step", variable_name="metric", value_name="value" + ) + + for theme, style in {"light": "whitegrid", "dark": "darkgrid"}.items(): + sns.set_theme(style=style) + fig, ax = plt.subplots(figsize=figsize or (8, 5)) + sns.lineplot(data=long.to_pandas(), x="step", y="value", hue="metric", ax=ax) + ax.set_title(title or "Metrics") + ax.set_xlabel("Step") + ax.set_ylabel("Value") + fig.tight_layout() + filename = output_dir / f"{stem}_{theme}.png" + fig.savefig(filename, dpi=300) + plt.close(fig) + + +def plot_agent_metrics( + agent_metrics: pl.DataFrame, output_dir: Path, stem: str, figsize: tuple[int, int] | None = None +) -> None: + """Plot agent-level metrics (if any) and write theme variants to disk. + + The function will attempt to preserve common id vars like `step`, + `seed` and `batch` if present; otherwise it uses the first column as + the id variable when melting. + """ + if agent_metrics is None or agent_metrics.is_empty(): + return + + # prefer common id_vars if available + preferred = ["step", "seed", "batch"] + id_vars = [c for c in preferred if c in agent_metrics.columns] + if not id_vars: + # fall back to using the first column as id + id_vars = [agent_metrics.columns[0]] + + melted = agent_metrics.melt(id_vars=id_vars, variable_name="metric", value_name="value") + + for theme, style in {"light": "whitegrid", "dark": "darkgrid"}.items(): + sns.set_theme(style=style) + fig, ax = plt.subplots(figsize=figsize or (10, 6)) + sns.lineplot(data=melted.to_pandas(), x=id_vars[0], y="value", hue="metric", ax=ax) + ax.set_title("Agent metrics") + ax.set_xlabel(id_vars[0].capitalize()) + ax.set_ylabel("Value") + fig.tight_layout() + filename = output_dir / f"{stem}_agents_{theme}.png" + fig.savefig(filename, dpi=300) + plt.close(fig) + + +__all__ = ["SimulationResult", "plot_model_metrics", "plot_agent_metrics"] From db4c32d03fe0bc9000020aabac44738b16a67a06 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 26 Sep 2025 10:05:49 +0200 Subject: [PATCH 242/329] feat: add plotting module for visualizing model and agent metrics; remove deprecated utils module --- examples/boltzmann_wealth/backend_frames.py | 27 +- examples/plotting.py | 281 ++++++++++++++++++++ examples/utils.py | 106 -------- 3 files changed, 299 insertions(+), 115 deletions(-) create mode 100644 examples/plotting.py delete mode 100644 examples/utils.py diff --git a/examples/boltzmann_wealth/backend_frames.py b/examples/boltzmann_wealth/backend_frames.py index 67b803e6..efcad516 100644 --- a/examples/boltzmann_wealth/backend_frames.py +++ b/examples/boltzmann_wealth/backend_frames.py @@ -12,9 +12,8 @@ from time import perf_counter from mesa_frames import AgentSet, DataCollector, Model - - -from examples.utils import SimulationResult, plot_model_metrics +from examples.utils import SimulationResult +from examples.plotting import plot_model_metrics # Note: by default we create a timestamped results directory under `results/`. @@ -55,7 +54,9 @@ def step(self) -> None: return # Use the model RNG to seed Polars sampling so results are reproducible recipients = self.df.sample( - n=len(self.active_agents), with_replacement=True, seed=self.random.integers(np.iinfo(np.int32).max) + n=len(self.active_agents), + with_replacement=True, + seed=self.random.integers(np.iinfo(np.int32).max), ) # donors lose one unit self["active", "wealth"] -= 1 @@ -102,9 +103,9 @@ def simulate( return SimulationResult(datacollector=model.datacollector) - app = typer.Typer(add_completion=False) + @app.command() def run( agents: Annotated[int, typer.Option(help="Number of agents to simulate.")] = 5000, @@ -112,17 +113,25 @@ def run( seed: Annotated[int | None, typer.Option(help="Optional RNG seed.")] = None, plot: Annotated[bool, typer.Option(help="Render Seaborn plots.")] = True, save_results: Annotated[bool, typer.Option(help="Persist metrics as CSV.")] = True, - results_dir: Annotated[Path | None, typer.Option(help="Directory to write CSV results and plots into. If omitted a timestamped subdir under `results/` is used.")] = None, + results_dir: Annotated[ + Path | None, + typer.Option( + help="Directory to write CSV results and plots into. If omitted a timestamped subdir under `results/` is used." + ), + ] = None, ) -> None: - typer.echo(f"Running Boltzmann wealth model (mesa-frames) with {agents} agents for {steps} steps") + typer.echo( + f"Running Boltzmann wealth model (mesa-frames) with {agents} agents for {steps} steps" + ) timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") if results_dir is None: - results_dir = (Path(__file__).resolve().parent / "results" / timestamp).resolve() + results_dir = ( + Path(__file__).resolve().parent / "results" / timestamp + ).resolve() results_dir.mkdir(parents=True, exist_ok=True) start_time = perf_counter() result = simulate(agents=agents, steps=steps, seed=seed, results_dir=results_dir) - typer.echo(f"Simulation complete in {perf_counter() - start_time:.2f} seconds") model_metrics = result.datacollector.data["model"].select("step", "gini") diff --git a/examples/plotting.py b/examples/plotting.py new file mode 100644 index 00000000..5313dcf8 --- /dev/null +++ b/examples/plotting.py @@ -0,0 +1,281 @@ +# examples/plotting.py +from __future__ import annotations + +from pathlib import Path +from typing import Sequence +import re + +import polars as pl +import seaborn as sns +import matplotlib.pyplot as plt +from matplotlib.ticker import FormatStrFormatter +from matplotlib.figure import Figure +from matplotlib.axes import Axes + +# ----------------------------- Shared theme ---------------------------------- + +_THEMES = { + "light": dict( + style="whitegrid", + rc={ + "axes.spines.top": False, + "axes.spines.right": False, + }, + ), + "dark": dict( + style="whitegrid", + rc={ + # real dark background + readable foreground + "figure.facecolor": "#0b1021", + "axes.facecolor": "#0b1021", + "axes.edgecolor": "#d6d6d7", + "axes.labelcolor": "#e8e8ea", + "text.color": "#e8e8ea", + "xtick.color": "#c9c9cb", + "ytick.color": "#c9c9cb", + "grid.color": "#2a2f4a", + "grid.alpha": 0.35, + "axes.spines.top": False, + "axes.spines.right":False, + "legend.facecolor": "#121734", + "legend.edgecolor": "#3b3f5a", + }, + ), +} + + +def _shorten_seed(text: str | None) -> str | None: + """Turn '... seed=1234567890123' into '... seed=12345678…' if present.""" + if not text: + return text + m = re.search(r"seed=([^;,\s]+)", text) + if not m: + return text + raw = m.group(1) + short = (raw[:8] + "…") if len(raw) > 10 else raw + return re.sub(r"seed=[^;,\s]+", f"seed={short}", text) + + +def _apply_titles(fig: Figure, ax: Axes, title: str, subtitle: str | None) -> None: + """Consistent title placement: figure-level title + small italic subtitle.""" + fig.suptitle(title, fontsize=18, y=0.98) + ax.set_title(_shorten_seed(subtitle) or "", fontsize=12, fontstyle="italic", pad=4) + + +def _finalize_and_save(fig: Figure, output_dir: Path, stem: str, theme: str) -> None: + """Tight layout with space for suptitle, export PNG + (optional) SVG.""" + output_dir.mkdir(parents=True, exist_ok=True) + fig.tight_layout(rect=[0, 0, 1, 0.94]) + png = output_dir / f"{stem}_{theme}.png" + fig.savefig(png, dpi=300) + try: + fig.savefig(output_dir / f"{stem}_{theme}.svg", bbox_inches="tight") + except Exception: + pass # SVG is a nice-to-have + plt.close(fig) + + +# -------------------------- Public: model metrics ---------------------------- + +def plot_model_metrics( + metrics: pl.DataFrame, + output_dir: Path, + stem: str, + title: str, + *, + subtitle: str = "", + figsize: tuple[int, int] | None = None, + agents: int | None = None, + steps: int | None = None, +) -> None: + """ + Plot time-series metrics from a Polars DataFrame and export light/dark PNG/SVG. + + - Auto-detects `step` or adds one if missing. + - Melts all non-`step` columns into long form. + - If there's a single metric (e.g., 'gini'), removes legend and uses a + descriptive y-axis label (e.g., 'Gini coefficient'). + - Optional `agents` and `steps` will be appended to the suptitle as + "(N=, T=)"; if `steps` is omitted it will be inferred + from the `step` column when available. + """ + if metrics.is_empty(): + return + + if "step" not in metrics.columns: + metrics = metrics.with_row_index("step") + + # If steps not provided, try to infer from the data (max step + 1). Keep it None if we can't determine it. + if steps is None: + try: + steps = int(metrics.select(pl.col("step").max()).item()) + 1 + except Exception: + steps = None + + value_cols: Sequence[str] = [c for c in metrics.columns if c != "step"] + if not value_cols: + return + + long = ( + metrics.select(["step", *value_cols]) + .unpivot(index="step", on=value_cols, variable_name="metric", value_name="value") + .to_pandas() + ) + + # Compose informative title with optional (N, T) + if agents is not None and steps is not None: + full_title = f"{title} (N={agents}, T={steps})" + elif agents is not None: + full_title = f"{title} (N={agents})" + elif steps is not None: + full_title = f"{title} (T={steps})" + else: + full_title = title + + for theme, cfg in _THEMES.items(): + sns.set_theme(**cfg) + sns.set_context("talk") + fig, ax = plt.subplots(figsize=figsize or (10, 6)) + + sns.lineplot(data=long, x="step", y="value", hue="metric", linewidth=2, ax=ax) + + _apply_titles(fig, ax, full_title, subtitle) + + ax.set_xlabel("Step") + unique_metrics = long["metric"].unique() + + if len(unique_metrics) == 1: + name = unique_metrics[0] + ax.set_ylabel(name.capitalize()) + leg = ax.get_legend() + if leg is not None: + leg.remove() + vals = long.loc[long["metric"] == name, "value"] + if not vals.empty: + vmin, vmax = float(vals.min()), float(vals.max()) + pad = max(0.005, (vmax - vmin) * 0.05) + ax.set_ylim(vmin - pad, vmax + pad) + else: + ax.set_ylabel("Value") + leg = ax.get_legend() + if theme == "dark" and leg is not None: + leg.set_title(None) + leg.get_frame().set_alpha(0.8) + + ax.yaxis.set_major_formatter(FormatStrFormatter("%.3f")) + ax.margins(x=0.01) + + _finalize_and_save(fig, output_dir, stem, theme) + + +# -------------------------- Public: agent metrics ---------------------------- + +def plot_agent_metrics( + agent_metrics: pl.DataFrame, + output_dir: Path, + stem: str, + *, + title: str = "Agent metrics", + subtitle: str = "", + figsize: tuple[int, int] | None = None, +) -> None: + """ + Plot agent-level metrics (multi-series) and export light/dark PNG/SVG. + + - Preserves common id vars if present: `step`, `seed`, `batch`. + - Uses the first column as id if none of the preferred ids exist. + """ + if agent_metrics is None or agent_metrics.is_empty(): + return + + preferred = ["step", "seed", "batch"] + id_vars = [c for c in preferred if c in agent_metrics.columns] or [agent_metrics.columns[0]] + + # Determine which columns to unpivot (all columns except the id vars). + value_cols = [c for c in agent_metrics.columns if c not in id_vars] + if not value_cols: + return + + melted = ( + agent_metrics.unpivot( + index=id_vars, on=value_cols, variable_name="metric", value_name="value" + ) + .to_pandas() + ) + + xcol = id_vars[0] + + for theme, cfg in _THEMES.items(): + sns.set_theme(**cfg) + sns.set_context("talk") + fig, ax = plt.subplots(figsize=figsize or (10, 6)) + + sns.lineplot(data=melted, x=xcol, y="value", hue="metric", linewidth=1.8, ax=ax) + + _apply_titles(fig, ax, title, subtitle) + ax.set_xlabel(xcol.capitalize()) + ax.set_ylabel("Value") + + if theme == "dark": + leg = ax.get_legend() + if leg is not None: + leg.set_title(None) + leg.get_frame().set_alpha(0.8) + + _finalize_and_save(fig, output_dir, f"{stem}_agents", theme) + + +# -------------------------- Public: performance ------------------------------ + +def plot_performance( + df: pl.DataFrame, + output_dir: Path, + stem: str, + *, + title: str = "Runtime vs agents", + subtitle: str = "", + figsize: tuple[int, int] | None = None, +) -> None: + """ + Plot backend performance (runtime vs agents) with mean±sd error bars. + Expected columns: `agents`, `runtime_seconds`, `backend`. + """ + if df.is_empty(): + return + + pdf = df.to_pandas() + + for theme, cfg in _THEMES.items(): + sns.set_theme(**cfg) + sns.set_context("talk") + fig, ax = plt.subplots(figsize=figsize or (10, 6)) + + sns.lineplot( + data=pdf, + x="agents", + y="runtime_seconds", + hue="backend", + estimator="mean", + errorbar="sd", + marker="o", + ax=ax, + ) + + _apply_titles(fig, ax, title, subtitle) + ax.set_xlabel("Agents") + ax.set_ylabel("Runtime (seconds)") + + if theme == "dark": + leg = ax.get_legend() + if leg is not None: + leg.set_title(None) + leg.get_frame().set_alpha(0.8) + + _finalize_and_save(fig, output_dir, stem, theme) + + +__all__ = [ + "plot_model_metrics", + "plot_agent_metrics", + "plot_performance", +] \ No newline at end of file diff --git a/examples/utils.py b/examples/utils.py deleted file mode 100644 index ef8c3448..00000000 --- a/examples/utils.py +++ /dev/null @@ -1,106 +0,0 @@ -"""Utilities shared by the examples package. - -This module centralises small utilities used across the examples so they -don't have to duplicate simple data containers like SimulationResult. -""" -from __future__ import annotations - -from dataclasses import dataclass -from typing import Optional - -import polars as pl -from pathlib import Path -from typing import Sequence - -import matplotlib.pyplot as plt -import seaborn as sns - - - -@dataclass -class SimulationResult: - """Container for example simulation outputs. - - The dataclass is intentionally permissive: some backends only provide - `metrics`, while others also return `agent_metrics`. - """ - - model_metrics: pl.DataFrame - agent_metrics: Optional[pl.DataFrame] = None - - -def plot_model_metrics( - metrics: pl.DataFrame, - output_dir: Path, - stem: str, - title: str | None = None, - figsize: tuple[int, int] | None = None, -) -> None: - """Plot time-series metrics from a polars DataFrame. - - This helper auto-detects all columns except the `step` column and - plots them as separate series. It writes two theme variants - (light/dark) as PNG files under ``output_dir`` with the provided stem. - """ - if metrics.is_empty(): - return - - if "step" not in metrics.columns: - metrics = metrics.with_row_count("step") - - # melt all non-step columns into long form - value_cols: Sequence[str] = [c for c in metrics.columns if c != "step"] - if not value_cols: - return - long = metrics.select(["step", *value_cols]).melt( - id_vars="step", variable_name="metric", value_name="value" - ) - - for theme, style in {"light": "whitegrid", "dark": "darkgrid"}.items(): - sns.set_theme(style=style) - fig, ax = plt.subplots(figsize=figsize or (8, 5)) - sns.lineplot(data=long.to_pandas(), x="step", y="value", hue="metric", ax=ax) - ax.set_title(title or "Metrics") - ax.set_xlabel("Step") - ax.set_ylabel("Value") - fig.tight_layout() - filename = output_dir / f"{stem}_{theme}.png" - fig.savefig(filename, dpi=300) - plt.close(fig) - - -def plot_agent_metrics( - agent_metrics: pl.DataFrame, output_dir: Path, stem: str, figsize: tuple[int, int] | None = None -) -> None: - """Plot agent-level metrics (if any) and write theme variants to disk. - - The function will attempt to preserve common id vars like `step`, - `seed` and `batch` if present; otherwise it uses the first column as - the id variable when melting. - """ - if agent_metrics is None or agent_metrics.is_empty(): - return - - # prefer common id_vars if available - preferred = ["step", "seed", "batch"] - id_vars = [c for c in preferred if c in agent_metrics.columns] - if not id_vars: - # fall back to using the first column as id - id_vars = [agent_metrics.columns[0]] - - melted = agent_metrics.melt(id_vars=id_vars, variable_name="metric", value_name="value") - - for theme, style in {"light": "whitegrid", "dark": "darkgrid"}.items(): - sns.set_theme(style=style) - fig, ax = plt.subplots(figsize=figsize or (10, 6)) - sns.lineplot(data=melted.to_pandas(), x=id_vars[0], y="value", hue="metric", ax=ax) - ax.set_title("Agent metrics") - ax.set_xlabel(id_vars[0].capitalize()) - ax.set_ylabel("Value") - fig.tight_layout() - filename = output_dir / f"{stem}_agents_{theme}.png" - fig.savefig(filename, dpi=300) - plt.close(fig) - - -__all__ = ["SimulationResult", "plot_model_metrics", "plot_agent_metrics"] From 98278b8db3bd79d63eae4c6439d999e60b989039 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 26 Sep 2025 12:19:07 +0200 Subject: [PATCH 243/329] feat: implement Mesa backend for Boltzmann wealth model; add FramesSimulationResult class for simulation outputs --- examples/boltzmann_wealth/backend_frames.py | 8 +- examples/boltzmann_wealth/backend_mesa.py | 178 ++++++++++++++++++++ examples/utils.py | 23 +++ 3 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 examples/boltzmann_wealth/backend_mesa.py create mode 100644 examples/utils.py diff --git a/examples/boltzmann_wealth/backend_frames.py b/examples/boltzmann_wealth/backend_frames.py index efcad516..23efac92 100644 --- a/examples/boltzmann_wealth/backend_frames.py +++ b/examples/boltzmann_wealth/backend_frames.py @@ -12,7 +12,7 @@ from time import perf_counter from mesa_frames import AgentSet, DataCollector, Model -from examples.utils import SimulationResult +from examples.utils import FramesSimulationResult from examples.plotting import plot_model_metrics @@ -96,11 +96,11 @@ def simulate( steps: int, seed: int | None = None, results_dir: Path | None = None, -) -> SimulationResult: +) -> FramesSimulationResult: model = MoneyModel(agents, seed=seed, results_dir=results_dir) model.run(steps) # collect data from datacollector into memory first - return SimulationResult(datacollector=model.datacollector) + return FramesSimulationResult(datacollector=model.datacollector) app = typer.Typer(add_completion=False) @@ -136,7 +136,7 @@ def run( model_metrics = result.datacollector.data["model"].select("step", "gini") - typer.echo(f"Metrics in the final 5 steps: {model_metrics[-5:]}") + typer.echo(f"Metrics in the final 5 steps: {model_metrics.tail(5)}") if save_results: result.datacollector.flush() diff --git a/examples/boltzmann_wealth/backend_mesa.py b/examples/boltzmann_wealth/backend_mesa.py new file mode 100644 index 00000000..8b6e3162 --- /dev/null +++ b/examples/boltzmann_wealth/backend_mesa.py @@ -0,0 +1,178 @@ +"""Mesa implementation of the Boltzmann wealth model with Typer CLI.""" + +from __future__ import annotations + +from datetime import datetime, timezone +from pathlib import Path +from typing import Iterable, Annotated +import pandas as pd + +import matplotlib.pyplot as plt +import mesa +from mesa.datacollection import DataCollector +import numpy as np +import polars as pl +import seaborn as sns +import typer +from time import perf_counter + +from examples.utils import MesaSimulationResult +from examples.plotting import plot_model_metrics + + +def gini(values: Iterable[float]) -> float: + """Compute the Gini coefficient from an iterable of wealth values.""" + array = np.fromiter(values, dtype=float) + if array.size == 0: + return float("nan") + if np.allclose(array, 0.0): + return 0.0 + if np.allclose(array, array[0]): + return 0.0 + sorted_vals = np.sort(array) + n = sorted_vals.size + cumulative = np.cumsum(sorted_vals) + total = cumulative[-1] + if total == 0: + return 0.0 + index = np.arange(1, n + 1, dtype=float) + return float((2.0 * np.dot(index, sorted_vals) / (n * total)) - (n + 1) / n) + + +class MoneyAgent(mesa.Agent): + """Agent that passes one unit of wealth to a random neighbour.""" + + def __init__(self, model: "MoneyModel") -> None: + super().__init__(model) + self.wealth = 1 + + def step(self) -> None: + if self.wealth <= 0: + return + other = self.random.choice(self.model.agent_list) + if other is None: + return + other.wealth += 1 + self.wealth -= 1 + + +class MoneyModel(mesa.Model): + """Mesa backend that mirrors the mesa-frames Boltzmann wealth example.""" + + def __init__(self, agents: int, *, seed: int | None = None) -> None: + super().__init__() + if seed is None: + seed = self.random.randint(0, np.iinfo(np.int32).max) + self.reset_randomizer(seed) + self.agent_list: list[MoneyAgent] = [] + for _ in range(agents): + # NOTE: storing agents in a Python list keeps iteration fast for benchmarks. + agent = MoneyAgent(self) + self.agent_list.append(agent) + self.datacollector = DataCollector( + model_reporters={ + "gini": lambda m: gini(a.wealth for a in m.agent_list), + "seed": lambda m: seed, + } + ) + self.datacollector.collect(self) + + def step(self) -> None: + self.random.shuffle(self.agent_list) + for agent in self.agent_list: + agent.step() + self.datacollector.collect(self) + + def run(self, steps: int) -> None: + for _ in range(steps): + self.step() + + +def simulate(agents: int, steps: int, seed: int | None = None) -> MesaSimulationResult: + """Run the Mesa Boltzmann wealth model.""" + model = MoneyModel(agents, seed=seed) + model.run(steps) + + return MesaSimulationResult(datacollector=model.datacollector) + + +app = typer.Typer(add_completion=False) + +@app.command() +def run( + agents: Annotated[int, typer.Option(help="Number of agents to simulate.")] = 5000, + steps: Annotated[int, typer.Option(help="Number of model steps to run.")] = 100, + seed: Annotated[int | None, typer.Option(help="Optional RNG seed.")] = None, + plot: Annotated[bool, typer.Option(help="Render plots.")] = True, + save_results: Annotated[ + bool, + typer.Option(help="Persist metrics as CSV."), + ] = True, + results_dir: Annotated[ + Path | None, + typer.Option( + help=( + "Directory to write CSV results and plots into. If omitted a " + "timestamped subdir under `results/` is used." + ) + ), + ] = None, +) -> None: + """Execute the Mesa Boltzmann wealth simulation.""" + + typer.echo( + f"Running Boltzmann wealth model (mesa) with {agents} agents for {steps} steps" + ) + + # Resolve output folder + timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") + if results_dir is None: + results_dir = (Path(__file__).resolve().parent / "results" / timestamp).resolve() + results_dir.mkdir(parents=True, exist_ok=True) + + start_time = perf_counter() + # Run simulation (Mesa‑idiomatic): we only use DataCollector's public API + result = simulate(agents=agents, steps=steps, seed=seed) + typer.echo(f"Simulation completed in {perf_counter() - start_time:.3f} seconds") + dc = result.datacollector + + # ---- Extract metrics (no helper, no monkey‑patch): + # DataCollector returns a pandas DataFrame with the index as the step. + model_pd = dc.get_model_vars_dataframe() + model_pd = model_pd.reset_index() + # The first column is the step index; normalize name to "step". + model_pd = model_pd.rename(columns={model_pd.columns[0]: "step"}) + seed = model_pd["seed"].iloc[0] + model_pd = model_pd[['step', 'gini']] + + # Show a short tail in console for quick inspection + tail_str = model_pd.tail(5).to_string(index=False) + typer.echo(f"Metrics in the final 5 steps:\n{tail_str}") + + + # ---- Save CSV (same filename/layout as frames backend expects) + if save_results: + csv_path = results_dir / "model.csv" + model_pd.to_csv(csv_path, index=False) + + # ---- Plot (convert to Polars to reuse the shared plotting helper) + if plot and not model_pd.empty: + model_pl = pl.from_pandas(model_pd) + stem = f"gini_{timestamp}" + plot_model_metrics( + model_pl, + results_dir, + stem, + title="Boltzmann wealth — Gini", + subtitle=f"mesa backend; seed={seed}", + agents=agents, + steps=steps, + ) + typer.echo(f"Saved plots under {results_dir}") + + if save_results: + typer.echo(f"Saved CSV results under {results_dir}") + + +if __name__ == "__main__": + app() diff --git a/examples/utils.py b/examples/utils.py new file mode 100644 index 00000000..dbd165b4 --- /dev/null +++ b/examples/utils.py @@ -0,0 +1,23 @@ +from dataclasses import dataclass +import mesa_frames +import mesa + +@dataclass +class FramesSimulationResult: + """Container for example simulation outputs. + + The dataclass is intentionally permissive: some backends only provide + `metrics`, while others also return `agent_metrics`. + """ + + datacollector: mesa_frames.DataCollector + +@dataclass +class MesaSimulationResult: + """Container for example simulation outputs. + + The dataclass is intentionally permissive: some backends only provide + `metrics`, while others also return `agent_metrics`. + """ + + datacollector: mesa.DataCollector \ No newline at end of file From 2c625e147bbe0e8fcac842e1b875f9edf267389b Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 26 Sep 2025 12:22:16 +0200 Subject: [PATCH 244/329] fix: update typer dependency to allow any version >=0.9.0; remove perfplot from docs dependencies --- pyproject.toml | 3 +-- uv.lock | 32 -------------------------------- 2 files changed, 1 insertion(+), 34 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c130f9e5..addcc239 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,7 +64,7 @@ test = [ docs = [ { include-group = "typechecking" }, - "typer[all]>=0.9.0", + "typer>=0.9.0", "mkdocs-material>=9.6.14", "mkdocs-jupyter>=0.25.1", "mkdocs-git-revision-date-localized-plugin>=1.4.7", @@ -77,7 +77,6 @@ docs = [ "sphinx-copybutton>=0.5.2", "sphinx-design>=0.6.1", "autodocsumm>=0.2.14", - "perfplot>=0.10.2", "seaborn>=0.13.2", "sphinx-autobuild>=2025.8.25", "mesa>=3.2.0", diff --git a/uv.lock b/uv.lock index 8095193c..cc55da48 100644 --- a/uv.lock +++ b/uv.lock @@ -1161,19 +1161,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/8f/8e/9ad090d3553c280a8060fbf6e24dc1c0c29704ee7d1c372f0c174aa59285/matplotlib_inline-0.1.7-py3-none-any.whl", hash = "sha256:df192d39a4ff8f21b1895d72e6a13f5fcc5099f00fa84384e0ea28c2cc0653ca", size = 9899, upload-time = "2024-04-15T13:44:43.265Z" }, ] -[[package]] -name = "matplotx" -version = "0.3.10" -source = { registry = "https://pypi.org/simple" } -dependencies = [ - { name = "matplotlib" }, - { name = "numpy" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/c3/01/0e6938bb717fa7722d6d81336c62de71b815ce73e382aa1873a1e68ccc93/matplotx-0.3.10.tar.gz", hash = "sha256:b6926ce5274cf5da966cb46b90a8c7fefb761478c6c85c8f7ed3ee8ec90e86e5", size = 24041, upload-time = "2022-08-22T14:22:56.374Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/95/ef/e8a30503ae0c26681a9610c7f0be58646bea8119b98cc65c47661abc27a3/matplotx-0.3.10-py3-none-any.whl", hash = "sha256:4d7adafdb001c771d66d9362bb8ca99fcaed15319259223a714f36793dfabbb8", size = 25099, upload-time = "2022-08-22T14:22:54.733Z" }, -] - [[package]] name = "mdit-py-plugins" version = "0.5.0" @@ -1243,7 +1230,6 @@ dev = [ { name = "mkdocs-minify-plugin" }, { name = "numba" }, { name = "numpydoc" }, - { name = "perfplot" }, { name = "pre-commit" }, { name = "pydata-sphinx-theme" }, { name = "pytest" }, @@ -1268,7 +1254,6 @@ docs = [ { name = "mkdocs-material" }, { name = "mkdocs-minify-plugin" }, { name = "numpydoc" }, - { name = "perfplot" }, { name = "pydata-sphinx-theme" }, { name = "seaborn" }, { name = "sphinx" }, @@ -1309,7 +1294,6 @@ dev = [ { name = "mkdocs-minify-plugin", specifier = ">=0.8.0" }, { name = "numba", specifier = ">=0.60.0" }, { name = "numpydoc", specifier = ">=1.8.0" }, - { name = "perfplot", specifier = ">=0.10.2" }, { name = "pre-commit", specifier = ">=4.2.0" }, { name = "pydata-sphinx-theme", specifier = ">=0.16.1" }, { name = "pytest", specifier = ">=8.3.5" }, @@ -1334,7 +1318,6 @@ docs = [ { name = "mkdocs-material", specifier = ">=9.6.14" }, { name = "mkdocs-minify-plugin", specifier = ">=0.8.0" }, { name = "numpydoc", specifier = ">=1.8.0" }, - { name = "perfplot", specifier = ">=0.10.2" }, { name = "pydata-sphinx-theme", specifier = ">=0.16.1" }, { name = "seaborn", specifier = ">=0.13.2" }, { name = "sphinx", specifier = ">=7.4.7" }, @@ -1736,21 +1719,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/cc/20/ff623b09d963f88bfde16306a54e12ee5ea43e9b597108672ff3a408aad6/pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08", size = 31191, upload-time = "2023-12-10T22:30:43.14Z" }, ] -[[package]] -name = "perfplot" -version = "0.10.2" -source = { registry = "https://pypi.org/simple" } -dependencies = [ - { name = "matplotlib" }, - { name = "matplotx" }, - { name = "numpy" }, - { name = "rich" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/97/41/51d8b9caa150a050de16a229f627e4b37515dbff0075259e4e75aff7218b/perfplot-0.10.2.tar.gz", hash = "sha256:d76daa72334564b5c8825663f24d15db55ea33e938b34595a146e5e44ed87e41", size = 25044, upload-time = "2022-03-03T15:56:37.392Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/20/85/ffaf2c1f92d17916c089a5c860d23b3117398f19f467fd1de1026d03aebc/perfplot-0.10.2-py3-none-any.whl", hash = "sha256:545ce0f7f22509ad00092d79a794cdc6e9805383e6cedab2bfed3519a7ef4e19", size = 21198, upload-time = "2022-03-03T15:56:35.388Z" }, -] - [[package]] name = "pexpect" version = "4.9.0" From 37b2aec9771e903468a32a54bd8c7389cfa2ea41 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 26 Sep 2025 12:24:03 +0200 Subject: [PATCH 245/329] refactor: remove Sugarscape IG example files and reorganize backend structure --- examples/sugarscape_ig/__init__.py | 0 .../sugarscape_ig/backend_mesa/__init__.py | 1 + .../sugarscape_ig/performance_comparison.py | 224 ---------- examples/sugarscape_ig/ss_mesa/__init__.py | 0 examples/sugarscape_ig/ss_mesa/agents.py | 83 ---- examples/sugarscape_ig/ss_mesa/model.py | 77 ---- examples/sugarscape_ig/ss_polars/__init__.py | 0 examples/sugarscape_ig/ss_polars/agents.py | 406 ------------------ examples/sugarscape_ig/ss_polars/model.py | 57 --- 9 files changed, 1 insertion(+), 847 deletions(-) delete mode 100644 examples/sugarscape_ig/__init__.py create mode 100644 examples/sugarscape_ig/backend_mesa/__init__.py delete mode 100644 examples/sugarscape_ig/performance_comparison.py delete mode 100644 examples/sugarscape_ig/ss_mesa/__init__.py delete mode 100644 examples/sugarscape_ig/ss_mesa/agents.py delete mode 100644 examples/sugarscape_ig/ss_mesa/model.py delete mode 100644 examples/sugarscape_ig/ss_polars/__init__.py delete mode 100644 examples/sugarscape_ig/ss_polars/agents.py delete mode 100644 examples/sugarscape_ig/ss_polars/model.py diff --git a/examples/sugarscape_ig/__init__.py b/examples/sugarscape_ig/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/sugarscape_ig/backend_mesa/__init__.py b/examples/sugarscape_ig/backend_mesa/__init__.py new file mode 100644 index 00000000..463099c0 --- /dev/null +++ b/examples/sugarscape_ig/backend_mesa/__init__.py @@ -0,0 +1 @@ +"""Mesa backend package for Sugarscape IG examples.""" diff --git a/examples/sugarscape_ig/performance_comparison.py b/examples/sugarscape_ig/performance_comparison.py deleted file mode 100644 index d8d2f196..00000000 --- a/examples/sugarscape_ig/performance_comparison.py +++ /dev/null @@ -1,224 +0,0 @@ -import math - -import matplotlib.pyplot as plt -import numpy as np -import perfplot -import polars as pl -import seaborn as sns -from polars.testing import assert_frame_equal -from ss_mesa.model import SugarscapeMesa -from ss_polars.agents import ( - AntPolarsLoopDF, - AntPolarsLoopNoVec, - AntPolarsNumbaCPU, - AntPolarsNumbaGPU, - AntPolarsNumbaParallel, -) -from ss_polars.model import SugarscapePolars -from collections.abc import Callable - - -class SugarScapeSetup: - def __init__(self, n: int): - if n >= 10**6: - density = 0.17 # FLAME2-GPU - else: - density = 0.04 # mesa - self.n = n - self.seed = 42 - dimension = math.ceil(math.sqrt(n / density)) - random_gen = np.random.default_rng(self.seed) - self.sugar_grid = random_gen.integers(0, 4, (dimension, dimension)) - self.initial_sugar = random_gen.integers(6, 25, n) - self.metabolism = random_gen.integers(2, 4, n) - self.vision = random_gen.integers(1, 6, n) - self.initial_positions = pl.DataFrame( - schema={"dim_0": pl.Int64, "dim_1": pl.Int64} - ) - while self.initial_positions.shape[0] < n: - initial_pos_0 = random_gen.integers( - 0, dimension, n - self.initial_positions.shape[0] - ) - initial_pos_1 = random_gen.integers( - 0, dimension, n - self.initial_positions.shape[0] - ) - self.initial_positions = self.initial_positions.vstack( - pl.DataFrame( - { - "dim_0": initial_pos_0, - "dim_1": initial_pos_1, - } - ) - ).unique(maintain_order=True) - return - - -def mesa_implementation(setup: SugarScapeSetup): - model = SugarscapeMesa( - setup.n, - setup.sugar_grid, - setup.initial_sugar, - setup.metabolism, - setup.vision, - setup.initial_positions, - setup.seed, - ) - model.run_model(100) - return model - - -def mesa_frames_polars_loop_DF(setup: SugarScapeSetup): - model = SugarscapePolars( - AntPolarsLoopDF, - setup.n, - setup.sugar_grid, - setup.initial_sugar, - setup.metabolism, - setup.vision, - setup.initial_positions, - setup.seed, - ) - model.run_model(100) - return model - - -def mesa_frames_polars_loop_no_vec(setup: SugarScapeSetup): - model = SugarscapePolars( - AntPolarsLoopNoVec, - setup.n, - setup.sugar_grid, - setup.initial_sugar, - setup.metabolism, - setup.vision, - setup.initial_positions, - setup.seed, - ) - model.run_model(100) - return model - - -def mesa_frames_polars_numba_cpu(setup: SugarScapeSetup): - model = SugarscapePolars( - AntPolarsNumbaCPU, - setup.n, - setup.sugar_grid, - setup.initial_sugar, - setup.metabolism, - setup.vision, - setup.initial_positions, - setup.seed, - ) - model.run_model(100) - return model - - -def mesa_frames_polars_numba_gpu(setup: SugarScapeSetup): - model = SugarscapePolars( - AntPolarsNumbaGPU, - setup.n, - setup.sugar_grid, - setup.initial_sugar, - setup.metabolism, - setup.vision, - setup.initial_positions, - setup.seed, - ) - model.run_model(100) - return model - - -def mesa_frames_polars_numba_parallel(setup: SugarScapeSetup): - model = SugarscapePolars( - AntPolarsNumbaParallel, - setup.n, - setup.sugar_grid, - setup.initial_sugar, - setup.metabolism, - setup.vision, - setup.initial_positions, - setup.seed, - ) - model.run_model(100) - return model - - -def plot_and_print_benchmark( - labels: list[str], - kernels: list[Callable], - n_range: list[int], - title: str, - image_path: str, - equality_check: Callable | None = None, -): - out = perfplot.bench( - setup=SugarScapeSetup, - kernels=kernels, - labels=labels, - n_range=n_range, - xlabel="Number of agents", - equality_check=equality_check, - title=title, - ) - plt.ylabel("Execution time (s)") - out.save(image_path) - print("\nExecution times:") - for i, label in enumerate(labels): - print(f"---------------\n{label}:") - for n, t in zip(out.n_range, out.timings_s[i]): - print(f" Number of agents: {n}, Time: {t:.2f} seconds") - print("---------------") - - -def polars_equality_check(a: SugarscapePolars, b: SugarscapePolars): - assert_frame_equal(a.space.agents, b.space.agents, check_row_order=False) - assert_frame_equal(a.space.cells, b.space.cells, check_row_order=False) - return True - - -def main(): - # Mesa comparison - sns.set_theme(style="whitegrid") - labels_0 = [ - "mesa-frames (pl numba parallel)", - "mesa", - ] - kernels_0 = [ - mesa_frames_polars_numba_parallel, - mesa_implementation, - ] - n_range_0 = [k for k in range(10**5, 5 * 10**5 + 2, 10**5)] - title_0 = "100 steps of the SugarScape IG model:\n" + " vs ".join(labels_0) - image_path_0 = "mesa_comparison.png" - plot_and_print_benchmark(labels_0, kernels_0, n_range_0, title_0, image_path_0) - - # mesa-frames comparison - labels_1 = [ - "mesa-frames (pl loop DF)", - "mesa-frames (pl loop no vec)", - "mesa-frames (pl numba CPU)", - "mesa-frames (pl numba parallel)", - "mesa-frames (pl numba GPU)", - ] - # Polars best_moves (non-vectorized loop vs DF loop vs numba loop) - kernels_1 = [ - mesa_frames_polars_loop_DF, - mesa_frames_polars_loop_no_vec, - mesa_frames_polars_numba_cpu, - mesa_frames_polars_numba_parallel, - mesa_frames_polars_numba_gpu, - ] - n_range_1 = [k for k in range(10**6, 3 * 10**6 + 2, 10**6)] - title_1 = "100 steps of the SugarScape IG model:\n" + " vs ".join(labels_1) - image_path_1 = "polars_comparison.png" - plot_and_print_benchmark( - labels_1, - kernels_1, - n_range_1, - title_1, - image_path_1, - equality_check=polars_equality_check, - ) - - -if __name__ == "__main__": - main() diff --git a/examples/sugarscape_ig/ss_mesa/__init__.py b/examples/sugarscape_ig/ss_mesa/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/sugarscape_ig/ss_mesa/agents.py b/examples/sugarscape_ig/ss_mesa/agents.py deleted file mode 100644 index e4d1a700..00000000 --- a/examples/sugarscape_ig/ss_mesa/agents.py +++ /dev/null @@ -1,83 +0,0 @@ -import math - -import mesa - - -def get_distance(pos_1, pos_2): - """Get the distance between two point - - Args: - pos_1, pos_2: Coordinate tuples for both points. - """ - x1, y1 = pos_1 - x2, y2 = pos_2 - dx = x1 - x2 - dy = y1 - y2 - return math.sqrt(dx**2 + dy**2) - - -class AntMesa(mesa.Agent): - def __init__(self, model, moore=False, sugar=0, metabolism=0, vision=0): - super().__init__(model) - self.moore = moore - self.sugar = sugar - self.metabolism = metabolism - self.vision = vision - - def get_sugar(self, pos): - this_cell = self.model.space.get_cell_list_contents([pos]) - for agent in this_cell: - if type(agent) is Sugar: - return agent - - def is_occupied(self, pos): - this_cell = self.model.space.get_cell_list_contents([pos]) - return any(isinstance(agent, AntMesa) for agent in this_cell) - - def move(self): - # Get neighborhood within vision - neighbors = [ - i - for i in self.model.space.get_neighborhood( - self.pos, self.moore, False, radius=self.vision - ) - if not self.is_occupied(i) - ] - neighbors.append(self.pos) - # Look for location with the most sugar - max_sugar = max(self.get_sugar(pos).amount for pos in neighbors) - candidates = [ - pos for pos in neighbors if self.get_sugar(pos).amount == max_sugar - ] - # Narrow down to the nearest ones - min_dist = min(get_distance(self.pos, pos) for pos in candidates) - final_candidates = [ - pos for pos in candidates if get_distance(self.pos, pos) == min_dist - ] - self.random.shuffle(final_candidates) - self.model.space.move_agent(self, final_candidates[0]) - - def eat(self): - sugar_patch = self.get_sugar(self.pos) - self.sugar = self.sugar - self.metabolism + sugar_patch.amount - sugar_patch.amount = 0 - - def step(self): - self.move() - self.eat() - if self.sugar <= 0: - self.model.space.remove_agent(self) - self.model.agents.remove(self) - - -class Sugar(mesa.Agent): - def __init__(self, model, max_sugar): - super().__init__(model) - self.amount = max_sugar - self.max_sugar = max_sugar - - def step(self): - if self.model.space.is_cell_empty(self.pos): - self.amount = self.max_sugar - else: - self.amount = 0 diff --git a/examples/sugarscape_ig/ss_mesa/model.py b/examples/sugarscape_ig/ss_mesa/model.py deleted file mode 100644 index 43114413..00000000 --- a/examples/sugarscape_ig/ss_mesa/model.py +++ /dev/null @@ -1,77 +0,0 @@ -import mesa -import numpy as np -import polars as pl - -from .agents import AntMesa, Sugar - - -class SugarscapeMesa(mesa.Model): - """ - Sugarscape 2 Instant Growback - """ - - def __init__( - self, - n_agents: int, - sugar_grid: np.ndarray | None = None, - initial_sugar: np.ndarray | None = None, - metabolism: np.ndarray | None = None, - vision: np.ndarray | None = None, - initial_positions: pl.DataFrame | None = None, - seed: int | None = None, - width: int | None = None, - height: int | None = None, - ): - """ - Create a new Instant Growback model with the given parameters. - - """ - super().__init__() - - # Set parameters - if sugar_grid is None: - sugar_grid = np.random.randint(0, 4, (width, height)) - if initial_sugar is None: - initial_sugar = np.random.randint(6, 25, n_agents) - if metabolism is None: - metabolism = np.random.randint(2, 4, n_agents) - if vision is None: - vision = np.random.randint(1, 6, n_agents) - if seed is not None: - self.reset_randomizer(seed) - - self.width, self.height = sugar_grid.shape - self.n_agents = n_agents - self.space = mesa.space.MultiGrid(self.width, self.height, torus=False) - - self.sugars = [] - - for _, (x, y) in self.space.coord_iter(): - max_sugar = sugar_grid[x, y] - sugar = Sugar(self, max_sugar) - self.space.place_agent(sugar, (x, y)) - self.sugars.append(sugar) - - # Create agent: - for i in range(self.n_agents): - if initial_positions is not None: - x = initial_positions["dim_0"][i] - y = initial_positions["dim_1"][i] - else: - x = self.random.randrange(self.width) - y = self.random.randrange(self.height) - ssa = AntMesa(self, False, initial_sugar[i], metabolism[i], vision[i]) - self.agents.add(ssa) - self.space.place_agent(ssa, (x, y)) - - self.running = True - - def step(self): - self.agents.shuffle_do("step") - [sugar.step() for sugar in self.sugars] - - def run_model(self, step_count=200): - for _ in range(step_count): - if len(self.agents) == 0: - return - self.step() diff --git a/examples/sugarscape_ig/ss_polars/__init__.py b/examples/sugarscape_ig/ss_polars/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/sugarscape_ig/ss_polars/agents.py b/examples/sugarscape_ig/ss_polars/agents.py deleted file mode 100644 index 32ca91f5..00000000 --- a/examples/sugarscape_ig/ss_polars/agents.py +++ /dev/null @@ -1,406 +0,0 @@ -from abc import abstractmethod - -import numpy as np -import polars as pl -from numba import b1, guvectorize, int32 - -from mesa_frames import AgentSet, Model - - -class AntDFBase(AgentSet): - def __init__( - self, - model: Model, - n_agents: int, - initial_sugar: np.ndarray | None = None, - metabolism: np.ndarray | None = None, - vision: np.ndarray | None = None, - ): - super().__init__(model) - - if initial_sugar is None: - initial_sugar = model.random.integers(6, 25, n_agents) - if metabolism is None: - metabolism = model.random.integers(2, 4, n_agents) - if vision is None: - vision = model.random.integers(1, 6, n_agents) - - agents = pl.DataFrame( - { - "sugar": initial_sugar, - "metabolism": metabolism, - "vision": vision, - } - ) - self.add(agents) - - def eat(self): - # Only consider cells currently occupied by agents of this set - cells = self.space.cells.filter(pl.col("agent_id").is_not_null()) - mask_in_set = cells["agent_id"].is_in(self.index) - if mask_in_set.any(): - cells = cells.filter(mask_in_set) - ids = cells["agent_id"] - self[ids, "sugar"] = ( - self[ids, "sugar"] + cells["sugar"] - self[ids, "metabolism"] - ) - - def step(self): - self.shuffle().do("move").do("eat") - self.discard(self.df.filter(pl.col("sugar") <= 0)) - - def move(self): - neighborhood = self._get_neighborhood() - agent_order = self._get_agent_order(neighborhood) - neighborhood = self._prepare_neighborhood(neighborhood, agent_order) - best_moves = self.get_best_moves(neighborhood) - self.space.move_agents(agent_order["agent_id_center"], best_moves) - - def _get_neighborhood(self) -> pl.DataFrame: - """Get the neighborhood of each agent, completed with the sugar of the cell and the agent_id of the center cell - - NOTE: This method should be unnecessary if get_neighborhood/get_neighbors return the agent_id of the center cell and the properties of the cells - - Returns - ------- - pl.DataFrame - Neighborhood DataFrame - """ - neighborhood: pl.DataFrame = self.space.get_neighborhood( - radius=self["vision"], agents=self, include_center=True - ) - # Join self.space.cells to obtain properties ('sugar') per cell - - neighborhood = neighborhood.join(self.space.cells, on=["dim_0", "dim_1"]) - - # Join self.pos to obtain the agent_id of the center cell - # TODO: get_neighborhood/get_neighbors should return 'agent_id_center' instead of center position when input is AgentLike - - neighborhood = neighborhood.with_columns( - agent_id_center=neighborhood.join( - self.pos, - left_on=["dim_0_center", "dim_1_center"], - right_on=["dim_0", "dim_1"], - )["unique_id"] - ) - return neighborhood - - def _get_agent_order(self, neighborhood: pl.DataFrame) -> pl.DataFrame: - """Get the order of agents based on the original order of agents - - Parameters - ---------- - neighborhood : pl.DataFrame - Neighborhood DataFrame - - Returns - ------- - pl.DataFrame - DataFrame with 'agent_id_center' and 'agent_order' columns - """ - # Order of agents moves based on the original order of agents. - # The agent in his cell has order 0 (highest) - - return ( - neighborhood.unique( - subset=["agent_id_center"], keep="first", maintain_order=True - ) - .with_row_count("agent_order") - .select(["agent_id_center", "agent_order"]) - ) - - def _prepare_neighborhood( - self, neighborhood: pl.DataFrame, agent_order: pl.DataFrame - ) -> pl.DataFrame: - """Prepare the neighborhood DataFrame to find the best moves - - Parameters - ---------- - neighborhood : pl.DataFrame - Neighborhood DataFrame - agent_order : pl.DataFrame - DataFrame with 'agent_id_center' and 'agent_order' columns - - Returns - ------- - pl.DataFrame - Prepared neighborhood DataFrame - """ - neighborhood = neighborhood.join(agent_order, on="agent_id_center") - - # Add blocking agent order - neighborhood = neighborhood.join( - agent_order.select( - pl.col("agent_id_center").alias("agent_id"), - pl.col("agent_order").alias("blocking_agent_order"), - ), - on="agent_id", - how="left", - ).rename({"agent_id": "blocking_agent_id"}) - - # Filter only possible moves (agent is in his cell, blocking agent has moved before him or there is no blocking agent) - neighborhood = neighborhood.filter( - (pl.col("agent_order") >= pl.col("blocking_agent_order")) - | pl.col("blocking_agent_order").is_null() - ) - - # Sort neighborhood by agent_order & max_sugar (max_sugar because we will check anyway if the cell is empty) - # However, we need to make sure that the current agent cell is ordered by current sugar (since it's 0 until agent hasn't moved) - neighborhood = neighborhood.with_columns( - max_sugar=pl.when(pl.col("blocking_agent_id") == pl.col("agent_id_center")) - .then(pl.lit(0)) - .otherwise(pl.col("max_sugar")) - ).sort( - ["agent_order", "max_sugar", "radius", "dim_0"], - descending=[False, True, False, False], - ) - return neighborhood - - def get_best_moves(self, neighborhood: pl.DataFrame) -> pl.DataFrame: - """Get the best moves for each agent - - Parameters - ---------- - neighborhood : pl.DataFrame - Neighborhood DataFrame - - Returns - ------- - pl.DataFrame - DataFrame with the best moves for each agent - """ - raise NotImplementedError("Subclasses must implement this method") - - -class AntPolarsLoopDF(AntDFBase): - def get_best_moves(self, neighborhood: pl.DataFrame): - best_moves = pl.DataFrame() - - # While there are agents that do not have a best move, keep looking for one - while len(best_moves) < len(self.df): - # Check if there are previous agents that might make the same move (priority for the given move is > 1) - neighborhood = neighborhood.with_columns( - priority=pl.col("agent_order").cum_count().over(["dim_0", "dim_1"]) - ) - - # Get the best moves for each agent: - # If duplicates are found, select the one with the highest order - new_best_moves = ( - neighborhood.group_by("agent_id_center", maintain_order=True) - .first() - .unique(subset=["dim_0", "dim_1"], keep="first", maintain_order=True) - ) - # Agents can make the move if: - # - There is no blocking agent - # - The agent is in its own cell - # - The blocking agent has moved before him - # - There isn't a higher priority agent that might make the same move - - condition = pl.col("blocking_agent_id").is_null() | ( - pl.col("blocking_agent_id") == pl.col("agent_id_center") - ) - if len(best_moves) > 0: - condition = condition | pl.col("blocking_agent_id").is_in( - best_moves["agent_id_center"] - ) - - condition = condition & (pl.col("priority") == 1) - - new_best_moves = new_best_moves.filter(condition) - - best_moves = pl.concat([best_moves, new_best_moves]) - - # Remove agents that have already moved - neighborhood = neighborhood.filter( - ~pl.col("agent_id_center").is_in(best_moves["agent_id_center"]) - ) - - # Remove cells that have been already selected - neighborhood = neighborhood.join( - best_moves.select(["dim_0", "dim_1"]), on=["dim_0", "dim_1"], how="anti" - ) - - # Check if there are previous agents that might make the same move (priority for the given move is > 1) - neighborhood = neighborhood.with_columns( - priority=pl.col("agent_order").cum_count().over(["dim_0", "dim_1"]) - ) - return best_moves.sort("agent_order").select(["dim_0", "dim_1"]) - - -class AntPolarsLoop(AntDFBase): - numba_target = None - - def get_best_moves(self, neighborhood: pl.DataFrame): - occupied_cells, free_cells, target_cells = self._prepare_cells(neighborhood) - best_moves_func = self._get_best_moves() - - processed_agents = np.zeros(len(self.df), dtype=np.bool_) - - if self.numba_target is None: - # Non-vectorized case: we need to create and pass the best_moves array - map_batches_func = lambda df: best_moves_func( - occupied_cells, - free_cells, - target_cells, - df.struct.field("agent_order"), - df.struct.field("blocking_agent_order"), - processed_agents, - best_moves=np.full(len(self.df), -1, dtype=np.int32), - ) - else: - # Vectorized case: Polars will create the output array (best_moves) automatically - map_batches_func = lambda df: best_moves_func( - occupied_cells.astype(np.int32), - free_cells.astype(np.bool_), - target_cells.astype(np.int32), - df.struct.field("agent_order"), - df.struct.field("blocking_agent_order"), - processed_agents.astype(np.bool_), - ) - - best_moves = ( - # Only fill nulls for the column we need as an int sentinel; - # avoid touching UInt columns like 'blocking_agent_id'. - neighborhood.with_columns(pl.col("blocking_agent_order").fill_null(-1)) - .cast({"agent_order": pl.Int32, "blocking_agent_order": pl.Int32}) - .select( - pl.struct(["agent_order", "blocking_agent_order"]).map_batches( - map_batches_func, - return_dtype=pl.Int32, - ) - ) - .with_columns( - dim_0=pl.col("agent_order") // self.space.dimensions[1], - dim_1=pl.col("agent_order") % self.space.dimensions[1], - ) - .drop("agent_order") - ) - return best_moves - - def _prepare_cells( - self, neighborhood: pl.DataFrame - ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: - """Get the occupied and free cells and the target cells for each agent, - based on the neighborhood DataFrame such that the arrays refer to a flattened version of the grid - - Parameters - ---------- - neighborhood : pl.DataFrame - Neighborhood DataFrame - - Returns - ------- - tuple[np.ndarray, np.ndarray, np.ndarray] - occupied_cells, free_cells, target_cells - """ - occupied_cells = ( - neighborhood[["agent_id_center", "agent_order"]] - .unique() - .join(self.pos, left_on="agent_id_center", right_on="unique_id") - .with_columns( - flattened=(pl.col("dim_0") * self.space.dimensions[1] + pl.col("dim_1")) - ) - .sort("agent_order")["flattened"] - .to_numpy() - ) - free_cells = np.ones( - self.space.dimensions[0] * self.space.dimensions[1], dtype=np.bool_ - ) - free_cells[occupied_cells] = False - - target_cells = ( - neighborhood["dim_0"] * self.space.dimensions[1] + neighborhood["dim_1"] - ).to_numpy() - return occupied_cells, free_cells, target_cells - - def _get_best_moves(self): - raise NotImplementedError("Subclasses must implement this method") - - -class AntPolarsLoopNoVec(AntPolarsLoop): - # Non-vectorized case - def _get_best_moves(self): - def inner_get_best_moves( - occupied_cells: np.ndarray, - free_cells: np.ndarray, - target_cells: np.ndarray, - agent_id_center: np.ndarray, - blocking_agent: np.ndarray, - processed_agents: np.ndarray, - best_moves: np.ndarray, - ) -> np.ndarray: - for i, agent in enumerate(agent_id_center): - # If the agent has not moved yet - if not processed_agents[agent]: - # If the target cell is free - if free_cells[target_cells[i]] or blocking_agent[i] == agent: - best_moves[agent] = target_cells[i] - # Free current cell - free_cells[occupied_cells[agent]] = True - # Occupy target cell - free_cells[target_cells[i]] = False - processed_agents[agent] = True - return best_moves - - return inner_get_best_moves - - -class AntPolarsNumba(AntPolarsLoop): - # Vectorized case - def _get_best_moves(self): - @guvectorize( - [ - ( - int32[:], - b1[:], - int32[:], - int32[:], - int32[:], - b1[:], - int32[:], - ) - ], - "(n), (m), (p), (p), (p), (n)->(n)", - nopython=True, - target=self.numba_target, - # Writable inputs should be declared according to https://numba.pydata.org/numba-doc/dev/user/vectorize.html#overwriting-input-values - # In this case, there doesn't seem to be a difference. I will leave it commented for reference so that we can use CUDA target (which doesn't support writable_args) - # writable_args=( - # "free_cells", - # "processed_agents", - # ), - ) - def vectorized_get_best_moves( - occupied_cells, - free_cells, - target_cells, - agent_id_center, - blocking_agent, - processed_agents, - best_moves, - ): - for i, agent in enumerate(agent_id_center): - # If the agent has not moved yet - if not processed_agents[agent]: - # If the target cell is free - if free_cells[target_cells[i]] or blocking_agent[i] == agent: - best_moves[agent] = target_cells[i] - # Free current cell - free_cells[occupied_cells[agent]] = True - # Occupy target cell - free_cells[target_cells[i]] = False - processed_agents[agent] = True - - return vectorized_get_best_moves - - -class AntPolarsNumbaCPU(AntPolarsNumba): - numba_target = "cpu" - - -class AntPolarsNumbaParallel(AntPolarsNumba): - numba_target = "parallel" - - -class AntPolarsNumbaGPU(AntPolarsNumba): - numba_target = "cuda" diff --git a/examples/sugarscape_ig/ss_polars/model.py b/examples/sugarscape_ig/ss_polars/model.py deleted file mode 100644 index 36b2718e..00000000 --- a/examples/sugarscape_ig/ss_polars/model.py +++ /dev/null @@ -1,57 +0,0 @@ -import numpy as np -import polars as pl - -from mesa_frames import Grid, Model - -from .agents import AntDFBase - - -class SugarscapePolars(Model): - def __init__( - self, - agent_type: type[AntDFBase], - n_agents: int, - sugar_grid: np.ndarray | None = None, - initial_sugar: np.ndarray | None = None, - metabolism: np.ndarray | None = None, - vision: np.ndarray | None = None, - initial_positions: pl.DataFrame | None = None, - seed: int | None = None, - width: int | None = None, - height: int | None = None, - ): - super().__init__(seed) - if sugar_grid is None: - sugar_grid = self.random.integers(0, 4, (width, height)) - grid_dimensions = sugar_grid.shape - self.space = Grid( - self, grid_dimensions, neighborhood_type="von_neumann", capacity=1 - ) - dim_0 = pl.Series("dim_0", pl.arange(grid_dimensions[0], eager=True)).to_frame() - dim_1 = pl.Series("dim_1", pl.arange(grid_dimensions[1], eager=True)).to_frame() - sugar_grid = dim_0.join(dim_1, how="cross").with_columns( - sugar=sugar_grid.flatten(), max_sugar=sugar_grid.flatten() - ) - self.space.set_cells(sugar_grid) - # Create and register the main agent set; keep its name for later lookups - main_set = agent_type(self, n_agents, initial_sugar, metabolism, vision) - self.sets += main_set - self._main_set_name = main_set.name - if initial_positions is not None: - self.space.place_agents(self.sets, initial_positions) - else: - self.space.place_to_empty(self.sets) - - def run_model(self, steps: int) -> list[int]: - for _ in range(steps): - # Stop if the main agent set is empty - if len(self.sets[self._main_set_name]) == 0: # type: ignore[index] - return - empty_cells = self.space.empty_cells - full_cells = self.space.full_cells - max_sugar = self.space.cells.join( - empty_cells, on=["dim_0", "dim_1"] - ).select(pl.col("max_sugar")) - self.space.set_cells(full_cells, {"sugar": 0}) - self.space.set_cells(empty_cells, {"sugar": max_sugar}) - self.step() From 747a15d7f7afd12c075b974bf6e72efe2b72f58a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Sep 2025 10:25:01 +0000 Subject: [PATCH 246/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- benchmarks/cli.py | 62 +++++++++++++++-------- examples/boltzmann_wealth/backend_mesa.py | 13 +++-- examples/plotting.py | 44 ++++++++-------- examples/utils.py | 4 +- 4 files changed, 75 insertions(+), 48 deletions(-) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index c0b9355d..3accba5d 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -20,13 +20,14 @@ app = typer.Typer(add_completion=False) + class RunnerP(Protocol): - def __call__(self, agents: int, steps: int, seed: Optional[int] = None) -> None: ... + def __call__(self, agents: int, steps: int, seed: int | None = None) -> None: ... @dataclass(slots=True) class Backend: - name: Literal['mesa', 'frames'] + name: Literal["mesa", "frames"] runner: RunnerP @@ -59,6 +60,7 @@ class ModelConfig: ), } + def _parse_agents(value: str) -> list[int]: value = value.strip() if ":" in value: @@ -67,7 +69,7 @@ def _parse_agents(value: str) -> list[int]: raise typer.BadParameter("Ranges must use start:stop:step format") try: start, stop, step = (int(part) for part in parts) - except ValueError as exc: + except ValueError as exc: raise typer.BadParameter("Range values must be integers") from exc if step <= 0: raise typer.BadParameter("Step must be positive") @@ -87,6 +89,7 @@ def _parse_agents(value: str) -> list[int]: raise typer.BadParameter("Agent count must be positive") return [agents] + def _parse_models(value: str) -> list[str]: """Parse models option into a list of model keys. @@ -116,6 +119,7 @@ def _parse_models(value: str) -> list[str]: result.append(p) return result + def _plot_performance( df: pl.DataFrame, model_name: str, output_dir: Path, timestamp: str ) -> None: @@ -145,32 +149,46 @@ def _plot_performance( @app.command() def run( - models: Annotated[str, typer.Option( - help="Models to benchmark: boltzmann, sugarscape, or all", - callback=_parse_models - )] = "all", - agents: Annotated[list[int], typer.Option( - help="Agent count or range (start:stop:step)", - callback=_parse_agents - )] = "1000:5000:1000", - steps: Annotated[int, typer.Option( - min=0, - help="Number of steps per run.", - )] = 100, + models: Annotated[ + str, + typer.Option( + help="Models to benchmark: boltzmann, sugarscape, or all", + callback=_parse_models, + ), + ] = "all", + agents: Annotated[ + list[int], + typer.Option( + help="Agent count or range (start:stop:step)", callback=_parse_agents + ), + ] = "1000:5000:1000", + steps: Annotated[ + int, + typer.Option( + min=0, + help="Number of steps per run.", + ), + ] = 100, repeats: Annotated[int, typer.Option(help="Repeats per configuration.", min=1)] = 1, seed: Annotated[int, typer.Option(help="Optional RNG seed.")] = 42, save: Annotated[bool, typer.Option(help="Persist benchmark CSV results.")] = True, plot: Annotated[bool, typer.Option(help="Render performance plots.")] = True, - results_dir: Annotated[Path, typer.Option( - help="Directory for benchmark CSV results.", - )] = Path(__file__).resolve().parent / "results", - plots_dir: Annotated[Path, typer.Option( - help="Directory for benchmark plots.", - )] = Path(__file__).resolve().parent / "plots", + results_dir: Annotated[ + Path, + typer.Option( + help="Directory for benchmark CSV results.", + ), + ] = Path(__file__).resolve().parent / "results", + plots_dir: Annotated[ + Path, + typer.Option( + help="Directory for benchmark plots.", + ), + ] = Path(__file__).resolve().parent / "plots", ) -> None: """Run performance benchmarks for the models models.""" rows: list[dict[str, object]] = [] - timestamp = datetime.now(datetime.timezone.utc).strftime("%Y%m%d_%H%M%S") + timestamp = datetime.now(datetime.UTC).strftime("%Y%m%d_%H%M%S") for model in models: config = MODELS[model] typer.echo(f"Benchmarking {model} with agents {agents}") diff --git a/examples/boltzmann_wealth/backend_mesa.py b/examples/boltzmann_wealth/backend_mesa.py index 8b6e3162..8b86ad3e 100644 --- a/examples/boltzmann_wealth/backend_mesa.py +++ b/examples/boltzmann_wealth/backend_mesa.py @@ -4,7 +4,8 @@ from datetime import datetime, timezone from pathlib import Path -from typing import Iterable, Annotated +from typing import Annotated +from collections.abc import Iterable import pandas as pd import matplotlib.pyplot as plt @@ -42,7 +43,7 @@ def gini(values: Iterable[float]) -> float: class MoneyAgent(mesa.Agent): """Agent that passes one unit of wealth to a random neighbour.""" - def __init__(self, model: "MoneyModel") -> None: + def __init__(self, model: MoneyModel) -> None: super().__init__(model) self.wealth = 1 @@ -98,6 +99,7 @@ def simulate(agents: int, steps: int, seed: int | None = None) -> MesaSimulation app = typer.Typer(add_completion=False) + @app.command() def run( agents: Annotated[int, typer.Option(help="Number of agents to simulate.")] = 5000, @@ -127,7 +129,9 @@ def run( # Resolve output folder timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") if results_dir is None: - results_dir = (Path(__file__).resolve().parent / "results" / timestamp).resolve() + results_dir = ( + Path(__file__).resolve().parent / "results" / timestamp + ).resolve() results_dir.mkdir(parents=True, exist_ok=True) start_time = perf_counter() @@ -143,13 +147,12 @@ def run( # The first column is the step index; normalize name to "step". model_pd = model_pd.rename(columns={model_pd.columns[0]: "step"}) seed = model_pd["seed"].iloc[0] - model_pd = model_pd[['step', 'gini']] + model_pd = model_pd[["step", "gini"]] # Show a short tail in console for quick inspection tail_str = model_pd.tail(5).to_string(index=False) typer.echo(f"Metrics in the final 5 steps:\n{tail_str}") - # ---- Save CSV (same filename/layout as frames backend expects) if save_results: csv_path = results_dir / "model.csv" diff --git a/examples/plotting.py b/examples/plotting.py index 5313dcf8..0cf002c4 100644 --- a/examples/plotting.py +++ b/examples/plotting.py @@ -2,7 +2,7 @@ from __future__ import annotations from pathlib import Path -from typing import Sequence +from collections.abc import Sequence import re import polars as pl @@ -27,16 +27,16 @@ rc={ # real dark background + readable foreground "figure.facecolor": "#0b1021", - "axes.facecolor": "#0b1021", - "axes.edgecolor": "#d6d6d7", - "axes.labelcolor": "#e8e8ea", - "text.color": "#e8e8ea", - "xtick.color": "#c9c9cb", - "ytick.color": "#c9c9cb", - "grid.color": "#2a2f4a", - "grid.alpha": 0.35, - "axes.spines.top": False, - "axes.spines.right":False, + "axes.facecolor": "#0b1021", + "axes.edgecolor": "#d6d6d7", + "axes.labelcolor": "#e8e8ea", + "text.color": "#e8e8ea", + "xtick.color": "#c9c9cb", + "ytick.color": "#c9c9cb", + "grid.color": "#2a2f4a", + "grid.alpha": 0.35, + "axes.spines.top": False, + "axes.spines.right": False, "legend.facecolor": "#121734", "legend.edgecolor": "#3b3f5a", }, @@ -77,6 +77,7 @@ def _finalize_and_save(fig: Figure, output_dir: Path, stem: str, theme: str) -> # -------------------------- Public: model metrics ---------------------------- + def plot_model_metrics( metrics: pl.DataFrame, output_dir: Path, @@ -118,7 +119,9 @@ def plot_model_metrics( long = ( metrics.select(["step", *value_cols]) - .unpivot(index="step", on=value_cols, variable_name="metric", value_name="value") + .unpivot( + index="step", on=value_cols, variable_name="metric", value_name="value" + ) .to_pandas() ) @@ -170,6 +173,7 @@ def plot_model_metrics( # -------------------------- Public: agent metrics ---------------------------- + def plot_agent_metrics( agent_metrics: pl.DataFrame, output_dir: Path, @@ -189,19 +193,18 @@ def plot_agent_metrics( return preferred = ["step", "seed", "batch"] - id_vars = [c for c in preferred if c in agent_metrics.columns] or [agent_metrics.columns[0]] + id_vars = [c for c in preferred if c in agent_metrics.columns] or [ + agent_metrics.columns[0] + ] # Determine which columns to unpivot (all columns except the id vars). value_cols = [c for c in agent_metrics.columns if c not in id_vars] if not value_cols: return - melted = ( - agent_metrics.unpivot( - index=id_vars, on=value_cols, variable_name="metric", value_name="value" - ) - .to_pandas() - ) + melted = agent_metrics.unpivot( + index=id_vars, on=value_cols, variable_name="metric", value_name="value" + ).to_pandas() xcol = id_vars[0] @@ -227,6 +230,7 @@ def plot_agent_metrics( # -------------------------- Public: performance ------------------------------ + def plot_performance( df: pl.DataFrame, output_dir: Path, @@ -278,4 +282,4 @@ def plot_performance( "plot_model_metrics", "plot_agent_metrics", "plot_performance", -] \ No newline at end of file +] diff --git a/examples/utils.py b/examples/utils.py index dbd165b4..4d075dc4 100644 --- a/examples/utils.py +++ b/examples/utils.py @@ -2,6 +2,7 @@ import mesa_frames import mesa + @dataclass class FramesSimulationResult: """Container for example simulation outputs. @@ -12,6 +13,7 @@ class FramesSimulationResult: datacollector: mesa_frames.DataCollector + @dataclass class MesaSimulationResult: """Container for example simulation outputs. @@ -20,4 +22,4 @@ class MesaSimulationResult: `metrics`, while others also return `agent_metrics`. """ - datacollector: mesa.DataCollector \ No newline at end of file + datacollector: mesa.DataCollector From 91d7f39683bb512e5ef07cb498aca1b2c295858b Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 7 Oct 2025 13:17:52 +0200 Subject: [PATCH 247/329] feat: add Sugarscape IG implementation with Typer CLI; include agent and model classes for simulation --- .../sugarscape_ig/backend_frames/agents.py | 625 ++++++++++++++++++ .../sugarscape_ig/backend_frames/model.py | 475 +++++++++++++ 2 files changed, 1100 insertions(+) create mode 100644 examples/sugarscape_ig/backend_frames/agents.py create mode 100644 examples/sugarscape_ig/backend_frames/model.py diff --git a/examples/sugarscape_ig/backend_frames/agents.py b/examples/sugarscape_ig/backend_frames/agents.py new file mode 100644 index 00000000..5f5f6b6d --- /dev/null +++ b/examples/sugarscape_ig/backend_frames/agents.py @@ -0,0 +1,625 @@ +"""Agent implementations for the Sugarscape IG example (mesa-frames). + +This module provides the parallel (synchronous) movement variant as in the +advanced tutorial. The code and comments mirror +docs/general/user-guide/3_advanced_tutorial.py. +""" + +from __future__ import annotations + +import numpy as np +import polars as pl + +from mesa_frames import AgentSet, Model + + +class AntsBase(AgentSet): + """Base agent set for the Sugarscape tutorial. + + This class implements the common behaviour shared by all agent + movement variants (sequential, numba-accelerated and parallel). + + Notes + ----- + - Agents are expected to have integer traits: ``sugar``, ``metabolism`` + and ``vision``. These are validated in :meth:`__init__`. + - Subclasses must implement :meth:`move` which changes agent positions + on the grid (via :meth:`mesa_frames.Grid` helpers). + """ + + def __init__(self, model: Model, agent_frame: pl.DataFrame) -> None: + """Initialise the agent set and validate required trait columns. + + Parameters + ---------- + model : Model + The parent model which provides RNG and space. + agent_frame : pl.DataFrame + A Polars DataFrame with at least the columns ``sugar``, + ``metabolism`` and ``vision`` for each agent. + + Raises + ------ + ValueError + If required trait columns are missing from ``agent_frame``. + """ + super().__init__(model) + required = {"sugar", "metabolism", "vision"} + missing = required.difference(agent_frame.columns) + if missing: + raise ValueError( + f"Initial agent frame must include columns {sorted(required)}; missing {sorted(missing)}." + ) + self.add(agent_frame.clone()) + + def step(self) -> None: + """Advance the agent set by one time step. + + The update order is important: agents are first shuffled to randomise + move order (this is important only for sequential variants), then they move, harvest sugar + from their occupied cells, and finally any agents whose sugar falls + to zero or below are removed. + """ + # Randomise ordering for movement decisions when required by the + # implementation (e.g. sequential update uses this shuffle). + self.shuffle(inplace=True) + # Movement policy implemented by subclasses. + self.move() + # Agents harvest sugar on their occupied cells. + self.eat() + # Remove agents that starved after eating. + self._remove_starved() + + def move(self) -> None: # pragma: no cover + """Abstract movement method. + + Subclasses must override this method to update agent positions on the + grid. Implementations should use :meth:`mesa_frames.Grid.move_agents` + or similar helpers provided by the space API. + """ + raise NotImplementedError + + def eat(self) -> None: + """Agents harvest sugar from the cells they currently occupy. + + Behaviour: + - Look up the set of occupied cells (cells that reference an agent + id). + - For each occupied cell, add the cell sugar to the agent's sugar + stock and subtract the agent's metabolism cost. + - After agents harvest, set the sugar on those cells to zero (they + were consumed). + """ + # Map of currently occupied agent ids on the grid. + occupied_ids = self.index + # `occupied_ids` is a Polars Series; calling `is_in` with a Series + # of the same datatype is ambiguous in newer Polars. Use `implode` + # to collapse the Series into a list-like value for membership checks. + occupied_cells = self.space.cells.filter( + pl.col("agent_id").is_in(occupied_ids.implode()) + ) + if occupied_cells.is_empty(): + return + # The agent ordering here uses the agent_id values stored in the + # occupied cells frame; indexing the agent set with that vector updates + # the matching agents' sugar values in one vectorised write. + agent_ids = occupied_cells["agent_id"] + self[agent_ids, "sugar"] = ( + self[agent_ids, "sugar"] + + occupied_cells["sugar"] + - self[agent_ids, "metabolism"] + ) + # After harvesting, occupied cells have zero sugar. + self.space.set_cells( + occupied_cells.select(["dim_0", "dim_1"]), + {"sugar": pl.Series(np.zeros(len(occupied_cells), dtype=np.int64))}, + ) + + def _remove_starved(self) -> None: + """Discard agents whose sugar stock has fallen to zero or below. + + This method performs a vectorised filter on the agent frame and + removes any matching rows from the set. + """ + starved = self.df.filter(pl.col("sugar") <= 0) + if not starved.is_empty(): + # ``discard`` accepts a DataFrame of agents to remove. + self.discard(starved) + + +class AntsParallel(AntsBase): + def move(self) -> None: + """Move agents in parallel by ranking visible cells and resolving conflicts. + + Declarative mental model: express *what* each agent wants (ranked candidates), + then use dataframe ops to *allocate* (joins, group_by with a lottery). + Performance is handled by Polars/LazyFrames; avoid premature micro-optimisations. + + Returns + ------- + None + Movement updates happen in-place on the underlying space. + """ + # Early exit if there are no agents. + if len(self.df) == 0: + return + + # current_pos columns: + # ┌──────────┬────────────────┬────────────────┐ + # │ agent_id ┆ dim_0_center ┆ dim_1_center │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════════════╪════════════════╡ + current_pos = self.pos.select( + [ + pl.col("unique_id").alias("agent_id"), + pl.col("dim_0").alias("dim_0_center"), + pl.col("dim_1").alias("dim_1_center"), + ] + ) + + neighborhood = self._build_neighborhood_frame(current_pos) + choices, origins, max_rank = self._rank_candidates(neighborhood, current_pos) + if choices.is_empty(): + return + + assigned = self._resolve_conflicts_in_rounds(choices, origins, max_rank) + if assigned.is_empty(): + return + + # move_df columns: + # ┌────────────┬────────────┬────────────┐ + # │ unique_id ┆ dim_0 ┆ dim_1 │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞════════════╪════════════╪════════════╡ + move_df = pl.DataFrame( + { + "unique_id": assigned["agent_id"], + "dim_0": assigned["dim_0_candidate"], + "dim_1": assigned["dim_1_candidate"], + } + ) + # `move_agents` accepts IdsLike and SpaceCoordinates (Polars Series/DataFrame), + # so pass Series/DataFrame directly rather than converting to Python lists. + self.space.move_agents(move_df["unique_id"], move_df.select(["dim_0", "dim_1"])) + + def _build_neighborhood_frame(self, current_pos: pl.DataFrame) -> pl.DataFrame: + """Assemble the sugar-weighted neighbourhood for each sensing agent. + + Parameters + ---------- + current_pos : pl.DataFrame + DataFrame with columns ``agent_id``, ``dim_0_center`` and + ``dim_1_center`` describing the current position of each agent. + + Returns + ------- + pl.DataFrame + DataFrame with columns ``agent_id``, ``radius``, ``dim_0_candidate``, + ``dim_1_candidate`` and ``sugar`` describing the visible cells for + each agent. + """ + # Build a neighbourhood frame: for each agent and visible cell we + # attach the cell sugar. The raw offsets contain the candidate + # cell coordinates and the center coordinates for the sensing agent. + # Raw neighborhood columns: + # ┌────────────┬────────────┬────────┬────────────────┬────────────────┐ + # │ dim_0 ┆ dim_1 ┆ radius ┆ dim_0_center ┆ dim_1_center │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞════════════╪════════════╪════════╪════════════════╪════════════════╡ + neighborhood_cells = self.space.get_neighborhood( + radius=self["vision"], agents=self, include_center=True + ) + + # sugar_cells columns: + # ┌────────────┬────────────┬────────┐ + # │ dim_0 ┆ dim_1 ┆ sugar │ + # │ --- ┆ --- ┆ --- │ + # │ i64 ┆ i64 ┆ i64 │ + # ╞════════════╪════════════╪════════╡ + + sugar_cells = self.space.cells.select(["dim_0", "dim_1", "sugar"]) + + neighborhood_cells = ( + neighborhood_cells.join(sugar_cells, on=["dim_0", "dim_1"], how="left") + .with_columns(pl.col("sugar").fill_null(0)) + .rename({"dim_0": "dim_0_candidate", "dim_1": "dim_1_candidate"}) + ) + + neighborhood_cells = neighborhood_cells.join( + current_pos, + left_on=["dim_0_center", "dim_1_center"], + right_on=["dim_0_center", "dim_1_center"], + how="left", + ) + + # Final neighborhood columns: + # ┌──────────┬────────┬──────────────────┬──────────────────┬────────┐ + # │ agent_id ┆ radius ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════╪══════════════════╪══════════════════╪════════╡ + neighborhood_cells = neighborhood_cells.drop( + ["dim_0_center", "dim_1_center"] + ).select(["agent_id", "radius", "dim_0_candidate", "dim_1_candidate", "sugar"]) + + return neighborhood_cells + + def _rank_candidates( + self, + neighborhood: pl.DataFrame, + current_pos: pl.DataFrame, + ) -> tuple[pl.DataFrame, pl.DataFrame, pl.DataFrame]: + """Rank candidate destination cells for each agent. + + Parameters + ---------- + neighborhood : pl.DataFrame + Output of :meth:`_build_neighborhood_frame` with columns + ``agent_id``, ``radius``, ``dim_0_candidate``, ``dim_1_candidate`` + and ``sugar``. + current_pos : pl.DataFrame + Frame with columns ``agent_id``, ``dim_0_center`` and + ``dim_1_center`` describing where each agent currently stands. + + Returns + ------- + choices : pl.DataFrame + Ranked candidates per agent with columns ``agent_id``, + ``dim_0_candidate``, ``dim_1_candidate``, ``sugar``, ``radius`` and + ``rank``. + origins : pl.DataFrame + Original coordinates per agent with columns ``agent_id``, + ``dim_0`` and ``dim_1``. + max_rank : pl.DataFrame + Maximum available rank per agent with columns ``agent_id`` and + ``max_rank``. + """ + # Create ranked choices per agent: sort by sugar (desc), radius + # (asc), then coordinates. Keep the first unique entry per cell. + # choices columns (after select): + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╡ + choices = ( + neighborhood.select( + [ + "agent_id", + "dim_0_candidate", + "dim_1_candidate", + "sugar", + "radius", + ] + ) + .with_columns(pl.col("radius")) + .sort( + ["agent_id", "sugar", "radius", "dim_0_candidate", "dim_1_candidate"], + descending=[False, True, False, False, False], + ) + .unique( + subset=["agent_id", "dim_0_candidate", "dim_1_candidate"], + keep="first", + maintain_order=True, + ) + .with_columns(pl.col("agent_id").cum_count().over("agent_id").alias("rank")) + ) + + # Precompute per‑agent candidate rank once so conflict resolution can + # promote losers by incrementing a cheap `current_rank` counter, + # without re-sorting after each round. Alternative: drop taken cells + # and re-rank by sugar every round; simpler conceptually but requires + # repeated sorts and deduplication, which is heavier than filtering by + # `rank >= current_rank`. + + # Origins for fallback (if an agent exhausts candidates it stays put). + # origins columns: + # ┌──────────┬────────────┬────────────┐ + # │ agent_id ┆ dim_0 ┆ dim_1 │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════════╪════════════╡ + origins = current_pos.select( + [ + "agent_id", + pl.col("dim_0_center").alias("dim_0"), + pl.col("dim_1_center").alias("dim_1"), + ] + ) + + # Track the maximum available rank per agent to clamp promotions. + # This bounds `current_rank`; once an agent reaches `max_rank` and + # cannot secure a cell, they fall back to origin cleanly instead of + # chasing nonexistent ranks. + # max_rank columns: + # ┌──────────┬───────────┐ + # │ agent_id ┆ max_rank │ + # │ --- ┆ --- │ + # │ u64 ┆ u32 │ + # ╞══════════╪═══════════╡ + max_rank = choices.group_by("agent_id").agg( + pl.col("rank").max().alias("max_rank") + ) + return choices, origins, max_rank + + def _resolve_conflicts_in_rounds( + self, + choices: pl.DataFrame, + origins: pl.DataFrame, + max_rank: pl.DataFrame, + ) -> pl.DataFrame: + """Resolve movement conflicts through iterative lottery rounds. + + Parameters + ---------- + choices : pl.DataFrame + Ranked candidate cells per agent with headers matching the + ``choices`` frame returned by :meth:`_rank_candidates`. + origins : pl.DataFrame + Agent origin coordinates with columns ``agent_id``, ``dim_0`` and + ``dim_1``. + max_rank : pl.DataFrame + Maximum rank offset per agent with columns ``agent_id`` and + ``max_rank``. + + Returns + ------- + pl.DataFrame + Allocated movements with columns ``agent_id``, ``dim_0_candidate`` + and ``dim_1_candidate``; each row records the destination assigned + to an agent. + """ + # Prepare unresolved agents and working tables. + agent_ids = choices["agent_id"].unique(maintain_order=True) + + # unresolved columns: + # ┌──────────┬────────────────┐ + # │ agent_id ┆ current_rank │ + # │ --- ┆ --- │ + # │ u64 ┆ i64 │ + # ╞══════════╪════════════════╡ + unresolved = pl.DataFrame( + { + "agent_id": agent_ids, + "current_rank": pl.Series(np.zeros(len(agent_ids), dtype=np.int64)), + } + ) + + # assigned columns: + # ┌──────────┬──────────────────┬──────────────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate │ + # │ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 │ + # ╞══════════╪══════════════════╪══════════════════╡ + assigned = pl.DataFrame( + { + "agent_id": pl.Series( + name="agent_id", values=[], dtype=agent_ids.dtype + ), + "dim_0_candidate": pl.Series( + name="dim_0_candidate", values=[], dtype=pl.Int64 + ), + "dim_1_candidate": pl.Series( + name="dim_1_candidate", values=[], dtype=pl.Int64 + ), + } + ) + + # taken columns: + # ┌──────────────────┬──────────────────┐ + # │ dim_0_candidate ┆ dim_1_candidate │ + # │ --- ┆ --- │ + # │ i64 ┆ i64 │ + # ╞══════════════════╪══════════════════╡ + taken = pl.DataFrame( + { + "dim_0_candidate": pl.Series( + name="dim_0_candidate", values=[], dtype=pl.Int64 + ), + "dim_1_candidate": pl.Series( + name="dim_1_candidate", values=[], dtype=pl.Int64 + ), + } + ) + + # Resolve in rounds: each unresolved agent proposes its current-ranked + # candidate; winners per-cell are selected at random and losers are + # promoted to their next choice. + while unresolved.height > 0: + # Using precomputed `rank` lets us select candidates with + # `rank >= current_rank` and avoid re-ranking after each round. + # Alternative: remove taken cells and re-sort remaining candidates + # by sugar/distance per round (heavier due to repeated sort/dedupe). + # candidate_pool columns (after join with unresolved): + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────┬──────────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ rank ┆ current_rank │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u32 ┆ i64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════╪══════════════╡ + candidate_pool = choices.join(unresolved, on="agent_id") + candidate_pool = candidate_pool.filter( + pl.col("rank") >= pl.col("current_rank") + ) + if not taken.is_empty(): + candidate_pool = candidate_pool.join( + taken, + on=["dim_0_candidate", "dim_1_candidate"], + how="anti", + ) + + if candidate_pool.is_empty(): + # No available candidates — everyone falls back to origin. + # Note: this covers both agents with no visible cells left and + # the case where all remaining candidates are already taken. + # fallback columns: + # ┌──────────┬────────────┬────────────┬──────────────┐ + # │ agent_id ┆ dim_0 ┆ dim_1 ┆ current_rank │ + # │ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 │ + # ╞══════════╪════════════╪════════════╪══════════════╡ + fallback = unresolved.join(origins, on="agent_id", how="left") + assigned = pl.concat( + [ + assigned, + fallback.select( + [ + "agent_id", + pl.col("dim_0").alias("dim_0_candidate"), + pl.col("dim_1").alias("dim_1_candidate"), + ] + ), + ], + how="vertical", + ) + break + + # best_candidates columns (per agent first choice): + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────┬──────────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ rank ┆ current_rank │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ + # │ u64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ u32 ┆ i64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════╪══════════════╡ + best_candidates = ( + candidate_pool.sort(["agent_id", "rank"]) + .group_by("agent_id", maintain_order=True) + .first() + ) + + # Agents that had no candidate this round fall back to origin. + # missing columns: + # ┌──────────┬──────────────┐ + # │ agent_id ┆ current_rank │ + # │ --- ┆ --- │ + # │ u64 ┆ i64 │ + # ╞══════════╪══════════════╡ + missing = unresolved.join( + best_candidates.select("agent_id"), on="agent_id", how="anti" + ) + if not missing.is_empty(): + # fallback (missing) columns match fallback table above. + fallback = missing.join(origins, on="agent_id", how="left") + assigned = pl.concat( + [ + assigned, + fallback.select( + [ + "agent_id", + pl.col("dim_0").alias("dim_0_candidate"), + pl.col("dim_1").alias("dim_1_candidate"), + ] + ), + ], + how="vertical", + ) + taken = pl.concat( + [ + taken, + fallback.select( + [ + pl.col("dim_0").alias("dim_0_candidate"), + pl.col("dim_1").alias("dim_1_candidate"), + ] + ), + ], + how="vertical", + ) + unresolved = unresolved.join( + missing.select("agent_id"), on="agent_id", how="anti" + ) + best_candidates = best_candidates.join( + missing.select("agent_id"), on="agent_id", how="anti" + ) + if unresolved.is_empty() or best_candidates.is_empty(): + continue + + # Add a small random lottery to break ties deterministically for + # each candidate set. + lottery = pl.Series("lottery", self.random.random(best_candidates.height)) + best_candidates = best_candidates.with_columns(lottery) + + # winners columns: + # ┌──────────┬──────────────────┬──────────────────┬────────┬────────┬──────┬──────────────┬─────────┐ + # │ agent_id ┆ dim_0_candidate ┆ dim_1_candidate ┆ sugar ┆ radius ┆ rank ┆ current_rank │ lottery │ + # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ f64 │ + # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════╪══════════════╪═════════╡ + winners = ( + best_candidates.sort(["dim_0_candidate", "dim_1_candidate", "lottery"]) + .group_by(["dim_0_candidate", "dim_1_candidate"], maintain_order=True) + .first() + ) + + assigned = pl.concat( + [ + assigned, + winners.select( + [ + "agent_id", + pl.col("dim_0_candidate"), + pl.col("dim_1_candidate"), + ] + ), + ], + how="vertical", + ) + taken = pl.concat( + [ + taken, + winners.select(["dim_0_candidate", "dim_1_candidate"]), + ], + how="vertical", + ) + + winner_ids = winners.select("agent_id") + unresolved = unresolved.join(winner_ids, on="agent_id", how="anti") + if unresolved.is_empty(): + break + + # loser candidates columns mirror best_candidates (minus winners). + losers = best_candidates.join(winner_ids, on="agent_id", how="anti") + if losers.is_empty(): + continue + + # loser_updates columns (after select): + # ┌──────────┬───────────┐ + # │ agent_id ┆ next_rank │ + # │ --- ┆ --- │ + # │ u64 ┆ i64 │ + # ╞══════════╪═══════════╡ + loser_updates = ( + losers.select( + "agent_id", + (pl.col("rank") + 1).cast(pl.Int64).alias("next_rank"), + ) + .join(max_rank, on="agent_id", how="left") + .with_columns( + pl.min_horizontal(pl.col("next_rank"), pl.col("max_rank")).alias( + "next_rank" + ) + ) + .select(["agent_id", "next_rank"]) + ) + + # Promote losers' current_rank (if any) and continue. + # unresolved (updated) retains columns agent_id/current_rank. + unresolved = ( + unresolved.join(loser_updates, on="agent_id", how="left") + .with_columns( + pl.when(pl.col("next_rank").is_not_null()) + .then(pl.col("next_rank")) + .otherwise(pl.col("current_rank")) + .alias("current_rank") + ) + .drop("next_rank") + ) + + return assigned + + +__all__ = [ + "AntsBase", + "AntsParallel", +] + diff --git a/examples/sugarscape_ig/backend_frames/model.py b/examples/sugarscape_ig/backend_frames/model.py new file mode 100644 index 00000000..da4e1250 --- /dev/null +++ b/examples/sugarscape_ig/backend_frames/model.py @@ -0,0 +1,475 @@ +"""Mesa-frames implementation of Sugarscape IG with Typer CLI. + +This mirrors the advanced tutorial in docs/general/user-guide/3_advanced_tutorial.py +and exposes a simple CLI to run the parallel update variant, save CSVs, and plot +the Gini trajectory. +""" + +from __future__ import annotations + +from datetime import datetime, timezone +from pathlib import Path +from typing import Annotated +from time import perf_counter + +import numpy as np +import polars as pl +import typer + +from mesa_frames import DataCollector, Grid, Model +from examples.utils import FramesSimulationResult +from examples.plotting import plot_model_metrics + +from examples.sugarscape_ig.backend_frames.agents import AntsBase, AntsParallel + + +# Model-level reporters + + +def gini(model: Model) -> float: + """Compute the Gini coefficient of agent sugar holdings. + + The function reads the primary agent set from ``model.sets[0]`` and + computes the population Gini coefficient on the ``sugar`` column. The + implementation is robust to empty sets and zero-total sugar. + + Parameters + ---------- + model : Model + The simulation model that contains agent sets. The primary agent set + is expected to be at ``model.sets[0]`` and to expose a Polars DataFrame + under ``.df`` with a ``sugar`` column. + + Returns + ------- + float + Gini coefficient in the range [0, 1] if defined, ``0.0`` when the + total sugar is zero, and ``nan`` when the agent set is empty or too + small to measure. + """ + if len(model.sets) == 0: + return float("nan") + + primary_set = model.sets[0] + if len(primary_set) == 0: + return float("nan") + + sugar = primary_set.df["sugar"].to_numpy().astype(np.float64) + + if sugar.size == 0: + return float("nan") + sorted_vals = np.sort(sugar.astype(np.float64)) + n = sorted_vals.size + if n == 0: + return float("nan") + cumulative = np.cumsum(sorted_vals) + total = cumulative[-1] + if total == 0: + return 0.0 + index = np.arange(1, n + 1, dtype=np.float64) + return float((2.0 * np.dot(index, sorted_vals) / (n * total)) - (n + 1) / n) + + +def corr_sugar_metabolism(model: Model) -> float: + """Pearson correlation between agent sugar and metabolism. + + This reporter extracts the ``sugar`` and ``metabolism`` columns from the + primary agent set and returns their Pearson correlation coefficient. When + the agent set is empty or contains insufficient variation the function + returns ``nan``. + + Parameters + ---------- + model : Model + The simulation model that contains agent sets. The primary agent set + is expected to be at ``model.sets[0]`` and provide a Polars DataFrame + with ``sugar`` and ``metabolism`` columns. + + Returns + ------- + float + Pearson correlation coefficient between sugar and metabolism, or + ``nan`` when the correlation is undefined (empty set or constant + values). + """ + if len(model.sets) == 0: + return float("nan") + + primary_set = model.sets[0] + if len(primary_set) == 0: + return float("nan") + + agent_df = primary_set.df + sugar = agent_df["sugar"].to_numpy().astype(np.float64) + metabolism = agent_df["metabolism"].to_numpy().astype(np.float64) + return _safe_corr(sugar, metabolism) + + +def corr_sugar_vision(model: Model) -> float: + """Pearson correlation between agent sugar and vision. + + Extracts the ``sugar`` and ``vision`` columns from the primary agent set + and returns their Pearson correlation coefficient. If the reporter cannot + compute a meaningful correlation (for example, when the agent set is + empty or values are constant) it returns ``nan``. + + Parameters + ---------- + model : Model + The simulation model that contains agent sets. The primary agent set + is expected to be at ``model.sets[0]`` and provide a Polars DataFrame + with ``sugar`` and ``vision`` columns. + + Returns + ------- + float + Pearson correlation coefficient between sugar and vision, or ``nan`` + when the correlation is undefined. + """ + if len(model.sets) == 0: + return float("nan") + + primary_set = model.sets[0] + if len(primary_set) == 0: + return float("nan") + + agent_df = primary_set.df + sugar = agent_df["sugar"].to_numpy().astype(np.float64) + vision = agent_df["vision"].to_numpy().astype(np.float64) + return _safe_corr(sugar, vision) + + +def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: + """Safely compute Pearson correlation between two 1-D arrays. + + This helper guards against degenerate inputs (too few observations or + constant arrays) which would make the Pearson correlation undefined or + numerically unstable. When a valid correlation can be computed the + function returns a Python float. + + Parameters + ---------- + x : np.ndarray + One-dimensional numeric array containing the first variable to + correlate. + y : np.ndarray + One-dimensional numeric array containing the second variable to + correlate. + + Returns + ------- + float + Pearson correlation coefficient as a Python float, or ``nan`` if the + correlation is undefined (fewer than 2 observations or constant + inputs). + """ + if x.size < 2 or y.size < 2: + return float("nan") + if np.allclose(x, x[0]) or np.allclose(y, y[0]): + return float("nan") + return float(np.corrcoef(x, y)[0, 1]) + + +class Sugarscape(Model): + """Minimal Sugarscape model used throughout the tutorial. + + This class wires together a grid that stores ``sugar`` per cell, an + agent set implementation (passed in as ``agent_type``), and a + data collector that records model- and agent-level statistics. + + The model's responsibilities are to: + - create the sugar landscape (cells with current and maximum sugar) + - create and place agents on the grid + - advance the sugar regrowth rule each step + - run the model for a fixed number of steps and collect data + + Parameters + ---------- + agent_type : type[AntsBase] + The :class:`AgentSet` subclass implementing the movement rules + (sequential, numba-accelerated, or parallel). + n_agents : int + Number of agents to create and place on the grid. + width : int + Grid width (number of columns). + height : int + Grid height (number of rows). + max_sugar : int, optional + Upper bound for the randomly initialised sugar values on the grid, + by default 4. + seed : int | None, optional + RNG seed to make runs reproducible across variants, by default None. + + Notes + ----- + The grid uses a von Neumann neighbourhood and capacity 1 (at most one + agent per cell). Both the sugar landscape and initial agent traits are + drawn from ``self.random`` so different movement variants can be + instantiated with identical initial conditions by passing the same seed. + """ + + def __init__( + self, + agent_type: type[AntsBase], + n_agents: int, + *, + width: int, + height: int, + max_sugar: int = 4, + seed: int | None = None, + results_dir: Path | None = None, + ) -> None: + if n_agents > width * height: + raise ValueError( + "Cannot place more agents than grid cells when capacity is 1." + ) + super().__init__(seed) + + # 1. Let's create the sugar grid and set up the space + + sugar_grid_df = self._generate_sugar_grid(width, height, max_sugar) + self.space = Grid( + self, [width, height], neighborhood_type="von_neumann", capacity=1 + ) + self.space.set_cells(sugar_grid_df) + self._max_sugar = sugar_grid_df.select(["dim_0", "dim_1", "max_sugar"]) + + # 2. Now we create the agents and place them on the grid + + agent_frame = self._generate_agent_frame(n_agents) + main_set = agent_type(self, agent_frame) + self.sets += main_set + self.space.place_to_empty(self.sets) + + # 3. Finally we set up the data collector + storage_uri = str(results_dir) if results_dir is not None else None + self.datacollector = DataCollector( + model=self, + model_reporters={ + "mean_sugar": lambda m: 0.0 + if len(m.sets[0]) == 0 + else float(m.sets[0].df["sugar"].mean()), + "total_sugar": lambda m: float(m.sets[0].df["sugar"].sum()) + if len(m.sets[0]) + else 0.0, + "agents_alive": lambda m: float(len(m.sets[0])) if len(m.sets) else 0.0, + "gini": gini, + "corr_sugar_metabolism": corr_sugar_metabolism, + "corr_sugar_vision": corr_sugar_vision, + }, + agent_reporters={"traits": ["sugar", "metabolism", "vision"]}, + storage="csv", + storage_uri=storage_uri, + ) + self.datacollector.collect() + + def _generate_sugar_grid( + self, width: int, height: int, max_sugar: int + ) -> pl.DataFrame: + """Generate a random sugar grid. + + Parameters + ---------- + width : int + Grid width (number of columns). + height : int + Grid height (number of rows). + max_sugar : int + Maximum sugar value (inclusive) for each cell. + + Returns + ------- + pl.DataFrame + DataFrame with columns ``dim_0``, ``dim_1``, ``sugar`` (current + amount) and ``max_sugar`` (regrowth target). + """ + sugar_vals = self.random.integers( + 0, max_sugar + 1, size=(width, height), dtype=np.int64 + ) + dim_0 = pl.Series("dim_0", pl.arange(width, eager=True)).to_frame() + dim_1 = pl.Series("dim_1", pl.arange(height, eager=True)).to_frame() + return dim_0.join(dim_1, how="cross").with_columns( + sugar=sugar_vals.flatten(), max_sugar=sugar_vals.flatten() + ) + + def _generate_agent_frame(self, n_agents: int) -> pl.DataFrame: + """Create the initial agent frame populated with agent traits. + + Parameters + ---------- + n_agents : int + Number of agents to create. + + Returns + ------- + pl.DataFrame + DataFrame with columns ``sugar``, ``metabolism`` and ``vision`` + (integer values) for each agent. + """ + rng = self.random + return pl.DataFrame( + { + "sugar": rng.integers(6, 25, size=n_agents, dtype=np.int64), + "metabolism": rng.integers(2, 5, size=n_agents, dtype=np.int64), + "vision": rng.integers(1, 6, size=n_agents, dtype=np.int64), + } + ) + + def step(self) -> None: + """Advance the model by one step. + + Notes + ----- + The per-step ordering is important and this tutorial implements the + classic Sugarscape "instant growback": agents move and eat first, + and then empty cells are refilled immediately (move -> eat -> regrow + -> collect). + """ + if len(self.sets[0]) == 0: + self.running = False + return + self.sets[0].step() + self._advance_sugar_field() + self.datacollector.collect() + if len(self.sets[0]) == 0: + self.running = False + + def run(self, steps: int) -> None: + """Run the model for a fixed number of steps. + + Parameters + ---------- + steps : int + Maximum number of steps to run. The model may terminate earlier if + ``self.running`` is set to ``False`` (for example, when all agents + have died). + """ + for _ in range(steps): + if not self.running: + break + self.step() + + def _advance_sugar_field(self) -> None: + """Apply the instant-growback sugar regrowth rule. + + Empty cells (no agent present) are refilled to their ``max_sugar`` + value. Cells that are occupied are set to zero because agents harvest + the sugar when they eat. The method uses vectorised DataFrame joins + and writes to keep the operation efficient. + """ + empty_cells = self.space.empty_cells + if not empty_cells.is_empty(): + # Look up the maximum sugar for each empty cell and restore it. + refresh = empty_cells.join( + self._max_sugar, on=["dim_0", "dim_1"], how="left" + ) + self.space.set_cells(empty_cells, {"sugar": refresh["max_sugar"]}) + full_cells = self.space.full_cells + if not full_cells.is_empty(): + # Occupied cells have just been harvested; set their sugar to 0. + zeros = pl.Series(np.zeros(len(full_cells), dtype=np.int64)) + self.space.set_cells(full_cells, {"sugar": zeros}) + + +def simulate( + *, + agents: int, + steps: int, + width: int, + height: int, + max_sugar: int = 4, + seed: int | None = None, + results_dir: Path | None = None, +) -> FramesSimulationResult: + model = Sugarscape( + agent_type=AntsParallel, + n_agents=agents, + width=width, + height=height, + max_sugar=max_sugar, + seed=seed, + results_dir=results_dir, + ) + model.run(steps) + return FramesSimulationResult(datacollector=model.datacollector) + + +app = typer.Typer(add_completion=False) + + +@app.command() +def run( + agents: Annotated[int, typer.Option(help="Number of agents to simulate.")] = 400, + width: Annotated[int, typer.Option(help="Grid width (columns).")] = 40, + height: Annotated[int, typer.Option(help="Grid height (rows).")] = 40, + steps: Annotated[int, typer.Option(help="Number of model steps to run.")] = 60, + max_sugar: Annotated[int, typer.Option(help="Maximum sugar per cell.")] = 4, + seed: Annotated[int | None, typer.Option(help="Optional RNG seed.")] = None, + plot: Annotated[bool, typer.Option(help="Render Seaborn plots.")] = True, + save_results: Annotated[bool, typer.Option(help="Persist metrics as CSV.")] = True, + results_dir: Annotated[ + Path | None, + typer.Option( + help="Directory to write CSV results and plots into. If omitted a timestamped subdir under `results/` is used." + ), + ] = None, +) -> None: + typer.echo( + f"Running Sugarscape IG (mesa-frames, parallel) with {agents} agents on {width}x{height} for {steps} steps" + ) + timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") + if results_dir is None: + results_dir = ( + Path(__file__).resolve().parent / "results" / timestamp + ).resolve() + results_dir.mkdir(parents=True, exist_ok=True) + + start_time = perf_counter() + result = simulate( + agents=agents, + steps=steps, + width=width, + height=height, + max_sugar=max_sugar, + seed=seed, + results_dir=results_dir, + ) + typer.echo(f"Simulation complete in {perf_counter() - start_time:.2f} seconds") + + model_metrics = result.datacollector.data["model"].drop(['seed', 'batch']) + typer.echo(f"Metrics in the final 5 steps: {model_metrics.tail(5)}") + + if save_results: + result.datacollector.flush() + + if plot: + # Create a subdirectory for per-metric plots under the timestamped + # results directory. For each column in the model metrics (except + # the step index) create a single-metric DataFrame and call the + # shared plotting helper to export light/dark PNG+SVG variants. + plots_dir = results_dir / "plots" + plots_dir.mkdir(parents=True, exist_ok=True) + + # Determine which columns to plot (preserve 'step' if present). + value_cols = [c for c in model_metrics.columns if c != "step"] + for col in value_cols: + stem = f"{col}_{timestamp}" + single = model_metrics.select(["step", col]) if "step" in model_metrics.columns else model_metrics.select([col]) + plot_model_metrics( + single, + plots_dir, + stem, + title=f"Sugarscape IG — {col.capitalize()}", + subtitle=f"mesa-frames backend; seed={result.datacollector.seed}", + agents=agents, + steps=steps, + ) + + typer.echo(f"Saved plots under {plots_dir}") + + typer.echo(f"Saved CSV results under {results_dir}") + + +if __name__ == "__main__": + app() + From 6a251ce4512731913433eda8cf661bcb7396fa85 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 7 Oct 2025 13:18:03 +0200 Subject: [PATCH 248/329] fix: remove extras from typer dependency in development and documentation requirements --- uv.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uv.lock b/uv.lock index cc55da48..dfd4cc46 100644 --- a/uv.lock +++ b/uv.lock @@ -1305,7 +1305,7 @@ dev = [ { name = "sphinx-copybutton", specifier = ">=0.5.2" }, { name = "sphinx-design", specifier = ">=0.6.1" }, { name = "sphinx-rtd-theme", specifier = ">=3.0.2" }, - { name = "typer", extras = ["all"], specifier = ">=0.9.0" }, + { name = "typer", specifier = ">=0.9.0" }, ] docs = [ { name = "autodocsumm", specifier = ">=0.2.14" }, @@ -1325,7 +1325,7 @@ docs = [ { name = "sphinx-copybutton", specifier = ">=0.5.2" }, { name = "sphinx-design", specifier = ">=0.6.1" }, { name = "sphinx-rtd-theme", specifier = ">=3.0.2" }, - { name = "typer", extras = ["all"], specifier = ">=0.9.0" }, + { name = "typer", specifier = ">=0.9.0" }, ] test = [ { name = "beartype", specifier = ">=0.21.0" }, From 9db77800d50f296e8e134ebbdbc0e6c3fac05bd5 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 7 Oct 2025 13:26:15 +0200 Subject: [PATCH 249/329] feat: add Sugarscape IG model with Typer CLI for simulation and data collection --- examples/sugarscape_ig/backend_mesa/agents.py | 82 +++++++ examples/sugarscape_ig/backend_mesa/model.py | 229 ++++++++++++++++++ 2 files changed, 311 insertions(+) create mode 100644 examples/sugarscape_ig/backend_mesa/agents.py create mode 100644 examples/sugarscape_ig/backend_mesa/model.py diff --git a/examples/sugarscape_ig/backend_mesa/agents.py b/examples/sugarscape_ig/backend_mesa/agents.py new file mode 100644 index 00000000..d6a7fd16 --- /dev/null +++ b/examples/sugarscape_ig/backend_mesa/agents.py @@ -0,0 +1,82 @@ +"""Mesa agents for the Sugarscape IG example (sequential/asynchronous update). + +Implements the movement rule (sense along cardinal axes up to `vision`, choose +highest-sugar cell with tie-breakers by distance and coordinates). Eating, +starvation, and regrowth are orchestrated by the model to preserve the order +move -> eat -> regrow -> collect, mirroring the tutorial schedule. +""" + +from __future__ import annotations + +from typing import Tuple + +import mesa + + +class AntAgent(mesa.Agent): + """Sugarscape ant with sugar/metabolism/vision traits and movement.""" + + def __init__( + self, + model: "Sugarscape", + *, + sugar: int, + metabolism: int, + vision: int, + ) -> None: + super().__init__(model) + self.sugar = int(sugar) + self.metabolism = int(metabolism) + self.vision = int(vision) + + # --- Movement helpers (sequential/asynchronous) --- + + def _visible_cells(self, origin: Tuple[int, int]) -> list[Tuple[int, int]]: + x0, y0 = origin + width, height = self.model.width, self.model.height + cells: list[Tuple[int, int]] = [origin] + for step in range(1, self.vision + 1): + if x0 + step < width: + cells.append((x0 + step, y0)) + if x0 - step >= 0: + cells.append((x0 - step, y0)) + if y0 + step < height: + cells.append((x0, y0 + step)) + if y0 - step >= 0: + cells.append((x0, y0 - step)) + return cells + + def _choose_best_cell(self, origin: Tuple[int, int]) -> Tuple[int, int]: + # Highest sugar; tie-break by Manhattan distance from origin; then coords. + best_cell = origin + best_sugar = int(self.model.sugar_current[origin[0], origin[1]]) + best_distance = 0 + ox, oy = origin + for cx, cy in self._visible_cells(origin): + # Block occupied cells except the origin (own cell allowed as fallback). + if (cx, cy) != origin and not self.model.grid.is_cell_empty((cx, cy)): + continue + sugar_here = int(self.model.sugar_current[cx, cy]) + distance = abs(cx - ox) + abs(cy - oy) + better = False + if sugar_here > best_sugar: + better = True + elif sugar_here == best_sugar: + if distance < best_distance: + better = True + elif distance == best_distance and (cx, cy) < best_cell: + better = True + if better: + best_cell = (cx, cy) + best_sugar = sugar_here + best_distance = distance + return best_cell + + def move(self) -> None: + best = self._choose_best_cell(self.pos) + if best != self.pos: + self.model.grid.move_agent(self, best) + + +__all__ = ["AntAgent"] + diff --git a/examples/sugarscape_ig/backend_mesa/model.py b/examples/sugarscape_ig/backend_mesa/model.py new file mode 100644 index 00000000..e0d9bba2 --- /dev/null +++ b/examples/sugarscape_ig/backend_mesa/model.py @@ -0,0 +1,229 @@ +"""Mesa implementation of Sugarscape IG with Typer CLI (sequential update). + +Follows the same structure as the Boltzmann Mesa example: `simulate()` and a +`run` CLI command that saves CSV results and plots the Gini trajectory. The +model updates in the order move -> eat -> regrow -> collect, matching the +tutorial schedule. +""" + +from __future__ import annotations + +from datetime import datetime, timezone +from pathlib import Path +from typing import Iterable, Annotated +from time import perf_counter + +import mesa +from mesa.datacollection import DataCollector +from mesa.space import SingleGrid +import numpy as np +import pandas as pd +import polars as pl +import typer + +from examples.utils import MesaSimulationResult +from examples.plotting import plot_model_metrics + +from examples.sugarscape_ig.backend_mesa.agents import AntAgent + + +def gini(values: Iterable[float]) -> float: + array = np.fromiter(values, dtype=float) + if array.size == 0: + return float("nan") + if np.allclose(array, 0.0): + return 0.0 + if np.allclose(array, array[0]): + return 0.0 + sorted_vals = np.sort(array) + n = sorted_vals.size + cumulative = np.cumsum(sorted_vals) + total = cumulative[-1] + if total == 0: + return 0.0 + index = np.arange(1, n + 1, dtype=float) + return float((2.0 * np.dot(index, sorted_vals) / (n * total)) - (n + 1) / n) + + +class Sugarscape(mesa.Model): + def __init__( + self, + agents: int, + *, + width: int, + height: int, + max_sugar: int = 4, + seed: int | None = None, + ) -> None: + super().__init__() + if seed is None: + seed = self.random.randint(0, np.iinfo(np.int32).max) + self.reset_randomizer(seed) + self.width = int(width) + self.height = int(height) + + # Sugar field (current and max) as 2D arrays shaped (width, height) + numpy_rng = np.random.default_rng(seed) + self.sugar_max = numpy_rng.integers(0, max_sugar + 1, size=(width, height), dtype=np.int64) + self.sugar_current = self.sugar_max.copy() + + # Grid with capacity 1 per cell + self.grid = SingleGrid(width, height, torus=False) + + # Agents (Python list, manually shuffled/iterated for speed) + self.agent_list: list[AntAgent] = [] + # Place all agents on empty cells; also draw initial traits from model RNG + placed = 0 + while placed < agents: + x = int(self.random.randrange(0, width)) + y = int(self.random.randrange(0, height)) + if self.grid.is_cell_empty((x, y)): + a = AntAgent( + self, + sugar=int(self.random.randint(6, 25)), + metabolism=int(self.random.randint(2, 5)), + vision=int(self.random.randint(1, 6)), + ) + self.grid.place_agent(a, (x, y)) + self.agent_list.append(a) + placed += 1 + + self.datacollector = DataCollector( + model_reporters={ + "gini": lambda m: gini(a.sugar for a in m.agent_list), + "seed": lambda m: seed, + } + ) + self.datacollector.collect(self) + + # --- Scheduling --- + + def _harvest_and_survive(self) -> None: + survivors: list[AntAgent] = [] + for a in self.agent_list: + x, y = a.pos + a.sugar += int(self.sugar_current[x, y]) + a.sugar -= a.metabolism + # Harvested cells are emptied now; they wil\l be refilled if empty. + self.sugar_current[x, y] = 0 + if a.sugar > 0: + survivors.append(a) + else: + # Remove dead agent from grid + self.grid.remove_agent(a) + self.agent_list = survivors + + def _regrow(self) -> None: + # Empty cells regrow to max; occupied cells set to 0 (already zeroed on harvest) + for x in range(self.width): + for y in range(self.height): + if self.grid.is_cell_empty((x, y)): + self.sugar_current[x, y] = self.sugar_max[x, y] + else: + self.sugar_current[x, y] = 0 + + def step(self) -> None: + # Randomise order, move sequentially, then eat/starve, regrow, collect + self.random.shuffle(self.agent_list) + for a in self.agent_list: + a.move() + self._harvest_and_survive() + self._regrow() + self.datacollector.collect(self) + if not self.agent_list: + self.running = False + + def run(self, steps: int) -> None: + for _ in range(steps): + if not getattr(self, "running", True): + break + self.step() + + +def simulate( + *, + agents: int, + steps: int, + width: int, + height: int, + max_sugar: int = 4, + seed: int | None = None, +) -> MesaSimulationResult: + model = Sugarscape(agents, width=width, height=height, max_sugar=max_sugar, seed=seed) + model.run(steps) + return MesaSimulationResult(datacollector=model.datacollector) + + +app = typer.Typer(add_completion=False) + + +@app.command() +def run( + agents: Annotated[int, typer.Option(help="Number of agents to simulate.")] = 400, + width: Annotated[int, typer.Option(help="Grid width (columns).")] = 40, + height: Annotated[int, typer.Option(help="Grid height (rows).")] = 40, + steps: Annotated[int, typer.Option(help="Number of model steps to run.")] = 60, + max_sugar: Annotated[int, typer.Option(help="Maximum sugar per cell.")] = 4, + seed: Annotated[int | None, typer.Option(help="Optional RNG seed.")] = None, + plot: Annotated[bool, typer.Option(help="Render plots.")] = True, + save_results: Annotated[bool, typer.Option(help="Persist metrics as CSV.")] = True, + results_dir: Annotated[ + Path | None, + typer.Option( + help=( + "Directory to write CSV results and plots into. If omitted a " + "timestamped subdir under `results/` is used." + ) + ), + ] = None, +) -> None: + typer.echo( + f"Running Sugarscape IG (mesa, sequential) with {agents} agents on {width}x{height} for {steps} steps" + ) + + # Resolve output folder + timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") + if results_dir is None: + results_dir = (Path(__file__).resolve().parent / "results" / timestamp).resolve() + results_dir.mkdir(parents=True, exist_ok=True) + + start_time = perf_counter() + result = simulate(agents=agents, steps=steps, width=width, height=height, max_sugar=max_sugar, seed=seed) + typer.echo(f"Simulation completed in {perf_counter() - start_time:.3f} seconds") + dc = result.datacollector + + # Extract metrics using DataCollector API + model_pd = dc.get_model_vars_dataframe().reset_index().rename(columns={"index": "step"}) + seed_val = model_pd["seed"].iloc[0] + model_pd = model_pd[["step", "gini"]] + + # Show tail for quick inspection + typer.echo(f"Metrics in the final 5 steps:\n{model_pd.tail(5).to_string(index=False)}") + + # Save CSV + if save_results: + csv_path = results_dir / "model.csv" + model_pd.to_csv(csv_path, index=False) + + # Plot (convert to Polars to reuse example plotting helper) + if plot and not model_pd.empty: + model_pl = pl.from_pandas(model_pd) + stem = f"gini_{timestamp}" + plot_model_metrics( + model_pl, + results_dir, + stem, + title="Sugarscape IG — Gini", + subtitle=f"mesa backend; seed={seed_val}", + agents=agents, + steps=steps, + ) + typer.echo(f"Saved plots under {results_dir}") + + if save_results: + typer.echo(f"Saved CSV results under {results_dir}") + + +if __name__ == "__main__": + app() + From 543582fea40fad5a7426d5eb5652ac1f5b77cc4f Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 7 Oct 2025 18:15:58 +0200 Subject: [PATCH 250/329] feat: enhance model metrics extraction and plotting in Sugarscape IG simulation --- examples/sugarscape_ig/backend_mesa/model.py | 53 ++++++++++++-------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/examples/sugarscape_ig/backend_mesa/model.py b/examples/sugarscape_ig/backend_mesa/model.py index e0d9bba2..e02caf3d 100644 --- a/examples/sugarscape_ig/backend_mesa/model.py +++ b/examples/sugarscape_ig/backend_mesa/model.py @@ -194,34 +194,47 @@ def run( # Extract metrics using DataCollector API model_pd = dc.get_model_vars_dataframe().reset_index().rename(columns={"index": "step"}) - seed_val = model_pd["seed"].iloc[0] - model_pd = model_pd[["step", "gini"]] + # Keep the full model metrics (step + any model reporters) + seed_val = None + if "seed" in model_pd.columns and not model_pd.empty: + seed_val = model_pd["seed"].iloc[0] - # Show tail for quick inspection - typer.echo(f"Metrics in the final 5 steps:\n{model_pd.tail(5).to_string(index=False)}") + # Show tail for quick inspection (exclude seed column from display) + display_pd = model_pd.drop(columns=["seed"]) if "seed" in model_pd.columns else model_pd + typer.echo(f"Metrics in the final 5 steps:\n{display_pd.tail(5).to_string(index=False)}") - # Save CSV + # Save CSV (full model metrics) if save_results: csv_path = results_dir / "model.csv" model_pd.to_csv(csv_path, index=False) - # Plot (convert to Polars to reuse example plotting helper) + # Plot per-metric similar to the backend_frames example: create a + # `plots/` subdirectory and generate one figure per model metric column if plot and not model_pd.empty: - model_pl = pl.from_pandas(model_pd) - stem = f"gini_{timestamp}" - plot_model_metrics( - model_pl, - results_dir, - stem, - title="Sugarscape IG — Gini", - subtitle=f"mesa backend; seed={seed_val}", - agents=agents, - steps=steps, - ) - typer.echo(f"Saved plots under {results_dir}") + plots_dir = results_dir / "plots" + plots_dir.mkdir(parents=True, exist_ok=True) + + # Determine which columns to plot (preserve 'step' if present). + value_cols = [c for c in model_pd.columns if c != "step"] + for col in value_cols: + stem = f"{col}_{timestamp}" + single = model_pd[["step", col]] if "step" in model_pd.columns else model_pd[[col]] + # Convert the single-column pandas DataFrame to Polars for the + # shared plotting helper. + single_pl = pl.from_pandas(single) + plot_model_metrics( + single_pl, + plots_dir, + stem, + title=f"Sugarscape IG — {col.capitalize()}", + subtitle=f"mesa backend; seed={seed_val}", + agents=agents, + steps=steps, + ) - if save_results: - typer.echo(f"Saved CSV results under {results_dir}") + typer.echo(f"Saved plots under {plots_dir}") + + typer.echo(f"Saved CSV results under {results_dir}") if __name__ == "__main__": From 53d940c7b17436bb5cf0293f55effc2684f1028d Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 7 Oct 2025 18:19:07 +0200 Subject: [PATCH 251/329] feat: add correlation functions for sugar metabolism and vision; enhance data collection in Sugarscape model --- examples/sugarscape_ig/backend_mesa/model.py | 37 +++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/examples/sugarscape_ig/backend_mesa/model.py b/examples/sugarscape_ig/backend_mesa/model.py index e02caf3d..e1cca782 100644 --- a/examples/sugarscape_ig/backend_mesa/model.py +++ b/examples/sugarscape_ig/backend_mesa/model.py @@ -26,6 +26,31 @@ from examples.sugarscape_ig.backend_mesa.agents import AntAgent +def _safe_corr(x: np.ndarray, y: np.ndarray) -> float: + """Safely compute Pearson correlation between two 1-D arrays. + + Mirrors the Frames helper: returns nan for degenerate inputs. + """ + x = np.asarray(x, dtype=float) + y = np.asarray(y, dtype=float) + if x.size < 2 or y.size < 2: + return float("nan") + if np.allclose(x, x[0]) or np.allclose(y, y[0]): + return float("nan") + return float(np.corrcoef(x, y)[0, 1]) + + +def corr_sugar_metabolism(model: "Sugarscape") -> float: + sugars = np.fromiter((a.sugar for a in model.agent_list), dtype=float) + mets = np.fromiter((a.metabolism for a in model.agent_list), dtype=float) + return _safe_corr(sugars, mets) + + +def corr_sugar_vision(model: "Sugarscape") -> float: + sugars = np.fromiter((a.sugar for a in model.agent_list), dtype=float) + vision = np.fromiter((a.vision for a in model.agent_list), dtype=float) + return _safe_corr(sugars, vision) + def gini(values: Iterable[float]) -> float: array = np.fromiter(values, dtype=float) @@ -88,11 +113,21 @@ def __init__( self.agent_list.append(a) placed += 1 + # Model-level reporters mirroring the Frames implementation so CSVs + # are comparable across backends. self.datacollector = DataCollector( model_reporters={ + "mean_sugar": lambda m: float(np.mean([a.sugar for a in m.agent_list])) if m.agent_list else 0.0, + "total_sugar": lambda m: float(sum(a.sugar for a in m.agent_list)) if m.agent_list else 0.0, + "agents_alive": lambda m: float(len(m.agent_list)), "gini": lambda m: gini(a.sugar for a in m.agent_list), + "corr_sugar_metabolism": lambda m: corr_sugar_metabolism(m), + "corr_sugar_vision": lambda m: corr_sugar_vision(m), "seed": lambda m: seed, - } + }, + agent_reporters={ + "traits": lambda a: {"sugar": a.sugar, "metabolism": a.metabolism, "vision": a.vision} + }, ) self.datacollector.collect(self) From 6348ee7a984f90a03579234c139376fd208a71d5 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 7 Oct 2025 18:40:36 +0200 Subject: [PATCH 252/329] fix: address missing return statements in correlation and Gini functions --- examples/sugarscape_ig/backend_mesa/model.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/sugarscape_ig/backend_mesa/model.py b/examples/sugarscape_ig/backend_mesa/model.py index e1cca782..c2e05ee5 100644 --- a/examples/sugarscape_ig/backend_mesa/model.py +++ b/examples/sugarscape_ig/backend_mesa/model.py @@ -250,19 +250,25 @@ def run( plots_dir.mkdir(parents=True, exist_ok=True) # Determine which columns to plot (preserve 'step' if present). - value_cols = [c for c in model_pd.columns if c != "step"] + # Exclude 'seed' from plots so we don't create a chart for a constant + # model reporter; keep 'seed' in the CSV/dataframe for reproducibility. + value_cols = [c for c in model_pd.columns if c not in {"step", "seed"}] for col in value_cols: stem = f"{col}_{timestamp}" single = model_pd[["step", col]] if "step" in model_pd.columns else model_pd[[col]] # Convert the single-column pandas DataFrame to Polars for the # shared plotting helper. single_pl = pl.from_pandas(single) + # Omit seed from subtitle/plot metadata to avoid leaking a constant + # value into the figure (it remains in the saved CSV). If you want + # to include the seed in filenames or external metadata, prefer + # annotating the output folder or README instead. plot_model_metrics( single_pl, plots_dir, stem, - title=f"Sugarscape IG — {col.capitalize()}", - subtitle=f"mesa backend; seed={seed_val}", + title=f"Sugarscape IG  {col.capitalize()}", + subtitle="mesa backend", agents=agents, steps=steps, ) From 2ecc7247dc42f182db513d6c9628a3f7c97cc7ed Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 7 Oct 2025 18:42:30 +0200 Subject: [PATCH 253/329] fix: update datetime import and adjust agent type in CLI benchmark function --- benchmarks/cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index c0b9355d..bd0bacee 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -3,7 +3,7 @@ from __future__ import annotations from dataclasses import dataclass -from datetime import datetime +from datetime import datetime, timezone from pathlib import Path from time import perf_counter from typing import Literal, Annotated, Protocol, Optional @@ -149,7 +149,7 @@ def run( help="Models to benchmark: boltzmann, sugarscape, or all", callback=_parse_models )] = "all", - agents: Annotated[list[int], typer.Option( + agents: Annotated[str, typer.Option( help="Agent count or range (start:stop:step)", callback=_parse_agents )] = "1000:5000:1000", @@ -170,7 +170,7 @@ def run( ) -> None: """Run performance benchmarks for the models models.""" rows: list[dict[str, object]] = [] - timestamp = datetime.now(datetime.timezone.utc).strftime("%Y%m%d_%H%M%S") + timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") for model in models: config = MODELS[model] typer.echo(f"Benchmarking {model} with agents {agents}") From 542affd47cd8f78f74632e6fc5d31e50647e0549 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 7 Oct 2025 18:52:37 +0200 Subject: [PATCH 254/329] fix: update title format in run function for Sugarscape IG --- examples/sugarscape_ig/backend_mesa/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sugarscape_ig/backend_mesa/model.py b/examples/sugarscape_ig/backend_mesa/model.py index c2e05ee5..3fd39af0 100644 --- a/examples/sugarscape_ig/backend_mesa/model.py +++ b/examples/sugarscape_ig/backend_mesa/model.py @@ -267,7 +267,7 @@ def run( single_pl, plots_dir, stem, - title=f"Sugarscape IG  {col.capitalize()}", + title=f"Sugarscape IG - {col.capitalize()}", subtitle="mesa backend", agents=agents, steps=steps, From 3409e95c033b10b7987850a9306c3fb8df3a6141 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 14 Oct 2025 20:01:08 +0200 Subject: [PATCH 255/329] fix: adjust storage handling in DataCollector for benchmarks without results_dir --- benchmarks/cli.py | 19 +++++++++++++++++-- examples/boltzmann_wealth/backend_frames.py | 13 +++++++++++-- .../sugarscape_ig/backend_frames/model.py | 11 +++++++++-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index bd0bacee..1eb99a8c 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -8,6 +8,7 @@ from time import perf_counter from typing import Literal, Annotated, Protocol, Optional +import math import matplotlib.pyplot as plt import polars as pl import seaborn as sns @@ -49,11 +50,25 @@ class ModelConfig: backends=[ Backend( name="mesa", - runner=sugarscape_mesa.simulate, + runner=lambda agents, steps, seed=None: sugarscape_mesa.simulate( + agents=agents, + steps=steps, + width=int(max(20, math.ceil((agents) ** 0.5) * 2)), + height=int(max(20, math.ceil((agents) ** 0.5) * 2)), + seed=seed, + ), ), Backend( name="frames", - runner=sugarscape_frames.simulate, + # Benchmarks expect a runner signature (agents:int, steps:int, seed:int|None) + # Sugarscape frames simulate requires width/height; choose square close to agent count. + runner=lambda agents, steps, seed=None: sugarscape_frames.simulate( + agents=agents, + steps=steps, + width=int(max(20, math.ceil((agents) ** 0.5) * 2)), + height=int(max(20, math.ceil((agents) ** 0.5) * 2)), + seed=seed, + ), ), ], ), diff --git a/examples/boltzmann_wealth/backend_frames.py b/examples/boltzmann_wealth/backend_frames.py index 23efac92..6d08a511 100644 --- a/examples/boltzmann_wealth/backend_frames.py +++ b/examples/boltzmann_wealth/backend_frames.py @@ -72,13 +72,22 @@ def __init__( ) -> None: super().__init__(seed) self.sets += MoneyAgents(self, agents) - storage_uri = str(results_dir) if results_dir is not None else None + # For benchmarks we frequently call simulate() without providing a results_dir. + # Persisting to disk would add unnecessary IO overhead and a missing storage_uri + # currently raises in DataCollector validation. Fallback to in-memory collection + # when no results_dir is supplied; otherwise write CSV files under results_dir. + if results_dir is None: + storage = "memory" + storage_uri = None + else: + storage = "csv" + storage_uri = str(results_dir) self.datacollector = DataCollector( model=self, model_reporters={ "gini": lambda m: gini(m.sets[0].df), }, - storage="csv", + storage=storage, storage_uri=storage_uri, ) diff --git a/examples/sugarscape_ig/backend_frames/model.py b/examples/sugarscape_ig/backend_frames/model.py index da4e1250..0ede4d30 100644 --- a/examples/sugarscape_ig/backend_frames/model.py +++ b/examples/sugarscape_ig/backend_frames/model.py @@ -242,7 +242,14 @@ def __init__( self.space.place_to_empty(self.sets) # 3. Finally we set up the data collector - storage_uri = str(results_dir) if results_dir is not None else None + # Benchmarks may run without providing a results_dir; in that case avoid forcing + # a CSV storage backend (which requires a storage_uri) and keep data in memory. + if results_dir is None: + storage = "memory" + storage_uri = None + else: + storage = "csv" + storage_uri = str(results_dir) self.datacollector = DataCollector( model=self, model_reporters={ @@ -258,7 +265,7 @@ def __init__( "corr_sugar_vision": corr_sugar_vision, }, agent_reporters={"traits": ["sugar", "metabolism", "vision"]}, - storage="csv", + storage=storage, storage_uri=storage_uri, ) self.datacollector.collect() From 0a2f672f21e2e947741efada183eed56e2c90950 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Oct 2025 18:17:21 +0000 Subject: [PATCH 256/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- benchmarks/cli.py | 32 ++++++++++++-------- examples/sugarscape_ig/backend_mesa/model.py | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index 3352fc3d..34f3d485 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -164,18 +164,26 @@ def _plot_performance( @app.command() def run( - models: Annotated[str, typer.Option( - help="Models to benchmark: boltzmann, sugarscape, or all", - callback=_parse_models - )] = "all", - agents: Annotated[str, typer.Option( - help="Agent count or range (start:stop:step)", - callback=_parse_agents - )] = "1000:5000:1000", - steps: Annotated[int, typer.Option( - min=0, - help="Number of steps per run.", - )] = 100, + models: Annotated[ + str, + typer.Option( + help="Models to benchmark: boltzmann, sugarscape, or all", + callback=_parse_models, + ), + ] = "all", + agents: Annotated[ + str, + typer.Option( + help="Agent count or range (start:stop:step)", callback=_parse_agents + ), + ] = "1000:5000:1000", + steps: Annotated[ + int, + typer.Option( + min=0, + help="Number of steps per run.", + ), + ] = 100, repeats: Annotated[int, typer.Option(help="Repeats per configuration.", min=1)] = 1, seed: Annotated[int, typer.Option(help="Optional RNG seed.")] = 42, save: Annotated[bool, typer.Option(help="Persist benchmark CSV results.")] = True, diff --git a/examples/sugarscape_ig/backend_mesa/model.py b/examples/sugarscape_ig/backend_mesa/model.py index dabf6321..6e62137a 100644 --- a/examples/sugarscape_ig/backend_mesa/model.py +++ b/examples/sugarscape_ig/backend_mesa/model.py @@ -151,7 +151,7 @@ def _harvest_and_survive(self) -> None: x, y = a.pos a.sugar += int(self.sugar_current[x, y]) a.sugar -= a.metabolism - # Harvested cells are emptied now; they will be refilled if empty. + # Harvested cells are emptied now; they will be refilled if empty. self.sugar_current[x, y] = 0 if a.sugar > 0: survivors.append(a) From 2ba2e2263cfa1808743dbc5b76982160642a7263 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Tue, 14 Oct 2025 20:21:07 +0200 Subject: [PATCH 257/329] fix: improve formatting and type hints in run function parameters --- benchmarks/cli.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index 3352fc3d..34f3d485 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -164,18 +164,26 @@ def _plot_performance( @app.command() def run( - models: Annotated[str, typer.Option( - help="Models to benchmark: boltzmann, sugarscape, or all", - callback=_parse_models - )] = "all", - agents: Annotated[str, typer.Option( - help="Agent count or range (start:stop:step)", - callback=_parse_agents - )] = "1000:5000:1000", - steps: Annotated[int, typer.Option( - min=0, - help="Number of steps per run.", - )] = 100, + models: Annotated[ + str, + typer.Option( + help="Models to benchmark: boltzmann, sugarscape, or all", + callback=_parse_models, + ), + ] = "all", + agents: Annotated[ + str, + typer.Option( + help="Agent count or range (start:stop:step)", callback=_parse_agents + ), + ] = "1000:5000:1000", + steps: Annotated[ + int, + typer.Option( + min=0, + help="Number of steps per run.", + ), + ] = 100, repeats: Annotated[int, typer.Option(help="Repeats per configuration.", min=1)] = 1, seed: Annotated[int, typer.Option(help="Optional RNG seed.")] = 42, save: Annotated[bool, typer.Option(help="Persist benchmark CSV results.")] = True, From dc09d4eaa2356ffac286064888bd04f95abe166c Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:00:08 +0200 Subject: [PATCH 258/329] adding warning when runtime type checking is activated --- benchmarks/cli.py | 7 +++++++ examples/boltzmann_wealth/backend_frames.py | 7 +++++++ examples/sugarscape_ig/backend_frames/model.py | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index 34f3d485..6efc5834 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -4,6 +4,7 @@ from dataclasses import dataclass from datetime import datetime, timezone +import os from pathlib import Path from time import perf_counter from typing import Literal, Annotated, Protocol, Optional @@ -202,6 +203,12 @@ def run( ] = Path(__file__).resolve().parent / "plots", ) -> None: """Run performance benchmarks for the models models.""" + runtime_typechecking = os.environ.get("MESA_FRAMES_RUNTIME_TYPECHECKING", "") + if runtime_typechecking and runtime_typechecking.lower() not in {"0", "false"}: + typer.secho( + "Warning: MESA_FRAMES_RUNTIME_TYPECHECKING is enabled; benchmarks may run significantly slower.", + fg=typer.colors.YELLOW, + ) rows: list[dict[str, object]] = [] timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") for model in models: diff --git a/examples/boltzmann_wealth/backend_frames.py b/examples/boltzmann_wealth/backend_frames.py index 6d08a511..a665abcd 100644 --- a/examples/boltzmann_wealth/backend_frames.py +++ b/examples/boltzmann_wealth/backend_frames.py @@ -7,6 +7,7 @@ from typing import Annotated import numpy as np +import os import polars as pl import typer from time import perf_counter @@ -129,6 +130,12 @@ def run( ), ] = None, ) -> None: + runtime_typechecking = os.environ.get("MESA_FRAMES_RUNTIME_TYPECHECKING", "") + if runtime_typechecking and runtime_typechecking.lower() not in {"0", "false"}: + typer.secho( + "Warning: MESA_FRAMES_RUNTIME_TYPECHECKING is enabled; this run will be slower.", + fg=typer.colors.YELLOW, + ) typer.echo( f"Running Boltzmann wealth model (mesa-frames) with {agents} agents for {steps} steps" ) diff --git a/examples/sugarscape_ig/backend_frames/model.py b/examples/sugarscape_ig/backend_frames/model.py index 36ca7092..0aba1188 100644 --- a/examples/sugarscape_ig/backend_frames/model.py +++ b/examples/sugarscape_ig/backend_frames/model.py @@ -8,6 +8,7 @@ from __future__ import annotations from datetime import datetime, timezone +import os from pathlib import Path from typing import Annotated from time import perf_counter @@ -427,6 +428,12 @@ def run( typer.echo( f"Running Sugarscape IG (mesa-frames, parallel) with {agents} agents on {width}x{height} for {steps} steps" ) + runtime_typechecking = os.environ.get("MESA_FRAMES_RUNTIME_TYPECHECKING", "") + if runtime_typechecking and runtime_typechecking.lower() not in {"0", "false"}: + typer.secho( + "Warning: MESA_FRAMES_RUNTIME_TYPECHECKING is enabled; this run will be slower.", + fg=typer.colors.YELLOW, + ) timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") if results_dir is None: results_dir = ( From a8fa69263f0df288799d1a7ee96a8e278fa58aa0 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:15:29 +0200 Subject: [PATCH 259/329] fix: update wealth adjustment logic in MoneyAgents class --- examples/boltzmann_wealth/backend_frames.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/boltzmann_wealth/backend_frames.py b/examples/boltzmann_wealth/backend_frames.py index a665abcd..b9acb00d 100644 --- a/examples/boltzmann_wealth/backend_frames.py +++ b/examples/boltzmann_wealth/backend_frames.py @@ -60,9 +60,11 @@ def step(self) -> None: seed=self.random.integers(np.iinfo(np.int32).max), ) # donors lose one unit - self["active", "wealth"] -= 1 + self.df = self.df.with_columns(pl.when(pl.col("wealth") > 0).then(pl.col("wealth") - 1).otherwise(pl.col("wealth")).alias("wealth")) gains = recipients.group_by("unique_id").len() - self[gains, "wealth"] += gains["len"] + self.df = self.df.join(gains, on="unique_id", how="left").with_columns( + (pl.col("wealth") + pl.col("len").fill_null(0)).alias("wealth") + ).drop("len") class MoneyModel(Model): From 95acd65e4686e8c2788ea23fa96ad90abc0aefe7 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:29:51 +0200 Subject: [PATCH 260/329] fix: streamline wealth adjustment logic in MoneyAgents class --- examples/boltzmann_wealth/backend_frames.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/examples/boltzmann_wealth/backend_frames.py b/examples/boltzmann_wealth/backend_frames.py index b9acb00d..67a012d1 100644 --- a/examples/boltzmann_wealth/backend_frames.py +++ b/examples/boltzmann_wealth/backend_frames.py @@ -59,12 +59,21 @@ def step(self) -> None: with_replacement=True, seed=self.random.integers(np.iinfo(np.int32).max), ) - # donors lose one unit - self.df = self.df.with_columns(pl.when(pl.col("wealth") > 0).then(pl.col("wealth") - 1).otherwise(pl.col("wealth")).alias("wealth")) + # Combine donor loss (1 per active agent) and recipient gains in a single adjustment. gains = recipients.group_by("unique_id").len() - self.df = self.df.join(gains, on="unique_id", how="left").with_columns( - (pl.col("wealth") + pl.col("len").fill_null(0)).alias("wealth") - ).drop("len") + self.df = ( + self.df.join(gains, on="unique_id", how="left") + .with_columns( + ( + pl.col("wealth") + # each active agent loses 1 unit of wealth + + pl.when(pl.col("wealth") > 0).then(- 1).otherwise(0) + # each agent gains 1 unit of wealth for each time they were selected as a recipient + + pl.col("len").fill_null(0) + ).alias("wealth") + ) + .drop("len") + ) class MoneyModel(Model): From eba6082d7f79a3369f9b32dd31059f67ee388380 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:33:08 +0200 Subject: [PATCH 261/329] refactor: update _plot_performance to use centralized plotting utility for consistent theming --- benchmarks/cli.py | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index 6efc5834..3fc7435a 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -10,15 +10,16 @@ from typing import Literal, Annotated, Protocol, Optional import math -import matplotlib.pyplot as plt import polars as pl -import seaborn as sns import typer from examples.boltzmann_wealth import backend_frames as boltzmann_frames from examples.boltzmann_wealth import backend_mesa as boltzmann_mesa from examples.sugarscape_ig.backend_frames import model as sugarscape_frames from examples.sugarscape_ig.backend_mesa import model as sugarscape_mesa +from examples.plotting import ( + plot_performance as _examples_plot_performance, +) app = typer.Typer(add_completion=False) @@ -136,31 +137,23 @@ def _parse_models(value: str) -> list[str]: return result -def _plot_performance( - df: pl.DataFrame, model_name: str, output_dir: Path, timestamp: str -) -> None: +def _plot_performance(df: pl.DataFrame, model_name: str, output_dir: Path, timestamp: str) -> None: + """Wrap examples.plotting.plot_performance to ensure consistent theming. + + The original benchmark implementation used simple seaborn styles (whitegrid / darkgrid). + Our example plotting utilities define a much darker, high-contrast *true* dark theme + (custom rc params overriding bg/fg colors). Reuse that logic here so the + benchmark dark plots match the example dark plots users see elsewhere. + """ if df.is_empty(): return - for theme, style in {"light": "whitegrid", "dark": "darkgrid"}.items(): - sns.set_theme(style=style) - fig, ax = plt.subplots(figsize=(8, 5)) - sns.lineplot( - data=df.to_pandas(), - x="agents", - y="runtime_seconds", - hue="backend", - estimator="mean", - errorbar="sd", - marker="o", - ax=ax, - ) - ax.set_title(f"{model_name.title()} runtime vs agents") - ax.set_xlabel("Agents") - ax.set_ylabel("Runtime (seconds)") - fig.tight_layout() - filename = output_dir / f"{model_name}_runtime_{timestamp}_{theme}.png" - fig.savefig(filename, dpi=300) - plt.close(fig) + stem = f"{model_name}_runtime_{timestamp}" + _examples_plot_performance( + df.select(["agents", "runtime_seconds", "backend"]), + output_dir=output_dir, + stem=stem, + title=f"{model_name.title()} runtime vs agents", + ) @app.command() From 7e6032c8d03f04bbd1ce2230d373f766ef82c2a7 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:56:08 +0200 Subject: [PATCH 262/329] fix: adjust range validation to allow zero as a valid start endpoint --- benchmarks/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index 3fc7435a..b543939b 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -90,7 +90,7 @@ def _parse_agents(value: str) -> list[int]: raise typer.BadParameter("Range values must be integers") from exc if step <= 0: raise typer.BadParameter("Step must be positive") - if start <= 0 or stop <= 0: + if start < 0 or stop <= 0: raise typer.BadParameter("Range endpoints must be positive") if start > stop: raise typer.BadParameter("Range start must be <= stop") From 40332c8896e929120f8a3b6c1a960d096c9528e9 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:57:49 +0200 Subject: [PATCH 263/329] refactor: format _plot_performance function signature for improved readability fix: correct wealth adjustment logic in MoneyAgents class for clarity --- benchmarks/cli.py | 4 +++- examples/boltzmann_wealth/backend_frames.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index b543939b..4a7b0703 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -137,7 +137,9 @@ def _parse_models(value: str) -> list[str]: return result -def _plot_performance(df: pl.DataFrame, model_name: str, output_dir: Path, timestamp: str) -> None: +def _plot_performance( + df: pl.DataFrame, model_name: str, output_dir: Path, timestamp: str +) -> None: """Wrap examples.plotting.plot_performance to ensure consistent theming. The original benchmark implementation used simple seaborn styles (whitegrid / darkgrid). diff --git a/examples/boltzmann_wealth/backend_frames.py b/examples/boltzmann_wealth/backend_frames.py index 67a012d1..da26dba9 100644 --- a/examples/boltzmann_wealth/backend_frames.py +++ b/examples/boltzmann_wealth/backend_frames.py @@ -67,7 +67,7 @@ def step(self) -> None: ( pl.col("wealth") # each active agent loses 1 unit of wealth - + pl.when(pl.col("wealth") > 0).then(- 1).otherwise(0) + + pl.when(pl.col("wealth") > 0).then(-1).otherwise(0) # each agent gains 1 unit of wealth for each time they were selected as a recipient + pl.col("len").fill_null(0) ).alias("wealth") From d7ee3405af796623b5a0441f6fd4ac7f0ea73160 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 16 Oct 2025 13:08:34 +0200 Subject: [PATCH 264/329] fix: enhance sorting criteria in AntsParallel class for improved candidate selection --- examples/sugarscape_ig/backend_frames/agents.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/sugarscape_ig/backend_frames/agents.py b/examples/sugarscape_ig/backend_frames/agents.py index 9f307e72..e619df00 100644 --- a/examples/sugarscape_ig/backend_frames/agents.py +++ b/examples/sugarscape_ig/backend_frames/agents.py @@ -546,7 +546,9 @@ def _resolve_conflicts_in_rounds( # │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ f64 │ # ╞══════════╪══════════════════╪══════════════════╪════════╪════════╪══════╪══════════════╪═════════╡ winners = ( - best_candidates.sort(["dim_0_candidate", "dim_1_candidate", "lottery"]) + best_candidates.sort( + ["dim_0_candidate", "dim_1_candidate", "radius", "lottery"], + ) .group_by(["dim_0_candidate", "dim_1_candidate"], maintain_order=True) .first() ) From 9ec57c4162294fc365d3ed6addf96514c7ff0541 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Thu, 16 Oct 2025 16:22:00 +0200 Subject: [PATCH 265/329] feat: add completion messages to CLI for benchmarking runs --- benchmarks/cli.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index 4a7b0703..29f189e5 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -228,6 +228,13 @@ def run( "timestamp": timestamp, } ) + # Report completion of this run to the CLI + typer.echo( + f"Completed {backend.name} for model={model} agents={agents_count} steps={steps} seed={run_seed} repeat={repeat_idx} in {runtime:.3f}s" + ) + # Finished all runs for this model + typer.echo(f"Finished benchmarking model {model}") + if not rows: typer.echo("No benchmark data collected.") return From 25575d27cecedbb12c7ba303d315f8cb391d602d Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 09:50:30 +0200 Subject: [PATCH 266/329] refactor: update plot titles for clarity and enhance legend handling for improved readability --- benchmarks/cli.py | 3 ++- examples/plotting.py | 17 +++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index 29f189e5..eaf430d0 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -154,7 +154,8 @@ def _plot_performance( df.select(["agents", "runtime_seconds", "backend"]), output_dir=output_dir, stem=stem, - title=f"{model_name.title()} runtime vs agents", + # Prefer more concise, publication-style wording + title=f"{model_name.title()} runtime scaling", ) diff --git a/examples/plotting.py b/examples/plotting.py index 0cf002c4..17075451 100644 --- a/examples/plotting.py +++ b/examples/plotting.py @@ -268,12 +268,17 @@ def plot_performance( _apply_titles(fig, ax, title, subtitle) ax.set_xlabel("Agents") ax.set_ylabel("Runtime (seconds)") - - if theme == "dark": - leg = ax.get_legend() - if leg is not None: - leg.set_title(None) - leg.get_frame().set_alpha(0.8) + leg = ax.get_legend() + if leg is not None: + # Remove redundant legend title (backend) for both themes – label colors already distinguish. + leg.set_title(None) + frame = leg.get_frame() + if theme == "dark": + frame.set_alpha(0.8) + else: # light theme: subtle boxed legend for readability on white grid + frame.set_alpha(0.9) + frame.set_edgecolor("#d0d0d0") + frame.set_linewidth(0.8) _finalize_and_save(fig, output_dir, stem, theme) From 5979ff994aad4c6903c16abfc5b6a8d3015dacf1 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 09:53:35 +0200 Subject: [PATCH 267/329] fix: update .gitignore to include results and plots directories for examples and benchmarks --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 45729158..0034fd4a 100644 --- a/.gitignore +++ b/.gitignore @@ -159,4 +159,6 @@ docs/api/_build docs/general/user-guide/data_csv docs/general/user-guide/data_parquet docs/api/reference/**/mesa_frames.*.rst -examples/**/results \ No newline at end of file +examples/**/results +benchmarks/**/results +benchmarks/**/plots \ No newline at end of file From fcce4021ab2eecf74d8b69e9c24e8a2238669a91 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 10:03:55 +0200 Subject: [PATCH 268/329] refactor: enhance output directory structure for benchmark results and plots --- benchmarks/cli.py | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index eaf430d0..4732c388 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -188,15 +188,22 @@ def run( results_dir: Annotated[ Path, typer.Option( - help="Directory for benchmark CSV results.", + help=( + "Base directory for benchmark outputs. A timestamped subdirectory " + "(e.g. results/20250101_120000) is created with CSV files at the root " + "and a 'plots/' subfolder for images." + ), ), ] = Path(__file__).resolve().parent / "results", plots_dir: Annotated[ - Path, + Optional[Path], typer.Option( - help="Directory for benchmark plots.", + help=( + "(Deprecated) Explicit plots directory. If provided, overrides the default " + "'results//plots'. Prefer leaving unset to use the unified layout." + ), ), - ] = Path(__file__).resolve().parent / "plots", + ] = None, ) -> None: """Run performance benchmarks for the models models.""" runtime_typechecking = os.environ.get("MESA_FRAMES_RUNTIME_TYPECHECKING", "") @@ -206,7 +213,20 @@ def run( fg=typer.colors.YELLOW, ) rows: list[dict[str, object]] = [] + # Single timestamp per CLI invocation so all model results are co-located. timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") + # Create unified output layout: //{CSV files, plots/} + base_results_dir = results_dir + timestamp_dir = (base_results_dir / timestamp).resolve() + plots_subdir: Path + if plots_dir is not None: + # Backwards compatibility path – user wants a custom plots directory. + plots_subdir = plots_dir.resolve() + if plots_subdir.is_relative_to(timestamp_dir): # Python 3.11 method + # ensure parent timestamp dir exists too + timestamp_dir.mkdir(parents=True, exist_ok=True) + else: + plots_subdir = timestamp_dir / "plots" for model in models: config = MODELS[model] typer.echo(f"Benchmarking {model} with agents {agents}") @@ -241,18 +261,22 @@ def run( return df = pl.DataFrame(rows) if save: - results_dir.mkdir(parents=True, exist_ok=True) + timestamp_dir.mkdir(parents=True, exist_ok=True) for model in models: model_df = df.filter(pl.col("model") == model) - csv_path = results_dir / f"{model}_perf_{timestamp}.csv" + csv_path = timestamp_dir / f"{model}_perf_{timestamp}.csv" model_df.write_csv(csv_path) typer.echo(f"Saved {model} results to {csv_path}") if plot: - plots_dir.mkdir(parents=True, exist_ok=True) + plots_subdir.mkdir(parents=True, exist_ok=True) for model in models: model_df = df.filter(pl.col("model") == model) - _plot_performance(model_df, model, plots_dir, timestamp) - typer.echo(f"Saved {model} plots under {plots_dir}") + _plot_performance(model_df, model, plots_subdir, timestamp) + typer.echo(f"Saved {model} plots under {plots_subdir}") + + typer.echo( + f"Unified benchmark outputs written under {timestamp_dir} (CSV files) and {plots_subdir} (plots)" + ) if __name__ == "__main__": From e5bc07e0868de28139d01571c0ce14286133f32e Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 10:11:32 +0200 Subject: [PATCH 269/329] refactor: streamline plots directory handling in benchmark CLI --- benchmarks/README.md | 88 ++++++++++++++++++++++++++++++++++++++++++++ benchmarks/cli.py | 19 +--------- 2 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 benchmarks/README.md diff --git a/benchmarks/README.md b/benchmarks/README.md new file mode 100644 index 00000000..b23fd04b --- /dev/null +++ b/benchmarks/README.md @@ -0,0 +1,88 @@ +# Benchmarks + +Performance benchmarks compare Mesa Frames backends ("frames") with classic Mesa ("mesa") +implementations for a small set of representative models. They help track runtime scaling +and regressions. + +Currently included models: + +- **boltzmann**: Simple wealth exchange ("Boltzmann wealth") model. +- **sugarscape**: Sugarscape Immediate Growback variant (square grid sized relative to agent count). + +## Quick start + +``` +uv run benchmarks/cli.py +``` + +That command (with defaults) will: + +- Benchmark both models (`boltzmann`, `sugarscape`). +- Use agent counts 1000, 2000, 3000, 4000, 5000. +- Run 100 steps per simulation. +- Repeat each configuration once. +- Save CSV results and generate plots. + +## CLI options + +Invoke `uv run benchmarks/cli.py --help` to see full help. Key options: + +| Option | Default | Description | +| ------ | ------- | ----------- | +| `--models` | `all` | Comma list or `all`; accepted: `boltzmann`, `sugarscape`. | +| `--agents` | `1000:5000:1000` | Single int or range `start:stop:step`. | +| `--steps` | `100` | Steps per simulation run. | +| `--repeats` | `1` | How many repeats per (model, backend, agents) config. Seed increments per repeat. | +| `--seed` | `42` | Base RNG seed. Incremented by repeat index. | +| `--save / --no-save` | `--save` | Persist per‑model CSVs. | +| `--plot / --no-plot` | `--plot` | Generate scaling plots (PNG + possibly other formats). | +| `--results-dir` | `benchmarks/results` | Root directory that will receive a timestamped subdirectory. | + +Range parsing: `A:B:S` includes `A, A+S, ... <= B`. Final value > B is dropped. + +## Output layout + +Each invocation uses a single UTC timestamp, e.g. `20251016_173702`: + +``` +benchmarks/ + results/ + 20251016_173702/ + boltzmann_perf_20251016_173702.csv + sugarscape_perf_20251016_173702.csv + plots/ + boltzmann_runtime_20251016_173702_dark.png + sugarscape_runtime_20251016_173702_dark.png + ... (other themed variants if enabled) +``` + +CSV schema (one row per completed run): + +| Column | Meaning | +| ------ | ------- | +| `model` | Model key (`boltzmann`, `sugarscape`). | +| `backend` | `mesa` or `frames`. | +| `agents` | Agent count for that run. | +| `steps` | Steps simulated. | +| `seed` | Seed used (base seed + repeat index). | +| `repeat_idx` | Repeat counter starting at 0. | +| `runtime_seconds` | Wall-clock runtime for that run. | +| `timestamp` | Shared timestamp identifier for the benchmark batch. | + +## Performance tips + +- Ensure the environment variable `MESA_FRAMES_RUNTIME_TYPECHECKING` is **unset** or set to `0` / `false` when collecting performance numbers. Enabling it adds runtime type validation overhead and the CLI will warn you. +- Run multiple repeats (`--repeats 5`) to smooth variance. + +## Extending benchmarks + +To benchmark an additional model: + +1. Add or import both a Mesa implementation and a Frames implementation exposing a `simulate(agents:int, steps:int, seed:int|None, ...)` function. +2. Register it in `benchmarks/cli.py` inside the `MODELS` dict with two backends (names must be `mesa` and `frames`). +3. Ensure any extra spatial parameters are derived from `agents` inside the runner lambda (see sugarscape example). +4. Run the CLI to verify new CSV columns still align. + +## Related documentation + +See `docs/user-guide/5_benchmarks.md` (user-facing narrative) and the main project `README.md` for overall context. \ No newline at end of file diff --git a/benchmarks/cli.py b/benchmarks/cli.py index 4732c388..c9beb7d2 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -195,15 +195,6 @@ def run( ), ), ] = Path(__file__).resolve().parent / "results", - plots_dir: Annotated[ - Optional[Path], - typer.Option( - help=( - "(Deprecated) Explicit plots directory. If provided, overrides the default " - "'results//plots'. Prefer leaving unset to use the unified layout." - ), - ), - ] = None, ) -> None: """Run performance benchmarks for the models models.""" runtime_typechecking = os.environ.get("MESA_FRAMES_RUNTIME_TYPECHECKING", "") @@ -218,15 +209,7 @@ def run( # Create unified output layout: //{CSV files, plots/} base_results_dir = results_dir timestamp_dir = (base_results_dir / timestamp).resolve() - plots_subdir: Path - if plots_dir is not None: - # Backwards compatibility path – user wants a custom plots directory. - plots_subdir = plots_dir.resolve() - if plots_subdir.is_relative_to(timestamp_dir): # Python 3.11 method - # ensure parent timestamp dir exists too - timestamp_dir.mkdir(parents=True, exist_ok=True) - else: - plots_subdir = timestamp_dir / "plots" + plots_subdir: Path = timestamp_dir / "plots" for model in models: config = MODELS[model] typer.echo(f"Benchmarking {model} with agents {agents}") From d973b160c9ca12262e18e13a6b6fb034faf237de Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 10:20:05 +0200 Subject: [PATCH 270/329] feat: add comprehensive examples and usage instructions to README --- examples/README.md | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 examples/README.md diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..d6844dc8 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,106 @@ +# Examples + +This directory contains runnable example models and shared plotting/utilities +used in the tutorials and benchmarks. Each example provides **two backends**: + +- `mesa` (classic Mesa, object-per-agent) +- `frames` (Mesa Frames, vectorised agent sets / dataframe-centric) + +They expose a consistent Typer CLI so you can compare outputs and timings. + +## Contents + +``` +examples/ + boltzmann_wealth/ + backend_mesa.py # Mesa implementation + CLI (simulate() + run) + backend_frames.py # Frames implementation + CLI (simulate() + run) + sugarscape_ig/ + backend_mesa/ # Mesa Sugarscape (agents + model + CLI) + backend_frames/ # Frames Sugarscape (agents + model + CLI) + plotting.py # Shared plotting helpers (Seaborn + dark theme) + utils.py # Small dataclasses for simulation results +``` + +## Quick start + +Always run via `uv` from the project root. The simplest way to run an example +backend is to execute the module: + +``` +uv run examples/boltzmann_wealth/backend_frames.py +``` + +Each command will: + +1. Print a short banner with configuration. +2. Run the simulation and show elapsed time. +3. Emit a tail of the collected metrics (e.g. last 5 Gini values). +4. Save CSV metrics and optional plots in a timestamped directory under that + example's `results/` folder (unless overridden by `--results-dir`). + +## CLI symmetry + +Both backends accept similar options: + +- `--agents` (population size) +- `--steps` (number of simulated steps) +- `--seed` (optional RNG seed; Mesa backend resets model RNG) +- `--plot / --no-plot` (toggle plot generation) +- `--save-results / --no-save-results` (persist CSV outputs) +- `--results-dir` (override auto-created timestamped folder) + +The Frames Boltzmann backend stores model metrics in a Polars DataFrame via +`mesa_frames.DataCollector`; the Mesa backend uses the standard `mesa.DataCollector` +returning pandas DataFrames, then converts to Polars only for plotting so plots +look identical. + +## Data and metrics + +The saved CSV layout (Frames) places `model.csv` in the results directory with +columns like: `step, gini, `. +The Mesa implementations write +compatible CSVs. + +## Plotting helpers + +`examples/plotting.py` provides: + +- `plot_model_metrics(df, output_dir, stem, title, subtitle, agents, steps)` + Produces dark theme line plots of model-level metrics (currently Gini) and + stores PNG files under `output_dir` with names like `gini__dark.png`. +- `plot_performance(df, output_dir, stem, title)` used by `benchmarks/cli.py` to + generate runtime scaling plots. + +The dark theme matches the styling used in the documentation for visual +consistency. + +## Interacting programmatically + +Instead of using the CLIs you can import the simulation entry points directly: + +```python +from examples.boltzmann_wealth import backend_frames as bw_frames +result = bw_frames.simulate(agents=2000, steps=100, seed=123) +polars_df = result.datacollector.data["model"] # Polars DataFrame of metrics +``` + +Each `simulate()` returns a small dataclass (`FramesSimulationResult` or +`MesaSimulationResult`) holding the respective `DataCollector` instance so you +can further analyse the collected data. + +## Tips + +- To compare backends fairly, disable runtime type checking when measuring performance: + set environment variable `MESA_FRAMES_RUNTIME_TYPECHECKING=0`. +- Use the same `--seed` across runs for reproducible trajectories (given the + stochastic nature of agent interactions). +- Larger Sugarscape grids (width/height) increase memory and runtime; choose + sizes proportional to the square root of agent count for balanced density. + +## Adding Examples + +You can adapt these scripts to prototype new models: copy a backend pair, +rename the module, and implement your agent rules while keeping the API +surface (`simulate`, `run`) consistent so tooling and documentation patterns +continue to apply. From e56a699df19aed754eaaafc23675e7da99063489 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 10:26:22 +0200 Subject: [PATCH 271/329] feat: add README for Boltzmann Wealth Exchange Model example with usage instructions --- examples/boltzmann_wealth/README.md | 94 +++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 examples/boltzmann_wealth/README.md diff --git a/examples/boltzmann_wealth/README.md b/examples/boltzmann_wealth/README.md new file mode 100644 index 00000000..f31bb6f8 --- /dev/null +++ b/examples/boltzmann_wealth/README.md @@ -0,0 +1,94 @@ +# Boltzmann Wealth Exchange Model + +This example implements a simple wealth exchange ("Boltzmann money") model in two +backends: + +- `backend_frames.py` (Mesa Frames / vectorised `AgentSet`) +- `backend_mesa.py` (classic Mesa / object-per-agent) + +Both expose a Typer CLI with symmetric options so you can compare correctness +and performance directly. + +## Concept + +Each agent starts with 1 unit of wealth. At every step: + +1. Frames backend: all agents with strictly positive wealth become potential donors. + Each donor gives 1 unit of wealth, and a recipient is drawn (with replacement) + for every donating agent. A single vectorised update applies donor losses and + recipient gains. +2. Mesa backend: agents are shuffled and iterate sequentially; each agent with + positive wealth transfers 1 unit to a randomly selected peer. + +The stochastic exchange process leads to an emergent, increasingly unequal +wealth distribution and rising Gini coefficient, typically approaching a stable +level below 1 (due to conservation and continued mixing). + +## Reported Metrics + +The model records per-step population Gini (`gini`). You can extend reporters by +adding lambdas to `model_reporters` in either backend's constructor. + +Interpretation of `gini` trajectory: + +- Early steps: Gini ~ 0 (uniform initial wealth). +- Mid phase: Increasing Gini as random exchanges concentrate wealth. +- Late phase: Fluctuating plateau (a stochastic steady state) — exact level + varies with agent count and RNG seed. + +## Running + +Always run examples from the project root using `uv`: + +```bash +uv run examples/boltzmann_wealth/backend_frames.py --agents 5000 --steps 200 --seed 123 --plot --save-results +uv run examples/boltzmann_wealth/backend_mesa.py --agents 5000 --steps 200 --seed 123 --plot --save-results +``` + +CLI options (shared): + +- `--agents` Number of agents (default 5000) +- `--steps` Simulation steps (default 100) +- `--seed` Optional RNG seed for reproducibility +- `--plot / --no-plot` Generate line plot(s) of Gini +- `--save-results / --no-save-results` Persist CSV metrics +- `--results-dir` Override the auto timestamped directory under `results/` + +Frames backend additionally warns if runtime type checking is enabled because it +slows vectorised operations: set `MESA_FRAMES_RUNTIME_TYPECHECKING=0` for fair +performance comparisons. + +## Outputs + +Each run creates (or uses) a results directory like: + +``` +examples/boltzmann_wealth/results/20251016_173702/ + model.csv # step,gini + gini__dark.png (and possibly other theme variants) +``` + +Tail metrics are printed to console for quick inspection: + +``` +Metrics in the final 5 steps: shape: (5, 2) +┌──────┬───────┐ +│ step ┆ gini │ +│ --- ┆ --- │ +│ i64 ┆ f64 │ +├──────┼───────┤ +│ ... ┆ ... │ +└──────┴───────┘ +``` + +## Performance & Benchmarking + +Use the shared benchmarking CLI to compare scaling, checkout `benchmarks/README.md`. + +## Programmatic Use + +```python +from examples.boltzmann_wealth import backend_frames as bw_frames +result = bw_frames.simulate(agents=10000, steps=250, seed=42) +metrics = result.datacollector.data["model"] # Polars DataFrame +``` \ No newline at end of file From 7ae14951624f0a0e7b29853cf888dba3037dbe70 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 10:30:15 +0200 Subject: [PATCH 272/329] feat: add README for Sugarscape IG example with detailed usage instructions --- examples/sugarscape_ig/README.md | 103 +++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 examples/sugarscape_ig/README.md diff --git a/examples/sugarscape_ig/README.md b/examples/sugarscape_ig/README.md new file mode 100644 index 00000000..67f83d2c --- /dev/null +++ b/examples/sugarscape_ig/README.md @@ -0,0 +1,103 @@ +# Sugarscape IG (Instant Growback) + +This directory contains a minimal Instant Growback Sugarscape implementation in +both backends: + +- `backend_frames/` parallel (vectorised) movement variant using Mesa Frames +- `backend_mesa/` sequential (asynchronous) movement variant using classic Mesa + +The Instant Growback (IG) rule sequence is: move -> eat -> regrow -> collect. +Agents harvest sugar, pay metabolism costs, possibly die (starve), and empty +cells instantly regrow to their `max_sugar` value. + +## Core Dynamics + +Each agent has integer traits: + +- `sugar` (current stores) +- `metabolism` (per-step consumption) +- `vision` (how far the agent can see in cardinal directions) + +Movement policy (both backends conceptually): + +1. Sense visible cells along N/E/S/W up to `vision` steps (including origin). +2. Rank candidate cells by: (a) sugar (desc), (b) distance (asc), (c) coordinates + as deterministic tie-breaker. +3. Choose highest-ranked empty cell; fall back to origin if none available. + +The Frames parallel variant resolves conflicts by iterative lottery rounds using +rank promotion; the sequential Mesa variant inherently orders moves by shuffled +agent iteration. + +After moving, agents harvest sugar on their cell, pay metabolism, and starved +agents are removed. Empty cells regrow to their `max_sugar` value immediately. + +## Metrics Collected + +Both backends record population-level reporters each step: + +- `mean_sugar` Average sugar per surviving agent. +- `total_sugar` Aggregate sugar held by living agents. +- `agents_alive` Population size (declines as agents starve). +- `gini` Inequality in sugar holdings (0 = equal, higher = more unequal). +- `corr_sugar_metabolism` Pearson correlation (do high-metabolism agents retain sugar?). +- `corr_sugar_vision` Pearson correlation (does greater vision correlate with sugar?). + +Interpretation guidelines: + +- `agents_alive` typically decreases until a quasi steady state (metabolism vs regrowth) or total collapse. +- `mean_sugar` and `total_sugar` may stabilise if regrowth balances metabolism. +- Rising `gini` indicates emerging inequality; sustained high values suggest strong positional advantages. +- Correlations near 0 imply weak linear relationships; positive `corr_sugar_vision` suggests high vision aids resource gathering. Negative `corr_sugar_metabolism` can emerge if high metabolism accelerates starvation. + +## Running Examples + +From project root using `uv`: + +```bash +uv run examples/sugarscape_ig/backend_frames/model.py --agents 400 --width 40 --height 40 --steps 60 --seed 123 --plot --save-results +uv run examples/sugarscape_ig/backend_mesa/model.py --agents 400 --width 40 --height 40 --steps 60 --seed 123 --plot --save-results +``` + +Shared CLI options: + +- `--agents` Number of agents (default 400) +- `--width`, `--height` Grid dimensions (default 40x40) +- `--steps` Max steps (default 60) +- `--max-sugar` Initial/regrowth max sugar per cell (default 4) +- `--seed` Optional RNG seed +- `--plot / --no-plot` Generate per-metric plots +- `--save-results / --no-save-results` Persist CSV outputs +- `--results-dir` Override auto timestamped directory under `results/` + +Frames backend warns if `MESA_FRAMES_RUNTIME_TYPECHECKING` is enabled (disable for benchmarks). + +## Outputs + +Example output directory (frames): + +``` +examples/sugarscape_ig/backend_frames/results/20251016_173702/ + model.csv + plots/ + gini__dark.png + agents_alive__dark.png + mean_sugar__dark.png + ... +``` + +`model.csv` columns include: `step`, `mean_sugar`, `total_sugar`, `agents_alive`, +`gini`, `corr_sugar_metabolism`, `corr_sugar_vision`, plus backend-specific bookkeeping. +Mesa backend normalises to the same layout (excluding internal columns). + +## Performance & Benchmarking + +Use the shared benchmarking CLI to compare scaling, checkout `benchmarks/README.md`. + +## Programmatic Use + +```python +from examples.sugarscape_ig.backend_frames import model as sg_frames +res = sg_frames.simulate(agents=500, steps=80, width=50, height=50, seed=42) +metrics = res.datacollector.data["model"] # Polars DataFrame +``` \ No newline at end of file From a1c76a37514a2c52f17624de9817fb3d7a716154 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 10:34:07 +0200 Subject: [PATCH 273/329] docs: update README for Boltzmann Wealth Exchange and Sugarscape IG examples with improved structure and clarity --- examples/boltzmann_wealth/README.md | 6 ++++-- examples/sugarscape_ig/README.md | 12 +++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/examples/boltzmann_wealth/README.md b/examples/boltzmann_wealth/README.md index f31bb6f8..c1442239 100644 --- a/examples/boltzmann_wealth/README.md +++ b/examples/boltzmann_wealth/README.md @@ -1,5 +1,7 @@ # Boltzmann Wealth Exchange Model +## Overview + This example implements a simple wealth exchange ("Boltzmann money") model in two backends: @@ -29,7 +31,7 @@ level below 1 (due to conservation and continued mixing). The model records per-step population Gini (`gini`). You can extend reporters by adding lambdas to `model_reporters` in either backend's constructor. -Interpretation of `gini` trajectory: +Notes on interpretation: - Early steps: Gini ~ 0 (uniform initial wealth). - Mid phase: Increasing Gini as random exchanges concentrate wealth. @@ -45,7 +47,7 @@ uv run examples/boltzmann_wealth/backend_frames.py --agents 5000 --steps 200 --s uv run examples/boltzmann_wealth/backend_mesa.py --agents 5000 --steps 200 --seed 123 --plot --save-results ``` -CLI options (shared): +## CLI options - `--agents` Number of agents (default 5000) - `--steps` Simulation steps (default 100) diff --git a/examples/sugarscape_ig/README.md b/examples/sugarscape_ig/README.md index 67f83d2c..64d0d6ff 100644 --- a/examples/sugarscape_ig/README.md +++ b/examples/sugarscape_ig/README.md @@ -1,5 +1,7 @@ # Sugarscape IG (Instant Growback) +## Overview + This directory contains a minimal Instant Growback Sugarscape implementation in both backends: @@ -10,7 +12,7 @@ The Instant Growback (IG) rule sequence is: move -> eat -> regrow -> collect. Agents harvest sugar, pay metabolism costs, possibly die (starve), and empty cells instantly regrow to their `max_sugar` value. -## Core Dynamics +## Concept Each agent has integer traits: @@ -32,7 +34,7 @@ agent iteration. After moving, agents harvest sugar on their cell, pay metabolism, and starved agents are removed. Empty cells regrow to their `max_sugar` value immediately. -## Metrics Collected +## Reported Metrics Both backends record population-level reporters each step: @@ -43,14 +45,14 @@ Both backends record population-level reporters each step: - `corr_sugar_metabolism` Pearson correlation (do high-metabolism agents retain sugar?). - `corr_sugar_vision` Pearson correlation (does greater vision correlate with sugar?). -Interpretation guidelines: +Notes on interpretation: - `agents_alive` typically decreases until a quasi steady state (metabolism vs regrowth) or total collapse. - `mean_sugar` and `total_sugar` may stabilise if regrowth balances metabolism. - Rising `gini` indicates emerging inequality; sustained high values suggest strong positional advantages. - Correlations near 0 imply weak linear relationships; positive `corr_sugar_vision` suggests high vision aids resource gathering. Negative `corr_sugar_metabolism` can emerge if high metabolism accelerates starvation. -## Running Examples +## Running From project root using `uv`: @@ -59,7 +61,7 @@ uv run examples/sugarscape_ig/backend_frames/model.py --agents 400 --width 40 -- uv run examples/sugarscape_ig/backend_mesa/model.py --agents 400 --width 40 --height 40 --steps 60 --seed 123 --plot --save-results ``` -Shared CLI options: +## CLI options - `--agents` Number of agents (default 400) - `--width`, `--height` Grid dimensions (default 40x40) From 504d9f241ad64a3bf4824f0f141c76f4ea59d93e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 17 Oct 2025 08:34:31 +0000 Subject: [PATCH 274/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- benchmarks/README.md | 2 +- examples/README.md | 4 ++-- examples/boltzmann_wealth/README.md | 2 +- examples/sugarscape_ig/README.md | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/benchmarks/README.md b/benchmarks/README.md index b23fd04b..687093d8 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -85,4 +85,4 @@ To benchmark an additional model: ## Related documentation -See `docs/user-guide/5_benchmarks.md` (user-facing narrative) and the main project `README.md` for overall context. \ No newline at end of file +See `docs/user-guide/5_benchmarks.md` (user-facing narrative) and the main project `README.md` for overall context. diff --git a/examples/README.md b/examples/README.md index d6844dc8..64da9fea 100644 --- a/examples/README.md +++ b/examples/README.md @@ -45,7 +45,7 @@ Both backends accept similar options: - `--agents` (population size) - `--steps` (number of simulated steps) -- `--seed` (optional RNG seed; Mesa backend resets model RNG) +- `--seed` (optional RNG seed; Mesa backend resets model RNG) - `--plot / --no-plot` (toggle plot generation) - `--save-results / --no-save-results` (persist CSV outputs) - `--results-dir` (override auto-created timestamped folder) @@ -58,7 +58,7 @@ look identical. ## Data and metrics The saved CSV layout (Frames) places `model.csv` in the results directory with -columns like: `step, gini, `. +columns like: `step, gini, `. The Mesa implementations write compatible CSVs. diff --git a/examples/boltzmann_wealth/README.md b/examples/boltzmann_wealth/README.md index c1442239..dd7e8f11 100644 --- a/examples/boltzmann_wealth/README.md +++ b/examples/boltzmann_wealth/README.md @@ -93,4 +93,4 @@ Use the shared benchmarking CLI to compare scaling, checkout `benchmarks/README. from examples.boltzmann_wealth import backend_frames as bw_frames result = bw_frames.simulate(agents=10000, steps=250, seed=42) metrics = result.datacollector.data["model"] # Polars DataFrame -``` \ No newline at end of file +``` diff --git a/examples/sugarscape_ig/README.md b/examples/sugarscape_ig/README.md index 64d0d6ff..7940bcec 100644 --- a/examples/sugarscape_ig/README.md +++ b/examples/sugarscape_ig/README.md @@ -102,4 +102,4 @@ Use the shared benchmarking CLI to compare scaling, checkout `benchmarks/README. from examples.sugarscape_ig.backend_frames import model as sg_frames res = sg_frames.simulate(agents=500, steps=80, width=50, height=50, seed=42) metrics = res.datacollector.data["model"] # Polars DataFrame -``` \ No newline at end of file +``` From 85af55ab30830615a4ec0bc8b86f5050c3df1f9b Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 11:08:34 +0200 Subject: [PATCH 275/329] docs: update benchmark links and images in README for improved accuracy --- README.md | 6 +- examples/boltzmann_wealth/benchmark.svg | 1083 ++++++++++++++++++++++ examples/sugarscape_ig/benchmark.svg | 1091 +++++++++++++++++++++++ 3 files changed, 2177 insertions(+), 3 deletions(-) create mode 100644 examples/boltzmann_wealth/benchmark.svg create mode 100644 examples/sugarscape_ig/benchmark.svg diff --git a/README.md b/README.md index 9ff6205a..0d0a48fe 100644 --- a/README.md +++ b/README.md @@ -56,14 +56,14 @@ mesa-frames currently uses **Polars** as its backend. ## Benchmarks -[![Reproduce Benchmarks](https://img.shields.io/badge/Reproduce%20Benchmarks-📊-orange?style=for-the-badge)](https://projectmesa.github.io/mesa-frames/general/benchmarks/) +[![Reproduce Benchmarks](https://img.shields.io/badge/Reproduce%20Benchmarks-📊-orange?style=for-the-badge)](https://github.com/projectmesa/mesa-frames/blob/main/benchmarks/README.md) mesa-frames delivers consistent speedups across both toy and canonical ABMs. At 10k agents, it runs **~10× faster** than classic Mesa, and the gap grows with scale. -![Benchmark: Boltzmann Wealth](examples/boltzmann_wealth/boltzmann_benchmark.png) +![Benchmark: Boltzmann Wealth](examples/boltzmann_wealth/benchmark.svg) -![Benchmark: Sugarscape IG](examples/sugarscape/sugarscape_benchmark.png) +![Benchmark: Sugarscape IG](examples/sugarscape_ig/benchmark.svg) --- diff --git a/examples/boltzmann_wealth/benchmark.svg b/examples/boltzmann_wealth/benchmark.svg new file mode 100644 index 00000000..b3949bd0 --- /dev/null +++ b/examples/boltzmann_wealth/benchmark.svg @@ -0,0 +1,1083 @@ + + + + + + + + 2025-10-16T19:57:07.933517 + image/svg+xml + + + Matplotlib v3.10.5, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/sugarscape_ig/benchmark.svg b/examples/sugarscape_ig/benchmark.svg new file mode 100644 index 00000000..b7b95843 --- /dev/null +++ b/examples/sugarscape_ig/benchmark.svg @@ -0,0 +1,1091 @@ + + + + + + + + 2025-10-16T19:57:08.355947 + image/svg+xml + + + Matplotlib v3.10.5, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 55dbbec257298a2ca0ad505ff073ce2a8c33e5f2 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 11:28:59 +0200 Subject: [PATCH 276/329] chore: remove unused JavaScript and CSS references from mkdocs configuration --- mkdocs.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index a1caa258..3b31177b 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -96,15 +96,9 @@ markdown_extensions: # Extra JavaScript and CSS for rendering extra_javascript: - - javascripts/mathjax.js - - https://polyfill.io/v3/polyfill.min.js?features=es6 - https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js # Custom CSS for branding (brand-core then material adapter) -extra_css: - - stylesheets/brand-core.css - - stylesheets/brand-material.css - # Customization extra: social: From d4bc7fb1d36652dcfb77fcc6a103aa741f36f2fe Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 11:31:40 +0200 Subject: [PATCH 277/329] feat: add SVG files for Boltzmann and Sugarscape plots to enhance documentation --- docs/general/plots/boltzmann.svg | 90 +++++++++++++++++++++++++++++++ docs/general/plots/sugarscape.svg | 2 + 2 files changed, 92 insertions(+) create mode 100644 docs/general/plots/boltzmann.svg create mode 100644 docs/general/plots/sugarscape.svg diff --git a/docs/general/plots/boltzmann.svg b/docs/general/plots/boltzmann.svg new file mode 100644 index 00000000..b23fb9c3 --- /dev/null +++ b/docs/general/plots/boltzmann.svg @@ -0,0 +1,90 @@ + + + + + + + + 2025-10-16T19:57:07.933517 + image/svg+xml + + + Matplotlib v3.10.5, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/general/plots/sugarscape.svg b/docs/general/plots/sugarscape.svg new file mode 100644 index 00000000..189f5f79 --- /dev/null +++ b/docs/general/plots/sugarscape.svg @@ -0,0 +1,2 @@ + + From 3c4095d5fa379a63393dede6b4c0d45d556985fe Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 11:32:17 +0200 Subject: [PATCH 278/329] Implement code changes to enhance functionality and improve performance --- examples/boltzmann_wealth/benchmark.svg | 1083 ---------------------- examples/sugarscape_ig/benchmark.svg | 1091 ----------------------- 2 files changed, 2174 deletions(-) delete mode 100644 examples/boltzmann_wealth/benchmark.svg delete mode 100644 examples/sugarscape_ig/benchmark.svg diff --git a/examples/boltzmann_wealth/benchmark.svg b/examples/boltzmann_wealth/benchmark.svg deleted file mode 100644 index b3949bd0..00000000 --- a/examples/boltzmann_wealth/benchmark.svg +++ /dev/null @@ -1,1083 +0,0 @@ - - - - - - - - 2025-10-16T19:57:07.933517 - image/svg+xml - - - Matplotlib v3.10.5, https://matplotlib.org/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/examples/sugarscape_ig/benchmark.svg b/examples/sugarscape_ig/benchmark.svg deleted file mode 100644 index b7b95843..00000000 --- a/examples/sugarscape_ig/benchmark.svg +++ /dev/null @@ -1,1091 +0,0 @@ - - - - - - - - 2025-10-16T19:57:08.355947 - image/svg+xml - - - Matplotlib v3.10.5, https://matplotlib.org/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From d2511da519925bafd3024e771c4922a7494798b1 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 12:22:30 +0200 Subject: [PATCH 279/329] Implement code changes to enhance functionality and improve performance --- docs/general/plots/boltzmann.svg | 1077 ++++++++++++++++++++++++++-- docs/general/plots/sugarscape.svg | 1091 ++++++++++++++++++++++++++++- 2 files changed, 2125 insertions(+), 43 deletions(-) diff --git a/docs/general/plots/boltzmann.svg b/docs/general/plots/boltzmann.svg index b23fb9c3..b3949bd0 100644 --- a/docs/general/plots/boltzmann.svg +++ b/docs/general/plots/boltzmann.svg @@ -1,54 +1,54 @@ + "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> - - - - 2025-10-16T19:57:07.933517 - image/svg+xml - - - Matplotlib v3.10.5, https://matplotlib.org/ - - - - + + + + 2025-10-16T19:57:07.933517 + image/svg+xml + + + Matplotlib v3.10.5, https://matplotlib.org/ + + + + - + - - + - - - - + + - - - - - + + + - - - - - - + + + + - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + diff --git a/docs/general/plots/sugarscape.svg b/docs/general/plots/sugarscape.svg index 189f5f79..b7b95843 100644 --- a/docs/general/plots/sugarscape.svg +++ b/docs/general/plots/sugarscape.svg @@ -1,2 +1,1091 @@ - + + + + + + + 2025-10-16T19:57:08.355947 + image/svg+xml + + + Matplotlib v3.10.5, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From aa3fe6151337d906911df661c62abe72c9a0a256 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 12:23:09 +0200 Subject: [PATCH 280/329] docs: update benchmark image paths in README for improved accuracy --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0d0a48fe..075eab25 100644 --- a/README.md +++ b/README.md @@ -61,9 +61,9 @@ mesa-frames currently uses **Polars** as its backend. mesa-frames delivers consistent speedups across both toy and canonical ABMs. At 10k agents, it runs **~10× faster** than classic Mesa, and the gap grows with scale. -![Benchmark: Boltzmann Wealth](examples/boltzmann_wealth/benchmark.svg) +![Benchmark: Boltzmann Wealth](docs/general/plots/boltzmann.svg) -![Benchmark: Sugarscape IG](examples/sugarscape_ig/benchmark.svg) +![Benchmark: Sugarscape IG](docs/general/plots/sugarscape.svg) --- From ef724c2ac2a24b72ae8662a3f6658e6a26782281 Mon Sep 17 00:00:00 2001 From: Adam Amer <136176500+adamamer20@users.noreply.github.com> Date: Fri, 17 Oct 2025 14:19:29 +0200 Subject: [PATCH 281/329] docs: enhance performance descriptions in README for clarity and accuracy --- README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 075eab25..d6f33411 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,13 @@ mesa-frames currently uses **Polars** as its backend. [![Reproduce Benchmarks](https://img.shields.io/badge/Reproduce%20Benchmarks-📊-orange?style=for-the-badge)](https://github.com/projectmesa/mesa-frames/blob/main/benchmarks/README.md) -mesa-frames delivers consistent speedups across both toy and canonical ABMs. -At 10k agents, it runs **~10× faster** than classic Mesa, and the gap grows with scale. +**mesa-frames consistently outperforms classic Mesa across both toy and canonical ABMs.** + +In the Boltzmann model, it maintains near-constant runtimes even as agent count rises, achieving **up to 10× faster execution** at scale. + +In the more computation-intensive Sugarscape model, **mesa-frames roughly halves total runtime**. + +We still have room to optimize performance further (see [Roadmap](#roadmap)). ![Benchmark: Boltzmann Wealth](docs/general/plots/boltzmann.svg) @@ -123,7 +128,7 @@ uv sync --all-extras - Transition to LazyFrames for optimization and GPU support - Auto-vectorize existing Mesa models via decorator -- Increase possible Spaces +- Increase possible Spaces (Network, Continous...) - Refine the API to align to Mesa --- From a8107d2077e757bd9708c023e84ab2b72c78e82a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 17 Oct 2025 14:38:21 +0000 Subject: [PATCH 282/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- README.md | 4 +- docs/general/plots/boltzmann.svg | 1290 ++++++++++++++-------------- docs/general/plots/sugarscape.svg | 1302 ++++++++++++++--------------- 3 files changed, 1298 insertions(+), 1298 deletions(-) diff --git a/README.md b/README.md index d6f33411..d623bc40 100644 --- a/README.md +++ b/README.md @@ -58,9 +58,9 @@ mesa-frames currently uses **Polars** as its backend. [![Reproduce Benchmarks](https://img.shields.io/badge/Reproduce%20Benchmarks-📊-orange?style=for-the-badge)](https://github.com/projectmesa/mesa-frames/blob/main/benchmarks/README.md) -**mesa-frames consistently outperforms classic Mesa across both toy and canonical ABMs.** +**mesa-frames consistently outperforms classic Mesa across both toy and canonical ABMs.** -In the Boltzmann model, it maintains near-constant runtimes even as agent count rises, achieving **up to 10× faster execution** at scale. +In the Boltzmann model, it maintains near-constant runtimes even as agent count rises, achieving **up to 10× faster execution** at scale. In the more computation-intensive Sugarscape model, **mesa-frames roughly halves total runtime**. diff --git a/docs/general/plots/boltzmann.svg b/docs/general/plots/boltzmann.svg index b3949bd0..f21ca936 100644 --- a/docs/general/plots/boltzmann.svg +++ b/docs/general/plots/boltzmann.svg @@ -21,56 +21,56 @@ - - - - @@ -80,37 +80,37 @@ z - - @@ -124,24 +124,24 @@ z - - @@ -156,8 +156,8 @@ z - @@ -174,37 +174,37 @@ L 365.842287 82.463602 - - @@ -219,8 +219,8 @@ z - @@ -237,45 +237,45 @@ L 551.841874 82.463602 - - @@ -292,161 +292,161 @@ z - - - - - - @@ -462,8 +462,8 @@ z - @@ -475,8 +475,8 @@ L 672.741605 350.882566 - @@ -488,8 +488,8 @@ L 672.741605 284.457569 - @@ -502,8 +502,8 @@ L 672.741605 218.032571 - @@ -516,8 +516,8 @@ L 672.741605 151.607574 - @@ -532,200 +532,200 @@ L 672.741605 85.182576 - - - - - - - - - @@ -752,24 +752,24 @@ z - - @@ -784,24 +784,24 @@ z - - @@ -818,33 +818,33 @@ z - - - - @@ -854,46 +854,46 @@ L 106.792969 103.348993 - @@ -904,9 +904,9 @@ z - @@ -916,42 +916,42 @@ L 106.792969 126.688758 - - @@ -969,79 +969,79 @@ z - - - - diff --git a/docs/general/plots/sugarscape.svg b/docs/general/plots/sugarscape.svg index b7b95843..679002c9 100644 --- a/docs/general/plots/sugarscape.svg +++ b/docs/general/plots/sugarscape.svg @@ -21,56 +21,56 @@ - - - - @@ -80,37 +80,37 @@ z - - @@ -124,24 +124,24 @@ z - - @@ -156,8 +156,8 @@ z - @@ -174,37 +174,37 @@ L 377.312834 79.92 - - @@ -219,8 +219,8 @@ z - @@ -237,45 +237,45 @@ L 564.70333 79.92 - - @@ -292,161 +292,161 @@ z - - - - - - @@ -462,8 +462,8 @@ z - @@ -475,8 +475,8 @@ L 686.507152 350.763147 - @@ -489,8 +489,8 @@ L 686.507152 289.698824 - @@ -504,8 +504,8 @@ L 686.507152 228.634502 - @@ -519,8 +519,8 @@ L 686.507152 167.57018 - @@ -536,200 +536,200 @@ L 686.507152 106.505858 - - - - - - - - - @@ -756,24 +756,24 @@ z - - @@ -788,24 +788,24 @@ z - - @@ -822,33 +822,33 @@ z - - - - @@ -858,46 +858,46 @@ L 115.968516 100.805391 - @@ -908,9 +908,9 @@ z - @@ -920,42 +920,42 @@ L 115.968516 124.145156 - - @@ -973,82 +973,82 @@ z - - - From 47fc53e52e563aca1f9711c6da2dba6655775b91 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 17:07:04 +0200 Subject: [PATCH 283/329] fix: disable notebook execution in mkdocs configuration and adjust tutorial navigation --- mkdocs.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index 3b31177b..53ef819f 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -49,7 +49,7 @@ theme: plugins: - search - mkdocs-jupyter: - execute: true # Ensures the notebooks run and generate output + execute: false # Ensures the notebooks run and generate output - git-revision-date-localized: enable_creation_date: true - minify: @@ -113,10 +113,10 @@ nav: - User Guide: - Getting Started: user-guide/0_getting-started.md - Classes: user-guide/1_classes.md + - Tutorials: - Introductory Tutorial: user-guide/2_introductory-tutorial.ipynb - - Data Collector Tutorial: user-guide/4_datacollector.ipynb - Advanced Tutorial: user-guide/3_advanced-tutorial.ipynb - - Benchmarks: user-guide/5_benchmarks.md + - Data Collector Tutorial: user-guide/4_datacollector.ipynb - API Reference: api/index.html - Contributing: - Contribution Guide: contributing.md From 8ec84a09e00598b405b5743a5bc7903a76f8779f Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 17:13:31 +0200 Subject: [PATCH 284/329] fix: enable notebook execution in mkdocs configuration and restrict processing to .ipynb files --- mkdocs.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mkdocs.yml b/mkdocs.yml index 53ef819f..47f495f2 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -49,7 +49,8 @@ theme: plugins: - search - mkdocs-jupyter: - execute: false # Ensures the notebooks run and generate output + execute: true # Ensures the notebooks run and generate output + include: "**/*.ipynb" # Restrict processing to notebooks only (avoid executing raw .py tutorial files) - git-revision-date-localized: enable_creation_date: true - minify: From 165f3f76be685d3693e2d85f9aaabc95de764784 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 17:14:48 +0200 Subject: [PATCH 285/329] fix: update mkdocs-jupyter plugin configuration to restrict notebook processing to .ipynb files only --- mkdocs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs.yml b/mkdocs.yml index 47f495f2..bc374eed 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -50,7 +50,7 @@ plugins: - search - mkdocs-jupyter: execute: true # Ensures the notebooks run and generate output - include: "**/*.ipynb" # Restrict processing to notebooks only (avoid executing raw .py tutorial files) + include: ["*.ipynb"] # Restrict processing to notebooks only (avoid executing raw .py tutorial files) - git-revision-date-localized: enable_creation_date: true - minify: From 5e59e0503e485a69f1d8f6bea9ee728f3d5c1e0d Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 17:24:34 +0200 Subject: [PATCH 286/329] fix: remove outdated benchmarks documentation from user guide --- docs/general/user-guide/5_benchmarks.md | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 docs/general/user-guide/5_benchmarks.md diff --git a/docs/general/user-guide/5_benchmarks.md b/docs/general/user-guide/5_benchmarks.md deleted file mode 100644 index 233c394c..00000000 --- a/docs/general/user-guide/5_benchmarks.md +++ /dev/null @@ -1,21 +0,0 @@ -# Performance Boost 🏎️💨 - -mesa-frames offers significant performance improvements over the original mesa framework. Here are some benchmark results for different models: - -## Boltzmann Wealth Model 💰 - -[View the benchmark script](https://github.com/projectmesa/mesa-frames/blob/main/examples/boltzmann_wealth/performance_plot.py) - -### Comparison with mesa - -![Performance Graph BW](https://github.com/projectmesa/mesa-frames/raw/main/examples/boltzmann_wealth/boltzmann_with_mesa.png) - -### Comparison of mesa-frames implementations - -![Performance Graph BW without Mesa](https://github.com/projectmesa/mesa-frames/raw/main/examples/boltzmann_wealth/boltzmann_no_mesa.png) - -## SugarScape with Instantaneous Growback 🍬 - -[View the benchmark script](https://github.com/projectmesa/mesa-frames/blob/main/examples/sugarscape_ig/performance_comparison.py) - -![Performance Graph SS IG](https://github.com/projectmesa/mesa-frames/raw/main/examples/sugarscape_ig/mesa_comparison.png) From 65afb5b972bba138739ee548996851253f75f465 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 17:27:55 +0200 Subject: [PATCH 287/329] Add Data Collector tutorial in Jupyter Notebook and Python script - Created a new Jupyter Notebook `4_datacollector.ipynb` to demonstrate the usage of the `DataCollector` in `mesa-frames`, covering various storage backends and conditional triggers. - Added a corresponding Python script `4_datacollector.py` with the same content for users preferring script execution. - Updated `mkdocs.yml` to reflect the new tutorial paths under the Tutorials section. --- .gitignore | 5 +++-- .../{user-guide => tutorials}/2_introductory_tutorial.py | 0 .../{user-guide => tutorials}/3_advanced_tutorial.py | 0 docs/general/{user-guide => tutorials}/4_datacollector.py | 0 mkdocs.yml | 6 +++--- 5 files changed, 6 insertions(+), 5 deletions(-) rename docs/general/{user-guide => tutorials}/2_introductory_tutorial.py (100%) rename docs/general/{user-guide => tutorials}/3_advanced_tutorial.py (100%) rename docs/general/{user-guide => tutorials}/4_datacollector.py (100%) diff --git a/.gitignore b/.gitignore index 0034fd4a..11fa6a54 100644 --- a/.gitignore +++ b/.gitignore @@ -156,8 +156,9 @@ llm_rules.md .python-version docs/site docs/api/_build -docs/general/user-guide/data_csv -docs/general/user-guide/data_parquet +docs/general/tutorials/data_csv +docs/general/tutorials/data_parquet +docs/general/tutorials/*.ipynb docs/api/reference/**/mesa_frames.*.rst examples/**/results benchmarks/**/results diff --git a/docs/general/user-guide/2_introductory_tutorial.py b/docs/general/tutorials/2_introductory_tutorial.py similarity index 100% rename from docs/general/user-guide/2_introductory_tutorial.py rename to docs/general/tutorials/2_introductory_tutorial.py diff --git a/docs/general/user-guide/3_advanced_tutorial.py b/docs/general/tutorials/3_advanced_tutorial.py similarity index 100% rename from docs/general/user-guide/3_advanced_tutorial.py rename to docs/general/tutorials/3_advanced_tutorial.py diff --git a/docs/general/user-guide/4_datacollector.py b/docs/general/tutorials/4_datacollector.py similarity index 100% rename from docs/general/user-guide/4_datacollector.py rename to docs/general/tutorials/4_datacollector.py diff --git a/mkdocs.yml b/mkdocs.yml index bc374eed..33a08d51 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -115,9 +115,9 @@ nav: - Getting Started: user-guide/0_getting-started.md - Classes: user-guide/1_classes.md - Tutorials: - - Introductory Tutorial: user-guide/2_introductory-tutorial.ipynb - - Advanced Tutorial: user-guide/3_advanced-tutorial.ipynb - - Data Collector Tutorial: user-guide/4_datacollector.ipynb + - Introductory Tutorial: tutorials/2_introductory-tutorial.ipynb + - Advanced Tutorial: tutorials/3_advanced-tutorial.ipynb + - Data Collector Tutorial: tutorials/4_datacollector.ipynb - API Reference: api/index.html - Contributing: - Contribution Guide: contributing.md From 5511154ba44a8a75568f554f746c096a1d1063b0 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 17:39:16 +0200 Subject: [PATCH 288/329] fix: update agent_reporters format in Sugarscape model --- docs/general/tutorials/3_advanced_tutorial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/tutorials/3_advanced_tutorial.py b/docs/general/tutorials/3_advanced_tutorial.py index b1009734..38cba96c 100644 --- a/docs/general/tutorials/3_advanced_tutorial.py +++ b/docs/general/tutorials/3_advanced_tutorial.py @@ -327,7 +327,7 @@ def __init__( "corr_sugar_metabolism": corr_sugar_metabolism, "corr_sugar_vision": corr_sugar_vision, }, - agent_reporters={"traits": ["sugar", "metabolism", "vision"]}, + agent_reporters=["sugar", "metabolism", "vision"], ) self.datacollector.collect() From e6499c9b0dc8f583f257535100941d9c4d7da385 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 17:42:54 +0200 Subject: [PATCH 289/329] fix: correct file naming in Tutorials section of mkdocs configuration --- mkdocs.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index 33a08d51..d015b1f3 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -115,8 +115,8 @@ nav: - Getting Started: user-guide/0_getting-started.md - Classes: user-guide/1_classes.md - Tutorials: - - Introductory Tutorial: tutorials/2_introductory-tutorial.ipynb - - Advanced Tutorial: tutorials/3_advanced-tutorial.ipynb + - Introductory Tutorial: tutorials/2_introductory_tutorial.ipynb + - Advanced Tutorial: tutorials/3_advanced_tutorial.ipynb - Data Collector Tutorial: tutorials/4_datacollector.ipynb - API Reference: api/index.html - Contributing: From ec2b1568a2f7c971a6afce7857cc5264c4404a0c Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 17:56:44 +0200 Subject: [PATCH 290/329] fix: update agent_reporters format in Sugarscape model to use explicit key-value pairs --- docs/general/tutorials/3_advanced_tutorial.py | 12 ++++++++---- examples/sugarscape_ig/backend_frames/model.py | 6 +++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/docs/general/tutorials/3_advanced_tutorial.py b/docs/general/tutorials/3_advanced_tutorial.py index 38cba96c..bd5c7b10 100644 --- a/docs/general/tutorials/3_advanced_tutorial.py +++ b/docs/general/tutorials/3_advanced_tutorial.py @@ -327,7 +327,11 @@ def __init__( "corr_sugar_metabolism": corr_sugar_metabolism, "corr_sugar_vision": corr_sugar_vision, }, - agent_reporters=["sugar", "metabolism", "vision"], + agent_reporters={ + "sugar": "sugar", + "metabolism": "metabolism", + "vision": "vision", + } ) self.datacollector.collect() @@ -1459,9 +1463,9 @@ def _resolve_conflicts_in_rounds( # %% -GRID_WIDTH = 40 -GRID_HEIGHT = 40 -NUM_AGENTS = 400 +GRID_WIDTH = 20 +GRID_HEIGHT = 20 +NUM_AGENTS = 100 MODEL_STEPS = 60 MAX_SUGAR = 4 SEED = 42 diff --git a/examples/sugarscape_ig/backend_frames/model.py b/examples/sugarscape_ig/backend_frames/model.py index 0aba1188..1a5d336b 100644 --- a/examples/sugarscape_ig/backend_frames/model.py +++ b/examples/sugarscape_ig/backend_frames/model.py @@ -268,7 +268,11 @@ def __init__( "corr_sugar_metabolism": corr_sugar_metabolism, "corr_sugar_vision": corr_sugar_vision, }, - agent_reporters={"traits": ["sugar", "metabolism", "vision"]}, + agent_reporters={ + "sugar": "sugar", + "metabolism": "metabolism", + "vision": "vision", + }, storage=storage, storage_uri=storage_uri, ) From a9fc3bbdbdd0ac99e2dad5a67ce6370224fd05e0 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:01:21 +0200 Subject: [PATCH 291/329] fix: comment out installation commands in introductory and advanced tutorials --- docs/general/tutorials/2_introductory_tutorial.py | 2 +- docs/general/tutorials/3_advanced_tutorial.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/general/tutorials/2_introductory_tutorial.py b/docs/general/tutorials/2_introductory_tutorial.py index 8560034a..92f6f1f9 100644 --- a/docs/general/tutorials/2_introductory_tutorial.py +++ b/docs/general/tutorials/2_introductory_tutorial.py @@ -9,7 +9,7 @@ Run the following cell to install `mesa-frames` if you are using Google Colab.""" # %% -# !pip install git+https://github.com/projectmesa/mesa-frames mesa +# #!pip install git+https://github.com/projectmesa/mesa-frames mesa # %% [markdown] """ # Introductory Tutorial: Boltzmann Wealth Model with mesa-frames 💰🚀 diff --git a/docs/general/tutorials/3_advanced_tutorial.py b/docs/general/tutorials/3_advanced_tutorial.py index bd5c7b10..4883e7ca 100644 --- a/docs/general/tutorials/3_advanced_tutorial.py +++ b/docs/general/tutorials/3_advanced_tutorial.py @@ -56,7 +56,7 @@ # uncomment the cell below to install the required dependencies. # %% -# !pip install git+https://github.com/projectmesa/mesa-frames polars numba numpy +# #!pip install git+https://github.com/projectmesa/mesa-frames polars numba numpy # %% [markdown] """## 1. Imports""" From ddfa06f0c9118cb391b70cefcd30393b9fcfc097 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:12:11 +0200 Subject: [PATCH 292/329] fix: update contributing guidelines for clarity and completeness --- CONTRIBUTING.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bb8b4148..407dff82 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,16 +15,22 @@ Before contributing, we recommend reviewing our [roadmap](https://projectmesa.gi Before you begin contributing, ensure that you have the necessary tools installed: - **Install Python** (at least the version specified in `requires-python` of `pyproject.toml`). 🐍 + - We recommend using a virtual environment manager like: - - [Astral's UV](https://docs.astral.sh/uv/#installation) 🌟 - - [Hatch](https://hatch.pypa.io/latest/install/) 🏗️ + + - [Astral's UV](https://docs.astral.sh/uv/#installation) 🌟 + - [Hatch](https://hatch.pypa.io/latest/install/) 🏗️ + - Install **pre-commit** to enforce code quality standards before pushing changes: - - [Pre-commit installation guide](https://pre-commit.com/#install) ✅ - - [More about pre-commit hooks](https://stackoverflow.com/collectives/articles/71270196/how-to-use-pre-commit-to-automatically-correct-commits-and-merge-requests-with-g) + + - [Pre-commit installation guide](https://pre-commit.com/#install) ✅ + - [More about pre-commit hooks](https://stackoverflow.com/collectives/articles/71270196/how-to-use-pre-commit-to-automatically-correct-commits-and-merge-requests-with-g) + - If using **VS Code**, consider installing these extensions to automatically enforce formatting: - - [Ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) – Python linting & formatting 🐾 - - [Markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint) – Markdown linting (for documentation) ✍️ - - [Git Hooks](https://marketplace.visualstudio.com/items?itemName=lakshmikanthayyadevara.githooks) – Automatically runs & visualizes pre-commit hooks 🔗 + + - [Ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) – Python linting & formatting 🐾 + - [Markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint) – Markdown linting (for documentation) ✍️ + - [Git Hooks](https://marketplace.visualstudio.com/items?itemName=lakshmikanthayyadevara.githooks) – Automatically runs & visualizes pre-commit hooks 🔗 --- From b60686c9da38de21ced4787a3febf6653f4675ef Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:12:55 +0200 Subject: [PATCH 293/329] fix: update sample method calls in tutorials for consistency and accuracy --- docs/general/tutorials/3_advanced_tutorial.py | 2 +- docs/general/user-guide/0_getting-started.md | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/general/tutorials/3_advanced_tutorial.py b/docs/general/tutorials/3_advanced_tutorial.py index 4883e7ca..bf501ec3 100644 --- a/docs/general/tutorials/3_advanced_tutorial.py +++ b/docs/general/tutorials/3_advanced_tutorial.py @@ -331,7 +331,7 @@ def __init__( "sugar": "sugar", "metabolism": "metabolism", "vision": "vision", - } + }, ) self.datacollector.collect() diff --git a/docs/general/user-guide/0_getting-started.md b/docs/general/user-guide/0_getting-started.md index 51ebe319..76fd7cad 100644 --- a/docs/general/user-guide/0_getting-started.md +++ b/docs/general/user-guide/0_getting-started.md @@ -42,7 +42,7 @@ Here's a comparison between mesa-frames and mesa: self.select(self.wealth > 0) # Receiving agents are sampled (only native expressions currently supported) - other_agents = self.model.sets.sample( + other_agents = self.df.sample( n=len(self.active_agents), with_replacement=True ) @@ -92,9 +92,9 @@ If you're familiar with mesa, this guide will help you understand the key differ }) def step(self): givers = self.wealth > 0 - receivers = self.model.sets.sample(n=len(self.active_agents)) + receivers = self.df.sample(n=len(self.active_agents), with_replacement=True) self[givers, "wealth"] -= 1 - new_wealth = receivers.groupby("unique_id").count() + new_wealth = receivers.group_by("unique_id").len() self[new_wealth["unique_id"], "wealth"] += new_wealth["count"] ``` @@ -163,4 +163,4 @@ When simultaneous activation is not possible, you need to handle race conditions 2. **Looping Mechanism 🔁**: Implement a looping mechanism on vectorized operations. -For a more detailed implementation of handling race conditions, please refer to the `examples/sugarscape-ig` in the mesa-frames repository. This example demonstrates how to implement the Sugarscape model with instantaneous growback, which requires careful handling of sequential agent actions. +For a more detailed implementation of handling race conditions, please refer to the `examples/sugarscape_ig` in the mesa-frames repository. This example demonstrates how to implement the Sugarscape model with instantaneous growback, which requires careful handling of sequential agent actions. From 643cca0debbb256749472497a23a913dcf9f4c32 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:13:06 +0200 Subject: [PATCH 294/329] fix: format installation instructions for consistency in contributing guidelines --- CONTRIBUTING.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 407dff82..5f5256b9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,19 +18,19 @@ Before you begin contributing, ensure that you have the necessary tools installe - We recommend using a virtual environment manager like: - - [Astral's UV](https://docs.astral.sh/uv/#installation) 🌟 - - [Hatch](https://hatch.pypa.io/latest/install/) 🏗️ + - [Astral's UV](https://docs.astral.sh/uv/#installation) 🌟 + - [Hatch](https://hatch.pypa.io/latest/install/) 🏗️ - Install **pre-commit** to enforce code quality standards before pushing changes: - - [Pre-commit installation guide](https://pre-commit.com/#install) ✅ - - [More about pre-commit hooks](https://stackoverflow.com/collectives/articles/71270196/how-to-use-pre-commit-to-automatically-correct-commits-and-merge-requests-with-g) + - [Pre-commit installation guide](https://pre-commit.com/#install) ✅ + - [More about pre-commit hooks](https://stackoverflow.com/collectives/articles/71270196/how-to-use-pre-commit-to-automatically-correct-commits-and-merge-requests-with-g) - If using **VS Code**, consider installing these extensions to automatically enforce formatting: - - [Ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) – Python linting & formatting 🐾 - - [Markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint) – Markdown linting (for documentation) ✍️ - - [Git Hooks](https://marketplace.visualstudio.com/items?itemName=lakshmikanthayyadevara.githooks) – Automatically runs & visualizes pre-commit hooks 🔗 + - [Ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) – Python linting & formatting 🐾 + - [Markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint) – Markdown linting (for documentation) ✍️ + - [Git Hooks](https://marketplace.visualstudio.com/items?itemName=lakshmikanthayyadevara.githooks) – Automatically runs & visualizes pre-commit hooks 🔗 --- From cf0815a2bcabc627739d1dbdfd321adf017ab0ee Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:13:37 +0200 Subject: [PATCH 295/329] fix: update mkdocs configuration to disable notebook execution and add edit URI --- mkdocs.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mkdocs.yml b/mkdocs.yml index d015b1f3..0ec65c01 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -3,6 +3,7 @@ site_name: mesa-frames documentation site_url: https://projectmesa.github.io/mesa-frames repo_url: https://github.com/projectmesa/mesa-frames repo_name: projectmesa/mesa-frames +edit_uri: edit/main/docs/general/ docs_dir: docs/general # Theme configuration @@ -49,7 +50,7 @@ theme: plugins: - search - mkdocs-jupyter: - execute: true # Ensures the notebooks run and generate output + execute: false # Ensures the notebooks run and generate output include: ["*.ipynb"] # Restrict processing to notebooks only (avoid executing raw .py tutorial files) - git-revision-date-localized: enable_creation_date: true From 991a3ccabb2934b81d578928a942f84faea4a2df Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:14:07 +0200 Subject: [PATCH 296/329] fix: correct variable name in wealth distribution example for accuracy --- docs/general/user-guide/0_getting-started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/user-guide/0_getting-started.md b/docs/general/user-guide/0_getting-started.md index 76fd7cad..0566e643 100644 --- a/docs/general/user-guide/0_getting-started.md +++ b/docs/general/user-guide/0_getting-started.md @@ -95,7 +95,7 @@ If you're familiar with mesa, this guide will help you understand the key differ receivers = self.df.sample(n=len(self.active_agents), with_replacement=True) self[givers, "wealth"] -= 1 new_wealth = receivers.group_by("unique_id").len() - self[new_wealth["unique_id"], "wealth"] += new_wealth["count"] + self[new_wealth["unique_id"], "wealth"] += new_wealth["len"] ``` === "mesa" From a42d5b2be56268191d98067c9c8aab38e64fcac7 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:19:25 +0200 Subject: [PATCH 297/329] fix: update getting started guide for clarity and completeness --- docs/general/user-guide/0_getting-started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general/user-guide/0_getting-started.md b/docs/general/user-guide/0_getting-started.md index 0566e643..f4dbf049 100644 --- a/docs/general/user-guide/0_getting-started.md +++ b/docs/general/user-guide/0_getting-started.md @@ -163,4 +163,4 @@ When simultaneous activation is not possible, you need to handle race conditions 2. **Looping Mechanism 🔁**: Implement a looping mechanism on vectorized operations. -For a more detailed implementation of handling race conditions, please refer to the `examples/sugarscape_ig` in the mesa-frames repository. This example demonstrates how to implement the Sugarscape model with instantaneous growback, which requires careful handling of sequential agent actions. +For a more detailed implementation of handling race conditions, see the [Advanced Tutorial](../tutorials/3_advanced_tutorial.ipynb). It walks through the Sugarscape model with instantaneous growback and shows practical patterns for staged vectorization and conflict resolution. From 7b50f78fd99dc8e6778c46d2182ace5c6618d7df Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:26:13 +0200 Subject: [PATCH 298/329] fix: enhance runtime type checking section in contributing guide for clarity and detail --- CONTRIBUTING.md | 22 ++-- docs/general/development/index.md | 170 ------------------------------ mkdocs.yml | 1 - 3 files changed, 12 insertions(+), 181 deletions(-) delete mode 100644 docs/general/development/index.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5f5256b9..9f62f4f8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -99,22 +99,24 @@ This creates `.venv/` and installs mesa-frames with the development extras. uv run pytest -q --cov=mesa_frames --cov-report=term-missing ``` -- **Optional: Enable runtime type checking** during development for enhanced type safety: +-- **Optional: Runtime Type Checking (beartype)** 🔍 + + You can enable stricter runtime validation of function arguments/returns with `beartype` during local development: ```sh MESA_FRAMES_RUNTIME_TYPECHECKING=1 uv run pytest -q --cov=mesa_frames --cov-report=term-missing ``` - !!! tip "Automatically Enabled" - Runtime type checking is automatically enabled in these scenarios: - - - **Hatch development environment** (`hatch shell dev`) - - **VS Code debugging** (when using the debugger) - - **VS Code testing** (when running tests through VS Code's testing interface) - - No manual setup needed in these environments! + Quick facts: + - Automatically enabled in: Hatch dev env (`hatch shell dev`), VS Code debugger, and VS Code test runs. + - Enable manually by exporting `MESA_FRAMES_RUNTIME_TYPECHECKING=1` (any of 1/true/yes). + - Use only for development/debugging; adds overhead—disable for performance measurements or large simulations. + - Unset with your shell (e.g. `unset`/`Remove-Item Env:` depending on shell) to turn it off. - For more details on runtime type checking, see the [Development Guidelines](https://projectmesa.github.io/mesa-frames/development/). + Example for a one-off test run: + ```sh + MESA_FRAMES_RUNTIME_TYPECHECKING=1 uv run pytest -q + ``` #### **Step 6: Documentation Updates (If Needed)** 📖 diff --git a/docs/general/development/index.md b/docs/general/development/index.md deleted file mode 100644 index 8dbfd993..00000000 --- a/docs/general/development/index.md +++ /dev/null @@ -1,170 +0,0 @@ -# Development Guidelines - -## Runtime Type Checking 🔍 - -mesa-frames includes optional runtime type checking using [beartype](https://github.com/beartype/beartype) for development and debugging purposes. This feature helps catch type-related errors early during development and testing. - -!!! tip "Automatically Enabled" - Runtime type checking is **automatically enabled** in the following scenarios: - - - **Hatch development environment** (`hatch shell dev`) — via `pyproject.toml` configuration - - **VS Code debugging** — when using the debugger (`F5` or "Python Debugger: Current File") - - **VS Code testing** — when running tests through VS Code's testing interface - - No manual setup required in these environments! - -### Development Environment Setup - -#### Option 1: Hatch Development Environment (Recommended) - -The easiest way to enable runtime type checking is to use Hatch's development environment: - -```bash -# Enter the development environment (auto-enables runtime type checking) -hatch shell dev - -# Verify it's enabled -python -c "import os; print('Runtime type checking:', os.getenv('MESA_FRAMES_RUNTIME_TYPECHECKING'))" -# → Runtime type checking: true -``` - -#### Option 2: Manual Environment Variable - -For other development setups, you can manually enable runtime type checking: - -Runtime type checking can be enabled by setting the `MESA_FRAMES_RUNTIME_TYPECHECKING` environment variable: - -```bash -export MESA_FRAMES_RUNTIME_TYPECHECKING=1 -# or -export MESA_FRAMES_RUNTIME_TYPECHECKING=true -# or -export MESA_FRAMES_RUNTIME_TYPECHECKING=yes -``` - -### Usage Examples - -!!! info "Automatic Activation" - If you're using **Hatch dev environment**, **VS Code debugging**, or **VS Code testing**, runtime type checking is already enabled automatically. The examples below are for manual activation in other scenarios. - -#### For Development and Testing - -```bash -# Enable runtime type checking for testing -MESA_FRAMES_RUNTIME_TYPECHECKING=1 uv run pytest - -# Enable runtime type checking for running scripts -MESA_FRAMES_RUNTIME_TYPECHECKING=1 uv run python your_script.py -``` - -#### In Your IDE or Development Environment - -**VS Code** (Already Configured): - -- **Debugging**: Runtime type checking is automatically enabled when using VS Code's debugger -- **Testing**: Automatically enabled when running tests through VS Code's testing interface -- **Manual override**: You can also add it manually in `.vscode/settings.json`: - - ```json - { - "python.env": { - "MESA_FRAMES_RUNTIME_TYPECHECKING": "1" - } - } - ``` - -**PyCharm**: -In your run configuration, add the environment variable: - -```bash -MESA_FRAMES_RUNTIME_TYPECHECKING=1 -``` - -### How It Works - -When enabled, the runtime type checking system: - -1. **Automatically instruments** all mesa-frames packages with beartype decorators -2. **Validates function arguments** and return values at runtime -3. **Provides detailed error messages** when type mismatches occur -4. **Helps catch type-related bugs** during development - -### Requirements - -Runtime type checking requires the optional `beartype` dependency: - -```bash -# Install beartype for runtime type checking -uv add beartype -# or -pip install beartype -``` - -!!! note "Optional Dependency" - If `beartype` is not installed and runtime type checking is enabled, mesa-frames will issue a warning and continue without type checking. - -### Performance Considerations - -!!! warning "Development Only" - Runtime type checking adds significant overhead and should **only be used during development and testing**. Do not enable it in production environments. - -The overhead includes: - -- Function call interception and validation -- Type checking computations at runtime -- Memory usage for type checking infrastructure - -### When to Use Runtime Type Checking - -✅ **Automatically enabled (recommended):** - -- Hatch development environment (`hatch shell dev`) -- VS Code debugging sessions -- VS Code test execution -- Contributing to mesa-frames development - -✅ **Manual activation (when needed):** - -- Development and debugging in other IDEs -- Writing new features outside VS Code -- Running unit tests from command line -- Troubleshooting type-related issues - -❌ **Not recommended for:** - -- Production deployments -- Performance benchmarking -- Large-scale simulations -- Final model runs - -### Troubleshooting - -If you encounter issues with runtime type checking: - -1. **Check beartype installation:** - - ```bash - uv run python -c "import beartype; print(beartype.__version__)" - ``` - -2. **Verify environment variable:** - - ```bash - echo $MESA_FRAMES_RUNTIME_TYPECHECKING - ``` - -3. **For automatic configurations:** - - **Hatch dev**: Ensure you're in the dev environment (`hatch shell dev`) - - **VS Code debugging**: Check that the debugger configuration in `.vscode/launch.json` includes the environment variable - - **VS Code testing**: Verify that `.env.test` file exists and contains `MESA_FRAMES_RUNTIME_TYPECHECKING=true` - -4. **Check for warnings** in your application logs - -5. **Disable temporarily** if needed: - - ```bash - unset MESA_FRAMES_RUNTIME_TYPECHECKING - ``` - -!!! tip "Pro Tip" - Runtime type checking is particularly useful when developing custom AgentSet implementations or working with complex DataFrame operations where type safety is crucial. diff --git a/mkdocs.yml b/mkdocs.yml index 0ec65c01..071c9ee6 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -122,5 +122,4 @@ nav: - API Reference: api/index.html - Contributing: - Contribution Guide: contributing.md - - Development Guidelines: development/index.md - Roadmap: roadmap.md From be92cdc8e87a4a187e2a807de88eb683b07b2d85 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:40:49 +0200 Subject: [PATCH 299/329] fix: improve formatting and indentation in contributing guidelines for clarity --- CONTRIBUTING.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9f62f4f8..265d0d27 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -16,21 +16,21 @@ Before you begin contributing, ensure that you have the necessary tools installe - **Install Python** (at least the version specified in `requires-python` of `pyproject.toml`). 🐍 -- We recommend using a virtual environment manager like: +-- We recommend using a virtual environment manager like: - - [Astral's UV](https://docs.astral.sh/uv/#installation) 🌟 - - [Hatch](https://hatch.pypa.io/latest/install/) 🏗️ + - [Astral's UV](https://docs.astral.sh/uv/#installation) 🌟 + - [Hatch](https://hatch.pypa.io/latest/install/) 🏗️ - Install **pre-commit** to enforce code quality standards before pushing changes: - [Pre-commit installation guide](https://pre-commit.com/#install) ✅ - [More about pre-commit hooks](https://stackoverflow.com/collectives/articles/71270196/how-to-use-pre-commit-to-automatically-correct-commits-and-merge-requests-with-g) -- If using **VS Code**, consider installing these extensions to automatically enforce formatting: +-- If using **VS Code**, consider installing these extensions to automatically enforce formatting: - - [Ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) – Python linting & formatting 🐾 - - [Markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint) – Markdown linting (for documentation) ✍️ - - [Git Hooks](https://marketplace.visualstudio.com/items?itemName=lakshmikanthayyadevara.githooks) – Automatically runs & visualizes pre-commit hooks 🔗 + - [Ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) – Python linting & formatting 🐾 + - [Markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint) – Markdown linting (for documentation) ✍️ + - [Git Hooks](https://marketplace.visualstudio.com/items?itemName=lakshmikanthayyadevara.githooks) – Automatically runs & visualizes pre-commit hooks 🔗 --- From 1cf6ae628c1aa2f380f269c8d5cb4f39f2e9aed7 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:41:57 +0200 Subject: [PATCH 300/329] fix: improve formatting of runtime type checking section in contributing guide for clarity --- CONTRIBUTING.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 265d0d27..82a340c9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -108,12 +108,14 @@ This creates `.venv/` and installs mesa-frames with the development extras. ``` Quick facts: - - Automatically enabled in: Hatch dev env (`hatch shell dev`), VS Code debugger, and VS Code test runs. - - Enable manually by exporting `MESA_FRAMES_RUNTIME_TYPECHECKING=1` (any of 1/true/yes). - - Use only for development/debugging; adds overhead—disable for performance measurements or large simulations. - - Unset with your shell (e.g. `unset`/`Remove-Item Env:` depending on shell) to turn it off. + +- Automatically enabled in: Hatch dev env (`hatch shell dev`), VS Code debugger, and VS Code test runs. +- Enable manually by exporting `MESA_FRAMES_RUNTIME_TYPECHECKING=1` (any of 1/true/yes). +- Use only for development/debugging; adds overhead—disable for performance measurements or large simulations. +- Unset with your shell (e.g. `unset`/`Remove-Item Env:` depending on shell) to turn it off. Example for a one-off test run: + ```sh MESA_FRAMES_RUNTIME_TYPECHECKING=1 uv run pytest -q ``` From ef463d25e3f2446ef0182eed69ff378ccef63e08 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:46:56 +0200 Subject: [PATCH 301/329] fix: enhance clarity and detail in vectorized operations section of getting started guide --- docs/general/user-guide/0_getting-started.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/general/user-guide/0_getting-started.md b/docs/general/user-guide/0_getting-started.md index f4dbf049..9f412c93 100644 --- a/docs/general/user-guide/0_getting-started.md +++ b/docs/general/user-guide/0_getting-started.md @@ -15,20 +15,20 @@ Objects can be easily subclassed to respect mesa's object-oriented philosophy. ### Vectorized Operations ⚡ -mesa-frames leverages the power of vectorized operations provided by DataFrame libraries: +`mesa-frames` leverages **Polars** to replace Python loops with **column-wise expressions** executed in native Rust. +This allows you to update all agents simultaneously, the main source of `mesa-frames`' performance advantage. -- Operations are performed on entire columns of data at once -- This approach is significantly faster than iterating over individual agents -- Complex behaviors can be expressed in fewer lines of code +Unlike traditional `mesa` models, where the **activation order** of agents can affect results (see [Comer, 2014](http://mars.gmu.edu/bitstream/handle/1920/9070/Comer_gmu_0883E_10539.pdf)), +`mesa-frames` processes all agents **in parallel by default**. +This removes order-dependent effects, though you should handle conflicts explicitly when sequential logic is required. -Default to vectorized operations when expressing agent behaviour; that's where mesa-frames gains most of its speed-ups. If your agents must act sequentially (for example, to resolve conflicts or enforce ordering), fall back to loops or staged vectorized passes—mesa-frames will behave more like base mesa in those situations. We'll unpack these trade-offs in the SugarScape advanced tutorial. +!!! tip "Best practice" + Always start by expressing agent logic in a vectorized form. + Fall back to loops only when ordering or conflict resolution is essential. -It's important to note that in traditional `mesa` models, the order in which agents are activated can significantly impact the results of the model (see [Comer, 2014](http://mars.gmu.edu/bitstream/handle/1920/9070/Comer_gmu_0883E_10539.pdf)). `mesa-frames`, by default, doesn't have this issue as all agents are processed simultaneously. However, this comes with the trade-off of needing to carefully implement conflict resolution mechanisms when sequential processing is required. We'll discuss how to handle these situations later in this guide. +For a deeper understanding of vectorization and why it accelerates computation, see: -Check out these resources to understand vectorization and why it speeds up the code: - -- [What is vectorization?](https://stackoverflow.com/a/1422181) -- [Vectorization Explained, Step by Step](https://machinelearningcompass.com/machine_learning_math/vectorization/) +- [How vectorization speeds up your Python code — PythonSpeed](https://pythonspeed.com/articles/vectorization-python) Here's a comparison between mesa-frames and mesa: From be3072634ebaf3692b8738abf58a676dc0faff32 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 18:54:00 +0200 Subject: [PATCH 302/329] feat: add seed property and setter to Model class for random generator management --- mesa_frames/concrete/model.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index b91db207..00b1cf25 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -113,6 +113,28 @@ def reset_randomizer(self, seed: int | Sequence[int] | None) -> None: self._seed = seed self.random = np.random.default_rng(seed=self._seed) + @property + def seed(self) -> int | Sequence[int]: + """Return the current seed used by the model's random generator. + + Returns + ------- + int | Sequence[int] + The seed that initialized the underlying RNG. + """ + return self._seed + + @seed.setter + def seed(self, seed: int | Sequence[int] | None) -> None: + """Reset the model random generator using a new seed. + + Parameters + ---------- + seed : int | Sequence[int] | None + A new seed value; falls back to system entropy when ``None``. + """ + self.reset_randomizer(seed) + def run_model(self) -> None: """Run the model until the end condition is reached. From 8576a33370f8b1819dc1085acc1551b3c621596e Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 19:00:02 +0200 Subject: [PATCH 303/329] fix: update example sections to pluralize for consistency in AbstractDataCollector docstrings --- mesa_frames/abstract/datacollector.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mesa_frames/abstract/datacollector.py b/mesa_frames/abstract/datacollector.py index 6505408f..dd5cce57 100644 --- a/mesa_frames/abstract/datacollector.py +++ b/mesa_frames/abstract/datacollector.py @@ -121,8 +121,8 @@ def collect(self) -> None: This method calls _collect() to perform actual data collection. - Example - ------- + Examples + -------- >>> datacollector.collect() """ self._collect() @@ -133,8 +133,8 @@ def conditional_collect(self) -> None: This method calls _collect() to perform actual data collection only if trigger returns True - Example - ------- + Examples + -------- >>> datacollector.conditional_collect() """ if self._should_collect(): @@ -166,8 +166,8 @@ def data(self) -> Any: """ Returns collected data currently in memory as a dataframe. - Example: - ------- + Examples + -------- >>> df = datacollector.data >>> print(df) """ @@ -183,8 +183,8 @@ def flush(self) -> None: use this method to save collected data. - Example - ------- + Examples + -------- >>> datacollector.flush() >>> # Data is saved externally and in-memory buffers are cleared if configured """ @@ -219,7 +219,7 @@ def seed(self) -> int: """ Function to get the model seed. - Example: + Examples -------- >>> seed = datacollector.seed """ From 54fc12ca7189780ee50c34a4fbbe6221010f17b3 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 19:00:15 +0200 Subject: [PATCH 304/329] fix: update documentation for DataCollector to enhance clarity and consistency --- docs/api/reference/datacollector.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api/reference/datacollector.rst b/docs/api/reference/datacollector.rst index f1f2c68e..e95e2672 100644 --- a/docs/api/reference/datacollector.rst +++ b/docs/api/reference/datacollector.rst @@ -1,5 +1,5 @@ Data Collection -===== +=============== .. currentmodule:: mesa_frames @@ -65,4 +65,4 @@ API reference .. autoclass:: DataCollector :autosummary: - :autosummary-nosignatures: \ No newline at end of file + :autosummary-nosignatures: From a0b45eee4a2786340ebd916ffb81a6187e424b01 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 19:00:58 +0200 Subject: [PATCH 305/329] fix: correct introductory sentence in Model documentation for clarity --- docs/api/reference/model.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api/reference/model.rst b/docs/api/reference/model.rst index 74b7e4e5..0fb12b55 100644 --- a/docs/api/reference/model.rst +++ b/docs/api/reference/model.rst @@ -6,7 +6,7 @@ Model Quick intro ----------- -``Model`` orchestrates the simulation lifecycle: creating and registering ``AgentSet``s, stepping the simulation, and integrating with ``DataCollector`` and spatial ``Grid``s. Typical usage: +The ``Model`` orchestrates the simulation lifecycle: creating and registering ``AgentSet``s, stepping the simulation, and integrating with ``DataCollector`` and spatial ``Grid``s. Typical usage: - Instantiate ``Model``, add ``AgentSet`` instances to ``model.sets``. - Call ``model.sets.do('step')`` inside your model loop to trigger set-level updates. @@ -66,4 +66,4 @@ API reference .. autoclass:: Model :autosummary: - :autosummary-nosignatures: \ No newline at end of file + :autosummary-nosignatures: From f44eb182265b9ec483c9e8e9ffe28ba56ca7a4c9 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 19:15:50 +0200 Subject: [PATCH 306/329] fix: enhance documentation for AbstractAgentSet methods and add new abstract methods for selection, shuffling, and sorting --- mesa_frames/abstract/agentset.py | 118 ++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 2 deletions(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index ae5db2db..86a6ca90 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -21,7 +21,7 @@ from abc import abstractmethod from collections.abc import Collection, Iterable, Iterator from contextlib import suppress -from typing import Any, Literal, Self, overload +from typing import Any, Callable, Literal, Self, Sequence, overload from numpy.random import Generator @@ -222,7 +222,25 @@ def get( self, attr_names: str | Collection[str] | None = None, mask: AgentMask | None = None, - ) -> Series | DataFrame: ... + ) -> Series | DataFrame: + """Retrieve agent attributes as a Series or DataFrame. + + Parameters + ---------- + attr_names : str | Collection[str] | None, optional + Column name or collection of names to fetch. When ``None``, return + all agent attributes (excluding any internal identifiers). + mask : AgentMask | None, optional + Subset selector limiting which agents are included. ``None`` means + operate on the full set. + + Returns + ------- + Series | DataFrame + A Series when selecting a single attribute, otherwise a DataFrame + containing the requested columns. + """ + ... @abstractmethod def step(self) -> None: @@ -458,14 +476,35 @@ def name(self) -> str: @property def model(self) -> mesa_frames.concrete.model.Model: + """Return the parent model for this agent set. + + Returns + ------- + mesa_frames.concrete.model.Model + The model instance that owns this agent set. + """ return self._model @property def random(self) -> Generator: + """Return the random number generator shared with the model. + + Returns + ------- + numpy.random.Generator + Generator used for stochastic operations. + """ return self.model.random @property def space(self) -> mesa_frames.abstract.space.Space | None: + """Return the space attached to the parent model, if any. + + Returns + ------- + mesa_frames.abstract.space.Space | None + Spatial structure registered on the model, or ``None`` when absent. + """ return self.model.space @abstractmethod @@ -521,6 +560,81 @@ def set( """ ... + @abstractmethod + def select( + self, + mask: AgentMask | None = None, + filter_func: Callable[[Self], BoolSeries] | None = None, + n: int | None = None, + negate: bool = False, + inplace: bool = True, + ) -> Self: + """Update the active-agent mask using selection criteria. + + Parameters + ---------- + mask : AgentMask | None, optional + Pre-computed mask identifying agents to activate. + filter_func : Callable[[Self], BoolSeries] | None, optional + Callable evaluated on the agent set to produce an additional mask. + n : int | None, optional + Randomly sample ``n`` agents from the selected mask when provided. + negate : bool, optional + Invert the effective mask, by default False. + inplace : bool, optional + Whether to mutate in place or return an updated copy, by default True. + + Returns + ------- + Self + The updated AgentSet (or a modified copy when ``inplace=False``). + """ + ... + + @abstractmethod + def shuffle(self, inplace: bool = True) -> Self: + """Randomly permute agent order. + + Parameters + ---------- + inplace : bool, optional + Whether to mutate in place or return a shuffled copy, by default True. + + Returns + ------- + Self + The shuffled AgentSet (or a shuffled copy when ``inplace=False``). + """ + ... + + @abstractmethod + def sort( + self, + by: str | Sequence[str], + ascending: bool | Sequence[bool] = True, + inplace: bool = True, + **kwargs: Any, + ) -> Self: + """Sort agents by one or more columns. + + Parameters + ---------- + by : str | Sequence[str] + Column name(s) to sort on. + ascending : bool | Sequence[bool], optional + Sort order per column, by default True. + inplace : bool, optional + Whether to mutate in place or return a sorted copy, by default True. + **kwargs : Any + Backend-specific keyword arguments forwarded to the concrete sorter. + + Returns + ------- + Self + The sorted AgentSet (or a sorted copy when ``inplace=False``). + """ + ... + def __setitem__( self, key: str From 0837a043e36532fa6a6ad296efbce3717ed44b26 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 19:22:16 +0200 Subject: [PATCH 307/329] fix: update Sphinx configuration to enable autosummary generation overwrite and add new attributes to AgentSet documentation --- docs/api/conf.py | 2 ++ docs/api/reference/agents/index.rst | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/api/conf.py b/docs/api/conf.py index 745305dc..4998fbc2 100644 --- a/docs/api/conf.py +++ b/docs/api/conf.py @@ -81,6 +81,8 @@ "exclude-members": "__weakref__,__dict__,__module__,__annotations__,__firstlineno__,__static_attributes__,__abstractmethods__,__slots__", } +autosummary_generate_overwrite = True + # -- GitHub link and user guide settings ------------------------------------- github_root = "https://github.com/projectmesa/mesa-frames" diff --git a/docs/api/reference/agents/index.rst b/docs/api/reference/agents/index.rst index cc5dfef0..4576a204 100644 --- a/docs/api/reference/agents/index.rst +++ b/docs/api/reference/agents/index.rst @@ -68,6 +68,9 @@ API reference :toctree: AgentSet.df + AgentSet.model + AgentSet.random + AgentSet.space AgentSet.active_agents AgentSet.inactive_agents AgentSet.index @@ -183,4 +186,4 @@ API reference .. autoclass:: AgentSetRegistry :autosummary: - :autosummary-nosignatures: \ No newline at end of file + :autosummary-nosignatures: From 7fb155cc9798223a781872d0449bb74d9675ba83 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 19:24:52 +0200 Subject: [PATCH 308/329] fix: enhance documentation for AbstractAgentSet properties to improve clarity and usability --- mesa_frames/abstract/agentset.py | 49 ++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index 86a6ca90..cd220c64 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -424,6 +424,13 @@ def __reversed__(self) -> Iterator: @property def df(self) -> DataFrame: + """Return the full backing DataFrame for this agent set. + + Returns + ------- + DataFrame + Table containing every agent, including inactive records. + """ return self._df @df.setter @@ -439,18 +446,54 @@ def df(self, agents: DataFrame) -> None: @property @abstractmethod - def active_agents(self) -> DataFrame: ... + def active_agents(self) -> DataFrame: + """Return the subset of agents currently marked as active. + + Returns + ------- + DataFrame + DataFrame view containing only active agents. + """ + ... @property @abstractmethod - def inactive_agents(self) -> DataFrame: ... + def inactive_agents(self) -> DataFrame: + """Return the subset of agents currently marked as inactive. + + Returns + ------- + DataFrame + DataFrame view containing only inactive agents. + """ + ... @property @abstractmethod - def index(self) -> Index: ... + def index(self) -> Index: + """Return the unique identifier index for agents in this set. + + Returns + ------- + Index + Collection of unique agent identifiers. + """ + ... @property def pos(self) -> DataFrame: + """Return positional data for agents from the attached space. + + Returns + ------- + DataFrame + Position records aligned with each agent's ``unique_id``. + + Raises + ------ + AttributeError + If the model has no space attached. + """ if self.space is None: raise AttributeError( "Attempted to access `pos`, but the model has no space attached." From 6e76bbac5e68e3f316b4cdc09f5d3056630bc3ef Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 19:33:38 +0200 Subject: [PATCH 309/329] fix: update documentation for Space overview and examples for clarity and completeness --- docs/api/reference/space/index.rst | 37 +++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/api/reference/space/index.rst b/docs/api/reference/space/index.rst index c11b140d..aa8692f0 100644 --- a/docs/api/reference/space/index.rst +++ b/docs/api/reference/space/index.rst @@ -50,6 +50,20 @@ API reference :toctree: Grid.__init__ + Grid.copy + + .. rubric:: Placement & Movement + + .. autosummary:: + :nosignatures: + :toctree: + + Grid.place_agents + Grid.move_agents + Grid.place_to_empty + Grid.place_to_available + Grid.move_to_empty + Grid.move_to_available .. rubric:: Sampling & Queries @@ -57,10 +71,31 @@ API reference :nosignatures: :toctree: + Grid.get_neighbors + Grid.get_directions + Grid.get_distances + Grid.sample_cells + Grid.random_pos + Grid.is_empty + Grid.is_available + Grid.is_full + + .. rubric:: Accessors & Metadata + + .. autosummary:: + :nosignatures: + :toctree: + + Grid.dimensions + Grid.neighborhood_type + Grid.torus Grid.remaining_capacity + Grid.agents + Grid.model + Grid.random .. tab-item:: Full API .. autoclass:: Grid :autosummary: - :autosummary-nosignatures: \ No newline at end of file + :autosummary-nosignatures: From b99658bded3186a46a4f40ee9499d4f09ddafa7b Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 19:36:44 +0200 Subject: [PATCH 310/329] fix: improve code formatting and organization in documentation and agentset module --- docs/general/user-guide/0_getting-started.md | 8 ++++---- mesa_frames/abstract/agentset.py | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/general/user-guide/0_getting-started.md b/docs/general/user-guide/0_getting-started.md index 9f412c93..65011c23 100644 --- a/docs/general/user-guide/0_getting-started.md +++ b/docs/general/user-guide/0_getting-started.md @@ -15,15 +15,15 @@ Objects can be easily subclassed to respect mesa's object-oriented philosophy. ### Vectorized Operations ⚡ -`mesa-frames` leverages **Polars** to replace Python loops with **column-wise expressions** executed in native Rust. +`mesa-frames` leverages **Polars** to replace Python loops with **column-wise expressions** executed in native Rust. This allows you to update all agents simultaneously, the main source of `mesa-frames`' performance advantage. -Unlike traditional `mesa` models, where the **activation order** of agents can affect results (see [Comer, 2014](http://mars.gmu.edu/bitstream/handle/1920/9070/Comer_gmu_0883E_10539.pdf)), -`mesa-frames` processes all agents **in parallel by default**. +Unlike traditional `mesa` models, where the **activation order** of agents can affect results (see [Comer, 2014](http://mars.gmu.edu/bitstream/handle/1920/9070/Comer_gmu_0883E_10539.pdf)), +`mesa-frames` processes all agents **in parallel by default**. This removes order-dependent effects, though you should handle conflicts explicitly when sequential logic is required. !!! tip "Best practice" - Always start by expressing agent logic in a vectorized form. + Always start by expressing agent logic in a vectorized form. Fall back to loops only when ordering or conflict resolution is essential. For a deeper understanding of vectorization and why it accelerates computation, see: diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index cd220c64..806e3fc6 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -21,7 +21,9 @@ from abc import abstractmethod from collections.abc import Collection, Iterable, Iterator from contextlib import suppress -from typing import Any, Callable, Literal, Self, Sequence, overload +from typing import Any, Literal, Self, overload + +from collections.abc import Callable, Sequence from numpy.random import Generator From 3dd607a16c186f786e1c29733e17b8058080434d Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 19:37:09 +0200 Subject: [PATCH 311/329] fix: simplify return type annotation for random generator in AbstractAgentSet --- mesa_frames/abstract/agentset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa_frames/abstract/agentset.py b/mesa_frames/abstract/agentset.py index 806e3fc6..ad693bb6 100644 --- a/mesa_frames/abstract/agentset.py +++ b/mesa_frames/abstract/agentset.py @@ -536,7 +536,7 @@ def random(self) -> Generator: Returns ------- - numpy.random.Generator + Generator Generator used for stochastic operations. """ return self.model.random From 73b358a2b87bcfa7e67485ddeee70bb93d18ef82 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Fri, 17 Oct 2025 19:40:20 +0200 Subject: [PATCH 312/329] fix: correct spelling and formatting in README files for consistency --- README.md | 2 +- benchmarks/README.md | 4 ++-- examples/README.md | 4 ++-- examples/boltzmann_wealth/README.md | 4 ++-- examples/sugarscape_ig/README.md | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d623bc40..6c8b77fb 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ uv sync --all-extras - Transition to LazyFrames for optimization and GPU support - Auto-vectorize existing Mesa models via decorator -- Increase possible Spaces (Network, Continous...) +- Increase possible Spaces (Network, Continuous...) - Refine the API to align to Mesa --- diff --git a/benchmarks/README.md b/benchmarks/README.md index 687093d8..56ba998a 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -11,7 +11,7 @@ Currently included models: ## Quick start -``` +```bash uv run benchmarks/cli.py ``` @@ -44,7 +44,7 @@ Range parsing: `A:B:S` includes `A, A+S, ... <= B`. Final value > B is dropped. Each invocation uses a single UTC timestamp, e.g. `20251016_173702`: -``` +```text benchmarks/ results/ 20251016_173702/ diff --git a/examples/README.md b/examples/README.md index 64da9fea..359bbaf8 100644 --- a/examples/README.md +++ b/examples/README.md @@ -10,7 +10,7 @@ They expose a consistent Typer CLI so you can compare outputs and timings. ## Contents -``` +```text examples/ boltzmann_wealth/ backend_mesa.py # Mesa implementation + CLI (simulate() + run) @@ -27,7 +27,7 @@ examples/ Always run via `uv` from the project root. The simplest way to run an example backend is to execute the module: -``` +```bash uv run examples/boltzmann_wealth/backend_frames.py ``` diff --git a/examples/boltzmann_wealth/README.md b/examples/boltzmann_wealth/README.md index dd7e8f11..9999fe27 100644 --- a/examples/boltzmann_wealth/README.md +++ b/examples/boltzmann_wealth/README.md @@ -64,7 +64,7 @@ performance comparisons. Each run creates (or uses) a results directory like: -``` +```text examples/boltzmann_wealth/results/20251016_173702/ model.csv # step,gini gini__dark.png (and possibly other theme variants) @@ -72,7 +72,7 @@ examples/boltzmann_wealth/results/20251016_173702/ Tail metrics are printed to console for quick inspection: -``` +```text Metrics in the final 5 steps: shape: (5, 2) ┌──────┬───────┐ │ step ┆ gini │ diff --git a/examples/sugarscape_ig/README.md b/examples/sugarscape_ig/README.md index 7940bcec..f33970d5 100644 --- a/examples/sugarscape_ig/README.md +++ b/examples/sugarscape_ig/README.md @@ -78,7 +78,7 @@ Frames backend warns if `MESA_FRAMES_RUNTIME_TYPECHECKING` is enabled (disable f Example output directory (frames): -``` +```text examples/sugarscape_ig/backend_frames/results/20251016_173702/ model.csv plots/ From 5b0eb2399d97367bae3591a6d645f9077e714603 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 09:48:41 +0200 Subject: [PATCH 313/329] fix: update tutorial link and roadmap URL in README for accuracy --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6c8b77fb..e6db9522 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ We still have room to optimize performance further (see [Roadmap](#roadmap)). ## Quick Start -[![Explore the Tutorials](https://img.shields.io/badge/Explore%20the%20Tutorials-📚-blue?style=for-the-badge)](https://projectmesa.github.io/mesa-frames/general/user-guide/) +[![Explore the Tutorials](https://img.shields.io/badge/Explore%20the%20Tutorials-📚-blue?style=for-the-badge)](/mesa-frames/tutorials/2_introductory_tutorial/) 1. **Install** @@ -124,7 +124,7 @@ uv sync --all-extras ## Roadmap -> Community contributions welcome — see the [full roadmap](https://projectmesa.github.io/mesa-frames/general/roadmap) +> Community contributions welcome — see the [full roadmap](https://projectmesa.github.io/mesa-frames/roadmap) - Transition to LazyFrames for optimization and GPU support - Auto-vectorize existing Mesa models via decorator From e1da7eb0ce7a79aa6c66d15f07fa4788041294a6 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 09:49:08 +0200 Subject: [PATCH 314/329] fix: update README for improved clarity and organization --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e6db9522..b46acc8e 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ uv sync --all-extras ## Roadmap -> Community contributions welcome — see the [full roadmap](https://projectmesa.github.io/mesa-frames/roadmap) +> Community contributions welcome — see the [full roadmap](mesa-frames/roadmap) - Transition to LazyFrames for optimization and GPU support - Auto-vectorize existing Mesa models via decorator From b6d014d56285d2e3766747aa6ba66cc50bcb4639 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 11:06:06 +0200 Subject: [PATCH 315/329] fix: update section on transitioning from imperative code to behavioral rules for clarity and detail --- docs/general/user-guide/0_getting-started.md | 83 ++++++++++++++++++-- 1 file changed, 78 insertions(+), 5 deletions(-) diff --git a/docs/general/user-guide/0_getting-started.md b/docs/general/user-guide/0_getting-started.md index 65011c23..caebc1c9 100644 --- a/docs/general/user-guide/0_getting-started.md +++ b/docs/general/user-guide/0_getting-started.md @@ -146,12 +146,85 @@ If you're familiar with mesa, this guide will help you understand the key differ self.schedule.step() ``` -### Transition Tips 💡 +### From Imperative Code to Behavioral Rules 💭 -1. **Think in Sets 🎭**: Instead of individual agents, think about operations on groups of agents. -2. **Leverage DataFrame Operations 🛠️**: Familiarize yourself with Polars operations for efficient agent manipulation. -3. **Vectorize Logic 🚅**: Convert loops and conditionals to vectorized operations where possible. -4. **Use AgentSets 📦**: Group similar agents into AgentSets instead of creating many individual agent classes. +When scientists describe an ABM-like process they typically write a **system of state-transition functions**: + +$$ +x_i(t+1) = f_i\big(x_i(t),\; \mathcal{N}(i,t),\; E(t)\big) +$$ + +Here, $x_i(t)$ is the agent’s state, $\mathcal{N}(i,t)$ its neighborhood or local environment, and $E(t)$ a global environment; $f_i$ is the behavioral law. + +In classic `mesa`, agent behavior is implemented through explicit loops: each agent individually gathers information from its neighbors, computes its next state, and often stores this in a buffer to ensure synchronous updates. The behavioral law $f_i$ is distributed across multiple steps: neighbor iteration, temporary buffers, and scheduling logic, resulting in procedural, step-by-step control flow. + +In `mesa-frames`, these stages are unified into a single vectorized transformation. Agent interactions, state transitions, and updates are expressed as DataFrame operations (such as joins, group-bys, and column expressions) allowing all agents to process perceptions and commit actions simultaneously. This approach centralizes the behavioral law $f_i$ into concise, declarative rules, improving clarity and performance. + +#### Example: Network contagion (Linear Threshold) + +Behavioral rule: a node activates if the number of active neighbors ≥ its threshold. + +=== "mesa-frames" + + Single vectorized transformation. A join brings in source activity, a group-by aggregates exposures per destination, and a column expression applies the activation equation and commits in one pass, no explicit loops or staging structure needed. + + ```python + class Nodes(AgentSet): + # self.df columns: agent_id, active (bool), theta (int) + # self.model.space.edges: DataFrame[src, dst] + def step(self): + E = self.model.space.edges # [src, dst] + # Exposure: active neighbors per dst (vectorized join + groupby) + exposures = ( + E.join( + self.df.select(pl.col("agent_id").alias("src"), + pl.col("active").alias("src_active")), + on="src", how="left" + ) + .with_columns(pl.col("src_active").fill_null(False)) + .group_by("dst") + .agg(pl.col("src_active").sum().alias("k_active")) + ) + # Behavioral equation applied to all agents, committed in-place + self.df = ( + self.df + .join(exposures, left_on="agent_id", right_on="dst", how="left") + .with_columns(pl.col("k_active").fill_null(0)) + .with_columns( + (pl.col("active") | (pl.col("k_active") >= pl.col("theta"))) + .alias("active") + ) + .drop(["k_active", "dst"]) + ) + ``` + +=== "mesa" + + Two-phase imperative procedure. Each agent loops over its neighbors to count active ones (exposure), stores a provisional next state to avoid premature mutation, then a separate pass commits all buffered states for synchronicity. + + ```python + class Node(mesa.Agent): + def step(self): + # (1) Gather exposure: count active neighbors right now + k_active = sum( + 1 for j in self.model.G.neighbors(self.unique_id) + if self.model.id2agent[j].active + ) + # (2) Compute next state (don't mutate yet to stay synchronous) + self.next_active = self.active or (k_active >= self.theta) + + # Second pass (outside the agent method) performs the commit: + for a in model.agents: + a.active = a.next_active + ``` + +!!! tip "Transition tips — quick summary" + 1. Think in sets: operate on AgentSets/DataFrames, not per-agent objects. + 2. Write transitions as Polars column expressions; avoid Python loops. + 3. Use joins + group-bys to compute interactions/exposure across relations. + 4. Commit state synchronously in one vectorized pass. + 5. Group similar agents into one AgentSet with typed columns. + 6. Use UDFs or staged/iterative patterns only for true race/conflict cases. ### Handling Race Conditions 🏁 From 3df8eb2684c0141d801271d7e4944ca4df6a9fe1 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 11:15:32 +0200 Subject: [PATCH 316/329] feat: add changelog inclusion for better documentation accessibility --- docs/general/changelog.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/general/changelog.md diff --git a/docs/general/changelog.md b/docs/general/changelog.md new file mode 100644 index 00000000..8b75f036 --- /dev/null +++ b/docs/general/changelog.md @@ -0,0 +1 @@ +{% include-markdown "../../CHANGELOG.md" %} \ No newline at end of file From 47f1e188d1878bcefbb662f37616a6ccf02edec2 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 11:15:37 +0200 Subject: [PATCH 317/329] fix: remove unused brand-material.css file to streamline stylesheets --- docs/stylesheets/brand-material.css | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 docs/stylesheets/brand-material.css diff --git a/docs/stylesheets/brand-material.css b/docs/stylesheets/brand-material.css deleted file mode 100644 index e69de29b..00000000 From 4c9416e5e95ad3907017d3c5a5194f76c56e49b7 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 11:16:19 +0200 Subject: [PATCH 318/329] feat: add Changelog link to navigation for improved documentation accessibility --- CHANGELOG.md | 36 ++++++++++++++++++++++++++++++++++++ mkdocs.yml | 1 + 2 files changed, 37 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..06ecdaeb --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,36 @@ +## What's Changed +* Refactoring mesa.Agent, mesa.AgentSet, mesa.Model -> AgentSetDF, AgentsDF, ModelDF by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/8 +* setup: Migrate from setup.py to pyproject.toml by @rht in https://github.com/adamamer20/mesa-frames/pull/13 +* ci: Add pre-commit configuration by @rht in https://github.com/adamamer20/mesa-frames/pull/14 +* Merge requirements.txt into pyproject.toml by @rht in https://github.com/adamamer20/mesa-frames/pull/15 +* ci: Add GA for tests by @rht in https://github.com/adamamer20/mesa-frames/pull/17 +* Changes to AgentSetDF and AgentsDF before time.py -> CopyMixin by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/16 +* benchmark: Split Polars agent into native and concise by @rht in https://github.com/adamamer20/mesa-frames/pull/23 +* benchmark: Split pandas agent into native and concise by @rht in https://github.com/adamamer20/mesa-frames/pull/24 +* speed up mesa readme_plot script by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/26 +* Adding DataFrameMixin for improved reusability/encapsulation by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/27 +* Abstract SpaceDF by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/29 +* Adding Abstract DiscreteSpaceDF by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/30 +* Adding abstract GridDF by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/32 +* Additional methods and fixes to DataFrameMixin by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/43 +* Concrete GridPandas by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/44 +* [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/adamamer20/mesa-frames/pull/55 +* Fixes and Tests for PolarsMixin by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/56 +* Adding Comparison and Indexing methods to DataFrameMixin by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/58 +* Concrete GridPolars by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/60 +* Sugarscape Instantaneous Growback (Pandas-with-loop implementation) by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/63 +* Adding pydoclint and properly format docstring by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/69 +* Docs with material-from-mkdocs by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/70 +* Enforce correct numpy docstring formatting with ruff.pydocstyle by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/74 +* API Documentation with Sphinx by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/75 +* Move images from docs to docs/general to make it available for mkdocs by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/79 +* Adding user guide by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/81 +* Adding SugarScape IG (polars with loops) by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/71 +* Automatic publishing on PyPI on new release by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/77 + +## New Contributors +* @adamamer20 made their first contribution in https://github.com/adamamer20/mesa-frames/pull/8 +* @rht made their first contribution in https://github.com/adamamer20/mesa-frames/pull/13 +* @pre-commit-ci made their first contribution in https://github.com/adamamer20/mesa-frames/pull/55 + +**Full Changelog**: https://github.com/adamamer20/mesa-frames/commits/v0.1.0-alpha \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 071c9ee6..40afb584 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -123,3 +123,4 @@ nav: - Contributing: - Contribution Guide: contributing.md - Roadmap: roadmap.md + - Changelog: changelog.md From 87a67dac1bd2a0561b5e91c935ca62cfc0ba5cf8 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 11:20:42 +0200 Subject: [PATCH 319/329] fix: add missing newline at end of CHANGELOG.md for proper formatting --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06ecdaeb..230b7d90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## Version 0.1.0-alpha — 2024-08-28 + ## What's Changed * Refactoring mesa.Agent, mesa.AgentSet, mesa.Model -> AgentSetDF, AgentsDF, ModelDF by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/8 * setup: Migrate from setup.py to pyproject.toml by @rht in https://github.com/adamamer20/mesa-frames/pull/13 @@ -33,4 +35,4 @@ * @rht made their first contribution in https://github.com/adamamer20/mesa-frames/pull/13 * @pre-commit-ci made their first contribution in https://github.com/adamamer20/mesa-frames/pull/55 -**Full Changelog**: https://github.com/adamamer20/mesa-frames/commits/v0.1.0-alpha \ No newline at end of file +**Full Changelog**: https://github.com/adamamer20/mesa-frames/commits/v0.1.0-alpha From 85f8d9e5644f9aa742f7586c710c9e0e5234ec9e Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 11:21:31 +0200 Subject: [PATCH 320/329] feat: automate changelog generation and update process for new releases --- .github/workflows/publish.yml | 45 +++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 105824de..3ef3e856 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -51,6 +51,47 @@ jobs: with: files: | dist/* + - name: Generate changelog from release notes + id: notes + uses: actions/github-script@v7 + with: + script: | + const fs = require('fs'); + const tag = (context.payload.release && context.payload.release.tag_name) + ? context.payload.release.tag_name + : (process.env.GITHUB_REF || '').replace('refs/tags/', ''); + + const body = (context.payload.release && context.payload.release.body) ? context.payload.release.body : ''; + if (!body || body.trim().length === 0) { + core.setFailed('Release body is empty. Ensure the GitHub Release is created with auto-generated notes configured by .github/release.yml or supply a body.'); + } + + fs.writeFileSync('RELEASE_BODY.md', body, 'utf8'); + core.setOutput('tag', tag); + - name: Prepend notes to CHANGELOG.md + env: + TAG: ${{ steps.notes.outputs.tag }} + run: | + VERSION_NO_V=${TAG#v} + DATE_UTC=$(date -u +%Y-%m-%d) + echo "## Version ${VERSION_NO_V} — ${DATE_UTC}" > RELEASE_HEADER.md + echo "" >> RELEASE_HEADER.md + if [ -f CHANGELOG.md ]; then + cat RELEASE_HEADER.md RELEASE_BODY.md CHANGELOG.md > CHANGELOG.new + else + cat RELEASE_HEADER.md RELEASE_BODY.md > CHANGELOG.new + fi + mv CHANGELOG.new CHANGELOG.md + - name: Commit and push CHANGELOG update + env: + TAG: ${{ steps.notes.outputs.tag }} + run: | + git config user.name github-actions + git config user.email github-actions@github.com + git add CHANGELOG.md + # Avoid CI cycles + git commit -m "Changelog: add notes for ${TAG} [skip ci]" || echo "No changelog changes to commit" + git push origin main || true - name: Create or recreate version branch run: | CURRENT_VERSION=$(hatch version) @@ -81,6 +122,6 @@ jobs: # Commit and push the version bump git config user.name github-actions git config user.email github-actions@github.com - git add mesa_frames/__init__.py + git add mesa_frames/__init__.py CHANGELOG.md git commit -m "Bump version to $NEW_VERSION [skip ci]" - git push origin main \ No newline at end of file + git push origin main From af70ba3059253f67bf58977c4d22ec2ab732cc28 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 11:31:03 +0200 Subject: [PATCH 321/329] fix: update publish workflow to ensure changelog is generated and committed correctly --- .github/workflows/publish.yml | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 3ef3e856..7204a0bc 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -17,33 +17,27 @@ jobs: with: fetch-depth: 0 token: ${{ secrets.VERSION_PUSH_TOKEN }} - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: '3.x' - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install hatch + - name: Setup uv + uses: astral-sh/setup-uv@v3 - name: Set release version run: | # Get the tag from the GitHub release TAG=${GITHUB_REF#refs/tags/} # Remove 'v' prefix if present VERSION=${TAG#v} - hatch version $VERSION + uvx hatch version $VERSION - name: Build package - run: hatch build + run: uvx hatch build - name: Run tests - run: hatch run test:pytest + run: uvx hatch run test:pytest - name: Publish package to PyPI uses: pypa/gh-action-pypi-publish@release/v1 - name: Verify PyPI Release run: | # Verify PyPI release PACKAGE_NAME="mesa_frames" - CURRENT_VERSION=$(hatch version) - pip install $PACKAGE_NAME==$CURRENT_VERSION + CURRENT_VERSION=$(uvx hatch version) + uv pip install --system $PACKAGE_NAME==$CURRENT_VERSION python -c "import mesa_frames; print(mesa_frames.__version__)" - name: Update GitHub Release uses: softprops/action-gh-release@v1 @@ -94,7 +88,7 @@ jobs: git push origin main || true - name: Create or recreate version branch run: | - CURRENT_VERSION=$(hatch version) + CURRENT_VERSION=$(uvx hatch version) BRANCH_NAME="v$CURRENT_VERSION" git config user.name github-actions @@ -113,11 +107,11 @@ jobs: - name: Update to Next Version run: | # Bump to next development version - hatch version patch - hatch version dev + uvx hatch version patch + uvx hatch version dev # Get the new version - NEW_VERSION=$(hatch version) + NEW_VERSION=$(uvx hatch version) # Commit and push the version bump git config user.name github-actions From dd582b8fb9c8b8864f6ec67f41a154e6db157ebf Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 12:23:31 +0200 Subject: [PATCH 322/329] refactor: update ROADMAP.md for clarity and focus on near-term goals --- ROADMAP.md | 78 ++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 43 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 7dd953f5..731db1b8 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,67 +1,59 @@ -# Roadmap 🗺️ +# Roadmap -This document outlines the development roadmap for the mesa-frames project. It provides insights into our current priorities, upcoming features, and long-term vision. +This document outlines the near-term roadmap for mesa-frames as of October 2025. -## 0.1.0 Stable Release Goals 🎯 +### 1) LazyFrames for Polars + GPU -### 1. Transitioning polars implementation from eager API to lazy API +Switch Polars usage from eager to `LazyFrame` to enable better query optimization and GPU acceleration. -One of our major priorities was to move from pandas to polars as the primary dataframe backend. This transition was motivated by performance considerations. -Now we should transition to using the lazily evaluated version of polars. +Related issues: -**Related issues:** [#10: GPU integration: Dask, cuda (cudf) and RAPIDS (Polars)](https://github.com/projectmesa/mesa-frames/issues/10), [#89: Investigate using Ibis for the common interface library to any DF backend](https://github.com/projectmesa/mesa-frames/issues/89), [#52: Use of LazyFrames for Polars implementation](https://github.com/projectmesa/mesa-frames/issues/52) +- [#52: Use of LazyFrames for Polars implementation](https://github.com/projectmesa/mesa-frames/issues/52) -#### Progress and Next Steps +- [#144: Switch to LazyFrame for Polars implementation (PR)](https://github.com/projectmesa/mesa-frames/pull/144) -- We are exploring [Ibis](https://ibis-project.org/) or [narwhals](https://github.com/narwhals-dev/narwhals) as a common interface library that could support multiple backends (Polars, DuckDB, Spark etc.), but since most of the development is currently in polars, we will currently continue using Polars. -- We're transitioning to the lazy API, mainly in order to use GPU acceleration +- [#89: Investigate Ibis or Narwhals for backend flexibility](https://github.com/projectmesa/mesa-frames/issues/89) -### 2. Handling Concurrency Management +- [#122: Deprecate DataFrameMixin (remove during LazyFrames refactor)](https://github.com/projectmesa/mesa-frames/issues/122) -A critical aspect of agent-based models is efficiently managing concurrent agent movements, especially when multiple agents attempt to move to the same location simultaneously. We aim to implement abstractions that handle these concurrency conditions automatically. +Progress and next steps: +- Land [#144](https://github.com/projectmesa/mesa-frames/pull/144) and convert remaining eager paths to lazy. -**Related issues:** [#108: Adding abstraction of optimal agent movement](https://github.com/projectmesa/mesa-frames/issues/108), [#48: Emulate RandomActivation with DataFrame.rolling](https://github.com/projectmesa/mesa-frames/issues/48) +- Validate GPU execution paths and benchmark improvements. -#### Sugarscape Example of Concurrency Issues +- Revisit Ibis/Narwhals after LazyFrame stabilization. -Testing with many potential collisions revealed a specific issue: +- Fold DataFrameMixin removal into the LazyFrames transition ([#122](https://github.com/projectmesa/mesa-frames/issues/122)). -**Problem scenario:** +--- -- Consider two agents targeting the same cell: - - A mid-priority agent (higher in the agent order) - - A low-priority agent (lower in the agent order) -- The mid-priority agent has low preference for the cell -- The low-priority agent has high preference for the cell -- Without accounting for priority: - - The mid-priority agent's best moves kept getting "stolen" by higher priority agents - - This forced it to resort to lower preference target cells - - However, these lower preference cells were often already taken by lower priority agents in previous iterations +### 2) AgentSet Enhancements -**Solution approach:** +Expose movement methods from `AgentContainer` and provide optimized utilities for "move to optimal" workflows. -- Implement a "priority" count to ensure that each action is "legal" -- This prevents race conditions but requires recomputing the priority at each iteration -- Current implementation may be slower than Numba due to this overhead -- After the Ibis refactoring, we can investigate if lazy evaluation can help mitigate this performance issue +Related issues: +- [#108: Adding abstraction of optimal agent movement](https://github.com/projectmesa/mesa-frames/issues/108) -The Sugarscape example demonstrates the need for this abstraction, as multiple agents often attempt to move to the same cell simultaneously. By generalizing this functionality, we can eliminate the need for users to implement complex conflict resolution logic repeatedly. +- [#118: Adds move_to_optimal in DiscreteSpaceDF (PR)](https://github.com/projectmesa/mesa-frames/pull/118) -#### Progress and Next Steps +- [#82: Add movement methods to AgentContainer](https://github.com/projectmesa/mesa-frames/issues/82) -- Create utility functions in `DiscreteSpace` and `AgentSetRegistry` to move agents optimally based on specified attributes -- Provide built-in resolution strategies for common concurrency scenarios -- Ensure the implementation works efficiently with the vectorized approach of mesa-frames +Next steps: +- Consolidate movement APIs under `AgentContainer`. -### Additional 0.1.0 Goals +- Keep conflict resolution simple, vectorized, and well-documented. -- Complete core API stabilization -- Completely mirror mesa's functionality -- Improve documentation and examples -- Address outstanding bugs and performance issues +--- -## Beyond 0.1.0 +### 3) Research & Publication -Future roadmap items will be added as the project evolves and new priorities emerge. +JOSS paper preparation and submission. -We welcome community feedback on our roadmap! Please open an issue if you have suggestions or would like to contribute to any of these initiatives. +Related items: +- [#90: JOSS paper for the package](https://github.com/projectmesa/mesa-frames/issues/90) + +- [#107: paper - Adding Statement of Need (PR)](https://github.com/projectmesa/mesa-frames/pull/107) + +--- + +See [our contribution guide](/mesa-frames/contributing/) and browse all open items at https://github.com/projectmesa/mesa-frames/issues From 0921cbc4957298181d0bff97746ea4d594b21167 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 12:24:22 +0200 Subject: [PATCH 323/329] chore: update ROADMAP.md for consistency and clarity --- ROADMAP.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ROADMAP.md b/ROADMAP.md index 731db1b8..acb2a873 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -17,6 +17,7 @@ Related issues: - [#122: Deprecate DataFrameMixin (remove during LazyFrames refactor)](https://github.com/projectmesa/mesa-frames/issues/122) Progress and next steps: + - Land [#144](https://github.com/projectmesa/mesa-frames/pull/144) and convert remaining eager paths to lazy. - Validate GPU execution paths and benchmark improvements. @@ -32,6 +33,7 @@ Progress and next steps: Expose movement methods from `AgentContainer` and provide optimized utilities for "move to optimal" workflows. Related issues: + - [#108: Adding abstraction of optimal agent movement](https://github.com/projectmesa/mesa-frames/issues/108) - [#118: Adds move_to_optimal in DiscreteSpaceDF (PR)](https://github.com/projectmesa/mesa-frames/pull/118) @@ -39,6 +41,7 @@ Related issues: - [#82: Add movement methods to AgentContainer](https://github.com/projectmesa/mesa-frames/issues/82) Next steps: + - Consolidate movement APIs under `AgentContainer`. - Keep conflict resolution simple, vectorized, and well-documented. From cc751a72a11b2a67accb61917ae96f51b678c22b Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 12:30:36 +0200 Subject: [PATCH 324/329] fix: add missing newline in ROADMAP.md for proper formatting --- ROADMAP.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ROADMAP.md b/ROADMAP.md index acb2a873..8b237386 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -53,6 +53,7 @@ Next steps: JOSS paper preparation and submission. Related items: + - [#90: JOSS paper for the package](https://github.com/projectmesa/mesa-frames/issues/90) - [#107: paper - Adding Statement of Need (PR)](https://github.com/projectmesa/mesa-frames/pull/107) From 16fe18eef164994ad4af8808bb0a7dc860c73e6c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 19 Oct 2025 10:33:26 +0000 Subject: [PATCH 325/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGELOG.md | 66 ++++++++++++++++++++------------------- ROADMAP.md | 2 +- docs/general/changelog.md | 2 +- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 230b7d90..b7595854 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,38 +1,40 @@ ## Version 0.1.0-alpha — 2024-08-28 ## What's Changed -* Refactoring mesa.Agent, mesa.AgentSet, mesa.Model -> AgentSetDF, AgentsDF, ModelDF by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/8 -* setup: Migrate from setup.py to pyproject.toml by @rht in https://github.com/adamamer20/mesa-frames/pull/13 -* ci: Add pre-commit configuration by @rht in https://github.com/adamamer20/mesa-frames/pull/14 -* Merge requirements.txt into pyproject.toml by @rht in https://github.com/adamamer20/mesa-frames/pull/15 -* ci: Add GA for tests by @rht in https://github.com/adamamer20/mesa-frames/pull/17 -* Changes to AgentSetDF and AgentsDF before time.py -> CopyMixin by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/16 -* benchmark: Split Polars agent into native and concise by @rht in https://github.com/adamamer20/mesa-frames/pull/23 -* benchmark: Split pandas agent into native and concise by @rht in https://github.com/adamamer20/mesa-frames/pull/24 -* speed up mesa readme_plot script by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/26 -* Adding DataFrameMixin for improved reusability/encapsulation by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/27 -* Abstract SpaceDF by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/29 -* Adding Abstract DiscreteSpaceDF by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/30 -* Adding abstract GridDF by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/32 -* Additional methods and fixes to DataFrameMixin by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/43 -* Concrete GridPandas by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/44 -* [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/adamamer20/mesa-frames/pull/55 -* Fixes and Tests for PolarsMixin by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/56 -* Adding Comparison and Indexing methods to DataFrameMixin by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/58 -* Concrete GridPolars by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/60 -* Sugarscape Instantaneous Growback (Pandas-with-loop implementation) by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/63 -* Adding pydoclint and properly format docstring by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/69 -* Docs with material-from-mkdocs by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/70 -* Enforce correct numpy docstring formatting with ruff.pydocstyle by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/74 -* API Documentation with Sphinx by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/75 -* Move images from docs to docs/general to make it available for mkdocs by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/79 -* Adding user guide by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/81 -* Adding SugarScape IG (polars with loops) by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/71 -* Automatic publishing on PyPI on new release by @adamamer20 in https://github.com/adamamer20/mesa-frames/pull/77 + +* Refactoring mesa.Agent, mesa.AgentSet, mesa.Model -> AgentSetDF, AgentsDF, ModelDF by @adamamer20 in +* setup: Migrate from setup.py to pyproject.toml by @rht in +* ci: Add pre-commit configuration by @rht in +* Merge requirements.txt into pyproject.toml by @rht in +* ci: Add GA for tests by @rht in +* Changes to AgentSetDF and AgentsDF before time.py -> CopyMixin by @adamamer20 in +* benchmark: Split Polars agent into native and concise by @rht in +* benchmark: Split pandas agent into native and concise by @rht in +* speed up mesa readme_plot script by @adamamer20 in +* Adding DataFrameMixin for improved reusability/encapsulation by @adamamer20 in +* Abstract SpaceDF by @adamamer20 in +* Adding Abstract DiscreteSpaceDF by @adamamer20 in +* Adding abstract GridDF by @adamamer20 in +* Additional methods and fixes to DataFrameMixin by @adamamer20 in +* Concrete GridPandas by @adamamer20 in +* [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in +* Fixes and Tests for PolarsMixin by @adamamer20 in +* Adding Comparison and Indexing methods to DataFrameMixin by @adamamer20 in +* Concrete GridPolars by @adamamer20 in +* Sugarscape Instantaneous Growback (Pandas-with-loop implementation) by @adamamer20 in +* Adding pydoclint and properly format docstring by @adamamer20 in +* Docs with material-from-mkdocs by @adamamer20 in +* Enforce correct numpy docstring formatting with ruff.pydocstyle by @adamamer20 in +* API Documentation with Sphinx by @adamamer20 in +* Move images from docs to docs/general to make it available for mkdocs by @adamamer20 in +* Adding user guide by @adamamer20 in +* Adding SugarScape IG (polars with loops) by @adamamer20 in +* Automatic publishing on PyPI on new release by @adamamer20 in ## New Contributors -* @adamamer20 made their first contribution in https://github.com/adamamer20/mesa-frames/pull/8 -* @rht made their first contribution in https://github.com/adamamer20/mesa-frames/pull/13 -* @pre-commit-ci made their first contribution in https://github.com/adamamer20/mesa-frames/pull/55 -**Full Changelog**: https://github.com/adamamer20/mesa-frames/commits/v0.1.0-alpha +* @adamamer20 made their first contribution in +* @rht made their first contribution in +* @pre-commit-ci made their first contribution in + +**Full Changelog**: diff --git a/ROADMAP.md b/ROADMAP.md index 8b237386..e70292cf 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -60,4 +60,4 @@ Related items: --- -See [our contribution guide](/mesa-frames/contributing/) and browse all open items at https://github.com/projectmesa/mesa-frames/issues +See [our contribution guide](/mesa-frames/contributing/) and browse all open items at diff --git a/docs/general/changelog.md b/docs/general/changelog.md index 8b75f036..33ece5d4 100644 --- a/docs/general/changelog.md +++ b/docs/general/changelog.md @@ -1 +1 @@ -{% include-markdown "../../CHANGELOG.md" %} \ No newline at end of file +{% include-markdown "../../CHANGELOG.md" %} From a6ea4948d941d6e23ead7a54c2f5dcde5c9057e4 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Sun, 19 Oct 2025 12:42:44 +0200 Subject: [PATCH 326/329] fix: replace setup-python action with astral-sh/setup-uv for improved environment setup --- .github/workflows/docs-gh-pages.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/docs-gh-pages.yml b/.github/workflows/docs-gh-pages.yml index d60be123..220c646b 100644 --- a/.github/workflows/docs-gh-pages.yml +++ b/.github/workflows/docs-gh-pages.yml @@ -20,8 +20,7 @@ jobs: steps: - uses: actions/checkout@v4 with: { fetch-depth: 0 } - - uses: actions/setup-python@v5 - with: { python-version: '3.x' } + - uses: astral-sh/setup-uv@v6 - name: Install mesa-frames + docs deps From bac16c06048bcc74c594b2278736c28979638f85 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 20 Oct 2025 09:51:47 +0200 Subject: [PATCH 327/329] refactor: remove select() method from the abstract API in AbstractAgentSetRegistry --- mesa_frames/abstract/agentsetregistry.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index abb0ef69..44a52eb4 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -350,7 +350,6 @@ def remove( """ ... - # select() intentionally removed from the abstract API. @abstractmethod def replace( From fc9eef4aa0c67ef9e820fd0ff1a1dd5470198872 Mon Sep 17 00:00:00 2001 From: adamamer20 Date: Mon, 20 Oct 2025 09:56:17 +0200 Subject: [PATCH 328/329] fix: simplify rename logic in AgentSet class for better readability --- mesa_frames/concrete/agentset.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/mesa_frames/concrete/agentset.py b/mesa_frames/concrete/agentset.py index 2a9b1a55..7e1b5e0d 100644 --- a/mesa_frames/concrete/agentset.py +++ b/mesa_frames/concrete/agentset.py @@ -132,19 +132,12 @@ def rename(self, new_name: str, inplace: bool = True) -> Self: # Check if we have a model and can find the AgentSetRegistry that contains this set try: if self in self.model.sets: - # Save index to locate the copy on non-inplace path - try: - idx = list(self.model.sets).index(self) # type: ignore[arg-type] - except Exception: - idx = None reg = self.model.sets.rename(self, new_name, inplace=inplace) if inplace: return self - if idx is not None: - return reg[idx] - return reg.get(new_name) # type: ignore[return-value] - except Exception: - # Fall back to local rename if delegation fails + return reg[new_name] + except KeyError: + # Fall back to local rename if isn't found in a an AgentSetRegistry obj._name = new_name return obj From fd115b9dbc3549bc8ccf79a92d71027628c26ed7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 8 Dec 2025 18:56:53 +0000 Subject: [PATCH 329/329] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa_frames/abstract/agentsetregistry.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mesa_frames/abstract/agentsetregistry.py b/mesa_frames/abstract/agentsetregistry.py index d6d92a06..df8823be 100644 --- a/mesa_frames/abstract/agentsetregistry.py +++ b/mesa_frames/abstract/agentsetregistry.py @@ -348,7 +348,6 @@ def remove( """ ... - @abstractmethod def replace( self,