Repository navigation
Accept numpy integers and floats in AgentSet.select and CellCollection.select - #4153
Conversation
|
Performance benchmarks:
|
|
Performance benchmarks:
|
|
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. |
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. |
|
makes sense, thanks for confirming. |
|
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 |
|
|
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
left a comment
There was a problem hiding this comment.
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
CellCollectiontests are nearly a copy of theAgentSetones. Since both go through_resolve_at_most, one accept and one reject case forCellCollectionwould be enough.
Once I start moving stuff into a |
…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.
6214f94 to
8347a7d
Compare
Accept numpy integers and floats in
AgentSet.selectandCellCollection.selectselect(at_most=...)checkedisinstance(at_most, (int, float)), which rejects numpy scalars other than np.float64 (np.int64, np.uint8, np.float32, ...) with a TypeError, even thoughselect_randomalready 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._normalize_count, which accepts Python and numpy ints/floats, rejects Python and numpy bools, and returns a plain int or float._resolve_at_most, which holds the at_most validation that was duplicated in AbstractAgentSet.select and CellCollection.select._normalize_countinselect_randominstead of numbers.Integral/numbers.Real, so all three methods share one idiom.out of range, NaN) and numpy bools.
Closing #4035
I am inclined to add a mesa
utilitiesmodule in the near future and move the two new helper functions (and some existing helper stuff) there.Claude was used in developing this PR.