Skip to content

to replace random.seed(seed=None) using dedicated Generator instance(… #25765

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

Closed
wants to merge 4 commits into from
Closed

Conversation

qqwqqw689
Copy link

…according to numpy reference)

PR Summary

PR Checklist

Linked Issue

  • Added "closes #0000" in the PR description to link it to the original issue.

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@@ -11,12 +11,14 @@
plt.style.use('_mpl-gallery')

# make the data
rng = np.random.default_rng(seed=None)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
rng = np.random.default_rng(seed=None)
rng = np.random.default_rng(seed=19680801)

We use 19680801 as the seed anyplace we have to put in a seed.

Copy link
Member

Choose a reason for hiding this comment

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

This is not true for the "Plot types" examples. They all use 3, likely because they were taken from the cheat sheets. I'm in (weak) favour of keeping that (a) for consistency and (b) because 19680801 is more noisy.

Copy link
Member

Choose a reason for hiding this comment

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

@qqwqqw689 This needs a seed. I'd use "3" in this case as well.

I'm not clear why you have only changed one example.

@tacaswell
Copy link
Member

Thank you for your work on this, however I am a bit torn. The point of this example is to show how to use scatter not how to use numpy rng machinery. Would it be better to change the data all together to avoid it?

Just how strongly do the numpy devs want us to stop using the old way?

@qqwqqw689
Copy link
Author

The following is the original sentence.
I think maybe numpy devs just advise it .Not compulsory.

This is a convenience, legacy function(i.e. numpy.random.seed) that exists to support older code that uses the singleton RandomState. Best practice is to use a dedicated Generator instance rather than the random variate generation methods exposed directly in the random module.

@rcomer
Copy link
Member

rcomer commented Apr 25, 2023

Similar discussion at #19706, although back then the newness of the generators was also a concern.

@timhoffm
Copy link
Member

Just how strongly do the numpy devs want us to stop using the old way?

https://numpy.org/doc/stable/reference/random/legacy.html#functions-in-numpy-random states

Many of the RandomState methods above are exported as functions in numpy.random This usage is discouraged, as it is implemented via a global RandomState instance which is not advised on two counts: [...]

As of today I'm +/-0 on switching. While I like the old functional approach a bit better (feels simpler than creating rng state explicitly), the recommendation is clear and we should switch ultimately. I would not have actively pushed for the switch yet, but the new random generators are old enough now, so that there are no practical concerns.

@rcomer
Copy link
Member

rcomer commented Apr 26, 2023

