Skip to content

Commit 9fa6aec

Browse files
committed
Fix bad usage of numpy-legacy-random
1 parent beaf115 commit 9fa6aec

File tree

33 files changed

+49
-50
lines changed

33 files changed

+49
-50
lines changed

examples/brownian/brownian_motion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
# * `brownian_motion()` is a function that generates a random walk
66
# * `brownian_widget()` uses restructured plotting code given in article
77

8-
rs = np.random.RandomState()
8+
rng = np.random.default_rng()
99

1010

1111
# https://plotly.com/python/3d-line-plots/
1212
def brownian_motion(T=1, N=100, mu=0.1, sigma=0.01, S0=20):
1313
dt = float(T) / N
1414
t = np.linspace(0, T, N)
15-
W = rs.standard_normal(size=N)
15+
W = rng.standard_normal(size=N)
1616
W = np.cumsum(W) * np.sqrt(dt) # standard brownian motion
1717
X = (mu - 0.5 * sigma**2) * t + sigma * W
1818
S = S0 * np.exp(X) # geometric brownian motion

examples/cpuinfo/fakepsutil.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ def cpu_count(logical: bool = True):
77
return 8 if logical else 4
88

99

10-
rnd = np.random.RandomState()
11-
last_sample = rnd.uniform(0, 100, size=cpu_count(True))
10+
rng = np.random.default_rng()
11+
last_sample = rng.uniform(0, 100, size=cpu_count(True))
1212

1313

1414
def cpu_percent(percpu: bool = False):
1515
global last_sample
16-
delta = rnd.normal(scale=10, size=len(last_sample))
16+
delta = rng.normal(scale=10, size=len(last_sample))
1717
last_sample = (last_sample + delta).clip(0, 100)
1818
if percpu:
1919
return last_sample.tolist()

examples/express/accordion_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ def txt():
1717

1818
@render.plot
1919
def histogram():
20-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
20+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
2121
plt.hist(x, input.n(), density=True)

examples/express/column_wrap_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
@render.plot
1414
def histogram():
15-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
15+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1616
plt.hist(x, input.n(), density=True)
1717

1818
with ui.card():
1919

2020
@render.plot
2121
def histogram2():
22-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
22+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
2323
plt.hist(x, input.n(), density=True, color="red")

examples/express/nav_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@render.plot
1515
def histogram():
16-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
16+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1717
plt.hist(x, input.n(), density=True)
1818

1919
with ui.navset_card_underline():
@@ -24,5 +24,5 @@ def histogram():
2424

2525
@render.plot
2626
def histogram2():
27-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
27+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
2828
plt.hist(x, input.n2(), density=True)

examples/express/plot_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
@render.plot
1111
def histogram():
12-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
12+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1313
plt.hist(x, input.n(), density=True)

examples/express/shared_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@render.plot
1414
def histogram():
15-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
15+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1616
plt.hist(x, shared.rv(), density=True)
1717

1818

examples/express/sidebar_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
@render.plot
1212
def histogram():
13-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
13+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1414
plt.hist(x, input.n(), density=True)

examples/static_plots/app.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,14 @@
5454

5555

5656
def server(input: Inputs, output: Outputs, session: Session):
57-
rnd = np.random.RandomState()
57+
rng = np.random.default_rng()
5858

5959
@reactive.calc
6060
def fake_data():
6161
n = 5000
6262
mean = [0, 0]
63-
rng = np.random.RandomState(0)
6463
cov = [(input.var(), input.cov()), (input.cov(), 1 / input.var())]
65-
return rng.multivariate_normal(mean, cov, n).T
64+
return np.random.default_rng(seed=0).multivariate_normal(mean, cov, n).T
6665

6766
@render.plot
6867
def seaborn():
@@ -100,7 +99,7 @@ def plotnine():
10099
@render.plot
101100
def pandas():
102101
ts = pd.Series(
103-
rnd.randn(1000),
102+
rng.randn(1000),
104103
index=pd.date_range("1/1/2000", periods=1000),
105104
)
106105
ts = ts.cumsum()

shiny/api-examples/download/app-core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def make_example(id: str, label: str, title: str, desc: str, extra: Any = None):
7777

7878

7979
def server(input: Inputs, output: Outputs, session: Session):
80-
rnd = np.random.RandomState()
80+
rng = np.random.default_rng()
8181

