Skip to content

Accept numpy integers and floats in AgentSet.select and CellCollection.select - #4153

Merged
quaquel merged 1 commit into
mesa:mainfrom
quaquel:select-numpy-scalars
Nov 25, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
quaquel:select-numpy-scalars

Conversation

@quaquel

@quaquel quaquel commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Accept numpy integers and floats in AgentSet.select and CellCollection.select

select(at_most=...) checked isinstance(at_most, (int, float)), which rejects numpy scalars other than np.float64 (np.int64, np.uint8, np.float32, ...) with a TypeError, even though select_random already accepts them. There have been two previous PRs on this, both of which went stale. Also, I apply the fix consistently to both AgentSet.select and CellCollection.select, and harmonize the handling of numpy numbers inside AgentSet itself.

  • Add _normalize_count, which accepts Python and numpy ints/floats, rejects Python and numpy bools, and returns a plain int or float.
  • Add _resolve_at_most, which holds the at_most validation that was duplicated in AbstractAgentSet.select and CellCollection.select.
  • Use _normalize_count in select_random instead of numbers.Integral/numbers.Real, so all three methods share one idiom.
  • Widen the type hints and update the docstrings.
  • Add tests for accepted numpy types, invalid numpy values (negative,
    out of range, NaN) and numpy bools.

Closing #4035

I am inclined to add a mesa utilities module in the near future and move the two new helper functions (and some existing helper stuff) there.

Claude was used in developing this PR.

@github-actions github-actions Bot added the testing Release notes label label Nov 20, 2026
@quaquel quaquel added bug Release notes label trigger-benchmarks Special label that triggers the benchmarking CI labels Nov 20, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -7.3% [-7.7%, -6.8%] 🔵 +1.2% [+1.0%, +1.5%]
BoltzmannWealth large 🔵 -2.1% [-2.8%, -1.4%] 🟢 -7.3% [-10.3%, -4.5%]
Schelling small 🔵 -2.0% [-2.5%, -1.4%] 🔵 -0.2% [-0.9%, +0.6%]
Schelling large 🔵 -0.9% [-1.7%, -0.1%] 🔵 -1.5% [-3.5%, +0.2%]
WolfSheep small 🔵 -0.1% [-0.4%, +0.2%] 🔵 -2.4% [-3.7%, -1.0%]
WolfSheep large 🔵 -0.0% [-1.1%, +1.1%] 🔵 -2.0% [-3.3%, -0.8%]
SugarscapeG1mt small 🔵 +2.2% [+1.6%, +2.8%] 🔵 +1.3% [-0.4%, +2.9%]
SugarscapeG1mt large 🔵 +0.0% [-1.1%, +1.2%] 🔵 -0.4% [-1.8%, +1.2%]
BoidFlockers small 🔵 +2.5% [+1.9%, +3.0%] 🔵 +1.1% [+0.6%, +1.6%]
BoidFlockers large 🔴 +4.4% [+3.3%, +5.4%] 🔵 +1.5% [+1.0%, +1.9%]
MultiLevelAllianceModel small 🔵 +1.5% [+0.4%, +2.7%] 🔵 -0.0% [-0.4%, +0.3%]
MultiLevelAllianceModel large 🔵 +2.0% [+1.0%, +3.0%] 🔵 +2.1% [+0.7%, +3.7%]

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -9.4% [-10.7%, -8.0%] 🔵 +0.2% [-0.2%, +0.6%]
BoltzmannWealth large 🟢 -5.5% [-7.5%, -3.2%] 🟢 -17.0% [-20.4%, -12.5%]
Schelling small 🔵 -2.3% [-3.4%, -1.1%] 🔵 -3.0% [-4.1%, -1.9%]
Schelling large 🔵 -2.9% [-3.9%, -2.0%] 🟢 -17.3% [-19.5%, -14.8%]
WolfSheep small 🔵 -0.2% [-1.1%, +0.7%] 🔵 -1.1% [-3.6%, +1.8%]
WolfSheep large 🔵 -1.4% [-4.0%, +1.5%] 🔵 -0.3% [-1.9%, +1.7%]
SugarscapeG1mt small 🔵 +1.9% [+0.9%, +2.8%] 🔵 +1.1% [-0.9%, +3.3%]
SugarscapeG1mt large 🔵 +2.1% [-0.8%, +4.5%] 🔵 -1.1% [-4.4%, +2.3%]
BoidFlockers small 🔵 +1.4% [-0.1%, +2.9%] 🔵 -0.5% [-0.9%, -0.2%]
BoidFlockers large 🔵 -1.9% [-3.3%, -0.7%] 🔵 -2.3% [-2.6%, -2.0%]
MultiLevelAllianceModel small 🔵 -3.6% [-5.3%, -1.5%] 🔵 -2.4% [-3.2%, -1.6%]
MultiLevelAllianceModel large 🔵 +1.4% [-1.0%, +3.5%] 🔴 +11.2% [+9.5%, +12.7%]

