-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_sample.py
300 lines (228 loc) · 7.52 KB
/
test_sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""Unit tests for cryptorandom sampling functions."""
from __future__ import (absolute_import, division,
print_function, unicode_literals)
import numpy as np
from ..sample import *
from nose.tools import assert_raises, raises
class fake_generator():
"""
This generator just cycles through the numbers 0,...,9.
"""
def __init__(self):
self.counter = 0
def next(self):
"""
Get the next number
"""
self.counter += 1
if self.counter > 9:
self.counter = self.counter % 10
def random(self, size=None):
"""
Generate floats. They go from 0, 0.1, ..., 0.9 and then wrap back.
size controls the number generated. If size=None, just one is produced.
"""
if size == None:
self.next()
return self.counter/10
else:
rand = []
for i in range(size):
self.next()
rand.append(self.counter/10)
return rand
def randint(self, a, b, size=None):
"""
Generate random integers between a (inclusive) and b (exclusive).
size controls the number of ints generated.
If size=None, just one is produced.
"""
assert a <= b, "lower and upper limits are switched"
if size == None:
return int(a + (self.random()*10) % (b-a))
else:
return np.reshape(np.array([int(a + (self.random()*10) % (b-a)) \
for i in np.arange(np.prod(size))]), size)
def test_fake_generator():
"""
Make sure the fake generator works as expected
"""
ff = fake_generator()
out = ff.randint(0, 10, 10)
expected = np.concatenate([np.arange(1, 10), np.zeros(1)])
assert (out == expected).all()
assert ff.random() == (expected[-1]+1)/10
ff = fake_generator()
out = ff.randint(1, 11, 10)
assert (out == expected+1).all()
ff = fake_generator()
out = ff.randint(0, 20, 10)
assert (out == expected).all()
ff = fake_generator()
out = ff.random(2)
assert out == [0.1, 0.2]
def test_get_prng():
ff = fake_generator()
gg = get_prng(ff)
assert ff == gg
gg = get_prng()
assert isinstance(gg, SHA256)
gg = get_prng(5)
assert isinstance(gg, SHA256)
np.random.seed(234)
rand1 = np.random.random(size=5)
np.random.seed(234)
gg = get_prng(np.random)
rand2 = gg.random(size=5)
assert (rand1 == rand2).all()
@raises(AssertionError)
def test_random_sample_bad_N():
random_sample(-2, 2)
@raises(ValueError)
def test_random_sample_bad_a():
random_sample(2.5, 2)
@raises(AssertionError)
def test_random_sample_bad_p():
random_sample(5, 2, p=[0.25]*4)
@raises(AssertionError)
def test_random_sample_bad_size():
random_sample(2, 5)
@raises(TypeError)
def test_random_sample_bad_method1():
random_sample(5, 2, method="Exponential")
@raises(ValueError)
def test_random_sample_bad_method2():
random_sample(5, 2, replace=True, method="PIKK")
def test_fykd():
"""
Test Fisher-Yates shuffle for random samples, fykd_sample
"""
ff = fake_generator()
sam = fykd_sample(5, 2, prng=ff)
assert (sam == [1, 2]).all()
ff = fake_generator()
sam = random_sample(5, 2, method="Fisher-Yates", prng=ff)
assert (sam+1 == [1, 2]).all() # shift to 1-index
ff = fake_generator()
fruit = ['apple', 'banana', 'cherry', 'pear', 'plum']
sam = random_sample(fruit, 2, method="Fisher-Yates", prng=ff)
assert (sam == ['apple', 'banana']).all()
def test_pikk():
"""
Test PIKK
"""
ff = fake_generator()
sam = pikk(5, 2, prng=ff)
assert (sam == [1, 2]).all()
ff = fake_generator()
sam = random_sample(5, 2, method="PIKK", prng=ff)
assert (sam+1 == [1, 2]).all() # shift to 1-index
def test_recursive_sample():
"""
Test Cormen et al recursive_sample
"""
ff = fake_generator()
sam = recursive_sample(5, 2, prng=ff)
assert (sam == [2, 3]).all()
ff = fake_generator()
sam = random_sample(5, 2, method="recursive", prng=ff)
assert (sam+1 == [2, 3]).all() # shift to 1-index
@raises(RuntimeError)
def test_cormen_recursion_depth():
recursive_sample(2000, 1500)
def test_waterman_r():
"""
Test Waterman's algorithm R
"""
ff = fake_generator()
sam = waterman_r(5, 2, prng=ff)
assert (sam == [1, 3]).all()
ff = fake_generator()
sam = random_sample(5, 2, method="Waterman_R", prng=ff)
assert (sam+1 == [1, 3]).all() # shift to 1-index
def test_sbi():
"""
Test sample_by_index
"""
ff = fake_generator()
sam = sample_by_index(5, 2, prng=ff)
assert (sam == [2, 3]).all()
ff = fake_generator()
sam = random_sample(5, 2, method="sample_by_index", prng=ff)
assert (sam+1 == [2, 3]).all() # shift to 1-index
def test_vitter_z():
"""
Test Vitter's algorithm Z
"""
ff = fake_generator()
sam = vitter_z(5, 2, prng=ff)
assert (sam == [5, 2]).all()
ff = fake_generator()
sam = random_sample(5, 2, method="Vitter_Z", prng=ff)
assert (sam+1 == [5, 2]).all() # shift to 1-index
ff = fake_generator()
sam = vitter_z(500, 2, prng=ff)
assert (sam == [472, 422]).all()
ff = fake_generator()
sam = random_sample(500, 2, method="Vitter_Z", prng=ff)
assert (sam+1 == [472, 422]).all() # shift to 1-index
def test_elimination_sample():
"""
Test elimination_sample
"""
ff = fake_generator()
sam = elimination_sample(2, [0.2]*5, prng=ff)
assert (sam == [1, 1]).all()
ff = fake_generator()
sam = elimination_sample(2, [0.2]*5, replace=False, prng=ff)
assert (sam == [1, 2]).all()
ff = fake_generator()
sam = random_sample(5, 2, p=[0.2]*5, replace=True, method="Elimination", prng=ff)
assert (sam+1 == [1, 1]).all() # shift to 1-index
ff = fake_generator()
sam = random_sample(5, 2, p=[0.2]*5, replace=False, method="Elimination", prng=ff)
assert (sam+1 == [1, 2]).all() # shift to 1-index
def test_exponential_sample():
"""
Test elimination_sample
"""
ff = fake_generator()
sam = exponential_sample(2, [0.2]*5, prng=ff)
assert (sam == [5, 4]).all()
ff = fake_generator()
sam = random_sample(5, 2, p=[0.2]*5, replace=False, method="Exponential", prng=ff)
assert (sam+1 == [5, 4]).all() # shift to 1-index
def test_fykd_shuffle():
"""
Test Fisher-Yates shuffle for random permutations, fykd_shuffle
"""
ff = fake_generator()
sam = fykd_sample(5, 5, prng=ff)
assert (sam == [1, 2, 3, 4, 5]).all()
ff = fake_generator()
sam = random_permutation(5, method="Fisher-Yates", prng=ff)
assert (sam+1 == [1, 2, 3, 4, 5]).all() # shift to 1-index
ff = fake_generator()
fruit = ['apple', 'banana', 'cherry', 'pear', 'plum']
sam = random_permutation(fruit, method="Fisher-Yates", prng=ff)
assert (sam == fruit).all()
def test_pikk_shuffle():
"""
Test PIKK shuffling
"""
ff = fake_generator()
sam = pikk(5, 5, prng=ff)
assert (sam == [1, 2, 3, 4, 5]).all()
ff = fake_generator()
sam = random_permutation(5, method="random_sort", prng=ff)
assert (sam+1 == [1, 2, 3, 4, 5]).all() # shift to 1-index
def test_permute_by_index():
"""
Test permuting by index shuffling
"""
ff = fake_generator()
sam = sample_by_index(5, 5, prng=ff)
assert (sam == [2, 3, 1, 4, 5]).all()
ff = fake_generator()
sam = random_permutation(5, method="permute_by_index", prng=ff)
assert (sam+1 == [2, 3, 1, 4, 5]).all() # shift to 1-index