Skip to content

Accept numpy integer and float scalars in AgentSet.select_random - #3906

Merged
quaquel merged 1 commit into
mesa:mainfrom
Shivansh1205:fix/select-random-numpy-int
Oct 6, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
Shivansh1205:fix/select-random-numpy-int

Conversation

@Shivansh1205

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

AgentSet.select_random(n) raised TypeError for numpy integer scalars (np.int64, np.int32, np.uint8, ...) and for np.float32 fractions.

Bug / Issue

Fixes #3905

The type check used isinstance(n, (int, float)). NumPy integers aren't int subclasses, and np.float32 isn't a float subclass, so sample sizes coming from model.rng.integers(...) or numpy arithmetic were rejected. select(at_most=np.int64(...)) already accepts them.

Implementation

  • Validate with numbers.Real and branch on numbers.Integral. NumPy registers its scalar types with both ABCs.
  • Normalize an integral n to a Python int before use.
  • Reject np.bool_ alongside bool, so the existing "booleans are not sizes" rule still holds for numpy booleans.

Error messages and every other validation path are unchanged.

Testing

  • test_select_random_accepts_numpy_numbers is parametrized over np.int64, np.int32, np.uint8, np.float64 and np.float32.
    • The integer and np.float32 cases fail before the change with TypeError. np.float64 already passed because it subclasses float.
  • test_select_random_rejects_numpy_bool checks that np.True_ still raises TypeError.
  • tests/test_agentset.py, tests/test_agent.py and tests/test_model.py: 73 passed.
  • ruff check / ruff format --check clean.

select_random validated n with isinstance(n, (int, float)). NumPy integer
scalars are not int subclasses and np.float32 is not a float subclass, so
a sample size computed with model.rng or numpy arithmetic raised
TypeError, while select(at_most=np.int64(...)) worked.

Validate against numbers.Integral / numbers.Real instead and normalize
integral n to a Python int. bool and np.bool_ are still rejected.

Fixes mesa#3905
@github-actions github-actions Bot added the testing Release notes label label Oct 1, 2026
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +1.3% [+0.9%, +1.7%] 🔵 +2.1% [+1.8%, +2.4%]
BoltzmannWealth large 🔵 +1.2% [+0.5%, +1.8%] 🔵 -0.6% [-1.2%, +0.1%]
Schelling small 🔵 +0.2% [-0.3%, +0.6%] 🔵 +0.4% [-0.5%, +1.2%]
Schelling large 🔵 +1.6% [+0.9%, +2.2%] 🔵 +2.3% [+1.1%, +3.3%]
WolfSheep small 🔵 +2.6% [+2.1%, +3.2%] 🔵 +4.2% [+0.1%, +8.5%]
WolfSheep large 🔵 +0.4% [-0.5%, +1.1%] 🔵 +0.7% [-0.9%, +2.1%]
SugarscapeG1mt small 🔵 +2.1% [+1.7%, +2.6%] 🔵 +0.2% [-1.6%, +1.9%]
SugarscapeG1mt large 🔵 +1.4% [+0.5%, +2.4%] 🔵 +2.7% [+1.1%, +4.3%]
BoidFlockers small 🔵 +2.1% [+1.1%, +3.1%] 🔵 +2.6% [+2.2%, +3.0%]
BoidFlockers large 🔵 +0.6% [+0.1%, +1.3%] 🔵 +1.1% [+0.9%, +1.4%]
MultiLevelAllianceModel small 🔴 +11.2% [+9.0%, +13.3%] 🔵 +0.6% [+0.3%, +1.0%]
MultiLevelAllianceModel large 🔴 +12.5% [+10.6%, +14.6%] 🔵 -0.8% [-1.5%, -0.2%]

@quaquel quaquel added the enhancement Release notes label label Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AgentSet.select_random rejects numpy integers (e.g. np.int64) for n

2 participants