





'''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 						UpperCamelCase				(							_lowerCamelCase					:					Features       ):
    A__				=			np.inf

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

    _visit(_lowerCamelCase						,       _lowerCamelCase       )

    return None if batch_size is np.inf else batch_size






class    UpperCAmelCase	(       UpperCamelCase__				):
 def __init__(				self      :Tuple						,       lowercase_      :NestedDataStructureLike[PathLike]						,       lowercase_      :Optional[NamedSplit] = None						,       lowercase_      :Optional[Features] = None						,       lowercase_      :str = None						,       lowercase_      :bool = False						,       lowercase_      :bool = False						,       lowercase_      :Optional[int] = None						,       **lowercase_      :Tuple						,       )->							Union[str, Any]:
     super().__init__(
         lowercase_						,       split=lowercase_						,       features=lowercase_						,       cache_dir=lowercase_						,       keep_in_memory=lowercase_						,       streaming=lowercase_						,       num_proc=lowercase_						,       **lowercase_						,       )
     A__				=			path_or_paths if isinstance(lowercase_						,       lowercase_							) else {self.split: path_or_paths}
     A__				=			_PACKAGED_DATASETS_MODULES["parquet"][1]
     A__				=			Parquet(
         cache_dir=lowercase_						,       data_files=lowercase_						,       features=lowercase_						,       hash=lowercase_						,       **lowercase_						,       )



 def      UpperCAmelCase_							(				self      :Any							)->							Optional[int]:
     # Build iterable dataset
     if self.streaming:
         A__				=			self.builder.as_streaming_dataset(split=self.split							)
     # Build regular (map-style) dataset
     else:
         A__				=			None
         A__				=			None
         A__				=			None
         A__				=			None

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






class    UpperCAmelCase	:
 def __init__(				self      :Optional[int]						,       lowercase_      :Dataset						,       lowercase_      :Union[PathLike, BinaryIO]						,       lowercase_      :Optional[int] = None						,       **lowercase_      :Any						,       )->							int:
     A__				=			dataset
     A__				=			path_or_buf
     A__				=			batch_size or get_writer_batch_size(dataset.features							)
     A__				=			parquet_writer_kwargs
 def      UpperCAmelCase_							(				self      :List[str]							)->							int:
     A__				=			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:
             A__				=			self._write(file_obj=lowercase_						,       batch_size=lowercase_						,       **self.parquet_writer_kwargs							)
     else:
         A__				=			self._write(file_obj=self.path_or_buf						,       batch_size=lowercase_						,       **self.parquet_writer_kwargs							)
     return written



 def      UpperCAmelCase_							(				self      :Union[str, Any]						,       lowercase_      :BinaryIO						,       lowercase_      :int						,       **lowercase_      :Any							)->							int:
     A__				=			0
     A__				=			parquet_writer_kwargs.pop("path_or_buf"						,       lowercase_							)
     A__				=			self.dataset.features.arrow_schema

     A__				=			pq.ParquetWriter(lowercase_						,       schema=lowercase_						,       **lowercase_							)

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










'''simple docstring'''

from ..utils import DummyObject, requires_backends






class    UpperCAmelCase	(       metaclass=UpperCamelCase__				):
 __lowercase   		=				["""note_seq"""]
 def __init__(				self      :Optional[Any]						,       *lowercase_      :List[Any]						,       **lowercase_      :List[str]							)->							int:
     requires_backends(self						,       ["note_seq"]							)
 @classmethod
 def      UpperCAmelCase_							(				cls      :str						,       *lowercase_      :Union[str, Any]						,       **lowercase_      :Any							)->							Optional[int]:
     requires_backends(cls						,       ["note_seq"]							)



 @classmethod
 def      UpperCAmelCase_							(				cls      :Dict						,       *lowercase_      :Tuple						,       **lowercase_      :List[Any]							)->							Optional[Any]:
     requires_backends(cls						,       ["note_seq"]							)



