Skip to content

Commit 0ec0c9c

Browse files
committed
Use seeded random generator in test_writer.py
Replaces np.random.rand with a seeded numpy random generator (rng) in test_writer.py to ensure reproducible test results. Also adds a clarifying comment in conftest.py regarding config_path usage.
1 parent 7ec1e0c commit 0ec0c9c

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

src/napari_deeplabcut/_tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ def mapped_points(points, superkeypoints_assets, config_path):
211211
superkpts = superkeypoints_assets["data"]
212212

213213
# Required by _map_keypoints to locate and write config.yaml
214+
# NOTE: This relies on config_path pointing to a file directly under the
215+
# project directory, so that Path(config_path).parent is the project root.
214216
layer.metadata["project"] = str(Path(config_path).parent)
215217
header = layer.metadata["header"]
216218
bp1, bp2 = header.bodyparts[:2]

src/napari_deeplabcut/_tests/test_writer.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from napari_deeplabcut import _writer, misc
99

10+
rng = np.random.default_rng(42)
11+
1012

1113
# Basic tests
1214
def test_write_config(tmp_path):
@@ -22,7 +24,7 @@ def test_write_config(tmp_path):
2224

2325

2426
def test_write_image(tmp_path):
25-
img = (np.random.rand(10, 10) * 255).astype(np.uint8)
27+
img = (rng.random((10, 10)) * 255).astype(np.uint8)
2628
output = tmp_path / "test.png"
2729

2830
_writer._write_image(img, str(output))
@@ -81,7 +83,7 @@ def test_form_df_multi_animal(fake_keypoints):
8183
metadata = _fake_metadata_for_df(fake_keypoints, [f"img{i}.png" for i in range(n)])
8284

8385
# inds + (x,y)
84-
data = np.column_stack([np.arange(n), np.random.rand(n), np.random.rand(n)])
86+
data = np.column_stack([np.arange(n), rng.random(n), rng.random(n)])
8587

8688
df = _writer._form_df(data, metadata)
8789

@@ -116,7 +118,7 @@ def test_form_df_single_animal(fake_keypoints):
116118
metadata = _fake_metadata_for_df(df_single, [f"img{i}.png" for i in range(n)])
117119

118120
# inds + (x,y)
119-
points = np.column_stack([np.arange(n), np.random.rand(n), np.random.rand(n)])
121+
points = np.column_stack([np.arange(n), rng.random(n), rng.random(n)])
120122
out = _writer._form_df(points, metadata)
121123

122124
assert isinstance(out, pd.DataFrame)
@@ -166,8 +168,8 @@ def test_write_hdf_basic(tmp_path, fake_keypoints):
166168
points = np.column_stack(
167169
[
168170
np.arange(n_rows),
169-
np.random.rand(n_rows),
170-
np.random.rand(n_rows),
171+
rng.random(n_rows),
172+
rng.random(n_rows),
171173
]
172174
)
173175

@@ -230,8 +232,8 @@ def test_write_hdf_machine_prediction_merge(tmp_path, fake_keypoints):
230232
points = np.column_stack(
231233
[
232234
np.arange(n_rows),
233-
np.random.rand(n_rows),
234-
np.random.rand(n_rows),
235+
rng.random(n_rows),
236+
rng.random(n_rows),
235237
]
236238
)
237239

@@ -291,8 +293,8 @@ def test_write_hdf_machine_pred_no_gt(tmp_path, fake_keypoints):
291293
points = np.column_stack(
292294
[
293295
np.arange(n_rows),
294-
np.random.rand(n_rows),
295-
np.random.rand(n_rows),
296+
rng.random(n_rows),
297+
rng.random(n_rows),
296298
]
297299
)
298300

0 commit comments

Comments
 (0)