Skip to content

Commit

Permalink
MAINT: use random Generator instead of RandomState
Browse files Browse the repository at this point in the history
  • Loading branch information
mdhaber committed Feb 20, 2024
1 parent d8fb164 commit fb8e277
Show file tree
Hide file tree
Showing 26 changed files with 53 additions and 40 deletions.
5 changes: 3 additions & 2 deletions advanced/debugging/to_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import numpy as np
import scipy as sp
rng = np.random.default_rng(27446968)

FUNCTIONS = (
np.tan, # Dilating map
Expand Down Expand Up @@ -52,8 +53,8 @@ def compare_optimizers(optimizers):
functions all admitting a single root in zero and a upper and
lower bounds.
"""
random_a = -1.3 + np.random.random(size=100)
random_b = 0.3 + np.random.random(size=100)
random_a = -1.3 + rng.random(size=100)
random_b = 0.3 + rng.random(size=100)
param_grid = product(FUNCTIONS, random_a, random_b)
print("Benching 1D root-finder optimizers from scipy.optimize:")
for optimizer in OPTIMIZERS:
Expand Down
5 changes: 3 additions & 2 deletions advanced/debugging/to_debug_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import numpy as np
import scipy as sp
rng = np.random.default_rng(27446968)

FUNCTIONS = (
np.tan, # Dilating map
Expand Down Expand Up @@ -52,8 +53,8 @@ def compare_optimizers(optimizers):
functions all admitting a single root in zero and a upper and
lower bounds.
"""
random_a = -1.3 + np.random.random(size=100)
random_b = 0.3 + np.random.random(size=100)
random_a = -1.3 + rng.random(size=100)
random_b = 0.3 + rng.random(size=100)
param_grid = product(FUNCTIONS, random_a, random_b)
values = []
for value in param_grid:
Expand Down
2 changes: 1 addition & 1 deletion advanced/debugging/wiener_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def iterated_wiener(noisy_img, size=3):
rng = np.random.default_rng(27446968)

face = sp.datasets.face(gray=True)
noisy_face = face + 20 * np.random.randint(3, size=face.shape) - 30
noisy_face = face + 20 * rng.integers(3, size=face.shape) - 30

plt.matshow(face[cut], cmap=plt.cm.gray)
plt.matshow(noisy_face[cut], cmap=plt.cm.gray)
Expand Down
4 changes: 2 additions & 2 deletions advanced/image_processing/examples/plot_GMM.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
n = 10
l = 256
im = np.zeros((l, l))
points = l * np.random.random((2, n**2))
points = l * rng.random((2, n**2))
im[(points[0]).astype(int), (points[1]).astype(int)] = 1
im = sp.ndimage.gaussian_filter(im, sigma=l / (4.0 * n))

mask = (im > im.mean()).astype(float)


img = mask + 0.3 * np.random.randn(*mask.shape)
img = mask + 0.3 * rng.normal(size=mask.shape)

hist, bin_edges = np.histogram(img, bins=60)
bin_centers = 0.5 * (bin_edges[:-1] + bin_edges[1:])
Expand Down
4 changes: 2 additions & 2 deletions advanced/image_processing/examples/plot_clean_morpho.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
n = 10
l = 256
im = np.zeros((l, l))
points = l * np.random.random((2, n**2))
points = l * rng.random((2, n**2))
im[(points[0]).astype(int), (points[1]).astype(int)] = 1
im = sp.ndimage.gaussian_filter(im, sigma=l / (4.0 * n))

mask = (im > im.mean()).astype(float)


img = mask + 0.3 * np.random.randn(*mask.shape)
img = mask + 0.3 * rng.normal(size=mask.shape)

binary_img = img > 0.5

Expand Down
4 changes: 3 additions & 1 deletion advanced/image_processing/examples/plot_denoising.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import scipy as sp
import matplotlib.pyplot as plt

rng = np.random.default_rng(27446968)

im = np.zeros((20, 20))
im[5:-5, 5:-5] = 1
im = sp.ndimage.distance_transform_bf(im)
im_noise = im + 0.2 * np.random.randn(*im.shape)
im_noise = im + 0.2 * rng.normal(size=im.shape)

im_med = sp.ndimage.median_filter(im_noise, 3)

Expand Down
4 changes: 3 additions & 1 deletion advanced/image_processing/examples/plot_face_denoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import scipy as sp
import matplotlib.pyplot as plt

rng = np.random.default_rng(27446968)

f = sp.datasets.face(gray=True)
f = f[230:290, 220:320]

noisy = f + 0.4 * f.std() * np.random.random(f.shape)
noisy = f + 0.4 * f.std() * rng.random(f.shape)

gauss_denoised = sp.ndimage.gaussian_filter(noisy, 2)
med_denoised = sp.ndimage.median_filter(noisy, 3)
Expand Down
4 changes: 3 additions & 1 deletion advanced/image_processing/examples/plot_face_tv_denoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

from skimage.restoration import denoise_tv_chambolle

rng = np.random.default_rng(27446968)

f = sp.datasets.face(gray=True)
f = f[230:290, 220:320]

noisy = f + 0.4 * f.std() * np.random.random(f.shape)
noisy = f + 0.4 * f.std() * rng.random(f.shape)

tv_denoised = denoise_tv_chambolle(noisy, weight=10)

Expand Down
4 changes: 3 additions & 1 deletion advanced/image_processing/examples/plot_find_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import scipy as sp
import matplotlib.pyplot as plt

rng = np.random.default_rng(27446968)

im = np.zeros((256, 256))
im[64:-64, 64:-64] = 1

Expand All @@ -33,7 +35,7 @@
plt.axis("off")
plt.title("Sobel filter", fontsize=20)

im += 0.07 * np.random.random(im.shape)
im += 0.07 * rng.random(im.shape)

sx = sp.ndimage.sobel(im, axis=0, mode="constant")
sy = sp.ndimage.sobel(im, axis=1, mode="constant")
Expand Down
2 changes: 1 addition & 1 deletion advanced/image_processing/examples/plot_find_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
n = 10
l = 256
im = np.zeros((l, l))
points = l * np.random.random((2, n**2))
points = l * rng.random((2, n**2))
im[(points[0]).astype(int), (points[1]).astype(int)] = 1
im = sp.ndimage.gaussian_filter(im, sigma=l / (4.0 * n))

Expand Down
2 changes: 1 addition & 1 deletion advanced/image_processing/examples/plot_granulo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def granulometry(data, sizes=None):
n = 10
l = 256
im = np.zeros((l, l))
points = l * np.random.random((2, n**2))
points = l * rng.random((2, n**2))
im[(points[0]).astype(int), (points[1]).astype(int)] = 1
im = sp.ndimage.gaussian_filter(im, sigma=l / (4.0 * n))

Expand Down
4 changes: 2 additions & 2 deletions advanced/image_processing/examples/plot_greyscale_dilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import matplotlib.pyplot as plt

im = np.zeros((64, 64))
np.random.seed(2)
x, y = (63 * np.random.random((2, 8))).astype(int)
rng = np.random.default_rng(27446968)
x, y = (63 * rng.random((2, 8))).astype(int)
im[x, y] = np.arange(8)

bigger_points = sp.ndimage.grey_dilation(im, size=(5, 5), structure=np.ones((5, 5)))
Expand Down
4 changes: 2 additions & 2 deletions advanced/image_processing/examples/plot_histo_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
n = 10
l = 256
im = np.zeros((l, l))
points = l * np.random.random((2, n**2))
points = l * rng.random((2, n**2))
im[(points[0]).astype(int), (points[1]).astype(int)] = 1
im = sp.ndimage.gaussian_filter(im, sigma=l / (4.0 * n))

mask = (im > im.mean()).astype(float)

mask += 0.1 * im

img = mask + 0.2 * np.random.randn(*mask.shape)
img = mask + 0.2 * rng.normal(size=mask.shape)

hist, bin_edges = np.histogram(img, bins=60)
bin_centers = 0.5 * (bin_edges[:-1] + bin_edges[1:])
Expand Down
2 changes: 1 addition & 1 deletion advanced/image_processing/examples/plot_measure_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
n = 10
l = 256
im = np.zeros((l, l))
points = l * np.random.random((2, n**2))
points = l * rng.random((2, n**2))
im[(points[0]).astype(int), (points[1]).astype(int)] = 1
im = sp.ndimage.gaussian_filter(im, sigma=l / (4.0 * n))

Expand Down
4 changes: 2 additions & 2 deletions advanced/image_processing/examples/plot_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

square = np.zeros((32, 32))
square[10:-10, 10:-10] = 1
np.random.seed(2)
x, y = (32 * np.random.random((2, 20))).astype(int)
rng = np.random.default_rng(27446968)
x, y = (32 * rng.random((2, 20))).astype(int)
square[x, y] = 1

open_square = sp.ndimage.binary_opening(square)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
mask = img.astype(bool)
img = img.astype(float)

img += 1 + 0.2 * np.random.randn(*img.shape)
rng = np.random.default_rng(27446968)
img += 1 + 0.2 * rng.normal(size=img.shape)

# Convert the image into a graph with the value of the gradient on the
# edges.
Expand Down
2 changes: 1 addition & 1 deletion advanced/image_processing/examples/plot_synthetic_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
n = 10
l = 256
im = np.zeros((l, l))
points = l * np.random.random((2, n**2))
points = l * rng.random((2, n**2))
im[(points[0]).astype(int), (points[1]).astype(int)] = 1
im = sp.ndimage.gaussian_filter(im, sigma=l / (4.0 * n))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def mk_costs(ndim=2):
"Rosenbrock ": (rosenbrock, rosenbrock_prime, rosenbrock_hessian),
}

