Upload operators.py with huggingface_hub
Browse files31 unmodified lines3233343535363719 unmodified lines575859616061626392 unmodified lines15615715816015916016111 unmodified lines1731741751781761771781798 unmodified lines18818919019319119219332 unmodified lines22622722823222923023123213 unmodified lines24624724825224925025125225325425525625725825925825926026026126226326126526626226326426526632 unmodified lines29930030130530230330830430530630730830931031131231331431531631531731831978 unmodified lines39839940040040140240317 unmodified lines42142242342442442542611 unmodified lines43843944044244344144244344444544644744944844945045245145245345545445545645780 unmodified lines5385395405425415425436 unmodified lines550551552555553554555556250 unmodified lines80780880981281081181281381481581681781881982082181982282382482513 unmodified lines8398408418398428438448458468478488498508518528538468548558568578 unmodified lines8668678688618698708718728738748758768778788668678798808818828834 unmodified lines8888898908788918928938948958968978988992 unmodified lines90290390488788890590690790890951 unmodified lines9619629639469649659662 unmodified lines96997097195595697297397497597671 unmodified lines104810491050103410511052105310541 unmodified line10561057105810421043104410591060106110621063661 unmodified lines172517261727171217281729173017317 unmodified lines17391740174117261727174217431744174517461747174850 unmodified lines179918001801178417851786180218031804178918051806180718089 unmodified lines181818191820180518211822182318248 unmodified lines1833183418351820183618371838183913 unmodified lines1853185418551840185618571858185931 unmodified linesGeneral Operaotrs List:------------------------"""import collectionsimport copyimport operatorimport uuid19 unmodified linesimport requests
from .artifact import Artifact, fetch_artifactfrom .dataclass import NonPositionalField, OptionalFieldfrom .dataclass import DeprecatedField, NonPositionalField, OptionalFieldfrom .dict_utils import dict_delete, dict_get, dict_set, is_subpathfrom .operator import ( MultiStream,92 unmodified lines
mappers: Dict[str, Dict[str, str]] strict: bool = True use_query: bool = False process_every_value: bool = False
def verify(self):11 unmodified lines self, instance: Dict[str, Any], stream_name: Optional[str] = None ) -> Dict[str, Any]: for key, mapper in self.mappers.items(): value = dict_get(instance, key, use_dpath=self.use_query) value = dict_get(instance, key) if value is not None: if (self.process_every_value is True) and (not isinstance(value, list)): raise ValueError(8 unmodified lines instance, key, value, use_dpath=self.use_query, )
return instance32 unmodified lines
Args: fields (Dict[str, object]): The fields to add to each instance. use_query (bool) : Use '/' to access inner fields Use '/' to access inner fields use_deepcopy (bool) : Deep copy the input value to avoid later modifications
Examples:13 unmodified lines """
fields: Dict[str, object] use_query: bool = False use_query: bool = DeprecatedField( metadata={ "deprecation_msg": "Field 'use_query' is deprecated. From now on, default behavior is compatible to use_query=True. " "Please remove this field from your code." } ) use_deepcopy: bool = False
def process( self, instance: Dict[str, Any], stream_name: Optional[str] = None ) -> Dict[str, Any]: if self.use_query: for key, value in self.fields.items(): for key, value in self.fields.items(): if self.use_deepcopy: value = deepcopy(value) dict_set(instance, key, value, use_dpath=self.use_query) else: if self.use_deepcopy: self.fields = deepcopy(self.fields) instance.update(self.fields) value = deepcopy(value) dict_set(instance, key, value) return instance
32 unmodified lines The operator throws an AssertionError in either of these cases. field_to_field defaults to None process_every_value (bool): Processes the values in a list instead of the list as a value, similar to *var. Defaults to False use_query (bool): Whether to use dpath style queries. Defaults to False.
Note: if 'field' and 'to_field' (or both members of a pair in 'field_to_field') are equal (or share a common prefix if 'use_query'=True), then the result of the operation is saved within 'field' prefix if 'field' and 'to_field' contain a /), then the result of the operation is saved within 'field' """
field: Optional[str] = None to_field: Optional[str] = None field_to_field: Optional[Union[List[List[str]], Dict[str, str]]] = None use_query: bool = DeprecatedField( metadata={ "deprecation_msg": "Field 'use_query' is deprecated. From now on, default behavior is compatible to use_query=True. " "Please remove this field from your code." } ) process_every_value: bool = False use_query: bool = False get_default: Any = None not_exist_ok: bool = False
78 unmodified lines old_value = dict_get( instance, from_field, use_dpath=self.use_query, default=self.get_default, not_exist_ok=self.not_exist_ok, )17 unmodified lines instance, to_field, new_value, use_dpath=self.use_query, not_exist_ok=True, ) return instance11 unmodified linesclass RenameFields(FieldOperator): """Renames fields.
Move value from one field to another, potentially, if 'use_query'=True, from one branch into another. Remove the from field, potentially part of it in case of use_query. Move value from one field to another, potentially, if field name contains a /, from one branch into another. Remove the from field, potentially part of it in case of / in from_field.
Examples: RenameFields(field_to_field={"b": "c"}) will change inputs [{"a": 1, "b": 2}, {"a": 2, "b": 3}] to [{"a": 1, "c": 2}, {"a": 2, "c": 3}]
RenameFields(field_to_field={"b": "c/d"}, use_query=True) RenameFields(field_to_field={"b": "c/d"}) will change inputs [{"a": 1, "b": 2}, {"a": 2, "b": 3}] to [{"a": 1, "c": {"d": 2}}, {"a": 2, "c": {"d": 3}}]
RenameFields(field_to_field={"b": "b/d"}, use_query=True) RenameFields(field_to_field={"b": "b/d"}) will change inputs [{"a": 1, "b": 2}, {"a": 2, "b": 3}] to [{"a": 1, "b": {"d": 2}}, {"a": 2, "b": {"d": 3}}]
RenameFields(field_to_field={"b/c/e": "b/d"}, use_query=True) RenameFields(field_to_field={"b/c/e": "b/d"}) will change inputs [{"a": 1, "b": {"c": {"e": 2, "f": 20}}}] to [{"a": 1, "b": {"c": {"f": 20}, "d": 2}}]
"""80 unmodified lines old_value = dict_get( instance, field_name, use_dpath=True, default="", not_exist_ok=False, )6 unmodified lines raise RuntimeError( f"Error augmenting value '{old_value}' from '{field_name}' in instance: {instance}" ) from e dict_set(instance, field_name, new_value, use_dpath=True, not_exist_ok=True) dict_set(instance, field_name, new_value, not_exist_ok=True) return instance
250 unmodified lines
fields: List[str] to_field: str use_query: bool = False use_query: bool = DeprecatedField( metadata={ "deprecation_msg": "Field 'use_query' is deprecated. From now on, default behavior is compatible to use_query=True. " "Please remove this field from your code." } )
def process( self, instance: Dict[str, Any], stream_name: Optional[str] = None ) -> Dict[str, Any]: values = [] for field_name in self.fields: values.append(dict_get(instance, field_name, use_dpath=self.use_query)) values.append(dict_get(instance, field_name)) instance[self.to_field] = values return instance
13 unmodified lines fields: List[str] to_field: str longest: bool = False use_query: bool = False use_query: bool = DeprecatedField( metadata={ "deprecation_msg": "Field 'use_query' is deprecated. From now on, default behavior is compatible to use_query=True. " "Please remove this field from your code." } )
def process( self, instance: Dict[str, Any], stream_name: Optional[str] = None ) -> Dict[str, Any]: values = [] for field_name in self.fields: values.append(dict_get(instance, field_name, use_dpath=self.use_query)) values.append(dict_get(instance, field_name)) if self.longest: zipped = zip_longest(*values) else:8 unmodified lines search_in: str index_of: str to_field: str use_query: bool = False use_query: bool = DeprecatedField( metadata={ "deprecation_msg": "Field 'use_query' is deprecated. From now on, default behavior is compatible to use_query=True. " "Please remove this field from your code." } )
def process( self, instance: Dict[str, Any], stream_name: Optional[str] = None ) -> Dict[str, Any]: lst = dict_get(instance, self.search_in, use_dpath=self.use_query) item = dict_get(instance, self.index_of, use_dpath=self.use_query) lst = dict_get(instance, self.search_in) item = dict_get(instance, self.index_of) instance[self.to_field] = lst.index(item) return instance
4 unmodified lines field: str index: str to_field: str = None use_query: bool = False use_query: bool = DeprecatedField( metadata={ "deprecation_msg": "Field 'use_query' is deprecated. From now on, default behavior is compatible to use_query=True. " "Please remove this field from your code." } )
def prepare(self): if self.to_field is None:2 unmodified lines def process( self, instance: Dict[str, Any], stream_name: Optional[str] = None ) -> Dict[str, Any]: value = dict_get(instance, self.field, use_dpath=self.use_query) index_value = dict_get(instance, self.index, use_dpath=self.use_query) value = dict_get(instance, self.field) index_value = dict_get(instance, self.index) instance[self.to_field] = value[index_value] return instance
51 unmodified lines
Args (of parent class): field_to_field (Union[List[List], Dict[str, str]]): A list of lists, where each sublist contains the source field and the destination field, or a dictionary mapping source fields to destination fields. use_query (bool): Whether to use dpath for accessing fields. Defaults to False.
Examples: An input instance {"a": 2, "b": 3}, when processed by2 unmodified lines CopyField(field_to_field={"a": "c"} would yield {"a": 2, "b": 3, "c": 2}
with use_query=True, we can also copy inside the field: CopyFields(field_to_field={"a/0": "a"}, use_query=True) with field names containing / , we can also copy inside the field: CopyFields(field_to_field={"a/0": "a"}) would process instance {"a": [1, 3]} into {"a": 1}
71 unmodified lines self, instance: Dict[str, Any], stream_name: Optional[str] = None ) -> Dict[str, Any]: for field_name, type in self.fields.items(): value = dict_get(instance, field_name, use_dpath=self.use_nested_query) value = dict_get(instance, field_name) if self.process_every_value: assert isinstance( value, list1 unmodified line casted_value = self._cast_multiple(value, type, field_name) else: casted_value = self._cast_single(value, type, field_name) dict_set( instance, field_name, casted_value, use_dpath=self.use_nested_query )
dict_set(instance, field_name, casted_value) return instance
661 unmodified lines self, instance: Dict[str, Any], stream_name: Optional[str] = None ) -> Dict[str, Any]: for field_name in self.fields: values = dict_get(instance, field_name, use_dpath=True) values = dict_get(instance, field_name) values_was_a_list = isinstance(values, list) if not isinstance(values, list): values = [values]7 unmodified lines instance, field_name, new_values, use_dpath=True, set_multiple="*" in field_name, not_exist_ok=False, # the values to encode where just taken from there set_multiple="*" in field_name and isinstance(new_values, list) and len(new_values) > 0, )
return instance50 unmodified lines fields: List[str]
def signature(self, instance): return str( tuple(dict_get(instance, field, use_dpath=True) for field in self.fields) ) return str(tuple(dict_get(instance, field) for field in self.fields))
def process(self, stream: Stream, stream_name: Optional[str] = None) -> Generator: counter = collections.Counter() counter = Counter()
for instance in stream: counter[self.signature(instance)] += 19 unmodified lines lowest_count, self.max_instances // len(counter) )
counter = collections.Counter() counter = Counter()
for instance in stream: sign = self.signature(instance)8 unmodified lines Args: segments_boundaries (List[int]): distinct integers sorted in increasing order, that maps a given total length into the index of the least of them that exceeds the total length. (If none exceeds -- into one index beyond, namely, the length of segments_boudaries) beyond, namely, the length of segments_boundaries)
fields (Optional, List[str])
13 unmodified lines def signature(self, instance): total_len = 0 for field_name in self.fields: total_len += len(dict_get(instance, field_name, use_dpath=True)) total_len += len(dict_get(instance, field_name)) for i, val in enumerate(self.segments_boundaries): if total_len < val: return ioperators.py+70-54