-
Notifications
You must be signed in to change notification settings - Fork 14
Generate automatically unique_id #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
luca-patrignani
wants to merge
39
commits into
projectmesa:main
Choose a base branch
from
luca-patrignani:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
9f71b07
Generate automatically unique_id for pandas agent set
luca-patrignani d2483b9
Not write in passing list when creating an agent with a list
luca-patrignani 0f41ea8
Fix assertions for index
luca-patrignani 03b0275
Warn when passing unique_id while adding new agents
luca-patrignani f2ffac7
Add test for adding agents without specifing the unique_id
luca-patrignani da9e095
Rename test_add to test_add_with_unique_id
luca-patrignani a679200
Let add agents without specifing unique_id for polars and adapt Agent…
luca-patrignani 81c8072
Test if the a deprecation warning is raised when adding agents with a…
luca-patrignani bf72673
Use uuid4 stored as string as unique_id for pandas
luca-patrignani 7d81966
Reorder the agent set to its original order after a 'do' call
luca-patrignani 2481262
Fix add and set in polars
luca-patrignani e1ad4a8
Not reorder the agents after masked do
luca-patrignani b92d39a
Fix tests for polars
luca-patrignani 837dca4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4105548
Fix tests for agentsDF
luca-patrignani d9b8918
Fix some tests for grid pandas
luca-patrignani 5d79c71
Fix tests for grid in polars
luca-patrignani b9af6b4
Merge branch 'main' into main
luca-patrignani c58a75f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f6a96dc
Remove shift_index methods
luca-patrignani 8fba7b5
Fix select for pandas
luca-patrignani 9cfedfc
Add test for adding a pl.Dataframe with a unique_id column
luca-patrignani 3cb521b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 14fcb4f
Merge branch 'main' into main
adamamer20 325e35f
Use uint64 instead of uuid4 for representing unique_id in agent set
luca-patrignani 5e4316e
Use uint64 instead of uuid4 for representing unique_id in AgentsDF
luca-patrignani 7ed5478
Use uint64 instead of uuid4 for representing unique_id in polars spaces
luca-patrignani 84f8f25
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c76cae9
Fix test for pandas agentset
luca-patrignani 9c1722a
Remove some instanceof related to previous use of uuid as str
luca-patrignani bd1e22c
Remove useless debug trick for unique_ids generation
luca-patrignani 625ca3d
Remove pandas backend
luca-patrignani d38df22
Merge remote-tracking branch 'upstream/main'
luca-patrignani 24edd32
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] db62837
Remove unique_id manual assignment in tests
luca-patrignani 516e038
Merge branch 'main' into main
adamamer20 ff1c387
removing pandas import (outdated)
adamamer20 da8fa96
removing other unused imports
adamamer20 339ffa4
removed unusued type hint
adamamer20 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ | |
|
||
import polars as pl | ||
from polars._typing import IntoExpr | ||
from polars.exceptions import ShapeError | ||
from typing_extensions import Any, Self, overload | ||
|
||
from mesa_frames.concrete.agents import AgentSetDF | ||
|
@@ -95,12 +96,12 @@ | |
The model that the agent set belongs to. | ||
""" | ||
self._model = model | ||
self._agents = pl.DataFrame(schema={"unique_id": pl.Int64}) | ||
self._agents = pl.DataFrame() | ||
self._mask = pl.repeat(True, len(self._agents), dtype=pl.Boolean, eager=True) | ||
|
||
def add( | ||
self, | ||
agents: pl.DataFrame | Sequence[Any] | dict[str, Any], | ||
agents: Self | pl.DataFrame | Sequence[Any] | dict[str, Any], | ||
inplace: bool = True, | ||
) -> Self: | ||
"""Add agents to the AgentSetPolars. | ||
|
@@ -118,33 +119,40 @@ | |
The updated AgentSetPolars. | ||
""" | ||
obj = self._get_obj(inplace) | ||
if isinstance(agents, pl.DataFrame): | ||
if "unique_id" not in agents.columns: | ||
raise KeyError("DataFrame must have a unique_id column.") | ||
if isinstance(agents, AgentSetPolars): | ||
new_agents = agents.agents | ||
elif isinstance(agents, pl.DataFrame): | ||
if "unique_id" in agents.columns: | ||
raise ValueError("Dataframe should not have a unique_id column.") | ||
new_agents = agents | ||
elif isinstance(agents, dict): | ||
if "unique_id" not in agents: | ||
raise KeyError("Dictionary must have a unique_id key.") | ||
if "unique_id" in agents: | ||
raise ValueError("Dictionary should not have a unique_id key.") | ||
new_agents = pl.DataFrame(agents) | ||
else: | ||
if len(agents) != len(obj._agents.columns): | ||
if len(agents) != len(obj._agents.columns) - 1: | ||
raise ValueError( | ||
"Length of data must match the number of columns in the AgentSet if being added as a Collection." | ||
) | ||
new_agents = pl.DataFrame([agents], schema=obj._agents.schema) | ||
new_agents = pl.DataFrame( | ||
[self._generate_unique_ids(1).to_list() + agents], | ||
schema=obj._agents.schema, | ||
) | ||
|
||
if new_agents["unique_id"].dtype != pl.Int64: | ||
raise TypeError("unique_id column must be of type int64.") | ||
if "unique_id" not in new_agents: | ||
new_agents = new_agents.with_columns( | ||
self._generate_unique_ids(len(new_agents)).alias("unique_id") | ||
) | ||
|
||
# If self._mask is pl.Expr, then new mask is the same. | ||
# If self._mask is pl.Series[bool], then new mask has to be updated. | ||
|
||
if isinstance(obj._mask, pl.Series): | ||
originally_empty = len(obj._agents) == 0 | ||
if isinstance(obj._mask, pl.Series) and not originally_empty: | ||
original_active_indices = obj._agents.filter(obj._mask)["unique_id"] | ||
|
||
obj._agents = pl.concat([obj._agents, new_agents], how="diagonal_relaxed") | ||
|
||
if isinstance(obj._mask, pl.Series): | ||
if isinstance(obj._mask, pl.Series) and not originally_empty: | ||
obj._update_mask(original_active_indices, new_agents["unique_id"]) | ||
|
||
return obj | ||
|
@@ -161,8 +169,8 @@ | |
) -> bool | pl.Series: | ||
if isinstance(agents, pl.Series): | ||
return agents.is_in(self._agents["unique_id"]) | ||
elif isinstance(agents, Collection): | ||
return pl.Series(agents).is_in(self._agents["unique_id"]) | ||
elif isinstance(agents, Collection) and not isinstance(agents, str): | ||
return pl.Series(agents, dtype=pl.UInt64).is_in(self._agents["unique_id"]) | ||
else: | ||
return agents in self._agents["unique_id"] | ||
|
||
|
@@ -188,7 +196,6 @@ | |
inplace: bool = True, | ||
) -> Self: | ||
obj = self._get_obj(inplace) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain why you had to make changes to the set method? |
||
b_mask = obj._get_bool_mask(mask) | ||
masked_df = obj._get_masked_df(mask) | ||
|
||
if not attr_names: | ||
|
@@ -199,17 +206,15 @@ | |
masked_df: pl.DataFrame, attr_name: str, values: Any | ||
) -> pl.DataFrame: | ||
if isinstance(values, pl.DataFrame): | ||
return masked_df.with_columns(values.to_series().alias(attr_name)) | ||
elif isinstance(values, pl.Expr): | ||
return masked_df.with_columns(values.alias(attr_name)) | ||
if isinstance(values, pl.Series): | ||
return masked_df.with_columns(values.alias(attr_name)) | ||
values_series = values.to_series() | ||
elif isinstance(values, (pl.Expr, pl.Series, Collection)): | ||
values_series = pl.Series(values) | ||
else: | ||
if isinstance(values, Collection): | ||
values = pl.Series(values) | ||
else: | ||
values = pl.repeat(values, len(masked_df)) | ||
return masked_df.with_columns(values.alias(attr_name)) | ||
values_series = pl.repeat(values, len(masked_df)) | ||
try: | ||
return masked_df.with_columns(values_series.alias(attr_name)) | ||
except ShapeError as error: | ||
raise KeyError(error) | ||
|
||
if isinstance(attr_names, str) and values is not None: | ||
masked_df = process_single_attr(masked_df, attr_names, values) | ||
|
@@ -227,10 +232,19 @@ | |
raise ValueError( | ||
"attr_names must be a string, a collection of string or a dictionary with columns as keys and values." | ||
) | ||
unique_id_column = None | ||
if "unique_id" not in obj._agents: | ||
unique_id_column = self._generate_unique_ids(len(masked_df)).alias( | ||
"unique_id" | ||
) | ||
obj._agents = obj._agents.with_columns(unique_id_column) | ||
masked_df = masked_df.with_columns(unique_id_column) | ||
b_mask = obj._get_bool_mask(mask) | ||
non_masked_df = obj._agents.filter(b_mask.not_()) | ||
original_index = obj._agents.select("unique_id") | ||
obj._agents = pl.concat([non_masked_df, masked_df], how="diagonal_relaxed") | ||
obj._agents = original_index.join(obj._agents, on="unique_id", how="left") | ||
obj._update_mask(original_index, unique_id_column) | ||
return obj | ||
|
||
def select( | ||
|
@@ -371,9 +385,9 @@ | |
elif mask == "active": | ||
return self._mask | ||
elif isinstance(mask, Collection): | ||
return bool_mask_from_series(pl.Series(mask)) | ||
return bool_mask_from_series(pl.Series(mask, dtype=pl.UInt64)) | ||
else: | ||
return bool_mask_from_series(pl.Series([mask])) | ||
return bool_mask_from_series(pl.Series([mask], dtype=pl.UInt64)) | ||
|
||
def _get_masked_df( | ||
self, | ||
|
@@ -404,9 +418,9 @@ | |
return self._agents.filter(self._mask) | ||
else: | ||
if isinstance(mask, Collection): | ||
mask_series = pl.Series(mask) | ||
mask_series = pl.Series(mask, dtype=pl.UInt64) | ||
else: | ||
mask_series = pl.Series([mask]) | ||
mask_series = pl.Series([mask], dtype=pl.UInt64) | ||
if not mask_series.is_in(self._agents["unique_id"]).all(): | ||
raise KeyError( | ||
"Some 'unique_id' of mask are not present in DataFrame 'unique_id'." | ||
|
@@ -450,6 +464,11 @@ | |
super().__getattr__(key) | ||
return self._agents[key] | ||
|
||
def _generate_unique_ids(self, n: int) -> pl.Series: | ||
return pl.Series( | ||
self.random.integers(1, np.iinfo(np.uint64).max, size=n, dtype=np.uint64) | ||
) | ||
|
||
@overload | ||
def __getitem__( | ||
self, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You cannot add agents from another AgentSetPolars