




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import VideoMAEImageProcessor






class 	A	(						unittest.TestCase						):
     """simple docstring"""

     def __init__(			self     :      List[str]		,     A__     :      Any		,     A__     :      List[str]=7		,     A__     :      Tuple=3		,     A__     :      List[Any]=1_0		,     A__     :      int=1_8		,     A__     :      Any=3_0		,     A__     :      List[Any]=4_0_0		,     A__     :      Union[str, Any]=True		,     A__     :      str=None		,     A__     :      List[Any]=True		,     A__     :      Union[str, Any]=[0.5, 0.5, 0.5]		,     A__     :      List[Any]=[0.5, 0.5, 0.5]		,     A__     :      Dict=None		,     ) ->				Tuple:
           lowerCamelCase__					     = size if size is not None else {'shortest_edge': 1_8}
           lowerCamelCase__					     = crop_size if crop_size is not None else {'height': 1_8, 'width': 1_8}

           lowerCamelCase__					     = parent
           lowerCamelCase__					     = batch_size
           lowerCamelCase__					     = num_channels
           lowerCamelCase__					     = num_frames
           lowerCamelCase__					     = image_size
           lowerCamelCase__					     = min_resolution
           lowerCamelCase__					     = max_resolution
           lowerCamelCase__					     = do_resize
           lowerCamelCase__					     = size
           lowerCamelCase__					     = do_normalize
           lowerCamelCase__					     = image_mean
           lowerCamelCase__					     = image_std
           lowerCamelCase__					     = crop_size




     def        __snake_case      (			self     :      Any	) ->				str:
           return {
               "image_mean": self.image_mean,
               "image_std": self.image_std,
               "do_normalize": self.do_normalize,
               "do_resize": self.do_resize,
               "size": self.size,
               "crop_size": self.crop_size,
           }






