




import importlib
import inspect
from functools import wraps
from typing import TYPE_CHECKING, Optional, Union

from .download.streaming_download_manager import (
    xbasename,
    xdirname,
    xet_parse,
    xexists,
    xgetsize,
    xglob,
    xgzip_open,
    xisdir,
    xisfile,
    xjoin,
    xlistdir,
    xnumpy_load,
    xopen,
    xpandas_read_csv,
    xpandas_read_excel,
    xPath,
    xrelpath,
    xsio_loadmat,
    xsplit,
    xsplitext,
    xwalk,
    xxml_dom_minidom_parse,
)
from .utils.logging import get_logger
from .utils.patching import patch_submodule
from .utils.py_utils import get_imports


_UpperCAmelCase						:    Tuple      =  get_logger(__name__)


if TYPE_CHECKING:
     from .builder import DatasetBuilder



def    UpperCAmelCase__	(  lowerCamelCase,      lowerCamelCase = None ):
  lowercase     :Optional[Any]   						=    importlib.import_module(lowerCamelCase )

  # TODO(QL): always update the module to add subsequent new authentication
  if hasattr(lowerCamelCase,      "_patched_for_streaming" ) and module._patched_for_streaming:
    return

  def wrap_auth(lowerCamelCase ):
    @wraps(lowerCamelCase )
    def wrapper(*lowerCamelCase,      **lowerCamelCase ):
      return function(*lowerCamelCase,      use_auth_token=lowerCamelCase,      **lowerCamelCase )

    lowercase     :List[Any]   						=    "wrap_auth"
    return wrapper

  # open files in a streaming fashion
  patch_submodule(lowerCamelCase,      "open",      wrap_auth(lowerCamelCase ) ).start()
  patch_submodule(lowerCamelCase,      "os.listdir",      wrap_auth(lowerCamelCase ) ).start()
  patch_submodule(lowerCamelCase,      "os.walk",      wrap_auth(lowerCamelCase ) ).start()
  patch_submodule(lowerCamelCase,      "glob.glob",      wrap_auth(lowerCamelCase ) ).start()
  # allow to navigate in remote zip files
  patch_submodule(lowerCamelCase,      "os.path.join",      lowerCamelCase ).start()
  patch_submodule(lowerCamelCase,      "os.path.dirname",      lowerCamelCase ).start()
  patch_submodule(lowerCamelCase,      "os.path.basename",      lowerCamelCase ).start()
  patch_submodule(lowerCamelCase,      "os.path.relpath",      lowerCamelCase ).start()
  patch_submodule(lowerCamelCase,      "os.path.split",      lowerCamelCase ).start()
  patch_submodule(lowerCamelCase,      "os.path.splitext",      lowerCamelCase ).start()
  # allow checks on paths
  patch_submodule(lowerCamelCase,      "os.path.exists",      wrap_auth(lowerCamelCase ) ).start()
  patch_submodule(lowerCamelCase,      "os.path.isdir",      wrap_auth(lowerCamelCase ) ).start()
  patch_submodule(lowerCamelCase,      "os.path.isfile",      wrap_auth(lowerCamelCase ) ).start()
  patch_submodule(lowerCamelCase,      "os.path.getsize",      wrap_auth(lowerCamelCase ) ).start()
  patch_submodule(lowerCamelCase,      "pathlib.Path",      lowerCamelCase ).start()
  # file readers
  patch_submodule(lowerCamelCase,      "gzip.open",      wrap_auth(lowerCamelCase ) ).start()
  patch_submodule(lowerCamelCase,      "numpy.load",      wrap_auth(lowerCamelCase ) ).start()
  patch_submodule(lowerCamelCase,      "pandas.read_csv",      wrap_auth(lowerCamelCase ),      attrs=["__version__"] ).start()
  patch_submodule(lowerCamelCase,      "pandas.read_excel",      wrap_auth(lowerCamelCase ),      attrs=["__version__"] ).start()
  patch_submodule(lowerCamelCase,      "scipy.io.loadmat",      wrap_auth(lowerCamelCase ),      attrs=["__version__"] ).start()
  patch_submodule(lowerCamelCase,      "xml.etree.ElementTree.parse",      wrap_auth(lowerCamelCase ) ).start()
  patch_submodule(lowerCamelCase,      "xml.dom.minidom.parse",      wrap_auth(lowerCamelCase ) ).start()
  lowercase     :Union[str, Any]   						=    True