8282
@render.download()
8383
def download1():
@@ -100,8 +100,8 @@ def download2():
100100
"""
101101

102102
print(input.num_points())
103-
x = rnd.uniform(size=input.num_points())
104-
y = rnd.uniform(size=input.num_points())
103+
x = rng.uniform(size=input.num_points())
104+
y = rng.uniform(size=input.num_points())
105105
plt.figure()
106106
plt.scatter(x, y)
107107
plt.title(input.title())
@@ -110,7 +110,7 @@ def download2():
110110
yield buf.getvalue()
111111

112112
@render.download(
113-
filename=lambda: f"新型-{date.today().isoformat()}-{rnd.randint(100, 999)}.csv"
113+
filename=lambda: f"新型-{date.today().isoformat()}-{rng.randint(100, 999)}.csv"
114114
)
115115
async def download3():
116116
await asyncio.sleep(0.25)

shiny/api-examples/download/app-express.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from shiny.express import render, ui
1010

11-
rnd = np.random.RandomState()
11+
rng = np.random.default_rng()
1212

1313
ui.page_opts(title="Various download examples")
1414

@@ -43,8 +43,8 @@ def download2():
4343
"""
4444

4545
print(input.num_points())
46-
x = rnd.uniform(size=input.num_points())
47-
y = rnd.uniform(size=input.num_points())
46+
x = rng.uniform(size=input.num_points())
47+
y = rng.uniform(size=input.num_points())
4848
plt.figure()
4949
plt.scatter(x, y)
5050
plt.title(input.title())
@@ -59,7 +59,7 @@ def download2():
5959

6060
@render.download(
6161
label="Download filename",
62-
filename=lambda: f"新型-{date.today().isoformat()}-{rnd.randint(100, 999)}.csv",
62+
filename=lambda: f"新型-{date.today().isoformat()}-{rng.randint(100, 999)}.csv",
6363
)
6464
async def download3():
6565
await asyncio.sleep(0.25)

shiny/api-examples/input_action_button/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def server(input: Inputs, output: Outputs, session: Session):
1616
# (not when the slider is changed)
1717
@reactive.event(input.go, ignore_none=False)
1818
def plot():
19-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(input.n())
19+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(input.n())
2020
fig, ax = plt.subplots()
2121
ax.hist(x, bins=30, density=True)
2222
return fig

shiny/api-examples/input_action_button/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# (not when the slider is changed)
1414
@reactive.event(input.go, ignore_none=False)
1515
def plot():
16-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(input.n())
16+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(input.n())
1717
fig, ax = plt.subplots()
1818
ax.hist(x, bins=30, density=True)
1919
return fig

shiny/api-examples/input_action_link/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def server(input: Inputs, output: Outputs, session: Session):
1616
# the slider is changed
1717
@reactive.event(input.go, ignore_none=False)
1818
def plot():
19-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(input.n())
19+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(input.n())
2020
fig, ax = plt.subplots()
2121
ax.hist(x, bins=30, density=True)
2222
return fig

shiny/api-examples/input_action_link/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# the slider is changed
1414
@reactive.event(input.go, ignore_none=False)
1515
def plot():
16-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(input.n())
16+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(input.n())
1717
fig, ax = plt.subplots()
1818
ax.hist(x, bins=30, density=True)
1919
return fig

shiny/api-examples/input_dark_mode/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _():
4545

4646
@render.plot(alt="A histogram")
4747
def plot() -> object:
48-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
48+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
4949

5050
fig, ax = plt.subplots()
5151
ax.hist(x, input.n(), density=True)

shiny/api-examples/input_dark_mode/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@render.plot(alt="A histogram")
1515
def plot() -> object:
16-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
16+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1717

1818
fig, ax = plt.subplots()
1919
ax.hist(x, input.n(), density=True)

shiny/api-examples/input_slider/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
def server(input: Inputs, output: Outputs, session: Session):
1313
@render.plot
1414
def distPlot():
15-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
15+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1616

1717
fig, ax = plt.subplots()
1818
ax.hist(x, input.obs(), density=True)

shiny/api-examples/input_slider/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@render.plot
1010
def distPlot():
11-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
11+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1212

1313
fig, ax = plt.subplots()
1414
ax.hist(x, input.obs(), density=True)

shiny/api-examples/isolate/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def plot():
1818

1919
# ...but don't take a reactive dependency on the slider
2020
with reactive.isolate():
21-
x = 100 + 15 * np.random.RandomState(19680801).randn(input.n())
21+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(input.n())
2222

2323
fig, ax = plt.subplots()
2424
ax.hist(x, bins=30, density=True)

shiny/api-examples/isolate/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def plot():
1515

