
"""simple docstring"""




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


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import SwinaSRImageProcessor
						from transformers.image_transforms import get_image_size



class      lowerCAmelCase_						(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__=4_00       ,	SCREAMING_SNAKE_CASE__=True       ,	SCREAMING_SNAKE_CASE__=1 / 2_55       ,	SCREAMING_SNAKE_CASE__=True       ,	SCREAMING_SNAKE_CASE__=8       ,	) ->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	Dict									=       parent
											SCREAMING_SNAKE_CASE__						:	int									=       batch_size
											SCREAMING_SNAKE_CASE__						:	str									=       num_channels
											SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       image_size
											SCREAMING_SNAKE_CASE__						:	Optional[int]									=       min_resolution
											SCREAMING_SNAKE_CASE__						:	Optional[Any]									=       max_resolution
											SCREAMING_SNAKE_CASE__						:	int									=       do_rescale
											SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       rescale_factor
											SCREAMING_SNAKE_CASE__						:	Optional[Any]									=       do_pad
											SCREAMING_SNAKE_CASE__						:	Optional[int]									=       pad_size



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



											"""simple docstring"""




											return {
											    "do_rescale": self.do_rescale,
											    "rescale_factor": self.rescale_factor,
											    "do_pad": self.do_pad,
											    "pad_size": self.pad_size,
											}



				def      __magic_name__   (self       ,	SCREAMING_SNAKE_CASE__=False       ,	SCREAMING_SNAKE_CASE__=False       ,	SCREAMING_SNAKE_CASE__=False   ) ->		Optional[int]:



											"""simple docstring"""




											assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

											if equal_resolution:
																		SCREAMING_SNAKE_CASE__						:	Tuple									=       []
																		for i in range(self.batch_size   ):
																									image_inputs.append(
																									    np.random.randint(
																									        2_55       ,	size=(self.num_channels, self.max_resolution, self.max_resolution)       ,	dtype=np.uinta   )   )
											else:
																		SCREAMING_SNAKE_CASE__						:	int									=       []
																		for i in range(self.batch_size   ):
																									SCREAMING_SNAKE_CASE__						:	Optional[int]									=       np.random.choice(np.arange(self.min_resolution       ,	self.max_resolution   )       ,	2   )
																									image_inputs.append(np.random.randint(2_55       ,	size=(self.num_channels, width, height)       ,	dtype=np.uinta   )   )

											if not numpify and not torchify:
																		# PIL expects the channel dimension as last dimension
																		SCREAMING_SNAKE_CASE__						:	Optional[int]									=       [Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE__       ,	0       ,	-1   )   ) for x in image_inputs]

											if torchify:
																		SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       [torch.from_numpy(SCREAMING_SNAKE_CASE__   ) for x in image_inputs]

											return image_inputs