rng = np.random.RandomState(0)
starting_points = 4 * rng.rand(20, ndim) - 2
rng = np.random.default_rng(5982345892)
starting_points = 4 * rng.random((20, ndim)) - 2
if ndim > 100:
starting_points = starting_points[:10]
return costs, starting_points
Expand Down
8 changes: 4 additions & 4 deletions intro/matplotlib/examples/pretty_plots/plot_text_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
rng = np.random.default_rng()

for i in range(24):
index = np.random.randint(0, len(eqs))
index = rng.integers(0, len(eqs))
eq = eqs[index]
size = np.random.uniform(12, 32)
x, y = np.random.uniform(0, 1, 2)
alpha = np.random.uniform(0.25, 0.75)
size = rng.uniform(12, 32)
x, y = rng.uniform(0, 1, 2)
alpha = rng.uniform(0.25, 0.75)
plt.text(
x,
y,
Expand Down
2 changes: 1 addition & 1 deletion intro/numpy/examples/plot_chebyfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
rng = np.random.default_rng(27446968)

x = np.linspace(-1, 1, 2000)
y = np.cos(x) + 0.3 * np.random.rand(2000)
y = np.cos(x) + 0.3 * rng.random(2000)
p = np.polynomial.Chebyshev.fit(x, y, 90)

plt.plot(x, y, "r.")
Expand Down
2 changes: 1 addition & 1 deletion intro/numpy/examples/plot_polyfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
rng = np.random.default_rng(27446968)

x = np.linspace(0, 1, 20)
y = np.cos(x) + 0.3 * np.random.rand(20)
y = np.cos(x) + 0.3 * rng.random(20)
p = np.poly1d(np.polyfit(x, y, 3))

t = np.linspace(0, 1, 200)
Expand Down
2 changes: 1 addition & 1 deletion intro/scipy/examples/plot_fftpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
period = 5.0

time_vec = np.arange(0, 20, time_step)
sig = np.sin(2 * np.pi / period * time_vec) + 0.5 * np.random.randn(time_vec.size)
sig = np.sin(2 * np.pi / period * time_vec) + 0.5 * rng.normal(size=time_vec.size)

plt.figure(figsize=(6, 5))
plt.plot(time_vec, sig, label="Original signal")
Expand Down
4 changes: 2 additions & 2 deletions intro/scipy/examples/plot_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
# Generate data
import numpy as np

np.random.seed(0)
rng = np.random.default_rng(27446968)
measured_time = np.linspace(0, 1, 10)
noise = 1e-1 * (np.random.random(10) * 2 - 1)
noise = 1e-1 * (rng.random(10) * 2 - 1)
measures = np.sin(2 * np.pi * measured_time) + noise

# Interpolate it to new time points
Expand Down
2 changes: 1 addition & 1 deletion packages/scikit-image/examples/plot_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
l = 256
rng = np.random.default_rng(27446968)
im = np.zeros((l, l))
points = l * np.random.random((2, n**2))
points = l * rng.random((2, n**2))
im[(points[0]).astype(int), (points[1]).astype(int)] = 1
im = filters.gaussian(im, sigma=l / (4.0 * n))
blobs = im > 0.7 * im.mean()
Expand Down
4 changes: 2 additions & 2 deletions packages/scikit-learn/examples/plot_polynomial_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
cmap_bold = ListedColormap(["#FF0000", "#00FF00", "#0000FF"])


rng = np.random.RandomState(0)
x = 2 * rng.rand(100) - 1
rng = np.random.default_rng(27446968)
x = 2 * rng.random(100) - 1

f = lambda t: 1.2 * t**2 + 0.1 * t**3 - 0.4 * t**5 - 0.5 * t**9
y = f(x) + 0.4 * rng.normal(size=100)
Expand Down
6 changes: 4 additions & 2 deletions packages/scikit-learn/examples/plot_svm_non_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ def linear_model(rseed=42, n_samples=30):
# data with a non-linear separation


def nonlinear_model(rseed=42, n_samples=30):
radius = 40 * np.random.random(n_samples)
def nonlinear_model(rseed=27446968, n_samples=30):
rng = np.random.default_rng(rseed)

radius = 40 * rng.random(n_samples)
far_pts = radius > 20
radius[far_pts] *= 1.2
radius[~far_pts] *= 1.1
Expand Down

0 comments on commit fb8e277

Please sign in to comment.