
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


lowercase__				:				Tuple						=      datasets.utils.logging.get_logger(__name__)






@dataclass
class 							SCREAMING_SNAKE_CASE__     (				datasets.BuilderConfig     ):

							"""simple docstring"""






							_snake_case  		=	None
							_snake_case  		=	None
							_snake_case  		=	None
							_snake_case  		=	True
							_snake_case  		=	None
							_snake_case  		=	None
							_snake_case  		=	None
							_snake_case  		=	10000
							_snake_case  		=	None



							def      A__		(	self      )-> Tuple:





													'''simple docstring'''






													if self.sql is None:
																			raise ValueError('''sql must be specified'''      )
													if self.con is None:
																			raise ValueError('''con must be specified'''      )



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_ = None  ,						)-> str:





													'''simple docstring'''






													__UpperCamelCase  	=      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
													__UpperCamelCase  	=      config_kwargs['''sql''']
													if not isinstance(SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_      ):
																			if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
																									import sqlalchemy

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

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



							@property
							def      A__		(	self      )-> Tuple:





													'''simple docstring'''






													__UpperCamelCase  	=      {
													    '''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 							SCREAMING_SNAKE_CASE__     (				datasets.ArrowBasedBuilder     ):

							"""simple docstring"""






							_snake_case  		=	SqlConfig



							def      A__		(	self      )-> Optional[int]:





													'''simple docstring'''






													return datasets.DatasetInfo(features=self.config.features      )



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_      )-> List[Any]:





													'''simple docstring'''






													return [datasets.SplitGenerator(name=datasets.Split.TRAIN  ,						gen_kwargs={}      )]



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_      )-> pa.Table:





													'''simple docstring'''






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



							def      A__		(	self      )-> Dict:





													'''simple docstring'''






													__UpperCamelCase  	=      self.config.chunksize
													__UpperCamelCase  	=      pd.read_sql(
													    self.config.sql  ,						self.config.con  ,						chunksize=SCREAMING_SNAKE_CASE_  ,						**self.config.pd_read_sql_kwargs      )
													__UpperCamelCase  	=      [sql_reader] if chunksize is None else sql_reader
													for chunk_idx, df in enumerate(SCREAMING_SNAKE_CASE_      ):
																			__UpperCamelCase  	=      pa.Table.from_pandas(SCREAMING_SNAKE_CASE_      )
																			yield chunk_idx, self._cast_table(SCREAMING_SNAKE_CASE_      )






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_image_inputs


if is_torch_available():
				import torch

if is_vision_available():
				from PIL import Image

				from transformers import DeiTImageProcessor






class 							SCREAMING_SNAKE_CASE__     (				unittest.TestCase     ):

							"""simple docstring"""






							def __init__(	self  ,						SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_=7  ,						SCREAMING_SNAKE_CASE_=3  ,						SCREAMING_SNAKE_CASE_=18  ,						SCREAMING_SNAKE_CASE_=30  ,						SCREAMING_SNAKE_CASE_=400  ,						SCREAMING_SNAKE_CASE_=True  ,						SCREAMING_SNAKE_CASE_=None  ,						SCREAMING_SNAKE_CASE_=True  ,						SCREAMING_SNAKE_CASE_=None  ,						SCREAMING_SNAKE_CASE_=True  ,						SCREAMING_SNAKE_CASE_=[0.5, 0.5, 0.5]  ,						SCREAMING_SNAKE_CASE_=[0.5, 0.5, 0.5]  ,						)-> List[str]:





													'''simple docstring'''






													__UpperCamelCase  	=      size if size is not None else {'''height''': 20, '''width''': 20}
													__UpperCamelCase  	=      crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

													__UpperCamelCase  	=      parent
													__UpperCamelCase  	=      batch_size
													__UpperCamelCase  	=      num_channels
													__UpperCamelCase  	=      image_size
													__UpperCamelCase  	=      min_resolution
													__UpperCamelCase  	=      max_resolution
													__UpperCamelCase  	=      do_resize
													__UpperCamelCase  	=      size
													__UpperCamelCase  	=      do_center_crop
													__UpperCamelCase  	=      crop_size
													__UpperCamelCase  	=      do_normalize
													__UpperCamelCase  	=      image_mean
													__UpperCamelCase  	=      image_std



							def      A__		(	self      )-> int:





													'''simple docstring'''






													return {
													    "do_resize": self.do_resize,
													    "size": self.size,
													    "do_center_crop": self.do_center_crop,
													    "crop_size": self.crop_size,
													    "do_normalize": self.do_normalize,
													    "image_mean": self.image_mean,
													    "image_std": self.image_std,
													}






@require_torch
@require_vision
class 							SCREAMING_SNAKE_CASE__     (				SCREAMING_SNAKE_CASE_ ,		unittest.TestCase     ):

							"""simple docstring"""






							_snake_case  		=	DeiTImageProcessor if is_vision_available() else None
							_snake_case  		=	True



							def      A__		(	self      )-> int:





													'''simple docstring'''






													__UpperCamelCase  	=      DeiTImageProcessingTester(self      )



							@property
							def      A__		(	self      )-> Any:





													'''simple docstring'''






													return self.image_processor_tester.prepare_image_processor_dict()



							def      A__		(	self      )-> Optional[Any]:





													'''simple docstring'''






													__UpperCamelCase  	=      self.image_processing_class(**self.image_processor_dict      )
													self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_  ,						'''do_resize'''      )      )
													self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_  ,						'''size'''      )      )
													self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_  ,						'''do_center_crop'''      )      )
													self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_  ,						'''center_crop'''      )      )
													self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_  ,						'''do_normalize'''      )      )
													self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_  ,						'''image_mean'''      )      )
													self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_  ,						'''image_std'''      )      )



							def      A__		(	self      )-> int:





													'''simple docstring'''






													__UpperCamelCase  	=      self.image_processing_class.from_dict(self.image_processor_dict      )
													self.assertEqual(image_processor.size  ,						{'''height''': 20, '''width''': 20}      )
													self.assertEqual(image_processor.crop_size  ,						{'''height''': 18, '''width''': 18}      )

													__UpperCamelCase  	=      self.image_processing_class.from_dict(self.image_processor_dict  ,						size=42  ,						crop_size=84      )
													self.assertEqual(image_processor.size  ,						{'''height''': 42, '''width''': 42}      )
													self.assertEqual(image_processor.crop_size  ,						{'''height''': 84, '''width''': 84}      )



							def      A__		(	self      )-> Union[str, Any]:





													'''simple docstring'''






													pass



							def      A__		(	self      )-> str:





													'''simple docstring'''






													__UpperCamelCase  	=      self.image_processing_class(**self.image_processor_dict      )
													# create random PIL images
													__UpperCamelCase  	=      prepare_image_inputs(self.image_processor_tester  ,						equal_resolution=SCREAMING_SNAKE_CASE_      )
													for image in image_inputs:
																			self.assertIsInstance(SCREAMING_SNAKE_CASE_  ,						Image.Image      )

													# Test not batched input
													__UpperCamelCase  	=      image_processing(image_inputs[0]  ,						return_tensors='''pt'''      ).pixel_values
													self.assertEqual(
													    encoded_images.shape  ,						(
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )  ,						)

													# Test batched
													__UpperCamelCase  	=      image_processing(SCREAMING_SNAKE_CASE_  ,						return_tensors='''pt'''      ).pixel_values
													self.assertEqual(
													    encoded_images.shape  ,						(
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )  ,						)



							def      A__		(	self      )-> List[str]:





													'''simple docstring'''






													__UpperCamelCase  	=      self.image_processing_class(**self.image_processor_dict      )
													# create random numpy tensors
													__UpperCamelCase  	=      prepare_image_inputs(self.image_processor_tester  ,						equal_resolution=SCREAMING_SNAKE_CASE_  ,						numpify=SCREAMING_SNAKE_CASE_      )
													for image in image_inputs:
																			self.assertIsInstance(SCREAMING_SNAKE_CASE_  ,						np.ndarray      )

													# Test not batched input
													__UpperCamelCase  	=      image_processing(image_inputs[0]  ,						return_tensors='''pt'''      ).pixel_values
													self.assertEqual(
													    encoded_images.shape  ,						(
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )  ,						)

													# Test batched
													__UpperCamelCase  	=      image_processing(SCREAMING_SNAKE_CASE_  ,						return_tensors='''pt'''      ).pixel_values
													self.assertEqual(
													    encoded_images.shape  ,						(
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )  ,						)



							def      A__		(	self      )-> str:





													'''simple docstring'''






													__UpperCamelCase  	=      self.image_processing_class(**self.image_processor_dict      )
													# create random PyTorch tensors
													__UpperCamelCase  	=      prepare_image_inputs(self.image_processor_tester  ,						equal_resolution=SCREAMING_SNAKE_CASE_  ,						torchify=SCREAMING_SNAKE_CASE_      )
													for image in image_inputs:
																			self.assertIsInstance(SCREAMING_SNAKE_CASE_  ,						torch.Tensor      )

													# Test not batched input
													__UpperCamelCase  	=      image_processing(image_inputs[0]  ,						return_tensors='''pt'''      ).pixel_values
													self.assertEqual(
													    encoded_images.shape  ,						(
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )  ,						)

													# Test batched
													__UpperCamelCase  	=      image_processing(SCREAMING_SNAKE_CASE_  ,						return_tensors='''pt'''      ).pixel_values
													self.assertEqual(
													    encoded_images.shape  ,						(
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )  ,						)