def    UpperCAmelCase__	(  lowerCamelCase ):
  extend_module_for_streaming(builder.__module__,      use_auth_token=builder.use_auth_token )
  # if needed, we also have to extend additional internal imports (like wmt14 -> wmt_utils)
  if not builder.__module__.startswith("datasets." ):  # check that it's not a packaged builder like csv
    for imports in get_imports(inspect.getfile(builder.__class__ ) ):
      if imports[0] == "internal":
        lowercase     :Any   						=    imports[1]
        lowercase     :Any   						=    ".".join(builder.__module__.split("." )[:-1] + [internal_import_name] )
        extend_module_for_streaming(lowerCamelCase,      use_auth_token=builder.use_auth_token )

    # builders can inherit from other builders that might use streaming functionality
    # (for example, ImageFolder and AudioFolder inherit from FolderBuilder which implements examples generation)
    # but these parents builders are not patched automatically as they are not instantiated, so we patch them here
  from .builder import DatasetBuilder

  lowercase     :Union[str, Any]   						=    [
      cls.__module__
      for cls in type(lowerCamelCase ).__mro__[1:]  # make sure it's not the same module we've already patched
      if issubclass(cls,      lowerCamelCase ) and cls.__module__ != DatasetBuilder.__module__
  ]  # check it's not a standard builder from datasets.builder
  for module in parent_builder_modules:
    extend_module_for_streaming(lowerCamelCase,      use_auth_token=builder.use_auth_token )









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 (
    get_resize_output_image_size,
    normalize,
    pad,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging
from ...utils.import_utils import is_vision_available


_UpperCAmelCase						:    Optional[Any]      =  logging.get_logger(__name__)


if is_vision_available():
     import PIL




class  __lowerCAmelCase							(				lowerCAmelCase):
       _a			       =    ['''pixel_values''']






       def __init__( self:       Optional[int]							,       _lowerCAmelCase:       bool = True							,       _lowerCAmelCase:       Dict[str, int] = None							,       _lowerCAmelCase:       PILImageResampling = PILImageResampling.BILINEAR							,       _lowerCAmelCase:       bool = True							,       _lowerCAmelCase:       bool = False							,       _lowerCAmelCase:       bool = True							,       _lowerCAmelCase:       bool = True							,       _lowerCAmelCase:       Union[int, float] = 1 / 2_55							,       _lowerCAmelCase:       bool = True							,       _lowerCAmelCase:       Optional[Union[float, List[float]]] = None							,       _lowerCAmelCase:       Optional[Union[float, List[float]]] = None							,       **_lowerCAmelCase:       Dict							,       ):
         super().__init__(**_lowerCAmelCase					)

         lowercase     :Tuple   						=    size if size is not None else {"height": 25_60, "width": 19_20}
         if isinstance(_lowerCAmelCase							,       (tuple, list)					):
           # The previous feature extractor size parameter was in (width, height) format
           lowercase     :Union[str, Any]   						=    size[::-1]
         lowercase     :int   						=    get_size_dict(_lowerCAmelCase					)

         lowercase     :Optional[Any]   						=    do_resize
         lowercase     :int   						=    size
         lowercase     :List[str]   						=    resample
         lowercase     :Any   						=    do_thumbnail
         lowercase     :Optional[int]   						=    do_align_long_axis
         lowercase     :Tuple   						=    do_pad
         lowercase     :Union[str, Any]   						=    do_rescale
         lowercase     :Union[str, Any]   						=    rescale_factor
         lowercase     :Optional[int]   						=    do_normalize
         lowercase     :Dict   						=    image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
         lowercase     :Union[str, Any]   						=    image_std if image_std is not None else IMAGENET_STANDARD_STD






       def        SCREAMING_SNAKE_CASE     ( self:       int							,       _lowerCAmelCase:       np.ndarray							,       _lowerCAmelCase:       Dict[str, int]							,       _lowerCAmelCase:       Optional[Union[str, ChannelDimension]] = None					):
         lowercase						,     lowercase     :int   						=    get_image_size(_lowerCAmelCase					)
         lowercase						,     lowercase     :str   						=    size["height"], size["width"]

         if (output_width < output_height and input_width > input_height) or (
             output_width > output_height and input_width < input_height
         ):
           lowercase     :Any   						=    np.rotaa(_lowerCAmelCase							,       3					)

         if data_format is not None:
           lowercase     :Optional[int]   						=    to_channel_dimension_format(_lowerCAmelCase							,       _lowerCAmelCase					)

         return image






       def        SCREAMING_SNAKE_CASE     ( self:       Optional[int]							,       _lowerCAmelCase:       np.ndarray							,       _lowerCAmelCase:       Dict[str, int]							,       _lowerCAmelCase:       bool = False							,       _lowerCAmelCase:       Optional[Union[str, ChannelDimension]] = None							,       ):
         lowercase						,     lowercase     :Tuple   						=    size["height"], size["width"]
         lowercase						,     lowercase     :Optional[int]   						=    get_image_size(_lowerCAmelCase					)

         lowercase     :List[Any]   						=    output_width - input_width
         lowercase     :Dict   						=    output_height - input_height

         if random_padding:
           lowercase     :List[str]   						=    np.random.randint(low=0							,       high=delta_height + 1					)
           lowercase     :List[Any]   						=    np.random.randint(low=0							,       high=delta_width + 1					)
         else:
           lowercase     :Dict   						=    delta_height // 2
           lowercase     :int   						=    delta_width // 2

         lowercase     :Optional[int]   						=    delta_height - pad_top
         lowercase     :Dict   						=    delta_width - pad_left

         lowercase     :Optional[Any]   						=    ((pad_top, pad_bottom), (pad_left, pad_right))
         return pad(_lowerCAmelCase							,       _lowerCAmelCase							,       data_format=_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       str							,       *_lowerCAmelCase:       List[str]							,       **_lowerCAmelCase:       List[str]					):
         logger.info("pad is deprecated and will be removed in version 4.27. Please use pad_image instead."					)
         return self.pad_image(*_lowerCAmelCase							,       **_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]							,       _lowerCAmelCase:       np.ndarray							,       _lowerCAmelCase:       Dict[str, int]							,       _lowerCAmelCase:       PILImageResampling = PILImageResampling.BICUBIC							,       _lowerCAmelCase:       Optional[Union[str, ChannelDimension]] = None							,       **_lowerCAmelCase:       int							,       ):
         lowercase						,     lowercase     :Any   						=    get_image_size(_lowerCAmelCase					)
         lowercase						,     lowercase     :List[str]   						=    size["height"], size["width"]

         # We always resize to the smallest of either the input or output size.
         lowercase     :Union[str, Any]   						=    min(_lowerCAmelCase							,       _lowerCAmelCase					)
         lowercase     :Any   						=    min(_lowerCAmelCase							,       _lowerCAmelCase					)

         if height == input_height and width == input_width:
           return image

         if input_height > input_width:
           lowercase     :Union[str, Any]   						=    int(input_width * height / input_height					)
         elif input_width > input_height:
           lowercase     :int   						=    int(input_height * width / input_width					)

         return resize(
             _lowerCAmelCase							,       size=(height, width)							,       resample=_lowerCAmelCase							,       reducing_gap=2.0							,       data_format=_lowerCAmelCase							,       **_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       int							,       _lowerCAmelCase:       np.ndarray							,       _lowerCAmelCase:       Dict[str, int]							,       _lowerCAmelCase:       PILImageResampling = PILImageResampling.BICUBIC							,       _lowerCAmelCase:       Optional[Union[str, ChannelDimension]] = None							,       **_lowerCAmelCase:       str							,       ):
         lowercase     :Optional[Any]   						=    get_size_dict(_lowerCAmelCase					)
         lowercase     :Optional[Any]   						=    min(size["height"]							,       size["width"]					)
         lowercase     :Tuple   						=    get_resize_output_image_size(_lowerCAmelCase							,       size=_lowerCAmelCase							,       default_to_square=_lowerCAmelCase					)
         lowercase     :Optional[int]   						=    resize(_lowerCAmelCase							,       size=_lowerCAmelCase							,       resample=_lowerCAmelCase							,       data_format=_lowerCAmelCase							,       **_lowerCAmelCase					)
         return resized_image






       def        SCREAMING_SNAKE_CASE     ( self:       Dict							,       _lowerCAmelCase:       np.ndarray							,       _lowerCAmelCase:       Union[int, float]							,       _lowerCAmelCase:       Optional[Union[str, ChannelDimension]] = None							,       **_lowerCAmelCase:       List[Any]							,       ):
         return rescale(_lowerCAmelCase							,       scale=_lowerCAmelCase							,       data_format=_lowerCAmelCase							,       **_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       Any							,       _lowerCAmelCase:       np.ndarray							,       _lowerCAmelCase:       Union[float, List[float]]							,       _lowerCAmelCase:       Union[float, List[float]]							,       _lowerCAmelCase:       Optional[Union[str, ChannelDimension]] = None							,       **_lowerCAmelCase:       Dict							,       ):
         return normalize(_lowerCAmelCase							,       mean=_lowerCAmelCase							,       std=_lowerCAmelCase							,       data_format=_lowerCAmelCase							,       **_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple							,       _lowerCAmelCase:       ImageInput							,       _lowerCAmelCase:       bool = None							,       _lowerCAmelCase:       Dict[str, int] = None							,       _lowerCAmelCase:       PILImageResampling = None							,       _lowerCAmelCase:       bool = None							,       _lowerCAmelCase:       bool = None							,       _lowerCAmelCase:       bool = None							,       _lowerCAmelCase:       bool = False							,       _lowerCAmelCase:       bool = None							,       _lowerCAmelCase:       float = None							,       _lowerCAmelCase:       bool = None							,       _lowerCAmelCase:       Optional[Union[float, List[float]]] = None							,       _lowerCAmelCase:       Optional[Union[float, List[float]]] = None							,       _lowerCAmelCase:       Optional[Union[str, TensorType]] = None							,       _lowerCAmelCase:       Optional[ChannelDimension] = ChannelDimension.FIRST							,       **_lowerCAmelCase:       Optional[Any]							,       ):
         lowercase     :Union[str, Any]   						=    do_resize if do_resize is not None else self.do_resize
         lowercase     :Optional[int]   						=    size if size is not None else self.size
         if isinstance(_lowerCAmelCase							,       (tuple, list)					):
           # Previous feature extractor had size in (width, height) format
           lowercase     :List[str]   						=    size[::-1]
         lowercase     :List[str]   						=    get_size_dict(_lowerCAmelCase					)
         lowercase     :Optional[int]   						=    resample if resample is not None else self.resample
         lowercase     :Union[str, Any]   						=    do_thumbnail if do_thumbnail is not None else self.do_thumbnail
         lowercase     :str   						=    do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
         lowercase     :int   						=    do_pad if do_pad is not None else self.do_pad
         lowercase     :Optional[Any]   						=    do_rescale if do_rescale is not None else self.do_rescale
         lowercase     :str   						=    rescale_factor if rescale_factor is not None else self.rescale_factor
         lowercase     :int   						=    do_normalize if do_normalize is not None else self.do_normalize
         lowercase     :Dict   						=    image_mean if image_mean is not None else self.image_mean
         lowercase     :str   						=    image_std if image_std is not None else self.image_std

         lowercase     :Any   						=    make_list_of_images(_lowerCAmelCase					)

         if not valid_images(_lowerCAmelCase					):
           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."					)

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

         if do_normalize and (image_mean is None or image_std is None):
           raise ValueError("Image mean and std must be specified if do_normalize is True."					)

         # All transformations expect numpy arrays.
         lowercase     :Union[str, Any]   						=    [to_numpy_array(_lowerCAmelCase					) for image in images]

         if do_align_long_axis:
           lowercase     :Union[str, Any]   						=    [self.align_long_axis(_lowerCAmelCase							,       size=_lowerCAmelCase					) for image in images]

         if do_resize:
           lowercase     :Optional[Any]   						=    [self.resize(image=_lowerCAmelCase							,       size=_lowerCAmelCase							,       resample=_lowerCAmelCase					) for image in images]

         if do_thumbnail:
           lowercase     :str   						=    [self.thumbnail(image=_lowerCAmelCase							,       size=_lowerCAmelCase					) for image in images]

         if do_pad:
           lowercase     :List[Any]   						=    [self.pad_image(image=_lowerCAmelCase							,       size=_lowerCAmelCase							,       random_padding=_lowerCAmelCase					) for image in images]

         if do_rescale:
           lowercase     :Optional[Any]   						=    [self.rescale(image=_lowerCAmelCase							,       scale=_lowerCAmelCase					) for image in images]

         if do_normalize:
           lowercase     :Tuple   						=    [self.normalize(image=_lowerCAmelCase							,       mean=_lowerCAmelCase							,       std=_lowerCAmelCase					) for image in images]

         lowercase     :List[str]   						=    [to_channel_dimension_format(_lowerCAmelCase							,       _lowerCAmelCase					) for image in images]

         lowercase     :List[str]   						=    {"pixel_values": images}
         return BatchFeature(data=_lowerCAmelCase							,       tensor_type=_lowerCAmelCase					)



