


import pyarrow.parquet as pq
import pytest

from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config
from datasets.features.image import Image
from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases
def     __lowerCamelCase						(			UpperCAmelCase_						:		Tuple			,  UpperCAmelCase_						:		int						):




      """simple docstring"""





      assert isinstance(UpperCAmelCase_			,  UpperCAmelCase_						)
      assert dataset.num_rows == 4
      assert dataset.num_columns == 3
      assert dataset.column_names == ["col_1", "col_2", "col_3"]
      for feature, expected_dtype in expected_features.items():
            assert dataset.features[feature].dtype == expected_dtype
@pytest.mark.parametrize('''keep_in_memory'''			,  [False, True]						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		str						):




      """simple docstring"""





      a						:Union[str, Any]										=	tmp_path / '''cache'''
      a						:Dict										=	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
      with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
            a						:Tuple										=	ParquetDatasetReader(UpperCAmelCase_			,  cache_dir=UpperCAmelCase_			,  keep_in_memory=UpperCAmelCase_						).read()
      _check_parquet_dataset(UpperCAmelCase_			,  UpperCAmelCase_						)
@pytest.mark.parametrize(
    '''features'''			,  [
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		Union[str, Any]			,  UpperCAmelCase_						:		List[str]						):




      """simple docstring"""





      a						:Optional[Any]										=	tmp_path / '''cache'''
      a						:Tuple										=	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
      a						:str										=	features.copy() if features else default_expected_features
      a						:Dict										=	(
          Features({feature: Value(UpperCAmelCase_						) for feature, dtype in features.items()}						) if features is not None else None
      )
      a						:List[str]										=	ParquetDatasetReader(UpperCAmelCase_			,  features=UpperCAmelCase_			,  cache_dir=UpperCAmelCase_						).read()
      _check_parquet_dataset(UpperCAmelCase_			,  UpperCAmelCase_						)
@pytest.mark.parametrize('''split'''			,  [None, NamedSplit('''train'''						), '''train''', '''test''']						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		int			,  UpperCAmelCase_						:		Optional[Any]			,  UpperCAmelCase_						:		Dict						):




      """simple docstring"""





      a						:Optional[int]										=	tmp_path / '''cache'''
      a						:Tuple										=	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
      a						:Optional[int]										=	ParquetDatasetReader(UpperCAmelCase_			,  cache_dir=UpperCAmelCase_			,  split=UpperCAmelCase_						).read()
      _check_parquet_dataset(UpperCAmelCase_			,  UpperCAmelCase_						)
      assert dataset.split == split if split else "train"
@pytest.mark.parametrize('''path_type'''			,  [str, list]						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		Dict			,  UpperCAmelCase_						:		Any						):




      """simple docstring"""





      if issubclass(UpperCAmelCase_			,  UpperCAmelCase_						):
            a						:str										=	parquet_path
      elif issubclass(UpperCAmelCase_			,  UpperCAmelCase_						):
            a						:Optional[int]										=	[parquet_path]
      a						:Any										=	tmp_path / '''cache'''
      a						:List[Any]										=	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
      a						:str										=	ParquetDatasetReader(UpperCAmelCase_			,  cache_dir=UpperCAmelCase_						).read()
      _check_parquet_dataset(UpperCAmelCase_			,  UpperCAmelCase_						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		Tuple			,  UpperCAmelCase_						:		int=("train",)						):




      """simple docstring"""





      assert isinstance(UpperCAmelCase_			,  UpperCAmelCase_						)
      for split in splits:
            a						:Optional[Any]										=	dataset_dict[split]
            assert dataset.num_rows == 4
            assert dataset.num_columns == 3
            assert dataset.column_names == ["col_1", "col_2", "col_3"]
            for feature, expected_dtype in expected_features.items():
                  assert dataset.features[feature].dtype == expected_dtype
@pytest.mark.parametrize('''keep_in_memory'''			,  [False, True]						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		Optional[Any]						):




      """simple docstring"""





      a						:Dict										=	tmp_path / '''cache'''
      a						:List[str]										=	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
      with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
            a						:Any										=	ParquetDatasetReader(
                {'''train''': parquet_path}			,  cache_dir=UpperCAmelCase_			,  keep_in_memory=UpperCAmelCase_						).read()
      _check_parquet_datasetdict(UpperCAmelCase_			,  UpperCAmelCase_						)
@pytest.mark.parametrize(
    '''features'''			,  [
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		Tuple			,  UpperCAmelCase_						:		List[str]						):




      """simple docstring"""





      a						:Optional[Any]										=	tmp_path / '''cache'''
      a						:List[Any]										=	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
      a						:List[Any]										=	features.copy() if features else default_expected_features
      a						:Optional[Any]										=	(
          Features({feature: Value(UpperCAmelCase_						) for feature, dtype in features.items()}						) if features is not None else None
      )
      a						:int										=	ParquetDatasetReader({'''train''': parquet_path}			,  features=UpperCAmelCase_			,  cache_dir=UpperCAmelCase_						).read()
      _check_parquet_datasetdict(UpperCAmelCase_			,  UpperCAmelCase_						)
@pytest.mark.parametrize('''split'''			,  [None, NamedSplit('''train'''						), '''train''', '''test''']						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		Union[str, Any]			,  UpperCAmelCase_						:		Dict			,  UpperCAmelCase_						:		Optional[Any]						):




      """simple docstring"""





      if split:
            a						:Union[str, Any]										=	{split: parquet_path}
      else:
            a						:Any										=	'''train'''
            a						:int										=	{'''train''': parquet_path, '''test''': parquet_path}
      a						:List[str]										=	tmp_path / '''cache'''
      a						:List[Any]										=	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
      a						:Union[str, Any]										=	ParquetDatasetReader(UpperCAmelCase_			,  cache_dir=UpperCAmelCase_						).read()
      _check_parquet_datasetdict(UpperCAmelCase_			,  UpperCAmelCase_			,  splits=list(path.keys()						)						)
      assert all(dataset[split].split == split for split in path.keys()						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		Any			,  UpperCAmelCase_						:		List[Any]						):




      """simple docstring"""





      a						:int										=	ParquetDatasetWriter(UpperCAmelCase_			,  tmp_path / '''foo.parquet'''						)
      assert writer.write() > 0
      a						:Optional[int]										=	pq.ParquetFile(tmp_path / '''foo.parquet'''						)
      a						:Union[str, Any]										=	pf.read()
      assert dataset.data.table == output_table
def     __lowerCamelCase						(			UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		Dict						):




      """simple docstring"""





      a						:str										=	str(shared_datadir / '''test_image_rgb.jpg'''						)
      a						:List[Any]										=	{'''image''': [image_path]}
      a						:List[str]										=	Features({'''image''': Image()}						)
      a						:Optional[Any]										=	Dataset.from_dict(UpperCAmelCase_			,  features=UpperCAmelCase_						)
      a						:Any										=	ParquetDatasetWriter(UpperCAmelCase_			,  tmp_path / '''foo.parquet'''						)
      assert writer.write() > 0

      a						:Any										=	Dataset.from_parquet(str(tmp_path / '''foo.parquet'''						)						)
      assert dataset.features == reloaded_dataset.features

      a						:Tuple										=	ParquetDatasetReader(str(tmp_path / '''foo.parquet'''						)			,  streaming=UpperCAmelCase_						).read()
      assert dataset.features == reloaded_iterable_dataset.features






@pytest.mark.parametrize(
    '''feature, expected'''			,  [
        (Features({'''foo''': Value('''int32'''						)}						), None),
        (Features({'''image''': Image(), '''foo''': Value('''int32'''						)}						), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS),
        (Features({'''nested''': Sequence(Audio()						)}						), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		int			,  UpperCAmelCase_						:		Dict						):




      """simple docstring"""





      assert get_writer_batch_size(UpperCAmelCase_						) == expected









import math
def     __lowerCamelCase						(			UpperCAmelCase_						:		float			,  UpperCAmelCase_						:		float						):




      """simple docstring"""





      return math.pow(UpperCAmelCase_			,  2						) - a
def     __lowerCamelCase						(			UpperCAmelCase_						:		float						):




      """simple docstring"""





      return 2 * x
def     __lowerCamelCase						(			UpperCAmelCase_						:		float						):




      """simple docstring"""





      a						:int										=	2.0

      while start <= a:
            a						:int										=	math.pow(UpperCAmelCase_			,  2						)

      return start
def     __lowerCamelCase						(			UpperCAmelCase_						:		float			,  UpperCAmelCase_						:		int = 9999			,  UpperCAmelCase_						:		float = 0.00000000000001						):




      """simple docstring"""





      if a < 0:
            raise ValueError('''math domain error'''						)

      a						:List[Any]										=	get_initial_point(UpperCAmelCase_						)

      for _ in range(UpperCAmelCase_						):
            a						:Optional[int]										=	value
            a						:int										=	value - fx(UpperCAmelCase_			,  UpperCAmelCase_						) / fx_derivative(UpperCAmelCase_						)
            if abs(prev_value - value						) < tolerance:
                  return value

      return value


if __name__ == "__main__":
     from doctest import testmod

     testmod()





