


'''simple docstring'''







from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


a__  : Dict				=	logging.get_logger(__name__)
class  lowercase_					(   a__       ):
    __UpperCAmelCase             =     ['pixel_values']


    def __init__(					self  , a = True  , a = None  , a = PILImageResampling.BILINEAR  , a = True  , a = 1 / 2_55  , a = True  , a = None  , a = None  , **a  , ):
          super().__init__(**a   )
          UpperCamelCase__  =    size if size is not None else {"height": 2_24, "width": 2_24}
          UpperCamelCase__  =    get_size_dict(a   )
          UpperCamelCase__  =    do_resize
          UpperCamelCase__  =    do_rescale
          UpperCamelCase__  =    do_normalize
          UpperCamelCase__  =    size
          UpperCamelCase__  =    resample
          UpperCamelCase__  =    rescale_factor
          UpperCamelCase__  =    image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
          UpperCamelCase__  =    image_std if image_std is not None else IMAGENET_STANDARD_STD


    def        __a						(					self  , a  , a  , a = PILImageResampling.BILINEAR  , a = None  , **a  , ):
          UpperCamelCase__  =    get_size_dict(a   )
          if "height" not in size or "width" not in size:
                raise ValueError(f'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}'''   )
          return resize(
              a  , size=(size["height"], size["width"])  , resample=a  , data_format=a  , **a   )


    def        __a						(					self  , a  , a  , a = None  , **a   ):
          return rescale(a  , scale=a  , data_format=a  , **a   )


    def        __a						(					self  , a  , a  , a  , a = None  , **a  , ):
          return normalize(a  , mean=a  , std=a  , data_format=a  , **a   )


    def        __a						(					self  , a  , a = None  , a = None  , a = None  , a = None  , a = None  , a = None  , a = None  , a = None  , a = None  , a = ChannelDimension.FIRST  , **a  , ):
          UpperCamelCase__  =    do_resize if do_resize is not None else self.do_resize
          UpperCamelCase__  =    do_rescale if do_rescale is not None else self.do_rescale
          UpperCamelCase__  =    do_normalize if do_normalize is not None else self.do_normalize
          UpperCamelCase__  =    resample if resample is not None else self.resample
          UpperCamelCase__  =    rescale_factor if rescale_factor is not None else self.rescale_factor
          UpperCamelCase__  =    image_mean if image_mean is not None else self.image_mean
          UpperCamelCase__  =    image_std if image_std is not None else self.image_std

          UpperCamelCase__  =    size if size is not None else self.size
          UpperCamelCase__  =    get_size_dict(a   )

          UpperCamelCase__  =    make_list_of_images(a   )

          if not valid_images(a   ):
                raise ValueError(
                    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
                    "torch.Tensor, tf.Tensor or jax.ndarray."   )

          if do_resize and size is None:
                raise ValueError("Size must be specified if do_resize is True."   )

          if do_rescale and rescale_factor is None:
                raise ValueError("Rescale factor must be specified if do_rescale is True."   )

          # All transformations expect numpy arrays.
          UpperCamelCase__  =    [to_numpy_array(a   ) for image in images]

          if do_resize:
                UpperCamelCase__  =    [self.resize(image=a  , size=a  , resample=a   ) for image in images]

          if do_rescale:
                UpperCamelCase__  =    [self.rescale(image=a  , scale=a   ) for image in images]

          if do_normalize:
                UpperCamelCase__  =    [self.normalize(image=a  , mean=a  , std=a   ) for image in images]

          UpperCamelCase__  =    [to_channel_dimension_format(a  , a   ) for image in images]

          UpperCamelCase__  =    {"pixel_values": images}
          return BatchFeature(data=a  , tensor_type=a   )









'''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


a__  : Optional[int]				=	datasets.utils.logging.get_logger(__name__)
@dataclass
class  lowercase_					(   datasets.BuilderConfig       ):
    __UpperCAmelCase             =     None
    __UpperCAmelCase             =     None
    __UpperCAmelCase             =     None
    __UpperCAmelCase             =     True
    __UpperCAmelCase             =     None
    __UpperCAmelCase             =     None
    __UpperCAmelCase             =     None
    __UpperCAmelCase             =     10000
    __UpperCAmelCase             =     None


    def        __a							(					self   ):
          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  , a  , a = None  , ):
          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(a  , a   ):
                if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
                      import sqlalchemy

                      if isinstance(a  , 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(a   )}: {sql}'''   )
                else:
                      raise TypeError(
                          f'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(a   )}: {sql}'''   )
          UpperCamelCase__  =    config_kwargs["con"]
          if not isinstance(a  , a   ):
                UpperCamelCase__  =    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        __a							(					self   ):
          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  lowercase_					(   datasets.ArrowBasedBuilder       ):
    __UpperCAmelCase             =     SqlConfig


    def        __a							(					self   ):
          return datasets.DatasetInfo(features=self.config.features   )


    def        __a							(					self  , a   ):
          return [datasets.SplitGenerator(name=datasets.Split.TRAIN  , gen_kwargs={}   )]


    def        __a							(					self  , a   ):
          if self.config.features is not None:
                UpperCamelCase__  =    self.config.features.arrow_schema
                if all(not require_storage_cast(a   ) for feature in self.config.features.values()   ):
                      # cheaper cast
                      UpperCamelCase__  =    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
                      UpperCamelCase__  =    table_cast(a  , a   )
          return pa_table


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





