Skip to content

Commit 728c916

Browse files
Tests: Replace deprecated "np.random.seed()" with "np.random.Generator()" (NPY002 warning by ruff) (#2787)
* Follow NPY002 in test 'test_io.py' * Follow NPY002 in test 'test_x2sys_cross.py' * Fix tests
1 parent 90a1207 commit 728c916

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

pygmt/tests/test_io.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ def test_io_load_dataarray():
1414
GMTDataArrayAccessor information loaded.
1515
"""
1616
with GMTTempFile(suffix=".nc") as tmpfile:
17+
rng = np.random.default_rng()
1718
grid = xr.DataArray(
18-
data=np.random.rand(2, 2), coords=[[0.1, 0.2], [0.3, 0.4]], dims=("x", "y")
19+
data=rng.random((2, 2)), coords=[[0.1, 0.2], [0.3, 0.4]], dims=("x", "y")
1920
)
2021
grid.to_netcdf(tmpfile.name)
2122
dataarray = load_dataarray(tmpfile.name)

pygmt/tests/test_x2sys_cross.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ def test_x2sys_cross_input_two_dataframes(mock_x2sys_home):
110110
# Create pandas.DataFrame track tables
111111
tracks = []
112112
for i in range(2):
113-
np.random.seed(seed=i)
114-
track = pd.DataFrame(data=np.random.rand(10, 3), columns=("x", "y", "z"))
113+
rng = np.random.default_rng(seed=i)
114+
track = pd.DataFrame(data=rng.random((10, 3)), columns=("x", "y", "z"))
115115
track["time"] = pd.date_range(start=f"2020-{i}1-01", periods=10, freq="min")
116116
tracks.append(track)
117117

118118
output = x2sys_cross(tracks=tracks, tag=tag, coe="e")
119119

120120
assert isinstance(output, pd.DataFrame)
121-
assert output.shape == (30, 12)
121+
assert output.shape == (26, 12)
122122
columns = list(output.columns)
123123
assert columns[:6] == ["x", "y", "t_1", "t_2", "dist_1", "dist_2"]
124124
assert columns[6:] == ["head_1", "head_2", "vel_1", "vel_2", "z_X", "z_M"]
@@ -160,16 +160,16 @@ def test_x2sys_cross_input_two_filenames(mock_x2sys_home):
160160

161161
# Create temporary xyz files
162162
for i in range(2):
163-
np.random.seed(seed=i)
163+
rng = np.random.default_rng(seed=i)
164164
with open(
165165
os.path.join(os.getcwd(), f"track_{i}.xyz"), mode="w", encoding="utf8"
166166
) as fname:
167-
np.savetxt(fname=fname, X=np.random.rand(10, 3))
167+
np.savetxt(fname=fname, X=rng.random((10, 3)))
168168

169169
output = x2sys_cross(tracks=["track_0.xyz", "track_1.xyz"], tag=tag, coe="e")
170170

171171
assert isinstance(output, pd.DataFrame)
172-
assert output.shape == (24, 12)
172+
assert output.shape == (18, 12)
173173
columns = list(output.columns)
174174
assert columns[:6] == ["x", "y", "i_1", "i_2", "dist_1", "dist_2"]
175175
assert columns[6:] == ["head_1", "head_2", "vel_1", "vel_2", "z_X", "z_M"]

0 commit comments

Comments
 (0)