






"""simple docstring"""
import os
from typing import BinaryIO, Optional, Union

import numpy as np
import pyarrow.parquet as pq

from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config
from ..features.features import FeatureType, _visit
from ..formatting import query_table
from ..packaged_modules import _PACKAGED_DATASETS_MODULES
from ..packaged_modules.parquet.parquet import Parquet
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader


def 							A						(				__snake_case: Features  )    ->		Optional[int]:

       """simple docstring"""





       __magic_name__      				=    np.inf

       def set_batch_size(__snake_case: FeatureType  ) -> None:
              nonlocal batch_size
              if isinstance(__snake_case ,     __snake_case  ):
                     __magic_name__      				=    min(__snake_case ,     config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS  )
              elif isinstance(__snake_case ,     __snake_case  ):
                     __magic_name__      				=    min(__snake_case ,     config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS  )
              elif isinstance(__snake_case ,     __snake_case  ) and feature.dtype == "binary":
                     __magic_name__      				=    min(__snake_case ,     config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS  )

       _visit(__snake_case ,     __snake_case  )

       return None if batch_size is np.inf else batch_size



class        UpperCamelCase__ (				a_):







    """simple docstring"""







    def __init__(		self :	int				,    UpperCamelCase_ :	NestedDataStructureLike[PathLike]				,    UpperCamelCase_ :	Optional[NamedSplit] = None				,    UpperCamelCase_ :	Optional[Features] = None				,    UpperCamelCase_ :	str = None				,    UpperCamelCase_ :	bool = False				,    UpperCamelCase_ :	bool = False				,    UpperCamelCase_ :	Optional[int] = None				,    **UpperCamelCase_ :	Union[str, Any]				,    ):





           '''simple docstring'''

           super().__init__(
               UpperCamelCase_				,    split=UpperCamelCase_				,    features=UpperCamelCase_				,    cache_dir=UpperCamelCase_				,    keep_in_memory=UpperCamelCase_				,    streaming=UpperCamelCase_				,    num_proc=UpperCamelCase_				,    **UpperCamelCase_				,    )
           __magic_name__      				=    path_or_paths if isinstance(UpperCamelCase_				,    UpperCamelCase_ ) else {self.split: path_or_paths}
           __magic_name__      				=    _PACKAGED_DATASETS_MODULES['parquet'][1]
           __magic_name__      				=    Parquet(
               cache_dir=UpperCamelCase_				,    data_files=UpperCamelCase_				,    features=UpperCamelCase_				,    hash=UpperCamelCase_				,    **UpperCamelCase_				,    )







    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           if self.streaming:
                  __magic_name__      				=    self.builder.as_streaming_dataset(split=self.split )
           # Build regular (map-style) dataset
           else:
                  __magic_name__      				=    None
                  __magic_name__      				=    None
                  __magic_name__      				=    None
                  __magic_name__      				=    None

                  self.builder.download_and_prepare(
                      download_config=UpperCamelCase_				,    download_mode=UpperCamelCase_				,    verification_mode=UpperCamelCase_				,    base_path=UpperCamelCase_				,    num_proc=self.num_proc				,    )
                  __magic_name__      				=    self.builder.as_dataset(
                      split=self.split				,    verification_mode=UpperCamelCase_				,    in_memory=self.keep_in_memory )
           return dataset



