"""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      lowerCamelCase_(__SCREAMING_SNAKE_CASE  )->    Optional[int]:
							_SCREAMING_SNAKE_CASE		:						Dict			  =    np.inf

							def set_batch_size(__SCREAMING_SNAKE_CASE  ) -> None:
														nonlocal batch_size
														if isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE  ):
																					_SCREAMING_SNAKE_CASE		:						List[str]			  =    min(__SCREAMING_SNAKE_CASE     ,	config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS  )
														elif isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE  ):
																					_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    min(__SCREAMING_SNAKE_CASE     ,	config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS  )
														elif isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE  ) and feature.dtype == "binary":
																					_SCREAMING_SNAKE_CASE		:						Tuple			  =    min(__SCREAMING_SNAKE_CASE     ,	config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS  )

							_visit(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE  )

							return None if batch_size is np.inf else batch_size




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






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



											"""simple docstring"""







											super().__init__(
											    _A      ,    split=_A      ,    features=_A      ,    cache_dir=_A      ,    keep_in_memory=_A      ,    streaming=_A      ,    num_proc=_A      ,    **_A      ,    )
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    path_or_paths if isinstance(_A      ,    _A) else {self.split: path_or_paths}
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    _PACKAGED_DATASETS_MODULES["""parquet"""][1]
											_SCREAMING_SNAKE_CASE		:						Dict			  =    Parquet(
											    cache_dir=_A      ,    data_files=_A      ,    features=_A      ,    hash=_A      ,    **_A      ,    )





				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







											if self.streaming:
																		_SCREAMING_SNAKE_CASE		:						List[str]			  =    self.builder.as_streaming_dataset(split=self.split)
											# Build regular (map-style) dataset
											else:
																		_SCREAMING_SNAKE_CASE		:						str			  =    None
																		_SCREAMING_SNAKE_CASE		:						Any			  =    None
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    None
																		_SCREAMING_SNAKE_CASE		:						Any			  =    None

																		self.builder.download_and_prepare(
																		    download_config=_A      ,    download_mode=_A      ,    verification_mode=_A      ,    base_path=_A      ,    num_proc=self.num_proc      ,    )
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    self.builder.as_dataset(
																		    split=self.split      ,    verification_mode=_A      ,    in_memory=self.keep_in_memory)
											return dataset




class  _snake_case		:







				"""simple docstring"""






				def __init__(      self							:							Optional[int]      ,    _A							:							Dataset      ,    _A							:							Union[PathLike, BinaryIO]      ,    _A							:							Optional[int] = None      ,    **_A							:							List[Any]      ,    ):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    dataset
											_SCREAMING_SNAKE_CASE		:						str			  =    path_or_buf
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    batch_size or get_writer_batch_size(dataset.features)
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    parquet_writer_kwargs





				def 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    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:
																									_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    self._write(file_obj=_A      ,    batch_size=_A      ,    **self.parquet_writer_kwargs)
											else:
																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    self._write(file_obj=self.path_or_buf      ,    batch_size=_A      ,    **self.parquet_writer_kwargs)
											return written





				def 		_lowerCAmelCase		(      self							:							Optional[Any]      ,    _A							:							BinaryIO      ,    _A							:							int      ,    **_A							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    0
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    parquet_writer_kwargs.pop("""path_or_buf"""      ,    _A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    self.dataset.features.arrow_schema

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    pq.ParquetWriter(_A      ,    schema=_A      ,    **_A)

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



"""simple docstring"""





from __future__ import annotations



def      lowerCamelCase_(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	)->    tuple:
							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()


