Skip to content

Commit

Permalink
use local random generator in samplers
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaenzig committed Oct 7, 2024
1 parent e36f005 commit f4c6510
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
10 changes: 2 additions & 8 deletions src/eva/vision/data/wsi/patching/samplers/_utils.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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


Expand Down
6 changes: 4 additions & 2 deletions src/eva/vision/data/wsi/patching/samplers/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

0 comments on commit f4c6510

Please sign in to comment.