<commit_before>from functools import reduce
from itertools import chain

from bndl.compute.dataset import Dataset, Partition


class ZippedDataset(Dataset):
    def __init__(self, *sources, comb, dset_id=None):
        assert len(sources) > 1
        self.srcparts = [src.parts() for src in sources]
        self.pcount = len(self.srcparts[0])
        assert all(self.pcount == len(srcparts) for srcparts in self.srcparts[1:])
        self.comb = comb
        super().__init__(sources[0].ctx, src=sources, dset_id=dset_id)

    def parts(self):
        return [ZippedPartition(self, i, [srcpart[i] for srcpart in self.srcparts])
                for i in range(self.pcount)]


def _get_overlapping_workers(workers):
    return set(chain.from_iterable(workers)), reduce(lambda a, b: a.intersection(b), workers)


class ZippedPartition(Partition):
    def __init__(self, dset, idx, children):
        super().__init__(dset, idx, children)


    def _preferred_workers(self, workers):
        union, intersection = _get_overlapping_workers([
            set(child.preferred_workers(workers) or ())
            for child in self.src
        ])
        return intersection if intersection else union


    def _allowed_workers(self, workers):
        union, intersection = _get_overlapping_workers([
            set(child.allowed_workers(workers) or ())
            for child in self.src
        ])
        if not intersection:
            raise RuntimeError('Allowed workers for partitions %s must overlap'
                               % ', '.join(child.idx for child in self.src))
        else:
            return union


    def _compute(self):
        return self.dset.comb(*(child.compute() for child in self.src))<commit_msg>cleanup of old locality related code, zip can lean on Partition._locality<commit_after>from bndl.compute.dataset import Dataset, Partition


class ZippedDataset(Dataset):
    def __init__(self, *sources, comb, dset_id=None):
        assert len(sources) > 1
        self.srcparts = [src.parts() for src in sources]
        self.pcount = len(self.srcparts[0])
        assert all(self.pcount == len(srcparts) for srcparts in self.srcparts[1:])
        self.comb = comb
        super().__init__(sources[0].ctx, src=sources, dset_id=dset_id)

    def parts(self):
        return [ZippedPartition(self, i, [srcpart[i] for srcpart in self.srcparts])
                for i in range(self.pcount)]


class ZippedPartition(Partition):
    def __init__(self, dset, idx, children):
        super().__init__(dset, idx, children)

    def _compute(self):
        return self.dset.comb(*(child.compute() for child in self.src))
{'fraction_non_alphanumeric': 0.10727824109173617, 'fraction_numerical': 0.003032600454890068, 'mean_word_length': 2.9270833333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3055245', 'n_tokens_mistral': 793, 'n_tokens_neox': 778, 'n_words': 209}