1
1
2
2
"""
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)
5
5
6
6
Initializes a reservoir sample which can then be fitted with [`fit!`](@ref).
7
7
The first signature represents a sample where only a single element is collected.
@@ -10,118 +10,118 @@ they were collected with [`ordvalue`](@ref).
10
10
11
11
Look at the [`Sampling Algorithms`](@ref) section for the supported methods.
12
12
"""
13
- struct ReservoirSample {T} 1 === 1 end
13
+ struct ReservoirSampler {T} 1 === 1 end
14
14
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 ())
17
17
end
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 ())
20
20
end
21
- Base. @constprop :aggressive function ReservoirSample {T} (n:: Integer , method:: ReservoirAlgorithm = AlgL ();
21
+ Base. @constprop :aggressive function ReservoirSampler {T} (n:: Integer , method:: ReservoirAlgorithm = AlgL ();
22
22
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 ())
24
24
end
25
- Base. @constprop :aggressive function ReservoirSample {T} (rng:: AbstractRNG , n:: Integer ,
25
+ Base. @constprop :aggressive function ReservoirSampler {T} (rng:: AbstractRNG , n:: Integer ,
26
26
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 ())
28
28
end
29
29
30
30
"""
31
- fit!(rs::AbstractReservoirSample , el)
32
- fit!(rs::AbstractReservoirSample , el, w)
31
+ fit!(rs::AbstractReservoirSampler , el)
32
+ fit!(rs::AbstractReservoirSampler , el, w)
33
33
34
34
Updates the reservoir sample by taking into account the element passed.
35
35
If the sampling is weighted also the weight of the elements needs to be
36
36
passed.
37
37
"""
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)
40
40
41
41
"""
42
- value(rs::AbstractReservoirSample )
42
+ value(rs::AbstractReservoirSampler )
43
43
44
44
Returns the elements collected in the sample at the current
45
45
sampling stage.
46
46
47
47
Note 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
49
49
the sample can be more probable than others. To represent each one
50
50
with the same probability call `shuffle!` over the result.
51
51
"""
52
- OnlineStatsBase. value (s:: AbstractReservoirSample ) = error (" Abstract version" )
52
+ OnlineStatsBase. value (s:: AbstractReservoirSampler ) = error (" Abstract version" )
53
53
54
54
"""
55
- ordvalue(rs::AbstractReservoirSample )
55
+ ordvalue(rs::AbstractReservoirSampler )
56
56
57
57
Returns the elements collected in the sample at the current
58
58
sampling 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).
60
60
"""
61
- ordvalue (s:: AbstractReservoirSample ) = error (" Not an ordered sample" )
61
+ ordvalue (s:: AbstractReservoirSampler ) = error (" Not an ordered sample" )
62
62
63
63
"""
64
- nobs(rs::AbstractReservoirSample )
64
+ nobs(rs::AbstractReservoirSampler )
65
65
66
66
Returns the total number of elements that have been observed so far
67
67
during the sampling process.
68
68
"""
69
- OnlineStatsBase. nobs (s:: AbstractReservoirSample ) = s. seen_k
69
+ OnlineStatsBase. nobs (s:: AbstractReservoirSampler ) = s. seen_k
70
70
71
71
"""
72
- Base.empty!(rs::AbstractReservoirSample )
72
+ Base.empty!(rs::AbstractReservoirSampler )
73
73
74
74
Resets the reservoir sample to its initial state.
75
75
Useful to avoid allocating a new sample in some cases.
76
76
"""
77
- function Base. empty! (:: AbstractReservoirSample )
77
+ function Base. empty! (:: AbstractReservoirSampler )
78
78
error (" Abstract Version" )
79
79
end
80
80
81
81
"""
82
- Base.merge!(rs::AbstractReservoirSample , rs::AbstractReservoirSample ...)
82
+ Base.merge!(rs::AbstractReservoirSampler , rs::AbstractReservoirSampler ...)
83
83
84
84
Updates the first reservoir sample by merging its value with the values
85
85
of the other samples. Currently only supported for samples with replacement.
86
86
"""
87
- function Base. merge! (:: AbstractReservoirSample )
87
+ function Base. merge! (:: AbstractReservoirSampler )
88
88
error (" Abstract Version" )
89
89
end
90
90
91
91
"""
92
- Base.merge(rs::AbstractReservoirSample ...)
92
+ Base.merge(rs::AbstractReservoirSampler ...)
93
93
94
94
Creates a new reservoir sample by merging the values
95
95
of the samples passed. Currently only supported for sample
96
96
with replacement.
97
97
"""
98
- function OnlineStatsBase. merge (:: AbstractReservoirSample )
98
+ function OnlineStatsBase. merge (:: AbstractReservoirSampler )
99
99
error (" Abstract Version" )
100
100
end
101
101
102
102
"""
103
- StreamSample {T}([rng], iter, n, [N], method = AlgD())
103
+ StreamSampler {T}([rng], iter, n, [N], method = AlgD())
104
104
105
105
Initializes a stream sample, which can then be iterated over
106
106
to return the sampling elements of the iterable `iter` which
107
107
is 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
109
109
of elements in the stream `N`, if not provided it is assumed to be
110
110
available by calling `length(iter)`.
111
111
"""
112
- struct StreamSample {T} 1 === 1 end
112
+ struct StreamSampler {T} 1 === 1 end
113
113
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)
116
116
end
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)
119
119
end
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)
122
122
end
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)
125
125
end
126
126
127
127
"""
171
171
Base. @constprop :aggressive function itsample (rng:: AbstractRNG , iter, method = AlgRSWRSKIP ();
172
172
iter_type = infer_eltype (iter))
173
173
if Base. IteratorSize (iter) isa Base. SizeUnknown
174
- s = ReservoirSample {iter_type} (rng, method, ImmutSample ())
174
+ s = ReservoirSampler {iter_type} (rng, method, ImmutSample ())
175
175
return update_all! (s, iter)
176
176
else
177
177
return sorted_sample_single (rng, iter)
@@ -180,29 +180,29 @@ end
180
180
Base. @constprop :aggressive function itsample (rng:: AbstractRNG , iter, n:: Int , method = AlgL ();
181
181
iter_type = infer_eltype (iter), ordered = false )
182
182
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 ())
184
184
return update_all! (s, iter, ordered)
185
185
else
186
186
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))
188
188
return ordered ? s : shuffle! (rng, s)
189
189
end
190
190
end
191
191
function 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 ())
193
193
return update_all! (s, iter, wv)
194
194
end
195
195
Base. @constprop :aggressive function itsample (rng:: AbstractRNG , iter, wv:: Function , n:: Int , method = AlgAExpJ ();
196
196
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 ())
198
198
return update_all! (s, iter, ordered, wv)
199
199
end
200
200
function itsample (rngs:: Tuple , iters:: Tuple , n:: Int ,; iter_types = infer_eltype .(iters))
201
201
n_it = length (iters)
202
202
vs = Vector {Vector{Union{iter_types...}}} (undef, n_it)
203
203
ps = Vector {Float64} (undef, n_it)
204
204
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 ())
206
206
vs[i], ps[i] = update_all_p! (s, iters[i])
207
207
end
208
208
ps /= sum (ps)
@@ -213,7 +213,7 @@ function itsample(rngs::Tuple, iters::Tuple, wfuncs::Tuple, n::Int; iter_types =
213
213
vs = Vector {Vector{Union{iter_types...}}} (undef, n_it)
214
214
ps = Vector {Float64} (undef, n_it)
215
215
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 ())
217
217
vs[i], ps[i] = update_all_p! (s, iters[i], wfuncs[i])
218
218
end
219
219
ps /= sum (ps)
0 commit comments