@require_torch
@require_vision
class 	A	(						_lowerCAmelCase	,  unittest.TestCase						):
     """simple docstring"""

     _snake_case:		List[Any]   =VideoMAEImageProcessor if is_vision_available() else None




     def        __snake_case      (			self     :      Union[str, Any]	) ->				Union[str, Any]:
           lowerCamelCase__					     = VideoMAEImageProcessingTester(self	)




     @property
     def        __snake_case      (			self     :      int	) ->				Dict:
           return self.image_processor_tester.prepare_image_processor_dict()




     def        __snake_case      (			self     :      str	) ->				int:
           lowerCamelCase__					     = self.image_processing_class(**self.image_processor_dict	)
           self.assertTrue(hasattr(A__		,     'image_mean'	)	)
           self.assertTrue(hasattr(A__		,     'image_std'	)	)
           self.assertTrue(hasattr(A__		,     'do_normalize'	)	)
           self.assertTrue(hasattr(A__		,     'do_resize'	)	)
           self.assertTrue(hasattr(A__		,     'do_center_crop'	)	)
           self.assertTrue(hasattr(A__		,     'size'	)	)




     def        __snake_case      (			self     :      int	) ->				Union[str, Any]:
           lowerCamelCase__					     = self.image_processing_class.from_dict(self.image_processor_dict	)
           self.assertEqual(image_processor.size		,     {'shortest_edge': 1_8}	)
           self.assertEqual(image_processor.crop_size		,     {'height': 1_8, 'width': 1_8}	)

           lowerCamelCase__					     = self.image_processing_class.from_dict(self.image_processor_dict		,     size=4_2		,     crop_size=8_4	)
           self.assertEqual(image_processor.size		,     {'shortest_edge': 4_2}	)
           self.assertEqual(image_processor.crop_size		,     {'height': 8_4, 'width': 8_4}	)




     def        __snake_case      (			self     :      Optional[int]	) ->				Any:
           pass




     def        __snake_case      (			self     :      Union[str, Any]	) ->				Dict:
           # Initialize image_processing
           lowerCamelCase__					     = self.image_processing_class(**self.image_processor_dict	)
           # create random PIL videos
           lowerCamelCase__					     = prepare_video_inputs(self.image_processor_tester		,     equal_resolution=A__	)
           for video in video_inputs:
                 self.assertIsInstance(A__		,     A__	)
                 self.assertIsInstance(video[0]		,     Image.Image	)

           # Test not batched input
           lowerCamelCase__					     = image_processing(video_inputs[0]		,     return_tensors='pt'	).pixel_values
           self.assertEqual(
               encoded_videos.shape		,     (
                   1,
                   self.image_processor_tester.num_frames,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['height'],
                   self.image_processor_tester.crop_size['width'],
               )		,     )

           # Test batched
           lowerCamelCase__					     = image_processing(A__		,     return_tensors='pt'	).pixel_values
           self.assertEqual(
               encoded_videos.shape		,     (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_frames,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['height'],
                   self.image_processor_tester.crop_size['width'],
               )		,     )




     def        __snake_case      (			self     :      Tuple	) ->				Union[str, Any]:
           # Initialize image_processing
           lowerCamelCase__					     = self.image_processing_class(**self.image_processor_dict	)
           # create random numpy tensors
           lowerCamelCase__					     = prepare_video_inputs(self.image_processor_tester		,     equal_resolution=A__		,     numpify=A__	)
           for video in video_inputs:
                 self.assertIsInstance(A__		,     A__	)
                 self.assertIsInstance(video[0]		,     np.ndarray	)

           # Test not batched input
           lowerCamelCase__					     = image_processing(video_inputs[0]		,     return_tensors='pt'	).pixel_values
           self.assertEqual(
               encoded_videos.shape		,     (
                   1,
                   self.image_processor_tester.num_frames,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['height'],
                   self.image_processor_tester.crop_size['width'],
               )		,     )

           # Test batched
           lowerCamelCase__					     = image_processing(A__		,     return_tensors='pt'	).pixel_values
           self.assertEqual(
               encoded_videos.shape		,     (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_frames,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['height'],
                   self.image_processor_tester.crop_size['width'],
               )		,     )




     def        __snake_case      (			self     :      Optional[int]	) ->				Optional[int]:
           # Initialize image_processing
           lowerCamelCase__					     = self.image_processing_class(**self.image_processor_dict	)
           # create random PyTorch tensors
           lowerCamelCase__					     = prepare_video_inputs(self.image_processor_tester		,     equal_resolution=A__		,     torchify=A__	)
           for video in video_inputs:
                 self.assertIsInstance(A__		,     A__	)
                 self.assertIsInstance(video[0]		,     torch.Tensor	)

           # Test not batched input
           lowerCamelCase__					     = image_processing(video_inputs[0]		,     return_tensors='pt'	).pixel_values
           self.assertEqual(
               encoded_videos.shape		,     (
                   1,
                   self.image_processor_tester.num_frames,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['height'],
                   self.image_processor_tester.crop_size['width'],
               )		,     )

           # Test batched
           lowerCamelCase__					     = image_processing(A__		,     return_tensors='pt'	).pixel_values
           self.assertEqual(
               encoded_videos.shape		,     (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_frames,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['height'],
                   self.image_processor_tester.crop_size['width'],
               )		,     )





import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
      import sqlitea

      import sqlalchemy


__SCREAMING_SNAKE_CASE			:      Union[str, Any]      			=datasets.utils.logging.get_logger(__name__)






