<commit_before>from __future__ import division
import numpy as np
#cimport numpy as np
#import cython

def randomSample(n, N, weights=None, replace=False, method="Cormen", prng=np.random)
    '''
       Random sample of size n from a population of size N drawn with or without weights, with or without replacement.
       
       If no weights are provided, the sample is drawn with equal probability of selecting every item.
       If weights are provided, len(weights) must equal N.
       
       Sampling methods available are 
           Fisher-Yates:    sampling without weights, without replacement
           PIKK:            sampling without weights, without replacement (deprecated)
           Cormen:          samping without weights, without replacement
           Waterman_R:      sampling without weights, without replacement
           Vitter_Z:        sampling without weights, without replacement
           sample_by_index: sampling without weights, without replacement
           
           Exponential:     sampling with weights, without replacement (deprecated)
           Elimination:     sampling with weights, without replacement
           ...
    '''
    if weights is not None:
        assert len(weights) == N
    if not replace:
        assert n <= N
        
    methods = {
        "Fisher-Yates" : lambda  N, n: fykd(N, n, gen=prng),
        "PIKK" : lambda N, n: PIKK(N, n, gen=prng),
        "Cormen" : lambda N, n: Random_Sample(N, n, gen=prng),
        "Waterman_R" : lambda N, n: Algorithm_R(N, n, gen=prng),
        "Vitter_Z" :  # TODO
        "sample_by_index" : lambda N, n: sample_by_index(N, n, gen=prng)
        "Exponential" : lambda N, n: exponential_sample(n, weights, prng),
        "Elimination" : lambda N, n: elimination_sample(n, weights, replace, prng)
    }
    
    try: 
        sam = methods[method](N, n):
    except ValueError: 
        print("Sampling method is incompatible with the inputs")
    return sam
    
    
###################### Sampling functions #####################################

def fykd_sample(n, k, gen=np.random):
    '''
    Use fykd to sample k out of 1, ..., n
    '''
    a = list(range(1, n+1))
    rand = gen.random(k)
    ind = np.array(range(k))
    JJ = np.array(ind + rand*(n - ind), dtype = int)
    for i in range(k):
        J = JJ[i]
        a[i], a[J] = a[J], a[i]
    return(a[:k])


def PIKK(n, k, gen=np.random):
    '''
    PIKK Algorithm: permute indices and keep k
    '''
    return set(np.argsort(gen.random(n))[0:k])


def Random_Sample(n, k, gen=np.random):
    '''
    Recursive sampling algorithm from Cormen et al
    '''
    if k==0:
        return set()
    else:
        S = Random_Sample(n-1, k-1)
        i = gen.randint(1,n+1) 
        if i in S:
            S = S.union([n])
        else:
            S = S.union([i])
    return S


def Algorithm_R(n, k, gen=np.random):  
    '''
    Waterman's Algorithm R for resevoir SRSs
    '''
    S = list(range(1, k+1))  # fill the reservoir
    for t in range(k+1,n+1):
        i = gen.randint(1,t+1)
        if i <= k:
            S[i-1] = t
    return set(S)


def sample_by_index(int n, int k, gen=np.random):
    '''
    Generate a random sample of 1,...,n by selecting indices uniformly at random
    '''
    cdef int nprime = n
    cdef int j, w<commit_msg>BUG: fix typos, move Cython functions to the end and comment them out<commit_after>from __future__ import division
import numpy as np
#cimport numpy as np
#import cython

def randomSample(n, N, weights=None, replace=False, method="Cormen", prng=np.random):
    '''
       Random sample of size n from a population of size N drawn with or without weights, with or without replacement.
       
       If no weights are provided, the sample is drawn with equal probability of selecting every item.
       If weights are provided, len(weights) must equal N.
       
       Sampling methods available are 
           Fisher-Yates:    sampling without weights, without replacement
           PIKK:            sampling without weights, without replacement (deprecated)
           Cormen:          samping without weights, without replacement
           Waterman_R:      sampling without weights, without replacement
           Vitter_Z:        sampling without weights, without replacement
           sample_by_index: sampling without weights, without replacement
           
           Exponential:     sampling with weights, without replacement (deprecated)
           Elimination:     sampling with weights, without replacement
           ...
    '''
    if weights is not None:
        assert len(weights) == N
    if not replace:
        assert n <= N
        
    methods = {
        "Fisher-Yates" : lambda  N, n: fykd_sample(N, n, gen=prng),
        "PIKK" : lambda N, n: PIKK(N, n, gen=prng),
        "Cormen" : lambda N, n: Random_Sample(N, n, gen=prng),
        "Waterman_R" : lambda N, n: Algorithm_R(N, n, gen=prng),
# TODO        "Vitter_Z" :  
        "sample_by_index" : lambda N, n: sample_by_index(N, n, gen=prng),
        "Exponential" : lambda N, n: exponential_sample(n, weights, prng),
        "Elimination" : lambda N, n: elimination_sample(n, weights, replace, prng)
    }
    
    try: 
        sam = methods[method](N, n)
    except ValueError: 
        print("Sampling method is incompatible with the inputs")
    return sam
    
    
###################### Sampling functions #####################################

def fykd_sample(n, k, gen=np.random):
    '''
    Use fykd to sample k out of 1, ..., n
    '''
    a = list(range(1, n+1))
    rand = gen.random(k)
    ind = np.array(range(k))
    JJ = np.array(ind + rand*(n - ind), dtype = int)
    for i in range(k):
        J = JJ[i]
        a[i], a[J] = a[J], a[i]
    return(a[:k])


def PIKK(n, k, gen=np.random):
    '''
    PIKK Algorithm: permute indices and keep k
    '''
    return set(np.argsort(gen.random(n))[0:k])


def Random_Sample(n, k, gen=np.random):
    '''
    Recursive sampling algorithm from Cormen et al
    '''
    if k==0:
        return set()
    else:
        S = Random_Sample(n-1, k-1)
        i = gen.randint(1,n+1) 
        if i in S:
            S = S.union([n])
        else:
            S = S.union([i])
    return S


def Algorithm_R(n, k, gen=np.random):  
    '''
    Waterman's Algorithm R for resevoir SRSs
    '''
    S = list(range(1, k+1))  # fill the reservoir
    for t in range(k+1,n+1):
        i = gen.randint(1,t+1)
        if i <= k:
            S[i-1] = t
    return set(S)


def sample_by_index(n, k, gen=np.random):
    '''
    Generate a random sample of 1,...,n by selecting indices uniformly at random
    '''
    nprime = n
{'fraction_non_alphanumeric': 0.12606677646354245, 'fraction_numerical': 0.005090582422518341, 'mean_word_length': 2.263312164142648, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6849625', 'n_tokens_mistral': 2118, 'n_tokens_neox': 2065, 'n_words': 683}