From 4be4bce55ed0a7fe296d07a9a1303a28f60f1ded Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Laurent?= <francois.laurent@posteo.net>
Date: Thu, 5 Oct 2023 11:33:10 +0200
Subject: [PATCH] backward compatible rng seeding

---
 Project.toml       |  2 +-
 src/Dataloaders.jl | 15 +++++++++------
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/Project.toml b/Project.toml
index ca5795b..4e0bb0e 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,7 +1,7 @@
 name = "PlanarLarvae"
 uuid = "c2615984-ef14-4d40-b148-916c85b43307"
 authors = ["François Laurent", "Institut Pasteur"]
-version = "0.14"
+version = "0.14a"
 
 [deps]
 DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
diff --git a/src/Dataloaders.jl b/src/Dataloaders.jl
index 8edcde7..f417668 100644
--- a/src/Dataloaders.jl
+++ b/src/Dataloaders.jl
@@ -322,17 +322,19 @@ end
 
 abstract type RatioBasedSampling end
 
-struct ClassRatios <: RatioBasedSampling
+mutable struct ClassRatios <: RatioBasedSampling
     selectors
     majority_minority_ratio
     rng
+    seed
 end
 
-struct IntraClassRatios <: RatioBasedSampling
+mutable struct IntraClassRatios <: RatioBasedSampling
     selectors
     majority_minority_ratio
     intraclass
     rng
+    seed
 end
 
 function withselectors(sampler::T, selectors) where {T}
@@ -364,9 +366,9 @@ function ratiobasedsampling(selectors, majority_minority_ratio; seed=nothing, rn
         if isnothing(rng)
             rng = Random.default_rng()
         end
-        Random.seed!(rng, seed)
+        #Random.seed!(rng, seed)
     end
-    LazyIndex(ClassRatios(asselectors(selectors), majority_minority_ratio, rng))
+    LazyIndex(ClassRatios(asselectors(selectors), majority_minority_ratio, rng, seed))
 end
 
 function ratiobasedsampling(selectors, majority_minority_ratio, intraclass;
@@ -376,7 +378,7 @@ function ratiobasedsampling(selectors, majority_minority_ratio, intraclass;
         if isnothing(rng)
             rng = Random.default_rng()
         end
-        Random.seed!(rng, seed)
+        #Random.seed!(rng, seed)
     end
     intraclass = if intraclass isa Pair
         Dict(asselector(intraclass.first) => intraclass.second)
@@ -384,7 +386,7 @@ function ratiobasedsampling(selectors, majority_minority_ratio, intraclass;
         Dict(asselector(selector) => f for (selector, f) in intraclass)
     end
     LazyIndex(IntraClassRatios(asselectors(selectors), majority_minority_ratio, intraclass,
-                               rng))
+                               rng, seed))
 end
 
 function init!(_)
@@ -677,6 +679,7 @@ function sample(f, loader::DataLoader, features=:spine; kwargs...)
 end
 
 function sample(file::Formats.PreloadedFile, window, ix::LazyIndex, features; kwargs...)
+    ix.sampler.rng = Random.seed!(Random.default_rng(), ix.sampler.seed)
     sample(ix.sampler, file, window, ix.targetcounts[file], features; kwargs...)
 end
 
-- 
GitLab