Skip to content

Commit

Permalink
Changes to AgentSetDF and AgentsDF before time.py -> CopyMixin (#16)
Browse files Browse the repository at this point in the history
* Modified AgentSet and AgentsDF before adding
mesa_frames/time.py. Created a CopyMixin for copy and deepcopy methods
to let both AgentSet and Scheduler inherit.
Added possibility of using 'do' method with a mask in AgentSetDF,
such that the Scheduler can use it to run the method only on a
subset of agents.
Minor fixes to type hints and docstrings.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* pre-commit fixes

* minor fixes for tests

* moved sampling to last step (otherwise, can sample outside of final mask)

* from typing to typing-extensions

* added mask to AgentsDF.do

* bug fixes, added tests for AgentsDF

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* change to polars._typing and added test.__init__

* changed df comparison

* keys fix

* fix

* change to .toml for polars._typing

* added comment for typing-extensions, removed geopandas(will be added in the future for mesa-frames.geo)

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
adamamer20 and pre-commit-ci[bot] authored Jul 6, 2024
1 parent 564ca66 commit bf1ccd9
Show file tree
Hide file tree
Showing 18 changed files with 1,959 additions and 490 deletions.
8 changes: 4 additions & 4 deletions docs/api/mesa_frames.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ <h2>Submodules</h2>
<h1 class="modulename">
mesa_frames </h1>





</section>
</main>
<script>
Expand Down
6 changes: 3 additions & 3 deletions docs/api/mesa_frames/agent.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h2>API Documentation</h2>
<h1 class="modulename">
mesa_frames<wbr>.agent </h1>


<input id="mod-agent-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">

<label class="view-source-button" for="mod-agent-view-source"><span>View Source</span></label>
Expand Down Expand Up @@ -161,7 +161,7 @@ <h1 class="modulename">
<section id="AgentDF">
<input id="AgentDF-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
<div class="attr class">

<span class="def">class</span>
<span class="name">AgentDF</span>:

Expand Down Expand Up @@ -309,7 +309,7 @@ <h6 id="attributes">Attributes</h6>
<section id="GeoAgentDF">
<input id="GeoAgentDF-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
<div class="attr class">

<span class="def">class</span>
<span class="name">GeoAgentDF</span><wbr>(<span class="base"><a href="#AgentDF">AgentDF</a></span>):

Expand Down
18 changes: 9 additions & 9 deletions docs/api/mesa_frames/model.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ <h2>API Documentation</h2>
<h1 class="modulename">
mesa_frames<wbr>.model </h1>


<input id="mod-model-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">

<label class="view-source-button" for="mod-model-view-source"><span>View Source</span></label>
Expand Down Expand Up @@ -402,7 +402,7 @@ <h1 class="modulename">
<section id="ModelDF">
<input id="ModelDF-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
<div class="attr class">

<span class="def">class</span>
<span class="name">ModelDF</span>:

Expand Down Expand Up @@ -741,7 +741,7 @@ <h6 id="attributes">Attributes</h6>
<div id="ModelDF.__init__" class="classattr">
<input id="ModelDF.__init__-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
<div class="attr function">

<span class="name">ModelDF</span><span class="signature pdoc-code condensed">(<span class="param"><span class="n">unique_id</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span>, </span><span class="param"><span class="n">space</span><span class="o">=</span><span class="kc">None</span></span>)</span>

<label class="view-source-button" for="ModelDF.__init__-view-source"><span>View Source</span></label>
Expand Down Expand Up @@ -801,7 +801,7 @@ <h6 id="parameters">Parameters</h6>
<div id="ModelDF.get_agents_of_type" class="classattr">
<input id="ModelDF.get_agents_of_type-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
<div class="attr function">

<span class="def">def</span>
<span class="name">get_agents_of_type</span><span class="signature pdoc-code multiline">(<span class="param"> <span class="bp">self</span>,</span><span class="param"> <span class="n">agent_type</span><span class="p">:</span> <span class="nb">type</span><span class="p">[</span><span class="n"><a href="agent.html#AgentDF">mesa_frames.agent.AgentDF</a></span><span class="p">]</span></span><span class="return-annotation">) -> <span class="n">pandas</span><span class="o">.</span><span class="n">core</span><span class="o">.</span><span class="n">series</span><span class="o">.</span><span class="n">Series</span>:</span></span>

Expand Down Expand Up @@ -840,7 +840,7 @@ <h6 id="parameters">Parameters</h6>
<div id="ModelDF.run_model" class="classattr">
<input id="ModelDF.run_model-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
<div class="attr function">

<span class="def">def</span>
<span class="name">run_model</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">n_steps</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span>, </span><span class="param"><span class="n">merged_mro</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span></span><span class="return-annotation">) -> <span class="kc">None</span>:</span></span>

Expand Down Expand Up @@ -898,7 +898,7 @@ <h6 id="parameters">Parameters</h6>
<div id="ModelDF.step" class="classattr">
<input id="ModelDF.step-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
<div class="attr function">

<span class="def">def</span>
<span class="name">step</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">merged_mro</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span></span><span class="return-annotation">) -> <span class="kc">None</span>:</span></span>

Expand Down Expand Up @@ -948,7 +948,7 @@ <h6 id="parameters">Parameters</h6>
<div id="ModelDF.reset_randomizer" class="classattr">
<input id="ModelDF.reset_randomizer-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
<div class="attr function">

<span class="def">def</span>
<span class="name">reset_randomizer</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span>, </span><span class="param"><span class="n">seed</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span></span><span class="return-annotation">) -> <span class="kc">None</span>:</span></span>

