Skip to content

Examples: Replace deprecated "np.random.seed()" with "np.random.Generator()" (NPY002 warning by ruff) #2781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 31, 2023

Conversation

yvonnefroehlich
Copy link
Member

@yvonnefroehlich yvonnefroehlich commented Oct 30, 2023

Description of proposed changes

This PR aims to update the examples to follow ruff's NumPy rules, especially it addresses the NPY002 warning (please see https://docs.astral.sh/ruff/rules/numpy-legacy-random/):

NPY002 Replace legacy `np.random.seed` call with `np.random.Generator`

Example:

import numpy as np
np.random.seed(42)
np.random.normal()

is replaced by

rng = np.random.default_rng(seed=42)
rng.normal()

Affected examples:

  • gallery: histogram - old -> new
  • gallery: scatter_and_histograms - old -> new
  • gallery: points - old -> new
  • gallery: scatter - old -> new
  • tutorial: cartesian_histograms - old -> new

Please note that the plots are not completely identical. This is because the generated random numbers are not identical.

Related to the PRs

Fixes #2779 partly

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.
  • Use underscores (not hyphens) in names of Python files and directories.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@yvonnefroehlich yvonnefroehlich added the documentation Improvements or additions to documentation label Oct 30, 2023
@yvonnefroehlich yvonnefroehlich added this to the 0.11.0 milestone Oct 30, 2023
@seisman
Copy link
Member

seisman commented Oct 31, 2023

The PR title makes no sense to people who don't know NPY002. Could you please update the PR title to make it more clear what the changes are?

@yvonnefroehlich yvonnefroehlich changed the title Examples: Update regarding "NPY002" Examples: Replaace "np.random.seed" with "np.random.Generator" (NYP002 warning by ruff) Oct 31, 2023
@yvonnefroehlich yvonnefroehlich changed the title Examples: Replaace "np.random.seed" with "np.random.Generator" (NYP002 warning by ruff) Examples: Replace "np.random.seed" with "np.random.Generator" (NYP002 warning by ruff) Oct 31, 2023
@yvonnefroehlich yvonnefroehlich changed the title Examples: Replace "np.random.seed" with "np.random.Generator" (NYP002 warning by ruff) Examples: Replace deprecated "np.random.seed()" with "np.random.Generator()" (NYP002 warning by ruff) Oct 31, 2023
@yvonnefroehlich yvonnefroehlich changed the title Examples: Replace deprecated "np.random.seed()" with "np.random.Generator()" (NYP002 warning by ruff) Examples: Replace deprecated "np.random.seed()" with "np.random.Generator()" (NPY002 warning by ruff) Oct 31, 2023
Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

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

Minor suggestion to use kwargs, otherwise looks good! Just wait for the docs to be rebuilt and double-check that the transparency issue has been fixed with #2783.

@yvonnefroehlich
Copy link
Member Author

The PR title makes no sense to people who don't know NPY002. Could you please update the PR title to make it more clear what the changes are?

I have improved the title, added the links to the previews, and expanded the comment explaining this PR.

yvonnefroehlich and others added 5 commits October 31, 2023 08:48
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
yvonnefroehlich and others added 2 commits October 31, 2023 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ruff: Enable NPY (NumPy-specific) rules
3 participants