class        UpperCamelCase__ :







    """simple docstring"""







    def __init__(		self :	Optional[int]				,    UpperCamelCase_ :	Dataset				,    UpperCamelCase_ :	Union[PathLike, BinaryIO]				,    UpperCamelCase_ :	Optional[int] = None				,    **UpperCamelCase_ :	Dict				,    ):





           '''simple docstring'''

           __magic_name__      				=    dataset
           __magic_name__      				=    path_or_buf
           __magic_name__      				=    batch_size or get_writer_batch_size(dataset.features )
           __magic_name__      				=    parquet_writer_kwargs







    def 				a__			(		self :	Union[str, Any] ):





           '''simple docstring'''

           __magic_name__      				=    self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE

           if isinstance(self.path_or_buf				,    (str, bytes, os.PathLike) ):
                  with open(self.path_or_buf				,    'wb+' ) as buffer:
                         __magic_name__      				=    self._write(file_obj=UpperCamelCase_				,    batch_size=UpperCamelCase_				,    **self.parquet_writer_kwargs )
           else:
                  __magic_name__      				=    self._write(file_obj=self.path_or_buf				,    batch_size=UpperCamelCase_				,    **self.parquet_writer_kwargs )
           return written







    def 				a__			(		self :	Tuple				,    UpperCamelCase_ :	BinaryIO				,    UpperCamelCase_ :	int				,    **UpperCamelCase_ :	Any ):





           '''simple docstring'''

           __magic_name__      				=    0
           __magic_name__      				=    parquet_writer_kwargs.pop('path_or_buf'				,    UpperCamelCase_ )
           __magic_name__      				=    self.dataset.features.arrow_schema

           __magic_name__      				=    pq.ParquetWriter(UpperCamelCase_				,    schema=UpperCamelCase_				,    **UpperCamelCase_ )

           for offset in logging.tqdm(
               range(0				,    len(self.dataset )				,    UpperCamelCase_ )				,    unit='ba'				,    disable=not logging.is_progress_bar_enabled()				,    desc='Creating parquet from Arrow format'				,    ):
                  __magic_name__      				=    query_table(
                      table=self.dataset._data				,    key=slice(UpperCamelCase_				,    offset + batch_size )				,    indices=self.dataset._indices if self.dataset._indices is not None else None				,    )
                  writer.write_table(UpperCamelCase_ )
                  written += batch.nbytes
           writer.close()
           return written







"""simple docstring"""
from ..utils import DummyObject, requires_backends



class        UpperCamelCase__ (				metaclass=a_):







    """simple docstring"""
    __UpperCAmelCase           = ["""flax""", """transformers"""]







    def __init__(		self :	List[Any]				,    *UpperCamelCase_ :	str				,    **UpperCamelCase_ :	Any ):





           '''simple docstring'''

           requires_backends(self				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	Optional[int]				,    *UpperCamelCase_ :	Tuple				,    **UpperCamelCase_ :	List[Any] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	str				,    *UpperCamelCase_ :	Optional[Any]				,    **UpperCamelCase_ :	Dict ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )



class        UpperCamelCase__ (				metaclass=a_):







    """simple docstring"""
    __UpperCAmelCase           = ["""flax""", """transformers"""]







    def __init__(		self :	List[Any]				,    *UpperCamelCase_ :	str				,    **UpperCamelCase_ :	str ):





           '''simple docstring'''

           requires_backends(self				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	Optional[int]				,    *UpperCamelCase_ :	Optional[int]				,    **UpperCamelCase_ :	List[str] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	Tuple				,    *UpperCamelCase_ :	List[str]				,    **UpperCamelCase_ :	Optional[Any] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )



class        UpperCamelCase__ (				metaclass=a_):







    """simple docstring"""
    __UpperCAmelCase           = ["""flax""", """transformers"""]







    def __init__(		self :	Optional[int]				,    *UpperCamelCase_ :	List[Any]				,    **UpperCamelCase_ :	List[str] ):





           '''simple docstring'''

           requires_backends(self				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	str				,    *UpperCamelCase_ :	List[str]				,    **UpperCamelCase_ :	Tuple ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	Tuple				,    *UpperCamelCase_ :	Any				,    **UpperCamelCase_ :	List[str] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )



class        UpperCamelCase__ (				metaclass=a_):







    """simple docstring"""
    __UpperCAmelCase           = ["""flax""", """transformers"""]







    def __init__(		self :	Optional[Any]				,    *UpperCamelCase_ :	Optional[Any]				,    **UpperCamelCase_ :	Dict ):





           '''simple docstring'''

           requires_backends(self				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	str				,    *UpperCamelCase_ :	Dict				,    **UpperCamelCase_ :	List[str] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	str				,    *UpperCamelCase_ :	Optional[Any]				,    **UpperCamelCase_ :	Union[str, Any] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )