

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__				(			_A      : Features							):





  '''simple docstring'''







  a__  =np.inf

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

  _visit(_A	,      _A							)

  return None if batch_size is np.inf else batch_size







class 						__magic_name__			(							lowerCamelCase__							):

   '''simple docstring'''




   def __init__(				self,    lowercase_,    lowercase_ = None,    lowercase_ = None,    lowercase_ = None,    lowercase_ = False,    lowercase_ = False,    lowercase_ = None,    **lowercase_,    )							->    List[Any]:






     """simple docstring"""






     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       )							->    Dict:






     """simple docstring"""






     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 						__magic_name__			:

   '''simple docstring'''




   def __init__(				self,    lowercase_,    lowercase_,    lowercase_ = None,    **lowercase_,    )							->    Dict:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self       )							->    int:






     """simple docstring"""






     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,    lowercase_,    lowercase_,    **lowercase_       )							->    int:






     """simple docstring"""






     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



from __future__ import annotations




def 			UpperCAmelCase__				(			_A      : float	,      _A      : float	,      _A      : float	,      ):





  '''simple docstring'''







  if (electron_conc, hole_conc, intrinsic_conc).count(0							) != 1:
    raise ValueError('''You cannot supply more or less than 2 values'''							)
  elif electron_conc < 0:
    raise ValueError('''Electron concentration cannot be negative in a semiconductor'''							)
  elif hole_conc < 0:
    raise ValueError('''Hole concentration cannot be negative in a semiconductor'''							)
  elif intrinsic_conc < 0:
    raise ValueError(
        '''Intrinsic concentration cannot be negative in a semiconductor'''							)
  elif electron_conc == 0:
    return (
        "electron_conc",
        intrinsic_conc**2 / hole_conc,
    )
  elif hole_conc == 0:
    return (
        "hole_conc",
        intrinsic_conc**2 / electron_conc,
    )
  elif intrinsic_conc == 0:
    return (
        "intrinsic_conc",
        (electron_conc * hole_conc) ** 0.5,
    )
  else:
    return (-1, -1)


if __name__ == "__main__":
       import doctest

       doctest.testmod()
