Upload operators.py with huggingface_hub
Browse files1703 unmodified lines1704170517061707170817091703 unmodified linesyield from pageclass EncodeLabels(StreamInstanceOperator):"""Encode each value encountered in any field in 'fields' into the integers 0,1,...1703 unmodified lines1704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691703 unmodified linesyield from pageclass FeatureGroupedShuffle(Shuffle):"""Class for shuffling an input dataset by instance 'blocks', not on the individual instance level.Example is if the dataset consists of questions with paraphrases of it, and each question falls into a topic.All paraphrases have the same ID value as the original.In this case, we may want to shuffle on grouping_features = ['question ID'],to keep the paraphrases and original question together.We may also want to group by both 'question ID' and 'topic', if the question IDs are repeated between topics.In this case, grouping_features = ['question ID', 'topic']Args:grouping_features (list of strings): list of feature names to use to define the groups.a group is defined by each unique observed combination of data values for features in grouping_featuresshuffle_within_group (bool): whether to further shuffle the instances within each group block, keeping the block orderArgs (of superclass):page_size (int): The size of each page in the stream. Defaults to 1000.Note: shuffle_by_grouping_features determines the unique groups (unique combinations of values of grouping_features)separately by page (determined by page_size). If a block of instances in the same group are splitinto separate pages (either by a page break falling in the group, or the dataset was not sorted bygrouping_features), these instances will be shuffled separately and thus the grouping may bebroken up by pages. If the user wants to ensure the shuffle does the grouping and shufflingacross all pages, set the page_size to be larger than the dataset size.See outputs_2features_bigpage and outputs_2features_smallpage in test_grouped_shuffle."""grouping_features: List[str] = Noneshuffle_within_group: bool = Falsedef process(self, page: List[Dict], stream_name: Optional[str] = None) -> Generator:if self.grouping_features is None:super().process(page, stream_name)else:yield from self.shuffle_by_grouping_features(page)def shuffle_by_grouping_features(self, page):import itertoolsfrom collections import defaultdictgroups_to_instances = defaultdict(list)for item in page:groups_to_instances[tuple(item[ff] for ff in self.grouping_features)].append(item)# now extract the groups (i.e., lists of dicts with order preserved)page_blocks = list(groups_to_instances.values())# and now shuffle the blocksself.random_generator.shuffle(page_blocks)if self.shuffle_within_group:blocks = []# reshuffle the instances within each block, but keep the blocks in orderfor block in page_blocks:self.random_generator.shuffle(block)blocks.append(block)page_blocks = blocks# now flatten the list so it consists of individual dicts, but in (randomized) block orderreturn list(itertools.chain(*page_blocks))class EncodeLabels(StreamInstanceOperator):"""Encode each value encountered in any field in 'fields' into the integers 0,1,...
operators.py+60