@quaquel quaquel removed the trigger-benchmarks Special label that triggers the benchmarking CI label Nov 20, 2026
@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

nice cleanup, and the shared helper makes both classes behave the same way. i checked this against main: the numpy rejection in select is real there (np.int64 and np.float32 both raise TypeError on main) and this branch fixes it, with bools, nan and out-of-range values behaving exactly as before.

one small behaviour change that may or may not be intended: on main, select_random accepted any numbers.Real, so fractions.Fraction worked (select_random(n=Fraction(1, 2)) selects half the agents). _normalize_count narrows that to python and numpy ints and floats, so Fraction now raises TypeError. probably fine in practice, but worth a docstring line if intentional.

+1 on the utilities module - cell_collection importing the private _resolve_at_most works, but a shared home would make it less awkward.

@quaquel

quaquel commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

one small behaviour change that may or may not be intended: on main, select_random accepted any numbers.Real, so fractions.Fraction worked (select_random(n=Fraction(1, 2)) selects half the agents). _normalize_count narrows that to python and numpy ints and floats, so Fraction now raises TypeError. probably fine in practice, but worth a docstring line if intentional.

It's intentional. Also, allowing Fraction is something that came along with one the previous fixes. It has never been officially released on 3.5. So we can change it again if we want without needed a docstring remark on it.

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

makes sense, thanks for confirming.

@priyanshux-dev

Copy link
Copy Markdown
Contributor

Nice cleanup. The shared helper makes the behavior consistent across AgentSet and CellCollection, and accepting NumPy scalars here looks sensible.

One question: Would it be useful to add a test for another NumPy floating scalar type, such as np.float128 where available? I’m curious whether the intent is to accept all NumPy floating scalars.

@quaquel

quaquel commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

One question: Would it be useful to add a test for another NumPy floating scalar type, such as np.float128 where available? I’m curious whether the intent is to accept all NumPy floating scalars.

np.floating is the abstract base class for all NumPy floating-point scalar type. I fail to see the point of testing all possible NumPy floating-point types.

@mesa-triage-bot

Copy link
Copy Markdown

Thanks for fixing NumPy scalar selection consistently in both collections, @quaquel. Checks have no failures, but GitHub currently reports merge conflicts.

Please rebase onto main and resolve the conflicts so this can proceed to review.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

@EwoutH EwoutH left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good: with one shared helper, AgentSet.select, select_random and CellCollection.select now handle numpy scalars the same way. Tests pass locally.

Two small things:

  • The branch conflicts with main in tests/discrete_space/test_discrete_space.py (only adjacent new tests), so it needs a rebase.
  • Optional: the CellCollection tests are nearly a copy of the AgentSet ones. Since both go through _resolve_at_most, one accept and one reject case for CellCollection would be enough.

@quaquel

quaquel commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

Optional: the CellCollection tests are nearly a copy of the AgentSet ones. Since both go through _resolve_at_most, one accept and one reject case for CellCollection would be enough.

Once I start moving stuff into a utlitilities.py, we can simplify this. I'll keep it for now.

…n.select

`select(at_most=...)` checked `isinstance(at_most, (int, float))`, which
rejects numpy scalars other than np.float64 (np.int64, np.uint8,
np.float32, ...) with a TypeError, even though `select_random` already
accepts them.

- Add `_normalize_count`, which accepts Python and numpy ints/floats,
  rejects Python and numpy bools, and returns a plain int or float.
- Add `_resolve_at_most`, which holds the at_most validation that was
  duplicated in AbstractAgentSet.select and CellCollection.select.
- Use `_normalize_count` in `select_random` instead of
  numbers.Integral/numbers.Real, so all three methods share one idiom.
- Widen the type hints and update the docstrings.
- Add tests for accepted numpy types, invalid numpy values (negative,
  out of range, NaN) and numpy bools.
@quaquel
quaquel force-pushed the select-numpy-scalars branch from 6214f94 to 8347a7d Compare November 25, 2026 19:40
@quaquel
quaquel merged commit 5fa3140 into mesa:main Nov 25, 2026
17 checks passed
@quaquel
quaquel deleted the select-numpy-scalars branch November 25, 2026 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

select(at_most=...) rejects NumPy numeric scalars (regression from #3868)

4 participants