There are quite a lot of them...
$ grep -rl "np.random.seed" .
./plot_types/3D/scatter3d_simple.py
./plot_types/basic/bar.py
./plot_types/basic/fill_between.py
./plot_types/basic/step.py
./plot_types/basic/stem.py
./plot_types/basic/scatter_plot.py
./plot_types/stats/ecdf.py
./plot_types/stats/hist2d.py
./plot_types/stats/violin.py
./plot_types/stats/eventplot.py
./plot_types/stats/hexbin.py
./plot_types/stats/boxplot_plot.py
./plot_types/stats/hist_plot.py
./plot_types/stats/errorbar_plot.py
./plot_types/unstructured/tricontour.py
./plot_types/unstructured/triplot.py
./plot_types/unstructured/tricontourf.py
./plot_types/unstructured/tripcolor.py
./tutorials/pyplot.py
./tutorials/artists.py
./users_explain/quick_start.py
./users_explain/axes/mosaic.py
./users_explain/axes/colorbar_placement.py
./users_explain/axes/index.rst
./users_explain/artists/paths.py
./users_explain/colors/colormap-manipulation.py
./examples/ticks/date_demo_rrule.py
./examples/ticks/auto_ticks.py
./examples/ticks/date_concise_formatter.py
./examples/ticks/colorbar_tick_labelling_demo.py
./examples/ticks/dollar_ticks.py
./examples/units/artist_tests.py
./examples/axisartist/demo_floating_axes.py
./examples/style_sheets/plot_solarizedlight2.py
./examples/style_sheets/grayscale.py
./examples/style_sheets/style_sheets_reference.py
./examples/style_sheets/bmh.py
./examples/style_sheets/fivethirtyeight.py
./examples/style_sheets/ggplot.py
./examples/userdemo/annotate_text_arrow.py
./examples/event_handling/pick_event_demo2.py
./examples/event_handling/pick_event_demo.py
./examples/event_handling/keypress_demo.py
./examples/event_handling/data_browser.py
./examples/event_handling/lasso_demo.py
./examples/event_handling/looking_glass.py
./examples/event_handling/zoom_window.py
./examples/axes_grid1/demo_anchored_direction_arrows.py
./examples/axes_grid1/scatter_hist_locatable_axes.py
./examples/pie_and_polar_charts/polar_scatter.py
./examples/pie_and_polar_charts/polar_bar.py
./examples/statistics/hexbin_demo.py
./examples/statistics/histogram_histtypes.py
./examples/statistics/boxplot_color.py
./examples/statistics/violinplot.py
./examples/statistics/histogram_cumulative.py
./examples/statistics/errorbars_and_boxes.py
./examples/statistics/bxp.py
./examples/statistics/customized_violin.py
./examples/statistics/confidence_ellipse.py
./examples/statistics/multiple_histograms_side_by_side.py
./examples/statistics/boxplot_vs_violin.py
./examples/statistics/boxplot.py
./examples/statistics/histogram_multihist.py
./examples/statistics/histogram_features.py
./examples/statistics/boxplot_demo.py
./examples/mplot3d/scatter3d.py
./examples/mplot3d/hist3d.py
./examples/mplot3d/bars3d.py
./examples/mplot3d/2dcollections3d.py
./examples/mplot3d/polys3d.py
./examples/scales/power_norm.py
./examples/scales/scales.py
./examples/specialty_plots/hinton_demo.py
./examples/text_labels_and_annotations/custom_legends.py
./examples/text_labels_and_annotations/line_with_text.py
./examples/text_labels_and_annotations/placing_text_boxes.py
./examples/text_labels_and_annotations/watermark_text.py
./examples/text_labels_and_annotations/align_ylabels.py
./examples/widgets/range_slider.py
./examples/widgets/lasso_selector_demo_sgskip.py
./examples/widgets/span_selector.py
./examples/widgets/cursor.py
./examples/animation/frame_grabbing_sgskip.py
./examples/animation/strip_chart.py
./examples/animation/animated_histogram.py
./examples/animation/random_walk.py
./examples/animation/rain.py
./examples/animation/bayes_update.py
./examples/animation/unchained.py
./examples/animation/animation_demo.py
./examples/lines_bars_and_markers/fill_between_alpha.py
./examples/lines_bars_and_markers/csd_demo.py
./examples/lines_bars_and_markers/stackplot_demo.py
./examples/lines_bars_and_markers/vline_hline_demo.py
./examples/lines_bars_and_markers/spectrum_demo.py
./examples/lines_bars_and_markers/psd_demo.py
./examples/lines_bars_and_markers/scatter_with_legend.py
./examples/lines_bars_and_markers/scatter_hist.py
./examples/lines_bars_and_markers/scatter_masked.py
./examples/lines_bars_and_markers/barh.py
./examples/lines_bars_and_markers/scatter_star_poly.py
./examples/lines_bars_and_markers/bar_label_demo.py
./examples/lines_bars_and_markers/gradient_bar.py
./examples/lines_bars_and_markers/stairs_demo.py
./examples/lines_bars_and_markers/multivariate_marker_plot.py
./examples/lines_bars_and_markers/xcorr_acorr_demo.py
./examples/lines_bars_and_markers/eventcollection_demo.py
./examples/lines_bars_and_markers/filled_step.py
./examples/lines_bars_and_markers/cohere.py
./examples/lines_bars_and_markers/eventplot_demo.py
./examples/showcase/anatomy.py
./examples/misc/coords_report.py
./examples/misc/multiprocess_sgskip.py
./examples/misc/histogram_path.py
./examples/misc/keyword_plotting.py
./examples/misc/bbox_intersect.py
./examples/images_contours_and_fields/pcolormesh_levels.py
./examples/images_contours_and_fields/image_annotated_heatmap.py
./examples/images_contours_and_fields/pcolor_demo.py
./examples/images_contours_and_fields/spy_demos.py
./examples/images_contours_and_fields/multi_image.py
./examples/images_contours_and_fields/specgram_demo.py
./examples/images_contours_and_fields/interpolation_methods.py
./examples/images_contours_and_fields/image_demo.py
./examples/images_contours_and_fields/irregulardatagrid.py
./examples/images_contours_and_fields/image_zcoord.py
./examples/spines/spines_dropped.py
./examples/color/set_alpha.py
./examples/subplots_axes_and_figures/subplots_adjust.py
./examples/subplots_axes_and_figures/subfigures.py
./examples/subplots_axes_and_figures/broken_axis.py
./examples/subplots_axes_and_figures/secondary_axis.py
./examples/subplots_axes_and_figures/axes_demo.py
./examples/subplots_axes_and_figures/axes_box_aspect.py
./examples/user_interfaces/svg_histogram_sgskip.py
./examples/shapes_and_collections/dolphin.py
./examples/shapes_and_collections/scatter.py
./examples/shapes_and_collections/patch_collection.py
./examples/shapes_and_collections/ellipse_demo.py

@jklymak
Copy link
Member

jklymak commented Apr 27, 2023

I'm not in favour of a bunch of churn to "fix" these. OTOH, new issues should probably use the new generators, and folks could clean these up if doing other work to the example.

@tacaswell
Copy link
Member

I am now leaning towards switching all of our examples to use the new style RNGs. There are good technical reasons to switch and as we would like our down-stream projects to switch to using the explicit API in their docs, we should reciprocate when our up-streams make a similar request.

@rcomer
Copy link
Member

rcomer commented May 3, 2023

I am now leaning towards switching all of our examples to use the new style RNGs.

While looking for something else, I saw that ruff can do this for us 👀
https://beta.ruff.rs/docs/rules/numpy-legacy-random/

Edit: actually it is just a check rather than an autofix…

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

Successfully merging this pull request may close these issues.

6 participants