Expand Down Expand Up @@ -986,7 +986,7 @@ <h6 id="parameters">Parameters</h6>
<div id="ModelDF.create_agents" class="classattr">
<input id="ModelDF.create_agents-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
<div class="attr function">

<span class="def">def</span>
<span class="name">create_agents</span><span class="signature pdoc-code multiline">(<span class="param"> <span class="bp">self</span>,</span><span class="param"> <span class="n">n_agents</span><span class="p">:</span> <span class="nb">int</span>,</span><span class="param"> <span class="n">p_agents</span><span class="p">:</span> <span class="nb">dict</span><span class="p">[</span><span class="nb">type</span><span class="p">[</span><span class="n"><a href="agent.html#AgentDF">mesa_frames.agent.AgentDF</a></span><span class="p">],</span> <span class="nb">float</span><span class="p">]</span></span><span class="return-annotation">) -> <span class="kc">None</span>:</span></span>

Expand Down Expand Up @@ -1133,7 +1133,7 @@ <h6 id="parameters">Parameters</h6>
<div id="ModelDF.update_agents_masks" class="classattr">
<input id="ModelDF.update_agents_masks-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
<div class="attr function">

<span class="def">def</span>
<span class="name">update_agents_masks</span><span class="signature pdoc-code condensed">(<span class="param"><span class="bp">self</span></span><span class="return-annotation">) -> <span class="kc">None</span>:</span></span>

Expand Down
5 changes: 2 additions & 3 deletions docs/scripts/readme_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MoneyAgentPolars(AgentSetPolars):
def __init__(self, n: int, model: ModelDF):
super().__init__(model)
## Adding the agents to the agent set
# 1. Changing the agents attribute directly (not reccomended, if other agents were added before, they will be lost)
# 1. Changing the agents attribute directly (not recommended, if other agents were added before, they will be lost)
"""self.agents = pl.DataFrame(
{"unique_id": pl.arange(n, eager=True), "wealth": pl.ones(n, eager=True)}
)"""
Expand Down Expand Up @@ -150,7 +150,7 @@ class MoneyAgentPandas(AgentSetPandas):
def __init__(self, n: int, model: ModelDF) -> None:
super().__init__(model)
## Adding the agents to the agent set
# 1. Changing the agents attribute directly (not reccomended, if other agents were added before, they will be lost)
# 1. Changing the agents attribute directly (not recommended, if other agents were added before, they will be lost)
# self.agents = pd.DataFrame({"unique_id": np.arange(n), "wealth": np.ones(n)})
# 2. Adding the dataframe with add
# self.add(pd.DataFrame({"unique_id": np.arange(n), "wealth": np.ones(n)}))
Expand Down Expand Up @@ -226,7 +226,6 @@ def mesa_frames_pandas(n_agents: int) -> None:


def main():

sns.set_theme(style="whitegrid")

out = perfplot.bench(
Expand Down
7 changes: 7 additions & 0 deletions mesa_frames/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@
from mesa_frames.concrete.agentset_pandas import AgentSetPandas
from mesa_frames.concrete.agentset_polars import AgentSetPolars
from mesa_frames.concrete.model import ModelDF

__all__ = [
"AgentsDF",
"AgentSetPandas",
"AgentSetPolars",
"ModelDF",
]
Loading

0 comments on commit bf1ccd9

Please sign in to comment.