@require_torch
@require_vision
class      lowerCAmelCase_						(a__			, unittest.TestCase  ):


				"""simple docstring"""



				__UpperCamelCase				:						int 	=       SwinaSRImageProcessor if is_vision_available() else None



				def      __magic_name__   (self   ) ->		Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       SwinaSRImageProcessingTester(self   )



				@property
				def      __magic_name__   (self   ) ->		Tuple:



											"""simple docstring"""




											return self.image_processor_tester.prepare_image_processor_dict()



				def      __magic_name__   (self   ) ->		Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	str									=       self.image_processing_class(**self.image_processor_dict   )
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__       ,	"""do_rescale"""   )   )
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__       ,	"""rescale_factor"""   )   )
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__       ,	"""do_pad"""   )   )
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__       ,	"""pad_size"""   )   )



				def      __magic_name__   (self   ) ->		Any:



											"""simple docstring"""




											pass



				def      __magic_name__   (self       ,	SCREAMING_SNAKE_CASE__   ) ->		Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       get_image_size(SCREAMING_SNAKE_CASE__   )
											SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       self.image_processor_tester.pad_size

											SCREAMING_SNAKE_CASE__						:	List[str]									=       (old_height // size + 1) * size - old_height
											SCREAMING_SNAKE_CASE__						:	Optional[Any]									=       (old_width // size + 1) * size - old_width
											return old_height + pad_height, old_width + pad_width



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	List[Any]									=       self.image_processing_class(**self.image_processor_dict   )
											# create random PIL images
											SCREAMING_SNAKE_CASE__						:	int									=       self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE__   )
											for image in image_inputs:
																		self.assertIsInstance(SCREAMING_SNAKE_CASE__       ,	Image.Image   )

											# Test not batched input
											SCREAMING_SNAKE_CASE__						:	Dict									=       image_processor(image_inputs[0]       ,	return_tensors="""pt"""   ).pixel_values
											SCREAMING_SNAKE_CASE__						:	Dict									=       self.calculate_expected_size(np.array(image_inputs[0]   )   )
											self.assertEqual(
											    encoded_images.shape       ,	(
											        1,
											        self.image_processor_tester.num_channels,
											        expected_height,
											        expected_width,
											    )       ,	)



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	Dict									=       self.image_processing_class(**self.image_processor_dict   )
											# create random numpy tensors
											SCREAMING_SNAKE_CASE__						:	List[str]									=       self.image_processor_tester.prepare_inputs(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
											SCREAMING_SNAKE_CASE__						:	Dict									=       image_processor(image_inputs[0]       ,	return_tensors="""pt"""   ).pixel_values
											SCREAMING_SNAKE_CASE__						:	List[Any]									=       self.calculate_expected_size(image_inputs[0]   )
											self.assertEqual(
											    encoded_images.shape       ,	(
											        1,
											        self.image_processor_tester.num_channels,
											        expected_height,
											        expected_width,
											    )       ,	)



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	List[str]									=       self.image_processing_class(**self.image_processor_dict   )
											# create random PyTorch tensors
											SCREAMING_SNAKE_CASE__						:	List[str]									=       self.image_processor_tester.prepare_inputs(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
											SCREAMING_SNAKE_CASE__						:	Dict									=       image_processor(image_inputs[0]       ,	return_tensors="""pt"""   ).pixel_values
											SCREAMING_SNAKE_CASE__						:	Optional[int]									=       self.calculate_expected_size(image_inputs[0]   )
											self.assertEqual(
											    encoded_images.shape       ,	(
											        1,
											        self.image_processor_tester.num_channels,
											        expected_height,
											        expected_width,
											    )       ,	)






"""simple docstring"""




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


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



@dataclass
class      lowerCAmelCase_						(datasets.BuilderConfig  ):


				"""simple docstring"""



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



				def      __magic_name__   (self   ) ->		Dict:



											"""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      __magic_name__   (self       ,	SCREAMING_SNAKE_CASE__       ,	SCREAMING_SNAKE_CASE__ = None       ,	) ->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	List[Any]									=       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
											SCREAMING_SNAKE_CASE__						:	Dict									=       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   ):
																																SCREAMING_SNAKE_CASE__						:	str									=       sqlalchemy.create_engine(config_kwargs["""con"""].split("""://"""   )[0] + """://"""   )
																																SCREAMING_SNAKE_CASE__						:	Tuple									=       str(sql.compile(dialect=engine.dialect   )   )
																																SCREAMING_SNAKE_CASE__						:	Any									=       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}'''   )
											SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       config_kwargs["""con"""]
											if not isinstance(SCREAMING_SNAKE_CASE__       ,	SCREAMING_SNAKE_CASE__   ):
																		SCREAMING_SNAKE_CASE__						:	Optional[Any]									=       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      __magic_name__   (self   ) ->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	Optional[int]									=       {
											    """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      lowerCAmelCase_						(datasets.ArrowBasedBuilder  ):


				"""simple docstring"""



				__UpperCamelCase				:						List[str] 	=       SqlConfig



				def      __magic_name__   (self   ) ->		List[Any]:



											"""simple docstring"""




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



				def      __magic_name__   (self       ,	SCREAMING_SNAKE_CASE__   ) ->		List[str]:



											"""simple docstring"""




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



				def      __magic_name__   (self       ,	SCREAMING_SNAKE_CASE__   ) ->		pa.Table:



											"""simple docstring"""




											if self.config.features is not None:
																		SCREAMING_SNAKE_CASE__						:	List[str]									=       self.config.features.arrow_schema
																		if all(not require_storage_cast(SCREAMING_SNAKE_CASE__   ) for feature in self.config.features.values()   ):
																									# cheaper cast
																									SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       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
																									SCREAMING_SNAKE_CASE__						:	Optional[int]									=       table_cast(SCREAMING_SNAKE_CASE__       ,	SCREAMING_SNAKE_CASE__   )
											return pa_table



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



											"""simple docstring"""




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




