Skip to content

Replace usage of legacy numpy random api with modern api#7985

Merged
jenshnielsen merged 6 commits intomicrosoft:mainfrom
jenshnielsen:numpy_random_update
Mar 30, 2026
Merged

Replace usage of legacy numpy random api with modern api#7985
jenshnielsen merged 6 commits intomicrosoft:mainfrom
jenshnielsen:numpy_random_update

Conversation

@jenshnielsen
Copy link
Copy Markdown
Collaborator

No description provided.

…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().
@jenshnielsen jenshnielsen requested a review from a team as a code owner March 30, 2026 06:42
@jenshnielsen jenshnielsen requested a review from Copilot March 30, 2026 06:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 with np.random.default_rng() + Generator methods.
  • Introduced local/per-scope RNG instances in several places to avoid relying on global RNG state.
  • Removed NPY002 from Ruff ignore list in pyproject.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
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.08%. Comparing base (b60f7cb) to head (fc53730).
⚠️ Report is 7 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jenshnielsen jenshnielsen enabled auto-merge March 30, 2026 07:39
@jenshnielsen jenshnielsen added this pull request to the merge queue Mar 30, 2026
Merged via the queue into microsoft:main with commit e517667 Mar 30, 2026
18 checks passed
@jenshnielsen jenshnielsen deleted the numpy_random_update branch March 30, 2026 08:28
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.

3 participants