1616
# ...but don't take a reactive dependency on the slider
1717
with reactive.isolate():
18-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(input.n())
18+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(input.n())
1919

2020
fig, ax = plt.subplots()
2121
ax.hist(x, bins=30, density=True)

shiny/api-examples/layout_columns/model_plots.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
from shiny import ui
55

6-
rnd = np.random.RandomState()
6+
rng = np.random.default_rng()
77

88

99
def plot_loss_over_time():
1010
epochs = np.arange(1, 101)
11-
loss = 1000 / np.sqrt(epochs) + rnd.rand(100) * 25
11+
loss = 1000 / np.sqrt(epochs) + rng.uniform(size=100) * 25
1212

1313
fig = plt.figure(figsize=(10, 6))
1414
plt.plot(epochs, loss)
@@ -19,7 +19,7 @@ def plot_loss_over_time():
1919

2020
def plot_accuracy_over_time():
2121
epochs = np.arange(1, 101)
22-
accuracy = np.sqrt(epochs) / 12 + rnd.rand(100) * 0.15
22+
accuracy = np.sqrt(epochs) / 12 + rng.uniform(size=100) * 0.15
2323
accuracy = [np.min([np.max(accuracy[:i]), 1]) for i in range(1, 101)]
2424

2525
fig = plt.figure(figsize=(10, 6))
@@ -31,7 +31,7 @@ def plot_accuracy_over_time():
3131

3232
def plot_feature_importance():
3333
features = ["Product Category", "Price", "Brand", "Rating", "Number of Reviews"]
34-
importance = rnd.rand(5)
34+
importance = rng.uniform(size=5)
3535

3636
fig = plt.figure(figsize=(10, 6))
3737
plt.barh(features, importance)

shiny/api-examples/layout_sidebar/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def server(input: Inputs, output: Outputs, session: Session):
1717
@render.plot(alt="A histogram")
1818
def plot() -> object:
19-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
19+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
2020

2121
fig, ax = plt.subplots()
2222
ax.hist(x, input.n(), density=True)

shiny/api-examples/layout_sidebar/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@render.plot(alt="A histogram")
1111
def plot() -> object:
12-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
12+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1313

1414
fig, ax = plt.subplots()
1515
ax.hist(x, input.n(), density=True)

shiny/api-examples/output_plot/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
def server(input: Inputs, output: Outputs, session: Session):
1515
@render.plot
1616
def p():
17-
x_rand = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
17+
x_rand = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1818
fig, ax = plt.subplots()
1919
ax.hist(x_rand, int(input.n()), density=True)
2020
return fig

shiny/api-examples/output_plot/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@render.plot
1010
def p():
11-
x_rand = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
11+
x_rand = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1212
fig, ax = plt.subplots()
1313
ax.hist(x_rand, int(input.n()), density=True)
1414
return fig

shiny/api-examples/page_fixed/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
def server(input: Inputs, output: Outputs, session: Session):
1919
@render.plot(alt="A histogram")
2020
def plot() -> object:
21-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
21+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
2222

2323
fig, ax = plt.subplots()
2424
ax.hist(x, input.n(), density=True)

shiny/api-examples/page_fixed/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@render.plot(alt="A histogram")
1414
def plot() -> object:
15-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
15+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1616

1717
fig, ax = plt.subplots()
1818
ax.hist(x, input.n(), density=True)

shiny/api-examples/page_fluid/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def server(input: Inputs, output: Outputs, session: Session):
1717
@render.plot(alt="A histogram")
1818
def plot() -> object:
19-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
19+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
2020

2121
fig, ax = plt.subplots()
2222
ax.hist(x, input.n(), density=True)

shiny/api-examples/page_fluid/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@render.plot(alt="A histogram")
1414
def plot() -> object:
15-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
15+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1616

1717
fig, ax = plt.subplots()
1818
ax.hist(x, input.n(), density=True)

shiny/api-examples/page_sidebar/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def server(input: Inputs, output: Outputs, session: Session):
1717
@render.plot(alt="A histogram")
1818
def plot() -> object:
19-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
19+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
2020

2121
fig, ax = plt.subplots()
2222
ax.hist(x, input.n(), density=True)

shiny/api-examples/page_sidebar/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@render.plot(alt="A histogram")
1111
def plot() -> object:
12-
x = 100 + 15 * np.random.RandomState(seed=19680801).randn(437)
12+
x = 100 + 15 * np.random.default_rng(seed=19680801).randn(437)
1313

1414
fig, ax = plt.subplots()
1515
ax.hist(x, input.n(), density=True)

0 commit comments

Comments
 (0)