def test_sampling_no_replacement_single(self): elements = ['a'] weights = [33.3] elements_weights = zip(elements, weights) s = SamplableSet(1, 100, elements_weights) element, weight = s.sample(replace=False) assert element == 'a' and weight == 33.3 and len(s) == 0
def test_python_generator_empty(): elements = range(1, 10) weights = range(1, 10) s = SamplableSet(1, 10, zip(elements, weights)) s.clear() expected = [] assert list(expected) == list(s.__iter__())
def test_sampling_no_replacement_generator_error(self): elements = ['a', 'b'] weights = [33.3, 50.] elements_weights = zip(elements, weights) s = SamplableSet(1, 100, elements_weights) sample_list = [] with pytest.raises(KeyError): for sample in s.sample(n_samples=3, replace=False): sample_list.append(sample)
def test_sampling_no_replacement_generator(self): elements = ['a', 'b'] weights = [33.3, 50.] elements_weights = zip(elements, weights) s = SamplableSet(1, 100, elements_weights) sample_list = [] for sample in s.sample(n_samples=2, replace=False): sample_list.append(sample) assert sample_list[0][0] in elements and sample_list[1][0] in elements assert len(s) == 0
def test_sampling_no_replacement_generator(self): elements = ['a'] weights = [33.3] elements_weights = zip(elements, weights) s = SamplableSet(1, 100, elements_weights) sample_list = [] for sample in s.sample(n_samples=5, replace=False): sample_list.append(sample) assert sample_list == [('a', 33.3), None, None, None, None ] and len(s) == 0
def test_sampling_generator(self): elements = ['a'] weights = [33.3] elements_weights = zip(elements, weights) s = SamplableSet(1, 100, elements_weights) element_list = [] weight_list = [] for element, weight in s.sample(n_samples=5): element_list.append(element) weight_list.append(weight) assert element_list == ['a'] * 5 and weight_list == [33.3] * 5 and len( s) == 1
def test_next_error(self): elements = {1: 10, 2: 50} s = SamplableSet(1, 100, elements) s.init_iterator() s.next() with pytest.raises(StopIteration): s.next()
def test_clear(self): elements = ['a'] weights = [33.3] elements_weights = zip(elements, weights) s = SamplableSet(1, 100, elements_weights) s.clear() assert s.total_weight() == 0 and len(s) == 0 and s.empty() with pytest.raises(KeyError): s.sample()
def test_next_get_None(self): elements = {1: 10, 2: 50} s = SamplableSet(1, 100, elements) s.init_iterator() s.next() s.next() assert s.get_at_iterator() is None
def test_next_1(self): #elements in same group elements = {1: 10, 2: 10} s = SamplableSet(1, 100, elements) s.init_iterator() s.next() assert s.get_at_iterator() == (2, 10.)
def test_next_2(self): #elements in different groups elements = {1: 10, 2: 50} s = SamplableSet(1, 100, elements) s.init_iterator() s.next() assert s.get_at_iterator() == (2, 50.)
def test_clear(self): elements = ['a'] weights = [33.3] elements_weights = zip(elements, weights) s = SamplableSet(1, 100, elements_weights) s.clear() assert s.total_weight() == 0 and len(s) == 0 and s.sample() is None
def test_throw_error_2(self): with pytest.raises(ValueError): s = SamplableSet(1, np.inf)
def test_throw_error_1(self): with pytest.raises(ValueError): s = SamplableSet(0, 100)
def test_iterable_init(self): elements = ['a', 'b'] weights = [33.3, 66.6] elements_weights = zip(elements, weights) s = SamplableSet(1, 100, elements_weights) assert 'a' in s and 'b' in s
def test_dict_init(self): elements_weights = {3: 33.3, 6: 66.6} s = SamplableSet(1, 100, elements_weights) assert 3 in s and 6 in s
def test_empty_init_1(self): s = SamplableSet(1, 100) assert s.cpp_type is None s['a'] = 2. assert s.cpp_type == 'str' assert len(s) == 1 and s['a'] == 2.
def test_get_weight(self): s = SamplableSet(1, 10) s['a'] = 2. assert s['a'] == 2.
def test_throw_error_4(self): with pytest.raises(RuntimeError): s = SamplableSet(1, 2) s.size()
import numpy as np from SamplableSet import SamplableSet from time import time N = int(2e4) n = np.arange(N) np.random.shuffle(n) weights = np.random.random(N) + 0.1 _min, _max = np.amin(weights), np.amax(weights) weighted_items = [(int(_n), float(_w)) for _n, _w in zip(n, weights)] print("#========= CREATING ========") start = time() smpset = SamplableSet(0.1, 1.1, weighted_items) end = time() print("Orig:", end - start, "seconds") start = time() mckset = MockSamplableSet(0.1, 1.1, weighted_items) end = time() print("Mock:", end - start, "seconds") print("#========= ADDING ========") n = np.arange(N) + N np.random.shuffle(n) weights = np.random.random(N) + 0.1
def test_throw_error_5(self): with pytest.raises(RuntimeError): s = SamplableSet(1, 2) s.total_weight()
def test_insert(self): s = SamplableSet(1, 10) s['a'] = 2. assert len(s) == 1 and not s.empty()
def test_set_weight(self): s = SamplableSet(1, 10) s['a'] = 2. s['a'] = 3. assert s['a'] == 3. and len(s) == 1 and s.total_weight() == 3.
def test_get_weight_no_item(self): s = SamplableSet(1, 10) s['a'] = 2. with pytest.raises(KeyError): s['b']
def test_empty_init_2(self): s = SamplableSet(1, 100, {}) assert s.cpp_type is None s[1] = 5. assert s.cpp_type == 'int' assert len(s) == 1 and s[1] == 5.
def test_weight_out_of_bound_3(self): with pytest.raises(ValueError): s = SamplableSet(1, 10) s['a'] = 2. s['a'] = 11
def test_erase(self): s = SamplableSet(1, 10) s['a'] = 2. del s['a'] assert len(s) == 0 and s.empty()
choice_time_list = [] for N in Nlist: weights = np.arange(1, N + 1, dtype=np.float64) weights /= np.sum(weights) elements = list(range(1, N + 1)) #test random choice --- each time it is as if the distribution was new t1 = time() for i in range(5 * 10**4): np.random.choice(elements, p=weights) t2 = time() choice_time_list.append(t2 - t1) #test SamplableSet t1 = time() s = SamplableSet(min(weights), max(weights), zip(elements, weights)) for i in range(5 * 10**4): e, w = s.sample() s[e] = w * 1. t2 = time() ss_time_list.append(t2 - t1) #Plot results font_size = 8 plt.style.use('seaborn-paper') plt.rc('text', usetex=True) plt.rc('font', family='serif', serif='Computer Modern') plt.rc('xtick', labelsize=font_size) plt.rc('ytick', labelsize=font_size) plt.rc('axes', labelsize=font_size) plt.rc('legend', fontsize=font_size - 1)
def __init__(self, seed=None): """Empty constructor.""" if not seed is None: sset.seed(seed)