11
22"""
3- ReservoirSample {T}([rng], method = AlgRSWRSKIP())
4- ReservoirSample {T}([rng], n::Int, method = AlgL(); ordered = false)
3+ ReservoirSampler {T}([rng], method = AlgRSWRSKIP())
4+ ReservoirSampler {T}([rng], n::Int, method = AlgL(); ordered = false)
55
66Initializes a reservoir sample which can then be fitted with [`fit!`](@ref).
77The first signature represents a sample where only a single element is collected.
@@ -10,118 +10,118 @@ they were collected with [`ordvalue`](@ref).
1010
1111Look at the [`Sampling Algorithms`](@ref) section for the supported methods.
1212"""
13- struct ReservoirSample {T} 1 === 1 end
13+ struct ReservoirSampler {T} 1 === 1 end
1414
15- function ReservoirSample {T} (method:: ReservoirAlgorithm = AlgRSWRSKIP ()) where T
16- return ReservoirSample {T} (Random. default_rng (), method, MutSample ())
15+ function ReservoirSampler {T} (method:: ReservoirAlgorithm = AlgRSWRSKIP ()) where T
16+ return ReservoirSampler {T} (Random. default_rng (), method, MutSample ())
1717end
18- function ReservoirSample {T} (rng:: AbstractRNG , method:: ReservoirAlgorithm = AlgRSWRSKIP ()) where T
19- return ReservoirSample {T} (rng, method, MutSample ())
18+ function ReservoirSampler {T} (rng:: AbstractRNG , method:: ReservoirAlgorithm = AlgRSWRSKIP ()) where T
19+ return ReservoirSampler {T} (rng, method, MutSample ())
2020end
21- Base. @constprop :aggressive function ReservoirSample {T} (n:: Integer , method:: ReservoirAlgorithm = AlgL ();
21+ Base. @constprop :aggressive function ReservoirSampler {T} (n:: Integer , method:: ReservoirAlgorithm = AlgL ();
2222 ordered = false ) where T
23- return ReservoirSample {T} (Random. default_rng (), n, method, MutSample (), ordered ? Ord () : Unord ())
23+ return ReservoirSampler {T} (Random. default_rng (), n, method, MutSample (), ordered ? Ord () : Unord ())
2424end
25- Base. @constprop :aggressive function ReservoirSample {T} (rng:: AbstractRNG , n:: Integer ,
25+ Base. @constprop :aggressive function ReservoirSampler {T} (rng:: AbstractRNG , n:: Integer ,
2626 method:: ReservoirAlgorithm = AlgL (); ordered = false ) where T
27- return ReservoirSample {T} (rng, n, method, MutSample (), ordered ? Ord () : Unord ())
27+ return ReservoirSampler {T} (rng, n, method, MutSample (), ordered ? Ord () : Unord ())
2828end
2929
3030"""
31- fit!(rs::AbstractReservoirSample , el)
32- fit!(rs::AbstractReservoirSample , el, w)
31+ fit!(rs::AbstractReservoirSampler , el)
32+ fit!(rs::AbstractReservoirSampler , el, w)
3333
3434Updates the reservoir sample by taking into account the element passed.
3535If the sampling is weighted also the weight of the elements needs to be
3636passed.
3737"""
38- @inline OnlineStatsBase. fit! (s:: AbstractReservoirSample , el) = OnlineStatsBase. _fit! (s, el)
39- @inline OnlineStatsBase. fit! (s:: AbstractReservoirSample , el, w) = OnlineStatsBase. _fit! (s, el, w)
38+ @inline OnlineStatsBase. fit! (s:: AbstractReservoirSampler , el) = OnlineStatsBase. _fit! (s, el)
39+ @inline OnlineStatsBase. fit! (s:: AbstractReservoirSampler , el, w) = OnlineStatsBase. _fit! (s, el, w)
4040
4141"""
42- value(rs::AbstractReservoirSample )
42+ value(rs::AbstractReservoirSampler )
4343
4444Returns the elements collected in the sample at the current
4545sampling stage.
4646
4747Note that even if the sampling respects the schema it is assigned
48- when [`ReservoirSample `](@ref) is instantiated, some ordering in
48+ when [`ReservoirSampler `](@ref) is instantiated, some ordering in
4949the sample can be more probable than others. To represent each one
5050with the same probability call `shuffle!` over the result.
5151"""
52- OnlineStatsBase. value (s:: AbstractReservoirSample ) = error (" Abstract version" )
52+ OnlineStatsBase. value (s:: AbstractReservoirSampler ) = error (" Abstract version" )
5353
5454"""
55- ordvalue(rs::AbstractReservoirSample )
55+ ordvalue(rs::AbstractReservoirSampler )
5656
5757Returns the elements collected in the sample at the current
5858sampling stage in the order they were collected. This applies
59- only when `ordered = true` is passed in [`ReservoirSample `](@ref).
59+ only when `ordered = true` is passed in [`ReservoirSampler `](@ref).
6060"""
61- ordvalue (s:: AbstractReservoirSample ) = error (" Not an ordered sample" )
61+ ordvalue (s:: AbstractReservoirSampler ) = error (" Not an ordered sample" )
6262
6363"""
64- nobs(rs::AbstractReservoirSample )
64+ nobs(rs::AbstractReservoirSampler )
6565
6666Returns the total number of elements that have been observed so far
6767during the sampling process.
6868"""
69- OnlineStatsBase. nobs (s:: AbstractReservoirSample ) = s. seen_k
69+ OnlineStatsBase. nobs (s:: AbstractReservoirSampler ) = s. seen_k
7070
7171"""
72- Base.empty!(rs::AbstractReservoirSample )
72+ Base.empty!(rs::AbstractReservoirSampler )
7373
7474Resets the reservoir sample to its initial state.
7575Useful to avoid allocating a new sample in some cases.
7676"""
77- function Base. empty! (:: AbstractReservoirSample )
77+ function Base. empty! (:: AbstractReservoirSampler )
7878 error (" Abstract Version" )
7979end
8080
8181"""
82- Base.merge!(rs::AbstractReservoirSample , rs::AbstractReservoirSample ...)
82+ Base.merge!(rs::AbstractReservoirSampler , rs::AbstractReservoirSampler ...)
8383
8484Updates the first reservoir sample by merging its value with the values
8585of the other samples. Currently only supported for samples with replacement.
8686"""
87- function Base. merge! (:: AbstractReservoirSample )
87+ function Base. merge! (:: AbstractReservoirSampler )
8888 error (" Abstract Version" )
8989end
9090
9191"""
92- Base.merge(rs::AbstractReservoirSample ...)
92+ Base.merge(rs::AbstractReservoirSampler ...)
9393
9494Creates a new reservoir sample by merging the values
9595of the samples passed. Currently only supported for sample
9696with replacement.
9797"""
98- function OnlineStatsBase. merge (:: AbstractReservoirSample )
98+ function OnlineStatsBase. merge (:: AbstractReservoirSampler )
9999 error (" Abstract Version" )
100100end
101101
102102"""
103- StreamSample {T}([rng], iter, n, [N], method = AlgD())
103+ StreamSampler {T}([rng], iter, n, [N], method = AlgD())
104104
105105Initializes a stream sample, which can then be iterated over
106106to return the sampling elements of the iterable `iter` which
107107is assumed to have a eltype of `T`. The methods implemented in
108- [`StreamSample `](@ref) require the knowledge of the total number
108+ [`StreamSampler `](@ref) require the knowledge of the total number
109109of elements in the stream `N`, if not provided it is assumed to be
110110available by calling `length(iter)`.
111111"""
112- struct StreamSample {T} 1 === 1 end
112+ struct StreamSampler {T} 1 === 1 end
113113
114- function StreamSample {T} (iter, n, N, method:: StreamAlgorithm = AlgD ()) where T
115- return StreamSample {T} (Random. default_rng (), iter, n, N, method)
114+ function StreamSampler {T} (iter, n, N, method:: StreamAlgorithm = AlgD ()) where T
115+ return StreamSampler {T} (Random. default_rng (), iter, n, N, method)
116116end
117- function StreamSample {T} (iter, n, method:: StreamAlgorithm = AlgD ()) where T
118- return StreamSample {T} (Random. default_rng (), iter, n, length (iter), method)
117+ function StreamSampler {T} (iter, n, method:: StreamAlgorithm = AlgD ()) where T
118+ return StreamSampler {T} (Random. default_rng (), iter, n, length (iter), method)
119119end
120- function StreamSample {T} (rng:: AbstractRNG , iter, n, method:: StreamAlgorithm = AlgD ()) where T
121- return StreamSample {T} (rng, iter, n, length (iter), method)
120+ function StreamSampler {T} (rng:: AbstractRNG , iter, n, method:: StreamAlgorithm = AlgD ()) where T
121+ return StreamSampler {T} (rng, iter, n, length (iter), method)
122122end
123- function StreamSample {T} (rng:: AbstractRNG , iter, n, N, method:: StreamAlgorithm = AlgD ()) where T
124- return StreamSample {T} (rng, iter, n, N, method)
123+ function StreamSampler {T} (rng:: AbstractRNG , iter, n, N, method:: StreamAlgorithm = AlgD ()) where T
124+ return StreamSampler {T} (rng, iter, n, N, method)
125125end
126126
127127"""
171171Base. @constprop :aggressive function itsample (rng:: AbstractRNG , iter, method = AlgRSWRSKIP ();
172172 iter_type = infer_eltype (iter))
173173 if Base. IteratorSize (iter) isa Base. SizeUnknown
174- s = ReservoirSample {iter_type} (rng, method, ImmutSample ())
174+ s = ReservoirSampler {iter_type} (rng, method, ImmutSample ())
175175 return update_all! (s, iter)
176176 else
177177 return sorted_sample_single (rng, iter)
@@ -180,29 +180,29 @@ end
180180Base. @constprop :aggressive function itsample (rng:: AbstractRNG , iter, n:: Int , method = AlgL ();
181181 iter_type = infer_eltype (iter), ordered = false )
182182 if Base. IteratorSize (iter) isa Base. SizeUnknown
183- s = ReservoirSample {iter_type} (rng, n, method, ImmutSample (), ordered ? Ord () : Unord ())
183+ s = ReservoirSampler {iter_type} (rng, n, method, ImmutSample (), ordered ? Ord () : Unord ())
184184 return update_all! (s, iter, ordered)
185185 else
186186 m = method isa AlgL || method isa AlgR || method isa AlgD ? AlgD () : AlgORDSWR ()
187- s = collect (StreamSample {iter_type} (rng, iter, n, length (iter), m))
187+ s = collect (StreamSampler {iter_type} (rng, iter, n, length (iter), m))
188188 return ordered ? s : shuffle! (rng, s)
189189 end
190190end
191191function itsample (rng:: AbstractRNG , iter, wv:: Function , method = AlgWRSWRSKIP (); iter_type = infer_eltype (iter))
192- s = ReservoirSample {iter_type} (rng, method, ImmutSample ())
192+ s = ReservoirSampler {iter_type} (rng, method, ImmutSample ())
193193 return update_all! (s, iter, wv)
194194end
195195Base. @constprop :aggressive function itsample (rng:: AbstractRNG , iter, wv:: Function , n:: Int , method = AlgAExpJ ();
196196 iter_type = infer_eltype (iter), ordered = false )
197- s = ReservoirSample {iter_type} (rng, n, method, ImmutSample (), ordered ? Ord () : Unord ())
197+ s = ReservoirSampler {iter_type} (rng, n, method, ImmutSample (), ordered ? Ord () : Unord ())
198198 return update_all! (s, iter, ordered, wv)
199199end
200200function itsample (rngs:: Tuple , iters:: Tuple , n:: Int ,; iter_types = infer_eltype .(iters))
201201 n_it = length (iters)
202202 vs = Vector {Vector{Union{iter_types...}}} (undef, n_it)
203203 ps = Vector {Float64} (undef, n_it)
204204 Threads. @threads for i in 1 : n_it
205- s = ReservoirSample {iter_types[i]} (rngs[i], n, AlgRSWRSKIP (), ImmutSample (), Unord ())
205+ s = ReservoirSampler {iter_types[i]} (rngs[i], n, AlgRSWRSKIP (), ImmutSample (), Unord ())
206206 vs[i], ps[i] = update_all_p! (s, iters[i])
207207 end
208208 ps /= sum (ps)
@@ -213,7 +213,7 @@ function itsample(rngs::Tuple, iters::Tuple, wfuncs::Tuple, n::Int; iter_types =
213213 vs = Vector {Vector{Union{iter_types...}}} (undef, n_it)
214214 ps = Vector {Float64} (undef, n_it)
215215 Threads. @threads for i in 1 : n_it
216- s = ReservoirSample {iter_types[i]} (rngs[i], n, AlgWRSWRSKIP (), ImmutSample (), Unord ())
216+ s = ReservoirSampler {iter_types[i]} (rngs[i], n, AlgWRSWRSKIP (), ImmutSample (), Unord ())
217217 vs[i], ps[i] = update_all_p! (s, iters[i], wfuncs[i])
218218 end
219219 ps /= sum (ps)
0 commit comments