Skip to content

Reservoir #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sampling/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .core import jackknife
from .core import jackknife, shuffle, Reservoir

__version__ = '0.0.1'
45 changes: 45 additions & 0 deletions sampling/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import random as core_random


no_replace = '__no__replace__'
Expand Down Expand Up @@ -45,3 +46,47 @@ def jackknife(seq, replace=no_replace):
it = iter(seq)
yield itertools.chain(itertools.islice(it, i), replace,
itertools.islice(it, 1, None))


def shuffle(x, random=None):
""" Randomly reorder values of x

Pure version of standard ``random.shuffle``
"""
if isinstance(x, list):
x = x.copy()
x = list(x)
core_random.shuffle(x, random=random)
return x


class Reservoir(object):
""" Basic object for Reservoir Sampling

>>> res = Reservoir(3) # Reservoir of size 3
>>> for item in range(10):
... res.add(item)

Res contains three elements randomly chosen from ``range(10)``

>>> list(res) # doctest: +SKIP
[8, 3]
"""
__slots__ = 'size', 'random', 'storage', 'count'
def __init__(self, size, random=core_random.random):
self.size = size
self.random = random
self.storage = set()
self.count = 0

def add(self, item):
self.count += 1
if self.count <= self.size:
self.storage.add(item)
else:
if self.random() < float(self.size) / self.count:
dropped = self.storage.pop()
self.storage.add(item)

def __iter__(self):
return iter(self.storage)
25 changes: 24 additions & 1 deletion sampling/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sampling.core import jackknife
from sampling import jackknife, shuffle, Reservoir


def test_jacknife():
Expand All @@ -10,3 +10,26 @@ def test_jacknife():
(0, 2, 3), (1, 0, 3), (1, 2, 0))
assert tuple(tuple(x) for x in jackknife([])) == ()
assert tuple(tuple(x) for x in jackknife([1], replace=0)) == ((0,),)


def test_shuffle():
assert set(shuffle((1, 2, 3))) == set((1, 2, 3))


def test_Reservoir():
r = Reservoir(2)
r.add(1)
assert r.count == 1
assert r.size == 2
assert set(r) == set([1])

r.add(2)
assert r.count == 2
assert r.size == 2
print set(r)
assert set(r) == set([1, 2])

r.add(3)
assert r.count == 3
assert r.size == 2
assert tuple(sorted(r)) in ((1, 2), (1, 3), (2, 3))