diff --git a/src/eva/vision/data/wsi/patching/samplers/_utils.py b/src/eva/vision/data/wsi/patching/samplers/_utils.py index af8418df..f1fa3b7e 100644 --- a/src/eva/vision/data/wsi/patching/samplers/_utils.py +++ b/src/eva/vision/data/wsi/patching/samplers/_utils.py @@ -1,14 +1,8 @@ -import random from typing import Tuple import numpy as np -def set_seed(seed: int) -> None: - random.seed(seed) - np.random.seed(seed) - - def get_grid_coords_and_indices( layer_shape: Tuple[int, int], width: int, @@ -33,8 +27,8 @@ def get_grid_coords_and_indices( indices = list(range(len(x_y))) if shuffle: - set_seed(seed) - np.random.shuffle(indices) + random_generator = np.random.default_rng(seed) + random_generator.shuffle(indices) return x_y, indices diff --git a/src/eva/vision/data/wsi/patching/samplers/random.py b/src/eva/vision/data/wsi/patching/samplers/random.py index 09ae5729..b37a3a3e 100644 --- a/src/eva/vision/data/wsi/patching/samplers/random.py +++ b/src/eva/vision/data/wsi/patching/samplers/random.py @@ -18,6 +18,7 @@ def __init__(self, n_samples: int = 1, seed: int = 42): """Initializes the sampler.""" self.seed = seed self.n_samples = n_samples + self.random_generator = random.Random(seed) # nosec def sample( self, @@ -33,9 +34,10 @@ def sample( layer_shape: The shape of the layer. """ _utils.validate_dimensions(width, height, layer_shape) - _utils.set_seed(self.seed) x_max, y_max = layer_shape[0], layer_shape[1] for _ in range(self.n_samples): - x, y = random.randint(0, x_max - width), random.randint(0, y_max - height) # nosec + x, y = self.random_generator.randint(0, x_max - width), self.random_generator.randint( + 0, y_max - height + ) yield x, y