Skip to content

Commit 85d1cbb

Browse files
committed
Fix bad usage of numpy-legacy-random
1 parent beaf115 commit 85d1cbb

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)

0 commit comments

Comments
 (0)