




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__       (      _lowercase			,						_lowercase				) ->    Optional[Any]:

     '''simple docstring'''


     assert isinstance(_lowercase			,						_lowercase				)
     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__       (      _lowercase			,						_lowercase			,						_lowercase				) ->    Any:

     '''simple docstring'''


     UpperCAmelCase_      :							Optional[Any]       =      tmp_path / '''cache'''
     UpperCAmelCase_      :							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():
          UpperCAmelCase_      :							Optional[Any]       =      ParquetDatasetReader(_lowercase			,						cache_dir=_lowercase			,						keep_in_memory=_lowercase				).read()
     _check_parquet_dataset(_lowercase			,						_lowercase				)







@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__       (      _lowercase			,						_lowercase			,						_lowercase				) ->    Optional[Any]:

     '''simple docstring'''


     UpperCAmelCase_      :							List[str]       =      tmp_path / '''cache'''
     UpperCAmelCase_      :							Optional[int]       =      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     UpperCAmelCase_      :							Dict       =      features.copy() if features else default_expected_features
     UpperCAmelCase_      :							Any       =      (
         Features({feature: Value(_lowercase				) for feature, dtype in features.items()}				) if features is not None else None
     )
     UpperCAmelCase_      :							List[Any]       =      ParquetDatasetReader(_lowercase			,						features=_lowercase			,						cache_dir=_lowercase				).read()
     _check_parquet_dataset(_lowercase			,						_lowercase				)







@pytest.mark.parametrize('''split'''			,						[None, NamedSplit('''train'''				), '''train''', '''test''']				)
def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase				) ->    Optional[Any]:

     '''simple docstring'''


     UpperCAmelCase_      :							Union[str, Any]       =      tmp_path / '''cache'''
     UpperCAmelCase_      :							Dict       =      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     UpperCAmelCase_      :							Optional[int]       =      ParquetDatasetReader(_lowercase			,						cache_dir=_lowercase			,						split=_lowercase				).read()
     _check_parquet_dataset(_lowercase			,						_lowercase				)
     assert dataset.split == split if split else "train"







@pytest.mark.parametrize('''path_type'''			,						[str, list]				)
def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase				) ->    List[str]:

     '''simple docstring'''


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







def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase=("train",)				) ->    Tuple:

     '''simple docstring'''


     assert isinstance(_lowercase			,						_lowercase				)
     for split in splits:
          UpperCAmelCase_      :							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__       (      _lowercase			,						_lowercase			,						_lowercase				) ->    List[str]:

     '''simple docstring'''


     UpperCAmelCase_      :							List[Any]       =      tmp_path / '''cache'''
     UpperCAmelCase_      :							Union[str, Any]       =      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
          UpperCAmelCase_      :							Optional[int]       =      ParquetDatasetReader(
              {'''train''': parquet_path}			,						cache_dir=_lowercase			,						keep_in_memory=_lowercase				).read()
     _check_parquet_datasetdict(_lowercase			,						_lowercase				)







@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__       (      _lowercase			,						_lowercase			,						_lowercase				) ->    Optional[Any]:

     '''simple docstring'''


     UpperCAmelCase_      :							Any       =      tmp_path / '''cache'''
     UpperCAmelCase_      :							str       =      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     UpperCAmelCase_      :							List[Any]       =      features.copy() if features else default_expected_features
     UpperCAmelCase_      :							int       =      (
         Features({feature: Value(_lowercase				) for feature, dtype in features.items()}				) if features is not None else None
     )
     UpperCAmelCase_      :							Optional[Any]       =      ParquetDatasetReader({'''train''': parquet_path}			,						features=_lowercase			,						cache_dir=_lowercase				).read()
     _check_parquet_datasetdict(_lowercase			,						_lowercase				)







@pytest.mark.parametrize('''split'''			,						[None, NamedSplit('''train'''				), '''train''', '''test''']				)
def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase				) ->    Dict:

     '''simple docstring'''


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







def lowerCamelCase__       (      _lowercase			,						_lowercase				) ->    str:

     '''simple docstring'''


     UpperCAmelCase_      :							Optional[int]       =      ParquetDatasetWriter(_lowercase			,						tmp_path / '''foo.parquet'''				)
     assert writer.write() > 0
     UpperCAmelCase_      :							str       =      pq.ParquetFile(tmp_path / '''foo.parquet'''				)
     UpperCAmelCase_      :							Tuple       =      pf.read()
     assert dataset.data.table == output_table







def lowerCamelCase__       (      _lowercase			,						_lowercase				) ->    Union[str, Any]:

     '''simple docstring'''


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

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

     UpperCAmelCase_      :							int       =      ParquetDatasetReader(str(tmp_path / '''foo.parquet'''				)			,						streaming=_lowercase				).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__       (      _lowercase			,						_lowercase				) ->    Tuple:

     '''simple docstring'''


     assert get_writer_batch_size(_lowercase				) == expected





from math import ceil







def lowerCamelCase__       (      _lowercase = 1001				):

     '''simple docstring'''


     UpperCAmelCase_      :							Optional[Any]       =      1

     for i in range(1			,						int(ceil(n / 2.0				)				)				):
          UpperCAmelCase_      :							List[Any]       =      2 * i + 1
          UpperCAmelCase_      :							Optional[int]       =      2 * i
          UpperCAmelCase_      :							Any       =      total + 4 * odd**2 - 6 * even

     return total


if __name__ == "__main__":
     import sys

     if len(sys.argv) == 1:
          print(solution())
     else:
          try:
               __a          =			int(sys.argv[1])
               print(solution(n))
          except ValueError:
               print('Invalid entry - please enter a number')