Skip to content

fix(core): accept NumPy integer seeds - #536

Open
ChrisW09 wants to merge 3 commits into
mainfrom
fix/numpy-integer-seeds
Open

ChrisW09 wants to merge 3 commits into
mainfrom
fix/numpy-integer-seeds

Conversation

@ChrisW09

@ChrisW09 ChrisW09 commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #535

Problem

set_seed() validated seeds with isinstance(seed, int), so NumPy integer scalars raised ValueError: seed must be a non-negative integer. These come from rng.integers(...), np.arange(...) or a pandas column. The check failed both for direct calls and for random_state=np.int64(...) on any estimator. TabularDataModule also passed random_state straight to torch.Generator.manual_seed(), which needs a Python int.

Fix

  • deeptab/core/reproducibility.py: set_seed() accepts any non-negative numbers.Integral and converts it with int() before seeding Python, NumPy and torch. Non-integers (1.0, "7", np.float64) are still rejected. The numpydoc mentions NumPy integer scalars.
  • deeptab/data/datamodule.py: TabularDataModule stores random_state as a Python int (or None). Python and NumPy integers are converted, and the parameter is annotated int | np.integer | None; floats and strings raise TypeError instead of being truncated by int().

Tests

In tests/core/test_random_state.py:

  • TestSetSeedPrimitives::test_numpy_integer_seed_matches_python_int[int64|uint32|int32]: the same torch draws as the equivalent Python int.
  • TestSetSeedPrimitives::test_non_integer_seed_is_still_rejected[float|str|float64]
  • test_datamodule_rejects_non_integer_random_state[float|str] and test_datamodule_converts_numpy_integer_random_state
  • test_estimator_accepts_numpy_integer_random_state: an MLPRegressor with random_state=np.int64(...) gives the same predictions as with the Python int.

The NumPy-integer tests fail on main and pass with this change.

Docs

The set_seed docstring and the TabularDataModule random_state parameter are updated. No page changes needed.

Checks

  • just test: 3246 passed, 46 skipped, 28 xfailed; coverage 91.29%
  • just docs: build succeeded (warnings treated as errors)
  • just check: all hooks passed (ruff, prettier, pyright)

set_seed() only accepted Python ints, so NumPy integer scalars (from
rng.integers(), np.arange() or a pandas column) raised "seed must be a
non-negative integer", both directly and as an estimator's random_state.
Accept any numbers.Integral and convert it to int before seeding, and
store TabularDataModule.random_state as an int for torch.Generator.

Fixes #535
int() also turned 1.5 into 1 and "7" into 7, which used to fail. Only
Python and NumPy integers are converted now. The set_seed comment names
the call that actually rejects NumPy integers (random.seed), and the
parametrized seed tests get readable ids.

Refs #535

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] NumPy integer seeds are rejected: set_seed(np.int64(7)) and random_state=np.int64(...) raise ValueError

1 participant