@dataclass
class 	A	(						datasets.BuilderConfig						):
     """simple docstring"""

     _snake_case:		Union[str, "sqlalchemy.sql.Selectable"]   =None
     _snake_case:		Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]   =None
     _snake_case:		Optional[Union[str, List[str]]]   =None
     _snake_case:		bool   =True
     _snake_case:		Optional[Union[List, Tuple, Dict]]   =None
     _snake_case:		Optional[Union[List, Dict]]   =None
     _snake_case:		Optional[List[str]]   =None
     _snake_case:		Optional[int]   =10_000
     _snake_case:		Optional[datasets.Features]   =None




     def        __snake_case      (			self     :      Optional[Any]	) ->				Union[str, Any]:
           if self.sql is None:
                 raise ValueError('sql must be specified'	)
           if self.con is None:
                 raise ValueError('con must be specified'	)




     def        __snake_case      (			self     :      List[Any]		,     A__     :      dict		,     A__     :      Optional[datasets.Features] = None		,     ) ->				str:
           lowerCamelCase__					     = config_kwargs.copy()
           # We need to stringify the Selectable object to make its hash deterministic

           # The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
           lowerCamelCase__					     = config_kwargs['sql']
           if not isinstance(A__		,     A__	):
                 if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
                       import sqlalchemy

                       if isinstance(A__		,     sqlalchemy.sql.Selectable	):
                             lowerCamelCase__					     = sqlalchemy.create_engine(config_kwargs['con'].split('://'	)[0] + '://'	)
                             lowerCamelCase__					     = str(sql.compile(dialect=engine.dialect	)	)
                             lowerCamelCase__					     = sql_str
                       else:
                             raise TypeError(
                                 F"""Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(A__	)}: {sql}"""	)
                 else:
                       raise TypeError(
                           F"""Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(A__	)}: {sql}"""	)
           lowerCamelCase__					     = config_kwargs['con']
           if not isinstance(A__		,     A__	):
                 lowerCamelCase__					     = id(A__	)
                 logger.info(
                     F"""SQL connection 'con' of type {type(A__	)} couldn't be hashed properly. To enable hashing, specify 'con' as URI string instead."""	)

           return super().create_config_id(A__		,     custom_features=A__	)




     @property
     def        __snake_case      (			self     :      Any	) ->				Union[str, Any]:
           lowerCamelCase__					     = {
               'index_col': self.index_col,
               'columns': self.columns,
               'params': self.params,
               'coerce_float': self.coerce_float,
               'parse_dates': self.parse_dates,
           }
           return pd_read_sql_kwargs






class 	A	(						datasets.ArrowBasedBuilder						):
     """simple docstring"""

     _snake_case:		List[str]   =SqlConfig




     def        __snake_case      (			self     :      Tuple	) ->				Dict:
           return datasets.DatasetInfo(features=self.config.features	)




     def        __snake_case      (			self     :      List[Any]		,     A__     :      Any	) ->				List[Any]:
           return [datasets.SplitGenerator(name=datasets.Split.TRAIN		,     gen_kwargs={}	)]




     def        __snake_case      (			self     :      Union[str, Any]		,     A__     :      pa.Table	) ->				pa.Table:
           if self.config.features is not None:
                 lowerCamelCase__					     = self.config.features.arrow_schema
                 if all(not require_storage_cast(A__	) for feature in self.config.features.values()	):
                       # cheaper cast
                       lowerCamelCase__					     = pa.Table.from_arrays([pa_table[field.name] for field in schema]		,     schema=A__	)
                 else:
                       # more expensive cast; allows str <-> int/float or str to Audio for example
                       lowerCamelCase__					     = table_cast(A__		,     A__	)
           return pa_table




     def        __snake_case      (			self     :      int	) ->				List[Any]:
           lowerCamelCase__					     = self.config.chunksize
           lowerCamelCase__					     = pd.read_sql(
               self.config.sql		,     self.config.con		,     chunksize=A__		,     **self.config.pd_read_sql_kwargs	)
           lowerCamelCase__					     = [sql_reader] if chunksize is None else sql_reader
           for chunk_idx, df in enumerate(A__	):
                 lowerCamelCase__					     = pa.Table.from_pandas(A__	)
                 yield chunk_idx, self._cast_table(A__	)