
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 CLIPImageProcessor

class 		lowercase_       (       unittest.TestCase				):

 def __init__(      self     ,	__UpperCamelCase     ,	__UpperCamelCase=7     ,	__UpperCamelCase=3     ,	__UpperCamelCase=1_8     ,	__UpperCamelCase=3_0     ,	__UpperCamelCase=4_0_0     ,	__UpperCamelCase=True     ,	__UpperCamelCase=None     ,	__UpperCamelCase=True     ,	__UpperCamelCase=None     ,	__UpperCamelCase=True     ,	__UpperCamelCase=[0.48_145_466, 0.4_578_275, 0.40_821_073]     ,	__UpperCamelCase=[0.26_862_954, 0.26_130_258, 0.27_577_711]     ,	__UpperCamelCase=True     ,	):




        """simple docstring"""


        UpperCamelCase_							       =       size if size is not None else {"""shortest_edge""": 2_0}
        UpperCamelCase_							       =       crop_size if crop_size is not None else {"""height""": 1_8, """width""": 1_8}
        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
        UpperCamelCase_							       =       do_convert_rgb

 def lowerCamelCase_				(      self     ):




        """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,
            "do_convert_rgb": self.do_convert_rgb,
        }

 def lowerCamelCase_				(      self     ,	__UpperCamelCase=False     ,	__UpperCamelCase=False     ,	__UpperCamelCase=False     ):




        """simple docstring"""


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

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

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

        if torchify:
               UpperCamelCase_							       =       [torch.from_numpy(__UpperCamelCase     ) for x in image_inputs]

        return image_inputs



@require_torch
@require_vision
class 		lowercase_       (       __SCREAMING_SNAKE_CASE							,	unittest.TestCase				):
 A__						:				Any         =    CLIPImageProcessor if is_vision_available() else None

 def lowerCamelCase_				(      self     ):




        """simple docstring"""


        UpperCamelCase_							       =       CLIPImageProcessingTester(self     )

 @property
 def lowerCamelCase_				(      self     ):




        """simple docstring"""


        return self.image_processor_tester.prepare_image_processor_dict()

 def lowerCamelCase_				(      self     ):




        """simple docstring"""


        UpperCamelCase_							       =       self.image_processing_class(**self.image_processor_dict     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""do_resize"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""size"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""do_center_crop"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""center_crop"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""do_normalize"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""image_mean"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""image_std"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""do_convert_rgb"""     )     )

 def lowerCamelCase_				(      self     ):




        """simple docstring"""


        UpperCamelCase_							       =       self.image_processing_class.from_dict(self.image_processor_dict     )
        self.assertEqual(image_processor.size     ,	{"""shortest_edge""": 2_0}     )
        self.assertEqual(image_processor.crop_size     ,	{"""height""": 1_8, """width""": 1_8}     )

        UpperCamelCase_							       =       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 lowerCamelCase_				(      self     ):




        """simple docstring"""


        pass

 def lowerCamelCase_				(      self     ):




        """simple docstring"""


        UpperCamelCase_							       =       self.image_processing_class(**self.image_processor_dict     )
        # create random PIL images
        UpperCamelCase_							       =       self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase     )
        for image in image_inputs:
               self.assertIsInstance(__UpperCamelCase     ,	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(__UpperCamelCase     ,	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 lowerCamelCase_				(      self     ):




        """simple docstring"""


        UpperCamelCase_							       =       self.image_processing_class(**self.image_processor_dict     )
        # create random numpy tensors
        UpperCamelCase_							       =       self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase     ,	numpify=__UpperCamelCase     )
        for image in image_inputs:
               self.assertIsInstance(__UpperCamelCase     ,	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(__UpperCamelCase     ,	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 lowerCamelCase_				(      self     ):




        """simple docstring"""


        UpperCamelCase_							       =       self.image_processing_class(**self.image_processor_dict     )
        # create random PyTorch tensors
        UpperCamelCase_							       =       self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase     ,	torchify=__UpperCamelCase     )
        for image in image_inputs:
               self.assertIsInstance(__UpperCamelCase     ,	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(__UpperCamelCase     ,	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"""],
            )     ,	)



@require_torch
@require_vision
class 		lowercase_       (       __SCREAMING_SNAKE_CASE							,	unittest.TestCase				):
 A__						:				str         =    CLIPImageProcessor if is_vision_available() else None

 def lowerCamelCase_				(      self     ):




        """simple docstring"""


        UpperCamelCase_							       =       CLIPImageProcessingTester(self     ,	num_channels=4     )
        UpperCamelCase_							       =       3

 @property
 def lowerCamelCase_				(      self     ):




        """simple docstring"""


        return self.image_processor_tester.prepare_image_processor_dict()

 def lowerCamelCase_				(      self     ):




        """simple docstring"""


        UpperCamelCase_							       =       self.image_processing_class(**self.image_processor_dict     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""do_resize"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""size"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""do_center_crop"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""center_crop"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""do_normalize"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""image_mean"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""image_std"""     )     )
        self.assertTrue(hasattr(__UpperCamelCase     ,	"""do_convert_rgb"""     )     )

 def lowerCamelCase_				(      self     ):




        """simple docstring"""


        pass

 def lowerCamelCase_				(      self     ):




        """simple docstring"""


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

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

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





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


_A	  =					get_logger(__name__)


if TYPE_CHECKING:
      from .builder import DatasetBuilder




def 				lowerCamelCase__				(						a__  :  int	,      a__  :  Optional[Union[str, bool]] = None     )		->	Dict:
       UpperCamelCase_							       =       importlib.import_module(a__     )

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

       def wrap_auth(a__  :  int     ):
              @wraps(a__     )
              def wrapper(*a__  :  int	,      **a__  :  List[str]     ):
                     return function(*a__	,      use_auth_token=a__	,      **a__     )

              UpperCamelCase_							       =       """wrap_auth"""
              return wrapper

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







def 				lowerCamelCase__				(						a__  :  "DatasetBuilder"     )		->	Tuple:
       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":
                            UpperCamelCase_							       =       imports[1]
                            UpperCamelCase_							       =       """.""".join(builder.__module__.split("""."""     )[:-1] + [internal_import_name]     )
                            extend_module_for_streaming(a__	,      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

       UpperCamelCase_							       =       [
           cls.__module__
           for cls in type(a__     ).__mro__[1:]  # make sure it's not the same module we've already patched
           if issubclass(cls	,      a__     ) 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(a__	,      use_auth_token=builder.use_auth_token     )



