<commit_before>from collections import namedtuple
import random as rand

from wepy.walker import merge
from wepy.resampling.decision import Decision

class Resampler(object):

    def resample(self, walkers, decisions):
        raise NotImplementedError

ResamplingRecord = namedtuple("ResamplingRecord", ['decision', 'value'])


# stubs and examples of resamplers for testing purposes
class NoResampler(Resampler):

    def resample(self, walkers):

        resampling_record = [(Decision.NOTHING, i) for i in len(walkers)]

        return walkers, resampling_record

class RandomCloneMergeResampler(Resampler):
    def __init__(self, seed):
        rand.seed(seed)

    def resample(self, walkers):

        # choose number of clone-merges between 1 and 10
        n_clonemerges = rand.randint(0,10)

        walker_actions = [[] for walker in walkers]
        resampling_records = []
        for i in range(n_clonemerges):

            # choose a random walker to clone
            clone_idx = rand.randint(0, len(walkers))
            walker_actions[i].append(Decision.CLONE)
            clone_walker = walkers[clone_idx]
            # clone the chosen walker
            walker_clone = clone_walker.clone()
            # choose a destination slot (index in the list) to put the clone in
            # the walker occupying that slot will be squashed
            # can't choose the same slot it is in
            squash_idx = rand.choice(set(range(n_walkers)).difference(clone_idx))
            walker_actions[i].append(Decision.SQUASH)
            squash_walker = walkers[squash_idx]

            # find a random merge target that is not either of the
            # cloned walkers
            merge_idx = rand.choice(set(range(n_walkers)).difference([clone_idx, squash_idx]))
            walker_actions[i].append(Decision.KEEP_MERGE)
            merge_walker = walkers[merge_idx]

            # merge the squashed walker with the keep_merge walker
            merged_walker = squash_walker.squash(merge_walker)

            # make a new list of walkers
            resampled_walkers = []
            for idx, walker in enumerate(walkers):
                if idx == squash_idx:
                    resampled_walkers.append(walker_clone)
                elif idx == merge_idx:
                    resampled_walkers.append(merged_walker)
                else:
                    resample_walkers.append(walker)


            return resampled_walkers, resampling_records
<commit_msg>fixed some mistakes in RandomCloneMergeResampler<commit_after>from collections import namedtuple
import random as rand

from wepy.walker import merge
from wepy.resampling.decision import CloneMergeDecision

class Resampler(object):

    def resample(self, walkers, decisions):
        raise NotImplementedError

ResamplingRecord = namedtuple("ResamplingRecord", ['decision', 'value'])


# stubs and examples of resamplers for testing purposes
class NoResampler(Resampler):

    def resample(self, walkers):

        resampling_record = [(CloneMergeDecision.NOTHING, i) for i in len(walkers)]

        return walkers, resampling_record

class RandomCloneMergeResampler(Resampler):
    def __init__(self, seed):
        rand.seed(seed)

    def resample(self, walkers):

        n_walkers = len(walkers)
        # choose number of clone-merges between 1 and 10
        n_clonemerges = rand.randint(0,10)

        walker_actions = [[] for walker in walkers]
        resampling_records = []
        for i in range(n_clonemerges):

            # choose a random walker to clone
            clone_idx = rand.randint(0, len(walkers))
            walker_actions[i].append(CloneMergeDecision.CLONE)
            clone_walker = walkers[clone_idx]
            # clone the chosen walker
            walker_clone = clone_walker.clone()
            # choose a destination slot (index in the list) to put the clone in
            # the walker occupying that slot will be squashed
            # can't choose the same slot it is in
            squash_idx = rand.choice(set(range(n_walkers)).difference(clone_idx))
            walker_actions[i].append(CloneMergeDecision.SQUASH)
            squash_walker = walkers[squash_idx]

            # find a random merge target that is not either of the
            # cloned walkers
            merge_idx = rand.choice(set(range(n_walkers)).difference([clone_idx, squash_idx]))
            walker_actions[i].append(CloneMergeDecision.KEEP_MERGE)
            merge_walker = walkers[merge_idx]

            # merge the squashed walker with the keep_merge walker
            merged_walker = squash_walker.squash(merge_walker)

            # make a new list of walkers
            resampled_walkers = []
            for idx, walker in enumerate(walkers):
                if idx == squash_idx:
                    resampled_walkers.append(walker_clone)
                elif idx == merge_idx:
                    resampled_walkers.append(merged_walker)
                else:
                    resampled_walkers.append(walker)


            return resampled_walkers, resampling_records

{'fraction_non_alphanumeric': 0.07135836445842343, 'fraction_numerical': 0.002752113229801455, 'mean_word_length': 2.4471544715447155, 'pattern_counts': {'":': 0, '<': 3, '<?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': '492520', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1401, 'n_words': 378}