Replace usage of legacy numpy random api with modern api#7985
Merged
jenshnielsen merged 6 commits intomicrosoft:mainfrom Mar 30, 2026
Merged
Replace usage of legacy numpy random api with modern api#7985jenshnielsen merged 6 commits intomicrosoft:mainfrom
jenshnielsen merged 6 commits intomicrosoft:mainfrom
Conversation
…uments Replace np.random.rand, np.random.randn with np.random.default_rng() methods (random, standard_normal) to fix NPY002 lint violations. The RNG is created once per class instance in __init__ and reused across calls rather than creating a new generator on every invocation.
Replace np.random.rand, np.random.randn, np.random.randint, np.random.choice, np.random.random, np.random.random_sample, and np.random.seed with np.random.default_rng() equivalents to fix NPY002 lint violations. For seeded tests that relied on np.random.seed() to reproduce mock instrument output, capture the actual parameter data during the measurement and use it directly as expected values instead.
Replace np.random.rand, np.random.randn, np.random.randint, np.random.random_sample, and np.random.shuffle with np.random.default_rng() equivalents to fix NPY002 lint violations. Extract rng = np.random.default_rng() before loops to avoid creating a new generator on every iteration.
Fix NPY002 lint violation by using np.random.default_rng().random() instead of np.random.rand().
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the codebase to use NumPy’s modern Generator-based random API (default_rng) instead of the legacy global np.random.* functions, and removes the corresponding lint ignore.
Changes:
- Replaced legacy
np.random.*usage across tests, benchmarks, mock instruments, and docs withnp.random.default_rng()+Generatormethods. - Introduced local/per-scope RNG instances in several places to avoid relying on global RNG state.
- Removed
NPY002from Ruff ignore list inpyproject.toml.
Reviewed changes
Copilot reviewed 33 out of 33 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/validators/test_arrays.py | Uses default_rng().integers instead of np.random.randint in a validator test. |
| tests/parameter/test_parameter_with_setpoints.py | Replaces numpy.random.rand import with a module-level Generator. |
| tests/parameter/test_parameter_scale_offset.py | Updates randn usage to standard_normal. |
| tests/drivers/test_tektronix_AWG70000A.py | Migrates waveform/mask generation to Generator. |
| tests/drivers/test_tektronix_AWG5014C.py | Migrates waveform/mask generation to Generator. |
| tests/drivers/test_Keithley_2450.py | Migrates choice calls to Generator.choice. |
| tests/drivers/keysight_b1500/b1500_driver_tests/test_sampling_measurement.py | Replaces global seeding + rand with seeded default_rng(1). |
| tests/dataset_generators.py | Updates outlier dataset generator to default_rng().random. |
| tests/dataset/test_string_data.py | Migrates choice for string sampling to Generator.choice. |
| tests/dataset/test_plotting.py | Replaces random data sources for hypothesis examples and dummy instrument getters. |
| tests/dataset/test_dataset_basic.py | Migrates random array generation to Generator.random. |
| tests/dataset/test_database_creation_and_upgrading.py | Uses a per-run Generator for nested random loops. |
| tests/dataset/test__get_data_from_ds.py | Migrates multidimensional rand to Generator.random. |
| tests/dataset/measurement/test_measurement_context_manager.py | Migrates randn usage to standard_normal; adjusts expected values to avoid global seeding. |
| tests/dataset/conftest.py | Introduces instance RNGs in dummy spectrum/parameter implementations; replaces global seeding. |
| src/qcodes/instrument_drivers/mock_instruments/init.py | Adds per-instance RNGs and migrates noise/random generation to Generator. |
| pyproject.toml | Removes NPY002 from Ruff ignore list. |
| docs/examples/writing_drivers/A-ParameterWithSetpoints-Example-with-Dual-Setpoints.ipynb | Updates randn to default_rng().standard_normal. |
| docs/examples/plotting/auto_color_scale.ipynb | Uses a local Generator for repeated random calls. |
| docs/examples/Parameters/Simple-Example-of-ParameterWithSetpoints.ipynb | Updates rand to default_rng().random. |
| docs/examples/Parameters/Parameter-With-Setpoints-defined-on-a-different-instrument.ipynb | Updates rand to default_rng().random. |
| docs/examples/DataSet/Threaded data acquisition.ipynb | Uses Generator.standard_normal for noise in the decay example. |
| docs/examples/DataSet/Saving_data_in_the_background.ipynb | Updates scalar/vector noise generation to Generator API. |
| docs/examples/DataSet/Performing-measurements-using-qcodes-parameters-and-dataset.ipynb | Introduces local RNGs and migrates random sampling. |
| docs/examples/DataSet/Pedestrian example of subscribing to a DataSet.ipynb | Introduces local RNGs and migrates randn usage. |
| docs/examples/DataSet/Paramtypes explained.ipynb | Updates randn to default_rng().standard_normal. |
| docs/examples/DataSet/Offline plotting with categorical data.ipynb | Adds local RNGs and migrates random sampling calls. |
| docs/examples/DataSet/Offline Plotting Tutorial.ipynb | Uses local RNGs for shuffling and noise. |
| docs/examples/DataSet/Linking to parent datasets.ipynb | Updates noise generation to Generator.standard_normal. |
| docs/examples/DataSet/Extracting-runs-from-one-DB-file-to-another.ipynb | Updates randn loops to Generator.standard_normal. |
| docs/examples/DataSet/Dataset_Performance.ipynb | Uses a local RNG in performance example. |
| docs/examples/DataSet/Benchmarking.ipynb | Uses a local RNG in benchmarking examples. |
| benchmarking/benchmarks/dataset.py | Uses a local RNG for benchmark value generation. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7985 +/- ##
=======================================
Coverage 70.08% 70.08%
=======================================
Files 333 333
Lines 32250 32250
=======================================
+ Hits 22602 22604 +2
+ Misses 9648 9646 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
astafan8
approved these changes
Mar 30, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.